blob: 17f0e9e98697b9fff09ab918736d8252efa79fdc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
hayeswangac718b62013-05-02 16:01:25 +00002/*
hayeswangc7de7de2014-01-15 10:42:16 +08003 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00004 */
5
hayeswangac718b62013-05-02 16:01:25 +00006#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +00009#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/crc32.h>
15#include <linux/if_vlan.h>
16#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080017#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080018#include <linux/ip.h>
19#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080020#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080021#include <uapi/linux/mdio.h>
22#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080023#include <linux/usb/cdc.h>
hayeswang5ee3c602016-01-07 17:12:17 +080024#include <linux/suspend.h>
Hayes Wang252df8b2019-08-13 11:42:06 +080025#include <linux/atomic.h>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050026#include <linux/acpi.h>
hayeswangac718b62013-05-02 16:01:25 +000027
hayeswangd0942472015-09-07 11:57:43 +080028/* Information for net-next */
Hayes Wange4a50172019-08-13 11:42:09 +080029#define NETNEXT_VERSION "10"
hayeswangd0942472015-09-07 11:57:43 +080030
31/* Information for net */
Hayes Wangffa9fec2019-07-04 17:36:32 +080032#define NET_VERSION "10"
hayeswangd0942472015-09-07 11:57:43 +080033
34#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000035#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080036#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000037#define MODULENAME "r8152"
38
39#define R8152_PHY_ID 32
40
41#define PLA_IDR 0xc000
42#define PLA_RCR 0xc010
43#define PLA_RMS 0xc016
44#define PLA_RXFIFO_CTRL0 0xc0a0
45#define PLA_RXFIFO_CTRL1 0xc0a4
46#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080047#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000048#define PLA_FMC 0xc0b4
49#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080050#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080051#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000052#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080053#define PLA_BACKUP 0xd000
Kevin Lo59c0b472019-08-01 11:29:38 +080054#define PLA_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_TEREDO_TIMER 0xd2cc
56#define PLA_REALWOW_TIMER 0xd2e8
Hayes Wang13e04fbf2019-07-01 15:53:19 +080057#define PLA_SUSPEND_FLAG 0xd38a
58#define PLA_INDICATE_FALG 0xd38c
59#define PLA_EXTRA_STATUS 0xd398
hayeswang65b82d62017-06-15 14:44:03 +080060#define PLA_EFUSE_DATA 0xdd00
61#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000062#define PLA_LEDSEL 0xdd90
63#define PLA_LED_FEATURE 0xdd92
64#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080065#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000066#define PLA_GPHY_INTR_IMR 0xe022
67#define PLA_EEE_CR 0xe040
68#define PLA_EEEP_CR 0xe080
69#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080070#define PLA_MAC_PWR_CTRL2 0xe0ca
71#define PLA_MAC_PWR_CTRL3 0xe0cc
72#define PLA_MAC_PWR_CTRL4 0xe0ce
73#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000074#define PLA_TCR0 0xe610
75#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080076#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000077#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080078#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000079#define PLA_CR 0xe813
80#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080081#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
82#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000083#define PLA_CONFIG5 0xe822
84#define PLA_PHY_PWR 0xe84c
85#define PLA_OOB_CTRL 0xe84f
86#define PLA_CPCR 0xe854
87#define PLA_MISC_0 0xe858
88#define PLA_MISC_1 0xe85a
89#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080090#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000091#define PLA_SFF_STS_7 0xe8de
92#define PLA_PHYSTATUS 0xe908
93#define PLA_BP_BA 0xfc26
94#define PLA_BP_0 0xfc28
95#define PLA_BP_1 0xfc2a
96#define PLA_BP_2 0xfc2c
97#define PLA_BP_3 0xfc2e
98#define PLA_BP_4 0xfc30
99#define PLA_BP_5 0xfc32
100#define PLA_BP_6 0xfc34
101#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800102#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000103
hayeswang65bab842015-02-12 16:20:46 +0800104#define USB_USB2PHY 0xb41e
105#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800106#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800107#define USB_CSR_DUMMY1 0xb464
108#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000109#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800110#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800111#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800112#define USB_BURST_SIZE 0xcfc0
hayeswang65b82d62017-06-15 14:44:03 +0800113#define USB_LPM_CONFIG 0xcfd8
hayeswangac718b62013-05-02 16:01:25 +0000114#define USB_USB_CTRL 0xd406
115#define USB_PHY_CTRL 0xd408
116#define USB_TX_AGG 0xd40a
117#define USB_RX_BUF_TH 0xd40c
118#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800119#define USB_RX_EARLY_TIMEOUT 0xd42c
120#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800121#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
122#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000123#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800124#define USB_UPT_RXDMA_OWN 0xd437
hayeswang43779f82014-01-02 11:25:10 +0800125#define USB_TOLERANCE 0xd490
126#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800127#define USB_BMU_RESET 0xd4b0
hayeswang65b82d62017-06-15 14:44:03 +0800128#define USB_U1U2_TIMER 0xd4da
hayeswangac718b62013-05-02 16:01:25 +0000129#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800130#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800131#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600132#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800133#define USB_AFE_CTRL2 0xd824
hayeswang65b82d62017-06-15 14:44:03 +0800134#define USB_UPS_CFG 0xd842
135#define USB_UPS_FLAGS 0xd848
hayeswang43779f82014-01-02 11:25:10 +0800136#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000137#define USB_BP_BA 0xfc26
138#define USB_BP_0 0xfc28
139#define USB_BP_1 0xfc2a
140#define USB_BP_2 0xfc2c
141#define USB_BP_3 0xfc2e
142#define USB_BP_4 0xfc30
143#define USB_BP_5 0xfc32
144#define USB_BP_6 0xfc34
145#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800146#define USB_BP_EN 0xfc38
hayeswang65b82d62017-06-15 14:44:03 +0800147#define USB_BP_8 0xfc38
148#define USB_BP_9 0xfc3a
149#define USB_BP_10 0xfc3c
150#define USB_BP_11 0xfc3e
151#define USB_BP_12 0xfc40
152#define USB_BP_13 0xfc42
153#define USB_BP_14 0xfc44
154#define USB_BP_15 0xfc46
155#define USB_BP2_EN 0xfc48
hayeswangac718b62013-05-02 16:01:25 +0000156
157/* OCP Registers */
158#define OCP_ALDPS_CONFIG 0x2010
159#define OCP_EEE_CONFIG1 0x2080
160#define OCP_EEE_CONFIG2 0x2092
161#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800162#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000163#define OCP_EEE_AR 0xa41a
164#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800165#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800166#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800167#define OCP_POWER_CFG 0xa430
168#define OCP_EEE_CFG 0xa432
169#define OCP_SRAM_ADDR 0xa436
170#define OCP_SRAM_DATA 0xa438
171#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800172#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800173#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800174#define OCP_EEE_LPABLE 0xa5d2
hayeswang2dd49e02015-09-07 11:57:44 +0800175#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800176#define OCP_PHY_PATCH_STAT 0xb800
177#define OCP_PHY_PATCH_CMD 0xb820
178#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800179#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800180#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800181
182/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800183#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800184#define SRAM_LPF_CFG 0x8012
185#define SRAM_10M_AMP1 0x8080
186#define SRAM_10M_AMP2 0x8082
187#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000188
189/* PLA_RCR */
190#define RCR_AAP 0x00000001
191#define RCR_APM 0x00000002
192#define RCR_AM 0x00000004
193#define RCR_AB 0x00000008
194#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
195
196/* PLA_RXFIFO_CTRL0 */
197#define RXFIFO_THR1_NORMAL 0x00080002
198#define RXFIFO_THR1_OOB 0x01800003
199
200/* PLA_RXFIFO_CTRL1 */
201#define RXFIFO_THR2_FULL 0x00000060
202#define RXFIFO_THR2_HIGH 0x00000038
203#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800204#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000205
206/* PLA_RXFIFO_CTRL2 */
207#define RXFIFO_THR3_FULL 0x00000078
208#define RXFIFO_THR3_HIGH 0x00000048
209#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800210#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000211
212/* PLA_TXFIFO_CTRL */
213#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800214#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000215
hayeswang65bab842015-02-12 16:20:46 +0800216/* PLA_DMY_REG0 */
217#define ECM_ALDPS 0x0002
218
hayeswangac718b62013-05-02 16:01:25 +0000219/* PLA_FMC */
220#define FMC_FCR_MCU_EN 0x0001
221
222/* PLA_EEEP_CR */
223#define EEEP_CR_EEEP_TX 0x0002
224
hayeswang43779f82014-01-02 11:25:10 +0800225/* PLA_WDT6_CTRL */
226#define WDT6_SET_MODE 0x0010
227
hayeswangac718b62013-05-02 16:01:25 +0000228/* PLA_TCR0 */
229#define TCR0_TX_EMPTY 0x0800
230#define TCR0_AUTO_FIFO 0x0080
231
232/* PLA_TCR1 */
233#define VERSION_MASK 0x7cf0
234
hayeswang69b4b7a2014-07-10 10:58:54 +0800235/* PLA_MTPS */
236#define MTPS_JUMBO (12 * 1024 / 64)
237#define MTPS_DEFAULT (6 * 1024 / 64)
238
hayeswang4f1d4d52014-03-11 16:24:19 +0800239/* PLA_RSTTALLY */
240#define TALLY_RESET 0x0001
241
hayeswangac718b62013-05-02 16:01:25 +0000242/* PLA_CR */
243#define CR_RST 0x10
244#define CR_RE 0x08
245#define CR_TE 0x04
246
247/* PLA_CRWECR */
248#define CRWECR_NORAML 0x00
249#define CRWECR_CONFIG 0xc0
250
251/* PLA_OOB_CTRL */
252#define NOW_IS_OOB 0x80
253#define TXFIFO_EMPTY 0x20
254#define RXFIFO_EMPTY 0x10
255#define LINK_LIST_READY 0x02
256#define DIS_MCU_CLROOB 0x01
257#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
258
259/* PLA_MISC_1 */
260#define RXDY_GATED_EN 0x0008
261
262/* PLA_SFF_STS_7 */
263#define RE_INIT_LL 0x8000
264#define MCU_BORW_EN 0x4000
265
266/* PLA_CPCR */
267#define CPCR_RX_VLAN 0x0040
268
269/* PLA_CFG_WOL */
270#define MAGIC_EN 0x0001
271
hayeswang43779f82014-01-02 11:25:10 +0800272/* PLA_TEREDO_CFG */
273#define TEREDO_SEL 0x8000
274#define TEREDO_WAKE_MASK 0x7f00
275#define TEREDO_RS_EVENT_MASK 0x00fe
276#define OOB_TEREDO_EN 0x0001
277
Kevin Lo59c0b472019-08-01 11:29:38 +0800278/* PLA_BDC_CR */
hayeswangac718b62013-05-02 16:01:25 +0000279#define ALDPS_PROXY_MODE 0x0001
280
hayeswang65b82d62017-06-15 14:44:03 +0800281/* PLA_EFUSE_CMD */
282#define EFUSE_READ_CMD BIT(15)
283#define EFUSE_DATA_BIT16 BIT(7)
284
hayeswang21ff2e82014-02-18 21:49:06 +0800285/* PLA_CONFIG34 */
286#define LINK_ON_WAKE_EN 0x0010
287#define LINK_OFF_WAKE_EN 0x0008
288
hayeswangac718b62013-05-02 16:01:25 +0000289/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800290#define BWF_EN 0x0040
291#define MWF_EN 0x0020
292#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000293#define LAN_WAKE_EN 0x0002
294
295/* PLA_LED_FEATURE */
296#define LED_MODE_MASK 0x0700
297
298/* PLA_PHY_PWR */
299#define TX_10M_IDLE_EN 0x0080
300#define PFM_PWM_SWITCH 0x0040
301
302/* PLA_MAC_PWR_CTRL */
303#define D3_CLK_GATED_EN 0x00004000
304#define MCU_CLK_RATIO 0x07010f07
305#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800306#define ALDPS_SPDWN_RATIO 0x0f87
307
308/* PLA_MAC_PWR_CTRL2 */
309#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800310#define MAC_CLK_SPDWN_EN BIT(15)
hayeswang43779f82014-01-02 11:25:10 +0800311
312/* PLA_MAC_PWR_CTRL3 */
313#define PKT_AVAIL_SPDWN_EN 0x0100
314#define SUSPEND_SPDWN_EN 0x0004
315#define U1U2_SPDWN_EN 0x0002
316#define L1_SPDWN_EN 0x0001
317
318/* PLA_MAC_PWR_CTRL4 */
319#define PWRSAVE_SPDWN_EN 0x1000
320#define RXDV_SPDWN_EN 0x0800
321#define TX10MIDLE_EN 0x0100
322#define TP100_SPDWN_EN 0x0020
323#define TP500_SPDWN_EN 0x0010
324#define TP1000_SPDWN_EN 0x0008
325#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000326
327/* PLA_GPHY_INTR_IMR */
328#define GPHY_STS_MSK 0x0001
329#define SPEED_DOWN_MSK 0x0002
330#define SPDWN_RXDV_MSK 0x0004
331#define SPDWN_LINKCHG_MSK 0x0008
332
333/* PLA_PHYAR */
334#define PHYAR_FLAG 0x80000000
335
336/* PLA_EEE_CR */
337#define EEE_RX_EN 0x0001
338#define EEE_TX_EN 0x0002
339
hayeswang43779f82014-01-02 11:25:10 +0800340/* PLA_BOOT_CTRL */
341#define AUTOLOAD_DONE 0x0002
342
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800343/* PLA_SUSPEND_FLAG */
344#define LINK_CHG_EVENT BIT(0)
345
346/* PLA_INDICATE_FALG */
347#define UPCOMING_RUNTIME_D3 BIT(0)
348
349/* PLA_EXTRA_STATUS */
350#define LINK_CHANGE_FLAG BIT(8)
351
hayeswang65bab842015-02-12 16:20:46 +0800352/* USB_USB2PHY */
353#define USB2PHY_SUSPEND 0x0001
354#define USB2PHY_L1 0x0002
355
356/* USB_SSPHYLINK2 */
357#define pwd_dn_scale_mask 0x3ffe
358#define pwd_dn_scale(x) ((x) << 1)
359
360/* USB_CSR_DUMMY1 */
361#define DYNAMIC_BURST 0x0001
362
363/* USB_CSR_DUMMY2 */
364#define EP4_FULL_FC 0x0001
365
hayeswangac718b62013-05-02 16:01:25 +0000366/* USB_DEV_STAT */
367#define STAT_SPEED_MASK 0x0006
368#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800369#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000370
hayeswang65b82d62017-06-15 14:44:03 +0800371/* USB_LPM_CONFIG */
372#define LPM_U1U2_EN BIT(0)
373
hayeswangac718b62013-05-02 16:01:25 +0000374/* USB_TX_AGG */
375#define TX_AGG_MAX_THRESHOLD 0x03
376
377/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800378#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800379#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800380#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800381#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000382
383/* USB_TX_DMA */
384#define TEST_MODE_DISABLE 0x00000001
385#define TX_SIZE_ADJUST1 0x00000100
386
hayeswang93fe9b12016-06-16 10:55:18 +0800387/* USB_BMU_RESET */
388#define BMU_RESET_EP_IN 0x01
389#define BMU_RESET_EP_OUT 0x02
390
hayeswang65b82d62017-06-15 14:44:03 +0800391/* USB_UPT_RXDMA_OWN */
392#define OWN_UPDATE BIT(0)
393#define OWN_CLEAR BIT(1)
394
hayeswangac718b62013-05-02 16:01:25 +0000395/* USB_UPS_CTRL */
396#define POWER_CUT 0x0100
397
398/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800399#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000400
401/* USB_USB_CTRL */
402#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800403#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000404
hayeswang43779f82014-01-02 11:25:10 +0800405/* USB_U2P3_CTRL */
406#define U2P3_ENABLE 0x0001
407
408/* USB_POWER_CUT */
409#define PWR_EN 0x0001
410#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800411#define UPS_EN BIT(4)
412#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800413
414/* USB_MISC_0 */
415#define PCUT_STATUS 0x0001
416
hayeswang464ec102015-02-12 14:33:46 +0800417/* USB_RX_EARLY_TIMEOUT */
418#define COALESCE_SUPER 85000U
419#define COALESCE_HIGH 250000U
420#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800421
422/* USB_WDT11_CTRL */
423#define TIMER11_EN 0x0001
424
425/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800426/* bit 4 ~ 5: fifo empty boundary */
427#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
428/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800429#define LPM_TIMER_MASK 0x0c
430#define LPM_TIMER_500MS 0x04 /* 500 ms */
431#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800432#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800433
434/* USB_AFE_CTRL2 */
435#define SEN_VAL_MASK 0xf800
436#define SEN_VAL_NORMAL 0xa000
437#define SEL_RXIDLE 0x0100
438
hayeswang65b82d62017-06-15 14:44:03 +0800439/* USB_UPS_CFG */
440#define SAW_CNT_1MS_MASK 0x0fff
441
442/* USB_UPS_FLAGS */
443#define UPS_FLAGS_R_TUNE BIT(0)
444#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
445#define UPS_FLAGS_250M_CKDIV BIT(2)
446#define UPS_FLAGS_EN_ALDPS BIT(3)
447#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
448#define UPS_FLAGS_SPEED_MASK (0xf << 16)
449#define ups_flags_speed(x) ((x) << 16)
450#define UPS_FLAGS_EN_EEE BIT(20)
451#define UPS_FLAGS_EN_500M_EEE BIT(21)
452#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
453#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
454#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
455#define UPS_FLAGS_EN_GREEN BIT(26)
456#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
457
458enum spd_duplex {
459 NWAY_10M_HALF = 1,
460 NWAY_10M_FULL,
461 NWAY_100M_HALF,
462 NWAY_100M_FULL,
463 NWAY_1000M_FULL,
464 FORCE_10M_HALF,
465 FORCE_10M_FULL,
466 FORCE_100M_HALF,
467 FORCE_100M_FULL,
468};
469
hayeswangac718b62013-05-02 16:01:25 +0000470/* OCP_ALDPS_CONFIG */
471#define ENPWRSAVE 0x8000
472#define ENPDNPS 0x0200
473#define LINKENA 0x0100
474#define DIS_SDSAVE 0x0010
475
hayeswang43779f82014-01-02 11:25:10 +0800476/* OCP_PHY_STATUS */
477#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800478#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800479#define PHY_STAT_LAN_ON 3
480#define PHY_STAT_PWRDN 5
481
hayeswang65b82d62017-06-15 14:44:03 +0800482/* OCP_NCTL_CFG */
483#define PGA_RETURN_EN BIT(1)
484
hayeswang43779f82014-01-02 11:25:10 +0800485/* OCP_POWER_CFG */
486#define EEE_CLKDIV_EN 0x8000
487#define EN_ALDPS 0x0004
488#define EN_10M_PLLOFF 0x0001
489
hayeswangac718b62013-05-02 16:01:25 +0000490/* OCP_EEE_CONFIG1 */
491#define RG_TXLPI_MSK_HFDUP 0x8000
492#define RG_MATCLR_EN 0x4000
493#define EEE_10_CAP 0x2000
494#define EEE_NWAY_EN 0x1000
495#define TX_QUIET_EN 0x0200
496#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800497#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800498#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000499#define RG_RXLPI_MSK_HFDUP 0x0008
500#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
501
502/* OCP_EEE_CONFIG2 */
503#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
504#define RG_DACQUIET_EN 0x0400
505#define RG_LDVQUIET_EN 0x0200
506#define RG_CKRSEL 0x0020
507#define RG_EEEPRG_EN 0x0010
508
509/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800510#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800511#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000512#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
513#define MSK_PH 0x0006 /* bit 0 ~ 3 */
514
515/* OCP_EEE_AR */
516/* bit[15:14] function */
517#define FUN_ADDR 0x0000
518#define FUN_DATA 0x4000
519/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000520
hayeswang43779f82014-01-02 11:25:10 +0800521/* OCP_EEE_CFG */
522#define CTAP_SHORT_EN 0x0040
523#define EEE10_EN 0x0010
524
525/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800526#define EN_EEE_CMODE BIT(14)
527#define EN_EEE_1000 BIT(13)
528#define EN_EEE_100 BIT(12)
529#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800530#define EN_10M_BGOFF 0x0080
531
hayeswang2dd49e02015-09-07 11:57:44 +0800532/* OCP_PHY_STATE */
533#define TXDIS_STATE 0x01
534#define ABD_STATE 0x02
535
hayeswang65b82d62017-06-15 14:44:03 +0800536/* OCP_PHY_PATCH_STAT */
537#define PATCH_READY BIT(6)
538
539/* OCP_PHY_PATCH_CMD */
540#define PATCH_REQUEST BIT(4)
541
hayeswang43779f82014-01-02 11:25:10 +0800542/* OCP_ADC_CFG */
543#define CKADSEL_L 0x0100
544#define ADC_EN 0x0080
545#define EN_EMI_L 0x0040
546
hayeswang65b82d62017-06-15 14:44:03 +0800547/* OCP_SYSCLK_CFG */
548#define clk_div_expo(x) (min(x, 5) << 8)
549
550/* SRAM_GREEN_CFG */
551#define GREEN_ETH_EN BIT(15)
552#define R_TUNE_EN BIT(11)
553
hayeswang43779f82014-01-02 11:25:10 +0800554/* SRAM_LPF_CFG */
555#define LPF_AUTO_TUNE 0x8000
556
557/* SRAM_10M_AMP1 */
558#define GDAC_IB_UPALL 0x0008
559
560/* SRAM_10M_AMP2 */
561#define AMP_DN 0x0200
562
563/* SRAM_IMPEDANCE */
564#define RX_DRIVING_MASK 0x6000
565
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500566/* MAC PASSTHRU */
567#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600568#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800569#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500570#define EFUSE 0xcfdb
571#define PASS_THRU_MASK 0x1
572
hayeswangac718b62013-05-02 16:01:25 +0000573enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800574 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000575 _100bps = 0x08,
576 _10bps = 0x04,
577 LINK_STATUS = 0x02,
578 FULL_DUP = 0x01,
579};
580
hayeswang1764bcd2014-08-28 10:24:18 +0800581#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800582#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800583#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800584#define TX_ALIGN 4
585#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800586
Hayes Wange4a50172019-08-13 11:42:09 +0800587#define RTL8152_RX_MAX_PENDING 4096
Hayes Wang47922fc2019-08-13 11:42:08 +0800588#define RTL8152_RXFG_HEADSZ 256
589
hayeswang40a82912013-08-14 20:54:40 +0800590#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800591
hayeswangac718b62013-05-02 16:01:25 +0000592#define RTL8152_REQT_READ 0xc0
593#define RTL8152_REQT_WRITE 0x40
594#define RTL8152_REQ_GET_REGS 0x05
595#define RTL8152_REQ_SET_REGS 0x05
596
597#define BYTE_EN_DWORD 0xff
598#define BYTE_EN_WORD 0x33
599#define BYTE_EN_BYTE 0x11
600#define BYTE_EN_SIX_BYTES 0x3f
601#define BYTE_EN_START_MASK 0x0f
602#define BYTE_EN_END_MASK 0xf0
603
hayeswang69b4b7a2014-07-10 10:58:54 +0800604#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800605#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
606 ETH_FCS_LEN)
607#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800608#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800609#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800610#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800611#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800612 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000613
614/* rtl8152 flags */
615enum rtl8152_flags {
616 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000617 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800618 WORK_ENABLE,
619 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800620 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800621 PHY_RESET,
Hayes Wangd2187f82019-08-19 14:40:36 +0800622 SCHEDULE_TASKLET,
hayeswang65b82d62017-06-15 14:44:03 +0800623 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800624 DELL_TB_RX_AGG_BUG,
hayeswangac718b62013-05-02 16:01:25 +0000625};
626
627/* Define these values to match your device */
628#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200629#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800630#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200631#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700632#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700633#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800634#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000635
636#define MCU_TYPE_PLA 0x0100
637#define MCU_TYPE_USB 0x0000
638
hayeswang4f1d4d52014-03-11 16:24:19 +0800639struct tally_counter {
640 __le64 tx_packets;
641 __le64 rx_packets;
642 __le64 tx_errors;
643 __le32 rx_errors;
644 __le16 rx_missed;
645 __le16 align_errors;
646 __le32 tx_one_collision;
647 __le32 tx_multi_collision;
648 __le64 rx_unicast;
649 __le64 rx_broadcast;
650 __le32 rx_multicast;
651 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800652 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800653};
654
hayeswangac718b62013-05-02 16:01:25 +0000655struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800656 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000657#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800658
hayeswang500b6d72013-11-20 17:30:57 +0800659 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800660#define RD_UDP_CS BIT(23)
661#define RD_TCP_CS BIT(22)
662#define RD_IPV6_CS BIT(20)
663#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800664
hayeswang500b6d72013-11-20 17:30:57 +0800665 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800666#define IPF BIT(23) /* IP checksum fail */
667#define UDPF BIT(22) /* UDP checksum fail */
668#define TCPF BIT(21) /* TCP checksum fail */
669#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800670
hayeswang500b6d72013-11-20 17:30:57 +0800671 __le32 opts4;
672 __le32 opts5;
673 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000674};
675
676struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800677 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800678#define TX_FS BIT(31) /* First segment of a packet */
679#define TX_LS BIT(30) /* Final segment of a packet */
680#define GTSENDV4 BIT(28)
681#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800682#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800683#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800684#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800685
hayeswang500b6d72013-11-20 17:30:57 +0800686 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800687#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
688#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
689#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
690#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800691#define MSS_SHIFT 17
692#define MSS_MAX 0x7ffU
693#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800694#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800695#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000696};
697
hayeswangdff4e8a2013-08-16 16:09:33 +0800698struct r8152;
699
hayeswangebc2ec482013-08-14 20:54:38 +0800700struct rx_agg {
Hayes Wang252df8b2019-08-13 11:42:06 +0800701 struct list_head list, info_list;
hayeswangebc2ec482013-08-14 20:54:38 +0800702 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800703 struct r8152 *context;
Hayes Wangd55d7082019-08-13 11:42:07 +0800704 struct page *page;
hayeswangebc2ec482013-08-14 20:54:38 +0800705 void *buffer;
hayeswangebc2ec482013-08-14 20:54:38 +0800706};
707
708struct tx_agg {
709 struct list_head list;
710 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800711 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800712 void *buffer;
713 void *head;
714 u32 skb_num;
715 u32 skb_len;
716};
717
hayeswangac718b62013-05-02 16:01:25 +0000718struct r8152 {
719 unsigned long flags;
720 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800721 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800722 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000723 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800724 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800725 struct tx_agg tx_info[RTL8152_MAX_TX];
Hayes Wang47922fc2019-08-13 11:42:08 +0800726 struct list_head rx_info, rx_used;
hayeswangebc2ec482013-08-14 20:54:38 +0800727 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800728 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800729 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800730 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000731 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800732 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800733#ifdef CONFIG_PM_SLEEP
734 struct notifier_block pm_notifier;
735#endif
Hayes Wangd2187f82019-08-19 14:40:36 +0800736 struct tasklet_struct tx_tl;
hayeswangc81229c2014-01-02 11:22:42 +0800737
738 struct rtl_ops {
739 void (*init)(struct r8152 *);
740 int (*enable)(struct r8152 *);
741 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800742 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800743 void (*down)(struct r8152 *);
744 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800745 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
746 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800747 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800748 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800749 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800750 } rtl_ops;
751
Hayes Wang252df8b2019-08-13 11:42:06 +0800752 atomic_t rx_count;
753
Hayes Wangf4a93be2019-08-23 15:33:40 +0800754 bool eee_en;
hayeswang40a82912013-08-14 20:54:40 +0800755 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800756 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000757 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800758 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800759 u32 coalesce;
Hayes Wangec5791c2019-08-13 11:42:05 +0800760 u32 rx_buf_sz;
Hayes Wange4a50172019-08-13 11:42:09 +0800761 u32 rx_copybreak;
762 u32 rx_pending;
763
hayeswangac718b62013-05-02 16:01:25 +0000764 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800765 u16 speed;
Hayes Wangf4a93be2019-08-23 15:33:40 +0800766 u16 eee_adv;
hayeswang40a82912013-08-14 20:54:40 +0800767 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000768 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800769 u8 duplex;
770 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000771};
772
773enum rtl_version {
774 RTL_VER_UNKNOWN = 0,
775 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800776 RTL_VER_02,
777 RTL_VER_03,
778 RTL_VER_04,
779 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800780 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800781 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800782 RTL_VER_08,
783 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800784 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000785};
786
hayeswang60c89072014-03-07 11:04:39 +0800787enum tx_csum_stat {
788 TX_CSUM_SUCCESS = 0,
789 TX_CSUM_TSO,
790 TX_CSUM_NONE
791};
792
hayeswangac718b62013-05-02 16:01:25 +0000793/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
794 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
795 */
796static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800797static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000798
hayeswang52aec122014-09-02 10:27:52 +0800799#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800800 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800801
hayeswangac718b62013-05-02 16:01:25 +0000802static
803int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
804{
hayeswang31787f52013-07-31 17:21:25 +0800805 int ret;
806 void *tmp;
807
808 tmp = kmalloc(size, GFP_KERNEL);
809 if (!tmp)
810 return -ENOMEM;
811
812 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800813 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
814 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800815
816 memcpy(data, tmp, size);
817 kfree(tmp);
818
819 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000820}
821
822static
823int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
824{
hayeswang31787f52013-07-31 17:21:25 +0800825 int ret;
826 void *tmp;
827
Benoit Tainec4438f02014-05-26 17:21:23 +0200828 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800829 if (!tmp)
830 return -ENOMEM;
831
hayeswang31787f52013-07-31 17:21:25 +0800832 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800833 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
834 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800835
836 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800837
hayeswang31787f52013-07-31 17:21:25 +0800838 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000839}
840
Hayes Wangffa9fec2019-07-04 17:36:32 +0800841static void rtl_set_unplug(struct r8152 *tp)
842{
843 if (tp->udev->state == USB_STATE_NOTATTACHED) {
844 set_bit(RTL8152_UNPLUG, &tp->flags);
845 smp_mb__after_atomic();
846 }
847}
848
hayeswangac718b62013-05-02 16:01:25 +0000849static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800850 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000851{
hayeswang45f4a192014-01-06 17:08:41 +0800852 u16 limit = 64;
853 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000854
855 if (test_bit(RTL8152_UNPLUG, &tp->flags))
856 return -ENODEV;
857
858 /* both size and indix must be 4 bytes align */
859 if ((size & 3) || !size || (index & 3) || !data)
860 return -EPERM;
861
862 if ((u32)index + (u32)size > 0xffff)
863 return -EPERM;
864
865 while (size) {
866 if (size > limit) {
867 ret = get_registers(tp, index, type, limit, data);
868 if (ret < 0)
869 break;
870
871 index += limit;
872 data += limit;
873 size -= limit;
874 } else {
875 ret = get_registers(tp, index, type, size, data);
876 if (ret < 0)
877 break;
878
879 index += size;
880 data += size;
881 size = 0;
882 break;
883 }
884 }
885
hayeswang67610492014-10-30 11:46:40 +0800886 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800887 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800888
hayeswangac718b62013-05-02 16:01:25 +0000889 return ret;
890}
891
892static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800893 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000894{
hayeswang45f4a192014-01-06 17:08:41 +0800895 int ret;
896 u16 byteen_start, byteen_end, byen;
897 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000898
899 if (test_bit(RTL8152_UNPLUG, &tp->flags))
900 return -ENODEV;
901
902 /* both size and indix must be 4 bytes align */
903 if ((size & 3) || !size || (index & 3) || !data)
904 return -EPERM;
905
906 if ((u32)index + (u32)size > 0xffff)
907 return -EPERM;
908
909 byteen_start = byteen & BYTE_EN_START_MASK;
910 byteen_end = byteen & BYTE_EN_END_MASK;
911
912 byen = byteen_start | (byteen_start << 4);
913 ret = set_registers(tp, index, type | byen, 4, data);
914 if (ret < 0)
915 goto error1;
916
917 index += 4;
918 data += 4;
919 size -= 4;
920
921 if (size) {
922 size -= 4;
923
924 while (size) {
925 if (size > limit) {
926 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800927 type | BYTE_EN_DWORD,
928 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000929 if (ret < 0)
930 goto error1;
931
932 index += limit;
933 data += limit;
934 size -= limit;
935 } else {
936 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800937 type | BYTE_EN_DWORD,
938 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000939 if (ret < 0)
940 goto error1;
941
942 index += size;
943 data += size;
944 size = 0;
945 break;
946 }
947 }
948
949 byen = byteen_end | (byteen_end >> 4);
950 ret = set_registers(tp, index, type | byen, 4, data);
951 if (ret < 0)
952 goto error1;
953 }
954
955error1:
hayeswang67610492014-10-30 11:46:40 +0800956 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800957 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800958
hayeswangac718b62013-05-02 16:01:25 +0000959 return ret;
960}
961
962static inline
963int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
964{
965 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
966}
967
968static inline
969int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
970{
971 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
972}
973
974static inline
hayeswangac718b62013-05-02 16:01:25 +0000975int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
976{
977 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
978}
979
980static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
981{
hayeswangc8826de2013-07-31 17:21:26 +0800982 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000983
hayeswangc8826de2013-07-31 17:21:26 +0800984 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000985
986 return __le32_to_cpu(data);
987}
988
989static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
990{
hayeswangc8826de2013-07-31 17:21:26 +0800991 __le32 tmp = __cpu_to_le32(data);
992
993 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000994}
995
996static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
997{
998 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800999 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +08001000 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +00001001 u8 shift = index & 2;
1002
1003 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +08001004 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +00001005
hayeswangd8fbd272017-06-15 14:44:04 +08001006 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +00001007
hayeswangc8826de2013-07-31 17:21:26 +08001008 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001009 data >>= (shift * 8);
1010 data &= 0xffff;
1011
1012 return (u16)data;
1013}
1014
1015static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1016{
hayeswangc8826de2013-07-31 17:21:26 +08001017 u32 mask = 0xffff;
1018 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001019 u16 byen = BYTE_EN_WORD;
1020 u8 shift = index & 2;
1021
1022 data &= mask;
1023
1024 if (index & 2) {
1025 byen <<= shift;
1026 mask <<= (shift * 8);
1027 data <<= (shift * 8);
1028 index &= ~3;
1029 }
1030
hayeswangc8826de2013-07-31 17:21:26 +08001031 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001032
hayeswangc8826de2013-07-31 17:21:26 +08001033 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001034}
1035
1036static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1037{
1038 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001039 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001040 u8 shift = index & 3;
1041
1042 index &= ~3;
1043
hayeswangc8826de2013-07-31 17:21:26 +08001044 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001045
hayeswangc8826de2013-07-31 17:21:26 +08001046 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001047 data >>= (shift * 8);
1048 data &= 0xff;
1049
1050 return (u8)data;
1051}
1052
1053static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1054{
hayeswangc8826de2013-07-31 17:21:26 +08001055 u32 mask = 0xff;
1056 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001057 u16 byen = BYTE_EN_BYTE;
1058 u8 shift = index & 3;
1059
1060 data &= mask;
1061
1062 if (index & 3) {
1063 byen <<= shift;
1064 mask <<= (shift * 8);
1065 data <<= (shift * 8);
1066 index &= ~3;
1067 }
1068
hayeswangc8826de2013-07-31 17:21:26 +08001069 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001070
hayeswangc8826de2013-07-31 17:21:26 +08001071 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001072}
1073
hayeswangac244d32014-01-02 11:22:40 +08001074static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1075{
1076 u16 ocp_base, ocp_index;
1077
1078 ocp_base = addr & 0xf000;
1079 if (ocp_base != tp->ocp_base) {
1080 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1081 tp->ocp_base = ocp_base;
1082 }
1083
1084 ocp_index = (addr & 0x0fff) | 0xb000;
1085 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1086}
1087
hayeswange3fe0b12014-01-02 11:22:39 +08001088static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1089{
1090 u16 ocp_base, ocp_index;
1091
1092 ocp_base = addr & 0xf000;
1093 if (ocp_base != tp->ocp_base) {
1094 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1095 tp->ocp_base = ocp_base;
1096 }
1097
1098 ocp_index = (addr & 0x0fff) | 0xb000;
1099 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1100}
1101
hayeswangac244d32014-01-02 11:22:40 +08001102static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001103{
hayeswangac244d32014-01-02 11:22:40 +08001104 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001105}
1106
hayeswangac244d32014-01-02 11:22:40 +08001107static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001108{
hayeswangac244d32014-01-02 11:22:40 +08001109 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001110}
1111
hayeswang43779f82014-01-02 11:25:10 +08001112static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1113{
1114 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1115 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1116}
1117
hayeswang65b82d62017-06-15 14:44:03 +08001118static u16 sram_read(struct r8152 *tp, u16 addr)
1119{
1120 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1121 return ocp_reg_read(tp, OCP_SRAM_DATA);
1122}
1123
hayeswangac718b62013-05-02 16:01:25 +00001124static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1125{
1126 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001127 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001128
hayeswang68714382014-04-11 17:54:31 +08001129 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1130 return -ENODEV;
1131
hayeswangac718b62013-05-02 16:01:25 +00001132 if (phy_id != R8152_PHY_ID)
1133 return -EINVAL;
1134
hayeswang9a4be1b2014-02-18 21:49:07 +08001135 ret = r8152_mdio_read(tp, reg);
1136
hayeswang9a4be1b2014-02-18 21:49:07 +08001137 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001138}
1139
1140static
1141void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1142{
1143 struct r8152 *tp = netdev_priv(netdev);
1144
hayeswang68714382014-04-11 17:54:31 +08001145 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1146 return;
1147
hayeswangac718b62013-05-02 16:01:25 +00001148 if (phy_id != R8152_PHY_ID)
1149 return;
1150
1151 r8152_mdio_write(tp, reg, val);
1152}
1153
hayeswangb209af92014-08-25 15:53:00 +08001154static int
1155r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001156
hayeswang8ba789a2014-09-04 16:15:41 +08001157static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1158{
1159 struct r8152 *tp = netdev_priv(netdev);
1160 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001161 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001162
1163 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001164 goto out1;
1165
1166 ret = usb_autopm_get_interface(tp->intf);
1167 if (ret < 0)
1168 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001169
hayeswangb5403272014-10-09 18:00:26 +08001170 mutex_lock(&tp->control);
1171
hayeswang8ba789a2014-09-04 16:15:41 +08001172 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1173
1174 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1175 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1176 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1177
hayeswangb5403272014-10-09 18:00:26 +08001178 mutex_unlock(&tp->control);
1179
hayeswangea6a7112014-10-02 17:03:12 +08001180 usb_autopm_put_interface(tp->intf);
1181out1:
1182 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001183}
1184
Mario Limonciello9c273692018-12-11 08:16:14 -06001185/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001186 * host system provided MAC address.
1187 * Examples of this are Dell TB15 and Dell WD15 docks
1188 */
1189static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1190{
1191 acpi_status status;
1192 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1193 union acpi_object *obj;
1194 int ret = -EINVAL;
1195 u32 ocp_data;
1196 unsigned char buf[6];
1197
1198 /* test for -AD variant of RTL8153 */
1199 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001200 if ((ocp_data & AD_MASK) == 0x1000) {
1201 /* test for MAC address pass-through bit */
1202 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1203 if ((ocp_data & PASS_THRU_MASK) != 1) {
1204 netif_dbg(tp, probe, tp->netdev,
1205 "No efuse for RTL8153-AD MAC pass through\n");
1206 return -ENODEV;
1207 }
1208 } else {
David Chen8e29d232019-02-16 17:16:42 +08001209 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001210 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001211 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001212 netif_dbg(tp, probe, tp->netdev,
1213 "Invalid variant for MAC pass through\n");
1214 return -ENODEV;
1215 }
1216 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001217
1218 /* returns _AUXMAC_#AABBCCDDEEFF# */
1219 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1220 obj = (union acpi_object *)buffer.pointer;
1221 if (!ACPI_SUCCESS(status))
1222 return -ENODEV;
1223 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1224 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001225 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001226 obj->type, obj->string.length);
1227 goto amacout;
1228 }
1229 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1230 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1231 netif_warn(tp, probe, tp->netdev,
1232 "Invalid header when reading pass-thru MAC addr\n");
1233 goto amacout;
1234 }
1235 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1236 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1237 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001238 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1239 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001240 ret = -EINVAL;
1241 goto amacout;
1242 }
1243 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001244 netif_info(tp, probe, tp->netdev,
1245 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1246
1247amacout:
1248 kfree(obj);
1249 return ret;
1250}
1251
Mario Limonciello25766272019-04-04 13:46:53 -05001252static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1253{
1254 struct net_device *dev = tp->netdev;
1255 int ret;
1256
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001257 sa->sa_family = dev->type;
1258
Mario Limonciello25766272019-04-04 13:46:53 -05001259 if (tp->version == RTL_VER_01) {
1260 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1261 } else {
1262 /* if device doesn't support MAC pass through this will
1263 * be expected to be non-zero
1264 */
1265 ret = vendor_mac_passthru_addr_read(tp, sa);
1266 if (ret < 0)
1267 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1268 }
1269
1270 if (ret < 0) {
1271 netif_err(tp, probe, dev, "Get ether addr fail\n");
1272 } else if (!is_valid_ether_addr(sa->sa_data)) {
1273 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1274 sa->sa_data);
1275 eth_hw_addr_random(dev);
1276 ether_addr_copy(sa->sa_data, dev->dev_addr);
1277 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1278 sa->sa_data);
1279 return 0;
1280 }
1281
1282 return ret;
1283}
1284
hayeswang179bb6d2014-09-04 16:15:42 +08001285static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001286{
1287 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001288 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001289 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001290
Mario Limonciello25766272019-04-04 13:46:53 -05001291 ret = determine_ethernet_addr(tp, &sa);
1292 if (ret < 0)
1293 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001294
Mario Limonciello25766272019-04-04 13:46:53 -05001295 if (tp->version == RTL_VER_01)
1296 ether_addr_copy(dev->dev_addr, sa.sa_data);
1297 else
hayeswang179bb6d2014-09-04 16:15:42 +08001298 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001299
1300 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001301}
1302
hayeswangac718b62013-05-02 16:01:25 +00001303static void read_bulk_callback(struct urb *urb)
1304{
hayeswangac718b62013-05-02 16:01:25 +00001305 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001306 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001307 struct rx_agg *agg;
1308 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001309 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001310
hayeswangebc2ec482013-08-14 20:54:38 +08001311 agg = urb->context;
1312 if (!agg)
1313 return;
1314
1315 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001316 if (!tp)
1317 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001318
hayeswangac718b62013-05-02 16:01:25 +00001319 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1320 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001321
1322 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001323 return;
1324
hayeswangebc2ec482013-08-14 20:54:38 +08001325 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001326
1327 /* When link down, the driver would cancel all bulks. */
1328 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001329 if (!netif_carrier_ok(netdev))
1330 return;
1331
hayeswang9a4be1b2014-02-18 21:49:07 +08001332 usb_mark_last_busy(tp->udev);
1333
hayeswangac718b62013-05-02 16:01:25 +00001334 switch (status) {
1335 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001336 if (urb->actual_length < ETH_ZLEN)
1337 break;
1338
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001339 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001340 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001341 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001342 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001343 return;
hayeswangac718b62013-05-02 16:01:25 +00001344 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001345 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001346 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001347 return;
hayeswangac718b62013-05-02 16:01:25 +00001348 case -ENOENT:
1349 return; /* the urb is in unlink state */
1350 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001351 if (net_ratelimit())
1352 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001353 break;
hayeswangac718b62013-05-02 16:01:25 +00001354 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001355 if (net_ratelimit())
1356 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001357 break;
hayeswangac718b62013-05-02 16:01:25 +00001358 }
1359
hayeswanga0fccd42014-11-20 10:29:05 +08001360 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001361}
1362
1363static void write_bulk_callback(struct urb *urb)
1364{
hayeswangebc2ec482013-08-14 20:54:38 +08001365 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001366 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001367 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001368 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001369 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001370 int status = urb->status;
1371
hayeswangebc2ec482013-08-14 20:54:38 +08001372 agg = urb->context;
1373 if (!agg)
1374 return;
1375
1376 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001377 if (!tp)
1378 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001379
hayeswangd104eaf2014-03-06 15:07:17 +08001380 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001381 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001382 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001383 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001384 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001385 stats->tx_errors += agg->skb_num;
1386 } else {
1387 stats->tx_packets += agg->skb_num;
1388 stats->tx_bytes += agg->skb_len;
1389 }
1390
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001391 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001392 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001393 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001394
hayeswang9a4be1b2014-02-18 21:49:07 +08001395 usb_autopm_put_interface_async(tp->intf);
1396
hayeswangd104eaf2014-03-06 15:07:17 +08001397 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001398 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001399
1400 if (!test_bit(WORK_ENABLE, &tp->flags))
1401 return;
1402
1403 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1404 return;
1405
1406 if (!skb_queue_empty(&tp->tx_queue))
Hayes Wangd2187f82019-08-19 14:40:36 +08001407 tasklet_schedule(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08001408}
1409
hayeswang40a82912013-08-14 20:54:40 +08001410static void intr_callback(struct urb *urb)
1411{
1412 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001413 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001414 int status = urb->status;
1415 int res;
1416
1417 tp = urb->context;
1418 if (!tp)
1419 return;
1420
1421 if (!test_bit(WORK_ENABLE, &tp->flags))
1422 return;
1423
1424 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1425 return;
1426
1427 switch (status) {
1428 case 0: /* success */
1429 break;
1430 case -ECONNRESET: /* unlink */
1431 case -ESHUTDOWN:
1432 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001433 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001434 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001435 case -EPROTO:
1436 netif_info(tp, intr, tp->netdev,
1437 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001438 return;
1439 case -EOVERFLOW:
1440 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1441 goto resubmit;
1442 /* -EPIPE: should clear the halt */
1443 default:
1444 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1445 goto resubmit;
1446 }
1447
1448 d = urb->transfer_buffer;
1449 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001450 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001451 set_bit(RTL8152_LINK_CHG, &tp->flags);
1452 schedule_delayed_work(&tp->schedule, 0);
1453 }
1454 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001455 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001456 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001457 set_bit(RTL8152_LINK_CHG, &tp->flags);
1458 schedule_delayed_work(&tp->schedule, 0);
1459 }
1460 }
1461
1462resubmit:
1463 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001464 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001465 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001466 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001467 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001468 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001469 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001470 }
hayeswang40a82912013-08-14 20:54:40 +08001471}
1472
hayeswangebc2ec482013-08-14 20:54:38 +08001473static inline void *rx_agg_align(void *data)
1474{
hayeswang8e1f51b2014-01-02 11:22:41 +08001475 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001476}
1477
1478static inline void *tx_agg_align(void *data)
1479{
hayeswang8e1f51b2014-01-02 11:22:41 +08001480 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001481}
1482
Hayes Wang252df8b2019-08-13 11:42:06 +08001483static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1484{
1485 list_del(&agg->info_list);
1486
1487 usb_free_urb(agg->urb);
Hayes Wang47922fc2019-08-13 11:42:08 +08001488 put_page(agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001489 kfree(agg);
1490
1491 atomic_dec(&tp->rx_count);
1492}
1493
1494static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1495{
1496 struct net_device *netdev = tp->netdev;
1497 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
Hayes Wangd55d7082019-08-13 11:42:07 +08001498 unsigned int order = get_order(tp->rx_buf_sz);
Hayes Wang252df8b2019-08-13 11:42:06 +08001499 struct rx_agg *rx_agg;
1500 unsigned long flags;
Hayes Wang252df8b2019-08-13 11:42:06 +08001501
1502 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1503 if (!rx_agg)
1504 return NULL;
1505
Hayes Wang47922fc2019-08-13 11:42:08 +08001506 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
Hayes Wangd55d7082019-08-13 11:42:07 +08001507 if (!rx_agg->page)
Hayes Wang252df8b2019-08-13 11:42:06 +08001508 goto free_rx;
1509
Hayes Wangd55d7082019-08-13 11:42:07 +08001510 rx_agg->buffer = page_address(rx_agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001511
1512 rx_agg->urb = usb_alloc_urb(0, mflags);
1513 if (!rx_agg->urb)
1514 goto free_buf;
1515
1516 rx_agg->context = tp;
1517
1518 INIT_LIST_HEAD(&rx_agg->list);
1519 INIT_LIST_HEAD(&rx_agg->info_list);
1520 spin_lock_irqsave(&tp->rx_lock, flags);
1521 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1522 spin_unlock_irqrestore(&tp->rx_lock, flags);
1523
1524 atomic_inc(&tp->rx_count);
1525
1526 return rx_agg;
1527
1528free_buf:
Hayes Wangd55d7082019-08-13 11:42:07 +08001529 __free_pages(rx_agg->page, order);
Hayes Wang252df8b2019-08-13 11:42:06 +08001530free_rx:
1531 kfree(rx_agg);
1532 return NULL;
1533}
1534
hayeswangebc2ec482013-08-14 20:54:38 +08001535static void free_all_mem(struct r8152 *tp)
1536{
Hayes Wang252df8b2019-08-13 11:42:06 +08001537 struct rx_agg *agg, *agg_next;
1538 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001539 int i;
1540
Hayes Wang252df8b2019-08-13 11:42:06 +08001541 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001542
Hayes Wang252df8b2019-08-13 11:42:06 +08001543 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1544 free_rx_agg(tp, agg);
1545
1546 spin_unlock_irqrestore(&tp->rx_lock, flags);
1547
1548 WARN_ON(atomic_read(&tp->rx_count));
hayeswangebc2ec482013-08-14 20:54:38 +08001549
1550 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001551 usb_free_urb(tp->tx_info[i].urb);
1552 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001553
hayeswang9629e3c2014-01-15 10:42:15 +08001554 kfree(tp->tx_info[i].buffer);
1555 tp->tx_info[i].buffer = NULL;
1556 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001557 }
hayeswang40a82912013-08-14 20:54:40 +08001558
hayeswang9629e3c2014-01-15 10:42:15 +08001559 usb_free_urb(tp->intr_urb);
1560 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001561
hayeswang9629e3c2014-01-15 10:42:15 +08001562 kfree(tp->intr_buff);
1563 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001564}
1565
1566static int alloc_all_mem(struct r8152 *tp)
1567{
1568 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001569 struct usb_interface *intf = tp->intf;
1570 struct usb_host_interface *alt = intf->cur_altsetting;
1571 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001572 int node, i;
hayeswangebc2ec482013-08-14 20:54:38 +08001573
1574 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1575
1576 spin_lock_init(&tp->rx_lock);
1577 spin_lock_init(&tp->tx_lock);
Hayes Wang252df8b2019-08-13 11:42:06 +08001578 INIT_LIST_HEAD(&tp->rx_info);
hayeswangebc2ec482013-08-14 20:54:38 +08001579 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001580 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001581 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001582 skb_queue_head_init(&tp->rx_queue);
Hayes Wang252df8b2019-08-13 11:42:06 +08001583 atomic_set(&tp->rx_count, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001584
1585 for (i = 0; i < RTL8152_MAX_RX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001586 if (!alloc_rx_agg(tp, GFP_KERNEL))
hayeswangebc2ec482013-08-14 20:54:38 +08001587 goto err1;
hayeswangebc2ec482013-08-14 20:54:38 +08001588 }
1589
1590 for (i = 0; i < RTL8152_MAX_TX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001591 struct urb *urb;
1592 u8 *buf;
1593
hayeswang52aec122014-09-02 10:27:52 +08001594 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001595 if (!buf)
1596 goto err1;
1597
1598 if (buf != tx_agg_align(buf)) {
1599 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001600 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001601 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001602 if (!buf)
1603 goto err1;
1604 }
1605
1606 urb = usb_alloc_urb(0, GFP_KERNEL);
1607 if (!urb) {
1608 kfree(buf);
1609 goto err1;
1610 }
1611
1612 INIT_LIST_HEAD(&tp->tx_info[i].list);
1613 tp->tx_info[i].context = tp;
1614 tp->tx_info[i].urb = urb;
1615 tp->tx_info[i].buffer = buf;
1616 tp->tx_info[i].head = tx_agg_align(buf);
1617
1618 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1619 }
1620
hayeswang40a82912013-08-14 20:54:40 +08001621 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1622 if (!tp->intr_urb)
1623 goto err1;
1624
1625 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1626 if (!tp->intr_buff)
1627 goto err1;
1628
1629 tp->intr_interval = (int)ep_intr->desc.bInterval;
1630 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001631 tp->intr_buff, INTBUFSIZE, intr_callback,
1632 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001633
hayeswangebc2ec482013-08-14 20:54:38 +08001634 return 0;
1635
1636err1:
1637 free_all_mem(tp);
1638 return -ENOMEM;
1639}
1640
hayeswang0de98f62013-08-16 16:09:35 +08001641static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1642{
1643 struct tx_agg *agg = NULL;
1644 unsigned long flags;
1645
hayeswang21949ab2014-03-07 11:04:35 +08001646 if (list_empty(&tp->tx_free))
1647 return NULL;
1648
hayeswang0de98f62013-08-16 16:09:35 +08001649 spin_lock_irqsave(&tp->tx_lock, flags);
1650 if (!list_empty(&tp->tx_free)) {
1651 struct list_head *cursor;
1652
1653 cursor = tp->tx_free.next;
1654 list_del_init(cursor);
1655 agg = list_entry(cursor, struct tx_agg, list);
1656 }
1657 spin_unlock_irqrestore(&tp->tx_lock, flags);
1658
1659 return agg;
1660}
1661
hayeswangb209af92014-08-25 15:53:00 +08001662/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001663 * The hw limites the value the transport offset. When the offset is out of the
1664 * range, calculate the checksum by sw.
1665 */
1666static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1667 struct sk_buff_head *list)
1668{
1669 if (skb_shinfo(skb)->gso_size) {
1670 netdev_features_t features = tp->netdev->features;
1671 struct sk_buff_head seg_list;
1672 struct sk_buff *segs, *nskb;
1673
hayeswanga91d45f2014-07-11 16:48:27 +08001674 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001675 segs = skb_gso_segment(skb, features);
1676 if (IS_ERR(segs) || !segs)
1677 goto drop;
1678
1679 __skb_queue_head_init(&seg_list);
1680
1681 do {
1682 nskb = segs;
1683 segs = segs->next;
1684 nskb->next = NULL;
1685 __skb_queue_tail(&seg_list, nskb);
1686 } while (segs);
1687
1688 skb_queue_splice(&seg_list, list);
1689 dev_kfree_skb(skb);
1690 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1691 if (skb_checksum_help(skb) < 0)
1692 goto drop;
1693
1694 __skb_queue_head(list, skb);
1695 } else {
1696 struct net_device_stats *stats;
1697
1698drop:
1699 stats = &tp->netdev->stats;
1700 stats->tx_dropped++;
1701 dev_kfree_skb(skb);
1702 }
1703}
1704
hayeswangb209af92014-08-25 15:53:00 +08001705/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001706 * According to the document of microsoft, the TCP Pseudo Header excludes the
1707 * packet length for IPv6 TCP large packets.
1708 */
1709static int msdn_giant_send_check(struct sk_buff *skb)
1710{
1711 const struct ipv6hdr *ipv6h;
1712 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001713 int ret;
1714
1715 ret = skb_cow_head(skb, 0);
1716 if (ret)
1717 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001718
1719 ipv6h = ipv6_hdr(skb);
1720 th = tcp_hdr(skb);
1721
1722 th->check = 0;
1723 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1724
hayeswangfcb308d2014-03-11 10:20:32 +08001725 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001726}
1727
hayeswangc5554292014-09-12 10:43:11 +08001728static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1729{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001730 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001731 u32 opts2;
1732
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001733 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001734 desc->opts2 |= cpu_to_le32(opts2);
1735 }
1736}
1737
1738static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1739{
1740 u32 opts2 = le32_to_cpu(desc->opts2);
1741
1742 if (opts2 & RX_VLAN_TAG)
1743 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1744 swab16(opts2 & 0xffff));
1745}
1746
hayeswang60c89072014-03-07 11:04:39 +08001747static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1748 struct sk_buff *skb, u32 len, u32 transport_offset)
1749{
1750 u32 mss = skb_shinfo(skb)->gso_size;
1751 u32 opts1, opts2 = 0;
1752 int ret = TX_CSUM_SUCCESS;
1753
1754 WARN_ON_ONCE(len > TX_LEN_MAX);
1755
1756 opts1 = len | TX_FS | TX_LS;
1757
1758 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001759 if (transport_offset > GTTCPHO_MAX) {
1760 netif_warn(tp, tx_err, tp->netdev,
1761 "Invalid transport offset 0x%x for TSO\n",
1762 transport_offset);
1763 ret = TX_CSUM_TSO;
1764 goto unavailable;
1765 }
1766
hayeswang6e74d172015-02-06 11:30:50 +08001767 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001768 case htons(ETH_P_IP):
1769 opts1 |= GTSENDV4;
1770 break;
1771
hayeswang6128d1bb2014-03-07 11:04:40 +08001772 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001773 if (msdn_giant_send_check(skb)) {
1774 ret = TX_CSUM_TSO;
1775 goto unavailable;
1776 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001777 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001778 break;
1779
hayeswang60c89072014-03-07 11:04:39 +08001780 default:
1781 WARN_ON_ONCE(1);
1782 break;
1783 }
1784
1785 opts1 |= transport_offset << GTTCPHO_SHIFT;
1786 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1787 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001788 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001789
hayeswang6128d1bb2014-03-07 11:04:40 +08001790 if (transport_offset > TCPHO_MAX) {
1791 netif_warn(tp, tx_err, tp->netdev,
1792 "Invalid transport offset 0x%x\n",
1793 transport_offset);
1794 ret = TX_CSUM_NONE;
1795 goto unavailable;
1796 }
1797
hayeswang6e74d172015-02-06 11:30:50 +08001798 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001799 case htons(ETH_P_IP):
1800 opts2 |= IPV4_CS;
1801 ip_protocol = ip_hdr(skb)->protocol;
1802 break;
1803
1804 case htons(ETH_P_IPV6):
1805 opts2 |= IPV6_CS;
1806 ip_protocol = ipv6_hdr(skb)->nexthdr;
1807 break;
1808
1809 default:
1810 ip_protocol = IPPROTO_RAW;
1811 break;
1812 }
1813
hayeswang60c89072014-03-07 11:04:39 +08001814 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001815 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001816 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001817 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001818 else
hayeswang5bd23882013-08-14 20:54:39 +08001819 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001820
hayeswang60c89072014-03-07 11:04:39 +08001821 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001822 }
hayeswang60c89072014-03-07 11:04:39 +08001823
1824 desc->opts2 = cpu_to_le32(opts2);
1825 desc->opts1 = cpu_to_le32(opts1);
1826
hayeswang6128d1bb2014-03-07 11:04:40 +08001827unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001828 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001829}
1830
hayeswangb1379d92013-08-16 16:09:37 +08001831static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1832{
hayeswangd84130a2014-02-18 21:49:02 +08001833 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001834 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001835 u8 *tx_data;
1836
hayeswangd84130a2014-02-18 21:49:02 +08001837 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001838 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001839 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001840 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001841
hayeswangb1379d92013-08-16 16:09:37 +08001842 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001843 agg->skb_num = 0;
1844 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001845 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001846
hayeswang7937f9e2013-11-20 17:30:54 +08001847 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001848 struct tx_desc *tx_desc;
1849 struct sk_buff *skb;
1850 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001851 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001852
hayeswangd84130a2014-02-18 21:49:02 +08001853 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001854 if (!skb)
1855 break;
1856
hayeswang60c89072014-03-07 11:04:39 +08001857 len = skb->len + sizeof(*tx_desc);
1858
1859 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001860 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001861 break;
1862 }
1863
hayeswang7937f9e2013-11-20 17:30:54 +08001864 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001865 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001866
1867 offset = (u32)skb_transport_offset(skb);
1868
hayeswang6128d1bb2014-03-07 11:04:40 +08001869 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1870 r8152_csum_workaround(tp, skb, &skb_head);
1871 continue;
1872 }
hayeswang60c89072014-03-07 11:04:39 +08001873
hayeswangc5554292014-09-12 10:43:11 +08001874 rtl_tx_vlan_tag(tx_desc, skb);
1875
hayeswangb1379d92013-08-16 16:09:37 +08001876 tx_data += sizeof(*tx_desc);
1877
hayeswang60c89072014-03-07 11:04:39 +08001878 len = skb->len;
1879 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1880 struct net_device_stats *stats = &tp->netdev->stats;
1881
1882 stats->tx_dropped++;
1883 dev_kfree_skb_any(skb);
1884 tx_data -= sizeof(*tx_desc);
1885 continue;
1886 }
hayeswangb1379d92013-08-16 16:09:37 +08001887
hayeswang7937f9e2013-11-20 17:30:54 +08001888 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001889 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001890 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001891
1892 dev_kfree_skb_any(skb);
1893
hayeswang52aec122014-09-02 10:27:52 +08001894 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001895
1896 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1897 break;
hayeswangb1379d92013-08-16 16:09:37 +08001898 }
1899
hayeswangd84130a2014-02-18 21:49:02 +08001900 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001901 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001902 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001903 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001904 }
1905
hayeswang0c3121f2014-03-07 11:04:36 +08001906 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001907
1908 if (netif_queue_stopped(tp->netdev) &&
1909 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1910 netif_wake_queue(tp->netdev);
1911
hayeswang0c3121f2014-03-07 11:04:36 +08001912 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001913
hayeswang0c3121f2014-03-07 11:04:36 +08001914 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001915 if (ret < 0)
1916 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001917
hayeswangb1379d92013-08-16 16:09:37 +08001918 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1919 agg->head, (int)(tx_data - (u8 *)agg->head),
1920 (usb_complete_t)write_bulk_callback, agg);
1921
hayeswang0c3121f2014-03-07 11:04:36 +08001922 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001923 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001924 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001925
1926out_tx_fill:
1927 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001928}
1929
hayeswang565cab02014-03-07 11:04:38 +08001930static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1931{
1932 u8 checksum = CHECKSUM_NONE;
1933 u32 opts2, opts3;
1934
hayeswang19c0f402017-01-11 16:25:34 +08001935 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001936 goto return_result;
1937
1938 opts2 = le32_to_cpu(rx_desc->opts2);
1939 opts3 = le32_to_cpu(rx_desc->opts3);
1940
1941 if (opts2 & RD_IPV4_CS) {
1942 if (opts3 & IPF)
1943 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001944 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1945 checksum = CHECKSUM_UNNECESSARY;
1946 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001947 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001948 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001949 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1950 checksum = CHECKSUM_UNNECESSARY;
1951 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1952 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001953 }
1954
1955return_result:
1956 return checksum;
1957}
1958
Hayes Wang47922fc2019-08-13 11:42:08 +08001959static inline bool rx_count_exceed(struct r8152 *tp)
1960{
1961 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
1962}
1963
1964static inline int agg_offset(struct rx_agg *agg, void *addr)
1965{
1966 return (int)(addr - agg->buffer);
1967}
1968
1969static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
1970{
1971 struct rx_agg *agg, *agg_next, *agg_free = NULL;
1972 unsigned long flags;
1973
1974 spin_lock_irqsave(&tp->rx_lock, flags);
1975
1976 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
1977 if (page_count(agg->page) == 1) {
1978 if (!agg_free) {
1979 list_del_init(&agg->list);
1980 agg_free = agg;
1981 continue;
1982 }
1983 if (rx_count_exceed(tp)) {
1984 list_del_init(&agg->list);
1985 free_rx_agg(tp, agg);
1986 }
1987 break;
1988 }
1989 }
1990
1991 spin_unlock_irqrestore(&tp->rx_lock, flags);
1992
Hayes Wange4a50172019-08-13 11:42:09 +08001993 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
Hayes Wang47922fc2019-08-13 11:42:08 +08001994 agg_free = alloc_rx_agg(tp, mflags);
1995
1996 return agg_free;
1997}
1998
hayeswangd823ab62015-01-12 12:06:23 +08001999static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08002000{
hayeswanga5a4f462013-08-16 16:09:34 +08002001 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08002002 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08002003 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08002004 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08002005
2006 if (!skb_queue_empty(&tp->rx_queue)) {
2007 while (work_done < budget) {
2008 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2009 struct net_device *netdev = tp->netdev;
2010 struct net_device_stats *stats = &netdev->stats;
2011 unsigned int pkt_len;
2012
2013 if (!skb)
2014 break;
2015
2016 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08002017 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002018 work_done++;
2019 stats->rx_packets++;
2020 stats->rx_bytes += pkt_len;
2021 }
2022 }
hayeswangebc2ec482013-08-14 20:54:38 +08002023
hayeswangd84130a2014-02-18 21:49:02 +08002024 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08002025 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08002026
2027 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08002028 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08002029 list_splice_init(&tp->rx_done, &rx_queue);
2030 spin_unlock_irqrestore(&tp->rx_lock, flags);
2031
2032 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08002033 struct rx_desc *rx_desc;
Hayes Wang47922fc2019-08-13 11:42:08 +08002034 struct rx_agg *agg, *agg_free;
hayeswang43a44782013-08-16 16:09:36 +08002035 int len_used = 0;
2036 struct urb *urb;
2037 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08002038
hayeswangebc2ec482013-08-14 20:54:38 +08002039 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08002040
2041 agg = list_entry(cursor, struct rx_agg, list);
2042 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08002043 if (urb->actual_length < ETH_ZLEN)
2044 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08002045
Hayes Wang47922fc2019-08-13 11:42:08 +08002046 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2047
Hayes Wangd55d7082019-08-13 11:42:07 +08002048 rx_desc = agg->buffer;
2049 rx_data = agg->buffer;
hayeswang7937f9e2013-11-20 17:30:54 +08002050 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002051
hayeswang7937f9e2013-11-20 17:30:54 +08002052 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08002053 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08002054 struct net_device_stats *stats = &netdev->stats;
Hayes Wang47922fc2019-08-13 11:42:08 +08002055 unsigned int pkt_len, rx_frag_head_sz;
hayeswang43a44782013-08-16 16:09:36 +08002056 struct sk_buff *skb;
2057
hayeswang74544452017-06-09 17:11:47 +08002058 /* limite the skb numbers for rx_queue */
2059 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2060 break;
2061
hayeswang7937f9e2013-11-20 17:30:54 +08002062 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08002063 if (pkt_len < ETH_ZLEN)
2064 break;
2065
hayeswang7937f9e2013-11-20 17:30:54 +08002066 len_used += pkt_len;
2067 if (urb->actual_length < len_used)
2068 break;
2069
hayeswangb65c0c92017-06-21 11:25:18 +08002070 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08002071 rx_data += sizeof(struct rx_desc);
2072
Hayes Wange4a50172019-08-13 11:42:09 +08002073 if (!agg_free || tp->rx_copybreak > pkt_len)
Hayes Wang47922fc2019-08-13 11:42:08 +08002074 rx_frag_head_sz = pkt_len;
2075 else
Hayes Wange4a50172019-08-13 11:42:09 +08002076 rx_frag_head_sz = tp->rx_copybreak;
Hayes Wang47922fc2019-08-13 11:42:08 +08002077
2078 skb = napi_alloc_skb(napi, rx_frag_head_sz);
hayeswangebc2ec482013-08-14 20:54:38 +08002079 if (!skb) {
2080 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08002081 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08002082 }
hayeswang565cab02014-03-07 11:04:38 +08002083
2084 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
Hayes Wang47922fc2019-08-13 11:42:08 +08002085 memcpy(skb->data, rx_data, rx_frag_head_sz);
2086 skb_put(skb, rx_frag_head_sz);
2087 pkt_len -= rx_frag_head_sz;
2088 rx_data += rx_frag_head_sz;
2089 if (pkt_len) {
2090 skb_add_rx_frag(skb, 0, agg->page,
2091 agg_offset(agg, rx_data),
2092 pkt_len,
2093 SKB_DATA_ALIGN(pkt_len));
2094 get_page(agg->page);
2095 }
2096
hayeswangebc2ec482013-08-14 20:54:38 +08002097 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08002098 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002099 if (work_done < budget) {
hayeswangd823ab62015-01-12 12:06:23 +08002100 work_done++;
2101 stats->rx_packets++;
Hayes Wang47922fc2019-08-13 11:42:08 +08002102 stats->rx_bytes += skb->len;
Hayes Wang6636fb32019-08-19 11:15:19 +08002103 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002104 } else {
2105 __skb_queue_tail(&tp->rx_queue, skb);
2106 }
hayeswangebc2ec482013-08-14 20:54:38 +08002107
hayeswang5e2f7482014-03-07 11:04:37 +08002108find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002109 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002110 rx_desc = (struct rx_desc *)rx_data;
Hayes Wang47922fc2019-08-13 11:42:08 +08002111 len_used = agg_offset(agg, rx_data);
hayeswang7937f9e2013-11-20 17:30:54 +08002112 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002113 }
2114
Hayes Wang47922fc2019-08-13 11:42:08 +08002115 WARN_ON(!agg_free && page_count(agg->page) > 1);
2116
2117 if (agg_free) {
2118 spin_lock_irqsave(&tp->rx_lock, flags);
2119 if (page_count(agg->page) == 1) {
2120 list_add(&agg_free->list, &tp->rx_used);
2121 } else {
2122 list_add_tail(&agg->list, &tp->rx_used);
2123 agg = agg_free;
2124 urb = agg->urb;
2125 }
2126 spin_unlock_irqrestore(&tp->rx_lock, flags);
2127 }
2128
hayeswang0de98f62013-08-16 16:09:35 +08002129submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002130 if (!ret) {
2131 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2132 } else {
2133 urb->actual_length = 0;
2134 list_add_tail(&agg->list, next);
2135 }
2136 }
2137
2138 if (!list_empty(&rx_queue)) {
2139 spin_lock_irqsave(&tp->rx_lock, flags);
2140 list_splice_tail(&rx_queue, &tp->rx_done);
2141 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002142 }
hayeswangd823ab62015-01-12 12:06:23 +08002143
2144out1:
2145 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002146}
2147
2148static void tx_bottom(struct r8152 *tp)
2149{
hayeswangebc2ec482013-08-14 20:54:38 +08002150 int res;
2151
hayeswangb1379d92013-08-16 16:09:37 +08002152 do {
2153 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002154
hayeswangb1379d92013-08-16 16:09:37 +08002155 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002156 break;
2157
hayeswangb1379d92013-08-16 16:09:37 +08002158 agg = r8152_get_tx_agg(tp);
2159 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002160 break;
hayeswangb1379d92013-08-16 16:09:37 +08002161
2162 res = r8152_tx_agg_fill(tp, agg);
2163 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002164 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002165
2166 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002167 rtl_set_unplug(tp);
hayeswangb1379d92013-08-16 16:09:37 +08002168 netif_device_detach(netdev);
2169 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002170 struct net_device_stats *stats = &netdev->stats;
2171 unsigned long flags;
2172
hayeswangb1379d92013-08-16 16:09:37 +08002173 netif_warn(tp, tx_err, netdev,
2174 "failed tx_urb %d\n", res);
2175 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002176
hayeswangb1379d92013-08-16 16:09:37 +08002177 spin_lock_irqsave(&tp->tx_lock, flags);
2178 list_add_tail(&agg->list, &tp->tx_free);
2179 spin_unlock_irqrestore(&tp->tx_lock, flags);
2180 }
hayeswangebc2ec482013-08-14 20:54:38 +08002181 }
hayeswangb1379d92013-08-16 16:09:37 +08002182 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002183}
2184
Hayes Wangd2187f82019-08-19 14:40:36 +08002185static void bottom_half(unsigned long data)
hayeswangebc2ec482013-08-14 20:54:38 +08002186{
Hayes Wangd2187f82019-08-19 14:40:36 +08002187 struct r8152 *tp;
2188
2189 tp = (struct r8152 *)data;
2190
hayeswangebc2ec482013-08-14 20:54:38 +08002191 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2192 return;
2193
2194 if (!test_bit(WORK_ENABLE, &tp->flags))
2195 return;
2196
hayeswang7559fb2f2013-08-16 16:09:38 +08002197 /* When link down, the driver would cancel all bulks. */
2198 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002199 if (!netif_carrier_ok(tp->netdev))
2200 return;
2201
Hayes Wangd2187f82019-08-19 14:40:36 +08002202 clear_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002203
hayeswang0c3121f2014-03-07 11:04:36 +08002204 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002205}
2206
hayeswangd823ab62015-01-12 12:06:23 +08002207static int r8152_poll(struct napi_struct *napi, int budget)
2208{
2209 struct r8152 *tp = container_of(napi, struct r8152, napi);
2210 int work_done;
2211
2212 work_done = rx_bottom(tp, budget);
hayeswangd823ab62015-01-12 12:06:23 +08002213
2214 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002215 if (!napi_complete_done(napi, work_done))
2216 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002217 if (!list_empty(&tp->rx_done))
2218 napi_schedule(napi);
2219 }
2220
hayeswanga3307f92017-06-09 17:11:48 +08002221out:
hayeswangd823ab62015-01-12 12:06:23 +08002222 return work_done;
2223}
2224
hayeswangebc2ec482013-08-14 20:54:38 +08002225static
2226int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2227{
hayeswanga0fccd42014-11-20 10:29:05 +08002228 int ret;
2229
hayeswangef827a52015-01-09 10:26:36 +08002230 /* The rx would be stopped, so skip submitting */
2231 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2232 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2233 return 0;
2234
hayeswangebc2ec482013-08-14 20:54:38 +08002235 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
Hayes Wangd55d7082019-08-13 11:42:07 +08002236 agg->buffer, tp->rx_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002237 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002238
hayeswanga0fccd42014-11-20 10:29:05 +08002239 ret = usb_submit_urb(agg->urb, mem_flags);
2240 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002241 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002242 netif_device_detach(tp->netdev);
2243 } else if (ret) {
2244 struct urb *urb = agg->urb;
2245 unsigned long flags;
2246
2247 urb->actual_length = 0;
2248 spin_lock_irqsave(&tp->rx_lock, flags);
2249 list_add_tail(&agg->list, &tp->rx_done);
2250 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002251
2252 netif_err(tp, rx_err, tp->netdev,
2253 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2254
2255 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002256 }
2257
2258 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002259}
2260
hayeswang00a5e362014-02-18 21:48:59 +08002261static void rtl_drop_queued_tx(struct r8152 *tp)
2262{
2263 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002264 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002265 struct sk_buff *skb;
2266
hayeswangd84130a2014-02-18 21:49:02 +08002267 if (skb_queue_empty(tx_queue))
2268 return;
2269
2270 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002271 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002272 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002273 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002274
2275 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002276 dev_kfree_skb(skb);
2277 stats->tx_dropped++;
2278 }
2279}
2280
hayeswangac718b62013-05-02 16:01:25 +00002281static void rtl8152_tx_timeout(struct net_device *netdev)
2282{
2283 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002284
Hayes Wang4a8deae2014-01-07 11:18:22 +08002285 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002286
2287 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002288}
2289
2290static void rtl8152_set_rx_mode(struct net_device *netdev)
2291{
2292 struct r8152 *tp = netdev_priv(netdev);
2293
hayeswang51d979f2015-02-06 11:30:47 +08002294 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002295 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002296 schedule_delayed_work(&tp->schedule, 0);
2297 }
hayeswangac718b62013-05-02 16:01:25 +00002298}
2299
2300static void _rtl8152_set_rx_mode(struct net_device *netdev)
2301{
2302 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002303 u32 mc_filter[2]; /* Multicast hash filter */
2304 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002305 u32 ocp_data;
2306
hayeswangac718b62013-05-02 16:01:25 +00002307 netif_stop_queue(netdev);
2308 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2309 ocp_data &= ~RCR_ACPT_ALL;
2310 ocp_data |= RCR_AB | RCR_APM;
2311
2312 if (netdev->flags & IFF_PROMISC) {
2313 /* Unconditionally log net taps. */
2314 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2315 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002316 mc_filter[1] = 0xffffffff;
2317 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002318 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2319 (netdev->flags & IFF_ALLMULTI)) {
2320 /* Too many to filter perfectly -- accept all multicasts. */
2321 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002322 mc_filter[1] = 0xffffffff;
2323 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002324 } else {
2325 struct netdev_hw_addr *ha;
2326
hayeswangb209af92014-08-25 15:53:00 +08002327 mc_filter[1] = 0;
2328 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002329 netdev_for_each_mc_addr(ha, netdev) {
2330 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002331
hayeswangac718b62013-05-02 16:01:25 +00002332 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2333 ocp_data |= RCR_AM;
2334 }
2335 }
2336
hayeswang31787f52013-07-31 17:21:25 +08002337 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2338 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002339
hayeswang31787f52013-07-31 17:21:25 +08002340 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002341 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2342 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002343}
2344
hayeswanga5e31252015-01-06 17:41:58 +08002345static netdev_features_t
2346rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2347 netdev_features_t features)
2348{
2349 u32 mss = skb_shinfo(skb)->gso_size;
2350 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2351 int offset = skb_transport_offset(skb);
2352
2353 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002354 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002355 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2356 features &= ~NETIF_F_GSO_MASK;
2357
2358 return features;
2359}
2360
hayeswangac718b62013-05-02 16:01:25 +00002361static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002362 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002363{
2364 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002365
hayeswangac718b62013-05-02 16:01:25 +00002366 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002367
hayeswang61598782013-11-20 17:30:55 +08002368 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002369
hayeswang0c3121f2014-03-07 11:04:36 +08002370 if (!list_empty(&tp->tx_free)) {
2371 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
Hayes Wangd2187f82019-08-19 14:40:36 +08002372 set_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002373 schedule_delayed_work(&tp->schedule, 0);
2374 } else {
2375 usb_mark_last_busy(tp->udev);
Hayes Wangd2187f82019-08-19 14:40:36 +08002376 tasklet_schedule(&tp->tx_tl);
hayeswang0c3121f2014-03-07 11:04:36 +08002377 }
hayeswangb209af92014-08-25 15:53:00 +08002378 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002379 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002380 }
hayeswangdd1b1192013-11-20 17:30:56 +08002381
hayeswangac718b62013-05-02 16:01:25 +00002382 return NETDEV_TX_OK;
2383}
2384
2385static void r8152b_reset_packet_filter(struct r8152 *tp)
2386{
2387 u32 ocp_data;
2388
2389 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2390 ocp_data &= ~FMC_FCR_MCU_EN;
2391 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2392 ocp_data |= FMC_FCR_MCU_EN;
2393 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2394}
2395
2396static void rtl8152_nic_reset(struct r8152 *tp)
2397{
2398 int i;
2399
2400 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2401
2402 for (i = 0; i < 1000; i++) {
2403 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2404 break;
hayeswangb209af92014-08-25 15:53:00 +08002405 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002406 }
2407}
2408
hayeswangdd1b1192013-11-20 17:30:56 +08002409static void set_tx_qlen(struct r8152 *tp)
2410{
2411 struct net_device *netdev = tp->netdev;
2412
hayeswangb65c0c92017-06-21 11:25:18 +08002413 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002414 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002415}
2416
hayeswangac718b62013-05-02 16:01:25 +00002417static inline u8 rtl8152_get_speed(struct r8152 *tp)
2418{
2419 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2420}
2421
hayeswang507605a2014-01-02 11:22:43 +08002422static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002423{
hayeswangebc2ec482013-08-14 20:54:38 +08002424 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002425 u8 speed;
2426
2427 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002428 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002429 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002430 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002431 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2432 } else {
2433 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002434 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002435 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2436 }
hayeswang507605a2014-01-02 11:22:43 +08002437}
2438
hayeswang00a5e362014-02-18 21:48:59 +08002439static void rxdy_gated_en(struct r8152 *tp, bool enable)
2440{
2441 u32 ocp_data;
2442
2443 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2444 if (enable)
2445 ocp_data |= RXDY_GATED_EN;
2446 else
2447 ocp_data &= ~RXDY_GATED_EN;
2448 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2449}
2450
hayeswang445f7f42014-09-23 16:31:47 +08002451static int rtl_start_rx(struct r8152 *tp)
2452{
Hayes Wang252df8b2019-08-13 11:42:06 +08002453 struct rx_agg *agg, *agg_next;
2454 struct list_head tmp_list;
2455 unsigned long flags;
Hayes Wang47922fc2019-08-13 11:42:08 +08002456 int ret = 0, i = 0;
Hayes Wang252df8b2019-08-13 11:42:06 +08002457
2458 INIT_LIST_HEAD(&tmp_list);
2459
2460 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002461
2462 INIT_LIST_HEAD(&tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002463 INIT_LIST_HEAD(&tp->rx_used);
Hayes Wang252df8b2019-08-13 11:42:06 +08002464
2465 list_splice_init(&tp->rx_info, &tmp_list);
2466
2467 spin_unlock_irqrestore(&tp->rx_lock, flags);
2468
2469 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2470 INIT_LIST_HEAD(&agg->list);
2471
Hayes Wang47922fc2019-08-13 11:42:08 +08002472 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2473 if (++i > RTL8152_MAX_RX) {
2474 spin_lock_irqsave(&tp->rx_lock, flags);
2475 list_add_tail(&agg->list, &tp->rx_used);
2476 spin_unlock_irqrestore(&tp->rx_lock, flags);
2477 } else if (unlikely(ret < 0)) {
2478 spin_lock_irqsave(&tp->rx_lock, flags);
Hayes Wang252df8b2019-08-13 11:42:06 +08002479 list_add_tail(&agg->list, &tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002480 spin_unlock_irqrestore(&tp->rx_lock, flags);
2481 } else {
Hayes Wang252df8b2019-08-13 11:42:06 +08002482 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
Hayes Wang47922fc2019-08-13 11:42:08 +08002483 }
hayeswang445f7f42014-09-23 16:31:47 +08002484 }
2485
Hayes Wang252df8b2019-08-13 11:42:06 +08002486 spin_lock_irqsave(&tp->rx_lock, flags);
2487 WARN_ON(!list_empty(&tp->rx_info));
2488 list_splice(&tmp_list, &tp->rx_info);
2489 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang7bcf4f62014-11-20 10:29:06 +08002490
hayeswang445f7f42014-09-23 16:31:47 +08002491 return ret;
2492}
2493
2494static int rtl_stop_rx(struct r8152 *tp)
2495{
Hayes Wang252df8b2019-08-13 11:42:06 +08002496 struct rx_agg *agg, *agg_next;
2497 struct list_head tmp_list;
2498 unsigned long flags;
hayeswang445f7f42014-09-23 16:31:47 +08002499
Hayes Wang252df8b2019-08-13 11:42:06 +08002500 INIT_LIST_HEAD(&tmp_list);
2501
2502 /* The usb_kill_urb() couldn't be used in atomic.
2503 * Therefore, move the list of rx_info to a tmp one.
2504 * Then, list_for_each_entry_safe could be used without
2505 * spin lock.
2506 */
2507
2508 spin_lock_irqsave(&tp->rx_lock, flags);
2509 list_splice_init(&tp->rx_info, &tmp_list);
2510 spin_unlock_irqrestore(&tp->rx_lock, flags);
2511
Hayes Wang47922fc2019-08-13 11:42:08 +08002512 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2513 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2514 * equal to 1, so the other ones could be freed safely.
2515 */
2516 if (page_count(agg->page) > 1)
2517 free_rx_agg(tp, agg);
2518 else
2519 usb_kill_urb(agg->urb);
2520 }
Hayes Wang252df8b2019-08-13 11:42:06 +08002521
2522 /* Move back the list of temp to the rx_info */
2523 spin_lock_irqsave(&tp->rx_lock, flags);
2524 WARN_ON(!list_empty(&tp->rx_info));
2525 list_splice(&tmp_list, &tp->rx_info);
2526 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002527
hayeswangd823ab62015-01-12 12:06:23 +08002528 while (!skb_queue_empty(&tp->rx_queue))
2529 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2530
hayeswang445f7f42014-09-23 16:31:47 +08002531 return 0;
2532}
2533
Hayes Wang9fae5412019-07-03 15:11:56 +08002534static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2535{
2536 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2537 OWN_UPDATE | OWN_CLEAR);
2538}
2539
hayeswang507605a2014-01-02 11:22:43 +08002540static int rtl_enable(struct r8152 *tp)
2541{
2542 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002543
2544 r8152b_reset_packet_filter(tp);
2545
2546 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2547 ocp_data |= CR_RE | CR_TE;
2548 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2549
Hayes Wang9fae5412019-07-03 15:11:56 +08002550 switch (tp->version) {
2551 case RTL_VER_08:
2552 case RTL_VER_09:
2553 r8153b_rx_agg_chg_indicate(tp);
2554 break;
2555 default:
2556 break;
2557 }
2558
hayeswang00a5e362014-02-18 21:48:59 +08002559 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002560
hayeswangaa2e0922015-01-09 10:26:35 +08002561 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002562}
2563
hayeswang507605a2014-01-02 11:22:43 +08002564static int rtl8152_enable(struct r8152 *tp)
2565{
hayeswang68714382014-04-11 17:54:31 +08002566 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2567 return -ENODEV;
2568
hayeswang507605a2014-01-02 11:22:43 +08002569 set_tx_qlen(tp);
2570 rtl_set_eee_plus(tp);
2571
2572 return rtl_enable(tp);
2573}
2574
hayeswang464ec102015-02-12 14:33:46 +08002575static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002576{
hayeswang464ec102015-02-12 14:33:46 +08002577 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002578
hayeswang65b82d62017-06-15 14:44:03 +08002579 switch (tp->version) {
2580 case RTL_VER_03:
2581 case RTL_VER_04:
2582 case RTL_VER_05:
2583 case RTL_VER_06:
2584 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2585 ocp_data);
2586 break;
2587
2588 case RTL_VER_08:
2589 case RTL_VER_09:
2590 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2591 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2592 */
2593 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2594 128 / 8);
2595 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2596 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002597 break;
2598
2599 default:
2600 break;
2601 }
hayeswang464ec102015-02-12 14:33:46 +08002602}
2603
2604static void r8153_set_rx_early_size(struct r8152 *tp)
2605{
Hayes Wangec5791c2019-08-13 11:42:05 +08002606 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002607
hayeswang65b82d62017-06-15 14:44:03 +08002608 switch (tp->version) {
2609 case RTL_VER_03:
2610 case RTL_VER_04:
2611 case RTL_VER_05:
2612 case RTL_VER_06:
2613 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2614 ocp_data / 4);
2615 break;
2616 case RTL_VER_08:
2617 case RTL_VER_09:
2618 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2619 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002620 break;
2621 default:
2622 WARN_ON_ONCE(1);
2623 break;
2624 }
hayeswang43779f82014-01-02 11:25:10 +08002625}
2626
2627static int rtl8153_enable(struct r8152 *tp)
2628{
hayeswang68714382014-04-11 17:54:31 +08002629 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2630 return -ENODEV;
2631
hayeswang43779f82014-01-02 11:25:10 +08002632 set_tx_qlen(tp);
2633 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002634 r8153_set_rx_early_timeout(tp);
2635 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002636
2637 return rtl_enable(tp);
2638}
2639
hayeswangd70b1132014-09-19 15:17:18 +08002640static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002641{
hayeswangebc2ec482013-08-14 20:54:38 +08002642 u32 ocp_data;
2643 int i;
hayeswangac718b62013-05-02 16:01:25 +00002644
hayeswang68714382014-04-11 17:54:31 +08002645 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2646 rtl_drop_queued_tx(tp);
2647 return;
2648 }
2649
hayeswangac718b62013-05-02 16:01:25 +00002650 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2651 ocp_data &= ~RCR_ACPT_ALL;
2652 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2653
hayeswang00a5e362014-02-18 21:48:59 +08002654 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002655
2656 for (i = 0; i < RTL8152_MAX_TX; i++)
2657 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002658
hayeswang00a5e362014-02-18 21:48:59 +08002659 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002660
2661 for (i = 0; i < 1000; i++) {
2662 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2663 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2664 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002665 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002666 }
2667
2668 for (i = 0; i < 1000; i++) {
2669 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2670 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002671 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002672 }
2673
hayeswang445f7f42014-09-23 16:31:47 +08002674 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002675
2676 rtl8152_nic_reset(tp);
2677}
2678
hayeswang00a5e362014-02-18 21:48:59 +08002679static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2680{
2681 u32 ocp_data;
2682
2683 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2684 if (enable)
2685 ocp_data |= POWER_CUT;
2686 else
2687 ocp_data &= ~POWER_CUT;
2688 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2689
2690 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2691 ocp_data &= ~RESUME_INDICATE;
2692 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002693}
2694
hayeswangc5554292014-09-12 10:43:11 +08002695static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2696{
2697 u32 ocp_data;
2698
2699 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2700 if (enable)
2701 ocp_data |= CPCR_RX_VLAN;
2702 else
2703 ocp_data &= ~CPCR_RX_VLAN;
2704 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2705}
2706
2707static int rtl8152_set_features(struct net_device *dev,
2708 netdev_features_t features)
2709{
2710 netdev_features_t changed = features ^ dev->features;
2711 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002712 int ret;
2713
2714 ret = usb_autopm_get_interface(tp->intf);
2715 if (ret < 0)
2716 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002717
hayeswangb5403272014-10-09 18:00:26 +08002718 mutex_lock(&tp->control);
2719
hayeswangc5554292014-09-12 10:43:11 +08002720 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2721 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2722 rtl_rx_vlan_en(tp, true);
2723 else
2724 rtl_rx_vlan_en(tp, false);
2725 }
2726
hayeswangb5403272014-10-09 18:00:26 +08002727 mutex_unlock(&tp->control);
2728
hayeswang405f8a02014-10-09 18:00:24 +08002729 usb_autopm_put_interface(tp->intf);
2730
2731out:
2732 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002733}
2734
hayeswang21ff2e82014-02-18 21:49:06 +08002735#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2736
2737static u32 __rtl_get_wol(struct r8152 *tp)
2738{
2739 u32 ocp_data;
2740 u32 wolopts = 0;
2741
hayeswang21ff2e82014-02-18 21:49:06 +08002742 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2743 if (ocp_data & LINK_ON_WAKE_EN)
2744 wolopts |= WAKE_PHY;
2745
2746 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2747 if (ocp_data & UWF_EN)
2748 wolopts |= WAKE_UCAST;
2749 if (ocp_data & BWF_EN)
2750 wolopts |= WAKE_BCAST;
2751 if (ocp_data & MWF_EN)
2752 wolopts |= WAKE_MCAST;
2753
2754 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2755 if (ocp_data & MAGIC_EN)
2756 wolopts |= WAKE_MAGIC;
2757
2758 return wolopts;
2759}
2760
2761static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2762{
2763 u32 ocp_data;
2764
2765 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2766
2767 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2768 ocp_data &= ~LINK_ON_WAKE_EN;
2769 if (wolopts & WAKE_PHY)
2770 ocp_data |= LINK_ON_WAKE_EN;
2771 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2772
2773 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002774 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002775 if (wolopts & WAKE_UCAST)
2776 ocp_data |= UWF_EN;
2777 if (wolopts & WAKE_BCAST)
2778 ocp_data |= BWF_EN;
2779 if (wolopts & WAKE_MCAST)
2780 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002781 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2782
2783 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2784
2785 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2786 ocp_data &= ~MAGIC_EN;
2787 if (wolopts & WAKE_MAGIC)
2788 ocp_data |= MAGIC_EN;
2789 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2790
2791 if (wolopts & WAKE_ANY)
2792 device_set_wakeup_enable(&tp->udev->dev, true);
2793 else
2794 device_set_wakeup_enable(&tp->udev->dev, false);
2795}
2796
hayeswang134f98b2017-06-09 17:11:40 +08002797static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2798{
2799 /* MAC clock speed down */
2800 if (enable) {
2801 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2802 ALDPS_SPDWN_RATIO);
2803 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2804 EEE_SPDWN_RATIO);
2805 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2806 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2807 U1U2_SPDWN_EN | L1_SPDWN_EN);
2808 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2809 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2810 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2811 TP1000_SPDWN_EN);
2812 } else {
2813 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2814 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2815 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2816 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2817 }
2818}
2819
hayeswangb2143962015-07-24 13:54:23 +08002820static void r8153_u1u2en(struct r8152 *tp, bool enable)
2821{
2822 u8 u1u2[8];
2823
2824 if (enable)
2825 memset(u1u2, 0xff, sizeof(u1u2));
2826 else
2827 memset(u1u2, 0x00, sizeof(u1u2));
2828
2829 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2830}
2831
hayeswang65b82d62017-06-15 14:44:03 +08002832static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2833{
2834 u32 ocp_data;
2835
2836 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2837 if (enable)
2838 ocp_data |= LPM_U1U2_EN;
2839 else
2840 ocp_data &= ~LPM_U1U2_EN;
2841
2842 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2843}
2844
hayeswangb2143962015-07-24 13:54:23 +08002845static void r8153_u2p3en(struct r8152 *tp, bool enable)
2846{
2847 u32 ocp_data;
2848
2849 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002850 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002851 ocp_data |= U2P3_ENABLE;
2852 else
2853 ocp_data &= ~U2P3_ENABLE;
2854 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2855}
2856
hayeswang65b82d62017-06-15 14:44:03 +08002857static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2858{
2859 u32 ocp_data;
2860
2861 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2862 ocp_data &= ~clear;
2863 ocp_data |= set;
2864 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2865}
2866
2867static void r8153b_green_en(struct r8152 *tp, bool enable)
2868{
2869 u16 data;
2870
2871 if (enable) {
2872 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2873 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2874 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2875 } else {
2876 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2877 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2878 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2879 }
2880
2881 data = sram_read(tp, SRAM_GREEN_CFG);
2882 data |= GREEN_ETH_EN;
2883 sram_write(tp, SRAM_GREEN_CFG, data);
2884
2885 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2886}
2887
hayeswangc564b872017-06-09 17:11:38 +08002888static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2889{
2890 u16 data;
2891 int i;
2892
2893 for (i = 0; i < 500; i++) {
2894 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2895 data &= PHY_STAT_MASK;
2896 if (desired) {
2897 if (data == desired)
2898 break;
2899 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2900 data == PHY_STAT_EXT_INIT) {
2901 break;
2902 }
2903
2904 msleep(20);
2905 }
2906
2907 return data;
2908}
2909
hayeswang65b82d62017-06-15 14:44:03 +08002910static void r8153b_ups_en(struct r8152 *tp, bool enable)
2911{
2912 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2913
2914 if (enable) {
2915 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2916 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2917
2918 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2919 ocp_data |= BIT(0);
2920 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2921 } else {
2922 u16 data;
2923
2924 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2925 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2926
2927 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2928 ocp_data &= ~BIT(0);
2929 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2930
2931 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2932 ocp_data &= ~PCUT_STATUS;
2933 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2934
2935 data = r8153_phy_status(tp, 0);
2936
2937 switch (data) {
2938 case PHY_STAT_PWRDN:
2939 case PHY_STAT_EXT_INIT:
2940 r8153b_green_en(tp,
2941 test_bit(GREEN_ETHERNET, &tp->flags));
2942
2943 data = r8152_mdio_read(tp, MII_BMCR);
2944 data &= ~BMCR_PDOWN;
2945 data |= BMCR_RESET;
2946 r8152_mdio_write(tp, MII_BMCR, data);
2947
2948 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002949 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002950
2951 default:
2952 if (data != PHY_STAT_LAN_ON)
2953 netif_warn(tp, link, tp->netdev,
2954 "PHY not ready");
2955 break;
2956 }
2957 }
2958}
2959
hayeswangb2143962015-07-24 13:54:23 +08002960static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2961{
2962 u32 ocp_data;
2963
2964 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2965 if (enable)
2966 ocp_data |= PWR_EN | PHASE2_EN;
2967 else
2968 ocp_data &= ~(PWR_EN | PHASE2_EN);
2969 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2970
2971 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2972 ocp_data &= ~PCUT_STATUS;
2973 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2974}
2975
hayeswang65b82d62017-06-15 14:44:03 +08002976static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2977{
2978 u32 ocp_data;
2979
2980 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2981 if (enable)
2982 ocp_data |= PWR_EN | PHASE2_EN;
2983 else
2984 ocp_data &= ~PWR_EN;
2985 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2986
2987 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2988 ocp_data &= ~PCUT_STATUS;
2989 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2990}
2991
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002992static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08002993{
2994 u32 ocp_data;
2995
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002996 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08002997 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002998 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08002999 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003000 ocp_data &= ~UPCOMING_RUNTIME_D3;
3001 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003002
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003003 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3004 ocp_data &= ~LINK_CHG_EVENT;
3005 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3006
3007 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3008 ocp_data &= ~LINK_CHANGE_FLAG;
3009 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003010}
3011
hayeswang7daed8d2015-07-24 13:54:24 +08003012static bool rtl_can_wakeup(struct r8152 *tp)
3013{
3014 struct usb_device *udev = tp->udev;
3015
3016 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3017}
3018
hayeswang9a4be1b2014-02-18 21:49:07 +08003019static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3020{
3021 if (enable) {
3022 u32 ocp_data;
3023
3024 __rtl_set_wol(tp, WAKE_ANY);
3025
3026 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3027
3028 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3029 ocp_data |= LINK_OFF_WAKE_EN;
3030 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3031
3032 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3033 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08003034 u32 ocp_data;
3035
hayeswang9a4be1b2014-02-18 21:49:07 +08003036 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08003037
3038 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3039
3040 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3041 ocp_data &= ~LINK_OFF_WAKE_EN;
3042 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3043
3044 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08003045 }
3046}
hayeswangf95ae8a2016-06-30 15:33:35 +08003047
hayeswang2609af12016-07-05 16:11:46 +08003048static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3049{
hayeswang2609af12016-07-05 16:11:46 +08003050 if (enable) {
3051 r8153_u1u2en(tp, false);
3052 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003053 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08003054 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08003055 } else {
hayeswang02552752017-06-09 17:11:42 +08003056 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003057 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003058
3059 switch (tp->version) {
3060 case RTL_VER_03:
3061 case RTL_VER_04:
3062 break;
3063 case RTL_VER_05:
3064 case RTL_VER_06:
3065 default:
3066 r8153_u2p3en(tp, true);
3067 break;
3068 }
3069
hayeswangb2143962015-07-24 13:54:23 +08003070 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08003071 }
3072}
3073
hayeswang65b82d62017-06-15 14:44:03 +08003074static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3075{
3076 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003077 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003078 r8153b_u1u2en(tp, false);
3079 r8153_u2p3en(tp, false);
3080 rtl_runtime_suspend_enable(tp, true);
3081 r8153b_ups_en(tp, true);
3082 } else {
3083 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003084 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003085 rtl_runtime_suspend_enable(tp, false);
3086 r8153_u2p3en(tp, true);
3087 r8153b_u1u2en(tp, true);
3088 }
3089}
3090
hayeswang43499682014-02-18 21:48:58 +08003091static void r8153_teredo_off(struct r8152 *tp)
3092{
3093 u32 ocp_data;
3094
hayeswang65b82d62017-06-15 14:44:03 +08003095 switch (tp->version) {
3096 case RTL_VER_01:
3097 case RTL_VER_02:
3098 case RTL_VER_03:
3099 case RTL_VER_04:
3100 case RTL_VER_05:
3101 case RTL_VER_06:
3102 case RTL_VER_07:
3103 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3104 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3105 OOB_TEREDO_EN);
3106 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3107 break;
3108
3109 case RTL_VER_08:
3110 case RTL_VER_09:
3111 /* The bit 0 ~ 7 are relative with teredo settings. They are
3112 * W1C (write 1 to clear), so set all 1 to disable it.
3113 */
3114 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3115 break;
3116
3117 default:
3118 break;
3119 }
hayeswang43499682014-02-18 21:48:58 +08003120
3121 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3122 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3123 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3124}
3125
hayeswang93fe9b12016-06-16 10:55:18 +08003126static void rtl_reset_bmu(struct r8152 *tp)
3127{
3128 u32 ocp_data;
3129
3130 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3131 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3132 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3133 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3134 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3135}
3136
hayeswangcda9fb02016-01-07 17:51:12 +08003137static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08003138{
hayeswangcda9fb02016-01-07 17:51:12 +08003139 if (enable) {
3140 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
3141 LINKENA | DIS_SDSAVE);
3142 } else {
3143 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
3144 DIS_SDSAVE);
3145 msleep(20);
3146 }
hayeswang43499682014-02-18 21:48:58 +08003147}
3148
hayeswange6449532016-09-20 16:22:05 +08003149static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
3150{
3151 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
3152 ocp_reg_write(tp, OCP_EEE_DATA, reg);
3153 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
3154}
3155
3156static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3157{
3158 u16 data;
3159
3160 r8152_mmd_indirect(tp, dev, reg);
3161 data = ocp_reg_read(tp, OCP_EEE_DATA);
3162 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3163
3164 return data;
3165}
3166
3167static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3168{
3169 r8152_mmd_indirect(tp, dev, reg);
3170 ocp_reg_write(tp, OCP_EEE_DATA, data);
3171 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3172}
3173
3174static void r8152_eee_en(struct r8152 *tp, bool enable)
3175{
3176 u16 config1, config2, config3;
3177 u32 ocp_data;
3178
3179 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3180 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3181 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3182 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3183
3184 if (enable) {
3185 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3186 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3187 config1 |= sd_rise_time(1);
3188 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3189 config3 |= fast_snr(42);
3190 } else {
3191 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3192 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3193 RX_QUIET_EN);
3194 config1 |= sd_rise_time(7);
3195 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3196 config3 |= fast_snr(511);
3197 }
3198
3199 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3200 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3201 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3202 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3203}
3204
Hayes Wange7bde562019-08-23 15:33:41 +08003205static void r8153_eee_en(struct r8152 *tp, bool enable)
hayeswange6449532016-09-20 16:22:05 +08003206{
Hayes Wange7bde562019-08-23 15:33:41 +08003207 u32 ocp_data;
3208 u16 config;
3209
3210 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3211 config = ocp_reg_read(tp, OCP_EEE_CFG);
3212
3213 if (enable) {
3214 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3215 config |= EEE10_EN;
Hayes Wangf4a93be2019-08-23 15:33:40 +08003216 } else {
Hayes Wange7bde562019-08-23 15:33:41 +08003217 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3218 config &= ~EEE10_EN;
3219 }
3220
3221 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3222 ocp_reg_write(tp, OCP_EEE_CFG, config);
3223}
3224
3225static void r8153b_eee_en(struct r8152 *tp, bool enable)
3226{
3227 r8153_eee_en(tp, enable);
3228
3229 if (enable)
3230 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3231 else
3232 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3233}
3234
3235static void rtl_eee_enable(struct r8152 *tp, bool enable)
3236{
3237 switch (tp->version) {
3238 case RTL_VER_01:
3239 case RTL_VER_02:
3240 case RTL_VER_07:
3241 if (enable) {
3242 r8152_eee_en(tp, true);
3243 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
3244 tp->eee_adv);
3245 } else {
3246 r8152_eee_en(tp, false);
3247 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
3248 }
3249 break;
3250 case RTL_VER_03:
3251 case RTL_VER_04:
3252 case RTL_VER_05:
3253 case RTL_VER_06:
3254 if (enable) {
3255 r8153_eee_en(tp, true);
3256 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
3257 } else {
3258 r8153_eee_en(tp, false);
3259 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3260 }
3261 break;
3262 case RTL_VER_08:
3263 case RTL_VER_09:
3264 if (enable) {
3265 r8153b_eee_en(tp, true);
3266 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
3267 } else {
3268 r8153b_eee_en(tp, false);
3269 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3270 }
3271 break;
3272 default:
3273 break;
Hayes Wangf4a93be2019-08-23 15:33:40 +08003274 }
hayeswange6449532016-09-20 16:22:05 +08003275}
3276
3277static void r8152b_enable_fc(struct r8152 *tp)
3278{
3279 u16 anar;
3280
3281 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3282 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3283 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3284}
3285
hayeswangd70b1132014-09-19 15:17:18 +08003286static void rtl8152_disable(struct r8152 *tp)
3287{
hayeswangcda9fb02016-01-07 17:51:12 +08003288 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003289 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003290 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003291}
3292
hayeswang43499682014-02-18 21:48:58 +08003293static void r8152b_hw_phy_cfg(struct r8152 *tp)
3294{
Hayes Wange7bde562019-08-23 15:33:41 +08003295 rtl_eee_enable(tp, tp->eee_en);
hayeswangef39df82016-09-20 16:22:07 +08003296 r8152_aldps_en(tp, true);
3297 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003298
hayeswangaa66a5f2014-02-18 21:49:04 +08003299 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003300}
3301
hayeswangac718b62013-05-02 16:01:25 +00003302static void r8152b_exit_oob(struct r8152 *tp)
3303{
hayeswangdb8515e2014-03-06 15:07:16 +08003304 u32 ocp_data;
3305 int i;
hayeswangac718b62013-05-02 16:01:25 +00003306
3307 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3308 ocp_data &= ~RCR_ACPT_ALL;
3309 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3310
hayeswang00a5e362014-02-18 21:48:59 +08003311 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003312 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003313 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3314 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3315
3316 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3317 ocp_data &= ~NOW_IS_OOB;
3318 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3319
3320 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3321 ocp_data &= ~MCU_BORW_EN;
3322 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3323
3324 for (i = 0; i < 1000; i++) {
3325 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3326 if (ocp_data & LINK_LIST_READY)
3327 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003328 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003329 }
3330
3331 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3332 ocp_data |= RE_INIT_LL;
3333 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3334
3335 for (i = 0; i < 1000; i++) {
3336 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3337 if (ocp_data & LINK_LIST_READY)
3338 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003339 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003340 }
3341
3342 rtl8152_nic_reset(tp);
3343
3344 /* rx share fifo credit full threshold */
3345 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3346
hayeswanga3cc4652014-07-24 16:37:43 +08003347 if (tp->udev->speed == USB_SPEED_FULL ||
3348 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003349 /* rx share fifo credit near full threshold */
3350 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3351 RXFIFO_THR2_FULL);
3352 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3353 RXFIFO_THR3_FULL);
3354 } else {
3355 /* rx share fifo credit near full threshold */
3356 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3357 RXFIFO_THR2_HIGH);
3358 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3359 RXFIFO_THR3_HIGH);
3360 }
3361
3362 /* TX share fifo free credit full threshold */
3363 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3364
3365 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003366 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003367 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3368 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3369
hayeswangc5554292014-09-12 10:43:11 +08003370 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003371
3372 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3373
3374 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3375 ocp_data |= TCR0_AUTO_FIFO;
3376 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3377}
3378
3379static void r8152b_enter_oob(struct r8152 *tp)
3380{
hayeswang45f4a192014-01-06 17:08:41 +08003381 u32 ocp_data;
3382 int i;
hayeswangac718b62013-05-02 16:01:25 +00003383
3384 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3385 ocp_data &= ~NOW_IS_OOB;
3386 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3387
3388 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3389 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3390 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3391
hayeswangd70b1132014-09-19 15:17:18 +08003392 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003393
3394 for (i = 0; i < 1000; i++) {
3395 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3396 if (ocp_data & LINK_LIST_READY)
3397 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003398 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003399 }
3400
3401 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3402 ocp_data |= RE_INIT_LL;
3403 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3404
3405 for (i = 0; i < 1000; i++) {
3406 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3407 if (ocp_data & LINK_LIST_READY)
3408 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003409 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003410 }
3411
3412 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3413
hayeswangc5554292014-09-12 10:43:11 +08003414 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003415
Kevin Lo59c0b472019-08-01 11:29:38 +08003416 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00003417 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003418 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00003419
3420 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3421 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3422 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3423
hayeswang00a5e362014-02-18 21:48:59 +08003424 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003425
3426 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3427 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3428 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3429}
3430
hayeswang65b82d62017-06-15 14:44:03 +08003431static int r8153_patch_request(struct r8152 *tp, bool request)
3432{
3433 u16 data;
3434 int i;
3435
3436 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3437 if (request)
3438 data |= PATCH_REQUEST;
3439 else
3440 data &= ~PATCH_REQUEST;
3441 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3442
3443 for (i = 0; request && i < 5000; i++) {
3444 usleep_range(1000, 2000);
3445 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3446 break;
3447 }
3448
3449 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3450 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3451 r8153_patch_request(tp, false);
3452 return -ETIME;
3453 } else {
3454 return 0;
3455 }
3456}
3457
hayeswange6449532016-09-20 16:22:05 +08003458static void r8153_aldps_en(struct r8152 *tp, bool enable)
3459{
3460 u16 data;
3461
3462 data = ocp_reg_read(tp, OCP_POWER_CFG);
3463 if (enable) {
3464 data |= EN_ALDPS;
3465 ocp_reg_write(tp, OCP_POWER_CFG, data);
3466 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003467 int i;
3468
hayeswange6449532016-09-20 16:22:05 +08003469 data &= ~EN_ALDPS;
3470 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003471 for (i = 0; i < 20; i++) {
3472 usleep_range(1000, 2000);
3473 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3474 break;
3475 }
hayeswange6449532016-09-20 16:22:05 +08003476 }
3477}
3478
hayeswang65b82d62017-06-15 14:44:03 +08003479static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3480{
3481 r8153_aldps_en(tp, enable);
3482
3483 if (enable)
3484 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3485 else
3486 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3487}
3488
hayeswang65b82d62017-06-15 14:44:03 +08003489static void r8153b_enable_fc(struct r8152 *tp)
3490{
3491 r8152b_enable_fc(tp);
3492 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3493}
3494
hayeswang43779f82014-01-02 11:25:10 +08003495static void r8153_hw_phy_cfg(struct r8152 *tp)
3496{
3497 u32 ocp_data;
3498 u16 data;
3499
hayeswangd768c612016-09-20 16:22:09 +08003500 /* disable ALDPS before updating the PHY parameters */
3501 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003502
hayeswangd768c612016-09-20 16:22:09 +08003503 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08003504 rtl_eee_enable(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003505
3506 if (tp->version == RTL_VER_03) {
3507 data = ocp_reg_read(tp, OCP_EEE_CFG);
3508 data &= ~CTAP_SHORT_EN;
3509 ocp_reg_write(tp, OCP_EEE_CFG, data);
3510 }
3511
3512 data = ocp_reg_read(tp, OCP_POWER_CFG);
3513 data |= EEE_CLKDIV_EN;
3514 ocp_reg_write(tp, OCP_POWER_CFG, data);
3515
3516 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3517 data |= EN_10M_BGOFF;
3518 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3519 data = ocp_reg_read(tp, OCP_POWER_CFG);
3520 data |= EN_10M_PLLOFF;
3521 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003522 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003523
3524 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3525 ocp_data |= PFM_PWM_SWITCH;
3526 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3527
hayeswangb4d99de2015-01-19 17:02:46 +08003528 /* Enable LPF corner auto tune */
3529 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003530
hayeswangb4d99de2015-01-19 17:02:46 +08003531 /* Adjust 10M Amplitude */
3532 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3533 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003534
Hayes Wange7bde562019-08-23 15:33:41 +08003535 if (tp->eee_en)
3536 rtl_eee_enable(tp, true);
hayeswangaf0287e2016-09-20 16:22:08 +08003537
hayeswangef39df82016-09-20 16:22:07 +08003538 r8153_aldps_en(tp, true);
3539 r8152b_enable_fc(tp);
3540
hayeswang3cb32342017-06-09 17:11:43 +08003541 switch (tp->version) {
3542 case RTL_VER_03:
3543 case RTL_VER_04:
3544 break;
3545 case RTL_VER_05:
3546 case RTL_VER_06:
3547 default:
3548 r8153_u2p3en(tp, true);
3549 break;
3550 }
3551
hayeswangaa66a5f2014-02-18 21:49:04 +08003552 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003553}
3554
hayeswang65b82d62017-06-15 14:44:03 +08003555static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3556{
3557 u32 ocp_data;
3558
3559 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3560 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3561 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3562 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3563
3564 return ocp_data;
3565}
3566
3567static void r8153b_hw_phy_cfg(struct r8152 *tp)
3568{
3569 u32 ocp_data, ups_flags = 0;
3570 u16 data;
3571
3572 /* disable ALDPS before updating the PHY parameters */
3573 r8153b_aldps_en(tp, false);
3574
3575 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08003576 rtl_eee_enable(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003577
3578 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3579
3580 data = sram_read(tp, SRAM_GREEN_CFG);
3581 data |= R_TUNE_EN;
3582 sram_write(tp, SRAM_GREEN_CFG, data);
3583 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3584 data |= PGA_RETURN_EN;
3585 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3586
3587 /* ADC Bias Calibration:
3588 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3589 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3590 * ADC ioffset.
3591 */
3592 ocp_data = r8152_efuse_read(tp, 0x7d);
3593 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3594 if (data != 0xffff)
3595 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3596
3597 /* ups mode tx-link-pulse timing adjustment:
3598 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3599 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3600 */
3601 ocp_data = ocp_reg_read(tp, 0xc426);
3602 ocp_data &= 0x3fff;
3603 if (ocp_data) {
3604 u32 swr_cnt_1ms_ini;
3605
3606 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3607 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3608 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3609 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3610 }
3611
3612 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3613 ocp_data |= PFM_PWM_SWITCH;
3614 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3615
3616 /* Advnace EEE */
3617 if (!r8153_patch_request(tp, true)) {
3618 data = ocp_reg_read(tp, OCP_POWER_CFG);
3619 data |= EEE_CLKDIV_EN;
3620 ocp_reg_write(tp, OCP_POWER_CFG, data);
3621
3622 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3623 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3624 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3625
3626 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3627 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3628
3629 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3630 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3631 UPS_FLAGS_EEE_PLLOFF_GIGA;
3632
3633 r8153_patch_request(tp, false);
3634 }
3635
3636 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3637
Hayes Wange7bde562019-08-23 15:33:41 +08003638 if (tp->eee_en)
3639 rtl_eee_enable(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003640
3641 r8153b_aldps_en(tp, true);
3642 r8153b_enable_fc(tp);
3643 r8153_u2p3en(tp, true);
3644
3645 set_bit(PHY_RESET, &tp->flags);
3646}
3647
hayeswang43779f82014-01-02 11:25:10 +08003648static void r8153_first_init(struct r8152 *tp)
3649{
3650 u32 ocp_data;
3651 int i;
3652
hayeswang134f98b2017-06-09 17:11:40 +08003653 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003654 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003655 r8153_teredo_off(tp);
3656
3657 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3658 ocp_data &= ~RCR_ACPT_ALL;
3659 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3660
hayeswang43779f82014-01-02 11:25:10 +08003661 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003662 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003663
3664 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3665 ocp_data &= ~NOW_IS_OOB;
3666 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3667
3668 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3669 ocp_data &= ~MCU_BORW_EN;
3670 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3671
3672 for (i = 0; i < 1000; i++) {
3673 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3674 if (ocp_data & LINK_LIST_READY)
3675 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003676 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003677 }
3678
3679 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3680 ocp_data |= RE_INIT_LL;
3681 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3682
3683 for (i = 0; i < 1000; i++) {
3684 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3685 if (ocp_data & LINK_LIST_READY)
3686 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003687 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003688 }
3689
hayeswangc5554292014-09-12 10:43:11 +08003690 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003691
hayeswangb65c0c92017-06-21 11:25:18 +08003692 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003693 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003694 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003695
3696 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3697 ocp_data |= TCR0_AUTO_FIFO;
3698 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3699
3700 rtl8152_nic_reset(tp);
3701
3702 /* rx share fifo credit full threshold */
3703 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3704 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3705 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3706 /* TX share fifo free credit full threshold */
3707 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003708}
3709
3710static void r8153_enter_oob(struct r8152 *tp)
3711{
3712 u32 ocp_data;
3713 int i;
3714
hayeswang134f98b2017-06-09 17:11:40 +08003715 r8153_mac_clk_spd(tp, true);
3716
hayeswang43779f82014-01-02 11:25:10 +08003717 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3718 ocp_data &= ~NOW_IS_OOB;
3719 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3720
hayeswangd70b1132014-09-19 15:17:18 +08003721 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003722 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003723
3724 for (i = 0; i < 1000; i++) {
3725 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3726 if (ocp_data & LINK_LIST_READY)
3727 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003728 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003729 }
3730
3731 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3732 ocp_data |= RE_INIT_LL;
3733 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3734
3735 for (i = 0; i < 1000; i++) {
3736 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3737 if (ocp_data & LINK_LIST_READY)
3738 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003739 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003740 }
3741
hayeswangb65c0c92017-06-21 11:25:18 +08003742 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003743 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003744
hayeswang65b82d62017-06-15 14:44:03 +08003745 switch (tp->version) {
3746 case RTL_VER_03:
3747 case RTL_VER_04:
3748 case RTL_VER_05:
3749 case RTL_VER_06:
3750 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3751 ocp_data &= ~TEREDO_WAKE_MASK;
3752 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3753 break;
3754
3755 case RTL_VER_08:
3756 case RTL_VER_09:
3757 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3758 * type. Set it to zero. bits[7:0] are the W1C bits about
3759 * the events. Set them to all 1 to clear them.
3760 */
3761 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3762 break;
3763
3764 default:
3765 break;
3766 }
hayeswang43779f82014-01-02 11:25:10 +08003767
hayeswangc5554292014-09-12 10:43:11 +08003768 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003769
Kevin Lo59c0b472019-08-01 11:29:38 +08003770 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08003771 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003772 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003773
3774 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3775 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3776 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3777
hayeswang00a5e362014-02-18 21:48:59 +08003778 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003779
3780 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3781 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3782 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3783}
3784
hayeswangd70b1132014-09-19 15:17:18 +08003785static void rtl8153_disable(struct r8152 *tp)
3786{
hayeswangcda9fb02016-01-07 17:51:12 +08003787 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003788 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003789 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003790 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003791}
3792
hayeswang65b82d62017-06-15 14:44:03 +08003793static void rtl8153b_disable(struct r8152 *tp)
3794{
3795 r8153b_aldps_en(tp, false);
3796 rtl_disable(tp);
3797 rtl_reset_bmu(tp);
3798 r8153b_aldps_en(tp, true);
3799}
3800
hayeswangac718b62013-05-02 16:01:25 +00003801static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3802{
hayeswang43779f82014-01-02 11:25:10 +08003803 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003804 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003805 int ret = 0;
3806
hayeswangac718b62013-05-02 16:01:25 +00003807 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3808 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3809 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003810 if (tp->mii.supports_gmii) {
3811 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3812 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3813 } else {
3814 gbcr = 0;
3815 }
hayeswangac718b62013-05-02 16:01:25 +00003816
3817 if (autoneg == AUTONEG_DISABLE) {
3818 if (speed == SPEED_10) {
3819 bmcr = 0;
3820 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003821 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003822 } else if (speed == SPEED_100) {
3823 bmcr = BMCR_SPEED100;
3824 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003825 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003826 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3827 bmcr = BMCR_SPEED1000;
3828 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003829 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003830 } else {
3831 ret = -EINVAL;
3832 goto out;
3833 }
3834
hayeswang65b82d62017-06-15 14:44:03 +08003835 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003836 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003837 if (speed != SPEED_1000)
3838 speed_duplex++;
3839 }
hayeswangac718b62013-05-02 16:01:25 +00003840 } else {
3841 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003842 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003843 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003844 speed_duplex = NWAY_10M_FULL;
3845 } else {
hayeswangac718b62013-05-02 16:01:25 +00003846 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003847 speed_duplex = NWAY_10M_HALF;
3848 }
hayeswangac718b62013-05-02 16:01:25 +00003849 } else if (speed == SPEED_100) {
3850 if (duplex == DUPLEX_FULL) {
3851 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3852 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003853 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003854 } else {
3855 anar |= ADVERTISE_10HALF;
3856 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003857 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003858 }
hayeswang43779f82014-01-02 11:25:10 +08003859 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3860 if (duplex == DUPLEX_FULL) {
3861 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3862 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3863 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3864 } else {
3865 anar |= ADVERTISE_10HALF;
3866 anar |= ADVERTISE_100HALF;
3867 gbcr |= ADVERTISE_1000HALF;
3868 }
hayeswang65b82d62017-06-15 14:44:03 +08003869 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003870 } else {
3871 ret = -EINVAL;
3872 goto out;
3873 }
3874
3875 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3876 }
3877
hayeswangfae56172016-06-16 14:08:29 +08003878 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003879 bmcr |= BMCR_RESET;
3880
hayeswang43779f82014-01-02 11:25:10 +08003881 if (tp->mii.supports_gmii)
3882 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3883
hayeswangac718b62013-05-02 16:01:25 +00003884 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3885 r8152_mdio_write(tp, MII_BMCR, bmcr);
3886
hayeswang65b82d62017-06-15 14:44:03 +08003887 switch (tp->version) {
3888 case RTL_VER_08:
3889 case RTL_VER_09:
3890 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3891 UPS_FLAGS_SPEED_MASK);
3892 break;
3893
3894 default:
3895 break;
3896 }
3897
hayeswangfae56172016-06-16 14:08:29 +08003898 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003899 int i;
3900
hayeswangaa66a5f2014-02-18 21:49:04 +08003901 for (i = 0; i < 50; i++) {
3902 msleep(20);
3903 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3904 break;
3905 }
3906 }
3907
hayeswangac718b62013-05-02 16:01:25 +00003908out:
hayeswangac718b62013-05-02 16:01:25 +00003909 return ret;
3910}
3911
hayeswangd70b1132014-09-19 15:17:18 +08003912static void rtl8152_up(struct r8152 *tp)
3913{
3914 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3915 return;
3916
hayeswangcda9fb02016-01-07 17:51:12 +08003917 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003918 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003919 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003920}
3921
hayeswangac718b62013-05-02 16:01:25 +00003922static void rtl8152_down(struct r8152 *tp)
3923{
hayeswang68714382014-04-11 17:54:31 +08003924 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3925 rtl_drop_queued_tx(tp);
3926 return;
3927 }
3928
hayeswang00a5e362014-02-18 21:48:59 +08003929 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003930 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003931 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003932 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003933}
3934
hayeswangd70b1132014-09-19 15:17:18 +08003935static void rtl8153_up(struct r8152 *tp)
3936{
3937 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3938 return;
3939
hayeswangb2143962015-07-24 13:54:23 +08003940 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003941 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003942 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003943 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003944 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003945
3946 switch (tp->version) {
3947 case RTL_VER_03:
3948 case RTL_VER_04:
3949 break;
3950 case RTL_VER_05:
3951 case RTL_VER_06:
3952 default:
3953 r8153_u2p3en(tp, true);
3954 break;
3955 }
3956
hayeswangb2143962015-07-24 13:54:23 +08003957 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003958}
3959
hayeswang43779f82014-01-02 11:25:10 +08003960static void rtl8153_down(struct r8152 *tp)
3961{
hayeswang68714382014-04-11 17:54:31 +08003962 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3963 rtl_drop_queued_tx(tp);
3964 return;
3965 }
3966
hayeswangb9702722014-02-18 21:49:00 +08003967 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003968 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003969 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003970 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003971 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003972 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003973}
3974
hayeswang65b82d62017-06-15 14:44:03 +08003975static void rtl8153b_up(struct r8152 *tp)
3976{
3977 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3978 return;
3979
3980 r8153b_u1u2en(tp, false);
3981 r8153_u2p3en(tp, false);
3982 r8153b_aldps_en(tp, false);
3983
3984 r8153_first_init(tp);
3985 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3986
3987 r8153b_aldps_en(tp, true);
3988 r8153_u2p3en(tp, true);
3989 r8153b_u1u2en(tp, true);
3990}
3991
3992static void rtl8153b_down(struct r8152 *tp)
3993{
3994 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3995 rtl_drop_queued_tx(tp);
3996 return;
3997 }
3998
3999 r8153b_u1u2en(tp, false);
4000 r8153_u2p3en(tp, false);
4001 r8153b_power_cut_en(tp, false);
4002 r8153b_aldps_en(tp, false);
4003 r8153_enter_oob(tp);
4004 r8153b_aldps_en(tp, true);
4005}
4006
hayeswang2dd49e02015-09-07 11:57:44 +08004007static bool rtl8152_in_nway(struct r8152 *tp)
4008{
4009 u16 nway_state;
4010
4011 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
4012 tp->ocp_base = 0x2000;
4013 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
4014 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
4015
4016 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
4017 if (nway_state & 0xc000)
4018 return false;
4019 else
4020 return true;
4021}
4022
4023static bool rtl8153_in_nway(struct r8152 *tp)
4024{
4025 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
4026
4027 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
4028 return false;
4029 else
4030 return true;
4031}
4032
hayeswangac718b62013-05-02 16:01:25 +00004033static void set_carrier(struct r8152 *tp)
4034{
4035 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08004036 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00004037 u8 speed;
4038
4039 speed = rtl8152_get_speed(tp);
4040
4041 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08004042 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08004043 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08004044 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08004045 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004046 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08004047 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004048 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
4049 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08004050 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004051 netif_wake_queue(netdev);
4052 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08004053 } else if (netif_queue_stopped(netdev) &&
4054 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
4055 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004056 }
4057 } else {
hayeswang51d979f2015-02-06 11:30:47 +08004058 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00004059 netif_carrier_off(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08004060 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08004061 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08004062 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08004063 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004064 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08004065 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00004066 }
4067 }
hayeswangac718b62013-05-02 16:01:25 +00004068}
4069
4070static void rtl_work_func_t(struct work_struct *work)
4071{
4072 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
4073
hayeswanga1f83fe2014-11-12 10:05:05 +08004074 /* If the device is unplugged or !netif_running(), the workqueue
4075 * doesn't need to wake the device, and could return directly.
4076 */
4077 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
4078 return;
4079
hayeswang9a4be1b2014-02-18 21:49:07 +08004080 if (usb_autopm_get_interface(tp->intf) < 0)
4081 return;
4082
hayeswangac718b62013-05-02 16:01:25 +00004083 if (!test_bit(WORK_ENABLE, &tp->flags))
4084 goto out1;
4085
hayeswangb5403272014-10-09 18:00:26 +08004086 if (!mutex_trylock(&tp->control)) {
4087 schedule_delayed_work(&tp->schedule, 0);
4088 goto out1;
4089 }
4090
hayeswang216a8342016-01-07 17:51:11 +08004091 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08004092 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00004093
hayeswang216a8342016-01-07 17:51:11 +08004094 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00004095 _rtl8152_set_rx_mode(tp->netdev);
4096
Hayes Wangd2187f82019-08-19 14:40:36 +08004097 /* don't schedule tasket before linking */
4098 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
hayeswang216a8342016-01-07 17:51:11 +08004099 netif_carrier_ok(tp->netdev))
Hayes Wangd2187f82019-08-19 14:40:36 +08004100 tasklet_schedule(&tp->tx_tl);
hayeswangaa66a5f2014-02-18 21:49:04 +08004101
hayeswangb5403272014-10-09 18:00:26 +08004102 mutex_unlock(&tp->control);
4103
hayeswangac718b62013-05-02 16:01:25 +00004104out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08004105 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00004106}
4107
hayeswanga028a9e2016-06-13 17:49:36 +08004108static void rtl_hw_phy_work_func_t(struct work_struct *work)
4109{
4110 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
4111
4112 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4113 return;
4114
4115 if (usb_autopm_get_interface(tp->intf) < 0)
4116 return;
4117
4118 mutex_lock(&tp->control);
4119
4120 tp->rtl_ops.hw_phy_cfg(tp);
4121
hayeswangaa7e26b2016-06-13 17:49:38 +08004122 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08004123
hayeswanga028a9e2016-06-13 17:49:36 +08004124 mutex_unlock(&tp->control);
4125
4126 usb_autopm_put_interface(tp->intf);
4127}
4128
hayeswang5ee3c602016-01-07 17:12:17 +08004129#ifdef CONFIG_PM_SLEEP
4130static int rtl_notifier(struct notifier_block *nb, unsigned long action,
4131 void *data)
4132{
4133 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
4134
4135 switch (action) {
4136 case PM_HIBERNATION_PREPARE:
4137 case PM_SUSPEND_PREPARE:
4138 usb_autopm_get_interface(tp->intf);
4139 break;
4140
4141 case PM_POST_HIBERNATION:
4142 case PM_POST_SUSPEND:
4143 usb_autopm_put_interface(tp->intf);
4144 break;
4145
4146 case PM_POST_RESTORE:
4147 case PM_RESTORE_PREPARE:
4148 default:
4149 break;
4150 }
4151
4152 return NOTIFY_DONE;
4153}
4154#endif
4155
hayeswangac718b62013-05-02 16:01:25 +00004156static int rtl8152_open(struct net_device *netdev)
4157{
4158 struct r8152 *tp = netdev_priv(netdev);
4159 int res = 0;
4160
hayeswang7e9da482014-02-18 21:49:05 +08004161 res = alloc_all_mem(tp);
4162 if (res)
4163 goto out;
4164
hayeswang9a4be1b2014-02-18 21:49:07 +08004165 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004166 if (res < 0)
4167 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08004168
hayeswangb5403272014-10-09 18:00:26 +08004169 mutex_lock(&tp->control);
4170
hayeswang7e9da482014-02-18 21:49:05 +08004171 tp->rtl_ops.up(tp);
4172
hayeswang40a82912013-08-14 20:54:40 +08004173 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00004174 netif_start_queue(netdev);
4175 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08004176
hayeswang3d55f442014-02-06 11:55:48 +08004177 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4178 if (res) {
4179 if (res == -ENODEV)
4180 netif_device_detach(tp->netdev);
4181 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
4182 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08004183 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08004184 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08004185 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004186 tasklet_enable(&tp->tx_tl);
hayeswang3d55f442014-02-06 11:55:48 +08004187
hayeswangb5403272014-10-09 18:00:26 +08004188 mutex_unlock(&tp->control);
4189
hayeswang9a4be1b2014-02-18 21:49:07 +08004190 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08004191#ifdef CONFIG_PM_SLEEP
4192 tp->pm_notifier.notifier_call = rtl_notifier;
4193 register_pm_notifier(&tp->pm_notifier);
4194#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08004195 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004196
Guenter Roeckca0a7532016-11-09 19:51:25 -08004197out_unlock:
4198 mutex_unlock(&tp->control);
4199 usb_autopm_put_interface(tp->intf);
4200out_free:
4201 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08004202out:
hayeswangac718b62013-05-02 16:01:25 +00004203 return res;
4204}
4205
4206static int rtl8152_close(struct net_device *netdev)
4207{
4208 struct r8152 *tp = netdev_priv(netdev);
4209 int res = 0;
4210
hayeswang5ee3c602016-01-07 17:12:17 +08004211#ifdef CONFIG_PM_SLEEP
4212 unregister_pm_notifier(&tp->pm_notifier);
4213#endif
Hayes Wangd2187f82019-08-19 14:40:36 +08004214 tasklet_disable(&tp->tx_tl);
Jiri Slaby0ee1f472018-06-25 09:26:27 +02004215 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
4216 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004217 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08004218 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00004219 cancel_delayed_work_sync(&tp->schedule);
4220 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004221
4222 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004223 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004224 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004225 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004226 } else {
hayeswangb5403272014-10-09 18:00:26 +08004227 mutex_lock(&tp->control);
4228
hayeswang9a4be1b2014-02-18 21:49:07 +08004229 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004230
4231 mutex_unlock(&tp->control);
4232
hayeswang9a4be1b2014-02-18 21:49:07 +08004233 usb_autopm_put_interface(tp->intf);
4234 }
hayeswangac718b62013-05-02 16:01:25 +00004235
hayeswang7e9da482014-02-18 21:49:05 +08004236 free_all_mem(tp);
4237
hayeswangac718b62013-05-02 16:01:25 +00004238 return res;
4239}
4240
hayeswang4f1d4d52014-03-11 16:24:19 +08004241static void rtl_tally_reset(struct r8152 *tp)
4242{
4243 u32 ocp_data;
4244
4245 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4246 ocp_data |= TALLY_RESET;
4247 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4248}
4249
hayeswangac718b62013-05-02 16:01:25 +00004250static void r8152b_init(struct r8152 *tp)
4251{
hayeswangebc2ec482013-08-14 20:54:38 +08004252 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004253 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004254
hayeswang68714382014-04-11 17:54:31 +08004255 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4256 return;
4257
hayeswang2dd436d2016-09-20 16:22:06 +08004258 data = r8152_mdio_read(tp, MII_BMCR);
4259 if (data & BMCR_PDOWN) {
4260 data &= ~BMCR_PDOWN;
4261 r8152_mdio_write(tp, MII_BMCR, data);
4262 }
4263
hayeswangcda9fb02016-01-07 17:51:12 +08004264 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004265
hayeswangac718b62013-05-02 16:01:25 +00004266 if (tp->version == RTL_VER_01) {
4267 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4268 ocp_data &= ~LED_MODE_MASK;
4269 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4270 }
4271
hayeswang00a5e362014-02-18 21:48:59 +08004272 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004273
hayeswangac718b62013-05-02 16:01:25 +00004274 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4275 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4276 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4277 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4278 ocp_data &= ~MCU_CLK_RATIO_MASK;
4279 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4280 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4281 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4282 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4283 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4284
hayeswang4f1d4d52014-03-11 16:24:19 +08004285 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004286
hayeswangebc2ec482013-08-14 20:54:38 +08004287 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004288 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004289 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004290 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4291}
4292
hayeswang43779f82014-01-02 11:25:10 +08004293static void r8153_init(struct r8152 *tp)
4294{
4295 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004296 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004297 int i;
4298
hayeswang68714382014-04-11 17:54:31 +08004299 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4300 return;
4301
hayeswangb9702722014-02-18 21:49:00 +08004302 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004303
4304 for (i = 0; i < 500; i++) {
4305 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4306 AUTOLOAD_DONE)
4307 break;
4308 msleep(20);
4309 }
4310
hayeswangc564b872017-06-09 17:11:38 +08004311 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004312
hayeswang2dd436d2016-09-20 16:22:06 +08004313 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4314 tp->version == RTL_VER_05)
4315 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4316
4317 data = r8152_mdio_read(tp, MII_BMCR);
4318 if (data & BMCR_PDOWN) {
4319 data &= ~BMCR_PDOWN;
4320 r8152_mdio_write(tp, MII_BMCR, data);
4321 }
4322
hayeswangc564b872017-06-09 17:11:38 +08004323 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004324
hayeswangb9702722014-02-18 21:49:00 +08004325 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004326
hayeswang65bab842015-02-12 16:20:46 +08004327 if (tp->version == RTL_VER_04) {
4328 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4329 ocp_data &= ~pwd_dn_scale_mask;
4330 ocp_data |= pwd_dn_scale(96);
4331 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4332
4333 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4334 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4335 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4336 } else if (tp->version == RTL_VER_05) {
4337 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4338 ocp_data &= ~ECM_ALDPS;
4339 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4340
4341 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4342 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4343 ocp_data &= ~DYNAMIC_BURST;
4344 else
4345 ocp_data |= DYNAMIC_BURST;
4346 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004347 } else if (tp->version == RTL_VER_06) {
4348 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4349 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4350 ocp_data &= ~DYNAMIC_BURST;
4351 else
4352 ocp_data |= DYNAMIC_BURST;
4353 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004354 }
4355
4356 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4357 ocp_data |= EP4_FULL_FC;
4358 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4359
hayeswang43779f82014-01-02 11:25:10 +08004360 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4361 ocp_data &= ~TIMER11_EN;
4362 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4363
hayeswang43779f82014-01-02 11:25:10 +08004364 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4365 ocp_data &= ~LED_MODE_MASK;
4366 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4367
hayeswang65bab842015-02-12 16:20:46 +08004368 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004369 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004370 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004371 else
4372 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004373 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4374
4375 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4376 ocp_data &= ~SEN_VAL_MASK;
4377 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4378 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4379
hayeswang65bab842015-02-12 16:20:46 +08004380 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4381
hayeswangb9702722014-02-18 21:49:00 +08004382 r8153_power_cut_en(tp, false);
4383 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004384 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004385 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004386
hayeswange31f6362017-06-09 17:11:41 +08004387 /* rx aggregation */
4388 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4389 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004390 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4391 ocp_data |= RX_AGG_DISABLE;
4392
hayeswange31f6362017-06-09 17:11:41 +08004393 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004394
hayeswang4f1d4d52014-03-11 16:24:19 +08004395 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004396
4397 switch (tp->udev->speed) {
4398 case USB_SPEED_SUPER:
4399 case USB_SPEED_SUPER_PLUS:
4400 tp->coalesce = COALESCE_SUPER;
4401 break;
4402 case USB_SPEED_HIGH:
4403 tp->coalesce = COALESCE_HIGH;
4404 break;
4405 default:
4406 tp->coalesce = COALESCE_SLOW;
4407 break;
4408 }
hayeswang43779f82014-01-02 11:25:10 +08004409}
4410
hayeswang65b82d62017-06-15 14:44:03 +08004411static void r8153b_init(struct r8152 *tp)
4412{
4413 u32 ocp_data;
4414 u16 data;
4415 int i;
4416
4417 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4418 return;
4419
4420 r8153b_u1u2en(tp, false);
4421
4422 for (i = 0; i < 500; i++) {
4423 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4424 AUTOLOAD_DONE)
4425 break;
4426 msleep(20);
4427 }
4428
4429 data = r8153_phy_status(tp, 0);
4430
4431 data = r8152_mdio_read(tp, MII_BMCR);
4432 if (data & BMCR_PDOWN) {
4433 data &= ~BMCR_PDOWN;
4434 r8152_mdio_write(tp, MII_BMCR, data);
4435 }
4436
4437 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4438
4439 r8153_u2p3en(tp, false);
4440
4441 /* MSC timer = 0xfff * 8ms = 32760 ms */
4442 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4443
4444 /* U1/U2/L1 idle timer. 500 us */
4445 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4446
4447 r8153b_power_cut_en(tp, false);
4448 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08004449 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004450 rtl_runtime_suspend_enable(tp, false);
4451 r8153b_u1u2en(tp, true);
4452 usb_enable_lpm(tp->udev);
4453
4454 /* MAC clock speed down */
4455 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4456 ocp_data |= MAC_CLK_SPDWN_EN;
4457 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4458
4459 set_bit(GREEN_ETHERNET, &tp->flags);
4460
4461 /* rx aggregation */
4462 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4463 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4464 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4465
4466 rtl_tally_reset(tp);
4467
4468 tp->coalesce = 15000; /* 15 us */
4469}
4470
hayeswange5011392015-07-29 20:39:08 +08004471static int rtl8152_pre_reset(struct usb_interface *intf)
4472{
4473 struct r8152 *tp = usb_get_intfdata(intf);
4474 struct net_device *netdev;
4475
4476 if (!tp)
4477 return 0;
4478
4479 netdev = tp->netdev;
4480 if (!netif_running(netdev))
4481 return 0;
4482
hayeswangde9bf292017-01-26 09:38:32 +08004483 netif_stop_queue(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08004484 tasklet_disable(&tp->tx_tl);
hayeswange5011392015-07-29 20:39:08 +08004485 napi_disable(&tp->napi);
4486 clear_bit(WORK_ENABLE, &tp->flags);
4487 usb_kill_urb(tp->intr_urb);
4488 cancel_delayed_work_sync(&tp->schedule);
4489 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004490 mutex_lock(&tp->control);
4491 tp->rtl_ops.disable(tp);
4492 mutex_unlock(&tp->control);
4493 }
4494
4495 return 0;
4496}
4497
4498static int rtl8152_post_reset(struct usb_interface *intf)
4499{
4500 struct r8152 *tp = usb_get_intfdata(intf);
4501 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004502 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004503
4504 if (!tp)
4505 return 0;
4506
Mario Limonciello25766272019-04-04 13:46:53 -05004507 /* reset the MAC adddress in case of policy change */
4508 if (determine_ethernet_addr(tp, &sa) >= 0) {
4509 rtnl_lock();
4510 dev_set_mac_address (tp->netdev, &sa, NULL);
4511 rtnl_unlock();
4512 }
4513
hayeswange5011392015-07-29 20:39:08 +08004514 netdev = tp->netdev;
4515 if (!netif_running(netdev))
4516 return 0;
4517
4518 set_bit(WORK_ENABLE, &tp->flags);
4519 if (netif_carrier_ok(netdev)) {
4520 mutex_lock(&tp->control);
4521 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004522 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004523 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004524 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004525 }
4526
4527 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004528 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08004529 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004530 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004531
hayeswang7489bda2017-01-26 09:38:34 +08004532 if (!list_empty(&tp->rx_done))
4533 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004534
4535 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004536}
4537
hayeswang2dd49e02015-09-07 11:57:44 +08004538static bool delay_autosuspend(struct r8152 *tp)
4539{
4540 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4541 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4542
4543 /* This means a linking change occurs and the driver doesn't detect it,
4544 * yet. If the driver has disabled tx/rx and hw is linking on, the
4545 * device wouldn't wake up by receiving any packet.
4546 */
4547 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4548 return true;
4549
4550 /* If the linking down is occurred by nway, the device may miss the
4551 * linking change event. And it wouldn't wake when linking on.
4552 */
4553 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4554 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004555 else if (!skb_queue_empty(&tp->tx_queue))
4556 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004557 else
4558 return false;
4559}
4560
hayeswang21cbd0e2017-06-13 15:14:39 +08004561static int rtl8152_runtime_resume(struct r8152 *tp)
4562{
4563 struct net_device *netdev = tp->netdev;
4564
4565 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4566 struct napi_struct *napi = &tp->napi;
4567
4568 tp->rtl_ops.autosuspend_en(tp, false);
4569 napi_disable(napi);
4570 set_bit(WORK_ENABLE, &tp->flags);
4571
4572 if (netif_carrier_ok(netdev)) {
4573 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4574 rtl_start_rx(tp);
4575 } else {
4576 netif_carrier_off(netdev);
4577 tp->rtl_ops.disable(tp);
4578 netif_info(tp, link, netdev, "linking down\n");
4579 }
4580 }
4581
4582 napi_enable(napi);
4583 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4584 smp_mb__after_atomic();
4585
4586 if (!list_empty(&tp->rx_done))
4587 napi_schedule(&tp->napi);
4588
4589 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4590 } else {
4591 if (netdev->flags & IFF_UP)
4592 tp->rtl_ops.autosuspend_en(tp, false);
4593
4594 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4595 }
4596
4597 return 0;
4598}
4599
4600static int rtl8152_system_resume(struct r8152 *tp)
4601{
4602 struct net_device *netdev = tp->netdev;
4603
4604 netif_device_attach(netdev);
4605
4606 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4607 tp->rtl_ops.up(tp);
4608 netif_carrier_off(netdev);
4609 set_bit(WORK_ENABLE, &tp->flags);
4610 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4611 }
4612
4613 return 0;
4614}
4615
hayeswanga9c54ad2017-01-25 13:41:45 +08004616static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004617{
hayeswang6cc69f22014-10-17 16:55:08 +08004618 struct net_device *netdev = tp->netdev;
4619 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004620
hayeswang26afec32017-01-26 09:38:31 +08004621 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4622 smp_mb__after_atomic();
4623
hayeswang8fb28062017-01-10 17:04:06 +08004624 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004625 u32 rcr = 0;
4626
hayeswang75dc6922017-01-10 17:04:07 +08004627 if (netif_carrier_ok(netdev)) {
4628 u32 ocp_data;
4629
4630 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4631 ocp_data = rcr & ~RCR_ACPT_ALL;
4632 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4633 rxdy_gated_en(tp, true);
4634 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4635 PLA_OOB_CTRL);
4636 if (!(ocp_data & RXFIFO_EMPTY)) {
4637 rxdy_gated_en(tp, false);
4638 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004639 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4640 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004641 ret = -EBUSY;
4642 goto out1;
4643 }
4644 }
4645
hayeswang8fb28062017-01-10 17:04:06 +08004646 clear_bit(WORK_ENABLE, &tp->flags);
4647 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004648
hayeswang8fb28062017-01-10 17:04:06 +08004649 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004650
4651 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004652 struct napi_struct *napi = &tp->napi;
4653
4654 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004655 rtl_stop_rx(tp);
4656 rxdy_gated_en(tp, false);
4657 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004658 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004659 }
hayeswangbd882982017-06-13 15:14:40 +08004660
4661 if (delay_autosuspend(tp)) {
4662 rtl8152_runtime_resume(tp);
4663 ret = -EBUSY;
4664 }
hayeswang6cc69f22014-10-17 16:55:08 +08004665 }
4666
hayeswang8fb28062017-01-10 17:04:06 +08004667out1:
4668 return ret;
4669}
4670
4671static int rtl8152_system_suspend(struct r8152 *tp)
4672{
4673 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004674
4675 netif_device_detach(netdev);
4676
hayeswange3bd1a82014-10-29 11:12:17 +08004677 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004678 struct napi_struct *napi = &tp->napi;
4679
hayeswangac718b62013-05-02 16:01:25 +00004680 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004681 usb_kill_urb(tp->intr_urb);
Hayes Wangd2187f82019-08-19 14:40:36 +08004682 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08004683 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004684 cancel_delayed_work_sync(&tp->schedule);
4685 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004686 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08004687 tasklet_enable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00004688 }
hayeswang8fb28062017-01-10 17:04:06 +08004689
zhong jiangf7419172018-08-09 09:39:13 +08004690 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004691}
4692
4693static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4694{
4695 struct r8152 *tp = usb_get_intfdata(intf);
4696 int ret;
4697
4698 mutex_lock(&tp->control);
4699
4700 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004701 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004702 else
4703 ret = rtl8152_system_suspend(tp);
4704
hayeswangb5403272014-10-09 18:00:26 +08004705 mutex_unlock(&tp->control);
4706
hayeswang6cc69f22014-10-17 16:55:08 +08004707 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004708}
4709
4710static int rtl8152_resume(struct usb_interface *intf)
4711{
4712 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004713 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004714
hayeswangb5403272014-10-09 18:00:26 +08004715 mutex_lock(&tp->control);
4716
hayeswang21cbd0e2017-06-13 15:14:39 +08004717 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4718 ret = rtl8152_runtime_resume(tp);
4719 else
4720 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004721
hayeswangb5403272014-10-09 18:00:26 +08004722 mutex_unlock(&tp->control);
4723
hayeswang21cbd0e2017-06-13 15:14:39 +08004724 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004725}
4726
hayeswang7ec25412016-01-04 14:38:46 +08004727static int rtl8152_reset_resume(struct usb_interface *intf)
4728{
4729 struct r8152 *tp = usb_get_intfdata(intf);
4730
4731 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004732 mutex_lock(&tp->control);
4733 tp->rtl_ops.init(tp);
4734 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4735 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004736 return rtl8152_resume(intf);
4737}
4738
hayeswang21ff2e82014-02-18 21:49:06 +08004739static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4740{
4741 struct r8152 *tp = netdev_priv(dev);
4742
hayeswang9a4be1b2014-02-18 21:49:07 +08004743 if (usb_autopm_get_interface(tp->intf) < 0)
4744 return;
4745
hayeswang7daed8d2015-07-24 13:54:24 +08004746 if (!rtl_can_wakeup(tp)) {
4747 wol->supported = 0;
4748 wol->wolopts = 0;
4749 } else {
4750 mutex_lock(&tp->control);
4751 wol->supported = WAKE_ANY;
4752 wol->wolopts = __rtl_get_wol(tp);
4753 mutex_unlock(&tp->control);
4754 }
hayeswangb5403272014-10-09 18:00:26 +08004755
hayeswang9a4be1b2014-02-18 21:49:07 +08004756 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004757}
4758
4759static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4760{
4761 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004762 int ret;
4763
hayeswang7daed8d2015-07-24 13:54:24 +08004764 if (!rtl_can_wakeup(tp))
4765 return -EOPNOTSUPP;
4766
Florian Fainellif2750df2018-09-28 16:18:54 -07004767 if (wol->wolopts & ~WAKE_ANY)
4768 return -EINVAL;
4769
hayeswang9a4be1b2014-02-18 21:49:07 +08004770 ret = usb_autopm_get_interface(tp->intf);
4771 if (ret < 0)
4772 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004773
hayeswangb5403272014-10-09 18:00:26 +08004774 mutex_lock(&tp->control);
4775
hayeswang21ff2e82014-02-18 21:49:06 +08004776 __rtl_set_wol(tp, wol->wolopts);
4777 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4778
hayeswangb5403272014-10-09 18:00:26 +08004779 mutex_unlock(&tp->control);
4780
hayeswang9a4be1b2014-02-18 21:49:07 +08004781 usb_autopm_put_interface(tp->intf);
4782
4783out_set_wol:
4784 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004785}
4786
hayeswanga5ec27c2014-02-18 21:49:11 +08004787static u32 rtl8152_get_msglevel(struct net_device *dev)
4788{
4789 struct r8152 *tp = netdev_priv(dev);
4790
4791 return tp->msg_enable;
4792}
4793
4794static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4795{
4796 struct r8152 *tp = netdev_priv(dev);
4797
4798 tp->msg_enable = value;
4799}
4800
hayeswangac718b62013-05-02 16:01:25 +00004801static void rtl8152_get_drvinfo(struct net_device *netdev,
4802 struct ethtool_drvinfo *info)
4803{
4804 struct r8152 *tp = netdev_priv(netdev);
4805
hayeswangb0b46c72014-08-26 10:08:23 +08004806 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4807 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004808 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4809}
4810
4811static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004812int rtl8152_get_link_ksettings(struct net_device *netdev,
4813 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004814{
4815 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004816 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004817
4818 if (!tp->mii.mdio_read)
4819 return -EOPNOTSUPP;
4820
hayeswang8d4a4d72014-10-09 18:00:25 +08004821 ret = usb_autopm_get_interface(tp->intf);
4822 if (ret < 0)
4823 goto out;
4824
hayeswangb5403272014-10-09 18:00:26 +08004825 mutex_lock(&tp->control);
4826
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004827 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004828
hayeswangb5403272014-10-09 18:00:26 +08004829 mutex_unlock(&tp->control);
4830
hayeswang8d4a4d72014-10-09 18:00:25 +08004831 usb_autopm_put_interface(tp->intf);
4832
4833out:
4834 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004835}
4836
Philippe Reynes06144dc2017-03-12 22:41:58 +01004837static int rtl8152_set_link_ksettings(struct net_device *dev,
4838 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004839{
4840 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004841 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004842
hayeswang9a4be1b2014-02-18 21:49:07 +08004843 ret = usb_autopm_get_interface(tp->intf);
4844 if (ret < 0)
4845 goto out;
4846
hayeswangb5403272014-10-09 18:00:26 +08004847 mutex_lock(&tp->control);
4848
Philippe Reynes06144dc2017-03-12 22:41:58 +01004849 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4850 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004851 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004852 tp->autoneg = cmd->base.autoneg;
4853 tp->speed = cmd->base.speed;
4854 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004855 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004856
hayeswangb5403272014-10-09 18:00:26 +08004857 mutex_unlock(&tp->control);
4858
hayeswang9a4be1b2014-02-18 21:49:07 +08004859 usb_autopm_put_interface(tp->intf);
4860
4861out:
4862 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004863}
4864
hayeswang4f1d4d52014-03-11 16:24:19 +08004865static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4866 "tx_packets",
4867 "rx_packets",
4868 "tx_errors",
4869 "rx_errors",
4870 "rx_missed",
4871 "align_errors",
4872 "tx_single_collisions",
4873 "tx_multi_collisions",
4874 "rx_unicast",
4875 "rx_broadcast",
4876 "rx_multicast",
4877 "tx_aborted",
4878 "tx_underrun",
4879};
4880
4881static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4882{
4883 switch (sset) {
4884 case ETH_SS_STATS:
4885 return ARRAY_SIZE(rtl8152_gstrings);
4886 default:
4887 return -EOPNOTSUPP;
4888 }
4889}
4890
4891static void rtl8152_get_ethtool_stats(struct net_device *dev,
4892 struct ethtool_stats *stats, u64 *data)
4893{
4894 struct r8152 *tp = netdev_priv(dev);
4895 struct tally_counter tally;
4896
hayeswang0b030242014-07-08 14:49:28 +08004897 if (usb_autopm_get_interface(tp->intf) < 0)
4898 return;
4899
hayeswang4f1d4d52014-03-11 16:24:19 +08004900 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4901
hayeswang0b030242014-07-08 14:49:28 +08004902 usb_autopm_put_interface(tp->intf);
4903
hayeswang4f1d4d52014-03-11 16:24:19 +08004904 data[0] = le64_to_cpu(tally.tx_packets);
4905 data[1] = le64_to_cpu(tally.rx_packets);
4906 data[2] = le64_to_cpu(tally.tx_errors);
4907 data[3] = le32_to_cpu(tally.rx_errors);
4908 data[4] = le16_to_cpu(tally.rx_missed);
4909 data[5] = le16_to_cpu(tally.align_errors);
4910 data[6] = le32_to_cpu(tally.tx_one_collision);
4911 data[7] = le32_to_cpu(tally.tx_multi_collision);
4912 data[8] = le64_to_cpu(tally.rx_unicast);
4913 data[9] = le64_to_cpu(tally.rx_broadcast);
4914 data[10] = le32_to_cpu(tally.rx_multicast);
4915 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004916 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004917}
4918
4919static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4920{
4921 switch (stringset) {
4922 case ETH_SS_STATS:
4923 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4924 break;
4925 }
4926}
4927
hayeswangdf35d282014-09-25 20:54:02 +08004928static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4929{
Hayes Wangf4a93be2019-08-23 15:33:40 +08004930 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08004931 u16 val;
4932
4933 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4934 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4935
4936 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4937 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4938
4939 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4940 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4941
Hayes Wangf4a93be2019-08-23 15:33:40 +08004942 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08004943 eee->eee_active = !!(supported & adv & lp);
4944 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004945 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08004946 eee->lp_advertised = lp;
4947
4948 return 0;
4949}
4950
4951static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4952{
4953 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4954
Hayes Wangf4a93be2019-08-23 15:33:40 +08004955 tp->eee_en = eee->eee_enabled;
4956 tp->eee_adv = val;
4957
Hayes Wange7bde562019-08-23 15:33:41 +08004958 rtl_eee_enable(tp, tp->eee_en);
hayeswangdf35d282014-09-25 20:54:02 +08004959
4960 return 0;
4961}
4962
4963static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4964{
Hayes Wangf4a93be2019-08-23 15:33:40 +08004965 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08004966 u16 val;
4967
4968 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4969 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4970
4971 val = ocp_reg_read(tp, OCP_EEE_ADV);
4972 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4973
4974 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4975 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4976
Hayes Wangf4a93be2019-08-23 15:33:40 +08004977 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08004978 eee->eee_active = !!(supported & adv & lp);
4979 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004980 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08004981 eee->lp_advertised = lp;
4982
4983 return 0;
4984}
4985
hayeswangdf35d282014-09-25 20:54:02 +08004986static int
4987rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4988{
4989 struct r8152 *tp = netdev_priv(net);
4990 int ret;
4991
4992 ret = usb_autopm_get_interface(tp->intf);
4993 if (ret < 0)
4994 goto out;
4995
hayeswangb5403272014-10-09 18:00:26 +08004996 mutex_lock(&tp->control);
4997
hayeswangdf35d282014-09-25 20:54:02 +08004998 ret = tp->rtl_ops.eee_get(tp, edata);
4999
hayeswangb5403272014-10-09 18:00:26 +08005000 mutex_unlock(&tp->control);
5001
hayeswangdf35d282014-09-25 20:54:02 +08005002 usb_autopm_put_interface(tp->intf);
5003
5004out:
5005 return ret;
5006}
5007
5008static int
5009rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
5010{
5011 struct r8152 *tp = netdev_priv(net);
5012 int ret;
5013
5014 ret = usb_autopm_get_interface(tp->intf);
5015 if (ret < 0)
5016 goto out;
5017
hayeswangb5403272014-10-09 18:00:26 +08005018 mutex_lock(&tp->control);
5019
hayeswangdf35d282014-09-25 20:54:02 +08005020 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08005021 if (!ret)
5022 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08005023
hayeswangb5403272014-10-09 18:00:26 +08005024 mutex_unlock(&tp->control);
5025
hayeswangdf35d282014-09-25 20:54:02 +08005026 usb_autopm_put_interface(tp->intf);
5027
5028out:
5029 return ret;
5030}
5031
hayeswang8884f502014-10-28 14:05:52 +08005032static int rtl8152_nway_reset(struct net_device *dev)
5033{
5034 struct r8152 *tp = netdev_priv(dev);
5035 int ret;
5036
5037 ret = usb_autopm_get_interface(tp->intf);
5038 if (ret < 0)
5039 goto out;
5040
5041 mutex_lock(&tp->control);
5042
5043 ret = mii_nway_restart(&tp->mii);
5044
5045 mutex_unlock(&tp->control);
5046
5047 usb_autopm_put_interface(tp->intf);
5048
5049out:
5050 return ret;
5051}
5052
hayeswangefb3dd82015-02-12 14:33:48 +08005053static int rtl8152_get_coalesce(struct net_device *netdev,
5054 struct ethtool_coalesce *coalesce)
5055{
5056 struct r8152 *tp = netdev_priv(netdev);
5057
5058 switch (tp->version) {
5059 case RTL_VER_01:
5060 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005061 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08005062 return -EOPNOTSUPP;
5063 default:
5064 break;
5065 }
5066
5067 coalesce->rx_coalesce_usecs = tp->coalesce;
5068
5069 return 0;
5070}
5071
5072static int rtl8152_set_coalesce(struct net_device *netdev,
5073 struct ethtool_coalesce *coalesce)
5074{
5075 struct r8152 *tp = netdev_priv(netdev);
5076 int ret;
5077
5078 switch (tp->version) {
5079 case RTL_VER_01:
5080 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005081 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08005082 return -EOPNOTSUPP;
5083 default:
5084 break;
5085 }
5086
5087 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
5088 return -EINVAL;
5089
5090 ret = usb_autopm_get_interface(tp->intf);
5091 if (ret < 0)
5092 return ret;
5093
5094 mutex_lock(&tp->control);
5095
5096 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
5097 tp->coalesce = coalesce->rx_coalesce_usecs;
5098
Hayes Wang9fae5412019-07-03 15:11:56 +08005099 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
5100 netif_stop_queue(netdev);
5101 napi_disable(&tp->napi);
5102 tp->rtl_ops.disable(tp);
5103 tp->rtl_ops.enable(tp);
5104 rtl_start_rx(tp);
5105 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5106 _rtl8152_set_rx_mode(netdev);
5107 napi_enable(&tp->napi);
5108 netif_wake_queue(netdev);
5109 }
hayeswangefb3dd82015-02-12 14:33:48 +08005110 }
5111
5112 mutex_unlock(&tp->control);
5113
5114 usb_autopm_put_interface(tp->intf);
5115
5116 return ret;
5117}
5118
Hayes Wange4a50172019-08-13 11:42:09 +08005119static int rtl8152_get_tunable(struct net_device *netdev,
5120 const struct ethtool_tunable *tunable, void *d)
5121{
5122 struct r8152 *tp = netdev_priv(netdev);
5123
5124 switch (tunable->id) {
5125 case ETHTOOL_RX_COPYBREAK:
5126 *(u32 *)d = tp->rx_copybreak;
5127 break;
5128 default:
5129 return -EOPNOTSUPP;
5130 }
5131
5132 return 0;
5133}
5134
5135static int rtl8152_set_tunable(struct net_device *netdev,
5136 const struct ethtool_tunable *tunable,
5137 const void *d)
5138{
5139 struct r8152 *tp = netdev_priv(netdev);
5140 u32 val;
5141
5142 switch (tunable->id) {
5143 case ETHTOOL_RX_COPYBREAK:
5144 val = *(u32 *)d;
5145 if (val < ETH_ZLEN) {
5146 netif_err(tp, rx_err, netdev,
5147 "Invalid rx copy break value\n");
5148 return -EINVAL;
5149 }
5150
5151 if (tp->rx_copybreak != val) {
5152 napi_disable(&tp->napi);
5153 tp->rx_copybreak = val;
5154 napi_enable(&tp->napi);
5155 }
5156 break;
5157 default:
5158 return -EOPNOTSUPP;
5159 }
5160
5161 return 0;
5162}
5163
5164static void rtl8152_get_ringparam(struct net_device *netdev,
5165 struct ethtool_ringparam *ring)
5166{
5167 struct r8152 *tp = netdev_priv(netdev);
5168
5169 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
5170 ring->rx_pending = tp->rx_pending;
5171}
5172
5173static int rtl8152_set_ringparam(struct net_device *netdev,
5174 struct ethtool_ringparam *ring)
5175{
5176 struct r8152 *tp = netdev_priv(netdev);
5177
5178 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
5179 return -EINVAL;
5180
5181 if (tp->rx_pending != ring->rx_pending) {
5182 napi_disable(&tp->napi);
5183 tp->rx_pending = ring->rx_pending;
5184 napi_enable(&tp->napi);
5185 }
5186
5187 return 0;
5188}
5189
Julia Lawall407a4712016-09-01 00:21:22 +02005190static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00005191 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00005192 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08005193 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08005194 .get_msglevel = rtl8152_get_msglevel,
5195 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08005196 .get_wol = rtl8152_get_wol,
5197 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08005198 .get_strings = rtl8152_get_strings,
5199 .get_sset_count = rtl8152_get_sset_count,
5200 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08005201 .get_coalesce = rtl8152_get_coalesce,
5202 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08005203 .get_eee = rtl_ethtool_get_eee,
5204 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01005205 .get_link_ksettings = rtl8152_get_link_ksettings,
5206 .set_link_ksettings = rtl8152_set_link_ksettings,
Hayes Wange4a50172019-08-13 11:42:09 +08005207 .get_tunable = rtl8152_get_tunable,
5208 .set_tunable = rtl8152_set_tunable,
5209 .get_ringparam = rtl8152_get_ringparam,
5210 .set_ringparam = rtl8152_set_ringparam,
hayeswangac718b62013-05-02 16:01:25 +00005211};
5212
5213static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
5214{
5215 struct r8152 *tp = netdev_priv(netdev);
5216 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08005217 int res;
5218
hayeswang68714382014-04-11 17:54:31 +08005219 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5220 return -ENODEV;
5221
hayeswang9a4be1b2014-02-18 21:49:07 +08005222 res = usb_autopm_get_interface(tp->intf);
5223 if (res < 0)
5224 goto out;
hayeswangac718b62013-05-02 16:01:25 +00005225
5226 switch (cmd) {
5227 case SIOCGMIIPHY:
5228 data->phy_id = R8152_PHY_ID; /* Internal PHY */
5229 break;
5230
5231 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08005232 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005233 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08005234 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005235 break;
5236
5237 case SIOCSMIIREG:
5238 if (!capable(CAP_NET_ADMIN)) {
5239 res = -EPERM;
5240 break;
5241 }
hayeswangb5403272014-10-09 18:00:26 +08005242 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005243 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08005244 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005245 break;
5246
5247 default:
5248 res = -EOPNOTSUPP;
5249 }
5250
hayeswang9a4be1b2014-02-18 21:49:07 +08005251 usb_autopm_put_interface(tp->intf);
5252
5253out:
hayeswangac718b62013-05-02 16:01:25 +00005254 return res;
5255}
5256
hayeswang69b4b7a2014-07-10 10:58:54 +08005257static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5258{
5259 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08005260 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005261
5262 switch (tp->version) {
5263 case RTL_VER_01:
5264 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005265 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04005266 dev->mtu = new_mtu;
5267 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08005268 default:
5269 break;
5270 }
5271
hayeswang396e2e22015-02-12 14:33:47 +08005272 ret = usb_autopm_get_interface(tp->intf);
5273 if (ret < 0)
5274 return ret;
5275
5276 mutex_lock(&tp->control);
5277
hayeswang69b4b7a2014-07-10 10:58:54 +08005278 dev->mtu = new_mtu;
5279
hayeswang210c4f72017-03-20 16:13:44 +08005280 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005281 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005282
5283 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5284
5285 if (netif_carrier_ok(dev))
5286 r8153_set_rx_early_size(tp);
5287 }
hayeswang396e2e22015-02-12 14:33:47 +08005288
5289 mutex_unlock(&tp->control);
5290
5291 usb_autopm_put_interface(tp->intf);
5292
5293 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005294}
5295
hayeswangac718b62013-05-02 16:01:25 +00005296static const struct net_device_ops rtl8152_netdev_ops = {
5297 .ndo_open = rtl8152_open,
5298 .ndo_stop = rtl8152_close,
5299 .ndo_do_ioctl = rtl8152_ioctl,
5300 .ndo_start_xmit = rtl8152_start_xmit,
5301 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005302 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005303 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5304 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005305 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005306 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005307 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005308};
5309
hayeswange3fe0b12014-01-02 11:22:39 +08005310static void rtl8152_unload(struct r8152 *tp)
5311{
hayeswang68714382014-04-11 17:54:31 +08005312 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5313 return;
5314
hayeswang00a5e362014-02-18 21:48:59 +08005315 if (tp->version != RTL_VER_01)
5316 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005317}
5318
hayeswang43779f82014-01-02 11:25:10 +08005319static void rtl8153_unload(struct r8152 *tp)
5320{
hayeswang68714382014-04-11 17:54:31 +08005321 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5322 return;
5323
hayeswang49be1722014-10-01 13:25:11 +08005324 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005325}
5326
hayeswang65b82d62017-06-15 14:44:03 +08005327static void rtl8153b_unload(struct r8152 *tp)
5328{
5329 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5330 return;
5331
5332 r8153b_power_cut_en(tp, false);
5333}
5334
hayeswang55b65472014-11-06 12:47:39 +08005335static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005336{
5337 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005338 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005339
hayeswang55b65472014-11-06 12:47:39 +08005340 switch (tp->version) {
5341 case RTL_VER_01:
5342 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005343 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005344 ops->init = r8152b_init;
5345 ops->enable = rtl8152_enable;
5346 ops->disable = rtl8152_disable;
5347 ops->up = rtl8152_up;
5348 ops->down = rtl8152_down;
5349 ops->unload = rtl8152_unload;
5350 ops->eee_get = r8152_get_eee;
5351 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005352 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005353 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005354 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005355 tp->rx_buf_sz = 16 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005356 tp->eee_en = true;
5357 tp->eee_adv = MDIO_EEE_100TX;
hayeswang43779f82014-01-02 11:25:10 +08005358 break;
5359
hayeswang55b65472014-11-06 12:47:39 +08005360 case RTL_VER_03:
5361 case RTL_VER_04:
5362 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005363 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005364 ops->init = r8153_init;
5365 ops->enable = rtl8153_enable;
5366 ops->disable = rtl8153_disable;
5367 ops->up = rtl8153_up;
5368 ops->down = rtl8153_down;
5369 ops->unload = rtl8153_unload;
5370 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08005371 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005372 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005373 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005374 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005375 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005376 tp->eee_en = true;
5377 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswangc81229c2014-01-02 11:22:42 +08005378 break;
5379
hayeswang65b82d62017-06-15 14:44:03 +08005380 case RTL_VER_08:
5381 case RTL_VER_09:
5382 ops->init = r8153b_init;
5383 ops->enable = rtl8153_enable;
5384 ops->disable = rtl8153b_disable;
5385 ops->up = rtl8153b_up;
5386 ops->down = rtl8153b_down;
5387 ops->unload = rtl8153b_unload;
5388 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08005389 ops->eee_set = r8152_set_eee;
hayeswang65b82d62017-06-15 14:44:03 +08005390 ops->in_nway = rtl8153_in_nway;
5391 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5392 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08005393 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08005394 tp->eee_en = true;
5395 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswang65b82d62017-06-15 14:44:03 +08005396 break;
5397
hayeswangc81229c2014-01-02 11:22:42 +08005398 default:
hayeswang55b65472014-11-06 12:47:39 +08005399 ret = -ENODEV;
5400 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005401 break;
5402 }
5403
5404 return ret;
5405}
5406
hayeswang33928ee2017-03-17 11:20:13 +08005407static u8 rtl_get_version(struct usb_interface *intf)
5408{
5409 struct usb_device *udev = interface_to_usbdev(intf);
5410 u32 ocp_data = 0;
5411 __le32 *tmp;
5412 u8 version;
5413 int ret;
5414
5415 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5416 if (!tmp)
5417 return 0;
5418
5419 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5420 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5421 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5422 if (ret > 0)
5423 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5424
5425 kfree(tmp);
5426
5427 switch (ocp_data) {
5428 case 0x4c00:
5429 version = RTL_VER_01;
5430 break;
5431 case 0x4c10:
5432 version = RTL_VER_02;
5433 break;
5434 case 0x5c00:
5435 version = RTL_VER_03;
5436 break;
5437 case 0x5c10:
5438 version = RTL_VER_04;
5439 break;
5440 case 0x5c20:
5441 version = RTL_VER_05;
5442 break;
5443 case 0x5c30:
5444 version = RTL_VER_06;
5445 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005446 case 0x4800:
5447 version = RTL_VER_07;
5448 break;
hayeswang65b82d62017-06-15 14:44:03 +08005449 case 0x6000:
5450 version = RTL_VER_08;
5451 break;
5452 case 0x6010:
5453 version = RTL_VER_09;
5454 break;
hayeswang33928ee2017-03-17 11:20:13 +08005455 default:
5456 version = RTL_VER_UNKNOWN;
5457 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5458 break;
5459 }
5460
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005461 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5462
hayeswang33928ee2017-03-17 11:20:13 +08005463 return version;
5464}
5465
hayeswangac718b62013-05-02 16:01:25 +00005466static int rtl8152_probe(struct usb_interface *intf,
5467 const struct usb_device_id *id)
5468{
5469 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005470 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005471 struct r8152 *tp;
5472 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005473 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005474
hayeswang33928ee2017-03-17 11:20:13 +08005475 if (version == RTL_VER_UNKNOWN)
5476 return -ENODEV;
5477
hayeswang10c32712014-03-04 20:47:48 +08005478 if (udev->actconfig->desc.bConfigurationValue != 1) {
5479 usb_driver_set_configuration(udev, 1);
5480 return -ENODEV;
5481 }
5482
5483 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005484 netdev = alloc_etherdev(sizeof(struct r8152));
5485 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005486 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005487 return -ENOMEM;
5488 }
5489
hayeswangebc2ec482013-08-14 20:54:38 +08005490 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005491 tp = netdev_priv(netdev);
5492 tp->msg_enable = 0x7FFF;
5493
hayeswange3ad4122014-01-06 17:08:42 +08005494 tp->udev = udev;
5495 tp->netdev = netdev;
5496 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005497 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005498
hayeswang33928ee2017-03-17 11:20:13 +08005499 switch (version) {
5500 case RTL_VER_01:
5501 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005502 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005503 tp->mii.supports_gmii = 0;
5504 break;
5505 default:
5506 tp->mii.supports_gmii = 1;
5507 break;
5508 }
5509
hayeswang55b65472014-11-06 12:47:39 +08005510 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005511 if (ret)
5512 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005513
hayeswangb5403272014-10-09 18:00:26 +08005514 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005515 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005516 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
Hayes Wangd2187f82019-08-19 14:40:36 +08005517 tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp);
5518 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00005519
hayeswangac718b62013-05-02 16:01:25 +00005520 netdev->netdev_ops = &rtl8152_netdev_ops;
5521 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005522
hayeswang60c89072014-03-07 11:04:39 +08005523 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005524 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005525 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5526 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005527 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005528 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005529 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005530 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005531 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5532 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5533 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005534
hayeswang19c0f402017-01-11 16:25:34 +08005535 if (tp->version == RTL_VER_01) {
5536 netdev->features &= ~NETIF_F_RXCSUM;
5537 netdev->hw_features &= ~NETIF_F_RXCSUM;
5538 }
5539
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005540 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5541 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005542 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5543 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5544 }
5545
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005546 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005547 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005548
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005549 /* MTU range: 68 - 1500 or 9194 */
5550 netdev->min_mtu = ETH_MIN_MTU;
5551 switch (tp->version) {
5552 case RTL_VER_01:
5553 case RTL_VER_02:
5554 netdev->max_mtu = ETH_DATA_LEN;
5555 break;
5556 default:
5557 netdev->max_mtu = RTL8153_MAX_MTU;
5558 break;
5559 }
5560
hayeswangac718b62013-05-02 16:01:25 +00005561 tp->mii.dev = netdev;
5562 tp->mii.mdio_read = read_mii_word;
5563 tp->mii.mdio_write = write_mii_word;
5564 tp->mii.phy_id_mask = 0x3f;
5565 tp->mii.reg_num_mask = 0x1f;
5566 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005567
hayeswangaa7e26b2016-06-13 17:49:38 +08005568 tp->autoneg = AUTONEG_ENABLE;
5569 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5570 tp->duplex = DUPLEX_FULL;
5571
Hayes Wange4a50172019-08-13 11:42:09 +08005572 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
5573 tp->rx_pending = 10 * RTL8152_MAX_RX;
5574
hayeswang9a4be1b2014-02-18 21:49:07 +08005575 intf->needs_remote_wakeup = 1;
5576
hayeswangc81229c2014-01-02 11:22:42 +08005577 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005578 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005579 set_ethernet_addr(tp);
5580
hayeswangac718b62013-05-02 16:01:25 +00005581 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005582 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005583
hayeswangebc2ec482013-08-14 20:54:38 +08005584 ret = register_netdev(netdev);
5585 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005586 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005587 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005588 }
5589
hayeswang7daed8d2015-07-24 13:54:24 +08005590 if (!rtl_can_wakeup(tp))
5591 __rtl_set_wol(tp, 0);
5592
hayeswang21ff2e82014-02-18 21:49:06 +08005593 tp->saved_wolopts = __rtl_get_wol(tp);
5594 if (tp->saved_wolopts)
5595 device_set_wakeup_enable(&udev->dev, true);
5596 else
5597 device_set_wakeup_enable(&udev->dev, false);
5598
Hayes Wang4a8deae2014-01-07 11:18:22 +08005599 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005600
5601 return 0;
5602
hayeswangac718b62013-05-02 16:01:25 +00005603out1:
hayeswangd823ab62015-01-12 12:06:23 +08005604 netif_napi_del(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005605 tasklet_kill(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08005606 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005607out:
5608 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005609 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005610}
5611
hayeswangac718b62013-05-02 16:01:25 +00005612static void rtl8152_disconnect(struct usb_interface *intf)
5613{
5614 struct r8152 *tp = usb_get_intfdata(intf);
5615
5616 usb_set_intfdata(intf, NULL);
5617 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08005618 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08005619
hayeswangd823ab62015-01-12 12:06:23 +08005620 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005621 unregister_netdev(tp->netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08005622 tasklet_kill(&tp->tx_tl);
hayeswanga028a9e2016-06-13 17:49:36 +08005623 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005624 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005625 free_netdev(tp->netdev);
5626 }
5627}
5628
hayeswangd9a28c52014-12-04 10:43:11 +08005629#define REALTEK_USB_DEVICE(vend, prod) \
5630 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5631 USB_DEVICE_ID_MATCH_INT_CLASS, \
5632 .idVendor = (vend), \
5633 .idProduct = (prod), \
5634 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5635}, \
5636{ \
5637 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5638 USB_DEVICE_ID_MATCH_DEVICE, \
5639 .idVendor = (vend), \
5640 .idProduct = (prod), \
5641 .bInterfaceClass = USB_CLASS_COMM, \
5642 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5643 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5644
hayeswangac718b62013-05-02 16:01:25 +00005645/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305646static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005647 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005648 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5649 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005650 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5651 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005652 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005653 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005654 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5655 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5656 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5657 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5658 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005659 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005660 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005661 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005662 {}
5663};
5664
5665MODULE_DEVICE_TABLE(usb, rtl8152_table);
5666
5667static struct usb_driver rtl8152_driver = {
5668 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005669 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005670 .probe = rtl8152_probe,
5671 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005672 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005673 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005674 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005675 .pre_reset = rtl8152_pre_reset,
5676 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005677 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005678 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005679};
5680
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005681module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005682
5683MODULE_AUTHOR(DRIVER_AUTHOR);
5684MODULE_DESCRIPTION(DRIVER_DESC);
5685MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005686MODULE_VERSION(DRIVER_VERSION);