blob: 504db2348a3e5479a4e82fa711b60484eb43ffa2 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
hayeswangac718b62013-05-02 16:01:25 +00002/*
hayeswangc7de7de2014-01-15 10:42:16 +08003 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00004 */
5
hayeswangac718b62013-05-02 16:01:25 +00006#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +00009#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/crc32.h>
15#include <linux/if_vlan.h>
16#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080017#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080018#include <linux/ip.h>
19#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080020#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080021#include <uapi/linux/mdio.h>
22#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080023#include <linux/usb/cdc.h>
hayeswang5ee3c602016-01-07 17:12:17 +080024#include <linux/suspend.h>
Hayes Wang252df8b2019-08-13 11:42:06 +080025#include <linux/atomic.h>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050026#include <linux/acpi.h>
Hayes Wang9370f2d2019-10-16 11:02:42 +080027#include <linux/firmware.h>
28#include <crypto/hash.h>
hayeswangac718b62013-05-02 16:01:25 +000029
hayeswangd0942472015-09-07 11:57:43 +080030/* Information for net-next */
Hayes Wang9370f2d2019-10-16 11:02:42 +080031#define NETNEXT_VERSION "11"
hayeswangd0942472015-09-07 11:57:43 +080032
33/* Information for net */
Hayes Wangffa9fec2019-07-04 17:36:32 +080034#define NET_VERSION "10"
hayeswangd0942472015-09-07 11:57:43 +080035
36#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000037#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080038#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000039#define MODULENAME "r8152"
40
41#define R8152_PHY_ID 32
42
43#define PLA_IDR 0xc000
44#define PLA_RCR 0xc010
45#define PLA_RMS 0xc016
46#define PLA_RXFIFO_CTRL0 0xc0a0
47#define PLA_RXFIFO_CTRL1 0xc0a4
48#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080049#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000050#define PLA_FMC 0xc0b4
51#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080052#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080053#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000054#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080055#define PLA_BACKUP 0xd000
Kevin Lo59c0b472019-08-01 11:29:38 +080056#define PLA_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080057#define PLA_TEREDO_TIMER 0xd2cc
58#define PLA_REALWOW_TIMER 0xd2e8
Hayes Wang9370f2d2019-10-16 11:02:42 +080059#define PLA_UPHY_TIMER 0xd388
Hayes Wang13e04fbf2019-07-01 15:53:19 +080060#define PLA_SUSPEND_FLAG 0xd38a
61#define PLA_INDICATE_FALG 0xd38c
Hayes Wang9370f2d2019-10-16 11:02:42 +080062#define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */
63#define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */
Hayes Wang13e04fbf2019-07-01 15:53:19 +080064#define PLA_EXTRA_STATUS 0xd398
hayeswang65b82d62017-06-15 14:44:03 +080065#define PLA_EFUSE_DATA 0xdd00
66#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000067#define PLA_LEDSEL 0xdd90
68#define PLA_LED_FEATURE 0xdd92
69#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080070#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000071#define PLA_GPHY_INTR_IMR 0xe022
72#define PLA_EEE_CR 0xe040
73#define PLA_EEEP_CR 0xe080
74#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080075#define PLA_MAC_PWR_CTRL2 0xe0ca
76#define PLA_MAC_PWR_CTRL3 0xe0cc
77#define PLA_MAC_PWR_CTRL4 0xe0ce
78#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000079#define PLA_TCR0 0xe610
80#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080081#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000082#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080083#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000084#define PLA_CR 0xe813
85#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080086#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
87#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000088#define PLA_CONFIG5 0xe822
89#define PLA_PHY_PWR 0xe84c
90#define PLA_OOB_CTRL 0xe84f
91#define PLA_CPCR 0xe854
92#define PLA_MISC_0 0xe858
93#define PLA_MISC_1 0xe85a
94#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080095#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000096#define PLA_SFF_STS_7 0xe8de
97#define PLA_PHYSTATUS 0xe908
98#define PLA_BP_BA 0xfc26
99#define PLA_BP_0 0xfc28
100#define PLA_BP_1 0xfc2a
101#define PLA_BP_2 0xfc2c
102#define PLA_BP_3 0xfc2e
103#define PLA_BP_4 0xfc30
104#define PLA_BP_5 0xfc32
105#define PLA_BP_6 0xfc34
106#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800107#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000108
hayeswang65bab842015-02-12 16:20:46 +0800109#define USB_USB2PHY 0xb41e
110#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800111#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800112#define USB_CSR_DUMMY1 0xb464
113#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000114#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800115#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800116#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800117#define USB_BURST_SIZE 0xcfc0
Hayes Wang9370f2d2019-10-16 11:02:42 +0800118#define USB_FW_FIX_EN0 0xcfca
119#define USB_FW_FIX_EN1 0xcfcc
hayeswang65b82d62017-06-15 14:44:03 +0800120#define USB_LPM_CONFIG 0xcfd8
Hayes Wang9370f2d2019-10-16 11:02:42 +0800121#define USB_CSTMR 0xcfef /* RTL8153A */
122#define USB_FW_CTRL 0xd334 /* RTL8153B */
123#define USB_FC_TIMER 0xd340
hayeswangac718b62013-05-02 16:01:25 +0000124#define USB_USB_CTRL 0xd406
125#define USB_PHY_CTRL 0xd408
126#define USB_TX_AGG 0xd40a
127#define USB_RX_BUF_TH 0xd40c
128#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800129#define USB_RX_EARLY_TIMEOUT 0xd42c
130#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800131#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
132#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000133#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800134#define USB_UPT_RXDMA_OWN 0xd437
hayeswang43779f82014-01-02 11:25:10 +0800135#define USB_TOLERANCE 0xd490
136#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800137#define USB_BMU_RESET 0xd4b0
hayeswang65b82d62017-06-15 14:44:03 +0800138#define USB_U1U2_TIMER 0xd4da
Hayes Wang9370f2d2019-10-16 11:02:42 +0800139#define USB_FW_TASK 0xd4e8 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000140#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800141#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800142#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600143#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800144#define USB_AFE_CTRL2 0xd824
hayeswang65b82d62017-06-15 14:44:03 +0800145#define USB_UPS_CFG 0xd842
146#define USB_UPS_FLAGS 0xd848
Hayes Wang9370f2d2019-10-16 11:02:42 +0800147#define USB_WDT1_CTRL 0xe404
hayeswang43779f82014-01-02 11:25:10 +0800148#define USB_WDT11_CTRL 0xe43c
Hayes Wang9370f2d2019-10-16 11:02:42 +0800149#define USB_BP_BA PLA_BP_BA
150#define USB_BP_0 PLA_BP_0
151#define USB_BP_1 PLA_BP_1
152#define USB_BP_2 PLA_BP_2
153#define USB_BP_3 PLA_BP_3
154#define USB_BP_4 PLA_BP_4
155#define USB_BP_5 PLA_BP_5
156#define USB_BP_6 PLA_BP_6
157#define USB_BP_7 PLA_BP_7
158#define USB_BP_EN PLA_BP_EN /* RTL8153A */
159#define USB_BP_8 0xfc38 /* RTL8153B */
hayeswang65b82d62017-06-15 14:44:03 +0800160#define USB_BP_9 0xfc3a
161#define USB_BP_10 0xfc3c
162#define USB_BP_11 0xfc3e
163#define USB_BP_12 0xfc40
164#define USB_BP_13 0xfc42
165#define USB_BP_14 0xfc44
166#define USB_BP_15 0xfc46
167#define USB_BP2_EN 0xfc48
hayeswangac718b62013-05-02 16:01:25 +0000168
169/* OCP Registers */
170#define OCP_ALDPS_CONFIG 0x2010
171#define OCP_EEE_CONFIG1 0x2080
172#define OCP_EEE_CONFIG2 0x2092
173#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800174#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000175#define OCP_EEE_AR 0xa41a
176#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800177#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800178#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800179#define OCP_POWER_CFG 0xa430
180#define OCP_EEE_CFG 0xa432
181#define OCP_SRAM_ADDR 0xa436
182#define OCP_SRAM_DATA 0xa438
183#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800184#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800185#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800186#define OCP_EEE_LPABLE 0xa5d2
hayeswang2dd49e02015-09-07 11:57:44 +0800187#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800188#define OCP_PHY_PATCH_STAT 0xb800
189#define OCP_PHY_PATCH_CMD 0xb820
Hayes Wangaf142882019-10-21 11:41:13 +0800190#define OCP_PHY_LOCK 0xb82e
hayeswang65b82d62017-06-15 14:44:03 +0800191#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800192#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800193#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800194
195/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800196#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800197#define SRAM_LPF_CFG 0x8012
198#define SRAM_10M_AMP1 0x8080
199#define SRAM_10M_AMP2 0x8082
200#define SRAM_IMPEDANCE 0x8084
Hayes Wangaf142882019-10-21 11:41:13 +0800201#define SRAM_PHY_LOCK 0xb82e
hayeswangac718b62013-05-02 16:01:25 +0000202
203/* PLA_RCR */
204#define RCR_AAP 0x00000001
205#define RCR_APM 0x00000002
206#define RCR_AM 0x00000004
207#define RCR_AB 0x00000008
208#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
209
210/* PLA_RXFIFO_CTRL0 */
211#define RXFIFO_THR1_NORMAL 0x00080002
212#define RXFIFO_THR1_OOB 0x01800003
213
214/* PLA_RXFIFO_CTRL1 */
215#define RXFIFO_THR2_FULL 0x00000060
216#define RXFIFO_THR2_HIGH 0x00000038
217#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800218#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000219
220/* PLA_RXFIFO_CTRL2 */
221#define RXFIFO_THR3_FULL 0x00000078
222#define RXFIFO_THR3_HIGH 0x00000048
223#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800224#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000225
226/* PLA_TXFIFO_CTRL */
227#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800228#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000229
hayeswang65bab842015-02-12 16:20:46 +0800230/* PLA_DMY_REG0 */
231#define ECM_ALDPS 0x0002
232
hayeswangac718b62013-05-02 16:01:25 +0000233/* PLA_FMC */
234#define FMC_FCR_MCU_EN 0x0001
235
236/* PLA_EEEP_CR */
237#define EEEP_CR_EEEP_TX 0x0002
238
hayeswang43779f82014-01-02 11:25:10 +0800239/* PLA_WDT6_CTRL */
240#define WDT6_SET_MODE 0x0010
241
hayeswangac718b62013-05-02 16:01:25 +0000242/* PLA_TCR0 */
243#define TCR0_TX_EMPTY 0x0800
244#define TCR0_AUTO_FIFO 0x0080
245
246/* PLA_TCR1 */
247#define VERSION_MASK 0x7cf0
248
hayeswang69b4b7a2014-07-10 10:58:54 +0800249/* PLA_MTPS */
250#define MTPS_JUMBO (12 * 1024 / 64)
251#define MTPS_DEFAULT (6 * 1024 / 64)
252
hayeswang4f1d4d52014-03-11 16:24:19 +0800253/* PLA_RSTTALLY */
254#define TALLY_RESET 0x0001
255
hayeswangac718b62013-05-02 16:01:25 +0000256/* PLA_CR */
257#define CR_RST 0x10
258#define CR_RE 0x08
259#define CR_TE 0x04
260
261/* PLA_CRWECR */
262#define CRWECR_NORAML 0x00
263#define CRWECR_CONFIG 0xc0
264
265/* PLA_OOB_CTRL */
266#define NOW_IS_OOB 0x80
267#define TXFIFO_EMPTY 0x20
268#define RXFIFO_EMPTY 0x10
269#define LINK_LIST_READY 0x02
270#define DIS_MCU_CLROOB 0x01
271#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
272
273/* PLA_MISC_1 */
274#define RXDY_GATED_EN 0x0008
275
276/* PLA_SFF_STS_7 */
277#define RE_INIT_LL 0x8000
278#define MCU_BORW_EN 0x4000
279
280/* PLA_CPCR */
281#define CPCR_RX_VLAN 0x0040
282
283/* PLA_CFG_WOL */
284#define MAGIC_EN 0x0001
285
hayeswang43779f82014-01-02 11:25:10 +0800286/* PLA_TEREDO_CFG */
287#define TEREDO_SEL 0x8000
288#define TEREDO_WAKE_MASK 0x7f00
289#define TEREDO_RS_EVENT_MASK 0x00fe
290#define OOB_TEREDO_EN 0x0001
291
Kevin Lo59c0b472019-08-01 11:29:38 +0800292/* PLA_BDC_CR */
hayeswangac718b62013-05-02 16:01:25 +0000293#define ALDPS_PROXY_MODE 0x0001
294
hayeswang65b82d62017-06-15 14:44:03 +0800295/* PLA_EFUSE_CMD */
296#define EFUSE_READ_CMD BIT(15)
297#define EFUSE_DATA_BIT16 BIT(7)
298
hayeswang21ff2e82014-02-18 21:49:06 +0800299/* PLA_CONFIG34 */
300#define LINK_ON_WAKE_EN 0x0010
301#define LINK_OFF_WAKE_EN 0x0008
302
hayeswangac718b62013-05-02 16:01:25 +0000303/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800304#define BWF_EN 0x0040
305#define MWF_EN 0x0020
306#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000307#define LAN_WAKE_EN 0x0002
308
309/* PLA_LED_FEATURE */
310#define LED_MODE_MASK 0x0700
311
312/* PLA_PHY_PWR */
313#define TX_10M_IDLE_EN 0x0080
314#define PFM_PWM_SWITCH 0x0040
315
316/* PLA_MAC_PWR_CTRL */
317#define D3_CLK_GATED_EN 0x00004000
318#define MCU_CLK_RATIO 0x07010f07
319#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800320#define ALDPS_SPDWN_RATIO 0x0f87
321
322/* PLA_MAC_PWR_CTRL2 */
323#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800324#define MAC_CLK_SPDWN_EN BIT(15)
hayeswang43779f82014-01-02 11:25:10 +0800325
326/* PLA_MAC_PWR_CTRL3 */
327#define PKT_AVAIL_SPDWN_EN 0x0100
328#define SUSPEND_SPDWN_EN 0x0004
329#define U1U2_SPDWN_EN 0x0002
330#define L1_SPDWN_EN 0x0001
331
332/* PLA_MAC_PWR_CTRL4 */
333#define PWRSAVE_SPDWN_EN 0x1000
334#define RXDV_SPDWN_EN 0x0800
335#define TX10MIDLE_EN 0x0100
336#define TP100_SPDWN_EN 0x0020
337#define TP500_SPDWN_EN 0x0010
338#define TP1000_SPDWN_EN 0x0008
339#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000340
341/* PLA_GPHY_INTR_IMR */
342#define GPHY_STS_MSK 0x0001
343#define SPEED_DOWN_MSK 0x0002
344#define SPDWN_RXDV_MSK 0x0004
345#define SPDWN_LINKCHG_MSK 0x0008
346
347/* PLA_PHYAR */
348#define PHYAR_FLAG 0x80000000
349
350/* PLA_EEE_CR */
351#define EEE_RX_EN 0x0001
352#define EEE_TX_EN 0x0002
353
hayeswang43779f82014-01-02 11:25:10 +0800354/* PLA_BOOT_CTRL */
355#define AUTOLOAD_DONE 0x0002
356
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800357/* PLA_SUSPEND_FLAG */
358#define LINK_CHG_EVENT BIT(0)
359
360/* PLA_INDICATE_FALG */
361#define UPCOMING_RUNTIME_D3 BIT(0)
362
Hayes Wang9370f2d2019-10-16 11:02:42 +0800363/* PLA_MACDBG_PRE and PLA_MACDBG_POST */
364#define DEBUG_OE BIT(0)
365#define DEBUG_LTSSM 0x0082
366
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800367/* PLA_EXTRA_STATUS */
Hayes Wanga3914272020-01-22 16:02:05 +0800368#define CUR_LINK_OK BIT(15)
Hayes Wang9370f2d2019-10-16 11:02:42 +0800369#define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800370#define LINK_CHANGE_FLAG BIT(8)
Hayes Wanga3914272020-01-22 16:02:05 +0800371#define POLL_LINK_CHG BIT(0)
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800372
hayeswang65bab842015-02-12 16:20:46 +0800373/* USB_USB2PHY */
374#define USB2PHY_SUSPEND 0x0001
375#define USB2PHY_L1 0x0002
376
377/* USB_SSPHYLINK2 */
378#define pwd_dn_scale_mask 0x3ffe
379#define pwd_dn_scale(x) ((x) << 1)
380
381/* USB_CSR_DUMMY1 */
382#define DYNAMIC_BURST 0x0001
383
384/* USB_CSR_DUMMY2 */
385#define EP4_FULL_FC 0x0001
386
hayeswangac718b62013-05-02 16:01:25 +0000387/* USB_DEV_STAT */
388#define STAT_SPEED_MASK 0x0006
389#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800390#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000391
Hayes Wang9370f2d2019-10-16 11:02:42 +0800392/* USB_FW_FIX_EN0 */
393#define FW_FIX_SUSPEND BIT(14)
394
395/* USB_FW_FIX_EN1 */
396#define FW_IP_RESET_EN BIT(9)
397
hayeswang65b82d62017-06-15 14:44:03 +0800398/* USB_LPM_CONFIG */
399#define LPM_U1U2_EN BIT(0)
400
hayeswangac718b62013-05-02 16:01:25 +0000401/* USB_TX_AGG */
402#define TX_AGG_MAX_THRESHOLD 0x03
403
404/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800405#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800406#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800407#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800408#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000409
410/* USB_TX_DMA */
411#define TEST_MODE_DISABLE 0x00000001
412#define TX_SIZE_ADJUST1 0x00000100
413
hayeswang93fe9b12016-06-16 10:55:18 +0800414/* USB_BMU_RESET */
415#define BMU_RESET_EP_IN 0x01
416#define BMU_RESET_EP_OUT 0x02
417
hayeswang65b82d62017-06-15 14:44:03 +0800418/* USB_UPT_RXDMA_OWN */
419#define OWN_UPDATE BIT(0)
420#define OWN_CLEAR BIT(1)
421
Hayes Wang9370f2d2019-10-16 11:02:42 +0800422/* USB_FW_TASK */
423#define FC_PATCH_TASK BIT(1)
424
hayeswangac718b62013-05-02 16:01:25 +0000425/* USB_UPS_CTRL */
426#define POWER_CUT 0x0100
427
428/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800429#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000430
Hayes Wang9370f2d2019-10-16 11:02:42 +0800431/* USB_CSTMR */
432#define FORCE_SUPER BIT(0)
433
434/* USB_FW_CTRL */
435#define FLOW_CTRL_PATCH_OPT BIT(1)
436
437/* USB_FC_TIMER */
438#define CTRL_TIMER_EN BIT(15)
439
hayeswangac718b62013-05-02 16:01:25 +0000440/* USB_USB_CTRL */
441#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800442#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000443
hayeswang43779f82014-01-02 11:25:10 +0800444/* USB_U2P3_CTRL */
445#define U2P3_ENABLE 0x0001
446
447/* USB_POWER_CUT */
448#define PWR_EN 0x0001
449#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800450#define UPS_EN BIT(4)
451#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800452
453/* USB_MISC_0 */
454#define PCUT_STATUS 0x0001
455
hayeswang464ec102015-02-12 14:33:46 +0800456/* USB_RX_EARLY_TIMEOUT */
457#define COALESCE_SUPER 85000U
458#define COALESCE_HIGH 250000U
459#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800460
Hayes Wang9370f2d2019-10-16 11:02:42 +0800461/* USB_WDT1_CTRL */
462#define WTD1_EN BIT(0)
463
hayeswang43779f82014-01-02 11:25:10 +0800464/* USB_WDT11_CTRL */
465#define TIMER11_EN 0x0001
466
467/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800468/* bit 4 ~ 5: fifo empty boundary */
469#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
470/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800471#define LPM_TIMER_MASK 0x0c
472#define LPM_TIMER_500MS 0x04 /* 500 ms */
473#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800474#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800475
476/* USB_AFE_CTRL2 */
477#define SEN_VAL_MASK 0xf800
478#define SEN_VAL_NORMAL 0xa000
479#define SEL_RXIDLE 0x0100
480
hayeswang65b82d62017-06-15 14:44:03 +0800481/* USB_UPS_CFG */
482#define SAW_CNT_1MS_MASK 0x0fff
483
484/* USB_UPS_FLAGS */
485#define UPS_FLAGS_R_TUNE BIT(0)
486#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
487#define UPS_FLAGS_250M_CKDIV BIT(2)
488#define UPS_FLAGS_EN_ALDPS BIT(3)
489#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
hayeswang65b82d62017-06-15 14:44:03 +0800490#define ups_flags_speed(x) ((x) << 16)
491#define UPS_FLAGS_EN_EEE BIT(20)
492#define UPS_FLAGS_EN_500M_EEE BIT(21)
493#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800494#define UPS_FLAGS_EEE_PLLOFF_100 BIT(23)
hayeswang65b82d62017-06-15 14:44:03 +0800495#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
496#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
497#define UPS_FLAGS_EN_GREEN BIT(26)
498#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
499
500enum spd_duplex {
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800501 NWAY_10M_HALF,
hayeswang65b82d62017-06-15 14:44:03 +0800502 NWAY_10M_FULL,
503 NWAY_100M_HALF,
504 NWAY_100M_FULL,
505 NWAY_1000M_FULL,
506 FORCE_10M_HALF,
507 FORCE_10M_FULL,
508 FORCE_100M_HALF,
509 FORCE_100M_FULL,
510};
511
hayeswangac718b62013-05-02 16:01:25 +0000512/* OCP_ALDPS_CONFIG */
513#define ENPWRSAVE 0x8000
514#define ENPDNPS 0x0200
515#define LINKENA 0x0100
516#define DIS_SDSAVE 0x0010
517
hayeswang43779f82014-01-02 11:25:10 +0800518/* OCP_PHY_STATUS */
519#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800520#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800521#define PHY_STAT_LAN_ON 3
522#define PHY_STAT_PWRDN 5
523
hayeswang65b82d62017-06-15 14:44:03 +0800524/* OCP_NCTL_CFG */
525#define PGA_RETURN_EN BIT(1)
526
hayeswang43779f82014-01-02 11:25:10 +0800527/* OCP_POWER_CFG */
528#define EEE_CLKDIV_EN 0x8000
529#define EN_ALDPS 0x0004
530#define EN_10M_PLLOFF 0x0001
531
hayeswangac718b62013-05-02 16:01:25 +0000532/* OCP_EEE_CONFIG1 */
533#define RG_TXLPI_MSK_HFDUP 0x8000
534#define RG_MATCLR_EN 0x4000
535#define EEE_10_CAP 0x2000
536#define EEE_NWAY_EN 0x1000
537#define TX_QUIET_EN 0x0200
538#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800539#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800540#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000541#define RG_RXLPI_MSK_HFDUP 0x0008
542#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
543
544/* OCP_EEE_CONFIG2 */
545#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
546#define RG_DACQUIET_EN 0x0400
547#define RG_LDVQUIET_EN 0x0200
548#define RG_CKRSEL 0x0020
549#define RG_EEEPRG_EN 0x0010
550
551/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800552#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800553#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000554#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
555#define MSK_PH 0x0006 /* bit 0 ~ 3 */
556
557/* OCP_EEE_AR */
558/* bit[15:14] function */
559#define FUN_ADDR 0x0000
560#define FUN_DATA 0x4000
561/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000562
hayeswang43779f82014-01-02 11:25:10 +0800563/* OCP_EEE_CFG */
564#define CTAP_SHORT_EN 0x0040
565#define EEE10_EN 0x0010
566
567/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800568#define EN_EEE_CMODE BIT(14)
569#define EN_EEE_1000 BIT(13)
570#define EN_EEE_100 BIT(12)
571#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800572#define EN_10M_BGOFF 0x0080
573
hayeswang2dd49e02015-09-07 11:57:44 +0800574/* OCP_PHY_STATE */
575#define TXDIS_STATE 0x01
576#define ABD_STATE 0x02
577
hayeswang65b82d62017-06-15 14:44:03 +0800578/* OCP_PHY_PATCH_STAT */
579#define PATCH_READY BIT(6)
580
581/* OCP_PHY_PATCH_CMD */
582#define PATCH_REQUEST BIT(4)
583
Hayes Wangaf142882019-10-21 11:41:13 +0800584/* OCP_PHY_LOCK */
585#define PATCH_LOCK BIT(0)
586
hayeswang43779f82014-01-02 11:25:10 +0800587/* OCP_ADC_CFG */
588#define CKADSEL_L 0x0100
589#define ADC_EN 0x0080
590#define EN_EMI_L 0x0040
591
hayeswang65b82d62017-06-15 14:44:03 +0800592/* OCP_SYSCLK_CFG */
593#define clk_div_expo(x) (min(x, 5) << 8)
594
595/* SRAM_GREEN_CFG */
596#define GREEN_ETH_EN BIT(15)
597#define R_TUNE_EN BIT(11)
598
hayeswang43779f82014-01-02 11:25:10 +0800599/* SRAM_LPF_CFG */
600#define LPF_AUTO_TUNE 0x8000
601
602/* SRAM_10M_AMP1 */
603#define GDAC_IB_UPALL 0x0008
604
605/* SRAM_10M_AMP2 */
606#define AMP_DN 0x0200
607
608/* SRAM_IMPEDANCE */
609#define RX_DRIVING_MASK 0x6000
610
Hayes Wangaf142882019-10-21 11:41:13 +0800611/* SRAM_PHY_LOCK */
612#define PHY_PATCH_LOCK 0x0001
613
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500614/* MAC PASSTHRU */
615#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600616#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800617#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500618#define EFUSE 0xcfdb
619#define PASS_THRU_MASK 0x1
620
Hayes Wang9370f2d2019-10-16 11:02:42 +0800621#define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */
622
hayeswangac718b62013-05-02 16:01:25 +0000623enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800624 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000625 _100bps = 0x08,
626 _10bps = 0x04,
627 LINK_STATUS = 0x02,
628 FULL_DUP = 0x01,
629};
630
hayeswang1764bcd2014-08-28 10:24:18 +0800631#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800632#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800633#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800634#define TX_ALIGN 4
635#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800636
Hayes Wange4a50172019-08-13 11:42:09 +0800637#define RTL8152_RX_MAX_PENDING 4096
Hayes Wang47922fc2019-08-13 11:42:08 +0800638#define RTL8152_RXFG_HEADSZ 256
639
hayeswang40a82912013-08-14 20:54:40 +0800640#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800641
hayeswangac718b62013-05-02 16:01:25 +0000642#define RTL8152_REQT_READ 0xc0
643#define RTL8152_REQT_WRITE 0x40
644#define RTL8152_REQ_GET_REGS 0x05
645#define RTL8152_REQ_SET_REGS 0x05
646
647#define BYTE_EN_DWORD 0xff
648#define BYTE_EN_WORD 0x33
649#define BYTE_EN_BYTE 0x11
650#define BYTE_EN_SIX_BYTES 0x3f
651#define BYTE_EN_START_MASK 0x0f
652#define BYTE_EN_END_MASK 0xf0
653
hayeswang69b4b7a2014-07-10 10:58:54 +0800654#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800655#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
656 ETH_FCS_LEN)
657#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800658#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800659#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800660#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800661#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800662 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000663
664/* rtl8152 flags */
665enum rtl8152_flags {
666 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000667 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800668 WORK_ENABLE,
669 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800670 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800671 PHY_RESET,
Hayes Wangd2187f82019-08-19 14:40:36 +0800672 SCHEDULE_TASKLET,
hayeswang65b82d62017-06-15 14:44:03 +0800673 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800674 DELL_TB_RX_AGG_BUG,
Kai-Heng Feng96477222019-11-05 19:24:52 +0800675 LENOVO_MACPASSTHRU,
hayeswangac718b62013-05-02 16:01:25 +0000676};
677
678/* Define these values to match your device */
679#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200680#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800681#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200682#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700683#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700684#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800685#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000686
687#define MCU_TYPE_PLA 0x0100
688#define MCU_TYPE_USB 0x0000
689
hayeswang4f1d4d52014-03-11 16:24:19 +0800690struct tally_counter {
691 __le64 tx_packets;
692 __le64 rx_packets;
693 __le64 tx_errors;
694 __le32 rx_errors;
695 __le16 rx_missed;
696 __le16 align_errors;
697 __le32 tx_one_collision;
698 __le32 tx_multi_collision;
699 __le64 rx_unicast;
700 __le64 rx_broadcast;
701 __le32 rx_multicast;
702 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800703 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800704};
705
hayeswangac718b62013-05-02 16:01:25 +0000706struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800707 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000708#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800709
hayeswang500b6d72013-11-20 17:30:57 +0800710 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800711#define RD_UDP_CS BIT(23)
712#define RD_TCP_CS BIT(22)
713#define RD_IPV6_CS BIT(20)
714#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800715
hayeswang500b6d72013-11-20 17:30:57 +0800716 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800717#define IPF BIT(23) /* IP checksum fail */
718#define UDPF BIT(22) /* UDP checksum fail */
719#define TCPF BIT(21) /* TCP checksum fail */
720#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800721
hayeswang500b6d72013-11-20 17:30:57 +0800722 __le32 opts4;
723 __le32 opts5;
724 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000725};
726
727struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800728 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800729#define TX_FS BIT(31) /* First segment of a packet */
730#define TX_LS BIT(30) /* Final segment of a packet */
731#define GTSENDV4 BIT(28)
732#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800733#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800734#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800735#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800736
hayeswang500b6d72013-11-20 17:30:57 +0800737 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800738#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
739#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
740#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
741#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800742#define MSS_SHIFT 17
743#define MSS_MAX 0x7ffU
744#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800745#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800746#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000747};
748
hayeswangdff4e8a2013-08-16 16:09:33 +0800749struct r8152;
750
hayeswangebc2ec482013-08-14 20:54:38 +0800751struct rx_agg {
Hayes Wang252df8b2019-08-13 11:42:06 +0800752 struct list_head list, info_list;
hayeswangebc2ec482013-08-14 20:54:38 +0800753 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800754 struct r8152 *context;
Hayes Wangd55d7082019-08-13 11:42:07 +0800755 struct page *page;
hayeswangebc2ec482013-08-14 20:54:38 +0800756 void *buffer;
hayeswangebc2ec482013-08-14 20:54:38 +0800757};
758
759struct tx_agg {
760 struct list_head list;
761 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800762 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800763 void *buffer;
764 void *head;
765 u32 skb_num;
766 u32 skb_len;
767};
768
hayeswangac718b62013-05-02 16:01:25 +0000769struct r8152 {
770 unsigned long flags;
771 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800772 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800773 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000774 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800775 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800776 struct tx_agg tx_info[RTL8152_MAX_TX];
Hayes Wang47922fc2019-08-13 11:42:08 +0800777 struct list_head rx_info, rx_used;
hayeswangebc2ec482013-08-14 20:54:38 +0800778 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800779 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800780 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800781 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000782 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800783 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800784#ifdef CONFIG_PM_SLEEP
785 struct notifier_block pm_notifier;
786#endif
Hayes Wangd2187f82019-08-19 14:40:36 +0800787 struct tasklet_struct tx_tl;
hayeswangc81229c2014-01-02 11:22:42 +0800788
789 struct rtl_ops {
Prashant Malani151ea092019-10-02 14:09:33 -0700790 void (*init)(struct r8152 *tp);
791 int (*enable)(struct r8152 *tp);
792 void (*disable)(struct r8152 *tp);
793 void (*up)(struct r8152 *tp);
794 void (*down)(struct r8152 *tp);
795 void (*unload)(struct r8152 *tp);
796 int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee);
797 int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee);
798 bool (*in_nway)(struct r8152 *tp);
799 void (*hw_phy_cfg)(struct r8152 *tp);
hayeswang2609af12016-07-05 16:11:46 +0800800 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800801 } rtl_ops;
802
Hayes Wang0e5b36b2019-09-05 10:46:20 +0800803 struct ups_info {
804 u32 _10m_ckdiv:1;
805 u32 _250m_ckdiv:1;
806 u32 aldps:1;
807 u32 lite_mode:2;
808 u32 speed_duplex:4;
809 u32 eee:1;
810 u32 eee_lite:1;
811 u32 eee_ckdiv:1;
812 u32 eee_plloff_100:1;
813 u32 eee_plloff_giga:1;
814 u32 eee_cmod_lv:1;
815 u32 green:1;
816 u32 flow_control:1;
817 u32 ctap_short_off:1;
818 } ups_info;
819
Hayes Wang9370f2d2019-10-16 11:02:42 +0800820#define RTL_VER_SIZE 32
821
822 struct rtl_fw {
823 const char *fw_name;
824 const struct firmware *fw;
825
826 char version[RTL_VER_SIZE];
827 int (*pre_fw)(struct r8152 *tp);
828 int (*post_fw)(struct r8152 *tp);
829
830 bool retry;
831 } rtl_fw;
832
Hayes Wang252df8b2019-08-13 11:42:06 +0800833 atomic_t rx_count;
834
Hayes Wangf4a93be2019-08-23 15:33:40 +0800835 bool eee_en;
hayeswang40a82912013-08-14 20:54:40 +0800836 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800837 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000838 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800839 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800840 u32 coalesce;
Hayes Wang771efed2019-09-02 19:52:28 +0800841 u32 advertising;
Hayes Wangec5791c2019-08-13 11:42:05 +0800842 u32 rx_buf_sz;
Hayes Wange4a50172019-08-13 11:42:09 +0800843 u32 rx_copybreak;
844 u32 rx_pending;
845
hayeswangac718b62013-05-02 16:01:25 +0000846 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800847 u16 speed;
Hayes Wangf4a93be2019-08-23 15:33:40 +0800848 u16 eee_adv;
hayeswang40a82912013-08-14 20:54:40 +0800849 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000850 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800851 u8 duplex;
852 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000853};
854
Hayes Wang9370f2d2019-10-16 11:02:42 +0800855/**
856 * struct fw_block - block type and total length
857 * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
858 * RTL_FW_USB and so on.
859 * @length: total length of the current block.
860 */
861struct fw_block {
862 __le32 type;
863 __le32 length;
864} __packed;
865
866/**
867 * struct fw_header - header of the firmware file
868 * @checksum: checksum of sha256 which is calculated from the whole file
869 * except the checksum field of the file. That is, calculate sha256
870 * from the version field to the end of the file.
871 * @version: version of this firmware.
872 * @blocks: the first firmware block of the file
873 */
874struct fw_header {
875 u8 checksum[32];
876 char version[RTL_VER_SIZE];
877 struct fw_block blocks[0];
878} __packed;
879
880/**
Hayes Wanga66edaa2019-10-21 11:41:10 +0800881 * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
Hayes Wang9370f2d2019-10-16 11:02:42 +0800882 * The layout of the firmware block is:
Hayes Wanga66edaa2019-10-21 11:41:10 +0800883 * <struct fw_mac> + <info> + <firmware data>.
Hayes Wang9370f2d2019-10-16 11:02:42 +0800884 * @fw_offset: offset of the firmware binary data. The start address of
Hayes Wanga66edaa2019-10-21 11:41:10 +0800885 * the data would be the address of struct fw_mac + @fw_offset.
Hayes Wang9370f2d2019-10-16 11:02:42 +0800886 * @fw_reg: the register to load the firmware. Depends on chip.
887 * @bp_ba_addr: the register to write break point base address. Depends on
888 * chip.
889 * @bp_ba_value: break point base address. Depends on chip.
890 * @bp_en_addr: the register to write break point enabled mask. Depends
891 * on chip.
892 * @bp_en_value: break point enabled mask. Depends on the firmware.
893 * @bp_start: the start register of break points. Depends on chip.
894 * @bp_num: the break point number which needs to be set for this firmware.
895 * Depends on the firmware.
896 * @bp: break points. Depends on firmware.
897 * @fw_ver_reg: the register to store the fw version.
898 * @fw_ver_data: the firmware version of the current type.
899 * @info: additional information for debugging, and is followed by the
900 * binary data of firmware.
901 */
Hayes Wanga66edaa2019-10-21 11:41:10 +0800902struct fw_mac {
Hayes Wang9370f2d2019-10-16 11:02:42 +0800903 struct fw_block blk_hdr;
904 __le16 fw_offset;
905 __le16 fw_reg;
906 __le16 bp_ba_addr;
907 __le16 bp_ba_value;
908 __le16 bp_en_addr;
909 __le16 bp_en_value;
910 __le16 bp_start;
911 __le16 bp_num;
912 __le16 bp[16]; /* any value determined by firmware */
913 __le32 reserved;
914 __le16 fw_ver_reg;
915 u8 fw_ver_data;
916 char info[0];
917} __packed;
918
Hayes Wangaf142882019-10-21 11:41:13 +0800919/**
920 * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
921 * This is used to set patch key when loading the firmware of PHY.
922 * @key_reg: the register to write the patch key.
923 * @key_data: patch key.
924 */
925struct fw_phy_patch_key {
926 struct fw_block blk_hdr;
927 __le16 key_reg;
928 __le16 key_data;
929 __le32 reserved;
930} __packed;
931
932/**
933 * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
934 * The layout of the firmware block is:
935 * <struct fw_phy_nc> + <info> + <firmware data>.
936 * @fw_offset: offset of the firmware binary data. The start address of
937 * the data would be the address of struct fw_phy_nc + @fw_offset.
938 * @fw_reg: the register to load the firmware. Depends on chip.
939 * @ba_reg: the register to write the base address. Depends on chip.
940 * @ba_data: base address. Depends on chip.
941 * @patch_en_addr: the register of enabling patch mode. Depends on chip.
942 * @patch_en_value: patch mode enabled mask. Depends on the firmware.
943 * @mode_reg: the regitster of switching the mode.
944 * @mod_pre: the mode needing to be set before loading the firmware.
945 * @mod_post: the mode to be set when finishing to load the firmware.
946 * @bp_start: the start register of break points. Depends on chip.
947 * @bp_num: the break point number which needs to be set for this firmware.
948 * Depends on the firmware.
949 * @bp: break points. Depends on firmware.
950 * @info: additional information for debugging, and is followed by the
951 * binary data of firmware.
952 */
953struct fw_phy_nc {
954 struct fw_block blk_hdr;
955 __le16 fw_offset;
956 __le16 fw_reg;
957 __le16 ba_reg;
958 __le16 ba_data;
959 __le16 patch_en_addr;
960 __le16 patch_en_value;
961 __le16 mode_reg;
962 __le16 mode_pre;
963 __le16 mode_post;
964 __le16 reserved;
965 __le16 bp_start;
966 __le16 bp_num;
967 __le16 bp[4];
968 char info[0];
969} __packed;
970
Hayes Wang9370f2d2019-10-16 11:02:42 +0800971enum rtl_fw_type {
972 RTL_FW_END = 0,
973 RTL_FW_PLA,
974 RTL_FW_USB,
Hayes Wangaf142882019-10-21 11:41:13 +0800975 RTL_FW_PHY_START,
976 RTL_FW_PHY_STOP,
977 RTL_FW_PHY_NC,
Hayes Wang9370f2d2019-10-16 11:02:42 +0800978};
979
hayeswangac718b62013-05-02 16:01:25 +0000980enum rtl_version {
981 RTL_VER_UNKNOWN = 0,
982 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800983 RTL_VER_02,
984 RTL_VER_03,
985 RTL_VER_04,
986 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800987 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800988 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800989 RTL_VER_08,
990 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800991 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000992};
993
hayeswang60c89072014-03-07 11:04:39 +0800994enum tx_csum_stat {
995 TX_CSUM_SUCCESS = 0,
996 TX_CSUM_TSO,
997 TX_CSUM_NONE
998};
999
Hayes Wang771efed2019-09-02 19:52:28 +08001000#define RTL_ADVERTISED_10_HALF BIT(0)
1001#define RTL_ADVERTISED_10_FULL BIT(1)
1002#define RTL_ADVERTISED_100_HALF BIT(2)
1003#define RTL_ADVERTISED_100_FULL BIT(3)
1004#define RTL_ADVERTISED_1000_HALF BIT(4)
1005#define RTL_ADVERTISED_1000_FULL BIT(5)
1006
hayeswangac718b62013-05-02 16:01:25 +00001007/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1008 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1009 */
1010static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +08001011static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +00001012
hayeswang52aec122014-09-02 10:27:52 +08001013#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +08001014 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +08001015
hayeswangac718b62013-05-02 16:01:25 +00001016static
1017int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1018{
hayeswang31787f52013-07-31 17:21:25 +08001019 int ret;
1020 void *tmp;
1021
1022 tmp = kmalloc(size, GFP_KERNEL);
1023 if (!tmp)
1024 return -ENOMEM;
1025
1026 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +08001027 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1028 value, index, tmp, size, 500);
Prashant Malanif53a7ad2019-08-24 01:36:19 -07001029 if (ret < 0)
1030 memset(data, 0xff, size);
1031 else
1032 memcpy(data, tmp, size);
hayeswang31787f52013-07-31 17:21:25 +08001033
hayeswang31787f52013-07-31 17:21:25 +08001034 kfree(tmp);
1035
1036 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001037}
1038
1039static
1040int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1041{
hayeswang31787f52013-07-31 17:21:25 +08001042 int ret;
1043 void *tmp;
1044
Benoit Tainec4438f02014-05-26 17:21:23 +02001045 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +08001046 if (!tmp)
1047 return -ENOMEM;
1048
hayeswang31787f52013-07-31 17:21:25 +08001049 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +08001050 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1051 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +08001052
1053 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +08001054
hayeswang31787f52013-07-31 17:21:25 +08001055 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001056}
1057
Hayes Wangffa9fec2019-07-04 17:36:32 +08001058static void rtl_set_unplug(struct r8152 *tp)
1059{
1060 if (tp->udev->state == USB_STATE_NOTATTACHED) {
1061 set_bit(RTL8152_UNPLUG, &tp->flags);
1062 smp_mb__after_atomic();
1063 }
1064}
1065
hayeswangac718b62013-05-02 16:01:25 +00001066static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +08001067 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +00001068{
hayeswang45f4a192014-01-06 17:08:41 +08001069 u16 limit = 64;
1070 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00001071
1072 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1073 return -ENODEV;
1074
1075 /* both size and indix must be 4 bytes align */
1076 if ((size & 3) || !size || (index & 3) || !data)
1077 return -EPERM;
1078
1079 if ((u32)index + (u32)size > 0xffff)
1080 return -EPERM;
1081
1082 while (size) {
1083 if (size > limit) {
1084 ret = get_registers(tp, index, type, limit, data);
1085 if (ret < 0)
1086 break;
1087
1088 index += limit;
1089 data += limit;
1090 size -= limit;
1091 } else {
1092 ret = get_registers(tp, index, type, size, data);
1093 if (ret < 0)
1094 break;
1095
1096 index += size;
1097 data += size;
1098 size = 0;
1099 break;
1100 }
1101 }
1102
hayeswang67610492014-10-30 11:46:40 +08001103 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +08001104 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +08001105
hayeswangac718b62013-05-02 16:01:25 +00001106 return ret;
1107}
1108
1109static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +08001110 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +00001111{
hayeswang45f4a192014-01-06 17:08:41 +08001112 int ret;
1113 u16 byteen_start, byteen_end, byen;
1114 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +00001115
1116 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1117 return -ENODEV;
1118
1119 /* both size and indix must be 4 bytes align */
1120 if ((size & 3) || !size || (index & 3) || !data)
1121 return -EPERM;
1122
1123 if ((u32)index + (u32)size > 0xffff)
1124 return -EPERM;
1125
1126 byteen_start = byteen & BYTE_EN_START_MASK;
1127 byteen_end = byteen & BYTE_EN_END_MASK;
1128
1129 byen = byteen_start | (byteen_start << 4);
1130 ret = set_registers(tp, index, type | byen, 4, data);
1131 if (ret < 0)
1132 goto error1;
1133
1134 index += 4;
1135 data += 4;
1136 size -= 4;
1137
1138 if (size) {
1139 size -= 4;
1140
1141 while (size) {
1142 if (size > limit) {
1143 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +08001144 type | BYTE_EN_DWORD,
1145 limit, data);
hayeswangac718b62013-05-02 16:01:25 +00001146 if (ret < 0)
1147 goto error1;
1148
1149 index += limit;
1150 data += limit;
1151 size -= limit;
1152 } else {
1153 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +08001154 type | BYTE_EN_DWORD,
1155 size, data);
hayeswangac718b62013-05-02 16:01:25 +00001156 if (ret < 0)
1157 goto error1;
1158
1159 index += size;
1160 data += size;
1161 size = 0;
1162 break;
1163 }
1164 }
1165
1166 byen = byteen_end | (byteen_end >> 4);
1167 ret = set_registers(tp, index, type | byen, 4, data);
1168 if (ret < 0)
1169 goto error1;
1170 }
1171
1172error1:
hayeswang67610492014-10-30 11:46:40 +08001173 if (ret == -ENODEV)
Hayes Wangffa9fec2019-07-04 17:36:32 +08001174 rtl_set_unplug(tp);
hayeswang67610492014-10-30 11:46:40 +08001175
hayeswangac718b62013-05-02 16:01:25 +00001176 return ret;
1177}
1178
1179static inline
1180int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1181{
1182 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1183}
1184
1185static inline
1186int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1187{
1188 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1189}
1190
1191static inline
hayeswangac718b62013-05-02 16:01:25 +00001192int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1193{
1194 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1195}
1196
1197static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1198{
hayeswangc8826de2013-07-31 17:21:26 +08001199 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +00001200
hayeswangc8826de2013-07-31 17:21:26 +08001201 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +00001202
1203 return __le32_to_cpu(data);
1204}
1205
1206static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1207{
hayeswangc8826de2013-07-31 17:21:26 +08001208 __le32 tmp = __cpu_to_le32(data);
1209
1210 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001211}
1212
1213static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1214{
1215 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001216 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +08001217 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +00001218 u8 shift = index & 2;
1219
1220 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +08001221 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +00001222
hayeswangd8fbd272017-06-15 14:44:04 +08001223 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +00001224
hayeswangc8826de2013-07-31 17:21:26 +08001225 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001226 data >>= (shift * 8);
1227 data &= 0xffff;
1228
1229 return (u16)data;
1230}
1231
1232static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1233{
hayeswangc8826de2013-07-31 17:21:26 +08001234 u32 mask = 0xffff;
1235 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001236 u16 byen = BYTE_EN_WORD;
1237 u8 shift = index & 2;
1238
1239 data &= mask;
1240
1241 if (index & 2) {
1242 byen <<= shift;
1243 mask <<= (shift * 8);
1244 data <<= (shift * 8);
1245 index &= ~3;
1246 }
1247
hayeswangc8826de2013-07-31 17:21:26 +08001248 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001249
hayeswangc8826de2013-07-31 17:21:26 +08001250 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001251}
1252
1253static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1254{
1255 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001256 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001257 u8 shift = index & 3;
1258
1259 index &= ~3;
1260
hayeswangc8826de2013-07-31 17:21:26 +08001261 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001262
hayeswangc8826de2013-07-31 17:21:26 +08001263 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001264 data >>= (shift * 8);
1265 data &= 0xff;
1266
1267 return (u8)data;
1268}
1269
1270static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1271{
hayeswangc8826de2013-07-31 17:21:26 +08001272 u32 mask = 0xff;
1273 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001274 u16 byen = BYTE_EN_BYTE;
1275 u8 shift = index & 3;
1276
1277 data &= mask;
1278
1279 if (index & 3) {
1280 byen <<= shift;
1281 mask <<= (shift * 8);
1282 data <<= (shift * 8);
1283 index &= ~3;
1284 }
1285
hayeswangc8826de2013-07-31 17:21:26 +08001286 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001287
hayeswangc8826de2013-07-31 17:21:26 +08001288 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001289}
1290
hayeswangac244d32014-01-02 11:22:40 +08001291static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1292{
1293 u16 ocp_base, ocp_index;
1294
1295 ocp_base = addr & 0xf000;
1296 if (ocp_base != tp->ocp_base) {
1297 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1298 tp->ocp_base = ocp_base;
1299 }
1300
1301 ocp_index = (addr & 0x0fff) | 0xb000;
1302 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1303}
1304
hayeswange3fe0b12014-01-02 11:22:39 +08001305static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1306{
1307 u16 ocp_base, ocp_index;
1308
1309 ocp_base = addr & 0xf000;
1310 if (ocp_base != tp->ocp_base) {
1311 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1312 tp->ocp_base = ocp_base;
1313 }
1314
1315 ocp_index = (addr & 0x0fff) | 0xb000;
1316 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1317}
1318
hayeswangac244d32014-01-02 11:22:40 +08001319static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001320{
hayeswangac244d32014-01-02 11:22:40 +08001321 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001322}
1323
hayeswangac244d32014-01-02 11:22:40 +08001324static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001325{
hayeswangac244d32014-01-02 11:22:40 +08001326 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001327}
1328
hayeswang43779f82014-01-02 11:25:10 +08001329static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1330{
1331 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1332 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1333}
1334
hayeswang65b82d62017-06-15 14:44:03 +08001335static u16 sram_read(struct r8152 *tp, u16 addr)
1336{
1337 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1338 return ocp_reg_read(tp, OCP_SRAM_DATA);
1339}
1340
hayeswangac718b62013-05-02 16:01:25 +00001341static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1342{
1343 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001344 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001345
hayeswang68714382014-04-11 17:54:31 +08001346 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1347 return -ENODEV;
1348
hayeswangac718b62013-05-02 16:01:25 +00001349 if (phy_id != R8152_PHY_ID)
1350 return -EINVAL;
1351
hayeswang9a4be1b2014-02-18 21:49:07 +08001352 ret = r8152_mdio_read(tp, reg);
1353
hayeswang9a4be1b2014-02-18 21:49:07 +08001354 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001355}
1356
1357static
1358void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1359{
1360 struct r8152 *tp = netdev_priv(netdev);
1361
hayeswang68714382014-04-11 17:54:31 +08001362 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1363 return;
1364
hayeswangac718b62013-05-02 16:01:25 +00001365 if (phy_id != R8152_PHY_ID)
1366 return;
1367
1368 r8152_mdio_write(tp, reg, val);
1369}
1370
hayeswangb209af92014-08-25 15:53:00 +08001371static int
1372r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001373
hayeswang8ba789a2014-09-04 16:15:41 +08001374static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1375{
1376 struct r8152 *tp = netdev_priv(netdev);
1377 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001378 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001379
1380 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001381 goto out1;
1382
1383 ret = usb_autopm_get_interface(tp->intf);
1384 if (ret < 0)
1385 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001386
hayeswangb5403272014-10-09 18:00:26 +08001387 mutex_lock(&tp->control);
1388
hayeswang8ba789a2014-09-04 16:15:41 +08001389 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1390
1391 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1392 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1393 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1394
hayeswangb5403272014-10-09 18:00:26 +08001395 mutex_unlock(&tp->control);
1396
hayeswangea6a7112014-10-02 17:03:12 +08001397 usb_autopm_put_interface(tp->intf);
1398out1:
1399 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001400}
1401
Mario Limonciello9c273692018-12-11 08:16:14 -06001402/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001403 * host system provided MAC address.
1404 * Examples of this are Dell TB15 and Dell WD15 docks
1405 */
1406static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1407{
1408 acpi_status status;
1409 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1410 union acpi_object *obj;
1411 int ret = -EINVAL;
1412 u32 ocp_data;
1413 unsigned char buf[6];
Kai-Heng Feng96477222019-11-05 19:24:52 +08001414 char *mac_obj_name;
1415 acpi_object_type mac_obj_type;
1416 int mac_strlen;
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001417
Kai-Heng Feng96477222019-11-05 19:24:52 +08001418 if (test_bit(LENOVO_MACPASSTHRU, &tp->flags)) {
1419 mac_obj_name = "\\MACA";
1420 mac_obj_type = ACPI_TYPE_STRING;
1421 mac_strlen = 0x16;
Mario Limonciello9c273692018-12-11 08:16:14 -06001422 } else {
Kai-Heng Feng96477222019-11-05 19:24:52 +08001423 /* test for -AD variant of RTL8153 */
1424 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1425 if ((ocp_data & AD_MASK) == 0x1000) {
1426 /* test for MAC address pass-through bit */
1427 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1428 if ((ocp_data & PASS_THRU_MASK) != 1) {
1429 netif_dbg(tp, probe, tp->netdev,
1430 "No efuse for RTL8153-AD MAC pass through\n");
1431 return -ENODEV;
1432 }
1433 } else {
1434 /* test for RTL8153-BND and RTL8153-BD */
1435 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1436 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1437 netif_dbg(tp, probe, tp->netdev,
1438 "Invalid variant for MAC pass through\n");
1439 return -ENODEV;
1440 }
Mario Limonciello9c273692018-12-11 08:16:14 -06001441 }
Kai-Heng Feng96477222019-11-05 19:24:52 +08001442
1443 mac_obj_name = "\\_SB.AMAC";
1444 mac_obj_type = ACPI_TYPE_BUFFER;
1445 mac_strlen = 0x17;
Mario Limonciello9c273692018-12-11 08:16:14 -06001446 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001447
1448 /* returns _AUXMAC_#AABBCCDDEEFF# */
Kai-Heng Feng96477222019-11-05 19:24:52 +08001449 status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001450 obj = (union acpi_object *)buffer.pointer;
1451 if (!ACPI_SUCCESS(status))
1452 return -ENODEV;
Kai-Heng Feng96477222019-11-05 19:24:52 +08001453 if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001454 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001455 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001456 obj->type, obj->string.length);
1457 goto amacout;
1458 }
Kai-Heng Feng96477222019-11-05 19:24:52 +08001459
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001460 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1461 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1462 netif_warn(tp, probe, tp->netdev,
1463 "Invalid header when reading pass-thru MAC addr\n");
1464 goto amacout;
1465 }
1466 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1467 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1468 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001469 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1470 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001471 ret = -EINVAL;
1472 goto amacout;
1473 }
1474 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001475 netif_info(tp, probe, tp->netdev,
1476 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1477
1478amacout:
1479 kfree(obj);
1480 return ret;
1481}
1482
Mario Limonciello25766272019-04-04 13:46:53 -05001483static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1484{
1485 struct net_device *dev = tp->netdev;
1486 int ret;
1487
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001488 sa->sa_family = dev->type;
1489
Mario Limonciello25766272019-04-04 13:46:53 -05001490 if (tp->version == RTL_VER_01) {
1491 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1492 } else {
1493 /* if device doesn't support MAC pass through this will
1494 * be expected to be non-zero
1495 */
1496 ret = vendor_mac_passthru_addr_read(tp, sa);
1497 if (ret < 0)
1498 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1499 }
1500
1501 if (ret < 0) {
1502 netif_err(tp, probe, dev, "Get ether addr fail\n");
1503 } else if (!is_valid_ether_addr(sa->sa_data)) {
1504 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1505 sa->sa_data);
1506 eth_hw_addr_random(dev);
1507 ether_addr_copy(sa->sa_data, dev->dev_addr);
1508 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1509 sa->sa_data);
1510 return 0;
1511 }
1512
1513 return ret;
1514}
1515
hayeswang179bb6d2014-09-04 16:15:42 +08001516static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001517{
1518 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001519 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001520 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001521
Mario Limonciello25766272019-04-04 13:46:53 -05001522 ret = determine_ethernet_addr(tp, &sa);
1523 if (ret < 0)
1524 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001525
Mario Limonciello25766272019-04-04 13:46:53 -05001526 if (tp->version == RTL_VER_01)
1527 ether_addr_copy(dev->dev_addr, sa.sa_data);
1528 else
hayeswang179bb6d2014-09-04 16:15:42 +08001529 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001530
1531 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001532}
1533
hayeswangac718b62013-05-02 16:01:25 +00001534static void read_bulk_callback(struct urb *urb)
1535{
hayeswangac718b62013-05-02 16:01:25 +00001536 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001537 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001538 struct rx_agg *agg;
1539 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001540 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001541
hayeswangebc2ec482013-08-14 20:54:38 +08001542 agg = urb->context;
1543 if (!agg)
1544 return;
1545
1546 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001547 if (!tp)
1548 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001549
hayeswangac718b62013-05-02 16:01:25 +00001550 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1551 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001552
1553 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001554 return;
1555
hayeswangebc2ec482013-08-14 20:54:38 +08001556 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001557
1558 /* When link down, the driver would cancel all bulks. */
1559 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001560 if (!netif_carrier_ok(netdev))
1561 return;
1562
hayeswang9a4be1b2014-02-18 21:49:07 +08001563 usb_mark_last_busy(tp->udev);
1564
hayeswangac718b62013-05-02 16:01:25 +00001565 switch (status) {
1566 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001567 if (urb->actual_length < ETH_ZLEN)
1568 break;
1569
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001570 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001571 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001572 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001573 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001574 return;
hayeswangac718b62013-05-02 16:01:25 +00001575 case -ESHUTDOWN:
Hayes Wangffa9fec2019-07-04 17:36:32 +08001576 rtl_set_unplug(tp);
hayeswangac718b62013-05-02 16:01:25 +00001577 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001578 return;
hayeswangac718b62013-05-02 16:01:25 +00001579 case -ENOENT:
1580 return; /* the urb is in unlink state */
1581 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001582 if (net_ratelimit())
1583 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001584 break;
hayeswangac718b62013-05-02 16:01:25 +00001585 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001586 if (net_ratelimit())
1587 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001588 break;
hayeswangac718b62013-05-02 16:01:25 +00001589 }
1590
hayeswanga0fccd42014-11-20 10:29:05 +08001591 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001592}
1593
1594static void write_bulk_callback(struct urb *urb)
1595{
hayeswangebc2ec482013-08-14 20:54:38 +08001596 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001597 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001598 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001599 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001600 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001601 int status = urb->status;
1602
hayeswangebc2ec482013-08-14 20:54:38 +08001603 agg = urb->context;
1604 if (!agg)
1605 return;
1606
1607 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001608 if (!tp)
1609 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001610
hayeswangd104eaf2014-03-06 15:07:17 +08001611 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001612 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001613 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001614 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001615 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001616 stats->tx_errors += agg->skb_num;
1617 } else {
1618 stats->tx_packets += agg->skb_num;
1619 stats->tx_bytes += agg->skb_len;
1620 }
1621
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001622 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001623 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001624 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001625
hayeswang9a4be1b2014-02-18 21:49:07 +08001626 usb_autopm_put_interface_async(tp->intf);
1627
hayeswangd104eaf2014-03-06 15:07:17 +08001628 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001629 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001630
1631 if (!test_bit(WORK_ENABLE, &tp->flags))
1632 return;
1633
1634 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1635 return;
1636
1637 if (!skb_queue_empty(&tp->tx_queue))
Hayes Wangd2187f82019-08-19 14:40:36 +08001638 tasklet_schedule(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08001639}
1640
hayeswang40a82912013-08-14 20:54:40 +08001641static void intr_callback(struct urb *urb)
1642{
1643 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001644 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001645 int status = urb->status;
1646 int res;
1647
1648 tp = urb->context;
1649 if (!tp)
1650 return;
1651
1652 if (!test_bit(WORK_ENABLE, &tp->flags))
1653 return;
1654
1655 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1656 return;
1657
1658 switch (status) {
1659 case 0: /* success */
1660 break;
1661 case -ECONNRESET: /* unlink */
1662 case -ESHUTDOWN:
1663 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001664 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001665 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001666 case -EPROTO:
1667 netif_info(tp, intr, tp->netdev,
1668 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001669 return;
1670 case -EOVERFLOW:
1671 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1672 goto resubmit;
1673 /* -EPIPE: should clear the halt */
1674 default:
1675 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1676 goto resubmit;
1677 }
1678
1679 d = urb->transfer_buffer;
1680 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001681 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001682 set_bit(RTL8152_LINK_CHG, &tp->flags);
1683 schedule_delayed_work(&tp->schedule, 0);
1684 }
1685 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001686 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001687 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001688 set_bit(RTL8152_LINK_CHG, &tp->flags);
1689 schedule_delayed_work(&tp->schedule, 0);
1690 }
1691 }
1692
1693resubmit:
1694 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001695 if (res == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08001696 rtl_set_unplug(tp);
hayeswang40a82912013-08-14 20:54:40 +08001697 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001698 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001699 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001700 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001701 }
hayeswang40a82912013-08-14 20:54:40 +08001702}
1703
hayeswangebc2ec482013-08-14 20:54:38 +08001704static inline void *rx_agg_align(void *data)
1705{
hayeswang8e1f51b2014-01-02 11:22:41 +08001706 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001707}
1708
1709static inline void *tx_agg_align(void *data)
1710{
hayeswang8e1f51b2014-01-02 11:22:41 +08001711 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001712}
1713
Hayes Wang252df8b2019-08-13 11:42:06 +08001714static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1715{
1716 list_del(&agg->info_list);
1717
1718 usb_free_urb(agg->urb);
Hayes Wang47922fc2019-08-13 11:42:08 +08001719 put_page(agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001720 kfree(agg);
1721
1722 atomic_dec(&tp->rx_count);
1723}
1724
1725static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1726{
1727 struct net_device *netdev = tp->netdev;
1728 int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
Hayes Wangd55d7082019-08-13 11:42:07 +08001729 unsigned int order = get_order(tp->rx_buf_sz);
Hayes Wang252df8b2019-08-13 11:42:06 +08001730 struct rx_agg *rx_agg;
1731 unsigned long flags;
Hayes Wang252df8b2019-08-13 11:42:06 +08001732
1733 rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1734 if (!rx_agg)
1735 return NULL;
1736
Hayes Wang47922fc2019-08-13 11:42:08 +08001737 rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
Hayes Wangd55d7082019-08-13 11:42:07 +08001738 if (!rx_agg->page)
Hayes Wang252df8b2019-08-13 11:42:06 +08001739 goto free_rx;
1740
Hayes Wangd55d7082019-08-13 11:42:07 +08001741 rx_agg->buffer = page_address(rx_agg->page);
Hayes Wang252df8b2019-08-13 11:42:06 +08001742
1743 rx_agg->urb = usb_alloc_urb(0, mflags);
1744 if (!rx_agg->urb)
1745 goto free_buf;
1746
1747 rx_agg->context = tp;
1748
1749 INIT_LIST_HEAD(&rx_agg->list);
1750 INIT_LIST_HEAD(&rx_agg->info_list);
1751 spin_lock_irqsave(&tp->rx_lock, flags);
1752 list_add_tail(&rx_agg->info_list, &tp->rx_info);
1753 spin_unlock_irqrestore(&tp->rx_lock, flags);
1754
1755 atomic_inc(&tp->rx_count);
1756
1757 return rx_agg;
1758
1759free_buf:
Hayes Wangd55d7082019-08-13 11:42:07 +08001760 __free_pages(rx_agg->page, order);
Hayes Wang252df8b2019-08-13 11:42:06 +08001761free_rx:
1762 kfree(rx_agg);
1763 return NULL;
1764}
1765
hayeswangebc2ec482013-08-14 20:54:38 +08001766static void free_all_mem(struct r8152 *tp)
1767{
Hayes Wang252df8b2019-08-13 11:42:06 +08001768 struct rx_agg *agg, *agg_next;
1769 unsigned long flags;
hayeswangebc2ec482013-08-14 20:54:38 +08001770 int i;
1771
Hayes Wang252df8b2019-08-13 11:42:06 +08001772 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001773
Hayes Wang252df8b2019-08-13 11:42:06 +08001774 list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1775 free_rx_agg(tp, agg);
1776
1777 spin_unlock_irqrestore(&tp->rx_lock, flags);
1778
1779 WARN_ON(atomic_read(&tp->rx_count));
hayeswangebc2ec482013-08-14 20:54:38 +08001780
1781 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001782 usb_free_urb(tp->tx_info[i].urb);
1783 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001784
hayeswang9629e3c2014-01-15 10:42:15 +08001785 kfree(tp->tx_info[i].buffer);
1786 tp->tx_info[i].buffer = NULL;
1787 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001788 }
hayeswang40a82912013-08-14 20:54:40 +08001789
hayeswang9629e3c2014-01-15 10:42:15 +08001790 usb_free_urb(tp->intr_urb);
1791 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001792
hayeswang9629e3c2014-01-15 10:42:15 +08001793 kfree(tp->intr_buff);
1794 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001795}
1796
1797static int alloc_all_mem(struct r8152 *tp)
1798{
1799 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001800 struct usb_interface *intf = tp->intf;
1801 struct usb_host_interface *alt = intf->cur_altsetting;
1802 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001803 int node, i;
hayeswangebc2ec482013-08-14 20:54:38 +08001804
1805 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1806
1807 spin_lock_init(&tp->rx_lock);
1808 spin_lock_init(&tp->tx_lock);
Hayes Wang252df8b2019-08-13 11:42:06 +08001809 INIT_LIST_HEAD(&tp->rx_info);
hayeswangebc2ec482013-08-14 20:54:38 +08001810 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001811 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001812 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001813 skb_queue_head_init(&tp->rx_queue);
Hayes Wang252df8b2019-08-13 11:42:06 +08001814 atomic_set(&tp->rx_count, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001815
1816 for (i = 0; i < RTL8152_MAX_RX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001817 if (!alloc_rx_agg(tp, GFP_KERNEL))
hayeswangebc2ec482013-08-14 20:54:38 +08001818 goto err1;
hayeswangebc2ec482013-08-14 20:54:38 +08001819 }
1820
1821 for (i = 0; i < RTL8152_MAX_TX; i++) {
Hayes Wang252df8b2019-08-13 11:42:06 +08001822 struct urb *urb;
1823 u8 *buf;
1824
hayeswang52aec122014-09-02 10:27:52 +08001825 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001826 if (!buf)
1827 goto err1;
1828
1829 if (buf != tx_agg_align(buf)) {
1830 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001831 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001832 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001833 if (!buf)
1834 goto err1;
1835 }
1836
1837 urb = usb_alloc_urb(0, GFP_KERNEL);
1838 if (!urb) {
1839 kfree(buf);
1840 goto err1;
1841 }
1842
1843 INIT_LIST_HEAD(&tp->tx_info[i].list);
1844 tp->tx_info[i].context = tp;
1845 tp->tx_info[i].urb = urb;
1846 tp->tx_info[i].buffer = buf;
1847 tp->tx_info[i].head = tx_agg_align(buf);
1848
1849 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1850 }
1851
hayeswang40a82912013-08-14 20:54:40 +08001852 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1853 if (!tp->intr_urb)
1854 goto err1;
1855
1856 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1857 if (!tp->intr_buff)
1858 goto err1;
1859
1860 tp->intr_interval = (int)ep_intr->desc.bInterval;
1861 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001862 tp->intr_buff, INTBUFSIZE, intr_callback,
1863 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001864
hayeswangebc2ec482013-08-14 20:54:38 +08001865 return 0;
1866
1867err1:
1868 free_all_mem(tp);
1869 return -ENOMEM;
1870}
1871
hayeswang0de98f62013-08-16 16:09:35 +08001872static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1873{
1874 struct tx_agg *agg = NULL;
1875 unsigned long flags;
1876
hayeswang21949ab2014-03-07 11:04:35 +08001877 if (list_empty(&tp->tx_free))
1878 return NULL;
1879
hayeswang0de98f62013-08-16 16:09:35 +08001880 spin_lock_irqsave(&tp->tx_lock, flags);
1881 if (!list_empty(&tp->tx_free)) {
1882 struct list_head *cursor;
1883
1884 cursor = tp->tx_free.next;
1885 list_del_init(cursor);
1886 agg = list_entry(cursor, struct tx_agg, list);
1887 }
1888 spin_unlock_irqrestore(&tp->tx_lock, flags);
1889
1890 return agg;
1891}
1892
hayeswangb209af92014-08-25 15:53:00 +08001893/* r8152_csum_workaround()
Prashant Malanic01ebd62019-09-30 12:38:18 -07001894 * The hw limits the value of the transport offset. When the offset is out of
hayeswang6128d1bb2014-03-07 11:04:40 +08001895 * range, calculate the checksum by sw.
1896 */
1897static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1898 struct sk_buff_head *list)
1899{
1900 if (skb_shinfo(skb)->gso_size) {
1901 netdev_features_t features = tp->netdev->features;
1902 struct sk_buff_head seg_list;
1903 struct sk_buff *segs, *nskb;
1904
hayeswanga91d45f2014-07-11 16:48:27 +08001905 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001906 segs = skb_gso_segment(skb, features);
1907 if (IS_ERR(segs) || !segs)
1908 goto drop;
1909
1910 __skb_queue_head_init(&seg_list);
1911
1912 do {
1913 nskb = segs;
1914 segs = segs->next;
1915 nskb->next = NULL;
1916 __skb_queue_tail(&seg_list, nskb);
1917 } while (segs);
1918
1919 skb_queue_splice(&seg_list, list);
1920 dev_kfree_skb(skb);
1921 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1922 if (skb_checksum_help(skb) < 0)
1923 goto drop;
1924
1925 __skb_queue_head(list, skb);
1926 } else {
1927 struct net_device_stats *stats;
1928
1929drop:
1930 stats = &tp->netdev->stats;
1931 stats->tx_dropped++;
1932 dev_kfree_skb(skb);
1933 }
1934}
1935
hayeswangb209af92014-08-25 15:53:00 +08001936/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001937 * According to the document of microsoft, the TCP Pseudo Header excludes the
1938 * packet length for IPv6 TCP large packets.
1939 */
1940static int msdn_giant_send_check(struct sk_buff *skb)
1941{
1942 const struct ipv6hdr *ipv6h;
1943 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001944 int ret;
1945
1946 ret = skb_cow_head(skb, 0);
1947 if (ret)
1948 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001949
1950 ipv6h = ipv6_hdr(skb);
1951 th = tcp_hdr(skb);
1952
1953 th->check = 0;
1954 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1955
hayeswangfcb308d2014-03-11 10:20:32 +08001956 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001957}
1958
hayeswangc5554292014-09-12 10:43:11 +08001959static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1960{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001961 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001962 u32 opts2;
1963
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001964 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001965 desc->opts2 |= cpu_to_le32(opts2);
1966 }
1967}
1968
1969static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1970{
1971 u32 opts2 = le32_to_cpu(desc->opts2);
1972
1973 if (opts2 & RX_VLAN_TAG)
1974 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1975 swab16(opts2 & 0xffff));
1976}
1977
hayeswang60c89072014-03-07 11:04:39 +08001978static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1979 struct sk_buff *skb, u32 len, u32 transport_offset)
1980{
1981 u32 mss = skb_shinfo(skb)->gso_size;
1982 u32 opts1, opts2 = 0;
1983 int ret = TX_CSUM_SUCCESS;
1984
1985 WARN_ON_ONCE(len > TX_LEN_MAX);
1986
1987 opts1 = len | TX_FS | TX_LS;
1988
1989 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001990 if (transport_offset > GTTCPHO_MAX) {
1991 netif_warn(tp, tx_err, tp->netdev,
1992 "Invalid transport offset 0x%x for TSO\n",
1993 transport_offset);
1994 ret = TX_CSUM_TSO;
1995 goto unavailable;
1996 }
1997
hayeswang6e74d172015-02-06 11:30:50 +08001998 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001999 case htons(ETH_P_IP):
2000 opts1 |= GTSENDV4;
2001 break;
2002
hayeswang6128d1bb2014-03-07 11:04:40 +08002003 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08002004 if (msdn_giant_send_check(skb)) {
2005 ret = TX_CSUM_TSO;
2006 goto unavailable;
2007 }
hayeswang6128d1bb2014-03-07 11:04:40 +08002008 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08002009 break;
2010
hayeswang60c89072014-03-07 11:04:39 +08002011 default:
2012 WARN_ON_ONCE(1);
2013 break;
2014 }
2015
2016 opts1 |= transport_offset << GTTCPHO_SHIFT;
2017 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2018 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08002019 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08002020
hayeswang6128d1bb2014-03-07 11:04:40 +08002021 if (transport_offset > TCPHO_MAX) {
2022 netif_warn(tp, tx_err, tp->netdev,
2023 "Invalid transport offset 0x%x\n",
2024 transport_offset);
2025 ret = TX_CSUM_NONE;
2026 goto unavailable;
2027 }
2028
hayeswang6e74d172015-02-06 11:30:50 +08002029 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08002030 case htons(ETH_P_IP):
2031 opts2 |= IPV4_CS;
2032 ip_protocol = ip_hdr(skb)->protocol;
2033 break;
2034
2035 case htons(ETH_P_IPV6):
2036 opts2 |= IPV6_CS;
2037 ip_protocol = ipv6_hdr(skb)->nexthdr;
2038 break;
2039
2040 default:
2041 ip_protocol = IPPROTO_RAW;
2042 break;
2043 }
2044
hayeswang60c89072014-03-07 11:04:39 +08002045 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08002046 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08002047 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08002048 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08002049 else
hayeswang5bd23882013-08-14 20:54:39 +08002050 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08002051
hayeswang60c89072014-03-07 11:04:39 +08002052 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08002053 }
hayeswang60c89072014-03-07 11:04:39 +08002054
2055 desc->opts2 = cpu_to_le32(opts2);
2056 desc->opts1 = cpu_to_le32(opts1);
2057
hayeswang6128d1bb2014-03-07 11:04:40 +08002058unavailable:
hayeswang60c89072014-03-07 11:04:39 +08002059 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08002060}
2061
hayeswangb1379d92013-08-16 16:09:37 +08002062static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2063{
hayeswangd84130a2014-02-18 21:49:02 +08002064 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08002065 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08002066 u8 *tx_data;
2067
hayeswangd84130a2014-02-18 21:49:02 +08002068 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08002069 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002070 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08002071 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002072
hayeswangb1379d92013-08-16 16:09:37 +08002073 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08002074 agg->skb_num = 0;
2075 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08002076 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08002077
hayeswang7937f9e2013-11-20 17:30:54 +08002078 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08002079 struct tx_desc *tx_desc;
2080 struct sk_buff *skb;
2081 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08002082 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08002083
hayeswangd84130a2014-02-18 21:49:02 +08002084 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08002085 if (!skb)
2086 break;
2087
hayeswang60c89072014-03-07 11:04:39 +08002088 len = skb->len + sizeof(*tx_desc);
2089
2090 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08002091 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08002092 break;
2093 }
2094
hayeswang7937f9e2013-11-20 17:30:54 +08002095 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08002096 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08002097
2098 offset = (u32)skb_transport_offset(skb);
2099
hayeswang6128d1bb2014-03-07 11:04:40 +08002100 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
2101 r8152_csum_workaround(tp, skb, &skb_head);
2102 continue;
2103 }
hayeswang60c89072014-03-07 11:04:39 +08002104
hayeswangc5554292014-09-12 10:43:11 +08002105 rtl_tx_vlan_tag(tx_desc, skb);
2106
hayeswangb1379d92013-08-16 16:09:37 +08002107 tx_data += sizeof(*tx_desc);
2108
hayeswang60c89072014-03-07 11:04:39 +08002109 len = skb->len;
2110 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2111 struct net_device_stats *stats = &tp->netdev->stats;
2112
2113 stats->tx_dropped++;
2114 dev_kfree_skb_any(skb);
2115 tx_data -= sizeof(*tx_desc);
2116 continue;
2117 }
hayeswangb1379d92013-08-16 16:09:37 +08002118
hayeswang7937f9e2013-11-20 17:30:54 +08002119 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08002120 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08002121 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08002122
2123 dev_kfree_skb_any(skb);
2124
hayeswang52aec122014-09-02 10:27:52 +08002125 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08002126
2127 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
2128 break;
hayeswangb1379d92013-08-16 16:09:37 +08002129 }
2130
hayeswangd84130a2014-02-18 21:49:02 +08002131 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08002132 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002133 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08002134 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002135 }
2136
hayeswang0c3121f2014-03-07 11:04:36 +08002137 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08002138
2139 if (netif_queue_stopped(tp->netdev) &&
2140 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2141 netif_wake_queue(tp->netdev);
2142
hayeswang0c3121f2014-03-07 11:04:36 +08002143 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08002144
hayeswang0c3121f2014-03-07 11:04:36 +08002145 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08002146 if (ret < 0)
2147 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08002148
hayeswangb1379d92013-08-16 16:09:37 +08002149 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
2150 agg->head, (int)(tx_data - (u8 *)agg->head),
2151 (usb_complete_t)write_bulk_callback, agg);
2152
hayeswang0c3121f2014-03-07 11:04:36 +08002153 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08002154 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08002155 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08002156
2157out_tx_fill:
2158 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08002159}
2160
hayeswang565cab02014-03-07 11:04:38 +08002161static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2162{
2163 u8 checksum = CHECKSUM_NONE;
2164 u32 opts2, opts3;
2165
hayeswang19c0f402017-01-11 16:25:34 +08002166 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08002167 goto return_result;
2168
2169 opts2 = le32_to_cpu(rx_desc->opts2);
2170 opts3 = le32_to_cpu(rx_desc->opts3);
2171
2172 if (opts2 & RD_IPV4_CS) {
2173 if (opts3 & IPF)
2174 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08002175 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2176 checksum = CHECKSUM_UNNECESSARY;
2177 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08002178 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04002179 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08002180 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2181 checksum = CHECKSUM_UNNECESSARY;
2182 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2183 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08002184 }
2185
2186return_result:
2187 return checksum;
2188}
2189
Hayes Wang47922fc2019-08-13 11:42:08 +08002190static inline bool rx_count_exceed(struct r8152 *tp)
2191{
2192 return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2193}
2194
2195static inline int agg_offset(struct rx_agg *agg, void *addr)
2196{
2197 return (int)(addr - agg->buffer);
2198}
2199
2200static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2201{
2202 struct rx_agg *agg, *agg_next, *agg_free = NULL;
2203 unsigned long flags;
2204
2205 spin_lock_irqsave(&tp->rx_lock, flags);
2206
2207 list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2208 if (page_count(agg->page) == 1) {
2209 if (!agg_free) {
2210 list_del_init(&agg->list);
2211 agg_free = agg;
2212 continue;
2213 }
2214 if (rx_count_exceed(tp)) {
2215 list_del_init(&agg->list);
2216 free_rx_agg(tp, agg);
2217 }
2218 break;
2219 }
2220 }
2221
2222 spin_unlock_irqrestore(&tp->rx_lock, flags);
2223
Hayes Wange4a50172019-08-13 11:42:09 +08002224 if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
Hayes Wang47922fc2019-08-13 11:42:08 +08002225 agg_free = alloc_rx_agg(tp, mflags);
2226
2227 return agg_free;
2228}
2229
hayeswangd823ab62015-01-12 12:06:23 +08002230static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08002231{
hayeswanga5a4f462013-08-16 16:09:34 +08002232 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08002233 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08002234 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08002235 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08002236
2237 if (!skb_queue_empty(&tp->rx_queue)) {
2238 while (work_done < budget) {
2239 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2240 struct net_device *netdev = tp->netdev;
2241 struct net_device_stats *stats = &netdev->stats;
2242 unsigned int pkt_len;
2243
2244 if (!skb)
2245 break;
2246
2247 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08002248 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002249 work_done++;
2250 stats->rx_packets++;
2251 stats->rx_bytes += pkt_len;
2252 }
2253 }
hayeswangebc2ec482013-08-14 20:54:38 +08002254
hayeswangd84130a2014-02-18 21:49:02 +08002255 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08002256 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08002257
2258 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08002259 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08002260 list_splice_init(&tp->rx_done, &rx_queue);
2261 spin_unlock_irqrestore(&tp->rx_lock, flags);
2262
2263 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08002264 struct rx_desc *rx_desc;
Hayes Wang47922fc2019-08-13 11:42:08 +08002265 struct rx_agg *agg, *agg_free;
hayeswang43a44782013-08-16 16:09:36 +08002266 int len_used = 0;
2267 struct urb *urb;
2268 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08002269
hayeswangebc2ec482013-08-14 20:54:38 +08002270 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08002271
2272 agg = list_entry(cursor, struct rx_agg, list);
2273 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08002274 if (urb->actual_length < ETH_ZLEN)
2275 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08002276
Hayes Wang47922fc2019-08-13 11:42:08 +08002277 agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2278
Hayes Wangd55d7082019-08-13 11:42:07 +08002279 rx_desc = agg->buffer;
2280 rx_data = agg->buffer;
hayeswang7937f9e2013-11-20 17:30:54 +08002281 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002282
hayeswang7937f9e2013-11-20 17:30:54 +08002283 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08002284 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08002285 struct net_device_stats *stats = &netdev->stats;
Hayes Wang47922fc2019-08-13 11:42:08 +08002286 unsigned int pkt_len, rx_frag_head_sz;
hayeswang43a44782013-08-16 16:09:36 +08002287 struct sk_buff *skb;
2288
hayeswang74544452017-06-09 17:11:47 +08002289 /* limite the skb numbers for rx_queue */
2290 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2291 break;
2292
hayeswang7937f9e2013-11-20 17:30:54 +08002293 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08002294 if (pkt_len < ETH_ZLEN)
2295 break;
2296
hayeswang7937f9e2013-11-20 17:30:54 +08002297 len_used += pkt_len;
2298 if (urb->actual_length < len_used)
2299 break;
2300
hayeswangb65c0c92017-06-21 11:25:18 +08002301 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08002302 rx_data += sizeof(struct rx_desc);
2303
Hayes Wange4a50172019-08-13 11:42:09 +08002304 if (!agg_free || tp->rx_copybreak > pkt_len)
Hayes Wang47922fc2019-08-13 11:42:08 +08002305 rx_frag_head_sz = pkt_len;
2306 else
Hayes Wange4a50172019-08-13 11:42:09 +08002307 rx_frag_head_sz = tp->rx_copybreak;
Hayes Wang47922fc2019-08-13 11:42:08 +08002308
2309 skb = napi_alloc_skb(napi, rx_frag_head_sz);
hayeswangebc2ec482013-08-14 20:54:38 +08002310 if (!skb) {
2311 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08002312 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08002313 }
hayeswang565cab02014-03-07 11:04:38 +08002314
2315 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
Hayes Wang47922fc2019-08-13 11:42:08 +08002316 memcpy(skb->data, rx_data, rx_frag_head_sz);
2317 skb_put(skb, rx_frag_head_sz);
2318 pkt_len -= rx_frag_head_sz;
2319 rx_data += rx_frag_head_sz;
2320 if (pkt_len) {
2321 skb_add_rx_frag(skb, 0, agg->page,
2322 agg_offset(agg, rx_data),
2323 pkt_len,
2324 SKB_DATA_ALIGN(pkt_len));
2325 get_page(agg->page);
2326 }
2327
hayeswangebc2ec482013-08-14 20:54:38 +08002328 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08002329 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002330 if (work_done < budget) {
hayeswangd823ab62015-01-12 12:06:23 +08002331 work_done++;
2332 stats->rx_packets++;
Hayes Wang47922fc2019-08-13 11:42:08 +08002333 stats->rx_bytes += skb->len;
Hayes Wang6636fb32019-08-19 11:15:19 +08002334 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08002335 } else {
2336 __skb_queue_tail(&tp->rx_queue, skb);
2337 }
hayeswangebc2ec482013-08-14 20:54:38 +08002338
hayeswang5e2f7482014-03-07 11:04:37 +08002339find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08002340 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08002341 rx_desc = (struct rx_desc *)rx_data;
Hayes Wang47922fc2019-08-13 11:42:08 +08002342 len_used = agg_offset(agg, rx_data);
hayeswang7937f9e2013-11-20 17:30:54 +08002343 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08002344 }
2345
Hayes Wang47922fc2019-08-13 11:42:08 +08002346 WARN_ON(!agg_free && page_count(agg->page) > 1);
2347
2348 if (agg_free) {
2349 spin_lock_irqsave(&tp->rx_lock, flags);
2350 if (page_count(agg->page) == 1) {
2351 list_add(&agg_free->list, &tp->rx_used);
2352 } else {
2353 list_add_tail(&agg->list, &tp->rx_used);
2354 agg = agg_free;
2355 urb = agg->urb;
2356 }
2357 spin_unlock_irqrestore(&tp->rx_lock, flags);
2358 }
2359
hayeswang0de98f62013-08-16 16:09:35 +08002360submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002361 if (!ret) {
2362 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2363 } else {
2364 urb->actual_length = 0;
2365 list_add_tail(&agg->list, next);
2366 }
2367 }
2368
2369 if (!list_empty(&rx_queue)) {
2370 spin_lock_irqsave(&tp->rx_lock, flags);
2371 list_splice_tail(&rx_queue, &tp->rx_done);
2372 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002373 }
hayeswangd823ab62015-01-12 12:06:23 +08002374
2375out1:
2376 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002377}
2378
2379static void tx_bottom(struct r8152 *tp)
2380{
hayeswangebc2ec482013-08-14 20:54:38 +08002381 int res;
2382
hayeswangb1379d92013-08-16 16:09:37 +08002383 do {
Prashant Malanic01ebd62019-09-30 12:38:18 -07002384 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002385 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002386
hayeswangb1379d92013-08-16 16:09:37 +08002387 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002388 break;
2389
hayeswangb1379d92013-08-16 16:09:37 +08002390 agg = r8152_get_tx_agg(tp);
2391 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002392 break;
hayeswangb1379d92013-08-16 16:09:37 +08002393
2394 res = r8152_tx_agg_fill(tp, agg);
Prashant Malanic01ebd62019-09-30 12:38:18 -07002395 if (!res)
2396 continue;
hayeswangb1379d92013-08-16 16:09:37 +08002397
Prashant Malanic01ebd62019-09-30 12:38:18 -07002398 if (res == -ENODEV) {
2399 rtl_set_unplug(tp);
2400 netif_device_detach(netdev);
2401 } else {
2402 struct net_device_stats *stats = &netdev->stats;
2403 unsigned long flags;
hayeswang05e0f1a2014-03-06 15:07:18 +08002404
Prashant Malanic01ebd62019-09-30 12:38:18 -07002405 netif_warn(tp, tx_err, netdev,
2406 "failed tx_urb %d\n", res);
2407 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002408
Prashant Malanic01ebd62019-09-30 12:38:18 -07002409 spin_lock_irqsave(&tp->tx_lock, flags);
2410 list_add_tail(&agg->list, &tp->tx_free);
2411 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002412 }
hayeswangb1379d92013-08-16 16:09:37 +08002413 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002414}
2415
Hayes Wangd2187f82019-08-19 14:40:36 +08002416static void bottom_half(unsigned long data)
hayeswangebc2ec482013-08-14 20:54:38 +08002417{
Hayes Wangd2187f82019-08-19 14:40:36 +08002418 struct r8152 *tp;
2419
2420 tp = (struct r8152 *)data;
2421
hayeswangebc2ec482013-08-14 20:54:38 +08002422 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2423 return;
2424
2425 if (!test_bit(WORK_ENABLE, &tp->flags))
2426 return;
2427
hayeswang7559fb2f2013-08-16 16:09:38 +08002428 /* When link down, the driver would cancel all bulks. */
2429 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002430 if (!netif_carrier_ok(tp->netdev))
2431 return;
2432
Hayes Wangd2187f82019-08-19 14:40:36 +08002433 clear_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002434
hayeswang0c3121f2014-03-07 11:04:36 +08002435 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002436}
2437
hayeswangd823ab62015-01-12 12:06:23 +08002438static int r8152_poll(struct napi_struct *napi, int budget)
2439{
2440 struct r8152 *tp = container_of(napi, struct r8152, napi);
2441 int work_done;
2442
2443 work_done = rx_bottom(tp, budget);
hayeswangd823ab62015-01-12 12:06:23 +08002444
2445 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002446 if (!napi_complete_done(napi, work_done))
2447 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002448 if (!list_empty(&tp->rx_done))
2449 napi_schedule(napi);
2450 }
2451
hayeswanga3307f92017-06-09 17:11:48 +08002452out:
hayeswangd823ab62015-01-12 12:06:23 +08002453 return work_done;
2454}
2455
hayeswangebc2ec482013-08-14 20:54:38 +08002456static
2457int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2458{
hayeswanga0fccd42014-11-20 10:29:05 +08002459 int ret;
2460
hayeswangef827a52015-01-09 10:26:36 +08002461 /* The rx would be stopped, so skip submitting */
2462 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2463 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2464 return 0;
2465
hayeswangebc2ec482013-08-14 20:54:38 +08002466 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
Hayes Wangd55d7082019-08-13 11:42:07 +08002467 agg->buffer, tp->rx_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002468 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002469
hayeswanga0fccd42014-11-20 10:29:05 +08002470 ret = usb_submit_urb(agg->urb, mem_flags);
2471 if (ret == -ENODEV) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08002472 rtl_set_unplug(tp);
hayeswanga0fccd42014-11-20 10:29:05 +08002473 netif_device_detach(tp->netdev);
2474 } else if (ret) {
2475 struct urb *urb = agg->urb;
2476 unsigned long flags;
2477
2478 urb->actual_length = 0;
2479 spin_lock_irqsave(&tp->rx_lock, flags);
2480 list_add_tail(&agg->list, &tp->rx_done);
2481 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002482
2483 netif_err(tp, rx_err, tp->netdev,
2484 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2485
2486 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002487 }
2488
2489 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002490}
2491
hayeswang00a5e362014-02-18 21:48:59 +08002492static void rtl_drop_queued_tx(struct r8152 *tp)
2493{
2494 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002495 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002496 struct sk_buff *skb;
2497
hayeswangd84130a2014-02-18 21:49:02 +08002498 if (skb_queue_empty(tx_queue))
2499 return;
2500
2501 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002502 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002503 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002504 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002505
2506 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002507 dev_kfree_skb(skb);
2508 stats->tx_dropped++;
2509 }
2510}
2511
hayeswangac718b62013-05-02 16:01:25 +00002512static void rtl8152_tx_timeout(struct net_device *netdev)
2513{
2514 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002515
Hayes Wang4a8deae2014-01-07 11:18:22 +08002516 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002517
2518 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002519}
2520
2521static void rtl8152_set_rx_mode(struct net_device *netdev)
2522{
2523 struct r8152 *tp = netdev_priv(netdev);
2524
hayeswang51d979f2015-02-06 11:30:47 +08002525 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002526 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002527 schedule_delayed_work(&tp->schedule, 0);
2528 }
hayeswangac718b62013-05-02 16:01:25 +00002529}
2530
2531static void _rtl8152_set_rx_mode(struct net_device *netdev)
2532{
2533 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002534 u32 mc_filter[2]; /* Multicast hash filter */
2535 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002536 u32 ocp_data;
2537
hayeswangac718b62013-05-02 16:01:25 +00002538 netif_stop_queue(netdev);
2539 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2540 ocp_data &= ~RCR_ACPT_ALL;
2541 ocp_data |= RCR_AB | RCR_APM;
2542
2543 if (netdev->flags & IFF_PROMISC) {
2544 /* Unconditionally log net taps. */
2545 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2546 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002547 mc_filter[1] = 0xffffffff;
2548 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002549 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2550 (netdev->flags & IFF_ALLMULTI)) {
2551 /* Too many to filter perfectly -- accept all multicasts. */
2552 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002553 mc_filter[1] = 0xffffffff;
2554 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002555 } else {
2556 struct netdev_hw_addr *ha;
2557
hayeswangb209af92014-08-25 15:53:00 +08002558 mc_filter[1] = 0;
2559 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002560 netdev_for_each_mc_addr(ha, netdev) {
2561 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002562
hayeswangac718b62013-05-02 16:01:25 +00002563 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2564 ocp_data |= RCR_AM;
2565 }
2566 }
2567
hayeswang31787f52013-07-31 17:21:25 +08002568 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2569 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002570
hayeswang31787f52013-07-31 17:21:25 +08002571 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002572 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2573 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002574}
2575
hayeswanga5e31252015-01-06 17:41:58 +08002576static netdev_features_t
2577rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2578 netdev_features_t features)
2579{
2580 u32 mss = skb_shinfo(skb)->gso_size;
2581 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2582 int offset = skb_transport_offset(skb);
2583
2584 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002585 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002586 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2587 features &= ~NETIF_F_GSO_MASK;
2588
2589 return features;
2590}
2591
hayeswangac718b62013-05-02 16:01:25 +00002592static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002593 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002594{
2595 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002596
hayeswangac718b62013-05-02 16:01:25 +00002597 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002598
hayeswang61598782013-11-20 17:30:55 +08002599 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002600
hayeswang0c3121f2014-03-07 11:04:36 +08002601 if (!list_empty(&tp->tx_free)) {
2602 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
Hayes Wangd2187f82019-08-19 14:40:36 +08002603 set_bit(SCHEDULE_TASKLET, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002604 schedule_delayed_work(&tp->schedule, 0);
2605 } else {
2606 usb_mark_last_busy(tp->udev);
Hayes Wangd2187f82019-08-19 14:40:36 +08002607 tasklet_schedule(&tp->tx_tl);
hayeswang0c3121f2014-03-07 11:04:36 +08002608 }
hayeswangb209af92014-08-25 15:53:00 +08002609 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002610 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002611 }
hayeswangdd1b1192013-11-20 17:30:56 +08002612
hayeswangac718b62013-05-02 16:01:25 +00002613 return NETDEV_TX_OK;
2614}
2615
2616static void r8152b_reset_packet_filter(struct r8152 *tp)
2617{
2618 u32 ocp_data;
2619
2620 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2621 ocp_data &= ~FMC_FCR_MCU_EN;
2622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2623 ocp_data |= FMC_FCR_MCU_EN;
2624 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2625}
2626
2627static void rtl8152_nic_reset(struct r8152 *tp)
2628{
2629 int i;
2630
2631 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2632
2633 for (i = 0; i < 1000; i++) {
2634 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2635 break;
hayeswangb209af92014-08-25 15:53:00 +08002636 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002637 }
2638}
2639
hayeswangdd1b1192013-11-20 17:30:56 +08002640static void set_tx_qlen(struct r8152 *tp)
2641{
2642 struct net_device *netdev = tp->netdev;
2643
hayeswangb65c0c92017-06-21 11:25:18 +08002644 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002645 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002646}
2647
hayeswangac718b62013-05-02 16:01:25 +00002648static inline u8 rtl8152_get_speed(struct r8152 *tp)
2649{
2650 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2651}
2652
hayeswang507605a2014-01-02 11:22:43 +08002653static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002654{
hayeswangebc2ec482013-08-14 20:54:38 +08002655 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002656 u8 speed;
2657
2658 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002659 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002660 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002661 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002662 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2663 } else {
2664 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002665 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002666 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2667 }
hayeswang507605a2014-01-02 11:22:43 +08002668}
2669
hayeswang00a5e362014-02-18 21:48:59 +08002670static void rxdy_gated_en(struct r8152 *tp, bool enable)
2671{
2672 u32 ocp_data;
2673
2674 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2675 if (enable)
2676 ocp_data |= RXDY_GATED_EN;
2677 else
2678 ocp_data &= ~RXDY_GATED_EN;
2679 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2680}
2681
hayeswang445f7f42014-09-23 16:31:47 +08002682static int rtl_start_rx(struct r8152 *tp)
2683{
Hayes Wang252df8b2019-08-13 11:42:06 +08002684 struct rx_agg *agg, *agg_next;
2685 struct list_head tmp_list;
2686 unsigned long flags;
Hayes Wang47922fc2019-08-13 11:42:08 +08002687 int ret = 0, i = 0;
Hayes Wang252df8b2019-08-13 11:42:06 +08002688
2689 INIT_LIST_HEAD(&tmp_list);
2690
2691 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002692
2693 INIT_LIST_HEAD(&tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002694 INIT_LIST_HEAD(&tp->rx_used);
Hayes Wang252df8b2019-08-13 11:42:06 +08002695
2696 list_splice_init(&tp->rx_info, &tmp_list);
2697
2698 spin_unlock_irqrestore(&tp->rx_lock, flags);
2699
2700 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2701 INIT_LIST_HEAD(&agg->list);
2702
Hayes Wang47922fc2019-08-13 11:42:08 +08002703 /* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2704 if (++i > RTL8152_MAX_RX) {
2705 spin_lock_irqsave(&tp->rx_lock, flags);
2706 list_add_tail(&agg->list, &tp->rx_used);
2707 spin_unlock_irqrestore(&tp->rx_lock, flags);
2708 } else if (unlikely(ret < 0)) {
2709 spin_lock_irqsave(&tp->rx_lock, flags);
Hayes Wang252df8b2019-08-13 11:42:06 +08002710 list_add_tail(&agg->list, &tp->rx_done);
Hayes Wang47922fc2019-08-13 11:42:08 +08002711 spin_unlock_irqrestore(&tp->rx_lock, flags);
2712 } else {
Hayes Wang252df8b2019-08-13 11:42:06 +08002713 ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
Hayes Wang47922fc2019-08-13 11:42:08 +08002714 }
hayeswang445f7f42014-09-23 16:31:47 +08002715 }
2716
Hayes Wang252df8b2019-08-13 11:42:06 +08002717 spin_lock_irqsave(&tp->rx_lock, flags);
2718 WARN_ON(!list_empty(&tp->rx_info));
2719 list_splice(&tmp_list, &tp->rx_info);
2720 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang7bcf4f62014-11-20 10:29:06 +08002721
hayeswang445f7f42014-09-23 16:31:47 +08002722 return ret;
2723}
2724
2725static int rtl_stop_rx(struct r8152 *tp)
2726{
Hayes Wang252df8b2019-08-13 11:42:06 +08002727 struct rx_agg *agg, *agg_next;
2728 struct list_head tmp_list;
2729 unsigned long flags;
hayeswang445f7f42014-09-23 16:31:47 +08002730
Hayes Wang252df8b2019-08-13 11:42:06 +08002731 INIT_LIST_HEAD(&tmp_list);
2732
2733 /* The usb_kill_urb() couldn't be used in atomic.
2734 * Therefore, move the list of rx_info to a tmp one.
2735 * Then, list_for_each_entry_safe could be used without
2736 * spin lock.
2737 */
2738
2739 spin_lock_irqsave(&tp->rx_lock, flags);
2740 list_splice_init(&tp->rx_info, &tmp_list);
2741 spin_unlock_irqrestore(&tp->rx_lock, flags);
2742
Hayes Wang47922fc2019-08-13 11:42:08 +08002743 list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2744 /* At least RTL8152_MAX_RX rx_agg have the page_count being
2745 * equal to 1, so the other ones could be freed safely.
2746 */
2747 if (page_count(agg->page) > 1)
2748 free_rx_agg(tp, agg);
2749 else
2750 usb_kill_urb(agg->urb);
2751 }
Hayes Wang252df8b2019-08-13 11:42:06 +08002752
2753 /* Move back the list of temp to the rx_info */
2754 spin_lock_irqsave(&tp->rx_lock, flags);
2755 WARN_ON(!list_empty(&tp->rx_info));
2756 list_splice(&tmp_list, &tp->rx_info);
2757 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswang445f7f42014-09-23 16:31:47 +08002758
hayeswangd823ab62015-01-12 12:06:23 +08002759 while (!skb_queue_empty(&tp->rx_queue))
2760 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2761
hayeswang445f7f42014-09-23 16:31:47 +08002762 return 0;
2763}
2764
Hayes Wang9fae5412019-07-03 15:11:56 +08002765static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2766{
2767 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2768 OWN_UPDATE | OWN_CLEAR);
2769}
2770
hayeswang507605a2014-01-02 11:22:43 +08002771static int rtl_enable(struct r8152 *tp)
2772{
2773 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002774
2775 r8152b_reset_packet_filter(tp);
2776
2777 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2778 ocp_data |= CR_RE | CR_TE;
2779 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2780
Hayes Wang9fae5412019-07-03 15:11:56 +08002781 switch (tp->version) {
2782 case RTL_VER_08:
2783 case RTL_VER_09:
2784 r8153b_rx_agg_chg_indicate(tp);
2785 break;
2786 default:
2787 break;
2788 }
2789
hayeswang00a5e362014-02-18 21:48:59 +08002790 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002791
hayeswangaa2e0922015-01-09 10:26:35 +08002792 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002793}
2794
hayeswang507605a2014-01-02 11:22:43 +08002795static int rtl8152_enable(struct r8152 *tp)
2796{
hayeswang68714382014-04-11 17:54:31 +08002797 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2798 return -ENODEV;
2799
hayeswang507605a2014-01-02 11:22:43 +08002800 set_tx_qlen(tp);
2801 rtl_set_eee_plus(tp);
2802
2803 return rtl_enable(tp);
2804}
2805
hayeswang464ec102015-02-12 14:33:46 +08002806static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002807{
hayeswang464ec102015-02-12 14:33:46 +08002808 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002809
hayeswang65b82d62017-06-15 14:44:03 +08002810 switch (tp->version) {
2811 case RTL_VER_03:
2812 case RTL_VER_04:
2813 case RTL_VER_05:
2814 case RTL_VER_06:
2815 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2816 ocp_data);
2817 break;
2818
2819 case RTL_VER_08:
2820 case RTL_VER_09:
2821 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2822 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2823 */
2824 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2825 128 / 8);
2826 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2827 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002828 break;
2829
2830 default:
2831 break;
2832 }
hayeswang464ec102015-02-12 14:33:46 +08002833}
2834
2835static void r8153_set_rx_early_size(struct r8152 *tp)
2836{
Hayes Wangec5791c2019-08-13 11:42:05 +08002837 u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002838
hayeswang65b82d62017-06-15 14:44:03 +08002839 switch (tp->version) {
2840 case RTL_VER_03:
2841 case RTL_VER_04:
2842 case RTL_VER_05:
2843 case RTL_VER_06:
2844 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2845 ocp_data / 4);
2846 break;
2847 case RTL_VER_08:
2848 case RTL_VER_09:
2849 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2850 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002851 break;
2852 default:
2853 WARN_ON_ONCE(1);
2854 break;
2855 }
hayeswang43779f82014-01-02 11:25:10 +08002856}
2857
2858static int rtl8153_enable(struct r8152 *tp)
2859{
hayeswang68714382014-04-11 17:54:31 +08002860 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2861 return -ENODEV;
2862
hayeswang43779f82014-01-02 11:25:10 +08002863 set_tx_qlen(tp);
2864 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002865 r8153_set_rx_early_timeout(tp);
2866 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002867
Hayes Wangf99cd202020-01-22 16:02:06 +08002868 if (tp->version == RTL_VER_09) {
2869 u32 ocp_data;
2870
2871 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
2872 ocp_data &= ~FC_PATCH_TASK;
2873 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
2874 usleep_range(1000, 2000);
2875 ocp_data |= FC_PATCH_TASK;
2876 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
2877 }
2878
hayeswang43779f82014-01-02 11:25:10 +08002879 return rtl_enable(tp);
2880}
2881
hayeswangd70b1132014-09-19 15:17:18 +08002882static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002883{
hayeswangebc2ec482013-08-14 20:54:38 +08002884 u32 ocp_data;
2885 int i;
hayeswangac718b62013-05-02 16:01:25 +00002886
hayeswang68714382014-04-11 17:54:31 +08002887 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2888 rtl_drop_queued_tx(tp);
2889 return;
2890 }
2891
hayeswangac718b62013-05-02 16:01:25 +00002892 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2893 ocp_data &= ~RCR_ACPT_ALL;
2894 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2895
hayeswang00a5e362014-02-18 21:48:59 +08002896 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002897
2898 for (i = 0; i < RTL8152_MAX_TX; i++)
2899 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002900
hayeswang00a5e362014-02-18 21:48:59 +08002901 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002902
2903 for (i = 0; i < 1000; i++) {
2904 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2905 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2906 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002907 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002908 }
2909
2910 for (i = 0; i < 1000; i++) {
2911 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2912 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002913 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002914 }
2915
hayeswang445f7f42014-09-23 16:31:47 +08002916 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002917
2918 rtl8152_nic_reset(tp);
2919}
2920
hayeswang00a5e362014-02-18 21:48:59 +08002921static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2922{
2923 u32 ocp_data;
2924
2925 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2926 if (enable)
2927 ocp_data |= POWER_CUT;
2928 else
2929 ocp_data &= ~POWER_CUT;
2930 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2931
2932 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2933 ocp_data &= ~RESUME_INDICATE;
2934 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002935}
2936
hayeswangc5554292014-09-12 10:43:11 +08002937static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2938{
2939 u32 ocp_data;
2940
2941 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2942 if (enable)
2943 ocp_data |= CPCR_RX_VLAN;
2944 else
2945 ocp_data &= ~CPCR_RX_VLAN;
2946 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2947}
2948
2949static int rtl8152_set_features(struct net_device *dev,
2950 netdev_features_t features)
2951{
2952 netdev_features_t changed = features ^ dev->features;
2953 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002954 int ret;
2955
2956 ret = usb_autopm_get_interface(tp->intf);
2957 if (ret < 0)
2958 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002959
hayeswangb5403272014-10-09 18:00:26 +08002960 mutex_lock(&tp->control);
2961
hayeswangc5554292014-09-12 10:43:11 +08002962 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2963 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2964 rtl_rx_vlan_en(tp, true);
2965 else
2966 rtl_rx_vlan_en(tp, false);
2967 }
2968
hayeswangb5403272014-10-09 18:00:26 +08002969 mutex_unlock(&tp->control);
2970
hayeswang405f8a02014-10-09 18:00:24 +08002971 usb_autopm_put_interface(tp->intf);
2972
2973out:
2974 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002975}
2976
hayeswang21ff2e82014-02-18 21:49:06 +08002977#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2978
2979static u32 __rtl_get_wol(struct r8152 *tp)
2980{
2981 u32 ocp_data;
2982 u32 wolopts = 0;
2983
hayeswang21ff2e82014-02-18 21:49:06 +08002984 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2985 if (ocp_data & LINK_ON_WAKE_EN)
2986 wolopts |= WAKE_PHY;
2987
2988 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2989 if (ocp_data & UWF_EN)
2990 wolopts |= WAKE_UCAST;
2991 if (ocp_data & BWF_EN)
2992 wolopts |= WAKE_BCAST;
2993 if (ocp_data & MWF_EN)
2994 wolopts |= WAKE_MCAST;
2995
2996 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2997 if (ocp_data & MAGIC_EN)
2998 wolopts |= WAKE_MAGIC;
2999
3000 return wolopts;
3001}
3002
3003static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3004{
3005 u32 ocp_data;
3006
3007 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3008
3009 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3010 ocp_data &= ~LINK_ON_WAKE_EN;
3011 if (wolopts & WAKE_PHY)
3012 ocp_data |= LINK_ON_WAKE_EN;
3013 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3014
3015 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08003016 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08003017 if (wolopts & WAKE_UCAST)
3018 ocp_data |= UWF_EN;
3019 if (wolopts & WAKE_BCAST)
3020 ocp_data |= BWF_EN;
3021 if (wolopts & WAKE_MCAST)
3022 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08003023 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3024
3025 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3026
3027 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3028 ocp_data &= ~MAGIC_EN;
3029 if (wolopts & WAKE_MAGIC)
3030 ocp_data |= MAGIC_EN;
3031 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3032
3033 if (wolopts & WAKE_ANY)
3034 device_set_wakeup_enable(&tp->udev->dev, true);
3035 else
3036 device_set_wakeup_enable(&tp->udev->dev, false);
3037}
3038
hayeswang134f98b2017-06-09 17:11:40 +08003039static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
3040{
3041 /* MAC clock speed down */
3042 if (enable) {
3043 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3044 ALDPS_SPDWN_RATIO);
3045 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
3046 EEE_SPDWN_RATIO);
3047 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3048 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3049 U1U2_SPDWN_EN | L1_SPDWN_EN);
3050 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3051 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3052 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
3053 TP1000_SPDWN_EN);
3054 } else {
3055 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
3056 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
3057 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
3058 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
3059 }
3060}
3061
hayeswangb2143962015-07-24 13:54:23 +08003062static void r8153_u1u2en(struct r8152 *tp, bool enable)
3063{
3064 u8 u1u2[8];
3065
3066 if (enable)
3067 memset(u1u2, 0xff, sizeof(u1u2));
3068 else
3069 memset(u1u2, 0x00, sizeof(u1u2));
3070
3071 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3072}
3073
hayeswang65b82d62017-06-15 14:44:03 +08003074static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3075{
3076 u32 ocp_data;
3077
3078 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3079 if (enable)
3080 ocp_data |= LPM_U1U2_EN;
3081 else
3082 ocp_data &= ~LPM_U1U2_EN;
3083
3084 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3085}
3086
hayeswangb2143962015-07-24 13:54:23 +08003087static void r8153_u2p3en(struct r8152 *tp, bool enable)
3088{
3089 u32 ocp_data;
3090
3091 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08003092 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08003093 ocp_data |= U2P3_ENABLE;
3094 else
3095 ocp_data &= ~U2P3_ENABLE;
3096 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3097}
3098
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003099static void r8153b_ups_flags(struct r8152 *tp)
hayeswang65b82d62017-06-15 14:44:03 +08003100{
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003101 u32 ups_flags = 0;
hayeswang65b82d62017-06-15 14:44:03 +08003102
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003103 if (tp->ups_info.green)
3104 ups_flags |= UPS_FLAGS_EN_GREEN;
3105
3106 if (tp->ups_info.aldps)
3107 ups_flags |= UPS_FLAGS_EN_ALDPS;
3108
3109 if (tp->ups_info.eee)
3110 ups_flags |= UPS_FLAGS_EN_EEE;
3111
3112 if (tp->ups_info.flow_control)
3113 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3114
3115 if (tp->ups_info.eee_ckdiv)
3116 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3117
3118 if (tp->ups_info.eee_cmod_lv)
3119 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3120
3121 if (tp->ups_info._10m_ckdiv)
3122 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3123
3124 if (tp->ups_info.eee_plloff_100)
3125 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3126
3127 if (tp->ups_info.eee_plloff_giga)
3128 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3129
3130 if (tp->ups_info._250m_ckdiv)
3131 ups_flags |= UPS_FLAGS_250M_CKDIV;
3132
3133 if (tp->ups_info.ctap_short_off)
3134 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3135
3136 switch (tp->ups_info.speed_duplex) {
3137 case NWAY_10M_HALF:
3138 ups_flags |= ups_flags_speed(1);
3139 break;
3140 case NWAY_10M_FULL:
3141 ups_flags |= ups_flags_speed(2);
3142 break;
3143 case NWAY_100M_HALF:
3144 ups_flags |= ups_flags_speed(3);
3145 break;
3146 case NWAY_100M_FULL:
3147 ups_flags |= ups_flags_speed(4);
3148 break;
3149 case NWAY_1000M_FULL:
3150 ups_flags |= ups_flags_speed(5);
3151 break;
3152 case FORCE_10M_HALF:
3153 ups_flags |= ups_flags_speed(6);
3154 break;
3155 case FORCE_10M_FULL:
3156 ups_flags |= ups_flags_speed(7);
3157 break;
3158 case FORCE_100M_HALF:
3159 ups_flags |= ups_flags_speed(8);
3160 break;
3161 case FORCE_100M_FULL:
3162 ups_flags |= ups_flags_speed(9);
3163 break;
3164 default:
3165 break;
3166 }
3167
3168 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
hayeswang65b82d62017-06-15 14:44:03 +08003169}
3170
3171static void r8153b_green_en(struct r8152 *tp, bool enable)
3172{
3173 u16 data;
3174
3175 if (enable) {
3176 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
3177 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
3178 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
3179 } else {
3180 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
3181 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
3182 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
3183 }
3184
3185 data = sram_read(tp, SRAM_GREEN_CFG);
3186 data |= GREEN_ETH_EN;
3187 sram_write(tp, SRAM_GREEN_CFG, data);
3188
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003189 tp->ups_info.green = enable;
hayeswang65b82d62017-06-15 14:44:03 +08003190}
3191
hayeswangc564b872017-06-09 17:11:38 +08003192static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3193{
3194 u16 data;
3195 int i;
3196
3197 for (i = 0; i < 500; i++) {
3198 data = ocp_reg_read(tp, OCP_PHY_STATUS);
3199 data &= PHY_STAT_MASK;
3200 if (desired) {
3201 if (data == desired)
3202 break;
3203 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3204 data == PHY_STAT_EXT_INIT) {
3205 break;
3206 }
3207
3208 msleep(20);
3209 }
3210
3211 return data;
3212}
3213
hayeswang65b82d62017-06-15 14:44:03 +08003214static void r8153b_ups_en(struct r8152 *tp, bool enable)
3215{
3216 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3217
3218 if (enable) {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003219 r8153b_ups_flags(tp);
3220
hayeswang65b82d62017-06-15 14:44:03 +08003221 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3222 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3223
3224 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3225 ocp_data |= BIT(0);
3226 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3227 } else {
3228 u16 data;
3229
3230 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3231 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3232
3233 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3234 ocp_data &= ~BIT(0);
3235 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3236
3237 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3238 ocp_data &= ~PCUT_STATUS;
3239 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3240
3241 data = r8153_phy_status(tp, 0);
3242
3243 switch (data) {
3244 case PHY_STAT_PWRDN:
3245 case PHY_STAT_EXT_INIT:
3246 r8153b_green_en(tp,
3247 test_bit(GREEN_ETHERNET, &tp->flags));
3248
3249 data = r8152_mdio_read(tp, MII_BMCR);
3250 data &= ~BMCR_PDOWN;
3251 data |= BMCR_RESET;
3252 r8152_mdio_write(tp, MII_BMCR, data);
3253
3254 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05003255 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08003256
3257 default:
3258 if (data != PHY_STAT_LAN_ON)
3259 netif_warn(tp, link, tp->netdev,
3260 "PHY not ready");
3261 break;
3262 }
3263 }
3264}
3265
hayeswangb2143962015-07-24 13:54:23 +08003266static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3267{
3268 u32 ocp_data;
3269
3270 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3271 if (enable)
3272 ocp_data |= PWR_EN | PHASE2_EN;
3273 else
3274 ocp_data &= ~(PWR_EN | PHASE2_EN);
3275 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3276
3277 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3278 ocp_data &= ~PCUT_STATUS;
3279 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3280}
3281
hayeswang65b82d62017-06-15 14:44:03 +08003282static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3283{
3284 u32 ocp_data;
3285
3286 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3287 if (enable)
3288 ocp_data |= PWR_EN | PHASE2_EN;
3289 else
3290 ocp_data &= ~PWR_EN;
3291 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3292
3293 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3294 ocp_data &= ~PCUT_STATUS;
3295 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3296}
3297
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003298static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08003299{
3300 u32 ocp_data;
3301
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003302 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08003303 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003304 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08003305 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003306 ocp_data &= ~UPCOMING_RUNTIME_D3;
3307 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003308
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003309 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3310 ocp_data &= ~LINK_CHG_EVENT;
3311 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3312
3313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3314 ocp_data &= ~LINK_CHANGE_FLAG;
3315 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003316}
3317
hayeswang7daed8d2015-07-24 13:54:24 +08003318static bool rtl_can_wakeup(struct r8152 *tp)
3319{
3320 struct usb_device *udev = tp->udev;
3321
3322 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3323}
3324
hayeswang9a4be1b2014-02-18 21:49:07 +08003325static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3326{
3327 if (enable) {
3328 u32 ocp_data;
3329
3330 __rtl_set_wol(tp, WAKE_ANY);
3331
3332 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3333
3334 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3335 ocp_data |= LINK_OFF_WAKE_EN;
3336 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3337
3338 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3339 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08003340 u32 ocp_data;
3341
hayeswang9a4be1b2014-02-18 21:49:07 +08003342 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08003343
3344 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3345
3346 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3347 ocp_data &= ~LINK_OFF_WAKE_EN;
3348 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3349
3350 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08003351 }
3352}
hayeswangf95ae8a2016-06-30 15:33:35 +08003353
hayeswang2609af12016-07-05 16:11:46 +08003354static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3355{
hayeswang2609af12016-07-05 16:11:46 +08003356 if (enable) {
3357 r8153_u1u2en(tp, false);
3358 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003359 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08003360 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08003361 } else {
hayeswang02552752017-06-09 17:11:42 +08003362 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003363 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003364
3365 switch (tp->version) {
3366 case RTL_VER_03:
3367 case RTL_VER_04:
3368 break;
3369 case RTL_VER_05:
3370 case RTL_VER_06:
3371 default:
3372 r8153_u2p3en(tp, true);
3373 break;
3374 }
3375
hayeswangb2143962015-07-24 13:54:23 +08003376 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08003377 }
3378}
3379
hayeswang65b82d62017-06-15 14:44:03 +08003380static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3381{
3382 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003383 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003384 r8153b_u1u2en(tp, false);
3385 r8153_u2p3en(tp, false);
3386 rtl_runtime_suspend_enable(tp, true);
3387 r8153b_ups_en(tp, true);
3388 } else {
3389 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003390 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003391 rtl_runtime_suspend_enable(tp, false);
3392 r8153_u2p3en(tp, true);
3393 r8153b_u1u2en(tp, true);
3394 }
3395}
3396
hayeswang43499682014-02-18 21:48:58 +08003397static void r8153_teredo_off(struct r8152 *tp)
3398{
3399 u32 ocp_data;
3400
hayeswang65b82d62017-06-15 14:44:03 +08003401 switch (tp->version) {
3402 case RTL_VER_01:
3403 case RTL_VER_02:
3404 case RTL_VER_03:
3405 case RTL_VER_04:
3406 case RTL_VER_05:
3407 case RTL_VER_06:
3408 case RTL_VER_07:
3409 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3410 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3411 OOB_TEREDO_EN);
3412 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3413 break;
3414
3415 case RTL_VER_08:
3416 case RTL_VER_09:
3417 /* The bit 0 ~ 7 are relative with teredo settings. They are
3418 * W1C (write 1 to clear), so set all 1 to disable it.
3419 */
3420 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3421 break;
3422
3423 default:
3424 break;
3425 }
hayeswang43499682014-02-18 21:48:58 +08003426
3427 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3428 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3429 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3430}
3431
hayeswang93fe9b12016-06-16 10:55:18 +08003432static void rtl_reset_bmu(struct r8152 *tp)
3433{
3434 u32 ocp_data;
3435
3436 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3437 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3438 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3439 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3440 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3441}
3442
Hayes Wang9370f2d2019-10-16 11:02:42 +08003443/* Clear the bp to stop the firmware before loading a new one */
3444static void rtl_clear_bp(struct r8152 *tp, u16 type)
3445{
3446 switch (tp->version) {
3447 case RTL_VER_01:
3448 case RTL_VER_02:
3449 case RTL_VER_07:
3450 break;
3451 case RTL_VER_03:
3452 case RTL_VER_04:
3453 case RTL_VER_05:
3454 case RTL_VER_06:
3455 ocp_write_byte(tp, type, PLA_BP_EN, 0);
3456 break;
3457 case RTL_VER_08:
3458 case RTL_VER_09:
3459 default:
3460 if (type == MCU_TYPE_USB) {
3461 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
3462
3463 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
3464 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
3465 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
3466 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
3467 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
3468 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
3469 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
3470 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
3471 } else {
3472 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
3473 }
3474 break;
3475 }
3476
3477 ocp_write_word(tp, type, PLA_BP_0, 0);
3478 ocp_write_word(tp, type, PLA_BP_1, 0);
3479 ocp_write_word(tp, type, PLA_BP_2, 0);
3480 ocp_write_word(tp, type, PLA_BP_3, 0);
3481 ocp_write_word(tp, type, PLA_BP_4, 0);
3482 ocp_write_word(tp, type, PLA_BP_5, 0);
3483 ocp_write_word(tp, type, PLA_BP_6, 0);
3484 ocp_write_word(tp, type, PLA_BP_7, 0);
3485
3486 /* wait 3 ms to make sure the firmware is stopped */
3487 usleep_range(3000, 6000);
3488 ocp_write_word(tp, type, PLA_BP_BA, 0);
3489}
3490
Hayes Wang470e3912019-10-21 11:41:12 +08003491static int r8153_patch_request(struct r8152 *tp, bool request)
3492{
3493 u16 data;
3494 int i;
3495
3496 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3497 if (request)
3498 data |= PATCH_REQUEST;
3499 else
3500 data &= ~PATCH_REQUEST;
3501 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3502
3503 for (i = 0; request && i < 5000; i++) {
3504 usleep_range(1000, 2000);
3505 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3506 break;
3507 }
3508
3509 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3510 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3511 r8153_patch_request(tp, false);
3512 return -ETIME;
3513 } else {
3514 return 0;
3515 }
3516}
3517
Hayes Wangaf142882019-10-21 11:41:13 +08003518static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key)
3519{
3520 if (r8153_patch_request(tp, true)) {
3521 dev_err(&tp->intf->dev, "patch request fail\n");
3522 return -ETIME;
3523 }
3524
3525 sram_write(tp, key_addr, patch_key);
3526 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
3527
3528 return 0;
3529}
3530
3531static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr)
3532{
3533 u16 data;
3534
3535 sram_write(tp, 0x0000, 0x0000);
3536
3537 data = ocp_reg_read(tp, OCP_PHY_LOCK);
3538 data &= ~PATCH_LOCK;
3539 ocp_reg_write(tp, OCP_PHY_LOCK, data);
3540
3541 sram_write(tp, key_addr, 0x0000);
3542
3543 r8153_patch_request(tp, false);
3544
3545 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
3546
3547 return 0;
3548}
3549
3550static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
3551{
3552 u32 length;
3553 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
3554 bool rc = false;
3555
3556 switch (tp->version) {
3557 case RTL_VER_04:
3558 case RTL_VER_05:
3559 case RTL_VER_06:
3560 fw_reg = 0xa014;
3561 ba_reg = 0xa012;
3562 patch_en_addr = 0xa01a;
3563 mode_reg = 0xb820;
3564 bp_start = 0xa000;
3565 break;
3566 default:
3567 goto out;
3568 }
3569
3570 fw_offset = __le16_to_cpu(phy->fw_offset);
3571 if (fw_offset < sizeof(*phy)) {
3572 dev_err(&tp->intf->dev, "fw_offset too small\n");
3573 goto out;
3574 }
3575
3576 length = __le32_to_cpu(phy->blk_hdr.length);
3577 if (length < fw_offset) {
3578 dev_err(&tp->intf->dev, "invalid fw_offset\n");
3579 goto out;
3580 }
3581
3582 length -= __le16_to_cpu(phy->fw_offset);
3583 if (!length || (length & 1)) {
3584 dev_err(&tp->intf->dev, "invalid block length\n");
3585 goto out;
3586 }
3587
3588 if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
3589 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
3590 goto out;
3591 }
3592
3593 if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
3594 dev_err(&tp->intf->dev, "invalid base address register\n");
3595 goto out;
3596 }
3597
3598 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
3599 dev_err(&tp->intf->dev,
3600 "invalid patch mode enabled register\n");
3601 goto out;
3602 }
3603
3604 if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
3605 dev_err(&tp->intf->dev,
3606 "invalid register to switch the mode\n");
3607 goto out;
3608 }
3609
3610 if (__le16_to_cpu(phy->bp_start) != bp_start) {
3611 dev_err(&tp->intf->dev,
3612 "invalid start register of break point\n");
3613 goto out;
3614 }
3615
3616 if (__le16_to_cpu(phy->bp_num) > 4) {
3617 dev_err(&tp->intf->dev, "invalid break point number\n");
3618 goto out;
3619 }
3620
3621 rc = true;
3622out:
3623 return rc;
3624}
3625
Hayes Wanga66edaa2019-10-21 11:41:10 +08003626static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
Hayes Wang9370f2d2019-10-16 11:02:42 +08003627{
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003628 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003629 bool rc = false;
3630 u32 length, type;
3631 int i, max_bp;
3632
Hayes Wanga66edaa2019-10-21 11:41:10 +08003633 type = __le32_to_cpu(mac->blk_hdr.type);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003634 if (type == RTL_FW_PLA) {
3635 switch (tp->version) {
3636 case RTL_VER_01:
3637 case RTL_VER_02:
3638 case RTL_VER_07:
3639 fw_reg = 0xf800;
3640 bp_ba_addr = PLA_BP_BA;
3641 bp_en_addr = 0;
3642 bp_start = PLA_BP_0;
3643 max_bp = 8;
3644 break;
3645 case RTL_VER_03:
3646 case RTL_VER_04:
3647 case RTL_VER_05:
3648 case RTL_VER_06:
3649 case RTL_VER_08:
3650 case RTL_VER_09:
3651 fw_reg = 0xf800;
3652 bp_ba_addr = PLA_BP_BA;
3653 bp_en_addr = PLA_BP_EN;
3654 bp_start = PLA_BP_0;
3655 max_bp = 8;
3656 break;
3657 default:
3658 goto out;
3659 }
3660 } else if (type == RTL_FW_USB) {
3661 switch (tp->version) {
3662 case RTL_VER_03:
3663 case RTL_VER_04:
3664 case RTL_VER_05:
3665 case RTL_VER_06:
3666 fw_reg = 0xf800;
3667 bp_ba_addr = USB_BP_BA;
3668 bp_en_addr = USB_BP_EN;
3669 bp_start = USB_BP_0;
3670 max_bp = 8;
3671 break;
3672 case RTL_VER_08:
3673 case RTL_VER_09:
3674 fw_reg = 0xe600;
3675 bp_ba_addr = USB_BP_BA;
3676 bp_en_addr = USB_BP2_EN;
3677 bp_start = USB_BP_0;
3678 max_bp = 16;
3679 break;
3680 case RTL_VER_01:
3681 case RTL_VER_02:
3682 case RTL_VER_07:
3683 default:
3684 goto out;
3685 }
3686 } else {
3687 goto out;
3688 }
3689
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003690 fw_offset = __le16_to_cpu(mac->fw_offset);
3691 if (fw_offset < sizeof(*mac)) {
3692 dev_err(&tp->intf->dev, "fw_offset too small\n");
3693 goto out;
3694 }
3695
Hayes Wanga66edaa2019-10-21 11:41:10 +08003696 length = __le32_to_cpu(mac->blk_hdr.length);
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003697 if (length < fw_offset) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003698 dev_err(&tp->intf->dev, "invalid fw_offset\n");
3699 goto out;
3700 }
3701
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003702 length -= fw_offset;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003703 if (length < 4 || (length & 3)) {
3704 dev_err(&tp->intf->dev, "invalid block length\n");
3705 goto out;
3706 }
3707
Hayes Wanga66edaa2019-10-21 11:41:10 +08003708 if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003709 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
3710 goto out;
3711 }
3712
Hayes Wanga66edaa2019-10-21 11:41:10 +08003713 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003714 dev_err(&tp->intf->dev, "invalid base address register\n");
3715 goto out;
3716 }
3717
Hayes Wanga66edaa2019-10-21 11:41:10 +08003718 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003719 dev_err(&tp->intf->dev, "invalid enabled mask register\n");
3720 goto out;
3721 }
3722
Hayes Wanga66edaa2019-10-21 11:41:10 +08003723 if (__le16_to_cpu(mac->bp_start) != bp_start) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003724 dev_err(&tp->intf->dev,
3725 "invalid start register of break point\n");
3726 goto out;
3727 }
3728
Hayes Wanga66edaa2019-10-21 11:41:10 +08003729 if (__le16_to_cpu(mac->bp_num) > max_bp) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003730 dev_err(&tp->intf->dev, "invalid break point number\n");
3731 goto out;
3732 }
3733
Hayes Wanga66edaa2019-10-21 11:41:10 +08003734 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
3735 if (mac->bp[i]) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003736 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
3737 goto out;
3738 }
3739 }
3740
3741 rc = true;
3742out:
3743 return rc;
3744}
3745
3746/* Verify the checksum for the firmware file. It is calculated from the version
3747 * field to the end of the file. Compare the result with the checksum field to
3748 * make sure the file is correct.
3749 */
3750static long rtl8152_fw_verify_checksum(struct r8152 *tp,
3751 struct fw_header *fw_hdr, size_t size)
3752{
3753 unsigned char checksum[sizeof(fw_hdr->checksum)];
3754 struct crypto_shash *alg;
3755 struct shash_desc *sdesc;
3756 size_t len;
3757 long rc;
3758
3759 alg = crypto_alloc_shash("sha256", 0, 0);
3760 if (IS_ERR(alg)) {
3761 rc = PTR_ERR(alg);
3762 goto out;
3763 }
3764
3765 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
3766 rc = -EFAULT;
3767 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
3768 crypto_shash_digestsize(alg));
3769 goto free_shash;
3770 }
3771
3772 len = sizeof(*sdesc) + crypto_shash_descsize(alg);
3773 sdesc = kmalloc(len, GFP_KERNEL);
3774 if (!sdesc) {
3775 rc = -ENOMEM;
3776 goto free_shash;
3777 }
3778 sdesc->tfm = alg;
3779
3780 len = size - sizeof(fw_hdr->checksum);
3781 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
3782 kfree(sdesc);
3783 if (rc)
3784 goto free_shash;
3785
3786 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
3787 dev_err(&tp->intf->dev, "checksum fail\n");
3788 rc = -EFAULT;
3789 }
3790
3791free_shash:
3792 crypto_free_shash(alg);
3793out:
3794 return rc;
3795}
3796
3797static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
3798{
3799 const struct firmware *fw = rtl_fw->fw;
3800 struct fw_header *fw_hdr = (struct fw_header *)fw->data;
Hayes Wanga66edaa2019-10-21 11:41:10 +08003801 struct fw_mac *pla = NULL, *usb = NULL;
Hayes Wangaf142882019-10-21 11:41:13 +08003802 struct fw_phy_patch_key *start = NULL;
3803 struct fw_phy_nc *phy_nc = NULL;
3804 struct fw_block *stop = NULL;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003805 long ret = -EFAULT;
3806 int i;
3807
3808 if (fw->size < sizeof(*fw_hdr)) {
3809 dev_err(&tp->intf->dev, "file too small\n");
3810 goto fail;
3811 }
3812
3813 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
3814 if (ret)
3815 goto fail;
3816
3817 ret = -EFAULT;
3818
3819 for (i = sizeof(*fw_hdr); i < fw->size;) {
3820 struct fw_block *block = (struct fw_block *)&fw->data[i];
3821 u32 type;
3822
3823 if ((i + sizeof(*block)) > fw->size)
3824 goto fail;
3825
3826 type = __le32_to_cpu(block->type);
3827 switch (type) {
3828 case RTL_FW_END:
3829 if (__le32_to_cpu(block->length) != sizeof(*block))
3830 goto fail;
Hayes Wangaf142882019-10-21 11:41:13 +08003831 goto fw_end;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003832 case RTL_FW_PLA:
3833 if (pla) {
3834 dev_err(&tp->intf->dev,
3835 "multiple PLA firmware encountered");
3836 goto fail;
3837 }
3838
Hayes Wanga66edaa2019-10-21 11:41:10 +08003839 pla = (struct fw_mac *)block;
3840 if (!rtl8152_is_fw_mac_ok(tp, pla)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003841 dev_err(&tp->intf->dev,
Hayes Wanga66edaa2019-10-21 11:41:10 +08003842 "check PLA firmware failed\n");
Hayes Wang9370f2d2019-10-16 11:02:42 +08003843 goto fail;
3844 }
3845 break;
3846 case RTL_FW_USB:
3847 if (usb) {
3848 dev_err(&tp->intf->dev,
3849 "multiple USB firmware encountered");
3850 goto fail;
3851 }
3852
Hayes Wanga66edaa2019-10-21 11:41:10 +08003853 usb = (struct fw_mac *)block;
3854 if (!rtl8152_is_fw_mac_ok(tp, usb)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003855 dev_err(&tp->intf->dev,
Hayes Wanga66edaa2019-10-21 11:41:10 +08003856 "check USB firmware failed\n");
Hayes Wang9370f2d2019-10-16 11:02:42 +08003857 goto fail;
3858 }
3859 break;
Hayes Wangaf142882019-10-21 11:41:13 +08003860 case RTL_FW_PHY_START:
3861 if (start || phy_nc || stop) {
3862 dev_err(&tp->intf->dev,
3863 "check PHY_START fail\n");
3864 goto fail;
3865 }
3866
3867 if (__le32_to_cpu(block->length) != sizeof(*start)) {
3868 dev_err(&tp->intf->dev,
3869 "Invalid length for PHY_START\n");
3870 goto fail;
3871 }
3872
3873 start = (struct fw_phy_patch_key *)block;
3874 break;
3875 case RTL_FW_PHY_STOP:
3876 if (stop || !start) {
3877 dev_err(&tp->intf->dev,
3878 "Check PHY_STOP fail\n");
3879 goto fail;
3880 }
3881
3882 if (__le32_to_cpu(block->length) != sizeof(*block)) {
3883 dev_err(&tp->intf->dev,
3884 "Invalid length for PHY_STOP\n");
3885 goto fail;
3886 }
3887
3888 stop = block;
3889 break;
3890 case RTL_FW_PHY_NC:
3891 if (!start || stop) {
3892 dev_err(&tp->intf->dev,
3893 "check PHY_NC fail\n");
3894 goto fail;
3895 }
3896
3897 if (phy_nc) {
3898 dev_err(&tp->intf->dev,
3899 "multiple PHY NC encountered\n");
3900 goto fail;
3901 }
3902
3903 phy_nc = (struct fw_phy_nc *)block;
3904 if (!rtl8152_is_fw_phy_nc_ok(tp, phy_nc)) {
3905 dev_err(&tp->intf->dev,
3906 "check PHY NC firmware failed\n");
3907 goto fail;
3908 }
3909
3910 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003911 default:
3912 dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
3913 type);
3914 break;
3915 }
3916
3917 /* next block */
3918 i += ALIGN(__le32_to_cpu(block->length), 8);
3919 }
3920
Hayes Wangaf142882019-10-21 11:41:13 +08003921fw_end:
3922 if ((phy_nc || start) && !stop) {
3923 dev_err(&tp->intf->dev, "without PHY_STOP\n");
3924 goto fail;
3925 }
3926
Hayes Wang9370f2d2019-10-16 11:02:42 +08003927 return 0;
3928fail:
3929 return ret;
3930}
3931
Hayes Wangaf142882019-10-21 11:41:13 +08003932static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
3933{
3934 u16 mode_reg, bp_index;
3935 u32 length, i, num;
3936 __le16 *data;
3937
3938 mode_reg = __le16_to_cpu(phy->mode_reg);
3939 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
3940 sram_write(tp, __le16_to_cpu(phy->ba_reg),
3941 __le16_to_cpu(phy->ba_data));
3942
3943 length = __le32_to_cpu(phy->blk_hdr.length);
3944 length -= __le16_to_cpu(phy->fw_offset);
3945 num = length / 2;
3946 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
3947
3948 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
3949 for (i = 0; i < num; i++)
3950 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
3951
3952 sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
3953 __le16_to_cpu(phy->patch_en_value));
3954
3955 bp_index = __le16_to_cpu(phy->bp_start);
3956 num = __le16_to_cpu(phy->bp_num);
3957 for (i = 0; i < num; i++) {
3958 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
3959 bp_index += 2;
3960 }
3961
3962 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
3963
3964 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
3965}
3966
Hayes Wanga66edaa2019-10-21 11:41:10 +08003967static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
Hayes Wang9370f2d2019-10-16 11:02:42 +08003968{
3969 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
3970 u32 length;
3971 u8 *data;
3972 int i;
3973
Hayes Wanga66edaa2019-10-21 11:41:10 +08003974 switch (__le32_to_cpu(mac->blk_hdr.type)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003975 case RTL_FW_PLA:
3976 type = MCU_TYPE_PLA;
3977 break;
3978 case RTL_FW_USB:
3979 type = MCU_TYPE_USB;
3980 break;
3981 default:
3982 return;
3983 }
3984
3985 rtl_clear_bp(tp, type);
3986
3987 /* Enable backup/restore of MACDBG. This is required after clearing PLA
3988 * break points and before applying the PLA firmware.
3989 */
3990 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
3991 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
3992 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
3993 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
3994 }
3995
Hayes Wanga66edaa2019-10-21 11:41:10 +08003996 length = __le32_to_cpu(mac->blk_hdr.length);
3997 length -= __le16_to_cpu(mac->fw_offset);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003998
Hayes Wanga66edaa2019-10-21 11:41:10 +08003999 data = (u8 *)mac;
4000 data += __le16_to_cpu(mac->fw_offset);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004001
Hayes Wanga66edaa2019-10-21 11:41:10 +08004002 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
Hayes Wang9370f2d2019-10-16 11:02:42 +08004003 type);
4004
Hayes Wanga66edaa2019-10-21 11:41:10 +08004005 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
4006 __le16_to_cpu(mac->bp_ba_value));
Hayes Wang9370f2d2019-10-16 11:02:42 +08004007
Hayes Wanga66edaa2019-10-21 11:41:10 +08004008 bp_index = __le16_to_cpu(mac->bp_start);
4009 bp_num = __le16_to_cpu(mac->bp_num);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004010 for (i = 0; i < bp_num; i++) {
Hayes Wanga66edaa2019-10-21 11:41:10 +08004011 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
Hayes Wang9370f2d2019-10-16 11:02:42 +08004012 bp_index += 2;
4013 }
4014
Hayes Wanga66edaa2019-10-21 11:41:10 +08004015 bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004016 if (bp_en_addr)
4017 ocp_write_word(tp, type, bp_en_addr,
Hayes Wanga66edaa2019-10-21 11:41:10 +08004018 __le16_to_cpu(mac->bp_en_value));
Hayes Wang9370f2d2019-10-16 11:02:42 +08004019
Hayes Wanga66edaa2019-10-21 11:41:10 +08004020 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004021 if (fw_ver_reg)
4022 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
Hayes Wanga66edaa2019-10-21 11:41:10 +08004023 mac->fw_ver_data);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004024
Hayes Wanga66edaa2019-10-21 11:41:10 +08004025 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004026}
4027
4028static void rtl8152_apply_firmware(struct r8152 *tp)
4029{
4030 struct rtl_fw *rtl_fw = &tp->rtl_fw;
Hayes Wang8e484eb2019-10-23 21:24:44 +08004031 const struct firmware *fw;
4032 struct fw_header *fw_hdr;
Hayes Wangaf142882019-10-21 11:41:13 +08004033 struct fw_phy_patch_key *key;
4034 u16 key_addr = 0;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004035 int i;
4036
4037 if (IS_ERR_OR_NULL(rtl_fw->fw))
4038 return;
4039
Hayes Wang8e484eb2019-10-23 21:24:44 +08004040 fw = rtl_fw->fw;
4041 fw_hdr = (struct fw_header *)fw->data;
4042
Hayes Wang9370f2d2019-10-16 11:02:42 +08004043 if (rtl_fw->pre_fw)
4044 rtl_fw->pre_fw(tp);
4045
4046 for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
4047 struct fw_block *block = (struct fw_block *)&fw->data[i];
4048
4049 switch (__le32_to_cpu(block->type)) {
4050 case RTL_FW_END:
4051 goto post_fw;
4052 case RTL_FW_PLA:
4053 case RTL_FW_USB:
Hayes Wanga66edaa2019-10-21 11:41:10 +08004054 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004055 break;
Hayes Wangaf142882019-10-21 11:41:13 +08004056 case RTL_FW_PHY_START:
4057 key = (struct fw_phy_patch_key *)block;
4058 key_addr = __le16_to_cpu(key->key_reg);
4059 r8153_pre_ram_code(tp, key_addr,
4060 __le16_to_cpu(key->key_data));
4061 break;
4062 case RTL_FW_PHY_STOP:
4063 WARN_ON(!key_addr);
4064 r8153_post_ram_code(tp, key_addr);
4065 break;
4066 case RTL_FW_PHY_NC:
4067 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
4068 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004069 default:
4070 break;
4071 }
4072
4073 i += ALIGN(__le32_to_cpu(block->length), 8);
4074 }
4075
4076post_fw:
4077 if (rtl_fw->post_fw)
4078 rtl_fw->post_fw(tp);
4079
4080 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
4081 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
4082}
4083
4084static void rtl8152_release_firmware(struct r8152 *tp)
4085{
4086 struct rtl_fw *rtl_fw = &tp->rtl_fw;
4087
4088 if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
4089 release_firmware(rtl_fw->fw);
4090 rtl_fw->fw = NULL;
4091 }
4092}
4093
4094static int rtl8152_request_firmware(struct r8152 *tp)
4095{
4096 struct rtl_fw *rtl_fw = &tp->rtl_fw;
4097 long rc;
4098
4099 if (rtl_fw->fw || !rtl_fw->fw_name) {
4100 dev_info(&tp->intf->dev, "skip request firmware\n");
4101 rc = 0;
4102 goto result;
4103 }
4104
4105 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
4106 if (rc < 0)
4107 goto result;
4108
4109 rc = rtl8152_check_firmware(tp, rtl_fw);
4110 if (rc < 0)
4111 release_firmware(rtl_fw->fw);
4112
4113result:
4114 if (rc) {
4115 rtl_fw->fw = ERR_PTR(rc);
4116
4117 dev_warn(&tp->intf->dev,
4118 "unable to load firmware patch %s (%ld)\n",
4119 rtl_fw->fw_name, rc);
4120 }
4121
4122 return rc;
4123}
4124
hayeswangcda9fb02016-01-07 17:51:12 +08004125static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08004126{
hayeswangcda9fb02016-01-07 17:51:12 +08004127 if (enable) {
4128 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
4129 LINKENA | DIS_SDSAVE);
4130 } else {
4131 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
4132 DIS_SDSAVE);
4133 msleep(20);
4134 }
hayeswang43499682014-02-18 21:48:58 +08004135}
4136
hayeswange6449532016-09-20 16:22:05 +08004137static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
4138{
4139 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
4140 ocp_reg_write(tp, OCP_EEE_DATA, reg);
4141 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
4142}
4143
4144static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
4145{
4146 u16 data;
4147
4148 r8152_mmd_indirect(tp, dev, reg);
4149 data = ocp_reg_read(tp, OCP_EEE_DATA);
4150 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
4151
4152 return data;
4153}
4154
4155static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
4156{
4157 r8152_mmd_indirect(tp, dev, reg);
4158 ocp_reg_write(tp, OCP_EEE_DATA, data);
4159 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
4160}
4161
4162static void r8152_eee_en(struct r8152 *tp, bool enable)
4163{
4164 u16 config1, config2, config3;
4165 u32 ocp_data;
4166
4167 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4168 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
4169 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
4170 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
4171
4172 if (enable) {
4173 ocp_data |= EEE_RX_EN | EEE_TX_EN;
4174 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
4175 config1 |= sd_rise_time(1);
4176 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
4177 config3 |= fast_snr(42);
4178 } else {
4179 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
4180 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
4181 RX_QUIET_EN);
4182 config1 |= sd_rise_time(7);
4183 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
4184 config3 |= fast_snr(511);
4185 }
4186
4187 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
4188 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
4189 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
4190 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
4191}
4192
Hayes Wange7bde562019-08-23 15:33:41 +08004193static void r8153_eee_en(struct r8152 *tp, bool enable)
hayeswange6449532016-09-20 16:22:05 +08004194{
Hayes Wange7bde562019-08-23 15:33:41 +08004195 u32 ocp_data;
4196 u16 config;
4197
4198 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4199 config = ocp_reg_read(tp, OCP_EEE_CFG);
4200
4201 if (enable) {
4202 ocp_data |= EEE_RX_EN | EEE_TX_EN;
4203 config |= EEE10_EN;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004204 } else {
Hayes Wange7bde562019-08-23 15:33:41 +08004205 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
4206 config &= ~EEE10_EN;
4207 }
4208
4209 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
4210 ocp_reg_write(tp, OCP_EEE_CFG, config);
Hayes Wange7bde562019-08-23 15:33:41 +08004211
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004212 tp->ups_info.eee = enable;
Hayes Wange7bde562019-08-23 15:33:41 +08004213}
4214
4215static void rtl_eee_enable(struct r8152 *tp, bool enable)
4216{
4217 switch (tp->version) {
4218 case RTL_VER_01:
4219 case RTL_VER_02:
4220 case RTL_VER_07:
4221 if (enable) {
4222 r8152_eee_en(tp, true);
4223 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
4224 tp->eee_adv);
4225 } else {
4226 r8152_eee_en(tp, false);
4227 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
4228 }
4229 break;
4230 case RTL_VER_03:
4231 case RTL_VER_04:
4232 case RTL_VER_05:
4233 case RTL_VER_06:
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004234 case RTL_VER_08:
4235 case RTL_VER_09:
Hayes Wange7bde562019-08-23 15:33:41 +08004236 if (enable) {
4237 r8153_eee_en(tp, true);
4238 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
4239 } else {
4240 r8153_eee_en(tp, false);
4241 ocp_reg_write(tp, OCP_EEE_ADV, 0);
4242 }
4243 break;
Hayes Wange7bde562019-08-23 15:33:41 +08004244 default:
4245 break;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004246 }
hayeswange6449532016-09-20 16:22:05 +08004247}
4248
4249static void r8152b_enable_fc(struct r8152 *tp)
4250{
4251 u16 anar;
4252
4253 anar = r8152_mdio_read(tp, MII_ADVERTISE);
4254 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4255 r8152_mdio_write(tp, MII_ADVERTISE, anar);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004256
4257 tp->ups_info.flow_control = true;
hayeswange6449532016-09-20 16:22:05 +08004258}
4259
hayeswangd70b1132014-09-19 15:17:18 +08004260static void rtl8152_disable(struct r8152 *tp)
4261{
hayeswangcda9fb02016-01-07 17:51:12 +08004262 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004263 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004264 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004265}
4266
hayeswang43499682014-02-18 21:48:58 +08004267static void r8152b_hw_phy_cfg(struct r8152 *tp)
4268{
Hayes Wang9370f2d2019-10-16 11:02:42 +08004269 rtl8152_apply_firmware(tp);
Hayes Wange7bde562019-08-23 15:33:41 +08004270 rtl_eee_enable(tp, tp->eee_en);
hayeswangef39df82016-09-20 16:22:07 +08004271 r8152_aldps_en(tp, true);
4272 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08004273
hayeswangaa66a5f2014-02-18 21:49:04 +08004274 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08004275}
4276
Prashant Malani5f71c842019-10-01 01:35:57 -07004277static void wait_oob_link_list_ready(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004278{
hayeswangdb8515e2014-03-06 15:07:16 +08004279 u32 ocp_data;
4280 int i;
hayeswangac718b62013-05-02 16:01:25 +00004281
Prashant Malani5f71c842019-10-01 01:35:57 -07004282 for (i = 0; i < 1000; i++) {
4283 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4284 if (ocp_data & LINK_LIST_READY)
4285 break;
4286 usleep_range(1000, 2000);
4287 }
4288}
4289
4290static void r8152b_exit_oob(struct r8152 *tp)
4291{
4292 u32 ocp_data;
4293
hayeswangac718b62013-05-02 16:01:25 +00004294 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4295 ocp_data &= ~RCR_ACPT_ALL;
4296 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4297
hayeswang00a5e362014-02-18 21:48:59 +08004298 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08004299 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00004300 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
4301 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
4302
4303 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4304 ocp_data &= ~NOW_IS_OOB;
4305 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4306
4307 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4308 ocp_data &= ~MCU_BORW_EN;
4309 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4310
Prashant Malani5f71c842019-10-01 01:35:57 -07004311 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004312
4313 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4314 ocp_data |= RE_INIT_LL;
4315 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4316
Prashant Malani5f71c842019-10-01 01:35:57 -07004317 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004318
4319 rtl8152_nic_reset(tp);
4320
4321 /* rx share fifo credit full threshold */
4322 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
4323
hayeswanga3cc4652014-07-24 16:37:43 +08004324 if (tp->udev->speed == USB_SPEED_FULL ||
4325 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00004326 /* rx share fifo credit near full threshold */
4327 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
4328 RXFIFO_THR2_FULL);
4329 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
4330 RXFIFO_THR3_FULL);
4331 } else {
4332 /* rx share fifo credit near full threshold */
4333 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
4334 RXFIFO_THR2_HIGH);
4335 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
4336 RXFIFO_THR3_HIGH);
4337 }
4338
4339 /* TX share fifo free credit full threshold */
4340 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
4341
4342 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08004343 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00004344 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
4345 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
4346
hayeswangc5554292014-09-12 10:43:11 +08004347 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00004348
4349 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
4350
4351 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
4352 ocp_data |= TCR0_AUTO_FIFO;
4353 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
4354}
4355
4356static void r8152b_enter_oob(struct r8152 *tp)
4357{
hayeswang45f4a192014-01-06 17:08:41 +08004358 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00004359
4360 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4361 ocp_data &= ~NOW_IS_OOB;
4362 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4363
4364 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
4365 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
4366 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
4367
hayeswangd70b1132014-09-19 15:17:18 +08004368 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00004369
Prashant Malani5f71c842019-10-01 01:35:57 -07004370 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004371
4372 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4373 ocp_data |= RE_INIT_LL;
4374 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4375
Prashant Malani5f71c842019-10-01 01:35:57 -07004376 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004377
4378 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
4379
hayeswangc5554292014-09-12 10:43:11 +08004380 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00004381
Kevin Lo59c0b472019-08-01 11:29:38 +08004382 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00004383 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08004384 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00004385
4386 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4387 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
4388 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4389
hayeswang00a5e362014-02-18 21:48:59 +08004390 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004391
4392 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4393 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
4394 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4395}
4396
Hayes Wang9370f2d2019-10-16 11:02:42 +08004397static int r8153_pre_firmware_1(struct r8152 *tp)
4398{
4399 int i;
4400
4401 /* Wait till the WTD timer is ready. It would take at most 104 ms. */
4402 for (i = 0; i < 104; i++) {
4403 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
4404
4405 if (!(ocp_data & WTD1_EN))
4406 break;
4407 usleep_range(1000, 2000);
4408 }
4409
4410 return 0;
4411}
4412
4413static int r8153_post_firmware_1(struct r8152 *tp)
4414{
4415 /* set USB_BP_4 to support USB_SPEED_SUPER only */
4416 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
4417 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
4418
4419 /* reset UPHY timer to 36 ms */
4420 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
4421
4422 return 0;
4423}
4424
4425static int r8153_pre_firmware_2(struct r8152 *tp)
4426{
4427 u32 ocp_data;
4428
4429 r8153_pre_firmware_1(tp);
4430
4431 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
4432 ocp_data &= ~FW_FIX_SUSPEND;
4433 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
4434
4435 return 0;
4436}
4437
4438static int r8153_post_firmware_2(struct r8152 *tp)
4439{
4440 u32 ocp_data;
4441
4442 /* enable bp0 if support USB_SPEED_SUPER only */
4443 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
4444 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
4445 ocp_data |= BIT(0);
4446 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
4447 }
4448
4449 /* reset UPHY timer to 36 ms */
4450 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
4451
4452 /* enable U3P3 check, set the counter to 4 */
4453 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
4454
4455 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
4456 ocp_data |= FW_FIX_SUSPEND;
4457 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
4458
4459 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4460 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4461 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4462
4463 return 0;
4464}
4465
4466static int r8153_post_firmware_3(struct r8152 *tp)
4467{
4468 u32 ocp_data;
4469
4470 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4471 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4472 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4473
4474 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
4475 ocp_data |= FW_IP_RESET_EN;
4476 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
4477
4478 return 0;
4479}
4480
4481static int r8153b_pre_firmware_1(struct r8152 *tp)
4482{
4483 /* enable fc timer and set timer to 1 second. */
4484 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
4485 CTRL_TIMER_EN | (1000 / 8));
4486
4487 return 0;
4488}
4489
4490static int r8153b_post_firmware_1(struct r8152 *tp)
4491{
4492 u32 ocp_data;
4493
4494 /* enable bp0 for RTL8153-BND */
4495 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
4496 if (ocp_data & BND_MASK) {
4497 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
4498 ocp_data |= BIT(0);
4499 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
4500 }
4501
4502 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
4503 ocp_data |= FLOW_CTRL_PATCH_OPT;
4504 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
4505
4506 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
4507 ocp_data |= FC_PATCH_TASK;
4508 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
4509
4510 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
4511 ocp_data |= FW_IP_RESET_EN;
4512 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
4513
4514 return 0;
4515}
4516
hayeswange6449532016-09-20 16:22:05 +08004517static void r8153_aldps_en(struct r8152 *tp, bool enable)
4518{
4519 u16 data;
4520
4521 data = ocp_reg_read(tp, OCP_POWER_CFG);
4522 if (enable) {
4523 data |= EN_ALDPS;
4524 ocp_reg_write(tp, OCP_POWER_CFG, data);
4525 } else {
hayeswang4214cc52017-06-09 17:11:46 +08004526 int i;
4527
hayeswange6449532016-09-20 16:22:05 +08004528 data &= ~EN_ALDPS;
4529 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08004530 for (i = 0; i < 20; i++) {
4531 usleep_range(1000, 2000);
4532 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
4533 break;
4534 }
hayeswange6449532016-09-20 16:22:05 +08004535 }
hayeswange6449532016-09-20 16:22:05 +08004536
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004537 tp->ups_info.aldps = enable;
hayeswang65b82d62017-06-15 14:44:03 +08004538}
4539
hayeswang43779f82014-01-02 11:25:10 +08004540static void r8153_hw_phy_cfg(struct r8152 *tp)
4541{
4542 u32 ocp_data;
4543 u16 data;
4544
hayeswangd768c612016-09-20 16:22:09 +08004545 /* disable ALDPS before updating the PHY parameters */
4546 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08004547
hayeswangd768c612016-09-20 16:22:09 +08004548 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08004549 rtl_eee_enable(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004550
Hayes Wang9370f2d2019-10-16 11:02:42 +08004551 rtl8152_apply_firmware(tp);
4552
hayeswang43779f82014-01-02 11:25:10 +08004553 if (tp->version == RTL_VER_03) {
4554 data = ocp_reg_read(tp, OCP_EEE_CFG);
4555 data &= ~CTAP_SHORT_EN;
4556 ocp_reg_write(tp, OCP_EEE_CFG, data);
4557 }
4558
4559 data = ocp_reg_read(tp, OCP_POWER_CFG);
4560 data |= EEE_CLKDIV_EN;
4561 ocp_reg_write(tp, OCP_POWER_CFG, data);
4562
4563 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
4564 data |= EN_10M_BGOFF;
4565 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
4566 data = ocp_reg_read(tp, OCP_POWER_CFG);
4567 data |= EN_10M_PLLOFF;
4568 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08004569 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08004570
4571 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4572 ocp_data |= PFM_PWM_SWITCH;
4573 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4574
hayeswangb4d99de2015-01-19 17:02:46 +08004575 /* Enable LPF corner auto tune */
4576 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08004577
hayeswangb4d99de2015-01-19 17:02:46 +08004578 /* Adjust 10M Amplitude */
4579 sram_write(tp, SRAM_10M_AMP1, 0x00af);
4580 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08004581
Hayes Wange7bde562019-08-23 15:33:41 +08004582 if (tp->eee_en)
4583 rtl_eee_enable(tp, true);
hayeswangaf0287e2016-09-20 16:22:08 +08004584
hayeswangef39df82016-09-20 16:22:07 +08004585 r8153_aldps_en(tp, true);
4586 r8152b_enable_fc(tp);
4587
hayeswang3cb32342017-06-09 17:11:43 +08004588 switch (tp->version) {
4589 case RTL_VER_03:
4590 case RTL_VER_04:
4591 break;
4592 case RTL_VER_05:
4593 case RTL_VER_06:
4594 default:
4595 r8153_u2p3en(tp, true);
4596 break;
4597 }
4598
hayeswangaa66a5f2014-02-18 21:49:04 +08004599 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08004600}
4601
hayeswang65b82d62017-06-15 14:44:03 +08004602static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
4603{
4604 u32 ocp_data;
4605
4606 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
4607 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
4608 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
4609 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
4610
4611 return ocp_data;
4612}
4613
4614static void r8153b_hw_phy_cfg(struct r8152 *tp)
4615{
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004616 u32 ocp_data;
hayeswang65b82d62017-06-15 14:44:03 +08004617 u16 data;
4618
4619 /* disable ALDPS before updating the PHY parameters */
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004620 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004621
4622 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08004623 rtl_eee_enable(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004624
Hayes Wang9370f2d2019-10-16 11:02:42 +08004625 rtl8152_apply_firmware(tp);
4626
hayeswang65b82d62017-06-15 14:44:03 +08004627 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
4628
4629 data = sram_read(tp, SRAM_GREEN_CFG);
4630 data |= R_TUNE_EN;
4631 sram_write(tp, SRAM_GREEN_CFG, data);
4632 data = ocp_reg_read(tp, OCP_NCTL_CFG);
4633 data |= PGA_RETURN_EN;
4634 ocp_reg_write(tp, OCP_NCTL_CFG, data);
4635
4636 /* ADC Bias Calibration:
4637 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
4638 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
4639 * ADC ioffset.
4640 */
4641 ocp_data = r8152_efuse_read(tp, 0x7d);
4642 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
4643 if (data != 0xffff)
4644 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
4645
4646 /* ups mode tx-link-pulse timing adjustment:
4647 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
4648 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
4649 */
4650 ocp_data = ocp_reg_read(tp, 0xc426);
4651 ocp_data &= 0x3fff;
4652 if (ocp_data) {
4653 u32 swr_cnt_1ms_ini;
4654
4655 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
4656 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
4657 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
4658 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
4659 }
4660
4661 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4662 ocp_data |= PFM_PWM_SWITCH;
4663 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4664
4665 /* Advnace EEE */
4666 if (!r8153_patch_request(tp, true)) {
4667 data = ocp_reg_read(tp, OCP_POWER_CFG);
4668 data |= EEE_CLKDIV_EN;
4669 ocp_reg_write(tp, OCP_POWER_CFG, data);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004670 tp->ups_info.eee_ckdiv = true;
hayeswang65b82d62017-06-15 14:44:03 +08004671
4672 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
4673 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
4674 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004675 tp->ups_info.eee_cmod_lv = true;
4676 tp->ups_info._10m_ckdiv = true;
4677 tp->ups_info.eee_plloff_giga = true;
hayeswang65b82d62017-06-15 14:44:03 +08004678
4679 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
4680 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004681 tp->ups_info._250m_ckdiv = true;
hayeswang65b82d62017-06-15 14:44:03 +08004682
4683 r8153_patch_request(tp, false);
4684 }
4685
Hayes Wange7bde562019-08-23 15:33:41 +08004686 if (tp->eee_en)
4687 rtl_eee_enable(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08004688
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004689 r8153_aldps_en(tp, true);
4690 r8152b_enable_fc(tp);
hayeswang65b82d62017-06-15 14:44:03 +08004691 r8153_u2p3en(tp, true);
4692
4693 set_bit(PHY_RESET, &tp->flags);
4694}
4695
hayeswang43779f82014-01-02 11:25:10 +08004696static void r8153_first_init(struct r8152 *tp)
4697{
4698 u32 ocp_data;
hayeswang43779f82014-01-02 11:25:10 +08004699
hayeswang134f98b2017-06-09 17:11:40 +08004700 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08004701 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08004702 r8153_teredo_off(tp);
4703
4704 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4705 ocp_data &= ~RCR_ACPT_ALL;
4706 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4707
hayeswang43779f82014-01-02 11:25:10 +08004708 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08004709 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08004710
4711 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4712 ocp_data &= ~NOW_IS_OOB;
4713 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4714
4715 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4716 ocp_data &= ~MCU_BORW_EN;
4717 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4718
Prashant Malani5f71c842019-10-01 01:35:57 -07004719 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004720
4721 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4722 ocp_data |= RE_INIT_LL;
4723 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4724
Prashant Malani5f71c842019-10-01 01:35:57 -07004725 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004726
hayeswangc5554292014-09-12 10:43:11 +08004727 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08004728
hayeswangb65c0c92017-06-21 11:25:18 +08004729 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08004730 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08004731 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08004732
4733 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
4734 ocp_data |= TCR0_AUTO_FIFO;
4735 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
4736
4737 rtl8152_nic_reset(tp);
4738
4739 /* rx share fifo credit full threshold */
4740 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
4741 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
4742 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
4743 /* TX share fifo free credit full threshold */
4744 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08004745}
4746
4747static void r8153_enter_oob(struct r8152 *tp)
4748{
4749 u32 ocp_data;
hayeswang43779f82014-01-02 11:25:10 +08004750
hayeswang134f98b2017-06-09 17:11:40 +08004751 r8153_mac_clk_spd(tp, true);
4752
hayeswang43779f82014-01-02 11:25:10 +08004753 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4754 ocp_data &= ~NOW_IS_OOB;
4755 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4756
hayeswangd70b1132014-09-19 15:17:18 +08004757 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08004758 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08004759
Prashant Malani5f71c842019-10-01 01:35:57 -07004760 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004761
4762 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4763 ocp_data |= RE_INIT_LL;
4764 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4765
Prashant Malani5f71c842019-10-01 01:35:57 -07004766 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004767
hayeswangb65c0c92017-06-21 11:25:18 +08004768 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08004769 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004770
hayeswang65b82d62017-06-15 14:44:03 +08004771 switch (tp->version) {
4772 case RTL_VER_03:
4773 case RTL_VER_04:
4774 case RTL_VER_05:
4775 case RTL_VER_06:
4776 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
4777 ocp_data &= ~TEREDO_WAKE_MASK;
4778 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
4779 break;
4780
4781 case RTL_VER_08:
4782 case RTL_VER_09:
4783 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
4784 * type. Set it to zero. bits[7:0] are the W1C bits about
4785 * the events. Set them to all 1 to clear them.
4786 */
4787 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
4788 break;
4789
4790 default:
4791 break;
4792 }
hayeswang43779f82014-01-02 11:25:10 +08004793
hayeswangc5554292014-09-12 10:43:11 +08004794 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08004795
Kevin Lo59c0b472019-08-01 11:29:38 +08004796 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08004797 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08004798 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004799
4800 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4801 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
4802 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4803
hayeswang00a5e362014-02-18 21:48:59 +08004804 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004805
4806 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4807 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
4808 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4809}
4810
hayeswangd70b1132014-09-19 15:17:18 +08004811static void rtl8153_disable(struct r8152 *tp)
4812{
hayeswangcda9fb02016-01-07 17:51:12 +08004813 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004814 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08004815 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004816 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004817}
4818
Hayes Wang771efed2019-09-02 19:52:28 +08004819static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
4820 u32 advertising)
hayeswangac718b62013-05-02 16:01:25 +00004821{
Hayes Wang771efed2019-09-02 19:52:28 +08004822 u16 bmcr;
hayeswangac718b62013-05-02 16:01:25 +00004823 int ret = 0;
4824
hayeswangac718b62013-05-02 16:01:25 +00004825 if (autoneg == AUTONEG_DISABLE) {
Hayes Wang771efed2019-09-02 19:52:28 +08004826 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
4827 return -EINVAL;
4828
4829 switch (speed) {
4830 case SPEED_10:
4831 bmcr = BMCR_SPEED10;
4832 if (duplex == DUPLEX_FULL) {
4833 bmcr |= BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004834 tp->ups_info.speed_duplex = FORCE_10M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004835 } else {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004836 tp->ups_info.speed_duplex = FORCE_10M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004837 }
4838 break;
4839 case SPEED_100:
hayeswangac718b62013-05-02 16:01:25 +00004840 bmcr = BMCR_SPEED100;
Hayes Wang771efed2019-09-02 19:52:28 +08004841 if (duplex == DUPLEX_FULL) {
4842 bmcr |= BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004843 tp->ups_info.speed_duplex = FORCE_100M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004844 } else {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004845 tp->ups_info.speed_duplex = FORCE_100M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004846 }
4847 break;
4848 case SPEED_1000:
4849 if (tp->mii.supports_gmii) {
4850 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004851 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004852 break;
4853 }
4854 /* fall through */
4855 default:
hayeswangac718b62013-05-02 16:01:25 +00004856 ret = -EINVAL;
4857 goto out;
4858 }
4859
Hayes Wang771efed2019-09-02 19:52:28 +08004860 if (duplex == DUPLEX_FULL)
4861 tp->mii.full_duplex = 1;
4862 else
4863 tp->mii.full_duplex = 0;
4864
4865 tp->mii.force_media = 1;
hayeswangac718b62013-05-02 16:01:25 +00004866 } else {
Hayes Wang771efed2019-09-02 19:52:28 +08004867 u16 anar, tmp1;
4868 u32 support;
4869
4870 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
4871 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
4872
4873 if (tp->mii.supports_gmii)
4874 support |= RTL_ADVERTISED_1000_FULL;
4875
4876 if (!(advertising & support))
4877 return -EINVAL;
4878
4879 anar = r8152_mdio_read(tp, MII_ADVERTISE);
4880 tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
4881 ADVERTISE_100HALF | ADVERTISE_100FULL);
4882 if (advertising & RTL_ADVERTISED_10_HALF) {
4883 tmp1 |= ADVERTISE_10HALF;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004884 tp->ups_info.speed_duplex = NWAY_10M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004885 }
4886 if (advertising & RTL_ADVERTISED_10_FULL) {
4887 tmp1 |= ADVERTISE_10FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004888 tp->ups_info.speed_duplex = NWAY_10M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004889 }
4890
4891 if (advertising & RTL_ADVERTISED_100_HALF) {
4892 tmp1 |= ADVERTISE_100HALF;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004893 tp->ups_info.speed_duplex = NWAY_100M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004894 }
4895 if (advertising & RTL_ADVERTISED_100_FULL) {
4896 tmp1 |= ADVERTISE_100FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004897 tp->ups_info.speed_duplex = NWAY_100M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004898 }
4899
4900 if (anar != tmp1) {
4901 r8152_mdio_write(tp, MII_ADVERTISE, tmp1);
4902 tp->mii.advertising = tmp1;
4903 }
4904
4905 if (tp->mii.supports_gmii) {
4906 u16 gbcr;
4907
4908 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
4909 tmp1 = gbcr & ~(ADVERTISE_1000FULL |
4910 ADVERTISE_1000HALF);
4911
4912 if (advertising & RTL_ADVERTISED_1000_FULL) {
4913 tmp1 |= ADVERTISE_1000FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004914 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
hayeswang65b82d62017-06-15 14:44:03 +08004915 }
Hayes Wang771efed2019-09-02 19:52:28 +08004916
4917 if (gbcr != tmp1)
4918 r8152_mdio_write(tp, MII_CTRL1000, tmp1);
hayeswangac718b62013-05-02 16:01:25 +00004919 }
4920
4921 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Hayes Wang771efed2019-09-02 19:52:28 +08004922
4923 tp->mii.force_media = 0;
hayeswangac718b62013-05-02 16:01:25 +00004924 }
4925
hayeswangfae56172016-06-16 14:08:29 +08004926 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08004927 bmcr |= BMCR_RESET;
4928
hayeswangac718b62013-05-02 16:01:25 +00004929 r8152_mdio_write(tp, MII_BMCR, bmcr);
4930
hayeswangfae56172016-06-16 14:08:29 +08004931 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08004932 int i;
4933
hayeswangaa66a5f2014-02-18 21:49:04 +08004934 for (i = 0; i < 50; i++) {
4935 msleep(20);
4936 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
4937 break;
4938 }
4939 }
4940
hayeswangac718b62013-05-02 16:01:25 +00004941out:
hayeswangac718b62013-05-02 16:01:25 +00004942 return ret;
4943}
4944
hayeswangd70b1132014-09-19 15:17:18 +08004945static void rtl8152_up(struct r8152 *tp)
4946{
4947 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4948 return;
4949
hayeswangcda9fb02016-01-07 17:51:12 +08004950 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004951 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004952 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004953}
4954
hayeswangac718b62013-05-02 16:01:25 +00004955static void rtl8152_down(struct r8152 *tp)
4956{
hayeswang68714382014-04-11 17:54:31 +08004957 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4958 rtl_drop_queued_tx(tp);
4959 return;
4960 }
4961
hayeswang00a5e362014-02-18 21:48:59 +08004962 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08004963 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004964 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004965 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00004966}
4967
hayeswangd70b1132014-09-19 15:17:18 +08004968static void rtl8153_up(struct r8152 *tp)
4969{
4970 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4971 return;
4972
hayeswangb2143962015-07-24 13:54:23 +08004973 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08004974 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08004975 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004976 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004977 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08004978
4979 switch (tp->version) {
4980 case RTL_VER_03:
4981 case RTL_VER_04:
4982 break;
4983 case RTL_VER_05:
4984 case RTL_VER_06:
4985 default:
4986 r8153_u2p3en(tp, true);
4987 break;
4988 }
4989
hayeswangb2143962015-07-24 13:54:23 +08004990 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004991}
4992
hayeswang43779f82014-01-02 11:25:10 +08004993static void rtl8153_down(struct r8152 *tp)
4994{
hayeswang68714382014-04-11 17:54:31 +08004995 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4996 rtl_drop_queued_tx(tp);
4997 return;
4998 }
4999
hayeswangb9702722014-02-18 21:49:00 +08005000 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08005001 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08005002 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08005003 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005004 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08005005 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08005006}
5007
hayeswang65b82d62017-06-15 14:44:03 +08005008static void rtl8153b_up(struct r8152 *tp)
5009{
5010 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5011 return;
5012
5013 r8153b_u1u2en(tp, false);
5014 r8153_u2p3en(tp, false);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005015 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005016
5017 r8153_first_init(tp);
5018 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
5019
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005020 r8153_aldps_en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08005021 r8153_u2p3en(tp, true);
5022 r8153b_u1u2en(tp, true);
5023}
5024
5025static void rtl8153b_down(struct r8152 *tp)
5026{
5027 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
5028 rtl_drop_queued_tx(tp);
5029 return;
5030 }
5031
5032 r8153b_u1u2en(tp, false);
5033 r8153_u2p3en(tp, false);
5034 r8153b_power_cut_en(tp, false);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005035 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005036 r8153_enter_oob(tp);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005037 r8153_aldps_en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08005038}
5039
hayeswang2dd49e02015-09-07 11:57:44 +08005040static bool rtl8152_in_nway(struct r8152 *tp)
5041{
5042 u16 nway_state;
5043
5044 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
5045 tp->ocp_base = 0x2000;
5046 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
5047 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
5048
5049 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
5050 if (nway_state & 0xc000)
5051 return false;
5052 else
5053 return true;
5054}
5055
5056static bool rtl8153_in_nway(struct r8152 *tp)
5057{
5058 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
5059
5060 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
5061 return false;
5062 else
5063 return true;
5064}
5065
hayeswangac718b62013-05-02 16:01:25 +00005066static void set_carrier(struct r8152 *tp)
5067{
5068 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08005069 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00005070 u8 speed;
5071
5072 speed = rtl8152_get_speed(tp);
5073
5074 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08005075 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08005076 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08005077 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08005078 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00005079 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08005080 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08005081 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5082 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08005083 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08005084 netif_wake_queue(netdev);
5085 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08005086 } else if (netif_queue_stopped(netdev) &&
5087 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
5088 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00005089 }
5090 } else {
hayeswang51d979f2015-02-06 11:30:47 +08005091 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00005092 netif_carrier_off(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08005093 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08005094 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08005095 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08005096 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005097 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08005098 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00005099 }
5100 }
hayeswangac718b62013-05-02 16:01:25 +00005101}
5102
5103static void rtl_work_func_t(struct work_struct *work)
5104{
5105 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
5106
hayeswanga1f83fe2014-11-12 10:05:05 +08005107 /* If the device is unplugged or !netif_running(), the workqueue
5108 * doesn't need to wake the device, and could return directly.
5109 */
5110 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
5111 return;
5112
hayeswang9a4be1b2014-02-18 21:49:07 +08005113 if (usb_autopm_get_interface(tp->intf) < 0)
5114 return;
5115
hayeswangac718b62013-05-02 16:01:25 +00005116 if (!test_bit(WORK_ENABLE, &tp->flags))
5117 goto out1;
5118
hayeswangb5403272014-10-09 18:00:26 +08005119 if (!mutex_trylock(&tp->control)) {
5120 schedule_delayed_work(&tp->schedule, 0);
5121 goto out1;
5122 }
5123
hayeswang216a8342016-01-07 17:51:11 +08005124 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08005125 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00005126
hayeswang216a8342016-01-07 17:51:11 +08005127 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00005128 _rtl8152_set_rx_mode(tp->netdev);
5129
Hayes Wangd2187f82019-08-19 14:40:36 +08005130 /* don't schedule tasket before linking */
5131 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
hayeswang216a8342016-01-07 17:51:11 +08005132 netif_carrier_ok(tp->netdev))
Hayes Wangd2187f82019-08-19 14:40:36 +08005133 tasklet_schedule(&tp->tx_tl);
hayeswangaa66a5f2014-02-18 21:49:04 +08005134
hayeswangb5403272014-10-09 18:00:26 +08005135 mutex_unlock(&tp->control);
5136
hayeswangac718b62013-05-02 16:01:25 +00005137out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08005138 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00005139}
5140
hayeswanga028a9e2016-06-13 17:49:36 +08005141static void rtl_hw_phy_work_func_t(struct work_struct *work)
5142{
5143 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
5144
5145 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5146 return;
5147
5148 if (usb_autopm_get_interface(tp->intf) < 0)
5149 return;
5150
5151 mutex_lock(&tp->control);
5152
Hayes Wang9370f2d2019-10-16 11:02:42 +08005153 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
5154 tp->rtl_fw.retry = false;
5155 tp->rtl_fw.fw = NULL;
5156
5157 /* Delay execution in case request_firmware() is not ready yet.
5158 */
5159 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
5160 goto ignore_once;
5161 }
5162
hayeswanga028a9e2016-06-13 17:49:36 +08005163 tp->rtl_ops.hw_phy_cfg(tp);
5164
Hayes Wang771efed2019-09-02 19:52:28 +08005165 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
5166 tp->advertising);
hayeswang9d21c0d2016-06-13 17:49:37 +08005167
Hayes Wang9370f2d2019-10-16 11:02:42 +08005168ignore_once:
hayeswanga028a9e2016-06-13 17:49:36 +08005169 mutex_unlock(&tp->control);
5170
5171 usb_autopm_put_interface(tp->intf);
5172}
5173
hayeswang5ee3c602016-01-07 17:12:17 +08005174#ifdef CONFIG_PM_SLEEP
5175static int rtl_notifier(struct notifier_block *nb, unsigned long action,
5176 void *data)
5177{
5178 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
5179
5180 switch (action) {
5181 case PM_HIBERNATION_PREPARE:
5182 case PM_SUSPEND_PREPARE:
5183 usb_autopm_get_interface(tp->intf);
5184 break;
5185
5186 case PM_POST_HIBERNATION:
5187 case PM_POST_SUSPEND:
5188 usb_autopm_put_interface(tp->intf);
5189 break;
5190
5191 case PM_POST_RESTORE:
5192 case PM_RESTORE_PREPARE:
5193 default:
5194 break;
5195 }
5196
5197 return NOTIFY_DONE;
5198}
5199#endif
5200
hayeswangac718b62013-05-02 16:01:25 +00005201static int rtl8152_open(struct net_device *netdev)
5202{
5203 struct r8152 *tp = netdev_priv(netdev);
5204 int res = 0;
5205
Hayes Wang9370f2d2019-10-16 11:02:42 +08005206 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
5207 cancel_delayed_work_sync(&tp->hw_phy_work);
5208 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
5209 }
5210
hayeswang7e9da482014-02-18 21:49:05 +08005211 res = alloc_all_mem(tp);
5212 if (res)
5213 goto out;
5214
hayeswang9a4be1b2014-02-18 21:49:07 +08005215 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08005216 if (res < 0)
5217 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08005218
hayeswangb5403272014-10-09 18:00:26 +08005219 mutex_lock(&tp->control);
5220
hayeswang7e9da482014-02-18 21:49:05 +08005221 tp->rtl_ops.up(tp);
5222
hayeswang40a82912013-08-14 20:54:40 +08005223 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00005224 netif_start_queue(netdev);
5225 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08005226
hayeswang3d55f442014-02-06 11:55:48 +08005227 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
5228 if (res) {
5229 if (res == -ENODEV)
5230 netif_device_detach(tp->netdev);
5231 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
5232 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08005233 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08005234 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08005235 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005236 tasklet_enable(&tp->tx_tl);
hayeswang3d55f442014-02-06 11:55:48 +08005237
hayeswangb5403272014-10-09 18:00:26 +08005238 mutex_unlock(&tp->control);
5239
hayeswang9a4be1b2014-02-18 21:49:07 +08005240 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08005241#ifdef CONFIG_PM_SLEEP
5242 tp->pm_notifier.notifier_call = rtl_notifier;
5243 register_pm_notifier(&tp->pm_notifier);
5244#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08005245 return 0;
hayeswangac718b62013-05-02 16:01:25 +00005246
Guenter Roeckca0a7532016-11-09 19:51:25 -08005247out_unlock:
5248 mutex_unlock(&tp->control);
5249 usb_autopm_put_interface(tp->intf);
5250out_free:
5251 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08005252out:
hayeswangac718b62013-05-02 16:01:25 +00005253 return res;
5254}
5255
5256static int rtl8152_close(struct net_device *netdev)
5257{
5258 struct r8152 *tp = netdev_priv(netdev);
5259 int res = 0;
5260
hayeswang5ee3c602016-01-07 17:12:17 +08005261#ifdef CONFIG_PM_SLEEP
5262 unregister_pm_notifier(&tp->pm_notifier);
5263#endif
Hayes Wangd2187f82019-08-19 14:40:36 +08005264 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00005265 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08005266 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00005267 cancel_delayed_work_sync(&tp->schedule);
Prashant Malani84811412019-11-20 11:40:21 -08005268 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005269 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08005270
5271 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08005272 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08005273 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08005274 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08005275 } else {
hayeswangb5403272014-10-09 18:00:26 +08005276 mutex_lock(&tp->control);
5277
hayeswang9a4be1b2014-02-18 21:49:07 +08005278 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08005279
5280 mutex_unlock(&tp->control);
5281
hayeswang9a4be1b2014-02-18 21:49:07 +08005282 usb_autopm_put_interface(tp->intf);
5283 }
hayeswangac718b62013-05-02 16:01:25 +00005284
hayeswang7e9da482014-02-18 21:49:05 +08005285 free_all_mem(tp);
5286
hayeswangac718b62013-05-02 16:01:25 +00005287 return res;
5288}
5289
hayeswang4f1d4d52014-03-11 16:24:19 +08005290static void rtl_tally_reset(struct r8152 *tp)
5291{
5292 u32 ocp_data;
5293
5294 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
5295 ocp_data |= TALLY_RESET;
5296 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
5297}
5298
hayeswangac718b62013-05-02 16:01:25 +00005299static void r8152b_init(struct r8152 *tp)
5300{
hayeswangebc2ec482013-08-14 20:54:38 +08005301 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08005302 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00005303
hayeswang68714382014-04-11 17:54:31 +08005304 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5305 return;
5306
hayeswang2dd436d2016-09-20 16:22:06 +08005307 data = r8152_mdio_read(tp, MII_BMCR);
5308 if (data & BMCR_PDOWN) {
5309 data &= ~BMCR_PDOWN;
5310 r8152_mdio_write(tp, MII_BMCR, data);
5311 }
5312
hayeswangcda9fb02016-01-07 17:51:12 +08005313 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08005314
hayeswangac718b62013-05-02 16:01:25 +00005315 if (tp->version == RTL_VER_01) {
5316 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
5317 ocp_data &= ~LED_MODE_MASK;
5318 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
5319 }
5320
hayeswang00a5e362014-02-18 21:48:59 +08005321 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00005322
hayeswangac718b62013-05-02 16:01:25 +00005323 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5324 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
5325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5326 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
5327 ocp_data &= ~MCU_CLK_RATIO_MASK;
5328 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
5329 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
5330 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
5331 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
5332 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
5333
hayeswang4f1d4d52014-03-11 16:24:19 +08005334 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00005335
hayeswangebc2ec482013-08-14 20:54:38 +08005336 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00005337 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08005338 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00005339 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5340}
5341
hayeswang43779f82014-01-02 11:25:10 +08005342static void r8153_init(struct r8152 *tp)
5343{
5344 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08005345 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08005346 int i;
5347
hayeswang68714382014-04-11 17:54:31 +08005348 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5349 return;
5350
hayeswangb9702722014-02-18 21:49:00 +08005351 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005352
5353 for (i = 0; i < 500; i++) {
5354 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
5355 AUTOLOAD_DONE)
5356 break;
5357 msleep(20);
5358 }
5359
hayeswangc564b872017-06-09 17:11:38 +08005360 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08005361
hayeswang2dd436d2016-09-20 16:22:06 +08005362 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
5363 tp->version == RTL_VER_05)
5364 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
5365
5366 data = r8152_mdio_read(tp, MII_BMCR);
5367 if (data & BMCR_PDOWN) {
5368 data &= ~BMCR_PDOWN;
5369 r8152_mdio_write(tp, MII_BMCR, data);
5370 }
5371
hayeswangc564b872017-06-09 17:11:38 +08005372 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08005373
hayeswangb9702722014-02-18 21:49:00 +08005374 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005375
hayeswang65bab842015-02-12 16:20:46 +08005376 if (tp->version == RTL_VER_04) {
5377 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
5378 ocp_data &= ~pwd_dn_scale_mask;
5379 ocp_data |= pwd_dn_scale(96);
5380 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
5381
5382 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5383 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5384 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5385 } else if (tp->version == RTL_VER_05) {
5386 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
5387 ocp_data &= ~ECM_ALDPS;
5388 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
5389
5390 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
5391 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
5392 ocp_data &= ~DYNAMIC_BURST;
5393 else
5394 ocp_data |= DYNAMIC_BURST;
5395 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08005396 } else if (tp->version == RTL_VER_06) {
5397 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
5398 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
5399 ocp_data &= ~DYNAMIC_BURST;
5400 else
5401 ocp_data |= DYNAMIC_BURST;
5402 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
Hayes Wanga3914272020-01-22 16:02:05 +08005403
5404 r8153_queue_wake(tp, false);
5405
5406 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
5407 if (rtl8152_get_speed(tp) & LINK_STATUS)
5408 ocp_data |= CUR_LINK_OK;
5409 else
5410 ocp_data &= ~CUR_LINK_OK;
5411 ocp_data |= POLL_LINK_CHG;
5412 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08005413 }
5414
5415 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
5416 ocp_data |= EP4_FULL_FC;
5417 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
5418
hayeswang43779f82014-01-02 11:25:10 +08005419 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
5420 ocp_data &= ~TIMER11_EN;
5421 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
5422
hayeswang43779f82014-01-02 11:25:10 +08005423 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
5424 ocp_data &= ~LED_MODE_MASK;
5425 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
5426
hayeswang65bab842015-02-12 16:20:46 +08005427 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02005428 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08005429 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08005430 else
5431 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08005432 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
5433
5434 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
5435 ocp_data &= ~SEN_VAL_MASK;
5436 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
5437 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
5438
hayeswang65bab842015-02-12 16:20:46 +08005439 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
5440
hayeswangb9702722014-02-18 21:49:00 +08005441 r8153_power_cut_en(tp, false);
Hayes Wanga3914272020-01-22 16:02:05 +08005442 rtl_runtime_suspend_enable(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08005443 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08005444 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08005445 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08005446
hayeswange31f6362017-06-09 17:11:41 +08005447 /* rx aggregation */
5448 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5449 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005450 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
5451 ocp_data |= RX_AGG_DISABLE;
5452
hayeswange31f6362017-06-09 17:11:41 +08005453 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08005454
hayeswang4f1d4d52014-03-11 16:24:19 +08005455 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08005456
5457 switch (tp->udev->speed) {
5458 case USB_SPEED_SUPER:
5459 case USB_SPEED_SUPER_PLUS:
5460 tp->coalesce = COALESCE_SUPER;
5461 break;
5462 case USB_SPEED_HIGH:
5463 tp->coalesce = COALESCE_HIGH;
5464 break;
5465 default:
5466 tp->coalesce = COALESCE_SLOW;
5467 break;
5468 }
hayeswang43779f82014-01-02 11:25:10 +08005469}
5470
hayeswang65b82d62017-06-15 14:44:03 +08005471static void r8153b_init(struct r8152 *tp)
5472{
5473 u32 ocp_data;
5474 u16 data;
5475 int i;
5476
5477 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5478 return;
5479
5480 r8153b_u1u2en(tp, false);
5481
5482 for (i = 0; i < 500; i++) {
5483 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
5484 AUTOLOAD_DONE)
5485 break;
5486 msleep(20);
5487 }
5488
5489 data = r8153_phy_status(tp, 0);
5490
5491 data = r8152_mdio_read(tp, MII_BMCR);
5492 if (data & BMCR_PDOWN) {
5493 data &= ~BMCR_PDOWN;
5494 r8152_mdio_write(tp, MII_BMCR, data);
5495 }
5496
5497 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
5498
5499 r8153_u2p3en(tp, false);
5500
5501 /* MSC timer = 0xfff * 8ms = 32760 ms */
5502 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
5503
5504 /* U1/U2/L1 idle timer. 500 us */
5505 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5506
5507 r8153b_power_cut_en(tp, false);
5508 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08005509 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005510 rtl_runtime_suspend_enable(tp, false);
Hayes Wanga3914272020-01-22 16:02:05 +08005511
5512 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
5513 if (rtl8152_get_speed(tp) & LINK_STATUS)
5514 ocp_data |= CUR_LINK_OK;
5515 else
5516 ocp_data &= ~CUR_LINK_OK;
5517 ocp_data |= POLL_LINK_CHG;
5518 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08005519 r8153b_u1u2en(tp, true);
5520 usb_enable_lpm(tp->udev);
5521
5522 /* MAC clock speed down */
5523 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
5524 ocp_data |= MAC_CLK_SPDWN_EN;
5525 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
5526
5527 set_bit(GREEN_ETHERNET, &tp->flags);
5528
5529 /* rx aggregation */
5530 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5531 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
5532 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5533
5534 rtl_tally_reset(tp);
5535
5536 tp->coalesce = 15000; /* 15 us */
5537}
5538
hayeswange5011392015-07-29 20:39:08 +08005539static int rtl8152_pre_reset(struct usb_interface *intf)
5540{
5541 struct r8152 *tp = usb_get_intfdata(intf);
5542 struct net_device *netdev;
5543
5544 if (!tp)
5545 return 0;
5546
5547 netdev = tp->netdev;
5548 if (!netif_running(netdev))
5549 return 0;
5550
hayeswangde9bf292017-01-26 09:38:32 +08005551 netif_stop_queue(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08005552 tasklet_disable(&tp->tx_tl);
hayeswange5011392015-07-29 20:39:08 +08005553 clear_bit(WORK_ENABLE, &tp->flags);
5554 usb_kill_urb(tp->intr_urb);
5555 cancel_delayed_work_sync(&tp->schedule);
Hayes Wang5b1d9c12019-11-22 16:21:09 +08005556 napi_disable(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08005557 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08005558 mutex_lock(&tp->control);
5559 tp->rtl_ops.disable(tp);
5560 mutex_unlock(&tp->control);
5561 }
5562
5563 return 0;
5564}
5565
5566static int rtl8152_post_reset(struct usb_interface *intf)
5567{
5568 struct r8152 *tp = usb_get_intfdata(intf);
5569 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05005570 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08005571
5572 if (!tp)
5573 return 0;
5574
Mario Limonciello25766272019-04-04 13:46:53 -05005575 /* reset the MAC adddress in case of policy change */
5576 if (determine_ethernet_addr(tp, &sa) >= 0) {
5577 rtnl_lock();
5578 dev_set_mac_address (tp->netdev, &sa, NULL);
5579 rtnl_unlock();
5580 }
5581
hayeswange5011392015-07-29 20:39:08 +08005582 netdev = tp->netdev;
5583 if (!netif_running(netdev))
5584 return 0;
5585
5586 set_bit(WORK_ENABLE, &tp->flags);
5587 if (netif_carrier_ok(netdev)) {
5588 mutex_lock(&tp->control);
5589 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08005590 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08005591 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08005592 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08005593 }
5594
5595 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005596 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08005597 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08005598 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08005599
hayeswang7489bda2017-01-26 09:38:34 +08005600 if (!list_empty(&tp->rx_done))
5601 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08005602
5603 return 0;
hayeswangac718b62013-05-02 16:01:25 +00005604}
5605
hayeswang2dd49e02015-09-07 11:57:44 +08005606static bool delay_autosuspend(struct r8152 *tp)
5607{
5608 bool sw_linking = !!netif_carrier_ok(tp->netdev);
5609 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
5610
5611 /* This means a linking change occurs and the driver doesn't detect it,
5612 * yet. If the driver has disabled tx/rx and hw is linking on, the
5613 * device wouldn't wake up by receiving any packet.
5614 */
5615 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
5616 return true;
5617
5618 /* If the linking down is occurred by nway, the device may miss the
5619 * linking change event. And it wouldn't wake when linking on.
5620 */
5621 if (!sw_linking && tp->rtl_ops.in_nway(tp))
5622 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08005623 else if (!skb_queue_empty(&tp->tx_queue))
5624 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08005625 else
5626 return false;
5627}
5628
hayeswang21cbd0e2017-06-13 15:14:39 +08005629static int rtl8152_runtime_resume(struct r8152 *tp)
5630{
5631 struct net_device *netdev = tp->netdev;
5632
5633 if (netif_running(netdev) && netdev->flags & IFF_UP) {
5634 struct napi_struct *napi = &tp->napi;
5635
5636 tp->rtl_ops.autosuspend_en(tp, false);
5637 napi_disable(napi);
5638 set_bit(WORK_ENABLE, &tp->flags);
5639
5640 if (netif_carrier_ok(netdev)) {
5641 if (rtl8152_get_speed(tp) & LINK_STATUS) {
5642 rtl_start_rx(tp);
5643 } else {
5644 netif_carrier_off(netdev);
5645 tp->rtl_ops.disable(tp);
5646 netif_info(tp, link, netdev, "linking down\n");
5647 }
5648 }
5649
5650 napi_enable(napi);
5651 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5652 smp_mb__after_atomic();
5653
5654 if (!list_empty(&tp->rx_done))
5655 napi_schedule(&tp->napi);
5656
5657 usb_submit_urb(tp->intr_urb, GFP_NOIO);
5658 } else {
5659 if (netdev->flags & IFF_UP)
5660 tp->rtl_ops.autosuspend_en(tp, false);
5661
5662 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5663 }
5664
5665 return 0;
5666}
5667
5668static int rtl8152_system_resume(struct r8152 *tp)
5669{
5670 struct net_device *netdev = tp->netdev;
5671
5672 netif_device_attach(netdev);
5673
Hayes Wang5b1d9c12019-11-22 16:21:09 +08005674 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
hayeswang21cbd0e2017-06-13 15:14:39 +08005675 tp->rtl_ops.up(tp);
5676 netif_carrier_off(netdev);
5677 set_bit(WORK_ENABLE, &tp->flags);
5678 usb_submit_urb(tp->intr_urb, GFP_NOIO);
5679 }
5680
5681 return 0;
5682}
5683
hayeswanga9c54ad2017-01-25 13:41:45 +08005684static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00005685{
hayeswang6cc69f22014-10-17 16:55:08 +08005686 struct net_device *netdev = tp->netdev;
5687 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00005688
hayeswang26afec32017-01-26 09:38:31 +08005689 set_bit(SELECTIVE_SUSPEND, &tp->flags);
5690 smp_mb__after_atomic();
5691
hayeswang8fb28062017-01-10 17:04:06 +08005692 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08005693 u32 rcr = 0;
5694
hayeswang75dc6922017-01-10 17:04:07 +08005695 if (netif_carrier_ok(netdev)) {
5696 u32 ocp_data;
5697
5698 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5699 ocp_data = rcr & ~RCR_ACPT_ALL;
5700 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5701 rxdy_gated_en(tp, true);
5702 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
5703 PLA_OOB_CTRL);
5704 if (!(ocp_data & RXFIFO_EMPTY)) {
5705 rxdy_gated_en(tp, false);
5706 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08005707 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5708 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08005709 ret = -EBUSY;
5710 goto out1;
5711 }
5712 }
5713
hayeswang8fb28062017-01-10 17:04:06 +08005714 clear_bit(WORK_ENABLE, &tp->flags);
5715 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08005716
hayeswang8fb28062017-01-10 17:04:06 +08005717 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08005718
5719 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08005720 struct napi_struct *napi = &tp->napi;
5721
5722 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08005723 rtl_stop_rx(tp);
5724 rxdy_gated_en(tp, false);
5725 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08005726 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08005727 }
hayeswangbd882982017-06-13 15:14:40 +08005728
5729 if (delay_autosuspend(tp)) {
5730 rtl8152_runtime_resume(tp);
5731 ret = -EBUSY;
5732 }
hayeswang6cc69f22014-10-17 16:55:08 +08005733 }
5734
hayeswang8fb28062017-01-10 17:04:06 +08005735out1:
5736 return ret;
5737}
5738
5739static int rtl8152_system_suspend(struct r8152 *tp)
5740{
5741 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08005742
5743 netif_device_detach(netdev);
5744
hayeswange3bd1a82014-10-29 11:12:17 +08005745 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08005746 struct napi_struct *napi = &tp->napi;
5747
hayeswangac718b62013-05-02 16:01:25 +00005748 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08005749 usb_kill_urb(tp->intr_urb);
Hayes Wangd2187f82019-08-19 14:40:36 +08005750 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08005751 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08005752 cancel_delayed_work_sync(&tp->schedule);
5753 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08005754 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005755 tasklet_enable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00005756 }
hayeswang8fb28062017-01-10 17:04:06 +08005757
zhong jiangf7419172018-08-09 09:39:13 +08005758 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08005759}
5760
5761static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
5762{
5763 struct r8152 *tp = usb_get_intfdata(intf);
5764 int ret;
5765
5766 mutex_lock(&tp->control);
5767
5768 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08005769 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08005770 else
5771 ret = rtl8152_system_suspend(tp);
5772
hayeswangb5403272014-10-09 18:00:26 +08005773 mutex_unlock(&tp->control);
5774
hayeswang6cc69f22014-10-17 16:55:08 +08005775 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005776}
5777
5778static int rtl8152_resume(struct usb_interface *intf)
5779{
5780 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08005781 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005782
hayeswangb5403272014-10-09 18:00:26 +08005783 mutex_lock(&tp->control);
5784
hayeswang21cbd0e2017-06-13 15:14:39 +08005785 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
5786 ret = rtl8152_runtime_resume(tp);
5787 else
5788 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00005789
hayeswangb5403272014-10-09 18:00:26 +08005790 mutex_unlock(&tp->control);
5791
hayeswang21cbd0e2017-06-13 15:14:39 +08005792 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005793}
5794
hayeswang7ec25412016-01-04 14:38:46 +08005795static int rtl8152_reset_resume(struct usb_interface *intf)
5796{
5797 struct r8152 *tp = usb_get_intfdata(intf);
5798
5799 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08005800 tp->rtl_ops.init(tp);
5801 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
Kai-Heng Fenga54cdee2019-10-04 20:51:04 +08005802 set_ethernet_addr(tp);
hayeswang7ec25412016-01-04 14:38:46 +08005803 return rtl8152_resume(intf);
5804}
5805
hayeswang21ff2e82014-02-18 21:49:06 +08005806static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
5807{
5808 struct r8152 *tp = netdev_priv(dev);
5809
hayeswang9a4be1b2014-02-18 21:49:07 +08005810 if (usb_autopm_get_interface(tp->intf) < 0)
5811 return;
5812
hayeswang7daed8d2015-07-24 13:54:24 +08005813 if (!rtl_can_wakeup(tp)) {
5814 wol->supported = 0;
5815 wol->wolopts = 0;
5816 } else {
5817 mutex_lock(&tp->control);
5818 wol->supported = WAKE_ANY;
5819 wol->wolopts = __rtl_get_wol(tp);
5820 mutex_unlock(&tp->control);
5821 }
hayeswangb5403272014-10-09 18:00:26 +08005822
hayeswang9a4be1b2014-02-18 21:49:07 +08005823 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08005824}
5825
5826static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
5827{
5828 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08005829 int ret;
5830
hayeswang7daed8d2015-07-24 13:54:24 +08005831 if (!rtl_can_wakeup(tp))
5832 return -EOPNOTSUPP;
5833
Florian Fainellif2750df2018-09-28 16:18:54 -07005834 if (wol->wolopts & ~WAKE_ANY)
5835 return -EINVAL;
5836
hayeswang9a4be1b2014-02-18 21:49:07 +08005837 ret = usb_autopm_get_interface(tp->intf);
5838 if (ret < 0)
5839 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08005840
hayeswangb5403272014-10-09 18:00:26 +08005841 mutex_lock(&tp->control);
5842
hayeswang21ff2e82014-02-18 21:49:06 +08005843 __rtl_set_wol(tp, wol->wolopts);
5844 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
5845
hayeswangb5403272014-10-09 18:00:26 +08005846 mutex_unlock(&tp->control);
5847
hayeswang9a4be1b2014-02-18 21:49:07 +08005848 usb_autopm_put_interface(tp->intf);
5849
5850out_set_wol:
5851 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08005852}
5853
hayeswanga5ec27c2014-02-18 21:49:11 +08005854static u32 rtl8152_get_msglevel(struct net_device *dev)
5855{
5856 struct r8152 *tp = netdev_priv(dev);
5857
5858 return tp->msg_enable;
5859}
5860
5861static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
5862{
5863 struct r8152 *tp = netdev_priv(dev);
5864
5865 tp->msg_enable = value;
5866}
5867
hayeswangac718b62013-05-02 16:01:25 +00005868static void rtl8152_get_drvinfo(struct net_device *netdev,
5869 struct ethtool_drvinfo *info)
5870{
5871 struct r8152 *tp = netdev_priv(netdev);
5872
hayeswangb0b46c72014-08-26 10:08:23 +08005873 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
5874 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00005875 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
Hayes Wang9370f2d2019-10-16 11:02:42 +08005876 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
5877 strlcpy(info->fw_version, tp->rtl_fw.version,
5878 sizeof(info->fw_version));
hayeswangac718b62013-05-02 16:01:25 +00005879}
5880
5881static
Philippe Reynes06144dc2017-03-12 22:41:58 +01005882int rtl8152_get_link_ksettings(struct net_device *netdev,
5883 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00005884{
5885 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08005886 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005887
5888 if (!tp->mii.mdio_read)
5889 return -EOPNOTSUPP;
5890
hayeswang8d4a4d72014-10-09 18:00:25 +08005891 ret = usb_autopm_get_interface(tp->intf);
5892 if (ret < 0)
5893 goto out;
5894
hayeswangb5403272014-10-09 18:00:26 +08005895 mutex_lock(&tp->control);
5896
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03005897 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08005898
hayeswangb5403272014-10-09 18:00:26 +08005899 mutex_unlock(&tp->control);
5900
hayeswang8d4a4d72014-10-09 18:00:25 +08005901 usb_autopm_put_interface(tp->intf);
5902
5903out:
5904 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005905}
5906
Philippe Reynes06144dc2017-03-12 22:41:58 +01005907static int rtl8152_set_link_ksettings(struct net_device *dev,
5908 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00005909{
5910 struct r8152 *tp = netdev_priv(dev);
Hayes Wang771efed2019-09-02 19:52:28 +08005911 u32 advertising = 0;
hayeswang9a4be1b2014-02-18 21:49:07 +08005912 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005913
hayeswang9a4be1b2014-02-18 21:49:07 +08005914 ret = usb_autopm_get_interface(tp->intf);
5915 if (ret < 0)
5916 goto out;
5917
Hayes Wang771efed2019-09-02 19:52:28 +08005918 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
5919 cmd->link_modes.advertising))
5920 advertising |= RTL_ADVERTISED_10_HALF;
5921
5922 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
5923 cmd->link_modes.advertising))
5924 advertising |= RTL_ADVERTISED_10_FULL;
5925
5926 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
5927 cmd->link_modes.advertising))
5928 advertising |= RTL_ADVERTISED_100_HALF;
5929
5930 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
5931 cmd->link_modes.advertising))
5932 advertising |= RTL_ADVERTISED_100_FULL;
5933
5934 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
5935 cmd->link_modes.advertising))
5936 advertising |= RTL_ADVERTISED_1000_HALF;
5937
5938 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
5939 cmd->link_modes.advertising))
5940 advertising |= RTL_ADVERTISED_1000_FULL;
5941
hayeswangb5403272014-10-09 18:00:26 +08005942 mutex_lock(&tp->control);
5943
Philippe Reynes06144dc2017-03-12 22:41:58 +01005944 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
Hayes Wang771efed2019-09-02 19:52:28 +08005945 cmd->base.duplex, advertising);
hayeswangaa7e26b2016-06-13 17:49:38 +08005946 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01005947 tp->autoneg = cmd->base.autoneg;
5948 tp->speed = cmd->base.speed;
5949 tp->duplex = cmd->base.duplex;
Hayes Wang771efed2019-09-02 19:52:28 +08005950 tp->advertising = advertising;
hayeswangaa7e26b2016-06-13 17:49:38 +08005951 }
hayeswang9a4be1b2014-02-18 21:49:07 +08005952
hayeswangb5403272014-10-09 18:00:26 +08005953 mutex_unlock(&tp->control);
5954
hayeswang9a4be1b2014-02-18 21:49:07 +08005955 usb_autopm_put_interface(tp->intf);
5956
5957out:
5958 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005959}
5960
hayeswang4f1d4d52014-03-11 16:24:19 +08005961static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
5962 "tx_packets",
5963 "rx_packets",
5964 "tx_errors",
5965 "rx_errors",
5966 "rx_missed",
5967 "align_errors",
5968 "tx_single_collisions",
5969 "tx_multi_collisions",
5970 "rx_unicast",
5971 "rx_broadcast",
5972 "rx_multicast",
5973 "tx_aborted",
5974 "tx_underrun",
5975};
5976
5977static int rtl8152_get_sset_count(struct net_device *dev, int sset)
5978{
5979 switch (sset) {
5980 case ETH_SS_STATS:
5981 return ARRAY_SIZE(rtl8152_gstrings);
5982 default:
5983 return -EOPNOTSUPP;
5984 }
5985}
5986
5987static void rtl8152_get_ethtool_stats(struct net_device *dev,
5988 struct ethtool_stats *stats, u64 *data)
5989{
5990 struct r8152 *tp = netdev_priv(dev);
5991 struct tally_counter tally;
5992
hayeswang0b030242014-07-08 14:49:28 +08005993 if (usb_autopm_get_interface(tp->intf) < 0)
5994 return;
5995
hayeswang4f1d4d52014-03-11 16:24:19 +08005996 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
5997
hayeswang0b030242014-07-08 14:49:28 +08005998 usb_autopm_put_interface(tp->intf);
5999
hayeswang4f1d4d52014-03-11 16:24:19 +08006000 data[0] = le64_to_cpu(tally.tx_packets);
6001 data[1] = le64_to_cpu(tally.rx_packets);
6002 data[2] = le64_to_cpu(tally.tx_errors);
6003 data[3] = le32_to_cpu(tally.rx_errors);
6004 data[4] = le16_to_cpu(tally.rx_missed);
6005 data[5] = le16_to_cpu(tally.align_errors);
6006 data[6] = le32_to_cpu(tally.tx_one_collision);
6007 data[7] = le32_to_cpu(tally.tx_multi_collision);
6008 data[8] = le64_to_cpu(tally.rx_unicast);
6009 data[9] = le64_to_cpu(tally.rx_broadcast);
6010 data[10] = le32_to_cpu(tally.rx_multicast);
6011 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08006012 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08006013}
6014
6015static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
6016{
6017 switch (stringset) {
6018 case ETH_SS_STATS:
6019 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
6020 break;
6021 }
6022}
6023
hayeswangdf35d282014-09-25 20:54:02 +08006024static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
6025{
Hayes Wangf4a93be2019-08-23 15:33:40 +08006026 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08006027 u16 val;
6028
6029 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
6030 supported = mmd_eee_cap_to_ethtool_sup_t(val);
6031
6032 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
6033 adv = mmd_eee_adv_to_ethtool_adv_t(val);
6034
6035 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
6036 lp = mmd_eee_adv_to_ethtool_adv_t(val);
6037
Hayes Wangf4a93be2019-08-23 15:33:40 +08006038 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08006039 eee->eee_active = !!(supported & adv & lp);
6040 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006041 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08006042 eee->lp_advertised = lp;
6043
6044 return 0;
6045}
6046
6047static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
6048{
6049 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
6050
Hayes Wangf4a93be2019-08-23 15:33:40 +08006051 tp->eee_en = eee->eee_enabled;
6052 tp->eee_adv = val;
6053
Hayes Wange7bde562019-08-23 15:33:41 +08006054 rtl_eee_enable(tp, tp->eee_en);
hayeswangdf35d282014-09-25 20:54:02 +08006055
6056 return 0;
6057}
6058
6059static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
6060{
Hayes Wangf4a93be2019-08-23 15:33:40 +08006061 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08006062 u16 val;
6063
6064 val = ocp_reg_read(tp, OCP_EEE_ABLE);
6065 supported = mmd_eee_cap_to_ethtool_sup_t(val);
6066
6067 val = ocp_reg_read(tp, OCP_EEE_ADV);
6068 adv = mmd_eee_adv_to_ethtool_adv_t(val);
6069
6070 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
6071 lp = mmd_eee_adv_to_ethtool_adv_t(val);
6072
Hayes Wangf4a93be2019-08-23 15:33:40 +08006073 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08006074 eee->eee_active = !!(supported & adv & lp);
6075 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006076 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08006077 eee->lp_advertised = lp;
6078
6079 return 0;
6080}
6081
hayeswangdf35d282014-09-25 20:54:02 +08006082static int
6083rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
6084{
6085 struct r8152 *tp = netdev_priv(net);
6086 int ret;
6087
6088 ret = usb_autopm_get_interface(tp->intf);
6089 if (ret < 0)
6090 goto out;
6091
hayeswangb5403272014-10-09 18:00:26 +08006092 mutex_lock(&tp->control);
6093
hayeswangdf35d282014-09-25 20:54:02 +08006094 ret = tp->rtl_ops.eee_get(tp, edata);
6095
hayeswangb5403272014-10-09 18:00:26 +08006096 mutex_unlock(&tp->control);
6097
hayeswangdf35d282014-09-25 20:54:02 +08006098 usb_autopm_put_interface(tp->intf);
6099
6100out:
6101 return ret;
6102}
6103
6104static int
6105rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
6106{
6107 struct r8152 *tp = netdev_priv(net);
6108 int ret;
6109
6110 ret = usb_autopm_get_interface(tp->intf);
6111 if (ret < 0)
6112 goto out;
6113
hayeswangb5403272014-10-09 18:00:26 +08006114 mutex_lock(&tp->control);
6115
hayeswangdf35d282014-09-25 20:54:02 +08006116 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08006117 if (!ret)
6118 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08006119
hayeswangb5403272014-10-09 18:00:26 +08006120 mutex_unlock(&tp->control);
6121
hayeswangdf35d282014-09-25 20:54:02 +08006122 usb_autopm_put_interface(tp->intf);
6123
6124out:
6125 return ret;
6126}
6127
hayeswang8884f502014-10-28 14:05:52 +08006128static int rtl8152_nway_reset(struct net_device *dev)
6129{
6130 struct r8152 *tp = netdev_priv(dev);
6131 int ret;
6132
6133 ret = usb_autopm_get_interface(tp->intf);
6134 if (ret < 0)
6135 goto out;
6136
6137 mutex_lock(&tp->control);
6138
6139 ret = mii_nway_restart(&tp->mii);
6140
6141 mutex_unlock(&tp->control);
6142
6143 usb_autopm_put_interface(tp->intf);
6144
6145out:
6146 return ret;
6147}
6148
hayeswangefb3dd82015-02-12 14:33:48 +08006149static int rtl8152_get_coalesce(struct net_device *netdev,
6150 struct ethtool_coalesce *coalesce)
6151{
6152 struct r8152 *tp = netdev_priv(netdev);
6153
6154 switch (tp->version) {
6155 case RTL_VER_01:
6156 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006157 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08006158 return -EOPNOTSUPP;
6159 default:
6160 break;
6161 }
6162
6163 coalesce->rx_coalesce_usecs = tp->coalesce;
6164
6165 return 0;
6166}
6167
6168static int rtl8152_set_coalesce(struct net_device *netdev,
6169 struct ethtool_coalesce *coalesce)
6170{
6171 struct r8152 *tp = netdev_priv(netdev);
6172 int ret;
6173
6174 switch (tp->version) {
6175 case RTL_VER_01:
6176 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006177 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08006178 return -EOPNOTSUPP;
6179 default:
6180 break;
6181 }
6182
6183 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
6184 return -EINVAL;
6185
6186 ret = usb_autopm_get_interface(tp->intf);
6187 if (ret < 0)
6188 return ret;
6189
6190 mutex_lock(&tp->control);
6191
6192 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
6193 tp->coalesce = coalesce->rx_coalesce_usecs;
6194
Hayes Wang9fae5412019-07-03 15:11:56 +08006195 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
6196 netif_stop_queue(netdev);
6197 napi_disable(&tp->napi);
6198 tp->rtl_ops.disable(tp);
6199 tp->rtl_ops.enable(tp);
6200 rtl_start_rx(tp);
6201 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6202 _rtl8152_set_rx_mode(netdev);
6203 napi_enable(&tp->napi);
6204 netif_wake_queue(netdev);
6205 }
hayeswangefb3dd82015-02-12 14:33:48 +08006206 }
6207
6208 mutex_unlock(&tp->control);
6209
6210 usb_autopm_put_interface(tp->intf);
6211
6212 return ret;
6213}
6214
Hayes Wange4a50172019-08-13 11:42:09 +08006215static int rtl8152_get_tunable(struct net_device *netdev,
6216 const struct ethtool_tunable *tunable, void *d)
6217{
6218 struct r8152 *tp = netdev_priv(netdev);
6219
6220 switch (tunable->id) {
6221 case ETHTOOL_RX_COPYBREAK:
6222 *(u32 *)d = tp->rx_copybreak;
6223 break;
6224 default:
6225 return -EOPNOTSUPP;
6226 }
6227
6228 return 0;
6229}
6230
6231static int rtl8152_set_tunable(struct net_device *netdev,
6232 const struct ethtool_tunable *tunable,
6233 const void *d)
6234{
6235 struct r8152 *tp = netdev_priv(netdev);
6236 u32 val;
6237
6238 switch (tunable->id) {
6239 case ETHTOOL_RX_COPYBREAK:
6240 val = *(u32 *)d;
6241 if (val < ETH_ZLEN) {
6242 netif_err(tp, rx_err, netdev,
6243 "Invalid rx copy break value\n");
6244 return -EINVAL;
6245 }
6246
6247 if (tp->rx_copybreak != val) {
Hayes Wang5b1d9c12019-11-22 16:21:09 +08006248 if (netdev->flags & IFF_UP) {
6249 mutex_lock(&tp->control);
6250 napi_disable(&tp->napi);
6251 tp->rx_copybreak = val;
6252 napi_enable(&tp->napi);
6253 mutex_unlock(&tp->control);
6254 } else {
6255 tp->rx_copybreak = val;
6256 }
Hayes Wange4a50172019-08-13 11:42:09 +08006257 }
6258 break;
6259 default:
6260 return -EOPNOTSUPP;
6261 }
6262
6263 return 0;
6264}
6265
6266static void rtl8152_get_ringparam(struct net_device *netdev,
6267 struct ethtool_ringparam *ring)
6268{
6269 struct r8152 *tp = netdev_priv(netdev);
6270
6271 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
6272 ring->rx_pending = tp->rx_pending;
6273}
6274
6275static int rtl8152_set_ringparam(struct net_device *netdev,
6276 struct ethtool_ringparam *ring)
6277{
6278 struct r8152 *tp = netdev_priv(netdev);
6279
6280 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
6281 return -EINVAL;
6282
6283 if (tp->rx_pending != ring->rx_pending) {
Hayes Wang5b1d9c12019-11-22 16:21:09 +08006284 if (netdev->flags & IFF_UP) {
6285 mutex_lock(&tp->control);
6286 napi_disable(&tp->napi);
6287 tp->rx_pending = ring->rx_pending;
6288 napi_enable(&tp->napi);
6289 mutex_unlock(&tp->control);
6290 } else {
6291 tp->rx_pending = ring->rx_pending;
6292 }
Hayes Wange4a50172019-08-13 11:42:09 +08006293 }
6294
6295 return 0;
6296}
6297
Julia Lawall407a4712016-09-01 00:21:22 +02006298static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00006299 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00006300 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08006301 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08006302 .get_msglevel = rtl8152_get_msglevel,
6303 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08006304 .get_wol = rtl8152_get_wol,
6305 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08006306 .get_strings = rtl8152_get_strings,
6307 .get_sset_count = rtl8152_get_sset_count,
6308 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08006309 .get_coalesce = rtl8152_get_coalesce,
6310 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08006311 .get_eee = rtl_ethtool_get_eee,
6312 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01006313 .get_link_ksettings = rtl8152_get_link_ksettings,
6314 .set_link_ksettings = rtl8152_set_link_ksettings,
Hayes Wange4a50172019-08-13 11:42:09 +08006315 .get_tunable = rtl8152_get_tunable,
6316 .set_tunable = rtl8152_set_tunable,
6317 .get_ringparam = rtl8152_get_ringparam,
6318 .set_ringparam = rtl8152_set_ringparam,
hayeswangac718b62013-05-02 16:01:25 +00006319};
6320
6321static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
6322{
6323 struct r8152 *tp = netdev_priv(netdev);
6324 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08006325 int res;
6326
hayeswang68714382014-04-11 17:54:31 +08006327 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6328 return -ENODEV;
6329
hayeswang9a4be1b2014-02-18 21:49:07 +08006330 res = usb_autopm_get_interface(tp->intf);
6331 if (res < 0)
6332 goto out;
hayeswangac718b62013-05-02 16:01:25 +00006333
6334 switch (cmd) {
6335 case SIOCGMIIPHY:
6336 data->phy_id = R8152_PHY_ID; /* Internal PHY */
6337 break;
6338
6339 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08006340 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006341 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08006342 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006343 break;
6344
6345 case SIOCSMIIREG:
6346 if (!capable(CAP_NET_ADMIN)) {
6347 res = -EPERM;
6348 break;
6349 }
hayeswangb5403272014-10-09 18:00:26 +08006350 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006351 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08006352 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006353 break;
6354
6355 default:
6356 res = -EOPNOTSUPP;
6357 }
6358
hayeswang9a4be1b2014-02-18 21:49:07 +08006359 usb_autopm_put_interface(tp->intf);
6360
6361out:
hayeswangac718b62013-05-02 16:01:25 +00006362 return res;
6363}
6364
hayeswang69b4b7a2014-07-10 10:58:54 +08006365static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
6366{
6367 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08006368 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08006369
6370 switch (tp->version) {
6371 case RTL_VER_01:
6372 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006373 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04006374 dev->mtu = new_mtu;
6375 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08006376 default:
6377 break;
6378 }
6379
hayeswang396e2e22015-02-12 14:33:47 +08006380 ret = usb_autopm_get_interface(tp->intf);
6381 if (ret < 0)
6382 return ret;
6383
6384 mutex_lock(&tp->control);
6385
hayeswang69b4b7a2014-07-10 10:58:54 +08006386 dev->mtu = new_mtu;
6387
hayeswang210c4f72017-03-20 16:13:44 +08006388 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08006389 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08006390
6391 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
6392
6393 if (netif_carrier_ok(dev))
6394 r8153_set_rx_early_size(tp);
6395 }
hayeswang396e2e22015-02-12 14:33:47 +08006396
6397 mutex_unlock(&tp->control);
6398
6399 usb_autopm_put_interface(tp->intf);
6400
6401 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08006402}
6403
hayeswangac718b62013-05-02 16:01:25 +00006404static const struct net_device_ops rtl8152_netdev_ops = {
6405 .ndo_open = rtl8152_open,
6406 .ndo_stop = rtl8152_close,
6407 .ndo_do_ioctl = rtl8152_ioctl,
6408 .ndo_start_xmit = rtl8152_start_xmit,
6409 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08006410 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00006411 .ndo_set_rx_mode = rtl8152_set_rx_mode,
6412 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08006413 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00006414 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08006415 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00006416};
6417
hayeswange3fe0b12014-01-02 11:22:39 +08006418static void rtl8152_unload(struct r8152 *tp)
6419{
hayeswang68714382014-04-11 17:54:31 +08006420 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6421 return;
6422
hayeswang00a5e362014-02-18 21:48:59 +08006423 if (tp->version != RTL_VER_01)
6424 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08006425}
6426
hayeswang43779f82014-01-02 11:25:10 +08006427static void rtl8153_unload(struct r8152 *tp)
6428{
hayeswang68714382014-04-11 17:54:31 +08006429 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6430 return;
6431
hayeswang49be1722014-10-01 13:25:11 +08006432 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08006433}
6434
hayeswang65b82d62017-06-15 14:44:03 +08006435static void rtl8153b_unload(struct r8152 *tp)
6436{
6437 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6438 return;
6439
6440 r8153b_power_cut_en(tp, false);
6441}
6442
hayeswang55b65472014-11-06 12:47:39 +08006443static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08006444{
6445 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08006446 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08006447
hayeswang55b65472014-11-06 12:47:39 +08006448 switch (tp->version) {
6449 case RTL_VER_01:
6450 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006451 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08006452 ops->init = r8152b_init;
6453 ops->enable = rtl8152_enable;
6454 ops->disable = rtl8152_disable;
6455 ops->up = rtl8152_up;
6456 ops->down = rtl8152_down;
6457 ops->unload = rtl8152_unload;
6458 ops->eee_get = r8152_get_eee;
6459 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08006460 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08006461 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08006462 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08006463 tp->rx_buf_sz = 16 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006464 tp->eee_en = true;
6465 tp->eee_adv = MDIO_EEE_100TX;
hayeswang43779f82014-01-02 11:25:10 +08006466 break;
6467
hayeswang55b65472014-11-06 12:47:39 +08006468 case RTL_VER_03:
6469 case RTL_VER_04:
6470 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08006471 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08006472 ops->init = r8153_init;
6473 ops->enable = rtl8153_enable;
6474 ops->disable = rtl8153_disable;
6475 ops->up = rtl8153_up;
6476 ops->down = rtl8153_down;
6477 ops->unload = rtl8153_unload;
6478 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08006479 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08006480 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08006481 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08006482 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08006483 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006484 tp->eee_en = true;
6485 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswangc81229c2014-01-02 11:22:42 +08006486 break;
6487
hayeswang65b82d62017-06-15 14:44:03 +08006488 case RTL_VER_08:
6489 case RTL_VER_09:
6490 ops->init = r8153b_init;
6491 ops->enable = rtl8153_enable;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006492 ops->disable = rtl8153_disable;
hayeswang65b82d62017-06-15 14:44:03 +08006493 ops->up = rtl8153b_up;
6494 ops->down = rtl8153b_down;
6495 ops->unload = rtl8153b_unload;
6496 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08006497 ops->eee_set = r8152_set_eee;
hayeswang65b82d62017-06-15 14:44:03 +08006498 ops->in_nway = rtl8153_in_nway;
6499 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
6500 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08006501 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006502 tp->eee_en = true;
6503 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswang65b82d62017-06-15 14:44:03 +08006504 break;
6505
hayeswangc81229c2014-01-02 11:22:42 +08006506 default:
hayeswang55b65472014-11-06 12:47:39 +08006507 ret = -ENODEV;
6508 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08006509 break;
6510 }
6511
6512 return ret;
6513}
6514
Hayes Wang9370f2d2019-10-16 11:02:42 +08006515#define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw"
6516#define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw"
6517#define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw"
6518#define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw"
6519
6520MODULE_FIRMWARE(FIRMWARE_8153A_2);
6521MODULE_FIRMWARE(FIRMWARE_8153A_3);
6522MODULE_FIRMWARE(FIRMWARE_8153A_4);
6523MODULE_FIRMWARE(FIRMWARE_8153B_2);
6524
6525static int rtl_fw_init(struct r8152 *tp)
6526{
6527 struct rtl_fw *rtl_fw = &tp->rtl_fw;
6528
6529 switch (tp->version) {
6530 case RTL_VER_04:
6531 rtl_fw->fw_name = FIRMWARE_8153A_2;
6532 rtl_fw->pre_fw = r8153_pre_firmware_1;
6533 rtl_fw->post_fw = r8153_post_firmware_1;
6534 break;
6535 case RTL_VER_05:
6536 rtl_fw->fw_name = FIRMWARE_8153A_3;
6537 rtl_fw->pre_fw = r8153_pre_firmware_2;
6538 rtl_fw->post_fw = r8153_post_firmware_2;
6539 break;
6540 case RTL_VER_06:
6541 rtl_fw->fw_name = FIRMWARE_8153A_4;
6542 rtl_fw->post_fw = r8153_post_firmware_3;
6543 break;
6544 case RTL_VER_09:
6545 rtl_fw->fw_name = FIRMWARE_8153B_2;
6546 rtl_fw->pre_fw = r8153b_pre_firmware_1;
6547 rtl_fw->post_fw = r8153b_post_firmware_1;
6548 break;
6549 default:
6550 break;
6551 }
6552
6553 return 0;
6554}
6555
hayeswang33928ee2017-03-17 11:20:13 +08006556static u8 rtl_get_version(struct usb_interface *intf)
6557{
6558 struct usb_device *udev = interface_to_usbdev(intf);
6559 u32 ocp_data = 0;
6560 __le32 *tmp;
6561 u8 version;
6562 int ret;
6563
6564 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
6565 if (!tmp)
6566 return 0;
6567
6568 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
6569 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
6570 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
6571 if (ret > 0)
6572 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
6573
6574 kfree(tmp);
6575
6576 switch (ocp_data) {
6577 case 0x4c00:
6578 version = RTL_VER_01;
6579 break;
6580 case 0x4c10:
6581 version = RTL_VER_02;
6582 break;
6583 case 0x5c00:
6584 version = RTL_VER_03;
6585 break;
6586 case 0x5c10:
6587 version = RTL_VER_04;
6588 break;
6589 case 0x5c20:
6590 version = RTL_VER_05;
6591 break;
6592 case 0x5c30:
6593 version = RTL_VER_06;
6594 break;
hayeswangc27b32c2017-06-15 14:44:02 +08006595 case 0x4800:
6596 version = RTL_VER_07;
6597 break;
hayeswang65b82d62017-06-15 14:44:03 +08006598 case 0x6000:
6599 version = RTL_VER_08;
6600 break;
6601 case 0x6010:
6602 version = RTL_VER_09;
6603 break;
hayeswang33928ee2017-03-17 11:20:13 +08006604 default:
6605 version = RTL_VER_UNKNOWN;
6606 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
6607 break;
6608 }
6609
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02006610 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
6611
hayeswang33928ee2017-03-17 11:20:13 +08006612 return version;
6613}
6614
hayeswangac718b62013-05-02 16:01:25 +00006615static int rtl8152_probe(struct usb_interface *intf,
6616 const struct usb_device_id *id)
6617{
6618 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08006619 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00006620 struct r8152 *tp;
6621 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08006622 int ret;
hayeswangac718b62013-05-02 16:01:25 +00006623
hayeswang33928ee2017-03-17 11:20:13 +08006624 if (version == RTL_VER_UNKNOWN)
6625 return -ENODEV;
6626
hayeswang10c32712014-03-04 20:47:48 +08006627 if (udev->actconfig->desc.bConfigurationValue != 1) {
6628 usb_driver_set_configuration(udev, 1);
6629 return -ENODEV;
6630 }
6631
Johan Hovold86f3f4c2020-01-14 09:27:29 +01006632 if (intf->cur_altsetting->desc.bNumEndpoints < 3)
6633 return -ENODEV;
6634
hayeswang10c32712014-03-04 20:47:48 +08006635 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00006636 netdev = alloc_etherdev(sizeof(struct r8152));
6637 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08006638 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00006639 return -ENOMEM;
6640 }
6641
hayeswangebc2ec482013-08-14 20:54:38 +08006642 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00006643 tp = netdev_priv(netdev);
6644 tp->msg_enable = 0x7FFF;
6645
hayeswange3ad4122014-01-06 17:08:42 +08006646 tp->udev = udev;
6647 tp->netdev = netdev;
6648 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08006649 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08006650
hayeswang33928ee2017-03-17 11:20:13 +08006651 switch (version) {
6652 case RTL_VER_01:
6653 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006654 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08006655 tp->mii.supports_gmii = 0;
6656 break;
6657 default:
6658 tp->mii.supports_gmii = 1;
6659 break;
6660 }
6661
hayeswang55b65472014-11-06 12:47:39 +08006662 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08006663 if (ret)
6664 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08006665
Hayes Wang9370f2d2019-10-16 11:02:42 +08006666 rtl_fw_init(tp);
6667
hayeswangb5403272014-10-09 18:00:26 +08006668 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006669 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08006670 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
Hayes Wangd2187f82019-08-19 14:40:36 +08006671 tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp);
6672 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00006673
hayeswangac718b62013-05-02 16:01:25 +00006674 netdev->netdev_ops = &rtl8152_netdev_ops;
6675 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08006676
hayeswang60c89072014-03-07 11:04:39 +08006677 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08006678 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08006679 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
6680 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08006681 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08006682 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08006683 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08006684 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08006685 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6686 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
6687 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08006688
hayeswang19c0f402017-01-11 16:25:34 +08006689 if (tp->version == RTL_VER_01) {
6690 netdev->features &= ~NETIF_F_RXCSUM;
6691 netdev->hw_features &= ~NETIF_F_RXCSUM;
6692 }
6693
Kai-Heng Feng96477222019-11-05 19:24:52 +08006694 if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO &&
6695 le16_to_cpu(udev->descriptor.idProduct) == 0x3082)
6696 set_bit(LENOVO_MACPASSTHRU, &tp->flags);
6697
Kai-Heng Feng176eb612018-08-20 12:43:51 +08006698 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
Prashant Malani151ea092019-10-02 14:09:33 -07006699 (!strcmp(udev->serial, "000001000000") ||
6700 !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08006701 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
6702 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
6703 }
6704
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006705 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08006706 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00006707
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04006708 /* MTU range: 68 - 1500 or 9194 */
6709 netdev->min_mtu = ETH_MIN_MTU;
6710 switch (tp->version) {
6711 case RTL_VER_01:
6712 case RTL_VER_02:
6713 netdev->max_mtu = ETH_DATA_LEN;
6714 break;
6715 default:
6716 netdev->max_mtu = RTL8153_MAX_MTU;
6717 break;
6718 }
6719
hayeswangac718b62013-05-02 16:01:25 +00006720 tp->mii.dev = netdev;
6721 tp->mii.mdio_read = read_mii_word;
6722 tp->mii.mdio_write = write_mii_word;
6723 tp->mii.phy_id_mask = 0x3f;
6724 tp->mii.reg_num_mask = 0x1f;
6725 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00006726
hayeswangaa7e26b2016-06-13 17:49:38 +08006727 tp->autoneg = AUTONEG_ENABLE;
Hayes Wang771efed2019-09-02 19:52:28 +08006728 tp->speed = SPEED_100;
6729 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6730 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6731 if (tp->mii.supports_gmii) {
6732 tp->speed = SPEED_1000;
6733 tp->advertising |= RTL_ADVERTISED_1000_FULL;
6734 }
hayeswangaa7e26b2016-06-13 17:49:38 +08006735 tp->duplex = DUPLEX_FULL;
6736
Hayes Wange4a50172019-08-13 11:42:09 +08006737 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
6738 tp->rx_pending = 10 * RTL8152_MAX_RX;
6739
hayeswang9a4be1b2014-02-18 21:49:07 +08006740 intf->needs_remote_wakeup = 1;
6741
hayeswangc81229c2014-01-02 11:22:42 +08006742 tp->rtl_ops.init(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08006743#if IS_BUILTIN(CONFIG_USB_RTL8152)
6744 /* Retry in case request_firmware() is not ready yet. */
6745 tp->rtl_fw.retry = true;
6746#endif
hayeswanga028a9e2016-06-13 17:49:36 +08006747 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00006748 set_ethernet_addr(tp);
6749
hayeswangac718b62013-05-02 16:01:25 +00006750 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08006751 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00006752
hayeswangebc2ec482013-08-14 20:54:38 +08006753 ret = register_netdev(netdev);
6754 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08006755 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08006756 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00006757 }
6758
hayeswang7daed8d2015-07-24 13:54:24 +08006759 if (!rtl_can_wakeup(tp))
6760 __rtl_set_wol(tp, 0);
6761
hayeswang21ff2e82014-02-18 21:49:06 +08006762 tp->saved_wolopts = __rtl_get_wol(tp);
6763 if (tp->saved_wolopts)
6764 device_set_wakeup_enable(&udev->dev, true);
6765 else
6766 device_set_wakeup_enable(&udev->dev, false);
6767
Hayes Wang4a8deae2014-01-07 11:18:22 +08006768 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00006769
6770 return 0;
6771
hayeswangac718b62013-05-02 16:01:25 +00006772out1:
Hayes Wangd2187f82019-08-19 14:40:36 +08006773 tasklet_kill(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08006774 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00006775out:
6776 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08006777 return ret;
hayeswangac718b62013-05-02 16:01:25 +00006778}
6779
hayeswangac718b62013-05-02 16:01:25 +00006780static void rtl8152_disconnect(struct usb_interface *intf)
6781{
6782 struct r8152 *tp = usb_get_intfdata(intf);
6783
6784 usb_set_intfdata(intf, NULL);
6785 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08006786 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08006787
hayeswangac718b62013-05-02 16:01:25 +00006788 unregister_netdev(tp->netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08006789 tasklet_kill(&tp->tx_tl);
hayeswanga028a9e2016-06-13 17:49:36 +08006790 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08006791 tp->rtl_ops.unload(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08006792 rtl8152_release_firmware(tp);
hayeswangac718b62013-05-02 16:01:25 +00006793 free_netdev(tp->netdev);
6794 }
6795}
6796
hayeswangd9a28c52014-12-04 10:43:11 +08006797#define REALTEK_USB_DEVICE(vend, prod) \
6798 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
6799 USB_DEVICE_ID_MATCH_INT_CLASS, \
6800 .idVendor = (vend), \
6801 .idProduct = (prod), \
6802 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
6803}, \
6804{ \
6805 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
6806 USB_DEVICE_ID_MATCH_DEVICE, \
6807 .idVendor = (vend), \
6808 .idProduct = (prod), \
6809 .bInterfaceClass = USB_CLASS_COMM, \
6810 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
6811 .bInterfaceProtocol = USB_CDC_PROTO_NONE
6812
hayeswangac718b62013-05-02 16:01:25 +00006813/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05306814static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08006815 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08006816 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
6817 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02006818 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
6819 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08006820 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04006821 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08006822 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
6823 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
Kai-Heng Feng96477222019-11-05 19:24:52 +08006824 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082)},
hayeswangd248caf2016-10-18 11:41:48 +08006825 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
6826 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
6827 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Kazutoshi Noguchib3060532019-10-21 00:03:07 +09006828 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)},
Grant Grundler90841042017-09-28 11:35:00 -07006829 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07006830 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08006831 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00006832 {}
6833};
6834
6835MODULE_DEVICE_TABLE(usb, rtl8152_table);
6836
6837static struct usb_driver rtl8152_driver = {
6838 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08006839 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00006840 .probe = rtl8152_probe,
6841 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00006842 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08006843 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08006844 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08006845 .pre_reset = rtl8152_pre_reset,
6846 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08006847 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08006848 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00006849};
6850
Sachin Kamatb4236daa2013-05-16 17:48:08 +00006851module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00006852
6853MODULE_AUTHOR(DRIVER_AUTHOR);
6854MODULE_DESCRIPTION(DRIVER_DESC);
6855MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07006856MODULE_VERSION(DRIVER_VERSION);