blob: b01bfa63860dbc9c305cdbe51520951eb97af9d9 [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
hayeswangc7de7de2014-01-15 10:42:16 +08002 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswangac718b62013-05-02 16:01:25 +00003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 *
8 */
9
hayeswangac718b62013-05-02 16:01:25 +000010#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
16#include <linux/ethtool.h>
17#include <linux/usb.h>
18#include <linux/crc32.h>
19#include <linux/if_vlan.h>
20#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080021#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080022#include <linux/ip.h>
23#include <linux/ipv6.h>
hayeswang6128d1bb2014-03-07 11:04:40 +080024#include <net/ip6_checksum.h>
hayeswang4c4a6b12014-09-25 20:54:00 +080025#include <uapi/linux/mdio.h>
26#include <linux/mdio.h>
hayeswangd9a28c52014-12-04 10:43:11 +080027#include <linux/usb/cdc.h>
hayeswang5ee3c602016-01-07 17:12:17 +080028#include <linux/suspend.h>
Mario Limonciello34ee32c2016-07-11 19:58:04 -050029#include <linux/acpi.h>
hayeswangac718b62013-05-02 16:01:25 +000030
hayeswangd0942472015-09-07 11:57:43 +080031/* Information for net-next */
hayeswang65b82d62017-06-15 14:44:03 +080032#define NETNEXT_VERSION "09"
hayeswangd0942472015-09-07 11:57:43 +080033
34/* Information for net */
hayeswangb20cb602017-03-20 16:13:45 +080035#define NET_VERSION "9"
hayeswangd0942472015-09-07 11:57:43 +080036
37#define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
hayeswangac718b62013-05-02 16:01:25 +000038#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang44d942a2014-01-15 10:42:14 +080039#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswangac718b62013-05-02 16:01:25 +000040#define MODULENAME "r8152"
41
42#define R8152_PHY_ID 32
43
44#define PLA_IDR 0xc000
45#define PLA_RCR 0xc010
46#define PLA_RMS 0xc016
47#define PLA_RXFIFO_CTRL0 0xc0a0
48#define PLA_RXFIFO_CTRL1 0xc0a4
49#define PLA_RXFIFO_CTRL2 0xc0a8
hayeswang65bab842015-02-12 16:20:46 +080050#define PLA_DMY_REG0 0xc0b0
hayeswangac718b62013-05-02 16:01:25 +000051#define PLA_FMC 0xc0b4
52#define PLA_CFG_WOL 0xc0b6
hayeswang43779f82014-01-02 11:25:10 +080053#define PLA_TEREDO_CFG 0xc0bc
hayeswang65b82d62017-06-15 14:44:03 +080054#define PLA_TEREDO_WAKE_BASE 0xc0c4
hayeswangac718b62013-05-02 16:01:25 +000055#define PLA_MAR 0xcd00
hayeswang43779f82014-01-02 11:25:10 +080056#define PLA_BACKUP 0xd000
hayeswangac718b62013-05-02 16:01:25 +000057#define PAL_BDC_CR 0xd1a0
hayeswang43779f82014-01-02 11:25:10 +080058#define PLA_TEREDO_TIMER 0xd2cc
59#define PLA_REALWOW_TIMER 0xd2e8
hayeswang65b82d62017-06-15 14:44:03 +080060#define PLA_EFUSE_DATA 0xdd00
61#define PLA_EFUSE_CMD 0xdd02
hayeswangac718b62013-05-02 16:01:25 +000062#define PLA_LEDSEL 0xdd90
63#define PLA_LED_FEATURE 0xdd92
64#define PLA_PHYAR 0xde00
hayeswang43779f82014-01-02 11:25:10 +080065#define PLA_BOOT_CTRL 0xe004
hayeswangac718b62013-05-02 16:01:25 +000066#define PLA_GPHY_INTR_IMR 0xe022
67#define PLA_EEE_CR 0xe040
68#define PLA_EEEP_CR 0xe080
69#define PLA_MAC_PWR_CTRL 0xe0c0
hayeswang43779f82014-01-02 11:25:10 +080070#define PLA_MAC_PWR_CTRL2 0xe0ca
71#define PLA_MAC_PWR_CTRL3 0xe0cc
72#define PLA_MAC_PWR_CTRL4 0xe0ce
73#define PLA_WDT6_CTRL 0xe428
hayeswangac718b62013-05-02 16:01:25 +000074#define PLA_TCR0 0xe610
75#define PLA_TCR1 0xe612
hayeswang69b4b7a2014-07-10 10:58:54 +080076#define PLA_MTPS 0xe615
hayeswangac718b62013-05-02 16:01:25 +000077#define PLA_TXFIFO_CTRL 0xe618
hayeswang4f1d4d52014-03-11 16:24:19 +080078#define PLA_RSTTALLY 0xe800
hayeswangac718b62013-05-02 16:01:25 +000079#define PLA_CR 0xe813
80#define PLA_CRWECR 0xe81c
hayeswang21ff2e82014-02-18 21:49:06 +080081#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
82#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
hayeswangac718b62013-05-02 16:01:25 +000083#define PLA_CONFIG5 0xe822
84#define PLA_PHY_PWR 0xe84c
85#define PLA_OOB_CTRL 0xe84f
86#define PLA_CPCR 0xe854
87#define PLA_MISC_0 0xe858
88#define PLA_MISC_1 0xe85a
89#define PLA_OCP_GPHY_BASE 0xe86c
hayeswang4f1d4d52014-03-11 16:24:19 +080090#define PLA_TALLYCNT 0xe890
hayeswangac718b62013-05-02 16:01:25 +000091#define PLA_SFF_STS_7 0xe8de
92#define PLA_PHYSTATUS 0xe908
93#define PLA_BP_BA 0xfc26
94#define PLA_BP_0 0xfc28
95#define PLA_BP_1 0xfc2a
96#define PLA_BP_2 0xfc2c
97#define PLA_BP_3 0xfc2e
98#define PLA_BP_4 0xfc30
99#define PLA_BP_5 0xfc32
100#define PLA_BP_6 0xfc34
101#define PLA_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800102#define PLA_BP_EN 0xfc38
hayeswangac718b62013-05-02 16:01:25 +0000103
hayeswang65bab842015-02-12 16:20:46 +0800104#define USB_USB2PHY 0xb41e
105#define USB_SSPHYLINK2 0xb428
hayeswang43779f82014-01-02 11:25:10 +0800106#define USB_U2P3_CTRL 0xb460
hayeswang65bab842015-02-12 16:20:46 +0800107#define USB_CSR_DUMMY1 0xb464
108#define USB_CSR_DUMMY2 0xb466
hayeswangac718b62013-05-02 16:01:25 +0000109#define USB_DEV_STAT 0xb808
hayeswang65bab842015-02-12 16:20:46 +0800110#define USB_CONNECT_TIMER 0xcbf8
hayeswang65b82d62017-06-15 14:44:03 +0800111#define USB_MSC_TIMER 0xcbfc
hayeswang65bab842015-02-12 16:20:46 +0800112#define USB_BURST_SIZE 0xcfc0
hayeswang65b82d62017-06-15 14:44:03 +0800113#define USB_LPM_CONFIG 0xcfd8
hayeswangac718b62013-05-02 16:01:25 +0000114#define USB_USB_CTRL 0xd406
115#define USB_PHY_CTRL 0xd408
116#define USB_TX_AGG 0xd40a
117#define USB_RX_BUF_TH 0xd40c
118#define USB_USB_TIMER 0xd428
hayeswang464ec102015-02-12 14:33:46 +0800119#define USB_RX_EARLY_TIMEOUT 0xd42c
120#define USB_RX_EARLY_SIZE 0xd42e
hayeswang65b82d62017-06-15 14:44:03 +0800121#define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
122#define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
hayeswangac718b62013-05-02 16:01:25 +0000123#define USB_TX_DMA 0xd434
hayeswang65b82d62017-06-15 14:44:03 +0800124#define USB_UPT_RXDMA_OWN 0xd437
hayeswang43779f82014-01-02 11:25:10 +0800125#define USB_TOLERANCE 0xd490
126#define USB_LPM_CTRL 0xd41a
hayeswang93fe9b12016-06-16 10:55:18 +0800127#define USB_BMU_RESET 0xd4b0
hayeswang65b82d62017-06-15 14:44:03 +0800128#define USB_U1U2_TIMER 0xd4da
hayeswangac718b62013-05-02 16:01:25 +0000129#define USB_UPS_CTRL 0xd800
hayeswang43779f82014-01-02 11:25:10 +0800130#define USB_POWER_CUT 0xd80a
hayeswang65b82d62017-06-15 14:44:03 +0800131#define USB_MISC_0 0xd81a
Mario Limonciello9c273692018-12-11 08:16:14 -0600132#define USB_MISC_1 0xd81f
hayeswang43779f82014-01-02 11:25:10 +0800133#define USB_AFE_CTRL2 0xd824
hayeswang65b82d62017-06-15 14:44:03 +0800134#define USB_UPS_CFG 0xd842
135#define USB_UPS_FLAGS 0xd848
hayeswang43779f82014-01-02 11:25:10 +0800136#define USB_WDT11_CTRL 0xe43c
hayeswangac718b62013-05-02 16:01:25 +0000137#define USB_BP_BA 0xfc26
138#define USB_BP_0 0xfc28
139#define USB_BP_1 0xfc2a
140#define USB_BP_2 0xfc2c
141#define USB_BP_3 0xfc2e
142#define USB_BP_4 0xfc30
143#define USB_BP_5 0xfc32
144#define USB_BP_6 0xfc34
145#define USB_BP_7 0xfc36
hayeswang43779f82014-01-02 11:25:10 +0800146#define USB_BP_EN 0xfc38
hayeswang65b82d62017-06-15 14:44:03 +0800147#define USB_BP_8 0xfc38
148#define USB_BP_9 0xfc3a
149#define USB_BP_10 0xfc3c
150#define USB_BP_11 0xfc3e
151#define USB_BP_12 0xfc40
152#define USB_BP_13 0xfc42
153#define USB_BP_14 0xfc44
154#define USB_BP_15 0xfc46
155#define USB_BP2_EN 0xfc48
hayeswangac718b62013-05-02 16:01:25 +0000156
157/* OCP Registers */
158#define OCP_ALDPS_CONFIG 0x2010
159#define OCP_EEE_CONFIG1 0x2080
160#define OCP_EEE_CONFIG2 0x2092
161#define OCP_EEE_CONFIG3 0x2094
hayeswangac244d32014-01-02 11:22:40 +0800162#define OCP_BASE_MII 0xa400
hayeswangac718b62013-05-02 16:01:25 +0000163#define OCP_EEE_AR 0xa41a
164#define OCP_EEE_DATA 0xa41c
hayeswang43779f82014-01-02 11:25:10 +0800165#define OCP_PHY_STATUS 0xa420
hayeswang65b82d62017-06-15 14:44:03 +0800166#define OCP_NCTL_CFG 0xa42c
hayeswang43779f82014-01-02 11:25:10 +0800167#define OCP_POWER_CFG 0xa430
168#define OCP_EEE_CFG 0xa432
169#define OCP_SRAM_ADDR 0xa436
170#define OCP_SRAM_DATA 0xa438
171#define OCP_DOWN_SPEED 0xa442
hayeswangdf35d282014-09-25 20:54:02 +0800172#define OCP_EEE_ABLE 0xa5c4
hayeswang4c4a6b12014-09-25 20:54:00 +0800173#define OCP_EEE_ADV 0xa5d0
hayeswangdf35d282014-09-25 20:54:02 +0800174#define OCP_EEE_LPABLE 0xa5d2
hayeswang2dd49e02015-09-07 11:57:44 +0800175#define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
hayeswang65b82d62017-06-15 14:44:03 +0800176#define OCP_PHY_PATCH_STAT 0xb800
177#define OCP_PHY_PATCH_CMD 0xb820
178#define OCP_ADC_IOFFSET 0xbcfc
hayeswang43779f82014-01-02 11:25:10 +0800179#define OCP_ADC_CFG 0xbc06
hayeswang65b82d62017-06-15 14:44:03 +0800180#define OCP_SYSCLK_CFG 0xc416
hayeswang43779f82014-01-02 11:25:10 +0800181
182/* SRAM Register */
hayeswang65b82d62017-06-15 14:44:03 +0800183#define SRAM_GREEN_CFG 0x8011
hayeswang43779f82014-01-02 11:25:10 +0800184#define SRAM_LPF_CFG 0x8012
185#define SRAM_10M_AMP1 0x8080
186#define SRAM_10M_AMP2 0x8082
187#define SRAM_IMPEDANCE 0x8084
hayeswangac718b62013-05-02 16:01:25 +0000188
189/* PLA_RCR */
190#define RCR_AAP 0x00000001
191#define RCR_APM 0x00000002
192#define RCR_AM 0x00000004
193#define RCR_AB 0x00000008
194#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
195
196/* PLA_RXFIFO_CTRL0 */
197#define RXFIFO_THR1_NORMAL 0x00080002
198#define RXFIFO_THR1_OOB 0x01800003
199
200/* PLA_RXFIFO_CTRL1 */
201#define RXFIFO_THR2_FULL 0x00000060
202#define RXFIFO_THR2_HIGH 0x00000038
203#define RXFIFO_THR2_OOB 0x0000004a
hayeswang43779f82014-01-02 11:25:10 +0800204#define RXFIFO_THR2_NORMAL 0x00a0
hayeswangac718b62013-05-02 16:01:25 +0000205
206/* PLA_RXFIFO_CTRL2 */
207#define RXFIFO_THR3_FULL 0x00000078
208#define RXFIFO_THR3_HIGH 0x00000048
209#define RXFIFO_THR3_OOB 0x0000005a
hayeswang43779f82014-01-02 11:25:10 +0800210#define RXFIFO_THR3_NORMAL 0x0110
hayeswangac718b62013-05-02 16:01:25 +0000211
212/* PLA_TXFIFO_CTRL */
213#define TXFIFO_THR_NORMAL 0x00400008
hayeswang43779f82014-01-02 11:25:10 +0800214#define TXFIFO_THR_NORMAL2 0x01000008
hayeswangac718b62013-05-02 16:01:25 +0000215
hayeswang65bab842015-02-12 16:20:46 +0800216/* PLA_DMY_REG0 */
217#define ECM_ALDPS 0x0002
218
hayeswangac718b62013-05-02 16:01:25 +0000219/* PLA_FMC */
220#define FMC_FCR_MCU_EN 0x0001
221
222/* PLA_EEEP_CR */
223#define EEEP_CR_EEEP_TX 0x0002
224
hayeswang43779f82014-01-02 11:25:10 +0800225/* PLA_WDT6_CTRL */
226#define WDT6_SET_MODE 0x0010
227
hayeswangac718b62013-05-02 16:01:25 +0000228/* PLA_TCR0 */
229#define TCR0_TX_EMPTY 0x0800
230#define TCR0_AUTO_FIFO 0x0080
231
232/* PLA_TCR1 */
233#define VERSION_MASK 0x7cf0
234
hayeswang69b4b7a2014-07-10 10:58:54 +0800235/* PLA_MTPS */
236#define MTPS_JUMBO (12 * 1024 / 64)
237#define MTPS_DEFAULT (6 * 1024 / 64)
238
hayeswang4f1d4d52014-03-11 16:24:19 +0800239/* PLA_RSTTALLY */
240#define TALLY_RESET 0x0001
241
hayeswangac718b62013-05-02 16:01:25 +0000242/* PLA_CR */
243#define CR_RST 0x10
244#define CR_RE 0x08
245#define CR_TE 0x04
246
247/* PLA_CRWECR */
248#define CRWECR_NORAML 0x00
249#define CRWECR_CONFIG 0xc0
250
251/* PLA_OOB_CTRL */
252#define NOW_IS_OOB 0x80
253#define TXFIFO_EMPTY 0x20
254#define RXFIFO_EMPTY 0x10
255#define LINK_LIST_READY 0x02
256#define DIS_MCU_CLROOB 0x01
257#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
258
259/* PLA_MISC_1 */
260#define RXDY_GATED_EN 0x0008
261
262/* PLA_SFF_STS_7 */
263#define RE_INIT_LL 0x8000
264#define MCU_BORW_EN 0x4000
265
266/* PLA_CPCR */
267#define CPCR_RX_VLAN 0x0040
268
269/* PLA_CFG_WOL */
270#define MAGIC_EN 0x0001
271
hayeswang43779f82014-01-02 11:25:10 +0800272/* PLA_TEREDO_CFG */
273#define TEREDO_SEL 0x8000
274#define TEREDO_WAKE_MASK 0x7f00
275#define TEREDO_RS_EVENT_MASK 0x00fe
276#define OOB_TEREDO_EN 0x0001
277
hayeswangac718b62013-05-02 16:01:25 +0000278/* PAL_BDC_CR */
279#define ALDPS_PROXY_MODE 0x0001
280
hayeswang65b82d62017-06-15 14:44:03 +0800281/* PLA_EFUSE_CMD */
282#define EFUSE_READ_CMD BIT(15)
283#define EFUSE_DATA_BIT16 BIT(7)
284
hayeswang21ff2e82014-02-18 21:49:06 +0800285/* PLA_CONFIG34 */
286#define LINK_ON_WAKE_EN 0x0010
287#define LINK_OFF_WAKE_EN 0x0008
288
hayeswangac718b62013-05-02 16:01:25 +0000289/* PLA_CONFIG5 */
hayeswang21ff2e82014-02-18 21:49:06 +0800290#define BWF_EN 0x0040
291#define MWF_EN 0x0020
292#define UWF_EN 0x0010
hayeswangac718b62013-05-02 16:01:25 +0000293#define LAN_WAKE_EN 0x0002
294
295/* PLA_LED_FEATURE */
296#define LED_MODE_MASK 0x0700
297
298/* PLA_PHY_PWR */
299#define TX_10M_IDLE_EN 0x0080
300#define PFM_PWM_SWITCH 0x0040
301
302/* PLA_MAC_PWR_CTRL */
303#define D3_CLK_GATED_EN 0x00004000
304#define MCU_CLK_RATIO 0x07010f07
305#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
hayeswang43779f82014-01-02 11:25:10 +0800306#define ALDPS_SPDWN_RATIO 0x0f87
307
308/* PLA_MAC_PWR_CTRL2 */
309#define EEE_SPDWN_RATIO 0x8007
hayeswang65b82d62017-06-15 14:44:03 +0800310#define MAC_CLK_SPDWN_EN BIT(15)
hayeswang43779f82014-01-02 11:25:10 +0800311
312/* PLA_MAC_PWR_CTRL3 */
313#define PKT_AVAIL_SPDWN_EN 0x0100
314#define SUSPEND_SPDWN_EN 0x0004
315#define U1U2_SPDWN_EN 0x0002
316#define L1_SPDWN_EN 0x0001
317
318/* PLA_MAC_PWR_CTRL4 */
319#define PWRSAVE_SPDWN_EN 0x1000
320#define RXDV_SPDWN_EN 0x0800
321#define TX10MIDLE_EN 0x0100
322#define TP100_SPDWN_EN 0x0020
323#define TP500_SPDWN_EN 0x0010
324#define TP1000_SPDWN_EN 0x0008
325#define EEE_SPDWN_EN 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000326
327/* PLA_GPHY_INTR_IMR */
328#define GPHY_STS_MSK 0x0001
329#define SPEED_DOWN_MSK 0x0002
330#define SPDWN_RXDV_MSK 0x0004
331#define SPDWN_LINKCHG_MSK 0x0008
332
333/* PLA_PHYAR */
334#define PHYAR_FLAG 0x80000000
335
336/* PLA_EEE_CR */
337#define EEE_RX_EN 0x0001
338#define EEE_TX_EN 0x0002
339
hayeswang43779f82014-01-02 11:25:10 +0800340/* PLA_BOOT_CTRL */
341#define AUTOLOAD_DONE 0x0002
342
hayeswang65bab842015-02-12 16:20:46 +0800343/* USB_USB2PHY */
344#define USB2PHY_SUSPEND 0x0001
345#define USB2PHY_L1 0x0002
346
347/* USB_SSPHYLINK2 */
348#define pwd_dn_scale_mask 0x3ffe
349#define pwd_dn_scale(x) ((x) << 1)
350
351/* USB_CSR_DUMMY1 */
352#define DYNAMIC_BURST 0x0001
353
354/* USB_CSR_DUMMY2 */
355#define EP4_FULL_FC 0x0001
356
hayeswangac718b62013-05-02 16:01:25 +0000357/* USB_DEV_STAT */
358#define STAT_SPEED_MASK 0x0006
359#define STAT_SPEED_HIGH 0x0000
hayeswanga3cc4652014-07-24 16:37:43 +0800360#define STAT_SPEED_FULL 0x0002
hayeswangac718b62013-05-02 16:01:25 +0000361
hayeswang65b82d62017-06-15 14:44:03 +0800362/* USB_LPM_CONFIG */
363#define LPM_U1U2_EN BIT(0)
364
hayeswangac718b62013-05-02 16:01:25 +0000365/* USB_TX_AGG */
366#define TX_AGG_MAX_THRESHOLD 0x03
367
368/* USB_RX_BUF_TH */
hayeswang43779f82014-01-02 11:25:10 +0800369#define RX_THR_SUPPER 0x0c350180
hayeswang8e1f51b2014-01-02 11:22:41 +0800370#define RX_THR_HIGH 0x7a120180
hayeswang43779f82014-01-02 11:25:10 +0800371#define RX_THR_SLOW 0xffff0180
hayeswang65b82d62017-06-15 14:44:03 +0800372#define RX_THR_B 0x00010001
hayeswangac718b62013-05-02 16:01:25 +0000373
374/* USB_TX_DMA */
375#define TEST_MODE_DISABLE 0x00000001
376#define TX_SIZE_ADJUST1 0x00000100
377
hayeswang93fe9b12016-06-16 10:55:18 +0800378/* USB_BMU_RESET */
379#define BMU_RESET_EP_IN 0x01
380#define BMU_RESET_EP_OUT 0x02
381
hayeswang65b82d62017-06-15 14:44:03 +0800382/* USB_UPT_RXDMA_OWN */
383#define OWN_UPDATE BIT(0)
384#define OWN_CLEAR BIT(1)
385
hayeswangac718b62013-05-02 16:01:25 +0000386/* USB_UPS_CTRL */
387#define POWER_CUT 0x0100
388
389/* USB_PM_CTRL_STATUS */
hayeswang8e1f51b2014-01-02 11:22:41 +0800390#define RESUME_INDICATE 0x0001
hayeswangac718b62013-05-02 16:01:25 +0000391
392/* USB_USB_CTRL */
393#define RX_AGG_DISABLE 0x0010
hayeswange90fba82015-07-31 11:23:39 +0800394#define RX_ZERO_EN 0x0080
hayeswangac718b62013-05-02 16:01:25 +0000395
hayeswang43779f82014-01-02 11:25:10 +0800396/* USB_U2P3_CTRL */
397#define U2P3_ENABLE 0x0001
398
399/* USB_POWER_CUT */
400#define PWR_EN 0x0001
401#define PHASE2_EN 0x0008
hayeswang65b82d62017-06-15 14:44:03 +0800402#define UPS_EN BIT(4)
403#define USP_PREWAKE BIT(5)
hayeswang43779f82014-01-02 11:25:10 +0800404
405/* USB_MISC_0 */
406#define PCUT_STATUS 0x0001
407
hayeswang464ec102015-02-12 14:33:46 +0800408/* USB_RX_EARLY_TIMEOUT */
409#define COALESCE_SUPER 85000U
410#define COALESCE_HIGH 250000U
411#define COALESCE_SLOW 524280U
hayeswang43779f82014-01-02 11:25:10 +0800412
413/* USB_WDT11_CTRL */
414#define TIMER11_EN 0x0001
415
416/* USB_LPM_CTRL */
hayeswang65bab842015-02-12 16:20:46 +0800417/* bit 4 ~ 5: fifo empty boundary */
418#define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
419/* bit 2 ~ 3: LMP timer */
hayeswang43779f82014-01-02 11:25:10 +0800420#define LPM_TIMER_MASK 0x0c
421#define LPM_TIMER_500MS 0x04 /* 500 ms */
422#define LPM_TIMER_500US 0x0c /* 500 us */
hayeswang65bab842015-02-12 16:20:46 +0800423#define ROK_EXIT_LPM 0x02
hayeswang43779f82014-01-02 11:25:10 +0800424
425/* USB_AFE_CTRL2 */
426#define SEN_VAL_MASK 0xf800
427#define SEN_VAL_NORMAL 0xa000
428#define SEL_RXIDLE 0x0100
429
hayeswang65b82d62017-06-15 14:44:03 +0800430/* USB_UPS_CFG */
431#define SAW_CNT_1MS_MASK 0x0fff
432
433/* USB_UPS_FLAGS */
434#define UPS_FLAGS_R_TUNE BIT(0)
435#define UPS_FLAGS_EN_10M_CKDIV BIT(1)
436#define UPS_FLAGS_250M_CKDIV BIT(2)
437#define UPS_FLAGS_EN_ALDPS BIT(3)
438#define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
439#define UPS_FLAGS_SPEED_MASK (0xf << 16)
440#define ups_flags_speed(x) ((x) << 16)
441#define UPS_FLAGS_EN_EEE BIT(20)
442#define UPS_FLAGS_EN_500M_EEE BIT(21)
443#define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
444#define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
445#define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
446#define UPS_FLAGS_EN_GREEN BIT(26)
447#define UPS_FLAGS_EN_FLOW_CTR BIT(27)
448
449enum spd_duplex {
450 NWAY_10M_HALF = 1,
451 NWAY_10M_FULL,
452 NWAY_100M_HALF,
453 NWAY_100M_FULL,
454 NWAY_1000M_FULL,
455 FORCE_10M_HALF,
456 FORCE_10M_FULL,
457 FORCE_100M_HALF,
458 FORCE_100M_FULL,
459};
460
hayeswangac718b62013-05-02 16:01:25 +0000461/* OCP_ALDPS_CONFIG */
462#define ENPWRSAVE 0x8000
463#define ENPDNPS 0x0200
464#define LINKENA 0x0100
465#define DIS_SDSAVE 0x0010
466
hayeswang43779f82014-01-02 11:25:10 +0800467/* OCP_PHY_STATUS */
468#define PHY_STAT_MASK 0x0007
hayeswangc564b872017-06-09 17:11:38 +0800469#define PHY_STAT_EXT_INIT 2
hayeswang43779f82014-01-02 11:25:10 +0800470#define PHY_STAT_LAN_ON 3
471#define PHY_STAT_PWRDN 5
472
hayeswang65b82d62017-06-15 14:44:03 +0800473/* OCP_NCTL_CFG */
474#define PGA_RETURN_EN BIT(1)
475
hayeswang43779f82014-01-02 11:25:10 +0800476/* OCP_POWER_CFG */
477#define EEE_CLKDIV_EN 0x8000
478#define EN_ALDPS 0x0004
479#define EN_10M_PLLOFF 0x0001
480
hayeswangac718b62013-05-02 16:01:25 +0000481/* OCP_EEE_CONFIG1 */
482#define RG_TXLPI_MSK_HFDUP 0x8000
483#define RG_MATCLR_EN 0x4000
484#define EEE_10_CAP 0x2000
485#define EEE_NWAY_EN 0x1000
486#define TX_QUIET_EN 0x0200
487#define RX_QUIET_EN 0x0100
hayeswangd24f6132014-09-25 20:54:01 +0800488#define sd_rise_time_mask 0x0070
hayeswang4c4a6b12014-09-25 20:54:00 +0800489#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
hayeswangac718b62013-05-02 16:01:25 +0000490#define RG_RXLPI_MSK_HFDUP 0x0008
491#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
492
493/* OCP_EEE_CONFIG2 */
494#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
495#define RG_DACQUIET_EN 0x0400
496#define RG_LDVQUIET_EN 0x0200
497#define RG_CKRSEL 0x0020
498#define RG_EEEPRG_EN 0x0010
499
500/* OCP_EEE_CONFIG3 */
hayeswangd24f6132014-09-25 20:54:01 +0800501#define fast_snr_mask 0xff80
hayeswang4c4a6b12014-09-25 20:54:00 +0800502#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
hayeswangac718b62013-05-02 16:01:25 +0000503#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
504#define MSK_PH 0x0006 /* bit 0 ~ 3 */
505
506/* OCP_EEE_AR */
507/* bit[15:14] function */
508#define FUN_ADDR 0x0000
509#define FUN_DATA 0x4000
510/* bit[4:0] device addr */
hayeswangac718b62013-05-02 16:01:25 +0000511
hayeswang43779f82014-01-02 11:25:10 +0800512/* OCP_EEE_CFG */
513#define CTAP_SHORT_EN 0x0040
514#define EEE10_EN 0x0010
515
516/* OCP_DOWN_SPEED */
hayeswang65b82d62017-06-15 14:44:03 +0800517#define EN_EEE_CMODE BIT(14)
518#define EN_EEE_1000 BIT(13)
519#define EN_EEE_100 BIT(12)
520#define EN_10M_CLKDIV BIT(11)
hayeswang43779f82014-01-02 11:25:10 +0800521#define EN_10M_BGOFF 0x0080
522
hayeswang2dd49e02015-09-07 11:57:44 +0800523/* OCP_PHY_STATE */
524#define TXDIS_STATE 0x01
525#define ABD_STATE 0x02
526
hayeswang65b82d62017-06-15 14:44:03 +0800527/* OCP_PHY_PATCH_STAT */
528#define PATCH_READY BIT(6)
529
530/* OCP_PHY_PATCH_CMD */
531#define PATCH_REQUEST BIT(4)
532
hayeswang43779f82014-01-02 11:25:10 +0800533/* OCP_ADC_CFG */
534#define CKADSEL_L 0x0100
535#define ADC_EN 0x0080
536#define EN_EMI_L 0x0040
537
hayeswang65b82d62017-06-15 14:44:03 +0800538/* OCP_SYSCLK_CFG */
539#define clk_div_expo(x) (min(x, 5) << 8)
540
541/* SRAM_GREEN_CFG */
542#define GREEN_ETH_EN BIT(15)
543#define R_TUNE_EN BIT(11)
544
hayeswang43779f82014-01-02 11:25:10 +0800545/* SRAM_LPF_CFG */
546#define LPF_AUTO_TUNE 0x8000
547
548/* SRAM_10M_AMP1 */
549#define GDAC_IB_UPALL 0x0008
550
551/* SRAM_10M_AMP2 */
552#define AMP_DN 0x0200
553
554/* SRAM_IMPEDANCE */
555#define RX_DRIVING_MASK 0x6000
556
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500557/* MAC PASSTHRU */
558#define AD_MASK 0xfee0
Mario Limonciello9c273692018-12-11 08:16:14 -0600559#define BND_MASK 0x0004
David Chen8e29d232019-02-16 17:16:42 +0800560#define BD_MASK 0x0001
Mario Limonciello34ee32c2016-07-11 19:58:04 -0500561#define EFUSE 0xcfdb
562#define PASS_THRU_MASK 0x1
563
hayeswangac718b62013-05-02 16:01:25 +0000564enum rtl_register_content {
hayeswang43779f82014-01-02 11:25:10 +0800565 _1000bps = 0x10,
hayeswangac718b62013-05-02 16:01:25 +0000566 _100bps = 0x08,
567 _10bps = 0x04,
568 LINK_STATUS = 0x02,
569 FULL_DUP = 0x01,
570};
571
hayeswang1764bcd2014-08-28 10:24:18 +0800572#define RTL8152_MAX_TX 4
hayeswangebc2ec482013-08-14 20:54:38 +0800573#define RTL8152_MAX_RX 10
hayeswang40a82912013-08-14 20:54:40 +0800574#define INTBUFSIZE 2
hayeswang8e1f51b2014-01-02 11:22:41 +0800575#define TX_ALIGN 4
576#define RX_ALIGN 8
hayeswang40a82912013-08-14 20:54:40 +0800577
578#define INTR_LINK 0x0004
hayeswangebc2ec482013-08-14 20:54:38 +0800579
hayeswangac718b62013-05-02 16:01:25 +0000580#define RTL8152_REQT_READ 0xc0
581#define RTL8152_REQT_WRITE 0x40
582#define RTL8152_REQ_GET_REGS 0x05
583#define RTL8152_REQ_SET_REGS 0x05
584
585#define BYTE_EN_DWORD 0xff
586#define BYTE_EN_WORD 0x33
587#define BYTE_EN_BYTE 0x11
588#define BYTE_EN_SIX_BYTES 0x3f
589#define BYTE_EN_START_MASK 0x0f
590#define BYTE_EN_END_MASK 0xf0
591
hayeswang69b4b7a2014-07-10 10:58:54 +0800592#define RTL8153_MAX_PACKET 9216 /* 9K */
hayeswangb65c0c92017-06-21 11:25:18 +0800593#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
594 ETH_FCS_LEN)
595#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
hayeswang69b4b7a2014-07-10 10:58:54 +0800596#define RTL8153_RMS RTL8153_MAX_PACKET
hayeswangb8125402014-07-03 11:55:48 +0800597#define RTL8152_TX_TIMEOUT (5 * HZ)
hayeswangd823ab62015-01-12 12:06:23 +0800598#define RTL8152_NAPI_WEIGHT 64
hayeswangb65c0c92017-06-21 11:25:18 +0800599#define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
hayeswangb20cb602017-03-20 16:13:45 +0800600 sizeof(struct rx_desc) + RX_ALIGN)
hayeswangac718b62013-05-02 16:01:25 +0000601
602/* rtl8152 flags */
603enum rtl8152_flags {
604 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000605 RTL8152_SET_RX_MODE,
hayeswang40a82912013-08-14 20:54:40 +0800606 WORK_ENABLE,
607 RTL8152_LINK_CHG,
hayeswang9a4be1b2014-02-18 21:49:07 +0800608 SELECTIVE_SUSPEND,
hayeswangaa66a5f2014-02-18 21:49:04 +0800609 PHY_RESET,
hayeswangd823ab62015-01-12 12:06:23 +0800610 SCHEDULE_NAPI,
hayeswang65b82d62017-06-15 14:44:03 +0800611 GREEN_ETHERNET,
Kai-Heng Feng0b165512018-01-16 16:46:27 +0800612 DELL_TB_RX_AGG_BUG,
hayeswangac718b62013-05-02 16:01:25 +0000613};
614
615/* Define these values to match your device */
616#define VENDOR_ID_REALTEK 0x0bda
René Rebed5b07cc2017-03-28 07:56:51 +0200617#define VENDOR_ID_MICROSOFT 0x045e
hayeswang43779f82014-01-02 11:25:10 +0800618#define VENDOR_ID_SAMSUNG 0x04e8
Christian Hesse347eec32015-03-31 14:10:07 +0200619#define VENDOR_ID_LENOVO 0x17ef
Grant Grundler90841042017-09-28 11:35:00 -0700620#define VENDOR_ID_LINKSYS 0x13b1
Zheng Liud065c3c12015-07-07 13:54:12 -0700621#define VENDOR_ID_NVIDIA 0x0955
Ran Wang9d11b062017-10-23 18:10:23 +0800622#define VENDOR_ID_TPLINK 0x2357
hayeswangac718b62013-05-02 16:01:25 +0000623
624#define MCU_TYPE_PLA 0x0100
625#define MCU_TYPE_USB 0x0000
626
hayeswang4f1d4d52014-03-11 16:24:19 +0800627struct tally_counter {
628 __le64 tx_packets;
629 __le64 rx_packets;
630 __le64 tx_errors;
631 __le32 rx_errors;
632 __le16 rx_missed;
633 __le16 align_errors;
634 __le32 tx_one_collision;
635 __le32 tx_multi_collision;
636 __le64 rx_unicast;
637 __le64 rx_broadcast;
638 __le32 rx_multicast;
639 __le16 tx_aborted;
hayeswangf37119c2014-10-28 14:05:51 +0800640 __le16 tx_underrun;
hayeswang4f1d4d52014-03-11 16:24:19 +0800641};
642
hayeswangac718b62013-05-02 16:01:25 +0000643struct rx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800644 __le32 opts1;
hayeswangac718b62013-05-02 16:01:25 +0000645#define RX_LEN_MASK 0x7fff
hayeswang565cab02014-03-07 11:04:38 +0800646
hayeswang500b6d72013-11-20 17:30:57 +0800647 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800648#define RD_UDP_CS BIT(23)
649#define RD_TCP_CS BIT(22)
650#define RD_IPV6_CS BIT(20)
651#define RD_IPV4_CS BIT(19)
hayeswang565cab02014-03-07 11:04:38 +0800652
hayeswang500b6d72013-11-20 17:30:57 +0800653 __le32 opts3;
hayeswangf5aaaa62015-02-06 11:30:51 +0800654#define IPF BIT(23) /* IP checksum fail */
655#define UDPF BIT(22) /* UDP checksum fail */
656#define TCPF BIT(21) /* TCP checksum fail */
657#define RX_VLAN_TAG BIT(16)
hayeswang565cab02014-03-07 11:04:38 +0800658
hayeswang500b6d72013-11-20 17:30:57 +0800659 __le32 opts4;
660 __le32 opts5;
661 __le32 opts6;
hayeswangac718b62013-05-02 16:01:25 +0000662};
663
664struct tx_desc {
hayeswang500b6d72013-11-20 17:30:57 +0800665 __le32 opts1;
hayeswangf5aaaa62015-02-06 11:30:51 +0800666#define TX_FS BIT(31) /* First segment of a packet */
667#define TX_LS BIT(30) /* Final segment of a packet */
668#define GTSENDV4 BIT(28)
669#define GTSENDV6 BIT(27)
hayeswang60c89072014-03-07 11:04:39 +0800670#define GTTCPHO_SHIFT 18
hayeswang6128d1bb2014-03-07 11:04:40 +0800671#define GTTCPHO_MAX 0x7fU
hayeswang60c89072014-03-07 11:04:39 +0800672#define TX_LEN_MAX 0x3ffffU
hayeswang5bd23882013-08-14 20:54:39 +0800673
hayeswang500b6d72013-11-20 17:30:57 +0800674 __le32 opts2;
hayeswangf5aaaa62015-02-06 11:30:51 +0800675#define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
676#define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
677#define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
678#define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
hayeswang60c89072014-03-07 11:04:39 +0800679#define MSS_SHIFT 17
680#define MSS_MAX 0x7ffU
681#define TCPHO_SHIFT 17
hayeswang6128d1bb2014-03-07 11:04:40 +0800682#define TCPHO_MAX 0x7ffU
hayeswangf5aaaa62015-02-06 11:30:51 +0800683#define TX_VLAN_TAG BIT(16)
hayeswangac718b62013-05-02 16:01:25 +0000684};
685
hayeswangdff4e8a2013-08-16 16:09:33 +0800686struct r8152;
687
hayeswangebc2ec482013-08-14 20:54:38 +0800688struct rx_agg {
689 struct list_head list;
690 struct urb *urb;
hayeswangdff4e8a2013-08-16 16:09:33 +0800691 struct r8152 *context;
hayeswangebc2ec482013-08-14 20:54:38 +0800692 void *buffer;
693 void *head;
694};
695
696struct tx_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 u32 skb_num;
703 u32 skb_len;
704};
705
hayeswangac718b62013-05-02 16:01:25 +0000706struct r8152 {
707 unsigned long flags;
708 struct usb_device *udev;
hayeswangd823ab62015-01-12 12:06:23 +0800709 struct napi_struct napi;
hayeswang40a82912013-08-14 20:54:40 +0800710 struct usb_interface *intf;
hayeswangac718b62013-05-02 16:01:25 +0000711 struct net_device *netdev;
hayeswang40a82912013-08-14 20:54:40 +0800712 struct urb *intr_urb;
hayeswangebc2ec482013-08-14 20:54:38 +0800713 struct tx_agg tx_info[RTL8152_MAX_TX];
714 struct rx_agg rx_info[RTL8152_MAX_RX];
715 struct list_head rx_done, tx_free;
hayeswangd823ab62015-01-12 12:06:23 +0800716 struct sk_buff_head tx_queue, rx_queue;
hayeswangebc2ec482013-08-14 20:54:38 +0800717 spinlock_t rx_lock, tx_lock;
hayeswanga028a9e2016-06-13 17:49:36 +0800718 struct delayed_work schedule, hw_phy_work;
hayeswangac718b62013-05-02 16:01:25 +0000719 struct mii_if_info mii;
hayeswangb5403272014-10-09 18:00:26 +0800720 struct mutex control; /* use for hw setting */
hayeswang5ee3c602016-01-07 17:12:17 +0800721#ifdef CONFIG_PM_SLEEP
722 struct notifier_block pm_notifier;
723#endif
hayeswangc81229c2014-01-02 11:22:42 +0800724
725 struct rtl_ops {
726 void (*init)(struct r8152 *);
727 int (*enable)(struct r8152 *);
728 void (*disable)(struct r8152 *);
hayeswang7e9da482014-02-18 21:49:05 +0800729 void (*up)(struct r8152 *);
hayeswangc81229c2014-01-02 11:22:42 +0800730 void (*down)(struct r8152 *);
731 void (*unload)(struct r8152 *);
hayeswangdf35d282014-09-25 20:54:02 +0800732 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
733 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
hayeswang2dd49e02015-09-07 11:57:44 +0800734 bool (*in_nway)(struct r8152 *);
hayeswanga028a9e2016-06-13 17:49:36 +0800735 void (*hw_phy_cfg)(struct r8152 *);
hayeswang2609af12016-07-05 16:11:46 +0800736 void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswangc81229c2014-01-02 11:22:42 +0800737 } rtl_ops;
738
hayeswang40a82912013-08-14 20:54:40 +0800739 int intr_interval;
hayeswang21ff2e82014-02-18 21:49:06 +0800740 u32 saved_wolopts;
hayeswangac718b62013-05-02 16:01:25 +0000741 u32 msg_enable;
hayeswangdd1b1192013-11-20 17:30:56 +0800742 u32 tx_qlen;
hayeswang464ec102015-02-12 14:33:46 +0800743 u32 coalesce;
hayeswangac718b62013-05-02 16:01:25 +0000744 u16 ocp_base;
hayeswangaa7e26b2016-06-13 17:49:38 +0800745 u16 speed;
hayeswang40a82912013-08-14 20:54:40 +0800746 u8 *intr_buff;
hayeswangac718b62013-05-02 16:01:25 +0000747 u8 version;
hayeswangaa7e26b2016-06-13 17:49:38 +0800748 u8 duplex;
749 u8 autoneg;
hayeswangac718b62013-05-02 16:01:25 +0000750};
751
752enum rtl_version {
753 RTL_VER_UNKNOWN = 0,
754 RTL_VER_01,
hayeswang43779f82014-01-02 11:25:10 +0800755 RTL_VER_02,
756 RTL_VER_03,
757 RTL_VER_04,
758 RTL_VER_05,
hayeswangfb02eb42015-07-22 15:27:41 +0800759 RTL_VER_06,
hayeswangc27b32c2017-06-15 14:44:02 +0800760 RTL_VER_07,
hayeswang65b82d62017-06-15 14:44:03 +0800761 RTL_VER_08,
762 RTL_VER_09,
hayeswang43779f82014-01-02 11:25:10 +0800763 RTL_VER_MAX
hayeswangac718b62013-05-02 16:01:25 +0000764};
765
hayeswang60c89072014-03-07 11:04:39 +0800766enum tx_csum_stat {
767 TX_CSUM_SUCCESS = 0,
768 TX_CSUM_TSO,
769 TX_CSUM_NONE
770};
771
hayeswangac718b62013-05-02 16:01:25 +0000772/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
773 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
774 */
775static const int multicast_filter_limit = 32;
hayeswang52aec122014-09-02 10:27:52 +0800776static unsigned int agg_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000777
hayeswang52aec122014-09-02 10:27:52 +0800778#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
hayeswangb65c0c92017-06-21 11:25:18 +0800779 VLAN_ETH_HLEN - ETH_FCS_LEN)
hayeswang60c89072014-03-07 11:04:39 +0800780
hayeswangac718b62013-05-02 16:01:25 +0000781static
782int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
783{
hayeswang31787f52013-07-31 17:21:25 +0800784 int ret;
785 void *tmp;
786
787 tmp = kmalloc(size, GFP_KERNEL);
788 if (!tmp)
789 return -ENOMEM;
790
791 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800792 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
793 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800794
795 memcpy(data, tmp, size);
796 kfree(tmp);
797
798 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000799}
800
801static
802int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
803{
hayeswang31787f52013-07-31 17:21:25 +0800804 int ret;
805 void *tmp;
806
Benoit Tainec4438f02014-05-26 17:21:23 +0200807 tmp = kmemdup(data, size, GFP_KERNEL);
hayeswang31787f52013-07-31 17:21:25 +0800808 if (!tmp)
809 return -ENOMEM;
810
hayeswang31787f52013-07-31 17:21:25 +0800811 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangb209af92014-08-25 15:53:00 +0800812 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
813 value, index, tmp, size, 500);
hayeswang31787f52013-07-31 17:21:25 +0800814
815 kfree(tmp);
hayeswangdb8515e2014-03-06 15:07:16 +0800816
hayeswang31787f52013-07-31 17:21:25 +0800817 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000818}
819
820static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
hayeswangb209af92014-08-25 15:53:00 +0800821 void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000822{
hayeswang45f4a192014-01-06 17:08:41 +0800823 u16 limit = 64;
824 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +0000825
826 if (test_bit(RTL8152_UNPLUG, &tp->flags))
827 return -ENODEV;
828
829 /* both size and indix must be 4 bytes align */
830 if ((size & 3) || !size || (index & 3) || !data)
831 return -EPERM;
832
833 if ((u32)index + (u32)size > 0xffff)
834 return -EPERM;
835
836 while (size) {
837 if (size > limit) {
838 ret = get_registers(tp, index, type, limit, data);
839 if (ret < 0)
840 break;
841
842 index += limit;
843 data += limit;
844 size -= limit;
845 } else {
846 ret = get_registers(tp, index, type, size, data);
847 if (ret < 0)
848 break;
849
850 index += size;
851 data += size;
852 size = 0;
853 break;
854 }
855 }
856
hayeswang67610492014-10-30 11:46:40 +0800857 if (ret == -ENODEV)
858 set_bit(RTL8152_UNPLUG, &tp->flags);
859
hayeswangac718b62013-05-02 16:01:25 +0000860 return ret;
861}
862
863static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
hayeswangb209af92014-08-25 15:53:00 +0800864 u16 size, void *data, u16 type)
hayeswangac718b62013-05-02 16:01:25 +0000865{
hayeswang45f4a192014-01-06 17:08:41 +0800866 int ret;
867 u16 byteen_start, byteen_end, byen;
868 u16 limit = 512;
hayeswangac718b62013-05-02 16:01:25 +0000869
870 if (test_bit(RTL8152_UNPLUG, &tp->flags))
871 return -ENODEV;
872
873 /* both size and indix must be 4 bytes align */
874 if ((size & 3) || !size || (index & 3) || !data)
875 return -EPERM;
876
877 if ((u32)index + (u32)size > 0xffff)
878 return -EPERM;
879
880 byteen_start = byteen & BYTE_EN_START_MASK;
881 byteen_end = byteen & BYTE_EN_END_MASK;
882
883 byen = byteen_start | (byteen_start << 4);
884 ret = set_registers(tp, index, type | byen, 4, data);
885 if (ret < 0)
886 goto error1;
887
888 index += 4;
889 data += 4;
890 size -= 4;
891
892 if (size) {
893 size -= 4;
894
895 while (size) {
896 if (size > limit) {
897 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800898 type | BYTE_EN_DWORD,
899 limit, data);
hayeswangac718b62013-05-02 16:01:25 +0000900 if (ret < 0)
901 goto error1;
902
903 index += limit;
904 data += limit;
905 size -= limit;
906 } else {
907 ret = set_registers(tp, index,
hayeswangb209af92014-08-25 15:53:00 +0800908 type | BYTE_EN_DWORD,
909 size, data);
hayeswangac718b62013-05-02 16:01:25 +0000910 if (ret < 0)
911 goto error1;
912
913 index += size;
914 data += size;
915 size = 0;
916 break;
917 }
918 }
919
920 byen = byteen_end | (byteen_end >> 4);
921 ret = set_registers(tp, index, type | byen, 4, data);
922 if (ret < 0)
923 goto error1;
924 }
925
926error1:
hayeswang67610492014-10-30 11:46:40 +0800927 if (ret == -ENODEV)
928 set_bit(RTL8152_UNPLUG, &tp->flags);
929
hayeswangac718b62013-05-02 16:01:25 +0000930 return ret;
931}
932
933static inline
934int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
935{
936 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
937}
938
939static inline
940int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
941{
942 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
943}
944
945static inline
hayeswangac718b62013-05-02 16:01:25 +0000946int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
947{
948 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
949}
950
951static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
952{
hayeswangc8826de2013-07-31 17:21:26 +0800953 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000954
hayeswangc8826de2013-07-31 17:21:26 +0800955 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000956
957 return __le32_to_cpu(data);
958}
959
960static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
961{
hayeswangc8826de2013-07-31 17:21:26 +0800962 __le32 tmp = __cpu_to_le32(data);
963
964 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000965}
966
967static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
968{
969 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800970 __le32 tmp;
hayeswangd8fbd272017-06-15 14:44:04 +0800971 u16 byen = BYTE_EN_WORD;
hayeswangac718b62013-05-02 16:01:25 +0000972 u8 shift = index & 2;
973
974 index &= ~3;
hayeswangd8fbd272017-06-15 14:44:04 +0800975 byen <<= shift;
hayeswangac718b62013-05-02 16:01:25 +0000976
hayeswangd8fbd272017-06-15 14:44:04 +0800977 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
hayeswangac718b62013-05-02 16:01:25 +0000978
hayeswangc8826de2013-07-31 17:21:26 +0800979 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000980 data >>= (shift * 8);
981 data &= 0xffff;
982
983 return (u16)data;
984}
985
986static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
987{
hayeswangc8826de2013-07-31 17:21:26 +0800988 u32 mask = 0xffff;
989 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000990 u16 byen = BYTE_EN_WORD;
991 u8 shift = index & 2;
992
993 data &= mask;
994
995 if (index & 2) {
996 byen <<= shift;
997 mask <<= (shift * 8);
998 data <<= (shift * 8);
999 index &= ~3;
1000 }
1001
hayeswangc8826de2013-07-31 17:21:26 +08001002 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001003
hayeswangc8826de2013-07-31 17:21:26 +08001004 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001005}
1006
1007static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1008{
1009 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +08001010 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001011 u8 shift = index & 3;
1012
1013 index &= ~3;
1014
hayeswangc8826de2013-07-31 17:21:26 +08001015 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001016
hayeswangc8826de2013-07-31 17:21:26 +08001017 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +00001018 data >>= (shift * 8);
1019 data &= 0xff;
1020
1021 return (u8)data;
1022}
1023
1024static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1025{
hayeswangc8826de2013-07-31 17:21:26 +08001026 u32 mask = 0xff;
1027 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +00001028 u16 byen = BYTE_EN_BYTE;
1029 u8 shift = index & 3;
1030
1031 data &= mask;
1032
1033 if (index & 3) {
1034 byen <<= shift;
1035 mask <<= (shift * 8);
1036 data <<= (shift * 8);
1037 index &= ~3;
1038 }
1039
hayeswangc8826de2013-07-31 17:21:26 +08001040 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +00001041
hayeswangc8826de2013-07-31 17:21:26 +08001042 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +00001043}
1044
hayeswangac244d32014-01-02 11:22:40 +08001045static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1046{
1047 u16 ocp_base, ocp_index;
1048
1049 ocp_base = addr & 0xf000;
1050 if (ocp_base != tp->ocp_base) {
1051 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1052 tp->ocp_base = ocp_base;
1053 }
1054
1055 ocp_index = (addr & 0x0fff) | 0xb000;
1056 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1057}
1058
hayeswange3fe0b12014-01-02 11:22:39 +08001059static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1060{
1061 u16 ocp_base, ocp_index;
1062
1063 ocp_base = addr & 0xf000;
1064 if (ocp_base != tp->ocp_base) {
1065 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1066 tp->ocp_base = ocp_base;
1067 }
1068
1069 ocp_index = (addr & 0x0fff) | 0xb000;
1070 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1071}
1072
hayeswangac244d32014-01-02 11:22:40 +08001073static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswangac718b62013-05-02 16:01:25 +00001074{
hayeswangac244d32014-01-02 11:22:40 +08001075 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
hayeswangac718b62013-05-02 16:01:25 +00001076}
1077
hayeswangac244d32014-01-02 11:22:40 +08001078static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
hayeswangac718b62013-05-02 16:01:25 +00001079{
hayeswangac244d32014-01-02 11:22:40 +08001080 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswangac718b62013-05-02 16:01:25 +00001081}
1082
hayeswang43779f82014-01-02 11:25:10 +08001083static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1084{
1085 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1086 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1087}
1088
hayeswang65b82d62017-06-15 14:44:03 +08001089static u16 sram_read(struct r8152 *tp, u16 addr)
1090{
1091 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1092 return ocp_reg_read(tp, OCP_SRAM_DATA);
1093}
1094
hayeswangac718b62013-05-02 16:01:25 +00001095static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1096{
1097 struct r8152 *tp = netdev_priv(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001098 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001099
hayeswang68714382014-04-11 17:54:31 +08001100 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1101 return -ENODEV;
1102
hayeswangac718b62013-05-02 16:01:25 +00001103 if (phy_id != R8152_PHY_ID)
1104 return -EINVAL;
1105
hayeswang9a4be1b2014-02-18 21:49:07 +08001106 ret = r8152_mdio_read(tp, reg);
1107
hayeswang9a4be1b2014-02-18 21:49:07 +08001108 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001109}
1110
1111static
1112void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1113{
1114 struct r8152 *tp = netdev_priv(netdev);
1115
hayeswang68714382014-04-11 17:54:31 +08001116 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1117 return;
1118
hayeswangac718b62013-05-02 16:01:25 +00001119 if (phy_id != R8152_PHY_ID)
1120 return;
1121
1122 r8152_mdio_write(tp, reg, val);
1123}
1124
hayeswangb209af92014-08-25 15:53:00 +08001125static int
1126r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001127
hayeswang8ba789a2014-09-04 16:15:41 +08001128static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1129{
1130 struct r8152 *tp = netdev_priv(netdev);
1131 struct sockaddr *addr = p;
hayeswangea6a7112014-10-02 17:03:12 +08001132 int ret = -EADDRNOTAVAIL;
hayeswang8ba789a2014-09-04 16:15:41 +08001133
1134 if (!is_valid_ether_addr(addr->sa_data))
hayeswangea6a7112014-10-02 17:03:12 +08001135 goto out1;
1136
1137 ret = usb_autopm_get_interface(tp->intf);
1138 if (ret < 0)
1139 goto out1;
hayeswang8ba789a2014-09-04 16:15:41 +08001140
hayeswangb5403272014-10-09 18:00:26 +08001141 mutex_lock(&tp->control);
1142
hayeswang8ba789a2014-09-04 16:15:41 +08001143 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1144
1145 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1146 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1147 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1148
hayeswangb5403272014-10-09 18:00:26 +08001149 mutex_unlock(&tp->control);
1150
hayeswangea6a7112014-10-02 17:03:12 +08001151 usb_autopm_put_interface(tp->intf);
1152out1:
1153 return ret;
hayeswang8ba789a2014-09-04 16:15:41 +08001154}
1155
Mario Limonciello9c273692018-12-11 08:16:14 -06001156/* Devices containing proper chips can support a persistent
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001157 * host system provided MAC address.
1158 * Examples of this are Dell TB15 and Dell WD15 docks
1159 */
1160static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1161{
1162 acpi_status status;
1163 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1164 union acpi_object *obj;
1165 int ret = -EINVAL;
1166 u32 ocp_data;
1167 unsigned char buf[6];
1168
1169 /* test for -AD variant of RTL8153 */
1170 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
Mario Limonciello9c273692018-12-11 08:16:14 -06001171 if ((ocp_data & AD_MASK) == 0x1000) {
1172 /* test for MAC address pass-through bit */
1173 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1174 if ((ocp_data & PASS_THRU_MASK) != 1) {
1175 netif_dbg(tp, probe, tp->netdev,
1176 "No efuse for RTL8153-AD MAC pass through\n");
1177 return -ENODEV;
1178 }
1179 } else {
David Chen8e29d232019-02-16 17:16:42 +08001180 /* test for RTL8153-BND and RTL8153-BD */
Mario Limonciello9c273692018-12-11 08:16:14 -06001181 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
David Chenc2869092019-02-20 13:47:19 +08001182 if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
Mario Limonciello9c273692018-12-11 08:16:14 -06001183 netif_dbg(tp, probe, tp->netdev,
1184 "Invalid variant for MAC pass through\n");
1185 return -ENODEV;
1186 }
1187 }
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001188
1189 /* returns _AUXMAC_#AABBCCDDEEFF# */
1190 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1191 obj = (union acpi_object *)buffer.pointer;
1192 if (!ACPI_SUCCESS(status))
1193 return -ENODEV;
1194 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1195 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001196 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001197 obj->type, obj->string.length);
1198 goto amacout;
1199 }
1200 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1201 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1202 netif_warn(tp, probe, tp->netdev,
1203 "Invalid header when reading pass-thru MAC addr\n");
1204 goto amacout;
1205 }
1206 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1207 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1208 netif_warn(tp, probe, tp->netdev,
hayeswang53700f02016-09-01 17:01:42 +08001209 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1210 ret, buf);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001211 ret = -EINVAL;
1212 goto amacout;
1213 }
1214 memcpy(sa->sa_data, buf, 6);
Mario Limonciello34ee32c2016-07-11 19:58:04 -05001215 netif_info(tp, probe, tp->netdev,
1216 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1217
1218amacout:
1219 kfree(obj);
1220 return ret;
1221}
1222
Mario Limonciello25766272019-04-04 13:46:53 -05001223static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1224{
1225 struct net_device *dev = tp->netdev;
1226 int ret;
1227
Crag.Wanga6cbcb72019-04-22 13:03:43 +08001228 sa->sa_family = dev->type;
1229
Mario Limonciello25766272019-04-04 13:46:53 -05001230 if (tp->version == RTL_VER_01) {
1231 ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1232 } else {
1233 /* if device doesn't support MAC pass through this will
1234 * be expected to be non-zero
1235 */
1236 ret = vendor_mac_passthru_addr_read(tp, sa);
1237 if (ret < 0)
1238 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1239 }
1240
1241 if (ret < 0) {
1242 netif_err(tp, probe, dev, "Get ether addr fail\n");
1243 } else if (!is_valid_ether_addr(sa->sa_data)) {
1244 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1245 sa->sa_data);
1246 eth_hw_addr_random(dev);
1247 ether_addr_copy(sa->sa_data, dev->dev_addr);
1248 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1249 sa->sa_data);
1250 return 0;
1251 }
1252
1253 return ret;
1254}
1255
hayeswang179bb6d2014-09-04 16:15:42 +08001256static int set_ethernet_addr(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00001257{
1258 struct net_device *dev = tp->netdev;
hayeswang179bb6d2014-09-04 16:15:42 +08001259 struct sockaddr sa;
hayeswang8a91c822014-02-18 21:49:01 +08001260 int ret;
hayeswangac718b62013-05-02 16:01:25 +00001261
Mario Limonciello25766272019-04-04 13:46:53 -05001262 ret = determine_ethernet_addr(tp, &sa);
1263 if (ret < 0)
1264 return ret;
hayeswang8a91c822014-02-18 21:49:01 +08001265
Mario Limonciello25766272019-04-04 13:46:53 -05001266 if (tp->version == RTL_VER_01)
1267 ether_addr_copy(dev->dev_addr, sa.sa_data);
1268 else
hayeswang179bb6d2014-09-04 16:15:42 +08001269 ret = rtl8152_set_mac_address(dev, &sa);
hayeswang179bb6d2014-09-04 16:15:42 +08001270
1271 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001272}
1273
hayeswangac718b62013-05-02 16:01:25 +00001274static void read_bulk_callback(struct urb *urb)
1275{
hayeswangac718b62013-05-02 16:01:25 +00001276 struct net_device *netdev;
hayeswangac718b62013-05-02 16:01:25 +00001277 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +08001278 struct rx_agg *agg;
1279 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001280 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001281
hayeswangebc2ec482013-08-14 20:54:38 +08001282 agg = urb->context;
1283 if (!agg)
1284 return;
1285
1286 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001287 if (!tp)
1288 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001289
hayeswangac718b62013-05-02 16:01:25 +00001290 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1291 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001292
1293 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00001294 return;
1295
hayeswangebc2ec482013-08-14 20:54:38 +08001296 netdev = tp->netdev;
hayeswang7559fb2f2013-08-16 16:09:38 +08001297
1298 /* When link down, the driver would cancel all bulks. */
1299 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08001300 if (!netif_carrier_ok(netdev))
1301 return;
1302
hayeswang9a4be1b2014-02-18 21:49:07 +08001303 usb_mark_last_busy(tp->udev);
1304
hayeswangac718b62013-05-02 16:01:25 +00001305 switch (status) {
1306 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +08001307 if (urb->actual_length < ETH_ZLEN)
1308 break;
1309
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001310 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001311 list_add_tail(&agg->list, &tp->rx_done);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001312 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08001313 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001314 return;
hayeswangac718b62013-05-02 16:01:25 +00001315 case -ESHUTDOWN:
1316 set_bit(RTL8152_UNPLUG, &tp->flags);
1317 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001318 return;
hayeswangac718b62013-05-02 16:01:25 +00001319 case -ENOENT:
1320 return; /* the urb is in unlink state */
1321 case -ETIME:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001322 if (net_ratelimit())
1323 netdev_warn(netdev, "maybe reset is needed?\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001324 break;
hayeswangac718b62013-05-02 16:01:25 +00001325 default:
Hayes Wang4a8deae2014-01-07 11:18:22 +08001326 if (net_ratelimit())
1327 netdev_warn(netdev, "Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001328 break;
hayeswangac718b62013-05-02 16:01:25 +00001329 }
1330
hayeswanga0fccd42014-11-20 10:29:05 +08001331 r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001332}
1333
1334static void write_bulk_callback(struct urb *urb)
1335{
hayeswangebc2ec482013-08-14 20:54:38 +08001336 struct net_device_stats *stats;
hayeswangd104eaf2014-03-06 15:07:17 +08001337 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08001338 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +00001339 struct r8152 *tp;
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001340 unsigned long flags;
hayeswangac718b62013-05-02 16:01:25 +00001341 int status = urb->status;
1342
hayeswangebc2ec482013-08-14 20:54:38 +08001343 agg = urb->context;
1344 if (!agg)
1345 return;
1346
1347 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +00001348 if (!tp)
1349 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001350
hayeswangd104eaf2014-03-06 15:07:17 +08001351 netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001352 stats = &netdev->stats;
hayeswangebc2ec482013-08-14 20:54:38 +08001353 if (status) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08001354 if (net_ratelimit())
hayeswangd104eaf2014-03-06 15:07:17 +08001355 netdev_warn(netdev, "Tx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +08001356 stats->tx_errors += agg->skb_num;
1357 } else {
1358 stats->tx_packets += agg->skb_num;
1359 stats->tx_bytes += agg->skb_len;
1360 }
1361
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001362 spin_lock_irqsave(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001363 list_add_tail(&agg->list, &tp->tx_free);
Sebastian Andrzej Siewiored7aa302018-06-20 21:31:20 +02001364 spin_unlock_irqrestore(&tp->tx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08001365
hayeswang9a4be1b2014-02-18 21:49:07 +08001366 usb_autopm_put_interface_async(tp->intf);
1367
hayeswangd104eaf2014-03-06 15:07:17 +08001368 if (!netif_carrier_ok(netdev))
hayeswangac718b62013-05-02 16:01:25 +00001369 return;
hayeswangebc2ec482013-08-14 20:54:38 +08001370
1371 if (!test_bit(WORK_ENABLE, &tp->flags))
1372 return;
1373
1374 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1375 return;
1376
1377 if (!skb_queue_empty(&tp->tx_queue))
hayeswangd823ab62015-01-12 12:06:23 +08001378 napi_schedule(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08001379}
1380
hayeswang40a82912013-08-14 20:54:40 +08001381static void intr_callback(struct urb *urb)
1382{
1383 struct r8152 *tp;
hayeswang500b6d72013-11-20 17:30:57 +08001384 __le16 *d;
hayeswang40a82912013-08-14 20:54:40 +08001385 int status = urb->status;
1386 int res;
1387
1388 tp = urb->context;
1389 if (!tp)
1390 return;
1391
1392 if (!test_bit(WORK_ENABLE, &tp->flags))
1393 return;
1394
1395 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1396 return;
1397
1398 switch (status) {
1399 case 0: /* success */
1400 break;
1401 case -ECONNRESET: /* unlink */
1402 case -ESHUTDOWN:
1403 netif_device_detach(tp->netdev);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05001404 /* fall through */
hayeswang40a82912013-08-14 20:54:40 +08001405 case -ENOENT:
hayeswangd59c8762014-10-31 13:35:57 +08001406 case -EPROTO:
1407 netif_info(tp, intr, tp->netdev,
1408 "Stop submitting intr, status %d\n", status);
hayeswang40a82912013-08-14 20:54:40 +08001409 return;
1410 case -EOVERFLOW:
1411 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1412 goto resubmit;
1413 /* -EPIPE: should clear the halt */
1414 default:
1415 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1416 goto resubmit;
1417 }
1418
1419 d = urb->transfer_buffer;
1420 if (INTR_LINK & __le16_to_cpu(d[0])) {
hayeswang51d979f2015-02-06 11:30:47 +08001421 if (!netif_carrier_ok(tp->netdev)) {
hayeswang40a82912013-08-14 20:54:40 +08001422 set_bit(RTL8152_LINK_CHG, &tp->flags);
1423 schedule_delayed_work(&tp->schedule, 0);
1424 }
1425 } else {
hayeswang51d979f2015-02-06 11:30:47 +08001426 if (netif_carrier_ok(tp->netdev)) {
hayeswang2f25abe2017-03-23 19:14:19 +08001427 netif_stop_queue(tp->netdev);
hayeswang40a82912013-08-14 20:54:40 +08001428 set_bit(RTL8152_LINK_CHG, &tp->flags);
1429 schedule_delayed_work(&tp->schedule, 0);
1430 }
1431 }
1432
1433resubmit:
1434 res = usb_submit_urb(urb, GFP_ATOMIC);
hayeswang67610492014-10-30 11:46:40 +08001435 if (res == -ENODEV) {
1436 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08001437 netif_device_detach(tp->netdev);
hayeswang67610492014-10-30 11:46:40 +08001438 } else if (res) {
hayeswang40a82912013-08-14 20:54:40 +08001439 netif_err(tp, intr, tp->netdev,
Hayes Wang4a8deae2014-01-07 11:18:22 +08001440 "can't resubmit intr, status %d\n", res);
hayeswang67610492014-10-30 11:46:40 +08001441 }
hayeswang40a82912013-08-14 20:54:40 +08001442}
1443
hayeswangebc2ec482013-08-14 20:54:38 +08001444static inline void *rx_agg_align(void *data)
1445{
hayeswang8e1f51b2014-01-02 11:22:41 +08001446 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001447}
1448
1449static inline void *tx_agg_align(void *data)
1450{
hayeswang8e1f51b2014-01-02 11:22:41 +08001451 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
hayeswangebc2ec482013-08-14 20:54:38 +08001452}
1453
1454static void free_all_mem(struct r8152 *tp)
1455{
1456 int i;
1457
1458 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001459 usb_free_urb(tp->rx_info[i].urb);
1460 tp->rx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001461
hayeswang9629e3c2014-01-15 10:42:15 +08001462 kfree(tp->rx_info[i].buffer);
1463 tp->rx_info[i].buffer = NULL;
1464 tp->rx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001465 }
1466
1467 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang9629e3c2014-01-15 10:42:15 +08001468 usb_free_urb(tp->tx_info[i].urb);
1469 tp->tx_info[i].urb = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001470
hayeswang9629e3c2014-01-15 10:42:15 +08001471 kfree(tp->tx_info[i].buffer);
1472 tp->tx_info[i].buffer = NULL;
1473 tp->tx_info[i].head = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001474 }
hayeswang40a82912013-08-14 20:54:40 +08001475
hayeswang9629e3c2014-01-15 10:42:15 +08001476 usb_free_urb(tp->intr_urb);
1477 tp->intr_urb = NULL;
hayeswang40a82912013-08-14 20:54:40 +08001478
hayeswang9629e3c2014-01-15 10:42:15 +08001479 kfree(tp->intr_buff);
1480 tp->intr_buff = NULL;
hayeswangebc2ec482013-08-14 20:54:38 +08001481}
1482
1483static int alloc_all_mem(struct r8152 *tp)
1484{
1485 struct net_device *netdev = tp->netdev;
hayeswang40a82912013-08-14 20:54:40 +08001486 struct usb_interface *intf = tp->intf;
1487 struct usb_host_interface *alt = intf->cur_altsetting;
1488 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
hayeswangebc2ec482013-08-14 20:54:38 +08001489 struct urb *urb;
1490 int node, i;
1491 u8 *buf;
1492
1493 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1494
1495 spin_lock_init(&tp->rx_lock);
1496 spin_lock_init(&tp->tx_lock);
hayeswangebc2ec482013-08-14 20:54:38 +08001497 INIT_LIST_HEAD(&tp->tx_free);
hayeswang98d068a2017-03-14 14:15:20 +08001498 INIT_LIST_HEAD(&tp->rx_done);
hayeswangebc2ec482013-08-14 20:54:38 +08001499 skb_queue_head_init(&tp->tx_queue);
hayeswangd823ab62015-01-12 12:06:23 +08001500 skb_queue_head_init(&tp->rx_queue);
hayeswangebc2ec482013-08-14 20:54:38 +08001501
1502 for (i = 0; i < RTL8152_MAX_RX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001503 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001504 if (!buf)
1505 goto err1;
1506
1507 if (buf != rx_agg_align(buf)) {
1508 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001509 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001510 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001511 if (!buf)
1512 goto err1;
1513 }
1514
1515 urb = usb_alloc_urb(0, GFP_KERNEL);
1516 if (!urb) {
1517 kfree(buf);
1518 goto err1;
1519 }
1520
1521 INIT_LIST_HEAD(&tp->rx_info[i].list);
1522 tp->rx_info[i].context = tp;
1523 tp->rx_info[i].urb = urb;
1524 tp->rx_info[i].buffer = buf;
1525 tp->rx_info[i].head = rx_agg_align(buf);
1526 }
1527
1528 for (i = 0; i < RTL8152_MAX_TX; i++) {
hayeswang52aec122014-09-02 10:27:52 +08001529 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
hayeswangebc2ec482013-08-14 20:54:38 +08001530 if (!buf)
1531 goto err1;
1532
1533 if (buf != tx_agg_align(buf)) {
1534 kfree(buf);
hayeswang52aec122014-09-02 10:27:52 +08001535 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
hayeswang8e1f51b2014-01-02 11:22:41 +08001536 node);
hayeswangebc2ec482013-08-14 20:54:38 +08001537 if (!buf)
1538 goto err1;
1539 }
1540
1541 urb = usb_alloc_urb(0, GFP_KERNEL);
1542 if (!urb) {
1543 kfree(buf);
1544 goto err1;
1545 }
1546
1547 INIT_LIST_HEAD(&tp->tx_info[i].list);
1548 tp->tx_info[i].context = tp;
1549 tp->tx_info[i].urb = urb;
1550 tp->tx_info[i].buffer = buf;
1551 tp->tx_info[i].head = tx_agg_align(buf);
1552
1553 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1554 }
1555
hayeswang40a82912013-08-14 20:54:40 +08001556 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1557 if (!tp->intr_urb)
1558 goto err1;
1559
1560 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1561 if (!tp->intr_buff)
1562 goto err1;
1563
1564 tp->intr_interval = (int)ep_intr->desc.bInterval;
1565 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
hayeswangb209af92014-08-25 15:53:00 +08001566 tp->intr_buff, INTBUFSIZE, intr_callback,
1567 tp, tp->intr_interval);
hayeswang40a82912013-08-14 20:54:40 +08001568
hayeswangebc2ec482013-08-14 20:54:38 +08001569 return 0;
1570
1571err1:
1572 free_all_mem(tp);
1573 return -ENOMEM;
1574}
1575
hayeswang0de98f62013-08-16 16:09:35 +08001576static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1577{
1578 struct tx_agg *agg = NULL;
1579 unsigned long flags;
1580
hayeswang21949ab2014-03-07 11:04:35 +08001581 if (list_empty(&tp->tx_free))
1582 return NULL;
1583
hayeswang0de98f62013-08-16 16:09:35 +08001584 spin_lock_irqsave(&tp->tx_lock, flags);
1585 if (!list_empty(&tp->tx_free)) {
1586 struct list_head *cursor;
1587
1588 cursor = tp->tx_free.next;
1589 list_del_init(cursor);
1590 agg = list_entry(cursor, struct tx_agg, list);
1591 }
1592 spin_unlock_irqrestore(&tp->tx_lock, flags);
1593
1594 return agg;
1595}
1596
hayeswangb209af92014-08-25 15:53:00 +08001597/* r8152_csum_workaround()
hayeswang6128d1bb2014-03-07 11:04:40 +08001598 * The hw limites the value the transport offset. When the offset is out of the
1599 * range, calculate the checksum by sw.
1600 */
1601static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1602 struct sk_buff_head *list)
1603{
1604 if (skb_shinfo(skb)->gso_size) {
1605 netdev_features_t features = tp->netdev->features;
1606 struct sk_buff_head seg_list;
1607 struct sk_buff *segs, *nskb;
1608
hayeswanga91d45f2014-07-11 16:48:27 +08001609 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
hayeswang6128d1bb2014-03-07 11:04:40 +08001610 segs = skb_gso_segment(skb, features);
1611 if (IS_ERR(segs) || !segs)
1612 goto drop;
1613
1614 __skb_queue_head_init(&seg_list);
1615
1616 do {
1617 nskb = segs;
1618 segs = segs->next;
1619 nskb->next = NULL;
1620 __skb_queue_tail(&seg_list, nskb);
1621 } while (segs);
1622
1623 skb_queue_splice(&seg_list, list);
1624 dev_kfree_skb(skb);
1625 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1626 if (skb_checksum_help(skb) < 0)
1627 goto drop;
1628
1629 __skb_queue_head(list, skb);
1630 } else {
1631 struct net_device_stats *stats;
1632
1633drop:
1634 stats = &tp->netdev->stats;
1635 stats->tx_dropped++;
1636 dev_kfree_skb(skb);
1637 }
1638}
1639
hayeswangb209af92014-08-25 15:53:00 +08001640/* msdn_giant_send_check()
hayeswang6128d1bb2014-03-07 11:04:40 +08001641 * According to the document of microsoft, the TCP Pseudo Header excludes the
1642 * packet length for IPv6 TCP large packets.
1643 */
1644static int msdn_giant_send_check(struct sk_buff *skb)
1645{
1646 const struct ipv6hdr *ipv6h;
1647 struct tcphdr *th;
hayeswangfcb308d2014-03-11 10:20:32 +08001648 int ret;
1649
1650 ret = skb_cow_head(skb, 0);
1651 if (ret)
1652 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001653
1654 ipv6h = ipv6_hdr(skb);
1655 th = tcp_hdr(skb);
1656
1657 th->check = 0;
1658 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1659
hayeswangfcb308d2014-03-11 10:20:32 +08001660 return ret;
hayeswang6128d1bb2014-03-07 11:04:40 +08001661}
1662
hayeswangc5554292014-09-12 10:43:11 +08001663static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1664{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001665 if (skb_vlan_tag_present(skb)) {
hayeswangc5554292014-09-12 10:43:11 +08001666 u32 opts2;
1667
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001668 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
hayeswangc5554292014-09-12 10:43:11 +08001669 desc->opts2 |= cpu_to_le32(opts2);
1670 }
1671}
1672
1673static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1674{
1675 u32 opts2 = le32_to_cpu(desc->opts2);
1676
1677 if (opts2 & RX_VLAN_TAG)
1678 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1679 swab16(opts2 & 0xffff));
1680}
1681
hayeswang60c89072014-03-07 11:04:39 +08001682static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1683 struct sk_buff *skb, u32 len, u32 transport_offset)
1684{
1685 u32 mss = skb_shinfo(skb)->gso_size;
1686 u32 opts1, opts2 = 0;
1687 int ret = TX_CSUM_SUCCESS;
1688
1689 WARN_ON_ONCE(len > TX_LEN_MAX);
1690
1691 opts1 = len | TX_FS | TX_LS;
1692
1693 if (mss) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001694 if (transport_offset > GTTCPHO_MAX) {
1695 netif_warn(tp, tx_err, tp->netdev,
1696 "Invalid transport offset 0x%x for TSO\n",
1697 transport_offset);
1698 ret = TX_CSUM_TSO;
1699 goto unavailable;
1700 }
1701
hayeswang6e74d172015-02-06 11:30:50 +08001702 switch (vlan_get_protocol(skb)) {
hayeswang60c89072014-03-07 11:04:39 +08001703 case htons(ETH_P_IP):
1704 opts1 |= GTSENDV4;
1705 break;
1706
hayeswang6128d1bb2014-03-07 11:04:40 +08001707 case htons(ETH_P_IPV6):
hayeswangfcb308d2014-03-11 10:20:32 +08001708 if (msdn_giant_send_check(skb)) {
1709 ret = TX_CSUM_TSO;
1710 goto unavailable;
1711 }
hayeswang6128d1bb2014-03-07 11:04:40 +08001712 opts1 |= GTSENDV6;
hayeswang6128d1bb2014-03-07 11:04:40 +08001713 break;
1714
hayeswang60c89072014-03-07 11:04:39 +08001715 default:
1716 WARN_ON_ONCE(1);
1717 break;
1718 }
1719
1720 opts1 |= transport_offset << GTTCPHO_SHIFT;
1721 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1722 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswang5bd23882013-08-14 20:54:39 +08001723 u8 ip_protocol;
hayeswang5bd23882013-08-14 20:54:39 +08001724
hayeswang6128d1bb2014-03-07 11:04:40 +08001725 if (transport_offset > TCPHO_MAX) {
1726 netif_warn(tp, tx_err, tp->netdev,
1727 "Invalid transport offset 0x%x\n",
1728 transport_offset);
1729 ret = TX_CSUM_NONE;
1730 goto unavailable;
1731 }
1732
hayeswang6e74d172015-02-06 11:30:50 +08001733 switch (vlan_get_protocol(skb)) {
hayeswang5bd23882013-08-14 20:54:39 +08001734 case htons(ETH_P_IP):
1735 opts2 |= IPV4_CS;
1736 ip_protocol = ip_hdr(skb)->protocol;
1737 break;
1738
1739 case htons(ETH_P_IPV6):
1740 opts2 |= IPV6_CS;
1741 ip_protocol = ipv6_hdr(skb)->nexthdr;
1742 break;
1743
1744 default:
1745 ip_protocol = IPPROTO_RAW;
1746 break;
1747 }
1748
hayeswang60c89072014-03-07 11:04:39 +08001749 if (ip_protocol == IPPROTO_TCP)
hayeswang5bd23882013-08-14 20:54:39 +08001750 opts2 |= TCP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001751 else if (ip_protocol == IPPROTO_UDP)
hayeswang5bd23882013-08-14 20:54:39 +08001752 opts2 |= UDP_CS;
hayeswang60c89072014-03-07 11:04:39 +08001753 else
hayeswang5bd23882013-08-14 20:54:39 +08001754 WARN_ON_ONCE(1);
hayeswang5bd23882013-08-14 20:54:39 +08001755
hayeswang60c89072014-03-07 11:04:39 +08001756 opts2 |= transport_offset << TCPHO_SHIFT;
hayeswang5bd23882013-08-14 20:54:39 +08001757 }
hayeswang60c89072014-03-07 11:04:39 +08001758
1759 desc->opts2 = cpu_to_le32(opts2);
1760 desc->opts1 = cpu_to_le32(opts1);
1761
hayeswang6128d1bb2014-03-07 11:04:40 +08001762unavailable:
hayeswang60c89072014-03-07 11:04:39 +08001763 return ret;
hayeswang5bd23882013-08-14 20:54:39 +08001764}
1765
hayeswangb1379d92013-08-16 16:09:37 +08001766static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1767{
hayeswangd84130a2014-02-18 21:49:02 +08001768 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang9a4be1b2014-02-18 21:49:07 +08001769 int remain, ret;
hayeswangb1379d92013-08-16 16:09:37 +08001770 u8 *tx_data;
1771
hayeswangd84130a2014-02-18 21:49:02 +08001772 __skb_queue_head_init(&skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001773 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001774 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang0c3121f2014-03-07 11:04:36 +08001775 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001776
hayeswangb1379d92013-08-16 16:09:37 +08001777 tx_data = agg->head;
hayeswangb209af92014-08-25 15:53:00 +08001778 agg->skb_num = 0;
1779 agg->skb_len = 0;
hayeswang52aec122014-09-02 10:27:52 +08001780 remain = agg_buf_sz;
hayeswangb1379d92013-08-16 16:09:37 +08001781
hayeswang7937f9e2013-11-20 17:30:54 +08001782 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
hayeswangb1379d92013-08-16 16:09:37 +08001783 struct tx_desc *tx_desc;
1784 struct sk_buff *skb;
1785 unsigned int len;
hayeswang60c89072014-03-07 11:04:39 +08001786 u32 offset;
hayeswangb1379d92013-08-16 16:09:37 +08001787
hayeswangd84130a2014-02-18 21:49:02 +08001788 skb = __skb_dequeue(&skb_head);
hayeswangb1379d92013-08-16 16:09:37 +08001789 if (!skb)
1790 break;
1791
hayeswang60c89072014-03-07 11:04:39 +08001792 len = skb->len + sizeof(*tx_desc);
1793
1794 if (len > remain) {
hayeswangd84130a2014-02-18 21:49:02 +08001795 __skb_queue_head(&skb_head, skb);
hayeswangb1379d92013-08-16 16:09:37 +08001796 break;
1797 }
1798
hayeswang7937f9e2013-11-20 17:30:54 +08001799 tx_data = tx_agg_align(tx_data);
hayeswangb1379d92013-08-16 16:09:37 +08001800 tx_desc = (struct tx_desc *)tx_data;
hayeswang60c89072014-03-07 11:04:39 +08001801
1802 offset = (u32)skb_transport_offset(skb);
1803
hayeswang6128d1bb2014-03-07 11:04:40 +08001804 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1805 r8152_csum_workaround(tp, skb, &skb_head);
1806 continue;
1807 }
hayeswang60c89072014-03-07 11:04:39 +08001808
hayeswangc5554292014-09-12 10:43:11 +08001809 rtl_tx_vlan_tag(tx_desc, skb);
1810
hayeswangb1379d92013-08-16 16:09:37 +08001811 tx_data += sizeof(*tx_desc);
1812
hayeswang60c89072014-03-07 11:04:39 +08001813 len = skb->len;
1814 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1815 struct net_device_stats *stats = &tp->netdev->stats;
1816
1817 stats->tx_dropped++;
1818 dev_kfree_skb_any(skb);
1819 tx_data -= sizeof(*tx_desc);
1820 continue;
1821 }
hayeswangb1379d92013-08-16 16:09:37 +08001822
hayeswang7937f9e2013-11-20 17:30:54 +08001823 tx_data += len;
hayeswang60c89072014-03-07 11:04:39 +08001824 agg->skb_len += len;
Eric Dumazet4c27bf32018-02-25 19:12:10 -08001825 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
hayeswang60c89072014-03-07 11:04:39 +08001826
1827 dev_kfree_skb_any(skb);
1828
hayeswang52aec122014-09-02 10:27:52 +08001829 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08001830
1831 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1832 break;
hayeswangb1379d92013-08-16 16:09:37 +08001833 }
1834
hayeswangd84130a2014-02-18 21:49:02 +08001835 if (!skb_queue_empty(&skb_head)) {
hayeswang0c3121f2014-03-07 11:04:36 +08001836 spin_lock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001837 skb_queue_splice(&skb_head, tx_queue);
hayeswang0c3121f2014-03-07 11:04:36 +08001838 spin_unlock(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08001839 }
1840
hayeswang0c3121f2014-03-07 11:04:36 +08001841 netif_tx_lock(tp->netdev);
hayeswangdd1b1192013-11-20 17:30:56 +08001842
1843 if (netif_queue_stopped(tp->netdev) &&
1844 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1845 netif_wake_queue(tp->netdev);
1846
hayeswang0c3121f2014-03-07 11:04:36 +08001847 netif_tx_unlock(tp->netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08001848
hayeswang0c3121f2014-03-07 11:04:36 +08001849 ret = usb_autopm_get_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001850 if (ret < 0)
1851 goto out_tx_fill;
hayeswangdd1b1192013-11-20 17:30:56 +08001852
hayeswangb1379d92013-08-16 16:09:37 +08001853 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1854 agg->head, (int)(tx_data - (u8 *)agg->head),
1855 (usb_complete_t)write_bulk_callback, agg);
1856
hayeswang0c3121f2014-03-07 11:04:36 +08001857 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswang9a4be1b2014-02-18 21:49:07 +08001858 if (ret < 0)
hayeswang0c3121f2014-03-07 11:04:36 +08001859 usb_autopm_put_interface_async(tp->intf);
hayeswang9a4be1b2014-02-18 21:49:07 +08001860
1861out_tx_fill:
1862 return ret;
hayeswangb1379d92013-08-16 16:09:37 +08001863}
1864
hayeswang565cab02014-03-07 11:04:38 +08001865static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1866{
1867 u8 checksum = CHECKSUM_NONE;
1868 u32 opts2, opts3;
1869
hayeswang19c0f402017-01-11 16:25:34 +08001870 if (!(tp->netdev->features & NETIF_F_RXCSUM))
hayeswang565cab02014-03-07 11:04:38 +08001871 goto return_result;
1872
1873 opts2 = le32_to_cpu(rx_desc->opts2);
1874 opts3 = le32_to_cpu(rx_desc->opts3);
1875
1876 if (opts2 & RD_IPV4_CS) {
1877 if (opts3 & IPF)
1878 checksum = CHECKSUM_NONE;
Hayes Wangea6499e2018-02-02 16:43:35 +08001879 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1880 checksum = CHECKSUM_UNNECESSARY;
1881 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
hayeswang565cab02014-03-07 11:04:38 +08001882 checksum = CHECKSUM_UNNECESSARY;
Mark Lordb9a321b2016-10-30 19:28:27 -04001883 } else if (opts2 & RD_IPV6_CS) {
hayeswang6128d1bb2014-03-07 11:04:40 +08001884 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1885 checksum = CHECKSUM_UNNECESSARY;
1886 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1887 checksum = CHECKSUM_UNNECESSARY;
hayeswang565cab02014-03-07 11:04:38 +08001888 }
1889
1890return_result:
1891 return checksum;
1892}
1893
hayeswangd823ab62015-01-12 12:06:23 +08001894static int rx_bottom(struct r8152 *tp, int budget)
hayeswangebc2ec482013-08-14 20:54:38 +08001895{
hayeswanga5a4f462013-08-16 16:09:34 +08001896 unsigned long flags;
hayeswangd84130a2014-02-18 21:49:02 +08001897 struct list_head *cursor, *next, rx_queue;
hayeswange1a2ca92015-02-06 11:30:45 +08001898 int ret = 0, work_done = 0;
hayeswangce594e92017-03-16 14:32:22 +08001899 struct napi_struct *napi = &tp->napi;
hayeswangd823ab62015-01-12 12:06:23 +08001900
1901 if (!skb_queue_empty(&tp->rx_queue)) {
1902 while (work_done < budget) {
1903 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1904 struct net_device *netdev = tp->netdev;
1905 struct net_device_stats *stats = &netdev->stats;
1906 unsigned int pkt_len;
1907
1908 if (!skb)
1909 break;
1910
1911 pkt_len = skb->len;
hayeswangce594e92017-03-16 14:32:22 +08001912 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001913 work_done++;
1914 stats->rx_packets++;
1915 stats->rx_bytes += pkt_len;
1916 }
1917 }
hayeswangebc2ec482013-08-14 20:54:38 +08001918
hayeswangd84130a2014-02-18 21:49:02 +08001919 if (list_empty(&tp->rx_done))
hayeswangd823ab62015-01-12 12:06:23 +08001920 goto out1;
hayeswangd84130a2014-02-18 21:49:02 +08001921
1922 INIT_LIST_HEAD(&rx_queue);
hayeswanga5a4f462013-08-16 16:09:34 +08001923 spin_lock_irqsave(&tp->rx_lock, flags);
hayeswangd84130a2014-02-18 21:49:02 +08001924 list_splice_init(&tp->rx_done, &rx_queue);
1925 spin_unlock_irqrestore(&tp->rx_lock, flags);
1926
1927 list_for_each_safe(cursor, next, &rx_queue) {
hayeswang43a44782013-08-16 16:09:36 +08001928 struct rx_desc *rx_desc;
1929 struct rx_agg *agg;
hayeswang43a44782013-08-16 16:09:36 +08001930 int len_used = 0;
1931 struct urb *urb;
1932 u8 *rx_data;
hayeswang43a44782013-08-16 16:09:36 +08001933
hayeswangebc2ec482013-08-14 20:54:38 +08001934 list_del_init(cursor);
hayeswangebc2ec482013-08-14 20:54:38 +08001935
1936 agg = list_entry(cursor, struct rx_agg, list);
1937 urb = agg->urb;
hayeswang0de98f62013-08-16 16:09:35 +08001938 if (urb->actual_length < ETH_ZLEN)
1939 goto submit;
hayeswangebc2ec482013-08-14 20:54:38 +08001940
hayeswangebc2ec482013-08-14 20:54:38 +08001941 rx_desc = agg->head;
1942 rx_data = agg->head;
hayeswang7937f9e2013-11-20 17:30:54 +08001943 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001944
hayeswang7937f9e2013-11-20 17:30:54 +08001945 while (urb->actual_length > len_used) {
hayeswang43a44782013-08-16 16:09:36 +08001946 struct net_device *netdev = tp->netdev;
hayeswang05e0f1a2014-03-06 15:07:18 +08001947 struct net_device_stats *stats = &netdev->stats;
hayeswang7937f9e2013-11-20 17:30:54 +08001948 unsigned int pkt_len;
hayeswang43a44782013-08-16 16:09:36 +08001949 struct sk_buff *skb;
1950
hayeswang74544452017-06-09 17:11:47 +08001951 /* limite the skb numbers for rx_queue */
1952 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
1953 break;
1954
hayeswang7937f9e2013-11-20 17:30:54 +08001955 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
hayeswangebc2ec482013-08-14 20:54:38 +08001956 if (pkt_len < ETH_ZLEN)
1957 break;
1958
hayeswang7937f9e2013-11-20 17:30:54 +08001959 len_used += pkt_len;
1960 if (urb->actual_length < len_used)
1961 break;
1962
hayeswangb65c0c92017-06-21 11:25:18 +08001963 pkt_len -= ETH_FCS_LEN;
hayeswangebc2ec482013-08-14 20:54:38 +08001964 rx_data += sizeof(struct rx_desc);
1965
hayeswangce594e92017-03-16 14:32:22 +08001966 skb = napi_alloc_skb(napi, pkt_len);
hayeswangebc2ec482013-08-14 20:54:38 +08001967 if (!skb) {
1968 stats->rx_dropped++;
hayeswang5e2f7482014-03-07 11:04:37 +08001969 goto find_next_rx;
hayeswangebc2ec482013-08-14 20:54:38 +08001970 }
hayeswang565cab02014-03-07 11:04:38 +08001971
1972 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001973 memcpy(skb->data, rx_data, pkt_len);
1974 skb_put(skb, pkt_len);
1975 skb->protocol = eth_type_trans(skb, netdev);
hayeswangc5554292014-09-12 10:43:11 +08001976 rtl_rx_vlan_tag(rx_desc, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001977 if (work_done < budget) {
hayeswangce594e92017-03-16 14:32:22 +08001978 napi_gro_receive(napi, skb);
hayeswangd823ab62015-01-12 12:06:23 +08001979 work_done++;
1980 stats->rx_packets++;
1981 stats->rx_bytes += pkt_len;
1982 } else {
1983 __skb_queue_tail(&tp->rx_queue, skb);
1984 }
hayeswangebc2ec482013-08-14 20:54:38 +08001985
hayeswang5e2f7482014-03-07 11:04:37 +08001986find_next_rx:
hayeswangb65c0c92017-06-21 11:25:18 +08001987 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
hayeswangebc2ec482013-08-14 20:54:38 +08001988 rx_desc = (struct rx_desc *)rx_data;
hayeswangebc2ec482013-08-14 20:54:38 +08001989 len_used = (int)(rx_data - (u8 *)agg->head);
hayeswang7937f9e2013-11-20 17:30:54 +08001990 len_used += sizeof(struct rx_desc);
hayeswangebc2ec482013-08-14 20:54:38 +08001991 }
1992
hayeswang0de98f62013-08-16 16:09:35 +08001993submit:
hayeswange1a2ca92015-02-06 11:30:45 +08001994 if (!ret) {
1995 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1996 } else {
1997 urb->actual_length = 0;
1998 list_add_tail(&agg->list, next);
1999 }
2000 }
2001
2002 if (!list_empty(&rx_queue)) {
2003 spin_lock_irqsave(&tp->rx_lock, flags);
2004 list_splice_tail(&rx_queue, &tp->rx_done);
2005 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangebc2ec482013-08-14 20:54:38 +08002006 }
hayeswangd823ab62015-01-12 12:06:23 +08002007
2008out1:
2009 return work_done;
hayeswangebc2ec482013-08-14 20:54:38 +08002010}
2011
2012static void tx_bottom(struct r8152 *tp)
2013{
hayeswangebc2ec482013-08-14 20:54:38 +08002014 int res;
2015
hayeswangb1379d92013-08-16 16:09:37 +08002016 do {
2017 struct tx_agg *agg;
hayeswangebc2ec482013-08-14 20:54:38 +08002018
hayeswangb1379d92013-08-16 16:09:37 +08002019 if (skb_queue_empty(&tp->tx_queue))
hayeswangebc2ec482013-08-14 20:54:38 +08002020 break;
2021
hayeswangb1379d92013-08-16 16:09:37 +08002022 agg = r8152_get_tx_agg(tp);
2023 if (!agg)
hayeswangebc2ec482013-08-14 20:54:38 +08002024 break;
hayeswangb1379d92013-08-16 16:09:37 +08002025
2026 res = r8152_tx_agg_fill(tp, agg);
2027 if (res) {
hayeswang05e0f1a2014-03-06 15:07:18 +08002028 struct net_device *netdev = tp->netdev;
hayeswangb1379d92013-08-16 16:09:37 +08002029
2030 if (res == -ENODEV) {
hayeswang67610492014-10-30 11:46:40 +08002031 set_bit(RTL8152_UNPLUG, &tp->flags);
hayeswangb1379d92013-08-16 16:09:37 +08002032 netif_device_detach(netdev);
2033 } else {
hayeswang05e0f1a2014-03-06 15:07:18 +08002034 struct net_device_stats *stats = &netdev->stats;
2035 unsigned long flags;
2036
hayeswangb1379d92013-08-16 16:09:37 +08002037 netif_warn(tp, tx_err, netdev,
2038 "failed tx_urb %d\n", res);
2039 stats->tx_dropped += agg->skb_num;
hayeswangdb8515e2014-03-06 15:07:16 +08002040
hayeswangb1379d92013-08-16 16:09:37 +08002041 spin_lock_irqsave(&tp->tx_lock, flags);
2042 list_add_tail(&agg->list, &tp->tx_free);
2043 spin_unlock_irqrestore(&tp->tx_lock, flags);
2044 }
hayeswangebc2ec482013-08-14 20:54:38 +08002045 }
hayeswangb1379d92013-08-16 16:09:37 +08002046 } while (res == 0);
hayeswangebc2ec482013-08-14 20:54:38 +08002047}
2048
hayeswangd823ab62015-01-12 12:06:23 +08002049static void bottom_half(struct r8152 *tp)
hayeswangebc2ec482013-08-14 20:54:38 +08002050{
hayeswangebc2ec482013-08-14 20:54:38 +08002051 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2052 return;
2053
2054 if (!test_bit(WORK_ENABLE, &tp->flags))
2055 return;
2056
hayeswang7559fb2f2013-08-16 16:09:38 +08002057 /* When link down, the driver would cancel all bulks. */
2058 /* This avoid the re-submitting bulk */
hayeswangebc2ec482013-08-14 20:54:38 +08002059 if (!netif_carrier_ok(tp->netdev))
2060 return;
2061
hayeswangd823ab62015-01-12 12:06:23 +08002062 clear_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang9451a112014-11-12 10:05:04 +08002063
hayeswang0c3121f2014-03-07 11:04:36 +08002064 tx_bottom(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002065}
2066
hayeswangd823ab62015-01-12 12:06:23 +08002067static int r8152_poll(struct napi_struct *napi, int budget)
2068{
2069 struct r8152 *tp = container_of(napi, struct r8152, napi);
2070 int work_done;
2071
2072 work_done = rx_bottom(tp, budget);
2073 bottom_half(tp);
2074
2075 if (work_done < budget) {
hayeswanga3307f92017-06-09 17:11:48 +08002076 if (!napi_complete_done(napi, work_done))
2077 goto out;
hayeswangd823ab62015-01-12 12:06:23 +08002078 if (!list_empty(&tp->rx_done))
2079 napi_schedule(napi);
hayeswang248b2132017-01-26 09:38:33 +08002080 else if (!skb_queue_empty(&tp->tx_queue) &&
2081 !list_empty(&tp->tx_free))
2082 napi_schedule(napi);
hayeswangd823ab62015-01-12 12:06:23 +08002083 }
2084
hayeswanga3307f92017-06-09 17:11:48 +08002085out:
hayeswangd823ab62015-01-12 12:06:23 +08002086 return work_done;
2087}
2088
hayeswangebc2ec482013-08-14 20:54:38 +08002089static
2090int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2091{
hayeswanga0fccd42014-11-20 10:29:05 +08002092 int ret;
2093
hayeswangef827a52015-01-09 10:26:36 +08002094 /* The rx would be stopped, so skip submitting */
2095 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2096 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2097 return 0;
2098
hayeswangebc2ec482013-08-14 20:54:38 +08002099 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
hayeswang52aec122014-09-02 10:27:52 +08002100 agg->head, agg_buf_sz,
hayeswangb209af92014-08-25 15:53:00 +08002101 (usb_complete_t)read_bulk_callback, agg);
hayeswangebc2ec482013-08-14 20:54:38 +08002102
hayeswanga0fccd42014-11-20 10:29:05 +08002103 ret = usb_submit_urb(agg->urb, mem_flags);
2104 if (ret == -ENODEV) {
2105 set_bit(RTL8152_UNPLUG, &tp->flags);
2106 netif_device_detach(tp->netdev);
2107 } else if (ret) {
2108 struct urb *urb = agg->urb;
2109 unsigned long flags;
2110
2111 urb->actual_length = 0;
2112 spin_lock_irqsave(&tp->rx_lock, flags);
2113 list_add_tail(&agg->list, &tp->rx_done);
2114 spin_unlock_irqrestore(&tp->rx_lock, flags);
hayeswangd823ab62015-01-12 12:06:23 +08002115
2116 netif_err(tp, rx_err, tp->netdev,
2117 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2118
2119 napi_schedule(&tp->napi);
hayeswanga0fccd42014-11-20 10:29:05 +08002120 }
2121
2122 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002123}
2124
hayeswang00a5e362014-02-18 21:48:59 +08002125static void rtl_drop_queued_tx(struct r8152 *tp)
2126{
2127 struct net_device_stats *stats = &tp->netdev->stats;
hayeswangd84130a2014-02-18 21:49:02 +08002128 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
hayeswang00a5e362014-02-18 21:48:59 +08002129 struct sk_buff *skb;
2130
hayeswangd84130a2014-02-18 21:49:02 +08002131 if (skb_queue_empty(tx_queue))
2132 return;
2133
2134 __skb_queue_head_init(&skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002135 spin_lock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002136 skb_queue_splice_init(tx_queue, &skb_head);
hayeswang2685d412014-03-07 11:04:34 +08002137 spin_unlock_bh(&tx_queue->lock);
hayeswangd84130a2014-02-18 21:49:02 +08002138
2139 while ((skb = __skb_dequeue(&skb_head))) {
hayeswang00a5e362014-02-18 21:48:59 +08002140 dev_kfree_skb(skb);
2141 stats->tx_dropped++;
2142 }
2143}
2144
hayeswangac718b62013-05-02 16:01:25 +00002145static void rtl8152_tx_timeout(struct net_device *netdev)
2146{
2147 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002148
Hayes Wang4a8deae2014-01-07 11:18:22 +08002149 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
hayeswang37608f32015-07-29 20:39:09 +08002150
2151 usb_queue_reset_device(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00002152}
2153
2154static void rtl8152_set_rx_mode(struct net_device *netdev)
2155{
2156 struct r8152 *tp = netdev_priv(netdev);
2157
hayeswang51d979f2015-02-06 11:30:47 +08002158 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00002159 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08002160 schedule_delayed_work(&tp->schedule, 0);
2161 }
hayeswangac718b62013-05-02 16:01:25 +00002162}
2163
2164static void _rtl8152_set_rx_mode(struct net_device *netdev)
2165{
2166 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08002167 u32 mc_filter[2]; /* Multicast hash filter */
2168 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00002169 u32 ocp_data;
2170
hayeswangac718b62013-05-02 16:01:25 +00002171 netif_stop_queue(netdev);
2172 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2173 ocp_data &= ~RCR_ACPT_ALL;
2174 ocp_data |= RCR_AB | RCR_APM;
2175
2176 if (netdev->flags & IFF_PROMISC) {
2177 /* Unconditionally log net taps. */
2178 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2179 ocp_data |= RCR_AM | RCR_AAP;
hayeswangb209af92014-08-25 15:53:00 +08002180 mc_filter[1] = 0xffffffff;
2181 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002182 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2183 (netdev->flags & IFF_ALLMULTI)) {
2184 /* Too many to filter perfectly -- accept all multicasts. */
2185 ocp_data |= RCR_AM;
hayeswangb209af92014-08-25 15:53:00 +08002186 mc_filter[1] = 0xffffffff;
2187 mc_filter[0] = 0xffffffff;
hayeswangac718b62013-05-02 16:01:25 +00002188 } else {
2189 struct netdev_hw_addr *ha;
2190
hayeswangb209af92014-08-25 15:53:00 +08002191 mc_filter[1] = 0;
2192 mc_filter[0] = 0;
hayeswangac718b62013-05-02 16:01:25 +00002193 netdev_for_each_mc_addr(ha, netdev) {
2194 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
hayeswangb209af92014-08-25 15:53:00 +08002195
hayeswangac718b62013-05-02 16:01:25 +00002196 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2197 ocp_data |= RCR_AM;
2198 }
2199 }
2200
hayeswang31787f52013-07-31 17:21:25 +08002201 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2202 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00002203
hayeswang31787f52013-07-31 17:21:25 +08002204 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00002205 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2206 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002207}
2208
hayeswanga5e31252015-01-06 17:41:58 +08002209static netdev_features_t
2210rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2211 netdev_features_t features)
2212{
2213 u32 mss = skb_shinfo(skb)->gso_size;
2214 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2215 int offset = skb_transport_offset(skb);
2216
2217 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
Tom Herberta1882222015-12-14 11:19:43 -08002218 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
hayeswanga5e31252015-01-06 17:41:58 +08002219 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2220 features &= ~NETIF_F_GSO_MASK;
2221
2222 return features;
2223}
2224
hayeswangac718b62013-05-02 16:01:25 +00002225static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
hayeswangb209af92014-08-25 15:53:00 +08002226 struct net_device *netdev)
hayeswangac718b62013-05-02 16:01:25 +00002227{
2228 struct r8152 *tp = netdev_priv(netdev);
hayeswangac718b62013-05-02 16:01:25 +00002229
hayeswangac718b62013-05-02 16:01:25 +00002230 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002231
hayeswang61598782013-11-20 17:30:55 +08002232 skb_queue_tail(&tp->tx_queue, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08002233
hayeswang0c3121f2014-03-07 11:04:36 +08002234 if (!list_empty(&tp->tx_free)) {
2235 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
hayeswangd823ab62015-01-12 12:06:23 +08002236 set_bit(SCHEDULE_NAPI, &tp->flags);
hayeswang0c3121f2014-03-07 11:04:36 +08002237 schedule_delayed_work(&tp->schedule, 0);
2238 } else {
2239 usb_mark_last_busy(tp->udev);
hayeswangd823ab62015-01-12 12:06:23 +08002240 napi_schedule(&tp->napi);
hayeswang0c3121f2014-03-07 11:04:36 +08002241 }
hayeswangb209af92014-08-25 15:53:00 +08002242 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
hayeswangdd1b1192013-11-20 17:30:56 +08002243 netif_stop_queue(netdev);
hayeswangb209af92014-08-25 15:53:00 +08002244 }
hayeswangdd1b1192013-11-20 17:30:56 +08002245
hayeswangac718b62013-05-02 16:01:25 +00002246 return NETDEV_TX_OK;
2247}
2248
2249static void r8152b_reset_packet_filter(struct r8152 *tp)
2250{
2251 u32 ocp_data;
2252
2253 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2254 ocp_data &= ~FMC_FCR_MCU_EN;
2255 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2256 ocp_data |= FMC_FCR_MCU_EN;
2257 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2258}
2259
2260static void rtl8152_nic_reset(struct r8152 *tp)
2261{
2262 int i;
2263
2264 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2265
2266 for (i = 0; i < 1000; i++) {
2267 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2268 break;
hayeswangb209af92014-08-25 15:53:00 +08002269 usleep_range(100, 400);
hayeswangac718b62013-05-02 16:01:25 +00002270 }
2271}
2272
hayeswangdd1b1192013-11-20 17:30:56 +08002273static void set_tx_qlen(struct r8152 *tp)
2274{
2275 struct net_device *netdev = tp->netdev;
2276
hayeswangb65c0c92017-06-21 11:25:18 +08002277 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
hayeswang52aec122014-09-02 10:27:52 +08002278 sizeof(struct tx_desc));
hayeswangdd1b1192013-11-20 17:30:56 +08002279}
2280
hayeswangac718b62013-05-02 16:01:25 +00002281static inline u8 rtl8152_get_speed(struct r8152 *tp)
2282{
2283 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2284}
2285
hayeswang507605a2014-01-02 11:22:43 +08002286static void rtl_set_eee_plus(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002287{
hayeswangebc2ec482013-08-14 20:54:38 +08002288 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002289 u8 speed;
2290
2291 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002292 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00002293 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002294 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002295 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2296 } else {
2297 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08002298 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00002299 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2300 }
hayeswang507605a2014-01-02 11:22:43 +08002301}
2302
hayeswang00a5e362014-02-18 21:48:59 +08002303static void rxdy_gated_en(struct r8152 *tp, bool enable)
2304{
2305 u32 ocp_data;
2306
2307 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2308 if (enable)
2309 ocp_data |= RXDY_GATED_EN;
2310 else
2311 ocp_data &= ~RXDY_GATED_EN;
2312 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2313}
2314
hayeswang445f7f42014-09-23 16:31:47 +08002315static int rtl_start_rx(struct r8152 *tp)
2316{
2317 int i, ret = 0;
2318
2319 INIT_LIST_HEAD(&tp->rx_done);
2320 for (i = 0; i < RTL8152_MAX_RX; i++) {
2321 INIT_LIST_HEAD(&tp->rx_info[i].list);
2322 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2323 if (ret)
2324 break;
2325 }
2326
hayeswang7bcf4f62014-11-20 10:29:06 +08002327 if (ret && ++i < RTL8152_MAX_RX) {
2328 struct list_head rx_queue;
2329 unsigned long flags;
2330
2331 INIT_LIST_HEAD(&rx_queue);
2332
2333 do {
2334 struct rx_agg *agg = &tp->rx_info[i++];
2335 struct urb *urb = agg->urb;
2336
2337 urb->actual_length = 0;
2338 list_add_tail(&agg->list, &rx_queue);
2339 } while (i < RTL8152_MAX_RX);
2340
2341 spin_lock_irqsave(&tp->rx_lock, flags);
2342 list_splice_tail(&rx_queue, &tp->rx_done);
2343 spin_unlock_irqrestore(&tp->rx_lock, flags);
2344 }
2345
hayeswang445f7f42014-09-23 16:31:47 +08002346 return ret;
2347}
2348
2349static int rtl_stop_rx(struct r8152 *tp)
2350{
2351 int i;
2352
2353 for (i = 0; i < RTL8152_MAX_RX; i++)
2354 usb_kill_urb(tp->rx_info[i].urb);
2355
hayeswangd823ab62015-01-12 12:06:23 +08002356 while (!skb_queue_empty(&tp->rx_queue))
2357 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2358
hayeswang445f7f42014-09-23 16:31:47 +08002359 return 0;
2360}
2361
hayeswang507605a2014-01-02 11:22:43 +08002362static int rtl_enable(struct r8152 *tp)
2363{
2364 u32 ocp_data;
hayeswangac718b62013-05-02 16:01:25 +00002365
2366 r8152b_reset_packet_filter(tp);
2367
2368 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2369 ocp_data |= CR_RE | CR_TE;
2370 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2371
hayeswang00a5e362014-02-18 21:48:59 +08002372 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00002373
hayeswangaa2e0922015-01-09 10:26:35 +08002374 return 0;
hayeswangac718b62013-05-02 16:01:25 +00002375}
2376
hayeswang507605a2014-01-02 11:22:43 +08002377static int rtl8152_enable(struct r8152 *tp)
2378{
hayeswang68714382014-04-11 17:54:31 +08002379 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2380 return -ENODEV;
2381
hayeswang507605a2014-01-02 11:22:43 +08002382 set_tx_qlen(tp);
2383 rtl_set_eee_plus(tp);
2384
2385 return rtl_enable(tp);
2386}
2387
hayeswang65b82d62017-06-15 14:44:03 +08002388static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2389{
2390 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2391 OWN_UPDATE | OWN_CLEAR);
2392}
2393
hayeswang464ec102015-02-12 14:33:46 +08002394static void r8153_set_rx_early_timeout(struct r8152 *tp)
hayeswang43779f82014-01-02 11:25:10 +08002395{
hayeswang464ec102015-02-12 14:33:46 +08002396 u32 ocp_data = tp->coalesce / 8;
hayeswang43779f82014-01-02 11:25:10 +08002397
hayeswang65b82d62017-06-15 14:44:03 +08002398 switch (tp->version) {
2399 case RTL_VER_03:
2400 case RTL_VER_04:
2401 case RTL_VER_05:
2402 case RTL_VER_06:
2403 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2404 ocp_data);
2405 break;
2406
2407 case RTL_VER_08:
2408 case RTL_VER_09:
2409 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2410 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2411 */
2412 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2413 128 / 8);
2414 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2415 ocp_data);
2416 r8153b_rx_agg_chg_indicate(tp);
2417 break;
2418
2419 default:
2420 break;
2421 }
hayeswang464ec102015-02-12 14:33:46 +08002422}
2423
2424static void r8153_set_rx_early_size(struct r8152 *tp)
2425{
hayeswang65b82d62017-06-15 14:44:03 +08002426 u32 ocp_data = agg_buf_sz - rx_reserved_size(tp->netdev->mtu);
hayeswang464ec102015-02-12 14:33:46 +08002427
hayeswang65b82d62017-06-15 14:44:03 +08002428 switch (tp->version) {
2429 case RTL_VER_03:
2430 case RTL_VER_04:
2431 case RTL_VER_05:
2432 case RTL_VER_06:
2433 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2434 ocp_data / 4);
2435 break;
2436 case RTL_VER_08:
2437 case RTL_VER_09:
2438 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2439 ocp_data / 8);
2440 r8153b_rx_agg_chg_indicate(tp);
2441 break;
2442 default:
2443 WARN_ON_ONCE(1);
2444 break;
2445 }
hayeswang43779f82014-01-02 11:25:10 +08002446}
2447
2448static int rtl8153_enable(struct r8152 *tp)
2449{
hayeswang68714382014-04-11 17:54:31 +08002450 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2451 return -ENODEV;
2452
hayeswang43779f82014-01-02 11:25:10 +08002453 set_tx_qlen(tp);
2454 rtl_set_eee_plus(tp);
hayeswang464ec102015-02-12 14:33:46 +08002455 r8153_set_rx_early_timeout(tp);
2456 r8153_set_rx_early_size(tp);
hayeswang43779f82014-01-02 11:25:10 +08002457
2458 return rtl_enable(tp);
2459}
2460
hayeswangd70b1132014-09-19 15:17:18 +08002461static void rtl_disable(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00002462{
hayeswangebc2ec482013-08-14 20:54:38 +08002463 u32 ocp_data;
2464 int i;
hayeswangac718b62013-05-02 16:01:25 +00002465
hayeswang68714382014-04-11 17:54:31 +08002466 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2467 rtl_drop_queued_tx(tp);
2468 return;
2469 }
2470
hayeswangac718b62013-05-02 16:01:25 +00002471 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2472 ocp_data &= ~RCR_ACPT_ALL;
2473 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2474
hayeswang00a5e362014-02-18 21:48:59 +08002475 rtl_drop_queued_tx(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002476
2477 for (i = 0; i < RTL8152_MAX_TX; i++)
2478 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00002479
hayeswang00a5e362014-02-18 21:48:59 +08002480 rxdy_gated_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00002481
2482 for (i = 0; i < 1000; i++) {
2483 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2484 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2485 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002486 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002487 }
2488
2489 for (i = 0; i < 1000; i++) {
2490 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2491 break;
hayeswang8ddfa072014-09-09 11:40:28 +08002492 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00002493 }
2494
hayeswang445f7f42014-09-23 16:31:47 +08002495 rtl_stop_rx(tp);
hayeswangac718b62013-05-02 16:01:25 +00002496
2497 rtl8152_nic_reset(tp);
2498}
2499
hayeswang00a5e362014-02-18 21:48:59 +08002500static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2501{
2502 u32 ocp_data;
2503
2504 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2505 if (enable)
2506 ocp_data |= POWER_CUT;
2507 else
2508 ocp_data &= ~POWER_CUT;
2509 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2510
2511 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2512 ocp_data &= ~RESUME_INDICATE;
2513 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
hayeswang00a5e362014-02-18 21:48:59 +08002514}
2515
hayeswangc5554292014-09-12 10:43:11 +08002516static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2517{
2518 u32 ocp_data;
2519
2520 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2521 if (enable)
2522 ocp_data |= CPCR_RX_VLAN;
2523 else
2524 ocp_data &= ~CPCR_RX_VLAN;
2525 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2526}
2527
2528static int rtl8152_set_features(struct net_device *dev,
2529 netdev_features_t features)
2530{
2531 netdev_features_t changed = features ^ dev->features;
2532 struct r8152 *tp = netdev_priv(dev);
hayeswang405f8a02014-10-09 18:00:24 +08002533 int ret;
2534
2535 ret = usb_autopm_get_interface(tp->intf);
2536 if (ret < 0)
2537 goto out;
hayeswangc5554292014-09-12 10:43:11 +08002538
hayeswangb5403272014-10-09 18:00:26 +08002539 mutex_lock(&tp->control);
2540
hayeswangc5554292014-09-12 10:43:11 +08002541 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2542 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2543 rtl_rx_vlan_en(tp, true);
2544 else
2545 rtl_rx_vlan_en(tp, false);
2546 }
2547
hayeswangb5403272014-10-09 18:00:26 +08002548 mutex_unlock(&tp->control);
2549
hayeswang405f8a02014-10-09 18:00:24 +08002550 usb_autopm_put_interface(tp->intf);
2551
2552out:
2553 return ret;
hayeswangc5554292014-09-12 10:43:11 +08002554}
2555
hayeswang21ff2e82014-02-18 21:49:06 +08002556#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2557
2558static u32 __rtl_get_wol(struct r8152 *tp)
2559{
2560 u32 ocp_data;
2561 u32 wolopts = 0;
2562
hayeswang21ff2e82014-02-18 21:49:06 +08002563 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2564 if (ocp_data & LINK_ON_WAKE_EN)
2565 wolopts |= WAKE_PHY;
2566
2567 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2568 if (ocp_data & UWF_EN)
2569 wolopts |= WAKE_UCAST;
2570 if (ocp_data & BWF_EN)
2571 wolopts |= WAKE_BCAST;
2572 if (ocp_data & MWF_EN)
2573 wolopts |= WAKE_MCAST;
2574
2575 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2576 if (ocp_data & MAGIC_EN)
2577 wolopts |= WAKE_MAGIC;
2578
2579 return wolopts;
2580}
2581
2582static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2583{
2584 u32 ocp_data;
2585
2586 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2587
2588 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2589 ocp_data &= ~LINK_ON_WAKE_EN;
2590 if (wolopts & WAKE_PHY)
2591 ocp_data |= LINK_ON_WAKE_EN;
2592 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2593
2594 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
hayeswang92f7d072016-07-06 17:35:59 +08002595 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
hayeswang21ff2e82014-02-18 21:49:06 +08002596 if (wolopts & WAKE_UCAST)
2597 ocp_data |= UWF_EN;
2598 if (wolopts & WAKE_BCAST)
2599 ocp_data |= BWF_EN;
2600 if (wolopts & WAKE_MCAST)
2601 ocp_data |= MWF_EN;
hayeswang21ff2e82014-02-18 21:49:06 +08002602 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2603
2604 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2605
2606 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2607 ocp_data &= ~MAGIC_EN;
2608 if (wolopts & WAKE_MAGIC)
2609 ocp_data |= MAGIC_EN;
2610 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2611
2612 if (wolopts & WAKE_ANY)
2613 device_set_wakeup_enable(&tp->udev->dev, true);
2614 else
2615 device_set_wakeup_enable(&tp->udev->dev, false);
2616}
2617
hayeswang134f98b2017-06-09 17:11:40 +08002618static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2619{
2620 /* MAC clock speed down */
2621 if (enable) {
2622 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2623 ALDPS_SPDWN_RATIO);
2624 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2625 EEE_SPDWN_RATIO);
2626 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2627 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2628 U1U2_SPDWN_EN | L1_SPDWN_EN);
2629 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2630 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2631 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2632 TP1000_SPDWN_EN);
2633 } else {
2634 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2635 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2636 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2637 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2638 }
2639}
2640
hayeswangb2143962015-07-24 13:54:23 +08002641static void r8153_u1u2en(struct r8152 *tp, bool enable)
2642{
2643 u8 u1u2[8];
2644
2645 if (enable)
2646 memset(u1u2, 0xff, sizeof(u1u2));
2647 else
2648 memset(u1u2, 0x00, sizeof(u1u2));
2649
2650 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2651}
2652
hayeswang65b82d62017-06-15 14:44:03 +08002653static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2654{
2655 u32 ocp_data;
2656
2657 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2658 if (enable)
2659 ocp_data |= LPM_U1U2_EN;
2660 else
2661 ocp_data &= ~LPM_U1U2_EN;
2662
2663 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2664}
2665
hayeswangb2143962015-07-24 13:54:23 +08002666static void r8153_u2p3en(struct r8152 *tp, bool enable)
2667{
2668 u32 ocp_data;
2669
2670 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
hayeswang3cb32342017-06-09 17:11:43 +08002671 if (enable)
hayeswangb2143962015-07-24 13:54:23 +08002672 ocp_data |= U2P3_ENABLE;
2673 else
2674 ocp_data &= ~U2P3_ENABLE;
2675 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2676}
2677
hayeswang65b82d62017-06-15 14:44:03 +08002678static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2679{
2680 u32 ocp_data;
2681
2682 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2683 ocp_data &= ~clear;
2684 ocp_data |= set;
2685 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2686}
2687
2688static void r8153b_green_en(struct r8152 *tp, bool enable)
2689{
2690 u16 data;
2691
2692 if (enable) {
2693 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2694 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2695 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2696 } else {
2697 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2698 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2699 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2700 }
2701
2702 data = sram_read(tp, SRAM_GREEN_CFG);
2703 data |= GREEN_ETH_EN;
2704 sram_write(tp, SRAM_GREEN_CFG, data);
2705
2706 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2707}
2708
hayeswangc564b872017-06-09 17:11:38 +08002709static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2710{
2711 u16 data;
2712 int i;
2713
2714 for (i = 0; i < 500; i++) {
2715 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2716 data &= PHY_STAT_MASK;
2717 if (desired) {
2718 if (data == desired)
2719 break;
2720 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2721 data == PHY_STAT_EXT_INIT) {
2722 break;
2723 }
2724
2725 msleep(20);
2726 }
2727
2728 return data;
2729}
2730
hayeswang65b82d62017-06-15 14:44:03 +08002731static void r8153b_ups_en(struct r8152 *tp, bool enable)
2732{
2733 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2734
2735 if (enable) {
2736 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2737 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2738
2739 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2740 ocp_data |= BIT(0);
2741 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2742 } else {
2743 u16 data;
2744
2745 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2746 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2747
2748 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2749 ocp_data &= ~BIT(0);
2750 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2751
2752 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2753 ocp_data &= ~PCUT_STATUS;
2754 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2755
2756 data = r8153_phy_status(tp, 0);
2757
2758 switch (data) {
2759 case PHY_STAT_PWRDN:
2760 case PHY_STAT_EXT_INIT:
2761 r8153b_green_en(tp,
2762 test_bit(GREEN_ETHERNET, &tp->flags));
2763
2764 data = r8152_mdio_read(tp, MII_BMCR);
2765 data &= ~BMCR_PDOWN;
2766 data |= BMCR_RESET;
2767 r8152_mdio_write(tp, MII_BMCR, data);
2768
2769 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
Gustavo A. R. Silva9ca78672018-06-28 13:50:48 -05002770 /* fall through */
hayeswang65b82d62017-06-15 14:44:03 +08002771
2772 default:
2773 if (data != PHY_STAT_LAN_ON)
2774 netif_warn(tp, link, tp->netdev,
2775 "PHY not ready");
2776 break;
2777 }
2778 }
2779}
2780
hayeswangb2143962015-07-24 13:54:23 +08002781static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2782{
2783 u32 ocp_data;
2784
2785 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2786 if (enable)
2787 ocp_data |= PWR_EN | PHASE2_EN;
2788 else
2789 ocp_data &= ~(PWR_EN | PHASE2_EN);
2790 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2791
2792 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2793 ocp_data &= ~PCUT_STATUS;
2794 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2795}
2796
hayeswang65b82d62017-06-15 14:44:03 +08002797static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2798{
2799 u32 ocp_data;
2800
2801 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2802 if (enable)
2803 ocp_data |= PWR_EN | PHASE2_EN;
2804 else
2805 ocp_data &= ~PWR_EN;
2806 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2807
2808 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2809 ocp_data &= ~PCUT_STATUS;
2810 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2811}
2812
2813static void r8153b_queue_wake(struct r8152 *tp, bool enable)
2814{
2815 u32 ocp_data;
2816
2817 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 0xd38a);
2818 if (enable)
2819 ocp_data |= BIT(0);
2820 else
2821 ocp_data &= ~BIT(0);
2822 ocp_write_byte(tp, MCU_TYPE_PLA, 0xd38a, ocp_data);
2823
2824 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 0xd38c);
2825 ocp_data &= ~BIT(0);
2826 ocp_write_byte(tp, MCU_TYPE_PLA, 0xd38c, ocp_data);
2827}
2828
hayeswang7daed8d2015-07-24 13:54:24 +08002829static bool rtl_can_wakeup(struct r8152 *tp)
2830{
2831 struct usb_device *udev = tp->udev;
2832
2833 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
2834}
2835
hayeswang9a4be1b2014-02-18 21:49:07 +08002836static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2837{
2838 if (enable) {
2839 u32 ocp_data;
2840
2841 __rtl_set_wol(tp, WAKE_ANY);
2842
2843 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2844
2845 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2846 ocp_data |= LINK_OFF_WAKE_EN;
2847 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2848
2849 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2850 } else {
hayeswangf95ae8a2016-06-30 15:33:35 +08002851 u32 ocp_data;
2852
hayeswang9a4be1b2014-02-18 21:49:07 +08002853 __rtl_set_wol(tp, tp->saved_wolopts);
hayeswangf95ae8a2016-06-30 15:33:35 +08002854
2855 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2856
2857 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2858 ocp_data &= ~LINK_OFF_WAKE_EN;
2859 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2860
2861 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
hayeswang2609af12016-07-05 16:11:46 +08002862 }
2863}
hayeswangf95ae8a2016-06-30 15:33:35 +08002864
hayeswang2609af12016-07-05 16:11:46 +08002865static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
2866{
hayeswang2609af12016-07-05 16:11:46 +08002867 if (enable) {
2868 r8153_u1u2en(tp, false);
2869 r8153_u2p3en(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002870 r8153_mac_clk_spd(tp, true);
hayeswang02552752017-06-09 17:11:42 +08002871 rtl_runtime_suspend_enable(tp, true);
hayeswang2609af12016-07-05 16:11:46 +08002872 } else {
hayeswang02552752017-06-09 17:11:42 +08002873 rtl_runtime_suspend_enable(tp, false);
hayeswang134f98b2017-06-09 17:11:40 +08002874 r8153_mac_clk_spd(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08002875
2876 switch (tp->version) {
2877 case RTL_VER_03:
2878 case RTL_VER_04:
2879 break;
2880 case RTL_VER_05:
2881 case RTL_VER_06:
2882 default:
2883 r8153_u2p3en(tp, true);
2884 break;
2885 }
2886
hayeswangb2143962015-07-24 13:54:23 +08002887 r8153_u1u2en(tp, true);
hayeswang9a4be1b2014-02-18 21:49:07 +08002888 }
2889}
2890
hayeswang65b82d62017-06-15 14:44:03 +08002891static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
2892{
2893 if (enable) {
2894 r8153b_queue_wake(tp, true);
2895 r8153b_u1u2en(tp, false);
2896 r8153_u2p3en(tp, false);
2897 rtl_runtime_suspend_enable(tp, true);
2898 r8153b_ups_en(tp, true);
2899 } else {
2900 r8153b_ups_en(tp, false);
2901 r8153b_queue_wake(tp, false);
2902 rtl_runtime_suspend_enable(tp, false);
2903 r8153_u2p3en(tp, true);
2904 r8153b_u1u2en(tp, true);
2905 }
2906}
2907
hayeswang43499682014-02-18 21:48:58 +08002908static void r8153_teredo_off(struct r8152 *tp)
2909{
2910 u32 ocp_data;
2911
hayeswang65b82d62017-06-15 14:44:03 +08002912 switch (tp->version) {
2913 case RTL_VER_01:
2914 case RTL_VER_02:
2915 case RTL_VER_03:
2916 case RTL_VER_04:
2917 case RTL_VER_05:
2918 case RTL_VER_06:
2919 case RTL_VER_07:
2920 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2921 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
2922 OOB_TEREDO_EN);
2923 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2924 break;
2925
2926 case RTL_VER_08:
2927 case RTL_VER_09:
2928 /* The bit 0 ~ 7 are relative with teredo settings. They are
2929 * W1C (write 1 to clear), so set all 1 to disable it.
2930 */
2931 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
2932 break;
2933
2934 default:
2935 break;
2936 }
hayeswang43499682014-02-18 21:48:58 +08002937
2938 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2939 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2940 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2941}
2942
hayeswang93fe9b12016-06-16 10:55:18 +08002943static void rtl_reset_bmu(struct r8152 *tp)
2944{
2945 u32 ocp_data;
2946
2947 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
2948 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
2949 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2950 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
2951 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2952}
2953
hayeswangcda9fb02016-01-07 17:51:12 +08002954static void r8152_aldps_en(struct r8152 *tp, bool enable)
hayeswang43499682014-02-18 21:48:58 +08002955{
hayeswangcda9fb02016-01-07 17:51:12 +08002956 if (enable) {
2957 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2958 LINKENA | DIS_SDSAVE);
2959 } else {
2960 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
2961 DIS_SDSAVE);
2962 msleep(20);
2963 }
hayeswang43499682014-02-18 21:48:58 +08002964}
2965
hayeswange6449532016-09-20 16:22:05 +08002966static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
2967{
2968 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
2969 ocp_reg_write(tp, OCP_EEE_DATA, reg);
2970 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
2971}
2972
2973static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
2974{
2975 u16 data;
2976
2977 r8152_mmd_indirect(tp, dev, reg);
2978 data = ocp_reg_read(tp, OCP_EEE_DATA);
2979 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2980
2981 return data;
2982}
2983
2984static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
2985{
2986 r8152_mmd_indirect(tp, dev, reg);
2987 ocp_reg_write(tp, OCP_EEE_DATA, data);
2988 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2989}
2990
2991static void r8152_eee_en(struct r8152 *tp, bool enable)
2992{
2993 u16 config1, config2, config3;
2994 u32 ocp_data;
2995
2996 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2997 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
2998 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
2999 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
3000
3001 if (enable) {
3002 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3003 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
3004 config1 |= sd_rise_time(1);
3005 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
3006 config3 |= fast_snr(42);
3007 } else {
3008 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3009 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
3010 RX_QUIET_EN);
3011 config1 |= sd_rise_time(7);
3012 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3013 config3 |= fast_snr(511);
3014 }
3015
3016 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3017 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3018 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3019 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3020}
3021
3022static void r8152b_enable_eee(struct r8152 *tp)
3023{
3024 r8152_eee_en(tp, true);
3025 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3026}
3027
3028static void r8152b_enable_fc(struct r8152 *tp)
3029{
3030 u16 anar;
3031
3032 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3033 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3034 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3035}
3036
hayeswangd70b1132014-09-19 15:17:18 +08003037static void rtl8152_disable(struct r8152 *tp)
3038{
hayeswangcda9fb02016-01-07 17:51:12 +08003039 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003040 rtl_disable(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003041 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003042}
3043
hayeswang43499682014-02-18 21:48:58 +08003044static void r8152b_hw_phy_cfg(struct r8152 *tp)
3045{
hayeswangef39df82016-09-20 16:22:07 +08003046 r8152b_enable_eee(tp);
3047 r8152_aldps_en(tp, true);
3048 r8152b_enable_fc(tp);
hayeswangf0cbe0a2014-02-18 21:49:03 +08003049
hayeswangaa66a5f2014-02-18 21:49:04 +08003050 set_bit(PHY_RESET, &tp->flags);
hayeswang43499682014-02-18 21:48:58 +08003051}
3052
hayeswangac718b62013-05-02 16:01:25 +00003053static void r8152b_exit_oob(struct r8152 *tp)
3054{
hayeswangdb8515e2014-03-06 15:07:16 +08003055 u32 ocp_data;
3056 int i;
hayeswangac718b62013-05-02 16:01:25 +00003057
3058 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3059 ocp_data &= ~RCR_ACPT_ALL;
3060 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3061
hayeswang00a5e362014-02-18 21:48:59 +08003062 rxdy_gated_en(tp, true);
hayeswangda9bd112014-02-18 21:49:08 +08003063 r8153_teredo_off(tp);
hayeswangac718b62013-05-02 16:01:25 +00003064 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3065 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3066
3067 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3068 ocp_data &= ~NOW_IS_OOB;
3069 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3070
3071 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3072 ocp_data &= ~MCU_BORW_EN;
3073 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3074
3075 for (i = 0; i < 1000; i++) {
3076 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3077 if (ocp_data & LINK_LIST_READY)
3078 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003079 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003080 }
3081
3082 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3083 ocp_data |= RE_INIT_LL;
3084 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3085
3086 for (i = 0; i < 1000; i++) {
3087 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3088 if (ocp_data & LINK_LIST_READY)
3089 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003090 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003091 }
3092
3093 rtl8152_nic_reset(tp);
3094
3095 /* rx share fifo credit full threshold */
3096 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3097
hayeswanga3cc4652014-07-24 16:37:43 +08003098 if (tp->udev->speed == USB_SPEED_FULL ||
3099 tp->udev->speed == USB_SPEED_LOW) {
hayeswangac718b62013-05-02 16:01:25 +00003100 /* rx share fifo credit near full threshold */
3101 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3102 RXFIFO_THR2_FULL);
3103 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3104 RXFIFO_THR3_FULL);
3105 } else {
3106 /* rx share fifo credit near full threshold */
3107 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3108 RXFIFO_THR2_HIGH);
3109 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3110 RXFIFO_THR3_HIGH);
3111 }
3112
3113 /* TX share fifo free credit full threshold */
3114 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3115
3116 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
hayeswang8e1f51b2014-01-02 11:22:41 +08003117 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
hayeswangac718b62013-05-02 16:01:25 +00003118 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3119 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3120
hayeswangc5554292014-09-12 10:43:11 +08003121 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswangac718b62013-05-02 16:01:25 +00003122
3123 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3124
3125 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3126 ocp_data |= TCR0_AUTO_FIFO;
3127 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3128}
3129
3130static void r8152b_enter_oob(struct r8152 *tp)
3131{
hayeswang45f4a192014-01-06 17:08:41 +08003132 u32 ocp_data;
3133 int i;
hayeswangac718b62013-05-02 16:01:25 +00003134
3135 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3136 ocp_data &= ~NOW_IS_OOB;
3137 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3138
3139 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3140 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3141 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3142
hayeswangd70b1132014-09-19 15:17:18 +08003143 rtl_disable(tp);
hayeswangac718b62013-05-02 16:01:25 +00003144
3145 for (i = 0; i < 1000; i++) {
3146 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3147 if (ocp_data & LINK_LIST_READY)
3148 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003149 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003150 }
3151
3152 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3153 ocp_data |= RE_INIT_LL;
3154 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3155
3156 for (i = 0; i < 1000; i++) {
3157 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3158 if (ocp_data & LINK_LIST_READY)
3159 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003160 usleep_range(1000, 2000);
hayeswangac718b62013-05-02 16:01:25 +00003161 }
3162
3163 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3164
hayeswangc5554292014-09-12 10:43:11 +08003165 rtl_rx_vlan_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003166
3167 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
3168 ocp_data |= ALDPS_PROXY_MODE;
3169 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
3170
3171 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3172 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3173 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3174
hayeswang00a5e362014-02-18 21:48:59 +08003175 rxdy_gated_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003176
3177 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3178 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3179 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3180}
3181
hayeswang65b82d62017-06-15 14:44:03 +08003182static int r8153_patch_request(struct r8152 *tp, bool request)
3183{
3184 u16 data;
3185 int i;
3186
3187 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3188 if (request)
3189 data |= PATCH_REQUEST;
3190 else
3191 data &= ~PATCH_REQUEST;
3192 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3193
3194 for (i = 0; request && i < 5000; i++) {
3195 usleep_range(1000, 2000);
3196 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3197 break;
3198 }
3199
3200 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3201 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3202 r8153_patch_request(tp, false);
3203 return -ETIME;
3204 } else {
3205 return 0;
3206 }
3207}
3208
hayeswange6449532016-09-20 16:22:05 +08003209static void r8153_aldps_en(struct r8152 *tp, bool enable)
3210{
3211 u16 data;
3212
3213 data = ocp_reg_read(tp, OCP_POWER_CFG);
3214 if (enable) {
3215 data |= EN_ALDPS;
3216 ocp_reg_write(tp, OCP_POWER_CFG, data);
3217 } else {
hayeswang4214cc52017-06-09 17:11:46 +08003218 int i;
3219
hayeswange6449532016-09-20 16:22:05 +08003220 data &= ~EN_ALDPS;
3221 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswang4214cc52017-06-09 17:11:46 +08003222 for (i = 0; i < 20; i++) {
3223 usleep_range(1000, 2000);
3224 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3225 break;
3226 }
hayeswange6449532016-09-20 16:22:05 +08003227 }
3228}
3229
hayeswang65b82d62017-06-15 14:44:03 +08003230static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3231{
3232 r8153_aldps_en(tp, enable);
3233
3234 if (enable)
3235 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3236 else
3237 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3238}
3239
hayeswange6449532016-09-20 16:22:05 +08003240static void r8153_eee_en(struct r8152 *tp, bool enable)
3241{
3242 u32 ocp_data;
3243 u16 config;
3244
3245 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3246 config = ocp_reg_read(tp, OCP_EEE_CFG);
3247
3248 if (enable) {
3249 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3250 config |= EEE10_EN;
3251 } else {
3252 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3253 config &= ~EEE10_EN;
3254 }
3255
3256 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3257 ocp_reg_write(tp, OCP_EEE_CFG, config);
3258}
3259
hayeswang65b82d62017-06-15 14:44:03 +08003260static void r8153b_eee_en(struct r8152 *tp, bool enable)
3261{
3262 r8153_eee_en(tp, enable);
3263
3264 if (enable)
3265 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3266 else
3267 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3268}
3269
3270static void r8153b_enable_fc(struct r8152 *tp)
3271{
3272 r8152b_enable_fc(tp);
3273 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3274}
3275
hayeswang43779f82014-01-02 11:25:10 +08003276static void r8153_hw_phy_cfg(struct r8152 *tp)
3277{
3278 u32 ocp_data;
3279 u16 data;
3280
hayeswangd768c612016-09-20 16:22:09 +08003281 /* disable ALDPS before updating the PHY parameters */
3282 r8153_aldps_en(tp, false);
hayeswangfb02eb42015-07-22 15:27:41 +08003283
hayeswangd768c612016-09-20 16:22:09 +08003284 /* disable EEE before updating the PHY parameters */
3285 r8153_eee_en(tp, false);
3286 ocp_reg_write(tp, OCP_EEE_ADV, 0);
hayeswang43779f82014-01-02 11:25:10 +08003287
3288 if (tp->version == RTL_VER_03) {
3289 data = ocp_reg_read(tp, OCP_EEE_CFG);
3290 data &= ~CTAP_SHORT_EN;
3291 ocp_reg_write(tp, OCP_EEE_CFG, data);
3292 }
3293
3294 data = ocp_reg_read(tp, OCP_POWER_CFG);
3295 data |= EEE_CLKDIV_EN;
3296 ocp_reg_write(tp, OCP_POWER_CFG, data);
3297
3298 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3299 data |= EN_10M_BGOFF;
3300 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3301 data = ocp_reg_read(tp, OCP_POWER_CFG);
3302 data |= EN_10M_PLLOFF;
3303 ocp_reg_write(tp, OCP_POWER_CFG, data);
hayeswangb4d99de2015-01-19 17:02:46 +08003304 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
hayeswang43779f82014-01-02 11:25:10 +08003305
3306 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3307 ocp_data |= PFM_PWM_SWITCH;
3308 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3309
hayeswangb4d99de2015-01-19 17:02:46 +08003310 /* Enable LPF corner auto tune */
3311 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
hayeswang43779f82014-01-02 11:25:10 +08003312
hayeswangb4d99de2015-01-19 17:02:46 +08003313 /* Adjust 10M Amplitude */
3314 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3315 sram_write(tp, SRAM_10M_AMP2, 0x0208);
hayeswangaa66a5f2014-02-18 21:49:04 +08003316
hayeswangaf0287e2016-09-20 16:22:08 +08003317 r8153_eee_en(tp, true);
3318 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3319
hayeswangef39df82016-09-20 16:22:07 +08003320 r8153_aldps_en(tp, true);
3321 r8152b_enable_fc(tp);
3322
hayeswang3cb32342017-06-09 17:11:43 +08003323 switch (tp->version) {
3324 case RTL_VER_03:
3325 case RTL_VER_04:
3326 break;
3327 case RTL_VER_05:
3328 case RTL_VER_06:
3329 default:
3330 r8153_u2p3en(tp, true);
3331 break;
3332 }
3333
hayeswangaa66a5f2014-02-18 21:49:04 +08003334 set_bit(PHY_RESET, &tp->flags);
hayeswang43779f82014-01-02 11:25:10 +08003335}
3336
hayeswang65b82d62017-06-15 14:44:03 +08003337static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3338{
3339 u32 ocp_data;
3340
3341 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3342 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3343 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3344 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3345
3346 return ocp_data;
3347}
3348
3349static void r8153b_hw_phy_cfg(struct r8152 *tp)
3350{
3351 u32 ocp_data, ups_flags = 0;
3352 u16 data;
3353
3354 /* disable ALDPS before updating the PHY parameters */
3355 r8153b_aldps_en(tp, false);
3356
3357 /* disable EEE before updating the PHY parameters */
3358 r8153b_eee_en(tp, false);
3359 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3360
3361 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3362
3363 data = sram_read(tp, SRAM_GREEN_CFG);
3364 data |= R_TUNE_EN;
3365 sram_write(tp, SRAM_GREEN_CFG, data);
3366 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3367 data |= PGA_RETURN_EN;
3368 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3369
3370 /* ADC Bias Calibration:
3371 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3372 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3373 * ADC ioffset.
3374 */
3375 ocp_data = r8152_efuse_read(tp, 0x7d);
3376 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3377 if (data != 0xffff)
3378 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3379
3380 /* ups mode tx-link-pulse timing adjustment:
3381 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3382 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3383 */
3384 ocp_data = ocp_reg_read(tp, 0xc426);
3385 ocp_data &= 0x3fff;
3386 if (ocp_data) {
3387 u32 swr_cnt_1ms_ini;
3388
3389 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3390 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3391 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3392 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3393 }
3394
3395 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3396 ocp_data |= PFM_PWM_SWITCH;
3397 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3398
3399 /* Advnace EEE */
3400 if (!r8153_patch_request(tp, true)) {
3401 data = ocp_reg_read(tp, OCP_POWER_CFG);
3402 data |= EEE_CLKDIV_EN;
3403 ocp_reg_write(tp, OCP_POWER_CFG, data);
3404
3405 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3406 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3407 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3408
3409 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3410 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3411
3412 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3413 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3414 UPS_FLAGS_EEE_PLLOFF_GIGA;
3415
3416 r8153_patch_request(tp, false);
3417 }
3418
3419 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3420
3421 r8153b_eee_en(tp, true);
3422 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3423
3424 r8153b_aldps_en(tp, true);
3425 r8153b_enable_fc(tp);
3426 r8153_u2p3en(tp, true);
3427
3428 set_bit(PHY_RESET, &tp->flags);
3429}
3430
hayeswang43779f82014-01-02 11:25:10 +08003431static void r8153_first_init(struct r8152 *tp)
3432{
3433 u32 ocp_data;
3434 int i;
3435
hayeswang134f98b2017-06-09 17:11:40 +08003436 r8153_mac_clk_spd(tp, false);
hayeswang00a5e362014-02-18 21:48:59 +08003437 rxdy_gated_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003438 r8153_teredo_off(tp);
3439
3440 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3441 ocp_data &= ~RCR_ACPT_ALL;
3442 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3443
hayeswang43779f82014-01-02 11:25:10 +08003444 rtl8152_nic_reset(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003445 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003446
3447 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3448 ocp_data &= ~NOW_IS_OOB;
3449 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3450
3451 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3452 ocp_data &= ~MCU_BORW_EN;
3453 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3454
3455 for (i = 0; i < 1000; i++) {
3456 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3457 if (ocp_data & LINK_LIST_READY)
3458 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003459 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003460 }
3461
3462 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3463 ocp_data |= RE_INIT_LL;
3464 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3465
3466 for (i = 0; i < 1000; i++) {
3467 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3468 if (ocp_data & LINK_LIST_READY)
3469 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003470 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003471 }
3472
hayeswangc5554292014-09-12 10:43:11 +08003473 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
hayeswang43779f82014-01-02 11:25:10 +08003474
hayeswangb65c0c92017-06-21 11:25:18 +08003475 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003476 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang69b4b7a2014-07-10 10:58:54 +08003477 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
hayeswang43779f82014-01-02 11:25:10 +08003478
3479 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3480 ocp_data |= TCR0_AUTO_FIFO;
3481 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3482
3483 rtl8152_nic_reset(tp);
3484
3485 /* rx share fifo credit full threshold */
3486 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3487 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3488 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3489 /* TX share fifo free credit full threshold */
3490 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
hayeswang43779f82014-01-02 11:25:10 +08003491}
3492
3493static void r8153_enter_oob(struct r8152 *tp)
3494{
3495 u32 ocp_data;
3496 int i;
3497
hayeswang134f98b2017-06-09 17:11:40 +08003498 r8153_mac_clk_spd(tp, true);
3499
hayeswang43779f82014-01-02 11:25:10 +08003500 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3501 ocp_data &= ~NOW_IS_OOB;
3502 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3503
hayeswangd70b1132014-09-19 15:17:18 +08003504 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003505 rtl_reset_bmu(tp);
hayeswang43779f82014-01-02 11:25:10 +08003506
3507 for (i = 0; i < 1000; i++) {
3508 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3509 if (ocp_data & LINK_LIST_READY)
3510 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003511 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003512 }
3513
3514 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3515 ocp_data |= RE_INIT_LL;
3516 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3517
3518 for (i = 0; i < 1000; i++) {
3519 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3520 if (ocp_data & LINK_LIST_READY)
3521 break;
hayeswang8ddfa072014-09-09 11:40:28 +08003522 usleep_range(1000, 2000);
hayeswang43779f82014-01-02 11:25:10 +08003523 }
3524
hayeswangb65c0c92017-06-21 11:25:18 +08003525 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08003526 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08003527
hayeswang65b82d62017-06-15 14:44:03 +08003528 switch (tp->version) {
3529 case RTL_VER_03:
3530 case RTL_VER_04:
3531 case RTL_VER_05:
3532 case RTL_VER_06:
3533 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3534 ocp_data &= ~TEREDO_WAKE_MASK;
3535 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3536 break;
3537
3538 case RTL_VER_08:
3539 case RTL_VER_09:
3540 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3541 * type. Set it to zero. bits[7:0] are the W1C bits about
3542 * the events. Set them to all 1 to clear them.
3543 */
3544 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3545 break;
3546
3547 default:
3548 break;
3549 }
hayeswang43779f82014-01-02 11:25:10 +08003550
hayeswangc5554292014-09-12 10:43:11 +08003551 rtl_rx_vlan_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003552
3553 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
3554 ocp_data |= ALDPS_PROXY_MODE;
3555 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
3556
3557 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3558 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3559 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3560
hayeswang00a5e362014-02-18 21:48:59 +08003561 rxdy_gated_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003562
3563 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3564 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3565 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3566}
3567
hayeswangd70b1132014-09-19 15:17:18 +08003568static void rtl8153_disable(struct r8152 *tp)
3569{
hayeswangcda9fb02016-01-07 17:51:12 +08003570 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003571 rtl_disable(tp);
hayeswang93fe9b12016-06-16 10:55:18 +08003572 rtl_reset_bmu(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003573 r8153_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003574}
3575
hayeswang65b82d62017-06-15 14:44:03 +08003576static void rtl8153b_disable(struct r8152 *tp)
3577{
3578 r8153b_aldps_en(tp, false);
3579 rtl_disable(tp);
3580 rtl_reset_bmu(tp);
3581 r8153b_aldps_en(tp, true);
3582}
3583
hayeswangac718b62013-05-02 16:01:25 +00003584static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3585{
hayeswang43779f82014-01-02 11:25:10 +08003586 u16 bmcr, anar, gbcr;
hayeswang65b82d62017-06-15 14:44:03 +08003587 enum spd_duplex speed_duplex;
hayeswangac718b62013-05-02 16:01:25 +00003588 int ret = 0;
3589
hayeswangac718b62013-05-02 16:01:25 +00003590 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3591 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3592 ADVERTISE_100HALF | ADVERTISE_100FULL);
hayeswang43779f82014-01-02 11:25:10 +08003593 if (tp->mii.supports_gmii) {
3594 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3595 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3596 } else {
3597 gbcr = 0;
3598 }
hayeswangac718b62013-05-02 16:01:25 +00003599
3600 if (autoneg == AUTONEG_DISABLE) {
3601 if (speed == SPEED_10) {
3602 bmcr = 0;
3603 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003604 speed_duplex = FORCE_10M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003605 } else if (speed == SPEED_100) {
3606 bmcr = BMCR_SPEED100;
3607 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003608 speed_duplex = FORCE_100M_HALF;
hayeswang43779f82014-01-02 11:25:10 +08003609 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3610 bmcr = BMCR_SPEED1000;
3611 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003612 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003613 } else {
3614 ret = -EINVAL;
3615 goto out;
3616 }
3617
hayeswang65b82d62017-06-15 14:44:03 +08003618 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003619 bmcr |= BMCR_FULLDPLX;
hayeswang65b82d62017-06-15 14:44:03 +08003620 if (speed != SPEED_1000)
3621 speed_duplex++;
3622 }
hayeswangac718b62013-05-02 16:01:25 +00003623 } else {
3624 if (speed == SPEED_10) {
hayeswang65b82d62017-06-15 14:44:03 +08003625 if (duplex == DUPLEX_FULL) {
hayeswangac718b62013-05-02 16:01:25 +00003626 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003627 speed_duplex = NWAY_10M_FULL;
3628 } else {
hayeswangac718b62013-05-02 16:01:25 +00003629 anar |= ADVERTISE_10HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003630 speed_duplex = NWAY_10M_HALF;
3631 }
hayeswangac718b62013-05-02 16:01:25 +00003632 } else if (speed == SPEED_100) {
3633 if (duplex == DUPLEX_FULL) {
3634 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3635 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
hayeswang65b82d62017-06-15 14:44:03 +08003636 speed_duplex = NWAY_100M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003637 } else {
3638 anar |= ADVERTISE_10HALF;
3639 anar |= ADVERTISE_100HALF;
hayeswang65b82d62017-06-15 14:44:03 +08003640 speed_duplex = NWAY_100M_HALF;
hayeswangac718b62013-05-02 16:01:25 +00003641 }
hayeswang43779f82014-01-02 11:25:10 +08003642 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3643 if (duplex == DUPLEX_FULL) {
3644 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3645 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3646 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3647 } else {
3648 anar |= ADVERTISE_10HALF;
3649 anar |= ADVERTISE_100HALF;
3650 gbcr |= ADVERTISE_1000HALF;
3651 }
hayeswang65b82d62017-06-15 14:44:03 +08003652 speed_duplex = NWAY_1000M_FULL;
hayeswangac718b62013-05-02 16:01:25 +00003653 } else {
3654 ret = -EINVAL;
3655 goto out;
3656 }
3657
3658 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3659 }
3660
hayeswangfae56172016-06-16 14:08:29 +08003661 if (test_and_clear_bit(PHY_RESET, &tp->flags))
hayeswangaa66a5f2014-02-18 21:49:04 +08003662 bmcr |= BMCR_RESET;
3663
hayeswang43779f82014-01-02 11:25:10 +08003664 if (tp->mii.supports_gmii)
3665 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3666
hayeswangac718b62013-05-02 16:01:25 +00003667 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3668 r8152_mdio_write(tp, MII_BMCR, bmcr);
3669
hayeswang65b82d62017-06-15 14:44:03 +08003670 switch (tp->version) {
3671 case RTL_VER_08:
3672 case RTL_VER_09:
3673 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3674 UPS_FLAGS_SPEED_MASK);
3675 break;
3676
3677 default:
3678 break;
3679 }
3680
hayeswangfae56172016-06-16 14:08:29 +08003681 if (bmcr & BMCR_RESET) {
hayeswangaa66a5f2014-02-18 21:49:04 +08003682 int i;
3683
hayeswangaa66a5f2014-02-18 21:49:04 +08003684 for (i = 0; i < 50; i++) {
3685 msleep(20);
3686 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3687 break;
3688 }
3689 }
3690
hayeswangac718b62013-05-02 16:01:25 +00003691out:
hayeswangac718b62013-05-02 16:01:25 +00003692 return ret;
3693}
3694
hayeswangd70b1132014-09-19 15:17:18 +08003695static void rtl8152_up(struct r8152 *tp)
3696{
3697 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3698 return;
3699
hayeswangcda9fb02016-01-07 17:51:12 +08003700 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003701 r8152b_exit_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003702 r8152_aldps_en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003703}
3704
hayeswangac718b62013-05-02 16:01:25 +00003705static void rtl8152_down(struct r8152 *tp)
3706{
hayeswang68714382014-04-11 17:54:31 +08003707 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3708 rtl_drop_queued_tx(tp);
3709 return;
3710 }
3711
hayeswang00a5e362014-02-18 21:48:59 +08003712 r8152_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003713 r8152_aldps_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00003714 r8152b_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003715 r8152_aldps_en(tp, true);
hayeswangac718b62013-05-02 16:01:25 +00003716}
3717
hayeswangd70b1132014-09-19 15:17:18 +08003718static void rtl8153_up(struct r8152 *tp)
3719{
3720 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3721 return;
3722
hayeswangb2143962015-07-24 13:54:23 +08003723 r8153_u1u2en(tp, false);
hayeswang3cb32342017-06-09 17:11:43 +08003724 r8153_u2p3en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003725 r8153_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08003726 r8153_first_init(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003727 r8153_aldps_en(tp, true);
hayeswang3cb32342017-06-09 17:11:43 +08003728
3729 switch (tp->version) {
3730 case RTL_VER_03:
3731 case RTL_VER_04:
3732 break;
3733 case RTL_VER_05:
3734 case RTL_VER_06:
3735 default:
3736 r8153_u2p3en(tp, true);
3737 break;
3738 }
3739
hayeswangb2143962015-07-24 13:54:23 +08003740 r8153_u1u2en(tp, true);
hayeswangd70b1132014-09-19 15:17:18 +08003741}
3742
hayeswang43779f82014-01-02 11:25:10 +08003743static void rtl8153_down(struct r8152 *tp)
3744{
hayeswang68714382014-04-11 17:54:31 +08003745 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3746 rtl_drop_queued_tx(tp);
3747 return;
3748 }
3749
hayeswangb9702722014-02-18 21:49:00 +08003750 r8153_u1u2en(tp, false);
hayeswangb2143962015-07-24 13:54:23 +08003751 r8153_u2p3en(tp, false);
hayeswangb9702722014-02-18 21:49:00 +08003752 r8153_power_cut_en(tp, false);
hayeswangcda9fb02016-01-07 17:51:12 +08003753 r8153_aldps_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08003754 r8153_enter_oob(tp);
hayeswangcda9fb02016-01-07 17:51:12 +08003755 r8153_aldps_en(tp, true);
hayeswang43779f82014-01-02 11:25:10 +08003756}
3757
hayeswang65b82d62017-06-15 14:44:03 +08003758static void rtl8153b_up(struct r8152 *tp)
3759{
3760 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3761 return;
3762
3763 r8153b_u1u2en(tp, false);
3764 r8153_u2p3en(tp, false);
3765 r8153b_aldps_en(tp, false);
3766
3767 r8153_first_init(tp);
3768 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3769
3770 r8153b_aldps_en(tp, true);
3771 r8153_u2p3en(tp, true);
3772 r8153b_u1u2en(tp, true);
3773}
3774
3775static void rtl8153b_down(struct r8152 *tp)
3776{
3777 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3778 rtl_drop_queued_tx(tp);
3779 return;
3780 }
3781
3782 r8153b_u1u2en(tp, false);
3783 r8153_u2p3en(tp, false);
3784 r8153b_power_cut_en(tp, false);
3785 r8153b_aldps_en(tp, false);
3786 r8153_enter_oob(tp);
3787 r8153b_aldps_en(tp, true);
3788}
3789
hayeswang2dd49e02015-09-07 11:57:44 +08003790static bool rtl8152_in_nway(struct r8152 *tp)
3791{
3792 u16 nway_state;
3793
3794 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3795 tp->ocp_base = 0x2000;
3796 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3797 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3798
3799 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3800 if (nway_state & 0xc000)
3801 return false;
3802 else
3803 return true;
3804}
3805
3806static bool rtl8153_in_nway(struct r8152 *tp)
3807{
3808 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
3809
3810 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
3811 return false;
3812 else
3813 return true;
3814}
3815
hayeswangac718b62013-05-02 16:01:25 +00003816static void set_carrier(struct r8152 *tp)
3817{
3818 struct net_device *netdev = tp->netdev;
hayeswangce594e92017-03-16 14:32:22 +08003819 struct napi_struct *napi = &tp->napi;
hayeswangac718b62013-05-02 16:01:25 +00003820 u8 speed;
3821
3822 speed = rtl8152_get_speed(tp);
3823
3824 if (speed & LINK_STATUS) {
hayeswang51d979f2015-02-06 11:30:47 +08003825 if (!netif_carrier_ok(netdev)) {
hayeswangc81229c2014-01-02 11:22:42 +08003826 tp->rtl_ops.enable(tp);
hayeswangde9bf292017-01-26 09:38:32 +08003827 netif_stop_queue(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003828 napi_disable(napi);
hayeswangac718b62013-05-02 16:01:25 +00003829 netif_carrier_on(netdev);
hayeswangaa2e0922015-01-09 10:26:35 +08003830 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08003831 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
3832 _rtl8152_set_rx_mode(netdev);
hayeswang41cec842015-07-24 13:54:25 +08003833 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08003834 netif_wake_queue(netdev);
3835 netif_info(tp, link, netdev, "carrier on\n");
hayeswang2f25abe2017-03-23 19:14:19 +08003836 } else if (netif_queue_stopped(netdev) &&
3837 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
3838 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003839 }
3840 } else {
hayeswang51d979f2015-02-06 11:30:47 +08003841 if (netif_carrier_ok(netdev)) {
hayeswangac718b62013-05-02 16:01:25 +00003842 netif_carrier_off(netdev);
hayeswangce594e92017-03-16 14:32:22 +08003843 napi_disable(napi);
hayeswangc81229c2014-01-02 11:22:42 +08003844 tp->rtl_ops.disable(tp);
hayeswangce594e92017-03-16 14:32:22 +08003845 napi_enable(napi);
hayeswangde9bf292017-01-26 09:38:32 +08003846 netif_info(tp, link, netdev, "carrier off\n");
hayeswangac718b62013-05-02 16:01:25 +00003847 }
3848 }
hayeswangac718b62013-05-02 16:01:25 +00003849}
3850
3851static void rtl_work_func_t(struct work_struct *work)
3852{
3853 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
3854
hayeswanga1f83fe2014-11-12 10:05:05 +08003855 /* If the device is unplugged or !netif_running(), the workqueue
3856 * doesn't need to wake the device, and could return directly.
3857 */
3858 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
3859 return;
3860
hayeswang9a4be1b2014-02-18 21:49:07 +08003861 if (usb_autopm_get_interface(tp->intf) < 0)
3862 return;
3863
hayeswangac718b62013-05-02 16:01:25 +00003864 if (!test_bit(WORK_ENABLE, &tp->flags))
3865 goto out1;
3866
hayeswangb5403272014-10-09 18:00:26 +08003867 if (!mutex_trylock(&tp->control)) {
3868 schedule_delayed_work(&tp->schedule, 0);
3869 goto out1;
3870 }
3871
hayeswang216a8342016-01-07 17:51:11 +08003872 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
hayeswang40a82912013-08-14 20:54:40 +08003873 set_carrier(tp);
hayeswangac718b62013-05-02 16:01:25 +00003874
hayeswang216a8342016-01-07 17:51:11 +08003875 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +00003876 _rtl8152_set_rx_mode(tp->netdev);
3877
hayeswangd823ab62015-01-12 12:06:23 +08003878 /* don't schedule napi before linking */
hayeswang216a8342016-01-07 17:51:11 +08003879 if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
3880 netif_carrier_ok(tp->netdev))
hayeswangd823ab62015-01-12 12:06:23 +08003881 napi_schedule(&tp->napi);
hayeswangaa66a5f2014-02-18 21:49:04 +08003882
hayeswangb5403272014-10-09 18:00:26 +08003883 mutex_unlock(&tp->control);
3884
hayeswangac718b62013-05-02 16:01:25 +00003885out1:
hayeswang9a4be1b2014-02-18 21:49:07 +08003886 usb_autopm_put_interface(tp->intf);
hayeswangac718b62013-05-02 16:01:25 +00003887}
3888
hayeswanga028a9e2016-06-13 17:49:36 +08003889static void rtl_hw_phy_work_func_t(struct work_struct *work)
3890{
3891 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
3892
3893 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3894 return;
3895
3896 if (usb_autopm_get_interface(tp->intf) < 0)
3897 return;
3898
3899 mutex_lock(&tp->control);
3900
3901 tp->rtl_ops.hw_phy_cfg(tp);
3902
hayeswangaa7e26b2016-06-13 17:49:38 +08003903 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
hayeswang9d21c0d2016-06-13 17:49:37 +08003904
hayeswanga028a9e2016-06-13 17:49:36 +08003905 mutex_unlock(&tp->control);
3906
3907 usb_autopm_put_interface(tp->intf);
3908}
3909
hayeswang5ee3c602016-01-07 17:12:17 +08003910#ifdef CONFIG_PM_SLEEP
3911static int rtl_notifier(struct notifier_block *nb, unsigned long action,
3912 void *data)
3913{
3914 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
3915
3916 switch (action) {
3917 case PM_HIBERNATION_PREPARE:
3918 case PM_SUSPEND_PREPARE:
3919 usb_autopm_get_interface(tp->intf);
3920 break;
3921
3922 case PM_POST_HIBERNATION:
3923 case PM_POST_SUSPEND:
3924 usb_autopm_put_interface(tp->intf);
3925 break;
3926
3927 case PM_POST_RESTORE:
3928 case PM_RESTORE_PREPARE:
3929 default:
3930 break;
3931 }
3932
3933 return NOTIFY_DONE;
3934}
3935#endif
3936
hayeswangac718b62013-05-02 16:01:25 +00003937static int rtl8152_open(struct net_device *netdev)
3938{
3939 struct r8152 *tp = netdev_priv(netdev);
3940 int res = 0;
3941
hayeswang7e9da482014-02-18 21:49:05 +08003942 res = alloc_all_mem(tp);
3943 if (res)
3944 goto out;
3945
hayeswang9a4be1b2014-02-18 21:49:07 +08003946 res = usb_autopm_get_interface(tp->intf);
Guenter Roeckca0a7532016-11-09 19:51:25 -08003947 if (res < 0)
3948 goto out_free;
hayeswang9a4be1b2014-02-18 21:49:07 +08003949
hayeswangb5403272014-10-09 18:00:26 +08003950 mutex_lock(&tp->control);
3951
hayeswang7e9da482014-02-18 21:49:05 +08003952 tp->rtl_ops.up(tp);
3953
hayeswang40a82912013-08-14 20:54:40 +08003954 netif_carrier_off(netdev);
hayeswangac718b62013-05-02 16:01:25 +00003955 netif_start_queue(netdev);
3956 set_bit(WORK_ENABLE, &tp->flags);
hayeswangdb8515e2014-03-06 15:07:16 +08003957
hayeswang3d55f442014-02-06 11:55:48 +08003958 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3959 if (res) {
3960 if (res == -ENODEV)
3961 netif_device_detach(tp->netdev);
3962 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3963 res);
Guenter Roeckca0a7532016-11-09 19:51:25 -08003964 goto out_unlock;
hayeswang3d55f442014-02-06 11:55:48 +08003965 }
Guenter Roeckca0a7532016-11-09 19:51:25 -08003966 napi_enable(&tp->napi);
hayeswang3d55f442014-02-06 11:55:48 +08003967
hayeswangb5403272014-10-09 18:00:26 +08003968 mutex_unlock(&tp->control);
3969
hayeswang9a4be1b2014-02-18 21:49:07 +08003970 usb_autopm_put_interface(tp->intf);
hayeswang5ee3c602016-01-07 17:12:17 +08003971#ifdef CONFIG_PM_SLEEP
3972 tp->pm_notifier.notifier_call = rtl_notifier;
3973 register_pm_notifier(&tp->pm_notifier);
3974#endif
Guenter Roeckca0a7532016-11-09 19:51:25 -08003975 return 0;
hayeswangac718b62013-05-02 16:01:25 +00003976
Guenter Roeckca0a7532016-11-09 19:51:25 -08003977out_unlock:
3978 mutex_unlock(&tp->control);
3979 usb_autopm_put_interface(tp->intf);
3980out_free:
3981 free_all_mem(tp);
hayeswang7e9da482014-02-18 21:49:05 +08003982out:
hayeswangac718b62013-05-02 16:01:25 +00003983 return res;
3984}
3985
3986static int rtl8152_close(struct net_device *netdev)
3987{
3988 struct r8152 *tp = netdev_priv(netdev);
3989 int res = 0;
3990
hayeswang5ee3c602016-01-07 17:12:17 +08003991#ifdef CONFIG_PM_SLEEP
3992 unregister_pm_notifier(&tp->pm_notifier);
3993#endif
Jiri Slaby0ee1f472018-06-25 09:26:27 +02003994 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
3995 napi_disable(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00003996 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang3d55f442014-02-06 11:55:48 +08003997 usb_kill_urb(tp->intr_urb);
hayeswangac718b62013-05-02 16:01:25 +00003998 cancel_delayed_work_sync(&tp->schedule);
3999 netif_stop_queue(netdev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004000
4001 res = usb_autopm_get_interface(tp->intf);
hayeswang53543db2015-02-06 11:30:48 +08004002 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
hayeswang9a4be1b2014-02-18 21:49:07 +08004003 rtl_drop_queued_tx(tp);
hayeswangd823ab62015-01-12 12:06:23 +08004004 rtl_stop_rx(tp);
hayeswang9a4be1b2014-02-18 21:49:07 +08004005 } else {
hayeswangb5403272014-10-09 18:00:26 +08004006 mutex_lock(&tp->control);
4007
hayeswang9a4be1b2014-02-18 21:49:07 +08004008 tp->rtl_ops.down(tp);
hayeswangb5403272014-10-09 18:00:26 +08004009
4010 mutex_unlock(&tp->control);
4011
hayeswang9a4be1b2014-02-18 21:49:07 +08004012 usb_autopm_put_interface(tp->intf);
4013 }
hayeswangac718b62013-05-02 16:01:25 +00004014
hayeswang7e9da482014-02-18 21:49:05 +08004015 free_all_mem(tp);
4016
hayeswangac718b62013-05-02 16:01:25 +00004017 return res;
4018}
4019
hayeswang4f1d4d52014-03-11 16:24:19 +08004020static void rtl_tally_reset(struct r8152 *tp)
4021{
4022 u32 ocp_data;
4023
4024 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4025 ocp_data |= TALLY_RESET;
4026 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4027}
4028
hayeswangac718b62013-05-02 16:01:25 +00004029static void r8152b_init(struct r8152 *tp)
4030{
hayeswangebc2ec482013-08-14 20:54:38 +08004031 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004032 u16 data;
hayeswangac718b62013-05-02 16:01:25 +00004033
hayeswang68714382014-04-11 17:54:31 +08004034 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4035 return;
4036
hayeswang2dd436d2016-09-20 16:22:06 +08004037 data = r8152_mdio_read(tp, MII_BMCR);
4038 if (data & BMCR_PDOWN) {
4039 data &= ~BMCR_PDOWN;
4040 r8152_mdio_write(tp, MII_BMCR, data);
4041 }
4042
hayeswangcda9fb02016-01-07 17:51:12 +08004043 r8152_aldps_en(tp, false);
hayeswangd70b1132014-09-19 15:17:18 +08004044
hayeswangac718b62013-05-02 16:01:25 +00004045 if (tp->version == RTL_VER_01) {
4046 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4047 ocp_data &= ~LED_MODE_MASK;
4048 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4049 }
4050
hayeswang00a5e362014-02-18 21:48:59 +08004051 r8152_power_cut_en(tp, false);
hayeswangac718b62013-05-02 16:01:25 +00004052
hayeswangac718b62013-05-02 16:01:25 +00004053 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4054 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4055 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4056 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4057 ocp_data &= ~MCU_CLK_RATIO_MASK;
4058 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4059 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4060 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4061 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4062 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4063
hayeswang4f1d4d52014-03-11 16:24:19 +08004064 rtl_tally_reset(tp);
hayeswangac718b62013-05-02 16:01:25 +00004065
hayeswangebc2ec482013-08-14 20:54:38 +08004066 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00004067 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswange90fba82015-07-31 11:23:39 +08004068 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
hayeswangac718b62013-05-02 16:01:25 +00004069 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4070}
4071
hayeswang43779f82014-01-02 11:25:10 +08004072static void r8153_init(struct r8152 *tp)
4073{
4074 u32 ocp_data;
hayeswang2dd436d2016-09-20 16:22:06 +08004075 u16 data;
hayeswang43779f82014-01-02 11:25:10 +08004076 int i;
4077
hayeswang68714382014-04-11 17:54:31 +08004078 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4079 return;
4080
hayeswangb9702722014-02-18 21:49:00 +08004081 r8153_u1u2en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004082
4083 for (i = 0; i < 500; i++) {
4084 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4085 AUTOLOAD_DONE)
4086 break;
4087 msleep(20);
4088 }
4089
hayeswangc564b872017-06-09 17:11:38 +08004090 data = r8153_phy_status(tp, 0);
hayeswang43779f82014-01-02 11:25:10 +08004091
hayeswang2dd436d2016-09-20 16:22:06 +08004092 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4093 tp->version == RTL_VER_05)
4094 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4095
4096 data = r8152_mdio_read(tp, MII_BMCR);
4097 if (data & BMCR_PDOWN) {
4098 data &= ~BMCR_PDOWN;
4099 r8152_mdio_write(tp, MII_BMCR, data);
4100 }
4101
hayeswangc564b872017-06-09 17:11:38 +08004102 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
hayeswang2dd436d2016-09-20 16:22:06 +08004103
hayeswangb9702722014-02-18 21:49:00 +08004104 r8153_u2p3en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08004105
hayeswang65bab842015-02-12 16:20:46 +08004106 if (tp->version == RTL_VER_04) {
4107 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4108 ocp_data &= ~pwd_dn_scale_mask;
4109 ocp_data |= pwd_dn_scale(96);
4110 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4111
4112 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4113 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4114 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4115 } else if (tp->version == RTL_VER_05) {
4116 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4117 ocp_data &= ~ECM_ALDPS;
4118 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4119
4120 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4121 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4122 ocp_data &= ~DYNAMIC_BURST;
4123 else
4124 ocp_data |= DYNAMIC_BURST;
4125 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswangfb02eb42015-07-22 15:27:41 +08004126 } else if (tp->version == RTL_VER_06) {
4127 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4128 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4129 ocp_data &= ~DYNAMIC_BURST;
4130 else
4131 ocp_data |= DYNAMIC_BURST;
4132 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
hayeswang65bab842015-02-12 16:20:46 +08004133 }
4134
4135 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4136 ocp_data |= EP4_FULL_FC;
4137 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4138
hayeswang43779f82014-01-02 11:25:10 +08004139 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4140 ocp_data &= ~TIMER11_EN;
4141 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4142
hayeswang43779f82014-01-02 11:25:10 +08004143 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4144 ocp_data &= ~LED_MODE_MASK;
4145 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4146
hayeswang65bab842015-02-12 16:20:46 +08004147 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
Oliver Neukum2b84af94a2016-05-02 13:06:14 +02004148 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
hayeswang43779f82014-01-02 11:25:10 +08004149 ocp_data |= LPM_TIMER_500MS;
hayeswang34203e22015-02-06 11:30:46 +08004150 else
4151 ocp_data |= LPM_TIMER_500US;
hayeswang43779f82014-01-02 11:25:10 +08004152 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4153
4154 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4155 ocp_data &= ~SEN_VAL_MASK;
4156 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4157 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4158
hayeswang65bab842015-02-12 16:20:46 +08004159 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4160
hayeswangb9702722014-02-18 21:49:00 +08004161 r8153_power_cut_en(tp, false);
4162 r8153_u1u2en(tp, true);
hayeswang134f98b2017-06-09 17:11:40 +08004163 r8153_mac_clk_spd(tp, false);
hayeswangee4761c2017-06-09 17:11:39 +08004164 usb_enable_lpm(tp->udev);
hayeswang43779f82014-01-02 11:25:10 +08004165
hayeswange31f6362017-06-09 17:11:41 +08004166 /* rx aggregation */
4167 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4168 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
Kai-Heng Feng0b165512018-01-16 16:46:27 +08004169 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4170 ocp_data |= RX_AGG_DISABLE;
4171
hayeswange31f6362017-06-09 17:11:41 +08004172 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
hayeswang43779f82014-01-02 11:25:10 +08004173
hayeswang4f1d4d52014-03-11 16:24:19 +08004174 rtl_tally_reset(tp);
hayeswang49d10342017-06-09 17:11:44 +08004175
4176 switch (tp->udev->speed) {
4177 case USB_SPEED_SUPER:
4178 case USB_SPEED_SUPER_PLUS:
4179 tp->coalesce = COALESCE_SUPER;
4180 break;
4181 case USB_SPEED_HIGH:
4182 tp->coalesce = COALESCE_HIGH;
4183 break;
4184 default:
4185 tp->coalesce = COALESCE_SLOW;
4186 break;
4187 }
hayeswang43779f82014-01-02 11:25:10 +08004188}
4189
hayeswang65b82d62017-06-15 14:44:03 +08004190static void r8153b_init(struct r8152 *tp)
4191{
4192 u32 ocp_data;
4193 u16 data;
4194 int i;
4195
4196 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4197 return;
4198
4199 r8153b_u1u2en(tp, false);
4200
4201 for (i = 0; i < 500; i++) {
4202 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4203 AUTOLOAD_DONE)
4204 break;
4205 msleep(20);
4206 }
4207
4208 data = r8153_phy_status(tp, 0);
4209
4210 data = r8152_mdio_read(tp, MII_BMCR);
4211 if (data & BMCR_PDOWN) {
4212 data &= ~BMCR_PDOWN;
4213 r8152_mdio_write(tp, MII_BMCR, data);
4214 }
4215
4216 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4217
4218 r8153_u2p3en(tp, false);
4219
4220 /* MSC timer = 0xfff * 8ms = 32760 ms */
4221 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4222
4223 /* U1/U2/L1 idle timer. 500 us */
4224 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4225
4226 r8153b_power_cut_en(tp, false);
4227 r8153b_ups_en(tp, false);
4228 r8153b_queue_wake(tp, false);
4229 rtl_runtime_suspend_enable(tp, false);
4230 r8153b_u1u2en(tp, true);
4231 usb_enable_lpm(tp->udev);
4232
4233 /* MAC clock speed down */
4234 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4235 ocp_data |= MAC_CLK_SPDWN_EN;
4236 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4237
4238 set_bit(GREEN_ETHERNET, &tp->flags);
4239
4240 /* rx aggregation */
4241 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4242 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4243 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4244
4245 rtl_tally_reset(tp);
4246
4247 tp->coalesce = 15000; /* 15 us */
4248}
4249
hayeswange5011392015-07-29 20:39:08 +08004250static int rtl8152_pre_reset(struct usb_interface *intf)
4251{
4252 struct r8152 *tp = usb_get_intfdata(intf);
4253 struct net_device *netdev;
4254
4255 if (!tp)
4256 return 0;
4257
4258 netdev = tp->netdev;
4259 if (!netif_running(netdev))
4260 return 0;
4261
hayeswangde9bf292017-01-26 09:38:32 +08004262 netif_stop_queue(netdev);
hayeswange5011392015-07-29 20:39:08 +08004263 napi_disable(&tp->napi);
4264 clear_bit(WORK_ENABLE, &tp->flags);
4265 usb_kill_urb(tp->intr_urb);
4266 cancel_delayed_work_sync(&tp->schedule);
4267 if (netif_carrier_ok(netdev)) {
hayeswange5011392015-07-29 20:39:08 +08004268 mutex_lock(&tp->control);
4269 tp->rtl_ops.disable(tp);
4270 mutex_unlock(&tp->control);
4271 }
4272
4273 return 0;
4274}
4275
4276static int rtl8152_post_reset(struct usb_interface *intf)
4277{
4278 struct r8152 *tp = usb_get_intfdata(intf);
4279 struct net_device *netdev;
Mario Limonciello25766272019-04-04 13:46:53 -05004280 struct sockaddr sa;
hayeswange5011392015-07-29 20:39:08 +08004281
4282 if (!tp)
4283 return 0;
4284
Mario Limonciello25766272019-04-04 13:46:53 -05004285 /* reset the MAC adddress in case of policy change */
4286 if (determine_ethernet_addr(tp, &sa) >= 0) {
4287 rtnl_lock();
4288 dev_set_mac_address (tp->netdev, &sa, NULL);
4289 rtnl_unlock();
4290 }
4291
hayeswange5011392015-07-29 20:39:08 +08004292 netdev = tp->netdev;
4293 if (!netif_running(netdev))
4294 return 0;
4295
4296 set_bit(WORK_ENABLE, &tp->flags);
4297 if (netif_carrier_ok(netdev)) {
4298 mutex_lock(&tp->control);
4299 tp->rtl_ops.enable(tp);
hayeswang2c561b22017-01-20 14:33:55 +08004300 rtl_start_rx(tp);
Hayes Wangaece4772018-02-02 16:43:36 +08004301 _rtl8152_set_rx_mode(netdev);
hayeswange5011392015-07-29 20:39:08 +08004302 mutex_unlock(&tp->control);
hayeswange5011392015-07-29 20:39:08 +08004303 }
4304
4305 napi_enable(&tp->napi);
hayeswangde9bf292017-01-26 09:38:32 +08004306 netif_wake_queue(netdev);
hayeswang2c561b22017-01-20 14:33:55 +08004307 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
hayeswange5011392015-07-29 20:39:08 +08004308
hayeswang7489bda2017-01-26 09:38:34 +08004309 if (!list_empty(&tp->rx_done))
4310 napi_schedule(&tp->napi);
hayeswange5011392015-07-29 20:39:08 +08004311
4312 return 0;
hayeswangac718b62013-05-02 16:01:25 +00004313}
4314
hayeswang2dd49e02015-09-07 11:57:44 +08004315static bool delay_autosuspend(struct r8152 *tp)
4316{
4317 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4318 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4319
4320 /* This means a linking change occurs and the driver doesn't detect it,
4321 * yet. If the driver has disabled tx/rx and hw is linking on, the
4322 * device wouldn't wake up by receiving any packet.
4323 */
4324 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4325 return true;
4326
4327 /* If the linking down is occurred by nway, the device may miss the
4328 * linking change event. And it wouldn't wake when linking on.
4329 */
4330 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4331 return true;
hayeswang6a0b76c2017-01-23 14:18:43 +08004332 else if (!skb_queue_empty(&tp->tx_queue))
4333 return true;
hayeswang2dd49e02015-09-07 11:57:44 +08004334 else
4335 return false;
4336}
4337
hayeswang21cbd0e2017-06-13 15:14:39 +08004338static int rtl8152_runtime_resume(struct r8152 *tp)
4339{
4340 struct net_device *netdev = tp->netdev;
4341
4342 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4343 struct napi_struct *napi = &tp->napi;
4344
4345 tp->rtl_ops.autosuspend_en(tp, false);
4346 napi_disable(napi);
4347 set_bit(WORK_ENABLE, &tp->flags);
4348
4349 if (netif_carrier_ok(netdev)) {
4350 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4351 rtl_start_rx(tp);
4352 } else {
4353 netif_carrier_off(netdev);
4354 tp->rtl_ops.disable(tp);
4355 netif_info(tp, link, netdev, "linking down\n");
4356 }
4357 }
4358
4359 napi_enable(napi);
4360 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4361 smp_mb__after_atomic();
4362
4363 if (!list_empty(&tp->rx_done))
4364 napi_schedule(&tp->napi);
4365
4366 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4367 } else {
4368 if (netdev->flags & IFF_UP)
4369 tp->rtl_ops.autosuspend_en(tp, false);
4370
4371 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4372 }
4373
4374 return 0;
4375}
4376
4377static int rtl8152_system_resume(struct r8152 *tp)
4378{
4379 struct net_device *netdev = tp->netdev;
4380
4381 netif_device_attach(netdev);
4382
4383 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4384 tp->rtl_ops.up(tp);
4385 netif_carrier_off(netdev);
4386 set_bit(WORK_ENABLE, &tp->flags);
4387 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4388 }
4389
4390 return 0;
4391}
4392
hayeswanga9c54ad2017-01-25 13:41:45 +08004393static int rtl8152_runtime_suspend(struct r8152 *tp)
hayeswangac718b62013-05-02 16:01:25 +00004394{
hayeswang6cc69f22014-10-17 16:55:08 +08004395 struct net_device *netdev = tp->netdev;
4396 int ret = 0;
hayeswangac718b62013-05-02 16:01:25 +00004397
hayeswang26afec32017-01-26 09:38:31 +08004398 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4399 smp_mb__after_atomic();
4400
hayeswang8fb28062017-01-10 17:04:06 +08004401 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswang75dc6922017-01-10 17:04:07 +08004402 u32 rcr = 0;
4403
hayeswang75dc6922017-01-10 17:04:07 +08004404 if (netif_carrier_ok(netdev)) {
4405 u32 ocp_data;
4406
4407 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4408 ocp_data = rcr & ~RCR_ACPT_ALL;
4409 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4410 rxdy_gated_en(tp, true);
4411 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4412 PLA_OOB_CTRL);
4413 if (!(ocp_data & RXFIFO_EMPTY)) {
4414 rxdy_gated_en(tp, false);
4415 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswang26afec32017-01-26 09:38:31 +08004416 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4417 smp_mb__after_atomic();
hayeswang75dc6922017-01-10 17:04:07 +08004418 ret = -EBUSY;
4419 goto out1;
4420 }
4421 }
4422
hayeswang8fb28062017-01-10 17:04:06 +08004423 clear_bit(WORK_ENABLE, &tp->flags);
4424 usb_kill_urb(tp->intr_urb);
hayeswang75dc6922017-01-10 17:04:07 +08004425
hayeswang8fb28062017-01-10 17:04:06 +08004426 tp->rtl_ops.autosuspend_en(tp, true);
hayeswang75dc6922017-01-10 17:04:07 +08004427
4428 if (netif_carrier_ok(netdev)) {
hayeswangce594e92017-03-16 14:32:22 +08004429 struct napi_struct *napi = &tp->napi;
4430
4431 napi_disable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004432 rtl_stop_rx(tp);
4433 rxdy_gated_en(tp, false);
4434 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
hayeswangce594e92017-03-16 14:32:22 +08004435 napi_enable(napi);
hayeswang75dc6922017-01-10 17:04:07 +08004436 }
hayeswangbd882982017-06-13 15:14:40 +08004437
4438 if (delay_autosuspend(tp)) {
4439 rtl8152_runtime_resume(tp);
4440 ret = -EBUSY;
4441 }
hayeswang6cc69f22014-10-17 16:55:08 +08004442 }
4443
hayeswang8fb28062017-01-10 17:04:06 +08004444out1:
4445 return ret;
4446}
4447
4448static int rtl8152_system_suspend(struct r8152 *tp)
4449{
4450 struct net_device *netdev = tp->netdev;
hayeswang8fb28062017-01-10 17:04:06 +08004451
4452 netif_device_detach(netdev);
4453
hayeswange3bd1a82014-10-29 11:12:17 +08004454 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
hayeswangce594e92017-03-16 14:32:22 +08004455 struct napi_struct *napi = &tp->napi;
4456
hayeswangac718b62013-05-02 16:01:25 +00004457 clear_bit(WORK_ENABLE, &tp->flags);
hayeswang40a82912013-08-14 20:54:40 +08004458 usb_kill_urb(tp->intr_urb);
hayeswangce594e92017-03-16 14:32:22 +08004459 napi_disable(napi);
hayeswang8fb28062017-01-10 17:04:06 +08004460 cancel_delayed_work_sync(&tp->schedule);
4461 tp->rtl_ops.down(tp);
hayeswangce594e92017-03-16 14:32:22 +08004462 napi_enable(napi);
hayeswangac718b62013-05-02 16:01:25 +00004463 }
hayeswang8fb28062017-01-10 17:04:06 +08004464
zhong jiangf7419172018-08-09 09:39:13 +08004465 return 0;
hayeswang8fb28062017-01-10 17:04:06 +08004466}
4467
4468static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4469{
4470 struct r8152 *tp = usb_get_intfdata(intf);
4471 int ret;
4472
4473 mutex_lock(&tp->control);
4474
4475 if (PMSG_IS_AUTO(message))
hayeswanga9c54ad2017-01-25 13:41:45 +08004476 ret = rtl8152_runtime_suspend(tp);
hayeswang8fb28062017-01-10 17:04:06 +08004477 else
4478 ret = rtl8152_system_suspend(tp);
4479
hayeswangb5403272014-10-09 18:00:26 +08004480 mutex_unlock(&tp->control);
4481
hayeswang6cc69f22014-10-17 16:55:08 +08004482 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004483}
4484
4485static int rtl8152_resume(struct usb_interface *intf)
4486{
4487 struct r8152 *tp = usb_get_intfdata(intf);
hayeswang21cbd0e2017-06-13 15:14:39 +08004488 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004489
hayeswangb5403272014-10-09 18:00:26 +08004490 mutex_lock(&tp->control);
4491
hayeswang21cbd0e2017-06-13 15:14:39 +08004492 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4493 ret = rtl8152_runtime_resume(tp);
4494 else
4495 ret = rtl8152_system_resume(tp);
hayeswangac718b62013-05-02 16:01:25 +00004496
hayeswangb5403272014-10-09 18:00:26 +08004497 mutex_unlock(&tp->control);
4498
hayeswang21cbd0e2017-06-13 15:14:39 +08004499 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004500}
4501
hayeswang7ec25412016-01-04 14:38:46 +08004502static int rtl8152_reset_resume(struct usb_interface *intf)
4503{
4504 struct r8152 *tp = usb_get_intfdata(intf);
4505
4506 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
hayeswangbefb2de2017-06-09 17:11:45 +08004507 mutex_lock(&tp->control);
4508 tp->rtl_ops.init(tp);
4509 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4510 mutex_unlock(&tp->control);
hayeswang7ec25412016-01-04 14:38:46 +08004511 return rtl8152_resume(intf);
4512}
4513
hayeswang21ff2e82014-02-18 21:49:06 +08004514static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4515{
4516 struct r8152 *tp = netdev_priv(dev);
4517
hayeswang9a4be1b2014-02-18 21:49:07 +08004518 if (usb_autopm_get_interface(tp->intf) < 0)
4519 return;
4520
hayeswang7daed8d2015-07-24 13:54:24 +08004521 if (!rtl_can_wakeup(tp)) {
4522 wol->supported = 0;
4523 wol->wolopts = 0;
4524 } else {
4525 mutex_lock(&tp->control);
4526 wol->supported = WAKE_ANY;
4527 wol->wolopts = __rtl_get_wol(tp);
4528 mutex_unlock(&tp->control);
4529 }
hayeswangb5403272014-10-09 18:00:26 +08004530
hayeswang9a4be1b2014-02-18 21:49:07 +08004531 usb_autopm_put_interface(tp->intf);
hayeswang21ff2e82014-02-18 21:49:06 +08004532}
4533
4534static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4535{
4536 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004537 int ret;
4538
hayeswang7daed8d2015-07-24 13:54:24 +08004539 if (!rtl_can_wakeup(tp))
4540 return -EOPNOTSUPP;
4541
Florian Fainellif2750df2018-09-28 16:18:54 -07004542 if (wol->wolopts & ~WAKE_ANY)
4543 return -EINVAL;
4544
hayeswang9a4be1b2014-02-18 21:49:07 +08004545 ret = usb_autopm_get_interface(tp->intf);
4546 if (ret < 0)
4547 goto out_set_wol;
hayeswang21ff2e82014-02-18 21:49:06 +08004548
hayeswangb5403272014-10-09 18:00:26 +08004549 mutex_lock(&tp->control);
4550
hayeswang21ff2e82014-02-18 21:49:06 +08004551 __rtl_set_wol(tp, wol->wolopts);
4552 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4553
hayeswangb5403272014-10-09 18:00:26 +08004554 mutex_unlock(&tp->control);
4555
hayeswang9a4be1b2014-02-18 21:49:07 +08004556 usb_autopm_put_interface(tp->intf);
4557
4558out_set_wol:
4559 return ret;
hayeswang21ff2e82014-02-18 21:49:06 +08004560}
4561
hayeswanga5ec27c2014-02-18 21:49:11 +08004562static u32 rtl8152_get_msglevel(struct net_device *dev)
4563{
4564 struct r8152 *tp = netdev_priv(dev);
4565
4566 return tp->msg_enable;
4567}
4568
4569static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4570{
4571 struct r8152 *tp = netdev_priv(dev);
4572
4573 tp->msg_enable = value;
4574}
4575
hayeswangac718b62013-05-02 16:01:25 +00004576static void rtl8152_get_drvinfo(struct net_device *netdev,
4577 struct ethtool_drvinfo *info)
4578{
4579 struct r8152 *tp = netdev_priv(netdev);
4580
hayeswangb0b46c72014-08-26 10:08:23 +08004581 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4582 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
hayeswangac718b62013-05-02 16:01:25 +00004583 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4584}
4585
4586static
Philippe Reynes06144dc2017-03-12 22:41:58 +01004587int rtl8152_get_link_ksettings(struct net_device *netdev,
4588 struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004589{
4590 struct r8152 *tp = netdev_priv(netdev);
hayeswang8d4a4d72014-10-09 18:00:25 +08004591 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004592
4593 if (!tp->mii.mdio_read)
4594 return -EOPNOTSUPP;
4595
hayeswang8d4a4d72014-10-09 18:00:25 +08004596 ret = usb_autopm_get_interface(tp->intf);
4597 if (ret < 0)
4598 goto out;
4599
hayeswangb5403272014-10-09 18:00:26 +08004600 mutex_lock(&tp->control);
4601
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03004602 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
hayeswang8d4a4d72014-10-09 18:00:25 +08004603
hayeswangb5403272014-10-09 18:00:26 +08004604 mutex_unlock(&tp->control);
4605
hayeswang8d4a4d72014-10-09 18:00:25 +08004606 usb_autopm_put_interface(tp->intf);
4607
4608out:
4609 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004610}
4611
Philippe Reynes06144dc2017-03-12 22:41:58 +01004612static int rtl8152_set_link_ksettings(struct net_device *dev,
4613 const struct ethtool_link_ksettings *cmd)
hayeswangac718b62013-05-02 16:01:25 +00004614{
4615 struct r8152 *tp = netdev_priv(dev);
hayeswang9a4be1b2014-02-18 21:49:07 +08004616 int ret;
hayeswangac718b62013-05-02 16:01:25 +00004617
hayeswang9a4be1b2014-02-18 21:49:07 +08004618 ret = usb_autopm_get_interface(tp->intf);
4619 if (ret < 0)
4620 goto out;
4621
hayeswangb5403272014-10-09 18:00:26 +08004622 mutex_lock(&tp->control);
4623
Philippe Reynes06144dc2017-03-12 22:41:58 +01004624 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4625 cmd->base.duplex);
hayeswangaa7e26b2016-06-13 17:49:38 +08004626 if (!ret) {
Philippe Reynes06144dc2017-03-12 22:41:58 +01004627 tp->autoneg = cmd->base.autoneg;
4628 tp->speed = cmd->base.speed;
4629 tp->duplex = cmd->base.duplex;
hayeswangaa7e26b2016-06-13 17:49:38 +08004630 }
hayeswang9a4be1b2014-02-18 21:49:07 +08004631
hayeswangb5403272014-10-09 18:00:26 +08004632 mutex_unlock(&tp->control);
4633
hayeswang9a4be1b2014-02-18 21:49:07 +08004634 usb_autopm_put_interface(tp->intf);
4635
4636out:
4637 return ret;
hayeswangac718b62013-05-02 16:01:25 +00004638}
4639
hayeswang4f1d4d52014-03-11 16:24:19 +08004640static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4641 "tx_packets",
4642 "rx_packets",
4643 "tx_errors",
4644 "rx_errors",
4645 "rx_missed",
4646 "align_errors",
4647 "tx_single_collisions",
4648 "tx_multi_collisions",
4649 "rx_unicast",
4650 "rx_broadcast",
4651 "rx_multicast",
4652 "tx_aborted",
4653 "tx_underrun",
4654};
4655
4656static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4657{
4658 switch (sset) {
4659 case ETH_SS_STATS:
4660 return ARRAY_SIZE(rtl8152_gstrings);
4661 default:
4662 return -EOPNOTSUPP;
4663 }
4664}
4665
4666static void rtl8152_get_ethtool_stats(struct net_device *dev,
4667 struct ethtool_stats *stats, u64 *data)
4668{
4669 struct r8152 *tp = netdev_priv(dev);
4670 struct tally_counter tally;
4671
hayeswang0b030242014-07-08 14:49:28 +08004672 if (usb_autopm_get_interface(tp->intf) < 0)
4673 return;
4674
hayeswang4f1d4d52014-03-11 16:24:19 +08004675 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4676
hayeswang0b030242014-07-08 14:49:28 +08004677 usb_autopm_put_interface(tp->intf);
4678
hayeswang4f1d4d52014-03-11 16:24:19 +08004679 data[0] = le64_to_cpu(tally.tx_packets);
4680 data[1] = le64_to_cpu(tally.rx_packets);
4681 data[2] = le64_to_cpu(tally.tx_errors);
4682 data[3] = le32_to_cpu(tally.rx_errors);
4683 data[4] = le16_to_cpu(tally.rx_missed);
4684 data[5] = le16_to_cpu(tally.align_errors);
4685 data[6] = le32_to_cpu(tally.tx_one_collision);
4686 data[7] = le32_to_cpu(tally.tx_multi_collision);
4687 data[8] = le64_to_cpu(tally.rx_unicast);
4688 data[9] = le64_to_cpu(tally.rx_broadcast);
4689 data[10] = le32_to_cpu(tally.rx_multicast);
4690 data[11] = le16_to_cpu(tally.tx_aborted);
hayeswangf37119c2014-10-28 14:05:51 +08004691 data[12] = le16_to_cpu(tally.tx_underrun);
hayeswang4f1d4d52014-03-11 16:24:19 +08004692}
4693
4694static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4695{
4696 switch (stringset) {
4697 case ETH_SS_STATS:
4698 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4699 break;
4700 }
4701}
4702
hayeswangdf35d282014-09-25 20:54:02 +08004703static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4704{
4705 u32 ocp_data, lp, adv, supported = 0;
4706 u16 val;
4707
4708 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4709 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4710
4711 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4712 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4713
4714 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4715 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4716
4717 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4718 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4719
4720 eee->eee_enabled = !!ocp_data;
4721 eee->eee_active = !!(supported & adv & lp);
4722 eee->supported = supported;
4723 eee->advertised = adv;
4724 eee->lp_advertised = lp;
4725
4726 return 0;
4727}
4728
4729static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4730{
4731 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4732
4733 r8152_eee_en(tp, eee->eee_enabled);
4734
4735 if (!eee->eee_enabled)
4736 val = 0;
4737
4738 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4739
4740 return 0;
4741}
4742
4743static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4744{
4745 u32 ocp_data, lp, adv, supported = 0;
4746 u16 val;
4747
4748 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4749 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4750
4751 val = ocp_reg_read(tp, OCP_EEE_ADV);
4752 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4753
4754 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4755 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4756
4757 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4758 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4759
4760 eee->eee_enabled = !!ocp_data;
4761 eee->eee_active = !!(supported & adv & lp);
4762 eee->supported = supported;
4763 eee->advertised = adv;
4764 eee->lp_advertised = lp;
4765
4766 return 0;
4767}
4768
4769static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4770{
4771 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4772
4773 r8153_eee_en(tp, eee->eee_enabled);
4774
4775 if (!eee->eee_enabled)
4776 val = 0;
4777
4778 ocp_reg_write(tp, OCP_EEE_ADV, val);
4779
4780 return 0;
4781}
4782
hayeswang65b82d62017-06-15 14:44:03 +08004783static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4784{
4785 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4786
4787 r8153b_eee_en(tp, eee->eee_enabled);
4788
4789 if (!eee->eee_enabled)
4790 val = 0;
4791
4792 ocp_reg_write(tp, OCP_EEE_ADV, val);
4793
4794 return 0;
4795}
4796
hayeswangdf35d282014-09-25 20:54:02 +08004797static int
4798rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4799{
4800 struct r8152 *tp = netdev_priv(net);
4801 int ret;
4802
4803 ret = usb_autopm_get_interface(tp->intf);
4804 if (ret < 0)
4805 goto out;
4806
hayeswangb5403272014-10-09 18:00:26 +08004807 mutex_lock(&tp->control);
4808
hayeswangdf35d282014-09-25 20:54:02 +08004809 ret = tp->rtl_ops.eee_get(tp, edata);
4810
hayeswangb5403272014-10-09 18:00:26 +08004811 mutex_unlock(&tp->control);
4812
hayeswangdf35d282014-09-25 20:54:02 +08004813 usb_autopm_put_interface(tp->intf);
4814
4815out:
4816 return ret;
4817}
4818
4819static int
4820rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
4821{
4822 struct r8152 *tp = netdev_priv(net);
4823 int ret;
4824
4825 ret = usb_autopm_get_interface(tp->intf);
4826 if (ret < 0)
4827 goto out;
4828
hayeswangb5403272014-10-09 18:00:26 +08004829 mutex_lock(&tp->control);
4830
hayeswangdf35d282014-09-25 20:54:02 +08004831 ret = tp->rtl_ops.eee_set(tp, edata);
hayeswang9d31a7b2014-10-06 10:36:04 +08004832 if (!ret)
4833 ret = mii_nway_restart(&tp->mii);
hayeswangdf35d282014-09-25 20:54:02 +08004834
hayeswangb5403272014-10-09 18:00:26 +08004835 mutex_unlock(&tp->control);
4836
hayeswangdf35d282014-09-25 20:54:02 +08004837 usb_autopm_put_interface(tp->intf);
4838
4839out:
4840 return ret;
4841}
4842
hayeswang8884f502014-10-28 14:05:52 +08004843static int rtl8152_nway_reset(struct net_device *dev)
4844{
4845 struct r8152 *tp = netdev_priv(dev);
4846 int ret;
4847
4848 ret = usb_autopm_get_interface(tp->intf);
4849 if (ret < 0)
4850 goto out;
4851
4852 mutex_lock(&tp->control);
4853
4854 ret = mii_nway_restart(&tp->mii);
4855
4856 mutex_unlock(&tp->control);
4857
4858 usb_autopm_put_interface(tp->intf);
4859
4860out:
4861 return ret;
4862}
4863
hayeswangefb3dd82015-02-12 14:33:48 +08004864static int rtl8152_get_coalesce(struct net_device *netdev,
4865 struct ethtool_coalesce *coalesce)
4866{
4867 struct r8152 *tp = netdev_priv(netdev);
4868
4869 switch (tp->version) {
4870 case RTL_VER_01:
4871 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004872 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004873 return -EOPNOTSUPP;
4874 default:
4875 break;
4876 }
4877
4878 coalesce->rx_coalesce_usecs = tp->coalesce;
4879
4880 return 0;
4881}
4882
4883static int rtl8152_set_coalesce(struct net_device *netdev,
4884 struct ethtool_coalesce *coalesce)
4885{
4886 struct r8152 *tp = netdev_priv(netdev);
4887 int ret;
4888
4889 switch (tp->version) {
4890 case RTL_VER_01:
4891 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004892 case RTL_VER_07:
hayeswangefb3dd82015-02-12 14:33:48 +08004893 return -EOPNOTSUPP;
4894 default:
4895 break;
4896 }
4897
4898 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
4899 return -EINVAL;
4900
4901 ret = usb_autopm_get_interface(tp->intf);
4902 if (ret < 0)
4903 return ret;
4904
4905 mutex_lock(&tp->control);
4906
4907 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
4908 tp->coalesce = coalesce->rx_coalesce_usecs;
4909
4910 if (netif_running(tp->netdev) && netif_carrier_ok(netdev))
4911 r8153_set_rx_early_timeout(tp);
4912 }
4913
4914 mutex_unlock(&tp->control);
4915
4916 usb_autopm_put_interface(tp->intf);
4917
4918 return ret;
4919}
4920
Julia Lawall407a4712016-09-01 00:21:22 +02004921static const struct ethtool_ops ops = {
hayeswangac718b62013-05-02 16:01:25 +00004922 .get_drvinfo = rtl8152_get_drvinfo,
hayeswangac718b62013-05-02 16:01:25 +00004923 .get_link = ethtool_op_get_link,
hayeswang8884f502014-10-28 14:05:52 +08004924 .nway_reset = rtl8152_nway_reset,
hayeswanga5ec27c2014-02-18 21:49:11 +08004925 .get_msglevel = rtl8152_get_msglevel,
4926 .set_msglevel = rtl8152_set_msglevel,
hayeswang21ff2e82014-02-18 21:49:06 +08004927 .get_wol = rtl8152_get_wol,
4928 .set_wol = rtl8152_set_wol,
hayeswang4f1d4d52014-03-11 16:24:19 +08004929 .get_strings = rtl8152_get_strings,
4930 .get_sset_count = rtl8152_get_sset_count,
4931 .get_ethtool_stats = rtl8152_get_ethtool_stats,
hayeswangefb3dd82015-02-12 14:33:48 +08004932 .get_coalesce = rtl8152_get_coalesce,
4933 .set_coalesce = rtl8152_set_coalesce,
hayeswangdf35d282014-09-25 20:54:02 +08004934 .get_eee = rtl_ethtool_get_eee,
4935 .set_eee = rtl_ethtool_set_eee,
Philippe Reynes06144dc2017-03-12 22:41:58 +01004936 .get_link_ksettings = rtl8152_get_link_ksettings,
4937 .set_link_ksettings = rtl8152_set_link_ksettings,
hayeswangac718b62013-05-02 16:01:25 +00004938};
4939
4940static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
4941{
4942 struct r8152 *tp = netdev_priv(netdev);
4943 struct mii_ioctl_data *data = if_mii(rq);
hayeswang9a4be1b2014-02-18 21:49:07 +08004944 int res;
4945
hayeswang68714382014-04-11 17:54:31 +08004946 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4947 return -ENODEV;
4948
hayeswang9a4be1b2014-02-18 21:49:07 +08004949 res = usb_autopm_get_interface(tp->intf);
4950 if (res < 0)
4951 goto out;
hayeswangac718b62013-05-02 16:01:25 +00004952
4953 switch (cmd) {
4954 case SIOCGMIIPHY:
4955 data->phy_id = R8152_PHY_ID; /* Internal PHY */
4956 break;
4957
4958 case SIOCGMIIREG:
hayeswangb5403272014-10-09 18:00:26 +08004959 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004960 data->val_out = r8152_mdio_read(tp, data->reg_num);
hayeswangb5403272014-10-09 18:00:26 +08004961 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004962 break;
4963
4964 case SIOCSMIIREG:
4965 if (!capable(CAP_NET_ADMIN)) {
4966 res = -EPERM;
4967 break;
4968 }
hayeswangb5403272014-10-09 18:00:26 +08004969 mutex_lock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004970 r8152_mdio_write(tp, data->reg_num, data->val_in);
hayeswangb5403272014-10-09 18:00:26 +08004971 mutex_unlock(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00004972 break;
4973
4974 default:
4975 res = -EOPNOTSUPP;
4976 }
4977
hayeswang9a4be1b2014-02-18 21:49:07 +08004978 usb_autopm_put_interface(tp->intf);
4979
4980out:
hayeswangac718b62013-05-02 16:01:25 +00004981 return res;
4982}
4983
hayeswang69b4b7a2014-07-10 10:58:54 +08004984static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
4985{
4986 struct r8152 *tp = netdev_priv(dev);
hayeswang396e2e22015-02-12 14:33:47 +08004987 int ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08004988
4989 switch (tp->version) {
4990 case RTL_VER_01:
4991 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08004992 case RTL_VER_07:
Jarod Wilsona52ad512016-10-07 22:04:34 -04004993 dev->mtu = new_mtu;
4994 return 0;
hayeswang69b4b7a2014-07-10 10:58:54 +08004995 default:
4996 break;
4997 }
4998
hayeswang396e2e22015-02-12 14:33:47 +08004999 ret = usb_autopm_get_interface(tp->intf);
5000 if (ret < 0)
5001 return ret;
5002
5003 mutex_lock(&tp->control);
5004
hayeswang69b4b7a2014-07-10 10:58:54 +08005005 dev->mtu = new_mtu;
5006
hayeswang210c4f72017-03-20 16:13:44 +08005007 if (netif_running(dev)) {
hayeswangb65c0c92017-06-21 11:25:18 +08005008 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
hayeswang210c4f72017-03-20 16:13:44 +08005009
5010 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
5011
5012 if (netif_carrier_ok(dev))
5013 r8153_set_rx_early_size(tp);
5014 }
hayeswang396e2e22015-02-12 14:33:47 +08005015
5016 mutex_unlock(&tp->control);
5017
5018 usb_autopm_put_interface(tp->intf);
5019
5020 return ret;
hayeswang69b4b7a2014-07-10 10:58:54 +08005021}
5022
hayeswangac718b62013-05-02 16:01:25 +00005023static const struct net_device_ops rtl8152_netdev_ops = {
5024 .ndo_open = rtl8152_open,
5025 .ndo_stop = rtl8152_close,
5026 .ndo_do_ioctl = rtl8152_ioctl,
5027 .ndo_start_xmit = rtl8152_start_xmit,
5028 .ndo_tx_timeout = rtl8152_tx_timeout,
hayeswangc5554292014-09-12 10:43:11 +08005029 .ndo_set_features = rtl8152_set_features,
hayeswangac718b62013-05-02 16:01:25 +00005030 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5031 .ndo_set_mac_address = rtl8152_set_mac_address,
hayeswang69b4b7a2014-07-10 10:58:54 +08005032 .ndo_change_mtu = rtl8152_change_mtu,
hayeswangac718b62013-05-02 16:01:25 +00005033 .ndo_validate_addr = eth_validate_addr,
hayeswanga5e31252015-01-06 17:41:58 +08005034 .ndo_features_check = rtl8152_features_check,
hayeswangac718b62013-05-02 16:01:25 +00005035};
5036
hayeswange3fe0b12014-01-02 11:22:39 +08005037static void rtl8152_unload(struct r8152 *tp)
5038{
hayeswang68714382014-04-11 17:54:31 +08005039 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5040 return;
5041
hayeswang00a5e362014-02-18 21:48:59 +08005042 if (tp->version != RTL_VER_01)
5043 r8152_power_cut_en(tp, true);
hayeswange3fe0b12014-01-02 11:22:39 +08005044}
5045
hayeswang43779f82014-01-02 11:25:10 +08005046static void rtl8153_unload(struct r8152 *tp)
5047{
hayeswang68714382014-04-11 17:54:31 +08005048 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5049 return;
5050
hayeswang49be1722014-10-01 13:25:11 +08005051 r8153_power_cut_en(tp, false);
hayeswang43779f82014-01-02 11:25:10 +08005052}
5053
hayeswang65b82d62017-06-15 14:44:03 +08005054static void rtl8153b_unload(struct r8152 *tp)
5055{
5056 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5057 return;
5058
5059 r8153b_power_cut_en(tp, false);
5060}
5061
hayeswang55b65472014-11-06 12:47:39 +08005062static int rtl_ops_init(struct r8152 *tp)
hayeswangc81229c2014-01-02 11:22:42 +08005063{
5064 struct rtl_ops *ops = &tp->rtl_ops;
hayeswang55b65472014-11-06 12:47:39 +08005065 int ret = 0;
hayeswangc81229c2014-01-02 11:22:42 +08005066
hayeswang55b65472014-11-06 12:47:39 +08005067 switch (tp->version) {
5068 case RTL_VER_01:
5069 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005070 case RTL_VER_07:
hayeswang55b65472014-11-06 12:47:39 +08005071 ops->init = r8152b_init;
5072 ops->enable = rtl8152_enable;
5073 ops->disable = rtl8152_disable;
5074 ops->up = rtl8152_up;
5075 ops->down = rtl8152_down;
5076 ops->unload = rtl8152_unload;
5077 ops->eee_get = r8152_get_eee;
5078 ops->eee_set = r8152_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005079 ops->in_nway = rtl8152_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005080 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005081 ops->autosuspend_en = rtl_runtime_suspend_enable;
hayeswang43779f82014-01-02 11:25:10 +08005082 break;
5083
hayeswang55b65472014-11-06 12:47:39 +08005084 case RTL_VER_03:
5085 case RTL_VER_04:
5086 case RTL_VER_05:
hayeswangfb02eb42015-07-22 15:27:41 +08005087 case RTL_VER_06:
hayeswang55b65472014-11-06 12:47:39 +08005088 ops->init = r8153_init;
5089 ops->enable = rtl8153_enable;
5090 ops->disable = rtl8153_disable;
5091 ops->up = rtl8153_up;
5092 ops->down = rtl8153_down;
5093 ops->unload = rtl8153_unload;
5094 ops->eee_get = r8153_get_eee;
5095 ops->eee_set = r8153_set_eee;
hayeswang2dd49e02015-09-07 11:57:44 +08005096 ops->in_nway = rtl8153_in_nway;
hayeswanga028a9e2016-06-13 17:49:36 +08005097 ops->hw_phy_cfg = r8153_hw_phy_cfg;
hayeswang2609af12016-07-05 16:11:46 +08005098 ops->autosuspend_en = rtl8153_runtime_enable;
hayeswangc81229c2014-01-02 11:22:42 +08005099 break;
5100
hayeswang65b82d62017-06-15 14:44:03 +08005101 case RTL_VER_08:
5102 case RTL_VER_09:
5103 ops->init = r8153b_init;
5104 ops->enable = rtl8153_enable;
5105 ops->disable = rtl8153b_disable;
5106 ops->up = rtl8153b_up;
5107 ops->down = rtl8153b_down;
5108 ops->unload = rtl8153b_unload;
5109 ops->eee_get = r8153_get_eee;
5110 ops->eee_set = r8153b_set_eee;
5111 ops->in_nway = rtl8153_in_nway;
5112 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5113 ops->autosuspend_en = rtl8153b_runtime_enable;
5114 break;
5115
hayeswangc81229c2014-01-02 11:22:42 +08005116 default:
hayeswang55b65472014-11-06 12:47:39 +08005117 ret = -ENODEV;
5118 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
hayeswangc81229c2014-01-02 11:22:42 +08005119 break;
5120 }
5121
5122 return ret;
5123}
5124
hayeswang33928ee2017-03-17 11:20:13 +08005125static u8 rtl_get_version(struct usb_interface *intf)
5126{
5127 struct usb_device *udev = interface_to_usbdev(intf);
5128 u32 ocp_data = 0;
5129 __le32 *tmp;
5130 u8 version;
5131 int ret;
5132
5133 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5134 if (!tmp)
5135 return 0;
5136
5137 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5138 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5139 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5140 if (ret > 0)
5141 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5142
5143 kfree(tmp);
5144
5145 switch (ocp_data) {
5146 case 0x4c00:
5147 version = RTL_VER_01;
5148 break;
5149 case 0x4c10:
5150 version = RTL_VER_02;
5151 break;
5152 case 0x5c00:
5153 version = RTL_VER_03;
5154 break;
5155 case 0x5c10:
5156 version = RTL_VER_04;
5157 break;
5158 case 0x5c20:
5159 version = RTL_VER_05;
5160 break;
5161 case 0x5c30:
5162 version = RTL_VER_06;
5163 break;
hayeswangc27b32c2017-06-15 14:44:02 +08005164 case 0x4800:
5165 version = RTL_VER_07;
5166 break;
hayeswang65b82d62017-06-15 14:44:03 +08005167 case 0x6000:
5168 version = RTL_VER_08;
5169 break;
5170 case 0x6010:
5171 version = RTL_VER_09;
5172 break;
hayeswang33928ee2017-03-17 11:20:13 +08005173 default:
5174 version = RTL_VER_UNKNOWN;
5175 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5176 break;
5177 }
5178
Oliver Neukumeb3c28c2017-06-12 13:56:51 +02005179 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5180
hayeswang33928ee2017-03-17 11:20:13 +08005181 return version;
5182}
5183
hayeswangac718b62013-05-02 16:01:25 +00005184static int rtl8152_probe(struct usb_interface *intf,
5185 const struct usb_device_id *id)
5186{
5187 struct usb_device *udev = interface_to_usbdev(intf);
hayeswang33928ee2017-03-17 11:20:13 +08005188 u8 version = rtl_get_version(intf);
hayeswangac718b62013-05-02 16:01:25 +00005189 struct r8152 *tp;
5190 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08005191 int ret;
hayeswangac718b62013-05-02 16:01:25 +00005192
hayeswang33928ee2017-03-17 11:20:13 +08005193 if (version == RTL_VER_UNKNOWN)
5194 return -ENODEV;
5195
hayeswang10c32712014-03-04 20:47:48 +08005196 if (udev->actconfig->desc.bConfigurationValue != 1) {
5197 usb_driver_set_configuration(udev, 1);
5198 return -ENODEV;
5199 }
5200
5201 usb_reset_device(udev);
hayeswangac718b62013-05-02 16:01:25 +00005202 netdev = alloc_etherdev(sizeof(struct r8152));
5203 if (!netdev) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005204 dev_err(&intf->dev, "Out of memory\n");
hayeswangac718b62013-05-02 16:01:25 +00005205 return -ENOMEM;
5206 }
5207
hayeswangebc2ec482013-08-14 20:54:38 +08005208 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00005209 tp = netdev_priv(netdev);
5210 tp->msg_enable = 0x7FFF;
5211
hayeswange3ad4122014-01-06 17:08:42 +08005212 tp->udev = udev;
5213 tp->netdev = netdev;
5214 tp->intf = intf;
hayeswang33928ee2017-03-17 11:20:13 +08005215 tp->version = version;
hayeswange3ad4122014-01-06 17:08:42 +08005216
hayeswang33928ee2017-03-17 11:20:13 +08005217 switch (version) {
5218 case RTL_VER_01:
5219 case RTL_VER_02:
hayeswangc27b32c2017-06-15 14:44:02 +08005220 case RTL_VER_07:
hayeswang33928ee2017-03-17 11:20:13 +08005221 tp->mii.supports_gmii = 0;
5222 break;
5223 default:
5224 tp->mii.supports_gmii = 1;
5225 break;
5226 }
5227
hayeswang55b65472014-11-06 12:47:39 +08005228 ret = rtl_ops_init(tp);
hayeswang31ca1de2014-01-06 17:08:43 +08005229 if (ret)
5230 goto out;
hayeswangc81229c2014-01-02 11:22:42 +08005231
hayeswangb5403272014-10-09 18:00:26 +08005232 mutex_init(&tp->control);
hayeswangac718b62013-05-02 16:01:25 +00005233 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
hayeswanga028a9e2016-06-13 17:49:36 +08005234 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
hayeswangac718b62013-05-02 16:01:25 +00005235
hayeswangac718b62013-05-02 16:01:25 +00005236 netdev->netdev_ops = &rtl8152_netdev_ops;
5237 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08005238
hayeswang60c89072014-03-07 11:04:39 +08005239 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005240 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
hayeswangc5554292014-09-12 10:43:11 +08005241 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5242 NETIF_F_HW_VLAN_CTAG_TX;
hayeswang60c89072014-03-07 11:04:39 +08005243 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
hayeswang6128d1bb2014-03-07 11:04:40 +08005244 NETIF_F_TSO | NETIF_F_FRAGLIST |
hayeswangc5554292014-09-12 10:43:11 +08005245 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
hayeswangccc39fa2015-02-06 11:30:49 +08005246 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
hayeswangc5554292014-09-12 10:43:11 +08005247 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5248 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5249 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswangdb8515e2014-03-06 15:07:16 +08005250
hayeswang19c0f402017-01-11 16:25:34 +08005251 if (tp->version == RTL_VER_01) {
5252 netdev->features &= ~NETIF_F_RXCSUM;
5253 netdev->hw_features &= ~NETIF_F_RXCSUM;
5254 }
5255
Kai-Heng Feng176eb612018-08-20 12:43:51 +08005256 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5257 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
Kai-Heng Feng0b165512018-01-16 16:46:27 +08005258 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5259 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5260 }
5261
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005262 netdev->ethtool_ops = &ops;
hayeswang60c89072014-03-07 11:04:39 +08005263 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
hayeswangac718b62013-05-02 16:01:25 +00005264
Jarod Wilsonf77f0ae2016-10-20 13:55:17 -04005265 /* MTU range: 68 - 1500 or 9194 */
5266 netdev->min_mtu = ETH_MIN_MTU;
5267 switch (tp->version) {
5268 case RTL_VER_01:
5269 case RTL_VER_02:
5270 netdev->max_mtu = ETH_DATA_LEN;
5271 break;
5272 default:
5273 netdev->max_mtu = RTL8153_MAX_MTU;
5274 break;
5275 }
5276
hayeswangac718b62013-05-02 16:01:25 +00005277 tp->mii.dev = netdev;
5278 tp->mii.mdio_read = read_mii_word;
5279 tp->mii.mdio_write = write_mii_word;
5280 tp->mii.phy_id_mask = 0x3f;
5281 tp->mii.reg_num_mask = 0x1f;
5282 tp->mii.phy_id = R8152_PHY_ID;
hayeswangac718b62013-05-02 16:01:25 +00005283
hayeswangaa7e26b2016-06-13 17:49:38 +08005284 tp->autoneg = AUTONEG_ENABLE;
5285 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5286 tp->duplex = DUPLEX_FULL;
5287
hayeswang9a4be1b2014-02-18 21:49:07 +08005288 intf->needs_remote_wakeup = 1;
5289
hayeswangc81229c2014-01-02 11:22:42 +08005290 tp->rtl_ops.init(tp);
hayeswanga028a9e2016-06-13 17:49:36 +08005291 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
hayeswangac718b62013-05-02 16:01:25 +00005292 set_ethernet_addr(tp);
5293
hayeswangac718b62013-05-02 16:01:25 +00005294 usb_set_intfdata(intf, tp);
hayeswangd823ab62015-01-12 12:06:23 +08005295 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
hayeswangac718b62013-05-02 16:01:25 +00005296
hayeswangebc2ec482013-08-14 20:54:38 +08005297 ret = register_netdev(netdev);
5298 if (ret != 0) {
Hayes Wang4a8deae2014-01-07 11:18:22 +08005299 netif_err(tp, probe, netdev, "couldn't register the device\n");
hayeswangebc2ec482013-08-14 20:54:38 +08005300 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00005301 }
5302
hayeswang7daed8d2015-07-24 13:54:24 +08005303 if (!rtl_can_wakeup(tp))
5304 __rtl_set_wol(tp, 0);
5305
hayeswang21ff2e82014-02-18 21:49:06 +08005306 tp->saved_wolopts = __rtl_get_wol(tp);
5307 if (tp->saved_wolopts)
5308 device_set_wakeup_enable(&udev->dev, true);
5309 else
5310 device_set_wakeup_enable(&udev->dev, false);
5311
Hayes Wang4a8deae2014-01-07 11:18:22 +08005312 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
hayeswangac718b62013-05-02 16:01:25 +00005313
5314 return 0;
5315
hayeswangac718b62013-05-02 16:01:25 +00005316out1:
hayeswangd823ab62015-01-12 12:06:23 +08005317 netif_napi_del(&tp->napi);
hayeswangebc2ec482013-08-14 20:54:38 +08005318 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00005319out:
5320 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08005321 return ret;
hayeswangac718b62013-05-02 16:01:25 +00005322}
5323
hayeswangac718b62013-05-02 16:01:25 +00005324static void rtl8152_disconnect(struct usb_interface *intf)
5325{
5326 struct r8152 *tp = usb_get_intfdata(intf);
5327
5328 usb_set_intfdata(intf, NULL);
5329 if (tp) {
hayeswangf561de32014-09-30 16:48:01 +08005330 struct usb_device *udev = tp->udev;
5331
5332 if (udev->state == USB_STATE_NOTATTACHED)
5333 set_bit(RTL8152_UNPLUG, &tp->flags);
5334
hayeswangd823ab62015-01-12 12:06:23 +08005335 netif_napi_del(&tp->napi);
hayeswangac718b62013-05-02 16:01:25 +00005336 unregister_netdev(tp->netdev);
hayeswanga028a9e2016-06-13 17:49:36 +08005337 cancel_delayed_work_sync(&tp->hw_phy_work);
hayeswangc81229c2014-01-02 11:22:42 +08005338 tp->rtl_ops.unload(tp);
hayeswangac718b62013-05-02 16:01:25 +00005339 free_netdev(tp->netdev);
5340 }
5341}
5342
hayeswangd9a28c52014-12-04 10:43:11 +08005343#define REALTEK_USB_DEVICE(vend, prod) \
5344 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5345 USB_DEVICE_ID_MATCH_INT_CLASS, \
5346 .idVendor = (vend), \
5347 .idProduct = (prod), \
5348 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5349}, \
5350{ \
5351 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5352 USB_DEVICE_ID_MATCH_DEVICE, \
5353 .idVendor = (vend), \
5354 .idProduct = (prod), \
5355 .bInterfaceClass = USB_CLASS_COMM, \
5356 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5357 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5358
hayeswangac718b62013-05-02 16:01:25 +00005359/* table of devices that work with this driver */
Arvind Yadav9b4355f2017-08-08 21:28:05 +05305360static const struct usb_device_id rtl8152_table[] = {
hayeswangc27b32c2017-06-15 14:44:02 +08005361 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
hayeswangd9a28c52014-12-04 10:43:11 +08005362 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5363 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
René Rebed5b07cc2017-03-28 07:56:51 +02005364 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5365 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
hayeswangd9a28c52014-12-04 10:43:11 +08005366 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
Vasily Titskiy1006da12015-05-06 10:31:21 -04005367 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
hayeswangd248caf2016-10-18 11:41:48 +08005368 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5369 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5370 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5371 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5372 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
Grant Grundler90841042017-09-28 11:35:00 -07005373 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
Zheng Liud065c3c12015-07-07 13:54:12 -07005374 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
Ran Wang9d11b062017-10-23 18:10:23 +08005375 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
hayeswangac718b62013-05-02 16:01:25 +00005376 {}
5377};
5378
5379MODULE_DEVICE_TABLE(usb, rtl8152_table);
5380
5381static struct usb_driver rtl8152_driver = {
5382 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08005383 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00005384 .probe = rtl8152_probe,
5385 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00005386 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08005387 .resume = rtl8152_resume,
hayeswang7ec25412016-01-04 14:38:46 +08005388 .reset_resume = rtl8152_reset_resume,
hayeswange5011392015-07-29 20:39:08 +08005389 .pre_reset = rtl8152_pre_reset,
5390 .post_reset = rtl8152_post_reset,
hayeswang9a4be1b2014-02-18 21:49:07 +08005391 .supports_autosuspend = 1,
hayeswanga6347822014-02-18 21:49:10 +08005392 .disable_hub_initiated_lpm = 1,
hayeswangac718b62013-05-02 16:01:25 +00005393};
5394
Sachin Kamatb4236daa2013-05-16 17:48:08 +00005395module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00005396
5397MODULE_AUTHOR(DRIVER_AUTHOR);
5398MODULE_DESCRIPTION(DRIVER_DESC);
5399MODULE_LICENSE("GPL");
Grant Grundlerc961e872016-07-14 11:27:16 -07005400MODULE_VERSION(DRIVER_VERSION);