blob: 0cc03a9ff5457922a581cbdf5d76d34186575ae6 [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>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050025#include <linux/acpi.h>
hayeswangac718b62013-05-02 16:01:25 +000026
hayeswangd0942472015-09-07 11:57:43 +080027/* Information for net-next */
hayeswang65b82d62017-06-15 14:44:03 +080028#define NETNEXT_VERSION "09"
hayeswangd0942472015-09-07 11:57:43 +080029
30/* Information for net */
Hayes Wangffa9fec2019-07-04 17:36:32 +080031#define NET_VERSION "10"
hayeswangd0942472015-09-07 11:57:43 +080032
33#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000034#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080035#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000036#define MODULENAME "r8152"
37
38#define R8152_PHY_ID 32
39
40#define PLA_IDR 0xc000
41#define PLA_RCR 0xc010
42#define PLA_RMS 0xc016
43#define PLA_RXFIFO_CTRL0 0xc0a0
44#define PLA_RXFIFO_CTRL1 0xc0a4
45#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080046#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_FMC 0xc0b4
48#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080049#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080050#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080052#define PLA_BACKUP 0xd000
Kevin Lo59c0b472019-08-01 11:29:38 +080053#define PLA_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080054#define PLA_TEREDO_TIMER 0xd2cc
55#define PLA_REALWOW_TIMER 0xd2e8
Hayes Wang13e04fbf2019-07-01 15:53:19 +080056#define PLA_SUSPEND_FLAG 0xd38a
57#define PLA_INDICATE_FALG 0xd38c
58#define PLA_EXTRA_STATUS 0xd398
hayeswang65b82d62017-06-15 14:44:03 +080059#define PLA_EFUSE_DATA 0xdd00
60#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000061#define PLA_LEDSEL 0xdd90
62#define PLA_LED_FEATURE 0xdd92
63#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080064#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000065#define PLA_GPHY_INTR_IMR 0xe022
66#define PLA_EEE_CR 0xe040
67#define PLA_EEEP_CR 0xe080
68#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080069#define PLA_MAC_PWR_CTRL2 0xe0ca
70#define PLA_MAC_PWR_CTRL3 0xe0cc
71#define PLA_MAC_PWR_CTRL4 0xe0ce
72#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000073#define PLA_TCR0 0xe610
74#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080075#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000076#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080077#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000078#define PLA_CR 0xe813
79#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080080#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
81#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000082#define PLA_CONFIG5 0xe822
83#define PLA_PHY_PWR 0xe84c
84#define PLA_OOB_CTRL 0xe84f
85#define PLA_CPCR 0xe854
86#define PLA_MISC_0 0xe858
87#define PLA_MISC_1 0xe85a
88#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080089#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000090#define PLA_SFF_STS_7 0xe8de
91#define PLA_PHYSTATUS 0xe908
92#define PLA_BP_BA 0xfc26
93#define PLA_BP_0 0xfc28
94#define PLA_BP_1 0xfc2a
95#define PLA_BP_2 0xfc2c
96#define PLA_BP_3 0xfc2e
97#define PLA_BP_4 0xfc30
98#define PLA_BP_5 0xfc32
99#define PLA_BP_6 0xfc34
100#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800101#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000102
hayeswang65bab842015-02-12 16:20:46 +0800103#define USB_USB2PHY 0xb41e
104#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800105#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800106#define USB_CSR_DUMMY1 0xb464
107#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000108#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800109#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800110#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800111#define USB_BURST_SIZE 0xcfc0
hayeswang65b82d62017-06-15 14:44:03 +0800112#define USB_LPM_CONFIG 0xcfd8
hayeswangac718b62013-05-02 16:01:25 +0000113#define USB_USB_CTRL 0xd406
114#define USB_PHY_CTRL 0xd408
115#define USB_TX_AGG 0xd40a
116#define USB_RX_BUF_TH 0xd40c
117#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800118#define USB_RX_EARLY_TIMEOUT 0xd42c
119#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800120#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
121#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000122#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800123#define USB_UPT_RXDMA_OWN 0xd437
hayeswang43779f82014-01-02 11:25:10 +0800124#define USB_TOLERANCE 0xd490
125#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800126#define USB_BMU_RESET 0xd4b0
hayeswang65b82d62017-06-15 14:44:03 +0800127#define USB_U1U2_TIMER 0xd4da
hayeswangac718b62013-05-02 16:01:25 +0000128#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800129#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800130#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600131#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800132#define USB_AFE_CTRL2 0xd824
hayeswang65b82d62017-06-15 14:44:03 +0800133#define USB_UPS_CFG 0xd842
134#define USB_UPS_FLAGS 0xd848
hayeswang43779f82014-01-02 11:25:10 +0800135#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000136#define USB_BP_BA 0xfc26
137#define USB_BP_0 0xfc28
138#define USB_BP_1 0xfc2a
139#define USB_BP_2 0xfc2c
140#define USB_BP_3 0xfc2e
141#define USB_BP_4 0xfc30
142#define USB_BP_5 0xfc32
143#define USB_BP_6 0xfc34
144#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800145#define USB_BP_EN 0xfc38
hayeswang65b82d62017-06-15 14:44:03 +0800146#define USB_BP_8 0xfc38
147#define USB_BP_9 0xfc3a
148#define USB_BP_10 0xfc3c
149#define USB_BP_11 0xfc3e
150#define USB_BP_12 0xfc40
151#define USB_BP_13 0xfc42
152#define USB_BP_14 0xfc44
153#define USB_BP_15 0xfc46
154#define USB_BP2_EN 0xfc48
hayeswangac718b62013-05-02 16:01:25 +0000155
156/* OCP Registers */
157#define OCP_ALDPS_CONFIG 0x2010
158#define OCP_EEE_CONFIG1 0x2080
159#define OCP_EEE_CONFIG2 0x2092
160#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800161#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000162#define OCP_EEE_AR 0xa41a
163#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800164#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800165#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800166#define OCP_POWER_CFG 0xa430
167#define OCP_EEE_CFG 0xa432
168#define OCP_SRAM_ADDR 0xa436
169#define OCP_SRAM_DATA 0xa438
170#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800171#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800172#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800173#define OCP_EEE_LPABLE 0xa5d2
hayeswang2dd49e02015-09-07 11:57:44 +0800174#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800175#define OCP_PHY_PATCH_STAT 0xb800
176#define OCP_PHY_PATCH_CMD 0xb820
177#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800178#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800179#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800180
181/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800182#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800183#define SRAM_LPF_CFG 0x8012
184#define SRAM_10M_AMP1 0x8080
185#define SRAM_10M_AMP2 0x8082
186#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000187
188/* PLA_RCR */
189#define RCR_AAP 0x00000001
190#define RCR_APM 0x00000002
191#define RCR_AM 0x00000004
192#define RCR_AB 0x00000008
193#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
194
195/* PLA_RXFIFO_CTRL0 */
196#define RXFIFO_THR1_NORMAL 0x00080002
197#define RXFIFO_THR1_OOB 0x01800003
198
199/* PLA_RXFIFO_CTRL1 */
200#define RXFIFO_THR2_FULL 0x00000060
201#define RXFIFO_THR2_HIGH 0x00000038
202#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800203#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000204
205/* PLA_RXFIFO_CTRL2 */
206#define RXFIFO_THR3_FULL 0x00000078
207#define RXFIFO_THR3_HIGH 0x00000048
208#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800209#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000210
211/* PLA_TXFIFO_CTRL */
212#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800213#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000214
hayeswang65bab842015-02-12 16:20:46 +0800215/* PLA_DMY_REG0 */
216#define ECM_ALDPS 0x0002
217
hayeswangac718b62013-05-02 16:01:25 +0000218/* PLA_FMC */
219#define FMC_FCR_MCU_EN 0x0001
220
221/* PLA_EEEP_CR */
222#define EEEP_CR_EEEP_TX 0x0002
223
hayeswang43779f82014-01-02 11:25:10 +0800224/* PLA_WDT6_CTRL */
225#define WDT6_SET_MODE 0x0010
226
hayeswangac718b62013-05-02 16:01:25 +0000227/* PLA_TCR0 */
228#define TCR0_TX_EMPTY 0x0800
229#define TCR0_AUTO_FIFO 0x0080
230
231/* PLA_TCR1 */
232#define VERSION_MASK 0x7cf0
233
hayeswang69b4b7a2014-07-10 10:58:54 +0800234/* PLA_MTPS */
235#define MTPS_JUMBO (12 * 1024 / 64)
236#define MTPS_DEFAULT (6 * 1024 / 64)
237
hayeswang4f1d4d52014-03-11 16:24:19 +0800238/* PLA_RSTTALLY */
239#define TALLY_RESET 0x0001
240
hayeswangac718b62013-05-02 16:01:25 +0000241/* PLA_CR */
242#define CR_RST 0x10
243#define CR_RE 0x08
244#define CR_TE 0x04
245
246/* PLA_CRWECR */
247#define CRWECR_NORAML 0x00
248#define CRWECR_CONFIG 0xc0
249
250/* PLA_OOB_CTRL */
251#define NOW_IS_OOB 0x80
252#define TXFIFO_EMPTY 0x20
253#define RXFIFO_EMPTY 0x10
254#define LINK_LIST_READY 0x02
255#define DIS_MCU_CLROOB 0x01
256#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
257
258/* PLA_MISC_1 */
259#define RXDY_GATED_EN 0x0008
260
261/* PLA_SFF_STS_7 */
262#define RE_INIT_LL 0x8000
263#define MCU_BORW_EN 0x4000
264
265/* PLA_CPCR */
266#define CPCR_RX_VLAN 0x0040
267
268/* PLA_CFG_WOL */
269#define MAGIC_EN 0x0001
270
hayeswang43779f82014-01-02 11:25:10 +0800271/* PLA_TEREDO_CFG */
272#define TEREDO_SEL 0x8000
273#define TEREDO_WAKE_MASK 0x7f00
274#define TEREDO_RS_EVENT_MASK 0x00fe
275#define OOB_TEREDO_EN 0x0001
276
Kevin Lo59c0b472019-08-01 11:29:38 +0800277/* PLA_BDC_CR */
hayeswangac718b62013-05-02 16:01:25 +0000278#define ALDPS_PROXY_MODE 0x0001
279
hayeswang65b82d62017-06-15 14:44:03 +0800280/* PLA_EFUSE_CMD */
281#define EFUSE_READ_CMD BIT(15)
282#define EFUSE_DATA_BIT16 BIT(7)
283
hayeswang21ff2e82014-02-18 21:49:06 +0800284/* PLA_CONFIG34 */
285#define LINK_ON_WAKE_EN 0x0010
286#define LINK_OFF_WAKE_EN 0x0008
287
hayeswangac718b62013-05-02 16:01:25 +0000288/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800289#define BWF_EN 0x0040
290#define MWF_EN 0x0020
291#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000292#define LAN_WAKE_EN 0x0002
293
294/* PLA_LED_FEATURE */
295#define LED_MODE_MASK 0x0700
296
297/* PLA_PHY_PWR */
298#define TX_10M_IDLE_EN 0x0080
299#define PFM_PWM_SWITCH 0x0040
300
301/* PLA_MAC_PWR_CTRL */
302#define D3_CLK_GATED_EN 0x00004000
303#define MCU_CLK_RATIO 0x07010f07
304#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800305#define ALDPS_SPDWN_RATIO 0x0f87
306
307/* PLA_MAC_PWR_CTRL2 */
308#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800309#define MAC_CLK_SPDWN_EN BIT(15)
hayeswang43779f82014-01-02 11:25:10 +0800310
311/* PLA_MAC_PWR_CTRL3 */
312#define PKT_AVAIL_SPDWN_EN 0x0100
313#define SUSPEND_SPDWN_EN 0x0004
314#define U1U2_SPDWN_EN 0x0002
315#define L1_SPDWN_EN 0x0001
316
317/* PLA_MAC_PWR_CTRL4 */
318#define PWRSAVE_SPDWN_EN 0x1000
319#define RXDV_SPDWN_EN 0x0800
320#define TX10MIDLE_EN 0x0100
321#define TP100_SPDWN_EN 0x0020
322#define TP500_SPDWN_EN 0x0010
323#define TP1000_SPDWN_EN 0x0008
324#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000325
326/* PLA_GPHY_INTR_IMR */
327#define GPHY_STS_MSK 0x0001
328#define SPEED_DOWN_MSK 0x0002
329#define SPDWN_RXDV_MSK 0x0004
330#define SPDWN_LINKCHG_MSK 0x0008
331
332/* PLA_PHYAR */
333#define PHYAR_FLAG 0x80000000
334
335/* PLA_EEE_CR */
336#define EEE_RX_EN 0x0001
337#define EEE_TX_EN 0x0002
338
hayeswang43779f82014-01-02 11:25:10 +0800339/* PLA_BOOT_CTRL */
340#define AUTOLOAD_DONE 0x0002
341
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800342/* PLA_SUSPEND_FLAG */
343#define LINK_CHG_EVENT BIT(0)
344
345/* PLA_INDICATE_FALG */
346#define UPCOMING_RUNTIME_D3 BIT(0)
347
348/* PLA_EXTRA_STATUS */
349#define LINK_CHANGE_FLAG BIT(8)
350
hayeswang65bab842015-02-12 16:20:46 +0800351/* USB_USB2PHY */
352#define USB2PHY_SUSPEND 0x0001
353#define USB2PHY_L1 0x0002
354
355/* USB_SSPHYLINK2 */
356#define pwd_dn_scale_mask 0x3ffe
357#define pwd_dn_scale(x) ((x) << 1)
358
359/* USB_CSR_DUMMY1 */
360#define DYNAMIC_BURST 0x0001
361
362/* USB_CSR_DUMMY2 */
363#define EP4_FULL_FC 0x0001
364
hayeswangac718b62013-05-02 16:01:25 +0000365/* USB_DEV_STAT */
366#define STAT_SPEED_MASK 0x0006
367#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800368#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000369
hayeswang65b82d62017-06-15 14:44:03 +0800370/* USB_LPM_CONFIG */
371#define LPM_U1U2_EN BIT(0)
372
hayeswangac718b62013-05-02 16:01:25 +0000373/* USB_TX_AGG */
374#define TX_AGG_MAX_THRESHOLD 0x03
375
376/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800377#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800378#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800379#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800380#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000381
382/* USB_TX_DMA */
383#define TEST_MODE_DISABLE 0x00000001
384#define TX_SIZE_ADJUST1 0x00000100
385
hayeswang93fe9b12016-06-16 10:55:18 +0800386/* USB_BMU_RESET */
387#define BMU_RESET_EP_IN 0x01
388#define BMU_RESET_EP_OUT 0x02
389
hayeswang65b82d62017-06-15 14:44:03 +0800390/* USB_UPT_RXDMA_OWN */
391#define OWN_UPDATE BIT(0)
392#define OWN_CLEAR BIT(1)
393
hayeswangac718b62013-05-02 16:01:25 +0000394/* USB_UPS_CTRL */
395#define POWER_CUT 0x0100
396
397/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800398#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000399
400/* USB_USB_CTRL */
401#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800402#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000403
hayeswang43779f82014-01-02 11:25:10 +0800404/* USB_U2P3_CTRL */
405#define U2P3_ENABLE 0x0001
406
407/* USB_POWER_CUT */
408#define PWR_EN 0x0001
409#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800410#define UPS_EN BIT(4)
411#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800412
413/* USB_MISC_0 */
414#define PCUT_STATUS 0x0001
415
hayeswang464ec102015-02-12 14:33:46 +0800416/* USB_RX_EARLY_TIMEOUT */
417#define COALESCE_SUPER 85000U
418#define COALESCE_HIGH 250000U
419#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800420
421/* USB_WDT11_CTRL */
422#define TIMER11_EN 0x0001
423
424/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800425/* bit 4 ~ 5: fifo empty boundary */
426#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
427/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800428#define LPM_TIMER_MASK 0x0c
429#define LPM_TIMER_500MS 0x04 /* 500 ms */
430#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800431#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800432
433/* USB_AFE_CTRL2 */
434#define SEN_VAL_MASK 0xf800
435#define SEN_VAL_NORMAL 0xa000
436#define SEL_RXIDLE 0x0100
437
hayeswang65b82d62017-06-15 14:44:03 +0800438/* USB_UPS_CFG */
439#define SAW_CNT_1MS_MASK 0x0fff
440
441/* USB_UPS_FLAGS */
442#define UPS_FLAGS_R_TUNE BIT(0)
443#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
444#define UPS_FLAGS_250M_CKDIV BIT(2)
445#define UPS_FLAGS_EN_ALDPS BIT(3)
446#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
447#define UPS_FLAGS_SPEED_MASK (0xf << 16)
448#define ups_flags_speed(x) ((x) << 16)
449#define UPS_FLAGS_EN_EEE BIT(20)
450#define UPS_FLAGS_EN_500M_EEE BIT(21)
451#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
452#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
453#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
454#define UPS_FLAGS_EN_GREEN BIT(26)
455#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
456
457enum spd_duplex {
458 NWAY_10M_HALF = 1,
459 NWAY_10M_FULL,
460 NWAY_100M_HALF,
461 NWAY_100M_FULL,
462 NWAY_1000M_FULL,
463 FORCE_10M_HALF,
464 FORCE_10M_FULL,
465 FORCE_100M_HALF,
466 FORCE_100M_FULL,
467};
468
hayeswangac718b62013-05-02 16:01:25 +0000469/* OCP_ALDPS_CONFIG */
470#define ENPWRSAVE 0x8000
471#define ENPDNPS 0x0200
472#define LINKENA 0x0100
473#define DIS_SDSAVE 0x0010
474
hayeswang43779f82014-01-02 11:25:10 +0800475/* OCP_PHY_STATUS */
476#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800477#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800478#define PHY_STAT_LAN_ON 3
479#define PHY_STAT_PWRDN 5
480
hayeswang65b82d62017-06-15 14:44:03 +0800481/* OCP_NCTL_CFG */
482#define PGA_RETURN_EN BIT(1)
483
hayeswang43779f82014-01-02 11:25:10 +0800484/* OCP_POWER_CFG */
485#define EEE_CLKDIV_EN 0x8000
486#define EN_ALDPS 0x0004
487#define EN_10M_PLLOFF 0x0001
488
hayeswangac718b62013-05-02 16:01:25 +0000489/* OCP_EEE_CONFIG1 */
490#define RG_TXLPI_MSK_HFDUP 0x8000
491#define RG_MATCLR_EN 0x4000
492#define EEE_10_CAP 0x2000
493#define EEE_NWAY_EN 0x1000
494#define TX_QUIET_EN 0x0200
495#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800496#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800497#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000498#define RG_RXLPI_MSK_HFDUP 0x0008
499#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
500
501/* OCP_EEE_CONFIG2 */
502#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
503#define RG_DACQUIET_EN 0x0400
504#define RG_LDVQUIET_EN 0x0200
505#define RG_CKRSEL 0x0020
506#define RG_EEEPRG_EN 0x0010
507
508/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800509#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800510#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000511#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
512#define MSK_PH 0x0006 /* bit 0 ~ 3 */
513
514/* OCP_EEE_AR */
515/* bit[15:14] function */
516#define FUN_ADDR 0x0000
517#define FUN_DATA 0x4000
518/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000519
hayeswang43779f82014-01-02 11:25:10 +0800520/* OCP_EEE_CFG */
521#define CTAP_SHORT_EN 0x0040
522#define EEE10_EN 0x0010
523
524/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800525#define EN_EEE_CMODE BIT(14)
526#define EN_EEE_1000 BIT(13)
527#define EN_EEE_100 BIT(12)
528#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800529#define EN_10M_BGOFF 0x0080
530
hayeswang2dd49e02015-09-07 11:57:44 +0800531/* OCP_PHY_STATE */
532#define TXDIS_STATE 0x01
533#define ABD_STATE 0x02
534
hayeswang65b82d62017-06-15 14:44:03 +0800535/* OCP_PHY_PATCH_STAT */
536#define PATCH_READY BIT(6)
537
538/* OCP_PHY_PATCH_CMD */
539#define PATCH_REQUEST BIT(4)
540
hayeswang43779f82014-01-02 11:25:10 +0800541/* OCP_ADC_CFG */
542#define CKADSEL_L 0x0100
543#define ADC_EN 0x0080
544#define EN_EMI_L 0x0040
545
hayeswang65b82d62017-06-15 14:44:03 +0800546/* OCP_SYSCLK_CFG */
547#define clk_div_expo(x) (min(x, 5) << 8)
548
549/* SRAM_GREEN_CFG */
550#define GREEN_ETH_EN BIT(15)
551#define R_TUNE_EN BIT(11)
552
hayeswang43779f82014-01-02 11:25:10 +0800553/* SRAM_LPF_CFG */
554#define LPF_AUTO_TUNE 0x8000
555
556/* SRAM_10M_AMP1 */
557#define GDAC_IB_UPALL 0x0008
558
559/* SRAM_10M_AMP2 */
560#define AMP_DN 0x0200
561
562/* SRAM_IMPEDANCE */
563#define RX_DRIVING_MASK 0x6000
564
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500565/* MAC PASSTHRU */
566#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600567#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800568#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500569#define EFUSE 0xcfdb
570#define PASS_THRU_MASK 0x1
571
hayeswangac718b62013-05-02 16:01:25 +0000572enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800573 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000574 _100bps = 0x08,
575 _10bps = 0x04,
576 LINK_STATUS = 0x02,
577 FULL_DUP = 0x01,
578};
579
hayeswang1764bcd2014-08-28 10:24:18 +0800580#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800581#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800582#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800583#define TX_ALIGN 4
584#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800585
586#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800587
hayeswangac718b62013-05-02 16:01:25 +0000588#define RTL8152_REQT_READ 0xc0
589#define RTL8152_REQT_WRITE 0x40
590#define RTL8152_REQ_GET_REGS 0x05
591#define RTL8152_REQ_SET_REGS 0x05
592
593#define BYTE_EN_DWORD 0xff
594#define BYTE_EN_WORD 0x33
595#define BYTE_EN_BYTE 0x11
596#define BYTE_EN_SIX_BYTES 0x3f
597#define BYTE_EN_START_MASK 0x0f
598#define BYTE_EN_END_MASK 0xf0
599
hayeswang69b4b7a2014-07-10 10:58:54 +0800600#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800601#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
602 ETH_FCS_LEN)
603#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800604#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800605#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800606#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800607#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800608 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000609
610/* rtl8152 flags */
611enum rtl8152_flags {
612 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000613 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800614 WORK_ENABLE,
615 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800616 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800617 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800618 SCHEDULE_NAPI,
hayeswang65b82d62017-06-15 14:44:03 +0800619 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800620 DELL_TB_RX_AGG_BUG,
hayeswangac718b62013-05-02 16:01:25 +0000621};
622
623/* Define these values to match your device */
624#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200625#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800626#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200627#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700628#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700629#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800630#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000631
632#define MCU_TYPE_PLA 0x0100
633#define MCU_TYPE_USB 0x0000
634
hayeswang4f1d4d52014-03-11 16:24:19 +0800635struct tally_counter {
636 __le64 tx_packets;
637 __le64 rx_packets;
638 __le64 tx_errors;
639 __le32 rx_errors;
640 __le16 rx_missed;
641 __le16 align_errors;
642 __le32 tx_one_collision;
643 __le32 tx_multi_collision;
644 __le64 rx_unicast;
645 __le64 rx_broadcast;
646 __le32 rx_multicast;
647 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800648 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800649};
650
hayeswangac718b62013-05-02 16:01:25 +0000651struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800652 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000653#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800654
hayeswang500b6d72013-11-20 17:30:57 +0800655 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800656#define RD_UDP_CS BIT(23)
657#define RD_TCP_CS BIT(22)
658#define RD_IPV6_CS BIT(20)
659#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800660
hayeswang500b6d72013-11-20 17:30:57 +0800661 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800662#define IPF BIT(23) /* IP checksum fail */
663#define UDPF BIT(22) /* UDP checksum fail */
664#define TCPF BIT(21) /* TCP checksum fail */
665#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800666
hayeswang500b6d72013-11-20 17:30:57 +0800667 __le32 opts4;
668 __le32 opts5;
669 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000670};
671
672struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800673 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800674#define TX_FS BIT(31) /* First segment of a packet */
675#define TX_LS BIT(30) /* Final segment of a packet */
676#define GTSENDV4 BIT(28)
677#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800678#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800679#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800680#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800681
hayeswang500b6d72013-11-20 17:30:57 +0800682 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800683#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
684#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
685#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
686#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800687#define MSS_SHIFT 17
688#define MSS_MAX 0x7ffU
689#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800690#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800691#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000692};
693
hayeswangdff4e8a2013-08-16 16:09:33 +0800694struct r8152;
695
hayeswangebc2ec482013-08-14 20:54:38 +0800696struct rx_agg {
697 struct list_head list;
698 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800699 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800700 void *buffer;
701 void *head;
702};
703
704struct tx_agg {
705 struct list_head list;
706 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800707 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800708 void *buffer;
709 void *head;
710 u32 skb_num;
711 u32 skb_len;
712};
713
hayeswangac718b62013-05-02 16:01:25 +0000714struct r8152 {
715 unsigned long flags;
716 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800717 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800718 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000719 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800720 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800721 struct tx_agg tx_info[RTL8152_MAX_TX];
722 struct rx_agg rx_info[RTL8152_MAX_RX];
723 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800724 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800725 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800726 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000727 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800728 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800729#ifdef CONFIG_PM_SLEEP
730 struct notifier_block pm_notifier;
731#endif
hayeswangc81229c2014-01-02 11:22:42 +0800732
733 struct rtl_ops {
734 void (*init)(struct r8152 *);
735 int (*enable)(struct r8152 *);
736 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800737 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800738 void (*down)(struct r8152 *);
739 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800740 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
741 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800742 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800743 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800744 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800745 } rtl_ops;
746
hayeswang40a82912013-08-14 20:54:40 +0800747 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800748 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000749 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800750 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800751 u32 coalesce;
hayeswangac718b62013-05-02 16:01:25 +0000752 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800753 u16 speed;
hayeswang40a82912013-08-14 20:54:40 +0800754 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000755 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800756 u8 duplex;
757 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000758};
759
760enum rtl_version {
761 RTL_VER_UNKNOWN = 0,
762 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800763 RTL_VER_02,
764 RTL_VER_03,
765 RTL_VER_04,
766 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800767 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800768 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800769 RTL_VER_08,
770 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800771 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000772};
773
hayeswang60c89072014-03-07 11:04:39 +0800774enum tx_csum_stat {
775 TX_CSUM_SUCCESS = 0,
776 TX_CSUM_TSO,
777 TX_CSUM_NONE
778};
779
hayeswangac718b62013-05-02 16:01:25 +0000780/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
781 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
782 */
783static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800784static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000785
hayeswang52aec122014-09-02 10:27:52 +0800786#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800787 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800788
hayeswangac718b62013-05-02 16:01:25 +0000789static
790int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
791{
hayeswang31787f52013-07-31 17:21:25 +0800792 int ret;
793 void *tmp;
794
795 tmp = kmalloc(size, GFP_KERNEL);
796 if (!tmp)
797 return -ENOMEM;
798
799 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800800 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
801 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800802
803 memcpy(data, tmp, size);
804 kfree(tmp);
805
806 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000807}
808
809static
810int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
811{
hayeswang31787f52013-07-31 17:21:25 +0800812 int ret;
813 void *tmp;
814
Benoit Tainec4438f02014-05-26 17:21:23 +0200815 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800816 if (!tmp)
817 return -ENOMEM;
818
hayeswang31787f52013-07-31 17:21:25 +0800819 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800820 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
821 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800822
823 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800824
hayeswang31787f52013-07-31 17:21:25 +0800825 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000826}
827
Hayes Wangffa9fec2019-07-04 17:36:32 +0800828static void rtl_set_unplug(struct r8152 *tp)
829{
830 if (tp->udev->state == USB_STATE_NOTATTACHED) {
831 set_bit(RTL8152_UNPLUG, &tp->flags);
832 smp_mb__after_atomic();
833 }
834}
835
hayeswangac718b62013-05-02 16:01:25 +0000836static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800837 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000838{
hayeswang45f4a192014-01-06 17:08:41 +0800839 u16 limit = 64;
840 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000841
842 if (test_bit(RTL8152_UNPLUG, &tp->flags))
843 return -ENODEV;
844
845 /* both size and indix must be 4 bytes align */
846 if ((size & 3) || !size || (index & 3) || !data)
847 return -EPERM;
848
849 if ((u32)index + (u32)size > 0xffff)
850 return -EPERM;
851
852 while (size) {
853 if (size > limit) {
854 ret = get_registers(tp, index, type, limit, data);
855 if (ret < 0)
856 break;
857
858 index += limit;
859 data += limit;
860 size -= limit;
861 } else {
862 ret = get_registers(tp, index, type, size, data);
863 if (ret < 0)
864 break;
865
866 index += size;
867 data += size;
868 size = 0;
869 break;
870 }
871 }
872
hayeswang67610492014-10-30 11:46:40 +0800873 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800874 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800875
hayeswangac718b62013-05-02 16:01:25 +0000876 return ret;
877}
878
879static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800880 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000881{
hayeswang45f4a192014-01-06 17:08:41 +0800882 int ret;
883 u16 byteen_start, byteen_end, byen;
884 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000885
886 if (test_bit(RTL8152_UNPLUG, &tp->flags))
887 return -ENODEV;
888
889 /* both size and indix must be 4 bytes align */
890 if ((size & 3) || !size || (index & 3) || !data)
891 return -EPERM;
892
893 if ((u32)index + (u32)size > 0xffff)
894 return -EPERM;
895
896 byteen_start = byteen & BYTE_EN_START_MASK;
897 byteen_end = byteen & BYTE_EN_END_MASK;
898
899 byen = byteen_start | (byteen_start << 4);
900 ret = set_registers(tp, index, type | byen, 4, data);
901 if (ret < 0)
902 goto error1;
903
904 index += 4;
905 data += 4;
906 size -= 4;
907
908 if (size) {
909 size -= 4;
910
911 while (size) {
912 if (size > limit) {
913 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800914 type | BYTE_EN_DWORD,
915 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000916 if (ret < 0)
917 goto error1;
918
919 index += limit;
920 data += limit;
921 size -= limit;
922 } else {
923 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800924 type | BYTE_EN_DWORD,
925 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000926 if (ret < 0)
927 goto error1;
928
929 index += size;
930 data += size;
931 size = 0;
932 break;
933 }
934 }
935
936 byen = byteen_end | (byteen_end >> 4);
937 ret = set_registers(tp, index, type | byen, 4, data);
938 if (ret < 0)
939 goto error1;
940 }
941
942error1:
hayeswang67610492014-10-30 11:46:40 +0800943 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +0800944 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +0800945
hayeswangac718b62013-05-02 16:01:25 +0000946 return ret;
947}
948
949static inline
950int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
951{
952 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
953}
954
955static inline
956int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
957{
958 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
959}
960
961static inline
hayeswangac718b62013-05-02 16:01:25 +0000962int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
963{
964 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
965}
966
967static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
968{
hayeswangc8826de2013-07-31 17:21:26 +0800969 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000970
hayeswangc8826de2013-07-31 17:21:26 +0800971 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000972
973 return __le32_to_cpu(data);
974}
975
976static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
977{
hayeswangc8826de2013-07-31 17:21:26 +0800978 __le32 tmp = __cpu_to_le32(data);
979
980 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000981}
982
983static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
984{
985 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800986 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +0800987 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +0000988 u8 shift = index & 2;
989
990 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +0800991 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +0000992
hayeswangd8fbd272017-06-15 14:44:04 +0800993 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +0000994
hayeswangc8826de2013-07-31 17:21:26 +0800995 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000996 data >>= (shift * 8);
997 data &= 0xffff;
998
999 return (u16)data;
1000}
1001
1002static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1003{
hayeswangc8826de2013-07-31 17:21:26 +08001004 u32 mask = 0xffff;
1005 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001006 u16 byen = BYTE_EN_WORD;
1007 u8 shift = index & 2;
1008
1009 data &= mask;
1010
1011 if (index & 2) {
1012 byen <<= shift;
1013 mask <<= (shift * 8);
1014 data <<= (shift * 8);
1015 index &= ~3;
1016 }
1017
hayeswangc8826de2013-07-31 17:21:26 +08001018 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001019
hayeswangc8826de2013-07-31 17:21:26 +08001020 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001021}
1022
1023static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1024{
1025 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001026 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001027 u8 shift = index & 3;
1028
1029 index &= ~3;
1030
hayeswangc8826de2013-07-31 17:21:26 +08001031 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001032
hayeswangc8826de2013-07-31 17:21:26 +08001033 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001034 data >>= (shift * 8);
1035 data &= 0xff;
1036
1037 return (u8)data;
1038}
1039
1040static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1041{
hayeswangc8826de2013-07-31 17:21:26 +08001042 u32 mask = 0xff;
1043 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001044 u16 byen = BYTE_EN_BYTE;
1045 u8 shift = index & 3;
1046
1047 data &= mask;
1048
1049 if (index & 3) {
1050 byen <<= shift;
1051 mask <<= (shift * 8);
1052 data <<= (shift * 8);
1053 index &= ~3;
1054 }
1055
hayeswangc8826de2013-07-31 17:21:26 +08001056 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001057
hayeswangc8826de2013-07-31 17:21:26 +08001058 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001059}
1060
hayeswangac244d32014-01-02 11:22:40 +08001061static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1062{
1063 u16 ocp_base, ocp_index;
1064
1065 ocp_base = addr & 0xf000;
1066 if (ocp_base != tp->ocp_base) {
1067 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1068 tp->ocp_base = ocp_base;
1069 }
1070
1071 ocp_index = (addr & 0x0fff) | 0xb000;
1072 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1073}
1074
hayeswange3fe0b12014-01-02 11:22:39 +08001075static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1076{
1077 u16 ocp_base, ocp_index;
1078
1079 ocp_base = addr & 0xf000;
1080 if (ocp_base != tp->ocp_base) {
1081 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1082 tp->ocp_base = ocp_base;
1083 }
1084
1085 ocp_index = (addr & 0x0fff) | 0xb000;
1086 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1087}
1088
hayeswangac244d32014-01-02 11:22:40 +08001089static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001090{
hayeswangac244d32014-01-02 11:22:40 +08001091 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001092}
1093
hayeswangac244d32014-01-02 11:22:40 +08001094static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001095{
hayeswangac244d32014-01-02 11:22:40 +08001096 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001097}
1098
hayeswang43779f82014-01-02 11:25:10 +08001099static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1100{
1101 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1102 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1103}
1104
hayeswang65b82d62017-06-15 14:44:03 +08001105static u16 sram_read(struct r8152 *tp, u16 addr)
1106{
1107 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1108 return ocp_reg_read(tp, OCP_SRAM_DATA);
1109}
1110
hayeswangac718b62013-05-02 16:01:25 +00001111static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1112{
1113 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001114 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001115
hayeswang68714382014-04-11 17:54:31 +08001116 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1117 return -ENODEV;
1118
hayeswangac718b62013-05-02 16:01:25 +00001119 if (phy_id != R8152_PHY_ID)
1120 return -EINVAL;
1121
hayeswang9a4be1b2014-02-18 21:49:07 +08001122 ret = r8152_mdio_read(tp, reg);
1123
hayeswang9a4be1b2014-02-18 21:49:07 +08001124 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001125}
1126
1127static
1128void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1129{
1130 struct r8152 *tp = netdev_priv(netdev);
1131
hayeswang68714382014-04-11 17:54:31 +08001132 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1133 return;
1134
hayeswangac718b62013-05-02 16:01:25 +00001135 if (phy_id != R8152_PHY_ID)
1136 return;
1137
1138 r8152_mdio_write(tp, reg, val);
1139}
1140
hayeswangb209af92014-08-25 15:53:00 +08001141static int
1142r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001143
hayeswang8ba789a2014-09-04 16:15:41 +08001144static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1145{
1146 struct r8152 *tp = netdev_priv(netdev);
1147 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001148 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001149
1150 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001151 goto out1;
1152
1153 ret = usb_autopm_get_interface(tp->intf);
1154 if (ret < 0)
1155 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001156
hayeswangb5403272014-10-09 18:00:26 +08001157 mutex_lock(&tp->control);
1158
hayeswang8ba789a2014-09-04 16:15:41 +08001159 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1160
1161 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1162 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1163 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1164
hayeswangb5403272014-10-09 18:00:26 +08001165 mutex_unlock(&tp->control);
1166
hayeswangea6a7112014-10-02 17:03:12 +08001167 usb_autopm_put_interface(tp->intf);
1168out1:
1169 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001170}
1171
Mario Limonciello9c273692018-12-11 08:16:14 -06001172/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001173 * host system provided MAC address.
1174 * Examples of this are Dell TB15 and Dell WD15 docks
1175 */
1176static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1177{
1178 acpi_status status;
1179 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1180 union acpi_object *obj;
1181 int ret = -EINVAL;
1182 u32 ocp_data;
1183 unsigned char buf[6];
1184
1185 /* test for -AD variant of RTL8153 */
1186 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001187 if ((ocp_data & AD_MASK) == 0x1000) {
1188 /* test for MAC address pass-through bit */
1189 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1190 if ((ocp_data & PASS_THRU_MASK) != 1) {
1191 netif_dbg(tp, probe, tp->netdev,
1192 "No efuse for RTL8153-AD MAC pass through\n");
1193 return -ENODEV;
1194 }
1195 } else {
David Chen8e29d232019-02-16 17:16:42 +08001196 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001197 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001198 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001199 netif_dbg(tp, probe, tp->netdev,
1200 "Invalid variant for MAC pass through\n");
1201 return -ENODEV;
1202 }
1203 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001204
1205 /* returns _AUXMAC_#AABBCCDDEEFF# */
1206 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1207 obj = (union acpi_object *)buffer.pointer;
1208 if (!ACPI_SUCCESS(status))
1209 return -ENODEV;
1210 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1211 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001212 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001213 obj->type, obj->string.length);
1214 goto amacout;
1215 }
1216 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1217 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1218 netif_warn(tp, probe, tp->netdev,
1219 "Invalid header when reading pass-thru MAC addr\n");
1220 goto amacout;
1221 }
1222 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1223 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1224 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001225 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1226 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001227 ret = -EINVAL;
1228 goto amacout;
1229 }
1230 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001231 netif_info(tp, probe, tp->netdev,
1232 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1233
1234amacout:
1235 kfree(obj);
1236 return ret;
1237}
1238
Mario Limonciello25766272019-04-04 13:46:53 -05001239static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1240{
1241 struct net_device *dev = tp->netdev;
1242 int ret;
1243
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001244 sa->sa_family = dev->type;
1245
Mario Limonciello25766272019-04-04 13:46:53 -05001246 if (tp->version == RTL_VER_01) {
1247 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1248 } else {
1249 /* if device doesn't support MAC pass through this will
1250 * be expected to be non-zero
1251 */
1252 ret = vendor_mac_passthru_addr_read(tp, sa);
1253 if (ret < 0)
1254 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1255 }
1256
1257 if (ret < 0) {
1258 netif_err(tp, probe, dev, "Get ether addr fail\n");
1259 } else if (!is_valid_ether_addr(sa->sa_data)) {
1260 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1261 sa->sa_data);
1262 eth_hw_addr_random(dev);
1263 ether_addr_copy(sa->sa_data, dev->dev_addr);
1264 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1265 sa->sa_data);
1266 return 0;
1267 }
1268
1269 return ret;
1270}
1271
hayeswang179bb6d2014-09-04 16:15:42 +08001272static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001273{
1274 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001275 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001276 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001277
Mario Limonciello25766272019-04-04 13:46:53 -05001278 ret = determine_ethernet_addr(tp, &sa);
1279 if (ret < 0)
1280 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001281
Mario Limonciello25766272019-04-04 13:46:53 -05001282 if (tp->version == RTL_VER_01)
1283 ether_addr_copy(dev->dev_addr, sa.sa_data);
1284 else
hayeswang179bb6d2014-09-04 16:15:42 +08001285 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001286
1287 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001288}
1289
hayeswangac718b62013-05-02 16:01:25 +00001290static void read_bulk_callback(struct urb *urb)
1291{
hayeswangac718b62013-05-02 16:01:25 +00001292 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001293 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001294 struct rx_agg *agg;
1295 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001296 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001297
hayeswangebc2ec482013-08-14 20:54:38 +08001298 agg = urb->context;
1299 if (!agg)
1300 return;
1301
1302 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001303 if (!tp)
1304 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001305
hayeswangac718b62013-05-02 16:01:25 +00001306 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1307 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001308
1309 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001310 return;
1311
hayeswangebc2ec482013-08-14 20:54:38 +08001312 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001313
1314 /* When link down, the driver would cancel all bulks. */
1315 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001316 if (!netif_carrier_ok(netdev))
1317 return;
1318
hayeswang9a4be1b2014-02-18 21:49:07 +08001319 usb_mark_last_busy(tp->udev);
1320
hayeswangac718b62013-05-02 16:01:25 +00001321 switch (status) {
1322 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001323 if (urb->actual_length < ETH_ZLEN)
1324 break;
1325
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001326 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001327 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001328 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001329 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001330 return;
hayeswangac718b62013-05-02 16:01:25 +00001331 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001332 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001333 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001334 return;
hayeswangac718b62013-05-02 16:01:25 +00001335 case -ENOENT:
1336 return; /* the urb is in unlink state */
1337 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001338 if (net_ratelimit())
1339 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001340 break;
hayeswangac718b62013-05-02 16:01:25 +00001341 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001342 if (net_ratelimit())
1343 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001344 break;
hayeswangac718b62013-05-02 16:01:25 +00001345 }
1346
hayeswanga0fccd42014-11-20 10:29:05 +08001347 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001348}
1349
1350static void write_bulk_callback(struct urb *urb)
1351{
hayeswangebc2ec482013-08-14 20:54:38 +08001352 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001353 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001354 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001355 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001356 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001357 int status = urb->status;
1358
hayeswangebc2ec482013-08-14 20:54:38 +08001359 agg = urb->context;
1360 if (!agg)
1361 return;
1362
1363 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001364 if (!tp)
1365 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001366
hayeswangd104eaf2014-03-06 15:07:17 +08001367 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001368 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001369 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001370 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001371 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001372 stats->tx_errors += agg->skb_num;
1373 } else {
1374 stats->tx_packets += agg->skb_num;
1375 stats->tx_bytes += agg->skb_len;
1376 }
1377
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001378 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001379 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001380 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001381
hayeswang9a4be1b2014-02-18 21:49:07 +08001382 usb_autopm_put_interface_async(tp->intf);
1383
hayeswangd104eaf2014-03-06 15:07:17 +08001384 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001385 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001386
1387 if (!test_bit(WORK_ENABLE, &tp->flags))
1388 return;
1389
1390 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1391 return;
1392
1393 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001394 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001395}
1396
hayeswang40a82912013-08-14 20:54:40 +08001397static void intr_callback(struct urb *urb)
1398{
1399 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001400 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001401 int status = urb->status;
1402 int res;
1403
1404 tp = urb->context;
1405 if (!tp)
1406 return;
1407
1408 if (!test_bit(WORK_ENABLE, &tp->flags))
1409 return;
1410
1411 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1412 return;
1413
1414 switch (status) {
1415 case 0: /* success */
1416 break;
1417 case -ECONNRESET: /* unlink */
1418 case -ESHUTDOWN:
1419 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001420 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001421 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001422 case -EPROTO:
1423 netif_info(tp, intr, tp->netdev,
1424 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001425 return;
1426 case -EOVERFLOW:
1427 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1428 goto resubmit;
1429 /* -EPIPE: should clear the halt */
1430 default:
1431 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1432 goto resubmit;
1433 }
1434
1435 d = urb->transfer_buffer;
1436 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001437 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001438 set_bit(RTL8152_LINK_CHG, &tp->flags);
1439 schedule_delayed_work(&tp->schedule, 0);
1440 }
1441 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001442 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001443 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001444 set_bit(RTL8152_LINK_CHG, &tp->flags);
1445 schedule_delayed_work(&tp->schedule, 0);
1446 }
1447 }
1448
1449resubmit:
1450 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001451 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001452 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001453 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001454 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001455 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001456 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001457 }
hayeswang40a82912013-08-14 20:54:40 +08001458}
1459
hayeswangebc2ec482013-08-14 20:54:38 +08001460static inline void *rx_agg_align(void *data)
1461{
hayeswang8e1f51b2014-01-02 11:22:41 +08001462 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001463}
1464
1465static inline void *tx_agg_align(void *data)
1466{
hayeswang8e1f51b2014-01-02 11:22:41 +08001467 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001468}
1469
1470static void free_all_mem(struct r8152 *tp)
1471{
1472 int i;
1473
1474 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001475 usb_free_urb(tp->rx_info[i].urb);
1476 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001477
hayeswang9629e3c2014-01-15 10:42:15 +08001478 kfree(tp->rx_info[i].buffer);
1479 tp->rx_info[i].buffer = NULL;
1480 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001481 }
1482
1483 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001484 usb_free_urb(tp->tx_info[i].urb);
1485 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001486
hayeswang9629e3c2014-01-15 10:42:15 +08001487 kfree(tp->tx_info[i].buffer);
1488 tp->tx_info[i].buffer = NULL;
1489 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001490 }
hayeswang40a82912013-08-14 20:54:40 +08001491
hayeswang9629e3c2014-01-15 10:42:15 +08001492 usb_free_urb(tp->intr_urb);
1493 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001494
hayeswang9629e3c2014-01-15 10:42:15 +08001495 kfree(tp->intr_buff);
1496 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001497}
1498
1499static int alloc_all_mem(struct r8152 *tp)
1500{
1501 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001502 struct usb_interface *intf = tp->intf;
1503 struct usb_host_interface *alt = intf->cur_altsetting;
1504 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001505 struct urb *urb;
1506 int node, i;
1507 u8 *buf;
1508
1509 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1510
1511 spin_lock_init(&tp->rx_lock);
1512 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001513 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001514 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001515 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001516 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec482013-08-14 20:54:38 +08001517
1518 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001519 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001520 if (!buf)
1521 goto err1;
1522
1523 if (buf != rx_agg_align(buf)) {
1524 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001525 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001526 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001527 if (!buf)
1528 goto err1;
1529 }
1530
1531 urb = usb_alloc_urb(0, GFP_KERNEL);
1532 if (!urb) {
1533 kfree(buf);
1534 goto err1;
1535 }
1536
1537 INIT_LIST_HEAD(&tp->rx_info[i].list);
1538 tp->rx_info[i].context = tp;
1539 tp->rx_info[i].urb = urb;
1540 tp->rx_info[i].buffer = buf;
1541 tp->rx_info[i].head = rx_agg_align(buf);
1542 }
1543
1544 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001545 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001546 if (!buf)
1547 goto err1;
1548
1549 if (buf != tx_agg_align(buf)) {
1550 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001551 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001552 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001553 if (!buf)
1554 goto err1;
1555 }
1556
1557 urb = usb_alloc_urb(0, GFP_KERNEL);
1558 if (!urb) {
1559 kfree(buf);
1560 goto err1;
1561 }
1562
1563 INIT_LIST_HEAD(&tp->tx_info[i].list);
1564 tp->tx_info[i].context = tp;
1565 tp->tx_info[i].urb = urb;
1566 tp->tx_info[i].buffer = buf;
1567 tp->tx_info[i].head = tx_agg_align(buf);
1568
1569 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1570 }
1571
hayeswang40a82912013-08-14 20:54:40 +08001572 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1573 if (!tp->intr_urb)
1574 goto err1;
1575
1576 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1577 if (!tp->intr_buff)
1578 goto err1;
1579
1580 tp->intr_interval = (int)ep_intr->desc.bInterval;
1581 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001582 tp->intr_buff, INTBUFSIZE, intr_callback,
1583 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001584
hayeswangebc2ec482013-08-14 20:54:38 +08001585 return 0;
1586
1587err1:
1588 free_all_mem(tp);
1589 return -ENOMEM;
1590}
1591
hayeswang0de98f62013-08-16 16:09:35 +08001592static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1593{
1594 struct tx_agg *agg = NULL;
1595 unsigned long flags;
1596
hayeswang21949ab2014-03-07 11:04:35 +08001597 if (list_empty(&tp->tx_free))
1598 return NULL;
1599
hayeswang0de98f62013-08-16 16:09:35 +08001600 spin_lock_irqsave(&tp->tx_lock, flags);
1601 if (!list_empty(&tp->tx_free)) {
1602 struct list_head *cursor;
1603
1604 cursor = tp->tx_free.next;
1605 list_del_init(cursor);
1606 agg = list_entry(cursor, struct tx_agg, list);
1607 }
1608 spin_unlock_irqrestore(&tp->tx_lock, flags);
1609
1610 return agg;
1611}
1612
hayeswangb209af92014-08-25 15:53:00 +08001613/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001614 * The hw limites the value the transport offset. When the offset is out of the
1615 * range, calculate the checksum by sw.
1616 */
1617static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1618 struct sk_buff_head *list)
1619{
1620 if (skb_shinfo(skb)->gso_size) {
1621 netdev_features_t features = tp->netdev->features;
1622 struct sk_buff_head seg_list;
1623 struct sk_buff *segs, *nskb;
1624
hayeswanga91d45f2014-07-11 16:48:27 +08001625 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001626 segs = skb_gso_segment(skb, features);
1627 if (IS_ERR(segs) || !segs)
1628 goto drop;
1629
1630 __skb_queue_head_init(&seg_list);
1631
1632 do {
1633 nskb = segs;
1634 segs = segs->next;
1635 nskb->next = NULL;
1636 __skb_queue_tail(&seg_list, nskb);
1637 } while (segs);
1638
1639 skb_queue_splice(&seg_list, list);
1640 dev_kfree_skb(skb);
1641 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1642 if (skb_checksum_help(skb) < 0)
1643 goto drop;
1644
1645 __skb_queue_head(list, skb);
1646 } else {
1647 struct net_device_stats *stats;
1648
1649drop:
1650 stats = &tp->netdev->stats;
1651 stats->tx_dropped++;
1652 dev_kfree_skb(skb);
1653 }
1654}
1655
hayeswangb209af92014-08-25 15:53:00 +08001656/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001657 * According to the document of microsoft, the TCP Pseudo Header excludes the
1658 * packet length for IPv6 TCP large packets.
1659 */
1660static int msdn_giant_send_check(struct sk_buff *skb)
1661{
1662 const struct ipv6hdr *ipv6h;
1663 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001664 int ret;
1665
1666 ret = skb_cow_head(skb, 0);
1667 if (ret)
1668 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001669
1670 ipv6h = ipv6_hdr(skb);
1671 th = tcp_hdr(skb);
1672
1673 th->check = 0;
1674 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1675
hayeswangfcb308d2014-03-11 10:20:32 +08001676 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001677}
1678
hayeswangc5554292014-09-12 10:43:11 +08001679static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1680{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001681 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001682 u32 opts2;
1683
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001684 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001685 desc->opts2 |= cpu_to_le32(opts2);
1686 }
1687}
1688
1689static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1690{
1691 u32 opts2 = le32_to_cpu(desc->opts2);
1692
1693 if (opts2 & RX_VLAN_TAG)
1694 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1695 swab16(opts2 & 0xffff));
1696}
1697
hayeswang60c89072014-03-07 11:04:39 +08001698static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1699 struct sk_buff *skb, u32 len, u32 transport_offset)
1700{
1701 u32 mss = skb_shinfo(skb)->gso_size;
1702 u32 opts1, opts2 = 0;
1703 int ret = TX_CSUM_SUCCESS;
1704
1705 WARN_ON_ONCE(len > TX_LEN_MAX);
1706
1707 opts1 = len | TX_FS | TX_LS;
1708
1709 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001710 if (transport_offset > GTTCPHO_MAX) {
1711 netif_warn(tp, tx_err, tp->netdev,
1712 "Invalid transport offset 0x%x for TSO\n",
1713 transport_offset);
1714 ret = TX_CSUM_TSO;
1715 goto unavailable;
1716 }
1717
hayeswang6e74d172015-02-06 11:30:50 +08001718 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001719 case htons(ETH_P_IP):
1720 opts1 |= GTSENDV4;
1721 break;
1722
hayeswang6128d1bb2014-03-07 11:04:40 +08001723 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001724 if (msdn_giant_send_check(skb)) {
1725 ret = TX_CSUM_TSO;
1726 goto unavailable;
1727 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001728 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001729 break;
1730
hayeswang60c89072014-03-07 11:04:39 +08001731 default:
1732 WARN_ON_ONCE(1);
1733 break;
1734 }
1735
1736 opts1 |= transport_offset << GTTCPHO_SHIFT;
1737 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1738 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001739 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001740
hayeswang6128d1bb2014-03-07 11:04:40 +08001741 if (transport_offset > TCPHO_MAX) {
1742 netif_warn(tp, tx_err, tp->netdev,
1743 "Invalid transport offset 0x%x\n",
1744 transport_offset);
1745 ret = TX_CSUM_NONE;
1746 goto unavailable;
1747 }
1748
hayeswang6e74d172015-02-06 11:30:50 +08001749 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001750 case htons(ETH_P_IP):
1751 opts2 |= IPV4_CS;
1752 ip_protocol = ip_hdr(skb)->protocol;
1753 break;
1754
1755 case htons(ETH_P_IPV6):
1756 opts2 |= IPV6_CS;
1757 ip_protocol = ipv6_hdr(skb)->nexthdr;
1758 break;
1759
1760 default:
1761 ip_protocol = IPPROTO_RAW;
1762 break;
1763 }
1764
hayeswang60c89072014-03-07 11:04:39 +08001765 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001766 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001767 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001768 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001769 else
hayeswang5bd23882013-08-14 20:54:39 +08001770 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001771
hayeswang60c89072014-03-07 11:04:39 +08001772 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001773 }
hayeswang60c89072014-03-07 11:04:39 +08001774
1775 desc->opts2 = cpu_to_le32(opts2);
1776 desc->opts1 = cpu_to_le32(opts1);
1777
hayeswang6128d1bb2014-03-07 11:04:40 +08001778unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001779 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001780}
1781
hayeswangb1379d92013-08-16 16:09:37 +08001782static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1783{
hayeswangd84130a2014-02-18 21:49:02 +08001784 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001785 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001786 u8 *tx_data;
1787
hayeswangd84130a2014-02-18 21:49:02 +08001788 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001789 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001790 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001791 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001792
hayeswangb1379d92013-08-16 16:09:37 +08001793 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001794 agg->skb_num = 0;
1795 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001796 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001797
hayeswang7937f9e2013-11-20 17:30:54 +08001798 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001799 struct tx_desc *tx_desc;
1800 struct sk_buff *skb;
1801 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001802 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001803
hayeswangd84130a2014-02-18 21:49:02 +08001804 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001805 if (!skb)
1806 break;
1807
hayeswang60c89072014-03-07 11:04:39 +08001808 len = skb->len + sizeof(*tx_desc);
1809
1810 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001811 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001812 break;
1813 }
1814
hayeswang7937f9e2013-11-20 17:30:54 +08001815 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001816 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001817
1818 offset = (u32)skb_transport_offset(skb);
1819
hayeswang6128d1bb2014-03-07 11:04:40 +08001820 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1821 r8152_csum_workaround(tp, skb, &skb_head);
1822 continue;
1823 }
hayeswang60c89072014-03-07 11:04:39 +08001824
hayeswangc5554292014-09-12 10:43:11 +08001825 rtl_tx_vlan_tag(tx_desc, skb);
1826
hayeswangb1379d92013-08-16 16:09:37 +08001827 tx_data += sizeof(*tx_desc);
1828
hayeswang60c89072014-03-07 11:04:39 +08001829 len = skb->len;
1830 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1831 struct net_device_stats *stats = &tp->netdev->stats;
1832
1833 stats->tx_dropped++;
1834 dev_kfree_skb_any(skb);
1835 tx_data -= sizeof(*tx_desc);
1836 continue;
1837 }
hayeswangb1379d92013-08-16 16:09:37 +08001838
hayeswang7937f9e2013-11-20 17:30:54 +08001839 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001840 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001841 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001842
1843 dev_kfree_skb_any(skb);
1844
hayeswang52aec122014-09-02 10:27:52 +08001845 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001846
1847 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1848 break;
hayeswangb1379d92013-08-16 16:09:37 +08001849 }
1850
hayeswangd84130a2014-02-18 21:49:02 +08001851 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001852 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001853 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001854 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001855 }
1856
hayeswang0c3121f2014-03-07 11:04:36 +08001857 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001858
1859 if (netif_queue_stopped(tp->netdev) &&
1860 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1861 netif_wake_queue(tp->netdev);
1862
hayeswang0c3121f2014-03-07 11:04:36 +08001863 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001864
hayeswang0c3121f2014-03-07 11:04:36 +08001865 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001866 if (ret < 0)
1867 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001868
hayeswangb1379d92013-08-16 16:09:37 +08001869 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1870 agg->head, (int)(tx_data - (u8 *)agg->head),
1871 (usb_complete_t)write_bulk_callback, agg);
1872
hayeswang0c3121f2014-03-07 11:04:36 +08001873 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001874 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001875 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001876
1877out_tx_fill:
1878 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001879}
1880
hayeswang565cab02014-03-07 11:04:38 +08001881static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1882{
1883 u8 checksum = CHECKSUM_NONE;
1884 u32 opts2, opts3;
1885
hayeswang19c0f402017-01-11 16:25:34 +08001886 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001887 goto return_result;
1888
1889 opts2 = le32_to_cpu(rx_desc->opts2);
1890 opts3 = le32_to_cpu(rx_desc->opts3);
1891
1892 if (opts2 & RD_IPV4_CS) {
1893 if (opts3 & IPF)
1894 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001895 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1896 checksum = CHECKSUM_UNNECESSARY;
1897 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001898 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001899 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001900 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1901 checksum = CHECKSUM_UNNECESSARY;
1902 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1903 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001904 }
1905
1906return_result:
1907 return checksum;
1908}
1909
hayeswangd823ab62015-01-12 12:06:23 +08001910static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001911{
hayeswanga5a4f462013-08-16 16:09:34 +08001912 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001913 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001914 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08001915 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08001916
1917 if (!skb_queue_empty(&tp->rx_queue)) {
1918 while (work_done < budget) {
1919 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1920 struct net_device *netdev = tp->netdev;
1921 struct net_device_stats *stats = &netdev->stats;
1922 unsigned int pkt_len;
1923
1924 if (!skb)
1925 break;
1926
1927 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08001928 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001929 work_done++;
1930 stats->rx_packets++;
1931 stats->rx_bytes += pkt_len;
1932 }
1933 }
hayeswangebc2ec482013-08-14 20:54:38 +08001934
hayeswangd84130a2014-02-18 21:49:02 +08001935 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001936 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001937
1938 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001939 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001940 list_splice_init(&tp->rx_done, &rx_queue);
1941 spin_unlock_irqrestore(&tp->rx_lock, flags);
1942
1943 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001944 struct rx_desc *rx_desc;
1945 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001946 int len_used = 0;
1947 struct urb *urb;
1948 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001949
hayeswangebc2ec482013-08-14 20:54:38 +08001950 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001951
1952 agg = list_entry(cursor, struct rx_agg, list);
1953 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001954 if (urb->actual_length < ETH_ZLEN)
1955 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001956
hayeswangebc2ec482013-08-14 20:54:38 +08001957 rx_desc = agg->head;
1958 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001959 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001960
hayeswang7937f9e2013-11-20 17:30:54 +08001961 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001962 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001963 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001964 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001965 struct sk_buff *skb;
1966
hayeswang74544452017-06-09 17:11:47 +08001967 /* limite the skb numbers for rx_queue */
1968 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
1969 break;
1970
hayeswang7937f9e2013-11-20 17:30:54 +08001971 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001972 if (pkt_len < ETH_ZLEN)
1973 break;
1974
hayeswang7937f9e2013-11-20 17:30:54 +08001975 len_used += pkt_len;
1976 if (urb->actual_length < len_used)
1977 break;
1978
hayeswangb65c0c92017-06-21 11:25:18 +08001979 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08001980 rx_data += sizeof(struct rx_desc);
1981
hayeswangce594e92017-03-16 14:32:22 +08001982 skb = napi_alloc_skb(napi, pkt_len);
hayeswangebc2ec482013-08-14 20:54:38 +08001983 if (!skb) {
1984 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001985 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001986 }
hayeswang565cab02014-03-07 11:04:38 +08001987
1988 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001989 memcpy(skb->data, rx_data, pkt_len);
1990 skb_put(skb, pkt_len);
1991 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001992 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001993 if (work_done < budget) {
hayeswangce594e92017-03-16 14:32:22 +08001994 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001995 work_done++;
1996 stats->rx_packets++;
1997 stats->rx_bytes += pkt_len;
1998 } else {
1999 __skb_queue_tail(&tp->rx_queue, skb);
2000 }
hayeswangebc2ec482013-08-14 20:54:38 +08002001
hayeswang5e2f7482014-03-07 11:04:37 +08002002find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002003 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002004 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08002005 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08002006 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002007 }
2008
hayeswang0de98f62013-08-16 16:09:35 +08002009submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002010 if (!ret) {
2011 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2012 } else {
2013 urb->actual_length = 0;
2014 list_add_tail(&agg->list, next);
2015 }
2016 }
2017
2018 if (!list_empty(&rx_queue)) {
2019 spin_lock_irqsave(&tp->rx_lock, flags);
2020 list_splice_tail(&rx_queue, &tp->rx_done);
2021 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002022 }
hayeswangd823ab62015-01-12 12:06:23 +08002023
2024out1:
2025 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002026}
2027
2028static void tx_bottom(struct r8152 *tp)
2029{
hayeswangebc2ec482013-08-14 20:54:38 +08002030 int res;
2031
hayeswangb1379d92013-08-16 16:09:37 +08002032 do {
2033 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002034
hayeswangb1379d92013-08-16 16:09:37 +08002035 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002036 break;
2037
hayeswangb1379d92013-08-16 16:09:37 +08002038 agg = r8152_get_tx_agg(tp);
2039 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002040 break;
hayeswangb1379d92013-08-16 16:09:37 +08002041
2042 res = r8152_tx_agg_fill(tp, agg);
2043 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002044 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002045
2046 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002047 rtl_set_unplug(tp);
hayeswangb1379d92013-08-16 16:09:37 +08002048 netif_device_detach(netdev);
2049 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002050 struct net_device_stats *stats = &netdev->stats;
2051 unsigned long flags;
2052
hayeswangb1379d92013-08-16 16:09:37 +08002053 netif_warn(tp, tx_err, netdev,
2054 "failed tx_urb %d\n", res);
2055 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002056
hayeswangb1379d92013-08-16 16:09:37 +08002057 spin_lock_irqsave(&tp->tx_lock, flags);
2058 list_add_tail(&agg->list, &tp->tx_free);
2059 spin_unlock_irqrestore(&tp->tx_lock, flags);
2060 }
hayeswangebc2ec482013-08-14 20:54:38 +08002061 }
hayeswangb1379d92013-08-16 16:09:37 +08002062 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002063}
2064
hayeswangd823ab62015-01-12 12:06:23 +08002065static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08002066{
hayeswangebc2ec482013-08-14 20:54:38 +08002067 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2068 return;
2069
2070 if (!test_bit(WORK_ENABLE, &tp->flags))
2071 return;
2072
hayeswang7559fb2f2013-08-16 16:09:38 +08002073 /* When link down, the driver would cancel all bulks. */
2074 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002075 if (!netif_carrier_ok(tp->netdev))
2076 return;
2077
hayeswangd823ab62015-01-12 12:06:23 +08002078 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002079
hayeswang0c3121f2014-03-07 11:04:36 +08002080 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002081}
2082
hayeswangd823ab62015-01-12 12:06:23 +08002083static int r8152_poll(struct napi_struct *napi, int budget)
2084{
2085 struct r8152 *tp = container_of(napi, struct r8152, napi);
2086 int work_done;
2087
2088 work_done = rx_bottom(tp, budget);
2089 bottom_half(tp);
2090
2091 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002092 if (!napi_complete_done(napi, work_done))
2093 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002094 if (!list_empty(&tp->rx_done))
2095 napi_schedule(napi);
hayeswang248b2132017-01-26 09:38:33 +08002096 else if (!skb_queue_empty(&tp->tx_queue) &&
2097 !list_empty(&tp->tx_free))
2098 napi_schedule(napi);
hayeswangd823ab62015-01-12 12:06:23 +08002099 }
2100
hayeswanga3307f92017-06-09 17:11:48 +08002101out:
hayeswangd823ab62015-01-12 12:06:23 +08002102 return work_done;
2103}
2104
hayeswangebc2ec482013-08-14 20:54:38 +08002105static
2106int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2107{
hayeswanga0fccd42014-11-20 10:29:05 +08002108 int ret;
2109
hayeswangef827a52015-01-09 10:26:36 +08002110 /* The rx would be stopped, so skip submitting */
2111 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2112 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2113 return 0;
2114
hayeswangebc2ec482013-08-14 20:54:38 +08002115 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08002116 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002117 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002118
hayeswanga0fccd42014-11-20 10:29:05 +08002119 ret = usb_submit_urb(agg->urb, mem_flags);
2120 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002121 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002122 netif_device_detach(tp->netdev);
2123 } else if (ret) {
2124 struct urb *urb = agg->urb;
2125 unsigned long flags;
2126
2127 urb->actual_length = 0;
2128 spin_lock_irqsave(&tp->rx_lock, flags);
2129 list_add_tail(&agg->list, &tp->rx_done);
2130 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002131
2132 netif_err(tp, rx_err, tp->netdev,
2133 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2134
2135 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002136 }
2137
2138 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002139}
2140
hayeswang00a5e362014-02-18 21:48:59 +08002141static void rtl_drop_queued_tx(struct r8152 *tp)
2142{
2143 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002144 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002145 struct sk_buff *skb;
2146
hayeswangd84130a2014-02-18 21:49:02 +08002147 if (skb_queue_empty(tx_queue))
2148 return;
2149
2150 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002151 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002152 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002153 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002154
2155 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002156 dev_kfree_skb(skb);
2157 stats->tx_dropped++;
2158 }
2159}
2160
hayeswangac718b62013-05-02 16:01:25 +00002161static void rtl8152_tx_timeout(struct net_device *netdev)
2162{
2163 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002164
Hayes Wang4a8deae2014-01-07 11:18:22 +08002165 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002166
2167 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002168}
2169
2170static void rtl8152_set_rx_mode(struct net_device *netdev)
2171{
2172 struct r8152 *tp = netdev_priv(netdev);
2173
hayeswang51d979f2015-02-06 11:30:47 +08002174 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002175 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002176 schedule_delayed_work(&tp->schedule, 0);
2177 }
hayeswangac718b62013-05-02 16:01:25 +00002178}
2179
2180static void _rtl8152_set_rx_mode(struct net_device *netdev)
2181{
2182 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002183 u32 mc_filter[2]; /* Multicast hash filter */
2184 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002185 u32 ocp_data;
2186
hayeswangac718b62013-05-02 16:01:25 +00002187 netif_stop_queue(netdev);
2188 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2189 ocp_data &= ~RCR_ACPT_ALL;
2190 ocp_data |= RCR_AB | RCR_APM;
2191
2192 if (netdev->flags & IFF_PROMISC) {
2193 /* Unconditionally log net taps. */
2194 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2195 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002196 mc_filter[1] = 0xffffffff;
2197 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002198 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2199 (netdev->flags & IFF_ALLMULTI)) {
2200 /* Too many to filter perfectly -- accept all multicasts. */
2201 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002202 mc_filter[1] = 0xffffffff;
2203 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002204 } else {
2205 struct netdev_hw_addr *ha;
2206
hayeswangb209af92014-08-25 15:53:00 +08002207 mc_filter[1] = 0;
2208 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002209 netdev_for_each_mc_addr(ha, netdev) {
2210 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002211
hayeswangac718b62013-05-02 16:01:25 +00002212 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2213 ocp_data |= RCR_AM;
2214 }
2215 }
2216
hayeswang31787f52013-07-31 17:21:25 +08002217 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2218 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002219
hayeswang31787f52013-07-31 17:21:25 +08002220 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002221 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2222 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002223}
2224
hayeswanga5e31252015-01-06 17:41:58 +08002225static netdev_features_t
2226rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2227 netdev_features_t features)
2228{
2229 u32 mss = skb_shinfo(skb)->gso_size;
2230 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2231 int offset = skb_transport_offset(skb);
2232
2233 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002234 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002235 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2236 features &= ~NETIF_F_GSO_MASK;
2237
2238 return features;
2239}
2240
hayeswangac718b62013-05-02 16:01:25 +00002241static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002242 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002243{
2244 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002245
hayeswangac718b62013-05-02 16:01:25 +00002246 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002247
hayeswang61598782013-11-20 17:30:55 +08002248 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002249
hayeswang0c3121f2014-03-07 11:04:36 +08002250 if (!list_empty(&tp->tx_free)) {
2251 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08002252 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002253 schedule_delayed_work(&tp->schedule, 0);
2254 } else {
2255 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08002256 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002257 }
hayeswangb209af92014-08-25 15:53:00 +08002258 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002259 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002260 }
hayeswangdd1b1192013-11-20 17:30:56 +08002261
hayeswangac718b62013-05-02 16:01:25 +00002262 return NETDEV_TX_OK;
2263}
2264
2265static void r8152b_reset_packet_filter(struct r8152 *tp)
2266{
2267 u32 ocp_data;
2268
2269 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2270 ocp_data &= ~FMC_FCR_MCU_EN;
2271 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2272 ocp_data |= FMC_FCR_MCU_EN;
2273 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2274}
2275
2276static void rtl8152_nic_reset(struct r8152 *tp)
2277{
2278 int i;
2279
2280 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2281
2282 for (i = 0; i < 1000; i++) {
2283 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2284 break;
hayeswangb209af92014-08-25 15:53:00 +08002285 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002286 }
2287}
2288
hayeswangdd1b1192013-11-20 17:30:56 +08002289static void set_tx_qlen(struct r8152 *tp)
2290{
2291 struct net_device *netdev = tp->netdev;
2292
hayeswangb65c0c92017-06-21 11:25:18 +08002293 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002294 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002295}
2296
hayeswangac718b62013-05-02 16:01:25 +00002297static inline u8 rtl8152_get_speed(struct r8152 *tp)
2298{
2299 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2300}
2301
hayeswang507605a2014-01-02 11:22:43 +08002302static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002303{
hayeswangebc2ec482013-08-14 20:54:38 +08002304 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002305 u8 speed;
2306
2307 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002308 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002309 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002310 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2312 } else {
2313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002314 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002315 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2316 }
hayeswang507605a2014-01-02 11:22:43 +08002317}
2318
hayeswang00a5e362014-02-18 21:48:59 +08002319static void rxdy_gated_en(struct r8152 *tp, bool enable)
2320{
2321 u32 ocp_data;
2322
2323 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2324 if (enable)
2325 ocp_data |= RXDY_GATED_EN;
2326 else
2327 ocp_data &= ~RXDY_GATED_EN;
2328 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2329}
2330
hayeswang445f7f42014-09-23 16:31:47 +08002331static int rtl_start_rx(struct r8152 *tp)
2332{
2333 int i, ret = 0;
2334
2335 INIT_LIST_HEAD(&tp->rx_done);
2336 for (i = 0; i < RTL8152_MAX_RX; i++) {
2337 INIT_LIST_HEAD(&tp->rx_info[i].list);
2338 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2339 if (ret)
2340 break;
2341 }
2342
hayeswang7bcf4f62014-11-20 10:29:06 +08002343 if (ret && ++i < RTL8152_MAX_RX) {
2344 struct list_head rx_queue;
2345 unsigned long flags;
2346
2347 INIT_LIST_HEAD(&rx_queue);
2348
2349 do {
2350 struct rx_agg *agg = &tp->rx_info[i++];
2351 struct urb *urb = agg->urb;
2352
2353 urb->actual_length = 0;
2354 list_add_tail(&agg->list, &rx_queue);
2355 } while (i < RTL8152_MAX_RX);
2356
2357 spin_lock_irqsave(&tp->rx_lock, flags);
2358 list_splice_tail(&rx_queue, &tp->rx_done);
2359 spin_unlock_irqrestore(&tp->rx_lock, flags);
2360 }
2361
hayeswang445f7f42014-09-23 16:31:47 +08002362 return ret;
2363}
2364
2365static int rtl_stop_rx(struct r8152 *tp)
2366{
2367 int i;
2368
2369 for (i = 0; i < RTL8152_MAX_RX; i++)
2370 usb_kill_urb(tp->rx_info[i].urb);
2371
hayeswangd823ab62015-01-12 12:06:23 +08002372 while (!skb_queue_empty(&tp->rx_queue))
2373 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2374
hayeswang445f7f42014-09-23 16:31:47 +08002375 return 0;
2376}
2377
Hayes Wang9fae5412019-07-03 15:11:56 +08002378static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2379{
2380 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2381 OWN_UPDATE | OWN_CLEAR);
2382}
2383
hayeswang507605a2014-01-02 11:22:43 +08002384static int rtl_enable(struct r8152 *tp)
2385{
2386 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002387
2388 r8152b_reset_packet_filter(tp);
2389
2390 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2391 ocp_data |= CR_RE | CR_TE;
2392 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2393
Hayes Wang9fae5412019-07-03 15:11:56 +08002394 switch (tp->version) {
2395 case RTL_VER_08:
2396 case RTL_VER_09:
2397 r8153b_rx_agg_chg_indicate(tp);
2398 break;
2399 default:
2400 break;
2401 }
2402
hayeswang00a5e362014-02-18 21:48:59 +08002403 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002404
hayeswangaa2e0922015-01-09 10:26:35 +08002405 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002406}
2407
hayeswang507605a2014-01-02 11:22:43 +08002408static int rtl8152_enable(struct r8152 *tp)
2409{
hayeswang68714382014-04-11 17:54:31 +08002410 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2411 return -ENODEV;
2412
hayeswang507605a2014-01-02 11:22:43 +08002413 set_tx_qlen(tp);
2414 rtl_set_eee_plus(tp);
2415
2416 return rtl_enable(tp);
2417}
2418
hayeswang464ec102015-02-12 14:33:46 +08002419static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002420{
hayeswang464ec102015-02-12 14:33:46 +08002421 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002422
hayeswang65b82d62017-06-15 14:44:03 +08002423 switch (tp->version) {
2424 case RTL_VER_03:
2425 case RTL_VER_04:
2426 case RTL_VER_05:
2427 case RTL_VER_06:
2428 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2429 ocp_data);
2430 break;
2431
2432 case RTL_VER_08:
2433 case RTL_VER_09:
2434 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2435 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2436 */
2437 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2438 128 / 8);
2439 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2440 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002441 break;
2442
2443 default:
2444 break;
2445 }
hayeswang464ec102015-02-12 14:33:46 +08002446}
2447
2448static void r8153_set_rx_early_size(struct r8152 *tp)
2449{
hayeswang65b82d62017-06-15 14:44:03 +08002450 u32 ocp_data = agg_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002451
hayeswang65b82d62017-06-15 14:44:03 +08002452 switch (tp->version) {
2453 case RTL_VER_03:
2454 case RTL_VER_04:
2455 case RTL_VER_05:
2456 case RTL_VER_06:
2457 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2458 ocp_data / 4);
2459 break;
2460 case RTL_VER_08:
2461 case RTL_VER_09:
2462 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2463 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002464 break;
2465 default:
2466 WARN_ON_ONCE(1);
2467 break;
2468 }
hayeswang43779f82014-01-02 11:25:10 +08002469}
2470
2471static int rtl8153_enable(struct r8152 *tp)
2472{
hayeswang68714382014-04-11 17:54:31 +08002473 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2474 return -ENODEV;
2475
hayeswang43779f82014-01-02 11:25:10 +08002476 set_tx_qlen(tp);
2477 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002478 r8153_set_rx_early_timeout(tp);
2479 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002480
2481 return rtl_enable(tp);
2482}
2483
hayeswangd70b1132014-09-19 15:17:18 +08002484static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002485{
hayeswangebc2ec482013-08-14 20:54:38 +08002486 u32 ocp_data;
2487 int i;
hayeswangac718b62013-05-02 16:01:25 +00002488
hayeswang68714382014-04-11 17:54:31 +08002489 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2490 rtl_drop_queued_tx(tp);
2491 return;
2492 }
2493
hayeswangac718b62013-05-02 16:01:25 +00002494 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2495 ocp_data &= ~RCR_ACPT_ALL;
2496 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2497
hayeswang00a5e362014-02-18 21:48:59 +08002498 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002499
2500 for (i = 0; i < RTL8152_MAX_TX; i++)
2501 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002502
hayeswang00a5e362014-02-18 21:48:59 +08002503 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002504
2505 for (i = 0; i < 1000; i++) {
2506 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2507 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2508 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002509 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002510 }
2511
2512 for (i = 0; i < 1000; i++) {
2513 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2514 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002515 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002516 }
2517
hayeswang445f7f42014-09-23 16:31:47 +08002518 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002519
2520 rtl8152_nic_reset(tp);
2521}
2522
hayeswang00a5e362014-02-18 21:48:59 +08002523static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2524{
2525 u32 ocp_data;
2526
2527 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2528 if (enable)
2529 ocp_data |= POWER_CUT;
2530 else
2531 ocp_data &= ~POWER_CUT;
2532 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2533
2534 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2535 ocp_data &= ~RESUME_INDICATE;
2536 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002537}
2538
hayeswangc5554292014-09-12 10:43:11 +08002539static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2540{
2541 u32 ocp_data;
2542
2543 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2544 if (enable)
2545 ocp_data |= CPCR_RX_VLAN;
2546 else
2547 ocp_data &= ~CPCR_RX_VLAN;
2548 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2549}
2550
2551static int rtl8152_set_features(struct net_device *dev,
2552 netdev_features_t features)
2553{
2554 netdev_features_t changed = features ^ dev->features;
2555 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002556 int ret;
2557
2558 ret = usb_autopm_get_interface(tp->intf);
2559 if (ret < 0)
2560 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002561
hayeswangb5403272014-10-09 18:00:26 +08002562 mutex_lock(&tp->control);
2563
hayeswangc5554292014-09-12 10:43:11 +08002564 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2565 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2566 rtl_rx_vlan_en(tp, true);
2567 else
2568 rtl_rx_vlan_en(tp, false);
2569 }
2570
hayeswangb5403272014-10-09 18:00:26 +08002571 mutex_unlock(&tp->control);
2572
hayeswang405f8a02014-10-09 18:00:24 +08002573 usb_autopm_put_interface(tp->intf);
2574
2575out:
2576 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002577}
2578
hayeswang21ff2e82014-02-18 21:49:06 +08002579#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2580
2581static u32 __rtl_get_wol(struct r8152 *tp)
2582{
2583 u32 ocp_data;
2584 u32 wolopts = 0;
2585
hayeswang21ff2e82014-02-18 21:49:06 +08002586 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2587 if (ocp_data & LINK_ON_WAKE_EN)
2588 wolopts |= WAKE_PHY;
2589
2590 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2591 if (ocp_data & UWF_EN)
2592 wolopts |= WAKE_UCAST;
2593 if (ocp_data & BWF_EN)
2594 wolopts |= WAKE_BCAST;
2595 if (ocp_data & MWF_EN)
2596 wolopts |= WAKE_MCAST;
2597
2598 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2599 if (ocp_data & MAGIC_EN)
2600 wolopts |= WAKE_MAGIC;
2601
2602 return wolopts;
2603}
2604
2605static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2606{
2607 u32 ocp_data;
2608
2609 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2610
2611 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2612 ocp_data &= ~LINK_ON_WAKE_EN;
2613 if (wolopts & WAKE_PHY)
2614 ocp_data |= LINK_ON_WAKE_EN;
2615 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2616
2617 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002618 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002619 if (wolopts & WAKE_UCAST)
2620 ocp_data |= UWF_EN;
2621 if (wolopts & WAKE_BCAST)
2622 ocp_data |= BWF_EN;
2623 if (wolopts & WAKE_MCAST)
2624 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002625 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2626
2627 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2628
2629 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2630 ocp_data &= ~MAGIC_EN;
2631 if (wolopts & WAKE_MAGIC)
2632 ocp_data |= MAGIC_EN;
2633 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2634
2635 if (wolopts & WAKE_ANY)
2636 device_set_wakeup_enable(&tp->udev->dev, true);
2637 else
2638 device_set_wakeup_enable(&tp->udev->dev, false);
2639}
2640
hayeswang134f98b2017-06-09 17:11:40 +08002641static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2642{
2643 /* MAC clock speed down */
2644 if (enable) {
2645 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2646 ALDPS_SPDWN_RATIO);
2647 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2648 EEE_SPDWN_RATIO);
2649 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2650 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2651 U1U2_SPDWN_EN | L1_SPDWN_EN);
2652 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2653 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2654 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2655 TP1000_SPDWN_EN);
2656 } else {
2657 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2658 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2659 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2660 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2661 }
2662}
2663
hayeswangb2143962015-07-24 13:54:23 +08002664static void r8153_u1u2en(struct r8152 *tp, bool enable)
2665{
2666 u8 u1u2[8];
2667
2668 if (enable)
2669 memset(u1u2, 0xff, sizeof(u1u2));
2670 else
2671 memset(u1u2, 0x00, sizeof(u1u2));
2672
2673 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2674}
2675
hayeswang65b82d62017-06-15 14:44:03 +08002676static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2677{
2678 u32 ocp_data;
2679
2680 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2681 if (enable)
2682 ocp_data |= LPM_U1U2_EN;
2683 else
2684 ocp_data &= ~LPM_U1U2_EN;
2685
2686 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2687}
2688
hayeswangb2143962015-07-24 13:54:23 +08002689static void r8153_u2p3en(struct r8152 *tp, bool enable)
2690{
2691 u32 ocp_data;
2692
2693 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002694 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002695 ocp_data |= U2P3_ENABLE;
2696 else
2697 ocp_data &= ~U2P3_ENABLE;
2698 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2699}
2700
hayeswang65b82d62017-06-15 14:44:03 +08002701static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2702{
2703 u32 ocp_data;
2704
2705 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2706 ocp_data &= ~clear;
2707 ocp_data |= set;
2708 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2709}
2710
2711static void r8153b_green_en(struct r8152 *tp, bool enable)
2712{
2713 u16 data;
2714
2715 if (enable) {
2716 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2717 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2718 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2719 } else {
2720 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2721 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2722 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2723 }
2724
2725 data = sram_read(tp, SRAM_GREEN_CFG);
2726 data |= GREEN_ETH_EN;
2727 sram_write(tp, SRAM_GREEN_CFG, data);
2728
2729 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2730}
2731
hayeswangc564b872017-06-09 17:11:38 +08002732static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2733{
2734 u16 data;
2735 int i;
2736
2737 for (i = 0; i < 500; i++) {
2738 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2739 data &= PHY_STAT_MASK;
2740 if (desired) {
2741 if (data == desired)
2742 break;
2743 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2744 data == PHY_STAT_EXT_INIT) {
2745 break;
2746 }
2747
2748 msleep(20);
2749 }
2750
2751 return data;
2752}
2753
hayeswang65b82d62017-06-15 14:44:03 +08002754static void r8153b_ups_en(struct r8152 *tp, bool enable)
2755{
2756 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2757
2758 if (enable) {
2759 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2760 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2761
2762 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2763 ocp_data |= BIT(0);
2764 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2765 } else {
2766 u16 data;
2767
2768 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2769 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2770
2771 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2772 ocp_data &= ~BIT(0);
2773 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2774
2775 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2776 ocp_data &= ~PCUT_STATUS;
2777 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2778
2779 data = r8153_phy_status(tp, 0);
2780
2781 switch (data) {
2782 case PHY_STAT_PWRDN:
2783 case PHY_STAT_EXT_INIT:
2784 r8153b_green_en(tp,
2785 test_bit(GREEN_ETHERNET, &tp->flags));
2786
2787 data = r8152_mdio_read(tp, MII_BMCR);
2788 data &= ~BMCR_PDOWN;
2789 data |= BMCR_RESET;
2790 r8152_mdio_write(tp, MII_BMCR, data);
2791
2792 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002793 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002794
2795 default:
2796 if (data != PHY_STAT_LAN_ON)
2797 netif_warn(tp, link, tp->netdev,
2798 "PHY not ready");
2799 break;
2800 }
2801 }
2802}
2803
hayeswangb2143962015-07-24 13:54:23 +08002804static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2805{
2806 u32 ocp_data;
2807
2808 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2809 if (enable)
2810 ocp_data |= PWR_EN | PHASE2_EN;
2811 else
2812 ocp_data &= ~(PWR_EN | PHASE2_EN);
2813 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2814
2815 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2816 ocp_data &= ~PCUT_STATUS;
2817 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2818}
2819
hayeswang65b82d62017-06-15 14:44:03 +08002820static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2821{
2822 u32 ocp_data;
2823
2824 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2825 if (enable)
2826 ocp_data |= PWR_EN | PHASE2_EN;
2827 else
2828 ocp_data &= ~PWR_EN;
2829 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2830
2831 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2832 ocp_data &= ~PCUT_STATUS;
2833 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2834}
2835
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002836static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08002837{
2838 u32 ocp_data;
2839
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002840 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08002841 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002842 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08002843 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002844 ocp_data &= ~UPCOMING_RUNTIME_D3;
2845 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002846
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002847 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
2848 ocp_data &= ~LINK_CHG_EVENT;
2849 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
2850
2851 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
2852 ocp_data &= ~LINK_CHANGE_FLAG;
2853 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002854}
2855
hayeswang7daed8d2015-07-24 13:54:24 +08002856static bool rtl_can_wakeup(struct r8152 *tp)
2857{
2858 struct usb_device *udev = tp->udev;
2859
2860 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
2861}
2862
hayeswang9a4be1b2014-02-18 21:49:07 +08002863static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2864{
2865 if (enable) {
2866 u32 ocp_data;
2867
2868 __rtl_set_wol(tp, WAKE_ANY);
2869
2870 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2871
2872 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2873 ocp_data |= LINK_OFF_WAKE_EN;
2874 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2875
2876 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2877 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08002878 u32 ocp_data;
2879
hayeswang9a4be1b2014-02-18 21:49:07 +08002880 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08002881
2882 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2883
2884 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2885 ocp_data &= ~LINK_OFF_WAKE_EN;
2886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2887
2888 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08002889 }
2890}
hayeswangf95ae8a2016-06-30 15:33:35 +08002891
hayeswang2609af12016-07-05 16:11:46 +08002892static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
2893{
hayeswang2609af12016-07-05 16:11:46 +08002894 if (enable) {
2895 r8153_u1u2en(tp, false);
2896 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002897 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08002898 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08002899 } else {
hayeswang02552752017-06-09 17:11:42 +08002900 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002901 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08002902
2903 switch (tp->version) {
2904 case RTL_VER_03:
2905 case RTL_VER_04:
2906 break;
2907 case RTL_VER_05:
2908 case RTL_VER_06:
2909 default:
2910 r8153_u2p3en(tp, true);
2911 break;
2912 }
2913
hayeswangb2143962015-07-24 13:54:23 +08002914 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08002915 }
2916}
2917
hayeswang65b82d62017-06-15 14:44:03 +08002918static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
2919{
2920 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002921 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08002922 r8153b_u1u2en(tp, false);
2923 r8153_u2p3en(tp, false);
2924 rtl_runtime_suspend_enable(tp, true);
2925 r8153b_ups_en(tp, true);
2926 } else {
2927 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002928 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08002929 rtl_runtime_suspend_enable(tp, false);
2930 r8153_u2p3en(tp, true);
2931 r8153b_u1u2en(tp, true);
2932 }
2933}
2934
hayeswang43499682014-02-18 21:48:58 +08002935static void r8153_teredo_off(struct r8152 *tp)
2936{
2937 u32 ocp_data;
2938
hayeswang65b82d62017-06-15 14:44:03 +08002939 switch (tp->version) {
2940 case RTL_VER_01:
2941 case RTL_VER_02:
2942 case RTL_VER_03:
2943 case RTL_VER_04:
2944 case RTL_VER_05:
2945 case RTL_VER_06:
2946 case RTL_VER_07:
2947 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2948 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
2949 OOB_TEREDO_EN);
2950 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2951 break;
2952
2953 case RTL_VER_08:
2954 case RTL_VER_09:
2955 /* The bit 0 ~ 7 are relative with teredo settings. They are
2956 * W1C (write 1 to clear), so set all 1 to disable it.
2957 */
2958 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
2959 break;
2960
2961 default:
2962 break;
2963 }
hayeswang43499682014-02-18 21:48:58 +08002964
2965 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2966 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2967 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2968}
2969
hayeswang93fe9b12016-06-16 10:55:18 +08002970static void rtl_reset_bmu(struct r8152 *tp)
2971{
2972 u32 ocp_data;
2973
2974 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
2975 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
2976 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2977 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
2978 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2979}
2980
hayeswangcda9fb02016-01-07 17:51:12 +08002981static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08002982{
hayeswangcda9fb02016-01-07 17:51:12 +08002983 if (enable) {
2984 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2985 LINKENA | DIS_SDSAVE);
2986 } else {
2987 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
2988 DIS_SDSAVE);
2989 msleep(20);
2990 }
hayeswang43499682014-02-18 21:48:58 +08002991}
2992
hayeswange6449532016-09-20 16:22:05 +08002993static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
2994{
2995 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
2996 ocp_reg_write(tp, OCP_EEE_DATA, reg);
2997 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
2998}
2999
3000static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
3001{
3002 u16 data;
3003
3004 r8152_mmd_indirect(tp, dev, reg);
3005 data = ocp_reg_read(tp, OCP_EEE_DATA);
3006 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3007
3008 return data;
3009}
3010
3011static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3012{
3013 r8152_mmd_indirect(tp, dev, reg);
3014 ocp_reg_write(tp, OCP_EEE_DATA, data);
3015 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3016}
3017
3018static void r8152_eee_en(struct r8152 *tp, bool enable)
3019{
3020 u16 config1, config2, config3;
3021 u32 ocp_data;
3022
3023 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3024 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3025 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3026 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3027
3028 if (enable) {
3029 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3030 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3031 config1 |= sd_rise_time(1);
3032 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3033 config3 |= fast_snr(42);
3034 } else {
3035 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3036 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3037 RX_QUIET_EN);
3038 config1 |= sd_rise_time(7);
3039 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3040 config3 |= fast_snr(511);
3041 }
3042
3043 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3044 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3045 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3046 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3047}
3048
3049static void r8152b_enable_eee(struct r8152 *tp)
3050{
3051 r8152_eee_en(tp, true);
3052 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3053}
3054
3055static void r8152b_enable_fc(struct r8152 *tp)
3056{
3057 u16 anar;
3058
3059 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3060 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3061 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3062}
3063
hayeswangd70b1132014-09-19 15:17:18 +08003064static void rtl8152_disable(struct r8152 *tp)
3065{
hayeswangcda9fb02016-01-07 17:51:12 +08003066 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003067 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003068 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003069}
3070
hayeswang43499682014-02-18 21:48:58 +08003071static void r8152b_hw_phy_cfg(struct r8152 *tp)
3072{
hayeswangef39df82016-09-20 16:22:07 +08003073 r8152b_enable_eee(tp);
3074 r8152_aldps_en(tp, true);
3075 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003076
hayeswangaa66a5f2014-02-18 21:49:04 +08003077 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003078}
3079
hayeswangac718b62013-05-02 16:01:25 +00003080static void r8152b_exit_oob(struct r8152 *tp)
3081{
hayeswangdb8515e2014-03-06 15:07:16 +08003082 u32 ocp_data;
3083 int i;
hayeswangac718b62013-05-02 16:01:25 +00003084
3085 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3086 ocp_data &= ~RCR_ACPT_ALL;
3087 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3088
hayeswang00a5e362014-02-18 21:48:59 +08003089 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003090 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003091 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3092 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3093
3094 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3095 ocp_data &= ~NOW_IS_OOB;
3096 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3097
3098 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3099 ocp_data &= ~MCU_BORW_EN;
3100 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3101
3102 for (i = 0; i < 1000; i++) {
3103 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3104 if (ocp_data & LINK_LIST_READY)
3105 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003106 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003107 }
3108
3109 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3110 ocp_data |= RE_INIT_LL;
3111 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3112
3113 for (i = 0; i < 1000; i++) {
3114 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3115 if (ocp_data & LINK_LIST_READY)
3116 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003117 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003118 }
3119
3120 rtl8152_nic_reset(tp);
3121
3122 /* rx share fifo credit full threshold */
3123 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3124
hayeswanga3cc4652014-07-24 16:37:43 +08003125 if (tp->udev->speed == USB_SPEED_FULL ||
3126 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003127 /* rx share fifo credit near full threshold */
3128 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3129 RXFIFO_THR2_FULL);
3130 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3131 RXFIFO_THR3_FULL);
3132 } else {
3133 /* rx share fifo credit near full threshold */
3134 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3135 RXFIFO_THR2_HIGH);
3136 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3137 RXFIFO_THR3_HIGH);
3138 }
3139
3140 /* TX share fifo free credit full threshold */
3141 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3142
3143 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003144 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003145 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3146 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3147
hayeswangc5554292014-09-12 10:43:11 +08003148 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003149
3150 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3151
3152 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3153 ocp_data |= TCR0_AUTO_FIFO;
3154 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3155}
3156
3157static void r8152b_enter_oob(struct r8152 *tp)
3158{
hayeswang45f4a192014-01-06 17:08:41 +08003159 u32 ocp_data;
3160 int i;
hayeswangac718b62013-05-02 16:01:25 +00003161
3162 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3163 ocp_data &= ~NOW_IS_OOB;
3164 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3165
3166 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3167 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3168 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3169
hayeswangd70b1132014-09-19 15:17:18 +08003170 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003171
3172 for (i = 0; i < 1000; i++) {
3173 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3174 if (ocp_data & LINK_LIST_READY)
3175 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003176 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003177 }
3178
3179 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3180 ocp_data |= RE_INIT_LL;
3181 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3182
3183 for (i = 0; i < 1000; i++) {
3184 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3185 if (ocp_data & LINK_LIST_READY)
3186 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003187 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003188 }
3189
3190 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3191
hayeswangc5554292014-09-12 10:43:11 +08003192 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003193
Kevin Lo59c0b472019-08-01 11:29:38 +08003194 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00003195 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003196 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00003197
3198 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3199 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3200 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3201
hayeswang00a5e362014-02-18 21:48:59 +08003202 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003203
3204 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3205 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3206 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3207}
3208
hayeswang65b82d62017-06-15 14:44:03 +08003209static int r8153_patch_request(struct r8152 *tp, bool request)
3210{
3211 u16 data;
3212 int i;
3213
3214 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3215 if (request)
3216 data |= PATCH_REQUEST;
3217 else
3218 data &= ~PATCH_REQUEST;
3219 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3220
3221 for (i = 0; request && i < 5000; i++) {
3222 usleep_range(1000, 2000);
3223 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3224 break;
3225 }
3226
3227 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3228 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3229 r8153_patch_request(tp, false);
3230 return -ETIME;
3231 } else {
3232 return 0;
3233 }
3234}
3235
hayeswange6449532016-09-20 16:22:05 +08003236static void r8153_aldps_en(struct r8152 *tp, bool enable)
3237{
3238 u16 data;
3239
3240 data = ocp_reg_read(tp, OCP_POWER_CFG);
3241 if (enable) {
3242 data |= EN_ALDPS;
3243 ocp_reg_write(tp, OCP_POWER_CFG, data);
3244 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003245 int i;
3246
hayeswange6449532016-09-20 16:22:05 +08003247 data &= ~EN_ALDPS;
3248 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003249 for (i = 0; i < 20; i++) {
3250 usleep_range(1000, 2000);
3251 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3252 break;
3253 }
hayeswange6449532016-09-20 16:22:05 +08003254 }
3255}
3256
hayeswang65b82d62017-06-15 14:44:03 +08003257static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3258{
3259 r8153_aldps_en(tp, enable);
3260
3261 if (enable)
3262 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3263 else
3264 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3265}
3266
hayeswange6449532016-09-20 16:22:05 +08003267static void r8153_eee_en(struct r8152 *tp, bool enable)
3268{
3269 u32 ocp_data;
3270 u16 config;
3271
3272 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3273 config = ocp_reg_read(tp, OCP_EEE_CFG);
3274
3275 if (enable) {
3276 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3277 config |= EEE10_EN;
3278 } else {
3279 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3280 config &= ~EEE10_EN;
3281 }
3282
3283 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3284 ocp_reg_write(tp, OCP_EEE_CFG, config);
3285}
3286
hayeswang65b82d62017-06-15 14:44:03 +08003287static void r8153b_eee_en(struct r8152 *tp, bool enable)
3288{
3289 r8153_eee_en(tp, enable);
3290
3291 if (enable)
3292 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3293 else
3294 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3295}
3296
3297static void r8153b_enable_fc(struct r8152 *tp)
3298{
3299 r8152b_enable_fc(tp);
3300 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3301}
3302
hayeswang43779f82014-01-02 11:25:10 +08003303static void r8153_hw_phy_cfg(struct r8152 *tp)
3304{
3305 u32 ocp_data;
3306 u16 data;
3307
hayeswangd768c612016-09-20 16:22:09 +08003308 /* disable ALDPS before updating the PHY parameters */
3309 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003310
hayeswangd768c612016-09-20 16:22:09 +08003311 /* disable EEE before updating the PHY parameters */
3312 r8153_eee_en(tp, false);
3313 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang43779f82014-01-02 11:25:10 +08003314
3315 if (tp->version == RTL_VER_03) {
3316 data = ocp_reg_read(tp, OCP_EEE_CFG);
3317 data &= ~CTAP_SHORT_EN;
3318 ocp_reg_write(tp, OCP_EEE_CFG, data);
3319 }
3320
3321 data = ocp_reg_read(tp, OCP_POWER_CFG);
3322 data |= EEE_CLKDIV_EN;
3323 ocp_reg_write(tp, OCP_POWER_CFG, data);
3324
3325 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3326 data |= EN_10M_BGOFF;
3327 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3328 data = ocp_reg_read(tp, OCP_POWER_CFG);
3329 data |= EN_10M_PLLOFF;
3330 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003331 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003332
3333 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3334 ocp_data |= PFM_PWM_SWITCH;
3335 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3336
hayeswangb4d99de2015-01-19 17:02:46 +08003337 /* Enable LPF corner auto tune */
3338 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003339
hayeswangb4d99de2015-01-19 17:02:46 +08003340 /* Adjust 10M Amplitude */
3341 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3342 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003343
hayeswangaf0287e2016-09-20 16:22:08 +08003344 r8153_eee_en(tp, true);
3345 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3346
hayeswangef39df82016-09-20 16:22:07 +08003347 r8153_aldps_en(tp, true);
3348 r8152b_enable_fc(tp);
3349
hayeswang3cb32342017-06-09 17:11:43 +08003350 switch (tp->version) {
3351 case RTL_VER_03:
3352 case RTL_VER_04:
3353 break;
3354 case RTL_VER_05:
3355 case RTL_VER_06:
3356 default:
3357 r8153_u2p3en(tp, true);
3358 break;
3359 }
3360
hayeswangaa66a5f2014-02-18 21:49:04 +08003361 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003362}
3363
hayeswang65b82d62017-06-15 14:44:03 +08003364static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3365{
3366 u32 ocp_data;
3367
3368 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3369 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3370 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3371 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3372
3373 return ocp_data;
3374}
3375
3376static void r8153b_hw_phy_cfg(struct r8152 *tp)
3377{
3378 u32 ocp_data, ups_flags = 0;
3379 u16 data;
3380
3381 /* disable ALDPS before updating the PHY parameters */
3382 r8153b_aldps_en(tp, false);
3383
3384 /* disable EEE before updating the PHY parameters */
3385 r8153b_eee_en(tp, false);
3386 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3387
3388 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3389
3390 data = sram_read(tp, SRAM_GREEN_CFG);
3391 data |= R_TUNE_EN;
3392 sram_write(tp, SRAM_GREEN_CFG, data);
3393 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3394 data |= PGA_RETURN_EN;
3395 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3396
3397 /* ADC Bias Calibration:
3398 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3399 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3400 * ADC ioffset.
3401 */
3402 ocp_data = r8152_efuse_read(tp, 0x7d);
3403 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3404 if (data != 0xffff)
3405 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3406
3407 /* ups mode tx-link-pulse timing adjustment:
3408 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3409 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3410 */
3411 ocp_data = ocp_reg_read(tp, 0xc426);
3412 ocp_data &= 0x3fff;
3413 if (ocp_data) {
3414 u32 swr_cnt_1ms_ini;
3415
3416 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3417 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3418 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3419 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3420 }
3421
3422 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3423 ocp_data |= PFM_PWM_SWITCH;
3424 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3425
3426 /* Advnace EEE */
3427 if (!r8153_patch_request(tp, true)) {
3428 data = ocp_reg_read(tp, OCP_POWER_CFG);
3429 data |= EEE_CLKDIV_EN;
3430 ocp_reg_write(tp, OCP_POWER_CFG, data);
3431
3432 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3433 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3434 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3435
3436 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3437 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3438
3439 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3440 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3441 UPS_FLAGS_EEE_PLLOFF_GIGA;
3442
3443 r8153_patch_request(tp, false);
3444 }
3445
3446 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3447
3448 r8153b_eee_en(tp, true);
3449 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3450
3451 r8153b_aldps_en(tp, true);
3452 r8153b_enable_fc(tp);
3453 r8153_u2p3en(tp, true);
3454
3455 set_bit(PHY_RESET, &tp->flags);
3456}
3457
hayeswang43779f82014-01-02 11:25:10 +08003458static void r8153_first_init(struct r8152 *tp)
3459{
3460 u32 ocp_data;
3461 int i;
3462
hayeswang134f98b2017-06-09 17:11:40 +08003463 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003464 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003465 r8153_teredo_off(tp);
3466
3467 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3468 ocp_data &= ~RCR_ACPT_ALL;
3469 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3470
hayeswang43779f82014-01-02 11:25:10 +08003471 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003472 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003473
3474 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3475 ocp_data &= ~NOW_IS_OOB;
3476 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3477
3478 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3479 ocp_data &= ~MCU_BORW_EN;
3480 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3481
3482 for (i = 0; i < 1000; i++) {
3483 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3484 if (ocp_data & LINK_LIST_READY)
3485 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003486 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003487 }
3488
3489 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3490 ocp_data |= RE_INIT_LL;
3491 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3492
3493 for (i = 0; i < 1000; i++) {
3494 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3495 if (ocp_data & LINK_LIST_READY)
3496 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003497 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003498 }
3499
hayeswangc5554292014-09-12 10:43:11 +08003500 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003501
hayeswangb65c0c92017-06-21 11:25:18 +08003502 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003503 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003504 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003505
3506 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3507 ocp_data |= TCR0_AUTO_FIFO;
3508 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3509
3510 rtl8152_nic_reset(tp);
3511
3512 /* rx share fifo credit full threshold */
3513 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3514 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3515 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3516 /* TX share fifo free credit full threshold */
3517 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003518}
3519
3520static void r8153_enter_oob(struct r8152 *tp)
3521{
3522 u32 ocp_data;
3523 int i;
3524
hayeswang134f98b2017-06-09 17:11:40 +08003525 r8153_mac_clk_spd(tp, true);
3526
hayeswang43779f82014-01-02 11:25:10 +08003527 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3528 ocp_data &= ~NOW_IS_OOB;
3529 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3530
hayeswangd70b1132014-09-19 15:17:18 +08003531 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003532 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003533
3534 for (i = 0; i < 1000; i++) {
3535 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3536 if (ocp_data & LINK_LIST_READY)
3537 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003538 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003539 }
3540
3541 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3542 ocp_data |= RE_INIT_LL;
3543 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3544
3545 for (i = 0; i < 1000; i++) {
3546 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3547 if (ocp_data & LINK_LIST_READY)
3548 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003549 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003550 }
3551
hayeswangb65c0c92017-06-21 11:25:18 +08003552 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003553 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003554
hayeswang65b82d62017-06-15 14:44:03 +08003555 switch (tp->version) {
3556 case RTL_VER_03:
3557 case RTL_VER_04:
3558 case RTL_VER_05:
3559 case RTL_VER_06:
3560 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3561 ocp_data &= ~TEREDO_WAKE_MASK;
3562 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3563 break;
3564
3565 case RTL_VER_08:
3566 case RTL_VER_09:
3567 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3568 * type. Set it to zero. bits[7:0] are the W1C bits about
3569 * the events. Set them to all 1 to clear them.
3570 */
3571 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3572 break;
3573
3574 default:
3575 break;
3576 }
hayeswang43779f82014-01-02 11:25:10 +08003577
hayeswangc5554292014-09-12 10:43:11 +08003578 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003579
Kevin Lo59c0b472019-08-01 11:29:38 +08003580 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08003581 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08003582 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003583
3584 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3585 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3586 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3587
hayeswang00a5e362014-02-18 21:48:59 +08003588 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003589
3590 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3591 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3592 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3593}
3594
hayeswangd70b1132014-09-19 15:17:18 +08003595static void rtl8153_disable(struct r8152 *tp)
3596{
hayeswangcda9fb02016-01-07 17:51:12 +08003597 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003598 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003599 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003600 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003601}
3602
hayeswang65b82d62017-06-15 14:44:03 +08003603static void rtl8153b_disable(struct r8152 *tp)
3604{
3605 r8153b_aldps_en(tp, false);
3606 rtl_disable(tp);
3607 rtl_reset_bmu(tp);
3608 r8153b_aldps_en(tp, true);
3609}
3610
hayeswangac718b62013-05-02 16:01:25 +00003611static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3612{
hayeswang43779f82014-01-02 11:25:10 +08003613 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003614 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003615 int ret = 0;
3616
hayeswangac718b62013-05-02 16:01:25 +00003617 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3618 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3619 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003620 if (tp->mii.supports_gmii) {
3621 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3622 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3623 } else {
3624 gbcr = 0;
3625 }
hayeswangac718b62013-05-02 16:01:25 +00003626
3627 if (autoneg == AUTONEG_DISABLE) {
3628 if (speed == SPEED_10) {
3629 bmcr = 0;
3630 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003631 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003632 } else if (speed == SPEED_100) {
3633 bmcr = BMCR_SPEED100;
3634 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003635 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003636 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3637 bmcr = BMCR_SPEED1000;
3638 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003639 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003640 } else {
3641 ret = -EINVAL;
3642 goto out;
3643 }
3644
hayeswang65b82d62017-06-15 14:44:03 +08003645 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003646 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003647 if (speed != SPEED_1000)
3648 speed_duplex++;
3649 }
hayeswangac718b62013-05-02 16:01:25 +00003650 } else {
3651 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003652 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003653 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003654 speed_duplex = NWAY_10M_FULL;
3655 } else {
hayeswangac718b62013-05-02 16:01:25 +00003656 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003657 speed_duplex = NWAY_10M_HALF;
3658 }
hayeswangac718b62013-05-02 16:01:25 +00003659 } else if (speed == SPEED_100) {
3660 if (duplex == DUPLEX_FULL) {
3661 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3662 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003663 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003664 } else {
3665 anar |= ADVERTISE_10HALF;
3666 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003667 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003668 }
hayeswang43779f82014-01-02 11:25:10 +08003669 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3670 if (duplex == DUPLEX_FULL) {
3671 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3672 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3673 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3674 } else {
3675 anar |= ADVERTISE_10HALF;
3676 anar |= ADVERTISE_100HALF;
3677 gbcr |= ADVERTISE_1000HALF;
3678 }
hayeswang65b82d62017-06-15 14:44:03 +08003679 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003680 } else {
3681 ret = -EINVAL;
3682 goto out;
3683 }
3684
3685 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3686 }
3687
hayeswangfae56172016-06-16 14:08:29 +08003688 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003689 bmcr |= BMCR_RESET;
3690
hayeswang43779f82014-01-02 11:25:10 +08003691 if (tp->mii.supports_gmii)
3692 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3693
hayeswangac718b62013-05-02 16:01:25 +00003694 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3695 r8152_mdio_write(tp, MII_BMCR, bmcr);
3696
hayeswang65b82d62017-06-15 14:44:03 +08003697 switch (tp->version) {
3698 case RTL_VER_08:
3699 case RTL_VER_09:
3700 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3701 UPS_FLAGS_SPEED_MASK);
3702 break;
3703
3704 default:
3705 break;
3706 }
3707
hayeswangfae56172016-06-16 14:08:29 +08003708 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003709 int i;
3710
hayeswangaa66a5f2014-02-18 21:49:04 +08003711 for (i = 0; i < 50; i++) {
3712 msleep(20);
3713 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3714 break;
3715 }
3716 }
3717
hayeswangac718b62013-05-02 16:01:25 +00003718out:
hayeswangac718b62013-05-02 16:01:25 +00003719 return ret;
3720}
3721
hayeswangd70b1132014-09-19 15:17:18 +08003722static void rtl8152_up(struct r8152 *tp)
3723{
3724 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3725 return;
3726
hayeswangcda9fb02016-01-07 17:51:12 +08003727 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003728 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003729 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003730}
3731
hayeswangac718b62013-05-02 16:01:25 +00003732static void rtl8152_down(struct r8152 *tp)
3733{
hayeswang68714382014-04-11 17:54:31 +08003734 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3735 rtl_drop_queued_tx(tp);
3736 return;
3737 }
3738
hayeswang00a5e362014-02-18 21:48:59 +08003739 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003740 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003741 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003742 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003743}
3744
hayeswangd70b1132014-09-19 15:17:18 +08003745static void rtl8153_up(struct r8152 *tp)
3746{
3747 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3748 return;
3749
hayeswangb2143962015-07-24 13:54:23 +08003750 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003751 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003752 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003753 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003754 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003755
3756 switch (tp->version) {
3757 case RTL_VER_03:
3758 case RTL_VER_04:
3759 break;
3760 case RTL_VER_05:
3761 case RTL_VER_06:
3762 default:
3763 r8153_u2p3en(tp, true);
3764 break;
3765 }
3766
hayeswangb2143962015-07-24 13:54:23 +08003767 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003768}
3769
hayeswang43779f82014-01-02 11:25:10 +08003770static void rtl8153_down(struct r8152 *tp)
3771{
hayeswang68714382014-04-11 17:54:31 +08003772 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3773 rtl_drop_queued_tx(tp);
3774 return;
3775 }
3776
hayeswangb9702722014-02-18 21:49:00 +08003777 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003778 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003779 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003780 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003781 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003782 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003783}
3784
hayeswang65b82d62017-06-15 14:44:03 +08003785static void rtl8153b_up(struct r8152 *tp)
3786{
3787 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3788 return;
3789
3790 r8153b_u1u2en(tp, false);
3791 r8153_u2p3en(tp, false);
3792 r8153b_aldps_en(tp, false);
3793
3794 r8153_first_init(tp);
3795 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3796
3797 r8153b_aldps_en(tp, true);
3798 r8153_u2p3en(tp, true);
3799 r8153b_u1u2en(tp, true);
3800}
3801
3802static void rtl8153b_down(struct r8152 *tp)
3803{
3804 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3805 rtl_drop_queued_tx(tp);
3806 return;
3807 }
3808
3809 r8153b_u1u2en(tp, false);
3810 r8153_u2p3en(tp, false);
3811 r8153b_power_cut_en(tp, false);
3812 r8153b_aldps_en(tp, false);
3813 r8153_enter_oob(tp);
3814 r8153b_aldps_en(tp, true);
3815}
3816
hayeswang2dd49e02015-09-07 11:57:44 +08003817static bool rtl8152_in_nway(struct r8152 *tp)
3818{
3819 u16 nway_state;
3820
3821 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3822 tp->ocp_base = 0x2000;
3823 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3824 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3825
3826 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3827 if (nway_state & 0xc000)
3828 return false;
3829 else
3830 return true;
3831}
3832
3833static bool rtl8153_in_nway(struct r8152 *tp)
3834{
3835 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
3836
3837 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
3838 return false;
3839 else
3840 return true;
3841}
3842
hayeswangac718b62013-05-02 16:01:25 +00003843static void set_carrier(struct r8152 *tp)
3844{
3845 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08003846 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00003847 u8 speed;
3848
3849 speed = rtl8152_get_speed(tp);
3850
3851 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08003852 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08003853 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08003854 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003855 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00003856 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08003857 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08003858 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
3859 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08003860 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08003861 netif_wake_queue(netdev);
3862 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08003863 } else if (netif_queue_stopped(netdev) &&
3864 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
3865 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003866 }
3867 } else {
hayeswang51d979f2015-02-06 11:30:47 +08003868 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00003869 netif_carrier_off(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003870 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08003871 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08003872 napi_enable(napi);
hayeswangde9bf292017-01-26 09:38:32 +08003873 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00003874 }
3875 }
hayeswangac718b62013-05-02 16:01:25 +00003876}
3877
3878static void rtl_work_func_t(struct work_struct *work)
3879{
3880 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
3881
hayeswanga1f83fe2014-11-12 10:05:05 +08003882 /* If the device is unplugged or !netif_running(), the workqueue
3883 * doesn't need to wake the device, and could return directly.
3884 */
3885 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
3886 return;
3887
hayeswang9a4be1b2014-02-18 21:49:07 +08003888 if (usb_autopm_get_interface(tp->intf) < 0)
3889 return;
3890
hayeswangac718b62013-05-02 16:01:25 +00003891 if (!test_bit(WORK_ENABLE, &tp->flags))
3892 goto out1;
3893
hayeswangb5403272014-10-09 18:00:26 +08003894 if (!mutex_trylock(&tp->control)) {
3895 schedule_delayed_work(&tp->schedule, 0);
3896 goto out1;
3897 }
3898
hayeswang216a8342016-01-07 17:51:11 +08003899 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08003900 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00003901
hayeswang216a8342016-01-07 17:51:11 +08003902 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00003903 _rtl8152_set_rx_mode(tp->netdev);
3904
hayeswangd823ab62015-01-12 12:06:23 +08003905 /* don't schedule napi before linking */
hayeswang216a8342016-01-07 17:51:11 +08003906 if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
3907 netif_carrier_ok(tp->netdev))
hayeswangd823ab62015-01-12 12:06:23 +08003908 napi_schedule(&tp->napi);
hayeswangaa66a5f2014-02-18 21:49:04 +08003909
hayeswangb5403272014-10-09 18:00:26 +08003910 mutex_unlock(&tp->control);
3911
hayeswangac718b62013-05-02 16:01:25 +00003912out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08003913 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003914}
3915
hayeswanga028a9e2016-06-13 17:49:36 +08003916static void rtl_hw_phy_work_func_t(struct work_struct *work)
3917{
3918 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
3919
3920 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3921 return;
3922
3923 if (usb_autopm_get_interface(tp->intf) < 0)
3924 return;
3925
3926 mutex_lock(&tp->control);
3927
3928 tp->rtl_ops.hw_phy_cfg(tp);
3929
hayeswangaa7e26b2016-06-13 17:49:38 +08003930 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08003931
hayeswanga028a9e2016-06-13 17:49:36 +08003932 mutex_unlock(&tp->control);
3933
3934 usb_autopm_put_interface(tp->intf);
3935}
3936
hayeswang5ee3c602016-01-07 17:12:17 +08003937#ifdef CONFIG_PM_SLEEP
3938static int rtl_notifier(struct notifier_block *nb, unsigned long action,
3939 void *data)
3940{
3941 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
3942
3943 switch (action) {
3944 case PM_HIBERNATION_PREPARE:
3945 case PM_SUSPEND_PREPARE:
3946 usb_autopm_get_interface(tp->intf);
3947 break;
3948
3949 case PM_POST_HIBERNATION:
3950 case PM_POST_SUSPEND:
3951 usb_autopm_put_interface(tp->intf);
3952 break;
3953
3954 case PM_POST_RESTORE:
3955 case PM_RESTORE_PREPARE:
3956 default:
3957 break;
3958 }
3959
3960 return NOTIFY_DONE;
3961}
3962#endif
3963
hayeswangac718b62013-05-02 16:01:25 +00003964static int rtl8152_open(struct net_device *netdev)
3965{
3966 struct r8152 *tp = netdev_priv(netdev);
3967 int res = 0;
3968
hayeswang7e9da482014-02-18 21:49:05 +08003969 res = alloc_all_mem(tp);
3970 if (res)
3971 goto out;
3972
hayeswang9a4be1b2014-02-18 21:49:07 +08003973 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08003974 if (res < 0)
3975 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08003976
hayeswangb5403272014-10-09 18:00:26 +08003977 mutex_lock(&tp->control);
3978
hayeswang7e9da482014-02-18 21:49:05 +08003979 tp->rtl_ops.up(tp);
3980
hayeswang40a82912013-08-14 20:54:40 +08003981 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003982 netif_start_queue(netdev);
3983 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003984
hayeswang3d55f442014-02-06 11:55:48 +08003985 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3986 if (res) {
3987 if (res == -ENODEV)
3988 netif_device_detach(tp->netdev);
3989 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3990 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08003991 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08003992 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08003993 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003994
hayeswangb5403272014-10-09 18:00:26 +08003995 mutex_unlock(&tp->control);
3996
hayeswang9a4be1b2014-02-18 21:49:07 +08003997 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08003998#ifdef CONFIG_PM_SLEEP
3999 tp->pm_notifier.notifier_call = rtl_notifier;
4000 register_pm_notifier(&tp->pm_notifier);
4001#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08004002 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004003
Guenter Roeckca0a7532016-11-09 19:51:25 -08004004out_unlock:
4005 mutex_unlock(&tp->control);
4006 usb_autopm_put_interface(tp->intf);
4007out_free:
4008 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08004009out:
hayeswangac718b62013-05-02 16:01:25 +00004010 return res;
4011}
4012
4013static int rtl8152_close(struct net_device *netdev)
4014{
4015 struct r8152 *tp = netdev_priv(netdev);
4016 int res = 0;
4017
hayeswang5ee3c602016-01-07 17:12:17 +08004018#ifdef CONFIG_PM_SLEEP
4019 unregister_pm_notifier(&tp->pm_notifier);
4020#endif
Jiri Slaby0ee1f472018-06-25 09:26:27 +02004021 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
4022 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004023 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08004024 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00004025 cancel_delayed_work_sync(&tp->schedule);
4026 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004027
4028 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004029 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004030 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004031 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004032 } else {
hayeswangb5403272014-10-09 18:00:26 +08004033 mutex_lock(&tp->control);
4034
hayeswang9a4be1b2014-02-18 21:49:07 +08004035 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004036
4037 mutex_unlock(&tp->control);
4038
hayeswang9a4be1b2014-02-18 21:49:07 +08004039 usb_autopm_put_interface(tp->intf);
4040 }
hayeswangac718b62013-05-02 16:01:25 +00004041
hayeswang7e9da482014-02-18 21:49:05 +08004042 free_all_mem(tp);
4043
hayeswangac718b62013-05-02 16:01:25 +00004044 return res;
4045}
4046
hayeswang4f1d4d52014-03-11 16:24:19 +08004047static void rtl_tally_reset(struct r8152 *tp)
4048{
4049 u32 ocp_data;
4050
4051 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4052 ocp_data |= TALLY_RESET;
4053 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4054}
4055
hayeswangac718b62013-05-02 16:01:25 +00004056static void r8152b_init(struct r8152 *tp)
4057{
hayeswangebc2ec482013-08-14 20:54:38 +08004058 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004059 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004060
hayeswang68714382014-04-11 17:54:31 +08004061 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4062 return;
4063
hayeswang2dd436d2016-09-20 16:22:06 +08004064 data = r8152_mdio_read(tp, MII_BMCR);
4065 if (data & BMCR_PDOWN) {
4066 data &= ~BMCR_PDOWN;
4067 r8152_mdio_write(tp, MII_BMCR, data);
4068 }
4069
hayeswangcda9fb02016-01-07 17:51:12 +08004070 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004071
hayeswangac718b62013-05-02 16:01:25 +00004072 if (tp->version == RTL_VER_01) {
4073 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4074 ocp_data &= ~LED_MODE_MASK;
4075 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4076 }
4077
hayeswang00a5e362014-02-18 21:48:59 +08004078 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004079
hayeswangac718b62013-05-02 16:01:25 +00004080 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4081 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4082 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4083 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4084 ocp_data &= ~MCU_CLK_RATIO_MASK;
4085 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4086 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4087 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4088 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4089 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4090
hayeswang4f1d4d52014-03-11 16:24:19 +08004091 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004092
hayeswangebc2ec482013-08-14 20:54:38 +08004093 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004094 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004095 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004096 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4097}
4098
hayeswang43779f82014-01-02 11:25:10 +08004099static void r8153_init(struct r8152 *tp)
4100{
4101 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004102 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004103 int i;
4104
hayeswang68714382014-04-11 17:54:31 +08004105 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4106 return;
4107
hayeswangb9702722014-02-18 21:49:00 +08004108 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004109
4110 for (i = 0; i < 500; i++) {
4111 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4112 AUTOLOAD_DONE)
4113 break;
4114 msleep(20);
4115 }
4116
hayeswangc564b872017-06-09 17:11:38 +08004117 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004118
hayeswang2dd436d2016-09-20 16:22:06 +08004119 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4120 tp->version == RTL_VER_05)
4121 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4122
4123 data = r8152_mdio_read(tp, MII_BMCR);
4124 if (data & BMCR_PDOWN) {
4125 data &= ~BMCR_PDOWN;
4126 r8152_mdio_write(tp, MII_BMCR, data);
4127 }
4128
hayeswangc564b872017-06-09 17:11:38 +08004129 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004130
hayeswangb9702722014-02-18 21:49:00 +08004131 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004132
hayeswang65bab842015-02-12 16:20:46 +08004133 if (tp->version == RTL_VER_04) {
4134 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4135 ocp_data &= ~pwd_dn_scale_mask;
4136 ocp_data |= pwd_dn_scale(96);
4137 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4138
4139 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4140 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4141 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4142 } else if (tp->version == RTL_VER_05) {
4143 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4144 ocp_data &= ~ECM_ALDPS;
4145 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4146
4147 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4148 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4149 ocp_data &= ~DYNAMIC_BURST;
4150 else
4151 ocp_data |= DYNAMIC_BURST;
4152 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004153 } else if (tp->version == RTL_VER_06) {
4154 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4155 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4156 ocp_data &= ~DYNAMIC_BURST;
4157 else
4158 ocp_data |= DYNAMIC_BURST;
4159 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004160 }
4161
4162 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4163 ocp_data |= EP4_FULL_FC;
4164 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4165
hayeswang43779f82014-01-02 11:25:10 +08004166 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4167 ocp_data &= ~TIMER11_EN;
4168 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4169
hayeswang43779f82014-01-02 11:25:10 +08004170 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4171 ocp_data &= ~LED_MODE_MASK;
4172 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4173
hayeswang65bab842015-02-12 16:20:46 +08004174 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004175 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004176 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004177 else
4178 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004179 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4180
4181 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4182 ocp_data &= ~SEN_VAL_MASK;
4183 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4184 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4185
hayeswang65bab842015-02-12 16:20:46 +08004186 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4187
hayeswangb9702722014-02-18 21:49:00 +08004188 r8153_power_cut_en(tp, false);
4189 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004190 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004191 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004192
hayeswange31f6362017-06-09 17:11:41 +08004193 /* rx aggregation */
4194 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4195 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004196 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4197 ocp_data |= RX_AGG_DISABLE;
4198
hayeswange31f6362017-06-09 17:11:41 +08004199 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004200
hayeswang4f1d4d52014-03-11 16:24:19 +08004201 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004202
4203 switch (tp->udev->speed) {
4204 case USB_SPEED_SUPER:
4205 case USB_SPEED_SUPER_PLUS:
4206 tp->coalesce = COALESCE_SUPER;
4207 break;
4208 case USB_SPEED_HIGH:
4209 tp->coalesce = COALESCE_HIGH;
4210 break;
4211 default:
4212 tp->coalesce = COALESCE_SLOW;
4213 break;
4214 }
hayeswang43779f82014-01-02 11:25:10 +08004215}
4216
hayeswang65b82d62017-06-15 14:44:03 +08004217static void r8153b_init(struct r8152 *tp)
4218{
4219 u32 ocp_data;
4220 u16 data;
4221 int i;
4222
4223 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4224 return;
4225
4226 r8153b_u1u2en(tp, false);
4227
4228 for (i = 0; i < 500; i++) {
4229 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4230 AUTOLOAD_DONE)
4231 break;
4232 msleep(20);
4233 }
4234
4235 data = r8153_phy_status(tp, 0);
4236
4237 data = r8152_mdio_read(tp, MII_BMCR);
4238 if (data & BMCR_PDOWN) {
4239 data &= ~BMCR_PDOWN;
4240 r8152_mdio_write(tp, MII_BMCR, data);
4241 }
4242
4243 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4244
4245 r8153_u2p3en(tp, false);
4246
4247 /* MSC timer = 0xfff * 8ms = 32760 ms */
4248 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4249
4250 /* U1/U2/L1 idle timer. 500 us */
4251 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4252
4253 r8153b_power_cut_en(tp, false);
4254 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08004255 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004256 rtl_runtime_suspend_enable(tp, false);
4257 r8153b_u1u2en(tp, true);
4258 usb_enable_lpm(tp->udev);
4259
4260 /* MAC clock speed down */
4261 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4262 ocp_data |= MAC_CLK_SPDWN_EN;
4263 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4264
4265 set_bit(GREEN_ETHERNET, &tp->flags);
4266
4267 /* rx aggregation */
4268 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4269 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4270 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4271
4272 rtl_tally_reset(tp);
4273
4274 tp->coalesce = 15000; /* 15 us */
4275}
4276
hayeswange5011392015-07-29 20:39:08 +08004277static int rtl8152_pre_reset(struct usb_interface *intf)
4278{
4279 struct r8152 *tp = usb_get_intfdata(intf);
4280 struct net_device *netdev;
4281
4282 if (!tp)
4283 return 0;
4284
4285 netdev = tp->netdev;
4286 if (!netif_running(netdev))
4287 return 0;
4288
hayeswangde9bf292017-01-26 09:38:32 +08004289 netif_stop_queue(netdev);
hayeswange5011392015-07-29 20:39:08 +08004290 napi_disable(&tp->napi);
4291 clear_bit(WORK_ENABLE, &tp->flags);
4292 usb_kill_urb(tp->intr_urb);
4293 cancel_delayed_work_sync(&tp->schedule);
4294 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004295 mutex_lock(&tp->control);
4296 tp->rtl_ops.disable(tp);
4297 mutex_unlock(&tp->control);
4298 }
4299
4300 return 0;
4301}
4302
4303static int rtl8152_post_reset(struct usb_interface *intf)
4304{
4305 struct r8152 *tp = usb_get_intfdata(intf);
4306 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004307 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004308
4309 if (!tp)
4310 return 0;
4311
Mario Limonciello25766272019-04-04 13:46:53 -05004312 /* reset the MAC adddress in case of policy change */
4313 if (determine_ethernet_addr(tp, &sa) >= 0) {
4314 rtnl_lock();
4315 dev_set_mac_address (tp->netdev, &sa, NULL);
4316 rtnl_unlock();
4317 }
4318
hayeswange5011392015-07-29 20:39:08 +08004319 netdev = tp->netdev;
4320 if (!netif_running(netdev))
4321 return 0;
4322
4323 set_bit(WORK_ENABLE, &tp->flags);
4324 if (netif_carrier_ok(netdev)) {
4325 mutex_lock(&tp->control);
4326 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004327 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004328 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004329 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004330 }
4331
4332 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004333 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004334 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004335
hayeswang7489bda2017-01-26 09:38:34 +08004336 if (!list_empty(&tp->rx_done))
4337 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004338
4339 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004340}
4341
hayeswang2dd49e02015-09-07 11:57:44 +08004342static bool delay_autosuspend(struct r8152 *tp)
4343{
4344 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4345 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4346
4347 /* This means a linking change occurs and the driver doesn't detect it,
4348 * yet. If the driver has disabled tx/rx and hw is linking on, the
4349 * device wouldn't wake up by receiving any packet.
4350 */
4351 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4352 return true;
4353
4354 /* If the linking down is occurred by nway, the device may miss the
4355 * linking change event. And it wouldn't wake when linking on.
4356 */
4357 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4358 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004359 else if (!skb_queue_empty(&tp->tx_queue))
4360 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004361 else
4362 return false;
4363}
4364
hayeswang21cbd0e2017-06-13 15:14:39 +08004365static int rtl8152_runtime_resume(struct r8152 *tp)
4366{
4367 struct net_device *netdev = tp->netdev;
4368
4369 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4370 struct napi_struct *napi = &tp->napi;
4371
4372 tp->rtl_ops.autosuspend_en(tp, false);
4373 napi_disable(napi);
4374 set_bit(WORK_ENABLE, &tp->flags);
4375
4376 if (netif_carrier_ok(netdev)) {
4377 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4378 rtl_start_rx(tp);
4379 } else {
4380 netif_carrier_off(netdev);
4381 tp->rtl_ops.disable(tp);
4382 netif_info(tp, link, netdev, "linking down\n");
4383 }
4384 }
4385
4386 napi_enable(napi);
4387 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4388 smp_mb__after_atomic();
4389
4390 if (!list_empty(&tp->rx_done))
4391 napi_schedule(&tp->napi);
4392
4393 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4394 } else {
4395 if (netdev->flags & IFF_UP)
4396 tp->rtl_ops.autosuspend_en(tp, false);
4397
4398 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4399 }
4400
4401 return 0;
4402}
4403
4404static int rtl8152_system_resume(struct r8152 *tp)
4405{
4406 struct net_device *netdev = tp->netdev;
4407
4408 netif_device_attach(netdev);
4409
4410 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4411 tp->rtl_ops.up(tp);
4412 netif_carrier_off(netdev);
4413 set_bit(WORK_ENABLE, &tp->flags);
4414 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4415 }
4416
4417 return 0;
4418}
4419
hayeswanga9c54ad2017-01-25 13:41:45 +08004420static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004421{
hayeswang6cc69f22014-10-17 16:55:08 +08004422 struct net_device *netdev = tp->netdev;
4423 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004424
hayeswang26afec32017-01-26 09:38:31 +08004425 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4426 smp_mb__after_atomic();
4427
hayeswang8fb28062017-01-10 17:04:06 +08004428 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004429 u32 rcr = 0;
4430
hayeswang75dc6922017-01-10 17:04:07 +08004431 if (netif_carrier_ok(netdev)) {
4432 u32 ocp_data;
4433
4434 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4435 ocp_data = rcr & ~RCR_ACPT_ALL;
4436 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4437 rxdy_gated_en(tp, true);
4438 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4439 PLA_OOB_CTRL);
4440 if (!(ocp_data & RXFIFO_EMPTY)) {
4441 rxdy_gated_en(tp, false);
4442 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004443 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4444 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004445 ret = -EBUSY;
4446 goto out1;
4447 }
4448 }
4449
hayeswang8fb28062017-01-10 17:04:06 +08004450 clear_bit(WORK_ENABLE, &tp->flags);
4451 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004452
hayeswang8fb28062017-01-10 17:04:06 +08004453 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004454
4455 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004456 struct napi_struct *napi = &tp->napi;
4457
4458 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004459 rtl_stop_rx(tp);
4460 rxdy_gated_en(tp, false);
4461 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004462 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004463 }
hayeswangbd882982017-06-13 15:14:40 +08004464
4465 if (delay_autosuspend(tp)) {
4466 rtl8152_runtime_resume(tp);
4467 ret = -EBUSY;
4468 }
hayeswang6cc69f22014-10-17 16:55:08 +08004469 }
4470
hayeswang8fb28062017-01-10 17:04:06 +08004471out1:
4472 return ret;
4473}
4474
4475static int rtl8152_system_suspend(struct r8152 *tp)
4476{
4477 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004478
4479 netif_device_detach(netdev);
4480
hayeswange3bd1a82014-10-29 11:12:17 +08004481 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004482 struct napi_struct *napi = &tp->napi;
4483
hayeswangac718b62013-05-02 16:01:25 +00004484 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004485 usb_kill_urb(tp->intr_urb);
hayeswangce594e92017-03-16 14:32:22 +08004486 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004487 cancel_delayed_work_sync(&tp->schedule);
4488 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004489 napi_enable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004490 }
hayeswang8fb28062017-01-10 17:04:06 +08004491
zhong jiangf7419172018-08-09 09:39:13 +08004492 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004493}
4494
4495static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4496{
4497 struct r8152 *tp = usb_get_intfdata(intf);
4498 int ret;
4499
4500 mutex_lock(&tp->control);
4501
4502 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004503 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004504 else
4505 ret = rtl8152_system_suspend(tp);
4506
hayeswangb5403272014-10-09 18:00:26 +08004507 mutex_unlock(&tp->control);
4508
hayeswang6cc69f22014-10-17 16:55:08 +08004509 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004510}
4511
4512static int rtl8152_resume(struct usb_interface *intf)
4513{
4514 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004515 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004516
hayeswangb5403272014-10-09 18:00:26 +08004517 mutex_lock(&tp->control);
4518
hayeswang21cbd0e2017-06-13 15:14:39 +08004519 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4520 ret = rtl8152_runtime_resume(tp);
4521 else
4522 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004523
hayeswangb5403272014-10-09 18:00:26 +08004524 mutex_unlock(&tp->control);
4525
hayeswang21cbd0e2017-06-13 15:14:39 +08004526 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004527}
4528
hayeswang7ec25412016-01-04 14:38:46 +08004529static int rtl8152_reset_resume(struct usb_interface *intf)
4530{
4531 struct r8152 *tp = usb_get_intfdata(intf);
4532
4533 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004534 mutex_lock(&tp->control);
4535 tp->rtl_ops.init(tp);
4536 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4537 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004538 return rtl8152_resume(intf);
4539}
4540
hayeswang21ff2e82014-02-18 21:49:06 +08004541static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4542{
4543 struct r8152 *tp = netdev_priv(dev);
4544
hayeswang9a4be1b2014-02-18 21:49:07 +08004545 if (usb_autopm_get_interface(tp->intf) < 0)
4546 return;
4547
hayeswang7daed8d2015-07-24 13:54:24 +08004548 if (!rtl_can_wakeup(tp)) {
4549 wol->supported = 0;
4550 wol->wolopts = 0;
4551 } else {
4552 mutex_lock(&tp->control);
4553 wol->supported = WAKE_ANY;
4554 wol->wolopts = __rtl_get_wol(tp);
4555 mutex_unlock(&tp->control);
4556 }
hayeswangb5403272014-10-09 18:00:26 +08004557
hayeswang9a4be1b2014-02-18 21:49:07 +08004558 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004559}
4560
4561static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4562{
4563 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004564 int ret;
4565
hayeswang7daed8d2015-07-24 13:54:24 +08004566 if (!rtl_can_wakeup(tp))
4567 return -EOPNOTSUPP;
4568
Florian Fainellif2750df2018-09-28 16:18:54 -07004569 if (wol->wolopts & ~WAKE_ANY)
4570 return -EINVAL;
4571
hayeswang9a4be1b2014-02-18 21:49:07 +08004572 ret = usb_autopm_get_interface(tp->intf);
4573 if (ret < 0)
4574 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004575
hayeswangb5403272014-10-09 18:00:26 +08004576 mutex_lock(&tp->control);
4577
hayeswang21ff2e82014-02-18 21:49:06 +08004578 __rtl_set_wol(tp, wol->wolopts);
4579 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4580
hayeswangb5403272014-10-09 18:00:26 +08004581 mutex_unlock(&tp->control);
4582
hayeswang9a4be1b2014-02-18 21:49:07 +08004583 usb_autopm_put_interface(tp->intf);
4584
4585out_set_wol:
4586 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004587}
4588
hayeswanga5ec27c2014-02-18 21:49:11 +08004589static u32 rtl8152_get_msglevel(struct net_device *dev)
4590{
4591 struct r8152 *tp = netdev_priv(dev);
4592
4593 return tp->msg_enable;
4594}
4595
4596static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4597{
4598 struct r8152 *tp = netdev_priv(dev);
4599
4600 tp->msg_enable = value;
4601}
4602
hayeswangac718b62013-05-02 16:01:25 +00004603static void rtl8152_get_drvinfo(struct net_device *netdev,
4604 struct ethtool_drvinfo *info)
4605{
4606 struct r8152 *tp = netdev_priv(netdev);
4607
hayeswangb0b46c72014-08-26 10:08:23 +08004608 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4609 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004610 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4611}
4612
4613static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004614int rtl8152_get_link_ksettings(struct net_device *netdev,
4615 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004616{
4617 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004618 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004619
4620 if (!tp->mii.mdio_read)
4621 return -EOPNOTSUPP;
4622
hayeswang8d4a4d72014-10-09 18:00:25 +08004623 ret = usb_autopm_get_interface(tp->intf);
4624 if (ret < 0)
4625 goto out;
4626
hayeswangb5403272014-10-09 18:00:26 +08004627 mutex_lock(&tp->control);
4628
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004629 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004630
hayeswangb5403272014-10-09 18:00:26 +08004631 mutex_unlock(&tp->control);
4632
hayeswang8d4a4d72014-10-09 18:00:25 +08004633 usb_autopm_put_interface(tp->intf);
4634
4635out:
4636 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004637}
4638
Philippe Reynes06144dc2017-03-12 22:41:58 +01004639static int rtl8152_set_link_ksettings(struct net_device *dev,
4640 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004641{
4642 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004643 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004644
hayeswang9a4be1b2014-02-18 21:49:07 +08004645 ret = usb_autopm_get_interface(tp->intf);
4646 if (ret < 0)
4647 goto out;
4648
hayeswangb5403272014-10-09 18:00:26 +08004649 mutex_lock(&tp->control);
4650
Philippe Reynes06144dc2017-03-12 22:41:58 +01004651 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4652 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004653 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004654 tp->autoneg = cmd->base.autoneg;
4655 tp->speed = cmd->base.speed;
4656 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004657 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004658
hayeswangb5403272014-10-09 18:00:26 +08004659 mutex_unlock(&tp->control);
4660
hayeswang9a4be1b2014-02-18 21:49:07 +08004661 usb_autopm_put_interface(tp->intf);
4662
4663out:
4664 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004665}
4666
hayeswang4f1d4d52014-03-11 16:24:19 +08004667static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4668 "tx_packets",
4669 "rx_packets",
4670 "tx_errors",
4671 "rx_errors",
4672 "rx_missed",
4673 "align_errors",
4674 "tx_single_collisions",
4675 "tx_multi_collisions",
4676 "rx_unicast",
4677 "rx_broadcast",
4678 "rx_multicast",
4679 "tx_aborted",
4680 "tx_underrun",
4681};
4682
4683static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4684{
4685 switch (sset) {
4686 case ETH_SS_STATS:
4687 return ARRAY_SIZE(rtl8152_gstrings);
4688 default:
4689 return -EOPNOTSUPP;
4690 }
4691}
4692
4693static void rtl8152_get_ethtool_stats(struct net_device *dev,
4694 struct ethtool_stats *stats, u64 *data)
4695{
4696 struct r8152 *tp = netdev_priv(dev);
4697 struct tally_counter tally;
4698
hayeswang0b030242014-07-08 14:49:28 +08004699 if (usb_autopm_get_interface(tp->intf) < 0)
4700 return;
4701
hayeswang4f1d4d52014-03-11 16:24:19 +08004702 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4703
hayeswang0b030242014-07-08 14:49:28 +08004704 usb_autopm_put_interface(tp->intf);
4705
hayeswang4f1d4d52014-03-11 16:24:19 +08004706 data[0] = le64_to_cpu(tally.tx_packets);
4707 data[1] = le64_to_cpu(tally.rx_packets);
4708 data[2] = le64_to_cpu(tally.tx_errors);
4709 data[3] = le32_to_cpu(tally.rx_errors);
4710 data[4] = le16_to_cpu(tally.rx_missed);
4711 data[5] = le16_to_cpu(tally.align_errors);
4712 data[6] = le32_to_cpu(tally.tx_one_collision);
4713 data[7] = le32_to_cpu(tally.tx_multi_collision);
4714 data[8] = le64_to_cpu(tally.rx_unicast);
4715 data[9] = le64_to_cpu(tally.rx_broadcast);
4716 data[10] = le32_to_cpu(tally.rx_multicast);
4717 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004718 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004719}
4720
4721static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4722{
4723 switch (stringset) {
4724 case ETH_SS_STATS:
4725 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4726 break;
4727 }
4728}
4729
hayeswangdf35d282014-09-25 20:54:02 +08004730static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4731{
4732 u32 ocp_data, lp, adv, supported = 0;
4733 u16 val;
4734
4735 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4736 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4737
4738 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4739 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4740
4741 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4742 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4743
4744 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4745 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4746
4747 eee->eee_enabled = !!ocp_data;
4748 eee->eee_active = !!(supported & adv & lp);
4749 eee->supported = supported;
4750 eee->advertised = adv;
4751 eee->lp_advertised = lp;
4752
4753 return 0;
4754}
4755
4756static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4757{
4758 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4759
4760 r8152_eee_en(tp, eee->eee_enabled);
4761
4762 if (!eee->eee_enabled)
4763 val = 0;
4764
4765 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4766
4767 return 0;
4768}
4769
4770static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4771{
4772 u32 ocp_data, lp, adv, supported = 0;
4773 u16 val;
4774
4775 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4776 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4777
4778 val = ocp_reg_read(tp, OCP_EEE_ADV);
4779 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4780
4781 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4782 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4783
4784 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4785 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4786
4787 eee->eee_enabled = !!ocp_data;
4788 eee->eee_active = !!(supported & adv & lp);
4789 eee->supported = supported;
4790 eee->advertised = adv;
4791 eee->lp_advertised = lp;
4792
4793 return 0;
4794}
4795
4796static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4797{
4798 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4799
4800 r8153_eee_en(tp, eee->eee_enabled);
4801
4802 if (!eee->eee_enabled)
4803 val = 0;
4804
4805 ocp_reg_write(tp, OCP_EEE_ADV, val);
4806
4807 return 0;
4808}
4809
hayeswang65b82d62017-06-15 14:44:03 +08004810static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4811{
4812 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4813
4814 r8153b_eee_en(tp, eee->eee_enabled);
4815
4816 if (!eee->eee_enabled)
4817 val = 0;
4818
4819 ocp_reg_write(tp, OCP_EEE_ADV, val);
4820
4821 return 0;
4822}
4823
hayeswangdf35d282014-09-25 20:54:02 +08004824static int
4825rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4826{
4827 struct r8152 *tp = netdev_priv(net);
4828 int ret;
4829
4830 ret = usb_autopm_get_interface(tp->intf);
4831 if (ret < 0)
4832 goto out;
4833
hayeswangb5403272014-10-09 18:00:26 +08004834 mutex_lock(&tp->control);
4835
hayeswangdf35d282014-09-25 20:54:02 +08004836 ret = tp->rtl_ops.eee_get(tp, edata);
4837
hayeswangb5403272014-10-09 18:00:26 +08004838 mutex_unlock(&tp->control);
4839
hayeswangdf35d282014-09-25 20:54:02 +08004840 usb_autopm_put_interface(tp->intf);
4841
4842out:
4843 return ret;
4844}
4845
4846static int
4847rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
4848{
4849 struct r8152 *tp = netdev_priv(net);
4850 int ret;
4851
4852 ret = usb_autopm_get_interface(tp->intf);
4853 if (ret < 0)
4854 goto out;
4855
hayeswangb5403272014-10-09 18:00:26 +08004856 mutex_lock(&tp->control);
4857
hayeswangdf35d282014-09-25 20:54:02 +08004858 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08004859 if (!ret)
4860 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08004861
hayeswangb5403272014-10-09 18:00:26 +08004862 mutex_unlock(&tp->control);
4863
hayeswangdf35d282014-09-25 20:54:02 +08004864 usb_autopm_put_interface(tp->intf);
4865
4866out:
4867 return ret;
4868}
4869
hayeswang8884f502014-10-28 14:05:52 +08004870static int rtl8152_nway_reset(struct net_device *dev)
4871{
4872 struct r8152 *tp = netdev_priv(dev);
4873 int ret;
4874
4875 ret = usb_autopm_get_interface(tp->intf);
4876 if (ret < 0)
4877 goto out;
4878
4879 mutex_lock(&tp->control);
4880
4881 ret = mii_nway_restart(&tp->mii);
4882
4883 mutex_unlock(&tp->control);
4884
4885 usb_autopm_put_interface(tp->intf);
4886
4887out:
4888 return ret;
4889}
4890
hayeswangefb3dd82015-02-12 14:33:48 +08004891static int rtl8152_get_coalesce(struct net_device *netdev,
4892 struct ethtool_coalesce *coalesce)
4893{
4894 struct r8152 *tp = netdev_priv(netdev);
4895
4896 switch (tp->version) {
4897 case RTL_VER_01:
4898 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004899 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004900 return -EOPNOTSUPP;
4901 default:
4902 break;
4903 }
4904
4905 coalesce->rx_coalesce_usecs = tp->coalesce;
4906
4907 return 0;
4908}
4909
4910static int rtl8152_set_coalesce(struct net_device *netdev,
4911 struct ethtool_coalesce *coalesce)
4912{
4913 struct r8152 *tp = netdev_priv(netdev);
4914 int ret;
4915
4916 switch (tp->version) {
4917 case RTL_VER_01:
4918 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004919 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004920 return -EOPNOTSUPP;
4921 default:
4922 break;
4923 }
4924
4925 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
4926 return -EINVAL;
4927
4928 ret = usb_autopm_get_interface(tp->intf);
4929 if (ret < 0)
4930 return ret;
4931
4932 mutex_lock(&tp->control);
4933
4934 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
4935 tp->coalesce = coalesce->rx_coalesce_usecs;
4936
Hayes Wang9fae5412019-07-03 15:11:56 +08004937 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
4938 netif_stop_queue(netdev);
4939 napi_disable(&tp->napi);
4940 tp->rtl_ops.disable(tp);
4941 tp->rtl_ops.enable(tp);
4942 rtl_start_rx(tp);
4943 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
4944 _rtl8152_set_rx_mode(netdev);
4945 napi_enable(&tp->napi);
4946 netif_wake_queue(netdev);
4947 }
hayeswangefb3dd82015-02-12 14:33:48 +08004948 }
4949
4950 mutex_unlock(&tp->control);
4951
4952 usb_autopm_put_interface(tp->intf);
4953
4954 return ret;
4955}
4956
Julia Lawall407a4712016-09-01 00:21:22 +02004957static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00004958 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00004959 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08004960 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08004961 .get_msglevel = rtl8152_get_msglevel,
4962 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08004963 .get_wol = rtl8152_get_wol,
4964 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08004965 .get_strings = rtl8152_get_strings,
4966 .get_sset_count = rtl8152_get_sset_count,
4967 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08004968 .get_coalesce = rtl8152_get_coalesce,
4969 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08004970 .get_eee = rtl_ethtool_get_eee,
4971 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01004972 .get_link_ksettings = rtl8152_get_link_ksettings,
4973 .set_link_ksettings = rtl8152_set_link_ksettings,
hayeswangac718b62013-05-02 16:01:25 +00004974};
4975
4976static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
4977{
4978 struct r8152 *tp = netdev_priv(netdev);
4979 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08004980 int res;
4981
hayeswang68714382014-04-11 17:54:31 +08004982 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4983 return -ENODEV;
4984
hayeswang9a4be1b2014-02-18 21:49:07 +08004985 res = usb_autopm_get_interface(tp->intf);
4986 if (res < 0)
4987 goto out;
hayeswangac718b62013-05-02 16:01:25 +00004988
4989 switch (cmd) {
4990 case SIOCGMIIPHY:
4991 data->phy_id = R8152_PHY_ID; /* Internal PHY */
4992 break;
4993
4994 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08004995 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004996 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08004997 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004998 break;
4999
5000 case SIOCSMIIREG:
5001 if (!capable(CAP_NET_ADMIN)) {
5002 res = -EPERM;
5003 break;
5004 }
hayeswangb5403272014-10-09 18:00:26 +08005005 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005006 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08005007 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005008 break;
5009
5010 default:
5011 res = -EOPNOTSUPP;
5012 }
5013
hayeswang9a4be1b2014-02-18 21:49:07 +08005014 usb_autopm_put_interface(tp->intf);
5015
5016out:
hayeswangac718b62013-05-02 16:01:25 +00005017 return res;
5018}
5019
hayeswang69b4b7a2014-07-10 10:58:54 +08005020static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5021{
5022 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08005023 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005024
5025 switch (tp->version) {
5026 case RTL_VER_01:
5027 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005028 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04005029 dev->mtu = new_mtu;
5030 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08005031 default:
5032 break;
5033 }
5034
hayeswang396e2e22015-02-12 14:33:47 +08005035 ret = usb_autopm_get_interface(tp->intf);
5036 if (ret < 0)
5037 return ret;
5038
5039 mutex_lock(&tp->control);
5040
hayeswang69b4b7a2014-07-10 10:58:54 +08005041 dev->mtu = new_mtu;
5042
hayeswang210c4f72017-03-20 16:13:44 +08005043 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005044 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005045
5046 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5047
5048 if (netif_carrier_ok(dev))
5049 r8153_set_rx_early_size(tp);
5050 }
hayeswang396e2e22015-02-12 14:33:47 +08005051
5052 mutex_unlock(&tp->control);
5053
5054 usb_autopm_put_interface(tp->intf);
5055
5056 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005057}
5058
hayeswangac718b62013-05-02 16:01:25 +00005059static const struct net_device_ops rtl8152_netdev_ops = {
5060 .ndo_open = rtl8152_open,
5061 .ndo_stop = rtl8152_close,
5062 .ndo_do_ioctl = rtl8152_ioctl,
5063 .ndo_start_xmit = rtl8152_start_xmit,
5064 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005065 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005066 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5067 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005068 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005069 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005070 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005071};
5072
hayeswange3fe0b12014-01-02 11:22:39 +08005073static void rtl8152_unload(struct r8152 *tp)
5074{
hayeswang68714382014-04-11 17:54:31 +08005075 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5076 return;
5077
hayeswang00a5e362014-02-18 21:48:59 +08005078 if (tp->version != RTL_VER_01)
5079 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005080}
5081
hayeswang43779f82014-01-02 11:25:10 +08005082static void rtl8153_unload(struct r8152 *tp)
5083{
hayeswang68714382014-04-11 17:54:31 +08005084 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5085 return;
5086
hayeswang49be1722014-10-01 13:25:11 +08005087 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005088}
5089
hayeswang65b82d62017-06-15 14:44:03 +08005090static void rtl8153b_unload(struct r8152 *tp)
5091{
5092 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5093 return;
5094
5095 r8153b_power_cut_en(tp, false);
5096}
5097
hayeswang55b65472014-11-06 12:47:39 +08005098static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005099{
5100 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005101 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005102
hayeswang55b65472014-11-06 12:47:39 +08005103 switch (tp->version) {
5104 case RTL_VER_01:
5105 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005106 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005107 ops->init = r8152b_init;
5108 ops->enable = rtl8152_enable;
5109 ops->disable = rtl8152_disable;
5110 ops->up = rtl8152_up;
5111 ops->down = rtl8152_down;
5112 ops->unload = rtl8152_unload;
5113 ops->eee_get = r8152_get_eee;
5114 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005115 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005116 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005117 ops->autosuspend_en = rtl_runtime_suspend_enable;
hayeswang43779f82014-01-02 11:25:10 +08005118 break;
5119
hayeswang55b65472014-11-06 12:47:39 +08005120 case RTL_VER_03:
5121 case RTL_VER_04:
5122 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005123 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005124 ops->init = r8153_init;
5125 ops->enable = rtl8153_enable;
5126 ops->disable = rtl8153_disable;
5127 ops->up = rtl8153_up;
5128 ops->down = rtl8153_down;
5129 ops->unload = rtl8153_unload;
5130 ops->eee_get = r8153_get_eee;
5131 ops->eee_set = r8153_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005132 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005133 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005134 ops->autosuspend_en = rtl8153_runtime_enable;
hayeswangc81229c2014-01-02 11:22:42 +08005135 break;
5136
hayeswang65b82d62017-06-15 14:44:03 +08005137 case RTL_VER_08:
5138 case RTL_VER_09:
5139 ops->init = r8153b_init;
5140 ops->enable = rtl8153_enable;
5141 ops->disable = rtl8153b_disable;
5142 ops->up = rtl8153b_up;
5143 ops->down = rtl8153b_down;
5144 ops->unload = rtl8153b_unload;
5145 ops->eee_get = r8153_get_eee;
5146 ops->eee_set = r8153b_set_eee;
5147 ops->in_nway = rtl8153_in_nway;
5148 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5149 ops->autosuspend_en = rtl8153b_runtime_enable;
5150 break;
5151
hayeswangc81229c2014-01-02 11:22:42 +08005152 default:
hayeswang55b65472014-11-06 12:47:39 +08005153 ret = -ENODEV;
5154 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005155 break;
5156 }
5157
5158 return ret;
5159}
5160
hayeswang33928ee2017-03-17 11:20:13 +08005161static u8 rtl_get_version(struct usb_interface *intf)
5162{
5163 struct usb_device *udev = interface_to_usbdev(intf);
5164 u32 ocp_data = 0;
5165 __le32 *tmp;
5166 u8 version;
5167 int ret;
5168
5169 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5170 if (!tmp)
5171 return 0;
5172
5173 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5174 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5175 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5176 if (ret > 0)
5177 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5178
5179 kfree(tmp);
5180
5181 switch (ocp_data) {
5182 case 0x4c00:
5183 version = RTL_VER_01;
5184 break;
5185 case 0x4c10:
5186 version = RTL_VER_02;
5187 break;
5188 case 0x5c00:
5189 version = RTL_VER_03;
5190 break;
5191 case 0x5c10:
5192 version = RTL_VER_04;
5193 break;
5194 case 0x5c20:
5195 version = RTL_VER_05;
5196 break;
5197 case 0x5c30:
5198 version = RTL_VER_06;
5199 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005200 case 0x4800:
5201 version = RTL_VER_07;
5202 break;
hayeswang65b82d62017-06-15 14:44:03 +08005203 case 0x6000:
5204 version = RTL_VER_08;
5205 break;
5206 case 0x6010:
5207 version = RTL_VER_09;
5208 break;
hayeswang33928ee2017-03-17 11:20:13 +08005209 default:
5210 version = RTL_VER_UNKNOWN;
5211 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5212 break;
5213 }
5214
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005215 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5216
hayeswang33928ee2017-03-17 11:20:13 +08005217 return version;
5218}
5219
hayeswangac718b62013-05-02 16:01:25 +00005220static int rtl8152_probe(struct usb_interface *intf,
5221 const struct usb_device_id *id)
5222{
5223 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005224 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005225 struct r8152 *tp;
5226 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005227 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005228
hayeswang33928ee2017-03-17 11:20:13 +08005229 if (version == RTL_VER_UNKNOWN)
5230 return -ENODEV;
5231
hayeswang10c32712014-03-04 20:47:48 +08005232 if (udev->actconfig->desc.bConfigurationValue != 1) {
5233 usb_driver_set_configuration(udev, 1);
5234 return -ENODEV;
5235 }
5236
5237 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005238 netdev = alloc_etherdev(sizeof(struct r8152));
5239 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005240 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005241 return -ENOMEM;
5242 }
5243
hayeswangebc2ec482013-08-14 20:54:38 +08005244 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005245 tp = netdev_priv(netdev);
5246 tp->msg_enable = 0x7FFF;
5247
hayeswange3ad4122014-01-06 17:08:42 +08005248 tp->udev = udev;
5249 tp->netdev = netdev;
5250 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005251 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005252
hayeswang33928ee2017-03-17 11:20:13 +08005253 switch (version) {
5254 case RTL_VER_01:
5255 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005256 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005257 tp->mii.supports_gmii = 0;
5258 break;
5259 default:
5260 tp->mii.supports_gmii = 1;
5261 break;
5262 }
5263
hayeswang55b65472014-11-06 12:47:39 +08005264 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005265 if (ret)
5266 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005267
hayeswangb5403272014-10-09 18:00:26 +08005268 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005269 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005270 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
hayeswangac718b62013-05-02 16:01:25 +00005271
hayeswangac718b62013-05-02 16:01:25 +00005272 netdev->netdev_ops = &rtl8152_netdev_ops;
5273 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005274
hayeswang60c89072014-03-07 11:04:39 +08005275 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005276 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005277 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5278 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005279 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005280 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005281 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005282 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005283 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5284 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5285 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005286
hayeswang19c0f402017-01-11 16:25:34 +08005287 if (tp->version == RTL_VER_01) {
5288 netdev->features &= ~NETIF_F_RXCSUM;
5289 netdev->hw_features &= ~NETIF_F_RXCSUM;
5290 }
5291
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005292 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5293 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005294 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5295 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5296 }
5297
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005298 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005299 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005300
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005301 /* MTU range: 68 - 1500 or 9194 */
5302 netdev->min_mtu = ETH_MIN_MTU;
5303 switch (tp->version) {
5304 case RTL_VER_01:
5305 case RTL_VER_02:
5306 netdev->max_mtu = ETH_DATA_LEN;
5307 break;
5308 default:
5309 netdev->max_mtu = RTL8153_MAX_MTU;
5310 break;
5311 }
5312
hayeswangac718b62013-05-02 16:01:25 +00005313 tp->mii.dev = netdev;
5314 tp->mii.mdio_read = read_mii_word;
5315 tp->mii.mdio_write = write_mii_word;
5316 tp->mii.phy_id_mask = 0x3f;
5317 tp->mii.reg_num_mask = 0x1f;
5318 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005319
hayeswangaa7e26b2016-06-13 17:49:38 +08005320 tp->autoneg = AUTONEG_ENABLE;
5321 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5322 tp->duplex = DUPLEX_FULL;
5323
hayeswang9a4be1b2014-02-18 21:49:07 +08005324 intf->needs_remote_wakeup = 1;
5325
hayeswangc81229c2014-01-02 11:22:42 +08005326 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005327 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005328 set_ethernet_addr(tp);
5329
hayeswangac718b62013-05-02 16:01:25 +00005330 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005331 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005332
hayeswangebc2ec482013-08-14 20:54:38 +08005333 ret = register_netdev(netdev);
5334 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005335 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005336 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005337 }
5338
hayeswang7daed8d2015-07-24 13:54:24 +08005339 if (!rtl_can_wakeup(tp))
5340 __rtl_set_wol(tp, 0);
5341
hayeswang21ff2e82014-02-18 21:49:06 +08005342 tp->saved_wolopts = __rtl_get_wol(tp);
5343 if (tp->saved_wolopts)
5344 device_set_wakeup_enable(&udev->dev, true);
5345 else
5346 device_set_wakeup_enable(&udev->dev, false);
5347
Hayes Wang4a8deae2014-01-07 11:18:22 +08005348 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005349
5350 return 0;
5351
hayeswangac718b62013-05-02 16:01:25 +00005352out1:
hayeswangd823ab62015-01-12 12:06:23 +08005353 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08005354 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005355out:
5356 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005357 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005358}
5359
hayeswangac718b62013-05-02 16:01:25 +00005360static void rtl8152_disconnect(struct usb_interface *intf)
5361{
5362 struct r8152 *tp = usb_get_intfdata(intf);
5363
5364 usb_set_intfdata(intf, NULL);
5365 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08005366 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08005367
hayeswangd823ab62015-01-12 12:06:23 +08005368 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005369 unregister_netdev(tp->netdev);
hayeswanga028a9e2016-06-13 17:49:36 +08005370 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005371 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005372 free_netdev(tp->netdev);
5373 }
5374}
5375
hayeswangd9a28c52014-12-04 10:43:11 +08005376#define REALTEK_USB_DEVICE(vend, prod) \
5377 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5378 USB_DEVICE_ID_MATCH_INT_CLASS, \
5379 .idVendor = (vend), \
5380 .idProduct = (prod), \
5381 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5382}, \
5383{ \
5384 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5385 USB_DEVICE_ID_MATCH_DEVICE, \
5386 .idVendor = (vend), \
5387 .idProduct = (prod), \
5388 .bInterfaceClass = USB_CLASS_COMM, \
5389 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5390 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5391
hayeswangac718b62013-05-02 16:01:25 +00005392/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305393static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005394 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005395 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5396 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005397 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5398 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005399 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005400 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005401 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5402 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5403 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5404 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5405 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005406 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005407 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005408 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005409 {}
5410};
5411
5412MODULE_DEVICE_TABLE(usb, rtl8152_table);
5413
5414static struct usb_driver rtl8152_driver = {
5415 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005416 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005417 .probe = rtl8152_probe,
5418 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005419 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005420 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005421 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005422 .pre_reset = rtl8152_pre_reset,
5423 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005424 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005425 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005426};
5427
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005428module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005429
5430MODULE_AUTHOR(DRIVER_AUTHOR);
5431MODULE_DESCRIPTION(DRIVER_DESC);
5432MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005433MODULE_VERSION(DRIVER_VERSION);