blob: f41cb728e99908a1bb8a8c645ebf3e97d983d354 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
hayeswangac718b62013-05-02 16:01:25 +00002/*
hayeswangc7de7de2014-01-15 10:42:16 +08003 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00004 */
5
hayeswangac718b62013-05-02 16:01:25 +00006#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +00009#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/crc32.h>
15#include <linux/if_vlan.h>
16#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080017#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080018#include <linux/ip.h>
19#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080020#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080021#include <uapi/linux/mdio.h>
22#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080023#include <linux/usb/cdc.h>
hayeswang5ee3c602016-01-07 17:12:17 +080024#include <linux/suspend.h>
Hayes Wang252df8b2019-08-13 11:42:06 +080025#include <linux/atomic.h>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050026#include <linux/acpi.h>
hayeswangac718b62013-05-02 16:01:25 +000027
hayeswangd0942472015-09-07 11:57:43 +080028/* Information for net-next */
hayeswang65b82d62017-06-15 14:44:03 +080029#define NETNEXT_VERSION "09"
hayeswangd0942472015-09-07 11:57:43 +080030
31/* Information for net */
Hayes Wangffa9fec2019-07-04 17:36:32 +080032#define NET_VERSION "10"
hayeswangd0942472015-09-07 11:57:43 +080033
34#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000035#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080036#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000037#define MODULENAME "r8152"
38
39#define R8152_PHY_ID 32
40
41#define PLA_IDR 0xc000
42#define PLA_RCR 0xc010
43#define PLA_RMS 0xc016
44#define PLA_RXFIFO_CTRL0 0xc0a0
45#define PLA_RXFIFO_CTRL1 0xc0a4
46#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080047#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000048#define PLA_FMC 0xc0b4
49#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080051#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080053#define PLA_BACKUP 0xd000
Kevin Lo59c0b472019-08-01 11:29:38 +080054#define PLA_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_TEREDO_TIMER 0xd2cc
56#define PLA_REALWOW_TIMER 0xd2e8
Hayes Wang13e04fbf2019-07-01 15:53:19 +080057#define PLA_SUSPEND_FLAG 0xd38a
58#define PLA_INDICATE_FALG 0xd38c
59#define PLA_EXTRA_STATUS 0xd398
hayeswang65b82d62017-06-15 14:44:03 +080060#define PLA_EFUSE_DATA 0xdd00
61#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000062#define PLA_LEDSEL 0xdd90
63#define PLA_LED_FEATURE 0xdd92
64#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080065#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000066#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
hayeswang43779f82014-01-02 11:25:10 +080070#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
hayeswangac718b62013-05-02 16:01:25 +000074#define PLA_TCR0 0xe610
75#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080076#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000077#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080078#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000079#define PLA_CR 0xe813
80#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080081#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
82#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000083#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
hayeswang4f1d4d52014-03-11 16:24:19 +080090#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000091#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
hayeswang43779f82014-01-02 11:25:10 +0800102#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000103
hayeswang65bab842015-02-12 16:20:46 +0800104#define USB_USB2PHY 0xb41e
105#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800106#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800107#define USB_CSR_DUMMY1 0xb464
108#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000109#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800110#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800111#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800112#define USB_BURST_SIZE 0xcfc0
hayeswang65b82d62017-06-15 14:44:03 +0800113#define USB_LPM_CONFIG 0xcfd8
hayeswangac718b62013-05-02 16:01:25 +0000114#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
hayeswang464ec102015-02-12 14:33:46 +0800119#define USB_RX_EARLY_TIMEOUT 0xd42c
120#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800121#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
122#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000123#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800124#define USB_UPT_RXDMA_OWN 0xd437
hayeswang43779f82014-01-02 11:25:10 +0800125#define USB_TOLERANCE 0xd490
126#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800127#define USB_BMU_RESET 0xd4b0
hayeswang65b82d62017-06-15 14:44:03 +0800128#define USB_U1U2_TIMER 0xd4da
hayeswangac718b62013-05-02 16:01:25 +0000129#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800130#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800131#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600132#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800133#define USB_AFE_CTRL2 0xd824
hayeswang65b82d62017-06-15 14:44:03 +0800134#define USB_UPS_CFG 0xd842
135#define USB_UPS_FLAGS 0xd848
hayeswang43779f82014-01-02 11:25:10 +0800136#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000137#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
hayeswang43779f82014-01-02 11:25:10 +0800146#define USB_BP_EN 0xfc38
hayeswang65b82d62017-06-15 14:44:03 +0800147#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
hayeswangac718b62013-05-02 16:01:25 +0000156
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
hayeswangac244d32014-01-02 11:22:40 +0800162#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000163#define OCP_EEE_AR 0xa41a
164#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800165#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800166#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800167#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
hayeswangdf35d282014-09-25 20:54:02 +0800172#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800173#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800174#define OCP_EEE_LPABLE 0xa5d2
hayeswang2dd49e02015-09-07 11:57:44 +0800175#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800176#define OCP_PHY_PATCH_STAT 0xb800
177#define OCP_PHY_PATCH_CMD 0xb820
178#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800179#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800180#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800181
182/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800183#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800184#define SRAM_LPF_CFG 0x8012
185#define SRAM_10M_AMP1 0x8080
186#define SRAM_10M_AMP2 0x8082
187#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000188
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
hayeswang43779f82014-01-02 11:25:10 +0800204#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000205
206/* PLA_RXFIFO_CTRL2 */
207#define RXFIFO_THR3_FULL 0x00000078
208#define RXFIFO_THR3_HIGH 0x00000048
209#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800210#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000211
212/* PLA_TXFIFO_CTRL */
213#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800214#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000215
hayeswang65bab842015-02-12 16:20:46 +0800216/* PLA_DMY_REG0 */
217#define ECM_ALDPS 0x0002
218
hayeswangac718b62013-05-02 16:01:25 +0000219/* PLA_FMC */
220#define FMC_FCR_MCU_EN 0x0001
221
222/* PLA_EEEP_CR */
223#define EEEP_CR_EEEP_TX 0x0002
224
hayeswang43779f82014-01-02 11:25:10 +0800225/* PLA_WDT6_CTRL */
226#define WDT6_SET_MODE 0x0010
227
hayeswangac718b62013-05-02 16:01:25 +0000228/* PLA_TCR0 */
229#define TCR0_TX_EMPTY 0x0800
230#define TCR0_AUTO_FIFO 0x0080
231
232/* PLA_TCR1 */
233#define VERSION_MASK 0x7cf0
234
hayeswang69b4b7a2014-07-10 10:58:54 +0800235/* PLA_MTPS */
236#define MTPS_JUMBO (12 * 1024 / 64)
237#define MTPS_DEFAULT (6 * 1024 / 64)
238
hayeswang4f1d4d52014-03-11 16:24:19 +0800239/* PLA_RSTTALLY */
240#define TALLY_RESET 0x0001
241
hayeswangac718b62013-05-02 16:01:25 +0000242/* 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
hayeswang43779f82014-01-02 11:25:10 +0800272/* 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
Kevin Lo59c0b472019-08-01 11:29:38 +0800278/* PLA_BDC_CR */
hayeswangac718b62013-05-02 16:01:25 +0000279#define ALDPS_PROXY_MODE 0x0001
280
hayeswang65b82d62017-06-15 14:44:03 +0800281/* PLA_EFUSE_CMD */
282#define EFUSE_READ_CMD BIT(15)
283#define EFUSE_DATA_BIT16 BIT(7)
284
hayeswang21ff2e82014-02-18 21:49:06 +0800285/* PLA_CONFIG34 */
286#define LINK_ON_WAKE_EN 0x0010
287#define LINK_OFF_WAKE_EN 0x0008
288
hayeswangac718b62013-05-02 16:01:25 +0000289/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800290#define BWF_EN 0x0040
291#define MWF_EN 0x0020
292#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000293#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
hayeswang43779f82014-01-02 11:25:10 +0800306#define ALDPS_SPDWN_RATIO 0x0f87
307
308/* PLA_MAC_PWR_CTRL2 */
309#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800310#define MAC_CLK_SPDWN_EN BIT(15)
hayeswang43779f82014-01-02 11:25:10 +0800311
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
hayeswangac718b62013-05-02 16:01:25 +0000326
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
hayeswang43779f82014-01-02 11:25:10 +0800340/* PLA_BOOT_CTRL */
341#define AUTOLOAD_DONE 0x0002
342
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800343/* PLA_SUSPEND_FLAG */
344#define LINK_CHG_EVENT BIT(0)
345
346/* PLA_INDICATE_FALG */
347#define UPCOMING_RUNTIME_D3 BIT(0)
348
349/* PLA_EXTRA_STATUS */
350#define LINK_CHANGE_FLAG BIT(8)
351
hayeswang65bab842015-02-12 16:20:46 +0800352/* USB_USB2PHY */
353#define USB2PHY_SUSPEND 0x0001
354#define USB2PHY_L1 0x0002
355
356/* USB_SSPHYLINK2 */
357#define pwd_dn_scale_mask 0x3ffe
358#define pwd_dn_scale(x) ((x) << 1)
359
360/* USB_CSR_DUMMY1 */
361#define DYNAMIC_BURST 0x0001
362
363/* USB_CSR_DUMMY2 */
364#define EP4_FULL_FC 0x0001
365
hayeswangac718b62013-05-02 16:01:25 +0000366/* USB_DEV_STAT */
367#define STAT_SPEED_MASK 0x0006
368#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800369#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000370
hayeswang65b82d62017-06-15 14:44:03 +0800371/* USB_LPM_CONFIG */
372#define LPM_U1U2_EN BIT(0)
373
hayeswangac718b62013-05-02 16:01:25 +0000374/* USB_TX_AGG */
375#define TX_AGG_MAX_THRESHOLD 0x03
376
377/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800378#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800379#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800380#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800381#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000382
383/* USB_TX_DMA */
384#define TEST_MODE_DISABLE 0x00000001
385#define TX_SIZE_ADJUST1 0x00000100
386
hayeswang93fe9b12016-06-16 10:55:18 +0800387/* USB_BMU_RESET */
388#define BMU_RESET_EP_IN 0x01
389#define BMU_RESET_EP_OUT 0x02
390
hayeswang65b82d62017-06-15 14:44:03 +0800391/* USB_UPT_RXDMA_OWN */
392#define OWN_UPDATE BIT(0)
393#define OWN_CLEAR BIT(1)
394
hayeswangac718b62013-05-02 16:01:25 +0000395/* USB_UPS_CTRL */
396#define POWER_CUT 0x0100
397
398/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800399#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000400
401/* USB_USB_CTRL */
402#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800403#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000404
hayeswang43779f82014-01-02 11:25:10 +0800405/* USB_U2P3_CTRL */
406#define U2P3_ENABLE 0x0001
407
408/* USB_POWER_CUT */
409#define PWR_EN 0x0001
410#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800411#define UPS_EN BIT(4)
412#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800413
414/* USB_MISC_0 */
415#define PCUT_STATUS 0x0001
416
hayeswang464ec102015-02-12 14:33:46 +0800417/* USB_RX_EARLY_TIMEOUT */
418#define COALESCE_SUPER 85000U
419#define COALESCE_HIGH 250000U
420#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800421
422/* USB_WDT11_CTRL */
423#define TIMER11_EN 0x0001
424
425/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800426/* bit 4 ~ 5: fifo empty boundary */
427#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
428/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800429#define LPM_TIMER_MASK 0x0c
430#define LPM_TIMER_500MS 0x04 /* 500 ms */
431#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800432#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800433
434/* USB_AFE_CTRL2 */
435#define SEN_VAL_MASK 0xf800
436#define SEN_VAL_NORMAL 0xa000
437#define SEL_RXIDLE 0x0100
438
hayeswang65b82d62017-06-15 14:44:03 +0800439/* USB_UPS_CFG */
440#define SAW_CNT_1MS_MASK 0x0fff
441
442/* USB_UPS_FLAGS */
443#define UPS_FLAGS_R_TUNE BIT(0)
444#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
445#define UPS_FLAGS_250M_CKDIV BIT(2)
446#define UPS_FLAGS_EN_ALDPS BIT(3)
447#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
448#define UPS_FLAGS_SPEED_MASK (0xf << 16)
449#define ups_flags_speed(x) ((x) << 16)
450#define UPS_FLAGS_EN_EEE BIT(20)
451#define UPS_FLAGS_EN_500M_EEE BIT(21)
452#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
453#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
454#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
455#define UPS_FLAGS_EN_GREEN BIT(26)
456#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
457
458enum spd_duplex {
459 NWAY_10M_HALF = 1,
460 NWAY_10M_FULL,
461 NWAY_100M_HALF,
462 NWAY_100M_FULL,
463 NWAY_1000M_FULL,
464 FORCE_10M_HALF,
465 FORCE_10M_FULL,
466 FORCE_100M_HALF,
467 FORCE_100M_FULL,
468};
469
hayeswangac718b62013-05-02 16:01:25 +0000470/* OCP_ALDPS_CONFIG */
471#define ENPWRSAVE 0x8000
472#define ENPDNPS 0x0200
473#define LINKENA 0x0100
474#define DIS_SDSAVE 0x0010
475
hayeswang43779f82014-01-02 11:25:10 +0800476/* OCP_PHY_STATUS */
477#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800478#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800479#define PHY_STAT_LAN_ON 3
480#define PHY_STAT_PWRDN 5
481
hayeswang65b82d62017-06-15 14:44:03 +0800482/* OCP_NCTL_CFG */
483#define PGA_RETURN_EN BIT(1)
484
hayeswang43779f82014-01-02 11:25:10 +0800485/* OCP_POWER_CFG */
486#define EEE_CLKDIV_EN 0x8000
487#define EN_ALDPS 0x0004
488#define EN_10M_PLLOFF 0x0001
489
hayeswangac718b62013-05-02 16:01:25 +0000490/* OCP_EEE_CONFIG1 */
491#define RG_TXLPI_MSK_HFDUP 0x8000
492#define RG_MATCLR_EN 0x4000
493#define EEE_10_CAP 0x2000
494#define EEE_NWAY_EN 0x1000
495#define TX_QUIET_EN 0x0200
496#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800497#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800498#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000499#define RG_RXLPI_MSK_HFDUP 0x0008
500#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
501
502/* OCP_EEE_CONFIG2 */
503#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
504#define RG_DACQUIET_EN 0x0400
505#define RG_LDVQUIET_EN 0x0200
506#define RG_CKRSEL 0x0020
507#define RG_EEEPRG_EN 0x0010
508
509/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800510#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800511#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000512#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
513#define MSK_PH 0x0006 /* bit 0 ~ 3 */
514
515/* OCP_EEE_AR */
516/* bit[15:14] function */
517#define FUN_ADDR 0x0000
518#define FUN_DATA 0x4000
519/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000520
hayeswang43779f82014-01-02 11:25:10 +0800521/* OCP_EEE_CFG */
522#define CTAP_SHORT_EN 0x0040
523#define EEE10_EN 0x0010
524
525/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800526#define EN_EEE_CMODE BIT(14)
527#define EN_EEE_1000 BIT(13)
528#define EN_EEE_100 BIT(12)
529#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800530#define EN_10M_BGOFF 0x0080
531
hayeswang2dd49e02015-09-07 11:57:44 +0800532/* OCP_PHY_STATE */
533#define TXDIS_STATE 0x01
534#define ABD_STATE 0x02
535
hayeswang65b82d62017-06-15 14:44:03 +0800536/* OCP_PHY_PATCH_STAT */
537#define PATCH_READY BIT(6)
538
539/* OCP_PHY_PATCH_CMD */
540#define PATCH_REQUEST BIT(4)
541
hayeswang43779f82014-01-02 11:25:10 +0800542/* OCP_ADC_CFG */
543#define CKADSEL_L 0x0100
544#define ADC_EN 0x0080
545#define EN_EMI_L 0x0040
546
hayeswang65b82d62017-06-15 14:44:03 +0800547/* OCP_SYSCLK_CFG */
548#define clk_div_expo(x) (min(x, 5) << 8)
549
550/* SRAM_GREEN_CFG */
551#define GREEN_ETH_EN BIT(15)
552#define R_TUNE_EN BIT(11)
553
hayeswang43779f82014-01-02 11:25:10 +0800554/* SRAM_LPF_CFG */
555#define LPF_AUTO_TUNE 0x8000
556
557/* SRAM_10M_AMP1 */
558#define GDAC_IB_UPALL 0x0008
559
560/* SRAM_10M_AMP2 */
561#define AMP_DN 0x0200
562
563/* SRAM_IMPEDANCE */
564#define RX_DRIVING_MASK 0x6000
565
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500566/* MAC PASSTHRU */
567#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600568#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800569#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500570#define EFUSE 0xcfdb
571#define PASS_THRU_MASK 0x1
572
hayeswangac718b62013-05-02 16:01:25 +0000573enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800574 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000575 _100bps = 0x08,
576 _10bps = 0x04,
577 LINK_STATUS = 0x02,
578 FULL_DUP = 0x01,
579};
580
hayeswang1764bcd2014-08-28 10:24:18 +0800581#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800582#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800583#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800584#define TX_ALIGN 4
585#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800586
587#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800588
hayeswangac718b62013-05-02 16:01:25 +0000589#define RTL8152_REQT_READ 0xc0
590#define RTL8152_REQT_WRITE 0x40
591#define RTL8152_REQ_GET_REGS 0x05
592#define RTL8152_REQ_SET_REGS 0x05
593
594#define BYTE_EN_DWORD 0xff
595#define BYTE_EN_WORD 0x33
596#define BYTE_EN_BYTE 0x11
597#define BYTE_EN_SIX_BYTES 0x3f
598#define BYTE_EN_START_MASK 0x0f
599#define BYTE_EN_END_MASK 0xf0
600
hayeswang69b4b7a2014-07-10 10:58:54 +0800601#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800602#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
603 ETH_FCS_LEN)
604#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800605#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800606#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800607#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800608#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800609 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000610
611/* rtl8152 flags */
612enum rtl8152_flags {
613 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000614 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800615 WORK_ENABLE,
616 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800617 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800618 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800619 SCHEDULE_NAPI,
hayeswang65b82d62017-06-15 14:44:03 +0800620 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800621 DELL_TB_RX_AGG_BUG,
hayeswangac718b62013-05-02 16:01:25 +0000622};
623
624/* Define these values to match your device */
625#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200626#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800627#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200628#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700629#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700630#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800631#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000632
633#define MCU_TYPE_PLA 0x0100
634#define MCU_TYPE_USB 0x0000
635
hayeswang4f1d4d52014-03-11 16:24:19 +0800636struct tally_counter {
637 __le64 tx_packets;
638 __le64 rx_packets;
639 __le64 tx_errors;
640 __le32 rx_errors;
641 __le16 rx_missed;
642 __le16 align_errors;
643 __le32 tx_one_collision;
644 __le32 tx_multi_collision;
645 __le64 rx_unicast;
646 __le64 rx_broadcast;
647 __le32 rx_multicast;
648 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800649 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800650};
651
hayeswangac718b62013-05-02 16:01:25 +0000652struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800653 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000654#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800655
hayeswang500b6d72013-11-20 17:30:57 +0800656 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800657#define RD_UDP_CS BIT(23)
658#define RD_TCP_CS BIT(22)
659#define RD_IPV6_CS BIT(20)
660#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800661
hayeswang500b6d72013-11-20 17:30:57 +0800662 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800663#define IPF BIT(23) /* IP checksum fail */
664#define UDPF BIT(22) /* UDP checksum fail */
665#define TCPF BIT(21) /* TCP checksum fail */
666#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800667
hayeswang500b6d72013-11-20 17:30:57 +0800668 __le32 opts4;
669 __le32 opts5;
670 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000671};
672
673struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800674 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800675#define TX_FS BIT(31) /* First segment of a packet */
676#define TX_LS BIT(30) /* Final segment of a packet */
677#define GTSENDV4 BIT(28)
678#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800679#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800680#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800681#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800682
hayeswang500b6d72013-11-20 17:30:57 +0800683 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800684#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
685#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
686#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
687#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800688#define MSS_SHIFT 17
689#define MSS_MAX 0x7ffU
690#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800691#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800692#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000693};
694
hayeswangdff4e8a2013-08-16 16:09:33 +0800695struct r8152;
696
hayeswangebc2ec482013-08-14 20:54:38 +0800697struct rx_agg {
Hayes Wang252df8b2019-08-13 11:42:06 +0800698 struct list_head list, info_list;
hayeswangebc2ec482013-08-14 20:54:38 +0800699 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800700 struct r8152 *context;
Hayes Wangd55d7082019-08-13 11:42:07 +0800701 struct page *page;
hayeswangebc2ec482013-08-14 20:54:38 +0800702 void *buffer;
hayeswangebc2ec482013-08-14 20:54:38 +0800703};
704
705struct tx_agg {
706 struct list_head list;
707 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800708 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800709 void *buffer;
710 void *head;
711 u32 skb_num;
712 u32 skb_len;
713};
714
hayeswangac718b62013-05-02 16:01:25 +0000715struct r8152 {
716 unsigned long flags;
717 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800718 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800719 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000720 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800721 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800722 struct tx_agg tx_info[RTL8152_MAX_TX];
Hayes Wang252df8b2019-08-13 11:42:06 +0800723 struct list_head rx_info;
hayeswangebc2ec482013-08-14 20:54:38 +0800724 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800725 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800726 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800727 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000728 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800729 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800730#ifdef CONFIG_PM_SLEEP
731 struct notifier_block pm_notifier;
732#endif
hayeswangc81229c2014-01-02 11:22:42 +0800733
734 struct rtl_ops {
735 void (*init)(struct r8152 *);
736 int (*enable)(struct r8152 *);
737 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800738 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800739 void (*down)(struct r8152 *);
740 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800741 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
742 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800743 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800744 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800745 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800746 } rtl_ops;
747
Hayes Wang252df8b2019-08-13 11:42:06 +0800748 atomic_t rx_count;
749
hayeswang40a82912013-08-14 20:54:40 +0800750 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800751 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000752 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800753 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800754 u32 coalesce;
Hayes Wangec5791c2019-08-13 11:42:05 +0800755 u32 rx_buf_sz;
hayeswangac718b62013-05-02 16:01:25 +0000756 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800757 u16 speed;
hayeswang40a82912013-08-14 20:54:40 +0800758 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000759 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800760 u8 duplex;
761 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000762};
763
764enum rtl_version {
765 RTL_VER_UNKNOWN = 0,
766 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800767 RTL_VER_02,
768 RTL_VER_03,
769 RTL_VER_04,
770 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800771 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800772 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800773 RTL_VER_08,
774 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800775 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000776};
777
hayeswang60c89072014-03-07 11:04:39 +0800778enum tx_csum_stat {
779 TX_CSUM_SUCCESS = 0,
780 TX_CSUM_TSO,
781 TX_CSUM_NONE
782};
783
hayeswangac718b62013-05-02 16:01:25 +0000784/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
785 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
786 */
787static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800788static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000789
hayeswang52aec122014-09-02 10:27:52 +0800790#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800791 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800792
hayeswangac718b62013-05-02 16:01:25 +0000793static
794int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
795{
hayeswang31787f52013-07-31 17:21:25 +0800796 int ret;
797 void *tmp;
798
799 tmp = kmalloc(size, GFP_KERNEL);
800 if (!tmp)
801 return -ENOMEM;
802
803 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800804 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
805 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800806
807 memcpy(data, tmp, size);
808 kfree(tmp);
809
810 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000811}
812
813static
814int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
815{
hayeswang31787f52013-07-31 17:21:25 +0800816 int ret;
817 void *tmp;
818
Benoit Tainec4438f02014-05-26 17:21:23 +0200819 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800820 if (!tmp)
821 return -ENOMEM;
822
hayeswang31787f52013-07-31 17:21:25 +0800823 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800824 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
825 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800826
827 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800828
hayeswang31787f52013-07-31 17:21:25 +0800829 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000830}
831
Hayes Wangffa9fec2019-07-04 17:36:32 +0800832static void rtl_set_unplug(struct r8152 *tp)
833{
834 if (tp->udev->state == USB_STATE_NOTATTACHED) {
835 set_bit(RTL8152_UNPLUG, &tp->flags);
836 smp_mb__after_atomic();
837 }
838}
839
hayeswangac718b62013-05-02 16:01:25 +0000840static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800841 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000842{
hayeswang45f4a192014-01-06 17:08:41 +0800843 u16 limit = 64;
844 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000845
846 if (test_bit(RTL8152_UNPLUG, &tp->flags))
847 return -ENODEV;
848
849 /* both size and indix must be 4 bytes align */
850 if ((size & 3) || !size || (index & 3) || !data)
851 return -EPERM;
852
853 if ((u32)index + (u32)size > 0xffff)
854 return -EPERM;
855
856 while (size) {
857 if (size > limit) {
858 ret = get_registers(tp, index, type, limit, data);
859 if (ret < 0)
860 break;
861
862 index += limit;
863 data += limit;
864 size -= limit;
865 } else {
866 ret = get_registers(tp, index, type, size, data);
867 if (ret < 0)
868 break;
869
870 index += size;
871 data += size;
872 size = 0;
873 break;
874 }
875 }
876
hayeswang67610492014-10-30 11:46:40 +0800877 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800878 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800879
hayeswangac718b62013-05-02 16:01:25 +0000880 return ret;
881}
882
883static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800884 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000885{
hayeswang45f4a192014-01-06 17:08:41 +0800886 int ret;
887 u16 byteen_start, byteen_end, byen;
888 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000889
890 if (test_bit(RTL8152_UNPLUG, &tp->flags))
891 return -ENODEV;
892
893 /* both size and indix must be 4 bytes align */
894 if ((size & 3) || !size || (index & 3) || !data)
895 return -EPERM;
896
897 if ((u32)index + (u32)size > 0xffff)
898 return -EPERM;
899
900 byteen_start = byteen & BYTE_EN_START_MASK;
901 byteen_end = byteen & BYTE_EN_END_MASK;
902
903 byen = byteen_start | (byteen_start << 4);
904 ret = set_registers(tp, index, type | byen, 4, data);
905 if (ret < 0)
906 goto error1;
907
908 index += 4;
909 data += 4;
910 size -= 4;
911
912 if (size) {
913 size -= 4;
914
915 while (size) {
916 if (size > limit) {
917 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800918 type | BYTE_EN_DWORD,
919 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000920 if (ret < 0)
921 goto error1;
922
923 index += limit;
924 data += limit;
925 size -= limit;
926 } else {
927 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800928 type | BYTE_EN_DWORD,
929 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000930 if (ret < 0)
931 goto error1;
932
933 index += size;
934 data += size;
935 size = 0;
936 break;
937 }
938 }
939
940 byen = byteen_end | (byteen_end >> 4);
941 ret = set_registers(tp, index, type | byen, 4, data);
942 if (ret < 0)
943 goto error1;
944 }
945
946error1:
hayeswang67610492014-10-30 11:46:40 +0800947 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800948 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800949
hayeswangac718b62013-05-02 16:01:25 +0000950 return ret;
951}
952
953static inline
954int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
955{
956 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
957}
958
959static inline
960int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
961{
962 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
963}
964
965static inline
hayeswangac718b62013-05-02 16:01:25 +0000966int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
967{
968 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
969}
970
971static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
972{
hayeswangc8826de2013-07-31 17:21:26 +0800973 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000974
hayeswangc8826de2013-07-31 17:21:26 +0800975 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000976
977 return __le32_to_cpu(data);
978}
979
980static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
981{
hayeswangc8826de2013-07-31 17:21:26 +0800982 __le32 tmp = __cpu_to_le32(data);
983
984 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000985}
986
987static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
988{
989 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800990 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +0800991 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +0000992 u8 shift = index & 2;
993
994 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +0800995 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +0000996
hayeswangd8fbd272017-06-15 14:44:04 +0800997 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +0000998
hayeswangc8826de2013-07-31 17:21:26 +0800999 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001000 data >>= (shift * 8);
1001 data &= 0xffff;
1002
1003 return (u16)data;
1004}
1005
1006static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1007{
hayeswangc8826de2013-07-31 17:21:26 +08001008 u32 mask = 0xffff;
1009 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001010 u16 byen = BYTE_EN_WORD;
1011 u8 shift = index & 2;
1012
1013 data &= mask;
1014
1015 if (index & 2) {
1016 byen <<= shift;
1017 mask <<= (shift * 8);
1018 data <<= (shift * 8);
1019 index &= ~3;
1020 }
1021
hayeswangc8826de2013-07-31 17:21:26 +08001022 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001023
hayeswangc8826de2013-07-31 17:21:26 +08001024 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001025}
1026
1027static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1028{
1029 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001030 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001031 u8 shift = index & 3;
1032
1033 index &= ~3;
1034
hayeswangc8826de2013-07-31 17:21:26 +08001035 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001036
hayeswangc8826de2013-07-31 17:21:26 +08001037 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001038 data >>= (shift * 8);
1039 data &= 0xff;
1040
1041 return (u8)data;
1042}
1043
1044static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1045{
hayeswangc8826de2013-07-31 17:21:26 +08001046 u32 mask = 0xff;
1047 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001048 u16 byen = BYTE_EN_BYTE;
1049 u8 shift = index & 3;
1050
1051 data &= mask;
1052
1053 if (index & 3) {
1054 byen <<= shift;
1055 mask <<= (shift * 8);
1056 data <<= (shift * 8);
1057 index &= ~3;
1058 }
1059
hayeswangc8826de2013-07-31 17:21:26 +08001060 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001061
hayeswangc8826de2013-07-31 17:21:26 +08001062 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001063}
1064
hayeswangac244d32014-01-02 11:22:40 +08001065static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1066{
1067 u16 ocp_base, ocp_index;
1068
1069 ocp_base = addr & 0xf000;
1070 if (ocp_base != tp->ocp_base) {
1071 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1072 tp->ocp_base = ocp_base;
1073 }
1074
1075 ocp_index = (addr & 0x0fff) | 0xb000;
1076 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1077}
1078
hayeswange3fe0b12014-01-02 11:22:39 +08001079static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1080{
1081 u16 ocp_base, ocp_index;
1082
1083 ocp_base = addr & 0xf000;
1084 if (ocp_base != tp->ocp_base) {
1085 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1086 tp->ocp_base = ocp_base;
1087 }
1088
1089 ocp_index = (addr & 0x0fff) | 0xb000;
1090 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1091}
1092
hayeswangac244d32014-01-02 11:22:40 +08001093static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001094{
hayeswangac244d32014-01-02 11:22:40 +08001095 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001096}
1097
hayeswangac244d32014-01-02 11:22:40 +08001098static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001099{
hayeswangac244d32014-01-02 11:22:40 +08001100 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001101}
1102
hayeswang43779f82014-01-02 11:25:10 +08001103static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1104{
1105 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1106 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1107}
1108
hayeswang65b82d62017-06-15 14:44:03 +08001109static u16 sram_read(struct r8152 *tp, u16 addr)
1110{
1111 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1112 return ocp_reg_read(tp, OCP_SRAM_DATA);
1113}
1114
hayeswangac718b62013-05-02 16:01:25 +00001115static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1116{
1117 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001118 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001119
hayeswang68714382014-04-11 17:54:31 +08001120 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1121 return -ENODEV;
1122
hayeswangac718b62013-05-02 16:01:25 +00001123 if (phy_id != R8152_PHY_ID)
1124 return -EINVAL;
1125
hayeswang9a4be1b2014-02-18 21:49:07 +08001126 ret = r8152_mdio_read(tp, reg);
1127
hayeswang9a4be1b2014-02-18 21:49:07 +08001128 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001129}
1130
1131static
1132void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1133{
1134 struct r8152 *tp = netdev_priv(netdev);
1135
hayeswang68714382014-04-11 17:54:31 +08001136 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1137 return;
1138
hayeswangac718b62013-05-02 16:01:25 +00001139 if (phy_id != R8152_PHY_ID)
1140 return;
1141
1142 r8152_mdio_write(tp, reg, val);
1143}
1144
hayeswangb209af92014-08-25 15:53:00 +08001145static int
1146r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001147
hayeswang8ba789a2014-09-04 16:15:41 +08001148static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1149{
1150 struct r8152 *tp = netdev_priv(netdev);
1151 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001152 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001153
1154 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001155 goto out1;
1156
1157 ret = usb_autopm_get_interface(tp->intf);
1158 if (ret < 0)
1159 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001160
hayeswangb5403272014-10-09 18:00:26 +08001161 mutex_lock(&tp->control);
1162
hayeswang8ba789a2014-09-04 16:15:41 +08001163 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1164
1165 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1166 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1167 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1168
hayeswangb5403272014-10-09 18:00:26 +08001169 mutex_unlock(&tp->control);
1170
hayeswangea6a7112014-10-02 17:03:12 +08001171 usb_autopm_put_interface(tp->intf);
1172out1:
1173 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001174}
1175
Mario Limonciello9c273692018-12-11 08:16:14 -06001176/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001177 * host system provided MAC address.
1178 * Examples of this are Dell TB15 and Dell WD15 docks
1179 */
1180static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1181{
1182 acpi_status status;
1183 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1184 union acpi_object *obj;
1185 int ret = -EINVAL;
1186 u32 ocp_data;
1187 unsigned char buf[6];
1188
1189 /* test for -AD variant of RTL8153 */
1190 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001191 if ((ocp_data & AD_MASK) == 0x1000) {
1192 /* test for MAC address pass-through bit */
1193 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1194 if ((ocp_data & PASS_THRU_MASK) != 1) {
1195 netif_dbg(tp, probe, tp->netdev,
1196 "No efuse for RTL8153-AD MAC pass through\n");
1197 return -ENODEV;
1198 }
1199 } else {
David Chen8e29d232019-02-16 17:16:42 +08001200 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001201 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001202 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001203 netif_dbg(tp, probe, tp->netdev,
1204 "Invalid variant for MAC pass through\n");
1205 return -ENODEV;
1206 }
1207 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001208
1209 /* returns _AUXMAC_#AABBCCDDEEFF# */
1210 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1211 obj = (union acpi_object *)buffer.pointer;
1212 if (!ACPI_SUCCESS(status))
1213 return -ENODEV;
1214 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1215 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001216 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001217 obj->type, obj->string.length);
1218 goto amacout;
1219 }
1220 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1221 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1222 netif_warn(tp, probe, tp->netdev,
1223 "Invalid header when reading pass-thru MAC addr\n");
1224 goto amacout;
1225 }
1226 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1227 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1228 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001229 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1230 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001231 ret = -EINVAL;
1232 goto amacout;
1233 }
1234 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001235 netif_info(tp, probe, tp->netdev,
1236 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1237
1238amacout:
1239 kfree(obj);
1240 return ret;
1241}
1242
Mario Limonciello25766272019-04-04 13:46:53 -05001243static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1244{
1245 struct net_device *dev = tp->netdev;
1246 int ret;
1247
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001248 sa->sa_family = dev->type;
1249
Mario Limonciello25766272019-04-04 13:46:53 -05001250 if (tp->version == RTL_VER_01) {
1251 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1252 } else {
1253 /* if device doesn't support MAC pass through this will
1254 * be expected to be non-zero
1255 */
1256 ret = vendor_mac_passthru_addr_read(tp, sa);
1257 if (ret < 0)
1258 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1259 }
1260
1261 if (ret < 0) {
1262 netif_err(tp, probe, dev, "Get ether addr fail\n");
1263 } else if (!is_valid_ether_addr(sa->sa_data)) {
1264 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1265 sa->sa_data);
1266 eth_hw_addr_random(dev);
1267 ether_addr_copy(sa->sa_data, dev->dev_addr);
1268 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1269 sa->sa_data);
1270 return 0;
1271 }
1272
1273 return ret;
1274}
1275
hayeswang179bb6d2014-09-04 16:15:42 +08001276static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001277{
1278 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001279 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001280 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001281
Mario Limonciello25766272019-04-04 13:46:53 -05001282 ret = determine_ethernet_addr(tp, &sa);
1283 if (ret < 0)
1284 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001285
Mario Limonciello25766272019-04-04 13:46:53 -05001286 if (tp->version == RTL_VER_01)
1287 ether_addr_copy(dev->dev_addr, sa.sa_data);
1288 else
hayeswang179bb6d2014-09-04 16:15:42 +08001289 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001290
1291 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001292}
1293
hayeswangac718b62013-05-02 16:01:25 +00001294static void read_bulk_callback(struct urb *urb)
1295{
hayeswangac718b62013-05-02 16:01:25 +00001296 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001297 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001298 struct rx_agg *agg;
1299 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001300 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001301
hayeswangebc2ec482013-08-14 20:54:38 +08001302 agg = urb->context;
1303 if (!agg)
1304 return;
1305
1306 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001307 if (!tp)
1308 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001309
hayeswangac718b62013-05-02 16:01:25 +00001310 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1311 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001312
1313 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001314 return;
1315
hayeswangebc2ec482013-08-14 20:54:38 +08001316 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001317
1318 /* When link down, the driver would cancel all bulks. */
1319 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001320 if (!netif_carrier_ok(netdev))
1321 return;
1322
hayeswang9a4be1b2014-02-18 21:49:07 +08001323 usb_mark_last_busy(tp->udev);
1324
hayeswangac718b62013-05-02 16:01:25 +00001325 switch (status) {
1326 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001327 if (urb->actual_length < ETH_ZLEN)
1328 break;
1329
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001330 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001331 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001332 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001333 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001334 return;
hayeswangac718b62013-05-02 16:01:25 +00001335 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001336 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001337 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001338 return;
hayeswangac718b62013-05-02 16:01:25 +00001339 case -ENOENT:
1340 return; /* the urb is in unlink state */
1341 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001342 if (net_ratelimit())
1343 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001344 break;
hayeswangac718b62013-05-02 16:01:25 +00001345 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001346 if (net_ratelimit())
1347 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001348 break;
hayeswangac718b62013-05-02 16:01:25 +00001349 }
1350
hayeswanga0fccd42014-11-20 10:29:05 +08001351 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001352}
1353
1354static void write_bulk_callback(struct urb *urb)
1355{
hayeswangebc2ec482013-08-14 20:54:38 +08001356 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001357 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001358 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001359 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001360 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001361 int status = urb->status;
1362
hayeswangebc2ec482013-08-14 20:54:38 +08001363 agg = urb->context;
1364 if (!agg)
1365 return;
1366
1367 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001368 if (!tp)
1369 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001370
hayeswangd104eaf2014-03-06 15:07:17 +08001371 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001372 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001373 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001374 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001375 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001376 stats->tx_errors += agg->skb_num;
1377 } else {
1378 stats->tx_packets += agg->skb_num;
1379 stats->tx_bytes += agg->skb_len;
1380 }
1381
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001382 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001383 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001384 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001385
hayeswang9a4be1b2014-02-18 21:49:07 +08001386 usb_autopm_put_interface_async(tp->intf);
1387
hayeswangd104eaf2014-03-06 15:07:17 +08001388 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001389 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001390
1391 if (!test_bit(WORK_ENABLE, &tp->flags))
1392 return;
1393
1394 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1395 return;
1396
1397 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001398 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001399}
1400
hayeswang40a82912013-08-14 20:54:40 +08001401static void intr_callback(struct urb *urb)
1402{
1403 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001404 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001405 int status = urb->status;
1406 int res;
1407
1408 tp = urb->context;
1409 if (!tp)
1410 return;
1411
1412 if (!test_bit(WORK_ENABLE, &tp->flags))
1413 return;
1414
1415 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1416 return;
1417
1418 switch (status) {
1419 case 0: /* success */
1420 break;
1421 case -ECONNRESET: /* unlink */
1422 case -ESHUTDOWN:
1423 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001424 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001425 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001426 case -EPROTO:
1427 netif_info(tp, intr, tp->netdev,
1428 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001429 return;
1430 case -EOVERFLOW:
1431 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1432 goto resubmit;
1433 /* -EPIPE: should clear the halt */
1434 default:
1435 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1436 goto resubmit;
1437 }
1438
1439 d = urb->transfer_buffer;
1440 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001441 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001442 set_bit(RTL8152_LINK_CHG, &tp->flags);
1443 schedule_delayed_work(&tp->schedule, 0);
1444 }
1445 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001446 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001447 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001448 set_bit(RTL8152_LINK_CHG, &tp->flags);
1449 schedule_delayed_work(&tp->schedule, 0);
1450 }
1451 }
1452
1453resubmit:
1454 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001455 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001456 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001457 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001458 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001459 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001460 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001461 }
hayeswang40a82912013-08-14 20:54:40 +08001462}
1463
hayeswangebc2ec482013-08-14 20:54:38 +08001464static inline void *rx_agg_align(void *data)
1465{
hayeswang8e1f51b2014-01-02 11:22:41 +08001466 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001467}
1468
1469static inline void *tx_agg_align(void *data)
1470{
hayeswang8e1f51b2014-01-02 11:22:41 +08001471 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001472}
1473
Hayes Wang252df8b2019-08-13 11:42:06 +08001474static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1475{
1476 list_del(&agg->info_list);
1477
1478 usb_free_urb(agg->urb);
Hayes Wangd55d7082019-08-13 11:42:07 +08001479 __free_pages(agg->page, get_order(tp->rx_buf_sz));
Hayes Wang252df8b2019-08-13 11:42:06 +08001480 kfree(agg);
1481
1482 atomic_dec(&tp->rx_count);
1483}
1484
1485static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1486{
1487 struct net_device *netdev = tp->netdev;
1488 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
Hayes Wangd55d7082019-08-13 11:42:07 +08001489 unsigned int order = get_order(tp->rx_buf_sz);
Hayes Wang252df8b2019-08-13 11:42:06 +08001490 struct rx_agg *rx_agg;
1491 unsigned long flags;
Hayes Wang252df8b2019-08-13 11:42:06 +08001492
1493 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1494 if (!rx_agg)
1495 return NULL;
1496
Hayes Wangd55d7082019-08-13 11:42:07 +08001497 rx_agg->page = alloc_pages(mflags, order);
1498 if (!rx_agg->page)
Hayes Wang252df8b2019-08-13 11:42:06 +08001499 goto free_rx;
1500
Hayes Wangd55d7082019-08-13 11:42:07 +08001501 rx_agg->buffer = page_address(rx_agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001502
1503 rx_agg->urb = usb_alloc_urb(0, mflags);
1504 if (!rx_agg->urb)
1505 goto free_buf;
1506
1507 rx_agg->context = tp;
1508
1509 INIT_LIST_HEAD(&rx_agg->list);
1510 INIT_LIST_HEAD(&rx_agg->info_list);
1511 spin_lock_irqsave(&tp->rx_lock, flags);
1512 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1513 spin_unlock_irqrestore(&tp->rx_lock, flags);
1514
1515 atomic_inc(&tp->rx_count);
1516
1517 return rx_agg;
1518
1519free_buf:
Hayes Wangd55d7082019-08-13 11:42:07 +08001520 __free_pages(rx_agg->page, order);
Hayes Wang252df8b2019-08-13 11:42:06 +08001521free_rx:
1522 kfree(rx_agg);
1523 return NULL;
1524}
1525
hayeswangebc2ec482013-08-14 20:54:38 +08001526static void free_all_mem(struct r8152 *tp)
1527{
Hayes Wang252df8b2019-08-13 11:42:06 +08001528 struct rx_agg *agg, *agg_next;
1529 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001530 int i;
1531
Hayes Wang252df8b2019-08-13 11:42:06 +08001532 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001533
Hayes Wang252df8b2019-08-13 11:42:06 +08001534 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1535 free_rx_agg(tp, agg);
1536
1537 spin_unlock_irqrestore(&tp->rx_lock, flags);
1538
1539 WARN_ON(atomic_read(&tp->rx_count));
hayeswangebc2ec482013-08-14 20:54:38 +08001540
1541 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001542 usb_free_urb(tp->tx_info[i].urb);
1543 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001544
hayeswang9629e3c2014-01-15 10:42:15 +08001545 kfree(tp->tx_info[i].buffer);
1546 tp->tx_info[i].buffer = NULL;
1547 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001548 }
hayeswang40a82912013-08-14 20:54:40 +08001549
hayeswang9629e3c2014-01-15 10:42:15 +08001550 usb_free_urb(tp->intr_urb);
1551 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001552
hayeswang9629e3c2014-01-15 10:42:15 +08001553 kfree(tp->intr_buff);
1554 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001555}
1556
1557static int alloc_all_mem(struct r8152 *tp)
1558{
1559 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001560 struct usb_interface *intf = tp->intf;
1561 struct usb_host_interface *alt = intf->cur_altsetting;
1562 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001563 int node, i;
hayeswangebc2ec482013-08-14 20:54:38 +08001564
1565 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1566
1567 spin_lock_init(&tp->rx_lock);
1568 spin_lock_init(&tp->tx_lock);
Hayes Wang252df8b2019-08-13 11:42:06 +08001569 INIT_LIST_HEAD(&tp->rx_info);
hayeswangebc2ec482013-08-14 20:54:38 +08001570 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001571 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001572 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001573 skb_queue_head_init(&tp->rx_queue);
Hayes Wang252df8b2019-08-13 11:42:06 +08001574 atomic_set(&tp->rx_count, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001575
1576 for (i = 0; i < RTL8152_MAX_RX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001577 if (!alloc_rx_agg(tp, GFP_KERNEL))
hayeswangebc2ec482013-08-14 20:54:38 +08001578 goto err1;
hayeswangebc2ec482013-08-14 20:54:38 +08001579 }
1580
1581 for (i = 0; i < RTL8152_MAX_TX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001582 struct urb *urb;
1583 u8 *buf;
1584
hayeswang52aec122014-09-02 10:27:52 +08001585 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001586 if (!buf)
1587 goto err1;
1588
1589 if (buf != tx_agg_align(buf)) {
1590 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001591 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001592 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001593 if (!buf)
1594 goto err1;
1595 }
1596
1597 urb = usb_alloc_urb(0, GFP_KERNEL);
1598 if (!urb) {
1599 kfree(buf);
1600 goto err1;
1601 }
1602
1603 INIT_LIST_HEAD(&tp->tx_info[i].list);
1604 tp->tx_info[i].context = tp;
1605 tp->tx_info[i].urb = urb;
1606 tp->tx_info[i].buffer = buf;
1607 tp->tx_info[i].head = tx_agg_align(buf);
1608
1609 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1610 }
1611
hayeswang40a82912013-08-14 20:54:40 +08001612 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1613 if (!tp->intr_urb)
1614 goto err1;
1615
1616 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1617 if (!tp->intr_buff)
1618 goto err1;
1619
1620 tp->intr_interval = (int)ep_intr->desc.bInterval;
1621 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001622 tp->intr_buff, INTBUFSIZE, intr_callback,
1623 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001624
hayeswangebc2ec482013-08-14 20:54:38 +08001625 return 0;
1626
1627err1:
1628 free_all_mem(tp);
1629 return -ENOMEM;
1630}
1631
hayeswang0de98f62013-08-16 16:09:35 +08001632static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1633{
1634 struct tx_agg *agg = NULL;
1635 unsigned long flags;
1636
hayeswang21949ab2014-03-07 11:04:35 +08001637 if (list_empty(&tp->tx_free))
1638 return NULL;
1639
hayeswang0de98f62013-08-16 16:09:35 +08001640 spin_lock_irqsave(&tp->tx_lock, flags);
1641 if (!list_empty(&tp->tx_free)) {
1642 struct list_head *cursor;
1643
1644 cursor = tp->tx_free.next;
1645 list_del_init(cursor);
1646 agg = list_entry(cursor, struct tx_agg, list);
1647 }
1648 spin_unlock_irqrestore(&tp->tx_lock, flags);
1649
1650 return agg;
1651}
1652
hayeswangb209af92014-08-25 15:53:00 +08001653/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001654 * The hw limites the value the transport offset. When the offset is out of the
1655 * range, calculate the checksum by sw.
1656 */
1657static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1658 struct sk_buff_head *list)
1659{
1660 if (skb_shinfo(skb)->gso_size) {
1661 netdev_features_t features = tp->netdev->features;
1662 struct sk_buff_head seg_list;
1663 struct sk_buff *segs, *nskb;
1664
hayeswanga91d45f2014-07-11 16:48:27 +08001665 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001666 segs = skb_gso_segment(skb, features);
1667 if (IS_ERR(segs) || !segs)
1668 goto drop;
1669
1670 __skb_queue_head_init(&seg_list);
1671
1672 do {
1673 nskb = segs;
1674 segs = segs->next;
1675 nskb->next = NULL;
1676 __skb_queue_tail(&seg_list, nskb);
1677 } while (segs);
1678
1679 skb_queue_splice(&seg_list, list);
1680 dev_kfree_skb(skb);
1681 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1682 if (skb_checksum_help(skb) < 0)
1683 goto drop;
1684
1685 __skb_queue_head(list, skb);
1686 } else {
1687 struct net_device_stats *stats;
1688
1689drop:
1690 stats = &tp->netdev->stats;
1691 stats->tx_dropped++;
1692 dev_kfree_skb(skb);
1693 }
1694}
1695
hayeswangb209af92014-08-25 15:53:00 +08001696/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001697 * According to the document of microsoft, the TCP Pseudo Header excludes the
1698 * packet length for IPv6 TCP large packets.
1699 */
1700static int msdn_giant_send_check(struct sk_buff *skb)
1701{
1702 const struct ipv6hdr *ipv6h;
1703 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001704 int ret;
1705
1706 ret = skb_cow_head(skb, 0);
1707 if (ret)
1708 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001709
1710 ipv6h = ipv6_hdr(skb);
1711 th = tcp_hdr(skb);
1712
1713 th->check = 0;
1714 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1715
hayeswangfcb308d2014-03-11 10:20:32 +08001716 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001717}
1718
hayeswangc5554292014-09-12 10:43:11 +08001719static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1720{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001721 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001722 u32 opts2;
1723
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001724 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001725 desc->opts2 |= cpu_to_le32(opts2);
1726 }
1727}
1728
1729static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1730{
1731 u32 opts2 = le32_to_cpu(desc->opts2);
1732
1733 if (opts2 & RX_VLAN_TAG)
1734 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1735 swab16(opts2 & 0xffff));
1736}
1737
hayeswang60c89072014-03-07 11:04:39 +08001738static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1739 struct sk_buff *skb, u32 len, u32 transport_offset)
1740{
1741 u32 mss = skb_shinfo(skb)->gso_size;
1742 u32 opts1, opts2 = 0;
1743 int ret = TX_CSUM_SUCCESS;
1744
1745 WARN_ON_ONCE(len > TX_LEN_MAX);
1746
1747 opts1 = len | TX_FS | TX_LS;
1748
1749 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001750 if (transport_offset > GTTCPHO_MAX) {
1751 netif_warn(tp, tx_err, tp->netdev,
1752 "Invalid transport offset 0x%x for TSO\n",
1753 transport_offset);
1754 ret = TX_CSUM_TSO;
1755 goto unavailable;
1756 }
1757
hayeswang6e74d172015-02-06 11:30:50 +08001758 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001759 case htons(ETH_P_IP):
1760 opts1 |= GTSENDV4;
1761 break;
1762
hayeswang6128d1bb2014-03-07 11:04:40 +08001763 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001764 if (msdn_giant_send_check(skb)) {
1765 ret = TX_CSUM_TSO;
1766 goto unavailable;
1767 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001768 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001769 break;
1770
hayeswang60c89072014-03-07 11:04:39 +08001771 default:
1772 WARN_ON_ONCE(1);
1773 break;
1774 }
1775
1776 opts1 |= transport_offset << GTTCPHO_SHIFT;
1777 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1778 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001779 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001780
hayeswang6128d1bb2014-03-07 11:04:40 +08001781 if (transport_offset > TCPHO_MAX) {
1782 netif_warn(tp, tx_err, tp->netdev,
1783 "Invalid transport offset 0x%x\n",
1784 transport_offset);
1785 ret = TX_CSUM_NONE;
1786 goto unavailable;
1787 }
1788
hayeswang6e74d172015-02-06 11:30:50 +08001789 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001790 case htons(ETH_P_IP):
1791 opts2 |= IPV4_CS;
1792 ip_protocol = ip_hdr(skb)->protocol;
1793 break;
1794
1795 case htons(ETH_P_IPV6):
1796 opts2 |= IPV6_CS;
1797 ip_protocol = ipv6_hdr(skb)->nexthdr;
1798 break;
1799
1800 default:
1801 ip_protocol = IPPROTO_RAW;
1802 break;
1803 }
1804
hayeswang60c89072014-03-07 11:04:39 +08001805 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001806 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001807 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001808 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001809 else
hayeswang5bd23882013-08-14 20:54:39 +08001810 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001811
hayeswang60c89072014-03-07 11:04:39 +08001812 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001813 }
hayeswang60c89072014-03-07 11:04:39 +08001814
1815 desc->opts2 = cpu_to_le32(opts2);
1816 desc->opts1 = cpu_to_le32(opts1);
1817
hayeswang6128d1bb2014-03-07 11:04:40 +08001818unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001819 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001820}
1821
hayeswangb1379d92013-08-16 16:09:37 +08001822static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1823{
hayeswangd84130a2014-02-18 21:49:02 +08001824 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001825 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001826 u8 *tx_data;
1827
hayeswangd84130a2014-02-18 21:49:02 +08001828 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001829 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001830 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001831 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001832
hayeswangb1379d92013-08-16 16:09:37 +08001833 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001834 agg->skb_num = 0;
1835 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001836 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001837
hayeswang7937f9e2013-11-20 17:30:54 +08001838 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001839 struct tx_desc *tx_desc;
1840 struct sk_buff *skb;
1841 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001842 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001843
hayeswangd84130a2014-02-18 21:49:02 +08001844 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001845 if (!skb)
1846 break;
1847
hayeswang60c89072014-03-07 11:04:39 +08001848 len = skb->len + sizeof(*tx_desc);
1849
1850 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001851 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001852 break;
1853 }
1854
hayeswang7937f9e2013-11-20 17:30:54 +08001855 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001856 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001857
1858 offset = (u32)skb_transport_offset(skb);
1859
hayeswang6128d1bb2014-03-07 11:04:40 +08001860 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1861 r8152_csum_workaround(tp, skb, &skb_head);
1862 continue;
1863 }
hayeswang60c89072014-03-07 11:04:39 +08001864
hayeswangc5554292014-09-12 10:43:11 +08001865 rtl_tx_vlan_tag(tx_desc, skb);
1866
hayeswangb1379d92013-08-16 16:09:37 +08001867 tx_data += sizeof(*tx_desc);
1868
hayeswang60c89072014-03-07 11:04:39 +08001869 len = skb->len;
1870 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1871 struct net_device_stats *stats = &tp->netdev->stats;
1872
1873 stats->tx_dropped++;
1874 dev_kfree_skb_any(skb);
1875 tx_data -= sizeof(*tx_desc);
1876 continue;
1877 }
hayeswangb1379d92013-08-16 16:09:37 +08001878
hayeswang7937f9e2013-11-20 17:30:54 +08001879 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001880 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001881 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001882
1883 dev_kfree_skb_any(skb);
1884
hayeswang52aec122014-09-02 10:27:52 +08001885 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001886
1887 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1888 break;
hayeswangb1379d92013-08-16 16:09:37 +08001889 }
1890
hayeswangd84130a2014-02-18 21:49:02 +08001891 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001892 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001893 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001894 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001895 }
1896
hayeswang0c3121f2014-03-07 11:04:36 +08001897 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001898
1899 if (netif_queue_stopped(tp->netdev) &&
1900 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1901 netif_wake_queue(tp->netdev);
1902
hayeswang0c3121f2014-03-07 11:04:36 +08001903 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001904
hayeswang0c3121f2014-03-07 11:04:36 +08001905 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001906 if (ret < 0)
1907 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001908
hayeswangb1379d92013-08-16 16:09:37 +08001909 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1910 agg->head, (int)(tx_data - (u8 *)agg->head),
1911 (usb_complete_t)write_bulk_callback, agg);
1912
hayeswang0c3121f2014-03-07 11:04:36 +08001913 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001914 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001915 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001916
1917out_tx_fill:
1918 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001919}
1920
hayeswang565cab02014-03-07 11:04:38 +08001921static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1922{
1923 u8 checksum = CHECKSUM_NONE;
1924 u32 opts2, opts3;
1925
hayeswang19c0f402017-01-11 16:25:34 +08001926 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001927 goto return_result;
1928
1929 opts2 = le32_to_cpu(rx_desc->opts2);
1930 opts3 = le32_to_cpu(rx_desc->opts3);
1931
1932 if (opts2 & RD_IPV4_CS) {
1933 if (opts3 & IPF)
1934 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001935 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1936 checksum = CHECKSUM_UNNECESSARY;
1937 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001938 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001939 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001940 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1941 checksum = CHECKSUM_UNNECESSARY;
1942 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1943 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001944 }
1945
1946return_result:
1947 return checksum;
1948}
1949
hayeswangd823ab62015-01-12 12:06:23 +08001950static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001951{
hayeswanga5a4f462013-08-16 16:09:34 +08001952 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001953 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001954 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08001955 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08001956
1957 if (!skb_queue_empty(&tp->rx_queue)) {
1958 while (work_done < budget) {
1959 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1960 struct net_device *netdev = tp->netdev;
1961 struct net_device_stats *stats = &netdev->stats;
1962 unsigned int pkt_len;
1963
1964 if (!skb)
1965 break;
1966
1967 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08001968 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001969 work_done++;
1970 stats->rx_packets++;
1971 stats->rx_bytes += pkt_len;
1972 }
1973 }
hayeswangebc2ec482013-08-14 20:54:38 +08001974
hayeswangd84130a2014-02-18 21:49:02 +08001975 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001976 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001977
1978 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001979 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001980 list_splice_init(&tp->rx_done, &rx_queue);
1981 spin_unlock_irqrestore(&tp->rx_lock, flags);
1982
1983 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001984 struct rx_desc *rx_desc;
1985 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001986 int len_used = 0;
1987 struct urb *urb;
1988 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001989
hayeswangebc2ec482013-08-14 20:54:38 +08001990 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001991
1992 agg = list_entry(cursor, struct rx_agg, list);
1993 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001994 if (urb->actual_length < ETH_ZLEN)
1995 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001996
Hayes Wangd55d7082019-08-13 11:42:07 +08001997 rx_desc = agg->buffer;
1998 rx_data = agg->buffer;
hayeswang7937f9e2013-11-20 17:30:54 +08001999 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002000
hayeswang7937f9e2013-11-20 17:30:54 +08002001 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08002002 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08002003 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08002004 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08002005 struct sk_buff *skb;
2006
hayeswang74544452017-06-09 17:11:47 +08002007 /* limite the skb numbers for rx_queue */
2008 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2009 break;
2010
hayeswang7937f9e2013-11-20 17:30:54 +08002011 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08002012 if (pkt_len < ETH_ZLEN)
2013 break;
2014
hayeswang7937f9e2013-11-20 17:30:54 +08002015 len_used += pkt_len;
2016 if (urb->actual_length < len_used)
2017 break;
2018
hayeswangb65c0c92017-06-21 11:25:18 +08002019 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08002020 rx_data += sizeof(struct rx_desc);
2021
hayeswangce594e92017-03-16 14:32:22 +08002022 skb = napi_alloc_skb(napi, pkt_len);
hayeswangebc2ec482013-08-14 20:54:38 +08002023 if (!skb) {
2024 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08002025 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08002026 }
hayeswang565cab02014-03-07 11:04:38 +08002027
2028 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002029 memcpy(skb->data, rx_data, pkt_len);
2030 skb_put(skb, pkt_len);
2031 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08002032 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002033 if (work_done < budget) {
hayeswangce594e92017-03-16 14:32:22 +08002034 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002035 work_done++;
2036 stats->rx_packets++;
2037 stats->rx_bytes += pkt_len;
2038 } else {
2039 __skb_queue_tail(&tp->rx_queue, skb);
2040 }
hayeswangebc2ec482013-08-14 20:54:38 +08002041
hayeswang5e2f7482014-03-07 11:04:37 +08002042find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002043 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002044 rx_desc = (struct rx_desc *)rx_data;
Hayes Wangd55d7082019-08-13 11:42:07 +08002045 len_used = (int)(rx_data - (u8 *)agg->buffer);
hayeswang7937f9e2013-11-20 17:30:54 +08002046 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002047 }
2048
hayeswang0de98f62013-08-16 16:09:35 +08002049submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002050 if (!ret) {
2051 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2052 } else {
2053 urb->actual_length = 0;
2054 list_add_tail(&agg->list, next);
2055 }
2056 }
2057
2058 if (!list_empty(&rx_queue)) {
2059 spin_lock_irqsave(&tp->rx_lock, flags);
2060 list_splice_tail(&rx_queue, &tp->rx_done);
2061 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002062 }
hayeswangd823ab62015-01-12 12:06:23 +08002063
2064out1:
2065 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002066}
2067
2068static void tx_bottom(struct r8152 *tp)
2069{
hayeswangebc2ec482013-08-14 20:54:38 +08002070 int res;
2071
hayeswangb1379d92013-08-16 16:09:37 +08002072 do {
2073 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002074
hayeswangb1379d92013-08-16 16:09:37 +08002075 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002076 break;
2077
hayeswangb1379d92013-08-16 16:09:37 +08002078 agg = r8152_get_tx_agg(tp);
2079 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002080 break;
hayeswangb1379d92013-08-16 16:09:37 +08002081
2082 res = r8152_tx_agg_fill(tp, agg);
2083 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002084 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002085
2086 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002087 rtl_set_unplug(tp);
hayeswangb1379d92013-08-16 16:09:37 +08002088 netif_device_detach(netdev);
2089 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002090 struct net_device_stats *stats = &netdev->stats;
2091 unsigned long flags;
2092
hayeswangb1379d92013-08-16 16:09:37 +08002093 netif_warn(tp, tx_err, netdev,
2094 "failed tx_urb %d\n", res);
2095 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002096
hayeswangb1379d92013-08-16 16:09:37 +08002097 spin_lock_irqsave(&tp->tx_lock, flags);
2098 list_add_tail(&agg->list, &tp->tx_free);
2099 spin_unlock_irqrestore(&tp->tx_lock, flags);
2100 }
hayeswangebc2ec482013-08-14 20:54:38 +08002101 }
hayeswangb1379d92013-08-16 16:09:37 +08002102 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002103}
2104
hayeswangd823ab62015-01-12 12:06:23 +08002105static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08002106{
hayeswangebc2ec482013-08-14 20:54:38 +08002107 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2108 return;
2109
2110 if (!test_bit(WORK_ENABLE, &tp->flags))
2111 return;
2112
hayeswang7559fb2f2013-08-16 16:09:38 +08002113 /* When link down, the driver would cancel all bulks. */
2114 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002115 if (!netif_carrier_ok(tp->netdev))
2116 return;
2117
hayeswangd823ab62015-01-12 12:06:23 +08002118 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002119
hayeswang0c3121f2014-03-07 11:04:36 +08002120 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002121}
2122
hayeswangd823ab62015-01-12 12:06:23 +08002123static int r8152_poll(struct napi_struct *napi, int budget)
2124{
2125 struct r8152 *tp = container_of(napi, struct r8152, napi);
2126 int work_done;
2127
2128 work_done = rx_bottom(tp, budget);
2129 bottom_half(tp);
2130
2131 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002132 if (!napi_complete_done(napi, work_done))
2133 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002134 if (!list_empty(&tp->rx_done))
2135 napi_schedule(napi);
hayeswang248b2132017-01-26 09:38:33 +08002136 else if (!skb_queue_empty(&tp->tx_queue) &&
2137 !list_empty(&tp->tx_free))
2138 napi_schedule(napi);
hayeswangd823ab62015-01-12 12:06:23 +08002139 }
2140
hayeswanga3307f92017-06-09 17:11:48 +08002141out:
hayeswangd823ab62015-01-12 12:06:23 +08002142 return work_done;
2143}
2144
hayeswangebc2ec482013-08-14 20:54:38 +08002145static
2146int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2147{
hayeswanga0fccd42014-11-20 10:29:05 +08002148 int ret;
2149
hayeswangef827a52015-01-09 10:26:36 +08002150 /* The rx would be stopped, so skip submitting */
2151 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2152 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2153 return 0;
2154
hayeswangebc2ec482013-08-14 20:54:38 +08002155 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
Hayes Wangd55d7082019-08-13 11:42:07 +08002156 agg->buffer, tp->rx_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002157 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002158
hayeswanga0fccd42014-11-20 10:29:05 +08002159 ret = usb_submit_urb(agg->urb, mem_flags);
2160 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002161 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002162 netif_device_detach(tp->netdev);
2163 } else if (ret) {
2164 struct urb *urb = agg->urb;
2165 unsigned long flags;
2166
2167 urb->actual_length = 0;
2168 spin_lock_irqsave(&tp->rx_lock, flags);
2169 list_add_tail(&agg->list, &tp->rx_done);
2170 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002171
2172 netif_err(tp, rx_err, tp->netdev,
2173 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2174
2175 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002176 }
2177
2178 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002179}
2180
hayeswang00a5e362014-02-18 21:48:59 +08002181static void rtl_drop_queued_tx(struct r8152 *tp)
2182{
2183 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002184 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002185 struct sk_buff *skb;
2186
hayeswangd84130a2014-02-18 21:49:02 +08002187 if (skb_queue_empty(tx_queue))
2188 return;
2189
2190 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002191 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002192 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002193 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002194
2195 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002196 dev_kfree_skb(skb);
2197 stats->tx_dropped++;
2198 }
2199}
2200
hayeswangac718b62013-05-02 16:01:25 +00002201static void rtl8152_tx_timeout(struct net_device *netdev)
2202{
2203 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002204
Hayes Wang4a8deae2014-01-07 11:18:22 +08002205 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002206
2207 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002208}
2209
2210static void rtl8152_set_rx_mode(struct net_device *netdev)
2211{
2212 struct r8152 *tp = netdev_priv(netdev);
2213
hayeswang51d979f2015-02-06 11:30:47 +08002214 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002215 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002216 schedule_delayed_work(&tp->schedule, 0);
2217 }
hayeswangac718b62013-05-02 16:01:25 +00002218}
2219
2220static void _rtl8152_set_rx_mode(struct net_device *netdev)
2221{
2222 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002223 u32 mc_filter[2]; /* Multicast hash filter */
2224 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002225 u32 ocp_data;
2226
hayeswangac718b62013-05-02 16:01:25 +00002227 netif_stop_queue(netdev);
2228 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2229 ocp_data &= ~RCR_ACPT_ALL;
2230 ocp_data |= RCR_AB | RCR_APM;
2231
2232 if (netdev->flags & IFF_PROMISC) {
2233 /* Unconditionally log net taps. */
2234 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2235 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002236 mc_filter[1] = 0xffffffff;
2237 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002238 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2239 (netdev->flags & IFF_ALLMULTI)) {
2240 /* Too many to filter perfectly -- accept all multicasts. */
2241 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002242 mc_filter[1] = 0xffffffff;
2243 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002244 } else {
2245 struct netdev_hw_addr *ha;
2246
hayeswangb209af92014-08-25 15:53:00 +08002247 mc_filter[1] = 0;
2248 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002249 netdev_for_each_mc_addr(ha, netdev) {
2250 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002251
hayeswangac718b62013-05-02 16:01:25 +00002252 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2253 ocp_data |= RCR_AM;
2254 }
2255 }
2256
hayeswang31787f52013-07-31 17:21:25 +08002257 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2258 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002259
hayeswang31787f52013-07-31 17:21:25 +08002260 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002261 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2262 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002263}
2264
hayeswanga5e31252015-01-06 17:41:58 +08002265static netdev_features_t
2266rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2267 netdev_features_t features)
2268{
2269 u32 mss = skb_shinfo(skb)->gso_size;
2270 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2271 int offset = skb_transport_offset(skb);
2272
2273 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002274 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002275 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2276 features &= ~NETIF_F_GSO_MASK;
2277
2278 return features;
2279}
2280
hayeswangac718b62013-05-02 16:01:25 +00002281static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002282 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002283{
2284 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002285
hayeswangac718b62013-05-02 16:01:25 +00002286 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002287
hayeswang61598782013-11-20 17:30:55 +08002288 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002289
hayeswang0c3121f2014-03-07 11:04:36 +08002290 if (!list_empty(&tp->tx_free)) {
2291 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08002292 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002293 schedule_delayed_work(&tp->schedule, 0);
2294 } else {
2295 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08002296 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002297 }
hayeswangb209af92014-08-25 15:53:00 +08002298 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002299 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002300 }
hayeswangdd1b1192013-11-20 17:30:56 +08002301
hayeswangac718b62013-05-02 16:01:25 +00002302 return NETDEV_TX_OK;
2303}
2304
2305static void r8152b_reset_packet_filter(struct r8152 *tp)
2306{
2307 u32 ocp_data;
2308
2309 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2310 ocp_data &= ~FMC_FCR_MCU_EN;
2311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2312 ocp_data |= FMC_FCR_MCU_EN;
2313 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2314}
2315
2316static void rtl8152_nic_reset(struct r8152 *tp)
2317{
2318 int i;
2319
2320 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2321
2322 for (i = 0; i < 1000; i++) {
2323 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2324 break;
hayeswangb209af92014-08-25 15:53:00 +08002325 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002326 }
2327}
2328
hayeswangdd1b1192013-11-20 17:30:56 +08002329static void set_tx_qlen(struct r8152 *tp)
2330{
2331 struct net_device *netdev = tp->netdev;
2332
hayeswangb65c0c92017-06-21 11:25:18 +08002333 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002334 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002335}
2336
hayeswangac718b62013-05-02 16:01:25 +00002337static inline u8 rtl8152_get_speed(struct r8152 *tp)
2338{
2339 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2340}
2341
hayeswang507605a2014-01-02 11:22:43 +08002342static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002343{
hayeswangebc2ec482013-08-14 20:54:38 +08002344 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002345 u8 speed;
2346
2347 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002348 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002349 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002350 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002351 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2352 } else {
2353 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002354 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002355 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2356 }
hayeswang507605a2014-01-02 11:22:43 +08002357}
2358
hayeswang00a5e362014-02-18 21:48:59 +08002359static void rxdy_gated_en(struct r8152 *tp, bool enable)
2360{
2361 u32 ocp_data;
2362
2363 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2364 if (enable)
2365 ocp_data |= RXDY_GATED_EN;
2366 else
2367 ocp_data &= ~RXDY_GATED_EN;
2368 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2369}
2370
hayeswang445f7f42014-09-23 16:31:47 +08002371static int rtl_start_rx(struct r8152 *tp)
2372{
Hayes Wang252df8b2019-08-13 11:42:06 +08002373 struct rx_agg *agg, *agg_next;
2374 struct list_head tmp_list;
2375 unsigned long flags;
2376 int ret = 0;
2377
2378 INIT_LIST_HEAD(&tmp_list);
2379
2380 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002381
2382 INIT_LIST_HEAD(&tp->rx_done);
Hayes Wang252df8b2019-08-13 11:42:06 +08002383
2384 list_splice_init(&tp->rx_info, &tmp_list);
2385
2386 spin_unlock_irqrestore(&tp->rx_lock, flags);
2387
2388 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2389 INIT_LIST_HEAD(&agg->list);
2390
2391 if (ret < 0)
2392 list_add_tail(&agg->list, &tp->rx_done);
2393 else
2394 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
hayeswang445f7f42014-09-23 16:31:47 +08002395 }
2396
Hayes Wang252df8b2019-08-13 11:42:06 +08002397 spin_lock_irqsave(&tp->rx_lock, flags);
2398 WARN_ON(!list_empty(&tp->rx_info));
2399 list_splice(&tmp_list, &tp->rx_info);
2400 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang7bcf4f62014-11-20 10:29:06 +08002401
hayeswang445f7f42014-09-23 16:31:47 +08002402 return ret;
2403}
2404
2405static int rtl_stop_rx(struct r8152 *tp)
2406{
Hayes Wang252df8b2019-08-13 11:42:06 +08002407 struct rx_agg *agg, *agg_next;
2408 struct list_head tmp_list;
2409 unsigned long flags;
hayeswang445f7f42014-09-23 16:31:47 +08002410
Hayes Wang252df8b2019-08-13 11:42:06 +08002411 INIT_LIST_HEAD(&tmp_list);
2412
2413 /* The usb_kill_urb() couldn't be used in atomic.
2414 * Therefore, move the list of rx_info to a tmp one.
2415 * Then, list_for_each_entry_safe could be used without
2416 * spin lock.
2417 */
2418
2419 spin_lock_irqsave(&tp->rx_lock, flags);
2420 list_splice_init(&tp->rx_info, &tmp_list);
2421 spin_unlock_irqrestore(&tp->rx_lock, flags);
2422
2423 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list)
2424 usb_kill_urb(agg->urb);
2425
2426 /* Move back the list of temp to the rx_info */
2427 spin_lock_irqsave(&tp->rx_lock, flags);
2428 WARN_ON(!list_empty(&tp->rx_info));
2429 list_splice(&tmp_list, &tp->rx_info);
2430 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002431
hayeswangd823ab62015-01-12 12:06:23 +08002432 while (!skb_queue_empty(&tp->rx_queue))
2433 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2434
hayeswang445f7f42014-09-23 16:31:47 +08002435 return 0;
2436}
2437
Hayes Wang9fae5412019-07-03 15:11:56 +08002438static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2439{
2440 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2441 OWN_UPDATE | OWN_CLEAR);
2442}
2443
hayeswang507605a2014-01-02 11:22:43 +08002444static int rtl_enable(struct r8152 *tp)
2445{
2446 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002447
2448 r8152b_reset_packet_filter(tp);
2449
2450 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2451 ocp_data |= CR_RE | CR_TE;
2452 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2453
Hayes Wang9fae5412019-07-03 15:11:56 +08002454 switch (tp->version) {
2455 case RTL_VER_08:
2456 case RTL_VER_09:
2457 r8153b_rx_agg_chg_indicate(tp);
2458 break;
2459 default:
2460 break;
2461 }
2462
hayeswang00a5e362014-02-18 21:48:59 +08002463 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002464
hayeswangaa2e0922015-01-09 10:26:35 +08002465 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002466}
2467
hayeswang507605a2014-01-02 11:22:43 +08002468static int rtl8152_enable(struct r8152 *tp)
2469{
hayeswang68714382014-04-11 17:54:31 +08002470 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2471 return -ENODEV;
2472
hayeswang507605a2014-01-02 11:22:43 +08002473 set_tx_qlen(tp);
2474 rtl_set_eee_plus(tp);
2475
2476 return rtl_enable(tp);
2477}
2478
hayeswang464ec102015-02-12 14:33:46 +08002479static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002480{
hayeswang464ec102015-02-12 14:33:46 +08002481 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002482
hayeswang65b82d62017-06-15 14:44:03 +08002483 switch (tp->version) {
2484 case RTL_VER_03:
2485 case RTL_VER_04:
2486 case RTL_VER_05:
2487 case RTL_VER_06:
2488 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2489 ocp_data);
2490 break;
2491
2492 case RTL_VER_08:
2493 case RTL_VER_09:
2494 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2495 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2496 */
2497 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2498 128 / 8);
2499 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2500 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002501 break;
2502
2503 default:
2504 break;
2505 }
hayeswang464ec102015-02-12 14:33:46 +08002506}
2507
2508static void r8153_set_rx_early_size(struct r8152 *tp)
2509{
Hayes Wangec5791c2019-08-13 11:42:05 +08002510 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002511
hayeswang65b82d62017-06-15 14:44:03 +08002512 switch (tp->version) {
2513 case RTL_VER_03:
2514 case RTL_VER_04:
2515 case RTL_VER_05:
2516 case RTL_VER_06:
2517 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2518 ocp_data / 4);
2519 break;
2520 case RTL_VER_08:
2521 case RTL_VER_09:
2522 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2523 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002524 break;
2525 default:
2526 WARN_ON_ONCE(1);
2527 break;
2528 }
hayeswang43779f82014-01-02 11:25:10 +08002529}
2530
2531static int rtl8153_enable(struct r8152 *tp)
2532{
hayeswang68714382014-04-11 17:54:31 +08002533 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2534 return -ENODEV;
2535
hayeswang43779f82014-01-02 11:25:10 +08002536 set_tx_qlen(tp);
2537 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002538 r8153_set_rx_early_timeout(tp);
2539 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002540
2541 return rtl_enable(tp);
2542}
2543
hayeswangd70b1132014-09-19 15:17:18 +08002544static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002545{
hayeswangebc2ec482013-08-14 20:54:38 +08002546 u32 ocp_data;
2547 int i;
hayeswangac718b62013-05-02 16:01:25 +00002548
hayeswang68714382014-04-11 17:54:31 +08002549 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2550 rtl_drop_queued_tx(tp);
2551 return;
2552 }
2553
hayeswangac718b62013-05-02 16:01:25 +00002554 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2555 ocp_data &= ~RCR_ACPT_ALL;
2556 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2557
hayeswang00a5e362014-02-18 21:48:59 +08002558 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002559
2560 for (i = 0; i < RTL8152_MAX_TX; i++)
2561 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002562
hayeswang00a5e362014-02-18 21:48:59 +08002563 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002564
2565 for (i = 0; i < 1000; i++) {
2566 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2567 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2568 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002569 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002570 }
2571
2572 for (i = 0; i < 1000; i++) {
2573 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2574 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002575 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002576 }
2577
hayeswang445f7f42014-09-23 16:31:47 +08002578 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002579
2580 rtl8152_nic_reset(tp);
2581}
2582
hayeswang00a5e362014-02-18 21:48:59 +08002583static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2584{
2585 u32 ocp_data;
2586
2587 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2588 if (enable)
2589 ocp_data |= POWER_CUT;
2590 else
2591 ocp_data &= ~POWER_CUT;
2592 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2593
2594 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2595 ocp_data &= ~RESUME_INDICATE;
2596 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002597}
2598
hayeswangc5554292014-09-12 10:43:11 +08002599static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2600{
2601 u32 ocp_data;
2602
2603 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2604 if (enable)
2605 ocp_data |= CPCR_RX_VLAN;
2606 else
2607 ocp_data &= ~CPCR_RX_VLAN;
2608 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2609}
2610
2611static int rtl8152_set_features(struct net_device *dev,
2612 netdev_features_t features)
2613{
2614 netdev_features_t changed = features ^ dev->features;
2615 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002616 int ret;
2617
2618 ret = usb_autopm_get_interface(tp->intf);
2619 if (ret < 0)
2620 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002621
hayeswangb5403272014-10-09 18:00:26 +08002622 mutex_lock(&tp->control);
2623
hayeswangc5554292014-09-12 10:43:11 +08002624 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2625 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2626 rtl_rx_vlan_en(tp, true);
2627 else
2628 rtl_rx_vlan_en(tp, false);
2629 }
2630
hayeswangb5403272014-10-09 18:00:26 +08002631 mutex_unlock(&tp->control);
2632
hayeswang405f8a02014-10-09 18:00:24 +08002633 usb_autopm_put_interface(tp->intf);
2634
2635out:
2636 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002637}
2638
hayeswang21ff2e82014-02-18 21:49:06 +08002639#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2640
2641static u32 __rtl_get_wol(struct r8152 *tp)
2642{
2643 u32 ocp_data;
2644 u32 wolopts = 0;
2645
hayeswang21ff2e82014-02-18 21:49:06 +08002646 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2647 if (ocp_data & LINK_ON_WAKE_EN)
2648 wolopts |= WAKE_PHY;
2649
2650 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2651 if (ocp_data & UWF_EN)
2652 wolopts |= WAKE_UCAST;
2653 if (ocp_data & BWF_EN)
2654 wolopts |= WAKE_BCAST;
2655 if (ocp_data & MWF_EN)
2656 wolopts |= WAKE_MCAST;
2657
2658 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2659 if (ocp_data & MAGIC_EN)
2660 wolopts |= WAKE_MAGIC;
2661
2662 return wolopts;
2663}
2664
2665static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2666{
2667 u32 ocp_data;
2668
2669 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2670
2671 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2672 ocp_data &= ~LINK_ON_WAKE_EN;
2673 if (wolopts & WAKE_PHY)
2674 ocp_data |= LINK_ON_WAKE_EN;
2675 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2676
2677 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002678 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002679 if (wolopts & WAKE_UCAST)
2680 ocp_data |= UWF_EN;
2681 if (wolopts & WAKE_BCAST)
2682 ocp_data |= BWF_EN;
2683 if (wolopts & WAKE_MCAST)
2684 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002685 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2686
2687 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2688
2689 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2690 ocp_data &= ~MAGIC_EN;
2691 if (wolopts & WAKE_MAGIC)
2692 ocp_data |= MAGIC_EN;
2693 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2694
2695 if (wolopts & WAKE_ANY)
2696 device_set_wakeup_enable(&tp->udev->dev, true);
2697 else
2698 device_set_wakeup_enable(&tp->udev->dev, false);
2699}
2700
hayeswang134f98b2017-06-09 17:11:40 +08002701static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2702{
2703 /* MAC clock speed down */
2704 if (enable) {
2705 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2706 ALDPS_SPDWN_RATIO);
2707 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2708 EEE_SPDWN_RATIO);
2709 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2710 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2711 U1U2_SPDWN_EN | L1_SPDWN_EN);
2712 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2713 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2714 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2715 TP1000_SPDWN_EN);
2716 } else {
2717 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2718 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2719 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2720 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2721 }
2722}
2723
hayeswangb2143962015-07-24 13:54:23 +08002724static void r8153_u1u2en(struct r8152 *tp, bool enable)
2725{
2726 u8 u1u2[8];
2727
2728 if (enable)
2729 memset(u1u2, 0xff, sizeof(u1u2));
2730 else
2731 memset(u1u2, 0x00, sizeof(u1u2));
2732
2733 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2734}
2735
hayeswang65b82d62017-06-15 14:44:03 +08002736static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2737{
2738 u32 ocp_data;
2739
2740 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2741 if (enable)
2742 ocp_data |= LPM_U1U2_EN;
2743 else
2744 ocp_data &= ~LPM_U1U2_EN;
2745
2746 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2747}
2748
hayeswangb2143962015-07-24 13:54:23 +08002749static void r8153_u2p3en(struct r8152 *tp, bool enable)
2750{
2751 u32 ocp_data;
2752
2753 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002754 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002755 ocp_data |= U2P3_ENABLE;
2756 else
2757 ocp_data &= ~U2P3_ENABLE;
2758 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2759}
2760
hayeswang65b82d62017-06-15 14:44:03 +08002761static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2762{
2763 u32 ocp_data;
2764
2765 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2766 ocp_data &= ~clear;
2767 ocp_data |= set;
2768 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2769}
2770
2771static void r8153b_green_en(struct r8152 *tp, bool enable)
2772{
2773 u16 data;
2774
2775 if (enable) {
2776 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2777 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2778 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2779 } else {
2780 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2781 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2782 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2783 }
2784
2785 data = sram_read(tp, SRAM_GREEN_CFG);
2786 data |= GREEN_ETH_EN;
2787 sram_write(tp, SRAM_GREEN_CFG, data);
2788
2789 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2790}
2791
hayeswangc564b872017-06-09 17:11:38 +08002792static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2793{
2794 u16 data;
2795 int i;
2796
2797 for (i = 0; i < 500; i++) {
2798 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2799 data &= PHY_STAT_MASK;
2800 if (desired) {
2801 if (data == desired)
2802 break;
2803 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2804 data == PHY_STAT_EXT_INIT) {
2805 break;
2806 }
2807
2808 msleep(20);
2809 }
2810
2811 return data;
2812}
2813
hayeswang65b82d62017-06-15 14:44:03 +08002814static void r8153b_ups_en(struct r8152 *tp, bool enable)
2815{
2816 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2817
2818 if (enable) {
2819 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2820 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2821
2822 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2823 ocp_data |= BIT(0);
2824 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2825 } else {
2826 u16 data;
2827
2828 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2829 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2830
2831 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2832 ocp_data &= ~BIT(0);
2833 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2834
2835 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2836 ocp_data &= ~PCUT_STATUS;
2837 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2838
2839 data = r8153_phy_status(tp, 0);
2840
2841 switch (data) {
2842 case PHY_STAT_PWRDN:
2843 case PHY_STAT_EXT_INIT:
2844 r8153b_green_en(tp,
2845 test_bit(GREEN_ETHERNET, &tp->flags));
2846
2847 data = r8152_mdio_read(tp, MII_BMCR);
2848 data &= ~BMCR_PDOWN;
2849 data |= BMCR_RESET;
2850 r8152_mdio_write(tp, MII_BMCR, data);
2851
2852 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002853 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002854
2855 default:
2856 if (data != PHY_STAT_LAN_ON)
2857 netif_warn(tp, link, tp->netdev,
2858 "PHY not ready");
2859 break;
2860 }
2861 }
2862}
2863
hayeswangb2143962015-07-24 13:54:23 +08002864static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2865{
2866 u32 ocp_data;
2867
2868 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2869 if (enable)
2870 ocp_data |= PWR_EN | PHASE2_EN;
2871 else
2872 ocp_data &= ~(PWR_EN | PHASE2_EN);
2873 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2874
2875 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2876 ocp_data &= ~PCUT_STATUS;
2877 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2878}
2879
hayeswang65b82d62017-06-15 14:44:03 +08002880static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2881{
2882 u32 ocp_data;
2883
2884 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2885 if (enable)
2886 ocp_data |= PWR_EN | PHASE2_EN;
2887 else
2888 ocp_data &= ~PWR_EN;
2889 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2890
2891 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2892 ocp_data &= ~PCUT_STATUS;
2893 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2894}
2895
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002896static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08002897{
2898 u32 ocp_data;
2899
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002900 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08002901 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002902 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08002903 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002904 ocp_data &= ~UPCOMING_RUNTIME_D3;
2905 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002906
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002907 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
2908 ocp_data &= ~LINK_CHG_EVENT;
2909 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
2910
2911 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
2912 ocp_data &= ~LINK_CHANGE_FLAG;
2913 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002914}
2915
hayeswang7daed8d2015-07-24 13:54:24 +08002916static bool rtl_can_wakeup(struct r8152 *tp)
2917{
2918 struct usb_device *udev = tp->udev;
2919
2920 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
2921}
2922
hayeswang9a4be1b2014-02-18 21:49:07 +08002923static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2924{
2925 if (enable) {
2926 u32 ocp_data;
2927
2928 __rtl_set_wol(tp, WAKE_ANY);
2929
2930 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2931
2932 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2933 ocp_data |= LINK_OFF_WAKE_EN;
2934 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2935
2936 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2937 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08002938 u32 ocp_data;
2939
hayeswang9a4be1b2014-02-18 21:49:07 +08002940 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08002941
2942 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2943
2944 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2945 ocp_data &= ~LINK_OFF_WAKE_EN;
2946 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2947
2948 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08002949 }
2950}
hayeswangf95ae8a2016-06-30 15:33:35 +08002951
hayeswang2609af12016-07-05 16:11:46 +08002952static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
2953{
hayeswang2609af12016-07-05 16:11:46 +08002954 if (enable) {
2955 r8153_u1u2en(tp, false);
2956 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002957 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08002958 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08002959 } else {
hayeswang02552752017-06-09 17:11:42 +08002960 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002961 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08002962
2963 switch (tp->version) {
2964 case RTL_VER_03:
2965 case RTL_VER_04:
2966 break;
2967 case RTL_VER_05:
2968 case RTL_VER_06:
2969 default:
2970 r8153_u2p3en(tp, true);
2971 break;
2972 }
2973
hayeswangb2143962015-07-24 13:54:23 +08002974 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08002975 }
2976}
2977
hayeswang65b82d62017-06-15 14:44:03 +08002978static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
2979{
2980 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002981 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08002982 r8153b_u1u2en(tp, false);
2983 r8153_u2p3en(tp, false);
2984 rtl_runtime_suspend_enable(tp, true);
2985 r8153b_ups_en(tp, true);
2986 } else {
2987 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002988 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08002989 rtl_runtime_suspend_enable(tp, false);
2990 r8153_u2p3en(tp, true);
2991 r8153b_u1u2en(tp, true);
2992 }
2993}
2994
hayeswang43499682014-02-18 21:48:58 +08002995static void r8153_teredo_off(struct r8152 *tp)
2996{
2997 u32 ocp_data;
2998
hayeswang65b82d62017-06-15 14:44:03 +08002999 switch (tp->version) {
3000 case RTL_VER_01:
3001 case RTL_VER_02:
3002 case RTL_VER_03:
3003 case RTL_VER_04:
3004 case RTL_VER_05:
3005 case RTL_VER_06:
3006 case RTL_VER_07:
3007 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3008 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3009 OOB_TEREDO_EN);
3010 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3011 break;
3012
3013 case RTL_VER_08:
3014 case RTL_VER_09:
3015 /* The bit 0 ~ 7 are relative with teredo settings. They are
3016 * W1C (write 1 to clear), so set all 1 to disable it.
3017 */
3018 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3019 break;
3020
3021 default:
3022 break;
3023 }
hayeswang43499682014-02-18 21:48:58 +08003024
3025 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3026 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3027 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3028}
3029
hayeswang93fe9b12016-06-16 10:55:18 +08003030static void rtl_reset_bmu(struct r8152 *tp)
3031{
3032 u32 ocp_data;
3033
3034 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3035 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3036 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3037 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3038 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3039}
3040
hayeswangcda9fb02016-01-07 17:51:12 +08003041static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08003042{
hayeswangcda9fb02016-01-07 17:51:12 +08003043 if (enable) {
3044 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
3045 LINKENA | DIS_SDSAVE);
3046 } else {
3047 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
3048 DIS_SDSAVE);
3049 msleep(20);
3050 }
hayeswang43499682014-02-18 21:48:58 +08003051}
3052
hayeswange6449532016-09-20 16:22:05 +08003053static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
3054{
3055 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3056 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3057 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3058}
3059
3060static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3061{
3062 u16 data;
3063
3064 r8152_mmd_indirect(tp, dev, reg);
3065 data = ocp_reg_read(tp, OCP_EEE_DATA);
3066 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3067
3068 return data;
3069}
3070
3071static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3072{
3073 r8152_mmd_indirect(tp, dev, reg);
3074 ocp_reg_write(tp, OCP_EEE_DATA, data);
3075 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3076}
3077
3078static void r8152_eee_en(struct r8152 *tp, bool enable)
3079{
3080 u16 config1, config2, config3;
3081 u32 ocp_data;
3082
3083 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3084 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3085 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3086 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3087
3088 if (enable) {
3089 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3090 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3091 config1 |= sd_rise_time(1);
3092 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3093 config3 |= fast_snr(42);
3094 } else {
3095 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3096 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3097 RX_QUIET_EN);
3098 config1 |= sd_rise_time(7);
3099 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3100 config3 |= fast_snr(511);
3101 }
3102
3103 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3104 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3105 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3106 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3107}
3108
3109static void r8152b_enable_eee(struct r8152 *tp)
3110{
3111 r8152_eee_en(tp, true);
3112 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3113}
3114
3115static void r8152b_enable_fc(struct r8152 *tp)
3116{
3117 u16 anar;
3118
3119 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3120 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3121 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3122}
3123
hayeswangd70b1132014-09-19 15:17:18 +08003124static void rtl8152_disable(struct r8152 *tp)
3125{
hayeswangcda9fb02016-01-07 17:51:12 +08003126 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003127 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003128 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003129}
3130
hayeswang43499682014-02-18 21:48:58 +08003131static void r8152b_hw_phy_cfg(struct r8152 *tp)
3132{
hayeswangef39df82016-09-20 16:22:07 +08003133 r8152b_enable_eee(tp);
3134 r8152_aldps_en(tp, true);
3135 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003136
hayeswangaa66a5f2014-02-18 21:49:04 +08003137 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003138}
3139
hayeswangac718b62013-05-02 16:01:25 +00003140static void r8152b_exit_oob(struct r8152 *tp)
3141{
hayeswangdb8515e2014-03-06 15:07:16 +08003142 u32 ocp_data;
3143 int i;
hayeswangac718b62013-05-02 16:01:25 +00003144
3145 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3146 ocp_data &= ~RCR_ACPT_ALL;
3147 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3148
hayeswang00a5e362014-02-18 21:48:59 +08003149 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003150 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003151 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3152 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3153
3154 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3155 ocp_data &= ~NOW_IS_OOB;
3156 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3157
3158 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3159 ocp_data &= ~MCU_BORW_EN;
3160 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3161
3162 for (i = 0; i < 1000; i++) {
3163 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3164 if (ocp_data & LINK_LIST_READY)
3165 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003166 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003167 }
3168
3169 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3170 ocp_data |= RE_INIT_LL;
3171 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3172
3173 for (i = 0; i < 1000; i++) {
3174 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3175 if (ocp_data & LINK_LIST_READY)
3176 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003177 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003178 }
3179
3180 rtl8152_nic_reset(tp);
3181
3182 /* rx share fifo credit full threshold */
3183 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3184
hayeswanga3cc4652014-07-24 16:37:43 +08003185 if (tp->udev->speed == USB_SPEED_FULL ||
3186 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003187 /* rx share fifo credit near full threshold */
3188 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3189 RXFIFO_THR2_FULL);
3190 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3191 RXFIFO_THR3_FULL);
3192 } else {
3193 /* rx share fifo credit near full threshold */
3194 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3195 RXFIFO_THR2_HIGH);
3196 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3197 RXFIFO_THR3_HIGH);
3198 }
3199
3200 /* TX share fifo free credit full threshold */
3201 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3202
3203 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003204 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003205 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3206 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3207
hayeswangc5554292014-09-12 10:43:11 +08003208 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003209
3210 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3211
3212 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3213 ocp_data |= TCR0_AUTO_FIFO;
3214 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3215}
3216
3217static void r8152b_enter_oob(struct r8152 *tp)
3218{
hayeswang45f4a192014-01-06 17:08:41 +08003219 u32 ocp_data;
3220 int i;
hayeswangac718b62013-05-02 16:01:25 +00003221
3222 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3223 ocp_data &= ~NOW_IS_OOB;
3224 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3225
3226 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3227 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3228 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3229
hayeswangd70b1132014-09-19 15:17:18 +08003230 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003231
3232 for (i = 0; i < 1000; i++) {
3233 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3234 if (ocp_data & LINK_LIST_READY)
3235 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003236 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003237 }
3238
3239 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3240 ocp_data |= RE_INIT_LL;
3241 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3242
3243 for (i = 0; i < 1000; i++) {
3244 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3245 if (ocp_data & LINK_LIST_READY)
3246 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003247 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003248 }
3249
3250 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3251
hayeswangc5554292014-09-12 10:43:11 +08003252 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003253
Kevin Lo59c0b472019-08-01 11:29:38 +08003254 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00003255 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003256 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00003257
3258 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3259 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3260 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3261
hayeswang00a5e362014-02-18 21:48:59 +08003262 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003263
3264 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3265 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3266 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3267}
3268
hayeswang65b82d62017-06-15 14:44:03 +08003269static int r8153_patch_request(struct r8152 *tp, bool request)
3270{
3271 u16 data;
3272 int i;
3273
3274 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3275 if (request)
3276 data |= PATCH_REQUEST;
3277 else
3278 data &= ~PATCH_REQUEST;
3279 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3280
3281 for (i = 0; request && i < 5000; i++) {
3282 usleep_range(1000, 2000);
3283 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3284 break;
3285 }
3286
3287 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3288 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3289 r8153_patch_request(tp, false);
3290 return -ETIME;
3291 } else {
3292 return 0;
3293 }
3294}
3295
hayeswange6449532016-09-20 16:22:05 +08003296static void r8153_aldps_en(struct r8152 *tp, bool enable)
3297{
3298 u16 data;
3299
3300 data = ocp_reg_read(tp, OCP_POWER_CFG);
3301 if (enable) {
3302 data |= EN_ALDPS;
3303 ocp_reg_write(tp, OCP_POWER_CFG, data);
3304 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003305 int i;
3306
hayeswange6449532016-09-20 16:22:05 +08003307 data &= ~EN_ALDPS;
3308 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003309 for (i = 0; i < 20; i++) {
3310 usleep_range(1000, 2000);
3311 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3312 break;
3313 }
hayeswange6449532016-09-20 16:22:05 +08003314 }
3315}
3316
hayeswang65b82d62017-06-15 14:44:03 +08003317static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3318{
3319 r8153_aldps_en(tp, enable);
3320
3321 if (enable)
3322 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3323 else
3324 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3325}
3326
hayeswange6449532016-09-20 16:22:05 +08003327static void r8153_eee_en(struct r8152 *tp, bool enable)
3328{
3329 u32 ocp_data;
3330 u16 config;
3331
3332 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3333 config = ocp_reg_read(tp, OCP_EEE_CFG);
3334
3335 if (enable) {
3336 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3337 config |= EEE10_EN;
3338 } else {
3339 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3340 config &= ~EEE10_EN;
3341 }
3342
3343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3344 ocp_reg_write(tp, OCP_EEE_CFG, config);
3345}
3346
hayeswang65b82d62017-06-15 14:44:03 +08003347static void r8153b_eee_en(struct r8152 *tp, bool enable)
3348{
3349 r8153_eee_en(tp, enable);
3350
3351 if (enable)
3352 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3353 else
3354 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3355}
3356
3357static void r8153b_enable_fc(struct r8152 *tp)
3358{
3359 r8152b_enable_fc(tp);
3360 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3361}
3362
hayeswang43779f82014-01-02 11:25:10 +08003363static void r8153_hw_phy_cfg(struct r8152 *tp)
3364{
3365 u32 ocp_data;
3366 u16 data;
3367
hayeswangd768c612016-09-20 16:22:09 +08003368 /* disable ALDPS before updating the PHY parameters */
3369 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003370
hayeswangd768c612016-09-20 16:22:09 +08003371 /* disable EEE before updating the PHY parameters */
3372 r8153_eee_en(tp, false);
3373 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang43779f82014-01-02 11:25:10 +08003374
3375 if (tp->version == RTL_VER_03) {
3376 data = ocp_reg_read(tp, OCP_EEE_CFG);
3377 data &= ~CTAP_SHORT_EN;
3378 ocp_reg_write(tp, OCP_EEE_CFG, data);
3379 }
3380
3381 data = ocp_reg_read(tp, OCP_POWER_CFG);
3382 data |= EEE_CLKDIV_EN;
3383 ocp_reg_write(tp, OCP_POWER_CFG, data);
3384
3385 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3386 data |= EN_10M_BGOFF;
3387 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3388 data = ocp_reg_read(tp, OCP_POWER_CFG);
3389 data |= EN_10M_PLLOFF;
3390 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003391 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003392
3393 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3394 ocp_data |= PFM_PWM_SWITCH;
3395 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3396
hayeswangb4d99de2015-01-19 17:02:46 +08003397 /* Enable LPF corner auto tune */
3398 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003399
hayeswangb4d99de2015-01-19 17:02:46 +08003400 /* Adjust 10M Amplitude */
3401 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3402 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003403
hayeswangaf0287e2016-09-20 16:22:08 +08003404 r8153_eee_en(tp, true);
3405 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3406
hayeswangef39df82016-09-20 16:22:07 +08003407 r8153_aldps_en(tp, true);
3408 r8152b_enable_fc(tp);
3409
hayeswang3cb32342017-06-09 17:11:43 +08003410 switch (tp->version) {
3411 case RTL_VER_03:
3412 case RTL_VER_04:
3413 break;
3414 case RTL_VER_05:
3415 case RTL_VER_06:
3416 default:
3417 r8153_u2p3en(tp, true);
3418 break;
3419 }
3420
hayeswangaa66a5f2014-02-18 21:49:04 +08003421 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003422}
3423
hayeswang65b82d62017-06-15 14:44:03 +08003424static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3425{
3426 u32 ocp_data;
3427
3428 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3429 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3430 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3431 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3432
3433 return ocp_data;
3434}
3435
3436static void r8153b_hw_phy_cfg(struct r8152 *tp)
3437{
3438 u32 ocp_data, ups_flags = 0;
3439 u16 data;
3440
3441 /* disable ALDPS before updating the PHY parameters */
3442 r8153b_aldps_en(tp, false);
3443
3444 /* disable EEE before updating the PHY parameters */
3445 r8153b_eee_en(tp, false);
3446 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3447
3448 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3449
3450 data = sram_read(tp, SRAM_GREEN_CFG);
3451 data |= R_TUNE_EN;
3452 sram_write(tp, SRAM_GREEN_CFG, data);
3453 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3454 data |= PGA_RETURN_EN;
3455 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3456
3457 /* ADC Bias Calibration:
3458 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3459 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3460 * ADC ioffset.
3461 */
3462 ocp_data = r8152_efuse_read(tp, 0x7d);
3463 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3464 if (data != 0xffff)
3465 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3466
3467 /* ups mode tx-link-pulse timing adjustment:
3468 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3469 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3470 */
3471 ocp_data = ocp_reg_read(tp, 0xc426);
3472 ocp_data &= 0x3fff;
3473 if (ocp_data) {
3474 u32 swr_cnt_1ms_ini;
3475
3476 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3477 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3478 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3479 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3480 }
3481
3482 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3483 ocp_data |= PFM_PWM_SWITCH;
3484 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3485
3486 /* Advnace EEE */
3487 if (!r8153_patch_request(tp, true)) {
3488 data = ocp_reg_read(tp, OCP_POWER_CFG);
3489 data |= EEE_CLKDIV_EN;
3490 ocp_reg_write(tp, OCP_POWER_CFG, data);
3491
3492 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3493 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3494 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3495
3496 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3497 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3498
3499 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3500 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3501 UPS_FLAGS_EEE_PLLOFF_GIGA;
3502
3503 r8153_patch_request(tp, false);
3504 }
3505
3506 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3507
3508 r8153b_eee_en(tp, true);
3509 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3510
3511 r8153b_aldps_en(tp, true);
3512 r8153b_enable_fc(tp);
3513 r8153_u2p3en(tp, true);
3514
3515 set_bit(PHY_RESET, &tp->flags);
3516}
3517
hayeswang43779f82014-01-02 11:25:10 +08003518static void r8153_first_init(struct r8152 *tp)
3519{
3520 u32 ocp_data;
3521 int i;
3522
hayeswang134f98b2017-06-09 17:11:40 +08003523 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003524 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003525 r8153_teredo_off(tp);
3526
3527 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3528 ocp_data &= ~RCR_ACPT_ALL;
3529 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3530
hayeswang43779f82014-01-02 11:25:10 +08003531 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003532 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003533
3534 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3535 ocp_data &= ~NOW_IS_OOB;
3536 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3537
3538 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3539 ocp_data &= ~MCU_BORW_EN;
3540 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3541
3542 for (i = 0; i < 1000; i++) {
3543 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3544 if (ocp_data & LINK_LIST_READY)
3545 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003546 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003547 }
3548
3549 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3550 ocp_data |= RE_INIT_LL;
3551 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3552
3553 for (i = 0; i < 1000; i++) {
3554 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3555 if (ocp_data & LINK_LIST_READY)
3556 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003557 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003558 }
3559
hayeswangc5554292014-09-12 10:43:11 +08003560 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003561
hayeswangb65c0c92017-06-21 11:25:18 +08003562 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003563 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003564 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003565
3566 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3567 ocp_data |= TCR0_AUTO_FIFO;
3568 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3569
3570 rtl8152_nic_reset(tp);
3571
3572 /* rx share fifo credit full threshold */
3573 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3574 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3575 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3576 /* TX share fifo free credit full threshold */
3577 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003578}
3579
3580static void r8153_enter_oob(struct r8152 *tp)
3581{
3582 u32 ocp_data;
3583 int i;
3584
hayeswang134f98b2017-06-09 17:11:40 +08003585 r8153_mac_clk_spd(tp, true);
3586
hayeswang43779f82014-01-02 11:25:10 +08003587 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3588 ocp_data &= ~NOW_IS_OOB;
3589 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3590
hayeswangd70b1132014-09-19 15:17:18 +08003591 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003592 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003593
3594 for (i = 0; i < 1000; i++) {
3595 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3596 if (ocp_data & LINK_LIST_READY)
3597 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003598 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003599 }
3600
3601 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3602 ocp_data |= RE_INIT_LL;
3603 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3604
3605 for (i = 0; i < 1000; i++) {
3606 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3607 if (ocp_data & LINK_LIST_READY)
3608 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003609 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003610 }
3611
hayeswangb65c0c92017-06-21 11:25:18 +08003612 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003613 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003614
hayeswang65b82d62017-06-15 14:44:03 +08003615 switch (tp->version) {
3616 case RTL_VER_03:
3617 case RTL_VER_04:
3618 case RTL_VER_05:
3619 case RTL_VER_06:
3620 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3621 ocp_data &= ~TEREDO_WAKE_MASK;
3622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3623 break;
3624
3625 case RTL_VER_08:
3626 case RTL_VER_09:
3627 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3628 * type. Set it to zero. bits[7:0] are the W1C bits about
3629 * the events. Set them to all 1 to clear them.
3630 */
3631 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3632 break;
3633
3634 default:
3635 break;
3636 }
hayeswang43779f82014-01-02 11:25:10 +08003637
hayeswangc5554292014-09-12 10:43:11 +08003638 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003639
Kevin Lo59c0b472019-08-01 11:29:38 +08003640 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08003641 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003642 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003643
3644 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3645 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3646 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3647
hayeswang00a5e362014-02-18 21:48:59 +08003648 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003649
3650 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3651 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3652 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3653}
3654
hayeswangd70b1132014-09-19 15:17:18 +08003655static void rtl8153_disable(struct r8152 *tp)
3656{
hayeswangcda9fb02016-01-07 17:51:12 +08003657 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003658 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003659 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003660 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003661}
3662
hayeswang65b82d62017-06-15 14:44:03 +08003663static void rtl8153b_disable(struct r8152 *tp)
3664{
3665 r8153b_aldps_en(tp, false);
3666 rtl_disable(tp);
3667 rtl_reset_bmu(tp);
3668 r8153b_aldps_en(tp, true);
3669}
3670
hayeswangac718b62013-05-02 16:01:25 +00003671static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3672{
hayeswang43779f82014-01-02 11:25:10 +08003673 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003674 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003675 int ret = 0;
3676
hayeswangac718b62013-05-02 16:01:25 +00003677 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3678 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3679 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003680 if (tp->mii.supports_gmii) {
3681 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3682 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3683 } else {
3684 gbcr = 0;
3685 }
hayeswangac718b62013-05-02 16:01:25 +00003686
3687 if (autoneg == AUTONEG_DISABLE) {
3688 if (speed == SPEED_10) {
3689 bmcr = 0;
3690 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003691 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003692 } else if (speed == SPEED_100) {
3693 bmcr = BMCR_SPEED100;
3694 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003695 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003696 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3697 bmcr = BMCR_SPEED1000;
3698 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003699 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003700 } else {
3701 ret = -EINVAL;
3702 goto out;
3703 }
3704
hayeswang65b82d62017-06-15 14:44:03 +08003705 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003706 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003707 if (speed != SPEED_1000)
3708 speed_duplex++;
3709 }
hayeswangac718b62013-05-02 16:01:25 +00003710 } else {
3711 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003712 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003713 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003714 speed_duplex = NWAY_10M_FULL;
3715 } else {
hayeswangac718b62013-05-02 16:01:25 +00003716 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003717 speed_duplex = NWAY_10M_HALF;
3718 }
hayeswangac718b62013-05-02 16:01:25 +00003719 } else if (speed == SPEED_100) {
3720 if (duplex == DUPLEX_FULL) {
3721 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3722 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003723 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003724 } else {
3725 anar |= ADVERTISE_10HALF;
3726 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003727 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003728 }
hayeswang43779f82014-01-02 11:25:10 +08003729 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3730 if (duplex == DUPLEX_FULL) {
3731 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3732 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3733 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3734 } else {
3735 anar |= ADVERTISE_10HALF;
3736 anar |= ADVERTISE_100HALF;
3737 gbcr |= ADVERTISE_1000HALF;
3738 }
hayeswang65b82d62017-06-15 14:44:03 +08003739 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003740 } else {
3741 ret = -EINVAL;
3742 goto out;
3743 }
3744
3745 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3746 }
3747
hayeswangfae56172016-06-16 14:08:29 +08003748 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003749 bmcr |= BMCR_RESET;
3750
hayeswang43779f82014-01-02 11:25:10 +08003751 if (tp->mii.supports_gmii)
3752 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3753
hayeswangac718b62013-05-02 16:01:25 +00003754 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3755 r8152_mdio_write(tp, MII_BMCR, bmcr);
3756
hayeswang65b82d62017-06-15 14:44:03 +08003757 switch (tp->version) {
3758 case RTL_VER_08:
3759 case RTL_VER_09:
3760 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3761 UPS_FLAGS_SPEED_MASK);
3762 break;
3763
3764 default:
3765 break;
3766 }
3767
hayeswangfae56172016-06-16 14:08:29 +08003768 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003769 int i;
3770
hayeswangaa66a5f2014-02-18 21:49:04 +08003771 for (i = 0; i < 50; i++) {
3772 msleep(20);
3773 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3774 break;
3775 }
3776 }
3777
hayeswangac718b62013-05-02 16:01:25 +00003778out:
hayeswangac718b62013-05-02 16:01:25 +00003779 return ret;
3780}
3781
hayeswangd70b1132014-09-19 15:17:18 +08003782static void rtl8152_up(struct r8152 *tp)
3783{
3784 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3785 return;
3786
hayeswangcda9fb02016-01-07 17:51:12 +08003787 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003788 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003789 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003790}
3791
hayeswangac718b62013-05-02 16:01:25 +00003792static void rtl8152_down(struct r8152 *tp)
3793{
hayeswang68714382014-04-11 17:54:31 +08003794 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3795 rtl_drop_queued_tx(tp);
3796 return;
3797 }
3798
hayeswang00a5e362014-02-18 21:48:59 +08003799 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003800 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003801 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003802 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003803}
3804
hayeswangd70b1132014-09-19 15:17:18 +08003805static void rtl8153_up(struct r8152 *tp)
3806{
3807 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3808 return;
3809
hayeswangb2143962015-07-24 13:54:23 +08003810 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003811 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003812 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003813 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003814 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003815
3816 switch (tp->version) {
3817 case RTL_VER_03:
3818 case RTL_VER_04:
3819 break;
3820 case RTL_VER_05:
3821 case RTL_VER_06:
3822 default:
3823 r8153_u2p3en(tp, true);
3824 break;
3825 }
3826
hayeswangb2143962015-07-24 13:54:23 +08003827 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003828}
3829
hayeswang43779f82014-01-02 11:25:10 +08003830static void rtl8153_down(struct r8152 *tp)
3831{
hayeswang68714382014-04-11 17:54:31 +08003832 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3833 rtl_drop_queued_tx(tp);
3834 return;
3835 }
3836
hayeswangb9702722014-02-18 21:49:00 +08003837 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003838 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003839 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003840 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003841 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003842 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003843}
3844
hayeswang65b82d62017-06-15 14:44:03 +08003845static void rtl8153b_up(struct r8152 *tp)
3846{
3847 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3848 return;
3849
3850 r8153b_u1u2en(tp, false);
3851 r8153_u2p3en(tp, false);
3852 r8153b_aldps_en(tp, false);
3853
3854 r8153_first_init(tp);
3855 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3856
3857 r8153b_aldps_en(tp, true);
3858 r8153_u2p3en(tp, true);
3859 r8153b_u1u2en(tp, true);
3860}
3861
3862static void rtl8153b_down(struct r8152 *tp)
3863{
3864 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3865 rtl_drop_queued_tx(tp);
3866 return;
3867 }
3868
3869 r8153b_u1u2en(tp, false);
3870 r8153_u2p3en(tp, false);
3871 r8153b_power_cut_en(tp, false);
3872 r8153b_aldps_en(tp, false);
3873 r8153_enter_oob(tp);
3874 r8153b_aldps_en(tp, true);
3875}
3876
hayeswang2dd49e02015-09-07 11:57:44 +08003877static bool rtl8152_in_nway(struct r8152 *tp)
3878{
3879 u16 nway_state;
3880
3881 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3882 tp->ocp_base = 0x2000;
3883 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3884 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3885
3886 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3887 if (nway_state & 0xc000)
3888 return false;
3889 else
3890 return true;
3891}
3892
3893static bool rtl8153_in_nway(struct r8152 *tp)
3894{
3895 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
3896
3897 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
3898 return false;
3899 else
3900 return true;
3901}
3902
hayeswangac718b62013-05-02 16:01:25 +00003903static void set_carrier(struct r8152 *tp)
3904{
3905 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08003906 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00003907 u8 speed;
3908
3909 speed = rtl8152_get_speed(tp);
3910
3911 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08003912 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08003913 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08003914 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003915 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00003916 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08003917 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08003918 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
3919 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08003920 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08003921 netif_wake_queue(netdev);
3922 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08003923 } else if (netif_queue_stopped(netdev) &&
3924 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
3925 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003926 }
3927 } else {
hayeswang51d979f2015-02-06 11:30:47 +08003928 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00003929 netif_carrier_off(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003930 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08003931 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08003932 napi_enable(napi);
hayeswangde9bf292017-01-26 09:38:32 +08003933 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00003934 }
3935 }
hayeswangac718b62013-05-02 16:01:25 +00003936}
3937
3938static void rtl_work_func_t(struct work_struct *work)
3939{
3940 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
3941
hayeswanga1f83fe2014-11-12 10:05:05 +08003942 /* If the device is unplugged or !netif_running(), the workqueue
3943 * doesn't need to wake the device, and could return directly.
3944 */
3945 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
3946 return;
3947
hayeswang9a4be1b2014-02-18 21:49:07 +08003948 if (usb_autopm_get_interface(tp->intf) < 0)
3949 return;
3950
hayeswangac718b62013-05-02 16:01:25 +00003951 if (!test_bit(WORK_ENABLE, &tp->flags))
3952 goto out1;
3953
hayeswangb5403272014-10-09 18:00:26 +08003954 if (!mutex_trylock(&tp->control)) {
3955 schedule_delayed_work(&tp->schedule, 0);
3956 goto out1;
3957 }
3958
hayeswang216a8342016-01-07 17:51:11 +08003959 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08003960 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00003961
hayeswang216a8342016-01-07 17:51:11 +08003962 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00003963 _rtl8152_set_rx_mode(tp->netdev);
3964
hayeswangd823ab62015-01-12 12:06:23 +08003965 /* don't schedule napi before linking */
hayeswang216a8342016-01-07 17:51:11 +08003966 if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
3967 netif_carrier_ok(tp->netdev))
hayeswangd823ab62015-01-12 12:06:23 +08003968 napi_schedule(&tp->napi);
hayeswangaa66a5f2014-02-18 21:49:04 +08003969
hayeswangb5403272014-10-09 18:00:26 +08003970 mutex_unlock(&tp->control);
3971
hayeswangac718b62013-05-02 16:01:25 +00003972out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08003973 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003974}
3975
hayeswanga028a9e2016-06-13 17:49:36 +08003976static void rtl_hw_phy_work_func_t(struct work_struct *work)
3977{
3978 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
3979
3980 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3981 return;
3982
3983 if (usb_autopm_get_interface(tp->intf) < 0)
3984 return;
3985
3986 mutex_lock(&tp->control);
3987
3988 tp->rtl_ops.hw_phy_cfg(tp);
3989
hayeswangaa7e26b2016-06-13 17:49:38 +08003990 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08003991
hayeswanga028a9e2016-06-13 17:49:36 +08003992 mutex_unlock(&tp->control);
3993
3994 usb_autopm_put_interface(tp->intf);
3995}
3996
hayeswang5ee3c602016-01-07 17:12:17 +08003997#ifdef CONFIG_PM_SLEEP
3998static int rtl_notifier(struct notifier_block *nb, unsigned long action,
3999 void *data)
4000{
4001 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
4002
4003 switch (action) {
4004 case PM_HIBERNATION_PREPARE:
4005 case PM_SUSPEND_PREPARE:
4006 usb_autopm_get_interface(tp->intf);
4007 break;
4008
4009 case PM_POST_HIBERNATION:
4010 case PM_POST_SUSPEND:
4011 usb_autopm_put_interface(tp->intf);
4012 break;
4013
4014 case PM_POST_RESTORE:
4015 case PM_RESTORE_PREPARE:
4016 default:
4017 break;
4018 }
4019
4020 return NOTIFY_DONE;
4021}
4022#endif
4023
hayeswangac718b62013-05-02 16:01:25 +00004024static int rtl8152_open(struct net_device *netdev)
4025{
4026 struct r8152 *tp = netdev_priv(netdev);
4027 int res = 0;
4028
hayeswang7e9da482014-02-18 21:49:05 +08004029 res = alloc_all_mem(tp);
4030 if (res)
4031 goto out;
4032
hayeswang9a4be1b2014-02-18 21:49:07 +08004033 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004034 if (res < 0)
4035 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08004036
hayeswangb5403272014-10-09 18:00:26 +08004037 mutex_lock(&tp->control);
4038
hayeswang7e9da482014-02-18 21:49:05 +08004039 tp->rtl_ops.up(tp);
4040
hayeswang40a82912013-08-14 20:54:40 +08004041 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004042 netif_start_queue(netdev);
4043 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08004044
hayeswang3d55f442014-02-06 11:55:48 +08004045 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4046 if (res) {
4047 if (res == -ENODEV)
4048 netif_device_detach(tp->netdev);
4049 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
4050 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004051 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08004052 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08004053 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08004054
hayeswangb5403272014-10-09 18:00:26 +08004055 mutex_unlock(&tp->control);
4056
hayeswang9a4be1b2014-02-18 21:49:07 +08004057 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08004058#ifdef CONFIG_PM_SLEEP
4059 tp->pm_notifier.notifier_call = rtl_notifier;
4060 register_pm_notifier(&tp->pm_notifier);
4061#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08004062 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004063
Guenter Roeckca0a7532016-11-09 19:51:25 -08004064out_unlock:
4065 mutex_unlock(&tp->control);
4066 usb_autopm_put_interface(tp->intf);
4067out_free:
4068 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08004069out:
hayeswangac718b62013-05-02 16:01:25 +00004070 return res;
4071}
4072
4073static int rtl8152_close(struct net_device *netdev)
4074{
4075 struct r8152 *tp = netdev_priv(netdev);
4076 int res = 0;
4077
hayeswang5ee3c602016-01-07 17:12:17 +08004078#ifdef CONFIG_PM_SLEEP
4079 unregister_pm_notifier(&tp->pm_notifier);
4080#endif
Jiri Slaby0ee1f472018-06-25 09:26:27 +02004081 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
4082 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004083 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08004084 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00004085 cancel_delayed_work_sync(&tp->schedule);
4086 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004087
4088 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004089 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004090 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004091 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004092 } else {
hayeswangb5403272014-10-09 18:00:26 +08004093 mutex_lock(&tp->control);
4094
hayeswang9a4be1b2014-02-18 21:49:07 +08004095 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004096
4097 mutex_unlock(&tp->control);
4098
hayeswang9a4be1b2014-02-18 21:49:07 +08004099 usb_autopm_put_interface(tp->intf);
4100 }
hayeswangac718b62013-05-02 16:01:25 +00004101
hayeswang7e9da482014-02-18 21:49:05 +08004102 free_all_mem(tp);
4103
hayeswangac718b62013-05-02 16:01:25 +00004104 return res;
4105}
4106
hayeswang4f1d4d52014-03-11 16:24:19 +08004107static void rtl_tally_reset(struct r8152 *tp)
4108{
4109 u32 ocp_data;
4110
4111 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4112 ocp_data |= TALLY_RESET;
4113 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4114}
4115
hayeswangac718b62013-05-02 16:01:25 +00004116static void r8152b_init(struct r8152 *tp)
4117{
hayeswangebc2ec482013-08-14 20:54:38 +08004118 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004119 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004120
hayeswang68714382014-04-11 17:54:31 +08004121 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4122 return;
4123
hayeswang2dd436d2016-09-20 16:22:06 +08004124 data = r8152_mdio_read(tp, MII_BMCR);
4125 if (data & BMCR_PDOWN) {
4126 data &= ~BMCR_PDOWN;
4127 r8152_mdio_write(tp, MII_BMCR, data);
4128 }
4129
hayeswangcda9fb02016-01-07 17:51:12 +08004130 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004131
hayeswangac718b62013-05-02 16:01:25 +00004132 if (tp->version == RTL_VER_01) {
4133 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4134 ocp_data &= ~LED_MODE_MASK;
4135 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4136 }
4137
hayeswang00a5e362014-02-18 21:48:59 +08004138 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004139
hayeswangac718b62013-05-02 16:01:25 +00004140 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4141 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4142 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4143 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4144 ocp_data &= ~MCU_CLK_RATIO_MASK;
4145 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4146 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4147 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4148 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4149 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4150
hayeswang4f1d4d52014-03-11 16:24:19 +08004151 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004152
hayeswangebc2ec482013-08-14 20:54:38 +08004153 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004154 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004155 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004156 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4157}
4158
hayeswang43779f82014-01-02 11:25:10 +08004159static void r8153_init(struct r8152 *tp)
4160{
4161 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004162 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004163 int i;
4164
hayeswang68714382014-04-11 17:54:31 +08004165 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4166 return;
4167
hayeswangb9702722014-02-18 21:49:00 +08004168 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004169
4170 for (i = 0; i < 500; i++) {
4171 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4172 AUTOLOAD_DONE)
4173 break;
4174 msleep(20);
4175 }
4176
hayeswangc564b872017-06-09 17:11:38 +08004177 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004178
hayeswang2dd436d2016-09-20 16:22:06 +08004179 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4180 tp->version == RTL_VER_05)
4181 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4182
4183 data = r8152_mdio_read(tp, MII_BMCR);
4184 if (data & BMCR_PDOWN) {
4185 data &= ~BMCR_PDOWN;
4186 r8152_mdio_write(tp, MII_BMCR, data);
4187 }
4188
hayeswangc564b872017-06-09 17:11:38 +08004189 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004190
hayeswangb9702722014-02-18 21:49:00 +08004191 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004192
hayeswang65bab842015-02-12 16:20:46 +08004193 if (tp->version == RTL_VER_04) {
4194 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4195 ocp_data &= ~pwd_dn_scale_mask;
4196 ocp_data |= pwd_dn_scale(96);
4197 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4198
4199 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4200 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4201 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4202 } else if (tp->version == RTL_VER_05) {
4203 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4204 ocp_data &= ~ECM_ALDPS;
4205 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4206
4207 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4208 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4209 ocp_data &= ~DYNAMIC_BURST;
4210 else
4211 ocp_data |= DYNAMIC_BURST;
4212 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004213 } else if (tp->version == RTL_VER_06) {
4214 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4215 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4216 ocp_data &= ~DYNAMIC_BURST;
4217 else
4218 ocp_data |= DYNAMIC_BURST;
4219 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004220 }
4221
4222 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4223 ocp_data |= EP4_FULL_FC;
4224 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4225
hayeswang43779f82014-01-02 11:25:10 +08004226 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4227 ocp_data &= ~TIMER11_EN;
4228 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4229
hayeswang43779f82014-01-02 11:25:10 +08004230 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4231 ocp_data &= ~LED_MODE_MASK;
4232 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4233
hayeswang65bab842015-02-12 16:20:46 +08004234 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004235 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004236 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004237 else
4238 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004239 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4240
4241 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4242 ocp_data &= ~SEN_VAL_MASK;
4243 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4244 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4245
hayeswang65bab842015-02-12 16:20:46 +08004246 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4247
hayeswangb9702722014-02-18 21:49:00 +08004248 r8153_power_cut_en(tp, false);
4249 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004250 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004251 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004252
hayeswange31f6362017-06-09 17:11:41 +08004253 /* rx aggregation */
4254 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4255 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004256 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4257 ocp_data |= RX_AGG_DISABLE;
4258
hayeswange31f6362017-06-09 17:11:41 +08004259 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004260
hayeswang4f1d4d52014-03-11 16:24:19 +08004261 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004262
4263 switch (tp->udev->speed) {
4264 case USB_SPEED_SUPER:
4265 case USB_SPEED_SUPER_PLUS:
4266 tp->coalesce = COALESCE_SUPER;
4267 break;
4268 case USB_SPEED_HIGH:
4269 tp->coalesce = COALESCE_HIGH;
4270 break;
4271 default:
4272 tp->coalesce = COALESCE_SLOW;
4273 break;
4274 }
hayeswang43779f82014-01-02 11:25:10 +08004275}
4276
hayeswang65b82d62017-06-15 14:44:03 +08004277static void r8153b_init(struct r8152 *tp)
4278{
4279 u32 ocp_data;
4280 u16 data;
4281 int i;
4282
4283 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4284 return;
4285
4286 r8153b_u1u2en(tp, false);
4287
4288 for (i = 0; i < 500; i++) {
4289 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4290 AUTOLOAD_DONE)
4291 break;
4292 msleep(20);
4293 }
4294
4295 data = r8153_phy_status(tp, 0);
4296
4297 data = r8152_mdio_read(tp, MII_BMCR);
4298 if (data & BMCR_PDOWN) {
4299 data &= ~BMCR_PDOWN;
4300 r8152_mdio_write(tp, MII_BMCR, data);
4301 }
4302
4303 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4304
4305 r8153_u2p3en(tp, false);
4306
4307 /* MSC timer = 0xfff * 8ms = 32760 ms */
4308 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4309
4310 /* U1/U2/L1 idle timer. 500 us */
4311 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4312
4313 r8153b_power_cut_en(tp, false);
4314 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08004315 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004316 rtl_runtime_suspend_enable(tp, false);
4317 r8153b_u1u2en(tp, true);
4318 usb_enable_lpm(tp->udev);
4319
4320 /* MAC clock speed down */
4321 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4322 ocp_data |= MAC_CLK_SPDWN_EN;
4323 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4324
4325 set_bit(GREEN_ETHERNET, &tp->flags);
4326
4327 /* rx aggregation */
4328 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4329 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4330 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4331
4332 rtl_tally_reset(tp);
4333
4334 tp->coalesce = 15000; /* 15 us */
4335}
4336
hayeswange5011392015-07-29 20:39:08 +08004337static int rtl8152_pre_reset(struct usb_interface *intf)
4338{
4339 struct r8152 *tp = usb_get_intfdata(intf);
4340 struct net_device *netdev;
4341
4342 if (!tp)
4343 return 0;
4344
4345 netdev = tp->netdev;
4346 if (!netif_running(netdev))
4347 return 0;
4348
hayeswangde9bf292017-01-26 09:38:32 +08004349 netif_stop_queue(netdev);
hayeswange5011392015-07-29 20:39:08 +08004350 napi_disable(&tp->napi);
4351 clear_bit(WORK_ENABLE, &tp->flags);
4352 usb_kill_urb(tp->intr_urb);
4353 cancel_delayed_work_sync(&tp->schedule);
4354 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004355 mutex_lock(&tp->control);
4356 tp->rtl_ops.disable(tp);
4357 mutex_unlock(&tp->control);
4358 }
4359
4360 return 0;
4361}
4362
4363static int rtl8152_post_reset(struct usb_interface *intf)
4364{
4365 struct r8152 *tp = usb_get_intfdata(intf);
4366 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004367 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004368
4369 if (!tp)
4370 return 0;
4371
Mario Limonciello25766272019-04-04 13:46:53 -05004372 /* reset the MAC adddress in case of policy change */
4373 if (determine_ethernet_addr(tp, &sa) >= 0) {
4374 rtnl_lock();
4375 dev_set_mac_address (tp->netdev, &sa, NULL);
4376 rtnl_unlock();
4377 }
4378
hayeswange5011392015-07-29 20:39:08 +08004379 netdev = tp->netdev;
4380 if (!netif_running(netdev))
4381 return 0;
4382
4383 set_bit(WORK_ENABLE, &tp->flags);
4384 if (netif_carrier_ok(netdev)) {
4385 mutex_lock(&tp->control);
4386 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004387 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004388 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004389 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004390 }
4391
4392 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004393 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004394 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004395
hayeswang7489bda2017-01-26 09:38:34 +08004396 if (!list_empty(&tp->rx_done))
4397 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004398
4399 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004400}
4401
hayeswang2dd49e02015-09-07 11:57:44 +08004402static bool delay_autosuspend(struct r8152 *tp)
4403{
4404 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4405 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4406
4407 /* This means a linking change occurs and the driver doesn't detect it,
4408 * yet. If the driver has disabled tx/rx and hw is linking on, the
4409 * device wouldn't wake up by receiving any packet.
4410 */
4411 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4412 return true;
4413
4414 /* If the linking down is occurred by nway, the device may miss the
4415 * linking change event. And it wouldn't wake when linking on.
4416 */
4417 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4418 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004419 else if (!skb_queue_empty(&tp->tx_queue))
4420 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004421 else
4422 return false;
4423}
4424
hayeswang21cbd0e2017-06-13 15:14:39 +08004425static int rtl8152_runtime_resume(struct r8152 *tp)
4426{
4427 struct net_device *netdev = tp->netdev;
4428
4429 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4430 struct napi_struct *napi = &tp->napi;
4431
4432 tp->rtl_ops.autosuspend_en(tp, false);
4433 napi_disable(napi);
4434 set_bit(WORK_ENABLE, &tp->flags);
4435
4436 if (netif_carrier_ok(netdev)) {
4437 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4438 rtl_start_rx(tp);
4439 } else {
4440 netif_carrier_off(netdev);
4441 tp->rtl_ops.disable(tp);
4442 netif_info(tp, link, netdev, "linking down\n");
4443 }
4444 }
4445
4446 napi_enable(napi);
4447 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4448 smp_mb__after_atomic();
4449
4450 if (!list_empty(&tp->rx_done))
4451 napi_schedule(&tp->napi);
4452
4453 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4454 } else {
4455 if (netdev->flags & IFF_UP)
4456 tp->rtl_ops.autosuspend_en(tp, false);
4457
4458 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4459 }
4460
4461 return 0;
4462}
4463
4464static int rtl8152_system_resume(struct r8152 *tp)
4465{
4466 struct net_device *netdev = tp->netdev;
4467
4468 netif_device_attach(netdev);
4469
4470 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4471 tp->rtl_ops.up(tp);
4472 netif_carrier_off(netdev);
4473 set_bit(WORK_ENABLE, &tp->flags);
4474 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4475 }
4476
4477 return 0;
4478}
4479
hayeswanga9c54ad2017-01-25 13:41:45 +08004480static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004481{
hayeswang6cc69f22014-10-17 16:55:08 +08004482 struct net_device *netdev = tp->netdev;
4483 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004484
hayeswang26afec32017-01-26 09:38:31 +08004485 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4486 smp_mb__after_atomic();
4487
hayeswang8fb28062017-01-10 17:04:06 +08004488 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004489 u32 rcr = 0;
4490
hayeswang75dc6922017-01-10 17:04:07 +08004491 if (netif_carrier_ok(netdev)) {
4492 u32 ocp_data;
4493
4494 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4495 ocp_data = rcr & ~RCR_ACPT_ALL;
4496 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4497 rxdy_gated_en(tp, true);
4498 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4499 PLA_OOB_CTRL);
4500 if (!(ocp_data & RXFIFO_EMPTY)) {
4501 rxdy_gated_en(tp, false);
4502 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004503 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4504 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004505 ret = -EBUSY;
4506 goto out1;
4507 }
4508 }
4509
hayeswang8fb28062017-01-10 17:04:06 +08004510 clear_bit(WORK_ENABLE, &tp->flags);
4511 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004512
hayeswang8fb28062017-01-10 17:04:06 +08004513 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004514
4515 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004516 struct napi_struct *napi = &tp->napi;
4517
4518 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004519 rtl_stop_rx(tp);
4520 rxdy_gated_en(tp, false);
4521 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004522 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004523 }
hayeswangbd882982017-06-13 15:14:40 +08004524
4525 if (delay_autosuspend(tp)) {
4526 rtl8152_runtime_resume(tp);
4527 ret = -EBUSY;
4528 }
hayeswang6cc69f22014-10-17 16:55:08 +08004529 }
4530
hayeswang8fb28062017-01-10 17:04:06 +08004531out1:
4532 return ret;
4533}
4534
4535static int rtl8152_system_suspend(struct r8152 *tp)
4536{
4537 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004538
4539 netif_device_detach(netdev);
4540
hayeswange3bd1a82014-10-29 11:12:17 +08004541 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004542 struct napi_struct *napi = &tp->napi;
4543
hayeswangac718b62013-05-02 16:01:25 +00004544 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004545 usb_kill_urb(tp->intr_urb);
hayeswangce594e92017-03-16 14:32:22 +08004546 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004547 cancel_delayed_work_sync(&tp->schedule);
4548 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004549 napi_enable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004550 }
hayeswang8fb28062017-01-10 17:04:06 +08004551
zhong jiangf7419172018-08-09 09:39:13 +08004552 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004553}
4554
4555static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4556{
4557 struct r8152 *tp = usb_get_intfdata(intf);
4558 int ret;
4559
4560 mutex_lock(&tp->control);
4561
4562 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004563 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004564 else
4565 ret = rtl8152_system_suspend(tp);
4566
hayeswangb5403272014-10-09 18:00:26 +08004567 mutex_unlock(&tp->control);
4568
hayeswang6cc69f22014-10-17 16:55:08 +08004569 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004570}
4571
4572static int rtl8152_resume(struct usb_interface *intf)
4573{
4574 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004575 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004576
hayeswangb5403272014-10-09 18:00:26 +08004577 mutex_lock(&tp->control);
4578
hayeswang21cbd0e2017-06-13 15:14:39 +08004579 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4580 ret = rtl8152_runtime_resume(tp);
4581 else
4582 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004583
hayeswangb5403272014-10-09 18:00:26 +08004584 mutex_unlock(&tp->control);
4585
hayeswang21cbd0e2017-06-13 15:14:39 +08004586 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004587}
4588
hayeswang7ec25412016-01-04 14:38:46 +08004589static int rtl8152_reset_resume(struct usb_interface *intf)
4590{
4591 struct r8152 *tp = usb_get_intfdata(intf);
4592
4593 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004594 mutex_lock(&tp->control);
4595 tp->rtl_ops.init(tp);
4596 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4597 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004598 return rtl8152_resume(intf);
4599}
4600
hayeswang21ff2e82014-02-18 21:49:06 +08004601static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4602{
4603 struct r8152 *tp = netdev_priv(dev);
4604
hayeswang9a4be1b2014-02-18 21:49:07 +08004605 if (usb_autopm_get_interface(tp->intf) < 0)
4606 return;
4607
hayeswang7daed8d2015-07-24 13:54:24 +08004608 if (!rtl_can_wakeup(tp)) {
4609 wol->supported = 0;
4610 wol->wolopts = 0;
4611 } else {
4612 mutex_lock(&tp->control);
4613 wol->supported = WAKE_ANY;
4614 wol->wolopts = __rtl_get_wol(tp);
4615 mutex_unlock(&tp->control);
4616 }
hayeswangb5403272014-10-09 18:00:26 +08004617
hayeswang9a4be1b2014-02-18 21:49:07 +08004618 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004619}
4620
4621static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4622{
4623 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004624 int ret;
4625
hayeswang7daed8d2015-07-24 13:54:24 +08004626 if (!rtl_can_wakeup(tp))
4627 return -EOPNOTSUPP;
4628
Florian Fainellif2750df2018-09-28 16:18:54 -07004629 if (wol->wolopts & ~WAKE_ANY)
4630 return -EINVAL;
4631
hayeswang9a4be1b2014-02-18 21:49:07 +08004632 ret = usb_autopm_get_interface(tp->intf);
4633 if (ret < 0)
4634 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004635
hayeswangb5403272014-10-09 18:00:26 +08004636 mutex_lock(&tp->control);
4637
hayeswang21ff2e82014-02-18 21:49:06 +08004638 __rtl_set_wol(tp, wol->wolopts);
4639 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4640
hayeswangb5403272014-10-09 18:00:26 +08004641 mutex_unlock(&tp->control);
4642
hayeswang9a4be1b2014-02-18 21:49:07 +08004643 usb_autopm_put_interface(tp->intf);
4644
4645out_set_wol:
4646 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004647}
4648
hayeswanga5ec27c2014-02-18 21:49:11 +08004649static u32 rtl8152_get_msglevel(struct net_device *dev)
4650{
4651 struct r8152 *tp = netdev_priv(dev);
4652
4653 return tp->msg_enable;
4654}
4655
4656static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4657{
4658 struct r8152 *tp = netdev_priv(dev);
4659
4660 tp->msg_enable = value;
4661}
4662
hayeswangac718b62013-05-02 16:01:25 +00004663static void rtl8152_get_drvinfo(struct net_device *netdev,
4664 struct ethtool_drvinfo *info)
4665{
4666 struct r8152 *tp = netdev_priv(netdev);
4667
hayeswangb0b46c72014-08-26 10:08:23 +08004668 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4669 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004670 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4671}
4672
4673static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004674int rtl8152_get_link_ksettings(struct net_device *netdev,
4675 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004676{
4677 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004678 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004679
4680 if (!tp->mii.mdio_read)
4681 return -EOPNOTSUPP;
4682
hayeswang8d4a4d72014-10-09 18:00:25 +08004683 ret = usb_autopm_get_interface(tp->intf);
4684 if (ret < 0)
4685 goto out;
4686
hayeswangb5403272014-10-09 18:00:26 +08004687 mutex_lock(&tp->control);
4688
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004689 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004690
hayeswangb5403272014-10-09 18:00:26 +08004691 mutex_unlock(&tp->control);
4692
hayeswang8d4a4d72014-10-09 18:00:25 +08004693 usb_autopm_put_interface(tp->intf);
4694
4695out:
4696 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004697}
4698
Philippe Reynes06144dc2017-03-12 22:41:58 +01004699static int rtl8152_set_link_ksettings(struct net_device *dev,
4700 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004701{
4702 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004703 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004704
hayeswang9a4be1b2014-02-18 21:49:07 +08004705 ret = usb_autopm_get_interface(tp->intf);
4706 if (ret < 0)
4707 goto out;
4708
hayeswangb5403272014-10-09 18:00:26 +08004709 mutex_lock(&tp->control);
4710
Philippe Reynes06144dc2017-03-12 22:41:58 +01004711 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4712 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004713 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004714 tp->autoneg = cmd->base.autoneg;
4715 tp->speed = cmd->base.speed;
4716 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004717 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004718
hayeswangb5403272014-10-09 18:00:26 +08004719 mutex_unlock(&tp->control);
4720
hayeswang9a4be1b2014-02-18 21:49:07 +08004721 usb_autopm_put_interface(tp->intf);
4722
4723out:
4724 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004725}
4726
hayeswang4f1d4d52014-03-11 16:24:19 +08004727static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4728 "tx_packets",
4729 "rx_packets",
4730 "tx_errors",
4731 "rx_errors",
4732 "rx_missed",
4733 "align_errors",
4734 "tx_single_collisions",
4735 "tx_multi_collisions",
4736 "rx_unicast",
4737 "rx_broadcast",
4738 "rx_multicast",
4739 "tx_aborted",
4740 "tx_underrun",
4741};
4742
4743static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4744{
4745 switch (sset) {
4746 case ETH_SS_STATS:
4747 return ARRAY_SIZE(rtl8152_gstrings);
4748 default:
4749 return -EOPNOTSUPP;
4750 }
4751}
4752
4753static void rtl8152_get_ethtool_stats(struct net_device *dev,
4754 struct ethtool_stats *stats, u64 *data)
4755{
4756 struct r8152 *tp = netdev_priv(dev);
4757 struct tally_counter tally;
4758
hayeswang0b030242014-07-08 14:49:28 +08004759 if (usb_autopm_get_interface(tp->intf) < 0)
4760 return;
4761
hayeswang4f1d4d52014-03-11 16:24:19 +08004762 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4763
hayeswang0b030242014-07-08 14:49:28 +08004764 usb_autopm_put_interface(tp->intf);
4765
hayeswang4f1d4d52014-03-11 16:24:19 +08004766 data[0] = le64_to_cpu(tally.tx_packets);
4767 data[1] = le64_to_cpu(tally.rx_packets);
4768 data[2] = le64_to_cpu(tally.tx_errors);
4769 data[3] = le32_to_cpu(tally.rx_errors);
4770 data[4] = le16_to_cpu(tally.rx_missed);
4771 data[5] = le16_to_cpu(tally.align_errors);
4772 data[6] = le32_to_cpu(tally.tx_one_collision);
4773 data[7] = le32_to_cpu(tally.tx_multi_collision);
4774 data[8] = le64_to_cpu(tally.rx_unicast);
4775 data[9] = le64_to_cpu(tally.rx_broadcast);
4776 data[10] = le32_to_cpu(tally.rx_multicast);
4777 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004778 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004779}
4780
4781static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4782{
4783 switch (stringset) {
4784 case ETH_SS_STATS:
4785 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4786 break;
4787 }
4788}
4789
hayeswangdf35d282014-09-25 20:54:02 +08004790static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4791{
4792 u32 ocp_data, lp, adv, supported = 0;
4793 u16 val;
4794
4795 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4796 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4797
4798 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4799 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4800
4801 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4802 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4803
4804 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4805 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4806
4807 eee->eee_enabled = !!ocp_data;
4808 eee->eee_active = !!(supported & adv & lp);
4809 eee->supported = supported;
4810 eee->advertised = adv;
4811 eee->lp_advertised = lp;
4812
4813 return 0;
4814}
4815
4816static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4817{
4818 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4819
4820 r8152_eee_en(tp, eee->eee_enabled);
4821
4822 if (!eee->eee_enabled)
4823 val = 0;
4824
4825 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4826
4827 return 0;
4828}
4829
4830static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4831{
4832 u32 ocp_data, lp, adv, supported = 0;
4833 u16 val;
4834
4835 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4836 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4837
4838 val = ocp_reg_read(tp, OCP_EEE_ADV);
4839 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4840
4841 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4842 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4843
4844 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4845 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4846
4847 eee->eee_enabled = !!ocp_data;
4848 eee->eee_active = !!(supported & adv & lp);
4849 eee->supported = supported;
4850 eee->advertised = adv;
4851 eee->lp_advertised = lp;
4852
4853 return 0;
4854}
4855
4856static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4857{
4858 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4859
4860 r8153_eee_en(tp, eee->eee_enabled);
4861
4862 if (!eee->eee_enabled)
4863 val = 0;
4864
4865 ocp_reg_write(tp, OCP_EEE_ADV, val);
4866
4867 return 0;
4868}
4869
hayeswang65b82d62017-06-15 14:44:03 +08004870static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4871{
4872 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4873
4874 r8153b_eee_en(tp, eee->eee_enabled);
4875
4876 if (!eee->eee_enabled)
4877 val = 0;
4878
4879 ocp_reg_write(tp, OCP_EEE_ADV, val);
4880
4881 return 0;
4882}
4883
hayeswangdf35d282014-09-25 20:54:02 +08004884static int
4885rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4886{
4887 struct r8152 *tp = netdev_priv(net);
4888 int ret;
4889
4890 ret = usb_autopm_get_interface(tp->intf);
4891 if (ret < 0)
4892 goto out;
4893
hayeswangb5403272014-10-09 18:00:26 +08004894 mutex_lock(&tp->control);
4895
hayeswangdf35d282014-09-25 20:54:02 +08004896 ret = tp->rtl_ops.eee_get(tp, edata);
4897
hayeswangb5403272014-10-09 18:00:26 +08004898 mutex_unlock(&tp->control);
4899
hayeswangdf35d282014-09-25 20:54:02 +08004900 usb_autopm_put_interface(tp->intf);
4901
4902out:
4903 return ret;
4904}
4905
4906static int
4907rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
4908{
4909 struct r8152 *tp = netdev_priv(net);
4910 int ret;
4911
4912 ret = usb_autopm_get_interface(tp->intf);
4913 if (ret < 0)
4914 goto out;
4915
hayeswangb5403272014-10-09 18:00:26 +08004916 mutex_lock(&tp->control);
4917
hayeswangdf35d282014-09-25 20:54:02 +08004918 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08004919 if (!ret)
4920 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08004921
hayeswangb5403272014-10-09 18:00:26 +08004922 mutex_unlock(&tp->control);
4923
hayeswangdf35d282014-09-25 20:54:02 +08004924 usb_autopm_put_interface(tp->intf);
4925
4926out:
4927 return ret;
4928}
4929
hayeswang8884f502014-10-28 14:05:52 +08004930static int rtl8152_nway_reset(struct net_device *dev)
4931{
4932 struct r8152 *tp = netdev_priv(dev);
4933 int ret;
4934
4935 ret = usb_autopm_get_interface(tp->intf);
4936 if (ret < 0)
4937 goto out;
4938
4939 mutex_lock(&tp->control);
4940
4941 ret = mii_nway_restart(&tp->mii);
4942
4943 mutex_unlock(&tp->control);
4944
4945 usb_autopm_put_interface(tp->intf);
4946
4947out:
4948 return ret;
4949}
4950
hayeswangefb3dd82015-02-12 14:33:48 +08004951static int rtl8152_get_coalesce(struct net_device *netdev,
4952 struct ethtool_coalesce *coalesce)
4953{
4954 struct r8152 *tp = netdev_priv(netdev);
4955
4956 switch (tp->version) {
4957 case RTL_VER_01:
4958 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004959 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004960 return -EOPNOTSUPP;
4961 default:
4962 break;
4963 }
4964
4965 coalesce->rx_coalesce_usecs = tp->coalesce;
4966
4967 return 0;
4968}
4969
4970static int rtl8152_set_coalesce(struct net_device *netdev,
4971 struct ethtool_coalesce *coalesce)
4972{
4973 struct r8152 *tp = netdev_priv(netdev);
4974 int ret;
4975
4976 switch (tp->version) {
4977 case RTL_VER_01:
4978 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004979 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004980 return -EOPNOTSUPP;
4981 default:
4982 break;
4983 }
4984
4985 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
4986 return -EINVAL;
4987
4988 ret = usb_autopm_get_interface(tp->intf);
4989 if (ret < 0)
4990 return ret;
4991
4992 mutex_lock(&tp->control);
4993
4994 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
4995 tp->coalesce = coalesce->rx_coalesce_usecs;
4996
Hayes Wang9fae5412019-07-03 15:11:56 +08004997 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
4998 netif_stop_queue(netdev);
4999 napi_disable(&tp->napi);
5000 tp->rtl_ops.disable(tp);
5001 tp->rtl_ops.enable(tp);
5002 rtl_start_rx(tp);
5003 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5004 _rtl8152_set_rx_mode(netdev);
5005 napi_enable(&tp->napi);
5006 netif_wake_queue(netdev);
5007 }
hayeswangefb3dd82015-02-12 14:33:48 +08005008 }
5009
5010 mutex_unlock(&tp->control);
5011
5012 usb_autopm_put_interface(tp->intf);
5013
5014 return ret;
5015}
5016
Julia Lawall407a4712016-09-01 00:21:22 +02005017static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00005018 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00005019 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08005020 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08005021 .get_msglevel = rtl8152_get_msglevel,
5022 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08005023 .get_wol = rtl8152_get_wol,
5024 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08005025 .get_strings = rtl8152_get_strings,
5026 .get_sset_count = rtl8152_get_sset_count,
5027 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08005028 .get_coalesce = rtl8152_get_coalesce,
5029 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08005030 .get_eee = rtl_ethtool_get_eee,
5031 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01005032 .get_link_ksettings = rtl8152_get_link_ksettings,
5033 .set_link_ksettings = rtl8152_set_link_ksettings,
hayeswangac718b62013-05-02 16:01:25 +00005034};
5035
5036static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
5037{
5038 struct r8152 *tp = netdev_priv(netdev);
5039 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08005040 int res;
5041
hayeswang68714382014-04-11 17:54:31 +08005042 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5043 return -ENODEV;
5044
hayeswang9a4be1b2014-02-18 21:49:07 +08005045 res = usb_autopm_get_interface(tp->intf);
5046 if (res < 0)
5047 goto out;
hayeswangac718b62013-05-02 16:01:25 +00005048
5049 switch (cmd) {
5050 case SIOCGMIIPHY:
5051 data->phy_id = R8152_PHY_ID; /* Internal PHY */
5052 break;
5053
5054 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08005055 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005056 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08005057 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005058 break;
5059
5060 case SIOCSMIIREG:
5061 if (!capable(CAP_NET_ADMIN)) {
5062 res = -EPERM;
5063 break;
5064 }
hayeswangb5403272014-10-09 18:00:26 +08005065 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005066 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08005067 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005068 break;
5069
5070 default:
5071 res = -EOPNOTSUPP;
5072 }
5073
hayeswang9a4be1b2014-02-18 21:49:07 +08005074 usb_autopm_put_interface(tp->intf);
5075
5076out:
hayeswangac718b62013-05-02 16:01:25 +00005077 return res;
5078}
5079
hayeswang69b4b7a2014-07-10 10:58:54 +08005080static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5081{
5082 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08005083 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005084
5085 switch (tp->version) {
5086 case RTL_VER_01:
5087 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005088 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04005089 dev->mtu = new_mtu;
5090 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08005091 default:
5092 break;
5093 }
5094
hayeswang396e2e22015-02-12 14:33:47 +08005095 ret = usb_autopm_get_interface(tp->intf);
5096 if (ret < 0)
5097 return ret;
5098
5099 mutex_lock(&tp->control);
5100
hayeswang69b4b7a2014-07-10 10:58:54 +08005101 dev->mtu = new_mtu;
5102
hayeswang210c4f72017-03-20 16:13:44 +08005103 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005104 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005105
5106 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5107
5108 if (netif_carrier_ok(dev))
5109 r8153_set_rx_early_size(tp);
5110 }
hayeswang396e2e22015-02-12 14:33:47 +08005111
5112 mutex_unlock(&tp->control);
5113
5114 usb_autopm_put_interface(tp->intf);
5115
5116 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005117}
5118
hayeswangac718b62013-05-02 16:01:25 +00005119static const struct net_device_ops rtl8152_netdev_ops = {
5120 .ndo_open = rtl8152_open,
5121 .ndo_stop = rtl8152_close,
5122 .ndo_do_ioctl = rtl8152_ioctl,
5123 .ndo_start_xmit = rtl8152_start_xmit,
5124 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005125 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005126 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5127 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005128 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005129 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005130 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005131};
5132
hayeswange3fe0b12014-01-02 11:22:39 +08005133static void rtl8152_unload(struct r8152 *tp)
5134{
hayeswang68714382014-04-11 17:54:31 +08005135 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5136 return;
5137
hayeswang00a5e362014-02-18 21:48:59 +08005138 if (tp->version != RTL_VER_01)
5139 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005140}
5141
hayeswang43779f82014-01-02 11:25:10 +08005142static void rtl8153_unload(struct r8152 *tp)
5143{
hayeswang68714382014-04-11 17:54:31 +08005144 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5145 return;
5146
hayeswang49be1722014-10-01 13:25:11 +08005147 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005148}
5149
hayeswang65b82d62017-06-15 14:44:03 +08005150static void rtl8153b_unload(struct r8152 *tp)
5151{
5152 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5153 return;
5154
5155 r8153b_power_cut_en(tp, false);
5156}
5157
hayeswang55b65472014-11-06 12:47:39 +08005158static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005159{
5160 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005161 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005162
hayeswang55b65472014-11-06 12:47:39 +08005163 switch (tp->version) {
5164 case RTL_VER_01:
5165 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005166 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005167 ops->init = r8152b_init;
5168 ops->enable = rtl8152_enable;
5169 ops->disable = rtl8152_disable;
5170 ops->up = rtl8152_up;
5171 ops->down = rtl8152_down;
5172 ops->unload = rtl8152_unload;
5173 ops->eee_get = r8152_get_eee;
5174 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005175 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005176 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005177 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005178 tp->rx_buf_sz = 16 * 1024;
hayeswang43779f82014-01-02 11:25:10 +08005179 break;
5180
hayeswang55b65472014-11-06 12:47:39 +08005181 case RTL_VER_03:
5182 case RTL_VER_04:
5183 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005184 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005185 ops->init = r8153_init;
5186 ops->enable = rtl8153_enable;
5187 ops->disable = rtl8153_disable;
5188 ops->up = rtl8153_up;
5189 ops->down = rtl8153_down;
5190 ops->unload = rtl8153_unload;
5191 ops->eee_get = r8153_get_eee;
5192 ops->eee_set = r8153_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005193 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005194 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005195 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005196 tp->rx_buf_sz = 32 * 1024;
hayeswangc81229c2014-01-02 11:22:42 +08005197 break;
5198
hayeswang65b82d62017-06-15 14:44:03 +08005199 case RTL_VER_08:
5200 case RTL_VER_09:
5201 ops->init = r8153b_init;
5202 ops->enable = rtl8153_enable;
5203 ops->disable = rtl8153b_disable;
5204 ops->up = rtl8153b_up;
5205 ops->down = rtl8153b_down;
5206 ops->unload = rtl8153b_unload;
5207 ops->eee_get = r8153_get_eee;
5208 ops->eee_set = r8153b_set_eee;
5209 ops->in_nway = rtl8153_in_nway;
5210 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5211 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005212 tp->rx_buf_sz = 32 * 1024;
hayeswang65b82d62017-06-15 14:44:03 +08005213 break;
5214
hayeswangc81229c2014-01-02 11:22:42 +08005215 default:
hayeswang55b65472014-11-06 12:47:39 +08005216 ret = -ENODEV;
5217 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005218 break;
5219 }
5220
5221 return ret;
5222}
5223
hayeswang33928ee2017-03-17 11:20:13 +08005224static u8 rtl_get_version(struct usb_interface *intf)
5225{
5226 struct usb_device *udev = interface_to_usbdev(intf);
5227 u32 ocp_data = 0;
5228 __le32 *tmp;
5229 u8 version;
5230 int ret;
5231
5232 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5233 if (!tmp)
5234 return 0;
5235
5236 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5237 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5238 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5239 if (ret > 0)
5240 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5241
5242 kfree(tmp);
5243
5244 switch (ocp_data) {
5245 case 0x4c00:
5246 version = RTL_VER_01;
5247 break;
5248 case 0x4c10:
5249 version = RTL_VER_02;
5250 break;
5251 case 0x5c00:
5252 version = RTL_VER_03;
5253 break;
5254 case 0x5c10:
5255 version = RTL_VER_04;
5256 break;
5257 case 0x5c20:
5258 version = RTL_VER_05;
5259 break;
5260 case 0x5c30:
5261 version = RTL_VER_06;
5262 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005263 case 0x4800:
5264 version = RTL_VER_07;
5265 break;
hayeswang65b82d62017-06-15 14:44:03 +08005266 case 0x6000:
5267 version = RTL_VER_08;
5268 break;
5269 case 0x6010:
5270 version = RTL_VER_09;
5271 break;
hayeswang33928ee2017-03-17 11:20:13 +08005272 default:
5273 version = RTL_VER_UNKNOWN;
5274 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5275 break;
5276 }
5277
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005278 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5279
hayeswang33928ee2017-03-17 11:20:13 +08005280 return version;
5281}
5282
hayeswangac718b62013-05-02 16:01:25 +00005283static int rtl8152_probe(struct usb_interface *intf,
5284 const struct usb_device_id *id)
5285{
5286 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005287 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005288 struct r8152 *tp;
5289 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005290 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005291
hayeswang33928ee2017-03-17 11:20:13 +08005292 if (version == RTL_VER_UNKNOWN)
5293 return -ENODEV;
5294
hayeswang10c32712014-03-04 20:47:48 +08005295 if (udev->actconfig->desc.bConfigurationValue != 1) {
5296 usb_driver_set_configuration(udev, 1);
5297 return -ENODEV;
5298 }
5299
5300 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005301 netdev = alloc_etherdev(sizeof(struct r8152));
5302 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005303 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005304 return -ENOMEM;
5305 }
5306
hayeswangebc2ec482013-08-14 20:54:38 +08005307 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005308 tp = netdev_priv(netdev);
5309 tp->msg_enable = 0x7FFF;
5310
hayeswange3ad4122014-01-06 17:08:42 +08005311 tp->udev = udev;
5312 tp->netdev = netdev;
5313 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005314 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005315
hayeswang33928ee2017-03-17 11:20:13 +08005316 switch (version) {
5317 case RTL_VER_01:
5318 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005319 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005320 tp->mii.supports_gmii = 0;
5321 break;
5322 default:
5323 tp->mii.supports_gmii = 1;
5324 break;
5325 }
5326
hayeswang55b65472014-11-06 12:47:39 +08005327 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005328 if (ret)
5329 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005330
hayeswangb5403272014-10-09 18:00:26 +08005331 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005332 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005333 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
hayeswangac718b62013-05-02 16:01:25 +00005334
hayeswangac718b62013-05-02 16:01:25 +00005335 netdev->netdev_ops = &rtl8152_netdev_ops;
5336 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005337
hayeswang60c89072014-03-07 11:04:39 +08005338 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005339 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005340 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5341 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005342 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005343 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005344 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005345 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005346 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5347 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5348 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005349
hayeswang19c0f402017-01-11 16:25:34 +08005350 if (tp->version == RTL_VER_01) {
5351 netdev->features &= ~NETIF_F_RXCSUM;
5352 netdev->hw_features &= ~NETIF_F_RXCSUM;
5353 }
5354
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005355 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5356 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005357 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5358 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5359 }
5360
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005361 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005362 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005363
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005364 /* MTU range: 68 - 1500 or 9194 */
5365 netdev->min_mtu = ETH_MIN_MTU;
5366 switch (tp->version) {
5367 case RTL_VER_01:
5368 case RTL_VER_02:
5369 netdev->max_mtu = ETH_DATA_LEN;
5370 break;
5371 default:
5372 netdev->max_mtu = RTL8153_MAX_MTU;
5373 break;
5374 }
5375
hayeswangac718b62013-05-02 16:01:25 +00005376 tp->mii.dev = netdev;
5377 tp->mii.mdio_read = read_mii_word;
5378 tp->mii.mdio_write = write_mii_word;
5379 tp->mii.phy_id_mask = 0x3f;
5380 tp->mii.reg_num_mask = 0x1f;
5381 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005382
hayeswangaa7e26b2016-06-13 17:49:38 +08005383 tp->autoneg = AUTONEG_ENABLE;
5384 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5385 tp->duplex = DUPLEX_FULL;
5386
hayeswang9a4be1b2014-02-18 21:49:07 +08005387 intf->needs_remote_wakeup = 1;
5388
hayeswangc81229c2014-01-02 11:22:42 +08005389 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005390 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005391 set_ethernet_addr(tp);
5392
hayeswangac718b62013-05-02 16:01:25 +00005393 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005394 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005395
hayeswangebc2ec482013-08-14 20:54:38 +08005396 ret = register_netdev(netdev);
5397 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005398 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005399 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005400 }
5401
hayeswang7daed8d2015-07-24 13:54:24 +08005402 if (!rtl_can_wakeup(tp))
5403 __rtl_set_wol(tp, 0);
5404
hayeswang21ff2e82014-02-18 21:49:06 +08005405 tp->saved_wolopts = __rtl_get_wol(tp);
5406 if (tp->saved_wolopts)
5407 device_set_wakeup_enable(&udev->dev, true);
5408 else
5409 device_set_wakeup_enable(&udev->dev, false);
5410
Hayes Wang4a8deae2014-01-07 11:18:22 +08005411 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005412
5413 return 0;
5414
hayeswangac718b62013-05-02 16:01:25 +00005415out1:
hayeswangd823ab62015-01-12 12:06:23 +08005416 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08005417 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005418out:
5419 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005420 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005421}
5422
hayeswangac718b62013-05-02 16:01:25 +00005423static void rtl8152_disconnect(struct usb_interface *intf)
5424{
5425 struct r8152 *tp = usb_get_intfdata(intf);
5426
5427 usb_set_intfdata(intf, NULL);
5428 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08005429 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08005430
hayeswangd823ab62015-01-12 12:06:23 +08005431 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005432 unregister_netdev(tp->netdev);
hayeswanga028a9e2016-06-13 17:49:36 +08005433 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005434 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005435 free_netdev(tp->netdev);
5436 }
5437}
5438
hayeswangd9a28c52014-12-04 10:43:11 +08005439#define REALTEK_USB_DEVICE(vend, prod) \
5440 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5441 USB_DEVICE_ID_MATCH_INT_CLASS, \
5442 .idVendor = (vend), \
5443 .idProduct = (prod), \
5444 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5445}, \
5446{ \
5447 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5448 USB_DEVICE_ID_MATCH_DEVICE, \
5449 .idVendor = (vend), \
5450 .idProduct = (prod), \
5451 .bInterfaceClass = USB_CLASS_COMM, \
5452 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5453 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5454
hayeswangac718b62013-05-02 16:01:25 +00005455/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305456static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005457 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005458 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5459 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005460 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5461 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005462 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005463 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005464 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5465 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5466 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5467 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5468 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005469 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005470 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005471 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005472 {}
5473};
5474
5475MODULE_DEVICE_TABLE(usb, rtl8152_table);
5476
5477static struct usb_driver rtl8152_driver = {
5478 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005479 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005480 .probe = rtl8152_probe,
5481 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005482 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005483 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005484 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005485 .pre_reset = rtl8152_pre_reset,
5486 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005487 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005488 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005489};
5490
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005491module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005492
5493MODULE_AUTHOR(DRIVER_AUTHOR);
5494MODULE_DESCRIPTION(DRIVER_DESC);
5495MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005496MODULE_VERSION(DRIVER_VERSION);