blob: 40d18e866269d594c4f406a366b07588ebe36f44 [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,
hayeswangd823ab62015-01-12 12:06:23 +0800622 SCHEDULE_NAPI,
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
hayeswangc81229c2014-01-02 11:22:42 +0800736
737 struct rtl_ops {
738 void (*init)(struct r8152 *);
739 int (*enable)(struct r8152 *);
740 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800741 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800742 void (*down)(struct r8152 *);
743 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800744 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
745 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800746 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800747 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800748 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800749 } rtl_ops;
750
Hayes Wang252df8b2019-08-13 11:42:06 +0800751 atomic_t rx_count;
752
hayeswang40a82912013-08-14 20:54:40 +0800753 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800754 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000755 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800756 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800757 u32 coalesce;
Hayes Wangec5791c2019-08-13 11:42:05 +0800758 u32 rx_buf_sz;
Hayes Wange4a50172019-08-13 11:42:09 +0800759 u32 rx_copybreak;
760 u32 rx_pending;
761
hayeswangac718b62013-05-02 16:01:25 +0000762 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800763 u16 speed;
hayeswang40a82912013-08-14 20:54:40 +0800764 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000765 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800766 u8 duplex;
767 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000768};
769
770enum rtl_version {
771 RTL_VER_UNKNOWN = 0,
772 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800773 RTL_VER_02,
774 RTL_VER_03,
775 RTL_VER_04,
776 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800777 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800778 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800779 RTL_VER_08,
780 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800781 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000782};
783
hayeswang60c89072014-03-07 11:04:39 +0800784enum tx_csum_stat {
785 TX_CSUM_SUCCESS = 0,
786 TX_CSUM_TSO,
787 TX_CSUM_NONE
788};
789
hayeswangac718b62013-05-02 16:01:25 +0000790/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
791 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
792 */
793static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800794static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000795
hayeswang52aec122014-09-02 10:27:52 +0800796#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800797 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800798
hayeswangac718b62013-05-02 16:01:25 +0000799static
800int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
801{
hayeswang31787f52013-07-31 17:21:25 +0800802 int ret;
803 void *tmp;
804
805 tmp = kmalloc(size, GFP_KERNEL);
806 if (!tmp)
807 return -ENOMEM;
808
809 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800810 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
811 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800812
813 memcpy(data, tmp, size);
814 kfree(tmp);
815
816 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000817}
818
819static
820int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
821{
hayeswang31787f52013-07-31 17:21:25 +0800822 int ret;
823 void *tmp;
824
Benoit Tainec4438f02014-05-26 17:21:23 +0200825 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800826 if (!tmp)
827 return -ENOMEM;
828
hayeswang31787f52013-07-31 17:21:25 +0800829 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800830 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
831 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800832
833 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800834
hayeswang31787f52013-07-31 17:21:25 +0800835 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000836}
837
Hayes Wangffa9fec2019-07-04 17:36:32 +0800838static void rtl_set_unplug(struct r8152 *tp)
839{
840 if (tp->udev->state == USB_STATE_NOTATTACHED) {
841 set_bit(RTL8152_UNPLUG, &tp->flags);
842 smp_mb__after_atomic();
843 }
844}
845
hayeswangac718b62013-05-02 16:01:25 +0000846static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800847 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000848{
hayeswang45f4a192014-01-06 17:08:41 +0800849 u16 limit = 64;
850 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000851
852 if (test_bit(RTL8152_UNPLUG, &tp->flags))
853 return -ENODEV;
854
855 /* both size and indix must be 4 bytes align */
856 if ((size & 3) || !size || (index & 3) || !data)
857 return -EPERM;
858
859 if ((u32)index + (u32)size > 0xffff)
860 return -EPERM;
861
862 while (size) {
863 if (size > limit) {
864 ret = get_registers(tp, index, type, limit, data);
865 if (ret < 0)
866 break;
867
868 index += limit;
869 data += limit;
870 size -= limit;
871 } else {
872 ret = get_registers(tp, index, type, size, data);
873 if (ret < 0)
874 break;
875
876 index += size;
877 data += size;
878 size = 0;
879 break;
880 }
881 }
882
hayeswang67610492014-10-30 11:46:40 +0800883 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800884 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800885
hayeswangac718b62013-05-02 16:01:25 +0000886 return ret;
887}
888
889static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800890 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000891{
hayeswang45f4a192014-01-06 17:08:41 +0800892 int ret;
893 u16 byteen_start, byteen_end, byen;
894 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000895
896 if (test_bit(RTL8152_UNPLUG, &tp->flags))
897 return -ENODEV;
898
899 /* both size and indix must be 4 bytes align */
900 if ((size & 3) || !size || (index & 3) || !data)
901 return -EPERM;
902
903 if ((u32)index + (u32)size > 0xffff)
904 return -EPERM;
905
906 byteen_start = byteen & BYTE_EN_START_MASK;
907 byteen_end = byteen & BYTE_EN_END_MASK;
908
909 byen = byteen_start | (byteen_start << 4);
910 ret = set_registers(tp, index, type | byen, 4, data);
911 if (ret < 0)
912 goto error1;
913
914 index += 4;
915 data += 4;
916 size -= 4;
917
918 if (size) {
919 size -= 4;
920
921 while (size) {
922 if (size > limit) {
923 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800924 type | BYTE_EN_DWORD,
925 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000926 if (ret < 0)
927 goto error1;
928
929 index += limit;
930 data += limit;
931 size -= limit;
932 } else {
933 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800934 type | BYTE_EN_DWORD,
935 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000936 if (ret < 0)
937 goto error1;
938
939 index += size;
940 data += size;
941 size = 0;
942 break;
943 }
944 }
945
946 byen = byteen_end | (byteen_end >> 4);
947 ret = set_registers(tp, index, type | byen, 4, data);
948 if (ret < 0)
949 goto error1;
950 }
951
952error1:
hayeswang67610492014-10-30 11:46:40 +0800953 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800954 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800955
hayeswangac718b62013-05-02 16:01:25 +0000956 return ret;
957}
958
959static inline
960int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
961{
962 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
963}
964
965static inline
966int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
967{
968 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
969}
970
971static inline
hayeswangac718b62013-05-02 16:01:25 +0000972int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
973{
974 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
975}
976
977static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
978{
hayeswangc8826de2013-07-31 17:21:26 +0800979 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000980
hayeswangc8826de2013-07-31 17:21:26 +0800981 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000982
983 return __le32_to_cpu(data);
984}
985
986static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
987{
hayeswangc8826de2013-07-31 17:21:26 +0800988 __le32 tmp = __cpu_to_le32(data);
989
990 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000991}
992
993static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
994{
995 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800996 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +0800997 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +0000998 u8 shift = index & 2;
999
1000 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +08001001 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +00001002
hayeswangd8fbd272017-06-15 14:44:04 +08001003 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +00001004
hayeswangc8826de2013-07-31 17:21:26 +08001005 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001006 data >>= (shift * 8);
1007 data &= 0xffff;
1008
1009 return (u16)data;
1010}
1011
1012static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1013{
hayeswangc8826de2013-07-31 17:21:26 +08001014 u32 mask = 0xffff;
1015 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001016 u16 byen = BYTE_EN_WORD;
1017 u8 shift = index & 2;
1018
1019 data &= mask;
1020
1021 if (index & 2) {
1022 byen <<= shift;
1023 mask <<= (shift * 8);
1024 data <<= (shift * 8);
1025 index &= ~3;
1026 }
1027
hayeswangc8826de2013-07-31 17:21:26 +08001028 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001029
hayeswangc8826de2013-07-31 17:21:26 +08001030 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001031}
1032
1033static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1034{
1035 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001036 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001037 u8 shift = index & 3;
1038
1039 index &= ~3;
1040
hayeswangc8826de2013-07-31 17:21:26 +08001041 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001042
hayeswangc8826de2013-07-31 17:21:26 +08001043 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001044 data >>= (shift * 8);
1045 data &= 0xff;
1046
1047 return (u8)data;
1048}
1049
1050static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1051{
hayeswangc8826de2013-07-31 17:21:26 +08001052 u32 mask = 0xff;
1053 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001054 u16 byen = BYTE_EN_BYTE;
1055 u8 shift = index & 3;
1056
1057 data &= mask;
1058
1059 if (index & 3) {
1060 byen <<= shift;
1061 mask <<= (shift * 8);
1062 data <<= (shift * 8);
1063 index &= ~3;
1064 }
1065
hayeswangc8826de2013-07-31 17:21:26 +08001066 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001067
hayeswangc8826de2013-07-31 17:21:26 +08001068 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001069}
1070
hayeswangac244d32014-01-02 11:22:40 +08001071static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1072{
1073 u16 ocp_base, ocp_index;
1074
1075 ocp_base = addr & 0xf000;
1076 if (ocp_base != tp->ocp_base) {
1077 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1078 tp->ocp_base = ocp_base;
1079 }
1080
1081 ocp_index = (addr & 0x0fff) | 0xb000;
1082 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1083}
1084
hayeswange3fe0b12014-01-02 11:22:39 +08001085static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1086{
1087 u16 ocp_base, ocp_index;
1088
1089 ocp_base = addr & 0xf000;
1090 if (ocp_base != tp->ocp_base) {
1091 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1092 tp->ocp_base = ocp_base;
1093 }
1094
1095 ocp_index = (addr & 0x0fff) | 0xb000;
1096 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1097}
1098
hayeswangac244d32014-01-02 11:22:40 +08001099static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001100{
hayeswangac244d32014-01-02 11:22:40 +08001101 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001102}
1103
hayeswangac244d32014-01-02 11:22:40 +08001104static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001105{
hayeswangac244d32014-01-02 11:22:40 +08001106 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001107}
1108
hayeswang43779f82014-01-02 11:25:10 +08001109static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1110{
1111 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1112 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1113}
1114
hayeswang65b82d62017-06-15 14:44:03 +08001115static u16 sram_read(struct r8152 *tp, u16 addr)
1116{
1117 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1118 return ocp_reg_read(tp, OCP_SRAM_DATA);
1119}
1120
hayeswangac718b62013-05-02 16:01:25 +00001121static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1122{
1123 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001124 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001125
hayeswang68714382014-04-11 17:54:31 +08001126 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1127 return -ENODEV;
1128
hayeswangac718b62013-05-02 16:01:25 +00001129 if (phy_id != R8152_PHY_ID)
1130 return -EINVAL;
1131
hayeswang9a4be1b2014-02-18 21:49:07 +08001132 ret = r8152_mdio_read(tp, reg);
1133
hayeswang9a4be1b2014-02-18 21:49:07 +08001134 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001135}
1136
1137static
1138void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1139{
1140 struct r8152 *tp = netdev_priv(netdev);
1141
hayeswang68714382014-04-11 17:54:31 +08001142 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1143 return;
1144
hayeswangac718b62013-05-02 16:01:25 +00001145 if (phy_id != R8152_PHY_ID)
1146 return;
1147
1148 r8152_mdio_write(tp, reg, val);
1149}
1150
hayeswangb209af92014-08-25 15:53:00 +08001151static int
1152r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001153
hayeswang8ba789a2014-09-04 16:15:41 +08001154static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1155{
1156 struct r8152 *tp = netdev_priv(netdev);
1157 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001158 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001159
1160 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001161 goto out1;
1162
1163 ret = usb_autopm_get_interface(tp->intf);
1164 if (ret < 0)
1165 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001166
hayeswangb5403272014-10-09 18:00:26 +08001167 mutex_lock(&tp->control);
1168
hayeswang8ba789a2014-09-04 16:15:41 +08001169 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1170
1171 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1172 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1173 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1174
hayeswangb5403272014-10-09 18:00:26 +08001175 mutex_unlock(&tp->control);
1176
hayeswangea6a7112014-10-02 17:03:12 +08001177 usb_autopm_put_interface(tp->intf);
1178out1:
1179 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001180}
1181
Mario Limonciello9c273692018-12-11 08:16:14 -06001182/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001183 * host system provided MAC address.
1184 * Examples of this are Dell TB15 and Dell WD15 docks
1185 */
1186static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1187{
1188 acpi_status status;
1189 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1190 union acpi_object *obj;
1191 int ret = -EINVAL;
1192 u32 ocp_data;
1193 unsigned char buf[6];
1194
1195 /* test for -AD variant of RTL8153 */
1196 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001197 if ((ocp_data & AD_MASK) == 0x1000) {
1198 /* test for MAC address pass-through bit */
1199 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1200 if ((ocp_data & PASS_THRU_MASK) != 1) {
1201 netif_dbg(tp, probe, tp->netdev,
1202 "No efuse for RTL8153-AD MAC pass through\n");
1203 return -ENODEV;
1204 }
1205 } else {
David Chen8e29d232019-02-16 17:16:42 +08001206 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001207 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001208 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001209 netif_dbg(tp, probe, tp->netdev,
1210 "Invalid variant for MAC pass through\n");
1211 return -ENODEV;
1212 }
1213 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001214
1215 /* returns _AUXMAC_#AABBCCDDEEFF# */
1216 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1217 obj = (union acpi_object *)buffer.pointer;
1218 if (!ACPI_SUCCESS(status))
1219 return -ENODEV;
1220 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1221 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001222 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001223 obj->type, obj->string.length);
1224 goto amacout;
1225 }
1226 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1227 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1228 netif_warn(tp, probe, tp->netdev,
1229 "Invalid header when reading pass-thru MAC addr\n");
1230 goto amacout;
1231 }
1232 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1233 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1234 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001235 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1236 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001237 ret = -EINVAL;
1238 goto amacout;
1239 }
1240 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001241 netif_info(tp, probe, tp->netdev,
1242 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1243
1244amacout:
1245 kfree(obj);
1246 return ret;
1247}
1248
Mario Limonciello25766272019-04-04 13:46:53 -05001249static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1250{
1251 struct net_device *dev = tp->netdev;
1252 int ret;
1253
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001254 sa->sa_family = dev->type;
1255
Mario Limonciello25766272019-04-04 13:46:53 -05001256 if (tp->version == RTL_VER_01) {
1257 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1258 } else {
1259 /* if device doesn't support MAC pass through this will
1260 * be expected to be non-zero
1261 */
1262 ret = vendor_mac_passthru_addr_read(tp, sa);
1263 if (ret < 0)
1264 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1265 }
1266
1267 if (ret < 0) {
1268 netif_err(tp, probe, dev, "Get ether addr fail\n");
1269 } else if (!is_valid_ether_addr(sa->sa_data)) {
1270 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1271 sa->sa_data);
1272 eth_hw_addr_random(dev);
1273 ether_addr_copy(sa->sa_data, dev->dev_addr);
1274 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1275 sa->sa_data);
1276 return 0;
1277 }
1278
1279 return ret;
1280}
1281
hayeswang179bb6d2014-09-04 16:15:42 +08001282static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001283{
1284 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001285 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001286 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001287
Mario Limonciello25766272019-04-04 13:46:53 -05001288 ret = determine_ethernet_addr(tp, &sa);
1289 if (ret < 0)
1290 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001291
Mario Limonciello25766272019-04-04 13:46:53 -05001292 if (tp->version == RTL_VER_01)
1293 ether_addr_copy(dev->dev_addr, sa.sa_data);
1294 else
hayeswang179bb6d2014-09-04 16:15:42 +08001295 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001296
1297 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001298}
1299
hayeswangac718b62013-05-02 16:01:25 +00001300static void read_bulk_callback(struct urb *urb)
1301{
hayeswangac718b62013-05-02 16:01:25 +00001302 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001303 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001304 struct rx_agg *agg;
1305 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001306 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001307
hayeswangebc2ec482013-08-14 20:54:38 +08001308 agg = urb->context;
1309 if (!agg)
1310 return;
1311
1312 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001313 if (!tp)
1314 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001315
hayeswangac718b62013-05-02 16:01:25 +00001316 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1317 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001318
1319 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001320 return;
1321
hayeswangebc2ec482013-08-14 20:54:38 +08001322 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001323
1324 /* When link down, the driver would cancel all bulks. */
1325 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001326 if (!netif_carrier_ok(netdev))
1327 return;
1328
hayeswang9a4be1b2014-02-18 21:49:07 +08001329 usb_mark_last_busy(tp->udev);
1330
hayeswangac718b62013-05-02 16:01:25 +00001331 switch (status) {
1332 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001333 if (urb->actual_length < ETH_ZLEN)
1334 break;
1335
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001336 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001337 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001338 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001339 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001340 return;
hayeswangac718b62013-05-02 16:01:25 +00001341 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001342 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001343 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001344 return;
hayeswangac718b62013-05-02 16:01:25 +00001345 case -ENOENT:
1346 return; /* the urb is in unlink state */
1347 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001348 if (net_ratelimit())
1349 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001350 break;
hayeswangac718b62013-05-02 16:01:25 +00001351 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001352 if (net_ratelimit())
1353 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001354 break;
hayeswangac718b62013-05-02 16:01:25 +00001355 }
1356
hayeswanga0fccd42014-11-20 10:29:05 +08001357 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001358}
1359
1360static void write_bulk_callback(struct urb *urb)
1361{
hayeswangebc2ec482013-08-14 20:54:38 +08001362 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001363 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001364 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001365 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001366 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001367 int status = urb->status;
1368
hayeswangebc2ec482013-08-14 20:54:38 +08001369 agg = urb->context;
1370 if (!agg)
1371 return;
1372
1373 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001374 if (!tp)
1375 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001376
hayeswangd104eaf2014-03-06 15:07:17 +08001377 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001378 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001379 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001380 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001381 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001382 stats->tx_errors += agg->skb_num;
1383 } else {
1384 stats->tx_packets += agg->skb_num;
1385 stats->tx_bytes += agg->skb_len;
1386 }
1387
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001388 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001389 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001390 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001391
hayeswang9a4be1b2014-02-18 21:49:07 +08001392 usb_autopm_put_interface_async(tp->intf);
1393
hayeswangd104eaf2014-03-06 15:07:17 +08001394 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001395 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001396
1397 if (!test_bit(WORK_ENABLE, &tp->flags))
1398 return;
1399
1400 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1401 return;
1402
1403 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001404 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001405}
1406
hayeswang40a82912013-08-14 20:54:40 +08001407static void intr_callback(struct urb *urb)
1408{
1409 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001410 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001411 int status = urb->status;
1412 int res;
1413
1414 tp = urb->context;
1415 if (!tp)
1416 return;
1417
1418 if (!test_bit(WORK_ENABLE, &tp->flags))
1419 return;
1420
1421 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1422 return;
1423
1424 switch (status) {
1425 case 0: /* success */
1426 break;
1427 case -ECONNRESET: /* unlink */
1428 case -ESHUTDOWN:
1429 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001430 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001431 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001432 case -EPROTO:
1433 netif_info(tp, intr, tp->netdev,
1434 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001435 return;
1436 case -EOVERFLOW:
1437 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1438 goto resubmit;
1439 /* -EPIPE: should clear the halt */
1440 default:
1441 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1442 goto resubmit;
1443 }
1444
1445 d = urb->transfer_buffer;
1446 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001447 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001448 set_bit(RTL8152_LINK_CHG, &tp->flags);
1449 schedule_delayed_work(&tp->schedule, 0);
1450 }
1451 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001452 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001453 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001454 set_bit(RTL8152_LINK_CHG, &tp->flags);
1455 schedule_delayed_work(&tp->schedule, 0);
1456 }
1457 }
1458
1459resubmit:
1460 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001461 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001462 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001463 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001464 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001465 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001466 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001467 }
hayeswang40a82912013-08-14 20:54:40 +08001468}
1469
hayeswangebc2ec482013-08-14 20:54:38 +08001470static inline void *rx_agg_align(void *data)
1471{
hayeswang8e1f51b2014-01-02 11:22:41 +08001472 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001473}
1474
1475static inline void *tx_agg_align(void *data)
1476{
hayeswang8e1f51b2014-01-02 11:22:41 +08001477 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001478}
1479
Hayes Wang252df8b2019-08-13 11:42:06 +08001480static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1481{
1482 list_del(&agg->info_list);
1483
1484 usb_free_urb(agg->urb);
Hayes Wang47922fc2019-08-13 11:42:08 +08001485 put_page(agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001486 kfree(agg);
1487
1488 atomic_dec(&tp->rx_count);
1489}
1490
1491static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1492{
1493 struct net_device *netdev = tp->netdev;
1494 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
Hayes Wangd55d7082019-08-13 11:42:07 +08001495 unsigned int order = get_order(tp->rx_buf_sz);
Hayes Wang252df8b2019-08-13 11:42:06 +08001496 struct rx_agg *rx_agg;
1497 unsigned long flags;
Hayes Wang252df8b2019-08-13 11:42:06 +08001498
1499 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1500 if (!rx_agg)
1501 return NULL;
1502
Hayes Wang47922fc2019-08-13 11:42:08 +08001503 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
Hayes Wangd55d7082019-08-13 11:42:07 +08001504 if (!rx_agg->page)
Hayes Wang252df8b2019-08-13 11:42:06 +08001505 goto free_rx;
1506
Hayes Wangd55d7082019-08-13 11:42:07 +08001507 rx_agg->buffer = page_address(rx_agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001508
1509 rx_agg->urb = usb_alloc_urb(0, mflags);
1510 if (!rx_agg->urb)
1511 goto free_buf;
1512
1513 rx_agg->context = tp;
1514
1515 INIT_LIST_HEAD(&rx_agg->list);
1516 INIT_LIST_HEAD(&rx_agg->info_list);
1517 spin_lock_irqsave(&tp->rx_lock, flags);
1518 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1519 spin_unlock_irqrestore(&tp->rx_lock, flags);
1520
1521 atomic_inc(&tp->rx_count);
1522
1523 return rx_agg;
1524
1525free_buf:
Hayes Wangd55d7082019-08-13 11:42:07 +08001526 __free_pages(rx_agg->page, order);
Hayes Wang252df8b2019-08-13 11:42:06 +08001527free_rx:
1528 kfree(rx_agg);
1529 return NULL;
1530}
1531
hayeswangebc2ec482013-08-14 20:54:38 +08001532static void free_all_mem(struct r8152 *tp)
1533{
Hayes Wang252df8b2019-08-13 11:42:06 +08001534 struct rx_agg *agg, *agg_next;
1535 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001536 int i;
1537
Hayes Wang252df8b2019-08-13 11:42:06 +08001538 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001539
Hayes Wang252df8b2019-08-13 11:42:06 +08001540 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1541 free_rx_agg(tp, agg);
1542
1543 spin_unlock_irqrestore(&tp->rx_lock, flags);
1544
1545 WARN_ON(atomic_read(&tp->rx_count));
hayeswangebc2ec482013-08-14 20:54:38 +08001546
1547 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001548 usb_free_urb(tp->tx_info[i].urb);
1549 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001550
hayeswang9629e3c2014-01-15 10:42:15 +08001551 kfree(tp->tx_info[i].buffer);
1552 tp->tx_info[i].buffer = NULL;
1553 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001554 }
hayeswang40a82912013-08-14 20:54:40 +08001555
hayeswang9629e3c2014-01-15 10:42:15 +08001556 usb_free_urb(tp->intr_urb);
1557 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001558
hayeswang9629e3c2014-01-15 10:42:15 +08001559 kfree(tp->intr_buff);
1560 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001561}
1562
1563static int alloc_all_mem(struct r8152 *tp)
1564{
1565 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001566 struct usb_interface *intf = tp->intf;
1567 struct usb_host_interface *alt = intf->cur_altsetting;
1568 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001569 int node, i;
hayeswangebc2ec482013-08-14 20:54:38 +08001570
1571 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1572
1573 spin_lock_init(&tp->rx_lock);
1574 spin_lock_init(&tp->tx_lock);
Hayes Wang252df8b2019-08-13 11:42:06 +08001575 INIT_LIST_HEAD(&tp->rx_info);
hayeswangebc2ec482013-08-14 20:54:38 +08001576 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001577 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001578 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001579 skb_queue_head_init(&tp->rx_queue);
Hayes Wang252df8b2019-08-13 11:42:06 +08001580 atomic_set(&tp->rx_count, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001581
1582 for (i = 0; i < RTL8152_MAX_RX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001583 if (!alloc_rx_agg(tp, GFP_KERNEL))
hayeswangebc2ec482013-08-14 20:54:38 +08001584 goto err1;
hayeswangebc2ec482013-08-14 20:54:38 +08001585 }
1586
1587 for (i = 0; i < RTL8152_MAX_TX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001588 struct urb *urb;
1589 u8 *buf;
1590
hayeswang52aec122014-09-02 10:27:52 +08001591 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001592 if (!buf)
1593 goto err1;
1594
1595 if (buf != tx_agg_align(buf)) {
1596 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001597 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001598 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001599 if (!buf)
1600 goto err1;
1601 }
1602
1603 urb = usb_alloc_urb(0, GFP_KERNEL);
1604 if (!urb) {
1605 kfree(buf);
1606 goto err1;
1607 }
1608
1609 INIT_LIST_HEAD(&tp->tx_info[i].list);
1610 tp->tx_info[i].context = tp;
1611 tp->tx_info[i].urb = urb;
1612 tp->tx_info[i].buffer = buf;
1613 tp->tx_info[i].head = tx_agg_align(buf);
1614
1615 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1616 }
1617
hayeswang40a82912013-08-14 20:54:40 +08001618 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1619 if (!tp->intr_urb)
1620 goto err1;
1621
1622 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1623 if (!tp->intr_buff)
1624 goto err1;
1625
1626 tp->intr_interval = (int)ep_intr->desc.bInterval;
1627 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001628 tp->intr_buff, INTBUFSIZE, intr_callback,
1629 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001630
hayeswangebc2ec482013-08-14 20:54:38 +08001631 return 0;
1632
1633err1:
1634 free_all_mem(tp);
1635 return -ENOMEM;
1636}
1637
hayeswang0de98f62013-08-16 16:09:35 +08001638static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1639{
1640 struct tx_agg *agg = NULL;
1641 unsigned long flags;
1642
hayeswang21949ab2014-03-07 11:04:35 +08001643 if (list_empty(&tp->tx_free))
1644 return NULL;
1645
hayeswang0de98f62013-08-16 16:09:35 +08001646 spin_lock_irqsave(&tp->tx_lock, flags);
1647 if (!list_empty(&tp->tx_free)) {
1648 struct list_head *cursor;
1649
1650 cursor = tp->tx_free.next;
1651 list_del_init(cursor);
1652 agg = list_entry(cursor, struct tx_agg, list);
1653 }
1654 spin_unlock_irqrestore(&tp->tx_lock, flags);
1655
1656 return agg;
1657}
1658
hayeswangb209af92014-08-25 15:53:00 +08001659/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001660 * The hw limites the value the transport offset. When the offset is out of the
1661 * range, calculate the checksum by sw.
1662 */
1663static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1664 struct sk_buff_head *list)
1665{
1666 if (skb_shinfo(skb)->gso_size) {
1667 netdev_features_t features = tp->netdev->features;
1668 struct sk_buff_head seg_list;
1669 struct sk_buff *segs, *nskb;
1670
hayeswanga91d45f2014-07-11 16:48:27 +08001671 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001672 segs = skb_gso_segment(skb, features);
1673 if (IS_ERR(segs) || !segs)
1674 goto drop;
1675
1676 __skb_queue_head_init(&seg_list);
1677
1678 do {
1679 nskb = segs;
1680 segs = segs->next;
1681 nskb->next = NULL;
1682 __skb_queue_tail(&seg_list, nskb);
1683 } while (segs);
1684
1685 skb_queue_splice(&seg_list, list);
1686 dev_kfree_skb(skb);
1687 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1688 if (skb_checksum_help(skb) < 0)
1689 goto drop;
1690
1691 __skb_queue_head(list, skb);
1692 } else {
1693 struct net_device_stats *stats;
1694
1695drop:
1696 stats = &tp->netdev->stats;
1697 stats->tx_dropped++;
1698 dev_kfree_skb(skb);
1699 }
1700}
1701
hayeswangb209af92014-08-25 15:53:00 +08001702/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001703 * According to the document of microsoft, the TCP Pseudo Header excludes the
1704 * packet length for IPv6 TCP large packets.
1705 */
1706static int msdn_giant_send_check(struct sk_buff *skb)
1707{
1708 const struct ipv6hdr *ipv6h;
1709 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001710 int ret;
1711
1712 ret = skb_cow_head(skb, 0);
1713 if (ret)
1714 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001715
1716 ipv6h = ipv6_hdr(skb);
1717 th = tcp_hdr(skb);
1718
1719 th->check = 0;
1720 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1721
hayeswangfcb308d2014-03-11 10:20:32 +08001722 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001723}
1724
hayeswangc5554292014-09-12 10:43:11 +08001725static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1726{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001727 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001728 u32 opts2;
1729
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001730 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001731 desc->opts2 |= cpu_to_le32(opts2);
1732 }
1733}
1734
1735static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1736{
1737 u32 opts2 = le32_to_cpu(desc->opts2);
1738
1739 if (opts2 & RX_VLAN_TAG)
1740 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1741 swab16(opts2 & 0xffff));
1742}
1743
hayeswang60c89072014-03-07 11:04:39 +08001744static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1745 struct sk_buff *skb, u32 len, u32 transport_offset)
1746{
1747 u32 mss = skb_shinfo(skb)->gso_size;
1748 u32 opts1, opts2 = 0;
1749 int ret = TX_CSUM_SUCCESS;
1750
1751 WARN_ON_ONCE(len > TX_LEN_MAX);
1752
1753 opts1 = len | TX_FS | TX_LS;
1754
1755 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001756 if (transport_offset > GTTCPHO_MAX) {
1757 netif_warn(tp, tx_err, tp->netdev,
1758 "Invalid transport offset 0x%x for TSO\n",
1759 transport_offset);
1760 ret = TX_CSUM_TSO;
1761 goto unavailable;
1762 }
1763
hayeswang6e74d172015-02-06 11:30:50 +08001764 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001765 case htons(ETH_P_IP):
1766 opts1 |= GTSENDV4;
1767 break;
1768
hayeswang6128d1bb2014-03-07 11:04:40 +08001769 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001770 if (msdn_giant_send_check(skb)) {
1771 ret = TX_CSUM_TSO;
1772 goto unavailable;
1773 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001774 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001775 break;
1776
hayeswang60c89072014-03-07 11:04:39 +08001777 default:
1778 WARN_ON_ONCE(1);
1779 break;
1780 }
1781
1782 opts1 |= transport_offset << GTTCPHO_SHIFT;
1783 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1784 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001785 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001786
hayeswang6128d1bb2014-03-07 11:04:40 +08001787 if (transport_offset > TCPHO_MAX) {
1788 netif_warn(tp, tx_err, tp->netdev,
1789 "Invalid transport offset 0x%x\n",
1790 transport_offset);
1791 ret = TX_CSUM_NONE;
1792 goto unavailable;
1793 }
1794
hayeswang6e74d172015-02-06 11:30:50 +08001795 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001796 case htons(ETH_P_IP):
1797 opts2 |= IPV4_CS;
1798 ip_protocol = ip_hdr(skb)->protocol;
1799 break;
1800
1801 case htons(ETH_P_IPV6):
1802 opts2 |= IPV6_CS;
1803 ip_protocol = ipv6_hdr(skb)->nexthdr;
1804 break;
1805
1806 default:
1807 ip_protocol = IPPROTO_RAW;
1808 break;
1809 }
1810
hayeswang60c89072014-03-07 11:04:39 +08001811 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001812 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001813 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001814 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001815 else
hayeswang5bd23882013-08-14 20:54:39 +08001816 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001817
hayeswang60c89072014-03-07 11:04:39 +08001818 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001819 }
hayeswang60c89072014-03-07 11:04:39 +08001820
1821 desc->opts2 = cpu_to_le32(opts2);
1822 desc->opts1 = cpu_to_le32(opts1);
1823
hayeswang6128d1bb2014-03-07 11:04:40 +08001824unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001825 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001826}
1827
hayeswangb1379d92013-08-16 16:09:37 +08001828static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1829{
hayeswangd84130a2014-02-18 21:49:02 +08001830 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001831 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001832 u8 *tx_data;
1833
hayeswangd84130a2014-02-18 21:49:02 +08001834 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001835 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001836 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001837 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001838
hayeswangb1379d92013-08-16 16:09:37 +08001839 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001840 agg->skb_num = 0;
1841 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001842 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001843
hayeswang7937f9e2013-11-20 17:30:54 +08001844 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001845 struct tx_desc *tx_desc;
1846 struct sk_buff *skb;
1847 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001848 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001849
hayeswangd84130a2014-02-18 21:49:02 +08001850 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001851 if (!skb)
1852 break;
1853
hayeswang60c89072014-03-07 11:04:39 +08001854 len = skb->len + sizeof(*tx_desc);
1855
1856 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001857 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001858 break;
1859 }
1860
hayeswang7937f9e2013-11-20 17:30:54 +08001861 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001862 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001863
1864 offset = (u32)skb_transport_offset(skb);
1865
hayeswang6128d1bb2014-03-07 11:04:40 +08001866 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1867 r8152_csum_workaround(tp, skb, &skb_head);
1868 continue;
1869 }
hayeswang60c89072014-03-07 11:04:39 +08001870
hayeswangc5554292014-09-12 10:43:11 +08001871 rtl_tx_vlan_tag(tx_desc, skb);
1872
hayeswangb1379d92013-08-16 16:09:37 +08001873 tx_data += sizeof(*tx_desc);
1874
hayeswang60c89072014-03-07 11:04:39 +08001875 len = skb->len;
1876 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1877 struct net_device_stats *stats = &tp->netdev->stats;
1878
1879 stats->tx_dropped++;
1880 dev_kfree_skb_any(skb);
1881 tx_data -= sizeof(*tx_desc);
1882 continue;
1883 }
hayeswangb1379d92013-08-16 16:09:37 +08001884
hayeswang7937f9e2013-11-20 17:30:54 +08001885 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001886 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001887 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001888
1889 dev_kfree_skb_any(skb);
1890
hayeswang52aec122014-09-02 10:27:52 +08001891 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001892
1893 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1894 break;
hayeswangb1379d92013-08-16 16:09:37 +08001895 }
1896
hayeswangd84130a2014-02-18 21:49:02 +08001897 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001898 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001899 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001900 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001901 }
1902
hayeswang0c3121f2014-03-07 11:04:36 +08001903 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001904
1905 if (netif_queue_stopped(tp->netdev) &&
1906 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1907 netif_wake_queue(tp->netdev);
1908
hayeswang0c3121f2014-03-07 11:04:36 +08001909 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001910
hayeswang0c3121f2014-03-07 11:04:36 +08001911 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001912 if (ret < 0)
1913 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001914
hayeswangb1379d92013-08-16 16:09:37 +08001915 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1916 agg->head, (int)(tx_data - (u8 *)agg->head),
1917 (usb_complete_t)write_bulk_callback, agg);
1918
hayeswang0c3121f2014-03-07 11:04:36 +08001919 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001920 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001921 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001922
1923out_tx_fill:
1924 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001925}
1926
hayeswang565cab02014-03-07 11:04:38 +08001927static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1928{
1929 u8 checksum = CHECKSUM_NONE;
1930 u32 opts2, opts3;
1931
hayeswang19c0f402017-01-11 16:25:34 +08001932 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001933 goto return_result;
1934
1935 opts2 = le32_to_cpu(rx_desc->opts2);
1936 opts3 = le32_to_cpu(rx_desc->opts3);
1937
1938 if (opts2 & RD_IPV4_CS) {
1939 if (opts3 & IPF)
1940 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001941 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1942 checksum = CHECKSUM_UNNECESSARY;
1943 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001944 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001945 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001946 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1947 checksum = CHECKSUM_UNNECESSARY;
1948 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1949 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001950 }
1951
1952return_result:
1953 return checksum;
1954}
1955
Hayes Wang47922fc2019-08-13 11:42:08 +08001956static inline bool rx_count_exceed(struct r8152 *tp)
1957{
1958 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
1959}
1960
1961static inline int agg_offset(struct rx_agg *agg, void *addr)
1962{
1963 return (int)(addr - agg->buffer);
1964}
1965
1966static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
1967{
1968 struct rx_agg *agg, *agg_next, *agg_free = NULL;
1969 unsigned long flags;
1970
1971 spin_lock_irqsave(&tp->rx_lock, flags);
1972
1973 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
1974 if (page_count(agg->page) == 1) {
1975 if (!agg_free) {
1976 list_del_init(&agg->list);
1977 agg_free = agg;
1978 continue;
1979 }
1980 if (rx_count_exceed(tp)) {
1981 list_del_init(&agg->list);
1982 free_rx_agg(tp, agg);
1983 }
1984 break;
1985 }
1986 }
1987
1988 spin_unlock_irqrestore(&tp->rx_lock, flags);
1989
Hayes Wange4a50172019-08-13 11:42:09 +08001990 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
Hayes Wang47922fc2019-08-13 11:42:08 +08001991 agg_free = alloc_rx_agg(tp, mflags);
1992
1993 return agg_free;
1994}
1995
hayeswangd823ab62015-01-12 12:06:23 +08001996static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001997{
hayeswanga5a4f462013-08-16 16:09:34 +08001998 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001999 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08002000 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08002001 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08002002
2003 if (!skb_queue_empty(&tp->rx_queue)) {
2004 while (work_done < budget) {
2005 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2006 struct net_device *netdev = tp->netdev;
2007 struct net_device_stats *stats = &netdev->stats;
2008 unsigned int pkt_len;
2009
2010 if (!skb)
2011 break;
2012
2013 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08002014 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002015 work_done++;
2016 stats->rx_packets++;
2017 stats->rx_bytes += pkt_len;
2018 }
2019 }
hayeswangebc2ec482013-08-14 20:54:38 +08002020
hayeswangd84130a2014-02-18 21:49:02 +08002021 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08002022 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08002023
2024 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08002025 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08002026 list_splice_init(&tp->rx_done, &rx_queue);
2027 spin_unlock_irqrestore(&tp->rx_lock, flags);
2028
2029 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08002030 struct rx_desc *rx_desc;
Hayes Wang47922fc2019-08-13 11:42:08 +08002031 struct rx_agg *agg, *agg_free;
hayeswang43a44782013-08-16 16:09:36 +08002032 int len_used = 0;
2033 struct urb *urb;
2034 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08002035
hayeswangebc2ec482013-08-14 20:54:38 +08002036 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08002037
2038 agg = list_entry(cursor, struct rx_agg, list);
2039 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08002040 if (urb->actual_length < ETH_ZLEN)
2041 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08002042
Hayes Wang47922fc2019-08-13 11:42:08 +08002043 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2044
Hayes Wangd55d7082019-08-13 11:42:07 +08002045 rx_desc = agg->buffer;
2046 rx_data = agg->buffer;
hayeswang7937f9e2013-11-20 17:30:54 +08002047 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002048
hayeswang7937f9e2013-11-20 17:30:54 +08002049 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08002050 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08002051 struct net_device_stats *stats = &netdev->stats;
Hayes Wang47922fc2019-08-13 11:42:08 +08002052 unsigned int pkt_len, rx_frag_head_sz;
hayeswang43a44782013-08-16 16:09:36 +08002053 struct sk_buff *skb;
2054
hayeswang74544452017-06-09 17:11:47 +08002055 /* limite the skb numbers for rx_queue */
2056 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2057 break;
2058
hayeswang7937f9e2013-11-20 17:30:54 +08002059 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08002060 if (pkt_len < ETH_ZLEN)
2061 break;
2062
hayeswang7937f9e2013-11-20 17:30:54 +08002063 len_used += pkt_len;
2064 if (urb->actual_length < len_used)
2065 break;
2066
hayeswangb65c0c92017-06-21 11:25:18 +08002067 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08002068 rx_data += sizeof(struct rx_desc);
2069
Hayes Wange4a50172019-08-13 11:42:09 +08002070 if (!agg_free || tp->rx_copybreak > pkt_len)
Hayes Wang47922fc2019-08-13 11:42:08 +08002071 rx_frag_head_sz = pkt_len;
2072 else
Hayes Wange4a50172019-08-13 11:42:09 +08002073 rx_frag_head_sz = tp->rx_copybreak;
Hayes Wang47922fc2019-08-13 11:42:08 +08002074
2075 skb = napi_alloc_skb(napi, rx_frag_head_sz);
hayeswangebc2ec482013-08-14 20:54:38 +08002076 if (!skb) {
2077 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08002078 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08002079 }
hayeswang565cab02014-03-07 11:04:38 +08002080
2081 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
Hayes Wang47922fc2019-08-13 11:42:08 +08002082 memcpy(skb->data, rx_data, rx_frag_head_sz);
2083 skb_put(skb, rx_frag_head_sz);
2084 pkt_len -= rx_frag_head_sz;
2085 rx_data += rx_frag_head_sz;
2086 if (pkt_len) {
2087 skb_add_rx_frag(skb, 0, agg->page,
2088 agg_offset(agg, rx_data),
2089 pkt_len,
2090 SKB_DATA_ALIGN(pkt_len));
2091 get_page(agg->page);
2092 }
2093
hayeswangebc2ec482013-08-14 20:54:38 +08002094 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08002095 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002096 if (work_done < budget) {
hayeswangce594e92017-03-16 14:32:22 +08002097 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002098 work_done++;
2099 stats->rx_packets++;
Hayes Wang47922fc2019-08-13 11:42:08 +08002100 stats->rx_bytes += skb->len;
hayeswangd823ab62015-01-12 12:06:23 +08002101 } else {
2102 __skb_queue_tail(&tp->rx_queue, skb);
2103 }
hayeswangebc2ec482013-08-14 20:54:38 +08002104
hayeswang5e2f7482014-03-07 11:04:37 +08002105find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002106 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002107 rx_desc = (struct rx_desc *)rx_data;
Hayes Wang47922fc2019-08-13 11:42:08 +08002108 len_used = agg_offset(agg, rx_data);
hayeswang7937f9e2013-11-20 17:30:54 +08002109 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002110 }
2111
Hayes Wang47922fc2019-08-13 11:42:08 +08002112 WARN_ON(!agg_free && page_count(agg->page) > 1);
2113
2114 if (agg_free) {
2115 spin_lock_irqsave(&tp->rx_lock, flags);
2116 if (page_count(agg->page) == 1) {
2117 list_add(&agg_free->list, &tp->rx_used);
2118 } else {
2119 list_add_tail(&agg->list, &tp->rx_used);
2120 agg = agg_free;
2121 urb = agg->urb;
2122 }
2123 spin_unlock_irqrestore(&tp->rx_lock, flags);
2124 }
2125
hayeswang0de98f62013-08-16 16:09:35 +08002126submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002127 if (!ret) {
2128 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2129 } else {
2130 urb->actual_length = 0;
2131 list_add_tail(&agg->list, next);
2132 }
2133 }
2134
2135 if (!list_empty(&rx_queue)) {
2136 spin_lock_irqsave(&tp->rx_lock, flags);
2137 list_splice_tail(&rx_queue, &tp->rx_done);
2138 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002139 }
hayeswangd823ab62015-01-12 12:06:23 +08002140
2141out1:
2142 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002143}
2144
2145static void tx_bottom(struct r8152 *tp)
2146{
hayeswangebc2ec482013-08-14 20:54:38 +08002147 int res;
2148
hayeswangb1379d92013-08-16 16:09:37 +08002149 do {
2150 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002151
hayeswangb1379d92013-08-16 16:09:37 +08002152 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002153 break;
2154
hayeswangb1379d92013-08-16 16:09:37 +08002155 agg = r8152_get_tx_agg(tp);
2156 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002157 break;
hayeswangb1379d92013-08-16 16:09:37 +08002158
2159 res = r8152_tx_agg_fill(tp, agg);
2160 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002161 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002162
2163 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002164 rtl_set_unplug(tp);
hayeswangb1379d92013-08-16 16:09:37 +08002165 netif_device_detach(netdev);
2166 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002167 struct net_device_stats *stats = &netdev->stats;
2168 unsigned long flags;
2169
hayeswangb1379d92013-08-16 16:09:37 +08002170 netif_warn(tp, tx_err, netdev,
2171 "failed tx_urb %d\n", res);
2172 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002173
hayeswangb1379d92013-08-16 16:09:37 +08002174 spin_lock_irqsave(&tp->tx_lock, flags);
2175 list_add_tail(&agg->list, &tp->tx_free);
2176 spin_unlock_irqrestore(&tp->tx_lock, flags);
2177 }
hayeswangebc2ec482013-08-14 20:54:38 +08002178 }
hayeswangb1379d92013-08-16 16:09:37 +08002179 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002180}
2181
hayeswangd823ab62015-01-12 12:06:23 +08002182static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08002183{
hayeswangebc2ec482013-08-14 20:54:38 +08002184 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2185 return;
2186
2187 if (!test_bit(WORK_ENABLE, &tp->flags))
2188 return;
2189
hayeswang7559fb2f2013-08-16 16:09:38 +08002190 /* When link down, the driver would cancel all bulks. */
2191 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002192 if (!netif_carrier_ok(tp->netdev))
2193 return;
2194
hayeswangd823ab62015-01-12 12:06:23 +08002195 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002196
hayeswang0c3121f2014-03-07 11:04:36 +08002197 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002198}
2199
hayeswangd823ab62015-01-12 12:06:23 +08002200static int r8152_poll(struct napi_struct *napi, int budget)
2201{
2202 struct r8152 *tp = container_of(napi, struct r8152, napi);
2203 int work_done;
2204
2205 work_done = rx_bottom(tp, budget);
2206 bottom_half(tp);
2207
2208 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002209 if (!napi_complete_done(napi, work_done))
2210 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002211 if (!list_empty(&tp->rx_done))
2212 napi_schedule(napi);
hayeswang248b2132017-01-26 09:38:33 +08002213 else if (!skb_queue_empty(&tp->tx_queue) &&
2214 !list_empty(&tp->tx_free))
2215 napi_schedule(napi);
hayeswangd823ab62015-01-12 12:06:23 +08002216 }
2217
hayeswanga3307f92017-06-09 17:11:48 +08002218out:
hayeswangd823ab62015-01-12 12:06:23 +08002219 return work_done;
2220}
2221
hayeswangebc2ec482013-08-14 20:54:38 +08002222static
2223int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2224{
hayeswanga0fccd42014-11-20 10:29:05 +08002225 int ret;
2226
hayeswangef827a52015-01-09 10:26:36 +08002227 /* The rx would be stopped, so skip submitting */
2228 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2229 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2230 return 0;
2231
hayeswangebc2ec482013-08-14 20:54:38 +08002232 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
Hayes Wangd55d7082019-08-13 11:42:07 +08002233 agg->buffer, tp->rx_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002234 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002235
hayeswanga0fccd42014-11-20 10:29:05 +08002236 ret = usb_submit_urb(agg->urb, mem_flags);
2237 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002238 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002239 netif_device_detach(tp->netdev);
2240 } else if (ret) {
2241 struct urb *urb = agg->urb;
2242 unsigned long flags;
2243
2244 urb->actual_length = 0;
2245 spin_lock_irqsave(&tp->rx_lock, flags);
2246 list_add_tail(&agg->list, &tp->rx_done);
2247 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002248
2249 netif_err(tp, rx_err, tp->netdev,
2250 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2251
2252 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002253 }
2254
2255 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002256}
2257
hayeswang00a5e362014-02-18 21:48:59 +08002258static void rtl_drop_queued_tx(struct r8152 *tp)
2259{
2260 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002261 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002262 struct sk_buff *skb;
2263
hayeswangd84130a2014-02-18 21:49:02 +08002264 if (skb_queue_empty(tx_queue))
2265 return;
2266
2267 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002268 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002269 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002270 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002271
2272 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002273 dev_kfree_skb(skb);
2274 stats->tx_dropped++;
2275 }
2276}
2277
hayeswangac718b62013-05-02 16:01:25 +00002278static void rtl8152_tx_timeout(struct net_device *netdev)
2279{
2280 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002281
Hayes Wang4a8deae2014-01-07 11:18:22 +08002282 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002283
2284 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002285}
2286
2287static void rtl8152_set_rx_mode(struct net_device *netdev)
2288{
2289 struct r8152 *tp = netdev_priv(netdev);
2290
hayeswang51d979f2015-02-06 11:30:47 +08002291 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002292 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002293 schedule_delayed_work(&tp->schedule, 0);
2294 }
hayeswangac718b62013-05-02 16:01:25 +00002295}
2296
2297static void _rtl8152_set_rx_mode(struct net_device *netdev)
2298{
2299 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002300 u32 mc_filter[2]; /* Multicast hash filter */
2301 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002302 u32 ocp_data;
2303
hayeswangac718b62013-05-02 16:01:25 +00002304 netif_stop_queue(netdev);
2305 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2306 ocp_data &= ~RCR_ACPT_ALL;
2307 ocp_data |= RCR_AB | RCR_APM;
2308
2309 if (netdev->flags & IFF_PROMISC) {
2310 /* Unconditionally log net taps. */
2311 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2312 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002313 mc_filter[1] = 0xffffffff;
2314 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002315 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2316 (netdev->flags & IFF_ALLMULTI)) {
2317 /* Too many to filter perfectly -- accept all multicasts. */
2318 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002319 mc_filter[1] = 0xffffffff;
2320 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002321 } else {
2322 struct netdev_hw_addr *ha;
2323
hayeswangb209af92014-08-25 15:53:00 +08002324 mc_filter[1] = 0;
2325 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002326 netdev_for_each_mc_addr(ha, netdev) {
2327 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002328
hayeswangac718b62013-05-02 16:01:25 +00002329 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2330 ocp_data |= RCR_AM;
2331 }
2332 }
2333
hayeswang31787f52013-07-31 17:21:25 +08002334 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2335 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002336
hayeswang31787f52013-07-31 17:21:25 +08002337 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002338 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2339 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002340}
2341
hayeswanga5e31252015-01-06 17:41:58 +08002342static netdev_features_t
2343rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2344 netdev_features_t features)
2345{
2346 u32 mss = skb_shinfo(skb)->gso_size;
2347 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2348 int offset = skb_transport_offset(skb);
2349
2350 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002351 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002352 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2353 features &= ~NETIF_F_GSO_MASK;
2354
2355 return features;
2356}
2357
hayeswangac718b62013-05-02 16:01:25 +00002358static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002359 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002360{
2361 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002362
hayeswangac718b62013-05-02 16:01:25 +00002363 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002364
hayeswang61598782013-11-20 17:30:55 +08002365 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002366
hayeswang0c3121f2014-03-07 11:04:36 +08002367 if (!list_empty(&tp->tx_free)) {
2368 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08002369 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002370 schedule_delayed_work(&tp->schedule, 0);
2371 } else {
2372 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08002373 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002374 }
hayeswangb209af92014-08-25 15:53:00 +08002375 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002376 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002377 }
hayeswangdd1b1192013-11-20 17:30:56 +08002378
hayeswangac718b62013-05-02 16:01:25 +00002379 return NETDEV_TX_OK;
2380}
2381
2382static void r8152b_reset_packet_filter(struct r8152 *tp)
2383{
2384 u32 ocp_data;
2385
2386 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2387 ocp_data &= ~FMC_FCR_MCU_EN;
2388 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2389 ocp_data |= FMC_FCR_MCU_EN;
2390 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2391}
2392
2393static void rtl8152_nic_reset(struct r8152 *tp)
2394{
2395 int i;
2396
2397 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2398
2399 for (i = 0; i < 1000; i++) {
2400 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2401 break;
hayeswangb209af92014-08-25 15:53:00 +08002402 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002403 }
2404}
2405
hayeswangdd1b1192013-11-20 17:30:56 +08002406static void set_tx_qlen(struct r8152 *tp)
2407{
2408 struct net_device *netdev = tp->netdev;
2409
hayeswangb65c0c92017-06-21 11:25:18 +08002410 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002411 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002412}
2413
hayeswangac718b62013-05-02 16:01:25 +00002414static inline u8 rtl8152_get_speed(struct r8152 *tp)
2415{
2416 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2417}
2418
hayeswang507605a2014-01-02 11:22:43 +08002419static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002420{
hayeswangebc2ec482013-08-14 20:54:38 +08002421 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002422 u8 speed;
2423
2424 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002425 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002426 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002427 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002428 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2429 } else {
2430 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002431 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002432 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2433 }
hayeswang507605a2014-01-02 11:22:43 +08002434}
2435
hayeswang00a5e362014-02-18 21:48:59 +08002436static void rxdy_gated_en(struct r8152 *tp, bool enable)
2437{
2438 u32 ocp_data;
2439
2440 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2441 if (enable)
2442 ocp_data |= RXDY_GATED_EN;
2443 else
2444 ocp_data &= ~RXDY_GATED_EN;
2445 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2446}
2447
hayeswang445f7f42014-09-23 16:31:47 +08002448static int rtl_start_rx(struct r8152 *tp)
2449{
Hayes Wang252df8b2019-08-13 11:42:06 +08002450 struct rx_agg *agg, *agg_next;
2451 struct list_head tmp_list;
2452 unsigned long flags;
Hayes Wang47922fc2019-08-13 11:42:08 +08002453 int ret = 0, i = 0;
Hayes Wang252df8b2019-08-13 11:42:06 +08002454
2455 INIT_LIST_HEAD(&tmp_list);
2456
2457 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002458
2459 INIT_LIST_HEAD(&tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002460 INIT_LIST_HEAD(&tp->rx_used);
Hayes Wang252df8b2019-08-13 11:42:06 +08002461
2462 list_splice_init(&tp->rx_info, &tmp_list);
2463
2464 spin_unlock_irqrestore(&tp->rx_lock, flags);
2465
2466 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2467 INIT_LIST_HEAD(&agg->list);
2468
Hayes Wang47922fc2019-08-13 11:42:08 +08002469 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2470 if (++i > RTL8152_MAX_RX) {
2471 spin_lock_irqsave(&tp->rx_lock, flags);
2472 list_add_tail(&agg->list, &tp->rx_used);
2473 spin_unlock_irqrestore(&tp->rx_lock, flags);
2474 } else if (unlikely(ret < 0)) {
2475 spin_lock_irqsave(&tp->rx_lock, flags);
Hayes Wang252df8b2019-08-13 11:42:06 +08002476 list_add_tail(&agg->list, &tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002477 spin_unlock_irqrestore(&tp->rx_lock, flags);
2478 } else {
Hayes Wang252df8b2019-08-13 11:42:06 +08002479 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
Hayes Wang47922fc2019-08-13 11:42:08 +08002480 }
hayeswang445f7f42014-09-23 16:31:47 +08002481 }
2482
Hayes Wang252df8b2019-08-13 11:42:06 +08002483 spin_lock_irqsave(&tp->rx_lock, flags);
2484 WARN_ON(!list_empty(&tp->rx_info));
2485 list_splice(&tmp_list, &tp->rx_info);
2486 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang7bcf4f62014-11-20 10:29:06 +08002487
hayeswang445f7f42014-09-23 16:31:47 +08002488 return ret;
2489}
2490
2491static int rtl_stop_rx(struct r8152 *tp)
2492{
Hayes Wang252df8b2019-08-13 11:42:06 +08002493 struct rx_agg *agg, *agg_next;
2494 struct list_head tmp_list;
2495 unsigned long flags;
hayeswang445f7f42014-09-23 16:31:47 +08002496
Hayes Wang252df8b2019-08-13 11:42:06 +08002497 INIT_LIST_HEAD(&tmp_list);
2498
2499 /* The usb_kill_urb() couldn't be used in atomic.
2500 * Therefore, move the list of rx_info to a tmp one.
2501 * Then, list_for_each_entry_safe could be used without
2502 * spin lock.
2503 */
2504
2505 spin_lock_irqsave(&tp->rx_lock, flags);
2506 list_splice_init(&tp->rx_info, &tmp_list);
2507 spin_unlock_irqrestore(&tp->rx_lock, flags);
2508
Hayes Wang47922fc2019-08-13 11:42:08 +08002509 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2510 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2511 * equal to 1, so the other ones could be freed safely.
2512 */
2513 if (page_count(agg->page) > 1)
2514 free_rx_agg(tp, agg);
2515 else
2516 usb_kill_urb(agg->urb);
2517 }
Hayes Wang252df8b2019-08-13 11:42:06 +08002518
2519 /* Move back the list of temp to the rx_info */
2520 spin_lock_irqsave(&tp->rx_lock, flags);
2521 WARN_ON(!list_empty(&tp->rx_info));
2522 list_splice(&tmp_list, &tp->rx_info);
2523 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002524
hayeswangd823ab62015-01-12 12:06:23 +08002525 while (!skb_queue_empty(&tp->rx_queue))
2526 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2527
hayeswang445f7f42014-09-23 16:31:47 +08002528 return 0;
2529}
2530
Hayes Wang9fae5412019-07-03 15:11:56 +08002531static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2532{
2533 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2534 OWN_UPDATE | OWN_CLEAR);
2535}
2536
hayeswang507605a2014-01-02 11:22:43 +08002537static int rtl_enable(struct r8152 *tp)
2538{
2539 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002540
2541 r8152b_reset_packet_filter(tp);
2542
2543 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2544 ocp_data |= CR_RE | CR_TE;
2545 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2546
Hayes Wang9fae5412019-07-03 15:11:56 +08002547 switch (tp->version) {
2548 case RTL_VER_08:
2549 case RTL_VER_09:
2550 r8153b_rx_agg_chg_indicate(tp);
2551 break;
2552 default:
2553 break;
2554 }
2555
hayeswang00a5e362014-02-18 21:48:59 +08002556 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002557
hayeswangaa2e0922015-01-09 10:26:35 +08002558 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002559}
2560
hayeswang507605a2014-01-02 11:22:43 +08002561static int rtl8152_enable(struct r8152 *tp)
2562{
hayeswang68714382014-04-11 17:54:31 +08002563 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2564 return -ENODEV;
2565
hayeswang507605a2014-01-02 11:22:43 +08002566 set_tx_qlen(tp);
2567 rtl_set_eee_plus(tp);
2568
2569 return rtl_enable(tp);
2570}
2571
hayeswang464ec102015-02-12 14:33:46 +08002572static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002573{
hayeswang464ec102015-02-12 14:33:46 +08002574 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002575
hayeswang65b82d62017-06-15 14:44:03 +08002576 switch (tp->version) {
2577 case RTL_VER_03:
2578 case RTL_VER_04:
2579 case RTL_VER_05:
2580 case RTL_VER_06:
2581 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2582 ocp_data);
2583 break;
2584
2585 case RTL_VER_08:
2586 case RTL_VER_09:
2587 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2588 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2589 */
2590 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2591 128 / 8);
2592 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2593 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002594 break;
2595
2596 default:
2597 break;
2598 }
hayeswang464ec102015-02-12 14:33:46 +08002599}
2600
2601static void r8153_set_rx_early_size(struct r8152 *tp)
2602{
Hayes Wangec5791c2019-08-13 11:42:05 +08002603 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002604
hayeswang65b82d62017-06-15 14:44:03 +08002605 switch (tp->version) {
2606 case RTL_VER_03:
2607 case RTL_VER_04:
2608 case RTL_VER_05:
2609 case RTL_VER_06:
2610 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2611 ocp_data / 4);
2612 break;
2613 case RTL_VER_08:
2614 case RTL_VER_09:
2615 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2616 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002617 break;
2618 default:
2619 WARN_ON_ONCE(1);
2620 break;
2621 }
hayeswang43779f82014-01-02 11:25:10 +08002622}
2623
2624static int rtl8153_enable(struct r8152 *tp)
2625{
hayeswang68714382014-04-11 17:54:31 +08002626 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2627 return -ENODEV;
2628
hayeswang43779f82014-01-02 11:25:10 +08002629 set_tx_qlen(tp);
2630 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002631 r8153_set_rx_early_timeout(tp);
2632 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002633
2634 return rtl_enable(tp);
2635}
2636
hayeswangd70b1132014-09-19 15:17:18 +08002637static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002638{
hayeswangebc2ec482013-08-14 20:54:38 +08002639 u32 ocp_data;
2640 int i;
hayeswangac718b62013-05-02 16:01:25 +00002641
hayeswang68714382014-04-11 17:54:31 +08002642 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2643 rtl_drop_queued_tx(tp);
2644 return;
2645 }
2646
hayeswangac718b62013-05-02 16:01:25 +00002647 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2648 ocp_data &= ~RCR_ACPT_ALL;
2649 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2650
hayeswang00a5e362014-02-18 21:48:59 +08002651 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002652
2653 for (i = 0; i < RTL8152_MAX_TX; i++)
2654 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002655
hayeswang00a5e362014-02-18 21:48:59 +08002656 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002657
2658 for (i = 0; i < 1000; i++) {
2659 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2660 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2661 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002662 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002663 }
2664
2665 for (i = 0; i < 1000; i++) {
2666 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2667 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002668 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002669 }
2670
hayeswang445f7f42014-09-23 16:31:47 +08002671 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002672
2673 rtl8152_nic_reset(tp);
2674}
2675
hayeswang00a5e362014-02-18 21:48:59 +08002676static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2677{
2678 u32 ocp_data;
2679
2680 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2681 if (enable)
2682 ocp_data |= POWER_CUT;
2683 else
2684 ocp_data &= ~POWER_CUT;
2685 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2686
2687 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2688 ocp_data &= ~RESUME_INDICATE;
2689 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002690}
2691
hayeswangc5554292014-09-12 10:43:11 +08002692static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2693{
2694 u32 ocp_data;
2695
2696 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2697 if (enable)
2698 ocp_data |= CPCR_RX_VLAN;
2699 else
2700 ocp_data &= ~CPCR_RX_VLAN;
2701 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2702}
2703
2704static int rtl8152_set_features(struct net_device *dev,
2705 netdev_features_t features)
2706{
2707 netdev_features_t changed = features ^ dev->features;
2708 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002709 int ret;
2710
2711 ret = usb_autopm_get_interface(tp->intf);
2712 if (ret < 0)
2713 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002714
hayeswangb5403272014-10-09 18:00:26 +08002715 mutex_lock(&tp->control);
2716
hayeswangc5554292014-09-12 10:43:11 +08002717 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2718 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2719 rtl_rx_vlan_en(tp, true);
2720 else
2721 rtl_rx_vlan_en(tp, false);
2722 }
2723
hayeswangb5403272014-10-09 18:00:26 +08002724 mutex_unlock(&tp->control);
2725
hayeswang405f8a02014-10-09 18:00:24 +08002726 usb_autopm_put_interface(tp->intf);
2727
2728out:
2729 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002730}
2731
hayeswang21ff2e82014-02-18 21:49:06 +08002732#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2733
2734static u32 __rtl_get_wol(struct r8152 *tp)
2735{
2736 u32 ocp_data;
2737 u32 wolopts = 0;
2738
hayeswang21ff2e82014-02-18 21:49:06 +08002739 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2740 if (ocp_data & LINK_ON_WAKE_EN)
2741 wolopts |= WAKE_PHY;
2742
2743 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2744 if (ocp_data & UWF_EN)
2745 wolopts |= WAKE_UCAST;
2746 if (ocp_data & BWF_EN)
2747 wolopts |= WAKE_BCAST;
2748 if (ocp_data & MWF_EN)
2749 wolopts |= WAKE_MCAST;
2750
2751 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2752 if (ocp_data & MAGIC_EN)
2753 wolopts |= WAKE_MAGIC;
2754
2755 return wolopts;
2756}
2757
2758static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2759{
2760 u32 ocp_data;
2761
2762 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2763
2764 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2765 ocp_data &= ~LINK_ON_WAKE_EN;
2766 if (wolopts & WAKE_PHY)
2767 ocp_data |= LINK_ON_WAKE_EN;
2768 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2769
2770 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002771 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002772 if (wolopts & WAKE_UCAST)
2773 ocp_data |= UWF_EN;
2774 if (wolopts & WAKE_BCAST)
2775 ocp_data |= BWF_EN;
2776 if (wolopts & WAKE_MCAST)
2777 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002778 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2779
2780 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2781
2782 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2783 ocp_data &= ~MAGIC_EN;
2784 if (wolopts & WAKE_MAGIC)
2785 ocp_data |= MAGIC_EN;
2786 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2787
2788 if (wolopts & WAKE_ANY)
2789 device_set_wakeup_enable(&tp->udev->dev, true);
2790 else
2791 device_set_wakeup_enable(&tp->udev->dev, false);
2792}
2793
hayeswang134f98b2017-06-09 17:11:40 +08002794static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2795{
2796 /* MAC clock speed down */
2797 if (enable) {
2798 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2799 ALDPS_SPDWN_RATIO);
2800 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2801 EEE_SPDWN_RATIO);
2802 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2803 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2804 U1U2_SPDWN_EN | L1_SPDWN_EN);
2805 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2806 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2807 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2808 TP1000_SPDWN_EN);
2809 } else {
2810 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2811 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2812 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2813 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2814 }
2815}
2816
hayeswangb2143962015-07-24 13:54:23 +08002817static void r8153_u1u2en(struct r8152 *tp, bool enable)
2818{
2819 u8 u1u2[8];
2820
2821 if (enable)
2822 memset(u1u2, 0xff, sizeof(u1u2));
2823 else
2824 memset(u1u2, 0x00, sizeof(u1u2));
2825
2826 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2827}
2828
hayeswang65b82d62017-06-15 14:44:03 +08002829static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2830{
2831 u32 ocp_data;
2832
2833 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2834 if (enable)
2835 ocp_data |= LPM_U1U2_EN;
2836 else
2837 ocp_data &= ~LPM_U1U2_EN;
2838
2839 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2840}
2841
hayeswangb2143962015-07-24 13:54:23 +08002842static void r8153_u2p3en(struct r8152 *tp, bool enable)
2843{
2844 u32 ocp_data;
2845
2846 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002847 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002848 ocp_data |= U2P3_ENABLE;
2849 else
2850 ocp_data &= ~U2P3_ENABLE;
2851 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2852}
2853
hayeswang65b82d62017-06-15 14:44:03 +08002854static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2855{
2856 u32 ocp_data;
2857
2858 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2859 ocp_data &= ~clear;
2860 ocp_data |= set;
2861 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2862}
2863
2864static void r8153b_green_en(struct r8152 *tp, bool enable)
2865{
2866 u16 data;
2867
2868 if (enable) {
2869 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2870 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2871 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2872 } else {
2873 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2874 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2875 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2876 }
2877
2878 data = sram_read(tp, SRAM_GREEN_CFG);
2879 data |= GREEN_ETH_EN;
2880 sram_write(tp, SRAM_GREEN_CFG, data);
2881
2882 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2883}
2884
hayeswangc564b872017-06-09 17:11:38 +08002885static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2886{
2887 u16 data;
2888 int i;
2889
2890 for (i = 0; i < 500; i++) {
2891 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2892 data &= PHY_STAT_MASK;
2893 if (desired) {
2894 if (data == desired)
2895 break;
2896 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2897 data == PHY_STAT_EXT_INIT) {
2898 break;
2899 }
2900
2901 msleep(20);
2902 }
2903
2904 return data;
2905}
2906
hayeswang65b82d62017-06-15 14:44:03 +08002907static void r8153b_ups_en(struct r8152 *tp, bool enable)
2908{
2909 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2910
2911 if (enable) {
2912 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2913 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2914
2915 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2916 ocp_data |= BIT(0);
2917 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2918 } else {
2919 u16 data;
2920
2921 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2922 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2923
2924 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2925 ocp_data &= ~BIT(0);
2926 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2927
2928 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2929 ocp_data &= ~PCUT_STATUS;
2930 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2931
2932 data = r8153_phy_status(tp, 0);
2933
2934 switch (data) {
2935 case PHY_STAT_PWRDN:
2936 case PHY_STAT_EXT_INIT:
2937 r8153b_green_en(tp,
2938 test_bit(GREEN_ETHERNET, &tp->flags));
2939
2940 data = r8152_mdio_read(tp, MII_BMCR);
2941 data &= ~BMCR_PDOWN;
2942 data |= BMCR_RESET;
2943 r8152_mdio_write(tp, MII_BMCR, data);
2944
2945 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002946 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002947
2948 default:
2949 if (data != PHY_STAT_LAN_ON)
2950 netif_warn(tp, link, tp->netdev,
2951 "PHY not ready");
2952 break;
2953 }
2954 }
2955}
2956
hayeswangb2143962015-07-24 13:54:23 +08002957static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2958{
2959 u32 ocp_data;
2960
2961 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2962 if (enable)
2963 ocp_data |= PWR_EN | PHASE2_EN;
2964 else
2965 ocp_data &= ~(PWR_EN | PHASE2_EN);
2966 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2967
2968 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2969 ocp_data &= ~PCUT_STATUS;
2970 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2971}
2972
hayeswang65b82d62017-06-15 14:44:03 +08002973static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2974{
2975 u32 ocp_data;
2976
2977 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2978 if (enable)
2979 ocp_data |= PWR_EN | PHASE2_EN;
2980 else
2981 ocp_data &= ~PWR_EN;
2982 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2983
2984 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2985 ocp_data &= ~PCUT_STATUS;
2986 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2987}
2988
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002989static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08002990{
2991 u32 ocp_data;
2992
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002993 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08002994 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002995 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08002996 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002997 ocp_data &= ~UPCOMING_RUNTIME_D3;
2998 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002999
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003000 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3001 ocp_data &= ~LINK_CHG_EVENT;
3002 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3003
3004 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3005 ocp_data &= ~LINK_CHANGE_FLAG;
3006 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003007}
3008
hayeswang7daed8d2015-07-24 13:54:24 +08003009static bool rtl_can_wakeup(struct r8152 *tp)
3010{
3011 struct usb_device *udev = tp->udev;
3012
3013 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3014}
3015
hayeswang9a4be1b2014-02-18 21:49:07 +08003016static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3017{
3018 if (enable) {
3019 u32 ocp_data;
3020
3021 __rtl_set_wol(tp, WAKE_ANY);
3022
3023 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3024
3025 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3026 ocp_data |= LINK_OFF_WAKE_EN;
3027 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3028
3029 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3030 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08003031 u32 ocp_data;
3032
hayeswang9a4be1b2014-02-18 21:49:07 +08003033 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08003034
3035 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3036
3037 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3038 ocp_data &= ~LINK_OFF_WAKE_EN;
3039 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3040
3041 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08003042 }
3043}
hayeswangf95ae8a2016-06-30 15:33:35 +08003044
hayeswang2609af12016-07-05 16:11:46 +08003045static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3046{
hayeswang2609af12016-07-05 16:11:46 +08003047 if (enable) {
3048 r8153_u1u2en(tp, false);
3049 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003050 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08003051 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08003052 } else {
hayeswang02552752017-06-09 17:11:42 +08003053 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003054 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003055
3056 switch (tp->version) {
3057 case RTL_VER_03:
3058 case RTL_VER_04:
3059 break;
3060 case RTL_VER_05:
3061 case RTL_VER_06:
3062 default:
3063 r8153_u2p3en(tp, true);
3064 break;
3065 }
3066
hayeswangb2143962015-07-24 13:54:23 +08003067 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08003068 }
3069}
3070
hayeswang65b82d62017-06-15 14:44:03 +08003071static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3072{
3073 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003074 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003075 r8153b_u1u2en(tp, false);
3076 r8153_u2p3en(tp, false);
3077 rtl_runtime_suspend_enable(tp, true);
3078 r8153b_ups_en(tp, true);
3079 } else {
3080 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003081 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003082 rtl_runtime_suspend_enable(tp, false);
3083 r8153_u2p3en(tp, true);
3084 r8153b_u1u2en(tp, true);
3085 }
3086}
3087
hayeswang43499682014-02-18 21:48:58 +08003088static void r8153_teredo_off(struct r8152 *tp)
3089{
3090 u32 ocp_data;
3091
hayeswang65b82d62017-06-15 14:44:03 +08003092 switch (tp->version) {
3093 case RTL_VER_01:
3094 case RTL_VER_02:
3095 case RTL_VER_03:
3096 case RTL_VER_04:
3097 case RTL_VER_05:
3098 case RTL_VER_06:
3099 case RTL_VER_07:
3100 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3101 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3102 OOB_TEREDO_EN);
3103 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3104 break;
3105
3106 case RTL_VER_08:
3107 case RTL_VER_09:
3108 /* The bit 0 ~ 7 are relative with teredo settings. They are
3109 * W1C (write 1 to clear), so set all 1 to disable it.
3110 */
3111 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3112 break;
3113
3114 default:
3115 break;
3116 }
hayeswang43499682014-02-18 21:48:58 +08003117
3118 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3119 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3120 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3121}
3122
hayeswang93fe9b12016-06-16 10:55:18 +08003123static void rtl_reset_bmu(struct r8152 *tp)
3124{
3125 u32 ocp_data;
3126
3127 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3128 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3129 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3130 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3131 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3132}
3133
hayeswangcda9fb02016-01-07 17:51:12 +08003134static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08003135{
hayeswangcda9fb02016-01-07 17:51:12 +08003136 if (enable) {
3137 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
3138 LINKENA | DIS_SDSAVE);
3139 } else {
3140 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
3141 DIS_SDSAVE);
3142 msleep(20);
3143 }
hayeswang43499682014-02-18 21:48:58 +08003144}
3145
hayeswange6449532016-09-20 16:22:05 +08003146static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
3147{
3148 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3149 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3150 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3151}
3152
3153static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3154{
3155 u16 data;
3156
3157 r8152_mmd_indirect(tp, dev, reg);
3158 data = ocp_reg_read(tp, OCP_EEE_DATA);
3159 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3160
3161 return data;
3162}
3163
3164static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3165{
3166 r8152_mmd_indirect(tp, dev, reg);
3167 ocp_reg_write(tp, OCP_EEE_DATA, data);
3168 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3169}
3170
3171static void r8152_eee_en(struct r8152 *tp, bool enable)
3172{
3173 u16 config1, config2, config3;
3174 u32 ocp_data;
3175
3176 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3177 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3178 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3179 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3180
3181 if (enable) {
3182 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3183 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3184 config1 |= sd_rise_time(1);
3185 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3186 config3 |= fast_snr(42);
3187 } else {
3188 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3189 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3190 RX_QUIET_EN);
3191 config1 |= sd_rise_time(7);
3192 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3193 config3 |= fast_snr(511);
3194 }
3195
3196 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3197 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3198 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3199 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3200}
3201
3202static void r8152b_enable_eee(struct r8152 *tp)
3203{
3204 r8152_eee_en(tp, true);
3205 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3206}
3207
3208static void r8152b_enable_fc(struct r8152 *tp)
3209{
3210 u16 anar;
3211
3212 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3213 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3214 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3215}
3216
hayeswangd70b1132014-09-19 15:17:18 +08003217static void rtl8152_disable(struct r8152 *tp)
3218{
hayeswangcda9fb02016-01-07 17:51:12 +08003219 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003220 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003221 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003222}
3223
hayeswang43499682014-02-18 21:48:58 +08003224static void r8152b_hw_phy_cfg(struct r8152 *tp)
3225{
hayeswangef39df82016-09-20 16:22:07 +08003226 r8152b_enable_eee(tp);
3227 r8152_aldps_en(tp, true);
3228 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003229
hayeswangaa66a5f2014-02-18 21:49:04 +08003230 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003231}
3232
hayeswangac718b62013-05-02 16:01:25 +00003233static void r8152b_exit_oob(struct r8152 *tp)
3234{
hayeswangdb8515e2014-03-06 15:07:16 +08003235 u32 ocp_data;
3236 int i;
hayeswangac718b62013-05-02 16:01:25 +00003237
3238 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3239 ocp_data &= ~RCR_ACPT_ALL;
3240 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3241
hayeswang00a5e362014-02-18 21:48:59 +08003242 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003243 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003244 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3245 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3246
3247 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3248 ocp_data &= ~NOW_IS_OOB;
3249 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3250
3251 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3252 ocp_data &= ~MCU_BORW_EN;
3253 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3254
3255 for (i = 0; i < 1000; i++) {
3256 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3257 if (ocp_data & LINK_LIST_READY)
3258 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003259 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003260 }
3261
3262 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3263 ocp_data |= RE_INIT_LL;
3264 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3265
3266 for (i = 0; i < 1000; i++) {
3267 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3268 if (ocp_data & LINK_LIST_READY)
3269 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003270 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003271 }
3272
3273 rtl8152_nic_reset(tp);
3274
3275 /* rx share fifo credit full threshold */
3276 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3277
hayeswanga3cc4652014-07-24 16:37:43 +08003278 if (tp->udev->speed == USB_SPEED_FULL ||
3279 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003280 /* rx share fifo credit near full threshold */
3281 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3282 RXFIFO_THR2_FULL);
3283 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3284 RXFIFO_THR3_FULL);
3285 } else {
3286 /* rx share fifo credit near full threshold */
3287 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3288 RXFIFO_THR2_HIGH);
3289 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3290 RXFIFO_THR3_HIGH);
3291 }
3292
3293 /* TX share fifo free credit full threshold */
3294 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3295
3296 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003297 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003298 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3299 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3300
hayeswangc5554292014-09-12 10:43:11 +08003301 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003302
3303 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3304
3305 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3306 ocp_data |= TCR0_AUTO_FIFO;
3307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3308}
3309
3310static void r8152b_enter_oob(struct r8152 *tp)
3311{
hayeswang45f4a192014-01-06 17:08:41 +08003312 u32 ocp_data;
3313 int i;
hayeswangac718b62013-05-02 16:01:25 +00003314
3315 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3316 ocp_data &= ~NOW_IS_OOB;
3317 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3318
3319 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3320 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3321 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3322
hayeswangd70b1132014-09-19 15:17:18 +08003323 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003324
3325 for (i = 0; i < 1000; i++) {
3326 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3327 if (ocp_data & LINK_LIST_READY)
3328 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003329 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003330 }
3331
3332 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3333 ocp_data |= RE_INIT_LL;
3334 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3335
3336 for (i = 0; i < 1000; i++) {
3337 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3338 if (ocp_data & LINK_LIST_READY)
3339 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003340 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003341 }
3342
3343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3344
hayeswangc5554292014-09-12 10:43:11 +08003345 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003346
Kevin Lo59c0b472019-08-01 11:29:38 +08003347 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00003348 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003349 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00003350
3351 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3352 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3353 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3354
hayeswang00a5e362014-02-18 21:48:59 +08003355 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003356
3357 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3358 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3359 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3360}
3361
hayeswang65b82d62017-06-15 14:44:03 +08003362static int r8153_patch_request(struct r8152 *tp, bool request)
3363{
3364 u16 data;
3365 int i;
3366
3367 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3368 if (request)
3369 data |= PATCH_REQUEST;
3370 else
3371 data &= ~PATCH_REQUEST;
3372 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3373
3374 for (i = 0; request && i < 5000; i++) {
3375 usleep_range(1000, 2000);
3376 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3377 break;
3378 }
3379
3380 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3381 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3382 r8153_patch_request(tp, false);
3383 return -ETIME;
3384 } else {
3385 return 0;
3386 }
3387}
3388
hayeswange6449532016-09-20 16:22:05 +08003389static void r8153_aldps_en(struct r8152 *tp, bool enable)
3390{
3391 u16 data;
3392
3393 data = ocp_reg_read(tp, OCP_POWER_CFG);
3394 if (enable) {
3395 data |= EN_ALDPS;
3396 ocp_reg_write(tp, OCP_POWER_CFG, data);
3397 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003398 int i;
3399
hayeswange6449532016-09-20 16:22:05 +08003400 data &= ~EN_ALDPS;
3401 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003402 for (i = 0; i < 20; i++) {
3403 usleep_range(1000, 2000);
3404 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3405 break;
3406 }
hayeswange6449532016-09-20 16:22:05 +08003407 }
3408}
3409
hayeswang65b82d62017-06-15 14:44:03 +08003410static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3411{
3412 r8153_aldps_en(tp, enable);
3413
3414 if (enable)
3415 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3416 else
3417 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3418}
3419
hayeswange6449532016-09-20 16:22:05 +08003420static void r8153_eee_en(struct r8152 *tp, bool enable)
3421{
3422 u32 ocp_data;
3423 u16 config;
3424
3425 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3426 config = ocp_reg_read(tp, OCP_EEE_CFG);
3427
3428 if (enable) {
3429 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3430 config |= EEE10_EN;
3431 } else {
3432 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3433 config &= ~EEE10_EN;
3434 }
3435
3436 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3437 ocp_reg_write(tp, OCP_EEE_CFG, config);
3438}
3439
hayeswang65b82d62017-06-15 14:44:03 +08003440static void r8153b_eee_en(struct r8152 *tp, bool enable)
3441{
3442 r8153_eee_en(tp, enable);
3443
3444 if (enable)
3445 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3446 else
3447 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3448}
3449
3450static void r8153b_enable_fc(struct r8152 *tp)
3451{
3452 r8152b_enable_fc(tp);
3453 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3454}
3455
hayeswang43779f82014-01-02 11:25:10 +08003456static void r8153_hw_phy_cfg(struct r8152 *tp)
3457{
3458 u32 ocp_data;
3459 u16 data;
3460
hayeswangd768c612016-09-20 16:22:09 +08003461 /* disable ALDPS before updating the PHY parameters */
3462 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003463
hayeswangd768c612016-09-20 16:22:09 +08003464 /* disable EEE before updating the PHY parameters */
3465 r8153_eee_en(tp, false);
3466 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang43779f82014-01-02 11:25:10 +08003467
3468 if (tp->version == RTL_VER_03) {
3469 data = ocp_reg_read(tp, OCP_EEE_CFG);
3470 data &= ~CTAP_SHORT_EN;
3471 ocp_reg_write(tp, OCP_EEE_CFG, data);
3472 }
3473
3474 data = ocp_reg_read(tp, OCP_POWER_CFG);
3475 data |= EEE_CLKDIV_EN;
3476 ocp_reg_write(tp, OCP_POWER_CFG, data);
3477
3478 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3479 data |= EN_10M_BGOFF;
3480 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3481 data = ocp_reg_read(tp, OCP_POWER_CFG);
3482 data |= EN_10M_PLLOFF;
3483 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003484 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003485
3486 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3487 ocp_data |= PFM_PWM_SWITCH;
3488 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3489
hayeswangb4d99de2015-01-19 17:02:46 +08003490 /* Enable LPF corner auto tune */
3491 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003492
hayeswangb4d99de2015-01-19 17:02:46 +08003493 /* Adjust 10M Amplitude */
3494 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3495 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003496
hayeswangaf0287e2016-09-20 16:22:08 +08003497 r8153_eee_en(tp, true);
3498 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3499
hayeswangef39df82016-09-20 16:22:07 +08003500 r8153_aldps_en(tp, true);
3501 r8152b_enable_fc(tp);
3502
hayeswang3cb32342017-06-09 17:11:43 +08003503 switch (tp->version) {
3504 case RTL_VER_03:
3505 case RTL_VER_04:
3506 break;
3507 case RTL_VER_05:
3508 case RTL_VER_06:
3509 default:
3510 r8153_u2p3en(tp, true);
3511 break;
3512 }
3513
hayeswangaa66a5f2014-02-18 21:49:04 +08003514 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003515}
3516
hayeswang65b82d62017-06-15 14:44:03 +08003517static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3518{
3519 u32 ocp_data;
3520
3521 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3522 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3523 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3524 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3525
3526 return ocp_data;
3527}
3528
3529static void r8153b_hw_phy_cfg(struct r8152 *tp)
3530{
3531 u32 ocp_data, ups_flags = 0;
3532 u16 data;
3533
3534 /* disable ALDPS before updating the PHY parameters */
3535 r8153b_aldps_en(tp, false);
3536
3537 /* disable EEE before updating the PHY parameters */
3538 r8153b_eee_en(tp, false);
3539 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3540
3541 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3542
3543 data = sram_read(tp, SRAM_GREEN_CFG);
3544 data |= R_TUNE_EN;
3545 sram_write(tp, SRAM_GREEN_CFG, data);
3546 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3547 data |= PGA_RETURN_EN;
3548 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3549
3550 /* ADC Bias Calibration:
3551 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3552 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3553 * ADC ioffset.
3554 */
3555 ocp_data = r8152_efuse_read(tp, 0x7d);
3556 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3557 if (data != 0xffff)
3558 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3559
3560 /* ups mode tx-link-pulse timing adjustment:
3561 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3562 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3563 */
3564 ocp_data = ocp_reg_read(tp, 0xc426);
3565 ocp_data &= 0x3fff;
3566 if (ocp_data) {
3567 u32 swr_cnt_1ms_ini;
3568
3569 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3570 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3571 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3572 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3573 }
3574
3575 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3576 ocp_data |= PFM_PWM_SWITCH;
3577 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3578
3579 /* Advnace EEE */
3580 if (!r8153_patch_request(tp, true)) {
3581 data = ocp_reg_read(tp, OCP_POWER_CFG);
3582 data |= EEE_CLKDIV_EN;
3583 ocp_reg_write(tp, OCP_POWER_CFG, data);
3584
3585 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3586 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3587 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3588
3589 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3590 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3591
3592 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3593 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3594 UPS_FLAGS_EEE_PLLOFF_GIGA;
3595
3596 r8153_patch_request(tp, false);
3597 }
3598
3599 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3600
3601 r8153b_eee_en(tp, true);
3602 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3603
3604 r8153b_aldps_en(tp, true);
3605 r8153b_enable_fc(tp);
3606 r8153_u2p3en(tp, true);
3607
3608 set_bit(PHY_RESET, &tp->flags);
3609}
3610
hayeswang43779f82014-01-02 11:25:10 +08003611static void r8153_first_init(struct r8152 *tp)
3612{
3613 u32 ocp_data;
3614 int i;
3615
hayeswang134f98b2017-06-09 17:11:40 +08003616 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003617 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003618 r8153_teredo_off(tp);
3619
3620 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3621 ocp_data &= ~RCR_ACPT_ALL;
3622 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3623
hayeswang43779f82014-01-02 11:25:10 +08003624 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003625 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003626
3627 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3628 ocp_data &= ~NOW_IS_OOB;
3629 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3630
3631 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3632 ocp_data &= ~MCU_BORW_EN;
3633 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3634
3635 for (i = 0; i < 1000; i++) {
3636 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3637 if (ocp_data & LINK_LIST_READY)
3638 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003639 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003640 }
3641
3642 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3643 ocp_data |= RE_INIT_LL;
3644 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3645
3646 for (i = 0; i < 1000; i++) {
3647 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3648 if (ocp_data & LINK_LIST_READY)
3649 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003650 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003651 }
3652
hayeswangc5554292014-09-12 10:43:11 +08003653 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003654
hayeswangb65c0c92017-06-21 11:25:18 +08003655 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003656 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003657 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003658
3659 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3660 ocp_data |= TCR0_AUTO_FIFO;
3661 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3662
3663 rtl8152_nic_reset(tp);
3664
3665 /* rx share fifo credit full threshold */
3666 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3667 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3668 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3669 /* TX share fifo free credit full threshold */
3670 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003671}
3672
3673static void r8153_enter_oob(struct r8152 *tp)
3674{
3675 u32 ocp_data;
3676 int i;
3677
hayeswang134f98b2017-06-09 17:11:40 +08003678 r8153_mac_clk_spd(tp, true);
3679
hayeswang43779f82014-01-02 11:25:10 +08003680 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3681 ocp_data &= ~NOW_IS_OOB;
3682 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3683
hayeswangd70b1132014-09-19 15:17:18 +08003684 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003685 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003686
3687 for (i = 0; i < 1000; i++) {
3688 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3689 if (ocp_data & LINK_LIST_READY)
3690 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003691 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003692 }
3693
3694 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3695 ocp_data |= RE_INIT_LL;
3696 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3697
3698 for (i = 0; i < 1000; i++) {
3699 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3700 if (ocp_data & LINK_LIST_READY)
3701 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003702 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003703 }
3704
hayeswangb65c0c92017-06-21 11:25:18 +08003705 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003706 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003707
hayeswang65b82d62017-06-15 14:44:03 +08003708 switch (tp->version) {
3709 case RTL_VER_03:
3710 case RTL_VER_04:
3711 case RTL_VER_05:
3712 case RTL_VER_06:
3713 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3714 ocp_data &= ~TEREDO_WAKE_MASK;
3715 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3716 break;
3717
3718 case RTL_VER_08:
3719 case RTL_VER_09:
3720 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3721 * type. Set it to zero. bits[7:0] are the W1C bits about
3722 * the events. Set them to all 1 to clear them.
3723 */
3724 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3725 break;
3726
3727 default:
3728 break;
3729 }
hayeswang43779f82014-01-02 11:25:10 +08003730
hayeswangc5554292014-09-12 10:43:11 +08003731 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003732
Kevin Lo59c0b472019-08-01 11:29:38 +08003733 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08003734 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003735 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003736
3737 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3738 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3739 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3740
hayeswang00a5e362014-02-18 21:48:59 +08003741 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003742
3743 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3744 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3745 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3746}
3747
hayeswangd70b1132014-09-19 15:17:18 +08003748static void rtl8153_disable(struct r8152 *tp)
3749{
hayeswangcda9fb02016-01-07 17:51:12 +08003750 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003751 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003752 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003753 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003754}
3755
hayeswang65b82d62017-06-15 14:44:03 +08003756static void rtl8153b_disable(struct r8152 *tp)
3757{
3758 r8153b_aldps_en(tp, false);
3759 rtl_disable(tp);
3760 rtl_reset_bmu(tp);
3761 r8153b_aldps_en(tp, true);
3762}
3763
hayeswangac718b62013-05-02 16:01:25 +00003764static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3765{
hayeswang43779f82014-01-02 11:25:10 +08003766 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003767 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003768 int ret = 0;
3769
hayeswangac718b62013-05-02 16:01:25 +00003770 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3771 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3772 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003773 if (tp->mii.supports_gmii) {
3774 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3775 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3776 } else {
3777 gbcr = 0;
3778 }
hayeswangac718b62013-05-02 16:01:25 +00003779
3780 if (autoneg == AUTONEG_DISABLE) {
3781 if (speed == SPEED_10) {
3782 bmcr = 0;
3783 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003784 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003785 } else if (speed == SPEED_100) {
3786 bmcr = BMCR_SPEED100;
3787 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003788 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003789 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3790 bmcr = BMCR_SPEED1000;
3791 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003792 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003793 } else {
3794 ret = -EINVAL;
3795 goto out;
3796 }
3797
hayeswang65b82d62017-06-15 14:44:03 +08003798 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003799 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003800 if (speed != SPEED_1000)
3801 speed_duplex++;
3802 }
hayeswangac718b62013-05-02 16:01:25 +00003803 } else {
3804 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003805 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003806 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003807 speed_duplex = NWAY_10M_FULL;
3808 } else {
hayeswangac718b62013-05-02 16:01:25 +00003809 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003810 speed_duplex = NWAY_10M_HALF;
3811 }
hayeswangac718b62013-05-02 16:01:25 +00003812 } else if (speed == SPEED_100) {
3813 if (duplex == DUPLEX_FULL) {
3814 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3815 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003816 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003817 } else {
3818 anar |= ADVERTISE_10HALF;
3819 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003820 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003821 }
hayeswang43779f82014-01-02 11:25:10 +08003822 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3823 if (duplex == DUPLEX_FULL) {
3824 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3825 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3826 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3827 } else {
3828 anar |= ADVERTISE_10HALF;
3829 anar |= ADVERTISE_100HALF;
3830 gbcr |= ADVERTISE_1000HALF;
3831 }
hayeswang65b82d62017-06-15 14:44:03 +08003832 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003833 } else {
3834 ret = -EINVAL;
3835 goto out;
3836 }
3837
3838 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3839 }
3840
hayeswangfae56172016-06-16 14:08:29 +08003841 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003842 bmcr |= BMCR_RESET;
3843
hayeswang43779f82014-01-02 11:25:10 +08003844 if (tp->mii.supports_gmii)
3845 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3846
hayeswangac718b62013-05-02 16:01:25 +00003847 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3848 r8152_mdio_write(tp, MII_BMCR, bmcr);
3849
hayeswang65b82d62017-06-15 14:44:03 +08003850 switch (tp->version) {
3851 case RTL_VER_08:
3852 case RTL_VER_09:
3853 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3854 UPS_FLAGS_SPEED_MASK);
3855 break;
3856
3857 default:
3858 break;
3859 }
3860
hayeswangfae56172016-06-16 14:08:29 +08003861 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003862 int i;
3863
hayeswangaa66a5f2014-02-18 21:49:04 +08003864 for (i = 0; i < 50; i++) {
3865 msleep(20);
3866 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3867 break;
3868 }
3869 }
3870
hayeswangac718b62013-05-02 16:01:25 +00003871out:
hayeswangac718b62013-05-02 16:01:25 +00003872 return ret;
3873}
3874
hayeswangd70b1132014-09-19 15:17:18 +08003875static void rtl8152_up(struct r8152 *tp)
3876{
3877 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3878 return;
3879
hayeswangcda9fb02016-01-07 17:51:12 +08003880 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003881 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003882 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003883}
3884
hayeswangac718b62013-05-02 16:01:25 +00003885static void rtl8152_down(struct r8152 *tp)
3886{
hayeswang68714382014-04-11 17:54:31 +08003887 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3888 rtl_drop_queued_tx(tp);
3889 return;
3890 }
3891
hayeswang00a5e362014-02-18 21:48:59 +08003892 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003893 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003894 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003895 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003896}
3897
hayeswangd70b1132014-09-19 15:17:18 +08003898static void rtl8153_up(struct r8152 *tp)
3899{
3900 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3901 return;
3902
hayeswangb2143962015-07-24 13:54:23 +08003903 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003904 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003905 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003906 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003907 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003908
3909 switch (tp->version) {
3910 case RTL_VER_03:
3911 case RTL_VER_04:
3912 break;
3913 case RTL_VER_05:
3914 case RTL_VER_06:
3915 default:
3916 r8153_u2p3en(tp, true);
3917 break;
3918 }
3919
hayeswangb2143962015-07-24 13:54:23 +08003920 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003921}
3922
hayeswang43779f82014-01-02 11:25:10 +08003923static void rtl8153_down(struct r8152 *tp)
3924{
hayeswang68714382014-04-11 17:54:31 +08003925 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3926 rtl_drop_queued_tx(tp);
3927 return;
3928 }
3929
hayeswangb9702722014-02-18 21:49:00 +08003930 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003931 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003932 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003933 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003934 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003935 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003936}
3937
hayeswang65b82d62017-06-15 14:44:03 +08003938static void rtl8153b_up(struct r8152 *tp)
3939{
3940 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3941 return;
3942
3943 r8153b_u1u2en(tp, false);
3944 r8153_u2p3en(tp, false);
3945 r8153b_aldps_en(tp, false);
3946
3947 r8153_first_init(tp);
3948 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3949
3950 r8153b_aldps_en(tp, true);
3951 r8153_u2p3en(tp, true);
3952 r8153b_u1u2en(tp, true);
3953}
3954
3955static void rtl8153b_down(struct r8152 *tp)
3956{
3957 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3958 rtl_drop_queued_tx(tp);
3959 return;
3960 }
3961
3962 r8153b_u1u2en(tp, false);
3963 r8153_u2p3en(tp, false);
3964 r8153b_power_cut_en(tp, false);
3965 r8153b_aldps_en(tp, false);
3966 r8153_enter_oob(tp);
3967 r8153b_aldps_en(tp, true);
3968}
3969
hayeswang2dd49e02015-09-07 11:57:44 +08003970static bool rtl8152_in_nway(struct r8152 *tp)
3971{
3972 u16 nway_state;
3973
3974 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3975 tp->ocp_base = 0x2000;
3976 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3977 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3978
3979 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3980 if (nway_state & 0xc000)
3981 return false;
3982 else
3983 return true;
3984}
3985
3986static bool rtl8153_in_nway(struct r8152 *tp)
3987{
3988 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
3989
3990 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
3991 return false;
3992 else
3993 return true;
3994}
3995
hayeswangac718b62013-05-02 16:01:25 +00003996static void set_carrier(struct r8152 *tp)
3997{
3998 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08003999 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00004000 u8 speed;
4001
4002 speed = rtl8152_get_speed(tp);
4003
4004 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08004005 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08004006 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08004007 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08004008 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004009 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08004010 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004011 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
4012 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08004013 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004014 netif_wake_queue(netdev);
4015 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08004016 } else if (netif_queue_stopped(netdev) &&
4017 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
4018 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004019 }
4020 } else {
hayeswang51d979f2015-02-06 11:30:47 +08004021 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00004022 netif_carrier_off(netdev);
hayeswangce594e92017-03-16 14:32:22 +08004023 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08004024 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08004025 napi_enable(napi);
hayeswangde9bf292017-01-26 09:38:32 +08004026 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00004027 }
4028 }
hayeswangac718b62013-05-02 16:01:25 +00004029}
4030
4031static void rtl_work_func_t(struct work_struct *work)
4032{
4033 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
4034
hayeswanga1f83fe2014-11-12 10:05:05 +08004035 /* If the device is unplugged or !netif_running(), the workqueue
4036 * doesn't need to wake the device, and could return directly.
4037 */
4038 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
4039 return;
4040
hayeswang9a4be1b2014-02-18 21:49:07 +08004041 if (usb_autopm_get_interface(tp->intf) < 0)
4042 return;
4043
hayeswangac718b62013-05-02 16:01:25 +00004044 if (!test_bit(WORK_ENABLE, &tp->flags))
4045 goto out1;
4046
hayeswangb5403272014-10-09 18:00:26 +08004047 if (!mutex_trylock(&tp->control)) {
4048 schedule_delayed_work(&tp->schedule, 0);
4049 goto out1;
4050 }
4051
hayeswang216a8342016-01-07 17:51:11 +08004052 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08004053 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00004054
hayeswang216a8342016-01-07 17:51:11 +08004055 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00004056 _rtl8152_set_rx_mode(tp->netdev);
4057
hayeswangd823ab62015-01-12 12:06:23 +08004058 /* don't schedule napi before linking */
hayeswang216a8342016-01-07 17:51:11 +08004059 if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
4060 netif_carrier_ok(tp->netdev))
hayeswangd823ab62015-01-12 12:06:23 +08004061 napi_schedule(&tp->napi);
hayeswangaa66a5f2014-02-18 21:49:04 +08004062
hayeswangb5403272014-10-09 18:00:26 +08004063 mutex_unlock(&tp->control);
4064
hayeswangac718b62013-05-02 16:01:25 +00004065out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08004066 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00004067}
4068
hayeswanga028a9e2016-06-13 17:49:36 +08004069static void rtl_hw_phy_work_func_t(struct work_struct *work)
4070{
4071 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
4072
4073 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4074 return;
4075
4076 if (usb_autopm_get_interface(tp->intf) < 0)
4077 return;
4078
4079 mutex_lock(&tp->control);
4080
4081 tp->rtl_ops.hw_phy_cfg(tp);
4082
hayeswangaa7e26b2016-06-13 17:49:38 +08004083 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08004084
hayeswanga028a9e2016-06-13 17:49:36 +08004085 mutex_unlock(&tp->control);
4086
4087 usb_autopm_put_interface(tp->intf);
4088}
4089
hayeswang5ee3c602016-01-07 17:12:17 +08004090#ifdef CONFIG_PM_SLEEP
4091static int rtl_notifier(struct notifier_block *nb, unsigned long action,
4092 void *data)
4093{
4094 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
4095
4096 switch (action) {
4097 case PM_HIBERNATION_PREPARE:
4098 case PM_SUSPEND_PREPARE:
4099 usb_autopm_get_interface(tp->intf);
4100 break;
4101
4102 case PM_POST_HIBERNATION:
4103 case PM_POST_SUSPEND:
4104 usb_autopm_put_interface(tp->intf);
4105 break;
4106
4107 case PM_POST_RESTORE:
4108 case PM_RESTORE_PREPARE:
4109 default:
4110 break;
4111 }
4112
4113 return NOTIFY_DONE;
4114}
4115#endif
4116
hayeswangac718b62013-05-02 16:01:25 +00004117static int rtl8152_open(struct net_device *netdev)
4118{
4119 struct r8152 *tp = netdev_priv(netdev);
4120 int res = 0;
4121
hayeswang7e9da482014-02-18 21:49:05 +08004122 res = alloc_all_mem(tp);
4123 if (res)
4124 goto out;
4125
hayeswang9a4be1b2014-02-18 21:49:07 +08004126 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004127 if (res < 0)
4128 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08004129
hayeswangb5403272014-10-09 18:00:26 +08004130 mutex_lock(&tp->control);
4131
hayeswang7e9da482014-02-18 21:49:05 +08004132 tp->rtl_ops.up(tp);
4133
hayeswang40a82912013-08-14 20:54:40 +08004134 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004135 netif_start_queue(netdev);
4136 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08004137
hayeswang3d55f442014-02-06 11:55:48 +08004138 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4139 if (res) {
4140 if (res == -ENODEV)
4141 netif_device_detach(tp->netdev);
4142 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
4143 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004144 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08004145 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08004146 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08004147
hayeswangb5403272014-10-09 18:00:26 +08004148 mutex_unlock(&tp->control);
4149
hayeswang9a4be1b2014-02-18 21:49:07 +08004150 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08004151#ifdef CONFIG_PM_SLEEP
4152 tp->pm_notifier.notifier_call = rtl_notifier;
4153 register_pm_notifier(&tp->pm_notifier);
4154#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08004155 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004156
Guenter Roeckca0a7532016-11-09 19:51:25 -08004157out_unlock:
4158 mutex_unlock(&tp->control);
4159 usb_autopm_put_interface(tp->intf);
4160out_free:
4161 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08004162out:
hayeswangac718b62013-05-02 16:01:25 +00004163 return res;
4164}
4165
4166static int rtl8152_close(struct net_device *netdev)
4167{
4168 struct r8152 *tp = netdev_priv(netdev);
4169 int res = 0;
4170
hayeswang5ee3c602016-01-07 17:12:17 +08004171#ifdef CONFIG_PM_SLEEP
4172 unregister_pm_notifier(&tp->pm_notifier);
4173#endif
Jiri Slaby0ee1f472018-06-25 09:26:27 +02004174 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
4175 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004176 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08004177 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00004178 cancel_delayed_work_sync(&tp->schedule);
4179 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004180
4181 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004182 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004183 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004184 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004185 } else {
hayeswangb5403272014-10-09 18:00:26 +08004186 mutex_lock(&tp->control);
4187
hayeswang9a4be1b2014-02-18 21:49:07 +08004188 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004189
4190 mutex_unlock(&tp->control);
4191
hayeswang9a4be1b2014-02-18 21:49:07 +08004192 usb_autopm_put_interface(tp->intf);
4193 }
hayeswangac718b62013-05-02 16:01:25 +00004194
hayeswang7e9da482014-02-18 21:49:05 +08004195 free_all_mem(tp);
4196
hayeswangac718b62013-05-02 16:01:25 +00004197 return res;
4198}
4199
hayeswang4f1d4d52014-03-11 16:24:19 +08004200static void rtl_tally_reset(struct r8152 *tp)
4201{
4202 u32 ocp_data;
4203
4204 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4205 ocp_data |= TALLY_RESET;
4206 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4207}
4208
hayeswangac718b62013-05-02 16:01:25 +00004209static void r8152b_init(struct r8152 *tp)
4210{
hayeswangebc2ec482013-08-14 20:54:38 +08004211 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004212 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004213
hayeswang68714382014-04-11 17:54:31 +08004214 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4215 return;
4216
hayeswang2dd436d2016-09-20 16:22:06 +08004217 data = r8152_mdio_read(tp, MII_BMCR);
4218 if (data & BMCR_PDOWN) {
4219 data &= ~BMCR_PDOWN;
4220 r8152_mdio_write(tp, MII_BMCR, data);
4221 }
4222
hayeswangcda9fb02016-01-07 17:51:12 +08004223 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004224
hayeswangac718b62013-05-02 16:01:25 +00004225 if (tp->version == RTL_VER_01) {
4226 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4227 ocp_data &= ~LED_MODE_MASK;
4228 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4229 }
4230
hayeswang00a5e362014-02-18 21:48:59 +08004231 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004232
hayeswangac718b62013-05-02 16:01:25 +00004233 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4234 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4235 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4236 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4237 ocp_data &= ~MCU_CLK_RATIO_MASK;
4238 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4239 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4240 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4241 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4242 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4243
hayeswang4f1d4d52014-03-11 16:24:19 +08004244 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004245
hayeswangebc2ec482013-08-14 20:54:38 +08004246 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004247 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004248 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004249 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4250}
4251
hayeswang43779f82014-01-02 11:25:10 +08004252static void r8153_init(struct r8152 *tp)
4253{
4254 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004255 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004256 int i;
4257
hayeswang68714382014-04-11 17:54:31 +08004258 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4259 return;
4260
hayeswangb9702722014-02-18 21:49:00 +08004261 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004262
4263 for (i = 0; i < 500; i++) {
4264 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4265 AUTOLOAD_DONE)
4266 break;
4267 msleep(20);
4268 }
4269
hayeswangc564b872017-06-09 17:11:38 +08004270 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004271
hayeswang2dd436d2016-09-20 16:22:06 +08004272 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4273 tp->version == RTL_VER_05)
4274 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4275
4276 data = r8152_mdio_read(tp, MII_BMCR);
4277 if (data & BMCR_PDOWN) {
4278 data &= ~BMCR_PDOWN;
4279 r8152_mdio_write(tp, MII_BMCR, data);
4280 }
4281
hayeswangc564b872017-06-09 17:11:38 +08004282 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004283
hayeswangb9702722014-02-18 21:49:00 +08004284 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004285
hayeswang65bab842015-02-12 16:20:46 +08004286 if (tp->version == RTL_VER_04) {
4287 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4288 ocp_data &= ~pwd_dn_scale_mask;
4289 ocp_data |= pwd_dn_scale(96);
4290 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4291
4292 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4293 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4294 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4295 } else if (tp->version == RTL_VER_05) {
4296 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4297 ocp_data &= ~ECM_ALDPS;
4298 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4299
4300 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4301 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4302 ocp_data &= ~DYNAMIC_BURST;
4303 else
4304 ocp_data |= DYNAMIC_BURST;
4305 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004306 } else if (tp->version == RTL_VER_06) {
4307 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4308 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4309 ocp_data &= ~DYNAMIC_BURST;
4310 else
4311 ocp_data |= DYNAMIC_BURST;
4312 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004313 }
4314
4315 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4316 ocp_data |= EP4_FULL_FC;
4317 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4318
hayeswang43779f82014-01-02 11:25:10 +08004319 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4320 ocp_data &= ~TIMER11_EN;
4321 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4322
hayeswang43779f82014-01-02 11:25:10 +08004323 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4324 ocp_data &= ~LED_MODE_MASK;
4325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4326
hayeswang65bab842015-02-12 16:20:46 +08004327 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004328 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004329 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004330 else
4331 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004332 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4333
4334 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4335 ocp_data &= ~SEN_VAL_MASK;
4336 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4337 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4338
hayeswang65bab842015-02-12 16:20:46 +08004339 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4340
hayeswangb9702722014-02-18 21:49:00 +08004341 r8153_power_cut_en(tp, false);
4342 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004343 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004344 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004345
hayeswange31f6362017-06-09 17:11:41 +08004346 /* rx aggregation */
4347 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4348 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004349 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4350 ocp_data |= RX_AGG_DISABLE;
4351
hayeswange31f6362017-06-09 17:11:41 +08004352 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004353
hayeswang4f1d4d52014-03-11 16:24:19 +08004354 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004355
4356 switch (tp->udev->speed) {
4357 case USB_SPEED_SUPER:
4358 case USB_SPEED_SUPER_PLUS:
4359 tp->coalesce = COALESCE_SUPER;
4360 break;
4361 case USB_SPEED_HIGH:
4362 tp->coalesce = COALESCE_HIGH;
4363 break;
4364 default:
4365 tp->coalesce = COALESCE_SLOW;
4366 break;
4367 }
hayeswang43779f82014-01-02 11:25:10 +08004368}
4369
hayeswang65b82d62017-06-15 14:44:03 +08004370static void r8153b_init(struct r8152 *tp)
4371{
4372 u32 ocp_data;
4373 u16 data;
4374 int i;
4375
4376 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4377 return;
4378
4379 r8153b_u1u2en(tp, false);
4380
4381 for (i = 0; i < 500; i++) {
4382 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4383 AUTOLOAD_DONE)
4384 break;
4385 msleep(20);
4386 }
4387
4388 data = r8153_phy_status(tp, 0);
4389
4390 data = r8152_mdio_read(tp, MII_BMCR);
4391 if (data & BMCR_PDOWN) {
4392 data &= ~BMCR_PDOWN;
4393 r8152_mdio_write(tp, MII_BMCR, data);
4394 }
4395
4396 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4397
4398 r8153_u2p3en(tp, false);
4399
4400 /* MSC timer = 0xfff * 8ms = 32760 ms */
4401 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4402
4403 /* U1/U2/L1 idle timer. 500 us */
4404 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4405
4406 r8153b_power_cut_en(tp, false);
4407 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08004408 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004409 rtl_runtime_suspend_enable(tp, false);
4410 r8153b_u1u2en(tp, true);
4411 usb_enable_lpm(tp->udev);
4412
4413 /* MAC clock speed down */
4414 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4415 ocp_data |= MAC_CLK_SPDWN_EN;
4416 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4417
4418 set_bit(GREEN_ETHERNET, &tp->flags);
4419
4420 /* rx aggregation */
4421 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4422 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4423 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4424
4425 rtl_tally_reset(tp);
4426
4427 tp->coalesce = 15000; /* 15 us */
4428}
4429
hayeswange5011392015-07-29 20:39:08 +08004430static int rtl8152_pre_reset(struct usb_interface *intf)
4431{
4432 struct r8152 *tp = usb_get_intfdata(intf);
4433 struct net_device *netdev;
4434
4435 if (!tp)
4436 return 0;
4437
4438 netdev = tp->netdev;
4439 if (!netif_running(netdev))
4440 return 0;
4441
hayeswangde9bf292017-01-26 09:38:32 +08004442 netif_stop_queue(netdev);
hayeswange5011392015-07-29 20:39:08 +08004443 napi_disable(&tp->napi);
4444 clear_bit(WORK_ENABLE, &tp->flags);
4445 usb_kill_urb(tp->intr_urb);
4446 cancel_delayed_work_sync(&tp->schedule);
4447 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004448 mutex_lock(&tp->control);
4449 tp->rtl_ops.disable(tp);
4450 mutex_unlock(&tp->control);
4451 }
4452
4453 return 0;
4454}
4455
4456static int rtl8152_post_reset(struct usb_interface *intf)
4457{
4458 struct r8152 *tp = usb_get_intfdata(intf);
4459 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004460 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004461
4462 if (!tp)
4463 return 0;
4464
Mario Limonciello25766272019-04-04 13:46:53 -05004465 /* reset the MAC adddress in case of policy change */
4466 if (determine_ethernet_addr(tp, &sa) >= 0) {
4467 rtnl_lock();
4468 dev_set_mac_address (tp->netdev, &sa, NULL);
4469 rtnl_unlock();
4470 }
4471
hayeswange5011392015-07-29 20:39:08 +08004472 netdev = tp->netdev;
4473 if (!netif_running(netdev))
4474 return 0;
4475
4476 set_bit(WORK_ENABLE, &tp->flags);
4477 if (netif_carrier_ok(netdev)) {
4478 mutex_lock(&tp->control);
4479 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004480 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004481 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004482 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004483 }
4484
4485 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004486 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004487 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004488
hayeswang7489bda2017-01-26 09:38:34 +08004489 if (!list_empty(&tp->rx_done))
4490 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004491
4492 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004493}
4494
hayeswang2dd49e02015-09-07 11:57:44 +08004495static bool delay_autosuspend(struct r8152 *tp)
4496{
4497 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4498 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4499
4500 /* This means a linking change occurs and the driver doesn't detect it,
4501 * yet. If the driver has disabled tx/rx and hw is linking on, the
4502 * device wouldn't wake up by receiving any packet.
4503 */
4504 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4505 return true;
4506
4507 /* If the linking down is occurred by nway, the device may miss the
4508 * linking change event. And it wouldn't wake when linking on.
4509 */
4510 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4511 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004512 else if (!skb_queue_empty(&tp->tx_queue))
4513 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004514 else
4515 return false;
4516}
4517
hayeswang21cbd0e2017-06-13 15:14:39 +08004518static int rtl8152_runtime_resume(struct r8152 *tp)
4519{
4520 struct net_device *netdev = tp->netdev;
4521
4522 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4523 struct napi_struct *napi = &tp->napi;
4524
4525 tp->rtl_ops.autosuspend_en(tp, false);
4526 napi_disable(napi);
4527 set_bit(WORK_ENABLE, &tp->flags);
4528
4529 if (netif_carrier_ok(netdev)) {
4530 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4531 rtl_start_rx(tp);
4532 } else {
4533 netif_carrier_off(netdev);
4534 tp->rtl_ops.disable(tp);
4535 netif_info(tp, link, netdev, "linking down\n");
4536 }
4537 }
4538
4539 napi_enable(napi);
4540 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4541 smp_mb__after_atomic();
4542
4543 if (!list_empty(&tp->rx_done))
4544 napi_schedule(&tp->napi);
4545
4546 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4547 } else {
4548 if (netdev->flags & IFF_UP)
4549 tp->rtl_ops.autosuspend_en(tp, false);
4550
4551 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4552 }
4553
4554 return 0;
4555}
4556
4557static int rtl8152_system_resume(struct r8152 *tp)
4558{
4559 struct net_device *netdev = tp->netdev;
4560
4561 netif_device_attach(netdev);
4562
4563 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4564 tp->rtl_ops.up(tp);
4565 netif_carrier_off(netdev);
4566 set_bit(WORK_ENABLE, &tp->flags);
4567 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4568 }
4569
4570 return 0;
4571}
4572
hayeswanga9c54ad2017-01-25 13:41:45 +08004573static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004574{
hayeswang6cc69f22014-10-17 16:55:08 +08004575 struct net_device *netdev = tp->netdev;
4576 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004577
hayeswang26afec32017-01-26 09:38:31 +08004578 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4579 smp_mb__after_atomic();
4580
hayeswang8fb28062017-01-10 17:04:06 +08004581 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004582 u32 rcr = 0;
4583
hayeswang75dc6922017-01-10 17:04:07 +08004584 if (netif_carrier_ok(netdev)) {
4585 u32 ocp_data;
4586
4587 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4588 ocp_data = rcr & ~RCR_ACPT_ALL;
4589 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4590 rxdy_gated_en(tp, true);
4591 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4592 PLA_OOB_CTRL);
4593 if (!(ocp_data & RXFIFO_EMPTY)) {
4594 rxdy_gated_en(tp, false);
4595 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004596 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4597 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004598 ret = -EBUSY;
4599 goto out1;
4600 }
4601 }
4602
hayeswang8fb28062017-01-10 17:04:06 +08004603 clear_bit(WORK_ENABLE, &tp->flags);
4604 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004605
hayeswang8fb28062017-01-10 17:04:06 +08004606 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004607
4608 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004609 struct napi_struct *napi = &tp->napi;
4610
4611 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004612 rtl_stop_rx(tp);
4613 rxdy_gated_en(tp, false);
4614 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004615 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004616 }
hayeswangbd882982017-06-13 15:14:40 +08004617
4618 if (delay_autosuspend(tp)) {
4619 rtl8152_runtime_resume(tp);
4620 ret = -EBUSY;
4621 }
hayeswang6cc69f22014-10-17 16:55:08 +08004622 }
4623
hayeswang8fb28062017-01-10 17:04:06 +08004624out1:
4625 return ret;
4626}
4627
4628static int rtl8152_system_suspend(struct r8152 *tp)
4629{
4630 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004631
4632 netif_device_detach(netdev);
4633
hayeswange3bd1a82014-10-29 11:12:17 +08004634 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004635 struct napi_struct *napi = &tp->napi;
4636
hayeswangac718b62013-05-02 16:01:25 +00004637 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004638 usb_kill_urb(tp->intr_urb);
hayeswangce594e92017-03-16 14:32:22 +08004639 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004640 cancel_delayed_work_sync(&tp->schedule);
4641 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004642 napi_enable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004643 }
hayeswang8fb28062017-01-10 17:04:06 +08004644
zhong jiangf7419172018-08-09 09:39:13 +08004645 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004646}
4647
4648static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4649{
4650 struct r8152 *tp = usb_get_intfdata(intf);
4651 int ret;
4652
4653 mutex_lock(&tp->control);
4654
4655 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004656 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004657 else
4658 ret = rtl8152_system_suspend(tp);
4659
hayeswangb5403272014-10-09 18:00:26 +08004660 mutex_unlock(&tp->control);
4661
hayeswang6cc69f22014-10-17 16:55:08 +08004662 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004663}
4664
4665static int rtl8152_resume(struct usb_interface *intf)
4666{
4667 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004668 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004669
hayeswangb5403272014-10-09 18:00:26 +08004670 mutex_lock(&tp->control);
4671
hayeswang21cbd0e2017-06-13 15:14:39 +08004672 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4673 ret = rtl8152_runtime_resume(tp);
4674 else
4675 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004676
hayeswangb5403272014-10-09 18:00:26 +08004677 mutex_unlock(&tp->control);
4678
hayeswang21cbd0e2017-06-13 15:14:39 +08004679 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004680}
4681
hayeswang7ec25412016-01-04 14:38:46 +08004682static int rtl8152_reset_resume(struct usb_interface *intf)
4683{
4684 struct r8152 *tp = usb_get_intfdata(intf);
4685
4686 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004687 mutex_lock(&tp->control);
4688 tp->rtl_ops.init(tp);
4689 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4690 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004691 return rtl8152_resume(intf);
4692}
4693
hayeswang21ff2e82014-02-18 21:49:06 +08004694static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4695{
4696 struct r8152 *tp = netdev_priv(dev);
4697
hayeswang9a4be1b2014-02-18 21:49:07 +08004698 if (usb_autopm_get_interface(tp->intf) < 0)
4699 return;
4700
hayeswang7daed8d2015-07-24 13:54:24 +08004701 if (!rtl_can_wakeup(tp)) {
4702 wol->supported = 0;
4703 wol->wolopts = 0;
4704 } else {
4705 mutex_lock(&tp->control);
4706 wol->supported = WAKE_ANY;
4707 wol->wolopts = __rtl_get_wol(tp);
4708 mutex_unlock(&tp->control);
4709 }
hayeswangb5403272014-10-09 18:00:26 +08004710
hayeswang9a4be1b2014-02-18 21:49:07 +08004711 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004712}
4713
4714static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4715{
4716 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004717 int ret;
4718
hayeswang7daed8d2015-07-24 13:54:24 +08004719 if (!rtl_can_wakeup(tp))
4720 return -EOPNOTSUPP;
4721
Florian Fainellif2750df2018-09-28 16:18:54 -07004722 if (wol->wolopts & ~WAKE_ANY)
4723 return -EINVAL;
4724
hayeswang9a4be1b2014-02-18 21:49:07 +08004725 ret = usb_autopm_get_interface(tp->intf);
4726 if (ret < 0)
4727 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004728
hayeswangb5403272014-10-09 18:00:26 +08004729 mutex_lock(&tp->control);
4730
hayeswang21ff2e82014-02-18 21:49:06 +08004731 __rtl_set_wol(tp, wol->wolopts);
4732 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4733
hayeswangb5403272014-10-09 18:00:26 +08004734 mutex_unlock(&tp->control);
4735
hayeswang9a4be1b2014-02-18 21:49:07 +08004736 usb_autopm_put_interface(tp->intf);
4737
4738out_set_wol:
4739 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004740}
4741
hayeswanga5ec27c2014-02-18 21:49:11 +08004742static u32 rtl8152_get_msglevel(struct net_device *dev)
4743{
4744 struct r8152 *tp = netdev_priv(dev);
4745
4746 return tp->msg_enable;
4747}
4748
4749static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4750{
4751 struct r8152 *tp = netdev_priv(dev);
4752
4753 tp->msg_enable = value;
4754}
4755
hayeswangac718b62013-05-02 16:01:25 +00004756static void rtl8152_get_drvinfo(struct net_device *netdev,
4757 struct ethtool_drvinfo *info)
4758{
4759 struct r8152 *tp = netdev_priv(netdev);
4760
hayeswangb0b46c72014-08-26 10:08:23 +08004761 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4762 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004763 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4764}
4765
4766static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004767int rtl8152_get_link_ksettings(struct net_device *netdev,
4768 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004769{
4770 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004771 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004772
4773 if (!tp->mii.mdio_read)
4774 return -EOPNOTSUPP;
4775
hayeswang8d4a4d72014-10-09 18:00:25 +08004776 ret = usb_autopm_get_interface(tp->intf);
4777 if (ret < 0)
4778 goto out;
4779
hayeswangb5403272014-10-09 18:00:26 +08004780 mutex_lock(&tp->control);
4781
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004782 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004783
hayeswangb5403272014-10-09 18:00:26 +08004784 mutex_unlock(&tp->control);
4785
hayeswang8d4a4d72014-10-09 18:00:25 +08004786 usb_autopm_put_interface(tp->intf);
4787
4788out:
4789 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004790}
4791
Philippe Reynes06144dc2017-03-12 22:41:58 +01004792static int rtl8152_set_link_ksettings(struct net_device *dev,
4793 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004794{
4795 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004796 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004797
hayeswang9a4be1b2014-02-18 21:49:07 +08004798 ret = usb_autopm_get_interface(tp->intf);
4799 if (ret < 0)
4800 goto out;
4801
hayeswangb5403272014-10-09 18:00:26 +08004802 mutex_lock(&tp->control);
4803
Philippe Reynes06144dc2017-03-12 22:41:58 +01004804 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4805 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004806 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004807 tp->autoneg = cmd->base.autoneg;
4808 tp->speed = cmd->base.speed;
4809 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004810 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004811
hayeswangb5403272014-10-09 18:00:26 +08004812 mutex_unlock(&tp->control);
4813
hayeswang9a4be1b2014-02-18 21:49:07 +08004814 usb_autopm_put_interface(tp->intf);
4815
4816out:
4817 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004818}
4819
hayeswang4f1d4d52014-03-11 16:24:19 +08004820static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4821 "tx_packets",
4822 "rx_packets",
4823 "tx_errors",
4824 "rx_errors",
4825 "rx_missed",
4826 "align_errors",
4827 "tx_single_collisions",
4828 "tx_multi_collisions",
4829 "rx_unicast",
4830 "rx_broadcast",
4831 "rx_multicast",
4832 "tx_aborted",
4833 "tx_underrun",
4834};
4835
4836static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4837{
4838 switch (sset) {
4839 case ETH_SS_STATS:
4840 return ARRAY_SIZE(rtl8152_gstrings);
4841 default:
4842 return -EOPNOTSUPP;
4843 }
4844}
4845
4846static void rtl8152_get_ethtool_stats(struct net_device *dev,
4847 struct ethtool_stats *stats, u64 *data)
4848{
4849 struct r8152 *tp = netdev_priv(dev);
4850 struct tally_counter tally;
4851
hayeswang0b030242014-07-08 14:49:28 +08004852 if (usb_autopm_get_interface(tp->intf) < 0)
4853 return;
4854
hayeswang4f1d4d52014-03-11 16:24:19 +08004855 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4856
hayeswang0b030242014-07-08 14:49:28 +08004857 usb_autopm_put_interface(tp->intf);
4858
hayeswang4f1d4d52014-03-11 16:24:19 +08004859 data[0] = le64_to_cpu(tally.tx_packets);
4860 data[1] = le64_to_cpu(tally.rx_packets);
4861 data[2] = le64_to_cpu(tally.tx_errors);
4862 data[3] = le32_to_cpu(tally.rx_errors);
4863 data[4] = le16_to_cpu(tally.rx_missed);
4864 data[5] = le16_to_cpu(tally.align_errors);
4865 data[6] = le32_to_cpu(tally.tx_one_collision);
4866 data[7] = le32_to_cpu(tally.tx_multi_collision);
4867 data[8] = le64_to_cpu(tally.rx_unicast);
4868 data[9] = le64_to_cpu(tally.rx_broadcast);
4869 data[10] = le32_to_cpu(tally.rx_multicast);
4870 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004871 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004872}
4873
4874static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4875{
4876 switch (stringset) {
4877 case ETH_SS_STATS:
4878 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4879 break;
4880 }
4881}
4882
hayeswangdf35d282014-09-25 20:54:02 +08004883static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4884{
4885 u32 ocp_data, lp, adv, supported = 0;
4886 u16 val;
4887
4888 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4889 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4890
4891 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4892 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4893
4894 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4895 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4896
4897 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4898 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4899
4900 eee->eee_enabled = !!ocp_data;
4901 eee->eee_active = !!(supported & adv & lp);
4902 eee->supported = supported;
4903 eee->advertised = adv;
4904 eee->lp_advertised = lp;
4905
4906 return 0;
4907}
4908
4909static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4910{
4911 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4912
4913 r8152_eee_en(tp, eee->eee_enabled);
4914
4915 if (!eee->eee_enabled)
4916 val = 0;
4917
4918 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4919
4920 return 0;
4921}
4922
4923static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4924{
4925 u32 ocp_data, lp, adv, supported = 0;
4926 u16 val;
4927
4928 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4929 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4930
4931 val = ocp_reg_read(tp, OCP_EEE_ADV);
4932 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4933
4934 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4935 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4936
4937 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4938 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4939
4940 eee->eee_enabled = !!ocp_data;
4941 eee->eee_active = !!(supported & adv & lp);
4942 eee->supported = supported;
4943 eee->advertised = adv;
4944 eee->lp_advertised = lp;
4945
4946 return 0;
4947}
4948
4949static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4950{
4951 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4952
4953 r8153_eee_en(tp, eee->eee_enabled);
4954
4955 if (!eee->eee_enabled)
4956 val = 0;
4957
4958 ocp_reg_write(tp, OCP_EEE_ADV, val);
4959
4960 return 0;
4961}
4962
hayeswang65b82d62017-06-15 14:44:03 +08004963static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4964{
4965 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4966
4967 r8153b_eee_en(tp, eee->eee_enabled);
4968
4969 if (!eee->eee_enabled)
4970 val = 0;
4971
4972 ocp_reg_write(tp, OCP_EEE_ADV, val);
4973
4974 return 0;
4975}
4976
hayeswangdf35d282014-09-25 20:54:02 +08004977static int
4978rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4979{
4980 struct r8152 *tp = netdev_priv(net);
4981 int ret;
4982
4983 ret = usb_autopm_get_interface(tp->intf);
4984 if (ret < 0)
4985 goto out;
4986
hayeswangb5403272014-10-09 18:00:26 +08004987 mutex_lock(&tp->control);
4988
hayeswangdf35d282014-09-25 20:54:02 +08004989 ret = tp->rtl_ops.eee_get(tp, edata);
4990
hayeswangb5403272014-10-09 18:00:26 +08004991 mutex_unlock(&tp->control);
4992
hayeswangdf35d282014-09-25 20:54:02 +08004993 usb_autopm_put_interface(tp->intf);
4994
4995out:
4996 return ret;
4997}
4998
4999static int
5000rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
5001{
5002 struct r8152 *tp = netdev_priv(net);
5003 int ret;
5004
5005 ret = usb_autopm_get_interface(tp->intf);
5006 if (ret < 0)
5007 goto out;
5008
hayeswangb5403272014-10-09 18:00:26 +08005009 mutex_lock(&tp->control);
5010
hayeswangdf35d282014-09-25 20:54:02 +08005011 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08005012 if (!ret)
5013 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08005014
hayeswangb5403272014-10-09 18:00:26 +08005015 mutex_unlock(&tp->control);
5016
hayeswangdf35d282014-09-25 20:54:02 +08005017 usb_autopm_put_interface(tp->intf);
5018
5019out:
5020 return ret;
5021}
5022
hayeswang8884f502014-10-28 14:05:52 +08005023static int rtl8152_nway_reset(struct net_device *dev)
5024{
5025 struct r8152 *tp = netdev_priv(dev);
5026 int ret;
5027
5028 ret = usb_autopm_get_interface(tp->intf);
5029 if (ret < 0)
5030 goto out;
5031
5032 mutex_lock(&tp->control);
5033
5034 ret = mii_nway_restart(&tp->mii);
5035
5036 mutex_unlock(&tp->control);
5037
5038 usb_autopm_put_interface(tp->intf);
5039
5040out:
5041 return ret;
5042}
5043
hayeswangefb3dd82015-02-12 14:33:48 +08005044static int rtl8152_get_coalesce(struct net_device *netdev,
5045 struct ethtool_coalesce *coalesce)
5046{
5047 struct r8152 *tp = netdev_priv(netdev);
5048
5049 switch (tp->version) {
5050 case RTL_VER_01:
5051 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005052 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08005053 return -EOPNOTSUPP;
5054 default:
5055 break;
5056 }
5057
5058 coalesce->rx_coalesce_usecs = tp->coalesce;
5059
5060 return 0;
5061}
5062
5063static int rtl8152_set_coalesce(struct net_device *netdev,
5064 struct ethtool_coalesce *coalesce)
5065{
5066 struct r8152 *tp = netdev_priv(netdev);
5067 int ret;
5068
5069 switch (tp->version) {
5070 case RTL_VER_01:
5071 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005072 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08005073 return -EOPNOTSUPP;
5074 default:
5075 break;
5076 }
5077
5078 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
5079 return -EINVAL;
5080
5081 ret = usb_autopm_get_interface(tp->intf);
5082 if (ret < 0)
5083 return ret;
5084
5085 mutex_lock(&tp->control);
5086
5087 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
5088 tp->coalesce = coalesce->rx_coalesce_usecs;
5089
Hayes Wang9fae5412019-07-03 15:11:56 +08005090 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
5091 netif_stop_queue(netdev);
5092 napi_disable(&tp->napi);
5093 tp->rtl_ops.disable(tp);
5094 tp->rtl_ops.enable(tp);
5095 rtl_start_rx(tp);
5096 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5097 _rtl8152_set_rx_mode(netdev);
5098 napi_enable(&tp->napi);
5099 netif_wake_queue(netdev);
5100 }
hayeswangefb3dd82015-02-12 14:33:48 +08005101 }
5102
5103 mutex_unlock(&tp->control);
5104
5105 usb_autopm_put_interface(tp->intf);
5106
5107 return ret;
5108}
5109
Hayes Wange4a50172019-08-13 11:42:09 +08005110static int rtl8152_get_tunable(struct net_device *netdev,
5111 const struct ethtool_tunable *tunable, void *d)
5112{
5113 struct r8152 *tp = netdev_priv(netdev);
5114
5115 switch (tunable->id) {
5116 case ETHTOOL_RX_COPYBREAK:
5117 *(u32 *)d = tp->rx_copybreak;
5118 break;
5119 default:
5120 return -EOPNOTSUPP;
5121 }
5122
5123 return 0;
5124}
5125
5126static int rtl8152_set_tunable(struct net_device *netdev,
5127 const struct ethtool_tunable *tunable,
5128 const void *d)
5129{
5130 struct r8152 *tp = netdev_priv(netdev);
5131 u32 val;
5132
5133 switch (tunable->id) {
5134 case ETHTOOL_RX_COPYBREAK:
5135 val = *(u32 *)d;
5136 if (val < ETH_ZLEN) {
5137 netif_err(tp, rx_err, netdev,
5138 "Invalid rx copy break value\n");
5139 return -EINVAL;
5140 }
5141
5142 if (tp->rx_copybreak != val) {
5143 napi_disable(&tp->napi);
5144 tp->rx_copybreak = val;
5145 napi_enable(&tp->napi);
5146 }
5147 break;
5148 default:
5149 return -EOPNOTSUPP;
5150 }
5151
5152 return 0;
5153}
5154
5155static void rtl8152_get_ringparam(struct net_device *netdev,
5156 struct ethtool_ringparam *ring)
5157{
5158 struct r8152 *tp = netdev_priv(netdev);
5159
5160 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
5161 ring->rx_pending = tp->rx_pending;
5162}
5163
5164static int rtl8152_set_ringparam(struct net_device *netdev,
5165 struct ethtool_ringparam *ring)
5166{
5167 struct r8152 *tp = netdev_priv(netdev);
5168
5169 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
5170 return -EINVAL;
5171
5172 if (tp->rx_pending != ring->rx_pending) {
5173 napi_disable(&tp->napi);
5174 tp->rx_pending = ring->rx_pending;
5175 napi_enable(&tp->napi);
5176 }
5177
5178 return 0;
5179}
5180
Julia Lawall407a4712016-09-01 00:21:22 +02005181static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00005182 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00005183 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08005184 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08005185 .get_msglevel = rtl8152_get_msglevel,
5186 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08005187 .get_wol = rtl8152_get_wol,
5188 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08005189 .get_strings = rtl8152_get_strings,
5190 .get_sset_count = rtl8152_get_sset_count,
5191 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08005192 .get_coalesce = rtl8152_get_coalesce,
5193 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08005194 .get_eee = rtl_ethtool_get_eee,
5195 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01005196 .get_link_ksettings = rtl8152_get_link_ksettings,
5197 .set_link_ksettings = rtl8152_set_link_ksettings,
Hayes Wange4a50172019-08-13 11:42:09 +08005198 .get_tunable = rtl8152_get_tunable,
5199 .set_tunable = rtl8152_set_tunable,
5200 .get_ringparam = rtl8152_get_ringparam,
5201 .set_ringparam = rtl8152_set_ringparam,
hayeswangac718b62013-05-02 16:01:25 +00005202};
5203
5204static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
5205{
5206 struct r8152 *tp = netdev_priv(netdev);
5207 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08005208 int res;
5209
hayeswang68714382014-04-11 17:54:31 +08005210 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5211 return -ENODEV;
5212
hayeswang9a4be1b2014-02-18 21:49:07 +08005213 res = usb_autopm_get_interface(tp->intf);
5214 if (res < 0)
5215 goto out;
hayeswangac718b62013-05-02 16:01:25 +00005216
5217 switch (cmd) {
5218 case SIOCGMIIPHY:
5219 data->phy_id = R8152_PHY_ID; /* Internal PHY */
5220 break;
5221
5222 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08005223 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005224 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08005225 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005226 break;
5227
5228 case SIOCSMIIREG:
5229 if (!capable(CAP_NET_ADMIN)) {
5230 res = -EPERM;
5231 break;
5232 }
hayeswangb5403272014-10-09 18:00:26 +08005233 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005234 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08005235 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005236 break;
5237
5238 default:
5239 res = -EOPNOTSUPP;
5240 }
5241
hayeswang9a4be1b2014-02-18 21:49:07 +08005242 usb_autopm_put_interface(tp->intf);
5243
5244out:
hayeswangac718b62013-05-02 16:01:25 +00005245 return res;
5246}
5247
hayeswang69b4b7a2014-07-10 10:58:54 +08005248static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5249{
5250 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08005251 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005252
5253 switch (tp->version) {
5254 case RTL_VER_01:
5255 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005256 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04005257 dev->mtu = new_mtu;
5258 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08005259 default:
5260 break;
5261 }
5262
hayeswang396e2e22015-02-12 14:33:47 +08005263 ret = usb_autopm_get_interface(tp->intf);
5264 if (ret < 0)
5265 return ret;
5266
5267 mutex_lock(&tp->control);
5268
hayeswang69b4b7a2014-07-10 10:58:54 +08005269 dev->mtu = new_mtu;
5270
hayeswang210c4f72017-03-20 16:13:44 +08005271 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005272 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005273
5274 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5275
5276 if (netif_carrier_ok(dev))
5277 r8153_set_rx_early_size(tp);
5278 }
hayeswang396e2e22015-02-12 14:33:47 +08005279
5280 mutex_unlock(&tp->control);
5281
5282 usb_autopm_put_interface(tp->intf);
5283
5284 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005285}
5286
hayeswangac718b62013-05-02 16:01:25 +00005287static const struct net_device_ops rtl8152_netdev_ops = {
5288 .ndo_open = rtl8152_open,
5289 .ndo_stop = rtl8152_close,
5290 .ndo_do_ioctl = rtl8152_ioctl,
5291 .ndo_start_xmit = rtl8152_start_xmit,
5292 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005293 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005294 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5295 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005296 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005297 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005298 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005299};
5300
hayeswange3fe0b12014-01-02 11:22:39 +08005301static void rtl8152_unload(struct r8152 *tp)
5302{
hayeswang68714382014-04-11 17:54:31 +08005303 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5304 return;
5305
hayeswang00a5e362014-02-18 21:48:59 +08005306 if (tp->version != RTL_VER_01)
5307 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005308}
5309
hayeswang43779f82014-01-02 11:25:10 +08005310static void rtl8153_unload(struct r8152 *tp)
5311{
hayeswang68714382014-04-11 17:54:31 +08005312 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5313 return;
5314
hayeswang49be1722014-10-01 13:25:11 +08005315 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005316}
5317
hayeswang65b82d62017-06-15 14:44:03 +08005318static void rtl8153b_unload(struct r8152 *tp)
5319{
5320 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5321 return;
5322
5323 r8153b_power_cut_en(tp, false);
5324}
5325
hayeswang55b65472014-11-06 12:47:39 +08005326static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005327{
5328 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005329 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005330
hayeswang55b65472014-11-06 12:47:39 +08005331 switch (tp->version) {
5332 case RTL_VER_01:
5333 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005334 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005335 ops->init = r8152b_init;
5336 ops->enable = rtl8152_enable;
5337 ops->disable = rtl8152_disable;
5338 ops->up = rtl8152_up;
5339 ops->down = rtl8152_down;
5340 ops->unload = rtl8152_unload;
5341 ops->eee_get = r8152_get_eee;
5342 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005343 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005344 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005345 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005346 tp->rx_buf_sz = 16 * 1024;
hayeswang43779f82014-01-02 11:25:10 +08005347 break;
5348
hayeswang55b65472014-11-06 12:47:39 +08005349 case RTL_VER_03:
5350 case RTL_VER_04:
5351 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005352 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005353 ops->init = r8153_init;
5354 ops->enable = rtl8153_enable;
5355 ops->disable = rtl8153_disable;
5356 ops->up = rtl8153_up;
5357 ops->down = rtl8153_down;
5358 ops->unload = rtl8153_unload;
5359 ops->eee_get = r8153_get_eee;
5360 ops->eee_set = r8153_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005361 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005362 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005363 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005364 tp->rx_buf_sz = 32 * 1024;
hayeswangc81229c2014-01-02 11:22:42 +08005365 break;
5366
hayeswang65b82d62017-06-15 14:44:03 +08005367 case RTL_VER_08:
5368 case RTL_VER_09:
5369 ops->init = r8153b_init;
5370 ops->enable = rtl8153_enable;
5371 ops->disable = rtl8153b_disable;
5372 ops->up = rtl8153b_up;
5373 ops->down = rtl8153b_down;
5374 ops->unload = rtl8153b_unload;
5375 ops->eee_get = r8153_get_eee;
5376 ops->eee_set = r8153b_set_eee;
5377 ops->in_nway = rtl8153_in_nway;
5378 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5379 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005380 tp->rx_buf_sz = 32 * 1024;
hayeswang65b82d62017-06-15 14:44:03 +08005381 break;
5382
hayeswangc81229c2014-01-02 11:22:42 +08005383 default:
hayeswang55b65472014-11-06 12:47:39 +08005384 ret = -ENODEV;
5385 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005386 break;
5387 }
5388
5389 return ret;
5390}
5391
hayeswang33928ee2017-03-17 11:20:13 +08005392static u8 rtl_get_version(struct usb_interface *intf)
5393{
5394 struct usb_device *udev = interface_to_usbdev(intf);
5395 u32 ocp_data = 0;
5396 __le32 *tmp;
5397 u8 version;
5398 int ret;
5399
5400 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5401 if (!tmp)
5402 return 0;
5403
5404 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5405 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5406 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5407 if (ret > 0)
5408 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5409
5410 kfree(tmp);
5411
5412 switch (ocp_data) {
5413 case 0x4c00:
5414 version = RTL_VER_01;
5415 break;
5416 case 0x4c10:
5417 version = RTL_VER_02;
5418 break;
5419 case 0x5c00:
5420 version = RTL_VER_03;
5421 break;
5422 case 0x5c10:
5423 version = RTL_VER_04;
5424 break;
5425 case 0x5c20:
5426 version = RTL_VER_05;
5427 break;
5428 case 0x5c30:
5429 version = RTL_VER_06;
5430 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005431 case 0x4800:
5432 version = RTL_VER_07;
5433 break;
hayeswang65b82d62017-06-15 14:44:03 +08005434 case 0x6000:
5435 version = RTL_VER_08;
5436 break;
5437 case 0x6010:
5438 version = RTL_VER_09;
5439 break;
hayeswang33928ee2017-03-17 11:20:13 +08005440 default:
5441 version = RTL_VER_UNKNOWN;
5442 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5443 break;
5444 }
5445
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005446 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5447
hayeswang33928ee2017-03-17 11:20:13 +08005448 return version;
5449}
5450
hayeswangac718b62013-05-02 16:01:25 +00005451static int rtl8152_probe(struct usb_interface *intf,
5452 const struct usb_device_id *id)
5453{
5454 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005455 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005456 struct r8152 *tp;
5457 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005458 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005459
hayeswang33928ee2017-03-17 11:20:13 +08005460 if (version == RTL_VER_UNKNOWN)
5461 return -ENODEV;
5462
hayeswang10c32712014-03-04 20:47:48 +08005463 if (udev->actconfig->desc.bConfigurationValue != 1) {
5464 usb_driver_set_configuration(udev, 1);
5465 return -ENODEV;
5466 }
5467
5468 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005469 netdev = alloc_etherdev(sizeof(struct r8152));
5470 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005471 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005472 return -ENOMEM;
5473 }
5474
hayeswangebc2ec482013-08-14 20:54:38 +08005475 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005476 tp = netdev_priv(netdev);
5477 tp->msg_enable = 0x7FFF;
5478
hayeswange3ad4122014-01-06 17:08:42 +08005479 tp->udev = udev;
5480 tp->netdev = netdev;
5481 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005482 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005483
hayeswang33928ee2017-03-17 11:20:13 +08005484 switch (version) {
5485 case RTL_VER_01:
5486 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005487 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005488 tp->mii.supports_gmii = 0;
5489 break;
5490 default:
5491 tp->mii.supports_gmii = 1;
5492 break;
5493 }
5494
hayeswang55b65472014-11-06 12:47:39 +08005495 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005496 if (ret)
5497 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005498
hayeswangb5403272014-10-09 18:00:26 +08005499 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005500 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005501 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
hayeswangac718b62013-05-02 16:01:25 +00005502
hayeswangac718b62013-05-02 16:01:25 +00005503 netdev->netdev_ops = &rtl8152_netdev_ops;
5504 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005505
hayeswang60c89072014-03-07 11:04:39 +08005506 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005507 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005508 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5509 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005510 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005511 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005512 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005513 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005514 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5515 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5516 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005517
hayeswang19c0f402017-01-11 16:25:34 +08005518 if (tp->version == RTL_VER_01) {
5519 netdev->features &= ~NETIF_F_RXCSUM;
5520 netdev->hw_features &= ~NETIF_F_RXCSUM;
5521 }
5522
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005523 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5524 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005525 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5526 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5527 }
5528
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005529 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005530 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005531
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005532 /* MTU range: 68 - 1500 or 9194 */
5533 netdev->min_mtu = ETH_MIN_MTU;
5534 switch (tp->version) {
5535 case RTL_VER_01:
5536 case RTL_VER_02:
5537 netdev->max_mtu = ETH_DATA_LEN;
5538 break;
5539 default:
5540 netdev->max_mtu = RTL8153_MAX_MTU;
5541 break;
5542 }
5543
hayeswangac718b62013-05-02 16:01:25 +00005544 tp->mii.dev = netdev;
5545 tp->mii.mdio_read = read_mii_word;
5546 tp->mii.mdio_write = write_mii_word;
5547 tp->mii.phy_id_mask = 0x3f;
5548 tp->mii.reg_num_mask = 0x1f;
5549 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005550
hayeswangaa7e26b2016-06-13 17:49:38 +08005551 tp->autoneg = AUTONEG_ENABLE;
5552 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5553 tp->duplex = DUPLEX_FULL;
5554
Hayes Wange4a50172019-08-13 11:42:09 +08005555 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
5556 tp->rx_pending = 10 * RTL8152_MAX_RX;
5557
hayeswang9a4be1b2014-02-18 21:49:07 +08005558 intf->needs_remote_wakeup = 1;
5559
hayeswangc81229c2014-01-02 11:22:42 +08005560 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005561 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005562 set_ethernet_addr(tp);
5563
hayeswangac718b62013-05-02 16:01:25 +00005564 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005565 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005566
hayeswangebc2ec482013-08-14 20:54:38 +08005567 ret = register_netdev(netdev);
5568 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005569 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005570 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005571 }
5572
hayeswang7daed8d2015-07-24 13:54:24 +08005573 if (!rtl_can_wakeup(tp))
5574 __rtl_set_wol(tp, 0);
5575
hayeswang21ff2e82014-02-18 21:49:06 +08005576 tp->saved_wolopts = __rtl_get_wol(tp);
5577 if (tp->saved_wolopts)
5578 device_set_wakeup_enable(&udev->dev, true);
5579 else
5580 device_set_wakeup_enable(&udev->dev, false);
5581
Hayes Wang4a8deae2014-01-07 11:18:22 +08005582 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005583
5584 return 0;
5585
hayeswangac718b62013-05-02 16:01:25 +00005586out1:
hayeswangd823ab62015-01-12 12:06:23 +08005587 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08005588 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005589out:
5590 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005591 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005592}
5593
hayeswangac718b62013-05-02 16:01:25 +00005594static void rtl8152_disconnect(struct usb_interface *intf)
5595{
5596 struct r8152 *tp = usb_get_intfdata(intf);
5597
5598 usb_set_intfdata(intf, NULL);
5599 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08005600 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08005601
hayeswangd823ab62015-01-12 12:06:23 +08005602 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005603 unregister_netdev(tp->netdev);
hayeswanga028a9e2016-06-13 17:49:36 +08005604 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005605 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005606 free_netdev(tp->netdev);
5607 }
5608}
5609
hayeswangd9a28c52014-12-04 10:43:11 +08005610#define REALTEK_USB_DEVICE(vend, prod) \
5611 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5612 USB_DEVICE_ID_MATCH_INT_CLASS, \
5613 .idVendor = (vend), \
5614 .idProduct = (prod), \
5615 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5616}, \
5617{ \
5618 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5619 USB_DEVICE_ID_MATCH_DEVICE, \
5620 .idVendor = (vend), \
5621 .idProduct = (prod), \
5622 .bInterfaceClass = USB_CLASS_COMM, \
5623 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5624 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5625
hayeswangac718b62013-05-02 16:01:25 +00005626/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305627static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005628 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005629 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5630 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005631 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5632 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005633 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005634 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005635 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5636 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5637 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5638 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5639 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005640 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005641 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005642 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005643 {}
5644};
5645
5646MODULE_DEVICE_TABLE(usb, rtl8152_table);
5647
5648static struct usb_driver rtl8152_driver = {
5649 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005650 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005651 .probe = rtl8152_probe,
5652 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005653 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005654 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005655 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005656 .pre_reset = rtl8152_pre_reset,
5657 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005658 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005659 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005660};
5661
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005662module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005663
5664MODULE_AUTHOR(DRIVER_AUTHOR);
5665MODULE_DESCRIPTION(DRIVER_DESC);
5666MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005667MODULE_VERSION(DRIVER_VERSION);