blob: e887ac86fbefbdff6319977c2935bea061431920 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
hayeswangac718b62013-05-02 16:01:25 +00002/*
hayeswangc7de7de2014-01-15 10:42:16 +08003 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00004 */
5
hayeswangac718b62013-05-02 16:01:25 +00006#include <linux/signal.h>
7#include <linux/slab.h>
8#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +00009#include <linux/netdevice.h>
10#include <linux/etherdevice.h>
11#include <linux/mii.h>
12#include <linux/ethtool.h>
13#include <linux/usb.h>
14#include <linux/crc32.h>
15#include <linux/if_vlan.h>
16#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080017#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080018#include <linux/ip.h>
19#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080020#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080021#include <uapi/linux/mdio.h>
22#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080023#include <linux/usb/cdc.h>
hayeswang5ee3c602016-01-07 17:12:17 +080024#include <linux/suspend.h>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050025#include <linux/acpi.h>
hayeswangac718b62013-05-02 16:01:25 +000026
hayeswangd0942472015-09-07 11:57:43 +080027/* Information for net-next */
hayeswang65b82d62017-06-15 14:44:03 +080028#define NETNEXT_VERSION "09"
hayeswangd0942472015-09-07 11:57:43 +080029
30/* Information for net */
hayeswangb20cb602017-03-20 16:13:45 +080031#define NET_VERSION "9"
hayeswangd0942472015-09-07 11:57:43 +080032
33#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000034#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080035#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000036#define MODULENAME "r8152"
37
38#define R8152_PHY_ID 32
39
40#define PLA_IDR 0xc000
41#define PLA_RCR 0xc010
42#define PLA_RMS 0xc016
43#define PLA_RXFIFO_CTRL0 0xc0a0
44#define PLA_RXFIFO_CTRL1 0xc0a4
45#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080046#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000047#define PLA_FMC 0xc0b4
48#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080049#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080050#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080052#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000053#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080054#define PLA_TEREDO_TIMER 0xd2cc
55#define PLA_REALWOW_TIMER 0xd2e8
Hayes Wang13e04fbf2019-07-01 15:53:19 +080056#define PLA_SUSPEND_FLAG 0xd38a
57#define PLA_INDICATE_FALG 0xd38c
58#define PLA_EXTRA_STATUS 0xd398
hayeswang65b82d62017-06-15 14:44:03 +080059#define PLA_EFUSE_DATA 0xdd00
60#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000061#define PLA_LEDSEL 0xdd90
62#define PLA_LED_FEATURE 0xdd92
63#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080064#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000065#define PLA_GPHY_INTR_IMR 0xe022
66#define PLA_EEE_CR 0xe040
67#define PLA_EEEP_CR 0xe080
68#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080069#define PLA_MAC_PWR_CTRL2 0xe0ca
70#define PLA_MAC_PWR_CTRL3 0xe0cc
71#define PLA_MAC_PWR_CTRL4 0xe0ce
72#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000073#define PLA_TCR0 0xe610
74#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080075#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000076#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080077#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000078#define PLA_CR 0xe813
79#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080080#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
81#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000082#define PLA_CONFIG5 0xe822
83#define PLA_PHY_PWR 0xe84c
84#define PLA_OOB_CTRL 0xe84f
85#define PLA_CPCR 0xe854
86#define PLA_MISC_0 0xe858
87#define PLA_MISC_1 0xe85a
88#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080089#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000090#define PLA_SFF_STS_7 0xe8de
91#define PLA_PHYSTATUS 0xe908
92#define PLA_BP_BA 0xfc26
93#define PLA_BP_0 0xfc28
94#define PLA_BP_1 0xfc2a
95#define PLA_BP_2 0xfc2c
96#define PLA_BP_3 0xfc2e
97#define PLA_BP_4 0xfc30
98#define PLA_BP_5 0xfc32
99#define PLA_BP_6 0xfc34
100#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800101#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000102
hayeswang65bab842015-02-12 16:20:46 +0800103#define USB_USB2PHY 0xb41e
104#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800105#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800106#define USB_CSR_DUMMY1 0xb464
107#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000108#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800109#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800110#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800111#define USB_BURST_SIZE 0xcfc0
hayeswang65b82d62017-06-15 14:44:03 +0800112#define USB_LPM_CONFIG 0xcfd8
hayeswangac718b62013-05-02 16:01:25 +0000113#define USB_USB_CTRL 0xd406
114#define USB_PHY_CTRL 0xd408
115#define USB_TX_AGG 0xd40a
116#define USB_RX_BUF_TH 0xd40c
117#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800118#define USB_RX_EARLY_TIMEOUT 0xd42c
119#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800120#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
121#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000122#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800123#define USB_UPT_RXDMA_OWN 0xd437
hayeswang43779f82014-01-02 11:25:10 +0800124#define USB_TOLERANCE 0xd490
125#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800126#define USB_BMU_RESET 0xd4b0
hayeswang65b82d62017-06-15 14:44:03 +0800127#define USB_U1U2_TIMER 0xd4da
hayeswangac718b62013-05-02 16:01:25 +0000128#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800129#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800130#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600131#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800132#define USB_AFE_CTRL2 0xd824
hayeswang65b82d62017-06-15 14:44:03 +0800133#define USB_UPS_CFG 0xd842
134#define USB_UPS_FLAGS 0xd848
hayeswang43779f82014-01-02 11:25:10 +0800135#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000136#define USB_BP_BA 0xfc26
137#define USB_BP_0 0xfc28
138#define USB_BP_1 0xfc2a
139#define USB_BP_2 0xfc2c
140#define USB_BP_3 0xfc2e
141#define USB_BP_4 0xfc30
142#define USB_BP_5 0xfc32
143#define USB_BP_6 0xfc34
144#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800145#define USB_BP_EN 0xfc38
hayeswang65b82d62017-06-15 14:44:03 +0800146#define USB_BP_8 0xfc38
147#define USB_BP_9 0xfc3a
148#define USB_BP_10 0xfc3c
149#define USB_BP_11 0xfc3e
150#define USB_BP_12 0xfc40
151#define USB_BP_13 0xfc42
152#define USB_BP_14 0xfc44
153#define USB_BP_15 0xfc46
154#define USB_BP2_EN 0xfc48
hayeswangac718b62013-05-02 16:01:25 +0000155
156/* OCP Registers */
157#define OCP_ALDPS_CONFIG 0x2010
158#define OCP_EEE_CONFIG1 0x2080
159#define OCP_EEE_CONFIG2 0x2092
160#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800161#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000162#define OCP_EEE_AR 0xa41a
163#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800164#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800165#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800166#define OCP_POWER_CFG 0xa430
167#define OCP_EEE_CFG 0xa432
168#define OCP_SRAM_ADDR 0xa436
169#define OCP_SRAM_DATA 0xa438
170#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800171#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800172#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800173#define OCP_EEE_LPABLE 0xa5d2
hayeswang2dd49e02015-09-07 11:57:44 +0800174#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800175#define OCP_PHY_PATCH_STAT 0xb800
176#define OCP_PHY_PATCH_CMD 0xb820
177#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800178#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800179#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800180
181/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800182#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800183#define SRAM_LPF_CFG 0x8012
184#define SRAM_10M_AMP1 0x8080
185#define SRAM_10M_AMP2 0x8082
186#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000187
188/* PLA_RCR */
189#define RCR_AAP 0x00000001
190#define RCR_APM 0x00000002
191#define RCR_AM 0x00000004
192#define RCR_AB 0x00000008
193#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
194
195/* PLA_RXFIFO_CTRL0 */
196#define RXFIFO_THR1_NORMAL 0x00080002
197#define RXFIFO_THR1_OOB 0x01800003
198
199/* PLA_RXFIFO_CTRL1 */
200#define RXFIFO_THR2_FULL 0x00000060
201#define RXFIFO_THR2_HIGH 0x00000038
202#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800203#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000204
205/* PLA_RXFIFO_CTRL2 */
206#define RXFIFO_THR3_FULL 0x00000078
207#define RXFIFO_THR3_HIGH 0x00000048
208#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800209#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000210
211/* PLA_TXFIFO_CTRL */
212#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800213#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000214
hayeswang65bab842015-02-12 16:20:46 +0800215/* PLA_DMY_REG0 */
216#define ECM_ALDPS 0x0002
217
hayeswangac718b62013-05-02 16:01:25 +0000218/* PLA_FMC */
219#define FMC_FCR_MCU_EN 0x0001
220
221/* PLA_EEEP_CR */
222#define EEEP_CR_EEEP_TX 0x0002
223
hayeswang43779f82014-01-02 11:25:10 +0800224/* PLA_WDT6_CTRL */
225#define WDT6_SET_MODE 0x0010
226
hayeswangac718b62013-05-02 16:01:25 +0000227/* PLA_TCR0 */
228#define TCR0_TX_EMPTY 0x0800
229#define TCR0_AUTO_FIFO 0x0080
230
231/* PLA_TCR1 */
232#define VERSION_MASK 0x7cf0
233
hayeswang69b4b7a2014-07-10 10:58:54 +0800234/* PLA_MTPS */
235#define MTPS_JUMBO (12 * 1024 / 64)
236#define MTPS_DEFAULT (6 * 1024 / 64)
237
hayeswang4f1d4d52014-03-11 16:24:19 +0800238/* PLA_RSTTALLY */
239#define TALLY_RESET 0x0001
240
hayeswangac718b62013-05-02 16:01:25 +0000241/* PLA_CR */
242#define CR_RST 0x10
243#define CR_RE 0x08
244#define CR_TE 0x04
245
246/* PLA_CRWECR */
247#define CRWECR_NORAML 0x00
248#define CRWECR_CONFIG 0xc0
249
250/* PLA_OOB_CTRL */
251#define NOW_IS_OOB 0x80
252#define TXFIFO_EMPTY 0x20
253#define RXFIFO_EMPTY 0x10
254#define LINK_LIST_READY 0x02
255#define DIS_MCU_CLROOB 0x01
256#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
257
258/* PLA_MISC_1 */
259#define RXDY_GATED_EN 0x0008
260
261/* PLA_SFF_STS_7 */
262#define RE_INIT_LL 0x8000
263#define MCU_BORW_EN 0x4000
264
265/* PLA_CPCR */
266#define CPCR_RX_VLAN 0x0040
267
268/* PLA_CFG_WOL */
269#define MAGIC_EN 0x0001
270
hayeswang43779f82014-01-02 11:25:10 +0800271/* PLA_TEREDO_CFG */
272#define TEREDO_SEL 0x8000
273#define TEREDO_WAKE_MASK 0x7f00
274#define TEREDO_RS_EVENT_MASK 0x00fe
275#define OOB_TEREDO_EN 0x0001
276
hayeswangac718b62013-05-02 16:01:25 +0000277/* PAL_BDC_CR */
278#define ALDPS_PROXY_MODE 0x0001
279
hayeswang65b82d62017-06-15 14:44:03 +0800280/* PLA_EFUSE_CMD */
281#define EFUSE_READ_CMD BIT(15)
282#define EFUSE_DATA_BIT16 BIT(7)
283
hayeswang21ff2e82014-02-18 21:49:06 +0800284/* PLA_CONFIG34 */
285#define LINK_ON_WAKE_EN 0x0010
286#define LINK_OFF_WAKE_EN 0x0008
287
hayeswangac718b62013-05-02 16:01:25 +0000288/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800289#define BWF_EN 0x0040
290#define MWF_EN 0x0020
291#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000292#define LAN_WAKE_EN 0x0002
293
294/* PLA_LED_FEATURE */
295#define LED_MODE_MASK 0x0700
296
297/* PLA_PHY_PWR */
298#define TX_10M_IDLE_EN 0x0080
299#define PFM_PWM_SWITCH 0x0040
300
301/* PLA_MAC_PWR_CTRL */
302#define D3_CLK_GATED_EN 0x00004000
303#define MCU_CLK_RATIO 0x07010f07
304#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800305#define ALDPS_SPDWN_RATIO 0x0f87
306
307/* PLA_MAC_PWR_CTRL2 */
308#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800309#define MAC_CLK_SPDWN_EN BIT(15)
hayeswang43779f82014-01-02 11:25:10 +0800310
311/* PLA_MAC_PWR_CTRL3 */
312#define PKT_AVAIL_SPDWN_EN 0x0100
313#define SUSPEND_SPDWN_EN 0x0004
314#define U1U2_SPDWN_EN 0x0002
315#define L1_SPDWN_EN 0x0001
316
317/* PLA_MAC_PWR_CTRL4 */
318#define PWRSAVE_SPDWN_EN 0x1000
319#define RXDV_SPDWN_EN 0x0800
320#define TX10MIDLE_EN 0x0100
321#define TP100_SPDWN_EN 0x0020
322#define TP500_SPDWN_EN 0x0010
323#define TP1000_SPDWN_EN 0x0008
324#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000325
326/* PLA_GPHY_INTR_IMR */
327#define GPHY_STS_MSK 0x0001
328#define SPEED_DOWN_MSK 0x0002
329#define SPDWN_RXDV_MSK 0x0004
330#define SPDWN_LINKCHG_MSK 0x0008
331
332/* PLA_PHYAR */
333#define PHYAR_FLAG 0x80000000
334
335/* PLA_EEE_CR */
336#define EEE_RX_EN 0x0001
337#define EEE_TX_EN 0x0002
338
hayeswang43779f82014-01-02 11:25:10 +0800339/* PLA_BOOT_CTRL */
340#define AUTOLOAD_DONE 0x0002
341
Hayes Wang13e04fbf2019-07-01 15:53:19 +0800342/* PLA_SUSPEND_FLAG */
343#define LINK_CHG_EVENT BIT(0)
344
345/* PLA_INDICATE_FALG */
346#define UPCOMING_RUNTIME_D3 BIT(0)
347
348/* PLA_EXTRA_STATUS */
349#define LINK_CHANGE_FLAG BIT(8)
350
hayeswang65bab842015-02-12 16:20:46 +0800351/* USB_USB2PHY */
352#define USB2PHY_SUSPEND 0x0001
353#define USB2PHY_L1 0x0002
354
355/* USB_SSPHYLINK2 */
356#define pwd_dn_scale_mask 0x3ffe
357#define pwd_dn_scale(x) ((x) << 1)
358
359/* USB_CSR_DUMMY1 */
360#define DYNAMIC_BURST 0x0001
361
362/* USB_CSR_DUMMY2 */
363#define EP4_FULL_FC 0x0001
364
hayeswangac718b62013-05-02 16:01:25 +0000365/* USB_DEV_STAT */
366#define STAT_SPEED_MASK 0x0006
367#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800368#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000369
hayeswang65b82d62017-06-15 14:44:03 +0800370/* USB_LPM_CONFIG */
371#define LPM_U1U2_EN BIT(0)
372
hayeswangac718b62013-05-02 16:01:25 +0000373/* USB_TX_AGG */
374#define TX_AGG_MAX_THRESHOLD 0x03
375
376/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800377#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800378#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800379#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800380#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000381
382/* USB_TX_DMA */
383#define TEST_MODE_DISABLE 0x00000001
384#define TX_SIZE_ADJUST1 0x00000100
385
hayeswang93fe9b12016-06-16 10:55:18 +0800386/* USB_BMU_RESET */
387#define BMU_RESET_EP_IN 0x01
388#define BMU_RESET_EP_OUT 0x02
389
hayeswang65b82d62017-06-15 14:44:03 +0800390/* USB_UPT_RXDMA_OWN */
391#define OWN_UPDATE BIT(0)
392#define OWN_CLEAR BIT(1)
393
hayeswangac718b62013-05-02 16:01:25 +0000394/* USB_UPS_CTRL */
395#define POWER_CUT 0x0100
396
397/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800398#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000399
400/* USB_USB_CTRL */
401#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800402#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000403
hayeswang43779f82014-01-02 11:25:10 +0800404/* USB_U2P3_CTRL */
405#define U2P3_ENABLE 0x0001
406
407/* USB_POWER_CUT */
408#define PWR_EN 0x0001
409#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800410#define UPS_EN BIT(4)
411#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800412
413/* USB_MISC_0 */
414#define PCUT_STATUS 0x0001
415
hayeswang464ec102015-02-12 14:33:46 +0800416/* USB_RX_EARLY_TIMEOUT */
417#define COALESCE_SUPER 85000U
418#define COALESCE_HIGH 250000U
419#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800420
421/* USB_WDT11_CTRL */
422#define TIMER11_EN 0x0001
423
424/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800425/* bit 4 ~ 5: fifo empty boundary */
426#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
427/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800428#define LPM_TIMER_MASK 0x0c
429#define LPM_TIMER_500MS 0x04 /* 500 ms */
430#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800431#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800432
433/* USB_AFE_CTRL2 */
434#define SEN_VAL_MASK 0xf800
435#define SEN_VAL_NORMAL 0xa000
436#define SEL_RXIDLE 0x0100
437
hayeswang65b82d62017-06-15 14:44:03 +0800438/* USB_UPS_CFG */
439#define SAW_CNT_1MS_MASK 0x0fff
440
441/* USB_UPS_FLAGS */
442#define UPS_FLAGS_R_TUNE BIT(0)
443#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
444#define UPS_FLAGS_250M_CKDIV BIT(2)
445#define UPS_FLAGS_EN_ALDPS BIT(3)
446#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
447#define UPS_FLAGS_SPEED_MASK (0xf << 16)
448#define ups_flags_speed(x) ((x) << 16)
449#define UPS_FLAGS_EN_EEE BIT(20)
450#define UPS_FLAGS_EN_500M_EEE BIT(21)
451#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
452#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
453#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
454#define UPS_FLAGS_EN_GREEN BIT(26)
455#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
456
457enum spd_duplex {
458 NWAY_10M_HALF = 1,
459 NWAY_10M_FULL,
460 NWAY_100M_HALF,
461 NWAY_100M_FULL,
462 NWAY_1000M_FULL,
463 FORCE_10M_HALF,
464 FORCE_10M_FULL,
465 FORCE_100M_HALF,
466 FORCE_100M_FULL,
467};
468
hayeswangac718b62013-05-02 16:01:25 +0000469/* OCP_ALDPS_CONFIG */
470#define ENPWRSAVE 0x8000
471#define ENPDNPS 0x0200
472#define LINKENA 0x0100
473#define DIS_SDSAVE 0x0010
474
hayeswang43779f82014-01-02 11:25:10 +0800475/* OCP_PHY_STATUS */
476#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800477#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800478#define PHY_STAT_LAN_ON 3
479#define PHY_STAT_PWRDN 5
480
hayeswang65b82d62017-06-15 14:44:03 +0800481/* OCP_NCTL_CFG */
482#define PGA_RETURN_EN BIT(1)
483
hayeswang43779f82014-01-02 11:25:10 +0800484/* OCP_POWER_CFG */
485#define EEE_CLKDIV_EN 0x8000
486#define EN_ALDPS 0x0004
487#define EN_10M_PLLOFF 0x0001
488
hayeswangac718b62013-05-02 16:01:25 +0000489/* OCP_EEE_CONFIG1 */
490#define RG_TXLPI_MSK_HFDUP 0x8000
491#define RG_MATCLR_EN 0x4000
492#define EEE_10_CAP 0x2000
493#define EEE_NWAY_EN 0x1000
494#define TX_QUIET_EN 0x0200
495#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800496#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800497#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000498#define RG_RXLPI_MSK_HFDUP 0x0008
499#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
500
501/* OCP_EEE_CONFIG2 */
502#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
503#define RG_DACQUIET_EN 0x0400
504#define RG_LDVQUIET_EN 0x0200
505#define RG_CKRSEL 0x0020
506#define RG_EEEPRG_EN 0x0010
507
508/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800509#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800510#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000511#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
512#define MSK_PH 0x0006 /* bit 0 ~ 3 */
513
514/* OCP_EEE_AR */
515/* bit[15:14] function */
516#define FUN_ADDR 0x0000
517#define FUN_DATA 0x4000
518/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000519
hayeswang43779f82014-01-02 11:25:10 +0800520/* OCP_EEE_CFG */
521#define CTAP_SHORT_EN 0x0040
522#define EEE10_EN 0x0010
523
524/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800525#define EN_EEE_CMODE BIT(14)
526#define EN_EEE_1000 BIT(13)
527#define EN_EEE_100 BIT(12)
528#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800529#define EN_10M_BGOFF 0x0080
530
hayeswang2dd49e02015-09-07 11:57:44 +0800531/* OCP_PHY_STATE */
532#define TXDIS_STATE 0x01
533#define ABD_STATE 0x02
534
hayeswang65b82d62017-06-15 14:44:03 +0800535/* OCP_PHY_PATCH_STAT */
536#define PATCH_READY BIT(6)
537
538/* OCP_PHY_PATCH_CMD */
539#define PATCH_REQUEST BIT(4)
540
hayeswang43779f82014-01-02 11:25:10 +0800541/* OCP_ADC_CFG */
542#define CKADSEL_L 0x0100
543#define ADC_EN 0x0080
544#define EN_EMI_L 0x0040
545
hayeswang65b82d62017-06-15 14:44:03 +0800546/* OCP_SYSCLK_CFG */
547#define clk_div_expo(x) (min(x, 5) << 8)
548
549/* SRAM_GREEN_CFG */
550#define GREEN_ETH_EN BIT(15)
551#define R_TUNE_EN BIT(11)
552
hayeswang43779f82014-01-02 11:25:10 +0800553/* SRAM_LPF_CFG */
554#define LPF_AUTO_TUNE 0x8000
555
556/* SRAM_10M_AMP1 */
557#define GDAC_IB_UPALL 0x0008
558
559/* SRAM_10M_AMP2 */
560#define AMP_DN 0x0200
561
562/* SRAM_IMPEDANCE */
563#define RX_DRIVING_MASK 0x6000
564
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500565/* MAC PASSTHRU */
566#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600567#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800568#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500569#define EFUSE 0xcfdb
570#define PASS_THRU_MASK 0x1
571
hayeswangac718b62013-05-02 16:01:25 +0000572enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800573 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000574 _100bps = 0x08,
575 _10bps = 0x04,
576 LINK_STATUS = 0x02,
577 FULL_DUP = 0x01,
578};
579
hayeswang1764bcd2014-08-28 10:24:18 +0800580#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800581#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800582#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800583#define TX_ALIGN 4
584#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800585
586#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800587
hayeswangac718b62013-05-02 16:01:25 +0000588#define RTL8152_REQT_READ 0xc0
589#define RTL8152_REQT_WRITE 0x40
590#define RTL8152_REQ_GET_REGS 0x05
591#define RTL8152_REQ_SET_REGS 0x05
592
593#define BYTE_EN_DWORD 0xff
594#define BYTE_EN_WORD 0x33
595#define BYTE_EN_BYTE 0x11
596#define BYTE_EN_SIX_BYTES 0x3f
597#define BYTE_EN_START_MASK 0x0f
598#define BYTE_EN_END_MASK 0xf0
599
hayeswang69b4b7a2014-07-10 10:58:54 +0800600#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800601#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
602 ETH_FCS_LEN)
603#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800604#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800605#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800606#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800607#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800608 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000609
610/* rtl8152 flags */
611enum rtl8152_flags {
612 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000613 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800614 WORK_ENABLE,
615 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800616 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800617 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800618 SCHEDULE_NAPI,
hayeswang65b82d62017-06-15 14:44:03 +0800619 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800620 DELL_TB_RX_AGG_BUG,
hayeswangac718b62013-05-02 16:01:25 +0000621};
622
623/* Define these values to match your device */
624#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200625#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800626#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200627#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700628#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700629#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800630#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000631
632#define MCU_TYPE_PLA 0x0100
633#define MCU_TYPE_USB 0x0000
634
hayeswang4f1d4d52014-03-11 16:24:19 +0800635struct tally_counter {
636 __le64 tx_packets;
637 __le64 rx_packets;
638 __le64 tx_errors;
639 __le32 rx_errors;
640 __le16 rx_missed;
641 __le16 align_errors;
642 __le32 tx_one_collision;
643 __le32 tx_multi_collision;
644 __le64 rx_unicast;
645 __le64 rx_broadcast;
646 __le32 rx_multicast;
647 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800648 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800649};
650
hayeswangac718b62013-05-02 16:01:25 +0000651struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800652 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000653#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800654
hayeswang500b6d72013-11-20 17:30:57 +0800655 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800656#define RD_UDP_CS BIT(23)
657#define RD_TCP_CS BIT(22)
658#define RD_IPV6_CS BIT(20)
659#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800660
hayeswang500b6d72013-11-20 17:30:57 +0800661 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800662#define IPF BIT(23) /* IP checksum fail */
663#define UDPF BIT(22) /* UDP checksum fail */
664#define TCPF BIT(21) /* TCP checksum fail */
665#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800666
hayeswang500b6d72013-11-20 17:30:57 +0800667 __le32 opts4;
668 __le32 opts5;
669 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000670};
671
672struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800673 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800674#define TX_FS BIT(31) /* First segment of a packet */
675#define TX_LS BIT(30) /* Final segment of a packet */
676#define GTSENDV4 BIT(28)
677#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800678#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800679#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800680#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800681
hayeswang500b6d72013-11-20 17:30:57 +0800682 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800683#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
684#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
685#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
686#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800687#define MSS_SHIFT 17
688#define MSS_MAX 0x7ffU
689#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800690#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800691#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000692};
693
hayeswangdff4e8a2013-08-16 16:09:33 +0800694struct r8152;
695
hayeswangebc2ec482013-08-14 20:54:38 +0800696struct rx_agg {
697 struct list_head list;
698 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800699 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800700 void *buffer;
701 void *head;
702};
703
704struct tx_agg {
705 struct list_head list;
706 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800707 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800708 void *buffer;
709 void *head;
710 u32 skb_num;
711 u32 skb_len;
712};
713
hayeswangac718b62013-05-02 16:01:25 +0000714struct r8152 {
715 unsigned long flags;
716 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800717 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800718 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000719 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800720 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800721 struct tx_agg tx_info[RTL8152_MAX_TX];
722 struct rx_agg rx_info[RTL8152_MAX_RX];
723 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800724 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800725 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800726 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000727 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800728 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800729#ifdef CONFIG_PM_SLEEP
730 struct notifier_block pm_notifier;
731#endif
hayeswangc81229c2014-01-02 11:22:42 +0800732
733 struct rtl_ops {
734 void (*init)(struct r8152 *);
735 int (*enable)(struct r8152 *);
736 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800737 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800738 void (*down)(struct r8152 *);
739 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800740 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
741 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800742 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800743 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800744 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800745 } rtl_ops;
746
hayeswang40a82912013-08-14 20:54:40 +0800747 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800748 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000749 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800750 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800751 u32 coalesce;
hayeswangac718b62013-05-02 16:01:25 +0000752 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800753 u16 speed;
hayeswang40a82912013-08-14 20:54:40 +0800754 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000755 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800756 u8 duplex;
757 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000758};
759
760enum rtl_version {
761 RTL_VER_UNKNOWN = 0,
762 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800763 RTL_VER_02,
764 RTL_VER_03,
765 RTL_VER_04,
766 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800767 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800768 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800769 RTL_VER_08,
770 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800771 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000772};
773
hayeswang60c89072014-03-07 11:04:39 +0800774enum tx_csum_stat {
775 TX_CSUM_SUCCESS = 0,
776 TX_CSUM_TSO,
777 TX_CSUM_NONE
778};
779
hayeswangac718b62013-05-02 16:01:25 +0000780/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
781 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
782 */
783static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800784static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000785
hayeswang52aec122014-09-02 10:27:52 +0800786#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800787 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800788
hayeswangac718b62013-05-02 16:01:25 +0000789static
790int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
791{
hayeswang31787f52013-07-31 17:21:25 +0800792 int ret;
793 void *tmp;
794
795 tmp = kmalloc(size, GFP_KERNEL);
796 if (!tmp)
797 return -ENOMEM;
798
799 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800800 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
801 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800802
803 memcpy(data, tmp, size);
804 kfree(tmp);
805
806 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000807}
808
809static
810int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
811{
hayeswang31787f52013-07-31 17:21:25 +0800812 int ret;
813 void *tmp;
814
Benoit Tainec4438f02014-05-26 17:21:23 +0200815 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800816 if (!tmp)
817 return -ENOMEM;
818
hayeswang31787f52013-07-31 17:21:25 +0800819 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800820 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
821 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800822
823 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800824
hayeswang31787f52013-07-31 17:21:25 +0800825 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000826}
827
828static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800829 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000830{
hayeswang45f4a192014-01-06 17:08:41 +0800831 u16 limit = 64;
832 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000833
834 if (test_bit(RTL8152_UNPLUG, &tp->flags))
835 return -ENODEV;
836
837 /* both size and indix must be 4 bytes align */
838 if ((size & 3) || !size || (index & 3) || !data)
839 return -EPERM;
840
841 if ((u32)index + (u32)size > 0xffff)
842 return -EPERM;
843
844 while (size) {
845 if (size > limit) {
846 ret = get_registers(tp, index, type, limit, data);
847 if (ret < 0)
848 break;
849
850 index += limit;
851 data += limit;
852 size -= limit;
853 } else {
854 ret = get_registers(tp, index, type, size, data);
855 if (ret < 0)
856 break;
857
858 index += size;
859 data += size;
860 size = 0;
861 break;
862 }
863 }
864
hayeswang67610492014-10-30 11:46:40 +0800865 if (ret == -ENODEV)
866 set_bit(RTL8152_UNPLUG, &tp->flags);
867
hayeswangac718b62013-05-02 16:01:25 +0000868 return ret;
869}
870
871static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800872 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000873{
hayeswang45f4a192014-01-06 17:08:41 +0800874 int ret;
875 u16 byteen_start, byteen_end, byen;
876 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000877
878 if (test_bit(RTL8152_UNPLUG, &tp->flags))
879 return -ENODEV;
880
881 /* both size and indix must be 4 bytes align */
882 if ((size & 3) || !size || (index & 3) || !data)
883 return -EPERM;
884
885 if ((u32)index + (u32)size > 0xffff)
886 return -EPERM;
887
888 byteen_start = byteen & BYTE_EN_START_MASK;
889 byteen_end = byteen & BYTE_EN_END_MASK;
890
891 byen = byteen_start | (byteen_start << 4);
892 ret = set_registers(tp, index, type | byen, 4, data);
893 if (ret < 0)
894 goto error1;
895
896 index += 4;
897 data += 4;
898 size -= 4;
899
900 if (size) {
901 size -= 4;
902
903 while (size) {
904 if (size > limit) {
905 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800906 type | BYTE_EN_DWORD,
907 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000908 if (ret < 0)
909 goto error1;
910
911 index += limit;
912 data += limit;
913 size -= limit;
914 } else {
915 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800916 type | BYTE_EN_DWORD,
917 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000918 if (ret < 0)
919 goto error1;
920
921 index += size;
922 data += size;
923 size = 0;
924 break;
925 }
926 }
927
928 byen = byteen_end | (byteen_end >> 4);
929 ret = set_registers(tp, index, type | byen, 4, data);
930 if (ret < 0)
931 goto error1;
932 }
933
934error1:
hayeswang67610492014-10-30 11:46:40 +0800935 if (ret == -ENODEV)
936 set_bit(RTL8152_UNPLUG, &tp->flags);
937
hayeswangac718b62013-05-02 16:01:25 +0000938 return ret;
939}
940
941static inline
942int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
943{
944 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
945}
946
947static inline
948int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
949{
950 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
951}
952
953static inline
hayeswangac718b62013-05-02 16:01:25 +0000954int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
955{
956 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
957}
958
959static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
960{
hayeswangc8826de2013-07-31 17:21:26 +0800961 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000962
hayeswangc8826de2013-07-31 17:21:26 +0800963 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000964
965 return __le32_to_cpu(data);
966}
967
968static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
969{
hayeswangc8826de2013-07-31 17:21:26 +0800970 __le32 tmp = __cpu_to_le32(data);
971
972 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000973}
974
975static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
976{
977 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800978 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +0800979 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +0000980 u8 shift = index & 2;
981
982 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +0800983 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +0000984
hayeswangd8fbd272017-06-15 14:44:04 +0800985 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +0000986
hayeswangc8826de2013-07-31 17:21:26 +0800987 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000988 data >>= (shift * 8);
989 data &= 0xffff;
990
991 return (u16)data;
992}
993
994static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
995{
hayeswangc8826de2013-07-31 17:21:26 +0800996 u32 mask = 0xffff;
997 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000998 u16 byen = BYTE_EN_WORD;
999 u8 shift = index & 2;
1000
1001 data &= mask;
1002
1003 if (index & 2) {
1004 byen <<= shift;
1005 mask <<= (shift * 8);
1006 data <<= (shift * 8);
1007 index &= ~3;
1008 }
1009
hayeswangc8826de2013-07-31 17:21:26 +08001010 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001011
hayeswangc8826de2013-07-31 17:21:26 +08001012 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001013}
1014
1015static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1016{
1017 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001018 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001019 u8 shift = index & 3;
1020
1021 index &= ~3;
1022
hayeswangc8826de2013-07-31 17:21:26 +08001023 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001024
hayeswangc8826de2013-07-31 17:21:26 +08001025 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001026 data >>= (shift * 8);
1027 data &= 0xff;
1028
1029 return (u8)data;
1030}
1031
1032static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1033{
hayeswangc8826de2013-07-31 17:21:26 +08001034 u32 mask = 0xff;
1035 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001036 u16 byen = BYTE_EN_BYTE;
1037 u8 shift = index & 3;
1038
1039 data &= mask;
1040
1041 if (index & 3) {
1042 byen <<= shift;
1043 mask <<= (shift * 8);
1044 data <<= (shift * 8);
1045 index &= ~3;
1046 }
1047
hayeswangc8826de2013-07-31 17:21:26 +08001048 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001049
hayeswangc8826de2013-07-31 17:21:26 +08001050 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001051}
1052
hayeswangac244d32014-01-02 11:22:40 +08001053static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1054{
1055 u16 ocp_base, ocp_index;
1056
1057 ocp_base = addr & 0xf000;
1058 if (ocp_base != tp->ocp_base) {
1059 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1060 tp->ocp_base = ocp_base;
1061 }
1062
1063 ocp_index = (addr & 0x0fff) | 0xb000;
1064 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1065}
1066
hayeswange3fe0b12014-01-02 11:22:39 +08001067static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1068{
1069 u16 ocp_base, ocp_index;
1070
1071 ocp_base = addr & 0xf000;
1072 if (ocp_base != tp->ocp_base) {
1073 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1074 tp->ocp_base = ocp_base;
1075 }
1076
1077 ocp_index = (addr & 0x0fff) | 0xb000;
1078 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1079}
1080
hayeswangac244d32014-01-02 11:22:40 +08001081static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001082{
hayeswangac244d32014-01-02 11:22:40 +08001083 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001084}
1085
hayeswangac244d32014-01-02 11:22:40 +08001086static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001087{
hayeswangac244d32014-01-02 11:22:40 +08001088 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001089}
1090
hayeswang43779f82014-01-02 11:25:10 +08001091static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1092{
1093 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1094 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1095}
1096
hayeswang65b82d62017-06-15 14:44:03 +08001097static u16 sram_read(struct r8152 *tp, u16 addr)
1098{
1099 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1100 return ocp_reg_read(tp, OCP_SRAM_DATA);
1101}
1102
hayeswangac718b62013-05-02 16:01:25 +00001103static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1104{
1105 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001106 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001107
hayeswang68714382014-04-11 17:54:31 +08001108 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1109 return -ENODEV;
1110
hayeswangac718b62013-05-02 16:01:25 +00001111 if (phy_id != R8152_PHY_ID)
1112 return -EINVAL;
1113
hayeswang9a4be1b2014-02-18 21:49:07 +08001114 ret = r8152_mdio_read(tp, reg);
1115
hayeswang9a4be1b2014-02-18 21:49:07 +08001116 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001117}
1118
1119static
1120void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1121{
1122 struct r8152 *tp = netdev_priv(netdev);
1123
hayeswang68714382014-04-11 17:54:31 +08001124 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1125 return;
1126
hayeswangac718b62013-05-02 16:01:25 +00001127 if (phy_id != R8152_PHY_ID)
1128 return;
1129
1130 r8152_mdio_write(tp, reg, val);
1131}
1132
hayeswangb209af92014-08-25 15:53:00 +08001133static int
1134r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001135
hayeswang8ba789a2014-09-04 16:15:41 +08001136static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1137{
1138 struct r8152 *tp = netdev_priv(netdev);
1139 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001140 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001141
1142 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001143 goto out1;
1144
1145 ret = usb_autopm_get_interface(tp->intf);
1146 if (ret < 0)
1147 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001148
hayeswangb5403272014-10-09 18:00:26 +08001149 mutex_lock(&tp->control);
1150
hayeswang8ba789a2014-09-04 16:15:41 +08001151 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1152
1153 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1154 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1155 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1156
hayeswangb5403272014-10-09 18:00:26 +08001157 mutex_unlock(&tp->control);
1158
hayeswangea6a7112014-10-02 17:03:12 +08001159 usb_autopm_put_interface(tp->intf);
1160out1:
1161 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001162}
1163
Mario Limonciello9c273692018-12-11 08:16:14 -06001164/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001165 * host system provided MAC address.
1166 * Examples of this are Dell TB15 and Dell WD15 docks
1167 */
1168static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1169{
1170 acpi_status status;
1171 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1172 union acpi_object *obj;
1173 int ret = -EINVAL;
1174 u32 ocp_data;
1175 unsigned char buf[6];
1176
1177 /* test for -AD variant of RTL8153 */
1178 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001179 if ((ocp_data & AD_MASK) == 0x1000) {
1180 /* test for MAC address pass-through bit */
1181 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1182 if ((ocp_data & PASS_THRU_MASK) != 1) {
1183 netif_dbg(tp, probe, tp->netdev,
1184 "No efuse for RTL8153-AD MAC pass through\n");
1185 return -ENODEV;
1186 }
1187 } else {
David Chen8e29d232019-02-16 17:16:42 +08001188 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001189 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001190 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001191 netif_dbg(tp, probe, tp->netdev,
1192 "Invalid variant for MAC pass through\n");
1193 return -ENODEV;
1194 }
1195 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001196
1197 /* returns _AUXMAC_#AABBCCDDEEFF# */
1198 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1199 obj = (union acpi_object *)buffer.pointer;
1200 if (!ACPI_SUCCESS(status))
1201 return -ENODEV;
1202 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1203 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001204 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001205 obj->type, obj->string.length);
1206 goto amacout;
1207 }
1208 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1209 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1210 netif_warn(tp, probe, tp->netdev,
1211 "Invalid header when reading pass-thru MAC addr\n");
1212 goto amacout;
1213 }
1214 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1215 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1216 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001217 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1218 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001219 ret = -EINVAL;
1220 goto amacout;
1221 }
1222 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001223 netif_info(tp, probe, tp->netdev,
1224 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1225
1226amacout:
1227 kfree(obj);
1228 return ret;
1229}
1230
Mario Limonciello25766272019-04-04 13:46:53 -05001231static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1232{
1233 struct net_device *dev = tp->netdev;
1234 int ret;
1235
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001236 sa->sa_family = dev->type;
1237
Mario Limonciello25766272019-04-04 13:46:53 -05001238 if (tp->version == RTL_VER_01) {
1239 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1240 } else {
1241 /* if device doesn't support MAC pass through this will
1242 * be expected to be non-zero
1243 */
1244 ret = vendor_mac_passthru_addr_read(tp, sa);
1245 if (ret < 0)
1246 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1247 }
1248
1249 if (ret < 0) {
1250 netif_err(tp, probe, dev, "Get ether addr fail\n");
1251 } else if (!is_valid_ether_addr(sa->sa_data)) {
1252 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1253 sa->sa_data);
1254 eth_hw_addr_random(dev);
1255 ether_addr_copy(sa->sa_data, dev->dev_addr);
1256 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1257 sa->sa_data);
1258 return 0;
1259 }
1260
1261 return ret;
1262}
1263
hayeswang179bb6d2014-09-04 16:15:42 +08001264static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001265{
1266 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001267 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001268 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001269
Mario Limonciello25766272019-04-04 13:46:53 -05001270 ret = determine_ethernet_addr(tp, &sa);
1271 if (ret < 0)
1272 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001273
Mario Limonciello25766272019-04-04 13:46:53 -05001274 if (tp->version == RTL_VER_01)
1275 ether_addr_copy(dev->dev_addr, sa.sa_data);
1276 else
hayeswang179bb6d2014-09-04 16:15:42 +08001277 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001278
1279 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001280}
1281
hayeswangac718b62013-05-02 16:01:25 +00001282static void read_bulk_callback(struct urb *urb)
1283{
hayeswangac718b62013-05-02 16:01:25 +00001284 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001285 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001286 struct rx_agg *agg;
1287 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001288 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001289
hayeswangebc2ec482013-08-14 20:54:38 +08001290 agg = urb->context;
1291 if (!agg)
1292 return;
1293
1294 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001295 if (!tp)
1296 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001297
hayeswangac718b62013-05-02 16:01:25 +00001298 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1299 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001300
1301 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001302 return;
1303
hayeswangebc2ec482013-08-14 20:54:38 +08001304 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001305
1306 /* When link down, the driver would cancel all bulks. */
1307 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001308 if (!netif_carrier_ok(netdev))
1309 return;
1310
hayeswang9a4be1b2014-02-18 21:49:07 +08001311 usb_mark_last_busy(tp->udev);
1312
hayeswangac718b62013-05-02 16:01:25 +00001313 switch (status) {
1314 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001315 if (urb->actual_length < ETH_ZLEN)
1316 break;
1317
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001318 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001319 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001320 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001321 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001322 return;
hayeswangac718b62013-05-02 16:01:25 +00001323 case -ESHUTDOWN:
1324 set_bit(RTL8152_UNPLUG, &tp->flags);
1325 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001326 return;
hayeswangac718b62013-05-02 16:01:25 +00001327 case -ENOENT:
1328 return; /* the urb is in unlink state */
1329 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001330 if (net_ratelimit())
1331 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001332 break;
hayeswangac718b62013-05-02 16:01:25 +00001333 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001334 if (net_ratelimit())
1335 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001336 break;
hayeswangac718b62013-05-02 16:01:25 +00001337 }
1338
hayeswanga0fccd42014-11-20 10:29:05 +08001339 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001340}
1341
1342static void write_bulk_callback(struct urb *urb)
1343{
hayeswangebc2ec482013-08-14 20:54:38 +08001344 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001345 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001346 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001347 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001348 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001349 int status = urb->status;
1350
hayeswangebc2ec482013-08-14 20:54:38 +08001351 agg = urb->context;
1352 if (!agg)
1353 return;
1354
1355 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001356 if (!tp)
1357 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001358
hayeswangd104eaf2014-03-06 15:07:17 +08001359 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001360 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001361 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001362 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001363 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001364 stats->tx_errors += agg->skb_num;
1365 } else {
1366 stats->tx_packets += agg->skb_num;
1367 stats->tx_bytes += agg->skb_len;
1368 }
1369
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001370 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001371 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001372 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001373
hayeswang9a4be1b2014-02-18 21:49:07 +08001374 usb_autopm_put_interface_async(tp->intf);
1375
hayeswangd104eaf2014-03-06 15:07:17 +08001376 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001377 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001378
1379 if (!test_bit(WORK_ENABLE, &tp->flags))
1380 return;
1381
1382 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1383 return;
1384
1385 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001386 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001387}
1388
hayeswang40a82912013-08-14 20:54:40 +08001389static void intr_callback(struct urb *urb)
1390{
1391 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001392 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001393 int status = urb->status;
1394 int res;
1395
1396 tp = urb->context;
1397 if (!tp)
1398 return;
1399
1400 if (!test_bit(WORK_ENABLE, &tp->flags))
1401 return;
1402
1403 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1404 return;
1405
1406 switch (status) {
1407 case 0: /* success */
1408 break;
1409 case -ECONNRESET: /* unlink */
1410 case -ESHUTDOWN:
1411 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001412 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001413 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001414 case -EPROTO:
1415 netif_info(tp, intr, tp->netdev,
1416 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001417 return;
1418 case -EOVERFLOW:
1419 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1420 goto resubmit;
1421 /* -EPIPE: should clear the halt */
1422 default:
1423 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1424 goto resubmit;
1425 }
1426
1427 d = urb->transfer_buffer;
1428 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001429 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001430 set_bit(RTL8152_LINK_CHG, &tp->flags);
1431 schedule_delayed_work(&tp->schedule, 0);
1432 }
1433 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001434 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001435 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001436 set_bit(RTL8152_LINK_CHG, &tp->flags);
1437 schedule_delayed_work(&tp->schedule, 0);
1438 }
1439 }
1440
1441resubmit:
1442 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001443 if (res == -ENODEV) {
1444 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001445 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001446 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001447 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001448 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001449 }
hayeswang40a82912013-08-14 20:54:40 +08001450}
1451
hayeswangebc2ec482013-08-14 20:54:38 +08001452static inline void *rx_agg_align(void *data)
1453{
hayeswang8e1f51b2014-01-02 11:22:41 +08001454 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001455}
1456
1457static inline void *tx_agg_align(void *data)
1458{
hayeswang8e1f51b2014-01-02 11:22:41 +08001459 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001460}
1461
1462static void free_all_mem(struct r8152 *tp)
1463{
1464 int i;
1465
1466 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001467 usb_free_urb(tp->rx_info[i].urb);
1468 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001469
hayeswang9629e3c2014-01-15 10:42:15 +08001470 kfree(tp->rx_info[i].buffer);
1471 tp->rx_info[i].buffer = NULL;
1472 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001473 }
1474
1475 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001476 usb_free_urb(tp->tx_info[i].urb);
1477 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001478
hayeswang9629e3c2014-01-15 10:42:15 +08001479 kfree(tp->tx_info[i].buffer);
1480 tp->tx_info[i].buffer = NULL;
1481 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001482 }
hayeswang40a82912013-08-14 20:54:40 +08001483
hayeswang9629e3c2014-01-15 10:42:15 +08001484 usb_free_urb(tp->intr_urb);
1485 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001486
hayeswang9629e3c2014-01-15 10:42:15 +08001487 kfree(tp->intr_buff);
1488 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001489}
1490
1491static int alloc_all_mem(struct r8152 *tp)
1492{
1493 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001494 struct usb_interface *intf = tp->intf;
1495 struct usb_host_interface *alt = intf->cur_altsetting;
1496 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001497 struct urb *urb;
1498 int node, i;
1499 u8 *buf;
1500
1501 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1502
1503 spin_lock_init(&tp->rx_lock);
1504 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001505 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001506 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001507 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001508 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec482013-08-14 20:54:38 +08001509
1510 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001511 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001512 if (!buf)
1513 goto err1;
1514
1515 if (buf != rx_agg_align(buf)) {
1516 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001517 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001518 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001519 if (!buf)
1520 goto err1;
1521 }
1522
1523 urb = usb_alloc_urb(0, GFP_KERNEL);
1524 if (!urb) {
1525 kfree(buf);
1526 goto err1;
1527 }
1528
1529 INIT_LIST_HEAD(&tp->rx_info[i].list);
1530 tp->rx_info[i].context = tp;
1531 tp->rx_info[i].urb = urb;
1532 tp->rx_info[i].buffer = buf;
1533 tp->rx_info[i].head = rx_agg_align(buf);
1534 }
1535
1536 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001537 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001538 if (!buf)
1539 goto err1;
1540
1541 if (buf != tx_agg_align(buf)) {
1542 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001543 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001544 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001545 if (!buf)
1546 goto err1;
1547 }
1548
1549 urb = usb_alloc_urb(0, GFP_KERNEL);
1550 if (!urb) {
1551 kfree(buf);
1552 goto err1;
1553 }
1554
1555 INIT_LIST_HEAD(&tp->tx_info[i].list);
1556 tp->tx_info[i].context = tp;
1557 tp->tx_info[i].urb = urb;
1558 tp->tx_info[i].buffer = buf;
1559 tp->tx_info[i].head = tx_agg_align(buf);
1560
1561 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1562 }
1563
hayeswang40a82912013-08-14 20:54:40 +08001564 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1565 if (!tp->intr_urb)
1566 goto err1;
1567
1568 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1569 if (!tp->intr_buff)
1570 goto err1;
1571
1572 tp->intr_interval = (int)ep_intr->desc.bInterval;
1573 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001574 tp->intr_buff, INTBUFSIZE, intr_callback,
1575 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001576
hayeswangebc2ec482013-08-14 20:54:38 +08001577 return 0;
1578
1579err1:
1580 free_all_mem(tp);
1581 return -ENOMEM;
1582}
1583
hayeswang0de98f62013-08-16 16:09:35 +08001584static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1585{
1586 struct tx_agg *agg = NULL;
1587 unsigned long flags;
1588
hayeswang21949ab2014-03-07 11:04:35 +08001589 if (list_empty(&tp->tx_free))
1590 return NULL;
1591
hayeswang0de98f62013-08-16 16:09:35 +08001592 spin_lock_irqsave(&tp->tx_lock, flags);
1593 if (!list_empty(&tp->tx_free)) {
1594 struct list_head *cursor;
1595
1596 cursor = tp->tx_free.next;
1597 list_del_init(cursor);
1598 agg = list_entry(cursor, struct tx_agg, list);
1599 }
1600 spin_unlock_irqrestore(&tp->tx_lock, flags);
1601
1602 return agg;
1603}
1604
hayeswangb209af92014-08-25 15:53:00 +08001605/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001606 * The hw limites the value the transport offset. When the offset is out of the
1607 * range, calculate the checksum by sw.
1608 */
1609static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1610 struct sk_buff_head *list)
1611{
1612 if (skb_shinfo(skb)->gso_size) {
1613 netdev_features_t features = tp->netdev->features;
1614 struct sk_buff_head seg_list;
1615 struct sk_buff *segs, *nskb;
1616
hayeswanga91d45f2014-07-11 16:48:27 +08001617 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001618 segs = skb_gso_segment(skb, features);
1619 if (IS_ERR(segs) || !segs)
1620 goto drop;
1621
1622 __skb_queue_head_init(&seg_list);
1623
1624 do {
1625 nskb = segs;
1626 segs = segs->next;
1627 nskb->next = NULL;
1628 __skb_queue_tail(&seg_list, nskb);
1629 } while (segs);
1630
1631 skb_queue_splice(&seg_list, list);
1632 dev_kfree_skb(skb);
1633 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1634 if (skb_checksum_help(skb) < 0)
1635 goto drop;
1636
1637 __skb_queue_head(list, skb);
1638 } else {
1639 struct net_device_stats *stats;
1640
1641drop:
1642 stats = &tp->netdev->stats;
1643 stats->tx_dropped++;
1644 dev_kfree_skb(skb);
1645 }
1646}
1647
hayeswangb209af92014-08-25 15:53:00 +08001648/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001649 * According to the document of microsoft, the TCP Pseudo Header excludes the
1650 * packet length for IPv6 TCP large packets.
1651 */
1652static int msdn_giant_send_check(struct sk_buff *skb)
1653{
1654 const struct ipv6hdr *ipv6h;
1655 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001656 int ret;
1657
1658 ret = skb_cow_head(skb, 0);
1659 if (ret)
1660 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001661
1662 ipv6h = ipv6_hdr(skb);
1663 th = tcp_hdr(skb);
1664
1665 th->check = 0;
1666 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1667
hayeswangfcb308d2014-03-11 10:20:32 +08001668 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001669}
1670
hayeswangc5554292014-09-12 10:43:11 +08001671static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1672{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001673 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001674 u32 opts2;
1675
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001676 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001677 desc->opts2 |= cpu_to_le32(opts2);
1678 }
1679}
1680
1681static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1682{
1683 u32 opts2 = le32_to_cpu(desc->opts2);
1684
1685 if (opts2 & RX_VLAN_TAG)
1686 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1687 swab16(opts2 & 0xffff));
1688}
1689
hayeswang60c89072014-03-07 11:04:39 +08001690static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1691 struct sk_buff *skb, u32 len, u32 transport_offset)
1692{
1693 u32 mss = skb_shinfo(skb)->gso_size;
1694 u32 opts1, opts2 = 0;
1695 int ret = TX_CSUM_SUCCESS;
1696
1697 WARN_ON_ONCE(len > TX_LEN_MAX);
1698
1699 opts1 = len | TX_FS | TX_LS;
1700
1701 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001702 if (transport_offset > GTTCPHO_MAX) {
1703 netif_warn(tp, tx_err, tp->netdev,
1704 "Invalid transport offset 0x%x for TSO\n",
1705 transport_offset);
1706 ret = TX_CSUM_TSO;
1707 goto unavailable;
1708 }
1709
hayeswang6e74d172015-02-06 11:30:50 +08001710 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001711 case htons(ETH_P_IP):
1712 opts1 |= GTSENDV4;
1713 break;
1714
hayeswang6128d1bb2014-03-07 11:04:40 +08001715 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001716 if (msdn_giant_send_check(skb)) {
1717 ret = TX_CSUM_TSO;
1718 goto unavailable;
1719 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001720 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001721 break;
1722
hayeswang60c89072014-03-07 11:04:39 +08001723 default:
1724 WARN_ON_ONCE(1);
1725 break;
1726 }
1727
1728 opts1 |= transport_offset << GTTCPHO_SHIFT;
1729 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1730 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001731 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001732
hayeswang6128d1bb2014-03-07 11:04:40 +08001733 if (transport_offset > TCPHO_MAX) {
1734 netif_warn(tp, tx_err, tp->netdev,
1735 "Invalid transport offset 0x%x\n",
1736 transport_offset);
1737 ret = TX_CSUM_NONE;
1738 goto unavailable;
1739 }
1740
hayeswang6e74d172015-02-06 11:30:50 +08001741 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001742 case htons(ETH_P_IP):
1743 opts2 |= IPV4_CS;
1744 ip_protocol = ip_hdr(skb)->protocol;
1745 break;
1746
1747 case htons(ETH_P_IPV6):
1748 opts2 |= IPV6_CS;
1749 ip_protocol = ipv6_hdr(skb)->nexthdr;
1750 break;
1751
1752 default:
1753 ip_protocol = IPPROTO_RAW;
1754 break;
1755 }
1756
hayeswang60c89072014-03-07 11:04:39 +08001757 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001758 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001759 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001760 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001761 else
hayeswang5bd23882013-08-14 20:54:39 +08001762 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001763
hayeswang60c89072014-03-07 11:04:39 +08001764 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001765 }
hayeswang60c89072014-03-07 11:04:39 +08001766
1767 desc->opts2 = cpu_to_le32(opts2);
1768 desc->opts1 = cpu_to_le32(opts1);
1769
hayeswang6128d1bb2014-03-07 11:04:40 +08001770unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001771 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001772}
1773
hayeswangb1379d92013-08-16 16:09:37 +08001774static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1775{
hayeswangd84130a2014-02-18 21:49:02 +08001776 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001777 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001778 u8 *tx_data;
1779
hayeswangd84130a2014-02-18 21:49:02 +08001780 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001781 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001782 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001783 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001784
hayeswangb1379d92013-08-16 16:09:37 +08001785 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001786 agg->skb_num = 0;
1787 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001788 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001789
hayeswang7937f9e2013-11-20 17:30:54 +08001790 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001791 struct tx_desc *tx_desc;
1792 struct sk_buff *skb;
1793 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001794 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001795
hayeswangd84130a2014-02-18 21:49:02 +08001796 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001797 if (!skb)
1798 break;
1799
hayeswang60c89072014-03-07 11:04:39 +08001800 len = skb->len + sizeof(*tx_desc);
1801
1802 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001803 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001804 break;
1805 }
1806
hayeswang7937f9e2013-11-20 17:30:54 +08001807 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001808 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001809
1810 offset = (u32)skb_transport_offset(skb);
1811
hayeswang6128d1bb2014-03-07 11:04:40 +08001812 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1813 r8152_csum_workaround(tp, skb, &skb_head);
1814 continue;
1815 }
hayeswang60c89072014-03-07 11:04:39 +08001816
hayeswangc5554292014-09-12 10:43:11 +08001817 rtl_tx_vlan_tag(tx_desc, skb);
1818
hayeswangb1379d92013-08-16 16:09:37 +08001819 tx_data += sizeof(*tx_desc);
1820
hayeswang60c89072014-03-07 11:04:39 +08001821 len = skb->len;
1822 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1823 struct net_device_stats *stats = &tp->netdev->stats;
1824
1825 stats->tx_dropped++;
1826 dev_kfree_skb_any(skb);
1827 tx_data -= sizeof(*tx_desc);
1828 continue;
1829 }
hayeswangb1379d92013-08-16 16:09:37 +08001830
hayeswang7937f9e2013-11-20 17:30:54 +08001831 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001832 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001833 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001834
1835 dev_kfree_skb_any(skb);
1836
hayeswang52aec122014-09-02 10:27:52 +08001837 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001838
1839 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1840 break;
hayeswangb1379d92013-08-16 16:09:37 +08001841 }
1842
hayeswangd84130a2014-02-18 21:49:02 +08001843 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001844 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001845 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001846 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001847 }
1848
hayeswang0c3121f2014-03-07 11:04:36 +08001849 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001850
1851 if (netif_queue_stopped(tp->netdev) &&
1852 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1853 netif_wake_queue(tp->netdev);
1854
hayeswang0c3121f2014-03-07 11:04:36 +08001855 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001856
hayeswang0c3121f2014-03-07 11:04:36 +08001857 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001858 if (ret < 0)
1859 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001860
hayeswangb1379d92013-08-16 16:09:37 +08001861 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1862 agg->head, (int)(tx_data - (u8 *)agg->head),
1863 (usb_complete_t)write_bulk_callback, agg);
1864
hayeswang0c3121f2014-03-07 11:04:36 +08001865 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001866 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001867 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001868
1869out_tx_fill:
1870 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001871}
1872
hayeswang565cab02014-03-07 11:04:38 +08001873static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1874{
1875 u8 checksum = CHECKSUM_NONE;
1876 u32 opts2, opts3;
1877
hayeswang19c0f402017-01-11 16:25:34 +08001878 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001879 goto return_result;
1880
1881 opts2 = le32_to_cpu(rx_desc->opts2);
1882 opts3 = le32_to_cpu(rx_desc->opts3);
1883
1884 if (opts2 & RD_IPV4_CS) {
1885 if (opts3 & IPF)
1886 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001887 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1888 checksum = CHECKSUM_UNNECESSARY;
1889 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001890 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001891 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001892 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1893 checksum = CHECKSUM_UNNECESSARY;
1894 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1895 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001896 }
1897
1898return_result:
1899 return checksum;
1900}
1901
hayeswangd823ab62015-01-12 12:06:23 +08001902static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001903{
hayeswanga5a4f462013-08-16 16:09:34 +08001904 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001905 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001906 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08001907 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08001908
1909 if (!skb_queue_empty(&tp->rx_queue)) {
1910 while (work_done < budget) {
1911 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1912 struct net_device *netdev = tp->netdev;
1913 struct net_device_stats *stats = &netdev->stats;
1914 unsigned int pkt_len;
1915
1916 if (!skb)
1917 break;
1918
1919 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08001920 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001921 work_done++;
1922 stats->rx_packets++;
1923 stats->rx_bytes += pkt_len;
1924 }
1925 }
hayeswangebc2ec482013-08-14 20:54:38 +08001926
hayeswangd84130a2014-02-18 21:49:02 +08001927 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001928 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001929
1930 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001931 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001932 list_splice_init(&tp->rx_done, &rx_queue);
1933 spin_unlock_irqrestore(&tp->rx_lock, flags);
1934
1935 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001936 struct rx_desc *rx_desc;
1937 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001938 int len_used = 0;
1939 struct urb *urb;
1940 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001941
hayeswangebc2ec482013-08-14 20:54:38 +08001942 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001943
1944 agg = list_entry(cursor, struct rx_agg, list);
1945 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001946 if (urb->actual_length < ETH_ZLEN)
1947 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001948
hayeswangebc2ec482013-08-14 20:54:38 +08001949 rx_desc = agg->head;
1950 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001951 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001952
hayeswang7937f9e2013-11-20 17:30:54 +08001953 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001954 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001955 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001956 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001957 struct sk_buff *skb;
1958
hayeswang74544452017-06-09 17:11:47 +08001959 /* limite the skb numbers for rx_queue */
1960 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
1961 break;
1962
hayeswang7937f9e2013-11-20 17:30:54 +08001963 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001964 if (pkt_len < ETH_ZLEN)
1965 break;
1966
hayeswang7937f9e2013-11-20 17:30:54 +08001967 len_used += pkt_len;
1968 if (urb->actual_length < len_used)
1969 break;
1970
hayeswangb65c0c92017-06-21 11:25:18 +08001971 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08001972 rx_data += sizeof(struct rx_desc);
1973
hayeswangce594e92017-03-16 14:32:22 +08001974 skb = napi_alloc_skb(napi, pkt_len);
hayeswangebc2ec482013-08-14 20:54:38 +08001975 if (!skb) {
1976 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001977 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001978 }
hayeswang565cab02014-03-07 11:04:38 +08001979
1980 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001981 memcpy(skb->data, rx_data, pkt_len);
1982 skb_put(skb, pkt_len);
1983 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001984 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001985 if (work_done < budget) {
hayeswangce594e92017-03-16 14:32:22 +08001986 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001987 work_done++;
1988 stats->rx_packets++;
1989 stats->rx_bytes += pkt_len;
1990 } else {
1991 __skb_queue_tail(&tp->rx_queue, skb);
1992 }
hayeswangebc2ec482013-08-14 20:54:38 +08001993
hayeswang5e2f7482014-03-07 11:04:37 +08001994find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08001995 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08001996 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001997 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001998 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001999 }
2000
hayeswang0de98f62013-08-16 16:09:35 +08002001submit:
hayeswange1a2ca92015-02-06 11:30:45 +08002002 if (!ret) {
2003 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2004 } else {
2005 urb->actual_length = 0;
2006 list_add_tail(&agg->list, next);
2007 }
2008 }
2009
2010 if (!list_empty(&rx_queue)) {
2011 spin_lock_irqsave(&tp->rx_lock, flags);
2012 list_splice_tail(&rx_queue, &tp->rx_done);
2013 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002014 }
hayeswangd823ab62015-01-12 12:06:23 +08002015
2016out1:
2017 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002018}
2019
2020static void tx_bottom(struct r8152 *tp)
2021{
hayeswangebc2ec482013-08-14 20:54:38 +08002022 int res;
2023
hayeswangb1379d92013-08-16 16:09:37 +08002024 do {
2025 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002026
hayeswangb1379d92013-08-16 16:09:37 +08002027 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002028 break;
2029
hayeswangb1379d92013-08-16 16:09:37 +08002030 agg = r8152_get_tx_agg(tp);
2031 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002032 break;
hayeswangb1379d92013-08-16 16:09:37 +08002033
2034 res = r8152_tx_agg_fill(tp, agg);
2035 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002036 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002037
2038 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08002039 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08002040 netif_device_detach(netdev);
2041 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002042 struct net_device_stats *stats = &netdev->stats;
2043 unsigned long flags;
2044
hayeswangb1379d92013-08-16 16:09:37 +08002045 netif_warn(tp, tx_err, netdev,
2046 "failed tx_urb %d\n", res);
2047 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002048
hayeswangb1379d92013-08-16 16:09:37 +08002049 spin_lock_irqsave(&tp->tx_lock, flags);
2050 list_add_tail(&agg->list, &tp->tx_free);
2051 spin_unlock_irqrestore(&tp->tx_lock, flags);
2052 }
hayeswangebc2ec482013-08-14 20:54:38 +08002053 }
hayeswangb1379d92013-08-16 16:09:37 +08002054 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002055}
2056
hayeswangd823ab62015-01-12 12:06:23 +08002057static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08002058{
hayeswangebc2ec482013-08-14 20:54:38 +08002059 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2060 return;
2061
2062 if (!test_bit(WORK_ENABLE, &tp->flags))
2063 return;
2064
hayeswang7559fb2f2013-08-16 16:09:38 +08002065 /* When link down, the driver would cancel all bulks. */
2066 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002067 if (!netif_carrier_ok(tp->netdev))
2068 return;
2069
hayeswangd823ab62015-01-12 12:06:23 +08002070 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002071
hayeswang0c3121f2014-03-07 11:04:36 +08002072 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002073}
2074
hayeswangd823ab62015-01-12 12:06:23 +08002075static int r8152_poll(struct napi_struct *napi, int budget)
2076{
2077 struct r8152 *tp = container_of(napi, struct r8152, napi);
2078 int work_done;
2079
2080 work_done = rx_bottom(tp, budget);
2081 bottom_half(tp);
2082
2083 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002084 if (!napi_complete_done(napi, work_done))
2085 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002086 if (!list_empty(&tp->rx_done))
2087 napi_schedule(napi);
hayeswang248b2132017-01-26 09:38:33 +08002088 else if (!skb_queue_empty(&tp->tx_queue) &&
2089 !list_empty(&tp->tx_free))
2090 napi_schedule(napi);
hayeswangd823ab62015-01-12 12:06:23 +08002091 }
2092
hayeswanga3307f92017-06-09 17:11:48 +08002093out:
hayeswangd823ab62015-01-12 12:06:23 +08002094 return work_done;
2095}
2096
hayeswangebc2ec482013-08-14 20:54:38 +08002097static
2098int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2099{
hayeswanga0fccd42014-11-20 10:29:05 +08002100 int ret;
2101
hayeswangef827a52015-01-09 10:26:36 +08002102 /* The rx would be stopped, so skip submitting */
2103 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2104 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2105 return 0;
2106
hayeswangebc2ec482013-08-14 20:54:38 +08002107 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08002108 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002109 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002110
hayeswanga0fccd42014-11-20 10:29:05 +08002111 ret = usb_submit_urb(agg->urb, mem_flags);
2112 if (ret == -ENODEV) {
2113 set_bit(RTL8152_UNPLUG, &tp->flags);
2114 netif_device_detach(tp->netdev);
2115 } else if (ret) {
2116 struct urb *urb = agg->urb;
2117 unsigned long flags;
2118
2119 urb->actual_length = 0;
2120 spin_lock_irqsave(&tp->rx_lock, flags);
2121 list_add_tail(&agg->list, &tp->rx_done);
2122 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002123
2124 netif_err(tp, rx_err, tp->netdev,
2125 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2126
2127 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002128 }
2129
2130 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002131}
2132
hayeswang00a5e362014-02-18 21:48:59 +08002133static void rtl_drop_queued_tx(struct r8152 *tp)
2134{
2135 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002136 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002137 struct sk_buff *skb;
2138
hayeswangd84130a2014-02-18 21:49:02 +08002139 if (skb_queue_empty(tx_queue))
2140 return;
2141
2142 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002143 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002144 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002145 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002146
2147 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002148 dev_kfree_skb(skb);
2149 stats->tx_dropped++;
2150 }
2151}
2152
hayeswangac718b62013-05-02 16:01:25 +00002153static void rtl8152_tx_timeout(struct net_device *netdev)
2154{
2155 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002156
Hayes Wang4a8deae2014-01-07 11:18:22 +08002157 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002158
2159 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002160}
2161
2162static void rtl8152_set_rx_mode(struct net_device *netdev)
2163{
2164 struct r8152 *tp = netdev_priv(netdev);
2165
hayeswang51d979f2015-02-06 11:30:47 +08002166 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002167 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002168 schedule_delayed_work(&tp->schedule, 0);
2169 }
hayeswangac718b62013-05-02 16:01:25 +00002170}
2171
2172static void _rtl8152_set_rx_mode(struct net_device *netdev)
2173{
2174 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002175 u32 mc_filter[2]; /* Multicast hash filter */
2176 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002177 u32 ocp_data;
2178
hayeswangac718b62013-05-02 16:01:25 +00002179 netif_stop_queue(netdev);
2180 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2181 ocp_data &= ~RCR_ACPT_ALL;
2182 ocp_data |= RCR_AB | RCR_APM;
2183
2184 if (netdev->flags & IFF_PROMISC) {
2185 /* Unconditionally log net taps. */
2186 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2187 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002188 mc_filter[1] = 0xffffffff;
2189 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002190 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2191 (netdev->flags & IFF_ALLMULTI)) {
2192 /* Too many to filter perfectly -- accept all multicasts. */
2193 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002194 mc_filter[1] = 0xffffffff;
2195 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002196 } else {
2197 struct netdev_hw_addr *ha;
2198
hayeswangb209af92014-08-25 15:53:00 +08002199 mc_filter[1] = 0;
2200 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002201 netdev_for_each_mc_addr(ha, netdev) {
2202 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002203
hayeswangac718b62013-05-02 16:01:25 +00002204 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2205 ocp_data |= RCR_AM;
2206 }
2207 }
2208
hayeswang31787f52013-07-31 17:21:25 +08002209 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2210 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002211
hayeswang31787f52013-07-31 17:21:25 +08002212 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002213 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2214 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002215}
2216
hayeswanga5e31252015-01-06 17:41:58 +08002217static netdev_features_t
2218rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2219 netdev_features_t features)
2220{
2221 u32 mss = skb_shinfo(skb)->gso_size;
2222 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2223 int offset = skb_transport_offset(skb);
2224
2225 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002226 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002227 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2228 features &= ~NETIF_F_GSO_MASK;
2229
2230 return features;
2231}
2232
hayeswangac718b62013-05-02 16:01:25 +00002233static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002234 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002235{
2236 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002237
hayeswangac718b62013-05-02 16:01:25 +00002238 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002239
hayeswang61598782013-11-20 17:30:55 +08002240 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002241
hayeswang0c3121f2014-03-07 11:04:36 +08002242 if (!list_empty(&tp->tx_free)) {
2243 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08002244 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002245 schedule_delayed_work(&tp->schedule, 0);
2246 } else {
2247 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08002248 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002249 }
hayeswangb209af92014-08-25 15:53:00 +08002250 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002251 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002252 }
hayeswangdd1b1192013-11-20 17:30:56 +08002253
hayeswangac718b62013-05-02 16:01:25 +00002254 return NETDEV_TX_OK;
2255}
2256
2257static void r8152b_reset_packet_filter(struct r8152 *tp)
2258{
2259 u32 ocp_data;
2260
2261 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2262 ocp_data &= ~FMC_FCR_MCU_EN;
2263 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2264 ocp_data |= FMC_FCR_MCU_EN;
2265 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2266}
2267
2268static void rtl8152_nic_reset(struct r8152 *tp)
2269{
2270 int i;
2271
2272 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2273
2274 for (i = 0; i < 1000; i++) {
2275 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2276 break;
hayeswangb209af92014-08-25 15:53:00 +08002277 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002278 }
2279}
2280
hayeswangdd1b1192013-11-20 17:30:56 +08002281static void set_tx_qlen(struct r8152 *tp)
2282{
2283 struct net_device *netdev = tp->netdev;
2284
hayeswangb65c0c92017-06-21 11:25:18 +08002285 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002286 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002287}
2288
hayeswangac718b62013-05-02 16:01:25 +00002289static inline u8 rtl8152_get_speed(struct r8152 *tp)
2290{
2291 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2292}
2293
hayeswang507605a2014-01-02 11:22:43 +08002294static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002295{
hayeswangebc2ec482013-08-14 20:54:38 +08002296 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002297 u8 speed;
2298
2299 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002300 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002301 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002302 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002303 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2304 } else {
2305 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002306 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002307 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2308 }
hayeswang507605a2014-01-02 11:22:43 +08002309}
2310
hayeswang00a5e362014-02-18 21:48:59 +08002311static void rxdy_gated_en(struct r8152 *tp, bool enable)
2312{
2313 u32 ocp_data;
2314
2315 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2316 if (enable)
2317 ocp_data |= RXDY_GATED_EN;
2318 else
2319 ocp_data &= ~RXDY_GATED_EN;
2320 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2321}
2322
hayeswang445f7f42014-09-23 16:31:47 +08002323static int rtl_start_rx(struct r8152 *tp)
2324{
2325 int i, ret = 0;
2326
2327 INIT_LIST_HEAD(&tp->rx_done);
2328 for (i = 0; i < RTL8152_MAX_RX; i++) {
2329 INIT_LIST_HEAD(&tp->rx_info[i].list);
2330 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2331 if (ret)
2332 break;
2333 }
2334
hayeswang7bcf4f62014-11-20 10:29:06 +08002335 if (ret && ++i < RTL8152_MAX_RX) {
2336 struct list_head rx_queue;
2337 unsigned long flags;
2338
2339 INIT_LIST_HEAD(&rx_queue);
2340
2341 do {
2342 struct rx_agg *agg = &tp->rx_info[i++];
2343 struct urb *urb = agg->urb;
2344
2345 urb->actual_length = 0;
2346 list_add_tail(&agg->list, &rx_queue);
2347 } while (i < RTL8152_MAX_RX);
2348
2349 spin_lock_irqsave(&tp->rx_lock, flags);
2350 list_splice_tail(&rx_queue, &tp->rx_done);
2351 spin_unlock_irqrestore(&tp->rx_lock, flags);
2352 }
2353
hayeswang445f7f42014-09-23 16:31:47 +08002354 return ret;
2355}
2356
2357static int rtl_stop_rx(struct r8152 *tp)
2358{
2359 int i;
2360
2361 for (i = 0; i < RTL8152_MAX_RX; i++)
2362 usb_kill_urb(tp->rx_info[i].urb);
2363
hayeswangd823ab62015-01-12 12:06:23 +08002364 while (!skb_queue_empty(&tp->rx_queue))
2365 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2366
hayeswang445f7f42014-09-23 16:31:47 +08002367 return 0;
2368}
2369
Hayes Wang9fae5412019-07-03 15:11:56 +08002370static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2371{
2372 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2373 OWN_UPDATE | OWN_CLEAR);
2374}
2375
hayeswang507605a2014-01-02 11:22:43 +08002376static int rtl_enable(struct r8152 *tp)
2377{
2378 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002379
2380 r8152b_reset_packet_filter(tp);
2381
2382 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2383 ocp_data |= CR_RE | CR_TE;
2384 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2385
Hayes Wang9fae5412019-07-03 15:11:56 +08002386 switch (tp->version) {
2387 case RTL_VER_08:
2388 case RTL_VER_09:
2389 r8153b_rx_agg_chg_indicate(tp);
2390 break;
2391 default:
2392 break;
2393 }
2394
hayeswang00a5e362014-02-18 21:48:59 +08002395 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002396
hayeswangaa2e0922015-01-09 10:26:35 +08002397 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002398}
2399
hayeswang507605a2014-01-02 11:22:43 +08002400static int rtl8152_enable(struct r8152 *tp)
2401{
hayeswang68714382014-04-11 17:54:31 +08002402 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2403 return -ENODEV;
2404
hayeswang507605a2014-01-02 11:22:43 +08002405 set_tx_qlen(tp);
2406 rtl_set_eee_plus(tp);
2407
2408 return rtl_enable(tp);
2409}
2410
hayeswang464ec102015-02-12 14:33:46 +08002411static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002412{
hayeswang464ec102015-02-12 14:33:46 +08002413 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002414
hayeswang65b82d62017-06-15 14:44:03 +08002415 switch (tp->version) {
2416 case RTL_VER_03:
2417 case RTL_VER_04:
2418 case RTL_VER_05:
2419 case RTL_VER_06:
2420 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2421 ocp_data);
2422 break;
2423
2424 case RTL_VER_08:
2425 case RTL_VER_09:
2426 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2427 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2428 */
2429 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2430 128 / 8);
2431 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2432 ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002433 break;
2434
2435 default:
2436 break;
2437 }
hayeswang464ec102015-02-12 14:33:46 +08002438}
2439
2440static void r8153_set_rx_early_size(struct r8152 *tp)
2441{
hayeswang65b82d62017-06-15 14:44:03 +08002442 u32 ocp_data = agg_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002443
hayeswang65b82d62017-06-15 14:44:03 +08002444 switch (tp->version) {
2445 case RTL_VER_03:
2446 case RTL_VER_04:
2447 case RTL_VER_05:
2448 case RTL_VER_06:
2449 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2450 ocp_data / 4);
2451 break;
2452 case RTL_VER_08:
2453 case RTL_VER_09:
2454 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2455 ocp_data / 8);
hayeswang65b82d62017-06-15 14:44:03 +08002456 break;
2457 default:
2458 WARN_ON_ONCE(1);
2459 break;
2460 }
hayeswang43779f82014-01-02 11:25:10 +08002461}
2462
2463static int rtl8153_enable(struct r8152 *tp)
2464{
hayeswang68714382014-04-11 17:54:31 +08002465 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2466 return -ENODEV;
2467
hayeswang43779f82014-01-02 11:25:10 +08002468 set_tx_qlen(tp);
2469 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002470 r8153_set_rx_early_timeout(tp);
2471 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002472
2473 return rtl_enable(tp);
2474}
2475
hayeswangd70b1132014-09-19 15:17:18 +08002476static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002477{
hayeswangebc2ec482013-08-14 20:54:38 +08002478 u32 ocp_data;
2479 int i;
hayeswangac718b62013-05-02 16:01:25 +00002480
hayeswang68714382014-04-11 17:54:31 +08002481 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2482 rtl_drop_queued_tx(tp);
2483 return;
2484 }
2485
hayeswangac718b62013-05-02 16:01:25 +00002486 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2487 ocp_data &= ~RCR_ACPT_ALL;
2488 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2489
hayeswang00a5e362014-02-18 21:48:59 +08002490 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002491
2492 for (i = 0; i < RTL8152_MAX_TX; i++)
2493 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002494
hayeswang00a5e362014-02-18 21:48:59 +08002495 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002496
2497 for (i = 0; i < 1000; i++) {
2498 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2499 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2500 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002501 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002502 }
2503
2504 for (i = 0; i < 1000; i++) {
2505 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2506 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002507 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002508 }
2509
hayeswang445f7f42014-09-23 16:31:47 +08002510 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002511
2512 rtl8152_nic_reset(tp);
2513}
2514
hayeswang00a5e362014-02-18 21:48:59 +08002515static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2516{
2517 u32 ocp_data;
2518
2519 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2520 if (enable)
2521 ocp_data |= POWER_CUT;
2522 else
2523 ocp_data &= ~POWER_CUT;
2524 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2525
2526 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2527 ocp_data &= ~RESUME_INDICATE;
2528 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002529}
2530
hayeswangc5554292014-09-12 10:43:11 +08002531static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2532{
2533 u32 ocp_data;
2534
2535 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2536 if (enable)
2537 ocp_data |= CPCR_RX_VLAN;
2538 else
2539 ocp_data &= ~CPCR_RX_VLAN;
2540 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2541}
2542
2543static int rtl8152_set_features(struct net_device *dev,
2544 netdev_features_t features)
2545{
2546 netdev_features_t changed = features ^ dev->features;
2547 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002548 int ret;
2549
2550 ret = usb_autopm_get_interface(tp->intf);
2551 if (ret < 0)
2552 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002553
hayeswangb5403272014-10-09 18:00:26 +08002554 mutex_lock(&tp->control);
2555
hayeswangc5554292014-09-12 10:43:11 +08002556 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2557 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2558 rtl_rx_vlan_en(tp, true);
2559 else
2560 rtl_rx_vlan_en(tp, false);
2561 }
2562
hayeswangb5403272014-10-09 18:00:26 +08002563 mutex_unlock(&tp->control);
2564
hayeswang405f8a02014-10-09 18:00:24 +08002565 usb_autopm_put_interface(tp->intf);
2566
2567out:
2568 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002569}
2570
hayeswang21ff2e82014-02-18 21:49:06 +08002571#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2572
2573static u32 __rtl_get_wol(struct r8152 *tp)
2574{
2575 u32 ocp_data;
2576 u32 wolopts = 0;
2577
hayeswang21ff2e82014-02-18 21:49:06 +08002578 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2579 if (ocp_data & LINK_ON_WAKE_EN)
2580 wolopts |= WAKE_PHY;
2581
2582 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2583 if (ocp_data & UWF_EN)
2584 wolopts |= WAKE_UCAST;
2585 if (ocp_data & BWF_EN)
2586 wolopts |= WAKE_BCAST;
2587 if (ocp_data & MWF_EN)
2588 wolopts |= WAKE_MCAST;
2589
2590 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2591 if (ocp_data & MAGIC_EN)
2592 wolopts |= WAKE_MAGIC;
2593
2594 return wolopts;
2595}
2596
2597static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2598{
2599 u32 ocp_data;
2600
2601 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2602
2603 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2604 ocp_data &= ~LINK_ON_WAKE_EN;
2605 if (wolopts & WAKE_PHY)
2606 ocp_data |= LINK_ON_WAKE_EN;
2607 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2608
2609 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002610 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002611 if (wolopts & WAKE_UCAST)
2612 ocp_data |= UWF_EN;
2613 if (wolopts & WAKE_BCAST)
2614 ocp_data |= BWF_EN;
2615 if (wolopts & WAKE_MCAST)
2616 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002617 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2618
2619 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2620
2621 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2622 ocp_data &= ~MAGIC_EN;
2623 if (wolopts & WAKE_MAGIC)
2624 ocp_data |= MAGIC_EN;
2625 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2626
2627 if (wolopts & WAKE_ANY)
2628 device_set_wakeup_enable(&tp->udev->dev, true);
2629 else
2630 device_set_wakeup_enable(&tp->udev->dev, false);
2631}
2632
hayeswang134f98b2017-06-09 17:11:40 +08002633static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2634{
2635 /* MAC clock speed down */
2636 if (enable) {
2637 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2638 ALDPS_SPDWN_RATIO);
2639 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2640 EEE_SPDWN_RATIO);
2641 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2642 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2643 U1U2_SPDWN_EN | L1_SPDWN_EN);
2644 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2645 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2646 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2647 TP1000_SPDWN_EN);
2648 } else {
2649 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2650 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2651 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2652 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2653 }
2654}
2655
hayeswangb2143962015-07-24 13:54:23 +08002656static void r8153_u1u2en(struct r8152 *tp, bool enable)
2657{
2658 u8 u1u2[8];
2659
2660 if (enable)
2661 memset(u1u2, 0xff, sizeof(u1u2));
2662 else
2663 memset(u1u2, 0x00, sizeof(u1u2));
2664
2665 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2666}
2667
hayeswang65b82d62017-06-15 14:44:03 +08002668static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2669{
2670 u32 ocp_data;
2671
2672 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2673 if (enable)
2674 ocp_data |= LPM_U1U2_EN;
2675 else
2676 ocp_data &= ~LPM_U1U2_EN;
2677
2678 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2679}
2680
hayeswangb2143962015-07-24 13:54:23 +08002681static void r8153_u2p3en(struct r8152 *tp, bool enable)
2682{
2683 u32 ocp_data;
2684
2685 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002686 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002687 ocp_data |= U2P3_ENABLE;
2688 else
2689 ocp_data &= ~U2P3_ENABLE;
2690 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2691}
2692
hayeswang65b82d62017-06-15 14:44:03 +08002693static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2694{
2695 u32 ocp_data;
2696
2697 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2698 ocp_data &= ~clear;
2699 ocp_data |= set;
2700 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2701}
2702
2703static void r8153b_green_en(struct r8152 *tp, bool enable)
2704{
2705 u16 data;
2706
2707 if (enable) {
2708 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2709 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2710 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2711 } else {
2712 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2713 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2714 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2715 }
2716
2717 data = sram_read(tp, SRAM_GREEN_CFG);
2718 data |= GREEN_ETH_EN;
2719 sram_write(tp, SRAM_GREEN_CFG, data);
2720
2721 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2722}
2723
hayeswangc564b872017-06-09 17:11:38 +08002724static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2725{
2726 u16 data;
2727 int i;
2728
2729 for (i = 0; i < 500; i++) {
2730 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2731 data &= PHY_STAT_MASK;
2732 if (desired) {
2733 if (data == desired)
2734 break;
2735 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2736 data == PHY_STAT_EXT_INIT) {
2737 break;
2738 }
2739
2740 msleep(20);
2741 }
2742
2743 return data;
2744}
2745
hayeswang65b82d62017-06-15 14:44:03 +08002746static void r8153b_ups_en(struct r8152 *tp, bool enable)
2747{
2748 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2749
2750 if (enable) {
2751 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2752 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2753
2754 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2755 ocp_data |= BIT(0);
2756 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2757 } else {
2758 u16 data;
2759
2760 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2761 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2762
2763 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2764 ocp_data &= ~BIT(0);
2765 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2766
2767 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2768 ocp_data &= ~PCUT_STATUS;
2769 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2770
2771 data = r8153_phy_status(tp, 0);
2772
2773 switch (data) {
2774 case PHY_STAT_PWRDN:
2775 case PHY_STAT_EXT_INIT:
2776 r8153b_green_en(tp,
2777 test_bit(GREEN_ETHERNET, &tp->flags));
2778
2779 data = r8152_mdio_read(tp, MII_BMCR);
2780 data &= ~BMCR_PDOWN;
2781 data |= BMCR_RESET;
2782 r8152_mdio_write(tp, MII_BMCR, data);
2783
2784 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002785 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002786
2787 default:
2788 if (data != PHY_STAT_LAN_ON)
2789 netif_warn(tp, link, tp->netdev,
2790 "PHY not ready");
2791 break;
2792 }
2793 }
2794}
2795
hayeswangb2143962015-07-24 13:54:23 +08002796static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2797{
2798 u32 ocp_data;
2799
2800 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2801 if (enable)
2802 ocp_data |= PWR_EN | PHASE2_EN;
2803 else
2804 ocp_data &= ~(PWR_EN | PHASE2_EN);
2805 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2806
2807 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2808 ocp_data &= ~PCUT_STATUS;
2809 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2810}
2811
hayeswang65b82d62017-06-15 14:44:03 +08002812static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2813{
2814 u32 ocp_data;
2815
2816 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2817 if (enable)
2818 ocp_data |= PWR_EN | PHASE2_EN;
2819 else
2820 ocp_data &= ~PWR_EN;
2821 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2822
2823 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2824 ocp_data &= ~PCUT_STATUS;
2825 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2826}
2827
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002828static void r8153_queue_wake(struct r8152 *tp, bool enable)
hayeswang65b82d62017-06-15 14:44:03 +08002829{
2830 u32 ocp_data;
2831
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002832 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
hayeswang65b82d62017-06-15 14:44:03 +08002833 if (enable)
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002834 ocp_data |= UPCOMING_RUNTIME_D3;
hayeswang65b82d62017-06-15 14:44:03 +08002835 else
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002836 ocp_data &= ~UPCOMING_RUNTIME_D3;
2837 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002838
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002839 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
2840 ocp_data &= ~LINK_CHG_EVENT;
2841 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
2842
2843 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
2844 ocp_data &= ~LINK_CHANGE_FLAG;
2845 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
hayeswang65b82d62017-06-15 14:44:03 +08002846}
2847
hayeswang7daed8d2015-07-24 13:54:24 +08002848static bool rtl_can_wakeup(struct r8152 *tp)
2849{
2850 struct usb_device *udev = tp->udev;
2851
2852 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
2853}
2854
hayeswang9a4be1b2014-02-18 21:49:07 +08002855static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2856{
2857 if (enable) {
2858 u32 ocp_data;
2859
2860 __rtl_set_wol(tp, WAKE_ANY);
2861
2862 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2863
2864 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2865 ocp_data |= LINK_OFF_WAKE_EN;
2866 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2867
2868 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2869 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08002870 u32 ocp_data;
2871
hayeswang9a4be1b2014-02-18 21:49:07 +08002872 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08002873
2874 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2875
2876 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2877 ocp_data &= ~LINK_OFF_WAKE_EN;
2878 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2879
2880 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08002881 }
2882}
hayeswangf95ae8a2016-06-30 15:33:35 +08002883
hayeswang2609af12016-07-05 16:11:46 +08002884static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
2885{
hayeswang2609af12016-07-05 16:11:46 +08002886 if (enable) {
2887 r8153_u1u2en(tp, false);
2888 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002889 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08002890 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08002891 } else {
hayeswang02552752017-06-09 17:11:42 +08002892 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002893 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08002894
2895 switch (tp->version) {
2896 case RTL_VER_03:
2897 case RTL_VER_04:
2898 break;
2899 case RTL_VER_05:
2900 case RTL_VER_06:
2901 default:
2902 r8153_u2p3en(tp, true);
2903 break;
2904 }
2905
hayeswangb2143962015-07-24 13:54:23 +08002906 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08002907 }
2908}
2909
hayeswang65b82d62017-06-15 14:44:03 +08002910static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
2911{
2912 if (enable) {
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002913 r8153_queue_wake(tp, true);
hayeswang65b82d62017-06-15 14:44:03 +08002914 r8153b_u1u2en(tp, false);
2915 r8153_u2p3en(tp, false);
2916 rtl_runtime_suspend_enable(tp, true);
2917 r8153b_ups_en(tp, true);
2918 } else {
2919 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08002920 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08002921 rtl_runtime_suspend_enable(tp, false);
2922 r8153_u2p3en(tp, true);
2923 r8153b_u1u2en(tp, true);
2924 }
2925}
2926
hayeswang43499682014-02-18 21:48:58 +08002927static void r8153_teredo_off(struct r8152 *tp)
2928{
2929 u32 ocp_data;
2930
hayeswang65b82d62017-06-15 14:44:03 +08002931 switch (tp->version) {
2932 case RTL_VER_01:
2933 case RTL_VER_02:
2934 case RTL_VER_03:
2935 case RTL_VER_04:
2936 case RTL_VER_05:
2937 case RTL_VER_06:
2938 case RTL_VER_07:
2939 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2940 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
2941 OOB_TEREDO_EN);
2942 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2943 break;
2944
2945 case RTL_VER_08:
2946 case RTL_VER_09:
2947 /* The bit 0 ~ 7 are relative with teredo settings. They are
2948 * W1C (write 1 to clear), so set all 1 to disable it.
2949 */
2950 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
2951 break;
2952
2953 default:
2954 break;
2955 }
hayeswang43499682014-02-18 21:48:58 +08002956
2957 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2958 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2959 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2960}
2961
hayeswang93fe9b12016-06-16 10:55:18 +08002962static void rtl_reset_bmu(struct r8152 *tp)
2963{
2964 u32 ocp_data;
2965
2966 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
2967 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
2968 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2969 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
2970 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2971}
2972
hayeswangcda9fb02016-01-07 17:51:12 +08002973static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08002974{
hayeswangcda9fb02016-01-07 17:51:12 +08002975 if (enable) {
2976 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2977 LINKENA | DIS_SDSAVE);
2978 } else {
2979 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
2980 DIS_SDSAVE);
2981 msleep(20);
2982 }
hayeswang43499682014-02-18 21:48:58 +08002983}
2984
hayeswange6449532016-09-20 16:22:05 +08002985static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
2986{
2987 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
2988 ocp_reg_write(tp, OCP_EEE_DATA, reg);
2989 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
2990}
2991
2992static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
2993{
2994 u16 data;
2995
2996 r8152_mmd_indirect(tp, dev, reg);
2997 data = ocp_reg_read(tp, OCP_EEE_DATA);
2998 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2999
3000 return data;
3001}
3002
3003static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
3004{
3005 r8152_mmd_indirect(tp, dev, reg);
3006 ocp_reg_write(tp, OCP_EEE_DATA, data);
3007 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
3008}
3009
3010static void r8152_eee_en(struct r8152 *tp, bool enable)
3011{
3012 u16 config1, config2, config3;
3013 u32 ocp_data;
3014
3015 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3016 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
3017 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
3018 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3019
3020 if (enable) {
3021 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3022 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3023 config1 |= sd_rise_time(1);
3024 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3025 config3 |= fast_snr(42);
3026 } else {
3027 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3028 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3029 RX_QUIET_EN);
3030 config1 |= sd_rise_time(7);
3031 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3032 config3 |= fast_snr(511);
3033 }
3034
3035 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3036 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3037 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3038 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3039}
3040
3041static void r8152b_enable_eee(struct r8152 *tp)
3042{
3043 r8152_eee_en(tp, true);
3044 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3045}
3046
3047static void r8152b_enable_fc(struct r8152 *tp)
3048{
3049 u16 anar;
3050
3051 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3052 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3053 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3054}
3055
hayeswangd70b1132014-09-19 15:17:18 +08003056static void rtl8152_disable(struct r8152 *tp)
3057{
hayeswangcda9fb02016-01-07 17:51:12 +08003058 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003059 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003060 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003061}
3062
hayeswang43499682014-02-18 21:48:58 +08003063static void r8152b_hw_phy_cfg(struct r8152 *tp)
3064{
hayeswangef39df82016-09-20 16:22:07 +08003065 r8152b_enable_eee(tp);
3066 r8152_aldps_en(tp, true);
3067 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003068
hayeswangaa66a5f2014-02-18 21:49:04 +08003069 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003070}
3071
hayeswangac718b62013-05-02 16:01:25 +00003072static void r8152b_exit_oob(struct r8152 *tp)
3073{
hayeswangdb8515e2014-03-06 15:07:16 +08003074 u32 ocp_data;
3075 int i;
hayeswangac718b62013-05-02 16:01:25 +00003076
3077 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3078 ocp_data &= ~RCR_ACPT_ALL;
3079 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3080
hayeswang00a5e362014-02-18 21:48:59 +08003081 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003082 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003083 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3084 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3085
3086 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3087 ocp_data &= ~NOW_IS_OOB;
3088 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3089
3090 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3091 ocp_data &= ~MCU_BORW_EN;
3092 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3093
3094 for (i = 0; i < 1000; i++) {
3095 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3096 if (ocp_data & LINK_LIST_READY)
3097 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003098 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003099 }
3100
3101 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3102 ocp_data |= RE_INIT_LL;
3103 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3104
3105 for (i = 0; i < 1000; i++) {
3106 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3107 if (ocp_data & LINK_LIST_READY)
3108 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003109 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003110 }
3111
3112 rtl8152_nic_reset(tp);
3113
3114 /* rx share fifo credit full threshold */
3115 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3116
hayeswanga3cc4652014-07-24 16:37:43 +08003117 if (tp->udev->speed == USB_SPEED_FULL ||
3118 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003119 /* rx share fifo credit near full threshold */
3120 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3121 RXFIFO_THR2_FULL);
3122 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3123 RXFIFO_THR3_FULL);
3124 } else {
3125 /* rx share fifo credit near full threshold */
3126 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3127 RXFIFO_THR2_HIGH);
3128 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3129 RXFIFO_THR3_HIGH);
3130 }
3131
3132 /* TX share fifo free credit full threshold */
3133 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3134
3135 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003136 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003137 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3138 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3139
hayeswangc5554292014-09-12 10:43:11 +08003140 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003141
3142 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3143
3144 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3145 ocp_data |= TCR0_AUTO_FIFO;
3146 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3147}
3148
3149static void r8152b_enter_oob(struct r8152 *tp)
3150{
hayeswang45f4a192014-01-06 17:08:41 +08003151 u32 ocp_data;
3152 int i;
hayeswangac718b62013-05-02 16:01:25 +00003153
3154 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3155 ocp_data &= ~NOW_IS_OOB;
3156 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3157
3158 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3159 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3160 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3161
hayeswangd70b1132014-09-19 15:17:18 +08003162 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003163
3164 for (i = 0; i < 1000; i++) {
3165 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3166 if (ocp_data & LINK_LIST_READY)
3167 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003168 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003169 }
3170
3171 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3172 ocp_data |= RE_INIT_LL;
3173 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3174
3175 for (i = 0; i < 1000; i++) {
3176 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3177 if (ocp_data & LINK_LIST_READY)
3178 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003179 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003180 }
3181
3182 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3183
hayeswangc5554292014-09-12 10:43:11 +08003184 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003185
3186 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
3187 ocp_data |= ALDPS_PROXY_MODE;
3188 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
3189
3190 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3191 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3192 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3193
hayeswang00a5e362014-02-18 21:48:59 +08003194 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003195
3196 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3197 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3198 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3199}
3200
hayeswang65b82d62017-06-15 14:44:03 +08003201static int r8153_patch_request(struct r8152 *tp, bool request)
3202{
3203 u16 data;
3204 int i;
3205
3206 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3207 if (request)
3208 data |= PATCH_REQUEST;
3209 else
3210 data &= ~PATCH_REQUEST;
3211 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3212
3213 for (i = 0; request && i < 5000; i++) {
3214 usleep_range(1000, 2000);
3215 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3216 break;
3217 }
3218
3219 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3220 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3221 r8153_patch_request(tp, false);
3222 return -ETIME;
3223 } else {
3224 return 0;
3225 }
3226}
3227
hayeswange6449532016-09-20 16:22:05 +08003228static void r8153_aldps_en(struct r8152 *tp, bool enable)
3229{
3230 u16 data;
3231
3232 data = ocp_reg_read(tp, OCP_POWER_CFG);
3233 if (enable) {
3234 data |= EN_ALDPS;
3235 ocp_reg_write(tp, OCP_POWER_CFG, data);
3236 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003237 int i;
3238
hayeswange6449532016-09-20 16:22:05 +08003239 data &= ~EN_ALDPS;
3240 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003241 for (i = 0; i < 20; i++) {
3242 usleep_range(1000, 2000);
3243 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3244 break;
3245 }
hayeswange6449532016-09-20 16:22:05 +08003246 }
3247}
3248
hayeswang65b82d62017-06-15 14:44:03 +08003249static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3250{
3251 r8153_aldps_en(tp, enable);
3252
3253 if (enable)
3254 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3255 else
3256 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3257}
3258
hayeswange6449532016-09-20 16:22:05 +08003259static void r8153_eee_en(struct r8152 *tp, bool enable)
3260{
3261 u32 ocp_data;
3262 u16 config;
3263
3264 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3265 config = ocp_reg_read(tp, OCP_EEE_CFG);
3266
3267 if (enable) {
3268 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3269 config |= EEE10_EN;
3270 } else {
3271 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3272 config &= ~EEE10_EN;
3273 }
3274
3275 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3276 ocp_reg_write(tp, OCP_EEE_CFG, config);
3277}
3278
hayeswang65b82d62017-06-15 14:44:03 +08003279static void r8153b_eee_en(struct r8152 *tp, bool enable)
3280{
3281 r8153_eee_en(tp, enable);
3282
3283 if (enable)
3284 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3285 else
3286 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3287}
3288
3289static void r8153b_enable_fc(struct r8152 *tp)
3290{
3291 r8152b_enable_fc(tp);
3292 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3293}
3294
hayeswang43779f82014-01-02 11:25:10 +08003295static void r8153_hw_phy_cfg(struct r8152 *tp)
3296{
3297 u32 ocp_data;
3298 u16 data;
3299
hayeswangd768c612016-09-20 16:22:09 +08003300 /* disable ALDPS before updating the PHY parameters */
3301 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003302
hayeswangd768c612016-09-20 16:22:09 +08003303 /* disable EEE before updating the PHY parameters */
3304 r8153_eee_en(tp, false);
3305 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang43779f82014-01-02 11:25:10 +08003306
3307 if (tp->version == RTL_VER_03) {
3308 data = ocp_reg_read(tp, OCP_EEE_CFG);
3309 data &= ~CTAP_SHORT_EN;
3310 ocp_reg_write(tp, OCP_EEE_CFG, data);
3311 }
3312
3313 data = ocp_reg_read(tp, OCP_POWER_CFG);
3314 data |= EEE_CLKDIV_EN;
3315 ocp_reg_write(tp, OCP_POWER_CFG, data);
3316
3317 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3318 data |= EN_10M_BGOFF;
3319 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3320 data = ocp_reg_read(tp, OCP_POWER_CFG);
3321 data |= EN_10M_PLLOFF;
3322 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003323 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003324
3325 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3326 ocp_data |= PFM_PWM_SWITCH;
3327 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3328
hayeswangb4d99de2015-01-19 17:02:46 +08003329 /* Enable LPF corner auto tune */
3330 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003331
hayeswangb4d99de2015-01-19 17:02:46 +08003332 /* Adjust 10M Amplitude */
3333 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3334 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003335
hayeswangaf0287e2016-09-20 16:22:08 +08003336 r8153_eee_en(tp, true);
3337 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3338
hayeswangef39df82016-09-20 16:22:07 +08003339 r8153_aldps_en(tp, true);
3340 r8152b_enable_fc(tp);
3341
hayeswang3cb32342017-06-09 17:11:43 +08003342 switch (tp->version) {
3343 case RTL_VER_03:
3344 case RTL_VER_04:
3345 break;
3346 case RTL_VER_05:
3347 case RTL_VER_06:
3348 default:
3349 r8153_u2p3en(tp, true);
3350 break;
3351 }
3352
hayeswangaa66a5f2014-02-18 21:49:04 +08003353 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003354}
3355
hayeswang65b82d62017-06-15 14:44:03 +08003356static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3357{
3358 u32 ocp_data;
3359
3360 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3361 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3362 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3363 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3364
3365 return ocp_data;
3366}
3367
3368static void r8153b_hw_phy_cfg(struct r8152 *tp)
3369{
3370 u32 ocp_data, ups_flags = 0;
3371 u16 data;
3372
3373 /* disable ALDPS before updating the PHY parameters */
3374 r8153b_aldps_en(tp, false);
3375
3376 /* disable EEE before updating the PHY parameters */
3377 r8153b_eee_en(tp, false);
3378 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3379
3380 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3381
3382 data = sram_read(tp, SRAM_GREEN_CFG);
3383 data |= R_TUNE_EN;
3384 sram_write(tp, SRAM_GREEN_CFG, data);
3385 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3386 data |= PGA_RETURN_EN;
3387 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3388
3389 /* ADC Bias Calibration:
3390 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3391 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3392 * ADC ioffset.
3393 */
3394 ocp_data = r8152_efuse_read(tp, 0x7d);
3395 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3396 if (data != 0xffff)
3397 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3398
3399 /* ups mode tx-link-pulse timing adjustment:
3400 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3401 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3402 */
3403 ocp_data = ocp_reg_read(tp, 0xc426);
3404 ocp_data &= 0x3fff;
3405 if (ocp_data) {
3406 u32 swr_cnt_1ms_ini;
3407
3408 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3409 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3410 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3411 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3412 }
3413
3414 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3415 ocp_data |= PFM_PWM_SWITCH;
3416 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3417
3418 /* Advnace EEE */
3419 if (!r8153_patch_request(tp, true)) {
3420 data = ocp_reg_read(tp, OCP_POWER_CFG);
3421 data |= EEE_CLKDIV_EN;
3422 ocp_reg_write(tp, OCP_POWER_CFG, data);
3423
3424 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3425 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3426 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3427
3428 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3429 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3430
3431 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3432 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3433 UPS_FLAGS_EEE_PLLOFF_GIGA;
3434
3435 r8153_patch_request(tp, false);
3436 }
3437
3438 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3439
3440 r8153b_eee_en(tp, true);
3441 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3442
3443 r8153b_aldps_en(tp, true);
3444 r8153b_enable_fc(tp);
3445 r8153_u2p3en(tp, true);
3446
3447 set_bit(PHY_RESET, &tp->flags);
3448}
3449
hayeswang43779f82014-01-02 11:25:10 +08003450static void r8153_first_init(struct r8152 *tp)
3451{
3452 u32 ocp_data;
3453 int i;
3454
hayeswang134f98b2017-06-09 17:11:40 +08003455 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003456 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003457 r8153_teredo_off(tp);
3458
3459 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3460 ocp_data &= ~RCR_ACPT_ALL;
3461 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3462
hayeswang43779f82014-01-02 11:25:10 +08003463 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003464 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003465
3466 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3467 ocp_data &= ~NOW_IS_OOB;
3468 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3469
3470 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3471 ocp_data &= ~MCU_BORW_EN;
3472 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3473
3474 for (i = 0; i < 1000; i++) {
3475 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3476 if (ocp_data & LINK_LIST_READY)
3477 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003478 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003479 }
3480
3481 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3482 ocp_data |= RE_INIT_LL;
3483 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3484
3485 for (i = 0; i < 1000; i++) {
3486 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3487 if (ocp_data & LINK_LIST_READY)
3488 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003489 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003490 }
3491
hayeswangc5554292014-09-12 10:43:11 +08003492 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003493
hayeswangb65c0c92017-06-21 11:25:18 +08003494 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003495 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003496 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003497
3498 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3499 ocp_data |= TCR0_AUTO_FIFO;
3500 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3501
3502 rtl8152_nic_reset(tp);
3503
3504 /* rx share fifo credit full threshold */
3505 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3506 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3507 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3508 /* TX share fifo free credit full threshold */
3509 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003510}
3511
3512static void r8153_enter_oob(struct r8152 *tp)
3513{
3514 u32 ocp_data;
3515 int i;
3516
hayeswang134f98b2017-06-09 17:11:40 +08003517 r8153_mac_clk_spd(tp, true);
3518
hayeswang43779f82014-01-02 11:25:10 +08003519 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3520 ocp_data &= ~NOW_IS_OOB;
3521 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3522
hayeswangd70b1132014-09-19 15:17:18 +08003523 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003524 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003525
3526 for (i = 0; i < 1000; i++) {
3527 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3528 if (ocp_data & LINK_LIST_READY)
3529 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003530 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003531 }
3532
3533 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3534 ocp_data |= RE_INIT_LL;
3535 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3536
3537 for (i = 0; i < 1000; i++) {
3538 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3539 if (ocp_data & LINK_LIST_READY)
3540 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003541 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003542 }
3543
hayeswangb65c0c92017-06-21 11:25:18 +08003544 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003545 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003546
hayeswang65b82d62017-06-15 14:44:03 +08003547 switch (tp->version) {
3548 case RTL_VER_03:
3549 case RTL_VER_04:
3550 case RTL_VER_05:
3551 case RTL_VER_06:
3552 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3553 ocp_data &= ~TEREDO_WAKE_MASK;
3554 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3555 break;
3556
3557 case RTL_VER_08:
3558 case RTL_VER_09:
3559 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3560 * type. Set it to zero. bits[7:0] are the W1C bits about
3561 * the events. Set them to all 1 to clear them.
3562 */
3563 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3564 break;
3565
3566 default:
3567 break;
3568 }
hayeswang43779f82014-01-02 11:25:10 +08003569
hayeswangc5554292014-09-12 10:43:11 +08003570 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003571
3572 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
3573 ocp_data |= ALDPS_PROXY_MODE;
3574 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
3575
3576 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3577 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3578 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3579
hayeswang00a5e362014-02-18 21:48:59 +08003580 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003581
3582 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3583 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3584 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3585}
3586
hayeswangd70b1132014-09-19 15:17:18 +08003587static void rtl8153_disable(struct r8152 *tp)
3588{
hayeswangcda9fb02016-01-07 17:51:12 +08003589 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003590 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003591 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003592 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003593}
3594
hayeswang65b82d62017-06-15 14:44:03 +08003595static void rtl8153b_disable(struct r8152 *tp)
3596{
3597 r8153b_aldps_en(tp, false);
3598 rtl_disable(tp);
3599 rtl_reset_bmu(tp);
3600 r8153b_aldps_en(tp, true);
3601}
3602
hayeswangac718b62013-05-02 16:01:25 +00003603static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3604{
hayeswang43779f82014-01-02 11:25:10 +08003605 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003606 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003607 int ret = 0;
3608
hayeswangac718b62013-05-02 16:01:25 +00003609 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3610 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3611 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003612 if (tp->mii.supports_gmii) {
3613 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3614 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3615 } else {
3616 gbcr = 0;
3617 }
hayeswangac718b62013-05-02 16:01:25 +00003618
3619 if (autoneg == AUTONEG_DISABLE) {
3620 if (speed == SPEED_10) {
3621 bmcr = 0;
3622 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003623 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003624 } else if (speed == SPEED_100) {
3625 bmcr = BMCR_SPEED100;
3626 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003627 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003628 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3629 bmcr = BMCR_SPEED1000;
3630 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003631 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003632 } else {
3633 ret = -EINVAL;
3634 goto out;
3635 }
3636
hayeswang65b82d62017-06-15 14:44:03 +08003637 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003638 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003639 if (speed != SPEED_1000)
3640 speed_duplex++;
3641 }
hayeswangac718b62013-05-02 16:01:25 +00003642 } else {
3643 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003644 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003645 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003646 speed_duplex = NWAY_10M_FULL;
3647 } else {
hayeswangac718b62013-05-02 16:01:25 +00003648 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003649 speed_duplex = NWAY_10M_HALF;
3650 }
hayeswangac718b62013-05-02 16:01:25 +00003651 } else if (speed == SPEED_100) {
3652 if (duplex == DUPLEX_FULL) {
3653 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3654 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003655 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003656 } else {
3657 anar |= ADVERTISE_10HALF;
3658 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003659 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003660 }
hayeswang43779f82014-01-02 11:25:10 +08003661 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3662 if (duplex == DUPLEX_FULL) {
3663 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3664 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3665 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3666 } else {
3667 anar |= ADVERTISE_10HALF;
3668 anar |= ADVERTISE_100HALF;
3669 gbcr |= ADVERTISE_1000HALF;
3670 }
hayeswang65b82d62017-06-15 14:44:03 +08003671 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003672 } else {
3673 ret = -EINVAL;
3674 goto out;
3675 }
3676
3677 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3678 }
3679
hayeswangfae56172016-06-16 14:08:29 +08003680 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003681 bmcr |= BMCR_RESET;
3682
hayeswang43779f82014-01-02 11:25:10 +08003683 if (tp->mii.supports_gmii)
3684 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3685
hayeswangac718b62013-05-02 16:01:25 +00003686 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3687 r8152_mdio_write(tp, MII_BMCR, bmcr);
3688
hayeswang65b82d62017-06-15 14:44:03 +08003689 switch (tp->version) {
3690 case RTL_VER_08:
3691 case RTL_VER_09:
3692 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3693 UPS_FLAGS_SPEED_MASK);
3694 break;
3695
3696 default:
3697 break;
3698 }
3699
hayeswangfae56172016-06-16 14:08:29 +08003700 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003701 int i;
3702
hayeswangaa66a5f2014-02-18 21:49:04 +08003703 for (i = 0; i < 50; i++) {
3704 msleep(20);
3705 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3706 break;
3707 }
3708 }
3709
hayeswangac718b62013-05-02 16:01:25 +00003710out:
hayeswangac718b62013-05-02 16:01:25 +00003711 return ret;
3712}
3713
hayeswangd70b1132014-09-19 15:17:18 +08003714static void rtl8152_up(struct r8152 *tp)
3715{
3716 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3717 return;
3718
hayeswangcda9fb02016-01-07 17:51:12 +08003719 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003720 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003721 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003722}
3723
hayeswangac718b62013-05-02 16:01:25 +00003724static void rtl8152_down(struct r8152 *tp)
3725{
hayeswang68714382014-04-11 17:54:31 +08003726 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3727 rtl_drop_queued_tx(tp);
3728 return;
3729 }
3730
hayeswang00a5e362014-02-18 21:48:59 +08003731 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003732 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003733 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003734 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003735}
3736
hayeswangd70b1132014-09-19 15:17:18 +08003737static void rtl8153_up(struct r8152 *tp)
3738{
3739 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3740 return;
3741
hayeswangb2143962015-07-24 13:54:23 +08003742 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003743 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003744 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003745 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003746 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003747
3748 switch (tp->version) {
3749 case RTL_VER_03:
3750 case RTL_VER_04:
3751 break;
3752 case RTL_VER_05:
3753 case RTL_VER_06:
3754 default:
3755 r8153_u2p3en(tp, true);
3756 break;
3757 }
3758
hayeswangb2143962015-07-24 13:54:23 +08003759 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003760}
3761
hayeswang43779f82014-01-02 11:25:10 +08003762static void rtl8153_down(struct r8152 *tp)
3763{
hayeswang68714382014-04-11 17:54:31 +08003764 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3765 rtl_drop_queued_tx(tp);
3766 return;
3767 }
3768
hayeswangb9702722014-02-18 21:49:00 +08003769 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003770 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003771 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003772 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003773 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003774 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003775}
3776
hayeswang65b82d62017-06-15 14:44:03 +08003777static void rtl8153b_up(struct r8152 *tp)
3778{
3779 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3780 return;
3781
3782 r8153b_u1u2en(tp, false);
3783 r8153_u2p3en(tp, false);
3784 r8153b_aldps_en(tp, false);
3785
3786 r8153_first_init(tp);
3787 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3788
3789 r8153b_aldps_en(tp, true);
3790 r8153_u2p3en(tp, true);
3791 r8153b_u1u2en(tp, true);
3792}
3793
3794static void rtl8153b_down(struct r8152 *tp)
3795{
3796 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3797 rtl_drop_queued_tx(tp);
3798 return;
3799 }
3800
3801 r8153b_u1u2en(tp, false);
3802 r8153_u2p3en(tp, false);
3803 r8153b_power_cut_en(tp, false);
3804 r8153b_aldps_en(tp, false);
3805 r8153_enter_oob(tp);
3806 r8153b_aldps_en(tp, true);
3807}
3808
hayeswang2dd49e02015-09-07 11:57:44 +08003809static bool rtl8152_in_nway(struct r8152 *tp)
3810{
3811 u16 nway_state;
3812
3813 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3814 tp->ocp_base = 0x2000;
3815 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3816 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3817
3818 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3819 if (nway_state & 0xc000)
3820 return false;
3821 else
3822 return true;
3823}
3824
3825static bool rtl8153_in_nway(struct r8152 *tp)
3826{
3827 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
3828
3829 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
3830 return false;
3831 else
3832 return true;
3833}
3834
hayeswangac718b62013-05-02 16:01:25 +00003835static void set_carrier(struct r8152 *tp)
3836{
3837 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08003838 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00003839 u8 speed;
3840
3841 speed = rtl8152_get_speed(tp);
3842
3843 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08003844 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08003845 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08003846 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003847 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00003848 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08003849 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08003850 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
3851 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08003852 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08003853 netif_wake_queue(netdev);
3854 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08003855 } else if (netif_queue_stopped(netdev) &&
3856 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
3857 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003858 }
3859 } else {
hayeswang51d979f2015-02-06 11:30:47 +08003860 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00003861 netif_carrier_off(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003862 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08003863 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08003864 napi_enable(napi);
hayeswangde9bf292017-01-26 09:38:32 +08003865 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00003866 }
3867 }
hayeswangac718b62013-05-02 16:01:25 +00003868}
3869
3870static void rtl_work_func_t(struct work_struct *work)
3871{
3872 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
3873
hayeswanga1f83fe2014-11-12 10:05:05 +08003874 /* If the device is unplugged or !netif_running(), the workqueue
3875 * doesn't need to wake the device, and could return directly.
3876 */
3877 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
3878 return;
3879
hayeswang9a4be1b2014-02-18 21:49:07 +08003880 if (usb_autopm_get_interface(tp->intf) < 0)
3881 return;
3882
hayeswangac718b62013-05-02 16:01:25 +00003883 if (!test_bit(WORK_ENABLE, &tp->flags))
3884 goto out1;
3885
hayeswangb5403272014-10-09 18:00:26 +08003886 if (!mutex_trylock(&tp->control)) {
3887 schedule_delayed_work(&tp->schedule, 0);
3888 goto out1;
3889 }
3890
hayeswang216a8342016-01-07 17:51:11 +08003891 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08003892 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00003893
hayeswang216a8342016-01-07 17:51:11 +08003894 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00003895 _rtl8152_set_rx_mode(tp->netdev);
3896
hayeswangd823ab62015-01-12 12:06:23 +08003897 /* don't schedule napi before linking */
hayeswang216a8342016-01-07 17:51:11 +08003898 if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
3899 netif_carrier_ok(tp->netdev))
hayeswangd823ab62015-01-12 12:06:23 +08003900 napi_schedule(&tp->napi);
hayeswangaa66a5f2014-02-18 21:49:04 +08003901
hayeswangb5403272014-10-09 18:00:26 +08003902 mutex_unlock(&tp->control);
3903
hayeswangac718b62013-05-02 16:01:25 +00003904out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08003905 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003906}
3907
hayeswanga028a9e2016-06-13 17:49:36 +08003908static void rtl_hw_phy_work_func_t(struct work_struct *work)
3909{
3910 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
3911
3912 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3913 return;
3914
3915 if (usb_autopm_get_interface(tp->intf) < 0)
3916 return;
3917
3918 mutex_lock(&tp->control);
3919
3920 tp->rtl_ops.hw_phy_cfg(tp);
3921
hayeswangaa7e26b2016-06-13 17:49:38 +08003922 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08003923
hayeswanga028a9e2016-06-13 17:49:36 +08003924 mutex_unlock(&tp->control);
3925
3926 usb_autopm_put_interface(tp->intf);
3927}
3928
hayeswang5ee3c602016-01-07 17:12:17 +08003929#ifdef CONFIG_PM_SLEEP
3930static int rtl_notifier(struct notifier_block *nb, unsigned long action,
3931 void *data)
3932{
3933 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
3934
3935 switch (action) {
3936 case PM_HIBERNATION_PREPARE:
3937 case PM_SUSPEND_PREPARE:
3938 usb_autopm_get_interface(tp->intf);
3939 break;
3940
3941 case PM_POST_HIBERNATION:
3942 case PM_POST_SUSPEND:
3943 usb_autopm_put_interface(tp->intf);
3944 break;
3945
3946 case PM_POST_RESTORE:
3947 case PM_RESTORE_PREPARE:
3948 default:
3949 break;
3950 }
3951
3952 return NOTIFY_DONE;
3953}
3954#endif
3955
hayeswangac718b62013-05-02 16:01:25 +00003956static int rtl8152_open(struct net_device *netdev)
3957{
3958 struct r8152 *tp = netdev_priv(netdev);
3959 int res = 0;
3960
hayeswang7e9da482014-02-18 21:49:05 +08003961 res = alloc_all_mem(tp);
3962 if (res)
3963 goto out;
3964
hayeswang9a4be1b2014-02-18 21:49:07 +08003965 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08003966 if (res < 0)
3967 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08003968
hayeswangb5403272014-10-09 18:00:26 +08003969 mutex_lock(&tp->control);
3970
hayeswang7e9da482014-02-18 21:49:05 +08003971 tp->rtl_ops.up(tp);
3972
hayeswang40a82912013-08-14 20:54:40 +08003973 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003974 netif_start_queue(netdev);
3975 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003976
hayeswang3d55f442014-02-06 11:55:48 +08003977 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3978 if (res) {
3979 if (res == -ENODEV)
3980 netif_device_detach(tp->netdev);
3981 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3982 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08003983 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08003984 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08003985 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003986
hayeswangb5403272014-10-09 18:00:26 +08003987 mutex_unlock(&tp->control);
3988
hayeswang9a4be1b2014-02-18 21:49:07 +08003989 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08003990#ifdef CONFIG_PM_SLEEP
3991 tp->pm_notifier.notifier_call = rtl_notifier;
3992 register_pm_notifier(&tp->pm_notifier);
3993#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08003994 return 0;
hayeswangac718b62013-05-02 16:01:25 +00003995
Guenter Roeckca0a7532016-11-09 19:51:25 -08003996out_unlock:
3997 mutex_unlock(&tp->control);
3998 usb_autopm_put_interface(tp->intf);
3999out_free:
4000 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08004001out:
hayeswangac718b62013-05-02 16:01:25 +00004002 return res;
4003}
4004
4005static int rtl8152_close(struct net_device *netdev)
4006{
4007 struct r8152 *tp = netdev_priv(netdev);
4008 int res = 0;
4009
hayeswang5ee3c602016-01-07 17:12:17 +08004010#ifdef CONFIG_PM_SLEEP
4011 unregister_pm_notifier(&tp->pm_notifier);
4012#endif
Jiri Slaby0ee1f472018-06-25 09:26:27 +02004013 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
4014 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00004015 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08004016 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00004017 cancel_delayed_work_sync(&tp->schedule);
4018 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004019
4020 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004021 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004022 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004023 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004024 } else {
hayeswangb5403272014-10-09 18:00:26 +08004025 mutex_lock(&tp->control);
4026
hayeswang9a4be1b2014-02-18 21:49:07 +08004027 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004028
4029 mutex_unlock(&tp->control);
4030
hayeswang9a4be1b2014-02-18 21:49:07 +08004031 usb_autopm_put_interface(tp->intf);
4032 }
hayeswangac718b62013-05-02 16:01:25 +00004033
hayeswang7e9da482014-02-18 21:49:05 +08004034 free_all_mem(tp);
4035
hayeswangac718b62013-05-02 16:01:25 +00004036 return res;
4037}
4038
hayeswang4f1d4d52014-03-11 16:24:19 +08004039static void rtl_tally_reset(struct r8152 *tp)
4040{
4041 u32 ocp_data;
4042
4043 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4044 ocp_data |= TALLY_RESET;
4045 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4046}
4047
hayeswangac718b62013-05-02 16:01:25 +00004048static void r8152b_init(struct r8152 *tp)
4049{
hayeswangebc2ec482013-08-14 20:54:38 +08004050 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004051 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004052
hayeswang68714382014-04-11 17:54:31 +08004053 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4054 return;
4055
hayeswang2dd436d2016-09-20 16:22:06 +08004056 data = r8152_mdio_read(tp, MII_BMCR);
4057 if (data & BMCR_PDOWN) {
4058 data &= ~BMCR_PDOWN;
4059 r8152_mdio_write(tp, MII_BMCR, data);
4060 }
4061
hayeswangcda9fb02016-01-07 17:51:12 +08004062 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004063
hayeswangac718b62013-05-02 16:01:25 +00004064 if (tp->version == RTL_VER_01) {
4065 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4066 ocp_data &= ~LED_MODE_MASK;
4067 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4068 }
4069
hayeswang00a5e362014-02-18 21:48:59 +08004070 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004071
hayeswangac718b62013-05-02 16:01:25 +00004072 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4073 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4074 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4075 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4076 ocp_data &= ~MCU_CLK_RATIO_MASK;
4077 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4078 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4079 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4080 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4081 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4082
hayeswang4f1d4d52014-03-11 16:24:19 +08004083 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004084
hayeswangebc2ec482013-08-14 20:54:38 +08004085 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004086 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004087 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004088 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4089}
4090
hayeswang43779f82014-01-02 11:25:10 +08004091static void r8153_init(struct r8152 *tp)
4092{
4093 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004094 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004095 int i;
4096
hayeswang68714382014-04-11 17:54:31 +08004097 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4098 return;
4099
hayeswangb9702722014-02-18 21:49:00 +08004100 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004101
4102 for (i = 0; i < 500; i++) {
4103 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4104 AUTOLOAD_DONE)
4105 break;
4106 msleep(20);
4107 }
4108
hayeswangc564b872017-06-09 17:11:38 +08004109 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004110
hayeswang2dd436d2016-09-20 16:22:06 +08004111 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4112 tp->version == RTL_VER_05)
4113 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4114
4115 data = r8152_mdio_read(tp, MII_BMCR);
4116 if (data & BMCR_PDOWN) {
4117 data &= ~BMCR_PDOWN;
4118 r8152_mdio_write(tp, MII_BMCR, data);
4119 }
4120
hayeswangc564b872017-06-09 17:11:38 +08004121 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004122
hayeswangb9702722014-02-18 21:49:00 +08004123 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004124
hayeswang65bab842015-02-12 16:20:46 +08004125 if (tp->version == RTL_VER_04) {
4126 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4127 ocp_data &= ~pwd_dn_scale_mask;
4128 ocp_data |= pwd_dn_scale(96);
4129 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4130
4131 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4132 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4133 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4134 } else if (tp->version == RTL_VER_05) {
4135 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4136 ocp_data &= ~ECM_ALDPS;
4137 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4138
4139 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4140 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4141 ocp_data &= ~DYNAMIC_BURST;
4142 else
4143 ocp_data |= DYNAMIC_BURST;
4144 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004145 } else if (tp->version == RTL_VER_06) {
4146 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4147 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4148 ocp_data &= ~DYNAMIC_BURST;
4149 else
4150 ocp_data |= DYNAMIC_BURST;
4151 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004152 }
4153
4154 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4155 ocp_data |= EP4_FULL_FC;
4156 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4157
hayeswang43779f82014-01-02 11:25:10 +08004158 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4159 ocp_data &= ~TIMER11_EN;
4160 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4161
hayeswang43779f82014-01-02 11:25:10 +08004162 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4163 ocp_data &= ~LED_MODE_MASK;
4164 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4165
hayeswang65bab842015-02-12 16:20:46 +08004166 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004167 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004168 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004169 else
4170 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004171 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4172
4173 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4174 ocp_data &= ~SEN_VAL_MASK;
4175 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4176 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4177
hayeswang65bab842015-02-12 16:20:46 +08004178 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4179
hayeswangb9702722014-02-18 21:49:00 +08004180 r8153_power_cut_en(tp, false);
4181 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004182 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004183 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004184
hayeswange31f6362017-06-09 17:11:41 +08004185 /* rx aggregation */
4186 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4187 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004188 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4189 ocp_data |= RX_AGG_DISABLE;
4190
hayeswange31f6362017-06-09 17:11:41 +08004191 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004192
hayeswang4f1d4d52014-03-11 16:24:19 +08004193 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004194
4195 switch (tp->udev->speed) {
4196 case USB_SPEED_SUPER:
4197 case USB_SPEED_SUPER_PLUS:
4198 tp->coalesce = COALESCE_SUPER;
4199 break;
4200 case USB_SPEED_HIGH:
4201 tp->coalesce = COALESCE_HIGH;
4202 break;
4203 default:
4204 tp->coalesce = COALESCE_SLOW;
4205 break;
4206 }
hayeswang43779f82014-01-02 11:25:10 +08004207}
4208
hayeswang65b82d62017-06-15 14:44:03 +08004209static void r8153b_init(struct r8152 *tp)
4210{
4211 u32 ocp_data;
4212 u16 data;
4213 int i;
4214
4215 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4216 return;
4217
4218 r8153b_u1u2en(tp, false);
4219
4220 for (i = 0; i < 500; i++) {
4221 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4222 AUTOLOAD_DONE)
4223 break;
4224 msleep(20);
4225 }
4226
4227 data = r8153_phy_status(tp, 0);
4228
4229 data = r8152_mdio_read(tp, MII_BMCR);
4230 if (data & BMCR_PDOWN) {
4231 data &= ~BMCR_PDOWN;
4232 r8152_mdio_write(tp, MII_BMCR, data);
4233 }
4234
4235 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4236
4237 r8153_u2p3en(tp, false);
4238
4239 /* MSC timer = 0xfff * 8ms = 32760 ms */
4240 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4241
4242 /* U1/U2/L1 idle timer. 500 us */
4243 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4244
4245 r8153b_power_cut_en(tp, false);
4246 r8153b_ups_en(tp, false);
Hayes Wang13e04fbf2019-07-01 15:53:19 +08004247 r8153_queue_wake(tp, false);
hayeswang65b82d62017-06-15 14:44:03 +08004248 rtl_runtime_suspend_enable(tp, false);
4249 r8153b_u1u2en(tp, true);
4250 usb_enable_lpm(tp->udev);
4251
4252 /* MAC clock speed down */
4253 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4254 ocp_data |= MAC_CLK_SPDWN_EN;
4255 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4256
4257 set_bit(GREEN_ETHERNET, &tp->flags);
4258
4259 /* rx aggregation */
4260 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4261 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4262 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4263
4264 rtl_tally_reset(tp);
4265
4266 tp->coalesce = 15000; /* 15 us */
4267}
4268
hayeswange5011392015-07-29 20:39:08 +08004269static int rtl8152_pre_reset(struct usb_interface *intf)
4270{
4271 struct r8152 *tp = usb_get_intfdata(intf);
4272 struct net_device *netdev;
4273
4274 if (!tp)
4275 return 0;
4276
4277 netdev = tp->netdev;
4278 if (!netif_running(netdev))
4279 return 0;
4280
hayeswangde9bf292017-01-26 09:38:32 +08004281 netif_stop_queue(netdev);
hayeswange5011392015-07-29 20:39:08 +08004282 napi_disable(&tp->napi);
4283 clear_bit(WORK_ENABLE, &tp->flags);
4284 usb_kill_urb(tp->intr_urb);
4285 cancel_delayed_work_sync(&tp->schedule);
4286 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004287 mutex_lock(&tp->control);
4288 tp->rtl_ops.disable(tp);
4289 mutex_unlock(&tp->control);
4290 }
4291
4292 return 0;
4293}
4294
4295static int rtl8152_post_reset(struct usb_interface *intf)
4296{
4297 struct r8152 *tp = usb_get_intfdata(intf);
4298 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004299 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004300
4301 if (!tp)
4302 return 0;
4303
Mario Limonciello25766272019-04-04 13:46:53 -05004304 /* reset the MAC adddress in case of policy change */
4305 if (determine_ethernet_addr(tp, &sa) >= 0) {
4306 rtnl_lock();
4307 dev_set_mac_address (tp->netdev, &sa, NULL);
4308 rtnl_unlock();
4309 }
4310
hayeswange5011392015-07-29 20:39:08 +08004311 netdev = tp->netdev;
4312 if (!netif_running(netdev))
4313 return 0;
4314
4315 set_bit(WORK_ENABLE, &tp->flags);
4316 if (netif_carrier_ok(netdev)) {
4317 mutex_lock(&tp->control);
4318 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004319 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004320 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004321 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004322 }
4323
4324 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004325 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004326 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004327
hayeswang7489bda2017-01-26 09:38:34 +08004328 if (!list_empty(&tp->rx_done))
4329 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004330
4331 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004332}
4333
hayeswang2dd49e02015-09-07 11:57:44 +08004334static bool delay_autosuspend(struct r8152 *tp)
4335{
4336 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4337 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4338
4339 /* This means a linking change occurs and the driver doesn't detect it,
4340 * yet. If the driver has disabled tx/rx and hw is linking on, the
4341 * device wouldn't wake up by receiving any packet.
4342 */
4343 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4344 return true;
4345
4346 /* If the linking down is occurred by nway, the device may miss the
4347 * linking change event. And it wouldn't wake when linking on.
4348 */
4349 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4350 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004351 else if (!skb_queue_empty(&tp->tx_queue))
4352 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004353 else
4354 return false;
4355}
4356
hayeswang21cbd0e2017-06-13 15:14:39 +08004357static int rtl8152_runtime_resume(struct r8152 *tp)
4358{
4359 struct net_device *netdev = tp->netdev;
4360
4361 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4362 struct napi_struct *napi = &tp->napi;
4363
4364 tp->rtl_ops.autosuspend_en(tp, false);
4365 napi_disable(napi);
4366 set_bit(WORK_ENABLE, &tp->flags);
4367
4368 if (netif_carrier_ok(netdev)) {
4369 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4370 rtl_start_rx(tp);
4371 } else {
4372 netif_carrier_off(netdev);
4373 tp->rtl_ops.disable(tp);
4374 netif_info(tp, link, netdev, "linking down\n");
4375 }
4376 }
4377
4378 napi_enable(napi);
4379 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4380 smp_mb__after_atomic();
4381
4382 if (!list_empty(&tp->rx_done))
4383 napi_schedule(&tp->napi);
4384
4385 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4386 } else {
4387 if (netdev->flags & IFF_UP)
4388 tp->rtl_ops.autosuspend_en(tp, false);
4389
4390 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4391 }
4392
4393 return 0;
4394}
4395
4396static int rtl8152_system_resume(struct r8152 *tp)
4397{
4398 struct net_device *netdev = tp->netdev;
4399
4400 netif_device_attach(netdev);
4401
4402 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4403 tp->rtl_ops.up(tp);
4404 netif_carrier_off(netdev);
4405 set_bit(WORK_ENABLE, &tp->flags);
4406 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4407 }
4408
4409 return 0;
4410}
4411
hayeswanga9c54ad2017-01-25 13:41:45 +08004412static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004413{
hayeswang6cc69f22014-10-17 16:55:08 +08004414 struct net_device *netdev = tp->netdev;
4415 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004416
hayeswang26afec32017-01-26 09:38:31 +08004417 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4418 smp_mb__after_atomic();
4419
hayeswang8fb28062017-01-10 17:04:06 +08004420 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004421 u32 rcr = 0;
4422
hayeswang75dc6922017-01-10 17:04:07 +08004423 if (netif_carrier_ok(netdev)) {
4424 u32 ocp_data;
4425
4426 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4427 ocp_data = rcr & ~RCR_ACPT_ALL;
4428 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4429 rxdy_gated_en(tp, true);
4430 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4431 PLA_OOB_CTRL);
4432 if (!(ocp_data & RXFIFO_EMPTY)) {
4433 rxdy_gated_en(tp, false);
4434 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004435 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4436 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004437 ret = -EBUSY;
4438 goto out1;
4439 }
4440 }
4441
hayeswang8fb28062017-01-10 17:04:06 +08004442 clear_bit(WORK_ENABLE, &tp->flags);
4443 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004444
hayeswang8fb28062017-01-10 17:04:06 +08004445 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004446
4447 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004448 struct napi_struct *napi = &tp->napi;
4449
4450 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004451 rtl_stop_rx(tp);
4452 rxdy_gated_en(tp, false);
4453 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004454 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004455 }
hayeswangbd882982017-06-13 15:14:40 +08004456
4457 if (delay_autosuspend(tp)) {
4458 rtl8152_runtime_resume(tp);
4459 ret = -EBUSY;
4460 }
hayeswang6cc69f22014-10-17 16:55:08 +08004461 }
4462
hayeswang8fb28062017-01-10 17:04:06 +08004463out1:
4464 return ret;
4465}
4466
4467static int rtl8152_system_suspend(struct r8152 *tp)
4468{
4469 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004470
4471 netif_device_detach(netdev);
4472
hayeswange3bd1a82014-10-29 11:12:17 +08004473 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004474 struct napi_struct *napi = &tp->napi;
4475
hayeswangac718b62013-05-02 16:01:25 +00004476 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004477 usb_kill_urb(tp->intr_urb);
hayeswangce594e92017-03-16 14:32:22 +08004478 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004479 cancel_delayed_work_sync(&tp->schedule);
4480 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004481 napi_enable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004482 }
hayeswang8fb28062017-01-10 17:04:06 +08004483
zhong jiangf7419172018-08-09 09:39:13 +08004484 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004485}
4486
4487static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4488{
4489 struct r8152 *tp = usb_get_intfdata(intf);
4490 int ret;
4491
4492 mutex_lock(&tp->control);
4493
4494 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004495 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004496 else
4497 ret = rtl8152_system_suspend(tp);
4498
hayeswangb5403272014-10-09 18:00:26 +08004499 mutex_unlock(&tp->control);
4500
hayeswang6cc69f22014-10-17 16:55:08 +08004501 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004502}
4503
4504static int rtl8152_resume(struct usb_interface *intf)
4505{
4506 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004507 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004508
hayeswangb5403272014-10-09 18:00:26 +08004509 mutex_lock(&tp->control);
4510
hayeswang21cbd0e2017-06-13 15:14:39 +08004511 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4512 ret = rtl8152_runtime_resume(tp);
4513 else
4514 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004515
hayeswangb5403272014-10-09 18:00:26 +08004516 mutex_unlock(&tp->control);
4517
hayeswang21cbd0e2017-06-13 15:14:39 +08004518 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004519}
4520
hayeswang7ec25412016-01-04 14:38:46 +08004521static int rtl8152_reset_resume(struct usb_interface *intf)
4522{
4523 struct r8152 *tp = usb_get_intfdata(intf);
4524
4525 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004526 mutex_lock(&tp->control);
4527 tp->rtl_ops.init(tp);
4528 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4529 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004530 return rtl8152_resume(intf);
4531}
4532
hayeswang21ff2e82014-02-18 21:49:06 +08004533static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4534{
4535 struct r8152 *tp = netdev_priv(dev);
4536
hayeswang9a4be1b2014-02-18 21:49:07 +08004537 if (usb_autopm_get_interface(tp->intf) < 0)
4538 return;
4539
hayeswang7daed8d2015-07-24 13:54:24 +08004540 if (!rtl_can_wakeup(tp)) {
4541 wol->supported = 0;
4542 wol->wolopts = 0;
4543 } else {
4544 mutex_lock(&tp->control);
4545 wol->supported = WAKE_ANY;
4546 wol->wolopts = __rtl_get_wol(tp);
4547 mutex_unlock(&tp->control);
4548 }
hayeswangb5403272014-10-09 18:00:26 +08004549
hayeswang9a4be1b2014-02-18 21:49:07 +08004550 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004551}
4552
4553static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4554{
4555 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004556 int ret;
4557
hayeswang7daed8d2015-07-24 13:54:24 +08004558 if (!rtl_can_wakeup(tp))
4559 return -EOPNOTSUPP;
4560
Florian Fainellif2750df2018-09-28 16:18:54 -07004561 if (wol->wolopts & ~WAKE_ANY)
4562 return -EINVAL;
4563
hayeswang9a4be1b2014-02-18 21:49:07 +08004564 ret = usb_autopm_get_interface(tp->intf);
4565 if (ret < 0)
4566 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004567
hayeswangb5403272014-10-09 18:00:26 +08004568 mutex_lock(&tp->control);
4569
hayeswang21ff2e82014-02-18 21:49:06 +08004570 __rtl_set_wol(tp, wol->wolopts);
4571 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4572
hayeswangb5403272014-10-09 18:00:26 +08004573 mutex_unlock(&tp->control);
4574
hayeswang9a4be1b2014-02-18 21:49:07 +08004575 usb_autopm_put_interface(tp->intf);
4576
4577out_set_wol:
4578 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004579}
4580
hayeswanga5ec27c2014-02-18 21:49:11 +08004581static u32 rtl8152_get_msglevel(struct net_device *dev)
4582{
4583 struct r8152 *tp = netdev_priv(dev);
4584
4585 return tp->msg_enable;
4586}
4587
4588static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4589{
4590 struct r8152 *tp = netdev_priv(dev);
4591
4592 tp->msg_enable = value;
4593}
4594
hayeswangac718b62013-05-02 16:01:25 +00004595static void rtl8152_get_drvinfo(struct net_device *netdev,
4596 struct ethtool_drvinfo *info)
4597{
4598 struct r8152 *tp = netdev_priv(netdev);
4599
hayeswangb0b46c72014-08-26 10:08:23 +08004600 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4601 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004602 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4603}
4604
4605static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004606int rtl8152_get_link_ksettings(struct net_device *netdev,
4607 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004608{
4609 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004610 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004611
4612 if (!tp->mii.mdio_read)
4613 return -EOPNOTSUPP;
4614
hayeswang8d4a4d72014-10-09 18:00:25 +08004615 ret = usb_autopm_get_interface(tp->intf);
4616 if (ret < 0)
4617 goto out;
4618
hayeswangb5403272014-10-09 18:00:26 +08004619 mutex_lock(&tp->control);
4620
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004621 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004622
hayeswangb5403272014-10-09 18:00:26 +08004623 mutex_unlock(&tp->control);
4624
hayeswang8d4a4d72014-10-09 18:00:25 +08004625 usb_autopm_put_interface(tp->intf);
4626
4627out:
4628 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004629}
4630
Philippe Reynes06144dc2017-03-12 22:41:58 +01004631static int rtl8152_set_link_ksettings(struct net_device *dev,
4632 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004633{
4634 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004635 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004636
hayeswang9a4be1b2014-02-18 21:49:07 +08004637 ret = usb_autopm_get_interface(tp->intf);
4638 if (ret < 0)
4639 goto out;
4640
hayeswangb5403272014-10-09 18:00:26 +08004641 mutex_lock(&tp->control);
4642
Philippe Reynes06144dc2017-03-12 22:41:58 +01004643 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4644 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004645 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004646 tp->autoneg = cmd->base.autoneg;
4647 tp->speed = cmd->base.speed;
4648 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004649 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004650
hayeswangb5403272014-10-09 18:00:26 +08004651 mutex_unlock(&tp->control);
4652
hayeswang9a4be1b2014-02-18 21:49:07 +08004653 usb_autopm_put_interface(tp->intf);
4654
4655out:
4656 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004657}
4658
hayeswang4f1d4d52014-03-11 16:24:19 +08004659static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4660 "tx_packets",
4661 "rx_packets",
4662 "tx_errors",
4663 "rx_errors",
4664 "rx_missed",
4665 "align_errors",
4666 "tx_single_collisions",
4667 "tx_multi_collisions",
4668 "rx_unicast",
4669 "rx_broadcast",
4670 "rx_multicast",
4671 "tx_aborted",
4672 "tx_underrun",
4673};
4674
4675static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4676{
4677 switch (sset) {
4678 case ETH_SS_STATS:
4679 return ARRAY_SIZE(rtl8152_gstrings);
4680 default:
4681 return -EOPNOTSUPP;
4682 }
4683}
4684
4685static void rtl8152_get_ethtool_stats(struct net_device *dev,
4686 struct ethtool_stats *stats, u64 *data)
4687{
4688 struct r8152 *tp = netdev_priv(dev);
4689 struct tally_counter tally;
4690
hayeswang0b030242014-07-08 14:49:28 +08004691 if (usb_autopm_get_interface(tp->intf) < 0)
4692 return;
4693
hayeswang4f1d4d52014-03-11 16:24:19 +08004694 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4695
hayeswang0b030242014-07-08 14:49:28 +08004696 usb_autopm_put_interface(tp->intf);
4697
hayeswang4f1d4d52014-03-11 16:24:19 +08004698 data[0] = le64_to_cpu(tally.tx_packets);
4699 data[1] = le64_to_cpu(tally.rx_packets);
4700 data[2] = le64_to_cpu(tally.tx_errors);
4701 data[3] = le32_to_cpu(tally.rx_errors);
4702 data[4] = le16_to_cpu(tally.rx_missed);
4703 data[5] = le16_to_cpu(tally.align_errors);
4704 data[6] = le32_to_cpu(tally.tx_one_collision);
4705 data[7] = le32_to_cpu(tally.tx_multi_collision);
4706 data[8] = le64_to_cpu(tally.rx_unicast);
4707 data[9] = le64_to_cpu(tally.rx_broadcast);
4708 data[10] = le32_to_cpu(tally.rx_multicast);
4709 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004710 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004711}
4712
4713static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4714{
4715 switch (stringset) {
4716 case ETH_SS_STATS:
4717 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4718 break;
4719 }
4720}
4721
hayeswangdf35d282014-09-25 20:54:02 +08004722static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4723{
4724 u32 ocp_data, lp, adv, supported = 0;
4725 u16 val;
4726
4727 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4728 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4729
4730 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4731 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4732
4733 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4734 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4735
4736 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4737 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4738
4739 eee->eee_enabled = !!ocp_data;
4740 eee->eee_active = !!(supported & adv & lp);
4741 eee->supported = supported;
4742 eee->advertised = adv;
4743 eee->lp_advertised = lp;
4744
4745 return 0;
4746}
4747
4748static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4749{
4750 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4751
4752 r8152_eee_en(tp, eee->eee_enabled);
4753
4754 if (!eee->eee_enabled)
4755 val = 0;
4756
4757 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4758
4759 return 0;
4760}
4761
4762static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4763{
4764 u32 ocp_data, lp, adv, supported = 0;
4765 u16 val;
4766
4767 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4768 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4769
4770 val = ocp_reg_read(tp, OCP_EEE_ADV);
4771 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4772
4773 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4774 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4775
4776 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4777 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4778
4779 eee->eee_enabled = !!ocp_data;
4780 eee->eee_active = !!(supported & adv & lp);
4781 eee->supported = supported;
4782 eee->advertised = adv;
4783 eee->lp_advertised = lp;
4784
4785 return 0;
4786}
4787
4788static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4789{
4790 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4791
4792 r8153_eee_en(tp, eee->eee_enabled);
4793
4794 if (!eee->eee_enabled)
4795 val = 0;
4796
4797 ocp_reg_write(tp, OCP_EEE_ADV, val);
4798
4799 return 0;
4800}
4801
hayeswang65b82d62017-06-15 14:44:03 +08004802static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4803{
4804 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4805
4806 r8153b_eee_en(tp, eee->eee_enabled);
4807
4808 if (!eee->eee_enabled)
4809 val = 0;
4810
4811 ocp_reg_write(tp, OCP_EEE_ADV, val);
4812
4813 return 0;
4814}
4815
hayeswangdf35d282014-09-25 20:54:02 +08004816static int
4817rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4818{
4819 struct r8152 *tp = netdev_priv(net);
4820 int ret;
4821
4822 ret = usb_autopm_get_interface(tp->intf);
4823 if (ret < 0)
4824 goto out;
4825
hayeswangb5403272014-10-09 18:00:26 +08004826 mutex_lock(&tp->control);
4827
hayeswangdf35d282014-09-25 20:54:02 +08004828 ret = tp->rtl_ops.eee_get(tp, edata);
4829
hayeswangb5403272014-10-09 18:00:26 +08004830 mutex_unlock(&tp->control);
4831
hayeswangdf35d282014-09-25 20:54:02 +08004832 usb_autopm_put_interface(tp->intf);
4833
4834out:
4835 return ret;
4836}
4837
4838static int
4839rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
4840{
4841 struct r8152 *tp = netdev_priv(net);
4842 int ret;
4843
4844 ret = usb_autopm_get_interface(tp->intf);
4845 if (ret < 0)
4846 goto out;
4847
hayeswangb5403272014-10-09 18:00:26 +08004848 mutex_lock(&tp->control);
4849
hayeswangdf35d282014-09-25 20:54:02 +08004850 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08004851 if (!ret)
4852 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08004853
hayeswangb5403272014-10-09 18:00:26 +08004854 mutex_unlock(&tp->control);
4855
hayeswangdf35d282014-09-25 20:54:02 +08004856 usb_autopm_put_interface(tp->intf);
4857
4858out:
4859 return ret;
4860}
4861
hayeswang8884f502014-10-28 14:05:52 +08004862static int rtl8152_nway_reset(struct net_device *dev)
4863{
4864 struct r8152 *tp = netdev_priv(dev);
4865 int ret;
4866
4867 ret = usb_autopm_get_interface(tp->intf);
4868 if (ret < 0)
4869 goto out;
4870
4871 mutex_lock(&tp->control);
4872
4873 ret = mii_nway_restart(&tp->mii);
4874
4875 mutex_unlock(&tp->control);
4876
4877 usb_autopm_put_interface(tp->intf);
4878
4879out:
4880 return ret;
4881}
4882
hayeswangefb3dd82015-02-12 14:33:48 +08004883static int rtl8152_get_coalesce(struct net_device *netdev,
4884 struct ethtool_coalesce *coalesce)
4885{
4886 struct r8152 *tp = netdev_priv(netdev);
4887
4888 switch (tp->version) {
4889 case RTL_VER_01:
4890 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004891 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004892 return -EOPNOTSUPP;
4893 default:
4894 break;
4895 }
4896
4897 coalesce->rx_coalesce_usecs = tp->coalesce;
4898
4899 return 0;
4900}
4901
4902static int rtl8152_set_coalesce(struct net_device *netdev,
4903 struct ethtool_coalesce *coalesce)
4904{
4905 struct r8152 *tp = netdev_priv(netdev);
4906 int ret;
4907
4908 switch (tp->version) {
4909 case RTL_VER_01:
4910 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004911 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004912 return -EOPNOTSUPP;
4913 default:
4914 break;
4915 }
4916
4917 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
4918 return -EINVAL;
4919
4920 ret = usb_autopm_get_interface(tp->intf);
4921 if (ret < 0)
4922 return ret;
4923
4924 mutex_lock(&tp->control);
4925
4926 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
4927 tp->coalesce = coalesce->rx_coalesce_usecs;
4928
Hayes Wang9fae5412019-07-03 15:11:56 +08004929 if (netif_running(netdev) && netif_carrier_ok(netdev)) {
4930 netif_stop_queue(netdev);
4931 napi_disable(&tp->napi);
4932 tp->rtl_ops.disable(tp);
4933 tp->rtl_ops.enable(tp);
4934 rtl_start_rx(tp);
4935 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
4936 _rtl8152_set_rx_mode(netdev);
4937 napi_enable(&tp->napi);
4938 netif_wake_queue(netdev);
4939 }
hayeswangefb3dd82015-02-12 14:33:48 +08004940 }
4941
4942 mutex_unlock(&tp->control);
4943
4944 usb_autopm_put_interface(tp->intf);
4945
4946 return ret;
4947}
4948
Julia Lawall407a4712016-09-01 00:21:22 +02004949static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00004950 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00004951 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08004952 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08004953 .get_msglevel = rtl8152_get_msglevel,
4954 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08004955 .get_wol = rtl8152_get_wol,
4956 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08004957 .get_strings = rtl8152_get_strings,
4958 .get_sset_count = rtl8152_get_sset_count,
4959 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08004960 .get_coalesce = rtl8152_get_coalesce,
4961 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08004962 .get_eee = rtl_ethtool_get_eee,
4963 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01004964 .get_link_ksettings = rtl8152_get_link_ksettings,
4965 .set_link_ksettings = rtl8152_set_link_ksettings,
hayeswangac718b62013-05-02 16:01:25 +00004966};
4967
4968static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
4969{
4970 struct r8152 *tp = netdev_priv(netdev);
4971 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08004972 int res;
4973
hayeswang68714382014-04-11 17:54:31 +08004974 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4975 return -ENODEV;
4976
hayeswang9a4be1b2014-02-18 21:49:07 +08004977 res = usb_autopm_get_interface(tp->intf);
4978 if (res < 0)
4979 goto out;
hayeswangac718b62013-05-02 16:01:25 +00004980
4981 switch (cmd) {
4982 case SIOCGMIIPHY:
4983 data->phy_id = R8152_PHY_ID; /* Internal PHY */
4984 break;
4985
4986 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08004987 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004988 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08004989 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004990 break;
4991
4992 case SIOCSMIIREG:
4993 if (!capable(CAP_NET_ADMIN)) {
4994 res = -EPERM;
4995 break;
4996 }
hayeswangb5403272014-10-09 18:00:26 +08004997 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004998 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08004999 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005000 break;
5001
5002 default:
5003 res = -EOPNOTSUPP;
5004 }
5005
hayeswang9a4be1b2014-02-18 21:49:07 +08005006 usb_autopm_put_interface(tp->intf);
5007
5008out:
hayeswangac718b62013-05-02 16:01:25 +00005009 return res;
5010}
5011
hayeswang69b4b7a2014-07-10 10:58:54 +08005012static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
5013{
5014 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08005015 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005016
5017 switch (tp->version) {
5018 case RTL_VER_01:
5019 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005020 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04005021 dev->mtu = new_mtu;
5022 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08005023 default:
5024 break;
5025 }
5026
hayeswang396e2e22015-02-12 14:33:47 +08005027 ret = usb_autopm_get_interface(tp->intf);
5028 if (ret < 0)
5029 return ret;
5030
5031 mutex_lock(&tp->control);
5032
hayeswang69b4b7a2014-07-10 10:58:54 +08005033 dev->mtu = new_mtu;
5034
hayeswang210c4f72017-03-20 16:13:44 +08005035 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005036 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005037
5038 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5039
5040 if (netif_carrier_ok(dev))
5041 r8153_set_rx_early_size(tp);
5042 }
hayeswang396e2e22015-02-12 14:33:47 +08005043
5044 mutex_unlock(&tp->control);
5045
5046 usb_autopm_put_interface(tp->intf);
5047
5048 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005049}
5050
hayeswangac718b62013-05-02 16:01:25 +00005051static const struct net_device_ops rtl8152_netdev_ops = {
5052 .ndo_open = rtl8152_open,
5053 .ndo_stop = rtl8152_close,
5054 .ndo_do_ioctl = rtl8152_ioctl,
5055 .ndo_start_xmit = rtl8152_start_xmit,
5056 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005057 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005058 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5059 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005060 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005061 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005062 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005063};
5064
hayeswange3fe0b12014-01-02 11:22:39 +08005065static void rtl8152_unload(struct r8152 *tp)
5066{
hayeswang68714382014-04-11 17:54:31 +08005067 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5068 return;
5069
hayeswang00a5e362014-02-18 21:48:59 +08005070 if (tp->version != RTL_VER_01)
5071 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005072}
5073
hayeswang43779f82014-01-02 11:25:10 +08005074static void rtl8153_unload(struct r8152 *tp)
5075{
hayeswang68714382014-04-11 17:54:31 +08005076 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5077 return;
5078
hayeswang49be1722014-10-01 13:25:11 +08005079 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005080}
5081
hayeswang65b82d62017-06-15 14:44:03 +08005082static void rtl8153b_unload(struct r8152 *tp)
5083{
5084 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5085 return;
5086
5087 r8153b_power_cut_en(tp, false);
5088}
5089
hayeswang55b65472014-11-06 12:47:39 +08005090static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005091{
5092 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005093 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005094
hayeswang55b65472014-11-06 12:47:39 +08005095 switch (tp->version) {
5096 case RTL_VER_01:
5097 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005098 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005099 ops->init = r8152b_init;
5100 ops->enable = rtl8152_enable;
5101 ops->disable = rtl8152_disable;
5102 ops->up = rtl8152_up;
5103 ops->down = rtl8152_down;
5104 ops->unload = rtl8152_unload;
5105 ops->eee_get = r8152_get_eee;
5106 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005107 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005108 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005109 ops->autosuspend_en = rtl_runtime_suspend_enable;
hayeswang43779f82014-01-02 11:25:10 +08005110 break;
5111
hayeswang55b65472014-11-06 12:47:39 +08005112 case RTL_VER_03:
5113 case RTL_VER_04:
5114 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005115 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005116 ops->init = r8153_init;
5117 ops->enable = rtl8153_enable;
5118 ops->disable = rtl8153_disable;
5119 ops->up = rtl8153_up;
5120 ops->down = rtl8153_down;
5121 ops->unload = rtl8153_unload;
5122 ops->eee_get = r8153_get_eee;
5123 ops->eee_set = r8153_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005124 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005125 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005126 ops->autosuspend_en = rtl8153_runtime_enable;
hayeswangc81229c2014-01-02 11:22:42 +08005127 break;
5128
hayeswang65b82d62017-06-15 14:44:03 +08005129 case RTL_VER_08:
5130 case RTL_VER_09:
5131 ops->init = r8153b_init;
5132 ops->enable = rtl8153_enable;
5133 ops->disable = rtl8153b_disable;
5134 ops->up = rtl8153b_up;
5135 ops->down = rtl8153b_down;
5136 ops->unload = rtl8153b_unload;
5137 ops->eee_get = r8153_get_eee;
5138 ops->eee_set = r8153b_set_eee;
5139 ops->in_nway = rtl8153_in_nway;
5140 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5141 ops->autosuspend_en = rtl8153b_runtime_enable;
5142 break;
5143
hayeswangc81229c2014-01-02 11:22:42 +08005144 default:
hayeswang55b65472014-11-06 12:47:39 +08005145 ret = -ENODEV;
5146 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005147 break;
5148 }
5149
5150 return ret;
5151}
5152
hayeswang33928ee2017-03-17 11:20:13 +08005153static u8 rtl_get_version(struct usb_interface *intf)
5154{
5155 struct usb_device *udev = interface_to_usbdev(intf);
5156 u32 ocp_data = 0;
5157 __le32 *tmp;
5158 u8 version;
5159 int ret;
5160
5161 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5162 if (!tmp)
5163 return 0;
5164
5165 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5166 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5167 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5168 if (ret > 0)
5169 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5170
5171 kfree(tmp);
5172
5173 switch (ocp_data) {
5174 case 0x4c00:
5175 version = RTL_VER_01;
5176 break;
5177 case 0x4c10:
5178 version = RTL_VER_02;
5179 break;
5180 case 0x5c00:
5181 version = RTL_VER_03;
5182 break;
5183 case 0x5c10:
5184 version = RTL_VER_04;
5185 break;
5186 case 0x5c20:
5187 version = RTL_VER_05;
5188 break;
5189 case 0x5c30:
5190 version = RTL_VER_06;
5191 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005192 case 0x4800:
5193 version = RTL_VER_07;
5194 break;
hayeswang65b82d62017-06-15 14:44:03 +08005195 case 0x6000:
5196 version = RTL_VER_08;
5197 break;
5198 case 0x6010:
5199 version = RTL_VER_09;
5200 break;
hayeswang33928ee2017-03-17 11:20:13 +08005201 default:
5202 version = RTL_VER_UNKNOWN;
5203 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5204 break;
5205 }
5206
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005207 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5208
hayeswang33928ee2017-03-17 11:20:13 +08005209 return version;
5210}
5211
hayeswangac718b62013-05-02 16:01:25 +00005212static int rtl8152_probe(struct usb_interface *intf,
5213 const struct usb_device_id *id)
5214{
5215 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005216 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005217 struct r8152 *tp;
5218 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005219 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005220
hayeswang33928ee2017-03-17 11:20:13 +08005221 if (version == RTL_VER_UNKNOWN)
5222 return -ENODEV;
5223
hayeswang10c32712014-03-04 20:47:48 +08005224 if (udev->actconfig->desc.bConfigurationValue != 1) {
5225 usb_driver_set_configuration(udev, 1);
5226 return -ENODEV;
5227 }
5228
5229 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005230 netdev = alloc_etherdev(sizeof(struct r8152));
5231 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005232 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005233 return -ENOMEM;
5234 }
5235
hayeswangebc2ec482013-08-14 20:54:38 +08005236 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005237 tp = netdev_priv(netdev);
5238 tp->msg_enable = 0x7FFF;
5239
hayeswange3ad4122014-01-06 17:08:42 +08005240 tp->udev = udev;
5241 tp->netdev = netdev;
5242 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005243 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005244
hayeswang33928ee2017-03-17 11:20:13 +08005245 switch (version) {
5246 case RTL_VER_01:
5247 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005248 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005249 tp->mii.supports_gmii = 0;
5250 break;
5251 default:
5252 tp->mii.supports_gmii = 1;
5253 break;
5254 }
5255
hayeswang55b65472014-11-06 12:47:39 +08005256 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005257 if (ret)
5258 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005259
hayeswangb5403272014-10-09 18:00:26 +08005260 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005261 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005262 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
hayeswangac718b62013-05-02 16:01:25 +00005263
hayeswangac718b62013-05-02 16:01:25 +00005264 netdev->netdev_ops = &rtl8152_netdev_ops;
5265 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005266
hayeswang60c89072014-03-07 11:04:39 +08005267 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005268 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005269 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5270 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005271 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005272 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005273 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005274 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005275 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5276 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5277 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005278
hayeswang19c0f402017-01-11 16:25:34 +08005279 if (tp->version == RTL_VER_01) {
5280 netdev->features &= ~NETIF_F_RXCSUM;
5281 netdev->hw_features &= ~NETIF_F_RXCSUM;
5282 }
5283
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005284 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5285 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005286 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5287 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5288 }
5289
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005290 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005291 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005292
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005293 /* MTU range: 68 - 1500 or 9194 */
5294 netdev->min_mtu = ETH_MIN_MTU;
5295 switch (tp->version) {
5296 case RTL_VER_01:
5297 case RTL_VER_02:
5298 netdev->max_mtu = ETH_DATA_LEN;
5299 break;
5300 default:
5301 netdev->max_mtu = RTL8153_MAX_MTU;
5302 break;
5303 }
5304
hayeswangac718b62013-05-02 16:01:25 +00005305 tp->mii.dev = netdev;
5306 tp->mii.mdio_read = read_mii_word;
5307 tp->mii.mdio_write = write_mii_word;
5308 tp->mii.phy_id_mask = 0x3f;
5309 tp->mii.reg_num_mask = 0x1f;
5310 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005311
hayeswangaa7e26b2016-06-13 17:49:38 +08005312 tp->autoneg = AUTONEG_ENABLE;
5313 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5314 tp->duplex = DUPLEX_FULL;
5315
hayeswang9a4be1b2014-02-18 21:49:07 +08005316 intf->needs_remote_wakeup = 1;
5317
hayeswangc81229c2014-01-02 11:22:42 +08005318 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005319 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005320 set_ethernet_addr(tp);
5321
hayeswangac718b62013-05-02 16:01:25 +00005322 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005323 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005324
hayeswangebc2ec482013-08-14 20:54:38 +08005325 ret = register_netdev(netdev);
5326 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005327 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005328 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005329 }
5330
hayeswang7daed8d2015-07-24 13:54:24 +08005331 if (!rtl_can_wakeup(tp))
5332 __rtl_set_wol(tp, 0);
5333
hayeswang21ff2e82014-02-18 21:49:06 +08005334 tp->saved_wolopts = __rtl_get_wol(tp);
5335 if (tp->saved_wolopts)
5336 device_set_wakeup_enable(&udev->dev, true);
5337 else
5338 device_set_wakeup_enable(&udev->dev, false);
5339
Hayes Wang4a8deae2014-01-07 11:18:22 +08005340 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005341
5342 return 0;
5343
hayeswangac718b62013-05-02 16:01:25 +00005344out1:
hayeswangd823ab62015-01-12 12:06:23 +08005345 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08005346 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005347out:
5348 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005349 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005350}
5351
hayeswangac718b62013-05-02 16:01:25 +00005352static void rtl8152_disconnect(struct usb_interface *intf)
5353{
5354 struct r8152 *tp = usb_get_intfdata(intf);
5355
5356 usb_set_intfdata(intf, NULL);
5357 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08005358 struct usb_device *udev = tp->udev;
5359
5360 if (udev->state == USB_STATE_NOTATTACHED)
5361 set_bit(RTL8152_UNPLUG, &tp->flags);
5362
hayeswangd823ab62015-01-12 12:06:23 +08005363 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005364 unregister_netdev(tp->netdev);
hayeswanga028a9e2016-06-13 17:49:36 +08005365 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005366 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005367 free_netdev(tp->netdev);
5368 }
5369}
5370
hayeswangd9a28c52014-12-04 10:43:11 +08005371#define REALTEK_USB_DEVICE(vend, prod) \
5372 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5373 USB_DEVICE_ID_MATCH_INT_CLASS, \
5374 .idVendor = (vend), \
5375 .idProduct = (prod), \
5376 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5377}, \
5378{ \
5379 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5380 USB_DEVICE_ID_MATCH_DEVICE, \
5381 .idVendor = (vend), \
5382 .idProduct = (prod), \
5383 .bInterfaceClass = USB_CLASS_COMM, \
5384 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5385 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5386
hayeswangac718b62013-05-02 16:01:25 +00005387/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305388static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005389 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005390 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5391 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005392 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5393 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005394 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005395 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005396 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5397 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5398 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5399 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5400 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005401 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005402 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005403 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005404 {}
5405};
5406
5407MODULE_DEVICE_TABLE(usb, rtl8152_table);
5408
5409static struct usb_driver rtl8152_driver = {
5410 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005411 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005412 .probe = rtl8152_probe,
5413 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005414 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005415 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005416 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005417 .pre_reset = rtl8152_pre_reset,
5418 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005419 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005420 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005421};
5422
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005423module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005424
5425MODULE_AUTHOR(DRIVER_AUTHOR);
5426MODULE_DESCRIPTION(DRIVER_DESC);
5427MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005428MODULE_VERSION(DRIVER_VERSION);