blob: 115559707683f8ddde7b47eeaba5b5a7d8e446cd [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
2868 return rtl_enable(tp);
2869}
2870
hayeswangd70b1132014-09-19 15:17:18 +08002871static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002872{
hayeswangebc2ec482013-08-14 20:54:38 +08002873 u32 ocp_data;
2874 int i;
hayeswangac718b62013-05-02 16:01:25 +00002875
hayeswang68714382014-04-11 17:54:31 +08002876 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2877 rtl_drop_queued_tx(tp);
2878 return;
2879 }
2880
hayeswangac718b62013-05-02 16:01:25 +00002881 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2882 ocp_data &= ~RCR_ACPT_ALL;
2883 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2884
hayeswang00a5e362014-02-18 21:48:59 +08002885 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002886
2887 for (i = 0; i < RTL8152_MAX_TX; i++)
2888 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002889
hayeswang00a5e362014-02-18 21:48:59 +08002890 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002891
2892 for (i = 0; i < 1000; i++) {
2893 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2894 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2895 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002896 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002897 }
2898
2899 for (i = 0; i < 1000; i++) {
2900 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2901 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002902 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002903 }
2904
hayeswang445f7f42014-09-23 16:31:47 +08002905 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002906
2907 rtl8152_nic_reset(tp);
2908}
2909
hayeswang00a5e362014-02-18 21:48:59 +08002910static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2911{
2912 u32 ocp_data;
2913
2914 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2915 if (enable)
2916 ocp_data |= POWER_CUT;
2917 else
2918 ocp_data &= ~POWER_CUT;
2919 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2920
2921 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2922 ocp_data &= ~RESUME_INDICATE;
2923 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002924}
2925
hayeswangc5554292014-09-12 10:43:11 +08002926static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2927{
2928 u32 ocp_data;
2929
2930 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2931 if (enable)
2932 ocp_data |= CPCR_RX_VLAN;
2933 else
2934 ocp_data &= ~CPCR_RX_VLAN;
2935 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2936}
2937
2938static int rtl8152_set_features(struct net_device *dev,
2939 netdev_features_t features)
2940{
2941 netdev_features_t changed = features ^ dev->features;
2942 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002943 int ret;
2944
2945 ret = usb_autopm_get_interface(tp->intf);
2946 if (ret < 0)
2947 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002948
hayeswangb5403272014-10-09 18:00:26 +08002949 mutex_lock(&tp->control);
2950
hayeswangc5554292014-09-12 10:43:11 +08002951 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2952 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2953 rtl_rx_vlan_en(tp, true);
2954 else
2955 rtl_rx_vlan_en(tp, false);
2956 }
2957
hayeswangb5403272014-10-09 18:00:26 +08002958 mutex_unlock(&tp->control);
2959
hayeswang405f8a02014-10-09 18:00:24 +08002960 usb_autopm_put_interface(tp->intf);
2961
2962out:
2963 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002964}
2965
hayeswang21ff2e82014-02-18 21:49:06 +08002966#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2967
2968static u32 __rtl_get_wol(struct r8152 *tp)
2969{
2970 u32 ocp_data;
2971 u32 wolopts = 0;
2972
hayeswang21ff2e82014-02-18 21:49:06 +08002973 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2974 if (ocp_data & LINK_ON_WAKE_EN)
2975 wolopts |= WAKE_PHY;
2976
2977 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2978 if (ocp_data & UWF_EN)
2979 wolopts |= WAKE_UCAST;
2980 if (ocp_data & BWF_EN)
2981 wolopts |= WAKE_BCAST;
2982 if (ocp_data & MWF_EN)
2983 wolopts |= WAKE_MCAST;
2984
2985 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2986 if (ocp_data & MAGIC_EN)
2987 wolopts |= WAKE_MAGIC;
2988
2989 return wolopts;
2990}
2991
2992static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2993{
2994 u32 ocp_data;
2995
2996 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2997
2998 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2999 ocp_data &= ~LINK_ON_WAKE_EN;
3000 if (wolopts & WAKE_PHY)
3001 ocp_data |= LINK_ON_WAKE_EN;
3002 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3003
3004 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08003005 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08003006 if (wolopts & WAKE_UCAST)
3007 ocp_data |= UWF_EN;
3008 if (wolopts & WAKE_BCAST)
3009 ocp_data |= BWF_EN;
3010 if (wolopts & WAKE_MCAST)
3011 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08003012 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3013
3014 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3015
3016 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3017 ocp_data &= ~MAGIC_EN;
3018 if (wolopts & WAKE_MAGIC)
3019 ocp_data |= MAGIC_EN;
3020 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3021
3022 if (wolopts & WAKE_ANY)
3023 device_set_wakeup_enable(&tp->udev->dev, true);
3024 else
3025 device_set_wakeup_enable(&tp->udev->dev, false);
3026}
3027
hayeswang134f98b2017-06-09 17:11:40 +08003028static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
3029{
3030 /* MAC clock speed down */
3031 if (enable) {
3032 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3033 ALDPS_SPDWN_RATIO);
3034 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
3035 EEE_SPDWN_RATIO);
3036 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3037 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3038 U1U2_SPDWN_EN | L1_SPDWN_EN);
3039 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3040 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3041 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
3042 TP1000_SPDWN_EN);
3043 } else {
3044 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
3045 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
3046 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
3047 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
3048 }
3049}
3050
hayeswangb2143962015-07-24 13:54:23 +08003051static void r8153_u1u2en(struct r8152 *tp, bool enable)
3052{
3053 u8 u1u2[8];
3054
3055 if (enable)
3056 memset(u1u2, 0xff, sizeof(u1u2));
3057 else
3058 memset(u1u2, 0x00, sizeof(u1u2));
3059
3060 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3061}
3062
hayeswang65b82d62017-06-15 14:44:03 +08003063static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3064{
3065 u32 ocp_data;
3066
3067 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3068 if (enable)
3069 ocp_data |= LPM_U1U2_EN;
3070 else
3071 ocp_data &= ~LPM_U1U2_EN;
3072
3073 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3074}
3075
hayeswangb2143962015-07-24 13:54:23 +08003076static void r8153_u2p3en(struct r8152 *tp, bool enable)
3077{
3078 u32 ocp_data;
3079
3080 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08003081 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08003082 ocp_data |= U2P3_ENABLE;
3083 else
3084 ocp_data &= ~U2P3_ENABLE;
3085 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3086}
3087
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003088static void r8153b_ups_flags(struct r8152 *tp)
hayeswang65b82d62017-06-15 14:44:03 +08003089{
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003090 u32 ups_flags = 0;
hayeswang65b82d62017-06-15 14:44:03 +08003091
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003092 if (tp->ups_info.green)
3093 ups_flags |= UPS_FLAGS_EN_GREEN;
3094
3095 if (tp->ups_info.aldps)
3096 ups_flags |= UPS_FLAGS_EN_ALDPS;
3097
3098 if (tp->ups_info.eee)
3099 ups_flags |= UPS_FLAGS_EN_EEE;
3100
3101 if (tp->ups_info.flow_control)
3102 ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3103
3104 if (tp->ups_info.eee_ckdiv)
3105 ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3106
3107 if (tp->ups_info.eee_cmod_lv)
3108 ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3109
3110 if (tp->ups_info._10m_ckdiv)
3111 ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3112
3113 if (tp->ups_info.eee_plloff_100)
3114 ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3115
3116 if (tp->ups_info.eee_plloff_giga)
3117 ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3118
3119 if (tp->ups_info._250m_ckdiv)
3120 ups_flags |= UPS_FLAGS_250M_CKDIV;
3121
3122 if (tp->ups_info.ctap_short_off)
3123 ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3124
3125 switch (tp->ups_info.speed_duplex) {
3126 case NWAY_10M_HALF:
3127 ups_flags |= ups_flags_speed(1);
3128 break;
3129 case NWAY_10M_FULL:
3130 ups_flags |= ups_flags_speed(2);
3131 break;
3132 case NWAY_100M_HALF:
3133 ups_flags |= ups_flags_speed(3);
3134 break;
3135 case NWAY_100M_FULL:
3136 ups_flags |= ups_flags_speed(4);
3137 break;
3138 case NWAY_1000M_FULL:
3139 ups_flags |= ups_flags_speed(5);
3140 break;
3141 case FORCE_10M_HALF:
3142 ups_flags |= ups_flags_speed(6);
3143 break;
3144 case FORCE_10M_FULL:
3145 ups_flags |= ups_flags_speed(7);
3146 break;
3147 case FORCE_100M_HALF:
3148 ups_flags |= ups_flags_speed(8);
3149 break;
3150 case FORCE_100M_FULL:
3151 ups_flags |= ups_flags_speed(9);
3152 break;
3153 default:
3154 break;
3155 }
3156
3157 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
hayeswang65b82d62017-06-15 14:44:03 +08003158}
3159
3160static void r8153b_green_en(struct r8152 *tp, bool enable)
3161{
3162 u16 data;
3163
3164 if (enable) {
3165 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
3166 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
3167 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
3168 } else {
3169 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
3170 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
3171 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
3172 }
3173
3174 data = sram_read(tp, SRAM_GREEN_CFG);
3175 data |= GREEN_ETH_EN;
3176 sram_write(tp, SRAM_GREEN_CFG, data);
3177
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003178 tp->ups_info.green = enable;
hayeswang65b82d62017-06-15 14:44:03 +08003179}
3180
hayeswangc564b872017-06-09 17:11:38 +08003181static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3182{
3183 u16 data;
3184 int i;
3185
3186 for (i = 0; i < 500; i++) {
3187 data = ocp_reg_read(tp, OCP_PHY_STATUS);
3188 data &= PHY_STAT_MASK;
3189 if (desired) {
3190 if (data == desired)
3191 break;
3192 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3193 data == PHY_STAT_EXT_INIT) {
3194 break;
3195 }
3196
3197 msleep(20);
3198 }
3199
3200 return data;
3201}
3202
hayeswang65b82d62017-06-15 14:44:03 +08003203static void r8153b_ups_en(struct r8152 *tp, bool enable)
3204{
3205 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3206
3207 if (enable) {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08003208 r8153b_ups_flags(tp);
3209
hayeswang65b82d62017-06-15 14:44:03 +08003210 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3211 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3212
3213 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3214 ocp_data |= BIT(0);
3215 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3216 } else {
3217 u16 data;
3218
3219 ocp_data &= ~(UPS_EN | USP_PREWAKE);
3220 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3221
3222 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3223 ocp_data &= ~BIT(0);
3224 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3225
3226 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3227 ocp_data &= ~PCUT_STATUS;
3228 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3229
3230 data = r8153_phy_status(tp, 0);
3231
3232 switch (data) {
3233 case PHY_STAT_PWRDN:
3234 case PHY_STAT_EXT_INIT:
3235 r8153b_green_en(tp,
3236 test_bit(GREEN_ETHERNET, &tp->flags));
3237
3238 data = r8152_mdio_read(tp, MII_BMCR);
3239 data &= ~BMCR_PDOWN;
3240 data |= BMCR_RESET;
3241 r8152_mdio_write(tp, MII_BMCR, data);
3242
3243 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05003244 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08003245
3246 default:
3247 if (data != PHY_STAT_LAN_ON)
3248 netif_warn(tp, link, tp->netdev,
3249 "PHY not ready");
3250 break;
3251 }
3252 }
3253}
3254
hayeswangb2143962015-07-24 13:54:23 +08003255static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3256{
3257 u32 ocp_data;
3258
3259 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3260 if (enable)
3261 ocp_data |= PWR_EN | PHASE2_EN;
3262 else
3263 ocp_data &= ~(PWR_EN | PHASE2_EN);
3264 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3265
3266 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3267 ocp_data &= ~PCUT_STATUS;
3268 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3269}
3270
hayeswang65b82d62017-06-15 14:44:03 +08003271static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3272{
3273 u32 ocp_data;
3274
3275 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3276 if (enable)
3277 ocp_data |= PWR_EN | PHASE2_EN;
3278 else
3279 ocp_data &= ~PWR_EN;
3280 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3281
3282 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3283 ocp_data &= ~PCUT_STATUS;
3284 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3285}
3286
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003287static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08003288{
3289 u32 ocp_data;
3290
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003291 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08003292 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003293 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08003294 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003295 ocp_data &= ~UPCOMING_RUNTIME_D3;
3296 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003297
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003298 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3299 ocp_data &= ~LINK_CHG_EVENT;
3300 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3301
3302 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3303 ocp_data &= ~LINK_CHANGE_FLAG;
3304 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08003305}
3306
hayeswang7daed8d2015-07-24 13:54:24 +08003307static bool rtl_can_wakeup(struct r8152 *tp)
3308{
3309 struct usb_device *udev = tp->udev;
3310
3311 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3312}
3313
hayeswang9a4be1b2014-02-18 21:49:07 +08003314static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3315{
3316 if (enable) {
3317 u32 ocp_data;
3318
3319 __rtl_set_wol(tp, WAKE_ANY);
3320
3321 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3322
3323 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3324 ocp_data |= LINK_OFF_WAKE_EN;
3325 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3326
3327 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3328 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08003329 u32 ocp_data;
3330
hayeswang9a4be1b2014-02-18 21:49:07 +08003331 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08003332
3333 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3334
3335 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3336 ocp_data &= ~LINK_OFF_WAKE_EN;
3337 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3338
3339 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08003340 }
3341}
hayeswangf95ae8a2016-06-30 15:33:35 +08003342
hayeswang2609af12016-07-05 16:11:46 +08003343static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3344{
hayeswang2609af12016-07-05 16:11:46 +08003345 if (enable) {
3346 r8153_u1u2en(tp, false);
3347 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003348 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08003349 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08003350 } else {
hayeswang02552752017-06-09 17:11:42 +08003351 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08003352 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003353
3354 switch (tp->version) {
3355 case RTL_VER_03:
3356 case RTL_VER_04:
3357 break;
3358 case RTL_VER_05:
3359 case RTL_VER_06:
3360 default:
3361 r8153_u2p3en(tp, true);
3362 break;
3363 }
3364
hayeswangb2143962015-07-24 13:54:23 +08003365 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08003366 }
3367}
3368
hayeswang65b82d62017-06-15 14:44:03 +08003369static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3370{
3371 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003372 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08003373 r8153b_u1u2en(tp, false);
3374 r8153_u2p3en(tp, false);
3375 rtl_runtime_suspend_enable(tp, true);
3376 r8153b_ups_en(tp, true);
3377 } else {
3378 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08003379 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08003380 rtl_runtime_suspend_enable(tp, false);
3381 r8153_u2p3en(tp, true);
3382 r8153b_u1u2en(tp, true);
3383 }
3384}
3385
hayeswang43499682014-02-18 21:48:58 +08003386static void r8153_teredo_off(struct r8152 *tp)
3387{
3388 u32 ocp_data;
3389
hayeswang65b82d62017-06-15 14:44:03 +08003390 switch (tp->version) {
3391 case RTL_VER_01:
3392 case RTL_VER_02:
3393 case RTL_VER_03:
3394 case RTL_VER_04:
3395 case RTL_VER_05:
3396 case RTL_VER_06:
3397 case RTL_VER_07:
3398 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3399 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3400 OOB_TEREDO_EN);
3401 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3402 break;
3403
3404 case RTL_VER_08:
3405 case RTL_VER_09:
3406 /* The bit 0 ~ 7 are relative with teredo settings. They are
3407 * W1C (write 1 to clear), so set all 1 to disable it.
3408 */
3409 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3410 break;
3411
3412 default:
3413 break;
3414 }
hayeswang43499682014-02-18 21:48:58 +08003415
3416 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3417 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3418 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3419}
3420
hayeswang93fe9b12016-06-16 10:55:18 +08003421static void rtl_reset_bmu(struct r8152 *tp)
3422{
3423 u32 ocp_data;
3424
3425 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3426 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3427 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3428 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3429 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3430}
3431
Hayes Wang9370f2d2019-10-16 11:02:42 +08003432/* Clear the bp to stop the firmware before loading a new one */
3433static void rtl_clear_bp(struct r8152 *tp, u16 type)
3434{
3435 switch (tp->version) {
3436 case RTL_VER_01:
3437 case RTL_VER_02:
3438 case RTL_VER_07:
3439 break;
3440 case RTL_VER_03:
3441 case RTL_VER_04:
3442 case RTL_VER_05:
3443 case RTL_VER_06:
3444 ocp_write_byte(tp, type, PLA_BP_EN, 0);
3445 break;
3446 case RTL_VER_08:
3447 case RTL_VER_09:
3448 default:
3449 if (type == MCU_TYPE_USB) {
3450 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
3451
3452 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
3453 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
3454 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
3455 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
3456 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
3457 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
3458 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
3459 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
3460 } else {
3461 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
3462 }
3463 break;
3464 }
3465
3466 ocp_write_word(tp, type, PLA_BP_0, 0);
3467 ocp_write_word(tp, type, PLA_BP_1, 0);
3468 ocp_write_word(tp, type, PLA_BP_2, 0);
3469 ocp_write_word(tp, type, PLA_BP_3, 0);
3470 ocp_write_word(tp, type, PLA_BP_4, 0);
3471 ocp_write_word(tp, type, PLA_BP_5, 0);
3472 ocp_write_word(tp, type, PLA_BP_6, 0);
3473 ocp_write_word(tp, type, PLA_BP_7, 0);
3474
3475 /* wait 3 ms to make sure the firmware is stopped */
3476 usleep_range(3000, 6000);
3477 ocp_write_word(tp, type, PLA_BP_BA, 0);
3478}
3479
Hayes Wang470e3912019-10-21 11:41:12 +08003480static int r8153_patch_request(struct r8152 *tp, bool request)
3481{
3482 u16 data;
3483 int i;
3484
3485 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3486 if (request)
3487 data |= PATCH_REQUEST;
3488 else
3489 data &= ~PATCH_REQUEST;
3490 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3491
3492 for (i = 0; request && i < 5000; i++) {
3493 usleep_range(1000, 2000);
3494 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3495 break;
3496 }
3497
3498 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3499 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3500 r8153_patch_request(tp, false);
3501 return -ETIME;
3502 } else {
3503 return 0;
3504 }
3505}
3506
Hayes Wangaf142882019-10-21 11:41:13 +08003507static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key)
3508{
3509 if (r8153_patch_request(tp, true)) {
3510 dev_err(&tp->intf->dev, "patch request fail\n");
3511 return -ETIME;
3512 }
3513
3514 sram_write(tp, key_addr, patch_key);
3515 sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
3516
3517 return 0;
3518}
3519
3520static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr)
3521{
3522 u16 data;
3523
3524 sram_write(tp, 0x0000, 0x0000);
3525
3526 data = ocp_reg_read(tp, OCP_PHY_LOCK);
3527 data &= ~PATCH_LOCK;
3528 ocp_reg_write(tp, OCP_PHY_LOCK, data);
3529
3530 sram_write(tp, key_addr, 0x0000);
3531
3532 r8153_patch_request(tp, false);
3533
3534 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
3535
3536 return 0;
3537}
3538
3539static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
3540{
3541 u32 length;
3542 u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
3543 bool rc = false;
3544
3545 switch (tp->version) {
3546 case RTL_VER_04:
3547 case RTL_VER_05:
3548 case RTL_VER_06:
3549 fw_reg = 0xa014;
3550 ba_reg = 0xa012;
3551 patch_en_addr = 0xa01a;
3552 mode_reg = 0xb820;
3553 bp_start = 0xa000;
3554 break;
3555 default:
3556 goto out;
3557 }
3558
3559 fw_offset = __le16_to_cpu(phy->fw_offset);
3560 if (fw_offset < sizeof(*phy)) {
3561 dev_err(&tp->intf->dev, "fw_offset too small\n");
3562 goto out;
3563 }
3564
3565 length = __le32_to_cpu(phy->blk_hdr.length);
3566 if (length < fw_offset) {
3567 dev_err(&tp->intf->dev, "invalid fw_offset\n");
3568 goto out;
3569 }
3570
3571 length -= __le16_to_cpu(phy->fw_offset);
3572 if (!length || (length & 1)) {
3573 dev_err(&tp->intf->dev, "invalid block length\n");
3574 goto out;
3575 }
3576
3577 if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
3578 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
3579 goto out;
3580 }
3581
3582 if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
3583 dev_err(&tp->intf->dev, "invalid base address register\n");
3584 goto out;
3585 }
3586
3587 if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
3588 dev_err(&tp->intf->dev,
3589 "invalid patch mode enabled register\n");
3590 goto out;
3591 }
3592
3593 if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
3594 dev_err(&tp->intf->dev,
3595 "invalid register to switch the mode\n");
3596 goto out;
3597 }
3598
3599 if (__le16_to_cpu(phy->bp_start) != bp_start) {
3600 dev_err(&tp->intf->dev,
3601 "invalid start register of break point\n");
3602 goto out;
3603 }
3604
3605 if (__le16_to_cpu(phy->bp_num) > 4) {
3606 dev_err(&tp->intf->dev, "invalid break point number\n");
3607 goto out;
3608 }
3609
3610 rc = true;
3611out:
3612 return rc;
3613}
3614
Hayes Wanga66edaa2019-10-21 11:41:10 +08003615static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
Hayes Wang9370f2d2019-10-16 11:02:42 +08003616{
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003617 u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003618 bool rc = false;
3619 u32 length, type;
3620 int i, max_bp;
3621
Hayes Wanga66edaa2019-10-21 11:41:10 +08003622 type = __le32_to_cpu(mac->blk_hdr.type);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003623 if (type == RTL_FW_PLA) {
3624 switch (tp->version) {
3625 case RTL_VER_01:
3626 case RTL_VER_02:
3627 case RTL_VER_07:
3628 fw_reg = 0xf800;
3629 bp_ba_addr = PLA_BP_BA;
3630 bp_en_addr = 0;
3631 bp_start = PLA_BP_0;
3632 max_bp = 8;
3633 break;
3634 case RTL_VER_03:
3635 case RTL_VER_04:
3636 case RTL_VER_05:
3637 case RTL_VER_06:
3638 case RTL_VER_08:
3639 case RTL_VER_09:
3640 fw_reg = 0xf800;
3641 bp_ba_addr = PLA_BP_BA;
3642 bp_en_addr = PLA_BP_EN;
3643 bp_start = PLA_BP_0;
3644 max_bp = 8;
3645 break;
3646 default:
3647 goto out;
3648 }
3649 } else if (type == RTL_FW_USB) {
3650 switch (tp->version) {
3651 case RTL_VER_03:
3652 case RTL_VER_04:
3653 case RTL_VER_05:
3654 case RTL_VER_06:
3655 fw_reg = 0xf800;
3656 bp_ba_addr = USB_BP_BA;
3657 bp_en_addr = USB_BP_EN;
3658 bp_start = USB_BP_0;
3659 max_bp = 8;
3660 break;
3661 case RTL_VER_08:
3662 case RTL_VER_09:
3663 fw_reg = 0xe600;
3664 bp_ba_addr = USB_BP_BA;
3665 bp_en_addr = USB_BP2_EN;
3666 bp_start = USB_BP_0;
3667 max_bp = 16;
3668 break;
3669 case RTL_VER_01:
3670 case RTL_VER_02:
3671 case RTL_VER_07:
3672 default:
3673 goto out;
3674 }
3675 } else {
3676 goto out;
3677 }
3678
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003679 fw_offset = __le16_to_cpu(mac->fw_offset);
3680 if (fw_offset < sizeof(*mac)) {
3681 dev_err(&tp->intf->dev, "fw_offset too small\n");
3682 goto out;
3683 }
3684
Hayes Wanga66edaa2019-10-21 11:41:10 +08003685 length = __le32_to_cpu(mac->blk_hdr.length);
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003686 if (length < fw_offset) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003687 dev_err(&tp->intf->dev, "invalid fw_offset\n");
3688 goto out;
3689 }
3690
Hayes Wang5a16a3d2019-10-21 11:41:11 +08003691 length -= fw_offset;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003692 if (length < 4 || (length & 3)) {
3693 dev_err(&tp->intf->dev, "invalid block length\n");
3694 goto out;
3695 }
3696
Hayes Wanga66edaa2019-10-21 11:41:10 +08003697 if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003698 dev_err(&tp->intf->dev, "invalid register to load firmware\n");
3699 goto out;
3700 }
3701
Hayes Wanga66edaa2019-10-21 11:41:10 +08003702 if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003703 dev_err(&tp->intf->dev, "invalid base address register\n");
3704 goto out;
3705 }
3706
Hayes Wanga66edaa2019-10-21 11:41:10 +08003707 if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003708 dev_err(&tp->intf->dev, "invalid enabled mask register\n");
3709 goto out;
3710 }
3711
Hayes Wanga66edaa2019-10-21 11:41:10 +08003712 if (__le16_to_cpu(mac->bp_start) != bp_start) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003713 dev_err(&tp->intf->dev,
3714 "invalid start register of break point\n");
3715 goto out;
3716 }
3717
Hayes Wanga66edaa2019-10-21 11:41:10 +08003718 if (__le16_to_cpu(mac->bp_num) > max_bp) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003719 dev_err(&tp->intf->dev, "invalid break point number\n");
3720 goto out;
3721 }
3722
Hayes Wanga66edaa2019-10-21 11:41:10 +08003723 for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
3724 if (mac->bp[i]) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003725 dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
3726 goto out;
3727 }
3728 }
3729
3730 rc = true;
3731out:
3732 return rc;
3733}
3734
3735/* Verify the checksum for the firmware file. It is calculated from the version
3736 * field to the end of the file. Compare the result with the checksum field to
3737 * make sure the file is correct.
3738 */
3739static long rtl8152_fw_verify_checksum(struct r8152 *tp,
3740 struct fw_header *fw_hdr, size_t size)
3741{
3742 unsigned char checksum[sizeof(fw_hdr->checksum)];
3743 struct crypto_shash *alg;
3744 struct shash_desc *sdesc;
3745 size_t len;
3746 long rc;
3747
3748 alg = crypto_alloc_shash("sha256", 0, 0);
3749 if (IS_ERR(alg)) {
3750 rc = PTR_ERR(alg);
3751 goto out;
3752 }
3753
3754 if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
3755 rc = -EFAULT;
3756 dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
3757 crypto_shash_digestsize(alg));
3758 goto free_shash;
3759 }
3760
3761 len = sizeof(*sdesc) + crypto_shash_descsize(alg);
3762 sdesc = kmalloc(len, GFP_KERNEL);
3763 if (!sdesc) {
3764 rc = -ENOMEM;
3765 goto free_shash;
3766 }
3767 sdesc->tfm = alg;
3768
3769 len = size - sizeof(fw_hdr->checksum);
3770 rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
3771 kfree(sdesc);
3772 if (rc)
3773 goto free_shash;
3774
3775 if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
3776 dev_err(&tp->intf->dev, "checksum fail\n");
3777 rc = -EFAULT;
3778 }
3779
3780free_shash:
3781 crypto_free_shash(alg);
3782out:
3783 return rc;
3784}
3785
3786static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
3787{
3788 const struct firmware *fw = rtl_fw->fw;
3789 struct fw_header *fw_hdr = (struct fw_header *)fw->data;
Hayes Wanga66edaa2019-10-21 11:41:10 +08003790 struct fw_mac *pla = NULL, *usb = NULL;
Hayes Wangaf142882019-10-21 11:41:13 +08003791 struct fw_phy_patch_key *start = NULL;
3792 struct fw_phy_nc *phy_nc = NULL;
3793 struct fw_block *stop = NULL;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003794 long ret = -EFAULT;
3795 int i;
3796
3797 if (fw->size < sizeof(*fw_hdr)) {
3798 dev_err(&tp->intf->dev, "file too small\n");
3799 goto fail;
3800 }
3801
3802 ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
3803 if (ret)
3804 goto fail;
3805
3806 ret = -EFAULT;
3807
3808 for (i = sizeof(*fw_hdr); i < fw->size;) {
3809 struct fw_block *block = (struct fw_block *)&fw->data[i];
3810 u32 type;
3811
3812 if ((i + sizeof(*block)) > fw->size)
3813 goto fail;
3814
3815 type = __le32_to_cpu(block->type);
3816 switch (type) {
3817 case RTL_FW_END:
3818 if (__le32_to_cpu(block->length) != sizeof(*block))
3819 goto fail;
Hayes Wangaf142882019-10-21 11:41:13 +08003820 goto fw_end;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003821 case RTL_FW_PLA:
3822 if (pla) {
3823 dev_err(&tp->intf->dev,
3824 "multiple PLA firmware encountered");
3825 goto fail;
3826 }
3827
Hayes Wanga66edaa2019-10-21 11:41:10 +08003828 pla = (struct fw_mac *)block;
3829 if (!rtl8152_is_fw_mac_ok(tp, pla)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003830 dev_err(&tp->intf->dev,
Hayes Wanga66edaa2019-10-21 11:41:10 +08003831 "check PLA firmware failed\n");
Hayes Wang9370f2d2019-10-16 11:02:42 +08003832 goto fail;
3833 }
3834 break;
3835 case RTL_FW_USB:
3836 if (usb) {
3837 dev_err(&tp->intf->dev,
3838 "multiple USB firmware encountered");
3839 goto fail;
3840 }
3841
Hayes Wanga66edaa2019-10-21 11:41:10 +08003842 usb = (struct fw_mac *)block;
3843 if (!rtl8152_is_fw_mac_ok(tp, usb)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003844 dev_err(&tp->intf->dev,
Hayes Wanga66edaa2019-10-21 11:41:10 +08003845 "check USB firmware failed\n");
Hayes Wang9370f2d2019-10-16 11:02:42 +08003846 goto fail;
3847 }
3848 break;
Hayes Wangaf142882019-10-21 11:41:13 +08003849 case RTL_FW_PHY_START:
3850 if (start || phy_nc || stop) {
3851 dev_err(&tp->intf->dev,
3852 "check PHY_START fail\n");
3853 goto fail;
3854 }
3855
3856 if (__le32_to_cpu(block->length) != sizeof(*start)) {
3857 dev_err(&tp->intf->dev,
3858 "Invalid length for PHY_START\n");
3859 goto fail;
3860 }
3861
3862 start = (struct fw_phy_patch_key *)block;
3863 break;
3864 case RTL_FW_PHY_STOP:
3865 if (stop || !start) {
3866 dev_err(&tp->intf->dev,
3867 "Check PHY_STOP fail\n");
3868 goto fail;
3869 }
3870
3871 if (__le32_to_cpu(block->length) != sizeof(*block)) {
3872 dev_err(&tp->intf->dev,
3873 "Invalid length for PHY_STOP\n");
3874 goto fail;
3875 }
3876
3877 stop = block;
3878 break;
3879 case RTL_FW_PHY_NC:
3880 if (!start || stop) {
3881 dev_err(&tp->intf->dev,
3882 "check PHY_NC fail\n");
3883 goto fail;
3884 }
3885
3886 if (phy_nc) {
3887 dev_err(&tp->intf->dev,
3888 "multiple PHY NC encountered\n");
3889 goto fail;
3890 }
3891
3892 phy_nc = (struct fw_phy_nc *)block;
3893 if (!rtl8152_is_fw_phy_nc_ok(tp, phy_nc)) {
3894 dev_err(&tp->intf->dev,
3895 "check PHY NC firmware failed\n");
3896 goto fail;
3897 }
3898
3899 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08003900 default:
3901 dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
3902 type);
3903 break;
3904 }
3905
3906 /* next block */
3907 i += ALIGN(__le32_to_cpu(block->length), 8);
3908 }
3909
Hayes Wangaf142882019-10-21 11:41:13 +08003910fw_end:
3911 if ((phy_nc || start) && !stop) {
3912 dev_err(&tp->intf->dev, "without PHY_STOP\n");
3913 goto fail;
3914 }
3915
Hayes Wang9370f2d2019-10-16 11:02:42 +08003916 return 0;
3917fail:
3918 return ret;
3919}
3920
Hayes Wangaf142882019-10-21 11:41:13 +08003921static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
3922{
3923 u16 mode_reg, bp_index;
3924 u32 length, i, num;
3925 __le16 *data;
3926
3927 mode_reg = __le16_to_cpu(phy->mode_reg);
3928 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
3929 sram_write(tp, __le16_to_cpu(phy->ba_reg),
3930 __le16_to_cpu(phy->ba_data));
3931
3932 length = __le32_to_cpu(phy->blk_hdr.length);
3933 length -= __le16_to_cpu(phy->fw_offset);
3934 num = length / 2;
3935 data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
3936
3937 ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
3938 for (i = 0; i < num; i++)
3939 ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
3940
3941 sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
3942 __le16_to_cpu(phy->patch_en_value));
3943
3944 bp_index = __le16_to_cpu(phy->bp_start);
3945 num = __le16_to_cpu(phy->bp_num);
3946 for (i = 0; i < num; i++) {
3947 sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
3948 bp_index += 2;
3949 }
3950
3951 sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
3952
3953 dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
3954}
3955
Hayes Wanga66edaa2019-10-21 11:41:10 +08003956static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
Hayes Wang9370f2d2019-10-16 11:02:42 +08003957{
3958 u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
3959 u32 length;
3960 u8 *data;
3961 int i;
3962
Hayes Wanga66edaa2019-10-21 11:41:10 +08003963 switch (__le32_to_cpu(mac->blk_hdr.type)) {
Hayes Wang9370f2d2019-10-16 11:02:42 +08003964 case RTL_FW_PLA:
3965 type = MCU_TYPE_PLA;
3966 break;
3967 case RTL_FW_USB:
3968 type = MCU_TYPE_USB;
3969 break;
3970 default:
3971 return;
3972 }
3973
3974 rtl_clear_bp(tp, type);
3975
3976 /* Enable backup/restore of MACDBG. This is required after clearing PLA
3977 * break points and before applying the PLA firmware.
3978 */
3979 if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
3980 !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
3981 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
3982 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
3983 }
3984
Hayes Wanga66edaa2019-10-21 11:41:10 +08003985 length = __le32_to_cpu(mac->blk_hdr.length);
3986 length -= __le16_to_cpu(mac->fw_offset);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003987
Hayes Wanga66edaa2019-10-21 11:41:10 +08003988 data = (u8 *)mac;
3989 data += __le16_to_cpu(mac->fw_offset);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003990
Hayes Wanga66edaa2019-10-21 11:41:10 +08003991 generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
Hayes Wang9370f2d2019-10-16 11:02:42 +08003992 type);
3993
Hayes Wanga66edaa2019-10-21 11:41:10 +08003994 ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
3995 __le16_to_cpu(mac->bp_ba_value));
Hayes Wang9370f2d2019-10-16 11:02:42 +08003996
Hayes Wanga66edaa2019-10-21 11:41:10 +08003997 bp_index = __le16_to_cpu(mac->bp_start);
3998 bp_num = __le16_to_cpu(mac->bp_num);
Hayes Wang9370f2d2019-10-16 11:02:42 +08003999 for (i = 0; i < bp_num; i++) {
Hayes Wanga66edaa2019-10-21 11:41:10 +08004000 ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
Hayes Wang9370f2d2019-10-16 11:02:42 +08004001 bp_index += 2;
4002 }
4003
Hayes Wanga66edaa2019-10-21 11:41:10 +08004004 bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004005 if (bp_en_addr)
4006 ocp_write_word(tp, type, bp_en_addr,
Hayes Wanga66edaa2019-10-21 11:41:10 +08004007 __le16_to_cpu(mac->bp_en_value));
Hayes Wang9370f2d2019-10-16 11:02:42 +08004008
Hayes Wanga66edaa2019-10-21 11:41:10 +08004009 fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004010 if (fw_ver_reg)
4011 ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
Hayes Wanga66edaa2019-10-21 11:41:10 +08004012 mac->fw_ver_data);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004013
Hayes Wanga66edaa2019-10-21 11:41:10 +08004014 dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004015}
4016
4017static void rtl8152_apply_firmware(struct r8152 *tp)
4018{
4019 struct rtl_fw *rtl_fw = &tp->rtl_fw;
Hayes Wang8e484eb2019-10-23 21:24:44 +08004020 const struct firmware *fw;
4021 struct fw_header *fw_hdr;
Hayes Wangaf142882019-10-21 11:41:13 +08004022 struct fw_phy_patch_key *key;
4023 u16 key_addr = 0;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004024 int i;
4025
4026 if (IS_ERR_OR_NULL(rtl_fw->fw))
4027 return;
4028
Hayes Wang8e484eb2019-10-23 21:24:44 +08004029 fw = rtl_fw->fw;
4030 fw_hdr = (struct fw_header *)fw->data;
4031
Hayes Wang9370f2d2019-10-16 11:02:42 +08004032 if (rtl_fw->pre_fw)
4033 rtl_fw->pre_fw(tp);
4034
4035 for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
4036 struct fw_block *block = (struct fw_block *)&fw->data[i];
4037
4038 switch (__le32_to_cpu(block->type)) {
4039 case RTL_FW_END:
4040 goto post_fw;
4041 case RTL_FW_PLA:
4042 case RTL_FW_USB:
Hayes Wanga66edaa2019-10-21 11:41:10 +08004043 rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
Hayes Wang9370f2d2019-10-16 11:02:42 +08004044 break;
Hayes Wangaf142882019-10-21 11:41:13 +08004045 case RTL_FW_PHY_START:
4046 key = (struct fw_phy_patch_key *)block;
4047 key_addr = __le16_to_cpu(key->key_reg);
4048 r8153_pre_ram_code(tp, key_addr,
4049 __le16_to_cpu(key->key_data));
4050 break;
4051 case RTL_FW_PHY_STOP:
4052 WARN_ON(!key_addr);
4053 r8153_post_ram_code(tp, key_addr);
4054 break;
4055 case RTL_FW_PHY_NC:
4056 rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
4057 break;
Hayes Wang9370f2d2019-10-16 11:02:42 +08004058 default:
4059 break;
4060 }
4061
4062 i += ALIGN(__le32_to_cpu(block->length), 8);
4063 }
4064
4065post_fw:
4066 if (rtl_fw->post_fw)
4067 rtl_fw->post_fw(tp);
4068
4069 strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
4070 dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
4071}
4072
4073static void rtl8152_release_firmware(struct r8152 *tp)
4074{
4075 struct rtl_fw *rtl_fw = &tp->rtl_fw;
4076
4077 if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
4078 release_firmware(rtl_fw->fw);
4079 rtl_fw->fw = NULL;
4080 }
4081}
4082
4083static int rtl8152_request_firmware(struct r8152 *tp)
4084{
4085 struct rtl_fw *rtl_fw = &tp->rtl_fw;
4086 long rc;
4087
4088 if (rtl_fw->fw || !rtl_fw->fw_name) {
4089 dev_info(&tp->intf->dev, "skip request firmware\n");
4090 rc = 0;
4091 goto result;
4092 }
4093
4094 rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
4095 if (rc < 0)
4096 goto result;
4097
4098 rc = rtl8152_check_firmware(tp, rtl_fw);
4099 if (rc < 0)
4100 release_firmware(rtl_fw->fw);
4101
4102result:
4103 if (rc) {
4104 rtl_fw->fw = ERR_PTR(rc);
4105
4106 dev_warn(&tp->intf->dev,
4107 "unable to load firmware patch %s (%ld)\n",
4108 rtl_fw->fw_name, rc);
4109 }
4110
4111 return rc;
4112}
4113
hayeswangcda9fb02016-01-07 17:51:12 +08004114static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08004115{
hayeswangcda9fb02016-01-07 17:51:12 +08004116 if (enable) {
4117 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
4118 LINKENA | DIS_SDSAVE);
4119 } else {
4120 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
4121 DIS_SDSAVE);
4122 msleep(20);
4123 }
hayeswang43499682014-02-18 21:48:58 +08004124}
4125
hayeswange6449532016-09-20 16:22:05 +08004126static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
4127{
4128 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
4129 ocp_reg_write(tp, OCP_EEE_DATA, reg);
4130 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
4131}
4132
4133static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
4134{
4135 u16 data;
4136
4137 r8152_mmd_indirect(tp, dev, reg);
4138 data = ocp_reg_read(tp, OCP_EEE_DATA);
4139 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
4140
4141 return data;
4142}
4143
4144static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
4145{
4146 r8152_mmd_indirect(tp, dev, reg);
4147 ocp_reg_write(tp, OCP_EEE_DATA, data);
4148 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
4149}
4150
4151static void r8152_eee_en(struct r8152 *tp, bool enable)
4152{
4153 u16 config1, config2, config3;
4154 u32 ocp_data;
4155
4156 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4157 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
4158 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
4159 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
4160
4161 if (enable) {
4162 ocp_data |= EEE_RX_EN | EEE_TX_EN;
4163 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
4164 config1 |= sd_rise_time(1);
4165 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
4166 config3 |= fast_snr(42);
4167 } else {
4168 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
4169 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
4170 RX_QUIET_EN);
4171 config1 |= sd_rise_time(7);
4172 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
4173 config3 |= fast_snr(511);
4174 }
4175
4176 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
4177 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
4178 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
4179 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
4180}
4181
Hayes Wange7bde562019-08-23 15:33:41 +08004182static void r8153_eee_en(struct r8152 *tp, bool enable)
hayeswange6449532016-09-20 16:22:05 +08004183{
Hayes Wange7bde562019-08-23 15:33:41 +08004184 u32 ocp_data;
4185 u16 config;
4186
4187 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4188 config = ocp_reg_read(tp, OCP_EEE_CFG);
4189
4190 if (enable) {
4191 ocp_data |= EEE_RX_EN | EEE_TX_EN;
4192 config |= EEE10_EN;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004193 } else {
Hayes Wange7bde562019-08-23 15:33:41 +08004194 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
4195 config &= ~EEE10_EN;
4196 }
4197
4198 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
4199 ocp_reg_write(tp, OCP_EEE_CFG, config);
Hayes Wange7bde562019-08-23 15:33:41 +08004200
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004201 tp->ups_info.eee = enable;
Hayes Wange7bde562019-08-23 15:33:41 +08004202}
4203
4204static void rtl_eee_enable(struct r8152 *tp, bool enable)
4205{
4206 switch (tp->version) {
4207 case RTL_VER_01:
4208 case RTL_VER_02:
4209 case RTL_VER_07:
4210 if (enable) {
4211 r8152_eee_en(tp, true);
4212 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
4213 tp->eee_adv);
4214 } else {
4215 r8152_eee_en(tp, false);
4216 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
4217 }
4218 break;
4219 case RTL_VER_03:
4220 case RTL_VER_04:
4221 case RTL_VER_05:
4222 case RTL_VER_06:
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004223 case RTL_VER_08:
4224 case RTL_VER_09:
Hayes Wange7bde562019-08-23 15:33:41 +08004225 if (enable) {
4226 r8153_eee_en(tp, true);
4227 ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
4228 } else {
4229 r8153_eee_en(tp, false);
4230 ocp_reg_write(tp, OCP_EEE_ADV, 0);
4231 }
4232 break;
Hayes Wange7bde562019-08-23 15:33:41 +08004233 default:
4234 break;
Hayes Wangf4a93be2019-08-23 15:33:40 +08004235 }
hayeswange6449532016-09-20 16:22:05 +08004236}
4237
4238static void r8152b_enable_fc(struct r8152 *tp)
4239{
4240 u16 anar;
4241
4242 anar = r8152_mdio_read(tp, MII_ADVERTISE);
4243 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4244 r8152_mdio_write(tp, MII_ADVERTISE, anar);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004245
4246 tp->ups_info.flow_control = true;
hayeswange6449532016-09-20 16:22:05 +08004247}
4248
hayeswangd70b1132014-09-19 15:17:18 +08004249static void rtl8152_disable(struct r8152 *tp)
4250{
hayeswangcda9fb02016-01-07 17:51:12 +08004251 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004252 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004253 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004254}
4255
hayeswang43499682014-02-18 21:48:58 +08004256static void r8152b_hw_phy_cfg(struct r8152 *tp)
4257{
Hayes Wang9370f2d2019-10-16 11:02:42 +08004258 rtl8152_apply_firmware(tp);
Hayes Wange7bde562019-08-23 15:33:41 +08004259 rtl_eee_enable(tp, tp->eee_en);
hayeswangef39df82016-09-20 16:22:07 +08004260 r8152_aldps_en(tp, true);
4261 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08004262
hayeswangaa66a5f2014-02-18 21:49:04 +08004263 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08004264}
4265
Prashant Malani5f71c842019-10-01 01:35:57 -07004266static void wait_oob_link_list_ready(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004267{
hayeswangdb8515e2014-03-06 15:07:16 +08004268 u32 ocp_data;
4269 int i;
hayeswangac718b62013-05-02 16:01:25 +00004270
Prashant Malani5f71c842019-10-01 01:35:57 -07004271 for (i = 0; i < 1000; i++) {
4272 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4273 if (ocp_data & LINK_LIST_READY)
4274 break;
4275 usleep_range(1000, 2000);
4276 }
4277}
4278
4279static void r8152b_exit_oob(struct r8152 *tp)
4280{
4281 u32 ocp_data;
4282
hayeswangac718b62013-05-02 16:01:25 +00004283 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4284 ocp_data &= ~RCR_ACPT_ALL;
4285 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4286
hayeswang00a5e362014-02-18 21:48:59 +08004287 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08004288 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00004289 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
4290 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
4291
4292 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4293 ocp_data &= ~NOW_IS_OOB;
4294 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4295
4296 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4297 ocp_data &= ~MCU_BORW_EN;
4298 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4299
Prashant Malani5f71c842019-10-01 01:35:57 -07004300 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004301
4302 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4303 ocp_data |= RE_INIT_LL;
4304 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4305
Prashant Malani5f71c842019-10-01 01:35:57 -07004306 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004307
4308 rtl8152_nic_reset(tp);
4309
4310 /* rx share fifo credit full threshold */
4311 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
4312
hayeswanga3cc4652014-07-24 16:37:43 +08004313 if (tp->udev->speed == USB_SPEED_FULL ||
4314 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00004315 /* rx share fifo credit near full threshold */
4316 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
4317 RXFIFO_THR2_FULL);
4318 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
4319 RXFIFO_THR3_FULL);
4320 } else {
4321 /* rx share fifo credit near full threshold */
4322 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
4323 RXFIFO_THR2_HIGH);
4324 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
4325 RXFIFO_THR3_HIGH);
4326 }
4327
4328 /* TX share fifo free credit full threshold */
4329 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
4330
4331 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08004332 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00004333 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
4334 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
4335
hayeswangc5554292014-09-12 10:43:11 +08004336 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00004337
4338 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
4339
4340 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
4341 ocp_data |= TCR0_AUTO_FIFO;
4342 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
4343}
4344
4345static void r8152b_enter_oob(struct r8152 *tp)
4346{
hayeswang45f4a192014-01-06 17:08:41 +08004347 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00004348
4349 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4350 ocp_data &= ~NOW_IS_OOB;
4351 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4352
4353 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
4354 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
4355 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
4356
hayeswangd70b1132014-09-19 15:17:18 +08004357 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00004358
Prashant Malani5f71c842019-10-01 01:35:57 -07004359 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004360
4361 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4362 ocp_data |= RE_INIT_LL;
4363 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4364
Prashant Malani5f71c842019-10-01 01:35:57 -07004365 wait_oob_link_list_ready(tp);
hayeswangac718b62013-05-02 16:01:25 +00004366
4367 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
4368
hayeswangc5554292014-09-12 10:43:11 +08004369 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00004370
Kevin Lo59c0b472019-08-01 11:29:38 +08004371 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswangac718b62013-05-02 16:01:25 +00004372 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08004373 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswangac718b62013-05-02 16:01:25 +00004374
4375 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4376 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
4377 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4378
hayeswang00a5e362014-02-18 21:48:59 +08004379 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004380
4381 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4382 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
4383 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4384}
4385
Hayes Wang9370f2d2019-10-16 11:02:42 +08004386static int r8153_pre_firmware_1(struct r8152 *tp)
4387{
4388 int i;
4389
4390 /* Wait till the WTD timer is ready. It would take at most 104 ms. */
4391 for (i = 0; i < 104; i++) {
4392 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
4393
4394 if (!(ocp_data & WTD1_EN))
4395 break;
4396 usleep_range(1000, 2000);
4397 }
4398
4399 return 0;
4400}
4401
4402static int r8153_post_firmware_1(struct r8152 *tp)
4403{
4404 /* set USB_BP_4 to support USB_SPEED_SUPER only */
4405 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
4406 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
4407
4408 /* reset UPHY timer to 36 ms */
4409 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
4410
4411 return 0;
4412}
4413
4414static int r8153_pre_firmware_2(struct r8152 *tp)
4415{
4416 u32 ocp_data;
4417
4418 r8153_pre_firmware_1(tp);
4419
4420 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
4421 ocp_data &= ~FW_FIX_SUSPEND;
4422 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
4423
4424 return 0;
4425}
4426
4427static int r8153_post_firmware_2(struct r8152 *tp)
4428{
4429 u32 ocp_data;
4430
4431 /* enable bp0 if support USB_SPEED_SUPER only */
4432 if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
4433 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
4434 ocp_data |= BIT(0);
4435 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
4436 }
4437
4438 /* reset UPHY timer to 36 ms */
4439 ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
4440
4441 /* enable U3P3 check, set the counter to 4 */
4442 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
4443
4444 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
4445 ocp_data |= FW_FIX_SUSPEND;
4446 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
4447
4448 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4449 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4450 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4451
4452 return 0;
4453}
4454
4455static int r8153_post_firmware_3(struct r8152 *tp)
4456{
4457 u32 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 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
4464 ocp_data |= FW_IP_RESET_EN;
4465 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
4466
4467 return 0;
4468}
4469
4470static int r8153b_pre_firmware_1(struct r8152 *tp)
4471{
4472 /* enable fc timer and set timer to 1 second. */
4473 ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
4474 CTRL_TIMER_EN | (1000 / 8));
4475
4476 return 0;
4477}
4478
4479static int r8153b_post_firmware_1(struct r8152 *tp)
4480{
4481 u32 ocp_data;
4482
4483 /* enable bp0 for RTL8153-BND */
4484 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
4485 if (ocp_data & BND_MASK) {
4486 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
4487 ocp_data |= BIT(0);
4488 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
4489 }
4490
4491 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
4492 ocp_data |= FLOW_CTRL_PATCH_OPT;
4493 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
4494
4495 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
4496 ocp_data |= FC_PATCH_TASK;
4497 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
4498
4499 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
4500 ocp_data |= FW_IP_RESET_EN;
4501 ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
4502
4503 return 0;
4504}
4505
hayeswange6449532016-09-20 16:22:05 +08004506static void r8153_aldps_en(struct r8152 *tp, bool enable)
4507{
4508 u16 data;
4509
4510 data = ocp_reg_read(tp, OCP_POWER_CFG);
4511 if (enable) {
4512 data |= EN_ALDPS;
4513 ocp_reg_write(tp, OCP_POWER_CFG, data);
4514 } else {
hayeswang4214cc52017-06-09 17:11:46 +08004515 int i;
4516
hayeswange6449532016-09-20 16:22:05 +08004517 data &= ~EN_ALDPS;
4518 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08004519 for (i = 0; i < 20; i++) {
4520 usleep_range(1000, 2000);
4521 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
4522 break;
4523 }
hayeswange6449532016-09-20 16:22:05 +08004524 }
hayeswange6449532016-09-20 16:22:05 +08004525
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004526 tp->ups_info.aldps = enable;
hayeswang65b82d62017-06-15 14:44:03 +08004527}
4528
hayeswang43779f82014-01-02 11:25:10 +08004529static void r8153_hw_phy_cfg(struct r8152 *tp)
4530{
4531 u32 ocp_data;
4532 u16 data;
4533
hayeswangd768c612016-09-20 16:22:09 +08004534 /* disable ALDPS before updating the PHY parameters */
4535 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08004536
hayeswangd768c612016-09-20 16:22:09 +08004537 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08004538 rtl_eee_enable(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004539
Hayes Wang9370f2d2019-10-16 11:02:42 +08004540 rtl8152_apply_firmware(tp);
4541
hayeswang43779f82014-01-02 11:25:10 +08004542 if (tp->version == RTL_VER_03) {
4543 data = ocp_reg_read(tp, OCP_EEE_CFG);
4544 data &= ~CTAP_SHORT_EN;
4545 ocp_reg_write(tp, OCP_EEE_CFG, data);
4546 }
4547
4548 data = ocp_reg_read(tp, OCP_POWER_CFG);
4549 data |= EEE_CLKDIV_EN;
4550 ocp_reg_write(tp, OCP_POWER_CFG, data);
4551
4552 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
4553 data |= EN_10M_BGOFF;
4554 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
4555 data = ocp_reg_read(tp, OCP_POWER_CFG);
4556 data |= EN_10M_PLLOFF;
4557 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08004558 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08004559
4560 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4561 ocp_data |= PFM_PWM_SWITCH;
4562 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4563
hayeswangb4d99de2015-01-19 17:02:46 +08004564 /* Enable LPF corner auto tune */
4565 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08004566
hayeswangb4d99de2015-01-19 17:02:46 +08004567 /* Adjust 10M Amplitude */
4568 sram_write(tp, SRAM_10M_AMP1, 0x00af);
4569 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08004570
Hayes Wange7bde562019-08-23 15:33:41 +08004571 if (tp->eee_en)
4572 rtl_eee_enable(tp, true);
hayeswangaf0287e2016-09-20 16:22:08 +08004573
hayeswangef39df82016-09-20 16:22:07 +08004574 r8153_aldps_en(tp, true);
4575 r8152b_enable_fc(tp);
4576
hayeswang3cb32342017-06-09 17:11:43 +08004577 switch (tp->version) {
4578 case RTL_VER_03:
4579 case RTL_VER_04:
4580 break;
4581 case RTL_VER_05:
4582 case RTL_VER_06:
4583 default:
4584 r8153_u2p3en(tp, true);
4585 break;
4586 }
4587
hayeswangaa66a5f2014-02-18 21:49:04 +08004588 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08004589}
4590
hayeswang65b82d62017-06-15 14:44:03 +08004591static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
4592{
4593 u32 ocp_data;
4594
4595 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
4596 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
4597 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
4598 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
4599
4600 return ocp_data;
4601}
4602
4603static void r8153b_hw_phy_cfg(struct r8152 *tp)
4604{
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004605 u32 ocp_data;
hayeswang65b82d62017-06-15 14:44:03 +08004606 u16 data;
4607
4608 /* disable ALDPS before updating the PHY parameters */
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004609 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004610
4611 /* disable EEE before updating the PHY parameters */
Hayes Wange7bde562019-08-23 15:33:41 +08004612 rtl_eee_enable(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004613
Hayes Wang9370f2d2019-10-16 11:02:42 +08004614 rtl8152_apply_firmware(tp);
4615
hayeswang65b82d62017-06-15 14:44:03 +08004616 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
4617
4618 data = sram_read(tp, SRAM_GREEN_CFG);
4619 data |= R_TUNE_EN;
4620 sram_write(tp, SRAM_GREEN_CFG, data);
4621 data = ocp_reg_read(tp, OCP_NCTL_CFG);
4622 data |= PGA_RETURN_EN;
4623 ocp_reg_write(tp, OCP_NCTL_CFG, data);
4624
4625 /* ADC Bias Calibration:
4626 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
4627 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
4628 * ADC ioffset.
4629 */
4630 ocp_data = r8152_efuse_read(tp, 0x7d);
4631 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
4632 if (data != 0xffff)
4633 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
4634
4635 /* ups mode tx-link-pulse timing adjustment:
4636 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
4637 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
4638 */
4639 ocp_data = ocp_reg_read(tp, 0xc426);
4640 ocp_data &= 0x3fff;
4641 if (ocp_data) {
4642 u32 swr_cnt_1ms_ini;
4643
4644 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
4645 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
4646 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
4647 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
4648 }
4649
4650 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4651 ocp_data |= PFM_PWM_SWITCH;
4652 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4653
4654 /* Advnace EEE */
4655 if (!r8153_patch_request(tp, true)) {
4656 data = ocp_reg_read(tp, OCP_POWER_CFG);
4657 data |= EEE_CLKDIV_EN;
4658 ocp_reg_write(tp, OCP_POWER_CFG, data);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004659 tp->ups_info.eee_ckdiv = true;
hayeswang65b82d62017-06-15 14:44:03 +08004660
4661 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
4662 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
4663 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004664 tp->ups_info.eee_cmod_lv = true;
4665 tp->ups_info._10m_ckdiv = true;
4666 tp->ups_info.eee_plloff_giga = true;
hayeswang65b82d62017-06-15 14:44:03 +08004667
4668 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
4669 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004670 tp->ups_info._250m_ckdiv = true;
hayeswang65b82d62017-06-15 14:44:03 +08004671
4672 r8153_patch_request(tp, false);
4673 }
4674
Hayes Wange7bde562019-08-23 15:33:41 +08004675 if (tp->eee_en)
4676 rtl_eee_enable(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08004677
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004678 r8153_aldps_en(tp, true);
4679 r8152b_enable_fc(tp);
hayeswang65b82d62017-06-15 14:44:03 +08004680 r8153_u2p3en(tp, true);
4681
4682 set_bit(PHY_RESET, &tp->flags);
4683}
4684
hayeswang43779f82014-01-02 11:25:10 +08004685static void r8153_first_init(struct r8152 *tp)
4686{
4687 u32 ocp_data;
hayeswang43779f82014-01-02 11:25:10 +08004688
hayeswang134f98b2017-06-09 17:11:40 +08004689 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08004690 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08004691 r8153_teredo_off(tp);
4692
4693 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4694 ocp_data &= ~RCR_ACPT_ALL;
4695 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4696
hayeswang43779f82014-01-02 11:25:10 +08004697 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08004698 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08004699
4700 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4701 ocp_data &= ~NOW_IS_OOB;
4702 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4703
4704 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4705 ocp_data &= ~MCU_BORW_EN;
4706 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4707
Prashant Malani5f71c842019-10-01 01:35:57 -07004708 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004709
4710 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4711 ocp_data |= RE_INIT_LL;
4712 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4713
Prashant Malani5f71c842019-10-01 01:35:57 -07004714 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004715
hayeswangc5554292014-09-12 10:43:11 +08004716 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08004717
hayeswangb65c0c92017-06-21 11:25:18 +08004718 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08004719 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08004720 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08004721
4722 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
4723 ocp_data |= TCR0_AUTO_FIFO;
4724 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
4725
4726 rtl8152_nic_reset(tp);
4727
4728 /* rx share fifo credit full threshold */
4729 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
4730 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
4731 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
4732 /* TX share fifo free credit full threshold */
4733 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08004734}
4735
4736static void r8153_enter_oob(struct r8152 *tp)
4737{
4738 u32 ocp_data;
hayeswang43779f82014-01-02 11:25:10 +08004739
hayeswang134f98b2017-06-09 17:11:40 +08004740 r8153_mac_clk_spd(tp, true);
4741
hayeswang43779f82014-01-02 11:25:10 +08004742 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4743 ocp_data &= ~NOW_IS_OOB;
4744 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4745
hayeswangd70b1132014-09-19 15:17:18 +08004746 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08004747 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08004748
Prashant Malani5f71c842019-10-01 01:35:57 -07004749 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004750
4751 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4752 ocp_data |= RE_INIT_LL;
4753 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4754
Prashant Malani5f71c842019-10-01 01:35:57 -07004755 wait_oob_link_list_ready(tp);
hayeswang43779f82014-01-02 11:25:10 +08004756
hayeswangb65c0c92017-06-21 11:25:18 +08004757 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08004758 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004759
hayeswang65b82d62017-06-15 14:44:03 +08004760 switch (tp->version) {
4761 case RTL_VER_03:
4762 case RTL_VER_04:
4763 case RTL_VER_05:
4764 case RTL_VER_06:
4765 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
4766 ocp_data &= ~TEREDO_WAKE_MASK;
4767 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
4768 break;
4769
4770 case RTL_VER_08:
4771 case RTL_VER_09:
4772 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
4773 * type. Set it to zero. bits[7:0] are the W1C bits about
4774 * the events. Set them to all 1 to clear them.
4775 */
4776 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
4777 break;
4778
4779 default:
4780 break;
4781 }
hayeswang43779f82014-01-02 11:25:10 +08004782
hayeswangc5554292014-09-12 10:43:11 +08004783 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08004784
Kevin Lo59c0b472019-08-01 11:29:38 +08004785 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
hayeswang43779f82014-01-02 11:25:10 +08004786 ocp_data |= ALDPS_PROXY_MODE;
Kevin Lo59c0b472019-08-01 11:29:38 +08004787 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004788
4789 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4790 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
4791 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4792
hayeswang00a5e362014-02-18 21:48:59 +08004793 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004794
4795 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4796 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
4797 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4798}
4799
hayeswangd70b1132014-09-19 15:17:18 +08004800static void rtl8153_disable(struct r8152 *tp)
4801{
hayeswangcda9fb02016-01-07 17:51:12 +08004802 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004803 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08004804 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004805 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004806}
4807
Hayes Wang771efed2019-09-02 19:52:28 +08004808static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
4809 u32 advertising)
hayeswangac718b62013-05-02 16:01:25 +00004810{
Hayes Wang771efed2019-09-02 19:52:28 +08004811 u16 bmcr;
hayeswangac718b62013-05-02 16:01:25 +00004812 int ret = 0;
4813
hayeswangac718b62013-05-02 16:01:25 +00004814 if (autoneg == AUTONEG_DISABLE) {
Hayes Wang771efed2019-09-02 19:52:28 +08004815 if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
4816 return -EINVAL;
4817
4818 switch (speed) {
4819 case SPEED_10:
4820 bmcr = BMCR_SPEED10;
4821 if (duplex == DUPLEX_FULL) {
4822 bmcr |= BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004823 tp->ups_info.speed_duplex = FORCE_10M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004824 } else {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004825 tp->ups_info.speed_duplex = FORCE_10M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004826 }
4827 break;
4828 case SPEED_100:
hayeswangac718b62013-05-02 16:01:25 +00004829 bmcr = BMCR_SPEED100;
Hayes Wang771efed2019-09-02 19:52:28 +08004830 if (duplex == DUPLEX_FULL) {
4831 bmcr |= BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004832 tp->ups_info.speed_duplex = FORCE_100M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004833 } else {
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004834 tp->ups_info.speed_duplex = FORCE_100M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004835 }
4836 break;
4837 case SPEED_1000:
4838 if (tp->mii.supports_gmii) {
4839 bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004840 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004841 break;
4842 }
4843 /* fall through */
4844 default:
hayeswangac718b62013-05-02 16:01:25 +00004845 ret = -EINVAL;
4846 goto out;
4847 }
4848
Hayes Wang771efed2019-09-02 19:52:28 +08004849 if (duplex == DUPLEX_FULL)
4850 tp->mii.full_duplex = 1;
4851 else
4852 tp->mii.full_duplex = 0;
4853
4854 tp->mii.force_media = 1;
hayeswangac718b62013-05-02 16:01:25 +00004855 } else {
Hayes Wang771efed2019-09-02 19:52:28 +08004856 u16 anar, tmp1;
4857 u32 support;
4858
4859 support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
4860 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
4861
4862 if (tp->mii.supports_gmii)
4863 support |= RTL_ADVERTISED_1000_FULL;
4864
4865 if (!(advertising & support))
4866 return -EINVAL;
4867
4868 anar = r8152_mdio_read(tp, MII_ADVERTISE);
4869 tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
4870 ADVERTISE_100HALF | ADVERTISE_100FULL);
4871 if (advertising & RTL_ADVERTISED_10_HALF) {
4872 tmp1 |= ADVERTISE_10HALF;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004873 tp->ups_info.speed_duplex = NWAY_10M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004874 }
4875 if (advertising & RTL_ADVERTISED_10_FULL) {
4876 tmp1 |= ADVERTISE_10FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004877 tp->ups_info.speed_duplex = NWAY_10M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004878 }
4879
4880 if (advertising & RTL_ADVERTISED_100_HALF) {
4881 tmp1 |= ADVERTISE_100HALF;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004882 tp->ups_info.speed_duplex = NWAY_100M_HALF;
Hayes Wang771efed2019-09-02 19:52:28 +08004883 }
4884 if (advertising & RTL_ADVERTISED_100_FULL) {
4885 tmp1 |= ADVERTISE_100FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004886 tp->ups_info.speed_duplex = NWAY_100M_FULL;
Hayes Wang771efed2019-09-02 19:52:28 +08004887 }
4888
4889 if (anar != tmp1) {
4890 r8152_mdio_write(tp, MII_ADVERTISE, tmp1);
4891 tp->mii.advertising = tmp1;
4892 }
4893
4894 if (tp->mii.supports_gmii) {
4895 u16 gbcr;
4896
4897 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
4898 tmp1 = gbcr & ~(ADVERTISE_1000FULL |
4899 ADVERTISE_1000HALF);
4900
4901 if (advertising & RTL_ADVERTISED_1000_FULL) {
4902 tmp1 |= ADVERTISE_1000FULL;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08004903 tp->ups_info.speed_duplex = NWAY_1000M_FULL;
hayeswang65b82d62017-06-15 14:44:03 +08004904 }
Hayes Wang771efed2019-09-02 19:52:28 +08004905
4906 if (gbcr != tmp1)
4907 r8152_mdio_write(tp, MII_CTRL1000, tmp1);
hayeswangac718b62013-05-02 16:01:25 +00004908 }
4909
4910 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Hayes Wang771efed2019-09-02 19:52:28 +08004911
4912 tp->mii.force_media = 0;
hayeswangac718b62013-05-02 16:01:25 +00004913 }
4914
hayeswangfae56172016-06-16 14:08:29 +08004915 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08004916 bmcr |= BMCR_RESET;
4917
hayeswangac718b62013-05-02 16:01:25 +00004918 r8152_mdio_write(tp, MII_BMCR, bmcr);
4919
hayeswangfae56172016-06-16 14:08:29 +08004920 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08004921 int i;
4922
hayeswangaa66a5f2014-02-18 21:49:04 +08004923 for (i = 0; i < 50; i++) {
4924 msleep(20);
4925 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
4926 break;
4927 }
4928 }
4929
hayeswangac718b62013-05-02 16:01:25 +00004930out:
hayeswangac718b62013-05-02 16:01:25 +00004931 return ret;
4932}
4933
hayeswangd70b1132014-09-19 15:17:18 +08004934static void rtl8152_up(struct r8152 *tp)
4935{
4936 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4937 return;
4938
hayeswangcda9fb02016-01-07 17:51:12 +08004939 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004940 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004941 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004942}
4943
hayeswangac718b62013-05-02 16:01:25 +00004944static void rtl8152_down(struct r8152 *tp)
4945{
hayeswang68714382014-04-11 17:54:31 +08004946 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4947 rtl_drop_queued_tx(tp);
4948 return;
4949 }
4950
hayeswang00a5e362014-02-18 21:48:59 +08004951 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08004952 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004953 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004954 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00004955}
4956
hayeswangd70b1132014-09-19 15:17:18 +08004957static void rtl8153_up(struct r8152 *tp)
4958{
4959 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4960 return;
4961
hayeswangb2143962015-07-24 13:54:23 +08004962 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08004963 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08004964 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004965 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004966 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08004967
4968 switch (tp->version) {
4969 case RTL_VER_03:
4970 case RTL_VER_04:
4971 break;
4972 case RTL_VER_05:
4973 case RTL_VER_06:
4974 default:
4975 r8153_u2p3en(tp, true);
4976 break;
4977 }
4978
hayeswangb2143962015-07-24 13:54:23 +08004979 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08004980}
4981
hayeswang43779f82014-01-02 11:25:10 +08004982static void rtl8153_down(struct r8152 *tp)
4983{
hayeswang68714382014-04-11 17:54:31 +08004984 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4985 rtl_drop_queued_tx(tp);
4986 return;
4987 }
4988
hayeswangb9702722014-02-18 21:49:00 +08004989 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08004990 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08004991 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08004992 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004993 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08004994 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08004995}
4996
hayeswang65b82d62017-06-15 14:44:03 +08004997static void rtl8153b_up(struct r8152 *tp)
4998{
4999 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5000 return;
5001
5002 r8153b_u1u2en(tp, false);
5003 r8153_u2p3en(tp, false);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005004 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005005
5006 r8153_first_init(tp);
5007 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
5008
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005009 r8153_aldps_en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08005010 r8153_u2p3en(tp, true);
5011 r8153b_u1u2en(tp, true);
5012}
5013
5014static void rtl8153b_down(struct r8152 *tp)
5015{
5016 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
5017 rtl_drop_queued_tx(tp);
5018 return;
5019 }
5020
5021 r8153b_u1u2en(tp, false);
5022 r8153_u2p3en(tp, false);
5023 r8153b_power_cut_en(tp, false);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005024 r8153_aldps_en(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005025 r8153_enter_oob(tp);
Hayes Wang0e5b36b2019-09-05 10:46:20 +08005026 r8153_aldps_en(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08005027}
5028
hayeswang2dd49e02015-09-07 11:57:44 +08005029static bool rtl8152_in_nway(struct r8152 *tp)
5030{
5031 u16 nway_state;
5032
5033 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
5034 tp->ocp_base = 0x2000;
5035 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
5036 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
5037
5038 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
5039 if (nway_state & 0xc000)
5040 return false;
5041 else
5042 return true;
5043}
5044
5045static bool rtl8153_in_nway(struct r8152 *tp)
5046{
5047 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
5048
5049 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
5050 return false;
5051 else
5052 return true;
5053}
5054
hayeswangac718b62013-05-02 16:01:25 +00005055static void set_carrier(struct r8152 *tp)
5056{
5057 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08005058 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00005059 u8 speed;
5060
5061 speed = rtl8152_get_speed(tp);
5062
5063 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08005064 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08005065 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08005066 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08005067 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00005068 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08005069 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08005070 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5071 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08005072 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08005073 netif_wake_queue(netdev);
5074 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08005075 } else if (netif_queue_stopped(netdev) &&
5076 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
5077 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00005078 }
5079 } else {
hayeswang51d979f2015-02-06 11:30:47 +08005080 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00005081 netif_carrier_off(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08005082 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08005083 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08005084 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08005085 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005086 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08005087 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00005088 }
5089 }
hayeswangac718b62013-05-02 16:01:25 +00005090}
5091
5092static void rtl_work_func_t(struct work_struct *work)
5093{
5094 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
5095
hayeswanga1f83fe2014-11-12 10:05:05 +08005096 /* If the device is unplugged or !netif_running(), the workqueue
5097 * doesn't need to wake the device, and could return directly.
5098 */
5099 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
5100 return;
5101
hayeswang9a4be1b2014-02-18 21:49:07 +08005102 if (usb_autopm_get_interface(tp->intf) < 0)
5103 return;
5104
hayeswangac718b62013-05-02 16:01:25 +00005105 if (!test_bit(WORK_ENABLE, &tp->flags))
5106 goto out1;
5107
hayeswangb5403272014-10-09 18:00:26 +08005108 if (!mutex_trylock(&tp->control)) {
5109 schedule_delayed_work(&tp->schedule, 0);
5110 goto out1;
5111 }
5112
hayeswang216a8342016-01-07 17:51:11 +08005113 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08005114 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00005115
hayeswang216a8342016-01-07 17:51:11 +08005116 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00005117 _rtl8152_set_rx_mode(tp->netdev);
5118
Hayes Wangd2187f82019-08-19 14:40:36 +08005119 /* don't schedule tasket before linking */
5120 if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
hayeswang216a8342016-01-07 17:51:11 +08005121 netif_carrier_ok(tp->netdev))
Hayes Wangd2187f82019-08-19 14:40:36 +08005122 tasklet_schedule(&tp->tx_tl);
hayeswangaa66a5f2014-02-18 21:49:04 +08005123
hayeswangb5403272014-10-09 18:00:26 +08005124 mutex_unlock(&tp->control);
5125
hayeswangac718b62013-05-02 16:01:25 +00005126out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08005127 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00005128}
5129
hayeswanga028a9e2016-06-13 17:49:36 +08005130static void rtl_hw_phy_work_func_t(struct work_struct *work)
5131{
5132 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
5133
5134 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5135 return;
5136
5137 if (usb_autopm_get_interface(tp->intf) < 0)
5138 return;
5139
5140 mutex_lock(&tp->control);
5141
Hayes Wang9370f2d2019-10-16 11:02:42 +08005142 if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
5143 tp->rtl_fw.retry = false;
5144 tp->rtl_fw.fw = NULL;
5145
5146 /* Delay execution in case request_firmware() is not ready yet.
5147 */
5148 queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
5149 goto ignore_once;
5150 }
5151
hayeswanga028a9e2016-06-13 17:49:36 +08005152 tp->rtl_ops.hw_phy_cfg(tp);
5153
Hayes Wang771efed2019-09-02 19:52:28 +08005154 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
5155 tp->advertising);
hayeswang9d21c0d2016-06-13 17:49:37 +08005156
Hayes Wang9370f2d2019-10-16 11:02:42 +08005157ignore_once:
hayeswanga028a9e2016-06-13 17:49:36 +08005158 mutex_unlock(&tp->control);
5159
5160 usb_autopm_put_interface(tp->intf);
5161}
5162
hayeswang5ee3c602016-01-07 17:12:17 +08005163#ifdef CONFIG_PM_SLEEP
5164static int rtl_notifier(struct notifier_block *nb, unsigned long action,
5165 void *data)
5166{
5167 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
5168
5169 switch (action) {
5170 case PM_HIBERNATION_PREPARE:
5171 case PM_SUSPEND_PREPARE:
5172 usb_autopm_get_interface(tp->intf);
5173 break;
5174
5175 case PM_POST_HIBERNATION:
5176 case PM_POST_SUSPEND:
5177 usb_autopm_put_interface(tp->intf);
5178 break;
5179
5180 case PM_POST_RESTORE:
5181 case PM_RESTORE_PREPARE:
5182 default:
5183 break;
5184 }
5185
5186 return NOTIFY_DONE;
5187}
5188#endif
5189
hayeswangac718b62013-05-02 16:01:25 +00005190static int rtl8152_open(struct net_device *netdev)
5191{
5192 struct r8152 *tp = netdev_priv(netdev);
5193 int res = 0;
5194
Hayes Wang9370f2d2019-10-16 11:02:42 +08005195 if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
5196 cancel_delayed_work_sync(&tp->hw_phy_work);
5197 rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
5198 }
5199
hayeswang7e9da482014-02-18 21:49:05 +08005200 res = alloc_all_mem(tp);
5201 if (res)
5202 goto out;
5203
hayeswang9a4be1b2014-02-18 21:49:07 +08005204 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08005205 if (res < 0)
5206 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08005207
hayeswangb5403272014-10-09 18:00:26 +08005208 mutex_lock(&tp->control);
5209
hayeswang7e9da482014-02-18 21:49:05 +08005210 tp->rtl_ops.up(tp);
5211
hayeswang40a82912013-08-14 20:54:40 +08005212 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00005213 netif_start_queue(netdev);
5214 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08005215
hayeswang3d55f442014-02-06 11:55:48 +08005216 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
5217 if (res) {
5218 if (res == -ENODEV)
5219 netif_device_detach(tp->netdev);
5220 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
5221 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08005222 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08005223 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08005224 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005225 tasklet_enable(&tp->tx_tl);
hayeswang3d55f442014-02-06 11:55:48 +08005226
hayeswangb5403272014-10-09 18:00:26 +08005227 mutex_unlock(&tp->control);
5228
hayeswang9a4be1b2014-02-18 21:49:07 +08005229 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08005230#ifdef CONFIG_PM_SLEEP
5231 tp->pm_notifier.notifier_call = rtl_notifier;
5232 register_pm_notifier(&tp->pm_notifier);
5233#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08005234 return 0;
hayeswangac718b62013-05-02 16:01:25 +00005235
Guenter Roeckca0a7532016-11-09 19:51:25 -08005236out_unlock:
5237 mutex_unlock(&tp->control);
5238 usb_autopm_put_interface(tp->intf);
5239out_free:
5240 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08005241out:
hayeswangac718b62013-05-02 16:01:25 +00005242 return res;
5243}
5244
5245static int rtl8152_close(struct net_device *netdev)
5246{
5247 struct r8152 *tp = netdev_priv(netdev);
5248 int res = 0;
5249
hayeswang5ee3c602016-01-07 17:12:17 +08005250#ifdef CONFIG_PM_SLEEP
5251 unregister_pm_notifier(&tp->pm_notifier);
5252#endif
Hayes Wangd2187f82019-08-19 14:40:36 +08005253 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00005254 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08005255 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00005256 cancel_delayed_work_sync(&tp->schedule);
Prashant Malani84811412019-11-20 11:40:21 -08005257 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005258 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08005259
5260 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08005261 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08005262 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08005263 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08005264 } else {
hayeswangb5403272014-10-09 18:00:26 +08005265 mutex_lock(&tp->control);
5266
hayeswang9a4be1b2014-02-18 21:49:07 +08005267 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08005268
5269 mutex_unlock(&tp->control);
5270
hayeswang9a4be1b2014-02-18 21:49:07 +08005271 usb_autopm_put_interface(tp->intf);
5272 }
hayeswangac718b62013-05-02 16:01:25 +00005273
hayeswang7e9da482014-02-18 21:49:05 +08005274 free_all_mem(tp);
5275
hayeswangac718b62013-05-02 16:01:25 +00005276 return res;
5277}
5278
hayeswang4f1d4d52014-03-11 16:24:19 +08005279static void rtl_tally_reset(struct r8152 *tp)
5280{
5281 u32 ocp_data;
5282
5283 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
5284 ocp_data |= TALLY_RESET;
5285 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
5286}
5287
hayeswangac718b62013-05-02 16:01:25 +00005288static void r8152b_init(struct r8152 *tp)
5289{
hayeswangebc2ec482013-08-14 20:54:38 +08005290 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08005291 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00005292
hayeswang68714382014-04-11 17:54:31 +08005293 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5294 return;
5295
hayeswang2dd436d2016-09-20 16:22:06 +08005296 data = r8152_mdio_read(tp, MII_BMCR);
5297 if (data & BMCR_PDOWN) {
5298 data &= ~BMCR_PDOWN;
5299 r8152_mdio_write(tp, MII_BMCR, data);
5300 }
5301
hayeswangcda9fb02016-01-07 17:51:12 +08005302 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08005303
hayeswangac718b62013-05-02 16:01:25 +00005304 if (tp->version == RTL_VER_01) {
5305 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
5306 ocp_data &= ~LED_MODE_MASK;
5307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
5308 }
5309
hayeswang00a5e362014-02-18 21:48:59 +08005310 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00005311
hayeswangac718b62013-05-02 16:01:25 +00005312 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5313 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
5314 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5315 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
5316 ocp_data &= ~MCU_CLK_RATIO_MASK;
5317 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
5318 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
5319 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
5320 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
5321 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
5322
hayeswang4f1d4d52014-03-11 16:24:19 +08005323 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00005324
hayeswangebc2ec482013-08-14 20:54:38 +08005325 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00005326 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08005327 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00005328 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5329}
5330
hayeswang43779f82014-01-02 11:25:10 +08005331static void r8153_init(struct r8152 *tp)
5332{
5333 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08005334 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08005335 int i;
5336
hayeswang68714382014-04-11 17:54:31 +08005337 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5338 return;
5339
hayeswangb9702722014-02-18 21:49:00 +08005340 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005341
5342 for (i = 0; i < 500; i++) {
5343 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
5344 AUTOLOAD_DONE)
5345 break;
5346 msleep(20);
5347 }
5348
hayeswangc564b872017-06-09 17:11:38 +08005349 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08005350
hayeswang2dd436d2016-09-20 16:22:06 +08005351 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
5352 tp->version == RTL_VER_05)
5353 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
5354
5355 data = r8152_mdio_read(tp, MII_BMCR);
5356 if (data & BMCR_PDOWN) {
5357 data &= ~BMCR_PDOWN;
5358 r8152_mdio_write(tp, MII_BMCR, data);
5359 }
5360
hayeswangc564b872017-06-09 17:11:38 +08005361 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08005362
hayeswangb9702722014-02-18 21:49:00 +08005363 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005364
hayeswang65bab842015-02-12 16:20:46 +08005365 if (tp->version == RTL_VER_04) {
5366 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
5367 ocp_data &= ~pwd_dn_scale_mask;
5368 ocp_data |= pwd_dn_scale(96);
5369 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
5370
5371 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5372 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5373 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5374 } else if (tp->version == RTL_VER_05) {
5375 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
5376 ocp_data &= ~ECM_ALDPS;
5377 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
5378
5379 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
5380 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
5381 ocp_data &= ~DYNAMIC_BURST;
5382 else
5383 ocp_data |= DYNAMIC_BURST;
5384 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08005385 } else if (tp->version == RTL_VER_06) {
5386 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
5387 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
5388 ocp_data &= ~DYNAMIC_BURST;
5389 else
5390 ocp_data |= DYNAMIC_BURST;
5391 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
Hayes Wanga3914272020-01-22 16:02:05 +08005392
5393 r8153_queue_wake(tp, false);
5394
5395 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
5396 if (rtl8152_get_speed(tp) & LINK_STATUS)
5397 ocp_data |= CUR_LINK_OK;
5398 else
5399 ocp_data &= ~CUR_LINK_OK;
5400 ocp_data |= POLL_LINK_CHG;
5401 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08005402 }
5403
5404 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
5405 ocp_data |= EP4_FULL_FC;
5406 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
5407
hayeswang43779f82014-01-02 11:25:10 +08005408 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
5409 ocp_data &= ~TIMER11_EN;
5410 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
5411
hayeswang43779f82014-01-02 11:25:10 +08005412 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
5413 ocp_data &= ~LED_MODE_MASK;
5414 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
5415
hayeswang65bab842015-02-12 16:20:46 +08005416 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02005417 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08005418 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08005419 else
5420 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08005421 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
5422
5423 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
5424 ocp_data &= ~SEN_VAL_MASK;
5425 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
5426 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
5427
hayeswang65bab842015-02-12 16:20:46 +08005428 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
5429
hayeswangb9702722014-02-18 21:49:00 +08005430 r8153_power_cut_en(tp, false);
Hayes Wanga3914272020-01-22 16:02:05 +08005431 rtl_runtime_suspend_enable(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08005432 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08005433 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08005434 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08005435
hayeswange31f6362017-06-09 17:11:41 +08005436 /* rx aggregation */
5437 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5438 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005439 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
5440 ocp_data |= RX_AGG_DISABLE;
5441
hayeswange31f6362017-06-09 17:11:41 +08005442 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08005443
hayeswang4f1d4d52014-03-11 16:24:19 +08005444 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08005445
5446 switch (tp->udev->speed) {
5447 case USB_SPEED_SUPER:
5448 case USB_SPEED_SUPER_PLUS:
5449 tp->coalesce = COALESCE_SUPER;
5450 break;
5451 case USB_SPEED_HIGH:
5452 tp->coalesce = COALESCE_HIGH;
5453 break;
5454 default:
5455 tp->coalesce = COALESCE_SLOW;
5456 break;
5457 }
hayeswang43779f82014-01-02 11:25:10 +08005458}
5459
hayeswang65b82d62017-06-15 14:44:03 +08005460static void r8153b_init(struct r8152 *tp)
5461{
5462 u32 ocp_data;
5463 u16 data;
5464 int i;
5465
5466 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5467 return;
5468
5469 r8153b_u1u2en(tp, false);
5470
5471 for (i = 0; i < 500; i++) {
5472 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
5473 AUTOLOAD_DONE)
5474 break;
5475 msleep(20);
5476 }
5477
5478 data = r8153_phy_status(tp, 0);
5479
5480 data = r8152_mdio_read(tp, MII_BMCR);
5481 if (data & BMCR_PDOWN) {
5482 data &= ~BMCR_PDOWN;
5483 r8152_mdio_write(tp, MII_BMCR, data);
5484 }
5485
5486 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
5487
5488 r8153_u2p3en(tp, false);
5489
5490 /* MSC timer = 0xfff * 8ms = 32760 ms */
5491 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
5492
5493 /* U1/U2/L1 idle timer. 500 us */
5494 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5495
5496 r8153b_power_cut_en(tp, false);
5497 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08005498 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08005499 rtl_runtime_suspend_enable(tp, false);
Hayes Wanga3914272020-01-22 16:02:05 +08005500
5501 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
5502 if (rtl8152_get_speed(tp) & LINK_STATUS)
5503 ocp_data |= CUR_LINK_OK;
5504 else
5505 ocp_data &= ~CUR_LINK_OK;
5506 ocp_data |= POLL_LINK_CHG;
5507 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08005508 r8153b_u1u2en(tp, true);
5509 usb_enable_lpm(tp->udev);
5510
5511 /* MAC clock speed down */
5512 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
5513 ocp_data |= MAC_CLK_SPDWN_EN;
5514 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
5515
5516 set_bit(GREEN_ETHERNET, &tp->flags);
5517
5518 /* rx aggregation */
5519 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5520 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
5521 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5522
5523 rtl_tally_reset(tp);
5524
5525 tp->coalesce = 15000; /* 15 us */
5526}
5527
hayeswange5011392015-07-29 20:39:08 +08005528static int rtl8152_pre_reset(struct usb_interface *intf)
5529{
5530 struct r8152 *tp = usb_get_intfdata(intf);
5531 struct net_device *netdev;
5532
5533 if (!tp)
5534 return 0;
5535
5536 netdev = tp->netdev;
5537 if (!netif_running(netdev))
5538 return 0;
5539
hayeswangde9bf292017-01-26 09:38:32 +08005540 netif_stop_queue(netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08005541 tasklet_disable(&tp->tx_tl);
hayeswange5011392015-07-29 20:39:08 +08005542 clear_bit(WORK_ENABLE, &tp->flags);
5543 usb_kill_urb(tp->intr_urb);
5544 cancel_delayed_work_sync(&tp->schedule);
Hayes Wang5b1d9c12019-11-22 16:21:09 +08005545 napi_disable(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08005546 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08005547 mutex_lock(&tp->control);
5548 tp->rtl_ops.disable(tp);
5549 mutex_unlock(&tp->control);
5550 }
5551
5552 return 0;
5553}
5554
5555static int rtl8152_post_reset(struct usb_interface *intf)
5556{
5557 struct r8152 *tp = usb_get_intfdata(intf);
5558 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05005559 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08005560
5561 if (!tp)
5562 return 0;
5563
Mario Limonciello25766272019-04-04 13:46:53 -05005564 /* reset the MAC adddress in case of policy change */
5565 if (determine_ethernet_addr(tp, &sa) >= 0) {
5566 rtnl_lock();
5567 dev_set_mac_address (tp->netdev, &sa, NULL);
5568 rtnl_unlock();
5569 }
5570
hayeswange5011392015-07-29 20:39:08 +08005571 netdev = tp->netdev;
5572 if (!netif_running(netdev))
5573 return 0;
5574
5575 set_bit(WORK_ENABLE, &tp->flags);
5576 if (netif_carrier_ok(netdev)) {
5577 mutex_lock(&tp->control);
5578 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08005579 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08005580 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08005581 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08005582 }
5583
5584 napi_enable(&tp->napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005585 tasklet_enable(&tp->tx_tl);
hayeswangde9bf292017-01-26 09:38:32 +08005586 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08005587 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08005588
hayeswang7489bda2017-01-26 09:38:34 +08005589 if (!list_empty(&tp->rx_done))
5590 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08005591
5592 return 0;
hayeswangac718b62013-05-02 16:01:25 +00005593}
5594
hayeswang2dd49e02015-09-07 11:57:44 +08005595static bool delay_autosuspend(struct r8152 *tp)
5596{
5597 bool sw_linking = !!netif_carrier_ok(tp->netdev);
5598 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
5599
5600 /* This means a linking change occurs and the driver doesn't detect it,
5601 * yet. If the driver has disabled tx/rx and hw is linking on, the
5602 * device wouldn't wake up by receiving any packet.
5603 */
5604 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
5605 return true;
5606
5607 /* If the linking down is occurred by nway, the device may miss the
5608 * linking change event. And it wouldn't wake when linking on.
5609 */
5610 if (!sw_linking && tp->rtl_ops.in_nway(tp))
5611 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08005612 else if (!skb_queue_empty(&tp->tx_queue))
5613 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08005614 else
5615 return false;
5616}
5617
hayeswang21cbd0e2017-06-13 15:14:39 +08005618static int rtl8152_runtime_resume(struct r8152 *tp)
5619{
5620 struct net_device *netdev = tp->netdev;
5621
5622 if (netif_running(netdev) && netdev->flags & IFF_UP) {
5623 struct napi_struct *napi = &tp->napi;
5624
5625 tp->rtl_ops.autosuspend_en(tp, false);
5626 napi_disable(napi);
5627 set_bit(WORK_ENABLE, &tp->flags);
5628
5629 if (netif_carrier_ok(netdev)) {
5630 if (rtl8152_get_speed(tp) & LINK_STATUS) {
5631 rtl_start_rx(tp);
5632 } else {
5633 netif_carrier_off(netdev);
5634 tp->rtl_ops.disable(tp);
5635 netif_info(tp, link, netdev, "linking down\n");
5636 }
5637 }
5638
5639 napi_enable(napi);
5640 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5641 smp_mb__after_atomic();
5642
5643 if (!list_empty(&tp->rx_done))
5644 napi_schedule(&tp->napi);
5645
5646 usb_submit_urb(tp->intr_urb, GFP_NOIO);
5647 } else {
5648 if (netdev->flags & IFF_UP)
5649 tp->rtl_ops.autosuspend_en(tp, false);
5650
5651 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5652 }
5653
5654 return 0;
5655}
5656
5657static int rtl8152_system_resume(struct r8152 *tp)
5658{
5659 struct net_device *netdev = tp->netdev;
5660
5661 netif_device_attach(netdev);
5662
Hayes Wang5b1d9c12019-11-22 16:21:09 +08005663 if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
hayeswang21cbd0e2017-06-13 15:14:39 +08005664 tp->rtl_ops.up(tp);
5665 netif_carrier_off(netdev);
5666 set_bit(WORK_ENABLE, &tp->flags);
5667 usb_submit_urb(tp->intr_urb, GFP_NOIO);
5668 }
5669
5670 return 0;
5671}
5672
hayeswanga9c54ad2017-01-25 13:41:45 +08005673static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00005674{
hayeswang6cc69f22014-10-17 16:55:08 +08005675 struct net_device *netdev = tp->netdev;
5676 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00005677
hayeswang26afec32017-01-26 09:38:31 +08005678 set_bit(SELECTIVE_SUSPEND, &tp->flags);
5679 smp_mb__after_atomic();
5680
hayeswang8fb28062017-01-10 17:04:06 +08005681 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08005682 u32 rcr = 0;
5683
hayeswang75dc6922017-01-10 17:04:07 +08005684 if (netif_carrier_ok(netdev)) {
5685 u32 ocp_data;
5686
5687 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5688 ocp_data = rcr & ~RCR_ACPT_ALL;
5689 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5690 rxdy_gated_en(tp, true);
5691 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
5692 PLA_OOB_CTRL);
5693 if (!(ocp_data & RXFIFO_EMPTY)) {
5694 rxdy_gated_en(tp, false);
5695 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08005696 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5697 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08005698 ret = -EBUSY;
5699 goto out1;
5700 }
5701 }
5702
hayeswang8fb28062017-01-10 17:04:06 +08005703 clear_bit(WORK_ENABLE, &tp->flags);
5704 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08005705
hayeswang8fb28062017-01-10 17:04:06 +08005706 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08005707
5708 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08005709 struct napi_struct *napi = &tp->napi;
5710
5711 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08005712 rtl_stop_rx(tp);
5713 rxdy_gated_en(tp, false);
5714 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08005715 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08005716 }
hayeswangbd882982017-06-13 15:14:40 +08005717
5718 if (delay_autosuspend(tp)) {
5719 rtl8152_runtime_resume(tp);
5720 ret = -EBUSY;
5721 }
hayeswang6cc69f22014-10-17 16:55:08 +08005722 }
5723
hayeswang8fb28062017-01-10 17:04:06 +08005724out1:
5725 return ret;
5726}
5727
5728static int rtl8152_system_suspend(struct r8152 *tp)
5729{
5730 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08005731
5732 netif_device_detach(netdev);
5733
hayeswange3bd1a82014-10-29 11:12:17 +08005734 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08005735 struct napi_struct *napi = &tp->napi;
5736
hayeswangac718b62013-05-02 16:01:25 +00005737 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08005738 usb_kill_urb(tp->intr_urb);
Hayes Wangd2187f82019-08-19 14:40:36 +08005739 tasklet_disable(&tp->tx_tl);
hayeswangce594e92017-03-16 14:32:22 +08005740 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08005741 cancel_delayed_work_sync(&tp->schedule);
5742 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08005743 napi_enable(napi);
Hayes Wangd2187f82019-08-19 14:40:36 +08005744 tasklet_enable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00005745 }
hayeswang8fb28062017-01-10 17:04:06 +08005746
zhong jiangf7419172018-08-09 09:39:13 +08005747 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08005748}
5749
5750static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
5751{
5752 struct r8152 *tp = usb_get_intfdata(intf);
5753 int ret;
5754
5755 mutex_lock(&tp->control);
5756
5757 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08005758 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08005759 else
5760 ret = rtl8152_system_suspend(tp);
5761
hayeswangb5403272014-10-09 18:00:26 +08005762 mutex_unlock(&tp->control);
5763
hayeswang6cc69f22014-10-17 16:55:08 +08005764 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005765}
5766
5767static int rtl8152_resume(struct usb_interface *intf)
5768{
5769 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08005770 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005771
hayeswangb5403272014-10-09 18:00:26 +08005772 mutex_lock(&tp->control);
5773
hayeswang21cbd0e2017-06-13 15:14:39 +08005774 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
5775 ret = rtl8152_runtime_resume(tp);
5776 else
5777 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00005778
hayeswangb5403272014-10-09 18:00:26 +08005779 mutex_unlock(&tp->control);
5780
hayeswang21cbd0e2017-06-13 15:14:39 +08005781 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005782}
5783
hayeswang7ec25412016-01-04 14:38:46 +08005784static int rtl8152_reset_resume(struct usb_interface *intf)
5785{
5786 struct r8152 *tp = usb_get_intfdata(intf);
5787
5788 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08005789 tp->rtl_ops.init(tp);
5790 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
Kai-Heng Fenga54cdee2019-10-04 20:51:04 +08005791 set_ethernet_addr(tp);
hayeswang7ec25412016-01-04 14:38:46 +08005792 return rtl8152_resume(intf);
5793}
5794
hayeswang21ff2e82014-02-18 21:49:06 +08005795static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
5796{
5797 struct r8152 *tp = netdev_priv(dev);
5798
hayeswang9a4be1b2014-02-18 21:49:07 +08005799 if (usb_autopm_get_interface(tp->intf) < 0)
5800 return;
5801
hayeswang7daed8d2015-07-24 13:54:24 +08005802 if (!rtl_can_wakeup(tp)) {
5803 wol->supported = 0;
5804 wol->wolopts = 0;
5805 } else {
5806 mutex_lock(&tp->control);
5807 wol->supported = WAKE_ANY;
5808 wol->wolopts = __rtl_get_wol(tp);
5809 mutex_unlock(&tp->control);
5810 }
hayeswangb5403272014-10-09 18:00:26 +08005811
hayeswang9a4be1b2014-02-18 21:49:07 +08005812 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08005813}
5814
5815static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
5816{
5817 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08005818 int ret;
5819
hayeswang7daed8d2015-07-24 13:54:24 +08005820 if (!rtl_can_wakeup(tp))
5821 return -EOPNOTSUPP;
5822
Florian Fainellif2750df2018-09-28 16:18:54 -07005823 if (wol->wolopts & ~WAKE_ANY)
5824 return -EINVAL;
5825
hayeswang9a4be1b2014-02-18 21:49:07 +08005826 ret = usb_autopm_get_interface(tp->intf);
5827 if (ret < 0)
5828 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08005829
hayeswangb5403272014-10-09 18:00:26 +08005830 mutex_lock(&tp->control);
5831
hayeswang21ff2e82014-02-18 21:49:06 +08005832 __rtl_set_wol(tp, wol->wolopts);
5833 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
5834
hayeswangb5403272014-10-09 18:00:26 +08005835 mutex_unlock(&tp->control);
5836
hayeswang9a4be1b2014-02-18 21:49:07 +08005837 usb_autopm_put_interface(tp->intf);
5838
5839out_set_wol:
5840 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08005841}
5842
hayeswanga5ec27c2014-02-18 21:49:11 +08005843static u32 rtl8152_get_msglevel(struct net_device *dev)
5844{
5845 struct r8152 *tp = netdev_priv(dev);
5846
5847 return tp->msg_enable;
5848}
5849
5850static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
5851{
5852 struct r8152 *tp = netdev_priv(dev);
5853
5854 tp->msg_enable = value;
5855}
5856
hayeswangac718b62013-05-02 16:01:25 +00005857static void rtl8152_get_drvinfo(struct net_device *netdev,
5858 struct ethtool_drvinfo *info)
5859{
5860 struct r8152 *tp = netdev_priv(netdev);
5861
hayeswangb0b46c72014-08-26 10:08:23 +08005862 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
5863 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00005864 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
Hayes Wang9370f2d2019-10-16 11:02:42 +08005865 if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
5866 strlcpy(info->fw_version, tp->rtl_fw.version,
5867 sizeof(info->fw_version));
hayeswangac718b62013-05-02 16:01:25 +00005868}
5869
5870static
Philippe Reynes06144dc2017-03-12 22:41:58 +01005871int rtl8152_get_link_ksettings(struct net_device *netdev,
5872 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00005873{
5874 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08005875 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005876
5877 if (!tp->mii.mdio_read)
5878 return -EOPNOTSUPP;
5879
hayeswang8d4a4d72014-10-09 18:00:25 +08005880 ret = usb_autopm_get_interface(tp->intf);
5881 if (ret < 0)
5882 goto out;
5883
hayeswangb5403272014-10-09 18:00:26 +08005884 mutex_lock(&tp->control);
5885
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03005886 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08005887
hayeswangb5403272014-10-09 18:00:26 +08005888 mutex_unlock(&tp->control);
5889
hayeswang8d4a4d72014-10-09 18:00:25 +08005890 usb_autopm_put_interface(tp->intf);
5891
5892out:
5893 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005894}
5895
Philippe Reynes06144dc2017-03-12 22:41:58 +01005896static int rtl8152_set_link_ksettings(struct net_device *dev,
5897 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00005898{
5899 struct r8152 *tp = netdev_priv(dev);
Hayes Wang771efed2019-09-02 19:52:28 +08005900 u32 advertising = 0;
hayeswang9a4be1b2014-02-18 21:49:07 +08005901 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005902
hayeswang9a4be1b2014-02-18 21:49:07 +08005903 ret = usb_autopm_get_interface(tp->intf);
5904 if (ret < 0)
5905 goto out;
5906
Hayes Wang771efed2019-09-02 19:52:28 +08005907 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
5908 cmd->link_modes.advertising))
5909 advertising |= RTL_ADVERTISED_10_HALF;
5910
5911 if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
5912 cmd->link_modes.advertising))
5913 advertising |= RTL_ADVERTISED_10_FULL;
5914
5915 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
5916 cmd->link_modes.advertising))
5917 advertising |= RTL_ADVERTISED_100_HALF;
5918
5919 if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
5920 cmd->link_modes.advertising))
5921 advertising |= RTL_ADVERTISED_100_FULL;
5922
5923 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
5924 cmd->link_modes.advertising))
5925 advertising |= RTL_ADVERTISED_1000_HALF;
5926
5927 if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
5928 cmd->link_modes.advertising))
5929 advertising |= RTL_ADVERTISED_1000_FULL;
5930
hayeswangb5403272014-10-09 18:00:26 +08005931 mutex_lock(&tp->control);
5932
Philippe Reynes06144dc2017-03-12 22:41:58 +01005933 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
Hayes Wang771efed2019-09-02 19:52:28 +08005934 cmd->base.duplex, advertising);
hayeswangaa7e26b2016-06-13 17:49:38 +08005935 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01005936 tp->autoneg = cmd->base.autoneg;
5937 tp->speed = cmd->base.speed;
5938 tp->duplex = cmd->base.duplex;
Hayes Wang771efed2019-09-02 19:52:28 +08005939 tp->advertising = advertising;
hayeswangaa7e26b2016-06-13 17:49:38 +08005940 }
hayeswang9a4be1b2014-02-18 21:49:07 +08005941
hayeswangb5403272014-10-09 18:00:26 +08005942 mutex_unlock(&tp->control);
5943
hayeswang9a4be1b2014-02-18 21:49:07 +08005944 usb_autopm_put_interface(tp->intf);
5945
5946out:
5947 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005948}
5949
hayeswang4f1d4d52014-03-11 16:24:19 +08005950static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
5951 "tx_packets",
5952 "rx_packets",
5953 "tx_errors",
5954 "rx_errors",
5955 "rx_missed",
5956 "align_errors",
5957 "tx_single_collisions",
5958 "tx_multi_collisions",
5959 "rx_unicast",
5960 "rx_broadcast",
5961 "rx_multicast",
5962 "tx_aborted",
5963 "tx_underrun",
5964};
5965
5966static int rtl8152_get_sset_count(struct net_device *dev, int sset)
5967{
5968 switch (sset) {
5969 case ETH_SS_STATS:
5970 return ARRAY_SIZE(rtl8152_gstrings);
5971 default:
5972 return -EOPNOTSUPP;
5973 }
5974}
5975
5976static void rtl8152_get_ethtool_stats(struct net_device *dev,
5977 struct ethtool_stats *stats, u64 *data)
5978{
5979 struct r8152 *tp = netdev_priv(dev);
5980 struct tally_counter tally;
5981
hayeswang0b030242014-07-08 14:49:28 +08005982 if (usb_autopm_get_interface(tp->intf) < 0)
5983 return;
5984
hayeswang4f1d4d52014-03-11 16:24:19 +08005985 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
5986
hayeswang0b030242014-07-08 14:49:28 +08005987 usb_autopm_put_interface(tp->intf);
5988
hayeswang4f1d4d52014-03-11 16:24:19 +08005989 data[0] = le64_to_cpu(tally.tx_packets);
5990 data[1] = le64_to_cpu(tally.rx_packets);
5991 data[2] = le64_to_cpu(tally.tx_errors);
5992 data[3] = le32_to_cpu(tally.rx_errors);
5993 data[4] = le16_to_cpu(tally.rx_missed);
5994 data[5] = le16_to_cpu(tally.align_errors);
5995 data[6] = le32_to_cpu(tally.tx_one_collision);
5996 data[7] = le32_to_cpu(tally.tx_multi_collision);
5997 data[8] = le64_to_cpu(tally.rx_unicast);
5998 data[9] = le64_to_cpu(tally.rx_broadcast);
5999 data[10] = le32_to_cpu(tally.rx_multicast);
6000 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08006001 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08006002}
6003
6004static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
6005{
6006 switch (stringset) {
6007 case ETH_SS_STATS:
6008 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
6009 break;
6010 }
6011}
6012
hayeswangdf35d282014-09-25 20:54:02 +08006013static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
6014{
Hayes Wangf4a93be2019-08-23 15:33:40 +08006015 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08006016 u16 val;
6017
6018 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
6019 supported = mmd_eee_cap_to_ethtool_sup_t(val);
6020
6021 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
6022 adv = mmd_eee_adv_to_ethtool_adv_t(val);
6023
6024 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
6025 lp = mmd_eee_adv_to_ethtool_adv_t(val);
6026
Hayes Wangf4a93be2019-08-23 15:33:40 +08006027 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08006028 eee->eee_active = !!(supported & adv & lp);
6029 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006030 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08006031 eee->lp_advertised = lp;
6032
6033 return 0;
6034}
6035
6036static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
6037{
6038 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
6039
Hayes Wangf4a93be2019-08-23 15:33:40 +08006040 tp->eee_en = eee->eee_enabled;
6041 tp->eee_adv = val;
6042
Hayes Wange7bde562019-08-23 15:33:41 +08006043 rtl_eee_enable(tp, tp->eee_en);
hayeswangdf35d282014-09-25 20:54:02 +08006044
6045 return 0;
6046}
6047
6048static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
6049{
Hayes Wangf4a93be2019-08-23 15:33:40 +08006050 u32 lp, adv, supported = 0;
hayeswangdf35d282014-09-25 20:54:02 +08006051 u16 val;
6052
6053 val = ocp_reg_read(tp, OCP_EEE_ABLE);
6054 supported = mmd_eee_cap_to_ethtool_sup_t(val);
6055
6056 val = ocp_reg_read(tp, OCP_EEE_ADV);
6057 adv = mmd_eee_adv_to_ethtool_adv_t(val);
6058
6059 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
6060 lp = mmd_eee_adv_to_ethtool_adv_t(val);
6061
Hayes Wangf4a93be2019-08-23 15:33:40 +08006062 eee->eee_enabled = tp->eee_en;
hayeswangdf35d282014-09-25 20:54:02 +08006063 eee->eee_active = !!(supported & adv & lp);
6064 eee->supported = supported;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006065 eee->advertised = tp->eee_adv;
hayeswangdf35d282014-09-25 20:54:02 +08006066 eee->lp_advertised = lp;
6067
6068 return 0;
6069}
6070
hayeswangdf35d282014-09-25 20:54:02 +08006071static int
6072rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
6073{
6074 struct r8152 *tp = netdev_priv(net);
6075 int ret;
6076
6077 ret = usb_autopm_get_interface(tp->intf);
6078 if (ret < 0)
6079 goto out;
6080
hayeswangb5403272014-10-09 18:00:26 +08006081 mutex_lock(&tp->control);
6082
hayeswangdf35d282014-09-25 20:54:02 +08006083 ret = tp->rtl_ops.eee_get(tp, edata);
6084
hayeswangb5403272014-10-09 18:00:26 +08006085 mutex_unlock(&tp->control);
6086
hayeswangdf35d282014-09-25 20:54:02 +08006087 usb_autopm_put_interface(tp->intf);
6088
6089out:
6090 return ret;
6091}
6092
6093static int
6094rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
6095{
6096 struct r8152 *tp = netdev_priv(net);
6097 int ret;
6098
6099 ret = usb_autopm_get_interface(tp->intf);
6100 if (ret < 0)
6101 goto out;
6102
hayeswangb5403272014-10-09 18:00:26 +08006103 mutex_lock(&tp->control);
6104
hayeswangdf35d282014-09-25 20:54:02 +08006105 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08006106 if (!ret)
6107 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08006108
hayeswangb5403272014-10-09 18:00:26 +08006109 mutex_unlock(&tp->control);
6110
hayeswangdf35d282014-09-25 20:54:02 +08006111 usb_autopm_put_interface(tp->intf);
6112
6113out:
6114 return ret;
6115}
6116
hayeswang8884f502014-10-28 14:05:52 +08006117static int rtl8152_nway_reset(struct net_device *dev)
6118{
6119 struct r8152 *tp = netdev_priv(dev);
6120 int ret;
6121
6122 ret = usb_autopm_get_interface(tp->intf);
6123 if (ret < 0)
6124 goto out;
6125
6126 mutex_lock(&tp->control);
6127
6128 ret = mii_nway_restart(&tp->mii);
6129
6130 mutex_unlock(&tp->control);
6131
6132 usb_autopm_put_interface(tp->intf);
6133
6134out:
6135 return ret;
6136}
6137
hayeswangefb3dd82015-02-12 14:33:48 +08006138static int rtl8152_get_coalesce(struct net_device *netdev,
6139 struct ethtool_coalesce *coalesce)
6140{
6141 struct r8152 *tp = netdev_priv(netdev);
6142
6143 switch (tp->version) {
6144 case RTL_VER_01:
6145 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006146 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08006147 return -EOPNOTSUPP;
6148 default:
6149 break;
6150 }
6151
6152 coalesce->rx_coalesce_usecs = tp->coalesce;
6153
6154 return 0;
6155}
6156
6157static int rtl8152_set_coalesce(struct net_device *netdev,
6158 struct ethtool_coalesce *coalesce)
6159{
6160 struct r8152 *tp = netdev_priv(netdev);
6161 int ret;
6162
6163 switch (tp->version) {
6164 case RTL_VER_01:
6165 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006166 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08006167 return -EOPNOTSUPP;
6168 default:
6169 break;
6170 }
6171
6172 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
6173 return -EINVAL;
6174
6175 ret = usb_autopm_get_interface(tp->intf);
6176 if (ret < 0)
6177 return ret;
6178
6179 mutex_lock(&tp->control);
6180
6181 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
6182 tp->coalesce = coalesce->rx_coalesce_usecs;
6183
Hayes Wang9fae5412019-07-03 15:11:56 +08006184 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
6185 netif_stop_queue(netdev);
6186 napi_disable(&tp->napi);
6187 tp->rtl_ops.disable(tp);
6188 tp->rtl_ops.enable(tp);
6189 rtl_start_rx(tp);
6190 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6191 _rtl8152_set_rx_mode(netdev);
6192 napi_enable(&tp->napi);
6193 netif_wake_queue(netdev);
6194 }
hayeswangefb3dd82015-02-12 14:33:48 +08006195 }
6196
6197 mutex_unlock(&tp->control);
6198
6199 usb_autopm_put_interface(tp->intf);
6200
6201 return ret;
6202}
6203
Hayes Wange4a50172019-08-13 11:42:09 +08006204static int rtl8152_get_tunable(struct net_device *netdev,
6205 const struct ethtool_tunable *tunable, void *d)
6206{
6207 struct r8152 *tp = netdev_priv(netdev);
6208
6209 switch (tunable->id) {
6210 case ETHTOOL_RX_COPYBREAK:
6211 *(u32 *)d = tp->rx_copybreak;
6212 break;
6213 default:
6214 return -EOPNOTSUPP;
6215 }
6216
6217 return 0;
6218}
6219
6220static int rtl8152_set_tunable(struct net_device *netdev,
6221 const struct ethtool_tunable *tunable,
6222 const void *d)
6223{
6224 struct r8152 *tp = netdev_priv(netdev);
6225 u32 val;
6226
6227 switch (tunable->id) {
6228 case ETHTOOL_RX_COPYBREAK:
6229 val = *(u32 *)d;
6230 if (val < ETH_ZLEN) {
6231 netif_err(tp, rx_err, netdev,
6232 "Invalid rx copy break value\n");
6233 return -EINVAL;
6234 }
6235
6236 if (tp->rx_copybreak != val) {
Hayes Wang5b1d9c12019-11-22 16:21:09 +08006237 if (netdev->flags & IFF_UP) {
6238 mutex_lock(&tp->control);
6239 napi_disable(&tp->napi);
6240 tp->rx_copybreak = val;
6241 napi_enable(&tp->napi);
6242 mutex_unlock(&tp->control);
6243 } else {
6244 tp->rx_copybreak = val;
6245 }
Hayes Wange4a50172019-08-13 11:42:09 +08006246 }
6247 break;
6248 default:
6249 return -EOPNOTSUPP;
6250 }
6251
6252 return 0;
6253}
6254
6255static void rtl8152_get_ringparam(struct net_device *netdev,
6256 struct ethtool_ringparam *ring)
6257{
6258 struct r8152 *tp = netdev_priv(netdev);
6259
6260 ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
6261 ring->rx_pending = tp->rx_pending;
6262}
6263
6264static int rtl8152_set_ringparam(struct net_device *netdev,
6265 struct ethtool_ringparam *ring)
6266{
6267 struct r8152 *tp = netdev_priv(netdev);
6268
6269 if (ring->rx_pending < (RTL8152_MAX_RX * 2))
6270 return -EINVAL;
6271
6272 if (tp->rx_pending != ring->rx_pending) {
Hayes Wang5b1d9c12019-11-22 16:21:09 +08006273 if (netdev->flags & IFF_UP) {
6274 mutex_lock(&tp->control);
6275 napi_disable(&tp->napi);
6276 tp->rx_pending = ring->rx_pending;
6277 napi_enable(&tp->napi);
6278 mutex_unlock(&tp->control);
6279 } else {
6280 tp->rx_pending = ring->rx_pending;
6281 }
Hayes Wange4a50172019-08-13 11:42:09 +08006282 }
6283
6284 return 0;
6285}
6286
Julia Lawall407a4712016-09-01 00:21:22 +02006287static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00006288 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00006289 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08006290 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08006291 .get_msglevel = rtl8152_get_msglevel,
6292 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08006293 .get_wol = rtl8152_get_wol,
6294 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08006295 .get_strings = rtl8152_get_strings,
6296 .get_sset_count = rtl8152_get_sset_count,
6297 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08006298 .get_coalesce = rtl8152_get_coalesce,
6299 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08006300 .get_eee = rtl_ethtool_get_eee,
6301 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01006302 .get_link_ksettings = rtl8152_get_link_ksettings,
6303 .set_link_ksettings = rtl8152_set_link_ksettings,
Hayes Wange4a50172019-08-13 11:42:09 +08006304 .get_tunable = rtl8152_get_tunable,
6305 .set_tunable = rtl8152_set_tunable,
6306 .get_ringparam = rtl8152_get_ringparam,
6307 .set_ringparam = rtl8152_set_ringparam,
hayeswangac718b62013-05-02 16:01:25 +00006308};
6309
6310static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
6311{
6312 struct r8152 *tp = netdev_priv(netdev);
6313 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08006314 int res;
6315
hayeswang68714382014-04-11 17:54:31 +08006316 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6317 return -ENODEV;
6318
hayeswang9a4be1b2014-02-18 21:49:07 +08006319 res = usb_autopm_get_interface(tp->intf);
6320 if (res < 0)
6321 goto out;
hayeswangac718b62013-05-02 16:01:25 +00006322
6323 switch (cmd) {
6324 case SIOCGMIIPHY:
6325 data->phy_id = R8152_PHY_ID; /* Internal PHY */
6326 break;
6327
6328 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08006329 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006330 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08006331 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006332 break;
6333
6334 case SIOCSMIIREG:
6335 if (!capable(CAP_NET_ADMIN)) {
6336 res = -EPERM;
6337 break;
6338 }
hayeswangb5403272014-10-09 18:00:26 +08006339 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006340 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08006341 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006342 break;
6343
6344 default:
6345 res = -EOPNOTSUPP;
6346 }
6347
hayeswang9a4be1b2014-02-18 21:49:07 +08006348 usb_autopm_put_interface(tp->intf);
6349
6350out:
hayeswangac718b62013-05-02 16:01:25 +00006351 return res;
6352}
6353
hayeswang69b4b7a2014-07-10 10:58:54 +08006354static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
6355{
6356 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08006357 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08006358
6359 switch (tp->version) {
6360 case RTL_VER_01:
6361 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006362 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04006363 dev->mtu = new_mtu;
6364 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08006365 default:
6366 break;
6367 }
6368
hayeswang396e2e22015-02-12 14:33:47 +08006369 ret = usb_autopm_get_interface(tp->intf);
6370 if (ret < 0)
6371 return ret;
6372
6373 mutex_lock(&tp->control);
6374
hayeswang69b4b7a2014-07-10 10:58:54 +08006375 dev->mtu = new_mtu;
6376
hayeswang210c4f72017-03-20 16:13:44 +08006377 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08006378 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08006379
6380 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
6381
6382 if (netif_carrier_ok(dev))
6383 r8153_set_rx_early_size(tp);
6384 }
hayeswang396e2e22015-02-12 14:33:47 +08006385
6386 mutex_unlock(&tp->control);
6387
6388 usb_autopm_put_interface(tp->intf);
6389
6390 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08006391}
6392
hayeswangac718b62013-05-02 16:01:25 +00006393static const struct net_device_ops rtl8152_netdev_ops = {
6394 .ndo_open = rtl8152_open,
6395 .ndo_stop = rtl8152_close,
6396 .ndo_do_ioctl = rtl8152_ioctl,
6397 .ndo_start_xmit = rtl8152_start_xmit,
6398 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08006399 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00006400 .ndo_set_rx_mode = rtl8152_set_rx_mode,
6401 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08006402 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00006403 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08006404 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00006405};
6406
hayeswange3fe0b12014-01-02 11:22:39 +08006407static void rtl8152_unload(struct r8152 *tp)
6408{
hayeswang68714382014-04-11 17:54:31 +08006409 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6410 return;
6411
hayeswang00a5e362014-02-18 21:48:59 +08006412 if (tp->version != RTL_VER_01)
6413 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08006414}
6415
hayeswang43779f82014-01-02 11:25:10 +08006416static void rtl8153_unload(struct r8152 *tp)
6417{
hayeswang68714382014-04-11 17:54:31 +08006418 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6419 return;
6420
hayeswang49be1722014-10-01 13:25:11 +08006421 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08006422}
6423
hayeswang65b82d62017-06-15 14:44:03 +08006424static void rtl8153b_unload(struct r8152 *tp)
6425{
6426 if (test_bit(RTL8152_UNPLUG, &tp->flags))
6427 return;
6428
6429 r8153b_power_cut_en(tp, false);
6430}
6431
hayeswang55b65472014-11-06 12:47:39 +08006432static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08006433{
6434 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08006435 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08006436
hayeswang55b65472014-11-06 12:47:39 +08006437 switch (tp->version) {
6438 case RTL_VER_01:
6439 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006440 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08006441 ops->init = r8152b_init;
6442 ops->enable = rtl8152_enable;
6443 ops->disable = rtl8152_disable;
6444 ops->up = rtl8152_up;
6445 ops->down = rtl8152_down;
6446 ops->unload = rtl8152_unload;
6447 ops->eee_get = r8152_get_eee;
6448 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08006449 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08006450 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08006451 ops->autosuspend_en = rtl_runtime_suspend_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08006452 tp->rx_buf_sz = 16 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006453 tp->eee_en = true;
6454 tp->eee_adv = MDIO_EEE_100TX;
hayeswang43779f82014-01-02 11:25:10 +08006455 break;
6456
hayeswang55b65472014-11-06 12:47:39 +08006457 case RTL_VER_03:
6458 case RTL_VER_04:
6459 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08006460 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08006461 ops->init = r8153_init;
6462 ops->enable = rtl8153_enable;
6463 ops->disable = rtl8153_disable;
6464 ops->up = rtl8153_up;
6465 ops->down = rtl8153_down;
6466 ops->unload = rtl8153_unload;
6467 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08006468 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08006469 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08006470 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08006471 ops->autosuspend_en = rtl8153_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08006472 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006473 tp->eee_en = true;
6474 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswangc81229c2014-01-02 11:22:42 +08006475 break;
6476
hayeswang65b82d62017-06-15 14:44:03 +08006477 case RTL_VER_08:
6478 case RTL_VER_09:
6479 ops->init = r8153b_init;
6480 ops->enable = rtl8153_enable;
Hayes Wang0e5b36b2019-09-05 10:46:20 +08006481 ops->disable = rtl8153_disable;
hayeswang65b82d62017-06-15 14:44:03 +08006482 ops->up = rtl8153b_up;
6483 ops->down = rtl8153b_down;
6484 ops->unload = rtl8153b_unload;
6485 ops->eee_get = r8153_get_eee;
Hayes Wange7bde562019-08-23 15:33:41 +08006486 ops->eee_set = r8152_set_eee;
hayeswang65b82d62017-06-15 14:44:03 +08006487 ops->in_nway = rtl8153_in_nway;
6488 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
6489 ops->autosuspend_en = rtl8153b_runtime_enable;
Hayes Wangec5791c2019-08-13 11:42:05 +08006490 tp->rx_buf_sz = 32 * 1024;
Hayes Wangf4a93be2019-08-23 15:33:40 +08006491 tp->eee_en = true;
6492 tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX;
hayeswang65b82d62017-06-15 14:44:03 +08006493 break;
6494
hayeswangc81229c2014-01-02 11:22:42 +08006495 default:
hayeswang55b65472014-11-06 12:47:39 +08006496 ret = -ENODEV;
6497 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08006498 break;
6499 }
6500
6501 return ret;
6502}
6503
Hayes Wang9370f2d2019-10-16 11:02:42 +08006504#define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw"
6505#define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw"
6506#define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw"
6507#define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw"
6508
6509MODULE_FIRMWARE(FIRMWARE_8153A_2);
6510MODULE_FIRMWARE(FIRMWARE_8153A_3);
6511MODULE_FIRMWARE(FIRMWARE_8153A_4);
6512MODULE_FIRMWARE(FIRMWARE_8153B_2);
6513
6514static int rtl_fw_init(struct r8152 *tp)
6515{
6516 struct rtl_fw *rtl_fw = &tp->rtl_fw;
6517
6518 switch (tp->version) {
6519 case RTL_VER_04:
6520 rtl_fw->fw_name = FIRMWARE_8153A_2;
6521 rtl_fw->pre_fw = r8153_pre_firmware_1;
6522 rtl_fw->post_fw = r8153_post_firmware_1;
6523 break;
6524 case RTL_VER_05:
6525 rtl_fw->fw_name = FIRMWARE_8153A_3;
6526 rtl_fw->pre_fw = r8153_pre_firmware_2;
6527 rtl_fw->post_fw = r8153_post_firmware_2;
6528 break;
6529 case RTL_VER_06:
6530 rtl_fw->fw_name = FIRMWARE_8153A_4;
6531 rtl_fw->post_fw = r8153_post_firmware_3;
6532 break;
6533 case RTL_VER_09:
6534 rtl_fw->fw_name = FIRMWARE_8153B_2;
6535 rtl_fw->pre_fw = r8153b_pre_firmware_1;
6536 rtl_fw->post_fw = r8153b_post_firmware_1;
6537 break;
6538 default:
6539 break;
6540 }
6541
6542 return 0;
6543}
6544
hayeswang33928ee2017-03-17 11:20:13 +08006545static u8 rtl_get_version(struct usb_interface *intf)
6546{
6547 struct usb_device *udev = interface_to_usbdev(intf);
6548 u32 ocp_data = 0;
6549 __le32 *tmp;
6550 u8 version;
6551 int ret;
6552
6553 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
6554 if (!tmp)
6555 return 0;
6556
6557 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
6558 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
6559 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
6560 if (ret > 0)
6561 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
6562
6563 kfree(tmp);
6564
6565 switch (ocp_data) {
6566 case 0x4c00:
6567 version = RTL_VER_01;
6568 break;
6569 case 0x4c10:
6570 version = RTL_VER_02;
6571 break;
6572 case 0x5c00:
6573 version = RTL_VER_03;
6574 break;
6575 case 0x5c10:
6576 version = RTL_VER_04;
6577 break;
6578 case 0x5c20:
6579 version = RTL_VER_05;
6580 break;
6581 case 0x5c30:
6582 version = RTL_VER_06;
6583 break;
hayeswangc27b32c2017-06-15 14:44:02 +08006584 case 0x4800:
6585 version = RTL_VER_07;
6586 break;
hayeswang65b82d62017-06-15 14:44:03 +08006587 case 0x6000:
6588 version = RTL_VER_08;
6589 break;
6590 case 0x6010:
6591 version = RTL_VER_09;
6592 break;
hayeswang33928ee2017-03-17 11:20:13 +08006593 default:
6594 version = RTL_VER_UNKNOWN;
6595 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
6596 break;
6597 }
6598
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02006599 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
6600
hayeswang33928ee2017-03-17 11:20:13 +08006601 return version;
6602}
6603
hayeswangac718b62013-05-02 16:01:25 +00006604static int rtl8152_probe(struct usb_interface *intf,
6605 const struct usb_device_id *id)
6606{
6607 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08006608 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00006609 struct r8152 *tp;
6610 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08006611 int ret;
hayeswangac718b62013-05-02 16:01:25 +00006612
hayeswang33928ee2017-03-17 11:20:13 +08006613 if (version == RTL_VER_UNKNOWN)
6614 return -ENODEV;
6615
hayeswang10c32712014-03-04 20:47:48 +08006616 if (udev->actconfig->desc.bConfigurationValue != 1) {
6617 usb_driver_set_configuration(udev, 1);
6618 return -ENODEV;
6619 }
6620
Johan Hovold86f3f4c2020-01-14 09:27:29 +01006621 if (intf->cur_altsetting->desc.bNumEndpoints < 3)
6622 return -ENODEV;
6623
hayeswang10c32712014-03-04 20:47:48 +08006624 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00006625 netdev = alloc_etherdev(sizeof(struct r8152));
6626 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08006627 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00006628 return -ENOMEM;
6629 }
6630
hayeswangebc2ec482013-08-14 20:54:38 +08006631 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00006632 tp = netdev_priv(netdev);
6633 tp->msg_enable = 0x7FFF;
6634
hayeswange3ad4122014-01-06 17:08:42 +08006635 tp->udev = udev;
6636 tp->netdev = netdev;
6637 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08006638 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08006639
hayeswang33928ee2017-03-17 11:20:13 +08006640 switch (version) {
6641 case RTL_VER_01:
6642 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08006643 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08006644 tp->mii.supports_gmii = 0;
6645 break;
6646 default:
6647 tp->mii.supports_gmii = 1;
6648 break;
6649 }
6650
hayeswang55b65472014-11-06 12:47:39 +08006651 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08006652 if (ret)
6653 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08006654
Hayes Wang9370f2d2019-10-16 11:02:42 +08006655 rtl_fw_init(tp);
6656
hayeswangb5403272014-10-09 18:00:26 +08006657 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00006658 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08006659 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
Hayes Wangd2187f82019-08-19 14:40:36 +08006660 tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp);
6661 tasklet_disable(&tp->tx_tl);
hayeswangac718b62013-05-02 16:01:25 +00006662
hayeswangac718b62013-05-02 16:01:25 +00006663 netdev->netdev_ops = &rtl8152_netdev_ops;
6664 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08006665
hayeswang60c89072014-03-07 11:04:39 +08006666 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08006667 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08006668 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
6669 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08006670 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08006671 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08006672 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08006673 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08006674 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6675 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
6676 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08006677
hayeswang19c0f402017-01-11 16:25:34 +08006678 if (tp->version == RTL_VER_01) {
6679 netdev->features &= ~NETIF_F_RXCSUM;
6680 netdev->hw_features &= ~NETIF_F_RXCSUM;
6681 }
6682
Kai-Heng Feng96477222019-11-05 19:24:52 +08006683 if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO &&
6684 le16_to_cpu(udev->descriptor.idProduct) == 0x3082)
6685 set_bit(LENOVO_MACPASSTHRU, &tp->flags);
6686
Kai-Heng Feng176eb612018-08-20 12:43:51 +08006687 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
Prashant Malani151ea092019-10-02 14:09:33 -07006688 (!strcmp(udev->serial, "000001000000") ||
6689 !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08006690 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
6691 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
6692 }
6693
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006694 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08006695 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00006696
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04006697 /* MTU range: 68 - 1500 or 9194 */
6698 netdev->min_mtu = ETH_MIN_MTU;
6699 switch (tp->version) {
6700 case RTL_VER_01:
6701 case RTL_VER_02:
6702 netdev->max_mtu = ETH_DATA_LEN;
6703 break;
6704 default:
6705 netdev->max_mtu = RTL8153_MAX_MTU;
6706 break;
6707 }
6708
hayeswangac718b62013-05-02 16:01:25 +00006709 tp->mii.dev = netdev;
6710 tp->mii.mdio_read = read_mii_word;
6711 tp->mii.mdio_write = write_mii_word;
6712 tp->mii.phy_id_mask = 0x3f;
6713 tp->mii.reg_num_mask = 0x1f;
6714 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00006715
hayeswangaa7e26b2016-06-13 17:49:38 +08006716 tp->autoneg = AUTONEG_ENABLE;
Hayes Wang771efed2019-09-02 19:52:28 +08006717 tp->speed = SPEED_100;
6718 tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6719 RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6720 if (tp->mii.supports_gmii) {
6721 tp->speed = SPEED_1000;
6722 tp->advertising |= RTL_ADVERTISED_1000_FULL;
6723 }
hayeswangaa7e26b2016-06-13 17:49:38 +08006724 tp->duplex = DUPLEX_FULL;
6725
Hayes Wange4a50172019-08-13 11:42:09 +08006726 tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
6727 tp->rx_pending = 10 * RTL8152_MAX_RX;
6728
hayeswang9a4be1b2014-02-18 21:49:07 +08006729 intf->needs_remote_wakeup = 1;
6730
hayeswangc81229c2014-01-02 11:22:42 +08006731 tp->rtl_ops.init(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08006732#if IS_BUILTIN(CONFIG_USB_RTL8152)
6733 /* Retry in case request_firmware() is not ready yet. */
6734 tp->rtl_fw.retry = true;
6735#endif
hayeswanga028a9e2016-06-13 17:49:36 +08006736 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00006737 set_ethernet_addr(tp);
6738
hayeswangac718b62013-05-02 16:01:25 +00006739 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08006740 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00006741
hayeswangebc2ec482013-08-14 20:54:38 +08006742 ret = register_netdev(netdev);
6743 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08006744 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08006745 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00006746 }
6747
hayeswang7daed8d2015-07-24 13:54:24 +08006748 if (!rtl_can_wakeup(tp))
6749 __rtl_set_wol(tp, 0);
6750
hayeswang21ff2e82014-02-18 21:49:06 +08006751 tp->saved_wolopts = __rtl_get_wol(tp);
6752 if (tp->saved_wolopts)
6753 device_set_wakeup_enable(&udev->dev, true);
6754 else
6755 device_set_wakeup_enable(&udev->dev, false);
6756
Hayes Wang4a8deae2014-01-07 11:18:22 +08006757 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00006758
6759 return 0;
6760
hayeswangac718b62013-05-02 16:01:25 +00006761out1:
Hayes Wangd2187f82019-08-19 14:40:36 +08006762 tasklet_kill(&tp->tx_tl);
hayeswangebc2ec482013-08-14 20:54:38 +08006763 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00006764out:
6765 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08006766 return ret;
hayeswangac718b62013-05-02 16:01:25 +00006767}
6768
hayeswangac718b62013-05-02 16:01:25 +00006769static void rtl8152_disconnect(struct usb_interface *intf)
6770{
6771 struct r8152 *tp = usb_get_intfdata(intf);
6772
6773 usb_set_intfdata(intf, NULL);
6774 if (tp) {
Hayes Wangffa9fec2019-07-04 17:36:32 +08006775 rtl_set_unplug(tp);
hayeswangf561de32014-09-30 16:48:01 +08006776
hayeswangac718b62013-05-02 16:01:25 +00006777 unregister_netdev(tp->netdev);
Hayes Wangd2187f82019-08-19 14:40:36 +08006778 tasklet_kill(&tp->tx_tl);
hayeswanga028a9e2016-06-13 17:49:36 +08006779 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08006780 tp->rtl_ops.unload(tp);
Hayes Wang9370f2d2019-10-16 11:02:42 +08006781 rtl8152_release_firmware(tp);
hayeswangac718b62013-05-02 16:01:25 +00006782 free_netdev(tp->netdev);
6783 }
6784}
6785
hayeswangd9a28c52014-12-04 10:43:11 +08006786#define REALTEK_USB_DEVICE(vend, prod) \
6787 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
6788 USB_DEVICE_ID_MATCH_INT_CLASS, \
6789 .idVendor = (vend), \
6790 .idProduct = (prod), \
6791 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
6792}, \
6793{ \
6794 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
6795 USB_DEVICE_ID_MATCH_DEVICE, \
6796 .idVendor = (vend), \
6797 .idProduct = (prod), \
6798 .bInterfaceClass = USB_CLASS_COMM, \
6799 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
6800 .bInterfaceProtocol = USB_CDC_PROTO_NONE
6801
hayeswangac718b62013-05-02 16:01:25 +00006802/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05306803static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08006804 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08006805 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
6806 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02006807 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
6808 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08006809 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04006810 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08006811 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
6812 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
Kai-Heng Feng96477222019-11-05 19:24:52 +08006813 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082)},
hayeswangd248caf2016-10-18 11:41:48 +08006814 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
6815 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
6816 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Kazutoshi Noguchib3060532019-10-21 00:03:07 +09006817 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)},
Grant Grundler90841042017-09-28 11:35:00 -07006818 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07006819 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08006820 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00006821 {}
6822};
6823
6824MODULE_DEVICE_TABLE(usb, rtl8152_table);
6825
6826static struct usb_driver rtl8152_driver = {
6827 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08006828 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00006829 .probe = rtl8152_probe,
6830 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00006831 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08006832 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08006833 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08006834 .pre_reset = rtl8152_pre_reset,
6835 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08006836 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08006837 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00006838};
6839
Sachin Kamatb4236daa2013-05-16 17:48:08 +00006840module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00006841
6842MODULE_AUTHOR(DRIVER_AUTHOR);
6843MODULE_DESCRIPTION(DRIVER_DESC);
6844MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07006845MODULE_VERSION(DRIVER_VERSION);