blob: a7aa48bee7326e631665acd1bac51ff309d928ea [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 */
Hayes Wange4a50172019-08-13 11:42:09 +080029#define NETNEXT_VERSION "10"
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
Hayes Wange4a50172019-08-13 11:42:09 +0800587#define RTL8152_RX_MAX_PENDING 4096
Hayes Wang47922fc2019-08-13 11:42:08 +0800588#define RTL8152_RXFG_HEADSZ 256
589
hayeswang40a82912013-08-14 20:54:40 +0800590#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800591
hayeswangac718b62013-05-02 16:01:25 +0000592#define RTL8152_REQT_READ 0xc0
593#define RTL8152_REQT_WRITE 0x40
594#define RTL8152_REQ_GET_REGS 0x05
595#define RTL8152_REQ_SET_REGS 0x05
596
597#define BYTE_EN_DWORD 0xff
598#define BYTE_EN_WORD 0x33
599#define BYTE_EN_BYTE 0x11
600#define BYTE_EN_SIX_BYTES 0x3f
601#define BYTE_EN_START_MASK 0x0f
602#define BYTE_EN_END_MASK 0xf0
603
hayeswang69b4b7a2014-07-10 10:58:54 +0800604#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800605#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
606 ETH_FCS_LEN)
607#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800608#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800609#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800610#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800611#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800612 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000613
614/* rtl8152 flags */
615enum rtl8152_flags {
616 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000617 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800618 WORK_ENABLE,
619 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800620 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800621 PHY_RESET,
Hayes Wangd2187f82019-08-19 14:40:36 +0800622 SCHEDULE_TASKLET,
hayeswang65b82d62017-06-15 14:44:03 +0800623 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800624 DELL_TB_RX_AGG_BUG,
hayeswangac718b62013-05-02 16:01:25 +0000625};
626
627/* Define these values to match your device */
628#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200629#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800630#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200631#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700632#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700633#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800634#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000635
636#define MCU_TYPE_PLA 0x0100
637#define MCU_TYPE_USB 0x0000
638
hayeswang4f1d4d52014-03-11 16:24:19 +0800639struct tally_counter {
640 __le64 tx_packets;
641 __le64 rx_packets;
642 __le64 tx_errors;
643 __le32 rx_errors;
644 __le16 rx_missed;
645 __le16 align_errors;
646 __le32 tx_one_collision;
647 __le32 tx_multi_collision;
648 __le64 rx_unicast;
649 __le64 rx_broadcast;
650 __le32 rx_multicast;
651 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800652 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800653};
654
hayeswangac718b62013-05-02 16:01:25 +0000655struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800656 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000657#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800658
hayeswang500b6d72013-11-20 17:30:57 +0800659 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800660#define RD_UDP_CS BIT(23)
661#define RD_TCP_CS BIT(22)
662#define RD_IPV6_CS BIT(20)
663#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800664
hayeswang500b6d72013-11-20 17:30:57 +0800665 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800666#define IPF BIT(23) /* IP checksum fail */
667#define UDPF BIT(22) /* UDP checksum fail */
668#define TCPF BIT(21) /* TCP checksum fail */
669#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800670
hayeswang500b6d72013-11-20 17:30:57 +0800671 __le32 opts4;
672 __le32 opts5;
673 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000674};
675
676struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800677 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800678#define TX_FS BIT(31) /* First segment of a packet */
679#define TX_LS BIT(30) /* Final segment of a packet */
680#define GTSENDV4 BIT(28)
681#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800682#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800683#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800684#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800685
hayeswang500b6d72013-11-20 17:30:57 +0800686 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800687#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
688#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
689#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
690#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800691#define MSS_SHIFT 17
692#define MSS_MAX 0x7ffU
693#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800694#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800695#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000696};
697
hayeswangdff4e8a2013-08-16 16:09:33 +0800698struct r8152;
699
hayeswangebc2ec482013-08-14 20:54:38 +0800700struct rx_agg {
Hayes Wang252df8b2019-08-13 11:42:06 +0800701 struct list_head list, info_list;
hayeswangebc2ec482013-08-14 20:54:38 +0800702 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800703 struct r8152 *context;
Hayes Wangd55d7082019-08-13 11:42:07 +0800704 struct page *page;
hayeswangebc2ec482013-08-14 20:54:38 +0800705 void *buffer;
hayeswangebc2ec482013-08-14 20:54:38 +0800706};
707
708struct tx_agg {
709 struct list_head list;
710 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800711 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800712 void *buffer;
713 void *head;
714 u32 skb_num;
715 u32 skb_len;
716};
717
hayeswangac718b62013-05-02 16:01:25 +0000718struct r8152 {
719 unsigned long flags;
720 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800721 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800722 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000723 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800724 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800725 struct tx_agg tx_info[RTL8152_MAX_TX];
Hayes Wang47922fc2019-08-13 11:42:08 +0800726 struct list_head rx_info, rx_used;
hayeswangebc2ec482013-08-14 20:54:38 +0800727 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800728 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800729 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800730 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000731 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800732 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800733#ifdef CONFIG_PM_SLEEP
734 struct notifier_block pm_notifier;
735#endif
Hayes Wangd2187f82019-08-19 14:40:36 +0800736 struct tasklet_struct tx_tl;
hayeswangc81229c2014-01-02 11:22:42 +0800737
738 struct rtl_ops {
739 void (*init)(struct r8152 *);
740 int (*enable)(struct r8152 *);
741 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800742 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800743 void (*down)(struct r8152 *);
744 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800745 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
746 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800747 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800748 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800749 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800750 } rtl_ops;
751
Hayes Wang252df8b2019-08-13 11:42:06 +0800752 atomic_t rx_count;
753
Hayes Wangf4a93be2019-08-23 15:33:40 +0800754 bool eee_en;
hayeswang40a82912013-08-14 20:54:40 +0800755 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800756 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000757 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800758 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800759 u32 coalesce;
Hayes Wangec5791c2019-08-13 11:42:05 +0800760 u32 rx_buf_sz;
Hayes Wange4a50172019-08-13 11:42:09 +0800761 u32 rx_copybreak;
762 u32 rx_pending;
763
hayeswangac718b62013-05-02 16:01:25 +0000764 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800765 u16 speed;
Hayes Wangf4a93be2019-08-23 15:33:40 +0800766 u16 eee_adv;
hayeswang40a82912013-08-14 20:54:40 +0800767 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000768 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800769 u8 duplex;
770 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000771};
772
773enum rtl_version {
774 RTL_VER_UNKNOWN = 0,
775 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800776 RTL_VER_02,
777 RTL_VER_03,
778 RTL_VER_04,
779 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800780 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800781 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800782 RTL_VER_08,
783 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800784 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000785};
786
hayeswang60c89072014-03-07 11:04:39 +0800787enum tx_csum_stat {
788 TX_CSUM_SUCCESS = 0,
789 TX_CSUM_TSO,
790 TX_CSUM_NONE
791};
792
hayeswangac718b62013-05-02 16:01:25 +0000793/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
794 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
795 */
796static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800797static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000798
hayeswang52aec122014-09-02 10:27:52 +0800799#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800800 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800801
hayeswangac718b62013-05-02 16:01:25 +0000802static
803int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
804{
hayeswang31787f52013-07-31 17:21:25 +0800805 int ret;
806 void *tmp;
807
808 tmp = kmalloc(size, GFP_KERNEL);
809 if (!tmp)
810 return -ENOMEM;
811
812 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800813 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
814 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800815
816 memcpy(data, tmp, size);
817 kfree(tmp);
818
819 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000820}
821
822static
823int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
824{
hayeswang31787f52013-07-31 17:21:25 +0800825 int ret;
826 void *tmp;
827
Benoit Tainec4438f02014-05-26 17:21:23 +0200828 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800829 if (!tmp)
830 return -ENOMEM;
831
hayeswang31787f52013-07-31 17:21:25 +0800832 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800833 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
834 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800835
836 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800837
hayeswang31787f52013-07-31 17:21:25 +0800838 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000839}
840
Hayes Wangffa9fec2019-07-04 17:36:32 +0800841static void rtl_set_unplug(struct r8152 *tp)
842{
843 if (tp->udev->state == USB_STATE_NOTATTACHED) {
844 set_bit(RTL8152_UNPLUG, &tp->flags);
845 smp_mb__after_atomic();
846 }
847}
848
hayeswangac718b62013-05-02 16:01:25 +0000849static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800850 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000851{
hayeswang45f4a192014-01-06 17:08:41 +0800852 u16 limit = 64;
853 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000854
855 if (test_bit(RTL8152_UNPLUG, &tp->flags))
856 return -ENODEV;
857
858 /* both size and indix must be 4 bytes align */
859 if ((size & 3) || !size || (index & 3) || !data)
860 return -EPERM;
861
862 if ((u32)index + (u32)size > 0xffff)
863 return -EPERM;
864
865 while (size) {
866 if (size > limit) {
867 ret = get_registers(tp, index, type, limit, data);
868 if (ret < 0)
869 break;
870
871 index += limit;
872 data += limit;
873 size -= limit;
874 } else {
875 ret = get_registers(tp, index, type, size, data);
876 if (ret < 0)
877 break;
878
879 index += size;
880 data += size;
881 size = 0;
882 break;
883 }
884 }
885
hayeswang67610492014-10-30 11:46:40 +0800886 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800887 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800888
hayeswangac718b62013-05-02 16:01:25 +0000889 return ret;
890}
891
892static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800893 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000894{
hayeswang45f4a192014-01-06 17:08:41 +0800895 int ret;
896 u16 byteen_start, byteen_end, byen;
897 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000898
899 if (test_bit(RTL8152_UNPLUG, &tp->flags))
900 return -ENODEV;
901
902 /* both size and indix must be 4 bytes align */
903 if ((size & 3) || !size || (index & 3) || !data)
904 return -EPERM;
905
906 if ((u32)index + (u32)size > 0xffff)
907 return -EPERM;
908
909 byteen_start = byteen & BYTE_EN_START_MASK;
910 byteen_end = byteen & BYTE_EN_END_MASK;
911
912 byen = byteen_start | (byteen_start << 4);
913 ret = set_registers(tp, index, type | byen, 4, data);
914 if (ret < 0)
915 goto error1;
916
917 index += 4;
918 data += 4;
919 size -= 4;
920
921 if (size) {
922 size -= 4;
923
924 while (size) {
925 if (size > limit) {
926 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800927 type | BYTE_EN_DWORD,
928 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000929 if (ret < 0)
930 goto error1;
931
932 index += limit;
933 data += limit;
934 size -= limit;
935 } else {
936 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800937 type | BYTE_EN_DWORD,
938 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000939 if (ret < 0)
940 goto error1;
941
942 index += size;
943 data += size;
944 size = 0;
945 break;
946 }
947 }
948
949 byen = byteen_end | (byteen_end >> 4);
950 ret = set_registers(tp, index, type | byen, 4, data);
951 if (ret < 0)
952 goto error1;
953 }
954
955error1:
hayeswang67610492014-10-30 11:46:40 +0800956 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800957 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800958
hayeswangac718b62013-05-02 16:01:25 +0000959 return ret;
960}
961
962static inline
963int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
964{
965 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
966}
967
968static inline
969int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
970{
971 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
972}
973
974static inline
hayeswangac718b62013-05-02 16:01:25 +0000975int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
976{
977 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
978}
979
980static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
981{
hayeswangc8826de2013-07-31 17:21:26 +0800982 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000983
hayeswangc8826de2013-07-31 17:21:26 +0800984 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000985
986 return __le32_to_cpu(data);
987}
988
989static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
990{
hayeswangc8826de2013-07-31 17:21:26 +0800991 __le32 tmp = __cpu_to_le32(data);
992
993 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000994}
995
996static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
997{
998 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800999 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +08001000 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +00001001 u8 shift = index & 2;
1002
1003 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +08001004 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +00001005
hayeswangd8fbd272017-06-15 14:44:04 +08001006 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +00001007
hayeswangc8826de2013-07-31 17:21:26 +08001008 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001009 data >>= (shift * 8);
1010 data &= 0xffff;
1011
1012 return (u16)data;
1013}
1014
1015static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1016{
hayeswangc8826de2013-07-31 17:21:26 +08001017 u32 mask = 0xffff;
1018 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001019 u16 byen = BYTE_EN_WORD;
1020 u8 shift = index & 2;
1021
1022 data &= mask;
1023
1024 if (index & 2) {
1025 byen <<= shift;
1026 mask <<= (shift * 8);
1027 data <<= (shift * 8);
1028 index &= ~3;
1029 }
1030
hayeswangc8826de2013-07-31 17:21:26 +08001031 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001032
hayeswangc8826de2013-07-31 17:21:26 +08001033 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001034}
1035
1036static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1037{
1038 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001039 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001040 u8 shift = index & 3;
1041
1042 index &= ~3;
1043
hayeswangc8826de2013-07-31 17:21:26 +08001044 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001045
hayeswangc8826de2013-07-31 17:21:26 +08001046 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001047 data >>= (shift * 8);
1048 data &= 0xff;
1049
1050 return (u8)data;
1051}
1052
1053static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1054{
hayeswangc8826de2013-07-31 17:21:26 +08001055 u32 mask = 0xff;
1056 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001057 u16 byen = BYTE_EN_BYTE;
1058 u8 shift = index & 3;
1059
1060 data &= mask;
1061
1062 if (index & 3) {
1063 byen <<= shift;
1064 mask <<= (shift * 8);
1065 data <<= (shift * 8);
1066 index &= ~3;
1067 }
1068
hayeswangc8826de2013-07-31 17:21:26 +08001069 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001070
hayeswangc8826de2013-07-31 17:21:26 +08001071 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001072}
1073
hayeswangac244d32014-01-02 11:22:40 +08001074static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1075{
1076 u16 ocp_base, ocp_index;
1077
1078 ocp_base = addr & 0xf000;
1079 if (ocp_base != tp->ocp_base) {
1080 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1081 tp->ocp_base = ocp_base;
1082 }
1083
1084 ocp_index = (addr & 0x0fff) | 0xb000;
1085 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1086}
1087
hayeswange3fe0b12014-01-02 11:22:39 +08001088static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1089{
1090 u16 ocp_base, ocp_index;
1091
1092 ocp_base = addr & 0xf000;
1093 if (ocp_base != tp->ocp_base) {
1094 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1095 tp->ocp_base = ocp_base;
1096 }
1097
1098 ocp_index = (addr & 0x0fff) | 0xb000;
1099 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1100}
1101
hayeswangac244d32014-01-02 11:22:40 +08001102static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001103{
hayeswangac244d32014-01-02 11:22:40 +08001104 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001105}
1106
hayeswangac244d32014-01-02 11:22:40 +08001107static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001108{
hayeswangac244d32014-01-02 11:22:40 +08001109 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001110}
1111
hayeswang43779f82014-01-02 11:25:10 +08001112static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1113{
1114 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1115 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1116}
1117
hayeswang65b82d62017-06-15 14:44:03 +08001118static u16 sram_read(struct r8152 *tp, u16 addr)
1119{
1120 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1121 return ocp_reg_read(tp, OCP_SRAM_DATA);
1122}
1123
hayeswangac718b62013-05-02 16:01:25 +00001124static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1125{
1126 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001127 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001128
hayeswang68714382014-04-11 17:54:31 +08001129 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1130 return -ENODEV;
1131
hayeswangac718b62013-05-02 16:01:25 +00001132 if (phy_id != R8152_PHY_ID)
1133 return -EINVAL;
1134
hayeswang9a4be1b2014-02-18 21:49:07 +08001135 ret = r8152_mdio_read(tp, reg);
1136
hayeswang9a4be1b2014-02-18 21:49:07 +08001137 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001138}
1139
1140static
1141void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1142{
1143 struct r8152 *tp = netdev_priv(netdev);
1144
hayeswang68714382014-04-11 17:54:31 +08001145 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1146 return;
1147
hayeswangac718b62013-05-02 16:01:25 +00001148 if (phy_id != R8152_PHY_ID)
1149 return;
1150
1151 r8152_mdio_write(tp, reg, val);
1152}
1153
hayeswangb209af92014-08-25 15:53:00 +08001154static int
1155r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001156
hayeswang8ba789a2014-09-04 16:15:41 +08001157static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1158{
1159 struct r8152 *tp = netdev_priv(netdev);
1160 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001161 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001162
1163 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001164 goto out1;
1165
1166 ret = usb_autopm_get_interface(tp->intf);
1167 if (ret < 0)
1168 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001169
hayeswangb5403272014-10-09 18:00:26 +08001170 mutex_lock(&tp->control);
1171
hayeswang8ba789a2014-09-04 16:15:41 +08001172 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1173
1174 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1175 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1176 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1177
hayeswangb5403272014-10-09 18:00:26 +08001178 mutex_unlock(&tp->control);
1179
hayeswangea6a7112014-10-02 17:03:12 +08001180 usb_autopm_put_interface(tp->intf);
1181out1:
1182 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001183}
1184
Mario Limonciello9c273692018-12-11 08:16:14 -06001185/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001186 * host system provided MAC address.
1187 * Examples of this are Dell TB15 and Dell WD15 docks
1188 */
1189static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1190{
1191 acpi_status status;
1192 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1193 union acpi_object *obj;
1194 int ret = -EINVAL;
1195 u32 ocp_data;
1196 unsigned char buf[6];
1197
1198 /* test for -AD variant of RTL8153 */
1199 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001200 if ((ocp_data & AD_MASK) == 0x1000) {
1201 /* test for MAC address pass-through bit */
1202 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1203 if ((ocp_data & PASS_THRU_MASK) != 1) {
1204 netif_dbg(tp, probe, tp->netdev,
1205 "No efuse for RTL8153-AD MAC pass through\n");
1206 return -ENODEV;
1207 }
1208 } else {
David Chen8e29d232019-02-16 17:16:42 +08001209 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001210 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001211 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001212 netif_dbg(tp, probe, tp->netdev,
1213 "Invalid variant for MAC pass through\n");
1214 return -ENODEV;
1215 }
1216 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001217
1218 /* returns _AUXMAC_#AABBCCDDEEFF# */
1219 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1220 obj = (union acpi_object *)buffer.pointer;
1221 if (!ACPI_SUCCESS(status))
1222 return -ENODEV;
1223 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1224 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001225 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001226 obj->type, obj->string.length);
1227 goto amacout;
1228 }
1229 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1230 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1231 netif_warn(tp, probe, tp->netdev,
1232 "Invalid header when reading pass-thru MAC addr\n");
1233 goto amacout;
1234 }
1235 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1236 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1237 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001238 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1239 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001240 ret = -EINVAL;
1241 goto amacout;
1242 }
1243 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001244 netif_info(tp, probe, tp->netdev,
1245 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1246
1247amacout:
1248 kfree(obj);
1249 return ret;
1250}
1251
Mario Limonciello25766272019-04-04 13:46:53 -05001252static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1253{
1254 struct net_device *dev = tp->netdev;
1255 int ret;
1256
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001257 sa->sa_family = dev->type;
1258
Mario Limonciello25766272019-04-04 13:46:53 -05001259 if (tp->version == RTL_VER_01) {
1260 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1261 } else {
1262 /* if device doesn't support MAC pass through this will
1263 * be expected to be non-zero
1264 */
1265 ret = vendor_mac_passthru_addr_read(tp, sa);
1266 if (ret < 0)
1267 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1268 }
1269
1270 if (ret < 0) {
1271 netif_err(tp, probe, dev, "Get ether addr fail\n");
1272 } else if (!is_valid_ether_addr(sa->sa_data)) {
1273 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1274 sa->sa_data);
1275 eth_hw_addr_random(dev);
1276 ether_addr_copy(sa->sa_data, dev->dev_addr);
1277 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1278 sa->sa_data);
1279 return 0;
1280 }
1281
1282 return ret;
1283}
1284
hayeswang179bb6d2014-09-04 16:15:42 +08001285static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001286{
1287 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001288 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001289 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001290
Mario Limonciello25766272019-04-04 13:46:53 -05001291 ret = determine_ethernet_addr(tp, &sa);
1292 if (ret < 0)
1293 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001294
Mario Limonciello25766272019-04-04 13:46:53 -05001295 if (tp->version == RTL_VER_01)
1296 ether_addr_copy(dev->dev_addr, sa.sa_data);
1297 else
hayeswang179bb6d2014-09-04 16:15:42 +08001298 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001299
1300 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001301}
1302
hayeswangac718b62013-05-02 16:01:25 +00001303static void read_bulk_callback(struct urb *urb)
1304{
hayeswangac718b62013-05-02 16:01:25 +00001305 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001306 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001307 struct rx_agg *agg;
1308 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001309 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001310
hayeswangebc2ec482013-08-14 20:54:38 +08001311 agg = urb->context;
1312 if (!agg)
1313 return;
1314
1315 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001316 if (!tp)
1317 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001318
hayeswangac718b62013-05-02 16:01:25 +00001319 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1320 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001321
1322 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001323 return;
1324
hayeswangebc2ec482013-08-14 20:54:38 +08001325 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001326
1327 /* When link down, the driver would cancel all bulks. */
1328 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001329 if (!netif_carrier_ok(netdev))
1330 return;
1331
hayeswang9a4be1b2014-02-18 21:49:07 +08001332 usb_mark_last_busy(tp->udev);
1333
hayeswangac718b62013-05-02 16:01:25 +00001334 switch (status) {
1335 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001336 if (urb->actual_length < ETH_ZLEN)
1337 break;
1338
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001339 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001340 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001341 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001342 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001343 return;
hayeswangac718b62013-05-02 16:01:25 +00001344 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001345 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001346 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001347 return;
hayeswangac718b62013-05-02 16:01:25 +00001348 case -ENOENT:
1349 return; /* the urb is in unlink state */
1350 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001351 if (net_ratelimit())
1352 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001353 break;
hayeswangac718b62013-05-02 16:01:25 +00001354 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001355 if (net_ratelimit())
1356 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001357 break;
hayeswangac718b62013-05-02 16:01:25 +00001358 }
1359
hayeswanga0fccd42014-11-20 10:29:05 +08001360 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001361}
1362
1363static void write_bulk_callback(struct urb *urb)
1364{
hayeswangebc2ec482013-08-14 20:54:38 +08001365 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001366 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001367 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001368 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001369 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001370 int status = urb->status;
1371
hayeswangebc2ec482013-08-14 20:54:38 +08001372 agg = urb->context;
1373 if (!agg)
1374 return;
1375
1376 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001377 if (!tp)
1378 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001379
hayeswangd104eaf2014-03-06 15:07:17 +08001380 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001381 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001382 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001383 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001384 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001385 stats->tx_errors += agg->skb_num;
1386 } else {
1387 stats->tx_packets += agg->skb_num;
1388 stats->tx_bytes += agg->skb_len;
1389 }
1390
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001391 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001392 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001393 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001394
hayeswang9a4be1b2014-02-18 21:49:07 +08001395 usb_autopm_put_interface_async(tp->intf);
1396
hayeswangd104eaf2014-03-06 15:07:17 +08001397 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001398 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001399
1400 if (!test_bit(WORK_ENABLE, &tp->flags))
1401 return;
1402
1403 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1404 return;
1405
1406 if (!skb_queue_empty(&tp->tx_queue))
Hayes Wangd2187f82019-08-19 14:40:36 +08001407 tasklet_schedule(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08001408}
1409
hayeswang40a82912013-08-14 20:54:40 +08001410static void intr_callback(struct urb *urb)
1411{
1412 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001413 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001414 int status = urb->status;
1415 int res;
1416
1417 tp = urb->context;
1418 if (!tp)
1419 return;
1420
1421 if (!test_bit(WORK_ENABLE, &tp->flags))
1422 return;
1423
1424 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1425 return;
1426
1427 switch (status) {
1428 case 0: /* success */
1429 break;
1430 case -ECONNRESET: /* unlink */
1431 case -ESHUTDOWN:
1432 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001433 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001434 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001435 case -EPROTO:
1436 netif_info(tp, intr, tp->netdev,
1437 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001438 return;
1439 case -EOVERFLOW:
1440 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1441 goto resubmit;
1442 /* -EPIPE: should clear the halt */
1443 default:
1444 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1445 goto resubmit;
1446 }
1447
1448 d = urb->transfer_buffer;
1449 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001450 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001451 set_bit(RTL8152_LINK_CHG, &tp->flags);
1452 schedule_delayed_work(&tp->schedule, 0);
1453 }
1454 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001455 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001456 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001457 set_bit(RTL8152_LINK_CHG, &tp->flags);
1458 schedule_delayed_work(&tp->schedule, 0);
1459 }
1460 }
1461
1462resubmit:
1463 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001464 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001465 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001466 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001467 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001468 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001469 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001470 }
hayeswang40a82912013-08-14 20:54:40 +08001471}
1472
hayeswangebc2ec482013-08-14 20:54:38 +08001473static inline void *rx_agg_align(void *data)
1474{
hayeswang8e1f51b2014-01-02 11:22:41 +08001475 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001476}
1477
1478static inline void *tx_agg_align(void *data)
1479{
hayeswang8e1f51b2014-01-02 11:22:41 +08001480 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001481}
1482
Hayes Wang252df8b2019-08-13 11:42:06 +08001483static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1484{
1485 list_del(&agg->info_list);
1486
1487 usb_free_urb(agg->urb);
Hayes Wang47922fc2019-08-13 11:42:08 +08001488 put_page(agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001489 kfree(agg);
1490
1491 atomic_dec(&tp->rx_count);
1492}
1493
1494static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1495{
1496 struct net_device *netdev = tp->netdev;
1497 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
Hayes Wangd55d7082019-08-13 11:42:07 +08001498 unsigned int order = get_order(tp->rx_buf_sz);
Hayes Wang252df8b2019-08-13 11:42:06 +08001499 struct rx_agg *rx_agg;
1500 unsigned long flags;
Hayes Wang252df8b2019-08-13 11:42:06 +08001501
1502 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1503 if (!rx_agg)
1504 return NULL;
1505
Hayes Wang47922fc2019-08-13 11:42:08 +08001506 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
Hayes Wangd55d7082019-08-13 11:42:07 +08001507 if (!rx_agg->page)
Hayes Wang252df8b2019-08-13 11:42:06 +08001508 goto free_rx;
1509
Hayes Wangd55d7082019-08-13 11:42:07 +08001510 rx_agg->buffer = page_address(rx_agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001511
1512 rx_agg->urb = usb_alloc_urb(0, mflags);
1513 if (!rx_agg->urb)
1514 goto free_buf;
1515
1516 rx_agg->context = tp;
1517
1518 INIT_LIST_HEAD(&rx_agg->list);
1519 INIT_LIST_HEAD(&rx_agg->info_list);
1520 spin_lock_irqsave(&tp->rx_lock, flags);
1521 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1522 spin_unlock_irqrestore(&tp->rx_lock, flags);
1523
1524 atomic_inc(&tp->rx_count);
1525
1526 return rx_agg;
1527
1528free_buf:
Hayes Wangd55d7082019-08-13 11:42:07 +08001529 __free_pages(rx_agg->page, order);
Hayes Wang252df8b2019-08-13 11:42:06 +08001530free_rx:
1531 kfree(rx_agg);
1532 return NULL;
1533}
1534
hayeswangebc2ec482013-08-14 20:54:38 +08001535static void free_all_mem(struct r8152 *tp)
1536{
Hayes Wang252df8b2019-08-13 11:42:06 +08001537 struct rx_agg *agg, *agg_next;
1538 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001539 int i;
1540
Hayes Wang252df8b2019-08-13 11:42:06 +08001541 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001542
Hayes Wang252df8b2019-08-13 11:42:06 +08001543 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1544 free_rx_agg(tp, agg);
1545
1546 spin_unlock_irqrestore(&tp->rx_lock, flags);
1547
1548 WARN_ON(atomic_read(&tp->rx_count));
hayeswangebc2ec482013-08-14 20:54:38 +08001549
1550 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001551 usb_free_urb(tp->tx_info[i].urb);
1552 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001553
hayeswang9629e3c2014-01-15 10:42:15 +08001554 kfree(tp->tx_info[i].buffer);
1555 tp->tx_info[i].buffer = NULL;
1556 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001557 }
hayeswang40a82912013-08-14 20:54:40 +08001558
hayeswang9629e3c2014-01-15 10:42:15 +08001559 usb_free_urb(tp->intr_urb);
1560 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001561
hayeswang9629e3c2014-01-15 10:42:15 +08001562 kfree(tp->intr_buff);
1563 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001564}
1565
1566static int alloc_all_mem(struct r8152 *tp)
1567{
1568 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001569 struct usb_interface *intf = tp->intf;
1570 struct usb_host_interface *alt = intf->cur_altsetting;
1571 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001572 int node, i;
hayeswangebc2ec482013-08-14 20:54:38 +08001573
1574 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1575
1576 spin_lock_init(&tp->rx_lock);
1577 spin_lock_init(&tp->tx_lock);
Hayes Wang252df8b2019-08-13 11:42:06 +08001578 INIT_LIST_HEAD(&tp->rx_info);
hayeswangebc2ec482013-08-14 20:54:38 +08001579 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001580 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001581 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001582 skb_queue_head_init(&tp->rx_queue);
Hayes Wang252df8b2019-08-13 11:42:06 +08001583 atomic_set(&tp->rx_count, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001584
1585 for (i = 0; i < RTL8152_MAX_RX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001586 if (!alloc_rx_agg(tp, GFP_KERNEL))
hayeswangebc2ec482013-08-14 20:54:38 +08001587 goto err1;
hayeswangebc2ec482013-08-14 20:54:38 +08001588 }
1589
1590 for (i = 0; i < RTL8152_MAX_TX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001591 struct urb *urb;
1592 u8 *buf;
1593
hayeswang52aec122014-09-02 10:27:52 +08001594 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001595 if (!buf)
1596 goto err1;
1597
1598 if (buf != tx_agg_align(buf)) {
1599 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001600 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001601 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001602 if (!buf)
1603 goto err1;
1604 }
1605
1606 urb = usb_alloc_urb(0, GFP_KERNEL);
1607 if (!urb) {
1608 kfree(buf);
1609 goto err1;
1610 }
1611
1612 INIT_LIST_HEAD(&tp->tx_info[i].list);
1613 tp->tx_info[i].context = tp;
1614 tp->tx_info[i].urb = urb;
1615 tp->tx_info[i].buffer = buf;
1616 tp->tx_info[i].head = tx_agg_align(buf);
1617
1618 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1619 }
1620
hayeswang40a82912013-08-14 20:54:40 +08001621 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1622 if (!tp->intr_urb)
1623 goto err1;
1624
1625 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1626 if (!tp->intr_buff)
1627 goto err1;
1628
1629 tp->intr_interval = (int)ep_intr->desc.bInterval;
1630 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001631 tp->intr_buff, INTBUFSIZE, intr_callback,
1632 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001633
hayeswangebc2ec482013-08-14 20:54:38 +08001634 return 0;
1635
1636err1:
1637 free_all_mem(tp);
1638 return -ENOMEM;
1639}
1640
hayeswang0de98f62013-08-16 16:09:35 +08001641static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1642{
1643 struct tx_agg *agg = NULL;
1644 unsigned long flags;
1645
hayeswang21949ab2014-03-07 11:04:35 +08001646 if (list_empty(&tp->tx_free))
1647 return NULL;
1648
hayeswang0de98f62013-08-16 16:09:35 +08001649 spin_lock_irqsave(&tp->tx_lock, flags);
1650 if (!list_empty(&tp->tx_free)) {
1651 struct list_head *cursor;
1652
1653 cursor = tp->tx_free.next;
1654 list_del_init(cursor);
1655 agg = list_entry(cursor, struct tx_agg, list);
1656 }
1657 spin_unlock_irqrestore(&tp->tx_lock, flags);
1658
1659 return agg;
1660}
1661
hayeswangb209af92014-08-25 15:53:00 +08001662/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001663 * The hw limites the value the transport offset. When the offset is out of the
1664 * range, calculate the checksum by sw.
1665 */
1666static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1667 struct sk_buff_head *list)
1668{
1669 if (skb_shinfo(skb)->gso_size) {
1670 netdev_features_t features = tp->netdev->features;
1671 struct sk_buff_head seg_list;
1672 struct sk_buff *segs, *nskb;
1673
hayeswanga91d45f2014-07-11 16:48:27 +08001674 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001675 segs = skb_gso_segment(skb, features);
1676 if (IS_ERR(segs) || !segs)
1677 goto drop;
1678
1679 __skb_queue_head_init(&seg_list);
1680
1681 do {
1682 nskb = segs;
1683 segs = segs->next;
1684 nskb->next = NULL;
1685 __skb_queue_tail(&seg_list, nskb);
1686 } while (segs);
1687
1688 skb_queue_splice(&seg_list, list);
1689 dev_kfree_skb(skb);
1690 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1691 if (skb_checksum_help(skb) < 0)
1692 goto drop;
1693
1694 __skb_queue_head(list, skb);
1695 } else {
1696 struct net_device_stats *stats;
1697
1698drop:
1699 stats = &tp->netdev->stats;
1700 stats->tx_dropped++;
1701 dev_kfree_skb(skb);
1702 }
1703}
1704
hayeswangb209af92014-08-25 15:53:00 +08001705/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001706 * According to the document of microsoft, the TCP Pseudo Header excludes the
1707 * packet length for IPv6 TCP large packets.
1708 */
1709static int msdn_giant_send_check(struct sk_buff *skb)
1710{
1711 const struct ipv6hdr *ipv6h;
1712 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001713 int ret;
1714
1715 ret = skb_cow_head(skb, 0);
1716 if (ret)
1717 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001718
1719 ipv6h = ipv6_hdr(skb);
1720 th = tcp_hdr(skb);
1721
1722 th->check = 0;
1723 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1724
hayeswangfcb308d2014-03-11 10:20:32 +08001725 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001726}
1727
hayeswangc5554292014-09-12 10:43:11 +08001728static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1729{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001730 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001731 u32 opts2;
1732
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001733 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001734 desc->opts2 |= cpu_to_le32(opts2);
1735 }
1736}
1737
1738static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1739{
1740 u32 opts2 = le32_to_cpu(desc->opts2);
1741
1742 if (opts2 & RX_VLAN_TAG)
1743 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1744 swab16(opts2 & 0xffff));
1745}
1746
hayeswang60c89072014-03-07 11:04:39 +08001747static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1748 struct sk_buff *skb, u32 len, u32 transport_offset)
1749{
1750 u32 mss = skb_shinfo(skb)->gso_size;
1751 u32 opts1, opts2 = 0;
1752 int ret = TX_CSUM_SUCCESS;
1753
1754 WARN_ON_ONCE(len > TX_LEN_MAX);
1755
1756 opts1 = len | TX_FS | TX_LS;
1757
1758 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001759 if (transport_offset > GTTCPHO_MAX) {
1760 netif_warn(tp, tx_err, tp->netdev,
1761 "Invalid transport offset 0x%x for TSO\n",
1762 transport_offset);
1763 ret = TX_CSUM_TSO;
1764 goto unavailable;
1765 }
1766
hayeswang6e74d172015-02-06 11:30:50 +08001767 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001768 case htons(ETH_P_IP):
1769 opts1 |= GTSENDV4;
1770 break;
1771
hayeswang6128d1bb2014-03-07 11:04:40 +08001772 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001773 if (msdn_giant_send_check(skb)) {
1774 ret = TX_CSUM_TSO;
1775 goto unavailable;
1776 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001777 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001778 break;
1779
hayeswang60c89072014-03-07 11:04:39 +08001780 default:
1781 WARN_ON_ONCE(1);
1782 break;
1783 }
1784
1785 opts1 |= transport_offset << GTTCPHO_SHIFT;
1786 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1787 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001788 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001789
hayeswang6128d1bb2014-03-07 11:04:40 +08001790 if (transport_offset > TCPHO_MAX) {
1791 netif_warn(tp, tx_err, tp->netdev,
1792 "Invalid transport offset 0x%x\n",
1793 transport_offset);
1794 ret = TX_CSUM_NONE;
1795 goto unavailable;
1796 }
1797
hayeswang6e74d172015-02-06 11:30:50 +08001798 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001799 case htons(ETH_P_IP):
1800 opts2 |= IPV4_CS;
1801 ip_protocol = ip_hdr(skb)->protocol;
1802 break;
1803
1804 case htons(ETH_P_IPV6):
1805 opts2 |= IPV6_CS;
1806 ip_protocol = ipv6_hdr(skb)->nexthdr;
1807 break;
1808
1809 default:
1810 ip_protocol = IPPROTO_RAW;
1811 break;
1812 }
1813
hayeswang60c89072014-03-07 11:04:39 +08001814 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001815 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001816 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001817 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001818 else
hayeswang5bd23882013-08-14 20:54:39 +08001819 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001820
hayeswang60c89072014-03-07 11:04:39 +08001821 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001822 }
hayeswang60c89072014-03-07 11:04:39 +08001823
1824 desc->opts2 = cpu_to_le32(opts2);
1825 desc->opts1 = cpu_to_le32(opts1);
1826
hayeswang6128d1bb2014-03-07 11:04:40 +08001827unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001828 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001829}
1830
hayeswangb1379d92013-08-16 16:09:37 +08001831static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1832{
hayeswangd84130a2014-02-18 21:49:02 +08001833 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001834 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001835 u8 *tx_data;
1836
hayeswangd84130a2014-02-18 21:49:02 +08001837 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001838 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001839 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001840 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001841
hayeswangb1379d92013-08-16 16:09:37 +08001842 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001843 agg->skb_num = 0;
1844 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001845 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001846
hayeswang7937f9e2013-11-20 17:30:54 +08001847 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001848 struct tx_desc *tx_desc;
1849 struct sk_buff *skb;
1850 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001851 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001852
hayeswangd84130a2014-02-18 21:49:02 +08001853 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001854 if (!skb)
1855 break;
1856
hayeswang60c89072014-03-07 11:04:39 +08001857 len = skb->len + sizeof(*tx_desc);
1858
1859 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001860 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001861 break;
1862 }
1863
hayeswang7937f9e2013-11-20 17:30:54 +08001864 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001865 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001866
1867 offset = (u32)skb_transport_offset(skb);
1868
hayeswang6128d1bb2014-03-07 11:04:40 +08001869 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1870 r8152_csum_workaround(tp, skb, &skb_head);
1871 continue;
1872 }
hayeswang60c89072014-03-07 11:04:39 +08001873
hayeswangc5554292014-09-12 10:43:11 +08001874 rtl_tx_vlan_tag(tx_desc, skb);
1875
hayeswangb1379d92013-08-16 16:09:37 +08001876 tx_data += sizeof(*tx_desc);
1877
hayeswang60c89072014-03-07 11:04:39 +08001878 len = skb->len;
1879 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1880 struct net_device_stats *stats = &tp->netdev->stats;
1881
1882 stats->tx_dropped++;
1883 dev_kfree_skb_any(skb);
1884 tx_data -= sizeof(*tx_desc);
1885 continue;
1886 }
hayeswangb1379d92013-08-16 16:09:37 +08001887
hayeswang7937f9e2013-11-20 17:30:54 +08001888 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001889 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001890 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001891
1892 dev_kfree_skb_any(skb);
1893
hayeswang52aec122014-09-02 10:27:52 +08001894 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001895
1896 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1897 break;
hayeswangb1379d92013-08-16 16:09:37 +08001898 }
1899
hayeswangd84130a2014-02-18 21:49:02 +08001900 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001901 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001902 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001903 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001904 }
1905
hayeswang0c3121f2014-03-07 11:04:36 +08001906 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001907
1908 if (netif_queue_stopped(tp->netdev) &&
1909 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1910 netif_wake_queue(tp->netdev);
1911
hayeswang0c3121f2014-03-07 11:04:36 +08001912 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001913
hayeswang0c3121f2014-03-07 11:04:36 +08001914 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001915 if (ret < 0)
1916 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001917
hayeswangb1379d92013-08-16 16:09:37 +08001918 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1919 agg->head, (int)(tx_data - (u8 *)agg->head),
1920 (usb_complete_t)write_bulk_callback, agg);
1921
hayeswang0c3121f2014-03-07 11:04:36 +08001922 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001923 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001924 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001925
1926out_tx_fill:
1927 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001928}
1929
hayeswang565cab02014-03-07 11:04:38 +08001930static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1931{
1932 u8 checksum = CHECKSUM_NONE;
1933 u32 opts2, opts3;
1934
hayeswang19c0f402017-01-11 16:25:34 +08001935 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001936 goto return_result;
1937
1938 opts2 = le32_to_cpu(rx_desc->opts2);
1939 opts3 = le32_to_cpu(rx_desc->opts3);
1940
1941 if (opts2 & RD_IPV4_CS) {
1942 if (opts3 & IPF)
1943 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001944 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1945 checksum = CHECKSUM_UNNECESSARY;
1946 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001947 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001948 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001949 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1950 checksum = CHECKSUM_UNNECESSARY;
1951 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1952 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001953 }
1954
1955return_result:
1956 return checksum;
1957}
1958
Hayes Wang47922fc2019-08-13 11:42:08 +08001959static inline bool rx_count_exceed(struct r8152 *tp)
1960{
1961 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
1962}
1963
1964static inline int agg_offset(struct rx_agg *agg, void *addr)
1965{
1966 return (int)(addr - agg->buffer);
1967}
1968
1969static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
1970{
1971 struct rx_agg *agg, *agg_next, *agg_free = NULL;
1972 unsigned long flags;
1973
1974 spin_lock_irqsave(&tp->rx_lock, flags);
1975
1976 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
1977 if (page_count(agg->page) == 1) {
1978 if (!agg_free) {
1979 list_del_init(&agg->list);
1980 agg_free = agg;
1981 continue;
1982 }
1983 if (rx_count_exceed(tp)) {
1984 list_del_init(&agg->list);
1985 free_rx_agg(tp, agg);
1986 }
1987 break;
1988 }
1989 }
1990
1991 spin_unlock_irqrestore(&tp->rx_lock, flags);
1992
Hayes Wange4a50172019-08-13 11:42:09 +08001993 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
Hayes Wang47922fc2019-08-13 11:42:08 +08001994 agg_free = alloc_rx_agg(tp, mflags);
1995
1996 return agg_free;
1997}
1998
hayeswangd823ab62015-01-12 12:06:23 +08001999static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08002000{
hayeswanga5a4f462013-08-16 16:09:34 +08002001 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08002002 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08002003 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08002004 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08002005
2006 if (!skb_queue_empty(&tp->rx_queue)) {
2007 while (work_done < budget) {
2008 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2009 struct net_device *netdev = tp->netdev;
2010 struct net_device_stats *stats = &netdev->stats;
2011 unsigned int pkt_len;
2012
2013 if (!skb)
2014 break;
2015
2016 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08002017 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002018 work_done++;
2019 stats->rx_packets++;
2020 stats->rx_bytes += pkt_len;
2021 }
2022 }
hayeswangebc2ec482013-08-14 20:54:38 +08002023
hayeswangd84130a2014-02-18 21:49:02 +08002024 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08002025 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08002026
2027 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08002028 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08002029 list_splice_init(&tp->rx_done, &rx_queue);
2030 spin_unlock_irqrestore(&tp->rx_lock, flags);
2031
2032 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08002033 struct rx_desc *rx_desc;
Hayes Wang47922fc2019-08-13 11:42:08 +08002034 struct rx_agg *agg, *agg_free;
hayeswang43a44782013-08-16 16:09:36 +08002035 int len_used = 0;
2036 struct urb *urb;
2037 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08002038
hayeswangebc2ec482013-08-14 20:54:38 +08002039 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08002040
2041 agg = list_entry(cursor, struct rx_agg, list);
2042 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08002043 if (urb->actual_length < ETH_ZLEN)
2044 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08002045
Hayes Wang47922fc2019-08-13 11:42:08 +08002046 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2047
Hayes Wangd55d7082019-08-13 11:42:07 +08002048 rx_desc = agg->buffer;
2049 rx_data = agg->buffer;
hayeswang7937f9e2013-11-20 17:30:54 +08002050 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002051
hayeswang7937f9e2013-11-20 17:30:54 +08002052 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08002053 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08002054 struct net_device_stats *stats = &netdev->stats;
Hayes Wang47922fc2019-08-13 11:42:08 +08002055 unsigned int pkt_len, rx_frag_head_sz;
hayeswang43a44782013-08-16 16:09:36 +08002056 struct sk_buff *skb;
2057
hayeswang74544452017-06-09 17:11:47 +08002058 /* limite the skb numbers for rx_queue */
2059 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2060 break;
2061
hayeswang7937f9e2013-11-20 17:30:54 +08002062 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08002063 if (pkt_len < ETH_ZLEN)
2064 break;
2065
hayeswang7937f9e2013-11-20 17:30:54 +08002066 len_used += pkt_len;
2067 if (urb->actual_length < len_used)
2068 break;
2069
hayeswangb65c0c92017-06-21 11:25:18 +08002070 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08002071 rx_data += sizeof(struct rx_desc);
2072
Hayes Wange4a50172019-08-13 11:42:09 +08002073 if (!agg_free || tp->rx_copybreak > pkt_len)
Hayes Wang47922fc2019-08-13 11:42:08 +08002074 rx_frag_head_sz = pkt_len;
2075 else
Hayes Wange4a50172019-08-13 11:42:09 +08002076 rx_frag_head_sz = tp->rx_copybreak;
Hayes Wang47922fc2019-08-13 11:42:08 +08002077
2078 skb = napi_alloc_skb(napi, rx_frag_head_sz);
hayeswangebc2ec482013-08-14 20:54:38 +08002079 if (!skb) {
2080 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08002081 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08002082 }
hayeswang565cab02014-03-07 11:04:38 +08002083
2084 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
Hayes Wang47922fc2019-08-13 11:42:08 +08002085 memcpy(skb->data, rx_data, rx_frag_head_sz);
2086 skb_put(skb, rx_frag_head_sz);
2087 pkt_len -= rx_frag_head_sz;
2088 rx_data += rx_frag_head_sz;
2089 if (pkt_len) {
2090 skb_add_rx_frag(skb, 0, agg->page,
2091 agg_offset(agg, rx_data),
2092 pkt_len,
2093 SKB_DATA_ALIGN(pkt_len));
2094 get_page(agg->page);
2095 }
2096
hayeswangebc2ec482013-08-14 20:54:38 +08002097 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08002098 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002099 if (work_done < budget) {
hayeswangd823ab62015-01-12 12:06:23 +08002100 work_done++;
2101 stats->rx_packets++;
Hayes Wang47922fc2019-08-13 11:42:08 +08002102 stats->rx_bytes += skb->len;
Hayes Wang6636fb32019-08-19 11:15:19 +08002103 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002104 } else {
2105 __skb_queue_tail(&tp->rx_queue, skb);
2106 }
hayeswangebc2ec482013-08-14 20:54:38 +08002107
hayeswang5e2f7482014-03-07 11:04:37 +08002108find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002109 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002110 rx_desc = (struct rx_desc *)rx_data;
Hayes Wang47922fc2019-08-13 11:42:08 +08002111 len_used = agg_offset(agg, rx_data);
hayeswang7937f9e2013-11-20 17:30:54 +08002112 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002113 }
2114
Hayes Wang47922fc2019-08-13 11:42:08 +08002115 WARN_ON(!agg_free && page_count(agg->page) > 1);
2116
2117 if (agg_free) {
2118 spin_lock_irqsave(&tp->rx_lock, flags);
2119 if (page_count(agg->page) == 1) {
2120 list_add(&agg_free->list, &tp->rx_used);
2121 } else {
2122 list_add_tail(&agg->list, &tp->rx_used);
2123 agg = agg_free;
2124 urb = agg->urb;
2125 }
2126 spin_unlock_irqrestore(&tp->rx_lock, flags);
2127 }
2128
hayeswang0de98f62013-08-16 16:09:35 +08002129submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002130 if (!ret) {
2131 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2132 } else {
2133 urb->actual_length = 0;
2134 list_add_tail(&agg->list, next);
2135 }
2136 }
2137
2138 if (!list_empty(&rx_queue)) {
2139 spin_lock_irqsave(&tp->rx_lock, flags);
2140 list_splice_tail(&rx_queue, &tp->rx_done);
2141 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002142 }
hayeswangd823ab62015-01-12 12:06:23 +08002143
2144out1:
2145 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002146}
2147
2148static void tx_bottom(struct r8152 *tp)
2149{
hayeswangebc2ec482013-08-14 20:54:38 +08002150 int res;
2151
hayeswangb1379d92013-08-16 16:09:37 +08002152 do {
2153 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002154
hayeswangb1379d92013-08-16 16:09:37 +08002155 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002156 break;
2157
hayeswangb1379d92013-08-16 16:09:37 +08002158 agg = r8152_get_tx_agg(tp);
2159 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002160 break;
hayeswangb1379d92013-08-16 16:09:37 +08002161
2162 res = r8152_tx_agg_fill(tp, agg);
2163 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002164 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002165
2166 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002167 rtl_set_unplug(tp);
hayeswangb1379d92013-08-16 16:09:37 +08002168 netif_device_detach(netdev);
2169 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002170 struct net_device_stats *stats = &netdev->stats;
2171 unsigned long flags;
2172
hayeswangb1379d92013-08-16 16:09:37 +08002173 netif_warn(tp, tx_err, netdev,
2174 "failed tx_urb %d\n", res);
2175 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002176
hayeswangb1379d92013-08-16 16:09:37 +08002177 spin_lock_irqsave(&tp->tx_lock, flags);
2178 list_add_tail(&agg->list, &tp->tx_free);
2179 spin_unlock_irqrestore(&tp->tx_lock, flags);
2180 }
hayeswangebc2ec482013-08-14 20:54:38 +08002181 }
hayeswangb1379d92013-08-16 16:09:37 +08002182 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002183}
2184
Hayes Wangd2187f82019-08-19 14:40:36 +08002185static void bottom_half(unsigned long data)
hayeswangebc2ec482013-08-14 20:54:38 +08002186{
Hayes Wangd2187f82019-08-19 14:40:36 +08002187 struct r8152 *tp;
2188
2189 tp = (struct r8152 *)data;
2190
hayeswangebc2ec482013-08-14 20:54:38 +08002191 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2192 return;
2193
2194 if (!test_bit(WORK_ENABLE, &tp->flags))
2195 return;
2196
hayeswang7559fb2f2013-08-16 16:09:38 +08002197 /* When link down, the driver would cancel all bulks. */
2198 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002199 if (!netif_carrier_ok(tp->netdev))
2200 return;
2201
Hayes Wangd2187f82019-08-19 14:40:36 +08002202 clear_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002203
hayeswang0c3121f2014-03-07 11:04:36 +08002204 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002205}
2206
hayeswangd823ab62015-01-12 12:06:23 +08002207static int r8152_poll(struct napi_struct *napi, int budget)
2208{
2209 struct r8152 *tp = container_of(napi, struct r8152, napi);
2210 int work_done;
2211
2212 work_done = rx_bottom(tp, budget);
hayeswangd823ab62015-01-12 12:06:23 +08002213
2214 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002215 if (!napi_complete_done(napi, work_done))
2216 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002217 if (!list_empty(&tp->rx_done))
2218 napi_schedule(napi);
2219 }
2220
hayeswanga3307f92017-06-09 17:11:48 +08002221out:
hayeswangd823ab62015-01-12 12:06:23 +08002222 return work_done;
2223}
2224
hayeswangebc2ec482013-08-14 20:54:38 +08002225static
2226int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2227{
hayeswanga0fccd42014-11-20 10:29:05 +08002228 int ret;
2229
hayeswangef827a52015-01-09 10:26:36 +08002230 /* The rx would be stopped, so skip submitting */
2231 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2232 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2233 return 0;
2234
hayeswangebc2ec482013-08-14 20:54:38 +08002235 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
Hayes Wangd55d7082019-08-13 11:42:07 +08002236 agg->buffer, tp->rx_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002237 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002238
hayeswanga0fccd42014-11-20 10:29:05 +08002239 ret = usb_submit_urb(agg->urb, mem_flags);
2240 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002241 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002242 netif_device_detach(tp->netdev);
2243 } else if (ret) {
2244 struct urb *urb = agg->urb;
2245 unsigned long flags;
2246
2247 urb->actual_length = 0;
2248 spin_lock_irqsave(&tp->rx_lock, flags);
2249 list_add_tail(&agg->list, &tp->rx_done);
2250 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002251
2252 netif_err(tp, rx_err, tp->netdev,
2253 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2254
2255 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002256 }
2257
2258 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002259}
2260
hayeswang00a5e362014-02-18 21:48:59 +08002261static void rtl_drop_queued_tx(struct r8152 *tp)
2262{
2263 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002264 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002265 struct sk_buff *skb;
2266
hayeswangd84130a2014-02-18 21:49:02 +08002267 if (skb_queue_empty(tx_queue))
2268 return;
2269
2270 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002271 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002272 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002273 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002274
2275 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002276 dev_kfree_skb(skb);
2277 stats->tx_dropped++;
2278 }
2279}
2280
hayeswangac718b62013-05-02 16:01:25 +00002281static void rtl8152_tx_timeout(struct net_device *netdev)
2282{
2283 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002284
Hayes Wang4a8deae2014-01-07 11:18:22 +08002285 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002286
2287 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002288}
2289
2290static void rtl8152_set_rx_mode(struct net_device *netdev)
2291{
2292 struct r8152 *tp = netdev_priv(netdev);
2293
hayeswang51d979f2015-02-06 11:30:47 +08002294 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002295 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002296 schedule_delayed_work(&tp->schedule, 0);
2297 }
hayeswangac718b62013-05-02 16:01:25 +00002298}
2299
2300static void _rtl8152_set_rx_mode(struct net_device *netdev)
2301{
2302 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002303 u32 mc_filter[2]; /* Multicast hash filter */
2304 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002305 u32 ocp_data;
2306
hayeswangac718b62013-05-02 16:01:25 +00002307 netif_stop_queue(netdev);
2308 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2309 ocp_data &= ~RCR_ACPT_ALL;
2310 ocp_data |= RCR_AB | RCR_APM;
2311
2312 if (netdev->flags & IFF_PROMISC) {
2313 /* Unconditionally log net taps. */
2314 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2315 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002316 mc_filter[1] = 0xffffffff;
2317 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002318 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2319 (netdev->flags & IFF_ALLMULTI)) {
2320 /* Too many to filter perfectly -- accept all multicasts. */
2321 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002322 mc_filter[1] = 0xffffffff;
2323 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002324 } else {
2325 struct netdev_hw_addr *ha;
2326
hayeswangb209af92014-08-25 15:53:00 +08002327 mc_filter[1] = 0;
2328 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002329 netdev_for_each_mc_addr(ha, netdev) {
2330 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002331
hayeswangac718b62013-05-02 16:01:25 +00002332 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2333 ocp_data |= RCR_AM;
2334 }
2335 }
2336
hayeswang31787f52013-07-31 17:21:25 +08002337 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2338 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002339
hayeswang31787f52013-07-31 17:21:25 +08002340 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002341 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2342 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002343}
2344
hayeswanga5e31252015-01-06 17:41:58 +08002345static netdev_features_t
2346rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2347 netdev_features_t features)
2348{
2349 u32 mss = skb_shinfo(skb)->gso_size;
2350 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2351 int offset = skb_transport_offset(skb);
2352
2353 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002354 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002355 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2356 features &= ~NETIF_F_GSO_MASK;
2357
2358 return features;
2359}
2360
hayeswangac718b62013-05-02 16:01:25 +00002361static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002362 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002363{
2364 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002365
hayeswangac718b62013-05-02 16:01:25 +00002366 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002367
hayeswang61598782013-11-20 17:30:55 +08002368 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002369
hayeswang0c3121f2014-03-07 11:04:36 +08002370 if (!list_empty(&tp->tx_free)) {
2371 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
Hayes Wangd2187f82019-08-19 14:40:36 +08002372 set_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002373 schedule_delayed_work(&tp->schedule, 0);
2374 } else {
2375 usb_mark_last_busy(tp->udev);
Hayes Wangd2187f82019-08-19 14:40:36 +08002376 tasklet_schedule(&tp->tx_tl);
hayeswang0c3121f2014-03-07 11:04:36 +08002377 }
hayeswangb209af92014-08-25 15:53:00 +08002378 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002379 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002380 }
hayeswangdd1b1192013-11-20 17:30:56 +08002381
hayeswangac718b62013-05-02 16:01:25 +00002382 return NETDEV_TX_OK;
2383}
2384
2385static void r8152b_reset_packet_filter(struct r8152 *tp)
2386{
2387 u32 ocp_data;
2388
2389 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2390 ocp_data &= ~FMC_FCR_MCU_EN;
2391 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2392 ocp_data |= FMC_FCR_MCU_EN;
2393 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2394}
2395
2396static void rtl8152_nic_reset(struct r8152 *tp)
2397{
2398 int i;
2399
2400 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2401
2402 for (i = 0; i < 1000; i++) {
2403 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2404 break;
hayeswangb209af92014-08-25 15:53:00 +08002405 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002406 }
2407}
2408
hayeswangdd1b1192013-11-20 17:30:56 +08002409static void set_tx_qlen(struct r8152 *tp)
2410{
2411 struct net_device *netdev = tp->netdev;
2412
hayeswangb65c0c92017-06-21 11:25:18 +08002413 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002414 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002415}
2416
hayeswangac718b62013-05-02 16:01:25 +00002417static inline u8 rtl8152_get_speed(struct r8152 *tp)
2418{
2419 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2420}
2421
hayeswang507605a2014-01-02 11:22:43 +08002422static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002423{
hayeswangebc2ec482013-08-14 20:54:38 +08002424 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002425 u8 speed;
2426
2427 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002428 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002429 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002430 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002431 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2432 } else {
2433 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002434 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002435 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2436 }
hayeswang507605a2014-01-02 11:22:43 +08002437}
2438
hayeswang00a5e362014-02-18 21:48:59 +08002439static void rxdy_gated_en(struct r8152 *tp, bool enable)
2440{
2441 u32 ocp_data;
2442
2443 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2444 if (enable)
2445 ocp_data |= RXDY_GATED_EN;
2446 else
2447 ocp_data &= ~RXDY_GATED_EN;
2448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2449}
2450
hayeswang445f7f42014-09-23 16:31:47 +08002451static int rtl_start_rx(struct r8152 *tp)
2452{
Hayes Wang252df8b2019-08-13 11:42:06 +08002453 struct rx_agg *agg, *agg_next;
2454 struct list_head tmp_list;
2455 unsigned long flags;
Hayes Wang47922fc2019-08-13 11:42:08 +08002456 int ret = 0, i = 0;
Hayes Wang252df8b2019-08-13 11:42:06 +08002457
2458 INIT_LIST_HEAD(&tmp_list);
2459
2460 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002461
2462 INIT_LIST_HEAD(&tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002463 INIT_LIST_HEAD(&tp->rx_used);
Hayes Wang252df8b2019-08-13 11:42:06 +08002464
2465 list_splice_init(&tp->rx_info, &tmp_list);
2466
2467 spin_unlock_irqrestore(&tp->rx_lock, flags);
2468
2469 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2470 INIT_LIST_HEAD(&agg->list);
2471
Hayes Wang47922fc2019-08-13 11:42:08 +08002472 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2473 if (++i > RTL8152_MAX_RX) {
2474 spin_lock_irqsave(&tp->rx_lock, flags);
2475 list_add_tail(&agg->list, &tp->rx_used);
2476 spin_unlock_irqrestore(&tp->rx_lock, flags);
2477 } else if (unlikely(ret < 0)) {
2478 spin_lock_irqsave(&tp->rx_lock, flags);
Hayes Wang252df8b2019-08-13 11:42:06 +08002479 list_add_tail(&agg->list, &tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002480 spin_unlock_irqrestore(&tp->rx_lock, flags);
2481 } else {
Hayes Wang252df8b2019-08-13 11:42:06 +08002482 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
Hayes Wang47922fc2019-08-13 11:42:08 +08002483 }
hayeswang445f7f42014-09-23 16:31:47 +08002484 }
2485
Hayes Wang252df8b2019-08-13 11:42:06 +08002486 spin_lock_irqsave(&tp->rx_lock, flags);
2487 WARN_ON(!list_empty(&tp->rx_info));
2488 list_splice(&tmp_list, &tp->rx_info);
2489 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang7bcf4f62014-11-20 10:29:06 +08002490
hayeswang445f7f42014-09-23 16:31:47 +08002491 return ret;
2492}
2493
2494static int rtl_stop_rx(struct r8152 *tp)
2495{
Hayes Wang252df8b2019-08-13 11:42:06 +08002496 struct rx_agg *agg, *agg_next;
2497 struct list_head tmp_list;
2498 unsigned long flags;
hayeswang445f7f42014-09-23 16:31:47 +08002499
Hayes Wang252df8b2019-08-13 11:42:06 +08002500 INIT_LIST_HEAD(&tmp_list);
2501
2502 /* The usb_kill_urb() couldn't be used in atomic.
2503 * Therefore, move the list of rx_info to a tmp one.
2504 * Then, list_for_each_entry_safe could be used without
2505 * spin lock.
2506 */
2507
2508 spin_lock_irqsave(&tp->rx_lock, flags);
2509 list_splice_init(&tp->rx_info, &tmp_list);
2510 spin_unlock_irqrestore(&tp->rx_lock, flags);
2511
Hayes Wang47922fc2019-08-13 11:42:08 +08002512 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2513 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2514 * equal to 1, so the other ones could be freed safely.
2515 */
2516 if (page_count(agg->page) > 1)
2517 free_rx_agg(tp, agg);
2518 else
2519 usb_kill_urb(agg->urb);
2520 }
Hayes Wang252df8b2019-08-13 11:42:06 +08002521
2522 /* Move back the list of temp to the rx_info */
2523 spin_lock_irqsave(&tp->rx_lock, flags);
2524 WARN_ON(!list_empty(&tp->rx_info));
2525 list_splice(&tmp_list, &tp->rx_info);
2526 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002527
hayeswangd823ab62015-01-12 12:06:23 +08002528 while (!skb_queue_empty(&tp->rx_queue))
2529 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2530
hayeswang445f7f42014-09-23 16:31:47 +08002531 return 0;
2532}
2533
Hayes Wang9fae5412019-07-03 15:11:56 +08002534static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2535{
2536 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2537 OWN_UPDATE | OWN_CLEAR);
2538}
2539
hayeswang507605a2014-01-02 11:22:43 +08002540static int rtl_enable(struct r8152 *tp)
2541{
2542 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002543
2544 r8152b_reset_packet_filter(tp);
2545
2546 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2547 ocp_data |= CR_RE | CR_TE;
2548 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2549
Hayes Wang9fae5412019-07-03 15:11:56 +08002550 switch (tp->version) {
2551 case RTL_VER_08:
2552 case RTL_VER_09:
2553 r8153b_rx_agg_chg_indicate(tp);
2554 break;
2555 default:
2556 break;
2557 }
2558
hayeswang00a5e362014-02-18 21:48:59 +08002559 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002560
hayeswangaa2e0922015-01-09 10:26:35 +08002561 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002562}
2563
hayeswang507605a2014-01-02 11:22:43 +08002564static int rtl8152_enable(struct r8152 *tp)
2565{
hayeswang68714382014-04-11 17:54:31 +08002566 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2567 return -ENODEV;
2568
hayeswang507605a2014-01-02 11:22:43 +08002569 set_tx_qlen(tp);
2570 rtl_set_eee_plus(tp);
2571
2572 return rtl_enable(tp);
2573}
2574
hayeswang464ec102015-02-12 14:33:46 +08002575static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002576{
hayeswang464ec102015-02-12 14:33:46 +08002577 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002578
hayeswang65b82d62017-06-15 14:44:03 +08002579 switch (tp->version) {
2580 case RTL_VER_03:
2581 case RTL_VER_04:
2582 case RTL_VER_05:
2583 case RTL_VER_06:
2584 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2585 ocp_data);
2586 break;
2587
2588 case RTL_VER_08:
2589 case RTL_VER_09:
2590 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2591 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2592 */
2593 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2594 128 / 8);
2595 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2596 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002597 break;
2598
2599 default:
2600 break;
2601 }
hayeswang464ec102015-02-12 14:33:46 +08002602}
2603
2604static void r8153_set_rx_early_size(struct r8152 *tp)
2605{
Hayes Wangec5791c2019-08-13 11:42:05 +08002606 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002607
hayeswang65b82d62017-06-15 14:44:03 +08002608 switch (tp->version) {
2609 case RTL_VER_03:
2610 case RTL_VER_04:
2611 case RTL_VER_05:
2612 case RTL_VER_06:
2613 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2614 ocp_data / 4);
2615 break;
2616 case RTL_VER_08:
2617 case RTL_VER_09:
2618 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2619 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002620 break;
2621 default:
2622 WARN_ON_ONCE(1);
2623 break;
2624 }
hayeswang43779f82014-01-02 11:25:10 +08002625}
2626
2627static int rtl8153_enable(struct r8152 *tp)
2628{
hayeswang68714382014-04-11 17:54:31 +08002629 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2630 return -ENODEV;
2631
hayeswang43779f82014-01-02 11:25:10 +08002632 set_tx_qlen(tp);
2633 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002634 r8153_set_rx_early_timeout(tp);
2635 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002636
2637 return rtl_enable(tp);
2638}
2639
hayeswangd70b1132014-09-19 15:17:18 +08002640static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002641{
hayeswangebc2ec482013-08-14 20:54:38 +08002642 u32 ocp_data;
2643 int i;
hayeswangac718b62013-05-02 16:01:25 +00002644
hayeswang68714382014-04-11 17:54:31 +08002645 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2646 rtl_drop_queued_tx(tp);
2647 return;
2648 }
2649
hayeswangac718b62013-05-02 16:01:25 +00002650 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2651 ocp_data &= ~RCR_ACPT_ALL;
2652 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2653
hayeswang00a5e362014-02-18 21:48:59 +08002654 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002655
2656 for (i = 0; i < RTL8152_MAX_TX; i++)
2657 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002658
hayeswang00a5e362014-02-18 21:48:59 +08002659 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002660
2661 for (i = 0; i < 1000; i++) {
2662 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2663 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2664 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002665 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002666 }
2667
2668 for (i = 0; i < 1000; i++) {
2669 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2670 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002671 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002672 }
2673
hayeswang445f7f42014-09-23 16:31:47 +08002674 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002675
2676 rtl8152_nic_reset(tp);
2677}
2678
hayeswang00a5e362014-02-18 21:48:59 +08002679static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2680{
2681 u32 ocp_data;
2682
2683 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2684 if (enable)
2685 ocp_data |= POWER_CUT;
2686 else
2687 ocp_data &= ~POWER_CUT;
2688 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2689
2690 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2691 ocp_data &= ~RESUME_INDICATE;
2692 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002693}
2694
hayeswangc5554292014-09-12 10:43:11 +08002695static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2696{
2697 u32 ocp_data;
2698
2699 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2700 if (enable)
2701 ocp_data |= CPCR_RX_VLAN;
2702 else
2703 ocp_data &= ~CPCR_RX_VLAN;
2704 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2705}
2706
2707static int rtl8152_set_features(struct net_device *dev,
2708 netdev_features_t features)
2709{
2710 netdev_features_t changed = features ^ dev->features;
2711 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002712 int ret;
2713
2714 ret = usb_autopm_get_interface(tp->intf);
2715 if (ret < 0)
2716 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002717
hayeswangb5403272014-10-09 18:00:26 +08002718 mutex_lock(&tp->control);
2719
hayeswangc5554292014-09-12 10:43:11 +08002720 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2721 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2722 rtl_rx_vlan_en(tp, true);
2723 else
2724 rtl_rx_vlan_en(tp, false);
2725 }
2726
hayeswangb5403272014-10-09 18:00:26 +08002727 mutex_unlock(&tp->control);
2728
hayeswang405f8a02014-10-09 18:00:24 +08002729 usb_autopm_put_interface(tp->intf);
2730
2731out:
2732 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002733}
2734
hayeswang21ff2e82014-02-18 21:49:06 +08002735#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2736
2737static u32 __rtl_get_wol(struct r8152 *tp)
2738{
2739 u32 ocp_data;
2740 u32 wolopts = 0;
2741
hayeswang21ff2e82014-02-18 21:49:06 +08002742 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2743 if (ocp_data & LINK_ON_WAKE_EN)
2744 wolopts |= WAKE_PHY;
2745
2746 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2747 if (ocp_data & UWF_EN)
2748 wolopts |= WAKE_UCAST;
2749 if (ocp_data & BWF_EN)
2750 wolopts |= WAKE_BCAST;
2751 if (ocp_data & MWF_EN)
2752 wolopts |= WAKE_MCAST;
2753
2754 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2755 if (ocp_data & MAGIC_EN)
2756 wolopts |= WAKE_MAGIC;
2757
2758 return wolopts;
2759}
2760
2761static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2762{
2763 u32 ocp_data;
2764
2765 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2766
2767 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2768 ocp_data &= ~LINK_ON_WAKE_EN;
2769 if (wolopts & WAKE_PHY)
2770 ocp_data |= LINK_ON_WAKE_EN;
2771 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2772
2773 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002774 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002775 if (wolopts & WAKE_UCAST)
2776 ocp_data |= UWF_EN;
2777 if (wolopts & WAKE_BCAST)
2778 ocp_data |= BWF_EN;
2779 if (wolopts & WAKE_MCAST)
2780 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002781 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2782
2783 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2784
2785 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2786 ocp_data &= ~MAGIC_EN;
2787 if (wolopts & WAKE_MAGIC)
2788 ocp_data |= MAGIC_EN;
2789 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2790
2791 if (wolopts & WAKE_ANY)
2792 device_set_wakeup_enable(&tp->udev->dev, true);
2793 else
2794 device_set_wakeup_enable(&tp->udev->dev, false);
2795}
2796
hayeswang134f98b2017-06-09 17:11:40 +08002797static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2798{
2799 /* MAC clock speed down */
2800 if (enable) {
2801 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2802 ALDPS_SPDWN_RATIO);
2803 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2804 EEE_SPDWN_RATIO);
2805 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2806 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2807 U1U2_SPDWN_EN | L1_SPDWN_EN);
2808 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2809 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2810 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2811 TP1000_SPDWN_EN);
2812 } else {
2813 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2814 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2815 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2816 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2817 }
2818}
2819
hayeswangb2143962015-07-24 13:54:23 +08002820static void r8153_u1u2en(struct r8152 *tp, bool enable)
2821{
2822 u8 u1u2[8];
2823
2824 if (enable)
2825 memset(u1u2, 0xff, sizeof(u1u2));
2826 else
2827 memset(u1u2, 0x00, sizeof(u1u2));
2828
2829 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2830}
2831
hayeswang65b82d62017-06-15 14:44:03 +08002832static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2833{
2834 u32 ocp_data;
2835
2836 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2837 if (enable)
2838 ocp_data |= LPM_U1U2_EN;
2839 else
2840 ocp_data &= ~LPM_U1U2_EN;
2841
2842 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2843}
2844
hayeswangb2143962015-07-24 13:54:23 +08002845static void r8153_u2p3en(struct r8152 *tp, bool enable)
2846{
2847 u32 ocp_data;
2848
2849 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002850 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002851 ocp_data |= U2P3_ENABLE;
2852 else
2853 ocp_data &= ~U2P3_ENABLE;
2854 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2855}
2856
hayeswang65b82d62017-06-15 14:44:03 +08002857static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2858{
2859 u32 ocp_data;
2860
2861 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2862 ocp_data &= ~clear;
2863 ocp_data |= set;
2864 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2865}
2866
2867static void r8153b_green_en(struct r8152 *tp, bool enable)
2868{
2869 u16 data;
2870
2871 if (enable) {
2872 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2873 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2874 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2875 } else {
2876 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2877 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2878 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2879 }
2880
2881 data = sram_read(tp, SRAM_GREEN_CFG);
2882 data |= GREEN_ETH_EN;
2883 sram_write(tp, SRAM_GREEN_CFG, data);
2884
2885 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2886}
2887
hayeswangc564b872017-06-09 17:11:38 +08002888static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2889{
2890 u16 data;
2891 int i;
2892
2893 for (i = 0; i < 500; i++) {
2894 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2895 data &= PHY_STAT_MASK;
2896 if (desired) {
2897 if (data == desired)
2898 break;
2899 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2900 data == PHY_STAT_EXT_INIT) {
2901 break;
2902 }
2903
2904 msleep(20);
2905 }
2906
2907 return data;
2908}
2909
hayeswang65b82d62017-06-15 14:44:03 +08002910static void r8153b_ups_en(struct r8152 *tp, bool enable)
2911{
2912 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2913
2914 if (enable) {
2915 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2916 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2917
2918 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2919 ocp_data |= BIT(0);
2920 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2921 } else {
2922 u16 data;
2923
2924 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2925 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2926
2927 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2928 ocp_data &= ~BIT(0);
2929 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2930
2931 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2932 ocp_data &= ~PCUT_STATUS;
2933 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2934
2935 data = r8153_phy_status(tp, 0);
2936
2937 switch (data) {
2938 case PHY_STAT_PWRDN:
2939 case PHY_STAT_EXT_INIT:
2940 r8153b_green_en(tp,
2941 test_bit(GREEN_ETHERNET, &tp->flags));
2942
2943 data = r8152_mdio_read(tp, MII_BMCR);
2944 data &= ~BMCR_PDOWN;
2945 data |= BMCR_RESET;
2946 r8152_mdio_write(tp, MII_BMCR, data);
2947
2948 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002949 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002950
2951 default:
2952 if (data != PHY_STAT_LAN_ON)
2953 netif_warn(tp, link, tp->netdev,
2954 "PHY not ready");
2955 break;
2956 }
2957 }
2958}
2959
hayeswangb2143962015-07-24 13:54:23 +08002960static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2961{
2962 u32 ocp_data;
2963
2964 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2965 if (enable)
2966 ocp_data |= PWR_EN | PHASE2_EN;
2967 else
2968 ocp_data &= ~(PWR_EN | PHASE2_EN);
2969 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2970
2971 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2972 ocp_data &= ~PCUT_STATUS;
2973 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2974}
2975
hayeswang65b82d62017-06-15 14:44:03 +08002976static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2977{
2978 u32 ocp_data;
2979
2980 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2981 if (enable)
2982 ocp_data |= PWR_EN | PHASE2_EN;
2983 else
2984 ocp_data &= ~PWR_EN;
2985 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2986
2987 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2988 ocp_data &= ~PCUT_STATUS;
2989 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2990}
2991
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002992static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08002993{
2994 u32 ocp_data;
2995
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002996 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08002997 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002998 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08002999 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003000 ocp_data &= ~UPCOMING_RUNTIME_D3;
3001 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003002
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003003 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3004 ocp_data &= ~LINK_CHG_EVENT;
3005 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3006
3007 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3008 ocp_data &= ~LINK_CHANGE_FLAG;
3009 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003010}
3011
hayeswang7daed8d2015-07-24 13:54:24 +08003012static bool rtl_can_wakeup(struct r8152 *tp)
3013{
3014 struct usb_device *udev = tp->udev;
3015
3016 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3017}
3018
hayeswang9a4be1b2014-02-18 21:49:07 +08003019static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3020{
3021 if (enable) {
3022 u32 ocp_data;
3023
3024 __rtl_set_wol(tp, WAKE_ANY);
3025
3026 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3027
3028 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3029 ocp_data |= LINK_OFF_WAKE_EN;
3030 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3031
3032 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3033 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08003034 u32 ocp_data;
3035
hayeswang9a4be1b2014-02-18 21:49:07 +08003036 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08003037
3038 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3039
3040 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3041 ocp_data &= ~LINK_OFF_WAKE_EN;
3042 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3043
3044 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08003045 }
3046}
hayeswangf95ae8a2016-06-30 15:33:35 +08003047
hayeswang2609af12016-07-05 16:11:46 +08003048static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3049{
hayeswang2609af12016-07-05 16:11:46 +08003050 if (enable) {
3051 r8153_u1u2en(tp, false);
3052 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003053 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08003054 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08003055 } else {
hayeswang02552752017-06-09 17:11:42 +08003056 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003057 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003058
3059 switch (tp->version) {
3060 case RTL_VER_03:
3061 case RTL_VER_04:
3062 break;
3063 case RTL_VER_05:
3064 case RTL_VER_06:
3065 default:
3066 r8153_u2p3en(tp, true);
3067 break;
3068 }
3069
hayeswangb2143962015-07-24 13:54:23 +08003070 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08003071 }
3072}
3073
hayeswang65b82d62017-06-15 14:44:03 +08003074static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3075{
3076 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003077 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003078 r8153b_u1u2en(tp, false);
3079 r8153_u2p3en(tp, false);
3080 rtl_runtime_suspend_enable(tp, true);
3081 r8153b_ups_en(tp, true);
3082 } else {
3083 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003084 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003085 rtl_runtime_suspend_enable(tp, false);
3086 r8153_u2p3en(tp, true);
3087 r8153b_u1u2en(tp, true);
3088 }
3089}
3090
hayeswang43499682014-02-18 21:48:58 +08003091static void r8153_teredo_off(struct r8152 *tp)
3092{
3093 u32 ocp_data;
3094
hayeswang65b82d62017-06-15 14:44:03 +08003095 switch (tp->version) {
3096 case RTL_VER_01:
3097 case RTL_VER_02:
3098 case RTL_VER_03:
3099 case RTL_VER_04:
3100 case RTL_VER_05:
3101 case RTL_VER_06:
3102 case RTL_VER_07:
3103 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3104 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3105 OOB_TEREDO_EN);
3106 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3107 break;
3108
3109 case RTL_VER_08:
3110 case RTL_VER_09:
3111 /* The bit 0 ~ 7 are relative with teredo settings. They are
3112 * W1C (write 1 to clear), so set all 1 to disable it.
3113 */
3114 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3115 break;
3116
3117 default:
3118 break;
3119 }
hayeswang43499682014-02-18 21:48:58 +08003120
3121 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3122 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3123 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3124}
3125
hayeswang93fe9b12016-06-16 10:55:18 +08003126static void rtl_reset_bmu(struct r8152 *tp)
3127{
3128 u32 ocp_data;
3129
3130 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3131 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3132 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3133 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3134 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3135}
3136
hayeswangcda9fb02016-01-07 17:51:12 +08003137static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08003138{
hayeswangcda9fb02016-01-07 17:51:12 +08003139 if (enable) {
3140 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
3141 LINKENA | DIS_SDSAVE);
3142 } else {
3143 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
3144 DIS_SDSAVE);
3145 msleep(20);
3146 }
hayeswang43499682014-02-18 21:48:58 +08003147}
3148
hayeswange6449532016-09-20 16:22:05 +08003149static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
3150{
3151 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3152 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3153 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3154}
3155
3156static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3157{
3158 u16 data;
3159
3160 r8152_mmd_indirect(tp, dev, reg);
3161 data = ocp_reg_read(tp, OCP_EEE_DATA);
3162 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3163
3164 return data;
3165}
3166
3167static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3168{
3169 r8152_mmd_indirect(tp, dev, reg);
3170 ocp_reg_write(tp, OCP_EEE_DATA, data);
3171 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3172}
3173
3174static void r8152_eee_en(struct r8152 *tp, bool enable)
3175{
3176 u16 config1, config2, config3;
3177 u32 ocp_data;
3178
3179 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3180 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3181 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3182 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3183
3184 if (enable) {
3185 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3186 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3187 config1 |= sd_rise_time(1);
3188 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3189 config3 |= fast_snr(42);
3190 } else {
3191 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3192 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3193 RX_QUIET_EN);
3194 config1 |= sd_rise_time(7);
3195 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3196 config3 |= fast_snr(511);
3197 }
3198
3199 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3200 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3201 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3202 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3203}
3204
3205static void r8152b_enable_eee(struct r8152 *tp)
3206{
Hayes Wangf4a93be2019-08-23 15:33:40 +08003207 if (tp->eee_en) {
3208 r8152_eee_en(tp, true);
3209 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, tp->eee_adv);
3210 } else {
3211 r8152_eee_en(tp, false);
3212 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
3213 }
hayeswange6449532016-09-20 16:22:05 +08003214}
3215
3216static void r8152b_enable_fc(struct r8152 *tp)
3217{
3218 u16 anar;
3219
3220 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3221 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3222 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3223}
3224
hayeswangd70b1132014-09-19 15:17:18 +08003225static void rtl8152_disable(struct r8152 *tp)
3226{
hayeswangcda9fb02016-01-07 17:51:12 +08003227 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003228 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003229 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003230}
3231
hayeswang43499682014-02-18 21:48:58 +08003232static void r8152b_hw_phy_cfg(struct r8152 *tp)
3233{
hayeswangef39df82016-09-20 16:22:07 +08003234 r8152b_enable_eee(tp);
3235 r8152_aldps_en(tp, true);
3236 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003237
hayeswangaa66a5f2014-02-18 21:49:04 +08003238 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003239}
3240
hayeswangac718b62013-05-02 16:01:25 +00003241static void r8152b_exit_oob(struct r8152 *tp)
3242{
hayeswangdb8515e2014-03-06 15:07:16 +08003243 u32 ocp_data;
3244 int i;
hayeswangac718b62013-05-02 16:01:25 +00003245
3246 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3247 ocp_data &= ~RCR_ACPT_ALL;
3248 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3249
hayeswang00a5e362014-02-18 21:48:59 +08003250 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003251 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003252 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3253 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3254
3255 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3256 ocp_data &= ~NOW_IS_OOB;
3257 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3258
3259 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3260 ocp_data &= ~MCU_BORW_EN;
3261 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3262
3263 for (i = 0; i < 1000; i++) {
3264 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3265 if (ocp_data & LINK_LIST_READY)
3266 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003267 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003268 }
3269
3270 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3271 ocp_data |= RE_INIT_LL;
3272 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3273
3274 for (i = 0; i < 1000; i++) {
3275 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3276 if (ocp_data & LINK_LIST_READY)
3277 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003278 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003279 }
3280
3281 rtl8152_nic_reset(tp);
3282
3283 /* rx share fifo credit full threshold */
3284 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3285
hayeswanga3cc4652014-07-24 16:37:43 +08003286 if (tp->udev->speed == USB_SPEED_FULL ||
3287 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003288 /* rx share fifo credit near full threshold */
3289 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3290 RXFIFO_THR2_FULL);
3291 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3292 RXFIFO_THR3_FULL);
3293 } else {
3294 /* rx share fifo credit near full threshold */
3295 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3296 RXFIFO_THR2_HIGH);
3297 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3298 RXFIFO_THR3_HIGH);
3299 }
3300
3301 /* TX share fifo free credit full threshold */
3302 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3303
3304 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003305 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003306 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3307 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3308
hayeswangc5554292014-09-12 10:43:11 +08003309 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003310
3311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3312
3313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3314 ocp_data |= TCR0_AUTO_FIFO;
3315 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3316}
3317
3318static void r8152b_enter_oob(struct r8152 *tp)
3319{
hayeswang45f4a192014-01-06 17:08:41 +08003320 u32 ocp_data;
3321 int i;
hayeswangac718b62013-05-02 16:01:25 +00003322
3323 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3324 ocp_data &= ~NOW_IS_OOB;
3325 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3326
3327 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3328 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3329 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3330
hayeswangd70b1132014-09-19 15:17:18 +08003331 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003332
3333 for (i = 0; i < 1000; i++) {
3334 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3335 if (ocp_data & LINK_LIST_READY)
3336 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003337 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003338 }
3339
3340 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3341 ocp_data |= RE_INIT_LL;
3342 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3343
3344 for (i = 0; i < 1000; i++) {
3345 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3346 if (ocp_data & LINK_LIST_READY)
3347 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003348 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003349 }
3350
3351 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3352
hayeswangc5554292014-09-12 10:43:11 +08003353 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003354
Kevin Lo59c0b472019-08-01 11:29:38 +08003355 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00003356 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003357 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00003358
3359 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3360 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3361 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3362
hayeswang00a5e362014-02-18 21:48:59 +08003363 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003364
3365 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3366 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3367 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3368}
3369
hayeswang65b82d62017-06-15 14:44:03 +08003370static int r8153_patch_request(struct r8152 *tp, bool request)
3371{
3372 u16 data;
3373 int i;
3374
3375 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3376 if (request)
3377 data |= PATCH_REQUEST;
3378 else
3379 data &= ~PATCH_REQUEST;
3380 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3381
3382 for (i = 0; request && i < 5000; i++) {
3383 usleep_range(1000, 2000);
3384 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3385 break;
3386 }
3387
3388 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3389 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3390 r8153_patch_request(tp, false);
3391 return -ETIME;
3392 } else {
3393 return 0;
3394 }
3395}
3396
hayeswange6449532016-09-20 16:22:05 +08003397static void r8153_aldps_en(struct r8152 *tp, bool enable)
3398{
3399 u16 data;
3400
3401 data = ocp_reg_read(tp, OCP_POWER_CFG);
3402 if (enable) {
3403 data |= EN_ALDPS;
3404 ocp_reg_write(tp, OCP_POWER_CFG, data);
3405 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003406 int i;
3407
hayeswange6449532016-09-20 16:22:05 +08003408 data &= ~EN_ALDPS;
3409 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003410 for (i = 0; i < 20; i++) {
3411 usleep_range(1000, 2000);
3412 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3413 break;
3414 }
hayeswange6449532016-09-20 16:22:05 +08003415 }
3416}
3417
hayeswang65b82d62017-06-15 14:44:03 +08003418static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3419{
3420 r8153_aldps_en(tp, enable);
3421
3422 if (enable)
3423 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3424 else
3425 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3426}
3427
hayeswange6449532016-09-20 16:22:05 +08003428static void r8153_eee_en(struct r8152 *tp, bool enable)
3429{
3430 u32 ocp_data;
3431 u16 config;
3432
3433 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3434 config = ocp_reg_read(tp, OCP_EEE_CFG);
3435
3436 if (enable) {
3437 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3438 config |= EEE10_EN;
3439 } else {
3440 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3441 config &= ~EEE10_EN;
3442 }
3443
3444 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3445 ocp_reg_write(tp, OCP_EEE_CFG, config);
3446}
3447
hayeswang65b82d62017-06-15 14:44:03 +08003448static void r8153b_eee_en(struct r8152 *tp, bool enable)
3449{
3450 r8153_eee_en(tp, enable);
3451
3452 if (enable)
3453 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3454 else
3455 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3456}
3457
3458static void r8153b_enable_fc(struct r8152 *tp)
3459{
3460 r8152b_enable_fc(tp);
3461 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3462}
3463
hayeswang43779f82014-01-02 11:25:10 +08003464static void r8153_hw_phy_cfg(struct r8152 *tp)
3465{
3466 u32 ocp_data;
3467 u16 data;
3468
hayeswangd768c612016-09-20 16:22:09 +08003469 /* disable ALDPS before updating the PHY parameters */
3470 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003471
hayeswangd768c612016-09-20 16:22:09 +08003472 /* disable EEE before updating the PHY parameters */
3473 r8153_eee_en(tp, false);
3474 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang43779f82014-01-02 11:25:10 +08003475
3476 if (tp->version == RTL_VER_03) {
3477 data = ocp_reg_read(tp, OCP_EEE_CFG);
3478 data &= ~CTAP_SHORT_EN;
3479 ocp_reg_write(tp, OCP_EEE_CFG, data);
3480 }
3481
3482 data = ocp_reg_read(tp, OCP_POWER_CFG);
3483 data |= EEE_CLKDIV_EN;
3484 ocp_reg_write(tp, OCP_POWER_CFG, data);
3485
3486 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3487 data |= EN_10M_BGOFF;
3488 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3489 data = ocp_reg_read(tp, OCP_POWER_CFG);
3490 data |= EN_10M_PLLOFF;
3491 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003492 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003493
3494 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3495 ocp_data |= PFM_PWM_SWITCH;
3496 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3497
hayeswangb4d99de2015-01-19 17:02:46 +08003498 /* Enable LPF corner auto tune */
3499 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003500
hayeswangb4d99de2015-01-19 17:02:46 +08003501 /* Adjust 10M Amplitude */
3502 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3503 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003504
Hayes Wangf4a93be2019-08-23 15:33:40 +08003505 if (tp->eee_en) {
3506 r8153_eee_en(tp, true);
3507 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
3508 }
hayeswangaf0287e2016-09-20 16:22:08 +08003509
hayeswangef39df82016-09-20 16:22:07 +08003510 r8153_aldps_en(tp, true);
3511 r8152b_enable_fc(tp);
3512
hayeswang3cb32342017-06-09 17:11:43 +08003513 switch (tp->version) {
3514 case RTL_VER_03:
3515 case RTL_VER_04:
3516 break;
3517 case RTL_VER_05:
3518 case RTL_VER_06:
3519 default:
3520 r8153_u2p3en(tp, true);
3521 break;
3522 }
3523
hayeswangaa66a5f2014-02-18 21:49:04 +08003524 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003525}
3526
hayeswang65b82d62017-06-15 14:44:03 +08003527static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3528{
3529 u32 ocp_data;
3530
3531 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3532 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3533 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3534 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3535
3536 return ocp_data;
3537}
3538
3539static void r8153b_hw_phy_cfg(struct r8152 *tp)
3540{
3541 u32 ocp_data, ups_flags = 0;
3542 u16 data;
3543
3544 /* disable ALDPS before updating the PHY parameters */
3545 r8153b_aldps_en(tp, false);
3546
3547 /* disable EEE before updating the PHY parameters */
3548 r8153b_eee_en(tp, false);
3549 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3550
3551 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3552
3553 data = sram_read(tp, SRAM_GREEN_CFG);
3554 data |= R_TUNE_EN;
3555 sram_write(tp, SRAM_GREEN_CFG, data);
3556 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3557 data |= PGA_RETURN_EN;
3558 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3559
3560 /* ADC Bias Calibration:
3561 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3562 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3563 * ADC ioffset.
3564 */
3565 ocp_data = r8152_efuse_read(tp, 0x7d);
3566 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3567 if (data != 0xffff)
3568 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3569
3570 /* ups mode tx-link-pulse timing adjustment:
3571 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3572 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3573 */
3574 ocp_data = ocp_reg_read(tp, 0xc426);
3575 ocp_data &= 0x3fff;
3576 if (ocp_data) {
3577 u32 swr_cnt_1ms_ini;
3578
3579 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3580 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3581 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3582 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3583 }
3584
3585 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3586 ocp_data |= PFM_PWM_SWITCH;
3587 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3588
3589 /* Advnace EEE */
3590 if (!r8153_patch_request(tp, true)) {
3591 data = ocp_reg_read(tp, OCP_POWER_CFG);
3592 data |= EEE_CLKDIV_EN;
3593 ocp_reg_write(tp, OCP_POWER_CFG, data);
3594
3595 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3596 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3597 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3598
3599 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3600 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3601
3602 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3603 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3604 UPS_FLAGS_EEE_PLLOFF_GIGA;
3605
3606 r8153_patch_request(tp, false);
3607 }
3608
3609 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3610
Hayes Wangf4a93be2019-08-23 15:33:40 +08003611 if (tp->eee_en) {
3612 r8153b_eee_en(tp, true);
3613 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
3614 }
hayeswang65b82d62017-06-15 14:44:03 +08003615
3616 r8153b_aldps_en(tp, true);
3617 r8153b_enable_fc(tp);
3618 r8153_u2p3en(tp, true);
3619
3620 set_bit(PHY_RESET, &tp->flags);
3621}
3622
hayeswang43779f82014-01-02 11:25:10 +08003623static void r8153_first_init(struct r8152 *tp)
3624{
3625 u32 ocp_data;
3626 int i;
3627
hayeswang134f98b2017-06-09 17:11:40 +08003628 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003629 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003630 r8153_teredo_off(tp);
3631
3632 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3633 ocp_data &= ~RCR_ACPT_ALL;
3634 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3635
hayeswang43779f82014-01-02 11:25:10 +08003636 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003637 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003638
3639 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3640 ocp_data &= ~NOW_IS_OOB;
3641 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3642
3643 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3644 ocp_data &= ~MCU_BORW_EN;
3645 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3646
3647 for (i = 0; i < 1000; i++) {
3648 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3649 if (ocp_data & LINK_LIST_READY)
3650 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003651 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003652 }
3653
3654 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3655 ocp_data |= RE_INIT_LL;
3656 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3657
3658 for (i = 0; i < 1000; i++) {
3659 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3660 if (ocp_data & LINK_LIST_READY)
3661 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003662 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003663 }
3664
hayeswangc5554292014-09-12 10:43:11 +08003665 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003666
hayeswangb65c0c92017-06-21 11:25:18 +08003667 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003668 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003669 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003670
3671 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3672 ocp_data |= TCR0_AUTO_FIFO;
3673 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3674
3675 rtl8152_nic_reset(tp);
3676
3677 /* rx share fifo credit full threshold */
3678 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3679 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3680 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3681 /* TX share fifo free credit full threshold */
3682 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003683}
3684
3685static void r8153_enter_oob(struct r8152 *tp)
3686{
3687 u32 ocp_data;
3688 int i;
3689
hayeswang134f98b2017-06-09 17:11:40 +08003690 r8153_mac_clk_spd(tp, true);
3691
hayeswang43779f82014-01-02 11:25:10 +08003692 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3693 ocp_data &= ~NOW_IS_OOB;
3694 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3695
hayeswangd70b1132014-09-19 15:17:18 +08003696 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003697 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003698
3699 for (i = 0; i < 1000; i++) {
3700 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3701 if (ocp_data & LINK_LIST_READY)
3702 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003703 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003704 }
3705
3706 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3707 ocp_data |= RE_INIT_LL;
3708 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3709
3710 for (i = 0; i < 1000; i++) {
3711 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3712 if (ocp_data & LINK_LIST_READY)
3713 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003714 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003715 }
3716
hayeswangb65c0c92017-06-21 11:25:18 +08003717 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003718 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003719
hayeswang65b82d62017-06-15 14:44:03 +08003720 switch (tp->version) {
3721 case RTL_VER_03:
3722 case RTL_VER_04:
3723 case RTL_VER_05:
3724 case RTL_VER_06:
3725 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3726 ocp_data &= ~TEREDO_WAKE_MASK;
3727 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3728 break;
3729
3730 case RTL_VER_08:
3731 case RTL_VER_09:
3732 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3733 * type. Set it to zero. bits[7:0] are the W1C bits about
3734 * the events. Set them to all 1 to clear them.
3735 */
3736 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3737 break;
3738
3739 default:
3740 break;
3741 }
hayeswang43779f82014-01-02 11:25:10 +08003742
hayeswangc5554292014-09-12 10:43:11 +08003743 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003744
Kevin Lo59c0b472019-08-01 11:29:38 +08003745 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08003746 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003747 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003748
3749 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3750 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3751 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3752
hayeswang00a5e362014-02-18 21:48:59 +08003753 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003754
3755 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3756 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3757 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3758}
3759
hayeswangd70b1132014-09-19 15:17:18 +08003760static void rtl8153_disable(struct r8152 *tp)
3761{
hayeswangcda9fb02016-01-07 17:51:12 +08003762 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003763 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003764 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003765 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003766}
3767
hayeswang65b82d62017-06-15 14:44:03 +08003768static void rtl8153b_disable(struct r8152 *tp)
3769{
3770 r8153b_aldps_en(tp, false);
3771 rtl_disable(tp);
3772 rtl_reset_bmu(tp);
3773 r8153b_aldps_en(tp, true);
3774}
3775
hayeswangac718b62013-05-02 16:01:25 +00003776static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3777{
hayeswang43779f82014-01-02 11:25:10 +08003778 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003779 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003780 int ret = 0;
3781
hayeswangac718b62013-05-02 16:01:25 +00003782 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3783 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3784 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003785 if (tp->mii.supports_gmii) {
3786 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3787 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3788 } else {
3789 gbcr = 0;
3790 }
hayeswangac718b62013-05-02 16:01:25 +00003791
3792 if (autoneg == AUTONEG_DISABLE) {
3793 if (speed == SPEED_10) {
3794 bmcr = 0;
3795 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003796 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003797 } else if (speed == SPEED_100) {
3798 bmcr = BMCR_SPEED100;
3799 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003800 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003801 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3802 bmcr = BMCR_SPEED1000;
3803 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003804 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003805 } else {
3806 ret = -EINVAL;
3807 goto out;
3808 }
3809
hayeswang65b82d62017-06-15 14:44:03 +08003810 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003811 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003812 if (speed != SPEED_1000)
3813 speed_duplex++;
3814 }
hayeswangac718b62013-05-02 16:01:25 +00003815 } else {
3816 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003817 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003818 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003819 speed_duplex = NWAY_10M_FULL;
3820 } else {
hayeswangac718b62013-05-02 16:01:25 +00003821 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003822 speed_duplex = NWAY_10M_HALF;
3823 }
hayeswangac718b62013-05-02 16:01:25 +00003824 } else if (speed == SPEED_100) {
3825 if (duplex == DUPLEX_FULL) {
3826 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3827 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003828 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003829 } else {
3830 anar |= ADVERTISE_10HALF;
3831 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003832 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003833 }
hayeswang43779f82014-01-02 11:25:10 +08003834 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3835 if (duplex == DUPLEX_FULL) {
3836 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3837 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3838 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3839 } else {
3840 anar |= ADVERTISE_10HALF;
3841 anar |= ADVERTISE_100HALF;
3842 gbcr |= ADVERTISE_1000HALF;
3843 }
hayeswang65b82d62017-06-15 14:44:03 +08003844 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003845 } else {
3846 ret = -EINVAL;
3847 goto out;
3848 }
3849
3850 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3851 }
3852
hayeswangfae56172016-06-16 14:08:29 +08003853 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003854 bmcr |= BMCR_RESET;
3855
hayeswang43779f82014-01-02 11:25:10 +08003856 if (tp->mii.supports_gmii)
3857 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3858
hayeswangac718b62013-05-02 16:01:25 +00003859 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3860 r8152_mdio_write(tp, MII_BMCR, bmcr);
3861
hayeswang65b82d62017-06-15 14:44:03 +08003862 switch (tp->version) {
3863 case RTL_VER_08:
3864 case RTL_VER_09:
3865 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3866 UPS_FLAGS_SPEED_MASK);
3867 break;
3868
3869 default:
3870 break;
3871 }
3872
hayeswangfae56172016-06-16 14:08:29 +08003873 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003874 int i;
3875
hayeswangaa66a5f2014-02-18 21:49:04 +08003876 for (i = 0; i < 50; i++) {
3877 msleep(20);
3878 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3879 break;
3880 }
3881 }
3882
hayeswangac718b62013-05-02 16:01:25 +00003883out:
hayeswangac718b62013-05-02 16:01:25 +00003884 return ret;
3885}
3886
hayeswangd70b1132014-09-19 15:17:18 +08003887static void rtl8152_up(struct r8152 *tp)
3888{
3889 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3890 return;
3891
hayeswangcda9fb02016-01-07 17:51:12 +08003892 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003893 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003894 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003895}
3896
hayeswangac718b62013-05-02 16:01:25 +00003897static void rtl8152_down(struct r8152 *tp)
3898{
hayeswang68714382014-04-11 17:54:31 +08003899 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3900 rtl_drop_queued_tx(tp);
3901 return;
3902 }
3903
hayeswang00a5e362014-02-18 21:48:59 +08003904 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003905 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003906 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003907 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003908}
3909
hayeswangd70b1132014-09-19 15:17:18 +08003910static void rtl8153_up(struct r8152 *tp)
3911{
3912 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3913 return;
3914
hayeswangb2143962015-07-24 13:54:23 +08003915 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003916 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003917 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003918 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003919 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003920
3921 switch (tp->version) {
3922 case RTL_VER_03:
3923 case RTL_VER_04:
3924 break;
3925 case RTL_VER_05:
3926 case RTL_VER_06:
3927 default:
3928 r8153_u2p3en(tp, true);
3929 break;
3930 }
3931
hayeswangb2143962015-07-24 13:54:23 +08003932 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003933}
3934
hayeswang43779f82014-01-02 11:25:10 +08003935static void rtl8153_down(struct r8152 *tp)
3936{
hayeswang68714382014-04-11 17:54:31 +08003937 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3938 rtl_drop_queued_tx(tp);
3939 return;
3940 }
3941
hayeswangb9702722014-02-18 21:49:00 +08003942 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003943 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003944 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003945 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003946 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003947 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003948}
3949
hayeswang65b82d62017-06-15 14:44:03 +08003950static void rtl8153b_up(struct r8152 *tp)
3951{
3952 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3953 return;
3954
3955 r8153b_u1u2en(tp, false);
3956 r8153_u2p3en(tp, false);
3957 r8153b_aldps_en(tp, false);
3958
3959 r8153_first_init(tp);
3960 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3961
3962 r8153b_aldps_en(tp, true);
3963 r8153_u2p3en(tp, true);
3964 r8153b_u1u2en(tp, true);
3965}
3966
3967static void rtl8153b_down(struct r8152 *tp)
3968{
3969 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3970 rtl_drop_queued_tx(tp);
3971 return;
3972 }
3973
3974 r8153b_u1u2en(tp, false);
3975 r8153_u2p3en(tp, false);
3976 r8153b_power_cut_en(tp, false);
3977 r8153b_aldps_en(tp, false);
3978 r8153_enter_oob(tp);
3979 r8153b_aldps_en(tp, true);
3980}
3981
hayeswang2dd49e02015-09-07 11:57:44 +08003982static bool rtl8152_in_nway(struct r8152 *tp)
3983{
3984 u16 nway_state;
3985
3986 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3987 tp->ocp_base = 0x2000;
3988 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3989 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3990
3991 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3992 if (nway_state & 0xc000)
3993 return false;
3994 else
3995 return true;
3996}
3997
3998static bool rtl8153_in_nway(struct r8152 *tp)
3999{
4000 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
4001
4002 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
4003 return false;
4004 else
4005 return true;
4006}
4007
hayeswangac718b62013-05-02 16:01:25 +00004008static void set_carrier(struct r8152 *tp)
4009{
4010 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08004011 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00004012 u8 speed;
4013
4014 speed = rtl8152_get_speed(tp);
4015
4016 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08004017 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08004018 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08004019 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08004020 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004021 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08004022 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004023 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
4024 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08004025 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004026 netif_wake_queue(netdev);
4027 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08004028 } else if (netif_queue_stopped(netdev) &&
4029 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
4030 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004031 }
4032 } else {
hayeswang51d979f2015-02-06 11:30:47 +08004033 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00004034 netif_carrier_off(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08004035 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08004036 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08004037 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08004038 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004039 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08004040 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00004041 }
4042 }
hayeswangac718b62013-05-02 16:01:25 +00004043}
4044
4045static void rtl_work_func_t(struct work_struct *work)
4046{
4047 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
4048
hayeswanga1f83fe2014-11-12 10:05:05 +08004049 /* If the device is unplugged or !netif_running(), the workqueue
4050 * doesn't need to wake the device, and could return directly.
4051 */
4052 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
4053 return;
4054
hayeswang9a4be1b2014-02-18 21:49:07 +08004055 if (usb_autopm_get_interface(tp->intf) < 0)
4056 return;
4057
hayeswangac718b62013-05-02 16:01:25 +00004058 if (!test_bit(WORK_ENABLE, &tp->flags))
4059 goto out1;
4060
hayeswangb5403272014-10-09 18:00:26 +08004061 if (!mutex_trylock(&tp->control)) {
4062 schedule_delayed_work(&tp->schedule, 0);
4063 goto out1;
4064 }
4065
hayeswang216a8342016-01-07 17:51:11 +08004066 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08004067 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00004068
hayeswang216a8342016-01-07 17:51:11 +08004069 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00004070 _rtl8152_set_rx_mode(tp->netdev);
4071
Hayes Wangd2187f82019-08-19 14:40:36 +08004072 /* don't schedule tasket before linking */
4073 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
hayeswang216a8342016-01-07 17:51:11 +08004074 netif_carrier_ok(tp->netdev))
Hayes Wangd2187f82019-08-19 14:40:36 +08004075 tasklet_schedule(&tp->tx_tl);
hayeswangaa66a5f2014-02-18 21:49:04 +08004076
hayeswangb5403272014-10-09 18:00:26 +08004077 mutex_unlock(&tp->control);
4078
hayeswangac718b62013-05-02 16:01:25 +00004079out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08004080 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00004081}
4082
hayeswanga028a9e2016-06-13 17:49:36 +08004083static void rtl_hw_phy_work_func_t(struct work_struct *work)
4084{
4085 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
4086
4087 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4088 return;
4089
4090 if (usb_autopm_get_interface(tp->intf) < 0)
4091 return;
4092
4093 mutex_lock(&tp->control);
4094
4095 tp->rtl_ops.hw_phy_cfg(tp);
4096
hayeswangaa7e26b2016-06-13 17:49:38 +08004097 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08004098
hayeswanga028a9e2016-06-13 17:49:36 +08004099 mutex_unlock(&tp->control);
4100
4101 usb_autopm_put_interface(tp->intf);
4102}
4103
hayeswang5ee3c602016-01-07 17:12:17 +08004104#ifdef CONFIG_PM_SLEEP
4105static int rtl_notifier(struct notifier_block *nb, unsigned long action,
4106 void *data)
4107{
4108 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
4109
4110 switch (action) {
4111 case PM_HIBERNATION_PREPARE:
4112 case PM_SUSPEND_PREPARE:
4113 usb_autopm_get_interface(tp->intf);
4114 break;
4115
4116 case PM_POST_HIBERNATION:
4117 case PM_POST_SUSPEND:
4118 usb_autopm_put_interface(tp->intf);
4119 break;
4120
4121 case PM_POST_RESTORE:
4122 case PM_RESTORE_PREPARE:
4123 default:
4124 break;
4125 }
4126
4127 return NOTIFY_DONE;
4128}
4129#endif
4130
hayeswangac718b62013-05-02 16:01:25 +00004131static int rtl8152_open(struct net_device *netdev)
4132{
4133 struct r8152 *tp = netdev_priv(netdev);
4134 int res = 0;
4135
hayeswang7e9da482014-02-18 21:49:05 +08004136 res = alloc_all_mem(tp);
4137 if (res)
4138 goto out;
4139
hayeswang9a4be1b2014-02-18 21:49:07 +08004140 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004141 if (res < 0)
4142 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08004143
hayeswangb5403272014-10-09 18:00:26 +08004144 mutex_lock(&tp->control);
4145
hayeswang7e9da482014-02-18 21:49:05 +08004146 tp->rtl_ops.up(tp);
4147
hayeswang40a82912013-08-14 20:54:40 +08004148 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004149 netif_start_queue(netdev);
4150 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08004151
hayeswang3d55f442014-02-06 11:55:48 +08004152 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4153 if (res) {
4154 if (res == -ENODEV)
4155 netif_device_detach(tp->netdev);
4156 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
4157 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004158 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08004159 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08004160 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004161 tasklet_enable(&tp->tx_tl);
hayeswang3d55f442014-02-06 11:55:48 +08004162
hayeswangb5403272014-10-09 18:00:26 +08004163 mutex_unlock(&tp->control);
4164
hayeswang9a4be1b2014-02-18 21:49:07 +08004165 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08004166#ifdef CONFIG_PM_SLEEP
4167 tp->pm_notifier.notifier_call = rtl_notifier;
4168 register_pm_notifier(&tp->pm_notifier);
4169#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08004170 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004171
Guenter Roeckca0a7532016-11-09 19:51:25 -08004172out_unlock:
4173 mutex_unlock(&tp->control);
4174 usb_autopm_put_interface(tp->intf);
4175out_free:
4176 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08004177out:
hayeswangac718b62013-05-02 16:01:25 +00004178 return res;
4179}
4180
4181static int rtl8152_close(struct net_device *netdev)
4182{
4183 struct r8152 *tp = netdev_priv(netdev);
4184 int res = 0;
4185
hayeswang5ee3c602016-01-07 17:12:17 +08004186#ifdef CONFIG_PM_SLEEP
4187 unregister_pm_notifier(&tp->pm_notifier);
4188#endif
Hayes Wangd2187f82019-08-19 14:40:36 +08004189 tasklet_disable(&tp->tx_tl);
Jiri Slaby0ee1f472018-06-25 09:26:27 +02004190 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
4191 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004192 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08004193 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00004194 cancel_delayed_work_sync(&tp->schedule);
4195 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004196
4197 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004198 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004199 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004200 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004201 } else {
hayeswangb5403272014-10-09 18:00:26 +08004202 mutex_lock(&tp->control);
4203
hayeswang9a4be1b2014-02-18 21:49:07 +08004204 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004205
4206 mutex_unlock(&tp->control);
4207
hayeswang9a4be1b2014-02-18 21:49:07 +08004208 usb_autopm_put_interface(tp->intf);
4209 }
hayeswangac718b62013-05-02 16:01:25 +00004210
hayeswang7e9da482014-02-18 21:49:05 +08004211 free_all_mem(tp);
4212
hayeswangac718b62013-05-02 16:01:25 +00004213 return res;
4214}
4215
hayeswang4f1d4d52014-03-11 16:24:19 +08004216static void rtl_tally_reset(struct r8152 *tp)
4217{
4218 u32 ocp_data;
4219
4220 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4221 ocp_data |= TALLY_RESET;
4222 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4223}
4224
hayeswangac718b62013-05-02 16:01:25 +00004225static void r8152b_init(struct r8152 *tp)
4226{
hayeswangebc2ec482013-08-14 20:54:38 +08004227 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004228 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004229
hayeswang68714382014-04-11 17:54:31 +08004230 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4231 return;
4232
hayeswang2dd436d2016-09-20 16:22:06 +08004233 data = r8152_mdio_read(tp, MII_BMCR);
4234 if (data & BMCR_PDOWN) {
4235 data &= ~BMCR_PDOWN;
4236 r8152_mdio_write(tp, MII_BMCR, data);
4237 }
4238
hayeswangcda9fb02016-01-07 17:51:12 +08004239 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004240
hayeswangac718b62013-05-02 16:01:25 +00004241 if (tp->version == RTL_VER_01) {
4242 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4243 ocp_data &= ~LED_MODE_MASK;
4244 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4245 }
4246
hayeswang00a5e362014-02-18 21:48:59 +08004247 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004248
hayeswangac718b62013-05-02 16:01:25 +00004249 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4250 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4251 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4252 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4253 ocp_data &= ~MCU_CLK_RATIO_MASK;
4254 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4255 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4256 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4257 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4258 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4259
hayeswang4f1d4d52014-03-11 16:24:19 +08004260 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004261
hayeswangebc2ec482013-08-14 20:54:38 +08004262 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004263 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004264 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004265 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4266}
4267
hayeswang43779f82014-01-02 11:25:10 +08004268static void r8153_init(struct r8152 *tp)
4269{
4270 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004271 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004272 int i;
4273
hayeswang68714382014-04-11 17:54:31 +08004274 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4275 return;
4276
hayeswangb9702722014-02-18 21:49:00 +08004277 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004278
4279 for (i = 0; i < 500; i++) {
4280 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4281 AUTOLOAD_DONE)
4282 break;
4283 msleep(20);
4284 }
4285
hayeswangc564b872017-06-09 17:11:38 +08004286 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004287
hayeswang2dd436d2016-09-20 16:22:06 +08004288 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4289 tp->version == RTL_VER_05)
4290 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4291
4292 data = r8152_mdio_read(tp, MII_BMCR);
4293 if (data & BMCR_PDOWN) {
4294 data &= ~BMCR_PDOWN;
4295 r8152_mdio_write(tp, MII_BMCR, data);
4296 }
4297
hayeswangc564b872017-06-09 17:11:38 +08004298 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004299
hayeswangb9702722014-02-18 21:49:00 +08004300 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004301
hayeswang65bab842015-02-12 16:20:46 +08004302 if (tp->version == RTL_VER_04) {
4303 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4304 ocp_data &= ~pwd_dn_scale_mask;
4305 ocp_data |= pwd_dn_scale(96);
4306 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4307
4308 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4309 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4310 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4311 } else if (tp->version == RTL_VER_05) {
4312 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4313 ocp_data &= ~ECM_ALDPS;
4314 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4315
4316 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4317 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4318 ocp_data &= ~DYNAMIC_BURST;
4319 else
4320 ocp_data |= DYNAMIC_BURST;
4321 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004322 } else if (tp->version == RTL_VER_06) {
4323 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4324 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4325 ocp_data &= ~DYNAMIC_BURST;
4326 else
4327 ocp_data |= DYNAMIC_BURST;
4328 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004329 }
4330
4331 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4332 ocp_data |= EP4_FULL_FC;
4333 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4334
hayeswang43779f82014-01-02 11:25:10 +08004335 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4336 ocp_data &= ~TIMER11_EN;
4337 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4338
hayeswang43779f82014-01-02 11:25:10 +08004339 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4340 ocp_data &= ~LED_MODE_MASK;
4341 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4342
hayeswang65bab842015-02-12 16:20:46 +08004343 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004344 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004345 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004346 else
4347 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004348 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4349
4350 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4351 ocp_data &= ~SEN_VAL_MASK;
4352 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4353 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4354
hayeswang65bab842015-02-12 16:20:46 +08004355 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4356
hayeswangb9702722014-02-18 21:49:00 +08004357 r8153_power_cut_en(tp, false);
4358 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004359 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004360 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004361
hayeswange31f6362017-06-09 17:11:41 +08004362 /* rx aggregation */
4363 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4364 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004365 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4366 ocp_data |= RX_AGG_DISABLE;
4367
hayeswange31f6362017-06-09 17:11:41 +08004368 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004369
hayeswang4f1d4d52014-03-11 16:24:19 +08004370 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004371
4372 switch (tp->udev->speed) {
4373 case USB_SPEED_SUPER:
4374 case USB_SPEED_SUPER_PLUS:
4375 tp->coalesce = COALESCE_SUPER;
4376 break;
4377 case USB_SPEED_HIGH:
4378 tp->coalesce = COALESCE_HIGH;
4379 break;
4380 default:
4381 tp->coalesce = COALESCE_SLOW;
4382 break;
4383 }
hayeswang43779f82014-01-02 11:25:10 +08004384}
4385
hayeswang65b82d62017-06-15 14:44:03 +08004386static void r8153b_init(struct r8152 *tp)
4387{
4388 u32 ocp_data;
4389 u16 data;
4390 int i;
4391
4392 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4393 return;
4394
4395 r8153b_u1u2en(tp, false);
4396
4397 for (i = 0; i < 500; i++) {
4398 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4399 AUTOLOAD_DONE)
4400 break;
4401 msleep(20);
4402 }
4403
4404 data = r8153_phy_status(tp, 0);
4405
4406 data = r8152_mdio_read(tp, MII_BMCR);
4407 if (data & BMCR_PDOWN) {
4408 data &= ~BMCR_PDOWN;
4409 r8152_mdio_write(tp, MII_BMCR, data);
4410 }
4411
4412 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4413
4414 r8153_u2p3en(tp, false);
4415
4416 /* MSC timer = 0xfff * 8ms = 32760 ms */
4417 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4418
4419 /* U1/U2/L1 idle timer. 500 us */
4420 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4421
4422 r8153b_power_cut_en(tp, false);
4423 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08004424 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004425 rtl_runtime_suspend_enable(tp, false);
4426 r8153b_u1u2en(tp, true);
4427 usb_enable_lpm(tp->udev);
4428
4429 /* MAC clock speed down */
4430 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4431 ocp_data |= MAC_CLK_SPDWN_EN;
4432 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4433
4434 set_bit(GREEN_ETHERNET, &tp->flags);
4435
4436 /* rx aggregation */
4437 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4438 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4439 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4440
4441 rtl_tally_reset(tp);
4442
4443 tp->coalesce = 15000; /* 15 us */
4444}
4445
hayeswange5011392015-07-29 20:39:08 +08004446static int rtl8152_pre_reset(struct usb_interface *intf)
4447{
4448 struct r8152 *tp = usb_get_intfdata(intf);
4449 struct net_device *netdev;
4450
4451 if (!tp)
4452 return 0;
4453
4454 netdev = tp->netdev;
4455 if (!netif_running(netdev))
4456 return 0;
4457
hayeswangde9bf292017-01-26 09:38:32 +08004458 netif_stop_queue(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08004459 tasklet_disable(&tp->tx_tl);
hayeswange5011392015-07-29 20:39:08 +08004460 napi_disable(&tp->napi);
4461 clear_bit(WORK_ENABLE, &tp->flags);
4462 usb_kill_urb(tp->intr_urb);
4463 cancel_delayed_work_sync(&tp->schedule);
4464 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004465 mutex_lock(&tp->control);
4466 tp->rtl_ops.disable(tp);
4467 mutex_unlock(&tp->control);
4468 }
4469
4470 return 0;
4471}
4472
4473static int rtl8152_post_reset(struct usb_interface *intf)
4474{
4475 struct r8152 *tp = usb_get_intfdata(intf);
4476 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004477 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004478
4479 if (!tp)
4480 return 0;
4481
Mario Limonciello25766272019-04-04 13:46:53 -05004482 /* reset the MAC adddress in case of policy change */
4483 if (determine_ethernet_addr(tp, &sa) >= 0) {
4484 rtnl_lock();
4485 dev_set_mac_address (tp->netdev, &sa, NULL);
4486 rtnl_unlock();
4487 }
4488
hayeswange5011392015-07-29 20:39:08 +08004489 netdev = tp->netdev;
4490 if (!netif_running(netdev))
4491 return 0;
4492
4493 set_bit(WORK_ENABLE, &tp->flags);
4494 if (netif_carrier_ok(netdev)) {
4495 mutex_lock(&tp->control);
4496 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004497 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004498 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004499 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004500 }
4501
4502 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004503 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08004504 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004505 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004506
hayeswang7489bda2017-01-26 09:38:34 +08004507 if (!list_empty(&tp->rx_done))
4508 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004509
4510 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004511}
4512
hayeswang2dd49e02015-09-07 11:57:44 +08004513static bool delay_autosuspend(struct r8152 *tp)
4514{
4515 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4516 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4517
4518 /* This means a linking change occurs and the driver doesn't detect it,
4519 * yet. If the driver has disabled tx/rx and hw is linking on, the
4520 * device wouldn't wake up by receiving any packet.
4521 */
4522 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4523 return true;
4524
4525 /* If the linking down is occurred by nway, the device may miss the
4526 * linking change event. And it wouldn't wake when linking on.
4527 */
4528 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4529 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004530 else if (!skb_queue_empty(&tp->tx_queue))
4531 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004532 else
4533 return false;
4534}
4535
hayeswang21cbd0e2017-06-13 15:14:39 +08004536static int rtl8152_runtime_resume(struct r8152 *tp)
4537{
4538 struct net_device *netdev = tp->netdev;
4539
4540 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4541 struct napi_struct *napi = &tp->napi;
4542
4543 tp->rtl_ops.autosuspend_en(tp, false);
4544 napi_disable(napi);
4545 set_bit(WORK_ENABLE, &tp->flags);
4546
4547 if (netif_carrier_ok(netdev)) {
4548 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4549 rtl_start_rx(tp);
4550 } else {
4551 netif_carrier_off(netdev);
4552 tp->rtl_ops.disable(tp);
4553 netif_info(tp, link, netdev, "linking down\n");
4554 }
4555 }
4556
4557 napi_enable(napi);
4558 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4559 smp_mb__after_atomic();
4560
4561 if (!list_empty(&tp->rx_done))
4562 napi_schedule(&tp->napi);
4563
4564 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4565 } else {
4566 if (netdev->flags & IFF_UP)
4567 tp->rtl_ops.autosuspend_en(tp, false);
4568
4569 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4570 }
4571
4572 return 0;
4573}
4574
4575static int rtl8152_system_resume(struct r8152 *tp)
4576{
4577 struct net_device *netdev = tp->netdev;
4578
4579 netif_device_attach(netdev);
4580
4581 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4582 tp->rtl_ops.up(tp);
4583 netif_carrier_off(netdev);
4584 set_bit(WORK_ENABLE, &tp->flags);
4585 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4586 }
4587
4588 return 0;
4589}
4590
hayeswanga9c54ad2017-01-25 13:41:45 +08004591static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004592{
hayeswang6cc69f22014-10-17 16:55:08 +08004593 struct net_device *netdev = tp->netdev;
4594 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004595
hayeswang26afec32017-01-26 09:38:31 +08004596 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4597 smp_mb__after_atomic();
4598
hayeswang8fb28062017-01-10 17:04:06 +08004599 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004600 u32 rcr = 0;
4601
hayeswang75dc6922017-01-10 17:04:07 +08004602 if (netif_carrier_ok(netdev)) {
4603 u32 ocp_data;
4604
4605 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4606 ocp_data = rcr & ~RCR_ACPT_ALL;
4607 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4608 rxdy_gated_en(tp, true);
4609 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4610 PLA_OOB_CTRL);
4611 if (!(ocp_data & RXFIFO_EMPTY)) {
4612 rxdy_gated_en(tp, false);
4613 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004614 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4615 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004616 ret = -EBUSY;
4617 goto out1;
4618 }
4619 }
4620
hayeswang8fb28062017-01-10 17:04:06 +08004621 clear_bit(WORK_ENABLE, &tp->flags);
4622 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004623
hayeswang8fb28062017-01-10 17:04:06 +08004624 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004625
4626 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004627 struct napi_struct *napi = &tp->napi;
4628
4629 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004630 rtl_stop_rx(tp);
4631 rxdy_gated_en(tp, false);
4632 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004633 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004634 }
hayeswangbd882982017-06-13 15:14:40 +08004635
4636 if (delay_autosuspend(tp)) {
4637 rtl8152_runtime_resume(tp);
4638 ret = -EBUSY;
4639 }
hayeswang6cc69f22014-10-17 16:55:08 +08004640 }
4641
hayeswang8fb28062017-01-10 17:04:06 +08004642out1:
4643 return ret;
4644}
4645
4646static int rtl8152_system_suspend(struct r8152 *tp)
4647{
4648 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004649
4650 netif_device_detach(netdev);
4651
hayeswange3bd1a82014-10-29 11:12:17 +08004652 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004653 struct napi_struct *napi = &tp->napi;
4654
hayeswangac718b62013-05-02 16:01:25 +00004655 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004656 usb_kill_urb(tp->intr_urb);
Hayes Wangd2187f82019-08-19 14:40:36 +08004657 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08004658 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004659 cancel_delayed_work_sync(&tp->schedule);
4660 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004661 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004662 tasklet_enable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00004663 }
hayeswang8fb28062017-01-10 17:04:06 +08004664
zhong jiangf7419172018-08-09 09:39:13 +08004665 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004666}
4667
4668static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4669{
4670 struct r8152 *tp = usb_get_intfdata(intf);
4671 int ret;
4672
4673 mutex_lock(&tp->control);
4674
4675 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004676 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004677 else
4678 ret = rtl8152_system_suspend(tp);
4679
hayeswangb5403272014-10-09 18:00:26 +08004680 mutex_unlock(&tp->control);
4681
hayeswang6cc69f22014-10-17 16:55:08 +08004682 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004683}
4684
4685static int rtl8152_resume(struct usb_interface *intf)
4686{
4687 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004688 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004689
hayeswangb5403272014-10-09 18:00:26 +08004690 mutex_lock(&tp->control);
4691
hayeswang21cbd0e2017-06-13 15:14:39 +08004692 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4693 ret = rtl8152_runtime_resume(tp);
4694 else
4695 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004696
hayeswangb5403272014-10-09 18:00:26 +08004697 mutex_unlock(&tp->control);
4698
hayeswang21cbd0e2017-06-13 15:14:39 +08004699 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004700}
4701
hayeswang7ec25412016-01-04 14:38:46 +08004702static int rtl8152_reset_resume(struct usb_interface *intf)
4703{
4704 struct r8152 *tp = usb_get_intfdata(intf);
4705
4706 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004707 mutex_lock(&tp->control);
4708 tp->rtl_ops.init(tp);
4709 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4710 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004711 return rtl8152_resume(intf);
4712}
4713
hayeswang21ff2e82014-02-18 21:49:06 +08004714static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4715{
4716 struct r8152 *tp = netdev_priv(dev);
4717
hayeswang9a4be1b2014-02-18 21:49:07 +08004718 if (usb_autopm_get_interface(tp->intf) < 0)
4719 return;
4720
hayeswang7daed8d2015-07-24 13:54:24 +08004721 if (!rtl_can_wakeup(tp)) {
4722 wol->supported = 0;
4723 wol->wolopts = 0;
4724 } else {
4725 mutex_lock(&tp->control);
4726 wol->supported = WAKE_ANY;
4727 wol->wolopts = __rtl_get_wol(tp);
4728 mutex_unlock(&tp->control);
4729 }
hayeswangb5403272014-10-09 18:00:26 +08004730
hayeswang9a4be1b2014-02-18 21:49:07 +08004731 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004732}
4733
4734static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4735{
4736 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004737 int ret;
4738
hayeswang7daed8d2015-07-24 13:54:24 +08004739 if (!rtl_can_wakeup(tp))
4740 return -EOPNOTSUPP;
4741
Florian Fainellif2750df2018-09-28 16:18:54 -07004742 if (wol->wolopts & ~WAKE_ANY)
4743 return -EINVAL;
4744
hayeswang9a4be1b2014-02-18 21:49:07 +08004745 ret = usb_autopm_get_interface(tp->intf);
4746 if (ret < 0)
4747 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004748
hayeswangb5403272014-10-09 18:00:26 +08004749 mutex_lock(&tp->control);
4750
hayeswang21ff2e82014-02-18 21:49:06 +08004751 __rtl_set_wol(tp, wol->wolopts);
4752 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4753
hayeswangb5403272014-10-09 18:00:26 +08004754 mutex_unlock(&tp->control);
4755
hayeswang9a4be1b2014-02-18 21:49:07 +08004756 usb_autopm_put_interface(tp->intf);
4757
4758out_set_wol:
4759 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004760}
4761
hayeswanga5ec27c2014-02-18 21:49:11 +08004762static u32 rtl8152_get_msglevel(struct net_device *dev)
4763{
4764 struct r8152 *tp = netdev_priv(dev);
4765
4766 return tp->msg_enable;
4767}
4768
4769static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4770{
4771 struct r8152 *tp = netdev_priv(dev);
4772
4773 tp->msg_enable = value;
4774}
4775
hayeswangac718b62013-05-02 16:01:25 +00004776static void rtl8152_get_drvinfo(struct net_device *netdev,
4777 struct ethtool_drvinfo *info)
4778{
4779 struct r8152 *tp = netdev_priv(netdev);
4780
hayeswangb0b46c72014-08-26 10:08:23 +08004781 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4782 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004783 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4784}
4785
4786static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004787int rtl8152_get_link_ksettings(struct net_device *netdev,
4788 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004789{
4790 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004791 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004792
4793 if (!tp->mii.mdio_read)
4794 return -EOPNOTSUPP;
4795
hayeswang8d4a4d72014-10-09 18:00:25 +08004796 ret = usb_autopm_get_interface(tp->intf);
4797 if (ret < 0)
4798 goto out;
4799
hayeswangb5403272014-10-09 18:00:26 +08004800 mutex_lock(&tp->control);
4801
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004802 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004803
hayeswangb5403272014-10-09 18:00:26 +08004804 mutex_unlock(&tp->control);
4805
hayeswang8d4a4d72014-10-09 18:00:25 +08004806 usb_autopm_put_interface(tp->intf);
4807
4808out:
4809 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004810}
4811
Philippe Reynes06144dc2017-03-12 22:41:58 +01004812static int rtl8152_set_link_ksettings(struct net_device *dev,
4813 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004814{
4815 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004816 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004817
hayeswang9a4be1b2014-02-18 21:49:07 +08004818 ret = usb_autopm_get_interface(tp->intf);
4819 if (ret < 0)
4820 goto out;
4821
hayeswangb5403272014-10-09 18:00:26 +08004822 mutex_lock(&tp->control);
4823
Philippe Reynes06144dc2017-03-12 22:41:58 +01004824 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4825 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004826 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004827 tp->autoneg = cmd->base.autoneg;
4828 tp->speed = cmd->base.speed;
4829 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004830 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004831
hayeswangb5403272014-10-09 18:00:26 +08004832 mutex_unlock(&tp->control);
4833
hayeswang9a4be1b2014-02-18 21:49:07 +08004834 usb_autopm_put_interface(tp->intf);
4835
4836out:
4837 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004838}
4839
hayeswang4f1d4d52014-03-11 16:24:19 +08004840static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4841 "tx_packets",
4842 "rx_packets",
4843 "tx_errors",
4844 "rx_errors",
4845 "rx_missed",
4846 "align_errors",
4847 "tx_single_collisions",
4848 "tx_multi_collisions",
4849 "rx_unicast",
4850 "rx_broadcast",
4851 "rx_multicast",
4852 "tx_aborted",
4853 "tx_underrun",
4854};
4855
4856static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4857{
4858 switch (sset) {
4859 case ETH_SS_STATS:
4860 return ARRAY_SIZE(rtl8152_gstrings);
4861 default:
4862 return -EOPNOTSUPP;
4863 }
4864}
4865
4866static void rtl8152_get_ethtool_stats(struct net_device *dev,
4867 struct ethtool_stats *stats, u64 *data)
4868{
4869 struct r8152 *tp = netdev_priv(dev);
4870 struct tally_counter tally;
4871
hayeswang0b030242014-07-08 14:49:28 +08004872 if (usb_autopm_get_interface(tp->intf) < 0)
4873 return;
4874
hayeswang4f1d4d52014-03-11 16:24:19 +08004875 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4876
hayeswang0b030242014-07-08 14:49:28 +08004877 usb_autopm_put_interface(tp->intf);
4878
hayeswang4f1d4d52014-03-11 16:24:19 +08004879 data[0] = le64_to_cpu(tally.tx_packets);
4880 data[1] = le64_to_cpu(tally.rx_packets);
4881 data[2] = le64_to_cpu(tally.tx_errors);
4882 data[3] = le32_to_cpu(tally.rx_errors);
4883 data[4] = le16_to_cpu(tally.rx_missed);
4884 data[5] = le16_to_cpu(tally.align_errors);
4885 data[6] = le32_to_cpu(tally.tx_one_collision);
4886 data[7] = le32_to_cpu(tally.tx_multi_collision);
4887 data[8] = le64_to_cpu(tally.rx_unicast);
4888 data[9] = le64_to_cpu(tally.rx_broadcast);
4889 data[10] = le32_to_cpu(tally.rx_multicast);
4890 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004891 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004892}
4893
4894static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4895{
4896 switch (stringset) {
4897 case ETH_SS_STATS:
4898 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4899 break;
4900 }
4901}
4902
hayeswangdf35d282014-09-25 20:54:02 +08004903static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4904{
Hayes Wangf4a93be2019-08-23 15:33:40 +08004905 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08004906 u16 val;
4907
4908 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4909 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4910
4911 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4912 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4913
4914 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4915 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4916
Hayes Wangf4a93be2019-08-23 15:33:40 +08004917 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08004918 eee->eee_active = !!(supported & adv & lp);
4919 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004920 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08004921 eee->lp_advertised = lp;
4922
4923 return 0;
4924}
4925
4926static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4927{
4928 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4929
Hayes Wangf4a93be2019-08-23 15:33:40 +08004930 tp->eee_en = eee->eee_enabled;
4931 tp->eee_adv = val;
4932
hayeswangdf35d282014-09-25 20:54:02 +08004933 r8152_eee_en(tp, eee->eee_enabled);
4934
Hayes Wangf4a93be2019-08-23 15:33:40 +08004935 if (eee->eee_enabled)
4936 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4937 else
4938 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
hayeswangdf35d282014-09-25 20:54:02 +08004939
4940 return 0;
4941}
4942
4943static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4944{
Hayes Wangf4a93be2019-08-23 15:33:40 +08004945 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08004946 u16 val;
4947
4948 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4949 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4950
4951 val = ocp_reg_read(tp, OCP_EEE_ADV);
4952 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4953
4954 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4955 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4956
Hayes Wangf4a93be2019-08-23 15:33:40 +08004957 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08004958 eee->eee_active = !!(supported & adv & lp);
4959 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004960 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08004961 eee->lp_advertised = lp;
4962
4963 return 0;
4964}
4965
4966static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4967{
4968 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4969
Hayes Wangf4a93be2019-08-23 15:33:40 +08004970 tp->eee_en = eee->eee_enabled;
4971 tp->eee_adv = val;
4972
hayeswangdf35d282014-09-25 20:54:02 +08004973 r8153_eee_en(tp, eee->eee_enabled);
4974
Hayes Wangf4a93be2019-08-23 15:33:40 +08004975 if (eee->eee_enabled)
4976 ocp_reg_write(tp, OCP_EEE_ADV, val);
4977 else
4978 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswangdf35d282014-09-25 20:54:02 +08004979
4980 return 0;
4981}
4982
hayeswang65b82d62017-06-15 14:44:03 +08004983static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4984{
4985 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4986
Hayes Wangf4a93be2019-08-23 15:33:40 +08004987 tp->eee_en = eee->eee_enabled;
4988 tp->eee_adv = val;
4989
hayeswang65b82d62017-06-15 14:44:03 +08004990 r8153b_eee_en(tp, eee->eee_enabled);
4991
Hayes Wangf4a93be2019-08-23 15:33:40 +08004992 if (eee->eee_enabled)
4993 ocp_reg_write(tp, OCP_EEE_ADV, val);
4994 else
4995 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang65b82d62017-06-15 14:44:03 +08004996
4997 return 0;
4998}
4999
hayeswangdf35d282014-09-25 20:54:02 +08005000static int
5001rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
5002{
5003 struct r8152 *tp = netdev_priv(net);
5004 int ret;
5005
5006 ret = usb_autopm_get_interface(tp->intf);
5007 if (ret < 0)
5008 goto out;
5009
hayeswangb5403272014-10-09 18:00:26 +08005010 mutex_lock(&tp->control);
5011
hayeswangdf35d282014-09-25 20:54:02 +08005012 ret = tp->rtl_ops.eee_get(tp, edata);
5013
hayeswangb5403272014-10-09 18:00:26 +08005014 mutex_unlock(&tp->control);
5015
hayeswangdf35d282014-09-25 20:54:02 +08005016 usb_autopm_put_interface(tp->intf);
5017
5018out:
5019 return ret;
5020}
5021
5022static int
5023rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
5024{
5025 struct r8152 *tp = netdev_priv(net);
5026 int ret;
5027
5028 ret = usb_autopm_get_interface(tp->intf);
5029 if (ret < 0)
5030 goto out;
5031
hayeswangb5403272014-10-09 18:00:26 +08005032 mutex_lock(&tp->control);
5033
hayeswangdf35d282014-09-25 20:54:02 +08005034 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08005035 if (!ret)
5036 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08005037
hayeswangb5403272014-10-09 18:00:26 +08005038 mutex_unlock(&tp->control);
5039
hayeswangdf35d282014-09-25 20:54:02 +08005040 usb_autopm_put_interface(tp->intf);
5041
5042out:
5043 return ret;
5044}
5045
hayeswang8884f502014-10-28 14:05:52 +08005046static int rtl8152_nway_reset(struct net_device *dev)
5047{
5048 struct r8152 *tp = netdev_priv(dev);
5049 int ret;
5050
5051 ret = usb_autopm_get_interface(tp->intf);
5052 if (ret < 0)
5053 goto out;
5054
5055 mutex_lock(&tp->control);
5056
5057 ret = mii_nway_restart(&tp->mii);
5058
5059 mutex_unlock(&tp->control);
5060
5061 usb_autopm_put_interface(tp->intf);
5062
5063out:
5064 return ret;
5065}
5066
hayeswangefb3dd82015-02-12 14:33:48 +08005067static int rtl8152_get_coalesce(struct net_device *netdev,
5068 struct ethtool_coalesce *coalesce)
5069{
5070 struct r8152 *tp = netdev_priv(netdev);
5071
5072 switch (tp->version) {
5073 case RTL_VER_01:
5074 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005075 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08005076 return -EOPNOTSUPP;
5077 default:
5078 break;
5079 }
5080
5081 coalesce->rx_coalesce_usecs = tp->coalesce;
5082
5083 return 0;
5084}
5085
5086static int rtl8152_set_coalesce(struct net_device *netdev,
5087 struct ethtool_coalesce *coalesce)
5088{
5089 struct r8152 *tp = netdev_priv(netdev);
5090 int ret;
5091
5092 switch (tp->version) {
5093 case RTL_VER_01:
5094 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005095 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08005096 return -EOPNOTSUPP;
5097 default:
5098 break;
5099 }
5100
5101 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
5102 return -EINVAL;
5103
5104 ret = usb_autopm_get_interface(tp->intf);
5105 if (ret < 0)
5106 return ret;
5107
5108 mutex_lock(&tp->control);
5109
5110 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
5111 tp->coalesce = coalesce->rx_coalesce_usecs;
5112
Hayes Wang9fae5412019-07-03 15:11:56 +08005113 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
5114 netif_stop_queue(netdev);
5115 napi_disable(&tp->napi);
5116 tp->rtl_ops.disable(tp);
5117 tp->rtl_ops.enable(tp);
5118 rtl_start_rx(tp);
5119 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5120 _rtl8152_set_rx_mode(netdev);
5121 napi_enable(&tp->napi);
5122 netif_wake_queue(netdev);
5123 }
hayeswangefb3dd82015-02-12 14:33:48 +08005124 }
5125
5126 mutex_unlock(&tp->control);
5127
5128 usb_autopm_put_interface(tp->intf);
5129
5130 return ret;
5131}
5132
Hayes Wange4a50172019-08-13 11:42:09 +08005133static int rtl8152_get_tunable(struct net_device *netdev,
5134 const struct ethtool_tunable *tunable, void *d)
5135{
5136 struct r8152 *tp = netdev_priv(netdev);
5137
5138 switch (tunable->id) {
5139 case ETHTOOL_RX_COPYBREAK:
5140 *(u32 *)d = tp->rx_copybreak;
5141 break;
5142 default:
5143 return -EOPNOTSUPP;
5144 }
5145
5146 return 0;
5147}
5148
5149static int rtl8152_set_tunable(struct net_device *netdev,
5150 const struct ethtool_tunable *tunable,
5151 const void *d)
5152{
5153 struct r8152 *tp = netdev_priv(netdev);
5154 u32 val;
5155
5156 switch (tunable->id) {
5157 case ETHTOOL_RX_COPYBREAK:
5158 val = *(u32 *)d;
5159 if (val < ETH_ZLEN) {
5160 netif_err(tp, rx_err, netdev,
5161 "Invalid rx copy break value\n");
5162 return -EINVAL;
5163 }
5164
5165 if (tp->rx_copybreak != val) {
5166 napi_disable(&tp->napi);
5167 tp->rx_copybreak = val;
5168 napi_enable(&tp->napi);
5169 }
5170 break;
5171 default:
5172 return -EOPNOTSUPP;
5173 }
5174
5175 return 0;
5176}
5177
5178static void rtl8152_get_ringparam(struct net_device *netdev,
5179 struct ethtool_ringparam *ring)
5180{
5181 struct r8152 *tp = netdev_priv(netdev);
5182
5183 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
5184 ring->rx_pending = tp->rx_pending;
5185}
5186
5187static int rtl8152_set_ringparam(struct net_device *netdev,
5188 struct ethtool_ringparam *ring)
5189{
5190 struct r8152 *tp = netdev_priv(netdev);
5191
5192 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
5193 return -EINVAL;
5194
5195 if (tp->rx_pending != ring->rx_pending) {
5196 napi_disable(&tp->napi);
5197 tp->rx_pending = ring->rx_pending;
5198 napi_enable(&tp->napi);
5199 }
5200
5201 return 0;
5202}
5203
Julia Lawall407a4712016-09-01 00:21:22 +02005204static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00005205 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00005206 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08005207 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08005208 .get_msglevel = rtl8152_get_msglevel,
5209 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08005210 .get_wol = rtl8152_get_wol,
5211 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08005212 .get_strings = rtl8152_get_strings,
5213 .get_sset_count = rtl8152_get_sset_count,
5214 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08005215 .get_coalesce = rtl8152_get_coalesce,
5216 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08005217 .get_eee = rtl_ethtool_get_eee,
5218 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01005219 .get_link_ksettings = rtl8152_get_link_ksettings,
5220 .set_link_ksettings = rtl8152_set_link_ksettings,
Hayes Wange4a50172019-08-13 11:42:09 +08005221 .get_tunable = rtl8152_get_tunable,
5222 .set_tunable = rtl8152_set_tunable,
5223 .get_ringparam = rtl8152_get_ringparam,
5224 .set_ringparam = rtl8152_set_ringparam,
hayeswangac718b62013-05-02 16:01:25 +00005225};
5226
5227static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
5228{
5229 struct r8152 *tp = netdev_priv(netdev);
5230 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08005231 int res;
5232
hayeswang68714382014-04-11 17:54:31 +08005233 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5234 return -ENODEV;
5235
hayeswang9a4be1b2014-02-18 21:49:07 +08005236 res = usb_autopm_get_interface(tp->intf);
5237 if (res < 0)
5238 goto out;
hayeswangac718b62013-05-02 16:01:25 +00005239
5240 switch (cmd) {
5241 case SIOCGMIIPHY:
5242 data->phy_id = R8152_PHY_ID; /* Internal PHY */
5243 break;
5244
5245 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08005246 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005247 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08005248 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005249 break;
5250
5251 case SIOCSMIIREG:
5252 if (!capable(CAP_NET_ADMIN)) {
5253 res = -EPERM;
5254 break;
5255 }
hayeswangb5403272014-10-09 18:00:26 +08005256 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005257 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08005258 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005259 break;
5260
5261 default:
5262 res = -EOPNOTSUPP;
5263 }
5264
hayeswang9a4be1b2014-02-18 21:49:07 +08005265 usb_autopm_put_interface(tp->intf);
5266
5267out:
hayeswangac718b62013-05-02 16:01:25 +00005268 return res;
5269}
5270
hayeswang69b4b7a2014-07-10 10:58:54 +08005271static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5272{
5273 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08005274 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005275
5276 switch (tp->version) {
5277 case RTL_VER_01:
5278 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005279 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04005280 dev->mtu = new_mtu;
5281 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08005282 default:
5283 break;
5284 }
5285
hayeswang396e2e22015-02-12 14:33:47 +08005286 ret = usb_autopm_get_interface(tp->intf);
5287 if (ret < 0)
5288 return ret;
5289
5290 mutex_lock(&tp->control);
5291
hayeswang69b4b7a2014-07-10 10:58:54 +08005292 dev->mtu = new_mtu;
5293
hayeswang210c4f72017-03-20 16:13:44 +08005294 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005295 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005296
5297 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5298
5299 if (netif_carrier_ok(dev))
5300 r8153_set_rx_early_size(tp);
5301 }
hayeswang396e2e22015-02-12 14:33:47 +08005302
5303 mutex_unlock(&tp->control);
5304
5305 usb_autopm_put_interface(tp->intf);
5306
5307 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005308}
5309
hayeswangac718b62013-05-02 16:01:25 +00005310static const struct net_device_ops rtl8152_netdev_ops = {
5311 .ndo_open = rtl8152_open,
5312 .ndo_stop = rtl8152_close,
5313 .ndo_do_ioctl = rtl8152_ioctl,
5314 .ndo_start_xmit = rtl8152_start_xmit,
5315 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005316 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005317 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5318 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005319 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005320 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005321 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005322};
5323
hayeswange3fe0b12014-01-02 11:22:39 +08005324static void rtl8152_unload(struct r8152 *tp)
5325{
hayeswang68714382014-04-11 17:54:31 +08005326 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5327 return;
5328
hayeswang00a5e362014-02-18 21:48:59 +08005329 if (tp->version != RTL_VER_01)
5330 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005331}
5332
hayeswang43779f82014-01-02 11:25:10 +08005333static void rtl8153_unload(struct r8152 *tp)
5334{
hayeswang68714382014-04-11 17:54:31 +08005335 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5336 return;
5337
hayeswang49be1722014-10-01 13:25:11 +08005338 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005339}
5340
hayeswang65b82d62017-06-15 14:44:03 +08005341static void rtl8153b_unload(struct r8152 *tp)
5342{
5343 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5344 return;
5345
5346 r8153b_power_cut_en(tp, false);
5347}
5348
hayeswang55b65472014-11-06 12:47:39 +08005349static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005350{
5351 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005352 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005353
hayeswang55b65472014-11-06 12:47:39 +08005354 switch (tp->version) {
5355 case RTL_VER_01:
5356 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005357 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005358 ops->init = r8152b_init;
5359 ops->enable = rtl8152_enable;
5360 ops->disable = rtl8152_disable;
5361 ops->up = rtl8152_up;
5362 ops->down = rtl8152_down;
5363 ops->unload = rtl8152_unload;
5364 ops->eee_get = r8152_get_eee;
5365 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005366 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005367 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005368 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005369 tp->rx_buf_sz = 16 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005370 tp->eee_en = true;
5371 tp->eee_adv = MDIO_EEE_100TX;
hayeswang43779f82014-01-02 11:25:10 +08005372 break;
5373
hayeswang55b65472014-11-06 12:47:39 +08005374 case RTL_VER_03:
5375 case RTL_VER_04:
5376 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005377 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005378 ops->init = r8153_init;
5379 ops->enable = rtl8153_enable;
5380 ops->disable = rtl8153_disable;
5381 ops->up = rtl8153_up;
5382 ops->down = rtl8153_down;
5383 ops->unload = rtl8153_unload;
5384 ops->eee_get = r8153_get_eee;
5385 ops->eee_set = r8153_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005386 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005387 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005388 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005389 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005390 tp->eee_en = true;
5391 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswangc81229c2014-01-02 11:22:42 +08005392 break;
5393
hayeswang65b82d62017-06-15 14:44:03 +08005394 case RTL_VER_08:
5395 case RTL_VER_09:
5396 ops->init = r8153b_init;
5397 ops->enable = rtl8153_enable;
5398 ops->disable = rtl8153b_disable;
5399 ops->up = rtl8153b_up;
5400 ops->down = rtl8153b_down;
5401 ops->unload = rtl8153b_unload;
5402 ops->eee_get = r8153_get_eee;
5403 ops->eee_set = r8153b_set_eee;
5404 ops->in_nway = rtl8153_in_nway;
5405 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5406 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005407 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005408 tp->eee_en = true;
5409 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswang65b82d62017-06-15 14:44:03 +08005410 break;
5411
hayeswangc81229c2014-01-02 11:22:42 +08005412 default:
hayeswang55b65472014-11-06 12:47:39 +08005413 ret = -ENODEV;
5414 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005415 break;
5416 }
5417
5418 return ret;
5419}
5420
hayeswang33928ee2017-03-17 11:20:13 +08005421static u8 rtl_get_version(struct usb_interface *intf)
5422{
5423 struct usb_device *udev = interface_to_usbdev(intf);
5424 u32 ocp_data = 0;
5425 __le32 *tmp;
5426 u8 version;
5427 int ret;
5428
5429 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5430 if (!tmp)
5431 return 0;
5432
5433 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5434 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5435 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5436 if (ret > 0)
5437 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5438
5439 kfree(tmp);
5440
5441 switch (ocp_data) {
5442 case 0x4c00:
5443 version = RTL_VER_01;
5444 break;
5445 case 0x4c10:
5446 version = RTL_VER_02;
5447 break;
5448 case 0x5c00:
5449 version = RTL_VER_03;
5450 break;
5451 case 0x5c10:
5452 version = RTL_VER_04;
5453 break;
5454 case 0x5c20:
5455 version = RTL_VER_05;
5456 break;
5457 case 0x5c30:
5458 version = RTL_VER_06;
5459 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005460 case 0x4800:
5461 version = RTL_VER_07;
5462 break;
hayeswang65b82d62017-06-15 14:44:03 +08005463 case 0x6000:
5464 version = RTL_VER_08;
5465 break;
5466 case 0x6010:
5467 version = RTL_VER_09;
5468 break;
hayeswang33928ee2017-03-17 11:20:13 +08005469 default:
5470 version = RTL_VER_UNKNOWN;
5471 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5472 break;
5473 }
5474
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005475 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5476
hayeswang33928ee2017-03-17 11:20:13 +08005477 return version;
5478}
5479
hayeswangac718b62013-05-02 16:01:25 +00005480static int rtl8152_probe(struct usb_interface *intf,
5481 const struct usb_device_id *id)
5482{
5483 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005484 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005485 struct r8152 *tp;
5486 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005487 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005488
hayeswang33928ee2017-03-17 11:20:13 +08005489 if (version == RTL_VER_UNKNOWN)
5490 return -ENODEV;
5491
hayeswang10c32712014-03-04 20:47:48 +08005492 if (udev->actconfig->desc.bConfigurationValue != 1) {
5493 usb_driver_set_configuration(udev, 1);
5494 return -ENODEV;
5495 }
5496
5497 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005498 netdev = alloc_etherdev(sizeof(struct r8152));
5499 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005500 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005501 return -ENOMEM;
5502 }
5503
hayeswangebc2ec482013-08-14 20:54:38 +08005504 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005505 tp = netdev_priv(netdev);
5506 tp->msg_enable = 0x7FFF;
5507
hayeswange3ad4122014-01-06 17:08:42 +08005508 tp->udev = udev;
5509 tp->netdev = netdev;
5510 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005511 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005512
hayeswang33928ee2017-03-17 11:20:13 +08005513 switch (version) {
5514 case RTL_VER_01:
5515 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005516 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005517 tp->mii.supports_gmii = 0;
5518 break;
5519 default:
5520 tp->mii.supports_gmii = 1;
5521 break;
5522 }
5523
hayeswang55b65472014-11-06 12:47:39 +08005524 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005525 if (ret)
5526 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005527
hayeswangb5403272014-10-09 18:00:26 +08005528 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005529 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005530 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
Hayes Wangd2187f82019-08-19 14:40:36 +08005531 tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp);
5532 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00005533
hayeswangac718b62013-05-02 16:01:25 +00005534 netdev->netdev_ops = &rtl8152_netdev_ops;
5535 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005536
hayeswang60c89072014-03-07 11:04:39 +08005537 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005538 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005539 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5540 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005541 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005542 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005543 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005544 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005545 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5546 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5547 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005548
hayeswang19c0f402017-01-11 16:25:34 +08005549 if (tp->version == RTL_VER_01) {
5550 netdev->features &= ~NETIF_F_RXCSUM;
5551 netdev->hw_features &= ~NETIF_F_RXCSUM;
5552 }
5553
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005554 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5555 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005556 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5557 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5558 }
5559
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005560 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005561 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005562
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005563 /* MTU range: 68 - 1500 or 9194 */
5564 netdev->min_mtu = ETH_MIN_MTU;
5565 switch (tp->version) {
5566 case RTL_VER_01:
5567 case RTL_VER_02:
5568 netdev->max_mtu = ETH_DATA_LEN;
5569 break;
5570 default:
5571 netdev->max_mtu = RTL8153_MAX_MTU;
5572 break;
5573 }
5574
hayeswangac718b62013-05-02 16:01:25 +00005575 tp->mii.dev = netdev;
5576 tp->mii.mdio_read = read_mii_word;
5577 tp->mii.mdio_write = write_mii_word;
5578 tp->mii.phy_id_mask = 0x3f;
5579 tp->mii.reg_num_mask = 0x1f;
5580 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005581
hayeswangaa7e26b2016-06-13 17:49:38 +08005582 tp->autoneg = AUTONEG_ENABLE;
5583 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5584 tp->duplex = DUPLEX_FULL;
5585
Hayes Wange4a50172019-08-13 11:42:09 +08005586 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
5587 tp->rx_pending = 10 * RTL8152_MAX_RX;
5588
hayeswang9a4be1b2014-02-18 21:49:07 +08005589 intf->needs_remote_wakeup = 1;
5590
hayeswangc81229c2014-01-02 11:22:42 +08005591 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005592 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005593 set_ethernet_addr(tp);
5594
hayeswangac718b62013-05-02 16:01:25 +00005595 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005596 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005597
hayeswangebc2ec482013-08-14 20:54:38 +08005598 ret = register_netdev(netdev);
5599 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005600 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005601 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005602 }
5603
hayeswang7daed8d2015-07-24 13:54:24 +08005604 if (!rtl_can_wakeup(tp))
5605 __rtl_set_wol(tp, 0);
5606
hayeswang21ff2e82014-02-18 21:49:06 +08005607 tp->saved_wolopts = __rtl_get_wol(tp);
5608 if (tp->saved_wolopts)
5609 device_set_wakeup_enable(&udev->dev, true);
5610 else
5611 device_set_wakeup_enable(&udev->dev, false);
5612
Hayes Wang4a8deae2014-01-07 11:18:22 +08005613 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005614
5615 return 0;
5616
hayeswangac718b62013-05-02 16:01:25 +00005617out1:
hayeswangd823ab62015-01-12 12:06:23 +08005618 netif_napi_del(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005619 tasklet_kill(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08005620 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005621out:
5622 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005623 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005624}
5625
hayeswangac718b62013-05-02 16:01:25 +00005626static void rtl8152_disconnect(struct usb_interface *intf)
5627{
5628 struct r8152 *tp = usb_get_intfdata(intf);
5629
5630 usb_set_intfdata(intf, NULL);
5631 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08005632 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08005633
hayeswangd823ab62015-01-12 12:06:23 +08005634 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005635 unregister_netdev(tp->netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08005636 tasklet_kill(&tp->tx_tl);
hayeswanga028a9e2016-06-13 17:49:36 +08005637 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005638 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005639 free_netdev(tp->netdev);
5640 }
5641}
5642
hayeswangd9a28c52014-12-04 10:43:11 +08005643#define REALTEK_USB_DEVICE(vend, prod) \
5644 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5645 USB_DEVICE_ID_MATCH_INT_CLASS, \
5646 .idVendor = (vend), \
5647 .idProduct = (prod), \
5648 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5649}, \
5650{ \
5651 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5652 USB_DEVICE_ID_MATCH_DEVICE, \
5653 .idVendor = (vend), \
5654 .idProduct = (prod), \
5655 .bInterfaceClass = USB_CLASS_COMM, \
5656 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5657 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5658
hayeswangac718b62013-05-02 16:01:25 +00005659/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305660static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005661 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005662 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5663 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005664 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5665 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005666 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005667 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005668 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5669 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5670 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5671 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5672 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005673 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005674 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005675 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005676 {}
5677};
5678
5679MODULE_DEVICE_TABLE(usb, rtl8152_table);
5680
5681static struct usb_driver rtl8152_driver = {
5682 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005683 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005684 .probe = rtl8152_probe,
5685 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005686 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005687 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005688 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005689 .pre_reset = rtl8152_pre_reset,
5690 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005691 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005692 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005693};
5694
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005695module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005696
5697MODULE_AUTHOR(DRIVER_AUTHOR);
5698MODULE_DESCRIPTION(DRIVER_DESC);
5699MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005700MODULE_VERSION(DRIVER_VERSION);