Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2 | /* |
hayeswang | c7de7de | 2014-01-15 10:42:16 +0800 | [diff] [blame] | 3 | * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved. |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6 | #include <linux/signal.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/module.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 9 | #include <linux/netdevice.h> |
| 10 | #include <linux/etherdevice.h> |
| 11 | #include <linux/mii.h> |
| 12 | #include <linux/ethtool.h> |
| 13 | #include <linux/usb.h> |
| 14 | #include <linux/crc32.h> |
| 15 | #include <linux/if_vlan.h> |
| 16 | #include <linux/uaccess.h> |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 17 | #include <linux/list.h> |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 18 | #include <linux/ip.h> |
| 19 | #include <linux/ipv6.h> |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 20 | #include <net/ip6_checksum.h> |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 21 | #include <uapi/linux/mdio.h> |
| 22 | #include <linux/mdio.h> |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 23 | #include <linux/usb/cdc.h> |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 24 | #include <linux/suspend.h> |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 25 | #include <linux/atomic.h> |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 26 | #include <linux/acpi.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 27 | |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 28 | /* Information for net-next */ |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 29 | #define NETNEXT_VERSION "10" |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 30 | |
| 31 | /* Information for net */ |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 32 | #define NET_VERSION "10" |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 33 | |
| 34 | #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 35 | #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" |
hayeswang | 44d942a | 2014-01-15 10:42:14 +0800 | [diff] [blame] | 36 | #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 37 | #define MODULENAME "r8152" |
| 38 | |
| 39 | #define R8152_PHY_ID 32 |
| 40 | |
| 41 | #define PLA_IDR 0xc000 |
| 42 | #define PLA_RCR 0xc010 |
| 43 | #define PLA_RMS 0xc016 |
| 44 | #define PLA_RXFIFO_CTRL0 0xc0a0 |
| 45 | #define PLA_RXFIFO_CTRL1 0xc0a4 |
| 46 | #define PLA_RXFIFO_CTRL2 0xc0a8 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 47 | #define PLA_DMY_REG0 0xc0b0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 48 | #define PLA_FMC 0xc0b4 |
| 49 | #define PLA_CFG_WOL 0xc0b6 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 50 | #define PLA_TEREDO_CFG 0xc0bc |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 51 | #define PLA_TEREDO_WAKE_BASE 0xc0c4 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 52 | #define PLA_MAR 0xcd00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 53 | #define PLA_BACKUP 0xd000 |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 54 | #define PLA_BDC_CR 0xd1a0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 55 | #define PLA_TEREDO_TIMER 0xd2cc |
| 56 | #define PLA_REALWOW_TIMER 0xd2e8 |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 57 | #define PLA_SUSPEND_FLAG 0xd38a |
| 58 | #define PLA_INDICATE_FALG 0xd38c |
| 59 | #define PLA_EXTRA_STATUS 0xd398 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 60 | #define PLA_EFUSE_DATA 0xdd00 |
| 61 | #define PLA_EFUSE_CMD 0xdd02 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 62 | #define PLA_LEDSEL 0xdd90 |
| 63 | #define PLA_LED_FEATURE 0xdd92 |
| 64 | #define PLA_PHYAR 0xde00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 65 | #define PLA_BOOT_CTRL 0xe004 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 66 | #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 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 70 | #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 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 74 | #define PLA_TCR0 0xe610 |
| 75 | #define PLA_TCR1 0xe612 |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 76 | #define PLA_MTPS 0xe615 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 77 | #define PLA_TXFIFO_CTRL 0xe618 |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 78 | #define PLA_RSTTALLY 0xe800 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 79 | #define PLA_CR 0xe813 |
| 80 | #define PLA_CRWECR 0xe81c |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 81 | #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ |
| 82 | #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 83 | #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 |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 90 | #define PLA_TALLYCNT 0xe890 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 91 | #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 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 102 | #define PLA_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 103 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 104 | #define USB_USB2PHY 0xb41e |
| 105 | #define USB_SSPHYLINK2 0xb428 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 106 | #define USB_U2P3_CTRL 0xb460 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 107 | #define USB_CSR_DUMMY1 0xb464 |
| 108 | #define USB_CSR_DUMMY2 0xb466 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 109 | #define USB_DEV_STAT 0xb808 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 110 | #define USB_CONNECT_TIMER 0xcbf8 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 111 | #define USB_MSC_TIMER 0xcbfc |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 112 | #define USB_BURST_SIZE 0xcfc0 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 113 | #define USB_LPM_CONFIG 0xcfd8 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 114 | #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 |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 119 | #define USB_RX_EARLY_TIMEOUT 0xd42c |
| 120 | #define USB_RX_EARLY_SIZE 0xd42e |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 121 | #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */ |
| 122 | #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 123 | #define USB_TX_DMA 0xd434 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 124 | #define USB_UPT_RXDMA_OWN 0xd437 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 125 | #define USB_TOLERANCE 0xd490 |
| 126 | #define USB_LPM_CTRL 0xd41a |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 127 | #define USB_BMU_RESET 0xd4b0 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 128 | #define USB_U1U2_TIMER 0xd4da |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 129 | #define USB_UPS_CTRL 0xd800 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 130 | #define USB_POWER_CUT 0xd80a |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 131 | #define USB_MISC_0 0xd81a |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 132 | #define USB_MISC_1 0xd81f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 133 | #define USB_AFE_CTRL2 0xd824 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 134 | #define USB_UPS_CFG 0xd842 |
| 135 | #define USB_UPS_FLAGS 0xd848 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 136 | #define USB_WDT11_CTRL 0xe43c |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 137 | #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 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 146 | #define USB_BP_EN 0xfc38 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 147 | #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 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 156 | |
| 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 |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 162 | #define OCP_BASE_MII 0xa400 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 163 | #define OCP_EEE_AR 0xa41a |
| 164 | #define OCP_EEE_DATA 0xa41c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 165 | #define OCP_PHY_STATUS 0xa420 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 166 | #define OCP_NCTL_CFG 0xa42c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 167 | #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 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 172 | #define OCP_EEE_ABLE 0xa5c4 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 173 | #define OCP_EEE_ADV 0xa5d0 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 174 | #define OCP_EEE_LPABLE 0xa5d2 |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 175 | #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 176 | #define OCP_PHY_PATCH_STAT 0xb800 |
| 177 | #define OCP_PHY_PATCH_CMD 0xb820 |
| 178 | #define OCP_ADC_IOFFSET 0xbcfc |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 179 | #define OCP_ADC_CFG 0xbc06 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 180 | #define OCP_SYSCLK_CFG 0xc416 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 181 | |
| 182 | /* SRAM Register */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 183 | #define SRAM_GREEN_CFG 0x8011 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 184 | #define SRAM_LPF_CFG 0x8012 |
| 185 | #define SRAM_10M_AMP1 0x8080 |
| 186 | #define SRAM_10M_AMP2 0x8082 |
| 187 | #define SRAM_IMPEDANCE 0x8084 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 188 | |
| 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 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 204 | #define RXFIFO_THR2_NORMAL 0x00a0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 205 | |
| 206 | /* PLA_RXFIFO_CTRL2 */ |
| 207 | #define RXFIFO_THR3_FULL 0x00000078 |
| 208 | #define RXFIFO_THR3_HIGH 0x00000048 |
| 209 | #define RXFIFO_THR3_OOB 0x0000005a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 210 | #define RXFIFO_THR3_NORMAL 0x0110 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 211 | |
| 212 | /* PLA_TXFIFO_CTRL */ |
| 213 | #define TXFIFO_THR_NORMAL 0x00400008 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 214 | #define TXFIFO_THR_NORMAL2 0x01000008 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 215 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 216 | /* PLA_DMY_REG0 */ |
| 217 | #define ECM_ALDPS 0x0002 |
| 218 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 219 | /* PLA_FMC */ |
| 220 | #define FMC_FCR_MCU_EN 0x0001 |
| 221 | |
| 222 | /* PLA_EEEP_CR */ |
| 223 | #define EEEP_CR_EEEP_TX 0x0002 |
| 224 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 225 | /* PLA_WDT6_CTRL */ |
| 226 | #define WDT6_SET_MODE 0x0010 |
| 227 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 228 | /* PLA_TCR0 */ |
| 229 | #define TCR0_TX_EMPTY 0x0800 |
| 230 | #define TCR0_AUTO_FIFO 0x0080 |
| 231 | |
| 232 | /* PLA_TCR1 */ |
| 233 | #define VERSION_MASK 0x7cf0 |
| 234 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 235 | /* PLA_MTPS */ |
| 236 | #define MTPS_JUMBO (12 * 1024 / 64) |
| 237 | #define MTPS_DEFAULT (6 * 1024 / 64) |
| 238 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 239 | /* PLA_RSTTALLY */ |
| 240 | #define TALLY_RESET 0x0001 |
| 241 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 242 | /* 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 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 272 | /* 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 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 278 | /* PLA_BDC_CR */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 279 | #define ALDPS_PROXY_MODE 0x0001 |
| 280 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 281 | /* PLA_EFUSE_CMD */ |
| 282 | #define EFUSE_READ_CMD BIT(15) |
| 283 | #define EFUSE_DATA_BIT16 BIT(7) |
| 284 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 285 | /* PLA_CONFIG34 */ |
| 286 | #define LINK_ON_WAKE_EN 0x0010 |
| 287 | #define LINK_OFF_WAKE_EN 0x0008 |
| 288 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 289 | /* PLA_CONFIG5 */ |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 290 | #define BWF_EN 0x0040 |
| 291 | #define MWF_EN 0x0020 |
| 292 | #define UWF_EN 0x0010 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 293 | #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 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 306 | #define ALDPS_SPDWN_RATIO 0x0f87 |
| 307 | |
| 308 | /* PLA_MAC_PWR_CTRL2 */ |
| 309 | #define EEE_SPDWN_RATIO 0x8007 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 310 | #define MAC_CLK_SPDWN_EN BIT(15) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 311 | |
| 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 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 326 | |
| 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 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 340 | /* PLA_BOOT_CTRL */ |
| 341 | #define AUTOLOAD_DONE 0x0002 |
| 342 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 343 | /* PLA_SUSPEND_FLAG */ |
| 344 | #define LINK_CHG_EVENT BIT(0) |
| 345 | |
| 346 | /* PLA_INDICATE_FALG */ |
| 347 | #define UPCOMING_RUNTIME_D3 BIT(0) |
| 348 | |
| 349 | /* PLA_EXTRA_STATUS */ |
| 350 | #define LINK_CHANGE_FLAG BIT(8) |
| 351 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 352 | /* USB_USB2PHY */ |
| 353 | #define USB2PHY_SUSPEND 0x0001 |
| 354 | #define USB2PHY_L1 0x0002 |
| 355 | |
| 356 | /* USB_SSPHYLINK2 */ |
| 357 | #define pwd_dn_scale_mask 0x3ffe |
| 358 | #define pwd_dn_scale(x) ((x) << 1) |
| 359 | |
| 360 | /* USB_CSR_DUMMY1 */ |
| 361 | #define DYNAMIC_BURST 0x0001 |
| 362 | |
| 363 | /* USB_CSR_DUMMY2 */ |
| 364 | #define EP4_FULL_FC 0x0001 |
| 365 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 366 | /* USB_DEV_STAT */ |
| 367 | #define STAT_SPEED_MASK 0x0006 |
| 368 | #define STAT_SPEED_HIGH 0x0000 |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 369 | #define STAT_SPEED_FULL 0x0002 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 370 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 371 | /* USB_LPM_CONFIG */ |
| 372 | #define LPM_U1U2_EN BIT(0) |
| 373 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 374 | /* USB_TX_AGG */ |
| 375 | #define TX_AGG_MAX_THRESHOLD 0x03 |
| 376 | |
| 377 | /* USB_RX_BUF_TH */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 378 | #define RX_THR_SUPPER 0x0c350180 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 379 | #define RX_THR_HIGH 0x7a120180 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 380 | #define RX_THR_SLOW 0xffff0180 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 381 | #define RX_THR_B 0x00010001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 382 | |
| 383 | /* USB_TX_DMA */ |
| 384 | #define TEST_MODE_DISABLE 0x00000001 |
| 385 | #define TX_SIZE_ADJUST1 0x00000100 |
| 386 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 387 | /* USB_BMU_RESET */ |
| 388 | #define BMU_RESET_EP_IN 0x01 |
| 389 | #define BMU_RESET_EP_OUT 0x02 |
| 390 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 391 | /* USB_UPT_RXDMA_OWN */ |
| 392 | #define OWN_UPDATE BIT(0) |
| 393 | #define OWN_CLEAR BIT(1) |
| 394 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 395 | /* USB_UPS_CTRL */ |
| 396 | #define POWER_CUT 0x0100 |
| 397 | |
| 398 | /* USB_PM_CTRL_STATUS */ |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 399 | #define RESUME_INDICATE 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 400 | |
| 401 | /* USB_USB_CTRL */ |
| 402 | #define RX_AGG_DISABLE 0x0010 |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 403 | #define RX_ZERO_EN 0x0080 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 404 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 405 | /* USB_U2P3_CTRL */ |
| 406 | #define U2P3_ENABLE 0x0001 |
| 407 | |
| 408 | /* USB_POWER_CUT */ |
| 409 | #define PWR_EN 0x0001 |
| 410 | #define PHASE2_EN 0x0008 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 411 | #define UPS_EN BIT(4) |
| 412 | #define USP_PREWAKE BIT(5) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 413 | |
| 414 | /* USB_MISC_0 */ |
| 415 | #define PCUT_STATUS 0x0001 |
| 416 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 417 | /* USB_RX_EARLY_TIMEOUT */ |
| 418 | #define COALESCE_SUPER 85000U |
| 419 | #define COALESCE_HIGH 250000U |
| 420 | #define COALESCE_SLOW 524280U |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 421 | |
| 422 | /* USB_WDT11_CTRL */ |
| 423 | #define TIMER11_EN 0x0001 |
| 424 | |
| 425 | /* USB_LPM_CTRL */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 426 | /* bit 4 ~ 5: fifo empty boundary */ |
| 427 | #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ |
| 428 | /* bit 2 ~ 3: LMP timer */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 429 | #define LPM_TIMER_MASK 0x0c |
| 430 | #define LPM_TIMER_500MS 0x04 /* 500 ms */ |
| 431 | #define LPM_TIMER_500US 0x0c /* 500 us */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 432 | #define ROK_EXIT_LPM 0x02 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 433 | |
| 434 | /* USB_AFE_CTRL2 */ |
| 435 | #define SEN_VAL_MASK 0xf800 |
| 436 | #define SEN_VAL_NORMAL 0xa000 |
| 437 | #define SEL_RXIDLE 0x0100 |
| 438 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 439 | /* USB_UPS_CFG */ |
| 440 | #define SAW_CNT_1MS_MASK 0x0fff |
| 441 | |
| 442 | /* USB_UPS_FLAGS */ |
| 443 | #define UPS_FLAGS_R_TUNE BIT(0) |
| 444 | #define UPS_FLAGS_EN_10M_CKDIV BIT(1) |
| 445 | #define UPS_FLAGS_250M_CKDIV BIT(2) |
| 446 | #define UPS_FLAGS_EN_ALDPS BIT(3) |
| 447 | #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) |
| 448 | #define UPS_FLAGS_SPEED_MASK (0xf << 16) |
| 449 | #define ups_flags_speed(x) ((x) << 16) |
| 450 | #define UPS_FLAGS_EN_EEE BIT(20) |
| 451 | #define UPS_FLAGS_EN_500M_EEE BIT(21) |
| 452 | #define UPS_FLAGS_EN_EEE_CKDIV BIT(22) |
| 453 | #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24) |
| 454 | #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25) |
| 455 | #define UPS_FLAGS_EN_GREEN BIT(26) |
| 456 | #define UPS_FLAGS_EN_FLOW_CTR BIT(27) |
| 457 | |
| 458 | enum spd_duplex { |
| 459 | NWAY_10M_HALF = 1, |
| 460 | NWAY_10M_FULL, |
| 461 | NWAY_100M_HALF, |
| 462 | NWAY_100M_FULL, |
| 463 | NWAY_1000M_FULL, |
| 464 | FORCE_10M_HALF, |
| 465 | FORCE_10M_FULL, |
| 466 | FORCE_100M_HALF, |
| 467 | FORCE_100M_FULL, |
| 468 | }; |
| 469 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 470 | /* OCP_ALDPS_CONFIG */ |
| 471 | #define ENPWRSAVE 0x8000 |
| 472 | #define ENPDNPS 0x0200 |
| 473 | #define LINKENA 0x0100 |
| 474 | #define DIS_SDSAVE 0x0010 |
| 475 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 476 | /* OCP_PHY_STATUS */ |
| 477 | #define PHY_STAT_MASK 0x0007 |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 478 | #define PHY_STAT_EXT_INIT 2 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 479 | #define PHY_STAT_LAN_ON 3 |
| 480 | #define PHY_STAT_PWRDN 5 |
| 481 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 482 | /* OCP_NCTL_CFG */ |
| 483 | #define PGA_RETURN_EN BIT(1) |
| 484 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 485 | /* OCP_POWER_CFG */ |
| 486 | #define EEE_CLKDIV_EN 0x8000 |
| 487 | #define EN_ALDPS 0x0004 |
| 488 | #define EN_10M_PLLOFF 0x0001 |
| 489 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 490 | /* OCP_EEE_CONFIG1 */ |
| 491 | #define RG_TXLPI_MSK_HFDUP 0x8000 |
| 492 | #define RG_MATCLR_EN 0x4000 |
| 493 | #define EEE_10_CAP 0x2000 |
| 494 | #define EEE_NWAY_EN 0x1000 |
| 495 | #define TX_QUIET_EN 0x0200 |
| 496 | #define RX_QUIET_EN 0x0100 |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 497 | #define sd_rise_time_mask 0x0070 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 498 | #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 499 | #define RG_RXLPI_MSK_HFDUP 0x0008 |
| 500 | #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ |
| 501 | |
| 502 | /* OCP_EEE_CONFIG2 */ |
| 503 | #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ |
| 504 | #define RG_DACQUIET_EN 0x0400 |
| 505 | #define RG_LDVQUIET_EN 0x0200 |
| 506 | #define RG_CKRSEL 0x0020 |
| 507 | #define RG_EEEPRG_EN 0x0010 |
| 508 | |
| 509 | /* OCP_EEE_CONFIG3 */ |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 510 | #define fast_snr_mask 0xff80 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 511 | #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 512 | #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ |
| 513 | #define MSK_PH 0x0006 /* bit 0 ~ 3 */ |
| 514 | |
| 515 | /* OCP_EEE_AR */ |
| 516 | /* bit[15:14] function */ |
| 517 | #define FUN_ADDR 0x0000 |
| 518 | #define FUN_DATA 0x4000 |
| 519 | /* bit[4:0] device addr */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 520 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 521 | /* OCP_EEE_CFG */ |
| 522 | #define CTAP_SHORT_EN 0x0040 |
| 523 | #define EEE10_EN 0x0010 |
| 524 | |
| 525 | /* OCP_DOWN_SPEED */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 526 | #define EN_EEE_CMODE BIT(14) |
| 527 | #define EN_EEE_1000 BIT(13) |
| 528 | #define EN_EEE_100 BIT(12) |
| 529 | #define EN_10M_CLKDIV BIT(11) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 530 | #define EN_10M_BGOFF 0x0080 |
| 531 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 532 | /* OCP_PHY_STATE */ |
| 533 | #define TXDIS_STATE 0x01 |
| 534 | #define ABD_STATE 0x02 |
| 535 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 536 | /* OCP_PHY_PATCH_STAT */ |
| 537 | #define PATCH_READY BIT(6) |
| 538 | |
| 539 | /* OCP_PHY_PATCH_CMD */ |
| 540 | #define PATCH_REQUEST BIT(4) |
| 541 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 542 | /* OCP_ADC_CFG */ |
| 543 | #define CKADSEL_L 0x0100 |
| 544 | #define ADC_EN 0x0080 |
| 545 | #define EN_EMI_L 0x0040 |
| 546 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 547 | /* OCP_SYSCLK_CFG */ |
| 548 | #define clk_div_expo(x) (min(x, 5) << 8) |
| 549 | |
| 550 | /* SRAM_GREEN_CFG */ |
| 551 | #define GREEN_ETH_EN BIT(15) |
| 552 | #define R_TUNE_EN BIT(11) |
| 553 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 554 | /* SRAM_LPF_CFG */ |
| 555 | #define LPF_AUTO_TUNE 0x8000 |
| 556 | |
| 557 | /* SRAM_10M_AMP1 */ |
| 558 | #define GDAC_IB_UPALL 0x0008 |
| 559 | |
| 560 | /* SRAM_10M_AMP2 */ |
| 561 | #define AMP_DN 0x0200 |
| 562 | |
| 563 | /* SRAM_IMPEDANCE */ |
| 564 | #define RX_DRIVING_MASK 0x6000 |
| 565 | |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 566 | /* MAC PASSTHRU */ |
| 567 | #define AD_MASK 0xfee0 |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 568 | #define BND_MASK 0x0004 |
David Chen | 8e29d23 | 2019-02-16 17:16:42 +0800 | [diff] [blame] | 569 | #define BD_MASK 0x0001 |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 570 | #define EFUSE 0xcfdb |
| 571 | #define PASS_THRU_MASK 0x1 |
| 572 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 573 | enum rtl_register_content { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 574 | _1000bps = 0x10, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 575 | _100bps = 0x08, |
| 576 | _10bps = 0x04, |
| 577 | LINK_STATUS = 0x02, |
| 578 | FULL_DUP = 0x01, |
| 579 | }; |
| 580 | |
hayeswang | 1764bcd | 2014-08-28 10:24:18 +0800 | [diff] [blame] | 581 | #define RTL8152_MAX_TX 4 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 582 | #define RTL8152_MAX_RX 10 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 583 | #define INTBUFSIZE 2 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 584 | #define TX_ALIGN 4 |
| 585 | #define RX_ALIGN 8 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 586 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 587 | #define RTL8152_RX_MAX_PENDING 4096 |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 588 | #define RTL8152_RXFG_HEADSZ 256 |
| 589 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 590 | #define INTR_LINK 0x0004 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 591 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 592 | #define RTL8152_REQT_READ 0xc0 |
| 593 | #define RTL8152_REQT_WRITE 0x40 |
| 594 | #define RTL8152_REQ_GET_REGS 0x05 |
| 595 | #define RTL8152_REQ_SET_REGS 0x05 |
| 596 | |
| 597 | #define BYTE_EN_DWORD 0xff |
| 598 | #define BYTE_EN_WORD 0x33 |
| 599 | #define BYTE_EN_BYTE 0x11 |
| 600 | #define BYTE_EN_SIX_BYTES 0x3f |
| 601 | #define BYTE_EN_START_MASK 0x0f |
| 602 | #define BYTE_EN_END_MASK 0xf0 |
| 603 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 604 | #define RTL8153_MAX_PACKET 9216 /* 9K */ |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 605 | #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \ |
| 606 | ETH_FCS_LEN) |
| 607 | #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 608 | #define RTL8153_RMS RTL8153_MAX_PACKET |
hayeswang | b812540 | 2014-07-03 11:55:48 +0800 | [diff] [blame] | 609 | #define RTL8152_TX_TIMEOUT (5 * HZ) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 610 | #define RTL8152_NAPI_WEIGHT 64 |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 611 | #define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \ |
hayeswang | b20cb60 | 2017-03-20 16:13:45 +0800 | [diff] [blame] | 612 | sizeof(struct rx_desc) + RX_ALIGN) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 613 | |
| 614 | /* rtl8152 flags */ |
| 615 | enum rtl8152_flags { |
| 616 | RTL8152_UNPLUG = 0, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 617 | RTL8152_SET_RX_MODE, |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 618 | WORK_ENABLE, |
| 619 | RTL8152_LINK_CHG, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 620 | SELECTIVE_SUSPEND, |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 621 | PHY_RESET, |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 622 | SCHEDULE_TASKLET, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 623 | GREEN_ETHERNET, |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 624 | DELL_TB_RX_AGG_BUG, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | /* Define these values to match your device */ |
| 628 | #define VENDOR_ID_REALTEK 0x0bda |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 629 | #define VENDOR_ID_MICROSOFT 0x045e |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 630 | #define VENDOR_ID_SAMSUNG 0x04e8 |
Christian Hesse | 347eec3 | 2015-03-31 14:10:07 +0200 | [diff] [blame] | 631 | #define VENDOR_ID_LENOVO 0x17ef |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 632 | #define VENDOR_ID_LINKSYS 0x13b1 |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 633 | #define VENDOR_ID_NVIDIA 0x0955 |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 634 | #define VENDOR_ID_TPLINK 0x2357 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 635 | |
| 636 | #define MCU_TYPE_PLA 0x0100 |
| 637 | #define MCU_TYPE_USB 0x0000 |
| 638 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 639 | struct tally_counter { |
| 640 | __le64 tx_packets; |
| 641 | __le64 rx_packets; |
| 642 | __le64 tx_errors; |
| 643 | __le32 rx_errors; |
| 644 | __le16 rx_missed; |
| 645 | __le16 align_errors; |
| 646 | __le32 tx_one_collision; |
| 647 | __le32 tx_multi_collision; |
| 648 | __le64 rx_unicast; |
| 649 | __le64 rx_broadcast; |
| 650 | __le32 rx_multicast; |
| 651 | __le16 tx_aborted; |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 652 | __le16 tx_underrun; |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 653 | }; |
| 654 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 655 | struct rx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 656 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 657 | #define RX_LEN_MASK 0x7fff |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 658 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 659 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 660 | #define RD_UDP_CS BIT(23) |
| 661 | #define RD_TCP_CS BIT(22) |
| 662 | #define RD_IPV6_CS BIT(20) |
| 663 | #define RD_IPV4_CS BIT(19) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 664 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 665 | __le32 opts3; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 666 | #define IPF BIT(23) /* IP checksum fail */ |
| 667 | #define UDPF BIT(22) /* UDP checksum fail */ |
| 668 | #define TCPF BIT(21) /* TCP checksum fail */ |
| 669 | #define RX_VLAN_TAG BIT(16) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 670 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 671 | __le32 opts4; |
| 672 | __le32 opts5; |
| 673 | __le32 opts6; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
| 676 | struct tx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 677 | __le32 opts1; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 678 | #define TX_FS BIT(31) /* First segment of a packet */ |
| 679 | #define TX_LS BIT(30) /* Final segment of a packet */ |
| 680 | #define GTSENDV4 BIT(28) |
| 681 | #define GTSENDV6 BIT(27) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 682 | #define GTTCPHO_SHIFT 18 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 683 | #define GTTCPHO_MAX 0x7fU |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 684 | #define TX_LEN_MAX 0x3ffffU |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 685 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 686 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 687 | #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ |
| 688 | #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ |
| 689 | #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ |
| 690 | #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 691 | #define MSS_SHIFT 17 |
| 692 | #define MSS_MAX 0x7ffU |
| 693 | #define TCPHO_SHIFT 17 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 694 | #define TCPHO_MAX 0x7ffU |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 695 | #define TX_VLAN_TAG BIT(16) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 696 | }; |
| 697 | |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 698 | struct r8152; |
| 699 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 700 | struct rx_agg { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 701 | struct list_head list, info_list; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 702 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 703 | struct r8152 *context; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 704 | struct page *page; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 705 | void *buffer; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | struct tx_agg { |
| 709 | struct list_head list; |
| 710 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 711 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 712 | void *buffer; |
| 713 | void *head; |
| 714 | u32 skb_num; |
| 715 | u32 skb_len; |
| 716 | }; |
| 717 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 718 | struct r8152 { |
| 719 | unsigned long flags; |
| 720 | struct usb_device *udev; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 721 | struct napi_struct napi; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 722 | struct usb_interface *intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 723 | struct net_device *netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 724 | struct urb *intr_urb; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 725 | struct tx_agg tx_info[RTL8152_MAX_TX]; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 726 | struct list_head rx_info, rx_used; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 727 | struct list_head rx_done, tx_free; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 728 | struct sk_buff_head tx_queue, rx_queue; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 729 | spinlock_t rx_lock, tx_lock; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 730 | struct delayed_work schedule, hw_phy_work; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 731 | struct mii_if_info mii; |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 732 | struct mutex control; /* use for hw setting */ |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 733 | #ifdef CONFIG_PM_SLEEP |
| 734 | struct notifier_block pm_notifier; |
| 735 | #endif |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 736 | struct tasklet_struct tx_tl; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 737 | |
| 738 | struct rtl_ops { |
| 739 | void (*init)(struct r8152 *); |
| 740 | int (*enable)(struct r8152 *); |
| 741 | void (*disable)(struct r8152 *); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 742 | void (*up)(struct r8152 *); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 743 | void (*down)(struct r8152 *); |
| 744 | void (*unload)(struct r8152 *); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 745 | int (*eee_get)(struct r8152 *, struct ethtool_eee *); |
| 746 | int (*eee_set)(struct r8152 *, struct ethtool_eee *); |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 747 | bool (*in_nway)(struct r8152 *); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 748 | void (*hw_phy_cfg)(struct r8152 *); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 749 | void (*autosuspend_en)(struct r8152 *tp, bool enable); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 750 | } rtl_ops; |
| 751 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 752 | atomic_t rx_count; |
| 753 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 754 | bool eee_en; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 755 | int intr_interval; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 756 | u32 saved_wolopts; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 757 | u32 msg_enable; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 758 | u32 tx_qlen; |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 759 | u32 coalesce; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 760 | u32 rx_buf_sz; |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 761 | u32 rx_copybreak; |
| 762 | u32 rx_pending; |
| 763 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 764 | u16 ocp_base; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 765 | u16 speed; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 766 | u16 eee_adv; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 767 | u8 *intr_buff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 768 | u8 version; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 769 | u8 duplex; |
| 770 | u8 autoneg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | enum rtl_version { |
| 774 | RTL_VER_UNKNOWN = 0, |
| 775 | RTL_VER_01, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 776 | RTL_VER_02, |
| 777 | RTL_VER_03, |
| 778 | RTL_VER_04, |
| 779 | RTL_VER_05, |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 780 | RTL_VER_06, |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 781 | RTL_VER_07, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 782 | RTL_VER_08, |
| 783 | RTL_VER_09, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 784 | RTL_VER_MAX |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 787 | enum tx_csum_stat { |
| 788 | TX_CSUM_SUCCESS = 0, |
| 789 | TX_CSUM_TSO, |
| 790 | TX_CSUM_NONE |
| 791 | }; |
| 792 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 793 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). |
| 794 | * The RTL chips use a 64 element hash table based on the Ethernet CRC. |
| 795 | */ |
| 796 | static const int multicast_filter_limit = 32; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 797 | static unsigned int agg_buf_sz = 16384; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 798 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 799 | #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \ |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 800 | VLAN_ETH_HLEN - ETH_FCS_LEN) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 801 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 802 | static |
| 803 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 804 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 805 | int ret; |
| 806 | void *tmp; |
| 807 | |
| 808 | tmp = kmalloc(size, GFP_KERNEL); |
| 809 | if (!tmp) |
| 810 | return -ENOMEM; |
| 811 | |
| 812 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 813 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 814 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 815 | |
| 816 | memcpy(data, tmp, size); |
| 817 | kfree(tmp); |
| 818 | |
| 819 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static |
| 823 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 824 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 825 | int ret; |
| 826 | void *tmp; |
| 827 | |
Benoit Taine | c4438f0 | 2014-05-26 17:21:23 +0200 | [diff] [blame] | 828 | tmp = kmemdup(data, size, GFP_KERNEL); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 829 | if (!tmp) |
| 830 | return -ENOMEM; |
| 831 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 832 | ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 833 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
| 834 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 835 | |
| 836 | kfree(tmp); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 837 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 838 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 841 | static void rtl_set_unplug(struct r8152 *tp) |
| 842 | { |
| 843 | if (tp->udev->state == USB_STATE_NOTATTACHED) { |
| 844 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 845 | smp_mb__after_atomic(); |
| 846 | } |
| 847 | } |
| 848 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 849 | static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 850 | void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 851 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 852 | u16 limit = 64; |
| 853 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 854 | |
| 855 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 856 | return -ENODEV; |
| 857 | |
| 858 | /* both size and indix must be 4 bytes align */ |
| 859 | if ((size & 3) || !size || (index & 3) || !data) |
| 860 | return -EPERM; |
| 861 | |
| 862 | if ((u32)index + (u32)size > 0xffff) |
| 863 | return -EPERM; |
| 864 | |
| 865 | while (size) { |
| 866 | if (size > limit) { |
| 867 | ret = get_registers(tp, index, type, limit, data); |
| 868 | if (ret < 0) |
| 869 | break; |
| 870 | |
| 871 | index += limit; |
| 872 | data += limit; |
| 873 | size -= limit; |
| 874 | } else { |
| 875 | ret = get_registers(tp, index, type, size, data); |
| 876 | if (ret < 0) |
| 877 | break; |
| 878 | |
| 879 | index += size; |
| 880 | data += size; |
| 881 | size = 0; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 886 | if (ret == -ENODEV) |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 887 | rtl_set_unplug(tp); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 888 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 893 | u16 size, void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 894 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 895 | int ret; |
| 896 | u16 byteen_start, byteen_end, byen; |
| 897 | u16 limit = 512; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 898 | |
| 899 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 900 | return -ENODEV; |
| 901 | |
| 902 | /* both size and indix must be 4 bytes align */ |
| 903 | if ((size & 3) || !size || (index & 3) || !data) |
| 904 | return -EPERM; |
| 905 | |
| 906 | if ((u32)index + (u32)size > 0xffff) |
| 907 | return -EPERM; |
| 908 | |
| 909 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 910 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 911 | |
| 912 | byen = byteen_start | (byteen_start << 4); |
| 913 | ret = set_registers(tp, index, type | byen, 4, data); |
| 914 | if (ret < 0) |
| 915 | goto error1; |
| 916 | |
| 917 | index += 4; |
| 918 | data += 4; |
| 919 | size -= 4; |
| 920 | |
| 921 | if (size) { |
| 922 | size -= 4; |
| 923 | |
| 924 | while (size) { |
| 925 | if (size > limit) { |
| 926 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 927 | type | BYTE_EN_DWORD, |
| 928 | limit, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 929 | if (ret < 0) |
| 930 | goto error1; |
| 931 | |
| 932 | index += limit; |
| 933 | data += limit; |
| 934 | size -= limit; |
| 935 | } else { |
| 936 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 937 | type | BYTE_EN_DWORD, |
| 938 | size, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 939 | if (ret < 0) |
| 940 | goto error1; |
| 941 | |
| 942 | index += size; |
| 943 | data += size; |
| 944 | size = 0; |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | byen = byteen_end | (byteen_end >> 4); |
| 950 | ret = set_registers(tp, index, type | byen, 4, data); |
| 951 | if (ret < 0) |
| 952 | goto error1; |
| 953 | } |
| 954 | |
| 955 | error1: |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 956 | if (ret == -ENODEV) |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 957 | rtl_set_unplug(tp); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 958 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | static inline |
| 963 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 964 | { |
| 965 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 966 | } |
| 967 | |
| 968 | static inline |
| 969 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 970 | { |
| 971 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 972 | } |
| 973 | |
| 974 | static inline |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 975 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 976 | { |
| 977 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 978 | } |
| 979 | |
| 980 | static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 981 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 982 | __le32 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 983 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 984 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 985 | |
| 986 | return __le32_to_cpu(data); |
| 987 | } |
| 988 | |
| 989 | static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 990 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 991 | __le32 tmp = __cpu_to_le32(data); |
| 992 | |
| 993 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 997 | { |
| 998 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 999 | __le32 tmp; |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 1000 | u16 byen = BYTE_EN_WORD; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1001 | u8 shift = index & 2; |
| 1002 | |
| 1003 | index &= ~3; |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 1004 | byen <<= shift; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1005 | |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 1006 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1007 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1008 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1009 | data >>= (shift * 8); |
| 1010 | data &= 0xffff; |
| 1011 | |
| 1012 | return (u16)data; |
| 1013 | } |
| 1014 | |
| 1015 | static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1016 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1017 | u32 mask = 0xffff; |
| 1018 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1019 | u16 byen = BYTE_EN_WORD; |
| 1020 | u8 shift = index & 2; |
| 1021 | |
| 1022 | data &= mask; |
| 1023 | |
| 1024 | if (index & 2) { |
| 1025 | byen <<= shift; |
| 1026 | mask <<= (shift * 8); |
| 1027 | data <<= (shift * 8); |
| 1028 | index &= ~3; |
| 1029 | } |
| 1030 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1031 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1032 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1033 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 1037 | { |
| 1038 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1039 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1040 | u8 shift = index & 3; |
| 1041 | |
| 1042 | index &= ~3; |
| 1043 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1044 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1045 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1046 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1047 | data >>= (shift * 8); |
| 1048 | data &= 0xff; |
| 1049 | |
| 1050 | return (u8)data; |
| 1051 | } |
| 1052 | |
| 1053 | static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1054 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1055 | u32 mask = 0xff; |
| 1056 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1057 | u16 byen = BYTE_EN_BYTE; |
| 1058 | u8 shift = index & 3; |
| 1059 | |
| 1060 | data &= mask; |
| 1061 | |
| 1062 | if (index & 3) { |
| 1063 | byen <<= shift; |
| 1064 | mask <<= (shift * 8); |
| 1065 | data <<= (shift * 8); |
| 1066 | index &= ~3; |
| 1067 | } |
| 1068 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1069 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1070 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1071 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1074 | static u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 1075 | { |
| 1076 | u16 ocp_base, ocp_index; |
| 1077 | |
| 1078 | ocp_base = addr & 0xf000; |
| 1079 | if (ocp_base != tp->ocp_base) { |
| 1080 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 1081 | tp->ocp_base = ocp_base; |
| 1082 | } |
| 1083 | |
| 1084 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 1085 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 1086 | } |
| 1087 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 1088 | static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 1089 | { |
| 1090 | u16 ocp_base, ocp_index; |
| 1091 | |
| 1092 | ocp_base = addr & 0xf000; |
| 1093 | if (ocp_base != tp->ocp_base) { |
| 1094 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 1095 | tp->ocp_base = ocp_base; |
| 1096 | } |
| 1097 | |
| 1098 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 1099 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 1100 | } |
| 1101 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1102 | static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1103 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1104 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1107 | static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1108 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1109 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 1112 | static void sram_write(struct r8152 *tp, u16 addr, u16 data) |
| 1113 | { |
| 1114 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 1115 | ocp_reg_write(tp, OCP_SRAM_DATA, data); |
| 1116 | } |
| 1117 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 1118 | static u16 sram_read(struct r8152 *tp, u16 addr) |
| 1119 | { |
| 1120 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 1121 | return ocp_reg_read(tp, OCP_SRAM_DATA); |
| 1122 | } |
| 1123 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1124 | static int read_mii_word(struct net_device *netdev, int phy_id, int reg) |
| 1125 | { |
| 1126 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1127 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1128 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 1129 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1130 | return -ENODEV; |
| 1131 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1132 | if (phy_id != R8152_PHY_ID) |
| 1133 | return -EINVAL; |
| 1134 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1135 | ret = r8152_mdio_read(tp, reg); |
| 1136 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1137 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static |
| 1141 | void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) |
| 1142 | { |
| 1143 | struct r8152 *tp = netdev_priv(netdev); |
| 1144 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 1145 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1146 | return; |
| 1147 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1148 | if (phy_id != R8152_PHY_ID) |
| 1149 | return; |
| 1150 | |
| 1151 | r8152_mdio_write(tp, reg, val); |
| 1152 | } |
| 1153 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1154 | static int |
| 1155 | r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1156 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1157 | static int rtl8152_set_mac_address(struct net_device *netdev, void *p) |
| 1158 | { |
| 1159 | struct r8152 *tp = netdev_priv(netdev); |
| 1160 | struct sockaddr *addr = p; |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1161 | int ret = -EADDRNOTAVAIL; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1162 | |
| 1163 | if (!is_valid_ether_addr(addr->sa_data)) |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1164 | goto out1; |
| 1165 | |
| 1166 | ret = usb_autopm_get_interface(tp->intf); |
| 1167 | if (ret < 0) |
| 1168 | goto out1; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1169 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1170 | mutex_lock(&tp->control); |
| 1171 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1172 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 1173 | |
| 1174 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 1175 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); |
| 1176 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1177 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1178 | mutex_unlock(&tp->control); |
| 1179 | |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1180 | usb_autopm_put_interface(tp->intf); |
| 1181 | out1: |
| 1182 | return ret; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1183 | } |
| 1184 | |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1185 | /* Devices containing proper chips can support a persistent |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1186 | * host system provided MAC address. |
| 1187 | * Examples of this are Dell TB15 and Dell WD15 docks |
| 1188 | */ |
| 1189 | static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa) |
| 1190 | { |
| 1191 | acpi_status status; |
| 1192 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1193 | union acpi_object *obj; |
| 1194 | int ret = -EINVAL; |
| 1195 | u32 ocp_data; |
| 1196 | unsigned char buf[6]; |
| 1197 | |
| 1198 | /* test for -AD variant of RTL8153 */ |
| 1199 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1200 | if ((ocp_data & AD_MASK) == 0x1000) { |
| 1201 | /* test for MAC address pass-through bit */ |
| 1202 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE); |
| 1203 | if ((ocp_data & PASS_THRU_MASK) != 1) { |
| 1204 | netif_dbg(tp, probe, tp->netdev, |
| 1205 | "No efuse for RTL8153-AD MAC pass through\n"); |
| 1206 | return -ENODEV; |
| 1207 | } |
| 1208 | } else { |
David Chen | 8e29d23 | 2019-02-16 17:16:42 +0800 | [diff] [blame] | 1209 | /* test for RTL8153-BND and RTL8153-BD */ |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1210 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); |
David Chen | c286909 | 2019-02-20 13:47:19 +0800 | [diff] [blame] | 1211 | if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) { |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1212 | netif_dbg(tp, probe, tp->netdev, |
| 1213 | "Invalid variant for MAC pass through\n"); |
| 1214 | return -ENODEV; |
| 1215 | } |
| 1216 | } |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1217 | |
| 1218 | /* returns _AUXMAC_#AABBCCDDEEFF# */ |
| 1219 | status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer); |
| 1220 | obj = (union acpi_object *)buffer.pointer; |
| 1221 | if (!ACPI_SUCCESS(status)) |
| 1222 | return -ENODEV; |
| 1223 | if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) { |
| 1224 | netif_warn(tp, probe, tp->netdev, |
hayeswang | 53700f0 | 2016-09-01 17:01:42 +0800 | [diff] [blame] | 1225 | "Invalid buffer for pass-thru MAC addr: (%d, %d)\n", |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1226 | obj->type, obj->string.length); |
| 1227 | goto amacout; |
| 1228 | } |
| 1229 | if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 || |
| 1230 | strncmp(obj->string.pointer + 0x15, "#", 1) != 0) { |
| 1231 | netif_warn(tp, probe, tp->netdev, |
| 1232 | "Invalid header when reading pass-thru MAC addr\n"); |
| 1233 | goto amacout; |
| 1234 | } |
| 1235 | ret = hex2bin(buf, obj->string.pointer + 9, 6); |
| 1236 | if (!(ret == 0 && is_valid_ether_addr(buf))) { |
| 1237 | netif_warn(tp, probe, tp->netdev, |
hayeswang | 53700f0 | 2016-09-01 17:01:42 +0800 | [diff] [blame] | 1238 | "Invalid MAC for pass-thru MAC addr: %d, %pM\n", |
| 1239 | ret, buf); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1240 | ret = -EINVAL; |
| 1241 | goto amacout; |
| 1242 | } |
| 1243 | memcpy(sa->sa_data, buf, 6); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1244 | netif_info(tp, probe, tp->netdev, |
| 1245 | "Using pass-thru MAC addr %pM\n", sa->sa_data); |
| 1246 | |
| 1247 | amacout: |
| 1248 | kfree(obj); |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1252 | static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa) |
| 1253 | { |
| 1254 | struct net_device *dev = tp->netdev; |
| 1255 | int ret; |
| 1256 | |
Crag.Wang | a6cbcb7 | 2019-04-22 13:03:43 +0800 | [diff] [blame] | 1257 | sa->sa_family = dev->type; |
| 1258 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1259 | if (tp->version == RTL_VER_01) { |
| 1260 | ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data); |
| 1261 | } else { |
| 1262 | /* if device doesn't support MAC pass through this will |
| 1263 | * be expected to be non-zero |
| 1264 | */ |
| 1265 | ret = vendor_mac_passthru_addr_read(tp, sa); |
| 1266 | if (ret < 0) |
| 1267 | ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data); |
| 1268 | } |
| 1269 | |
| 1270 | if (ret < 0) { |
| 1271 | netif_err(tp, probe, dev, "Get ether addr fail\n"); |
| 1272 | } else if (!is_valid_ether_addr(sa->sa_data)) { |
| 1273 | netif_err(tp, probe, dev, "Invalid ether addr %pM\n", |
| 1274 | sa->sa_data); |
| 1275 | eth_hw_addr_random(dev); |
| 1276 | ether_addr_copy(sa->sa_data, dev->dev_addr); |
| 1277 | netif_info(tp, probe, dev, "Random ether addr %pM\n", |
| 1278 | sa->sa_data); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1285 | static int set_ethernet_addr(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1286 | { |
| 1287 | struct net_device *dev = tp->netdev; |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1288 | struct sockaddr sa; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1289 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1290 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1291 | ret = determine_ethernet_addr(tp, &sa); |
| 1292 | if (ret < 0) |
| 1293 | return ret; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1294 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1295 | if (tp->version == RTL_VER_01) |
| 1296 | ether_addr_copy(dev->dev_addr, sa.sa_data); |
| 1297 | else |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1298 | ret = rtl8152_set_mac_address(dev, &sa); |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1299 | |
| 1300 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1303 | static void read_bulk_callback(struct urb *urb) |
| 1304 | { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1305 | struct net_device *netdev; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1306 | int status = urb->status; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1307 | struct rx_agg *agg; |
| 1308 | struct r8152 *tp; |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1309 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1310 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1311 | agg = urb->context; |
| 1312 | if (!agg) |
| 1313 | return; |
| 1314 | |
| 1315 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1316 | if (!tp) |
| 1317 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1318 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1319 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1320 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1321 | |
| 1322 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1325 | netdev = tp->netdev; |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1326 | |
| 1327 | /* When link down, the driver would cancel all bulks. */ |
| 1328 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1329 | if (!netif_carrier_ok(netdev)) |
| 1330 | return; |
| 1331 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1332 | usb_mark_last_busy(tp->udev); |
| 1333 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1334 | switch (status) { |
| 1335 | case 0: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1336 | if (urb->actual_length < ETH_ZLEN) |
| 1337 | break; |
| 1338 | |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1339 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1340 | list_add_tail(&agg->list, &tp->rx_done); |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1341 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1342 | napi_schedule(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1343 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1344 | case -ESHUTDOWN: |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1345 | rtl_set_unplug(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1346 | netif_device_detach(tp->netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1347 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1348 | case -ENOENT: |
| 1349 | return; /* the urb is in unlink state */ |
| 1350 | case -ETIME: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1351 | if (net_ratelimit()) |
| 1352 | netdev_warn(netdev, "maybe reset is needed?\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1353 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1354 | default: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1355 | if (net_ratelimit()) |
| 1356 | netdev_warn(netdev, "Rx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1357 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1360 | r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | static void write_bulk_callback(struct urb *urb) |
| 1364 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1365 | struct net_device_stats *stats; |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1366 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1367 | struct tx_agg *agg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1368 | struct r8152 *tp; |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1369 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1370 | int status = urb->status; |
| 1371 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1372 | agg = urb->context; |
| 1373 | if (!agg) |
| 1374 | return; |
| 1375 | |
| 1376 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1377 | if (!tp) |
| 1378 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1379 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1380 | netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1381 | stats = &netdev->stats; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1382 | if (status) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1383 | if (net_ratelimit()) |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1384 | netdev_warn(netdev, "Tx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1385 | stats->tx_errors += agg->skb_num; |
| 1386 | } else { |
| 1387 | stats->tx_packets += agg->skb_num; |
| 1388 | stats->tx_bytes += agg->skb_len; |
| 1389 | } |
| 1390 | |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1391 | spin_lock_irqsave(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1392 | list_add_tail(&agg->list, &tp->tx_free); |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1393 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1394 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1395 | usb_autopm_put_interface_async(tp->intf); |
| 1396 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1397 | if (!netif_carrier_ok(netdev)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1398 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1399 | |
| 1400 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1401 | return; |
| 1402 | |
| 1403 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1404 | return; |
| 1405 | |
| 1406 | if (!skb_queue_empty(&tp->tx_queue)) |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 1407 | tasklet_schedule(&tp->tx_tl); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1408 | } |
| 1409 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1410 | static void intr_callback(struct urb *urb) |
| 1411 | { |
| 1412 | struct r8152 *tp; |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 1413 | __le16 *d; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1414 | int status = urb->status; |
| 1415 | int res; |
| 1416 | |
| 1417 | tp = urb->context; |
| 1418 | if (!tp) |
| 1419 | return; |
| 1420 | |
| 1421 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1422 | return; |
| 1423 | |
| 1424 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1425 | return; |
| 1426 | |
| 1427 | switch (status) { |
| 1428 | case 0: /* success */ |
| 1429 | break; |
| 1430 | case -ECONNRESET: /* unlink */ |
| 1431 | case -ESHUTDOWN: |
| 1432 | netif_device_detach(tp->netdev); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 1433 | /* fall through */ |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1434 | case -ENOENT: |
hayeswang | d59c876 | 2014-10-31 13:35:57 +0800 | [diff] [blame] | 1435 | case -EPROTO: |
| 1436 | netif_info(tp, intr, tp->netdev, |
| 1437 | "Stop submitting intr, status %d\n", status); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1438 | return; |
| 1439 | case -EOVERFLOW: |
| 1440 | netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); |
| 1441 | goto resubmit; |
| 1442 | /* -EPIPE: should clear the halt */ |
| 1443 | default: |
| 1444 | netif_info(tp, intr, tp->netdev, "intr status %d\n", status); |
| 1445 | goto resubmit; |
| 1446 | } |
| 1447 | |
| 1448 | d = urb->transfer_buffer; |
| 1449 | if (INTR_LINK & __le16_to_cpu(d[0])) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1450 | if (!netif_carrier_ok(tp->netdev)) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1451 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1452 | schedule_delayed_work(&tp->schedule, 0); |
| 1453 | } |
| 1454 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1455 | if (netif_carrier_ok(tp->netdev)) { |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 1456 | netif_stop_queue(tp->netdev); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1457 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1458 | schedule_delayed_work(&tp->schedule, 0); |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | resubmit: |
| 1463 | res = usb_submit_urb(urb, GFP_ATOMIC); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1464 | if (res == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1465 | rtl_set_unplug(tp); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1466 | netif_device_detach(tp->netdev); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1467 | } else if (res) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1468 | netif_err(tp, intr, tp->netdev, |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1469 | "can't resubmit intr, status %d\n", res); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1470 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1471 | } |
| 1472 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1473 | static inline void *rx_agg_align(void *data) |
| 1474 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1475 | return (void *)ALIGN((uintptr_t)data, RX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static inline void *tx_agg_align(void *data) |
| 1479 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1480 | return (void *)ALIGN((uintptr_t)data, TX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1483 | static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg) |
| 1484 | { |
| 1485 | list_del(&agg->info_list); |
| 1486 | |
| 1487 | usb_free_urb(agg->urb); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 1488 | put_page(agg->page); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1489 | kfree(agg); |
| 1490 | |
| 1491 | atomic_dec(&tp->rx_count); |
| 1492 | } |
| 1493 | |
| 1494 | static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags) |
| 1495 | { |
| 1496 | struct net_device *netdev = tp->netdev; |
| 1497 | int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1498 | unsigned int order = get_order(tp->rx_buf_sz); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1499 | struct rx_agg *rx_agg; |
| 1500 | unsigned long flags; |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1501 | |
| 1502 | rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node); |
| 1503 | if (!rx_agg) |
| 1504 | return NULL; |
| 1505 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 1506 | rx_agg->page = alloc_pages(mflags | __GFP_COMP, order); |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1507 | if (!rx_agg->page) |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1508 | goto free_rx; |
| 1509 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1510 | rx_agg->buffer = page_address(rx_agg->page); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1511 | |
| 1512 | rx_agg->urb = usb_alloc_urb(0, mflags); |
| 1513 | if (!rx_agg->urb) |
| 1514 | goto free_buf; |
| 1515 | |
| 1516 | rx_agg->context = tp; |
| 1517 | |
| 1518 | INIT_LIST_HEAD(&rx_agg->list); |
| 1519 | INIT_LIST_HEAD(&rx_agg->info_list); |
| 1520 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1521 | list_add_tail(&rx_agg->info_list, &tp->rx_info); |
| 1522 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1523 | |
| 1524 | atomic_inc(&tp->rx_count); |
| 1525 | |
| 1526 | return rx_agg; |
| 1527 | |
| 1528 | free_buf: |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1529 | __free_pages(rx_agg->page, order); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1530 | free_rx: |
| 1531 | kfree(rx_agg); |
| 1532 | return NULL; |
| 1533 | } |
| 1534 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1535 | static void free_all_mem(struct r8152 *tp) |
| 1536 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1537 | struct rx_agg *agg, *agg_next; |
| 1538 | unsigned long flags; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1539 | int i; |
| 1540 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1541 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1542 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1543 | list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list) |
| 1544 | free_rx_agg(tp, agg); |
| 1545 | |
| 1546 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1547 | |
| 1548 | WARN_ON(atomic_read(&tp->rx_count)); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1549 | |
| 1550 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1551 | usb_free_urb(tp->tx_info[i].urb); |
| 1552 | tp->tx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1553 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1554 | kfree(tp->tx_info[i].buffer); |
| 1555 | tp->tx_info[i].buffer = NULL; |
| 1556 | tp->tx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1557 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1558 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1559 | usb_free_urb(tp->intr_urb); |
| 1560 | tp->intr_urb = NULL; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1561 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1562 | kfree(tp->intr_buff); |
| 1563 | tp->intr_buff = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static int alloc_all_mem(struct r8152 *tp) |
| 1567 | { |
| 1568 | struct net_device *netdev = tp->netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1569 | struct usb_interface *intf = tp->intf; |
| 1570 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1571 | struct usb_host_endpoint *ep_intr = alt->endpoint + 2; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1572 | int node, i; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1573 | |
| 1574 | node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
| 1575 | |
| 1576 | spin_lock_init(&tp->rx_lock); |
| 1577 | spin_lock_init(&tp->tx_lock); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1578 | INIT_LIST_HEAD(&tp->rx_info); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1579 | INIT_LIST_HEAD(&tp->tx_free); |
hayeswang | 98d068a | 2017-03-14 14:15:20 +0800 | [diff] [blame] | 1580 | INIT_LIST_HEAD(&tp->rx_done); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1581 | skb_queue_head_init(&tp->tx_queue); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1582 | skb_queue_head_init(&tp->rx_queue); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1583 | atomic_set(&tp->rx_count, 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1584 | |
| 1585 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1586 | if (!alloc_rx_agg(tp, GFP_KERNEL)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1587 | goto err1; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1591 | struct urb *urb; |
| 1592 | u8 *buf; |
| 1593 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1594 | buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1595 | if (!buf) |
| 1596 | goto err1; |
| 1597 | |
| 1598 | if (buf != tx_agg_align(buf)) { |
| 1599 | kfree(buf); |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1600 | buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL, |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1601 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1602 | if (!buf) |
| 1603 | goto err1; |
| 1604 | } |
| 1605 | |
| 1606 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1607 | if (!urb) { |
| 1608 | kfree(buf); |
| 1609 | goto err1; |
| 1610 | } |
| 1611 | |
| 1612 | INIT_LIST_HEAD(&tp->tx_info[i].list); |
| 1613 | tp->tx_info[i].context = tp; |
| 1614 | tp->tx_info[i].urb = urb; |
| 1615 | tp->tx_info[i].buffer = buf; |
| 1616 | tp->tx_info[i].head = tx_agg_align(buf); |
| 1617 | |
| 1618 | list_add_tail(&tp->tx_info[i].list, &tp->tx_free); |
| 1619 | } |
| 1620 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1621 | tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1622 | if (!tp->intr_urb) |
| 1623 | goto err1; |
| 1624 | |
| 1625 | tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 1626 | if (!tp->intr_buff) |
| 1627 | goto err1; |
| 1628 | |
| 1629 | tp->intr_interval = (int)ep_intr->desc.bInterval; |
| 1630 | usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1631 | tp->intr_buff, INTBUFSIZE, intr_callback, |
| 1632 | tp, tp->intr_interval); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1633 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1634 | return 0; |
| 1635 | |
| 1636 | err1: |
| 1637 | free_all_mem(tp); |
| 1638 | return -ENOMEM; |
| 1639 | } |
| 1640 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1641 | static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) |
| 1642 | { |
| 1643 | struct tx_agg *agg = NULL; |
| 1644 | unsigned long flags; |
| 1645 | |
hayeswang | 21949ab | 2014-03-07 11:04:35 +0800 | [diff] [blame] | 1646 | if (list_empty(&tp->tx_free)) |
| 1647 | return NULL; |
| 1648 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1649 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1650 | if (!list_empty(&tp->tx_free)) { |
| 1651 | struct list_head *cursor; |
| 1652 | |
| 1653 | cursor = tp->tx_free.next; |
| 1654 | list_del_init(cursor); |
| 1655 | agg = list_entry(cursor, struct tx_agg, list); |
| 1656 | } |
| 1657 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1658 | |
| 1659 | return agg; |
| 1660 | } |
| 1661 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1662 | /* r8152_csum_workaround() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1663 | * The hw limites the value the transport offset. When the offset is out of the |
| 1664 | * range, calculate the checksum by sw. |
| 1665 | */ |
| 1666 | static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, |
| 1667 | struct sk_buff_head *list) |
| 1668 | { |
| 1669 | if (skb_shinfo(skb)->gso_size) { |
| 1670 | netdev_features_t features = tp->netdev->features; |
| 1671 | struct sk_buff_head seg_list; |
| 1672 | struct sk_buff *segs, *nskb; |
| 1673 | |
hayeswang | a91d45f | 2014-07-11 16:48:27 +0800 | [diff] [blame] | 1674 | features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1675 | segs = skb_gso_segment(skb, features); |
| 1676 | if (IS_ERR(segs) || !segs) |
| 1677 | goto drop; |
| 1678 | |
| 1679 | __skb_queue_head_init(&seg_list); |
| 1680 | |
| 1681 | do { |
| 1682 | nskb = segs; |
| 1683 | segs = segs->next; |
| 1684 | nskb->next = NULL; |
| 1685 | __skb_queue_tail(&seg_list, nskb); |
| 1686 | } while (segs); |
| 1687 | |
| 1688 | skb_queue_splice(&seg_list, list); |
| 1689 | dev_kfree_skb(skb); |
| 1690 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1691 | if (skb_checksum_help(skb) < 0) |
| 1692 | goto drop; |
| 1693 | |
| 1694 | __skb_queue_head(list, skb); |
| 1695 | } else { |
| 1696 | struct net_device_stats *stats; |
| 1697 | |
| 1698 | drop: |
| 1699 | stats = &tp->netdev->stats; |
| 1700 | stats->tx_dropped++; |
| 1701 | dev_kfree_skb(skb); |
| 1702 | } |
| 1703 | } |
| 1704 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1705 | /* msdn_giant_send_check() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1706 | * According to the document of microsoft, the TCP Pseudo Header excludes the |
| 1707 | * packet length for IPv6 TCP large packets. |
| 1708 | */ |
| 1709 | static int msdn_giant_send_check(struct sk_buff *skb) |
| 1710 | { |
| 1711 | const struct ipv6hdr *ipv6h; |
| 1712 | struct tcphdr *th; |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1713 | int ret; |
| 1714 | |
| 1715 | ret = skb_cow_head(skb, 0); |
| 1716 | if (ret) |
| 1717 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1718 | |
| 1719 | ipv6h = ipv6_hdr(skb); |
| 1720 | th = tcp_hdr(skb); |
| 1721 | |
| 1722 | th->check = 0; |
| 1723 | th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0); |
| 1724 | |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1725 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1726 | } |
| 1727 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1728 | static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb) |
| 1729 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1730 | if (skb_vlan_tag_present(skb)) { |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1731 | u32 opts2; |
| 1732 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1733 | opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1734 | desc->opts2 |= cpu_to_le32(opts2); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) |
| 1739 | { |
| 1740 | u32 opts2 = le32_to_cpu(desc->opts2); |
| 1741 | |
| 1742 | if (opts2 & RX_VLAN_TAG) |
| 1743 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
| 1744 | swab16(opts2 & 0xffff)); |
| 1745 | } |
| 1746 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1747 | static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, |
| 1748 | struct sk_buff *skb, u32 len, u32 transport_offset) |
| 1749 | { |
| 1750 | u32 mss = skb_shinfo(skb)->gso_size; |
| 1751 | u32 opts1, opts2 = 0; |
| 1752 | int ret = TX_CSUM_SUCCESS; |
| 1753 | |
| 1754 | WARN_ON_ONCE(len > TX_LEN_MAX); |
| 1755 | |
| 1756 | opts1 = len | TX_FS | TX_LS; |
| 1757 | |
| 1758 | if (mss) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1759 | if (transport_offset > GTTCPHO_MAX) { |
| 1760 | netif_warn(tp, tx_err, tp->netdev, |
| 1761 | "Invalid transport offset 0x%x for TSO\n", |
| 1762 | transport_offset); |
| 1763 | ret = TX_CSUM_TSO; |
| 1764 | goto unavailable; |
| 1765 | } |
| 1766 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1767 | switch (vlan_get_protocol(skb)) { |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1768 | case htons(ETH_P_IP): |
| 1769 | opts1 |= GTSENDV4; |
| 1770 | break; |
| 1771 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1772 | case htons(ETH_P_IPV6): |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1773 | if (msdn_giant_send_check(skb)) { |
| 1774 | ret = TX_CSUM_TSO; |
| 1775 | goto unavailable; |
| 1776 | } |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1777 | opts1 |= GTSENDV6; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1778 | break; |
| 1779 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1780 | default: |
| 1781 | WARN_ON_ONCE(1); |
| 1782 | break; |
| 1783 | } |
| 1784 | |
| 1785 | opts1 |= transport_offset << GTTCPHO_SHIFT; |
| 1786 | opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; |
| 1787 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1788 | u8 ip_protocol; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1789 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1790 | if (transport_offset > TCPHO_MAX) { |
| 1791 | netif_warn(tp, tx_err, tp->netdev, |
| 1792 | "Invalid transport offset 0x%x\n", |
| 1793 | transport_offset); |
| 1794 | ret = TX_CSUM_NONE; |
| 1795 | goto unavailable; |
| 1796 | } |
| 1797 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1798 | switch (vlan_get_protocol(skb)) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1799 | case htons(ETH_P_IP): |
| 1800 | opts2 |= IPV4_CS; |
| 1801 | ip_protocol = ip_hdr(skb)->protocol; |
| 1802 | break; |
| 1803 | |
| 1804 | case htons(ETH_P_IPV6): |
| 1805 | opts2 |= IPV6_CS; |
| 1806 | ip_protocol = ipv6_hdr(skb)->nexthdr; |
| 1807 | break; |
| 1808 | |
| 1809 | default: |
| 1810 | ip_protocol = IPPROTO_RAW; |
| 1811 | break; |
| 1812 | } |
| 1813 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1814 | if (ip_protocol == IPPROTO_TCP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1815 | opts2 |= TCP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1816 | else if (ip_protocol == IPPROTO_UDP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1817 | opts2 |= UDP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1818 | else |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1819 | WARN_ON_ONCE(1); |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1820 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1821 | opts2 |= transport_offset << TCPHO_SHIFT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1822 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1823 | |
| 1824 | desc->opts2 = cpu_to_le32(opts2); |
| 1825 | desc->opts1 = cpu_to_le32(opts1); |
| 1826 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1827 | unavailable: |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1828 | return ret; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 1829 | } |
| 1830 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1831 | static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) |
| 1832 | { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1833 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1834 | int remain, ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1835 | u8 *tx_data; |
| 1836 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1837 | __skb_queue_head_init(&skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1838 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1839 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1840 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1841 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1842 | tx_data = agg->head; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1843 | agg->skb_num = 0; |
| 1844 | agg->skb_len = 0; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1845 | remain = agg_buf_sz; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1846 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1847 | while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1848 | struct tx_desc *tx_desc; |
| 1849 | struct sk_buff *skb; |
| 1850 | unsigned int len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1851 | u32 offset; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1852 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1853 | skb = __skb_dequeue(&skb_head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1854 | if (!skb) |
| 1855 | break; |
| 1856 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1857 | len = skb->len + sizeof(*tx_desc); |
| 1858 | |
| 1859 | if (len > remain) { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1860 | __skb_queue_head(&skb_head, skb); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1861 | break; |
| 1862 | } |
| 1863 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1864 | tx_data = tx_agg_align(tx_data); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1865 | tx_desc = (struct tx_desc *)tx_data; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1866 | |
| 1867 | offset = (u32)skb_transport_offset(skb); |
| 1868 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1869 | if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) { |
| 1870 | r8152_csum_workaround(tp, skb, &skb_head); |
| 1871 | continue; |
| 1872 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1873 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1874 | rtl_tx_vlan_tag(tx_desc, skb); |
| 1875 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1876 | tx_data += sizeof(*tx_desc); |
| 1877 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1878 | len = skb->len; |
| 1879 | if (skb_copy_bits(skb, 0, tx_data, len) < 0) { |
| 1880 | struct net_device_stats *stats = &tp->netdev->stats; |
| 1881 | |
| 1882 | stats->tx_dropped++; |
| 1883 | dev_kfree_skb_any(skb); |
| 1884 | tx_data -= sizeof(*tx_desc); |
| 1885 | continue; |
| 1886 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1887 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 1888 | tx_data += len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1889 | agg->skb_len += len; |
Eric Dumazet | 4c27bf3 | 2018-02-25 19:12:10 -0800 | [diff] [blame] | 1890 | agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1891 | |
| 1892 | dev_kfree_skb_any(skb); |
| 1893 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1894 | remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head); |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 1895 | |
| 1896 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 1897 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1898 | } |
| 1899 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1900 | if (!skb_queue_empty(&skb_head)) { |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1901 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1902 | skb_queue_splice(&skb_head, tx_queue); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1903 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 1904 | } |
| 1905 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1906 | netif_tx_lock(tp->netdev); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1907 | |
| 1908 | if (netif_queue_stopped(tp->netdev) && |
| 1909 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) |
| 1910 | netif_wake_queue(tp->netdev); |
| 1911 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1912 | netif_tx_unlock(tp->netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1913 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1914 | ret = usb_autopm_get_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1915 | if (ret < 0) |
| 1916 | goto out_tx_fill; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 1917 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1918 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), |
| 1919 | agg->head, (int)(tx_data - (u8 *)agg->head), |
| 1920 | (usb_complete_t)write_bulk_callback, agg); |
| 1921 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1922 | ret = usb_submit_urb(agg->urb, GFP_ATOMIC); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1923 | if (ret < 0) |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 1924 | usb_autopm_put_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1925 | |
| 1926 | out_tx_fill: |
| 1927 | return ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 1928 | } |
| 1929 | |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1930 | static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc) |
| 1931 | { |
| 1932 | u8 checksum = CHECKSUM_NONE; |
| 1933 | u32 opts2, opts3; |
| 1934 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 1935 | if (!(tp->netdev->features & NETIF_F_RXCSUM)) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1936 | goto return_result; |
| 1937 | |
| 1938 | opts2 = le32_to_cpu(rx_desc->opts2); |
| 1939 | opts3 = le32_to_cpu(rx_desc->opts3); |
| 1940 | |
| 1941 | if (opts2 & RD_IPV4_CS) { |
| 1942 | if (opts3 & IPF) |
| 1943 | checksum = CHECKSUM_NONE; |
Hayes Wang | ea6499e | 2018-02-02 16:43:35 +0800 | [diff] [blame] | 1944 | else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 1945 | checksum = CHECKSUM_UNNECESSARY; |
| 1946 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1947 | checksum = CHECKSUM_UNNECESSARY; |
Mark Lord | b9a321b | 2016-10-30 19:28:27 -0400 | [diff] [blame] | 1948 | } else if (opts2 & RD_IPV6_CS) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1949 | if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 1950 | checksum = CHECKSUM_UNNECESSARY; |
| 1951 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
| 1952 | checksum = CHECKSUM_UNNECESSARY; |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | return_result: |
| 1956 | return checksum; |
| 1957 | } |
| 1958 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 1959 | static inline bool rx_count_exceed(struct r8152 *tp) |
| 1960 | { |
| 1961 | return atomic_read(&tp->rx_count) > RTL8152_MAX_RX; |
| 1962 | } |
| 1963 | |
| 1964 | static inline int agg_offset(struct rx_agg *agg, void *addr) |
| 1965 | { |
| 1966 | return (int)(addr - agg->buffer); |
| 1967 | } |
| 1968 | |
| 1969 | static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags) |
| 1970 | { |
| 1971 | struct rx_agg *agg, *agg_next, *agg_free = NULL; |
| 1972 | unsigned long flags; |
| 1973 | |
| 1974 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1975 | |
| 1976 | list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) { |
| 1977 | if (page_count(agg->page) == 1) { |
| 1978 | if (!agg_free) { |
| 1979 | list_del_init(&agg->list); |
| 1980 | agg_free = agg; |
| 1981 | continue; |
| 1982 | } |
| 1983 | if (rx_count_exceed(tp)) { |
| 1984 | list_del_init(&agg->list); |
| 1985 | free_rx_agg(tp, agg); |
| 1986 | } |
| 1987 | break; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1992 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 1993 | if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending) |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 1994 | agg_free = alloc_rx_agg(tp, mflags); |
| 1995 | |
| 1996 | return agg_free; |
| 1997 | } |
| 1998 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1999 | static int rx_bottom(struct r8152 *tp, int budget) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2000 | { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 2001 | unsigned long flags; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2002 | struct list_head *cursor, *next, rx_queue; |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 2003 | int ret = 0, work_done = 0; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 2004 | struct napi_struct *napi = &tp->napi; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2005 | |
| 2006 | if (!skb_queue_empty(&tp->rx_queue)) { |
| 2007 | while (work_done < budget) { |
| 2008 | struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); |
| 2009 | struct net_device *netdev = tp->netdev; |
| 2010 | struct net_device_stats *stats = &netdev->stats; |
| 2011 | unsigned int pkt_len; |
| 2012 | |
| 2013 | if (!skb) |
| 2014 | break; |
| 2015 | |
| 2016 | pkt_len = skb->len; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 2017 | napi_gro_receive(napi, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2018 | work_done++; |
| 2019 | stats->rx_packets++; |
| 2020 | stats->rx_bytes += pkt_len; |
| 2021 | } |
| 2022 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2023 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2024 | if (list_empty(&tp->rx_done)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2025 | goto out1; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2026 | |
| 2027 | INIT_LIST_HEAD(&rx_queue); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 2028 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2029 | list_splice_init(&tp->rx_done, &rx_queue); |
| 2030 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2031 | |
| 2032 | list_for_each_safe(cursor, next, &rx_queue) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2033 | struct rx_desc *rx_desc; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2034 | struct rx_agg *agg, *agg_free; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2035 | int len_used = 0; |
| 2036 | struct urb *urb; |
| 2037 | u8 *rx_data; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2038 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2039 | list_del_init(cursor); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2040 | |
| 2041 | agg = list_entry(cursor, struct rx_agg, list); |
| 2042 | urb = agg->urb; |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 2043 | if (urb->actual_length < ETH_ZLEN) |
| 2044 | goto submit; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2045 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2046 | agg_free = rtl_get_free_rx(tp, GFP_ATOMIC); |
| 2047 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 2048 | rx_desc = agg->buffer; |
| 2049 | rx_data = agg->buffer; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2050 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2051 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2052 | while (urb->actual_length > len_used) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2053 | struct net_device *netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2054 | struct net_device_stats *stats = &netdev->stats; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2055 | unsigned int pkt_len, rx_frag_head_sz; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2056 | struct sk_buff *skb; |
| 2057 | |
hayeswang | 7454445 | 2017-06-09 17:11:47 +0800 | [diff] [blame] | 2058 | /* limite the skb numbers for rx_queue */ |
| 2059 | if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000)) |
| 2060 | break; |
| 2061 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2062 | pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2063 | if (pkt_len < ETH_ZLEN) |
| 2064 | break; |
| 2065 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2066 | len_used += pkt_len; |
| 2067 | if (urb->actual_length < len_used) |
| 2068 | break; |
| 2069 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2070 | pkt_len -= ETH_FCS_LEN; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2071 | rx_data += sizeof(struct rx_desc); |
| 2072 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 2073 | if (!agg_free || tp->rx_copybreak > pkt_len) |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2074 | rx_frag_head_sz = pkt_len; |
| 2075 | else |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 2076 | rx_frag_head_sz = tp->rx_copybreak; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2077 | |
| 2078 | skb = napi_alloc_skb(napi, rx_frag_head_sz); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2079 | if (!skb) { |
| 2080 | stats->rx_dropped++; |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 2081 | goto find_next_rx; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2082 | } |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2083 | |
| 2084 | skb->ip_summed = r8152_rx_csum(tp, rx_desc); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2085 | memcpy(skb->data, rx_data, rx_frag_head_sz); |
| 2086 | skb_put(skb, rx_frag_head_sz); |
| 2087 | pkt_len -= rx_frag_head_sz; |
| 2088 | rx_data += rx_frag_head_sz; |
| 2089 | if (pkt_len) { |
| 2090 | skb_add_rx_frag(skb, 0, agg->page, |
| 2091 | agg_offset(agg, rx_data), |
| 2092 | pkt_len, |
| 2093 | SKB_DATA_ALIGN(pkt_len)); |
| 2094 | get_page(agg->page); |
| 2095 | } |
| 2096 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2097 | skb->protocol = eth_type_trans(skb, netdev); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2098 | rtl_rx_vlan_tag(rx_desc, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2099 | if (work_done < budget) { |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2100 | work_done++; |
| 2101 | stats->rx_packets++; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2102 | stats->rx_bytes += skb->len; |
Hayes Wang | 6636fb3 | 2019-08-19 11:15:19 +0800 | [diff] [blame] | 2103 | napi_gro_receive(napi, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2104 | } else { |
| 2105 | __skb_queue_tail(&tp->rx_queue, skb); |
| 2106 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2107 | |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 2108 | find_next_rx: |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2109 | rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2110 | rx_desc = (struct rx_desc *)rx_data; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2111 | len_used = agg_offset(agg, rx_data); |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2112 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2113 | } |
| 2114 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2115 | WARN_ON(!agg_free && page_count(agg->page) > 1); |
| 2116 | |
| 2117 | if (agg_free) { |
| 2118 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2119 | if (page_count(agg->page) == 1) { |
| 2120 | list_add(&agg_free->list, &tp->rx_used); |
| 2121 | } else { |
| 2122 | list_add_tail(&agg->list, &tp->rx_used); |
| 2123 | agg = agg_free; |
| 2124 | urb = agg->urb; |
| 2125 | } |
| 2126 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2127 | } |
| 2128 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 2129 | submit: |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 2130 | if (!ret) { |
| 2131 | ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
| 2132 | } else { |
| 2133 | urb->actual_length = 0; |
| 2134 | list_add_tail(&agg->list, next); |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | if (!list_empty(&rx_queue)) { |
| 2139 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2140 | list_splice_tail(&rx_queue, &tp->rx_done); |
| 2141 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2142 | } |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2143 | |
| 2144 | out1: |
| 2145 | return work_done; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | static void tx_bottom(struct r8152 *tp) |
| 2149 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2150 | int res; |
| 2151 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2152 | do { |
| 2153 | struct tx_agg *agg; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2154 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2155 | if (skb_queue_empty(&tp->tx_queue)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2156 | break; |
| 2157 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2158 | agg = r8152_get_tx_agg(tp); |
| 2159 | if (!agg) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2160 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2161 | |
| 2162 | res = r8152_tx_agg_fill(tp, agg); |
| 2163 | if (res) { |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2164 | struct net_device *netdev = tp->netdev; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2165 | |
| 2166 | if (res == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 2167 | rtl_set_unplug(tp); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2168 | netif_device_detach(netdev); |
| 2169 | } else { |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2170 | struct net_device_stats *stats = &netdev->stats; |
| 2171 | unsigned long flags; |
| 2172 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2173 | netif_warn(tp, tx_err, netdev, |
| 2174 | "failed tx_urb %d\n", res); |
| 2175 | stats->tx_dropped += agg->skb_num; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 2176 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2177 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 2178 | list_add_tail(&agg->list, &tp->tx_free); |
| 2179 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 2180 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2181 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2182 | } while (res == 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2183 | } |
| 2184 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2185 | static void bottom_half(unsigned long data) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2186 | { |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2187 | struct r8152 *tp; |
| 2188 | |
| 2189 | tp = (struct r8152 *)data; |
| 2190 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2191 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2192 | return; |
| 2193 | |
| 2194 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 2195 | return; |
| 2196 | |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 2197 | /* When link down, the driver would cancel all bulks. */ |
| 2198 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2199 | if (!netif_carrier_ok(tp->netdev)) |
| 2200 | return; |
| 2201 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2202 | clear_bit(SCHEDULE_TASKLET, &tp->flags); |
hayeswang | 9451a11 | 2014-11-12 10:05:04 +0800 | [diff] [blame] | 2203 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2204 | tx_bottom(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2205 | } |
| 2206 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2207 | static int r8152_poll(struct napi_struct *napi, int budget) |
| 2208 | { |
| 2209 | struct r8152 *tp = container_of(napi, struct r8152, napi); |
| 2210 | int work_done; |
| 2211 | |
| 2212 | work_done = rx_bottom(tp, budget); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2213 | |
| 2214 | if (work_done < budget) { |
hayeswang | a3307f9 | 2017-06-09 17:11:48 +0800 | [diff] [blame] | 2215 | if (!napi_complete_done(napi, work_done)) |
| 2216 | goto out; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2217 | if (!list_empty(&tp->rx_done)) |
| 2218 | napi_schedule(napi); |
| 2219 | } |
| 2220 | |
hayeswang | a3307f9 | 2017-06-09 17:11:48 +0800 | [diff] [blame] | 2221 | out: |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2222 | return work_done; |
| 2223 | } |
| 2224 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2225 | static |
| 2226 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) |
| 2227 | { |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2228 | int ret; |
| 2229 | |
hayeswang | ef827a5 | 2015-01-09 10:26:36 +0800 | [diff] [blame] | 2230 | /* The rx would be stopped, so skip submitting */ |
| 2231 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || |
| 2232 | !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) |
| 2233 | return 0; |
| 2234 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2235 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1), |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 2236 | agg->buffer, tp->rx_buf_sz, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2237 | (usb_complete_t)read_bulk_callback, agg); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2238 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2239 | ret = usb_submit_urb(agg->urb, mem_flags); |
| 2240 | if (ret == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 2241 | rtl_set_unplug(tp); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2242 | netif_device_detach(tp->netdev); |
| 2243 | } else if (ret) { |
| 2244 | struct urb *urb = agg->urb; |
| 2245 | unsigned long flags; |
| 2246 | |
| 2247 | urb->actual_length = 0; |
| 2248 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2249 | list_add_tail(&agg->list, &tp->rx_done); |
| 2250 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2251 | |
| 2252 | netif_err(tp, rx_err, tp->netdev, |
| 2253 | "Couldn't submit rx[%p], ret = %d\n", agg, ret); |
| 2254 | |
| 2255 | napi_schedule(&tp->napi); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2261 | static void rtl_drop_queued_tx(struct r8152 *tp) |
| 2262 | { |
| 2263 | struct net_device_stats *stats = &tp->netdev->stats; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2264 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2265 | struct sk_buff *skb; |
| 2266 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2267 | if (skb_queue_empty(tx_queue)) |
| 2268 | return; |
| 2269 | |
| 2270 | __skb_queue_head_init(&skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 2271 | spin_lock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2272 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 2273 | spin_unlock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2274 | |
| 2275 | while ((skb = __skb_dequeue(&skb_head))) { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2276 | dev_kfree_skb(skb); |
| 2277 | stats->tx_dropped++; |
| 2278 | } |
| 2279 | } |
| 2280 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2281 | static void rtl8152_tx_timeout(struct net_device *netdev) |
| 2282 | { |
| 2283 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2284 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 2285 | netif_warn(tp, tx_err, netdev, "Tx timeout\n"); |
hayeswang | 37608f3 | 2015-07-29 20:39:09 +0800 | [diff] [blame] | 2286 | |
| 2287 | usb_queue_reset_device(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | static void rtl8152_set_rx_mode(struct net_device *netdev) |
| 2291 | { |
| 2292 | struct r8152 *tp = netdev_priv(netdev); |
| 2293 | |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 2294 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2295 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2296 | schedule_delayed_work(&tp->schedule, 0); |
| 2297 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | static void _rtl8152_set_rx_mode(struct net_device *netdev) |
| 2301 | { |
| 2302 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2303 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 2304 | __le32 tmp[2]; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2305 | u32 ocp_data; |
| 2306 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2307 | netif_stop_queue(netdev); |
| 2308 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2309 | ocp_data &= ~RCR_ACPT_ALL; |
| 2310 | ocp_data |= RCR_AB | RCR_APM; |
| 2311 | |
| 2312 | if (netdev->flags & IFF_PROMISC) { |
| 2313 | /* Unconditionally log net taps. */ |
| 2314 | netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); |
| 2315 | ocp_data |= RCR_AM | RCR_AAP; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2316 | mc_filter[1] = 0xffffffff; |
| 2317 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2318 | } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || |
| 2319 | (netdev->flags & IFF_ALLMULTI)) { |
| 2320 | /* Too many to filter perfectly -- accept all multicasts. */ |
| 2321 | ocp_data |= RCR_AM; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2322 | mc_filter[1] = 0xffffffff; |
| 2323 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2324 | } else { |
| 2325 | struct netdev_hw_addr *ha; |
| 2326 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2327 | mc_filter[1] = 0; |
| 2328 | mc_filter[0] = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2329 | netdev_for_each_mc_addr(ha, netdev) { |
| 2330 | int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2331 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2332 | mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); |
| 2333 | ocp_data |= RCR_AM; |
| 2334 | } |
| 2335 | } |
| 2336 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2337 | tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); |
| 2338 | tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2339 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2340 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2341 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2342 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 2345 | static netdev_features_t |
| 2346 | rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, |
| 2347 | netdev_features_t features) |
| 2348 | { |
| 2349 | u32 mss = skb_shinfo(skb)->gso_size; |
| 2350 | int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; |
| 2351 | int offset = skb_transport_offset(skb); |
| 2352 | |
| 2353 | if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset) |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 2354 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 2355 | else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) |
| 2356 | features &= ~NETIF_F_GSO_MASK; |
| 2357 | |
| 2358 | return features; |
| 2359 | } |
| 2360 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2361 | static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2362 | struct net_device *netdev) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2363 | { |
| 2364 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2365 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2366 | skb_tx_timestamp(skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2367 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 2368 | skb_queue_tail(&tp->tx_queue, skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2369 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2370 | if (!list_empty(&tp->tx_free)) { |
| 2371 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2372 | set_bit(SCHEDULE_TASKLET, &tp->flags); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2373 | schedule_delayed_work(&tp->schedule, 0); |
| 2374 | } else { |
| 2375 | usb_mark_last_busy(tp->udev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2376 | tasklet_schedule(&tp->tx_tl); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2377 | } |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2378 | } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2379 | netif_stop_queue(netdev); |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2380 | } |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2381 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2382 | return NETDEV_TX_OK; |
| 2383 | } |
| 2384 | |
| 2385 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 2386 | { |
| 2387 | u32 ocp_data; |
| 2388 | |
| 2389 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 2390 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 2391 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2392 | ocp_data |= FMC_FCR_MCU_EN; |
| 2393 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2394 | } |
| 2395 | |
| 2396 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 2397 | { |
| 2398 | int i; |
| 2399 | |
| 2400 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); |
| 2401 | |
| 2402 | for (i = 0; i < 1000; i++) { |
| 2403 | if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) |
| 2404 | break; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2405 | usleep_range(100, 400); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2409 | static void set_tx_qlen(struct r8152 *tp) |
| 2410 | { |
| 2411 | struct net_device *netdev = tp->netdev; |
| 2412 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2413 | tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN + |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 2414 | sizeof(struct tx_desc)); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2415 | } |
| 2416 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2417 | static inline u8 rtl8152_get_speed(struct r8152 *tp) |
| 2418 | { |
| 2419 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 2420 | } |
| 2421 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2422 | static void rtl_set_eee_plus(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2423 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2424 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2425 | u8 speed; |
| 2426 | |
| 2427 | speed = rtl8152_get_speed(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2428 | if (speed & _10bps) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2429 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2430 | ocp_data |= EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2431 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2432 | } else { |
| 2433 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2434 | ocp_data &= ~EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2435 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2436 | } |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2437 | } |
| 2438 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2439 | static void rxdy_gated_en(struct r8152 *tp, bool enable) |
| 2440 | { |
| 2441 | u32 ocp_data; |
| 2442 | |
| 2443 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 2444 | if (enable) |
| 2445 | ocp_data |= RXDY_GATED_EN; |
| 2446 | else |
| 2447 | ocp_data &= ~RXDY_GATED_EN; |
| 2448 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 2449 | } |
| 2450 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2451 | static int rtl_start_rx(struct r8152 *tp) |
| 2452 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2453 | struct rx_agg *agg, *agg_next; |
| 2454 | struct list_head tmp_list; |
| 2455 | unsigned long flags; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2456 | int ret = 0, i = 0; |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2457 | |
| 2458 | INIT_LIST_HEAD(&tmp_list); |
| 2459 | |
| 2460 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2461 | |
| 2462 | INIT_LIST_HEAD(&tp->rx_done); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2463 | INIT_LIST_HEAD(&tp->rx_used); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2464 | |
| 2465 | list_splice_init(&tp->rx_info, &tmp_list); |
| 2466 | |
| 2467 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2468 | |
| 2469 | list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { |
| 2470 | INIT_LIST_HEAD(&agg->list); |
| 2471 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2472 | /* Only RTL8152_MAX_RX rx_agg need to be submitted. */ |
| 2473 | if (++i > RTL8152_MAX_RX) { |
| 2474 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2475 | list_add_tail(&agg->list, &tp->rx_used); |
| 2476 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2477 | } else if (unlikely(ret < 0)) { |
| 2478 | spin_lock_irqsave(&tp->rx_lock, flags); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2479 | list_add_tail(&agg->list, &tp->rx_done); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2480 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2481 | } else { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2482 | ret = r8152_submit_rx(tp, agg, GFP_KERNEL); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2483 | } |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2484 | } |
| 2485 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2486 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2487 | WARN_ON(!list_empty(&tp->rx_info)); |
| 2488 | list_splice(&tmp_list, &tp->rx_info); |
| 2489 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | 7bcf4f6 | 2014-11-20 10:29:06 +0800 | [diff] [blame] | 2490 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2491 | return ret; |
| 2492 | } |
| 2493 | |
| 2494 | static int rtl_stop_rx(struct r8152 *tp) |
| 2495 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2496 | struct rx_agg *agg, *agg_next; |
| 2497 | struct list_head tmp_list; |
| 2498 | unsigned long flags; |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2499 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2500 | INIT_LIST_HEAD(&tmp_list); |
| 2501 | |
| 2502 | /* The usb_kill_urb() couldn't be used in atomic. |
| 2503 | * Therefore, move the list of rx_info to a tmp one. |
| 2504 | * Then, list_for_each_entry_safe could be used without |
| 2505 | * spin lock. |
| 2506 | */ |
| 2507 | |
| 2508 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2509 | list_splice_init(&tp->rx_info, &tmp_list); |
| 2510 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2511 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2512 | list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { |
| 2513 | /* At least RTL8152_MAX_RX rx_agg have the page_count being |
| 2514 | * equal to 1, so the other ones could be freed safely. |
| 2515 | */ |
| 2516 | if (page_count(agg->page) > 1) |
| 2517 | free_rx_agg(tp, agg); |
| 2518 | else |
| 2519 | usb_kill_urb(agg->urb); |
| 2520 | } |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2521 | |
| 2522 | /* Move back the list of temp to the rx_info */ |
| 2523 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2524 | WARN_ON(!list_empty(&tp->rx_info)); |
| 2525 | list_splice(&tmp_list, &tp->rx_info); |
| 2526 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2527 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2528 | while (!skb_queue_empty(&tp->rx_queue)) |
| 2529 | dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); |
| 2530 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2531 | return 0; |
| 2532 | } |
| 2533 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 2534 | static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) |
| 2535 | { |
| 2536 | ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, |
| 2537 | OWN_UPDATE | OWN_CLEAR); |
| 2538 | } |
| 2539 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2540 | static int rtl_enable(struct r8152 *tp) |
| 2541 | { |
| 2542 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2543 | |
| 2544 | r8152b_reset_packet_filter(tp); |
| 2545 | |
| 2546 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 2547 | ocp_data |= CR_RE | CR_TE; |
| 2548 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 2549 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 2550 | switch (tp->version) { |
| 2551 | case RTL_VER_08: |
| 2552 | case RTL_VER_09: |
| 2553 | r8153b_rx_agg_chg_indicate(tp); |
| 2554 | break; |
| 2555 | default: |
| 2556 | break; |
| 2557 | } |
| 2558 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2559 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2560 | |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 2561 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2564 | static int rtl8152_enable(struct r8152 *tp) |
| 2565 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2566 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2567 | return -ENODEV; |
| 2568 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2569 | set_tx_qlen(tp); |
| 2570 | rtl_set_eee_plus(tp); |
| 2571 | |
| 2572 | return rtl_enable(tp); |
| 2573 | } |
| 2574 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2575 | static void r8153_set_rx_early_timeout(struct r8152 *tp) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2576 | { |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2577 | u32 ocp_data = tp->coalesce / 8; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2578 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2579 | switch (tp->version) { |
| 2580 | case RTL_VER_03: |
| 2581 | case RTL_VER_04: |
| 2582 | case RTL_VER_05: |
| 2583 | case RTL_VER_06: |
| 2584 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 2585 | ocp_data); |
| 2586 | break; |
| 2587 | |
| 2588 | case RTL_VER_08: |
| 2589 | case RTL_VER_09: |
| 2590 | /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout |
| 2591 | * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. |
| 2592 | */ |
| 2593 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 2594 | 128 / 8); |
| 2595 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, |
| 2596 | ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2597 | break; |
| 2598 | |
| 2599 | default: |
| 2600 | break; |
| 2601 | } |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | static void r8153_set_rx_early_size(struct r8152 *tp) |
| 2605 | { |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 2606 | u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2607 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2608 | switch (tp->version) { |
| 2609 | case RTL_VER_03: |
| 2610 | case RTL_VER_04: |
| 2611 | case RTL_VER_05: |
| 2612 | case RTL_VER_06: |
| 2613 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 2614 | ocp_data / 4); |
| 2615 | break; |
| 2616 | case RTL_VER_08: |
| 2617 | case RTL_VER_09: |
| 2618 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 2619 | ocp_data / 8); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2620 | break; |
| 2621 | default: |
| 2622 | WARN_ON_ONCE(1); |
| 2623 | break; |
| 2624 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | static int rtl8153_enable(struct r8152 *tp) |
| 2628 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2629 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2630 | return -ENODEV; |
| 2631 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2632 | set_tx_qlen(tp); |
| 2633 | rtl_set_eee_plus(tp); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2634 | r8153_set_rx_early_timeout(tp); |
| 2635 | r8153_set_rx_early_size(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2636 | |
| 2637 | return rtl_enable(tp); |
| 2638 | } |
| 2639 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2640 | static void rtl_disable(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2641 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2642 | u32 ocp_data; |
| 2643 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2644 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2645 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2646 | rtl_drop_queued_tx(tp); |
| 2647 | return; |
| 2648 | } |
| 2649 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2650 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2651 | ocp_data &= ~RCR_ACPT_ALL; |
| 2652 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2653 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2654 | rtl_drop_queued_tx(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2655 | |
| 2656 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 2657 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2658 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2659 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2660 | |
| 2661 | for (i = 0; i < 1000; i++) { |
| 2662 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2663 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 2664 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2665 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | for (i = 0; i < 1000; i++) { |
| 2669 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 2670 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2671 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2674 | rtl_stop_rx(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2675 | |
| 2676 | rtl8152_nic_reset(tp); |
| 2677 | } |
| 2678 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2679 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 2680 | { |
| 2681 | u32 ocp_data; |
| 2682 | |
| 2683 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 2684 | if (enable) |
| 2685 | ocp_data |= POWER_CUT; |
| 2686 | else |
| 2687 | ocp_data &= ~POWER_CUT; |
| 2688 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 2689 | |
| 2690 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 2691 | ocp_data &= ~RESUME_INDICATE; |
| 2692 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2693 | } |
| 2694 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2695 | static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) |
| 2696 | { |
| 2697 | u32 ocp_data; |
| 2698 | |
| 2699 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2700 | if (enable) |
| 2701 | ocp_data |= CPCR_RX_VLAN; |
| 2702 | else |
| 2703 | ocp_data &= ~CPCR_RX_VLAN; |
| 2704 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2705 | } |
| 2706 | |
| 2707 | static int rtl8152_set_features(struct net_device *dev, |
| 2708 | netdev_features_t features) |
| 2709 | { |
| 2710 | netdev_features_t changed = features ^ dev->features; |
| 2711 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2712 | int ret; |
| 2713 | |
| 2714 | ret = usb_autopm_get_interface(tp->intf); |
| 2715 | if (ret < 0) |
| 2716 | goto out; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2717 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2718 | mutex_lock(&tp->control); |
| 2719 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2720 | if (changed & NETIF_F_HW_VLAN_CTAG_RX) { |
| 2721 | if (features & NETIF_F_HW_VLAN_CTAG_RX) |
| 2722 | rtl_rx_vlan_en(tp, true); |
| 2723 | else |
| 2724 | rtl_rx_vlan_en(tp, false); |
| 2725 | } |
| 2726 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2727 | mutex_unlock(&tp->control); |
| 2728 | |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2729 | usb_autopm_put_interface(tp->intf); |
| 2730 | |
| 2731 | out: |
| 2732 | return ret; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2733 | } |
| 2734 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2735 | #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) |
| 2736 | |
| 2737 | static u32 __rtl_get_wol(struct r8152 *tp) |
| 2738 | { |
| 2739 | u32 ocp_data; |
| 2740 | u32 wolopts = 0; |
| 2741 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2742 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2743 | if (ocp_data & LINK_ON_WAKE_EN) |
| 2744 | wolopts |= WAKE_PHY; |
| 2745 | |
| 2746 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2747 | if (ocp_data & UWF_EN) |
| 2748 | wolopts |= WAKE_UCAST; |
| 2749 | if (ocp_data & BWF_EN) |
| 2750 | wolopts |= WAKE_BCAST; |
| 2751 | if (ocp_data & MWF_EN) |
| 2752 | wolopts |= WAKE_MCAST; |
| 2753 | |
| 2754 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2755 | if (ocp_data & MAGIC_EN) |
| 2756 | wolopts |= WAKE_MAGIC; |
| 2757 | |
| 2758 | return wolopts; |
| 2759 | } |
| 2760 | |
| 2761 | static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) |
| 2762 | { |
| 2763 | u32 ocp_data; |
| 2764 | |
| 2765 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2766 | |
| 2767 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2768 | ocp_data &= ~LINK_ON_WAKE_EN; |
| 2769 | if (wolopts & WAKE_PHY) |
| 2770 | ocp_data |= LINK_ON_WAKE_EN; |
| 2771 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 2772 | |
| 2773 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
hayeswang | 92f7d07 | 2016-07-06 17:35:59 +0800 | [diff] [blame] | 2774 | ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2775 | if (wolopts & WAKE_UCAST) |
| 2776 | ocp_data |= UWF_EN; |
| 2777 | if (wolopts & WAKE_BCAST) |
| 2778 | ocp_data |= BWF_EN; |
| 2779 | if (wolopts & WAKE_MCAST) |
| 2780 | ocp_data |= MWF_EN; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2781 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); |
| 2782 | |
| 2783 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 2784 | |
| 2785 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2786 | ocp_data &= ~MAGIC_EN; |
| 2787 | if (wolopts & WAKE_MAGIC) |
| 2788 | ocp_data |= MAGIC_EN; |
| 2789 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 2790 | |
| 2791 | if (wolopts & WAKE_ANY) |
| 2792 | device_set_wakeup_enable(&tp->udev->dev, true); |
| 2793 | else |
| 2794 | device_set_wakeup_enable(&tp->udev->dev, false); |
| 2795 | } |
| 2796 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 2797 | static void r8153_mac_clk_spd(struct r8152 *tp, bool enable) |
| 2798 | { |
| 2799 | /* MAC clock speed down */ |
| 2800 | if (enable) { |
| 2801 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, |
| 2802 | ALDPS_SPDWN_RATIO); |
| 2803 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, |
| 2804 | EEE_SPDWN_RATIO); |
| 2805 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, |
| 2806 | PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | |
| 2807 | U1U2_SPDWN_EN | L1_SPDWN_EN); |
| 2808 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, |
| 2809 | PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | |
| 2810 | TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN | |
| 2811 | TP1000_SPDWN_EN); |
| 2812 | } else { |
| 2813 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0); |
| 2814 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0); |
| 2815 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0); |
| 2816 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0); |
| 2817 | } |
| 2818 | } |
| 2819 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2820 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
| 2821 | { |
| 2822 | u8 u1u2[8]; |
| 2823 | |
| 2824 | if (enable) |
| 2825 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 2826 | else |
| 2827 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 2828 | |
| 2829 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 2830 | } |
| 2831 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2832 | static void r8153b_u1u2en(struct r8152 *tp, bool enable) |
| 2833 | { |
| 2834 | u32 ocp_data; |
| 2835 | |
| 2836 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); |
| 2837 | if (enable) |
| 2838 | ocp_data |= LPM_U1U2_EN; |
| 2839 | else |
| 2840 | ocp_data &= ~LPM_U1U2_EN; |
| 2841 | |
| 2842 | ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); |
| 2843 | } |
| 2844 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2845 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
| 2846 | { |
| 2847 | u32 ocp_data; |
| 2848 | |
| 2849 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 2850 | if (enable) |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2851 | ocp_data |= U2P3_ENABLE; |
| 2852 | else |
| 2853 | ocp_data &= ~U2P3_ENABLE; |
| 2854 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 2855 | } |
| 2856 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2857 | static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear) |
| 2858 | { |
| 2859 | u32 ocp_data; |
| 2860 | |
| 2861 | ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS); |
| 2862 | ocp_data &= ~clear; |
| 2863 | ocp_data |= set; |
| 2864 | ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data); |
| 2865 | } |
| 2866 | |
| 2867 | static void r8153b_green_en(struct r8152 *tp, bool enable) |
| 2868 | { |
| 2869 | u16 data; |
| 2870 | |
| 2871 | if (enable) { |
| 2872 | sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ |
| 2873 | sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ |
| 2874 | sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ |
| 2875 | } else { |
| 2876 | sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ |
| 2877 | sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ |
| 2878 | sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ |
| 2879 | } |
| 2880 | |
| 2881 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 2882 | data |= GREEN_ETH_EN; |
| 2883 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 2884 | |
| 2885 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0); |
| 2886 | } |
| 2887 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 2888 | static u16 r8153_phy_status(struct r8152 *tp, u16 desired) |
| 2889 | { |
| 2890 | u16 data; |
| 2891 | int i; |
| 2892 | |
| 2893 | for (i = 0; i < 500; i++) { |
| 2894 | data = ocp_reg_read(tp, OCP_PHY_STATUS); |
| 2895 | data &= PHY_STAT_MASK; |
| 2896 | if (desired) { |
| 2897 | if (data == desired) |
| 2898 | break; |
| 2899 | } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || |
| 2900 | data == PHY_STAT_EXT_INIT) { |
| 2901 | break; |
| 2902 | } |
| 2903 | |
| 2904 | msleep(20); |
| 2905 | } |
| 2906 | |
| 2907 | return data; |
| 2908 | } |
| 2909 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2910 | static void r8153b_ups_en(struct r8152 *tp, bool enable) |
| 2911 | { |
| 2912 | u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2913 | |
| 2914 | if (enable) { |
| 2915 | ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; |
| 2916 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2917 | |
| 2918 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 2919 | ocp_data |= BIT(0); |
| 2920 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 2921 | } else { |
| 2922 | u16 data; |
| 2923 | |
| 2924 | ocp_data &= ~(UPS_EN | USP_PREWAKE); |
| 2925 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2926 | |
| 2927 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 2928 | ocp_data &= ~BIT(0); |
| 2929 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 2930 | |
| 2931 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2932 | ocp_data &= ~PCUT_STATUS; |
| 2933 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2934 | |
| 2935 | data = r8153_phy_status(tp, 0); |
| 2936 | |
| 2937 | switch (data) { |
| 2938 | case PHY_STAT_PWRDN: |
| 2939 | case PHY_STAT_EXT_INIT: |
| 2940 | r8153b_green_en(tp, |
| 2941 | test_bit(GREEN_ETHERNET, &tp->flags)); |
| 2942 | |
| 2943 | data = r8152_mdio_read(tp, MII_BMCR); |
| 2944 | data &= ~BMCR_PDOWN; |
| 2945 | data |= BMCR_RESET; |
| 2946 | r8152_mdio_write(tp, MII_BMCR, data); |
| 2947 | |
| 2948 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 2949 | /* fall through */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2950 | |
| 2951 | default: |
| 2952 | if (data != PHY_STAT_LAN_ON) |
| 2953 | netif_warn(tp, link, tp->netdev, |
| 2954 | "PHY not ready"); |
| 2955 | break; |
| 2956 | } |
| 2957 | } |
| 2958 | } |
| 2959 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 2960 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
| 2961 | { |
| 2962 | u32 ocp_data; |
| 2963 | |
| 2964 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2965 | if (enable) |
| 2966 | ocp_data |= PWR_EN | PHASE2_EN; |
| 2967 | else |
| 2968 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 2969 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2970 | |
| 2971 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2972 | ocp_data &= ~PCUT_STATUS; |
| 2973 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2974 | } |
| 2975 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2976 | static void r8153b_power_cut_en(struct r8152 *tp, bool enable) |
| 2977 | { |
| 2978 | u32 ocp_data; |
| 2979 | |
| 2980 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 2981 | if (enable) |
| 2982 | ocp_data |= PWR_EN | PHASE2_EN; |
| 2983 | else |
| 2984 | ocp_data &= ~PWR_EN; |
| 2985 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 2986 | |
| 2987 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 2988 | ocp_data &= ~PCUT_STATUS; |
| 2989 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 2990 | } |
| 2991 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2992 | static void r8153_queue_wake(struct r8152 *tp, bool enable) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2993 | { |
| 2994 | u32 ocp_data; |
| 2995 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2996 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2997 | if (enable) |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 2998 | ocp_data |= UPCOMING_RUNTIME_D3; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2999 | else |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3000 | ocp_data &= ~UPCOMING_RUNTIME_D3; |
| 3001 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3002 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3003 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); |
| 3004 | ocp_data &= ~LINK_CHG_EVENT; |
| 3005 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); |
| 3006 | |
| 3007 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 3008 | ocp_data &= ~LINK_CHANGE_FLAG; |
| 3009 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3010 | } |
| 3011 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 3012 | static bool rtl_can_wakeup(struct r8152 *tp) |
| 3013 | { |
| 3014 | struct usb_device *udev = tp->udev; |
| 3015 | |
| 3016 | return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); |
| 3017 | } |
| 3018 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3019 | static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) |
| 3020 | { |
| 3021 | if (enable) { |
| 3022 | u32 ocp_data; |
| 3023 | |
| 3024 | __rtl_set_wol(tp, WAKE_ANY); |
| 3025 | |
| 3026 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3027 | |
| 3028 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3029 | ocp_data |= LINK_OFF_WAKE_EN; |
| 3030 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3031 | |
| 3032 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3033 | } else { |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3034 | u32 ocp_data; |
| 3035 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3036 | __rtl_set_wol(tp, tp->saved_wolopts); |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3037 | |
| 3038 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3039 | |
| 3040 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3041 | ocp_data &= ~LINK_OFF_WAKE_EN; |
| 3042 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3043 | |
| 3044 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3045 | } |
| 3046 | } |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3047 | |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3048 | static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) |
| 3049 | { |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3050 | if (enable) { |
| 3051 | r8153_u1u2en(tp, false); |
| 3052 | r8153_u2p3en(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3053 | r8153_mac_clk_spd(tp, true); |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 3054 | rtl_runtime_suspend_enable(tp, true); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3055 | } else { |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 3056 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3057 | r8153_mac_clk_spd(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3058 | |
| 3059 | switch (tp->version) { |
| 3060 | case RTL_VER_03: |
| 3061 | case RTL_VER_04: |
| 3062 | break; |
| 3063 | case RTL_VER_05: |
| 3064 | case RTL_VER_06: |
| 3065 | default: |
| 3066 | r8153_u2p3en(tp, true); |
| 3067 | break; |
| 3068 | } |
| 3069 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3070 | r8153_u1u2en(tp, true); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3074 | static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) |
| 3075 | { |
| 3076 | if (enable) { |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3077 | r8153_queue_wake(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3078 | r8153b_u1u2en(tp, false); |
| 3079 | r8153_u2p3en(tp, false); |
| 3080 | rtl_runtime_suspend_enable(tp, true); |
| 3081 | r8153b_ups_en(tp, true); |
| 3082 | } else { |
| 3083 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3084 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3085 | rtl_runtime_suspend_enable(tp, false); |
| 3086 | r8153_u2p3en(tp, true); |
| 3087 | r8153b_u1u2en(tp, true); |
| 3088 | } |
| 3089 | } |
| 3090 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3091 | static void r8153_teredo_off(struct r8152 *tp) |
| 3092 | { |
| 3093 | u32 ocp_data; |
| 3094 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3095 | switch (tp->version) { |
| 3096 | case RTL_VER_01: |
| 3097 | case RTL_VER_02: |
| 3098 | case RTL_VER_03: |
| 3099 | case RTL_VER_04: |
| 3100 | case RTL_VER_05: |
| 3101 | case RTL_VER_06: |
| 3102 | case RTL_VER_07: |
| 3103 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 3104 | ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | |
| 3105 | OOB_TEREDO_EN); |
| 3106 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 3107 | break; |
| 3108 | |
| 3109 | case RTL_VER_08: |
| 3110 | case RTL_VER_09: |
| 3111 | /* The bit 0 ~ 7 are relative with teredo settings. They are |
| 3112 | * W1C (write 1 to clear), so set all 1 to disable it. |
| 3113 | */ |
| 3114 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); |
| 3115 | break; |
| 3116 | |
| 3117 | default: |
| 3118 | break; |
| 3119 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3120 | |
| 3121 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); |
| 3122 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); |
| 3123 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); |
| 3124 | } |
| 3125 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3126 | static void rtl_reset_bmu(struct r8152 *tp) |
| 3127 | { |
| 3128 | u32 ocp_data; |
| 3129 | |
| 3130 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); |
| 3131 | ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); |
| 3132 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3133 | ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; |
| 3134 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3135 | } |
| 3136 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3137 | static void r8152_aldps_en(struct r8152 *tp, bool enable) |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3138 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3139 | if (enable) { |
| 3140 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 3141 | LINKENA | DIS_SDSAVE); |
| 3142 | } else { |
| 3143 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | |
| 3144 | DIS_SDSAVE); |
| 3145 | msleep(20); |
| 3146 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3147 | } |
| 3148 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3149 | static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) |
| 3150 | { |
| 3151 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); |
| 3152 | ocp_reg_write(tp, OCP_EEE_DATA, reg); |
| 3153 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); |
| 3154 | } |
| 3155 | |
| 3156 | static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) |
| 3157 | { |
| 3158 | u16 data; |
| 3159 | |
| 3160 | r8152_mmd_indirect(tp, dev, reg); |
| 3161 | data = ocp_reg_read(tp, OCP_EEE_DATA); |
| 3162 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 3163 | |
| 3164 | return data; |
| 3165 | } |
| 3166 | |
| 3167 | static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) |
| 3168 | { |
| 3169 | r8152_mmd_indirect(tp, dev, reg); |
| 3170 | ocp_reg_write(tp, OCP_EEE_DATA, data); |
| 3171 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 3172 | } |
| 3173 | |
| 3174 | static void r8152_eee_en(struct r8152 *tp, bool enable) |
| 3175 | { |
| 3176 | u16 config1, config2, config3; |
| 3177 | u32 ocp_data; |
| 3178 | |
| 3179 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3180 | config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; |
| 3181 | config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); |
| 3182 | config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; |
| 3183 | |
| 3184 | if (enable) { |
| 3185 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 3186 | config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; |
| 3187 | config1 |= sd_rise_time(1); |
| 3188 | config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; |
| 3189 | config3 |= fast_snr(42); |
| 3190 | } else { |
| 3191 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 3192 | config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | |
| 3193 | RX_QUIET_EN); |
| 3194 | config1 |= sd_rise_time(7); |
| 3195 | config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); |
| 3196 | config3 |= fast_snr(511); |
| 3197 | } |
| 3198 | |
| 3199 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 3200 | ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); |
| 3201 | ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); |
| 3202 | ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); |
| 3203 | } |
| 3204 | |
| 3205 | static void r8152b_enable_eee(struct r8152 *tp) |
| 3206 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 3207 | if (tp->eee_en) { |
| 3208 | r8152_eee_en(tp, true); |
| 3209 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, tp->eee_adv); |
| 3210 | } else { |
| 3211 | r8152_eee_en(tp, false); |
| 3212 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); |
| 3213 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | static void r8152b_enable_fc(struct r8152 *tp) |
| 3217 | { |
| 3218 | u16 anar; |
| 3219 | |
| 3220 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 3221 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 3222 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 3223 | } |
| 3224 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3225 | static void rtl8152_disable(struct r8152 *tp) |
| 3226 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3227 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3228 | rtl_disable(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3229 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3230 | } |
| 3231 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3232 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 3233 | { |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 3234 | r8152b_enable_eee(tp); |
| 3235 | r8152_aldps_en(tp, true); |
| 3236 | r8152b_enable_fc(tp); |
hayeswang | f0cbe0a | 2014-02-18 21:49:03 +0800 | [diff] [blame] | 3237 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3238 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3239 | } |
| 3240 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3241 | static void r8152b_exit_oob(struct r8152 *tp) |
| 3242 | { |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 3243 | u32 ocp_data; |
| 3244 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3245 | |
| 3246 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3247 | ocp_data &= ~RCR_ACPT_ALL; |
| 3248 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3249 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3250 | rxdy_gated_en(tp, true); |
hayeswang | da9bd11 | 2014-02-18 21:49:08 +0800 | [diff] [blame] | 3251 | r8153_teredo_off(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3252 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3253 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 3254 | |
| 3255 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3256 | ocp_data &= ~NOW_IS_OOB; |
| 3257 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3258 | |
| 3259 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3260 | ocp_data &= ~MCU_BORW_EN; |
| 3261 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3262 | |
| 3263 | for (i = 0; i < 1000; i++) { |
| 3264 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3265 | if (ocp_data & LINK_LIST_READY) |
| 3266 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3267 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3271 | ocp_data |= RE_INIT_LL; |
| 3272 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3273 | |
| 3274 | for (i = 0; i < 1000; i++) { |
| 3275 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3276 | if (ocp_data & LINK_LIST_READY) |
| 3277 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3278 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | rtl8152_nic_reset(tp); |
| 3282 | |
| 3283 | /* rx share fifo credit full threshold */ |
| 3284 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 3285 | |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 3286 | if (tp->udev->speed == USB_SPEED_FULL || |
| 3287 | tp->udev->speed == USB_SPEED_LOW) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3288 | /* rx share fifo credit near full threshold */ |
| 3289 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 3290 | RXFIFO_THR2_FULL); |
| 3291 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 3292 | RXFIFO_THR3_FULL); |
| 3293 | } else { |
| 3294 | /* rx share fifo credit near full threshold */ |
| 3295 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 3296 | RXFIFO_THR2_HIGH); |
| 3297 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 3298 | RXFIFO_THR3_HIGH); |
| 3299 | } |
| 3300 | |
| 3301 | /* TX share fifo free credit full threshold */ |
| 3302 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 3303 | |
| 3304 | ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 3305 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3306 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 3307 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 3308 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3309 | rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3310 | |
| 3311 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 3312 | |
| 3313 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 3314 | ocp_data |= TCR0_AUTO_FIFO; |
| 3315 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 3316 | } |
| 3317 | |
| 3318 | static void r8152b_enter_oob(struct r8152 *tp) |
| 3319 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 3320 | u32 ocp_data; |
| 3321 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3322 | |
| 3323 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3324 | ocp_data &= ~NOW_IS_OOB; |
| 3325 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3326 | |
| 3327 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 3328 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 3329 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 3330 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3331 | rtl_disable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3332 | |
| 3333 | for (i = 0; i < 1000; i++) { |
| 3334 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3335 | if (ocp_data & LINK_LIST_READY) |
| 3336 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3337 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3341 | ocp_data |= RE_INIT_LL; |
| 3342 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3343 | |
| 3344 | for (i = 0; i < 1000; i++) { |
| 3345 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3346 | if (ocp_data & LINK_LIST_READY) |
| 3347 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3348 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 3352 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3353 | rtl_rx_vlan_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3354 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3355 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3356 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3357 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3358 | |
| 3359 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3360 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 3361 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3362 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3363 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3364 | |
| 3365 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3366 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 3367 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3368 | } |
| 3369 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3370 | static int r8153_patch_request(struct r8152 *tp, bool request) |
| 3371 | { |
| 3372 | u16 data; |
| 3373 | int i; |
| 3374 | |
| 3375 | data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); |
| 3376 | if (request) |
| 3377 | data |= PATCH_REQUEST; |
| 3378 | else |
| 3379 | data &= ~PATCH_REQUEST; |
| 3380 | ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); |
| 3381 | |
| 3382 | for (i = 0; request && i < 5000; i++) { |
| 3383 | usleep_range(1000, 2000); |
| 3384 | if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY) |
| 3385 | break; |
| 3386 | } |
| 3387 | |
| 3388 | if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { |
| 3389 | netif_err(tp, drv, tp->netdev, "patch request fail\n"); |
| 3390 | r8153_patch_request(tp, false); |
| 3391 | return -ETIME; |
| 3392 | } else { |
| 3393 | return 0; |
| 3394 | } |
| 3395 | } |
| 3396 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3397 | static void r8153_aldps_en(struct r8152 *tp, bool enable) |
| 3398 | { |
| 3399 | u16 data; |
| 3400 | |
| 3401 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3402 | if (enable) { |
| 3403 | data |= EN_ALDPS; |
| 3404 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 3405 | } else { |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 3406 | int i; |
| 3407 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3408 | data &= ~EN_ALDPS; |
| 3409 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 3410 | for (i = 0; i < 20; i++) { |
| 3411 | usleep_range(1000, 2000); |
| 3412 | if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) |
| 3413 | break; |
| 3414 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3415 | } |
| 3416 | } |
| 3417 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3418 | static void r8153b_aldps_en(struct r8152 *tp, bool enable) |
| 3419 | { |
| 3420 | r8153_aldps_en(tp, enable); |
| 3421 | |
| 3422 | if (enable) |
| 3423 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0); |
| 3424 | else |
| 3425 | r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS); |
| 3426 | } |
| 3427 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 3428 | static void r8153_eee_en(struct r8152 *tp, bool enable) |
| 3429 | { |
| 3430 | u32 ocp_data; |
| 3431 | u16 config; |
| 3432 | |
| 3433 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 3434 | config = ocp_reg_read(tp, OCP_EEE_CFG); |
| 3435 | |
| 3436 | if (enable) { |
| 3437 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 3438 | config |= EEE10_EN; |
| 3439 | } else { |
| 3440 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 3441 | config &= ~EEE10_EN; |
| 3442 | } |
| 3443 | |
| 3444 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 3445 | ocp_reg_write(tp, OCP_EEE_CFG, config); |
| 3446 | } |
| 3447 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3448 | static void r8153b_eee_en(struct r8152 *tp, bool enable) |
| 3449 | { |
| 3450 | r8153_eee_en(tp, enable); |
| 3451 | |
| 3452 | if (enable) |
| 3453 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0); |
| 3454 | else |
| 3455 | r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE); |
| 3456 | } |
| 3457 | |
| 3458 | static void r8153b_enable_fc(struct r8152 *tp) |
| 3459 | { |
| 3460 | r8152b_enable_fc(tp); |
| 3461 | r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0); |
| 3462 | } |
| 3463 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3464 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 3465 | { |
| 3466 | u32 ocp_data; |
| 3467 | u16 data; |
| 3468 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 3469 | /* disable ALDPS before updating the PHY parameters */ |
| 3470 | r8153_aldps_en(tp, false); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 3471 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 3472 | /* disable EEE before updating the PHY parameters */ |
| 3473 | r8153_eee_en(tp, false); |
| 3474 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3475 | |
| 3476 | if (tp->version == RTL_VER_03) { |
| 3477 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 3478 | data &= ~CTAP_SHORT_EN; |
| 3479 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 3480 | } |
| 3481 | |
| 3482 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3483 | data |= EEE_CLKDIV_EN; |
| 3484 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 3485 | |
| 3486 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 3487 | data |= EN_10M_BGOFF; |
| 3488 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 3489 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3490 | data |= EN_10M_PLLOFF; |
| 3491 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 3492 | sram_write(tp, SRAM_IMPEDANCE, 0x0b13); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3493 | |
| 3494 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 3495 | ocp_data |= PFM_PWM_SWITCH; |
| 3496 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 3497 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 3498 | /* Enable LPF corner auto tune */ |
| 3499 | sram_write(tp, SRAM_LPF_CFG, 0xf70f); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3500 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 3501 | /* Adjust 10M Amplitude */ |
| 3502 | sram_write(tp, SRAM_10M_AMP1, 0x00af); |
| 3503 | sram_write(tp, SRAM_10M_AMP2, 0x0208); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3504 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 3505 | if (tp->eee_en) { |
| 3506 | r8153_eee_en(tp, true); |
| 3507 | ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); |
| 3508 | } |
hayeswang | af0287e | 2016-09-20 16:22:08 +0800 | [diff] [blame] | 3509 | |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 3510 | r8153_aldps_en(tp, true); |
| 3511 | r8152b_enable_fc(tp); |
| 3512 | |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3513 | switch (tp->version) { |
| 3514 | case RTL_VER_03: |
| 3515 | case RTL_VER_04: |
| 3516 | break; |
| 3517 | case RTL_VER_05: |
| 3518 | case RTL_VER_06: |
| 3519 | default: |
| 3520 | r8153_u2p3en(tp, true); |
| 3521 | break; |
| 3522 | } |
| 3523 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3524 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3525 | } |
| 3526 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3527 | static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) |
| 3528 | { |
| 3529 | u32 ocp_data; |
| 3530 | |
| 3531 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); |
| 3532 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); |
| 3533 | ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ |
| 3534 | ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); |
| 3535 | |
| 3536 | return ocp_data; |
| 3537 | } |
| 3538 | |
| 3539 | static void r8153b_hw_phy_cfg(struct r8152 *tp) |
| 3540 | { |
| 3541 | u32 ocp_data, ups_flags = 0; |
| 3542 | u16 data; |
| 3543 | |
| 3544 | /* disable ALDPS before updating the PHY parameters */ |
| 3545 | r8153b_aldps_en(tp, false); |
| 3546 | |
| 3547 | /* disable EEE before updating the PHY parameters */ |
| 3548 | r8153b_eee_en(tp, false); |
| 3549 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
| 3550 | |
| 3551 | r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); |
| 3552 | |
| 3553 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 3554 | data |= R_TUNE_EN; |
| 3555 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 3556 | data = ocp_reg_read(tp, OCP_NCTL_CFG); |
| 3557 | data |= PGA_RETURN_EN; |
| 3558 | ocp_reg_write(tp, OCP_NCTL_CFG, data); |
| 3559 | |
| 3560 | /* ADC Bias Calibration: |
| 3561 | * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake |
| 3562 | * bit (bit3) to rebuild the real 16-bit data. Write the data to the |
| 3563 | * ADC ioffset. |
| 3564 | */ |
| 3565 | ocp_data = r8152_efuse_read(tp, 0x7d); |
| 3566 | data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); |
| 3567 | if (data != 0xffff) |
| 3568 | ocp_reg_write(tp, OCP_ADC_IOFFSET, data); |
| 3569 | |
| 3570 | /* ups mode tx-link-pulse timing adjustment: |
| 3571 | * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] |
| 3572 | * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt |
| 3573 | */ |
| 3574 | ocp_data = ocp_reg_read(tp, 0xc426); |
| 3575 | ocp_data &= 0x3fff; |
| 3576 | if (ocp_data) { |
| 3577 | u32 swr_cnt_1ms_ini; |
| 3578 | |
| 3579 | swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; |
| 3580 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); |
| 3581 | ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; |
| 3582 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); |
| 3583 | } |
| 3584 | |
| 3585 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 3586 | ocp_data |= PFM_PWM_SWITCH; |
| 3587 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 3588 | |
| 3589 | /* Advnace EEE */ |
| 3590 | if (!r8153_patch_request(tp, true)) { |
| 3591 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 3592 | data |= EEE_CLKDIV_EN; |
| 3593 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 3594 | |
| 3595 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 3596 | data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; |
| 3597 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 3598 | |
| 3599 | ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); |
| 3600 | ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); |
| 3601 | |
| 3602 | ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV | |
| 3603 | UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN | |
| 3604 | UPS_FLAGS_EEE_PLLOFF_GIGA; |
| 3605 | |
| 3606 | r8153_patch_request(tp, false); |
| 3607 | } |
| 3608 | |
| 3609 | r8153b_ups_flags_w1w0(tp, ups_flags, 0); |
| 3610 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 3611 | if (tp->eee_en) { |
| 3612 | r8153b_eee_en(tp, true); |
| 3613 | ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); |
| 3614 | } |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3615 | |
| 3616 | r8153b_aldps_en(tp, true); |
| 3617 | r8153b_enable_fc(tp); |
| 3618 | r8153_u2p3en(tp, true); |
| 3619 | |
| 3620 | set_bit(PHY_RESET, &tp->flags); |
| 3621 | } |
| 3622 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3623 | static void r8153_first_init(struct r8152 *tp) |
| 3624 | { |
| 3625 | u32 ocp_data; |
| 3626 | int i; |
| 3627 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3628 | r8153_mac_clk_spd(tp, false); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3629 | rxdy_gated_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3630 | r8153_teredo_off(tp); |
| 3631 | |
| 3632 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3633 | ocp_data &= ~RCR_ACPT_ALL; |
| 3634 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3635 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3636 | rtl8152_nic_reset(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3637 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3638 | |
| 3639 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3640 | ocp_data &= ~NOW_IS_OOB; |
| 3641 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3642 | |
| 3643 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3644 | ocp_data &= ~MCU_BORW_EN; |
| 3645 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3646 | |
| 3647 | for (i = 0; i < 1000; i++) { |
| 3648 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3649 | if (ocp_data & LINK_LIST_READY) |
| 3650 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3651 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3655 | ocp_data |= RE_INIT_LL; |
| 3656 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3657 | |
| 3658 | for (i = 0; i < 1000; i++) { |
| 3659 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3660 | if (ocp_data & LINK_LIST_READY) |
| 3661 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3662 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3663 | } |
| 3664 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3665 | rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3666 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 3667 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 3668 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 3669 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3670 | |
| 3671 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 3672 | ocp_data |= TCR0_AUTO_FIFO; |
| 3673 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 3674 | |
| 3675 | rtl8152_nic_reset(tp); |
| 3676 | |
| 3677 | /* rx share fifo credit full threshold */ |
| 3678 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 3679 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 3680 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 3681 | /* TX share fifo free credit full threshold */ |
| 3682 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | static void r8153_enter_oob(struct r8152 *tp) |
| 3686 | { |
| 3687 | u32 ocp_data; |
| 3688 | int i; |
| 3689 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3690 | r8153_mac_clk_spd(tp, true); |
| 3691 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3692 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3693 | ocp_data &= ~NOW_IS_OOB; |
| 3694 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3695 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3696 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3697 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3698 | |
| 3699 | for (i = 0; i < 1000; i++) { |
| 3700 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3701 | if (ocp_data & LINK_LIST_READY) |
| 3702 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3703 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 3707 | ocp_data |= RE_INIT_LL; |
| 3708 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 3709 | |
| 3710 | for (i = 0; i < 1000; i++) { |
| 3711 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3712 | if (ocp_data & LINK_LIST_READY) |
| 3713 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 3714 | usleep_range(1000, 2000); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3715 | } |
| 3716 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 3717 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 3718 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3719 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3720 | switch (tp->version) { |
| 3721 | case RTL_VER_03: |
| 3722 | case RTL_VER_04: |
| 3723 | case RTL_VER_05: |
| 3724 | case RTL_VER_06: |
| 3725 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 3726 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 3727 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 3728 | break; |
| 3729 | |
| 3730 | case RTL_VER_08: |
| 3731 | case RTL_VER_09: |
| 3732 | /* Clear teredo wake event. bit[15:8] is the teredo wakeup |
| 3733 | * type. Set it to zero. bits[7:0] are the W1C bits about |
| 3734 | * the events. Set them to all 1 to clear them. |
| 3735 | */ |
| 3736 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); |
| 3737 | break; |
| 3738 | |
| 3739 | default: |
| 3740 | break; |
| 3741 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3742 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 3743 | rtl_rx_vlan_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3744 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3745 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3746 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 3747 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3748 | |
| 3749 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 3750 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 3751 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 3752 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3753 | rxdy_gated_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3754 | |
| 3755 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 3756 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 3757 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 3758 | } |
| 3759 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3760 | static void rtl8153_disable(struct r8152 *tp) |
| 3761 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3762 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3763 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3764 | rtl_reset_bmu(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3765 | r8153_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3766 | } |
| 3767 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3768 | static void rtl8153b_disable(struct r8152 *tp) |
| 3769 | { |
| 3770 | r8153b_aldps_en(tp, false); |
| 3771 | rtl_disable(tp); |
| 3772 | rtl_reset_bmu(tp); |
| 3773 | r8153b_aldps_en(tp, true); |
| 3774 | } |
| 3775 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3776 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex) |
| 3777 | { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3778 | u16 bmcr, anar, gbcr; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3779 | enum spd_duplex speed_duplex; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3780 | int ret = 0; |
| 3781 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3782 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 3783 | anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 3784 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3785 | if (tp->mii.supports_gmii) { |
| 3786 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 3787 | gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 3788 | } else { |
| 3789 | gbcr = 0; |
| 3790 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3791 | |
| 3792 | if (autoneg == AUTONEG_DISABLE) { |
| 3793 | if (speed == SPEED_10) { |
| 3794 | bmcr = 0; |
| 3795 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3796 | speed_duplex = FORCE_10M_HALF; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3797 | } else if (speed == SPEED_100) { |
| 3798 | bmcr = BMCR_SPEED100; |
| 3799 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3800 | speed_duplex = FORCE_100M_HALF; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3801 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 3802 | bmcr = BMCR_SPEED1000; |
| 3803 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3804 | speed_duplex = NWAY_1000M_FULL; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3805 | } else { |
| 3806 | ret = -EINVAL; |
| 3807 | goto out; |
| 3808 | } |
| 3809 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3810 | if (duplex == DUPLEX_FULL) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3811 | bmcr |= BMCR_FULLDPLX; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3812 | if (speed != SPEED_1000) |
| 3813 | speed_duplex++; |
| 3814 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3815 | } else { |
| 3816 | if (speed == SPEED_10) { |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3817 | if (duplex == DUPLEX_FULL) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3818 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3819 | speed_duplex = NWAY_10M_FULL; |
| 3820 | } else { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3821 | anar |= ADVERTISE_10HALF; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3822 | speed_duplex = NWAY_10M_HALF; |
| 3823 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3824 | } else if (speed == SPEED_100) { |
| 3825 | if (duplex == DUPLEX_FULL) { |
| 3826 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 3827 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3828 | speed_duplex = NWAY_100M_FULL; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3829 | } else { |
| 3830 | anar |= ADVERTISE_10HALF; |
| 3831 | anar |= ADVERTISE_100HALF; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3832 | speed_duplex = NWAY_100M_HALF; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3833 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3834 | } else if (speed == SPEED_1000 && tp->mii.supports_gmii) { |
| 3835 | if (duplex == DUPLEX_FULL) { |
| 3836 | anar |= ADVERTISE_10HALF | ADVERTISE_10FULL; |
| 3837 | anar |= ADVERTISE_100HALF | ADVERTISE_100FULL; |
| 3838 | gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF; |
| 3839 | } else { |
| 3840 | anar |= ADVERTISE_10HALF; |
| 3841 | anar |= ADVERTISE_100HALF; |
| 3842 | gbcr |= ADVERTISE_1000HALF; |
| 3843 | } |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3844 | speed_duplex = NWAY_1000M_FULL; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3845 | } else { |
| 3846 | ret = -EINVAL; |
| 3847 | goto out; |
| 3848 | } |
| 3849 | |
| 3850 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
| 3851 | } |
| 3852 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 3853 | if (test_and_clear_bit(PHY_RESET, &tp->flags)) |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3854 | bmcr |= BMCR_RESET; |
| 3855 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3856 | if (tp->mii.supports_gmii) |
| 3857 | r8152_mdio_write(tp, MII_CTRL1000, gbcr); |
| 3858 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3859 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
| 3860 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 3861 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3862 | switch (tp->version) { |
| 3863 | case RTL_VER_08: |
| 3864 | case RTL_VER_09: |
| 3865 | r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex), |
| 3866 | UPS_FLAGS_SPEED_MASK); |
| 3867 | break; |
| 3868 | |
| 3869 | default: |
| 3870 | break; |
| 3871 | } |
| 3872 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 3873 | if (bmcr & BMCR_RESET) { |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3874 | int i; |
| 3875 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 3876 | for (i = 0; i < 50; i++) { |
| 3877 | msleep(20); |
| 3878 | if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) |
| 3879 | break; |
| 3880 | } |
| 3881 | } |
| 3882 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3883 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3884 | return ret; |
| 3885 | } |
| 3886 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3887 | static void rtl8152_up(struct r8152 *tp) |
| 3888 | { |
| 3889 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3890 | return; |
| 3891 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3892 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3893 | r8152b_exit_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3894 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3895 | } |
| 3896 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3897 | static void rtl8152_down(struct r8152 *tp) |
| 3898 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3899 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 3900 | rtl_drop_queued_tx(tp); |
| 3901 | return; |
| 3902 | } |
| 3903 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 3904 | r8152_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3905 | r8152_aldps_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3906 | r8152b_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3907 | r8152_aldps_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3910 | static void rtl8153_up(struct r8152 *tp) |
| 3911 | { |
| 3912 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3913 | return; |
| 3914 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3915 | r8153_u1u2en(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3916 | r8153_u2p3en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3917 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3918 | r8153_first_init(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3919 | r8153_aldps_en(tp, true); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3920 | |
| 3921 | switch (tp->version) { |
| 3922 | case RTL_VER_03: |
| 3923 | case RTL_VER_04: |
| 3924 | break; |
| 3925 | case RTL_VER_05: |
| 3926 | case RTL_VER_06: |
| 3927 | default: |
| 3928 | r8153_u2p3en(tp, true); |
| 3929 | break; |
| 3930 | } |
| 3931 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3932 | r8153_u1u2en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 3933 | } |
| 3934 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3935 | static void rtl8153_down(struct r8152 *tp) |
| 3936 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 3937 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 3938 | rtl_drop_queued_tx(tp); |
| 3939 | return; |
| 3940 | } |
| 3941 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3942 | r8153_u1u2en(tp, false); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3943 | r8153_u2p3en(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 3944 | r8153_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3945 | r8153_aldps_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3946 | r8153_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 3947 | r8153_aldps_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 3948 | } |
| 3949 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3950 | static void rtl8153b_up(struct r8152 *tp) |
| 3951 | { |
| 3952 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 3953 | return; |
| 3954 | |
| 3955 | r8153b_u1u2en(tp, false); |
| 3956 | r8153_u2p3en(tp, false); |
| 3957 | r8153b_aldps_en(tp, false); |
| 3958 | |
| 3959 | r8153_first_init(tp); |
| 3960 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); |
| 3961 | |
| 3962 | r8153b_aldps_en(tp, true); |
| 3963 | r8153_u2p3en(tp, true); |
| 3964 | r8153b_u1u2en(tp, true); |
| 3965 | } |
| 3966 | |
| 3967 | static void rtl8153b_down(struct r8152 *tp) |
| 3968 | { |
| 3969 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 3970 | rtl_drop_queued_tx(tp); |
| 3971 | return; |
| 3972 | } |
| 3973 | |
| 3974 | r8153b_u1u2en(tp, false); |
| 3975 | r8153_u2p3en(tp, false); |
| 3976 | r8153b_power_cut_en(tp, false); |
| 3977 | r8153b_aldps_en(tp, false); |
| 3978 | r8153_enter_oob(tp); |
| 3979 | r8153b_aldps_en(tp, true); |
| 3980 | } |
| 3981 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 3982 | static bool rtl8152_in_nway(struct r8152 *tp) |
| 3983 | { |
| 3984 | u16 nway_state; |
| 3985 | |
| 3986 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); |
| 3987 | tp->ocp_base = 0x2000; |
| 3988 | ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ |
| 3989 | nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); |
| 3990 | |
| 3991 | /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ |
| 3992 | if (nway_state & 0xc000) |
| 3993 | return false; |
| 3994 | else |
| 3995 | return true; |
| 3996 | } |
| 3997 | |
| 3998 | static bool rtl8153_in_nway(struct r8152 *tp) |
| 3999 | { |
| 4000 | u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; |
| 4001 | |
| 4002 | if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) |
| 4003 | return false; |
| 4004 | else |
| 4005 | return true; |
| 4006 | } |
| 4007 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4008 | static void set_carrier(struct r8152 *tp) |
| 4009 | { |
| 4010 | struct net_device *netdev = tp->netdev; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4011 | struct napi_struct *napi = &tp->napi; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4012 | u8 speed; |
| 4013 | |
| 4014 | speed = rtl8152_get_speed(tp); |
| 4015 | |
| 4016 | if (speed & LINK_STATUS) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 4017 | if (!netif_carrier_ok(netdev)) { |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4018 | tp->rtl_ops.enable(tp); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4019 | netif_stop_queue(netdev); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4020 | napi_disable(napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4021 | netif_carrier_on(netdev); |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 4022 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 4023 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 4024 | _rtl8152_set_rx_mode(netdev); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 4025 | napi_enable(&tp->napi); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4026 | netif_wake_queue(netdev); |
| 4027 | netif_info(tp, link, netdev, "carrier on\n"); |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 4028 | } else if (netif_queue_stopped(netdev) && |
| 4029 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { |
| 4030 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4031 | } |
| 4032 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 4033 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4034 | netif_carrier_off(netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4035 | tasklet_disable(&tp->tx_tl); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4036 | napi_disable(napi); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 4037 | tp->rtl_ops.disable(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4038 | napi_enable(napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4039 | tasklet_enable(&tp->tx_tl); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4040 | netif_info(tp, link, netdev, "carrier off\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4041 | } |
| 4042 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | static void rtl_work_func_t(struct work_struct *work) |
| 4046 | { |
| 4047 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 4048 | |
hayeswang | a1f83fe | 2014-11-12 10:05:05 +0800 | [diff] [blame] | 4049 | /* If the device is unplugged or !netif_running(), the workqueue |
| 4050 | * doesn't need to wake the device, and could return directly. |
| 4051 | */ |
| 4052 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) |
| 4053 | return; |
| 4054 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4055 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 4056 | return; |
| 4057 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4058 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 4059 | goto out1; |
| 4060 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4061 | if (!mutex_trylock(&tp->control)) { |
| 4062 | schedule_delayed_work(&tp->schedule, 0); |
| 4063 | goto out1; |
| 4064 | } |
| 4065 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 4066 | if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 4067 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4068 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 4069 | if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4070 | _rtl8152_set_rx_mode(tp->netdev); |
| 4071 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4072 | /* don't schedule tasket before linking */ |
| 4073 | if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 4074 | netif_carrier_ok(tp->netdev)) |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4075 | tasklet_schedule(&tp->tx_tl); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4076 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4077 | mutex_unlock(&tp->control); |
| 4078 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4079 | out1: |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4080 | usb_autopm_put_interface(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 4083 | static void rtl_hw_phy_work_func_t(struct work_struct *work) |
| 4084 | { |
| 4085 | struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); |
| 4086 | |
| 4087 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4088 | return; |
| 4089 | |
| 4090 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 4091 | return; |
| 4092 | |
| 4093 | mutex_lock(&tp->control); |
| 4094 | |
| 4095 | tp->rtl_ops.hw_phy_cfg(tp); |
| 4096 | |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 4097 | rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex); |
hayeswang | 9d21c0d | 2016-06-13 17:49:37 +0800 | [diff] [blame] | 4098 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 4099 | mutex_unlock(&tp->control); |
| 4100 | |
| 4101 | usb_autopm_put_interface(tp->intf); |
| 4102 | } |
| 4103 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 4104 | #ifdef CONFIG_PM_SLEEP |
| 4105 | static int rtl_notifier(struct notifier_block *nb, unsigned long action, |
| 4106 | void *data) |
| 4107 | { |
| 4108 | struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); |
| 4109 | |
| 4110 | switch (action) { |
| 4111 | case PM_HIBERNATION_PREPARE: |
| 4112 | case PM_SUSPEND_PREPARE: |
| 4113 | usb_autopm_get_interface(tp->intf); |
| 4114 | break; |
| 4115 | |
| 4116 | case PM_POST_HIBERNATION: |
| 4117 | case PM_POST_SUSPEND: |
| 4118 | usb_autopm_put_interface(tp->intf); |
| 4119 | break; |
| 4120 | |
| 4121 | case PM_POST_RESTORE: |
| 4122 | case PM_RESTORE_PREPARE: |
| 4123 | default: |
| 4124 | break; |
| 4125 | } |
| 4126 | |
| 4127 | return NOTIFY_DONE; |
| 4128 | } |
| 4129 | #endif |
| 4130 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4131 | static int rtl8152_open(struct net_device *netdev) |
| 4132 | { |
| 4133 | struct r8152 *tp = netdev_priv(netdev); |
| 4134 | int res = 0; |
| 4135 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4136 | res = alloc_all_mem(tp); |
| 4137 | if (res) |
| 4138 | goto out; |
| 4139 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4140 | res = usb_autopm_get_interface(tp->intf); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4141 | if (res < 0) |
| 4142 | goto out_free; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4143 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4144 | mutex_lock(&tp->control); |
| 4145 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4146 | tp->rtl_ops.up(tp); |
| 4147 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 4148 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4149 | netif_start_queue(netdev); |
| 4150 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 4151 | |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4152 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 4153 | if (res) { |
| 4154 | if (res == -ENODEV) |
| 4155 | netif_device_detach(tp->netdev); |
| 4156 | netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", |
| 4157 | res); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4158 | goto out_unlock; |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4159 | } |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4160 | napi_enable(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4161 | tasklet_enable(&tp->tx_tl); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4162 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4163 | mutex_unlock(&tp->control); |
| 4164 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4165 | usb_autopm_put_interface(tp->intf); |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 4166 | #ifdef CONFIG_PM_SLEEP |
| 4167 | tp->pm_notifier.notifier_call = rtl_notifier; |
| 4168 | register_pm_notifier(&tp->pm_notifier); |
| 4169 | #endif |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4170 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4171 | |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 4172 | out_unlock: |
| 4173 | mutex_unlock(&tp->control); |
| 4174 | usb_autopm_put_interface(tp->intf); |
| 4175 | out_free: |
| 4176 | free_all_mem(tp); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4177 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4178 | return res; |
| 4179 | } |
| 4180 | |
| 4181 | static int rtl8152_close(struct net_device *netdev) |
| 4182 | { |
| 4183 | struct r8152 *tp = netdev_priv(netdev); |
| 4184 | int res = 0; |
| 4185 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 4186 | #ifdef CONFIG_PM_SLEEP |
| 4187 | unregister_pm_notifier(&tp->pm_notifier); |
| 4188 | #endif |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4189 | tasklet_disable(&tp->tx_tl); |
Jiri Slaby | 0ee1f47 | 2018-06-25 09:26:27 +0200 | [diff] [blame] | 4190 | if (!test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4191 | napi_disable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4192 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 4193 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4194 | cancel_delayed_work_sync(&tp->schedule); |
| 4195 | netif_stop_queue(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4196 | |
| 4197 | res = usb_autopm_get_interface(tp->intf); |
hayeswang | 53543db | 2015-02-06 11:30:48 +0800 | [diff] [blame] | 4198 | if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4199 | rtl_drop_queued_tx(tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 4200 | rtl_stop_rx(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4201 | } else { |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4202 | mutex_lock(&tp->control); |
| 4203 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4204 | tp->rtl_ops.down(tp); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4205 | |
| 4206 | mutex_unlock(&tp->control); |
| 4207 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4208 | usb_autopm_put_interface(tp->intf); |
| 4209 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4210 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 4211 | free_all_mem(tp); |
| 4212 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4213 | return res; |
| 4214 | } |
| 4215 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4216 | static void rtl_tally_reset(struct r8152 *tp) |
| 4217 | { |
| 4218 | u32 ocp_data; |
| 4219 | |
| 4220 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); |
| 4221 | ocp_data |= TALLY_RESET; |
| 4222 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); |
| 4223 | } |
| 4224 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4225 | static void r8152b_init(struct r8152 *tp) |
| 4226 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4227 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4228 | u16 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4229 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4230 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4231 | return; |
| 4232 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4233 | data = r8152_mdio_read(tp, MII_BMCR); |
| 4234 | if (data & BMCR_PDOWN) { |
| 4235 | data &= ~BMCR_PDOWN; |
| 4236 | r8152_mdio_write(tp, MII_BMCR, data); |
| 4237 | } |
| 4238 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4239 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4240 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4241 | if (tp->version == RTL_VER_01) { |
| 4242 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 4243 | ocp_data &= ~LED_MODE_MASK; |
| 4244 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 4245 | } |
| 4246 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4247 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4248 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4249 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 4250 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 4251 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 4252 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 4253 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 4254 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 4255 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 4256 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 4257 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 4258 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 4259 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4260 | rtl_tally_reset(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4261 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 4262 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4263 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 4264 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4265 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 4266 | } |
| 4267 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4268 | static void r8153_init(struct r8152 *tp) |
| 4269 | { |
| 4270 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4271 | u16 data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4272 | int i; |
| 4273 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4274 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4275 | return; |
| 4276 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4277 | r8153_u1u2en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4278 | |
| 4279 | for (i = 0; i < 500; i++) { |
| 4280 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 4281 | AUTOLOAD_DONE) |
| 4282 | break; |
| 4283 | msleep(20); |
| 4284 | } |
| 4285 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 4286 | data = r8153_phy_status(tp, 0); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4287 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4288 | if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || |
| 4289 | tp->version == RTL_VER_05) |
| 4290 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 4291 | |
| 4292 | data = r8152_mdio_read(tp, MII_BMCR); |
| 4293 | if (data & BMCR_PDOWN) { |
| 4294 | data &= ~BMCR_PDOWN; |
| 4295 | r8152_mdio_write(tp, MII_BMCR, data); |
| 4296 | } |
| 4297 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 4298 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 4299 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4300 | r8153_u2p3en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4301 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4302 | if (tp->version == RTL_VER_04) { |
| 4303 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); |
| 4304 | ocp_data &= ~pwd_dn_scale_mask; |
| 4305 | ocp_data |= pwd_dn_scale(96); |
| 4306 | ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); |
| 4307 | |
| 4308 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 4309 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 4310 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 4311 | } else if (tp->version == RTL_VER_05) { |
| 4312 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); |
| 4313 | ocp_data &= ~ECM_ALDPS; |
| 4314 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); |
| 4315 | |
| 4316 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 4317 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 4318 | ocp_data &= ~DYNAMIC_BURST; |
| 4319 | else |
| 4320 | ocp_data |= DYNAMIC_BURST; |
| 4321 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 4322 | } else if (tp->version == RTL_VER_06) { |
| 4323 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 4324 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 4325 | ocp_data &= ~DYNAMIC_BURST; |
| 4326 | else |
| 4327 | ocp_data |= DYNAMIC_BURST; |
| 4328 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); |
| 4332 | ocp_data |= EP4_FULL_FC; |
| 4333 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); |
| 4334 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4335 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 4336 | ocp_data &= ~TIMER11_EN; |
| 4337 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 4338 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4339 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 4340 | ocp_data &= ~LED_MODE_MASK; |
| 4341 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 4342 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4343 | ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; |
Oliver Neukum | 2b84af94a | 2016-05-02 13:06:14 +0200 | [diff] [blame] | 4344 | if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4345 | ocp_data |= LPM_TIMER_500MS; |
hayeswang | 34203e2 | 2015-02-06 11:30:46 +0800 | [diff] [blame] | 4346 | else |
| 4347 | ocp_data |= LPM_TIMER_500US; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4348 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 4349 | |
| 4350 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 4351 | ocp_data &= ~SEN_VAL_MASK; |
| 4352 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 4353 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 4354 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 4355 | ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); |
| 4356 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4357 | r8153_power_cut_en(tp, false); |
| 4358 | r8153_u1u2en(tp, true); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 4359 | r8153_mac_clk_spd(tp, false); |
hayeswang | ee4761c | 2017-06-09 17:11:39 +0800 | [diff] [blame] | 4360 | usb_enable_lpm(tp->udev); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4361 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 4362 | /* rx aggregation */ |
| 4363 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 4364 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 4365 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 4366 | ocp_data |= RX_AGG_DISABLE; |
| 4367 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 4368 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4369 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4370 | rtl_tally_reset(tp); |
hayeswang | 49d1034 | 2017-06-09 17:11:44 +0800 | [diff] [blame] | 4371 | |
| 4372 | switch (tp->udev->speed) { |
| 4373 | case USB_SPEED_SUPER: |
| 4374 | case USB_SPEED_SUPER_PLUS: |
| 4375 | tp->coalesce = COALESCE_SUPER; |
| 4376 | break; |
| 4377 | case USB_SPEED_HIGH: |
| 4378 | tp->coalesce = COALESCE_HIGH; |
| 4379 | break; |
| 4380 | default: |
| 4381 | tp->coalesce = COALESCE_SLOW; |
| 4382 | break; |
| 4383 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4384 | } |
| 4385 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4386 | static void r8153b_init(struct r8152 *tp) |
| 4387 | { |
| 4388 | u32 ocp_data; |
| 4389 | u16 data; |
| 4390 | int i; |
| 4391 | |
| 4392 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4393 | return; |
| 4394 | |
| 4395 | r8153b_u1u2en(tp, false); |
| 4396 | |
| 4397 | for (i = 0; i < 500; i++) { |
| 4398 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 4399 | AUTOLOAD_DONE) |
| 4400 | break; |
| 4401 | msleep(20); |
| 4402 | } |
| 4403 | |
| 4404 | data = r8153_phy_status(tp, 0); |
| 4405 | |
| 4406 | data = r8152_mdio_read(tp, MII_BMCR); |
| 4407 | if (data & BMCR_PDOWN) { |
| 4408 | data &= ~BMCR_PDOWN; |
| 4409 | r8152_mdio_write(tp, MII_BMCR, data); |
| 4410 | } |
| 4411 | |
| 4412 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
| 4413 | |
| 4414 | r8153_u2p3en(tp, false); |
| 4415 | |
| 4416 | /* MSC timer = 0xfff * 8ms = 32760 ms */ |
| 4417 | ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); |
| 4418 | |
| 4419 | /* U1/U2/L1 idle timer. 500 us */ |
| 4420 | ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); |
| 4421 | |
| 4422 | r8153b_power_cut_en(tp, false); |
| 4423 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 4424 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4425 | rtl_runtime_suspend_enable(tp, false); |
| 4426 | r8153b_u1u2en(tp, true); |
| 4427 | usb_enable_lpm(tp->udev); |
| 4428 | |
| 4429 | /* MAC clock speed down */ |
| 4430 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); |
| 4431 | ocp_data |= MAC_CLK_SPDWN_EN; |
| 4432 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); |
| 4433 | |
| 4434 | set_bit(GREEN_ETHERNET, &tp->flags); |
| 4435 | |
| 4436 | /* rx aggregation */ |
| 4437 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 4438 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 4439 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 4440 | |
| 4441 | rtl_tally_reset(tp); |
| 4442 | |
| 4443 | tp->coalesce = 15000; /* 15 us */ |
| 4444 | } |
| 4445 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4446 | static int rtl8152_pre_reset(struct usb_interface *intf) |
| 4447 | { |
| 4448 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4449 | struct net_device *netdev; |
| 4450 | |
| 4451 | if (!tp) |
| 4452 | return 0; |
| 4453 | |
| 4454 | netdev = tp->netdev; |
| 4455 | if (!netif_running(netdev)) |
| 4456 | return 0; |
| 4457 | |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4458 | netif_stop_queue(netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4459 | tasklet_disable(&tp->tx_tl); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4460 | napi_disable(&tp->napi); |
| 4461 | clear_bit(WORK_ENABLE, &tp->flags); |
| 4462 | usb_kill_urb(tp->intr_urb); |
| 4463 | cancel_delayed_work_sync(&tp->schedule); |
| 4464 | if (netif_carrier_ok(netdev)) { |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4465 | mutex_lock(&tp->control); |
| 4466 | tp->rtl_ops.disable(tp); |
| 4467 | mutex_unlock(&tp->control); |
| 4468 | } |
| 4469 | |
| 4470 | return 0; |
| 4471 | } |
| 4472 | |
| 4473 | static int rtl8152_post_reset(struct usb_interface *intf) |
| 4474 | { |
| 4475 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4476 | struct net_device *netdev; |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 4477 | struct sockaddr sa; |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4478 | |
| 4479 | if (!tp) |
| 4480 | return 0; |
| 4481 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 4482 | /* reset the MAC adddress in case of policy change */ |
| 4483 | if (determine_ethernet_addr(tp, &sa) >= 0) { |
| 4484 | rtnl_lock(); |
| 4485 | dev_set_mac_address (tp->netdev, &sa, NULL); |
| 4486 | rtnl_unlock(); |
| 4487 | } |
| 4488 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4489 | netdev = tp->netdev; |
| 4490 | if (!netif_running(netdev)) |
| 4491 | return 0; |
| 4492 | |
| 4493 | set_bit(WORK_ENABLE, &tp->flags); |
| 4494 | if (netif_carrier_ok(netdev)) { |
| 4495 | mutex_lock(&tp->control); |
| 4496 | tp->rtl_ops.enable(tp); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 4497 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 4498 | _rtl8152_set_rx_mode(netdev); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4499 | mutex_unlock(&tp->control); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | napi_enable(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4503 | tasklet_enable(&tp->tx_tl); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 4504 | netif_wake_queue(netdev); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 4505 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4506 | |
hayeswang | 7489bda | 2017-01-26 09:38:34 +0800 | [diff] [blame] | 4507 | if (!list_empty(&tp->rx_done)) |
| 4508 | napi_schedule(&tp->napi); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 4509 | |
| 4510 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 4513 | static bool delay_autosuspend(struct r8152 *tp) |
| 4514 | { |
| 4515 | bool sw_linking = !!netif_carrier_ok(tp->netdev); |
| 4516 | bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); |
| 4517 | |
| 4518 | /* This means a linking change occurs and the driver doesn't detect it, |
| 4519 | * yet. If the driver has disabled tx/rx and hw is linking on, the |
| 4520 | * device wouldn't wake up by receiving any packet. |
| 4521 | */ |
| 4522 | if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) |
| 4523 | return true; |
| 4524 | |
| 4525 | /* If the linking down is occurred by nway, the device may miss the |
| 4526 | * linking change event. And it wouldn't wake when linking on. |
| 4527 | */ |
| 4528 | if (!sw_linking && tp->rtl_ops.in_nway(tp)) |
| 4529 | return true; |
hayeswang | 6a0b76c | 2017-01-23 14:18:43 +0800 | [diff] [blame] | 4530 | else if (!skb_queue_empty(&tp->tx_queue)) |
| 4531 | return true; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 4532 | else |
| 4533 | return false; |
| 4534 | } |
| 4535 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4536 | static int rtl8152_runtime_resume(struct r8152 *tp) |
| 4537 | { |
| 4538 | struct net_device *netdev = tp->netdev; |
| 4539 | |
| 4540 | if (netif_running(netdev) && netdev->flags & IFF_UP) { |
| 4541 | struct napi_struct *napi = &tp->napi; |
| 4542 | |
| 4543 | tp->rtl_ops.autosuspend_en(tp, false); |
| 4544 | napi_disable(napi); |
| 4545 | set_bit(WORK_ENABLE, &tp->flags); |
| 4546 | |
| 4547 | if (netif_carrier_ok(netdev)) { |
| 4548 | if (rtl8152_get_speed(tp) & LINK_STATUS) { |
| 4549 | rtl_start_rx(tp); |
| 4550 | } else { |
| 4551 | netif_carrier_off(netdev); |
| 4552 | tp->rtl_ops.disable(tp); |
| 4553 | netif_info(tp, link, netdev, "linking down\n"); |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | napi_enable(napi); |
| 4558 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4559 | smp_mb__after_atomic(); |
| 4560 | |
| 4561 | if (!list_empty(&tp->rx_done)) |
| 4562 | napi_schedule(&tp->napi); |
| 4563 | |
| 4564 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 4565 | } else { |
| 4566 | if (netdev->flags & IFF_UP) |
| 4567 | tp->rtl_ops.autosuspend_en(tp, false); |
| 4568 | |
| 4569 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4570 | } |
| 4571 | |
| 4572 | return 0; |
| 4573 | } |
| 4574 | |
| 4575 | static int rtl8152_system_resume(struct r8152 *tp) |
| 4576 | { |
| 4577 | struct net_device *netdev = tp->netdev; |
| 4578 | |
| 4579 | netif_device_attach(netdev); |
| 4580 | |
| 4581 | if (netif_running(netdev) && netdev->flags & IFF_UP) { |
| 4582 | tp->rtl_ops.up(tp); |
| 4583 | netif_carrier_off(netdev); |
| 4584 | set_bit(WORK_ENABLE, &tp->flags); |
| 4585 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 4586 | } |
| 4587 | |
| 4588 | return 0; |
| 4589 | } |
| 4590 | |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 4591 | static int rtl8152_runtime_suspend(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4592 | { |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 4593 | struct net_device *netdev = tp->netdev; |
| 4594 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4595 | |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 4596 | set_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4597 | smp_mb__after_atomic(); |
| 4598 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4599 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4600 | u32 rcr = 0; |
| 4601 | |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4602 | if (netif_carrier_ok(netdev)) { |
| 4603 | u32 ocp_data; |
| 4604 | |
| 4605 | rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4606 | ocp_data = rcr & ~RCR_ACPT_ALL; |
| 4607 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4608 | rxdy_gated_en(tp, true); |
| 4609 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, |
| 4610 | PLA_OOB_CTRL); |
| 4611 | if (!(ocp_data & RXFIFO_EMPTY)) { |
| 4612 | rxdy_gated_en(tp, false); |
| 4613 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 4614 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 4615 | smp_mb__after_atomic(); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4616 | ret = -EBUSY; |
| 4617 | goto out1; |
| 4618 | } |
| 4619 | } |
| 4620 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4621 | clear_bit(WORK_ENABLE, &tp->flags); |
| 4622 | usb_kill_urb(tp->intr_urb); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4623 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4624 | tp->rtl_ops.autosuspend_en(tp, true); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4625 | |
| 4626 | if (netif_carrier_ok(netdev)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4627 | struct napi_struct *napi = &tp->napi; |
| 4628 | |
| 4629 | napi_disable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4630 | rtl_stop_rx(tp); |
| 4631 | rxdy_gated_en(tp, false); |
| 4632 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4633 | napi_enable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 4634 | } |
hayeswang | bd88298 | 2017-06-13 15:14:40 +0800 | [diff] [blame] | 4635 | |
| 4636 | if (delay_autosuspend(tp)) { |
| 4637 | rtl8152_runtime_resume(tp); |
| 4638 | ret = -EBUSY; |
| 4639 | } |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 4640 | } |
| 4641 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4642 | out1: |
| 4643 | return ret; |
| 4644 | } |
| 4645 | |
| 4646 | static int rtl8152_system_suspend(struct r8152 *tp) |
| 4647 | { |
| 4648 | struct net_device *netdev = tp->netdev; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4649 | |
| 4650 | netif_device_detach(netdev); |
| 4651 | |
hayeswang | e3bd1a8 | 2014-10-29 11:12:17 +0800 | [diff] [blame] | 4652 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4653 | struct napi_struct *napi = &tp->napi; |
| 4654 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4655 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 4656 | usb_kill_urb(tp->intr_urb); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4657 | tasklet_disable(&tp->tx_tl); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4658 | napi_disable(napi); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4659 | cancel_delayed_work_sync(&tp->schedule); |
| 4660 | tp->rtl_ops.down(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 4661 | napi_enable(napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 4662 | tasklet_enable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4663 | } |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4664 | |
zhong jiang | f741917 | 2018-08-09 09:39:13 +0800 | [diff] [blame] | 4665 | return 0; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 4669 | { |
| 4670 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4671 | int ret; |
| 4672 | |
| 4673 | mutex_lock(&tp->control); |
| 4674 | |
| 4675 | if (PMSG_IS_AUTO(message)) |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 4676 | ret = rtl8152_runtime_suspend(tp); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 4677 | else |
| 4678 | ret = rtl8152_system_suspend(tp); |
| 4679 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4680 | mutex_unlock(&tp->control); |
| 4681 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 4682 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | static int rtl8152_resume(struct usb_interface *intf) |
| 4686 | { |
| 4687 | struct r8152 *tp = usb_get_intfdata(intf); |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4688 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4689 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4690 | mutex_lock(&tp->control); |
| 4691 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4692 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) |
| 4693 | ret = rtl8152_runtime_resume(tp); |
| 4694 | else |
| 4695 | ret = rtl8152_system_resume(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4696 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4697 | mutex_unlock(&tp->control); |
| 4698 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 4699 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 4702 | static int rtl8152_reset_resume(struct usb_interface *intf) |
| 4703 | { |
| 4704 | struct r8152 *tp = usb_get_intfdata(intf); |
| 4705 | |
| 4706 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
hayeswang | befb2de | 2017-06-09 17:11:45 +0800 | [diff] [blame] | 4707 | mutex_lock(&tp->control); |
| 4708 | tp->rtl_ops.init(tp); |
| 4709 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
| 4710 | mutex_unlock(&tp->control); |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 4711 | return rtl8152_resume(intf); |
| 4712 | } |
| 4713 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4714 | static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 4715 | { |
| 4716 | struct r8152 *tp = netdev_priv(dev); |
| 4717 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4718 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 4719 | return; |
| 4720 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 4721 | if (!rtl_can_wakeup(tp)) { |
| 4722 | wol->supported = 0; |
| 4723 | wol->wolopts = 0; |
| 4724 | } else { |
| 4725 | mutex_lock(&tp->control); |
| 4726 | wol->supported = WAKE_ANY; |
| 4727 | wol->wolopts = __rtl_get_wol(tp); |
| 4728 | mutex_unlock(&tp->control); |
| 4729 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4730 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4731 | usb_autopm_put_interface(tp->intf); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 4735 | { |
| 4736 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4737 | int ret; |
| 4738 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 4739 | if (!rtl_can_wakeup(tp)) |
| 4740 | return -EOPNOTSUPP; |
| 4741 | |
Florian Fainelli | f2750df | 2018-09-28 16:18:54 -0700 | [diff] [blame] | 4742 | if (wol->wolopts & ~WAKE_ANY) |
| 4743 | return -EINVAL; |
| 4744 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4745 | ret = usb_autopm_get_interface(tp->intf); |
| 4746 | if (ret < 0) |
| 4747 | goto out_set_wol; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4748 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4749 | mutex_lock(&tp->control); |
| 4750 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4751 | __rtl_set_wol(tp, wol->wolopts); |
| 4752 | tp->saved_wolopts = wol->wolopts & WAKE_ANY; |
| 4753 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4754 | mutex_unlock(&tp->control); |
| 4755 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4756 | usb_autopm_put_interface(tp->intf); |
| 4757 | |
| 4758 | out_set_wol: |
| 4759 | return ret; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 4760 | } |
| 4761 | |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 4762 | static u32 rtl8152_get_msglevel(struct net_device *dev) |
| 4763 | { |
| 4764 | struct r8152 *tp = netdev_priv(dev); |
| 4765 | |
| 4766 | return tp->msg_enable; |
| 4767 | } |
| 4768 | |
| 4769 | static void rtl8152_set_msglevel(struct net_device *dev, u32 value) |
| 4770 | { |
| 4771 | struct r8152 *tp = netdev_priv(dev); |
| 4772 | |
| 4773 | tp->msg_enable = value; |
| 4774 | } |
| 4775 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4776 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 4777 | struct ethtool_drvinfo *info) |
| 4778 | { |
| 4779 | struct r8152 *tp = netdev_priv(netdev); |
| 4780 | |
hayeswang | b0b46c7 | 2014-08-26 10:08:23 +0800 | [diff] [blame] | 4781 | strlcpy(info->driver, MODULENAME, sizeof(info->driver)); |
| 4782 | strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4783 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
| 4784 | } |
| 4785 | |
| 4786 | static |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4787 | int rtl8152_get_link_ksettings(struct net_device *netdev, |
| 4788 | struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4789 | { |
| 4790 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4791 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4792 | |
| 4793 | if (!tp->mii.mdio_read) |
| 4794 | return -EOPNOTSUPP; |
| 4795 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4796 | ret = usb_autopm_get_interface(tp->intf); |
| 4797 | if (ret < 0) |
| 4798 | goto out; |
| 4799 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4800 | mutex_lock(&tp->control); |
| 4801 | |
yuval.shaia@oracle.com | 82c01a8 | 2017-06-04 20:22:00 +0300 | [diff] [blame] | 4802 | mii_ethtool_get_link_ksettings(&tp->mii, cmd); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4803 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4804 | mutex_unlock(&tp->control); |
| 4805 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 4806 | usb_autopm_put_interface(tp->intf); |
| 4807 | |
| 4808 | out: |
| 4809 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4812 | static int rtl8152_set_link_ksettings(struct net_device *dev, |
| 4813 | const struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4814 | { |
| 4815 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4816 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4817 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4818 | ret = usb_autopm_get_interface(tp->intf); |
| 4819 | if (ret < 0) |
| 4820 | goto out; |
| 4821 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4822 | mutex_lock(&tp->control); |
| 4823 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4824 | ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, |
| 4825 | cmd->base.duplex); |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 4826 | if (!ret) { |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 4827 | tp->autoneg = cmd->base.autoneg; |
| 4828 | tp->speed = cmd->base.speed; |
| 4829 | tp->duplex = cmd->base.duplex; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 4830 | } |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4831 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 4832 | mutex_unlock(&tp->control); |
| 4833 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 4834 | usb_autopm_put_interface(tp->intf); |
| 4835 | |
| 4836 | out: |
| 4837 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4840 | static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { |
| 4841 | "tx_packets", |
| 4842 | "rx_packets", |
| 4843 | "tx_errors", |
| 4844 | "rx_errors", |
| 4845 | "rx_missed", |
| 4846 | "align_errors", |
| 4847 | "tx_single_collisions", |
| 4848 | "tx_multi_collisions", |
| 4849 | "rx_unicast", |
| 4850 | "rx_broadcast", |
| 4851 | "rx_multicast", |
| 4852 | "tx_aborted", |
| 4853 | "tx_underrun", |
| 4854 | }; |
| 4855 | |
| 4856 | static int rtl8152_get_sset_count(struct net_device *dev, int sset) |
| 4857 | { |
| 4858 | switch (sset) { |
| 4859 | case ETH_SS_STATS: |
| 4860 | return ARRAY_SIZE(rtl8152_gstrings); |
| 4861 | default: |
| 4862 | return -EOPNOTSUPP; |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | static void rtl8152_get_ethtool_stats(struct net_device *dev, |
| 4867 | struct ethtool_stats *stats, u64 *data) |
| 4868 | { |
| 4869 | struct r8152 *tp = netdev_priv(dev); |
| 4870 | struct tally_counter tally; |
| 4871 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 4872 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 4873 | return; |
| 4874 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4875 | generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); |
| 4876 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 4877 | usb_autopm_put_interface(tp->intf); |
| 4878 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4879 | data[0] = le64_to_cpu(tally.tx_packets); |
| 4880 | data[1] = le64_to_cpu(tally.rx_packets); |
| 4881 | data[2] = le64_to_cpu(tally.tx_errors); |
| 4882 | data[3] = le32_to_cpu(tally.rx_errors); |
| 4883 | data[4] = le16_to_cpu(tally.rx_missed); |
| 4884 | data[5] = le16_to_cpu(tally.align_errors); |
| 4885 | data[6] = le32_to_cpu(tally.tx_one_collision); |
| 4886 | data[7] = le32_to_cpu(tally.tx_multi_collision); |
| 4887 | data[8] = le64_to_cpu(tally.rx_unicast); |
| 4888 | data[9] = le64_to_cpu(tally.rx_broadcast); |
| 4889 | data[10] = le32_to_cpu(tally.rx_multicast); |
| 4890 | data[11] = le16_to_cpu(tally.tx_aborted); |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 4891 | data[12] = le16_to_cpu(tally.tx_underrun); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 4895 | { |
| 4896 | switch (stringset) { |
| 4897 | case ETH_SS_STATS: |
| 4898 | memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings)); |
| 4899 | break; |
| 4900 | } |
| 4901 | } |
| 4902 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4903 | static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4904 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4905 | u32 lp, adv, supported = 0; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4906 | u16 val; |
| 4907 | |
| 4908 | val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
| 4909 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 4910 | |
| 4911 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
| 4912 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4913 | |
| 4914 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
| 4915 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4916 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4917 | eee->eee_enabled = tp->eee_en; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4918 | eee->eee_active = !!(supported & adv & lp); |
| 4919 | eee->supported = supported; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4920 | eee->advertised = tp->eee_adv; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4921 | eee->lp_advertised = lp; |
| 4922 | |
| 4923 | return 0; |
| 4924 | } |
| 4925 | |
| 4926 | static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4927 | { |
| 4928 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 4929 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4930 | tp->eee_en = eee->eee_enabled; |
| 4931 | tp->eee_adv = val; |
| 4932 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4933 | r8152_eee_en(tp, eee->eee_enabled); |
| 4934 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4935 | if (eee->eee_enabled) |
| 4936 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val); |
| 4937 | else |
| 4938 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4939 | |
| 4940 | return 0; |
| 4941 | } |
| 4942 | |
| 4943 | static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4944 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4945 | u32 lp, adv, supported = 0; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4946 | u16 val; |
| 4947 | |
| 4948 | val = ocp_reg_read(tp, OCP_EEE_ABLE); |
| 4949 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 4950 | |
| 4951 | val = ocp_reg_read(tp, OCP_EEE_ADV); |
| 4952 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4953 | |
| 4954 | val = ocp_reg_read(tp, OCP_EEE_LPABLE); |
| 4955 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 4956 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4957 | eee->eee_enabled = tp->eee_en; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4958 | eee->eee_active = !!(supported & adv & lp); |
| 4959 | eee->supported = supported; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4960 | eee->advertised = tp->eee_adv; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4961 | eee->lp_advertised = lp; |
| 4962 | |
| 4963 | return 0; |
| 4964 | } |
| 4965 | |
| 4966 | static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4967 | { |
| 4968 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 4969 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4970 | tp->eee_en = eee->eee_enabled; |
| 4971 | tp->eee_adv = val; |
| 4972 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4973 | r8153_eee_en(tp, eee->eee_enabled); |
| 4974 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4975 | if (eee->eee_enabled) |
| 4976 | ocp_reg_write(tp, OCP_EEE_ADV, val); |
| 4977 | else |
| 4978 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 4979 | |
| 4980 | return 0; |
| 4981 | } |
| 4982 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4983 | static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 4984 | { |
| 4985 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 4986 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4987 | tp->eee_en = eee->eee_enabled; |
| 4988 | tp->eee_adv = val; |
| 4989 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4990 | r8153b_eee_en(tp, eee->eee_enabled); |
| 4991 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 4992 | if (eee->eee_enabled) |
| 4993 | ocp_reg_write(tp, OCP_EEE_ADV, val); |
| 4994 | else |
| 4995 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4996 | |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5000 | static int |
| 5001 | rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) |
| 5002 | { |
| 5003 | struct r8152 *tp = netdev_priv(net); |
| 5004 | int ret; |
| 5005 | |
| 5006 | ret = usb_autopm_get_interface(tp->intf); |
| 5007 | if (ret < 0) |
| 5008 | goto out; |
| 5009 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5010 | mutex_lock(&tp->control); |
| 5011 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5012 | ret = tp->rtl_ops.eee_get(tp, edata); |
| 5013 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5014 | mutex_unlock(&tp->control); |
| 5015 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5016 | usb_autopm_put_interface(tp->intf); |
| 5017 | |
| 5018 | out: |
| 5019 | return ret; |
| 5020 | } |
| 5021 | |
| 5022 | static int |
| 5023 | rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) |
| 5024 | { |
| 5025 | struct r8152 *tp = netdev_priv(net); |
| 5026 | int ret; |
| 5027 | |
| 5028 | ret = usb_autopm_get_interface(tp->intf); |
| 5029 | if (ret < 0) |
| 5030 | goto out; |
| 5031 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5032 | mutex_lock(&tp->control); |
| 5033 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5034 | ret = tp->rtl_ops.eee_set(tp, edata); |
hayeswang | 9d31a7b | 2014-10-06 10:36:04 +0800 | [diff] [blame] | 5035 | if (!ret) |
| 5036 | ret = mii_nway_restart(&tp->mii); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5037 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5038 | mutex_unlock(&tp->control); |
| 5039 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5040 | usb_autopm_put_interface(tp->intf); |
| 5041 | |
| 5042 | out: |
| 5043 | return ret; |
| 5044 | } |
| 5045 | |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 5046 | static int rtl8152_nway_reset(struct net_device *dev) |
| 5047 | { |
| 5048 | struct r8152 *tp = netdev_priv(dev); |
| 5049 | int ret; |
| 5050 | |
| 5051 | ret = usb_autopm_get_interface(tp->intf); |
| 5052 | if (ret < 0) |
| 5053 | goto out; |
| 5054 | |
| 5055 | mutex_lock(&tp->control); |
| 5056 | |
| 5057 | ret = mii_nway_restart(&tp->mii); |
| 5058 | |
| 5059 | mutex_unlock(&tp->control); |
| 5060 | |
| 5061 | usb_autopm_put_interface(tp->intf); |
| 5062 | |
| 5063 | out: |
| 5064 | return ret; |
| 5065 | } |
| 5066 | |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5067 | static int rtl8152_get_coalesce(struct net_device *netdev, |
| 5068 | struct ethtool_coalesce *coalesce) |
| 5069 | { |
| 5070 | struct r8152 *tp = netdev_priv(netdev); |
| 5071 | |
| 5072 | switch (tp->version) { |
| 5073 | case RTL_VER_01: |
| 5074 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5075 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5076 | return -EOPNOTSUPP; |
| 5077 | default: |
| 5078 | break; |
| 5079 | } |
| 5080 | |
| 5081 | coalesce->rx_coalesce_usecs = tp->coalesce; |
| 5082 | |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | static int rtl8152_set_coalesce(struct net_device *netdev, |
| 5087 | struct ethtool_coalesce *coalesce) |
| 5088 | { |
| 5089 | struct r8152 *tp = netdev_priv(netdev); |
| 5090 | int ret; |
| 5091 | |
| 5092 | switch (tp->version) { |
| 5093 | case RTL_VER_01: |
| 5094 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5095 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5096 | return -EOPNOTSUPP; |
| 5097 | default: |
| 5098 | break; |
| 5099 | } |
| 5100 | |
| 5101 | if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) |
| 5102 | return -EINVAL; |
| 5103 | |
| 5104 | ret = usb_autopm_get_interface(tp->intf); |
| 5105 | if (ret < 0) |
| 5106 | return ret; |
| 5107 | |
| 5108 | mutex_lock(&tp->control); |
| 5109 | |
| 5110 | if (tp->coalesce != coalesce->rx_coalesce_usecs) { |
| 5111 | tp->coalesce = coalesce->rx_coalesce_usecs; |
| 5112 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 5113 | if (netif_running(netdev) && netif_carrier_ok(netdev)) { |
| 5114 | netif_stop_queue(netdev); |
| 5115 | napi_disable(&tp->napi); |
| 5116 | tp->rtl_ops.disable(tp); |
| 5117 | tp->rtl_ops.enable(tp); |
| 5118 | rtl_start_rx(tp); |
| 5119 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 5120 | _rtl8152_set_rx_mode(netdev); |
| 5121 | napi_enable(&tp->napi); |
| 5122 | netif_wake_queue(netdev); |
| 5123 | } |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | mutex_unlock(&tp->control); |
| 5127 | |
| 5128 | usb_autopm_put_interface(tp->intf); |
| 5129 | |
| 5130 | return ret; |
| 5131 | } |
| 5132 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 5133 | static int rtl8152_get_tunable(struct net_device *netdev, |
| 5134 | const struct ethtool_tunable *tunable, void *d) |
| 5135 | { |
| 5136 | struct r8152 *tp = netdev_priv(netdev); |
| 5137 | |
| 5138 | switch (tunable->id) { |
| 5139 | case ETHTOOL_RX_COPYBREAK: |
| 5140 | *(u32 *)d = tp->rx_copybreak; |
| 5141 | break; |
| 5142 | default: |
| 5143 | return -EOPNOTSUPP; |
| 5144 | } |
| 5145 | |
| 5146 | return 0; |
| 5147 | } |
| 5148 | |
| 5149 | static int rtl8152_set_tunable(struct net_device *netdev, |
| 5150 | const struct ethtool_tunable *tunable, |
| 5151 | const void *d) |
| 5152 | { |
| 5153 | struct r8152 *tp = netdev_priv(netdev); |
| 5154 | u32 val; |
| 5155 | |
| 5156 | switch (tunable->id) { |
| 5157 | case ETHTOOL_RX_COPYBREAK: |
| 5158 | val = *(u32 *)d; |
| 5159 | if (val < ETH_ZLEN) { |
| 5160 | netif_err(tp, rx_err, netdev, |
| 5161 | "Invalid rx copy break value\n"); |
| 5162 | return -EINVAL; |
| 5163 | } |
| 5164 | |
| 5165 | if (tp->rx_copybreak != val) { |
| 5166 | napi_disable(&tp->napi); |
| 5167 | tp->rx_copybreak = val; |
| 5168 | napi_enable(&tp->napi); |
| 5169 | } |
| 5170 | break; |
| 5171 | default: |
| 5172 | return -EOPNOTSUPP; |
| 5173 | } |
| 5174 | |
| 5175 | return 0; |
| 5176 | } |
| 5177 | |
| 5178 | static void rtl8152_get_ringparam(struct net_device *netdev, |
| 5179 | struct ethtool_ringparam *ring) |
| 5180 | { |
| 5181 | struct r8152 *tp = netdev_priv(netdev); |
| 5182 | |
| 5183 | ring->rx_max_pending = RTL8152_RX_MAX_PENDING; |
| 5184 | ring->rx_pending = tp->rx_pending; |
| 5185 | } |
| 5186 | |
| 5187 | static int rtl8152_set_ringparam(struct net_device *netdev, |
| 5188 | struct ethtool_ringparam *ring) |
| 5189 | { |
| 5190 | struct r8152 *tp = netdev_priv(netdev); |
| 5191 | |
| 5192 | if (ring->rx_pending < (RTL8152_MAX_RX * 2)) |
| 5193 | return -EINVAL; |
| 5194 | |
| 5195 | if (tp->rx_pending != ring->rx_pending) { |
| 5196 | napi_disable(&tp->napi); |
| 5197 | tp->rx_pending = ring->rx_pending; |
| 5198 | napi_enable(&tp->napi); |
| 5199 | } |
| 5200 | |
| 5201 | return 0; |
| 5202 | } |
| 5203 | |
Julia Lawall | 407a471 | 2016-09-01 00:21:22 +0200 | [diff] [blame] | 5204 | static const struct ethtool_ops ops = { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5205 | .get_drvinfo = rtl8152_get_drvinfo, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5206 | .get_link = ethtool_op_get_link, |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 5207 | .nway_reset = rtl8152_nway_reset, |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 5208 | .get_msglevel = rtl8152_get_msglevel, |
| 5209 | .set_msglevel = rtl8152_set_msglevel, |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5210 | .get_wol = rtl8152_get_wol, |
| 5211 | .set_wol = rtl8152_set_wol, |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5212 | .get_strings = rtl8152_get_strings, |
| 5213 | .get_sset_count = rtl8152_get_sset_count, |
| 5214 | .get_ethtool_stats = rtl8152_get_ethtool_stats, |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 5215 | .get_coalesce = rtl8152_get_coalesce, |
| 5216 | .set_coalesce = rtl8152_set_coalesce, |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 5217 | .get_eee = rtl_ethtool_get_eee, |
| 5218 | .set_eee = rtl_ethtool_set_eee, |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5219 | .get_link_ksettings = rtl8152_get_link_ksettings, |
| 5220 | .set_link_ksettings = rtl8152_set_link_ksettings, |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 5221 | .get_tunable = rtl8152_get_tunable, |
| 5222 | .set_tunable = rtl8152_set_tunable, |
| 5223 | .get_ringparam = rtl8152_get_ringparam, |
| 5224 | .set_ringparam = rtl8152_set_ringparam, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5225 | }; |
| 5226 | |
| 5227 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 5228 | { |
| 5229 | struct r8152 *tp = netdev_priv(netdev); |
| 5230 | struct mii_ioctl_data *data = if_mii(rq); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5231 | int res; |
| 5232 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5233 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5234 | return -ENODEV; |
| 5235 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5236 | res = usb_autopm_get_interface(tp->intf); |
| 5237 | if (res < 0) |
| 5238 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5239 | |
| 5240 | switch (cmd) { |
| 5241 | case SIOCGMIIPHY: |
| 5242 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 5243 | break; |
| 5244 | |
| 5245 | case SIOCGMIIREG: |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5246 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5247 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5248 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5249 | break; |
| 5250 | |
| 5251 | case SIOCSMIIREG: |
| 5252 | if (!capable(CAP_NET_ADMIN)) { |
| 5253 | res = -EPERM; |
| 5254 | break; |
| 5255 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5256 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5257 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5258 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5259 | break; |
| 5260 | |
| 5261 | default: |
| 5262 | res = -EOPNOTSUPP; |
| 5263 | } |
| 5264 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5265 | usb_autopm_put_interface(tp->intf); |
| 5266 | |
| 5267 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5268 | return res; |
| 5269 | } |
| 5270 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5271 | static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) |
| 5272 | { |
| 5273 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 5274 | int ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5275 | |
| 5276 | switch (tp->version) { |
| 5277 | case RTL_VER_01: |
| 5278 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5279 | case RTL_VER_07: |
Jarod Wilson | a52ad51 | 2016-10-07 22:04:34 -0400 | [diff] [blame] | 5280 | dev->mtu = new_mtu; |
| 5281 | return 0; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5282 | default: |
| 5283 | break; |
| 5284 | } |
| 5285 | |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 5286 | ret = usb_autopm_get_interface(tp->intf); |
| 5287 | if (ret < 0) |
| 5288 | return ret; |
| 5289 | |
| 5290 | mutex_lock(&tp->control); |
| 5291 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5292 | dev->mtu = new_mtu; |
| 5293 | |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 5294 | if (netif_running(dev)) { |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 5295 | u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 5296 | |
| 5297 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms); |
| 5298 | |
| 5299 | if (netif_carrier_ok(dev)) |
| 5300 | r8153_set_rx_early_size(tp); |
| 5301 | } |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 5302 | |
| 5303 | mutex_unlock(&tp->control); |
| 5304 | |
| 5305 | usb_autopm_put_interface(tp->intf); |
| 5306 | |
| 5307 | return ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5308 | } |
| 5309 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5310 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 5311 | .ndo_open = rtl8152_open, |
| 5312 | .ndo_stop = rtl8152_close, |
| 5313 | .ndo_do_ioctl = rtl8152_ioctl, |
| 5314 | .ndo_start_xmit = rtl8152_start_xmit, |
| 5315 | .ndo_tx_timeout = rtl8152_tx_timeout, |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5316 | .ndo_set_features = rtl8152_set_features, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5317 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 5318 | .ndo_set_mac_address = rtl8152_set_mac_address, |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 5319 | .ndo_change_mtu = rtl8152_change_mtu, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5320 | .ndo_validate_addr = eth_validate_addr, |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 5321 | .ndo_features_check = rtl8152_features_check, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5322 | }; |
| 5323 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 5324 | static void rtl8152_unload(struct r8152 *tp) |
| 5325 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5326 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5327 | return; |
| 5328 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 5329 | if (tp->version != RTL_VER_01) |
| 5330 | r8152_power_cut_en(tp, true); |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 5331 | } |
| 5332 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5333 | static void rtl8153_unload(struct r8152 *tp) |
| 5334 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5335 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5336 | return; |
| 5337 | |
hayeswang | 49be172 | 2014-10-01 13:25:11 +0800 | [diff] [blame] | 5338 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5339 | } |
| 5340 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5341 | static void rtl8153b_unload(struct r8152 *tp) |
| 5342 | { |
| 5343 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5344 | return; |
| 5345 | |
| 5346 | r8153b_power_cut_en(tp, false); |
| 5347 | } |
| 5348 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5349 | static int rtl_ops_init(struct r8152 *tp) |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5350 | { |
| 5351 | struct rtl_ops *ops = &tp->rtl_ops; |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5352 | int ret = 0; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5353 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5354 | switch (tp->version) { |
| 5355 | case RTL_VER_01: |
| 5356 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5357 | case RTL_VER_07: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5358 | ops->init = r8152b_init; |
| 5359 | ops->enable = rtl8152_enable; |
| 5360 | ops->disable = rtl8152_disable; |
| 5361 | ops->up = rtl8152_up; |
| 5362 | ops->down = rtl8152_down; |
| 5363 | ops->unload = rtl8152_unload; |
| 5364 | ops->eee_get = r8152_get_eee; |
| 5365 | ops->eee_set = r8152_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5366 | ops->in_nway = rtl8152_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5367 | ops->hw_phy_cfg = r8152b_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 5368 | ops->autosuspend_en = rtl_runtime_suspend_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 5369 | tp->rx_buf_sz = 16 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 5370 | tp->eee_en = true; |
| 5371 | tp->eee_adv = MDIO_EEE_100TX; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5372 | break; |
| 5373 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5374 | case RTL_VER_03: |
| 5375 | case RTL_VER_04: |
| 5376 | case RTL_VER_05: |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 5377 | case RTL_VER_06: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5378 | ops->init = r8153_init; |
| 5379 | ops->enable = rtl8153_enable; |
| 5380 | ops->disable = rtl8153_disable; |
| 5381 | ops->up = rtl8153_up; |
| 5382 | ops->down = rtl8153_down; |
| 5383 | ops->unload = rtl8153_unload; |
| 5384 | ops->eee_get = r8153_get_eee; |
| 5385 | ops->eee_set = r8153_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5386 | ops->in_nway = rtl8153_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5387 | ops->hw_phy_cfg = r8153_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 5388 | ops->autosuspend_en = rtl8153_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 5389 | tp->rx_buf_sz = 32 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 5390 | tp->eee_en = true; |
| 5391 | tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5392 | break; |
| 5393 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5394 | case RTL_VER_08: |
| 5395 | case RTL_VER_09: |
| 5396 | ops->init = r8153b_init; |
| 5397 | ops->enable = rtl8153_enable; |
| 5398 | ops->disable = rtl8153b_disable; |
| 5399 | ops->up = rtl8153b_up; |
| 5400 | ops->down = rtl8153b_down; |
| 5401 | ops->unload = rtl8153b_unload; |
| 5402 | ops->eee_get = r8153_get_eee; |
| 5403 | ops->eee_set = r8153b_set_eee; |
| 5404 | ops->in_nway = rtl8153_in_nway; |
| 5405 | ops->hw_phy_cfg = r8153b_hw_phy_cfg; |
| 5406 | ops->autosuspend_en = rtl8153b_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 5407 | tp->rx_buf_sz = 32 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame^] | 5408 | tp->eee_en = true; |
| 5409 | tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5410 | break; |
| 5411 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5412 | default: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5413 | ret = -ENODEV; |
| 5414 | netif_err(tp, probe, tp->netdev, "Unknown Device\n"); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5415 | break; |
| 5416 | } |
| 5417 | |
| 5418 | return ret; |
| 5419 | } |
| 5420 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5421 | static u8 rtl_get_version(struct usb_interface *intf) |
| 5422 | { |
| 5423 | struct usb_device *udev = interface_to_usbdev(intf); |
| 5424 | u32 ocp_data = 0; |
| 5425 | __le32 *tmp; |
| 5426 | u8 version; |
| 5427 | int ret; |
| 5428 | |
| 5429 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 5430 | if (!tmp) |
| 5431 | return 0; |
| 5432 | |
| 5433 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 5434 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 5435 | PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500); |
| 5436 | if (ret > 0) |
| 5437 | ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; |
| 5438 | |
| 5439 | kfree(tmp); |
| 5440 | |
| 5441 | switch (ocp_data) { |
| 5442 | case 0x4c00: |
| 5443 | version = RTL_VER_01; |
| 5444 | break; |
| 5445 | case 0x4c10: |
| 5446 | version = RTL_VER_02; |
| 5447 | break; |
| 5448 | case 0x5c00: |
| 5449 | version = RTL_VER_03; |
| 5450 | break; |
| 5451 | case 0x5c10: |
| 5452 | version = RTL_VER_04; |
| 5453 | break; |
| 5454 | case 0x5c20: |
| 5455 | version = RTL_VER_05; |
| 5456 | break; |
| 5457 | case 0x5c30: |
| 5458 | version = RTL_VER_06; |
| 5459 | break; |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5460 | case 0x4800: |
| 5461 | version = RTL_VER_07; |
| 5462 | break; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5463 | case 0x6000: |
| 5464 | version = RTL_VER_08; |
| 5465 | break; |
| 5466 | case 0x6010: |
| 5467 | version = RTL_VER_09; |
| 5468 | break; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5469 | default: |
| 5470 | version = RTL_VER_UNKNOWN; |
| 5471 | dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data); |
| 5472 | break; |
| 5473 | } |
| 5474 | |
Oliver Neukum | eb3c28c | 2017-06-12 13:56:51 +0200 | [diff] [blame] | 5475 | dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); |
| 5476 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5477 | return version; |
| 5478 | } |
| 5479 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5480 | static int rtl8152_probe(struct usb_interface *intf, |
| 5481 | const struct usb_device_id *id) |
| 5482 | { |
| 5483 | struct usb_device *udev = interface_to_usbdev(intf); |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5484 | u8 version = rtl_get_version(intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5485 | struct r8152 *tp; |
| 5486 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5487 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5488 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5489 | if (version == RTL_VER_UNKNOWN) |
| 5490 | return -ENODEV; |
| 5491 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 5492 | if (udev->actconfig->desc.bConfigurationValue != 1) { |
| 5493 | usb_driver_set_configuration(udev, 1); |
| 5494 | return -ENODEV; |
| 5495 | } |
| 5496 | |
| 5497 | usb_reset_device(udev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5498 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 5499 | if (!netdev) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 5500 | dev_err(&intf->dev, "Out of memory\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5501 | return -ENOMEM; |
| 5502 | } |
| 5503 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5504 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5505 | tp = netdev_priv(netdev); |
| 5506 | tp->msg_enable = 0x7FFF; |
| 5507 | |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 5508 | tp->udev = udev; |
| 5509 | tp->netdev = netdev; |
| 5510 | tp->intf = intf; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5511 | tp->version = version; |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 5512 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5513 | switch (version) { |
| 5514 | case RTL_VER_01: |
| 5515 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5516 | case RTL_VER_07: |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 5517 | tp->mii.supports_gmii = 0; |
| 5518 | break; |
| 5519 | default: |
| 5520 | tp->mii.supports_gmii = 1; |
| 5521 | break; |
| 5522 | } |
| 5523 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 5524 | ret = rtl_ops_init(tp); |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 5525 | if (ret) |
| 5526 | goto out; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5527 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5528 | mutex_init(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5529 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5530 | INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5531 | tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp); |
| 5532 | tasklet_disable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5533 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5534 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 5535 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 5536 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 5537 | netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 5538 | NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5539 | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | |
| 5540 | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 5541 | netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 5542 | NETIF_F_TSO | NETIF_F_FRAGLIST | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5543 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | |
hayeswang | ccc39fa | 2015-02-06 11:30:49 +0800 | [diff] [blame] | 5544 | NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 5545 | netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | |
| 5546 | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | |
| 5547 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 5548 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 5549 | if (tp->version == RTL_VER_01) { |
| 5550 | netdev->features &= ~NETIF_F_RXCSUM; |
| 5551 | netdev->hw_features &= ~NETIF_F_RXCSUM; |
| 5552 | } |
| 5553 | |
Kai-Heng Feng | 176eb61 | 2018-08-20 12:43:51 +0800 | [diff] [blame] | 5554 | if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && |
| 5555 | (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) { |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 5556 | dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); |
| 5557 | set_bit(DELL_TB_RX_AGG_BUG, &tp->flags); |
| 5558 | } |
| 5559 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 5560 | netdev->ethtool_ops = &ops; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 5561 | netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5562 | |
Jarod Wilson | f77f0ae | 2016-10-20 13:55:17 -0400 | [diff] [blame] | 5563 | /* MTU range: 68 - 1500 or 9194 */ |
| 5564 | netdev->min_mtu = ETH_MIN_MTU; |
| 5565 | switch (tp->version) { |
| 5566 | case RTL_VER_01: |
| 5567 | case RTL_VER_02: |
| 5568 | netdev->max_mtu = ETH_DATA_LEN; |
| 5569 | break; |
| 5570 | default: |
| 5571 | netdev->max_mtu = RTL8153_MAX_MTU; |
| 5572 | break; |
| 5573 | } |
| 5574 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5575 | tp->mii.dev = netdev; |
| 5576 | tp->mii.mdio_read = read_mii_word; |
| 5577 | tp->mii.mdio_write = write_mii_word; |
| 5578 | tp->mii.phy_id_mask = 0x3f; |
| 5579 | tp->mii.reg_num_mask = 0x1f; |
| 5580 | tp->mii.phy_id = R8152_PHY_ID; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5581 | |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 5582 | tp->autoneg = AUTONEG_ENABLE; |
| 5583 | tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100; |
| 5584 | tp->duplex = DUPLEX_FULL; |
| 5585 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 5586 | tp->rx_copybreak = RTL8152_RXFG_HEADSZ; |
| 5587 | tp->rx_pending = 10 * RTL8152_MAX_RX; |
| 5588 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5589 | intf->needs_remote_wakeup = 1; |
| 5590 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5591 | tp->rtl_ops.init(tp); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5592 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5593 | set_ethernet_addr(tp); |
| 5594 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5595 | usb_set_intfdata(intf, tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5596 | netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5597 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5598 | ret = register_netdev(netdev); |
| 5599 | if (ret != 0) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 5600 | netif_err(tp, probe, netdev, "couldn't register the device\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5601 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 5604 | if (!rtl_can_wakeup(tp)) |
| 5605 | __rtl_set_wol(tp, 0); |
| 5606 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5607 | tp->saved_wolopts = __rtl_get_wol(tp); |
| 5608 | if (tp->saved_wolopts) |
| 5609 | device_set_wakeup_enable(&udev->dev, true); |
| 5610 | else |
| 5611 | device_set_wakeup_enable(&udev->dev, false); |
| 5612 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 5613 | netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5614 | |
| 5615 | return 0; |
| 5616 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5617 | out1: |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5618 | netif_napi_del(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5619 | tasklet_kill(&tp->tx_tl); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5620 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5621 | out: |
| 5622 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5623 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5626 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 5627 | { |
| 5628 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5629 | |
| 5630 | usb_set_intfdata(intf, NULL); |
| 5631 | if (tp) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 5632 | rtl_set_unplug(tp); |
hayeswang | f561de3 | 2014-09-30 16:48:01 +0800 | [diff] [blame] | 5633 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5634 | netif_napi_del(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5635 | unregister_netdev(tp->netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5636 | tasklet_kill(&tp->tx_tl); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5637 | cancel_delayed_work_sync(&tp->hw_phy_work); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5638 | tp->rtl_ops.unload(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5639 | free_netdev(tp->netdev); |
| 5640 | } |
| 5641 | } |
| 5642 | |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 5643 | #define REALTEK_USB_DEVICE(vend, prod) \ |
| 5644 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
| 5645 | USB_DEVICE_ID_MATCH_INT_CLASS, \ |
| 5646 | .idVendor = (vend), \ |
| 5647 | .idProduct = (prod), \ |
| 5648 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC \ |
| 5649 | }, \ |
| 5650 | { \ |
| 5651 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \ |
| 5652 | USB_DEVICE_ID_MATCH_DEVICE, \ |
| 5653 | .idVendor = (vend), \ |
| 5654 | .idProduct = (prod), \ |
| 5655 | .bInterfaceClass = USB_CLASS_COMM, \ |
| 5656 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ |
| 5657 | .bInterfaceProtocol = USB_CDC_PROTO_NONE |
| 5658 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5659 | /* table of devices that work with this driver */ |
Arvind Yadav | 9b4355f | 2017-08-08 21:28:05 +0530 | [diff] [blame] | 5660 | static const struct usb_device_id rtl8152_table[] = { |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 5661 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 5662 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)}, |
| 5663 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)}, |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 5664 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)}, |
| 5665 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 5666 | {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)}, |
Vasily Titskiy | 1006da1 | 2015-05-06 10:31:21 -0400 | [diff] [blame] | 5667 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)}, |
hayeswang | d248caf | 2016-10-18 11:41:48 +0800 | [diff] [blame] | 5668 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)}, |
| 5669 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)}, |
| 5670 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)}, |
| 5671 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)}, |
| 5672 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)}, |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 5673 | {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)}, |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 5674 | {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)}, |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 5675 | {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)}, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5676 | {} |
| 5677 | }; |
| 5678 | |
| 5679 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 5680 | |
| 5681 | static struct usb_driver rtl8152_driver = { |
| 5682 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5683 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5684 | .probe = rtl8152_probe, |
| 5685 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5686 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5687 | .resume = rtl8152_resume, |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 5688 | .reset_resume = rtl8152_reset_resume, |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5689 | .pre_reset = rtl8152_pre_reset, |
| 5690 | .post_reset = rtl8152_post_reset, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5691 | .supports_autosuspend = 1, |
hayeswang | a634782 | 2014-02-18 21:49:10 +0800 | [diff] [blame] | 5692 | .disable_hub_initiated_lpm = 1, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5693 | }; |
| 5694 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 5695 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5696 | |
| 5697 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 5698 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 5699 | MODULE_LICENSE("GPL"); |
Grant Grundler | c961e87 | 2016-07-14 11:27:16 -0700 | [diff] [blame] | 5700 | MODULE_VERSION(DRIVER_VERSION); |