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> |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 27 | #include <linux/firmware.h> |
| 28 | #include <crypto/hash.h> |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 29 | |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 30 | /* Information for net-next */ |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 31 | #define NETNEXT_VERSION "11" |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 32 | |
| 33 | /* Information for net */ |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 34 | #define NET_VERSION "10" |
hayeswang | d094247 | 2015-09-07 11:57:43 +0800 | [diff] [blame] | 35 | |
| 36 | #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 37 | #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>" |
hayeswang | 44d942a | 2014-01-15 10:42:14 +0800 | [diff] [blame] | 38 | #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters" |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 39 | #define MODULENAME "r8152" |
| 40 | |
| 41 | #define R8152_PHY_ID 32 |
| 42 | |
| 43 | #define PLA_IDR 0xc000 |
| 44 | #define PLA_RCR 0xc010 |
| 45 | #define PLA_RMS 0xc016 |
| 46 | #define PLA_RXFIFO_CTRL0 0xc0a0 |
| 47 | #define PLA_RXFIFO_CTRL1 0xc0a4 |
| 48 | #define PLA_RXFIFO_CTRL2 0xc0a8 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 49 | #define PLA_DMY_REG0 0xc0b0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 50 | #define PLA_FMC 0xc0b4 |
| 51 | #define PLA_CFG_WOL 0xc0b6 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 52 | #define PLA_TEREDO_CFG 0xc0bc |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 53 | #define PLA_TEREDO_WAKE_BASE 0xc0c4 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 54 | #define PLA_MAR 0xcd00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 55 | #define PLA_BACKUP 0xd000 |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 56 | #define PLA_BDC_CR 0xd1a0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 57 | #define PLA_TEREDO_TIMER 0xd2cc |
| 58 | #define PLA_REALWOW_TIMER 0xd2e8 |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 59 | #define PLA_UPHY_TIMER 0xd388 |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 60 | #define PLA_SUSPEND_FLAG 0xd38a |
| 61 | #define PLA_INDICATE_FALG 0xd38c |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 62 | #define PLA_MACDBG_PRE 0xd38c /* RTL_VER_04 only */ |
| 63 | #define PLA_MACDBG_POST 0xd38e /* RTL_VER_04 only */ |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 64 | #define PLA_EXTRA_STATUS 0xd398 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 65 | #define PLA_EFUSE_DATA 0xdd00 |
| 66 | #define PLA_EFUSE_CMD 0xdd02 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 67 | #define PLA_LEDSEL 0xdd90 |
| 68 | #define PLA_LED_FEATURE 0xdd92 |
| 69 | #define PLA_PHYAR 0xde00 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 70 | #define PLA_BOOT_CTRL 0xe004 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 71 | #define PLA_GPHY_INTR_IMR 0xe022 |
| 72 | #define PLA_EEE_CR 0xe040 |
| 73 | #define PLA_EEEP_CR 0xe080 |
| 74 | #define PLA_MAC_PWR_CTRL 0xe0c0 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 75 | #define PLA_MAC_PWR_CTRL2 0xe0ca |
| 76 | #define PLA_MAC_PWR_CTRL3 0xe0cc |
| 77 | #define PLA_MAC_PWR_CTRL4 0xe0ce |
| 78 | #define PLA_WDT6_CTRL 0xe428 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 79 | #define PLA_TCR0 0xe610 |
| 80 | #define PLA_TCR1 0xe612 |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 81 | #define PLA_MTPS 0xe615 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 82 | #define PLA_TXFIFO_CTRL 0xe618 |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 83 | #define PLA_RSTTALLY 0xe800 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 84 | #define PLA_CR 0xe813 |
| 85 | #define PLA_CRWECR 0xe81c |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 86 | #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */ |
| 87 | #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 88 | #define PLA_CONFIG5 0xe822 |
| 89 | #define PLA_PHY_PWR 0xe84c |
| 90 | #define PLA_OOB_CTRL 0xe84f |
| 91 | #define PLA_CPCR 0xe854 |
| 92 | #define PLA_MISC_0 0xe858 |
| 93 | #define PLA_MISC_1 0xe85a |
| 94 | #define PLA_OCP_GPHY_BASE 0xe86c |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 95 | #define PLA_TALLYCNT 0xe890 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 96 | #define PLA_SFF_STS_7 0xe8de |
| 97 | #define PLA_PHYSTATUS 0xe908 |
| 98 | #define PLA_BP_BA 0xfc26 |
| 99 | #define PLA_BP_0 0xfc28 |
| 100 | #define PLA_BP_1 0xfc2a |
| 101 | #define PLA_BP_2 0xfc2c |
| 102 | #define PLA_BP_3 0xfc2e |
| 103 | #define PLA_BP_4 0xfc30 |
| 104 | #define PLA_BP_5 0xfc32 |
| 105 | #define PLA_BP_6 0xfc34 |
| 106 | #define PLA_BP_7 0xfc36 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 107 | #define PLA_BP_EN 0xfc38 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 108 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 109 | #define USB_USB2PHY 0xb41e |
| 110 | #define USB_SSPHYLINK2 0xb428 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 111 | #define USB_U2P3_CTRL 0xb460 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 112 | #define USB_CSR_DUMMY1 0xb464 |
| 113 | #define USB_CSR_DUMMY2 0xb466 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 114 | #define USB_DEV_STAT 0xb808 |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 115 | #define USB_CONNECT_TIMER 0xcbf8 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 116 | #define USB_MSC_TIMER 0xcbfc |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 117 | #define USB_BURST_SIZE 0xcfc0 |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 118 | #define USB_FW_FIX_EN0 0xcfca |
| 119 | #define USB_FW_FIX_EN1 0xcfcc |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 120 | #define USB_LPM_CONFIG 0xcfd8 |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 121 | #define USB_CSTMR 0xcfef /* RTL8153A */ |
| 122 | #define USB_FW_CTRL 0xd334 /* RTL8153B */ |
| 123 | #define USB_FC_TIMER 0xd340 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 124 | #define USB_USB_CTRL 0xd406 |
| 125 | #define USB_PHY_CTRL 0xd408 |
| 126 | #define USB_TX_AGG 0xd40a |
| 127 | #define USB_RX_BUF_TH 0xd40c |
| 128 | #define USB_USB_TIMER 0xd428 |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 129 | #define USB_RX_EARLY_TIMEOUT 0xd42c |
| 130 | #define USB_RX_EARLY_SIZE 0xd42e |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 131 | #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */ |
| 132 | #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 133 | #define USB_TX_DMA 0xd434 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 134 | #define USB_UPT_RXDMA_OWN 0xd437 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 135 | #define USB_TOLERANCE 0xd490 |
| 136 | #define USB_LPM_CTRL 0xd41a |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 137 | #define USB_BMU_RESET 0xd4b0 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 138 | #define USB_U1U2_TIMER 0xd4da |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 139 | #define USB_FW_TASK 0xd4e8 /* RTL8153B */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 140 | #define USB_UPS_CTRL 0xd800 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 141 | #define USB_POWER_CUT 0xd80a |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 142 | #define USB_MISC_0 0xd81a |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 143 | #define USB_MISC_1 0xd81f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 144 | #define USB_AFE_CTRL2 0xd824 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 145 | #define USB_UPS_CFG 0xd842 |
| 146 | #define USB_UPS_FLAGS 0xd848 |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 147 | #define USB_WDT1_CTRL 0xe404 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 148 | #define USB_WDT11_CTRL 0xe43c |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 149 | #define USB_BP_BA PLA_BP_BA |
| 150 | #define USB_BP_0 PLA_BP_0 |
| 151 | #define USB_BP_1 PLA_BP_1 |
| 152 | #define USB_BP_2 PLA_BP_2 |
| 153 | #define USB_BP_3 PLA_BP_3 |
| 154 | #define USB_BP_4 PLA_BP_4 |
| 155 | #define USB_BP_5 PLA_BP_5 |
| 156 | #define USB_BP_6 PLA_BP_6 |
| 157 | #define USB_BP_7 PLA_BP_7 |
| 158 | #define USB_BP_EN PLA_BP_EN /* RTL8153A */ |
| 159 | #define USB_BP_8 0xfc38 /* RTL8153B */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 160 | #define USB_BP_9 0xfc3a |
| 161 | #define USB_BP_10 0xfc3c |
| 162 | #define USB_BP_11 0xfc3e |
| 163 | #define USB_BP_12 0xfc40 |
| 164 | #define USB_BP_13 0xfc42 |
| 165 | #define USB_BP_14 0xfc44 |
| 166 | #define USB_BP_15 0xfc46 |
| 167 | #define USB_BP2_EN 0xfc48 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 168 | |
| 169 | /* OCP Registers */ |
| 170 | #define OCP_ALDPS_CONFIG 0x2010 |
| 171 | #define OCP_EEE_CONFIG1 0x2080 |
| 172 | #define OCP_EEE_CONFIG2 0x2092 |
| 173 | #define OCP_EEE_CONFIG3 0x2094 |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 174 | #define OCP_BASE_MII 0xa400 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 175 | #define OCP_EEE_AR 0xa41a |
| 176 | #define OCP_EEE_DATA 0xa41c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 177 | #define OCP_PHY_STATUS 0xa420 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 178 | #define OCP_NCTL_CFG 0xa42c |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 179 | #define OCP_POWER_CFG 0xa430 |
| 180 | #define OCP_EEE_CFG 0xa432 |
| 181 | #define OCP_SRAM_ADDR 0xa436 |
| 182 | #define OCP_SRAM_DATA 0xa438 |
| 183 | #define OCP_DOWN_SPEED 0xa442 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 184 | #define OCP_EEE_ABLE 0xa5c4 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 185 | #define OCP_EEE_ADV 0xa5d0 |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 186 | #define OCP_EEE_LPABLE 0xa5d2 |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 187 | #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 188 | #define OCP_PHY_PATCH_STAT 0xb800 |
| 189 | #define OCP_PHY_PATCH_CMD 0xb820 |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 190 | #define OCP_PHY_LOCK 0xb82e |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 191 | #define OCP_ADC_IOFFSET 0xbcfc |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 192 | #define OCP_ADC_CFG 0xbc06 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 193 | #define OCP_SYSCLK_CFG 0xc416 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 194 | |
| 195 | /* SRAM Register */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 196 | #define SRAM_GREEN_CFG 0x8011 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 197 | #define SRAM_LPF_CFG 0x8012 |
| 198 | #define SRAM_10M_AMP1 0x8080 |
| 199 | #define SRAM_10M_AMP2 0x8082 |
| 200 | #define SRAM_IMPEDANCE 0x8084 |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 201 | #define SRAM_PHY_LOCK 0xb82e |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 202 | |
| 203 | /* PLA_RCR */ |
| 204 | #define RCR_AAP 0x00000001 |
| 205 | #define RCR_APM 0x00000002 |
| 206 | #define RCR_AM 0x00000004 |
| 207 | #define RCR_AB 0x00000008 |
| 208 | #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB) |
| 209 | |
| 210 | /* PLA_RXFIFO_CTRL0 */ |
| 211 | #define RXFIFO_THR1_NORMAL 0x00080002 |
| 212 | #define RXFIFO_THR1_OOB 0x01800003 |
| 213 | |
| 214 | /* PLA_RXFIFO_CTRL1 */ |
| 215 | #define RXFIFO_THR2_FULL 0x00000060 |
| 216 | #define RXFIFO_THR2_HIGH 0x00000038 |
| 217 | #define RXFIFO_THR2_OOB 0x0000004a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 218 | #define RXFIFO_THR2_NORMAL 0x00a0 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 219 | |
| 220 | /* PLA_RXFIFO_CTRL2 */ |
| 221 | #define RXFIFO_THR3_FULL 0x00000078 |
| 222 | #define RXFIFO_THR3_HIGH 0x00000048 |
| 223 | #define RXFIFO_THR3_OOB 0x0000005a |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 224 | #define RXFIFO_THR3_NORMAL 0x0110 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 225 | |
| 226 | /* PLA_TXFIFO_CTRL */ |
| 227 | #define TXFIFO_THR_NORMAL 0x00400008 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 228 | #define TXFIFO_THR_NORMAL2 0x01000008 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 229 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 230 | /* PLA_DMY_REG0 */ |
| 231 | #define ECM_ALDPS 0x0002 |
| 232 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 233 | /* PLA_FMC */ |
| 234 | #define FMC_FCR_MCU_EN 0x0001 |
| 235 | |
| 236 | /* PLA_EEEP_CR */ |
| 237 | #define EEEP_CR_EEEP_TX 0x0002 |
| 238 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 239 | /* PLA_WDT6_CTRL */ |
| 240 | #define WDT6_SET_MODE 0x0010 |
| 241 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 242 | /* PLA_TCR0 */ |
| 243 | #define TCR0_TX_EMPTY 0x0800 |
| 244 | #define TCR0_AUTO_FIFO 0x0080 |
| 245 | |
| 246 | /* PLA_TCR1 */ |
| 247 | #define VERSION_MASK 0x7cf0 |
| 248 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 249 | /* PLA_MTPS */ |
| 250 | #define MTPS_JUMBO (12 * 1024 / 64) |
| 251 | #define MTPS_DEFAULT (6 * 1024 / 64) |
| 252 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 253 | /* PLA_RSTTALLY */ |
| 254 | #define TALLY_RESET 0x0001 |
| 255 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 256 | /* PLA_CR */ |
| 257 | #define CR_RST 0x10 |
| 258 | #define CR_RE 0x08 |
| 259 | #define CR_TE 0x04 |
| 260 | |
| 261 | /* PLA_CRWECR */ |
| 262 | #define CRWECR_NORAML 0x00 |
| 263 | #define CRWECR_CONFIG 0xc0 |
| 264 | |
| 265 | /* PLA_OOB_CTRL */ |
| 266 | #define NOW_IS_OOB 0x80 |
| 267 | #define TXFIFO_EMPTY 0x20 |
| 268 | #define RXFIFO_EMPTY 0x10 |
| 269 | #define LINK_LIST_READY 0x02 |
| 270 | #define DIS_MCU_CLROOB 0x01 |
| 271 | #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY) |
| 272 | |
| 273 | /* PLA_MISC_1 */ |
| 274 | #define RXDY_GATED_EN 0x0008 |
| 275 | |
| 276 | /* PLA_SFF_STS_7 */ |
| 277 | #define RE_INIT_LL 0x8000 |
| 278 | #define MCU_BORW_EN 0x4000 |
| 279 | |
| 280 | /* PLA_CPCR */ |
| 281 | #define CPCR_RX_VLAN 0x0040 |
| 282 | |
| 283 | /* PLA_CFG_WOL */ |
| 284 | #define MAGIC_EN 0x0001 |
| 285 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 286 | /* PLA_TEREDO_CFG */ |
| 287 | #define TEREDO_SEL 0x8000 |
| 288 | #define TEREDO_WAKE_MASK 0x7f00 |
| 289 | #define TEREDO_RS_EVENT_MASK 0x00fe |
| 290 | #define OOB_TEREDO_EN 0x0001 |
| 291 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 292 | /* PLA_BDC_CR */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 293 | #define ALDPS_PROXY_MODE 0x0001 |
| 294 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 295 | /* PLA_EFUSE_CMD */ |
| 296 | #define EFUSE_READ_CMD BIT(15) |
| 297 | #define EFUSE_DATA_BIT16 BIT(7) |
| 298 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 299 | /* PLA_CONFIG34 */ |
| 300 | #define LINK_ON_WAKE_EN 0x0010 |
| 301 | #define LINK_OFF_WAKE_EN 0x0008 |
| 302 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 303 | /* PLA_CONFIG5 */ |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 304 | #define BWF_EN 0x0040 |
| 305 | #define MWF_EN 0x0020 |
| 306 | #define UWF_EN 0x0010 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 307 | #define LAN_WAKE_EN 0x0002 |
| 308 | |
| 309 | /* PLA_LED_FEATURE */ |
| 310 | #define LED_MODE_MASK 0x0700 |
| 311 | |
| 312 | /* PLA_PHY_PWR */ |
| 313 | #define TX_10M_IDLE_EN 0x0080 |
| 314 | #define PFM_PWM_SWITCH 0x0040 |
| 315 | |
| 316 | /* PLA_MAC_PWR_CTRL */ |
| 317 | #define D3_CLK_GATED_EN 0x00004000 |
| 318 | #define MCU_CLK_RATIO 0x07010f07 |
| 319 | #define MCU_CLK_RATIO_MASK 0x0f0f0f0f |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 320 | #define ALDPS_SPDWN_RATIO 0x0f87 |
| 321 | |
| 322 | /* PLA_MAC_PWR_CTRL2 */ |
| 323 | #define EEE_SPDWN_RATIO 0x8007 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 324 | #define MAC_CLK_SPDWN_EN BIT(15) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 325 | |
| 326 | /* PLA_MAC_PWR_CTRL3 */ |
| 327 | #define PKT_AVAIL_SPDWN_EN 0x0100 |
| 328 | #define SUSPEND_SPDWN_EN 0x0004 |
| 329 | #define U1U2_SPDWN_EN 0x0002 |
| 330 | #define L1_SPDWN_EN 0x0001 |
| 331 | |
| 332 | /* PLA_MAC_PWR_CTRL4 */ |
| 333 | #define PWRSAVE_SPDWN_EN 0x1000 |
| 334 | #define RXDV_SPDWN_EN 0x0800 |
| 335 | #define TX10MIDLE_EN 0x0100 |
| 336 | #define TP100_SPDWN_EN 0x0020 |
| 337 | #define TP500_SPDWN_EN 0x0010 |
| 338 | #define TP1000_SPDWN_EN 0x0008 |
| 339 | #define EEE_SPDWN_EN 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 340 | |
| 341 | /* PLA_GPHY_INTR_IMR */ |
| 342 | #define GPHY_STS_MSK 0x0001 |
| 343 | #define SPEED_DOWN_MSK 0x0002 |
| 344 | #define SPDWN_RXDV_MSK 0x0004 |
| 345 | #define SPDWN_LINKCHG_MSK 0x0008 |
| 346 | |
| 347 | /* PLA_PHYAR */ |
| 348 | #define PHYAR_FLAG 0x80000000 |
| 349 | |
| 350 | /* PLA_EEE_CR */ |
| 351 | #define EEE_RX_EN 0x0001 |
| 352 | #define EEE_TX_EN 0x0002 |
| 353 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 354 | /* PLA_BOOT_CTRL */ |
| 355 | #define AUTOLOAD_DONE 0x0002 |
| 356 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 357 | /* PLA_SUSPEND_FLAG */ |
| 358 | #define LINK_CHG_EVENT BIT(0) |
| 359 | |
| 360 | /* PLA_INDICATE_FALG */ |
| 361 | #define UPCOMING_RUNTIME_D3 BIT(0) |
| 362 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 363 | /* PLA_MACDBG_PRE and PLA_MACDBG_POST */ |
| 364 | #define DEBUG_OE BIT(0) |
| 365 | #define DEBUG_LTSSM 0x0082 |
| 366 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 367 | /* PLA_EXTRA_STATUS */ |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame^] | 368 | #define CUR_LINK_OK BIT(15) |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 369 | #define U3P3_CHECK_EN BIT(7) /* RTL_VER_05 only */ |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 370 | #define LINK_CHANGE_FLAG BIT(8) |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame^] | 371 | #define POLL_LINK_CHG BIT(0) |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 372 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 373 | /* USB_USB2PHY */ |
| 374 | #define USB2PHY_SUSPEND 0x0001 |
| 375 | #define USB2PHY_L1 0x0002 |
| 376 | |
| 377 | /* USB_SSPHYLINK2 */ |
| 378 | #define pwd_dn_scale_mask 0x3ffe |
| 379 | #define pwd_dn_scale(x) ((x) << 1) |
| 380 | |
| 381 | /* USB_CSR_DUMMY1 */ |
| 382 | #define DYNAMIC_BURST 0x0001 |
| 383 | |
| 384 | /* USB_CSR_DUMMY2 */ |
| 385 | #define EP4_FULL_FC 0x0001 |
| 386 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 387 | /* USB_DEV_STAT */ |
| 388 | #define STAT_SPEED_MASK 0x0006 |
| 389 | #define STAT_SPEED_HIGH 0x0000 |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 390 | #define STAT_SPEED_FULL 0x0002 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 391 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 392 | /* USB_FW_FIX_EN0 */ |
| 393 | #define FW_FIX_SUSPEND BIT(14) |
| 394 | |
| 395 | /* USB_FW_FIX_EN1 */ |
| 396 | #define FW_IP_RESET_EN BIT(9) |
| 397 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 398 | /* USB_LPM_CONFIG */ |
| 399 | #define LPM_U1U2_EN BIT(0) |
| 400 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 401 | /* USB_TX_AGG */ |
| 402 | #define TX_AGG_MAX_THRESHOLD 0x03 |
| 403 | |
| 404 | /* USB_RX_BUF_TH */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 405 | #define RX_THR_SUPPER 0x0c350180 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 406 | #define RX_THR_HIGH 0x7a120180 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 407 | #define RX_THR_SLOW 0xffff0180 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 408 | #define RX_THR_B 0x00010001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 409 | |
| 410 | /* USB_TX_DMA */ |
| 411 | #define TEST_MODE_DISABLE 0x00000001 |
| 412 | #define TX_SIZE_ADJUST1 0x00000100 |
| 413 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 414 | /* USB_BMU_RESET */ |
| 415 | #define BMU_RESET_EP_IN 0x01 |
| 416 | #define BMU_RESET_EP_OUT 0x02 |
| 417 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 418 | /* USB_UPT_RXDMA_OWN */ |
| 419 | #define OWN_UPDATE BIT(0) |
| 420 | #define OWN_CLEAR BIT(1) |
| 421 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 422 | /* USB_FW_TASK */ |
| 423 | #define FC_PATCH_TASK BIT(1) |
| 424 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 425 | /* USB_UPS_CTRL */ |
| 426 | #define POWER_CUT 0x0100 |
| 427 | |
| 428 | /* USB_PM_CTRL_STATUS */ |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 429 | #define RESUME_INDICATE 0x0001 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 430 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 431 | /* USB_CSTMR */ |
| 432 | #define FORCE_SUPER BIT(0) |
| 433 | |
| 434 | /* USB_FW_CTRL */ |
| 435 | #define FLOW_CTRL_PATCH_OPT BIT(1) |
| 436 | |
| 437 | /* USB_FC_TIMER */ |
| 438 | #define CTRL_TIMER_EN BIT(15) |
| 439 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 440 | /* USB_USB_CTRL */ |
| 441 | #define RX_AGG_DISABLE 0x0010 |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 442 | #define RX_ZERO_EN 0x0080 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 443 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 444 | /* USB_U2P3_CTRL */ |
| 445 | #define U2P3_ENABLE 0x0001 |
| 446 | |
| 447 | /* USB_POWER_CUT */ |
| 448 | #define PWR_EN 0x0001 |
| 449 | #define PHASE2_EN 0x0008 |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 450 | #define UPS_EN BIT(4) |
| 451 | #define USP_PREWAKE BIT(5) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 452 | |
| 453 | /* USB_MISC_0 */ |
| 454 | #define PCUT_STATUS 0x0001 |
| 455 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 456 | /* USB_RX_EARLY_TIMEOUT */ |
| 457 | #define COALESCE_SUPER 85000U |
| 458 | #define COALESCE_HIGH 250000U |
| 459 | #define COALESCE_SLOW 524280U |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 460 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 461 | /* USB_WDT1_CTRL */ |
| 462 | #define WTD1_EN BIT(0) |
| 463 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 464 | /* USB_WDT11_CTRL */ |
| 465 | #define TIMER11_EN 0x0001 |
| 466 | |
| 467 | /* USB_LPM_CTRL */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 468 | /* bit 4 ~ 5: fifo empty boundary */ |
| 469 | #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */ |
| 470 | /* bit 2 ~ 3: LMP timer */ |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 471 | #define LPM_TIMER_MASK 0x0c |
| 472 | #define LPM_TIMER_500MS 0x04 /* 500 ms */ |
| 473 | #define LPM_TIMER_500US 0x0c /* 500 us */ |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 474 | #define ROK_EXIT_LPM 0x02 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 475 | |
| 476 | /* USB_AFE_CTRL2 */ |
| 477 | #define SEN_VAL_MASK 0xf800 |
| 478 | #define SEN_VAL_NORMAL 0xa000 |
| 479 | #define SEL_RXIDLE 0x0100 |
| 480 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 481 | /* USB_UPS_CFG */ |
| 482 | #define SAW_CNT_1MS_MASK 0x0fff |
| 483 | |
| 484 | /* USB_UPS_FLAGS */ |
| 485 | #define UPS_FLAGS_R_TUNE BIT(0) |
| 486 | #define UPS_FLAGS_EN_10M_CKDIV BIT(1) |
| 487 | #define UPS_FLAGS_250M_CKDIV BIT(2) |
| 488 | #define UPS_FLAGS_EN_ALDPS BIT(3) |
| 489 | #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 490 | #define ups_flags_speed(x) ((x) << 16) |
| 491 | #define UPS_FLAGS_EN_EEE BIT(20) |
| 492 | #define UPS_FLAGS_EN_500M_EEE BIT(21) |
| 493 | #define UPS_FLAGS_EN_EEE_CKDIV BIT(22) |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 494 | #define UPS_FLAGS_EEE_PLLOFF_100 BIT(23) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 495 | #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24) |
| 496 | #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25) |
| 497 | #define UPS_FLAGS_EN_GREEN BIT(26) |
| 498 | #define UPS_FLAGS_EN_FLOW_CTR BIT(27) |
| 499 | |
| 500 | enum spd_duplex { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 501 | NWAY_10M_HALF, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 502 | NWAY_10M_FULL, |
| 503 | NWAY_100M_HALF, |
| 504 | NWAY_100M_FULL, |
| 505 | NWAY_1000M_FULL, |
| 506 | FORCE_10M_HALF, |
| 507 | FORCE_10M_FULL, |
| 508 | FORCE_100M_HALF, |
| 509 | FORCE_100M_FULL, |
| 510 | }; |
| 511 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 512 | /* OCP_ALDPS_CONFIG */ |
| 513 | #define ENPWRSAVE 0x8000 |
| 514 | #define ENPDNPS 0x0200 |
| 515 | #define LINKENA 0x0100 |
| 516 | #define DIS_SDSAVE 0x0010 |
| 517 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 518 | /* OCP_PHY_STATUS */ |
| 519 | #define PHY_STAT_MASK 0x0007 |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 520 | #define PHY_STAT_EXT_INIT 2 |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 521 | #define PHY_STAT_LAN_ON 3 |
| 522 | #define PHY_STAT_PWRDN 5 |
| 523 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 524 | /* OCP_NCTL_CFG */ |
| 525 | #define PGA_RETURN_EN BIT(1) |
| 526 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 527 | /* OCP_POWER_CFG */ |
| 528 | #define EEE_CLKDIV_EN 0x8000 |
| 529 | #define EN_ALDPS 0x0004 |
| 530 | #define EN_10M_PLLOFF 0x0001 |
| 531 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 532 | /* OCP_EEE_CONFIG1 */ |
| 533 | #define RG_TXLPI_MSK_HFDUP 0x8000 |
| 534 | #define RG_MATCLR_EN 0x4000 |
| 535 | #define EEE_10_CAP 0x2000 |
| 536 | #define EEE_NWAY_EN 0x1000 |
| 537 | #define TX_QUIET_EN 0x0200 |
| 538 | #define RX_QUIET_EN 0x0100 |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 539 | #define sd_rise_time_mask 0x0070 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 540 | #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 541 | #define RG_RXLPI_MSK_HFDUP 0x0008 |
| 542 | #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */ |
| 543 | |
| 544 | /* OCP_EEE_CONFIG2 */ |
| 545 | #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */ |
| 546 | #define RG_DACQUIET_EN 0x0400 |
| 547 | #define RG_LDVQUIET_EN 0x0200 |
| 548 | #define RG_CKRSEL 0x0020 |
| 549 | #define RG_EEEPRG_EN 0x0010 |
| 550 | |
| 551 | /* OCP_EEE_CONFIG3 */ |
hayeswang | d24f613 | 2014-09-25 20:54:01 +0800 | [diff] [blame] | 552 | #define fast_snr_mask 0xff80 |
hayeswang | 4c4a6b1 | 2014-09-25 20:54:00 +0800 | [diff] [blame] | 553 | #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 554 | #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */ |
| 555 | #define MSK_PH 0x0006 /* bit 0 ~ 3 */ |
| 556 | |
| 557 | /* OCP_EEE_AR */ |
| 558 | /* bit[15:14] function */ |
| 559 | #define FUN_ADDR 0x0000 |
| 560 | #define FUN_DATA 0x4000 |
| 561 | /* bit[4:0] device addr */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 562 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 563 | /* OCP_EEE_CFG */ |
| 564 | #define CTAP_SHORT_EN 0x0040 |
| 565 | #define EEE10_EN 0x0010 |
| 566 | |
| 567 | /* OCP_DOWN_SPEED */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 568 | #define EN_EEE_CMODE BIT(14) |
| 569 | #define EN_EEE_1000 BIT(13) |
| 570 | #define EN_EEE_100 BIT(12) |
| 571 | #define EN_10M_CLKDIV BIT(11) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 572 | #define EN_10M_BGOFF 0x0080 |
| 573 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 574 | /* OCP_PHY_STATE */ |
| 575 | #define TXDIS_STATE 0x01 |
| 576 | #define ABD_STATE 0x02 |
| 577 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 578 | /* OCP_PHY_PATCH_STAT */ |
| 579 | #define PATCH_READY BIT(6) |
| 580 | |
| 581 | /* OCP_PHY_PATCH_CMD */ |
| 582 | #define PATCH_REQUEST BIT(4) |
| 583 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 584 | /* OCP_PHY_LOCK */ |
| 585 | #define PATCH_LOCK BIT(0) |
| 586 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 587 | /* OCP_ADC_CFG */ |
| 588 | #define CKADSEL_L 0x0100 |
| 589 | #define ADC_EN 0x0080 |
| 590 | #define EN_EMI_L 0x0040 |
| 591 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 592 | /* OCP_SYSCLK_CFG */ |
| 593 | #define clk_div_expo(x) (min(x, 5) << 8) |
| 594 | |
| 595 | /* SRAM_GREEN_CFG */ |
| 596 | #define GREEN_ETH_EN BIT(15) |
| 597 | #define R_TUNE_EN BIT(11) |
| 598 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 599 | /* SRAM_LPF_CFG */ |
| 600 | #define LPF_AUTO_TUNE 0x8000 |
| 601 | |
| 602 | /* SRAM_10M_AMP1 */ |
| 603 | #define GDAC_IB_UPALL 0x0008 |
| 604 | |
| 605 | /* SRAM_10M_AMP2 */ |
| 606 | #define AMP_DN 0x0200 |
| 607 | |
| 608 | /* SRAM_IMPEDANCE */ |
| 609 | #define RX_DRIVING_MASK 0x6000 |
| 610 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 611 | /* SRAM_PHY_LOCK */ |
| 612 | #define PHY_PATCH_LOCK 0x0001 |
| 613 | |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 614 | /* MAC PASSTHRU */ |
| 615 | #define AD_MASK 0xfee0 |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 616 | #define BND_MASK 0x0004 |
David Chen | 8e29d23 | 2019-02-16 17:16:42 +0800 | [diff] [blame] | 617 | #define BD_MASK 0x0001 |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 618 | #define EFUSE 0xcfdb |
| 619 | #define PASS_THRU_MASK 0x1 |
| 620 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 621 | #define BP4_SUPER_ONLY 0x1578 /* RTL_VER_04 only */ |
| 622 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 623 | enum rtl_register_content { |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 624 | _1000bps = 0x10, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 625 | _100bps = 0x08, |
| 626 | _10bps = 0x04, |
| 627 | LINK_STATUS = 0x02, |
| 628 | FULL_DUP = 0x01, |
| 629 | }; |
| 630 | |
hayeswang | 1764bcd | 2014-08-28 10:24:18 +0800 | [diff] [blame] | 631 | #define RTL8152_MAX_TX 4 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 632 | #define RTL8152_MAX_RX 10 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 633 | #define INTBUFSIZE 2 |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 634 | #define TX_ALIGN 4 |
| 635 | #define RX_ALIGN 8 |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 636 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 637 | #define RTL8152_RX_MAX_PENDING 4096 |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 638 | #define RTL8152_RXFG_HEADSZ 256 |
| 639 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 640 | #define INTR_LINK 0x0004 |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 641 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 642 | #define RTL8152_REQT_READ 0xc0 |
| 643 | #define RTL8152_REQT_WRITE 0x40 |
| 644 | #define RTL8152_REQ_GET_REGS 0x05 |
| 645 | #define RTL8152_REQ_SET_REGS 0x05 |
| 646 | |
| 647 | #define BYTE_EN_DWORD 0xff |
| 648 | #define BYTE_EN_WORD 0x33 |
| 649 | #define BYTE_EN_BYTE 0x11 |
| 650 | #define BYTE_EN_SIX_BYTES 0x3f |
| 651 | #define BYTE_EN_START_MASK 0x0f |
| 652 | #define BYTE_EN_END_MASK 0xf0 |
| 653 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 654 | #define RTL8153_MAX_PACKET 9216 /* 9K */ |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 655 | #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \ |
| 656 | ETH_FCS_LEN) |
| 657 | #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 658 | #define RTL8153_RMS RTL8153_MAX_PACKET |
hayeswang | b812540 | 2014-07-03 11:55:48 +0800 | [diff] [blame] | 659 | #define RTL8152_TX_TIMEOUT (5 * HZ) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 660 | #define RTL8152_NAPI_WEIGHT 64 |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 661 | #define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \ |
hayeswang | b20cb60 | 2017-03-20 16:13:45 +0800 | [diff] [blame] | 662 | sizeof(struct rx_desc) + RX_ALIGN) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 663 | |
| 664 | /* rtl8152 flags */ |
| 665 | enum rtl8152_flags { |
| 666 | RTL8152_UNPLUG = 0, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 667 | RTL8152_SET_RX_MODE, |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 668 | WORK_ENABLE, |
| 669 | RTL8152_LINK_CHG, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 670 | SELECTIVE_SUSPEND, |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 671 | PHY_RESET, |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 672 | SCHEDULE_TASKLET, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 673 | GREEN_ETHERNET, |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 674 | DELL_TB_RX_AGG_BUG, |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 675 | LENOVO_MACPASSTHRU, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | /* Define these values to match your device */ |
| 679 | #define VENDOR_ID_REALTEK 0x0bda |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 680 | #define VENDOR_ID_MICROSOFT 0x045e |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 681 | #define VENDOR_ID_SAMSUNG 0x04e8 |
Christian Hesse | 347eec3 | 2015-03-31 14:10:07 +0200 | [diff] [blame] | 682 | #define VENDOR_ID_LENOVO 0x17ef |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 683 | #define VENDOR_ID_LINKSYS 0x13b1 |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 684 | #define VENDOR_ID_NVIDIA 0x0955 |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 685 | #define VENDOR_ID_TPLINK 0x2357 |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 686 | |
| 687 | #define MCU_TYPE_PLA 0x0100 |
| 688 | #define MCU_TYPE_USB 0x0000 |
| 689 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 690 | struct tally_counter { |
| 691 | __le64 tx_packets; |
| 692 | __le64 rx_packets; |
| 693 | __le64 tx_errors; |
| 694 | __le32 rx_errors; |
| 695 | __le16 rx_missed; |
| 696 | __le16 align_errors; |
| 697 | __le32 tx_one_collision; |
| 698 | __le32 tx_multi_collision; |
| 699 | __le64 rx_unicast; |
| 700 | __le64 rx_broadcast; |
| 701 | __le32 rx_multicast; |
| 702 | __le16 tx_aborted; |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 703 | __le16 tx_underrun; |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 704 | }; |
| 705 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 706 | struct rx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 707 | __le32 opts1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 708 | #define RX_LEN_MASK 0x7fff |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 709 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 710 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 711 | #define RD_UDP_CS BIT(23) |
| 712 | #define RD_TCP_CS BIT(22) |
| 713 | #define RD_IPV6_CS BIT(20) |
| 714 | #define RD_IPV4_CS BIT(19) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 715 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 716 | __le32 opts3; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 717 | #define IPF BIT(23) /* IP checksum fail */ |
| 718 | #define UDPF BIT(22) /* UDP checksum fail */ |
| 719 | #define TCPF BIT(21) /* TCP checksum fail */ |
| 720 | #define RX_VLAN_TAG BIT(16) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 721 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 722 | __le32 opts4; |
| 723 | __le32 opts5; |
| 724 | __le32 opts6; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 725 | }; |
| 726 | |
| 727 | struct tx_desc { |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 728 | __le32 opts1; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 729 | #define TX_FS BIT(31) /* First segment of a packet */ |
| 730 | #define TX_LS BIT(30) /* Final segment of a packet */ |
| 731 | #define GTSENDV4 BIT(28) |
| 732 | #define GTSENDV6 BIT(27) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 733 | #define GTTCPHO_SHIFT 18 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 734 | #define GTTCPHO_MAX 0x7fU |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 735 | #define TX_LEN_MAX 0x3ffffU |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 736 | |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 737 | __le32 opts2; |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 738 | #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */ |
| 739 | #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */ |
| 740 | #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */ |
| 741 | #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */ |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 742 | #define MSS_SHIFT 17 |
| 743 | #define MSS_MAX 0x7ffU |
| 744 | #define TCPHO_SHIFT 17 |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 745 | #define TCPHO_MAX 0x7ffU |
hayeswang | f5aaaa6 | 2015-02-06 11:30:51 +0800 | [diff] [blame] | 746 | #define TX_VLAN_TAG BIT(16) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 747 | }; |
| 748 | |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 749 | struct r8152; |
| 750 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 751 | struct rx_agg { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 752 | struct list_head list, info_list; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 753 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 754 | struct r8152 *context; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 755 | struct page *page; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 756 | void *buffer; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 757 | }; |
| 758 | |
| 759 | struct tx_agg { |
| 760 | struct list_head list; |
| 761 | struct urb *urb; |
hayeswang | dff4e8a | 2013-08-16 16:09:33 +0800 | [diff] [blame] | 762 | struct r8152 *context; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 763 | void *buffer; |
| 764 | void *head; |
| 765 | u32 skb_num; |
| 766 | u32 skb_len; |
| 767 | }; |
| 768 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 769 | struct r8152 { |
| 770 | unsigned long flags; |
| 771 | struct usb_device *udev; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 772 | struct napi_struct napi; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 773 | struct usb_interface *intf; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 774 | struct net_device *netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 775 | struct urb *intr_urb; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 776 | struct tx_agg tx_info[RTL8152_MAX_TX]; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 777 | struct list_head rx_info, rx_used; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 778 | struct list_head rx_done, tx_free; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 779 | struct sk_buff_head tx_queue, rx_queue; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 780 | spinlock_t rx_lock, tx_lock; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 781 | struct delayed_work schedule, hw_phy_work; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 782 | struct mii_if_info mii; |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 783 | struct mutex control; /* use for hw setting */ |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 784 | #ifdef CONFIG_PM_SLEEP |
| 785 | struct notifier_block pm_notifier; |
| 786 | #endif |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 787 | struct tasklet_struct tx_tl; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 788 | |
| 789 | struct rtl_ops { |
Prashant Malani | 151ea09 | 2019-10-02 14:09:33 -0700 | [diff] [blame] | 790 | void (*init)(struct r8152 *tp); |
| 791 | int (*enable)(struct r8152 *tp); |
| 792 | void (*disable)(struct r8152 *tp); |
| 793 | void (*up)(struct r8152 *tp); |
| 794 | void (*down)(struct r8152 *tp); |
| 795 | void (*unload)(struct r8152 *tp); |
| 796 | int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee); |
| 797 | int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee); |
| 798 | bool (*in_nway)(struct r8152 *tp); |
| 799 | void (*hw_phy_cfg)(struct r8152 *tp); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 800 | void (*autosuspend_en)(struct r8152 *tp, bool enable); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 801 | } rtl_ops; |
| 802 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 803 | struct ups_info { |
| 804 | u32 _10m_ckdiv:1; |
| 805 | u32 _250m_ckdiv:1; |
| 806 | u32 aldps:1; |
| 807 | u32 lite_mode:2; |
| 808 | u32 speed_duplex:4; |
| 809 | u32 eee:1; |
| 810 | u32 eee_lite:1; |
| 811 | u32 eee_ckdiv:1; |
| 812 | u32 eee_plloff_100:1; |
| 813 | u32 eee_plloff_giga:1; |
| 814 | u32 eee_cmod_lv:1; |
| 815 | u32 green:1; |
| 816 | u32 flow_control:1; |
| 817 | u32 ctap_short_off:1; |
| 818 | } ups_info; |
| 819 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 820 | #define RTL_VER_SIZE 32 |
| 821 | |
| 822 | struct rtl_fw { |
| 823 | const char *fw_name; |
| 824 | const struct firmware *fw; |
| 825 | |
| 826 | char version[RTL_VER_SIZE]; |
| 827 | int (*pre_fw)(struct r8152 *tp); |
| 828 | int (*post_fw)(struct r8152 *tp); |
| 829 | |
| 830 | bool retry; |
| 831 | } rtl_fw; |
| 832 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 833 | atomic_t rx_count; |
| 834 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 835 | bool eee_en; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 836 | int intr_interval; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 837 | u32 saved_wolopts; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 838 | u32 msg_enable; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 839 | u32 tx_qlen; |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 840 | u32 coalesce; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 841 | u32 advertising; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 842 | u32 rx_buf_sz; |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 843 | u32 rx_copybreak; |
| 844 | u32 rx_pending; |
| 845 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 846 | u16 ocp_base; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 847 | u16 speed; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 848 | u16 eee_adv; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 849 | u8 *intr_buff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 850 | u8 version; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 851 | u8 duplex; |
| 852 | u8 autoneg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 853 | }; |
| 854 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 855 | /** |
| 856 | * struct fw_block - block type and total length |
| 857 | * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA, |
| 858 | * RTL_FW_USB and so on. |
| 859 | * @length: total length of the current block. |
| 860 | */ |
| 861 | struct fw_block { |
| 862 | __le32 type; |
| 863 | __le32 length; |
| 864 | } __packed; |
| 865 | |
| 866 | /** |
| 867 | * struct fw_header - header of the firmware file |
| 868 | * @checksum: checksum of sha256 which is calculated from the whole file |
| 869 | * except the checksum field of the file. That is, calculate sha256 |
| 870 | * from the version field to the end of the file. |
| 871 | * @version: version of this firmware. |
| 872 | * @blocks: the first firmware block of the file |
| 873 | */ |
| 874 | struct fw_header { |
| 875 | u8 checksum[32]; |
| 876 | char version[RTL_VER_SIZE]; |
| 877 | struct fw_block blocks[0]; |
| 878 | } __packed; |
| 879 | |
| 880 | /** |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 881 | * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB. |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 882 | * The layout of the firmware block is: |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 883 | * <struct fw_mac> + <info> + <firmware data>. |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 884 | * @fw_offset: offset of the firmware binary data. The start address of |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 885 | * the data would be the address of struct fw_mac + @fw_offset. |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 886 | * @fw_reg: the register to load the firmware. Depends on chip. |
| 887 | * @bp_ba_addr: the register to write break point base address. Depends on |
| 888 | * chip. |
| 889 | * @bp_ba_value: break point base address. Depends on chip. |
| 890 | * @bp_en_addr: the register to write break point enabled mask. Depends |
| 891 | * on chip. |
| 892 | * @bp_en_value: break point enabled mask. Depends on the firmware. |
| 893 | * @bp_start: the start register of break points. Depends on chip. |
| 894 | * @bp_num: the break point number which needs to be set for this firmware. |
| 895 | * Depends on the firmware. |
| 896 | * @bp: break points. Depends on firmware. |
| 897 | * @fw_ver_reg: the register to store the fw version. |
| 898 | * @fw_ver_data: the firmware version of the current type. |
| 899 | * @info: additional information for debugging, and is followed by the |
| 900 | * binary data of firmware. |
| 901 | */ |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 902 | struct fw_mac { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 903 | struct fw_block blk_hdr; |
| 904 | __le16 fw_offset; |
| 905 | __le16 fw_reg; |
| 906 | __le16 bp_ba_addr; |
| 907 | __le16 bp_ba_value; |
| 908 | __le16 bp_en_addr; |
| 909 | __le16 bp_en_value; |
| 910 | __le16 bp_start; |
| 911 | __le16 bp_num; |
| 912 | __le16 bp[16]; /* any value determined by firmware */ |
| 913 | __le32 reserved; |
| 914 | __le16 fw_ver_reg; |
| 915 | u8 fw_ver_data; |
| 916 | char info[0]; |
| 917 | } __packed; |
| 918 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 919 | /** |
| 920 | * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START. |
| 921 | * This is used to set patch key when loading the firmware of PHY. |
| 922 | * @key_reg: the register to write the patch key. |
| 923 | * @key_data: patch key. |
| 924 | */ |
| 925 | struct fw_phy_patch_key { |
| 926 | struct fw_block blk_hdr; |
| 927 | __le16 key_reg; |
| 928 | __le16 key_data; |
| 929 | __le32 reserved; |
| 930 | } __packed; |
| 931 | |
| 932 | /** |
| 933 | * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC. |
| 934 | * The layout of the firmware block is: |
| 935 | * <struct fw_phy_nc> + <info> + <firmware data>. |
| 936 | * @fw_offset: offset of the firmware binary data. The start address of |
| 937 | * the data would be the address of struct fw_phy_nc + @fw_offset. |
| 938 | * @fw_reg: the register to load the firmware. Depends on chip. |
| 939 | * @ba_reg: the register to write the base address. Depends on chip. |
| 940 | * @ba_data: base address. Depends on chip. |
| 941 | * @patch_en_addr: the register of enabling patch mode. Depends on chip. |
| 942 | * @patch_en_value: patch mode enabled mask. Depends on the firmware. |
| 943 | * @mode_reg: the regitster of switching the mode. |
| 944 | * @mod_pre: the mode needing to be set before loading the firmware. |
| 945 | * @mod_post: the mode to be set when finishing to load the firmware. |
| 946 | * @bp_start: the start register of break points. Depends on chip. |
| 947 | * @bp_num: the break point number which needs to be set for this firmware. |
| 948 | * Depends on the firmware. |
| 949 | * @bp: break points. Depends on firmware. |
| 950 | * @info: additional information for debugging, and is followed by the |
| 951 | * binary data of firmware. |
| 952 | */ |
| 953 | struct fw_phy_nc { |
| 954 | struct fw_block blk_hdr; |
| 955 | __le16 fw_offset; |
| 956 | __le16 fw_reg; |
| 957 | __le16 ba_reg; |
| 958 | __le16 ba_data; |
| 959 | __le16 patch_en_addr; |
| 960 | __le16 patch_en_value; |
| 961 | __le16 mode_reg; |
| 962 | __le16 mode_pre; |
| 963 | __le16 mode_post; |
| 964 | __le16 reserved; |
| 965 | __le16 bp_start; |
| 966 | __le16 bp_num; |
| 967 | __le16 bp[4]; |
| 968 | char info[0]; |
| 969 | } __packed; |
| 970 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 971 | enum rtl_fw_type { |
| 972 | RTL_FW_END = 0, |
| 973 | RTL_FW_PLA, |
| 974 | RTL_FW_USB, |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 975 | RTL_FW_PHY_START, |
| 976 | RTL_FW_PHY_STOP, |
| 977 | RTL_FW_PHY_NC, |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 978 | }; |
| 979 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 980 | enum rtl_version { |
| 981 | RTL_VER_UNKNOWN = 0, |
| 982 | RTL_VER_01, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 983 | RTL_VER_02, |
| 984 | RTL_VER_03, |
| 985 | RTL_VER_04, |
| 986 | RTL_VER_05, |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 987 | RTL_VER_06, |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 988 | RTL_VER_07, |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 989 | RTL_VER_08, |
| 990 | RTL_VER_09, |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 991 | RTL_VER_MAX |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 992 | }; |
| 993 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 994 | enum tx_csum_stat { |
| 995 | TX_CSUM_SUCCESS = 0, |
| 996 | TX_CSUM_TSO, |
| 997 | TX_CSUM_NONE |
| 998 | }; |
| 999 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 1000 | #define RTL_ADVERTISED_10_HALF BIT(0) |
| 1001 | #define RTL_ADVERTISED_10_FULL BIT(1) |
| 1002 | #define RTL_ADVERTISED_100_HALF BIT(2) |
| 1003 | #define RTL_ADVERTISED_100_FULL BIT(3) |
| 1004 | #define RTL_ADVERTISED_1000_HALF BIT(4) |
| 1005 | #define RTL_ADVERTISED_1000_FULL BIT(5) |
| 1006 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1007 | /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). |
| 1008 | * The RTL chips use a 64 element hash table based on the Ethernet CRC. |
| 1009 | */ |
| 1010 | static const int multicast_filter_limit = 32; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1011 | static unsigned int agg_buf_sz = 16384; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1012 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1013 | #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \ |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 1014 | VLAN_ETH_HLEN - ETH_FCS_LEN) |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1015 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1016 | static |
| 1017 | int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 1018 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1019 | int ret; |
| 1020 | void *tmp; |
| 1021 | |
| 1022 | tmp = kmalloc(size, GFP_KERNEL); |
| 1023 | if (!tmp) |
| 1024 | return -ENOMEM; |
| 1025 | |
| 1026 | ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1027 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 1028 | value, index, tmp, size, 500); |
Prashant Malani | f53a7ad | 2019-08-24 01:36:19 -0700 | [diff] [blame] | 1029 | if (ret < 0) |
| 1030 | memset(data, 0xff, size); |
| 1031 | else |
| 1032 | memcpy(data, tmp, size); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1033 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1034 | kfree(tmp); |
| 1035 | |
| 1036 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static |
| 1040 | int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data) |
| 1041 | { |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1042 | int ret; |
| 1043 | void *tmp; |
| 1044 | |
Benoit Taine | c4438f0 | 2014-05-26 17:21:23 +0200 | [diff] [blame] | 1045 | tmp = kmemdup(data, size, GFP_KERNEL); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1046 | if (!tmp) |
| 1047 | return -ENOMEM; |
| 1048 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1049 | ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1050 | RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE, |
| 1051 | value, index, tmp, size, 500); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1052 | |
| 1053 | kfree(tmp); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 1054 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 1055 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1058 | static void rtl_set_unplug(struct r8152 *tp) |
| 1059 | { |
| 1060 | if (tp->udev->state == USB_STATE_NOTATTACHED) { |
| 1061 | set_bit(RTL8152_UNPLUG, &tp->flags); |
| 1062 | smp_mb__after_atomic(); |
| 1063 | } |
| 1064 | } |
| 1065 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1066 | static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1067 | void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1068 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 1069 | u16 limit = 64; |
| 1070 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1071 | |
| 1072 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1073 | return -ENODEV; |
| 1074 | |
| 1075 | /* both size and indix must be 4 bytes align */ |
| 1076 | if ((size & 3) || !size || (index & 3) || !data) |
| 1077 | return -EPERM; |
| 1078 | |
| 1079 | if ((u32)index + (u32)size > 0xffff) |
| 1080 | return -EPERM; |
| 1081 | |
| 1082 | while (size) { |
| 1083 | if (size > limit) { |
| 1084 | ret = get_registers(tp, index, type, limit, data); |
| 1085 | if (ret < 0) |
| 1086 | break; |
| 1087 | |
| 1088 | index += limit; |
| 1089 | data += limit; |
| 1090 | size -= limit; |
| 1091 | } else { |
| 1092 | ret = get_registers(tp, index, type, size, data); |
| 1093 | if (ret < 0) |
| 1094 | break; |
| 1095 | |
| 1096 | index += size; |
| 1097 | data += size; |
| 1098 | size = 0; |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1103 | if (ret == -ENODEV) |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1104 | rtl_set_unplug(tp); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1105 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1106 | return ret; |
| 1107 | } |
| 1108 | |
| 1109 | static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1110 | u16 size, void *data, u16 type) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1111 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 1112 | int ret; |
| 1113 | u16 byteen_start, byteen_end, byen; |
| 1114 | u16 limit = 512; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1117 | return -ENODEV; |
| 1118 | |
| 1119 | /* both size and indix must be 4 bytes align */ |
| 1120 | if ((size & 3) || !size || (index & 3) || !data) |
| 1121 | return -EPERM; |
| 1122 | |
| 1123 | if ((u32)index + (u32)size > 0xffff) |
| 1124 | return -EPERM; |
| 1125 | |
| 1126 | byteen_start = byteen & BYTE_EN_START_MASK; |
| 1127 | byteen_end = byteen & BYTE_EN_END_MASK; |
| 1128 | |
| 1129 | byen = byteen_start | (byteen_start << 4); |
| 1130 | ret = set_registers(tp, index, type | byen, 4, data); |
| 1131 | if (ret < 0) |
| 1132 | goto error1; |
| 1133 | |
| 1134 | index += 4; |
| 1135 | data += 4; |
| 1136 | size -= 4; |
| 1137 | |
| 1138 | if (size) { |
| 1139 | size -= 4; |
| 1140 | |
| 1141 | while (size) { |
| 1142 | if (size > limit) { |
| 1143 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1144 | type | BYTE_EN_DWORD, |
| 1145 | limit, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1146 | if (ret < 0) |
| 1147 | goto error1; |
| 1148 | |
| 1149 | index += limit; |
| 1150 | data += limit; |
| 1151 | size -= limit; |
| 1152 | } else { |
| 1153 | ret = set_registers(tp, index, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1154 | type | BYTE_EN_DWORD, |
| 1155 | size, data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1156 | if (ret < 0) |
| 1157 | goto error1; |
| 1158 | |
| 1159 | index += size; |
| 1160 | data += size; |
| 1161 | size = 0; |
| 1162 | break; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | byen = byteen_end | (byteen_end >> 4); |
| 1167 | ret = set_registers(tp, index, type | byen, 4, data); |
| 1168 | if (ret < 0) |
| 1169 | goto error1; |
| 1170 | } |
| 1171 | |
| 1172 | error1: |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1173 | if (ret == -ENODEV) |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1174 | rtl_set_unplug(tp); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1175 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | static inline |
| 1180 | int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data) |
| 1181 | { |
| 1182 | return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA); |
| 1183 | } |
| 1184 | |
| 1185 | static inline |
| 1186 | int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 1187 | { |
| 1188 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA); |
| 1189 | } |
| 1190 | |
| 1191 | static inline |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1192 | int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data) |
| 1193 | { |
| 1194 | return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB); |
| 1195 | } |
| 1196 | |
| 1197 | static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index) |
| 1198 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1199 | __le32 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1200 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1201 | generic_ocp_read(tp, index, sizeof(data), &data, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1202 | |
| 1203 | return __le32_to_cpu(data); |
| 1204 | } |
| 1205 | |
| 1206 | static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1207 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1208 | __le32 tmp = __cpu_to_le32(data); |
| 1209 | |
| 1210 | generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index) |
| 1214 | { |
| 1215 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1216 | __le32 tmp; |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 1217 | u16 byen = BYTE_EN_WORD; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1218 | u8 shift = index & 2; |
| 1219 | |
| 1220 | index &= ~3; |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 1221 | byen <<= shift; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1222 | |
hayeswang | d8fbd27 | 2017-06-15 14:44:04 +0800 | [diff] [blame] | 1223 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1224 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1225 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1226 | data >>= (shift * 8); |
| 1227 | data &= 0xffff; |
| 1228 | |
| 1229 | return (u16)data; |
| 1230 | } |
| 1231 | |
| 1232 | static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1233 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1234 | u32 mask = 0xffff; |
| 1235 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1236 | u16 byen = BYTE_EN_WORD; |
| 1237 | u8 shift = index & 2; |
| 1238 | |
| 1239 | data &= mask; |
| 1240 | |
| 1241 | if (index & 2) { |
| 1242 | byen <<= shift; |
| 1243 | mask <<= (shift * 8); |
| 1244 | data <<= (shift * 8); |
| 1245 | index &= ~3; |
| 1246 | } |
| 1247 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1248 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1249 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1250 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index) |
| 1254 | { |
| 1255 | u32 data; |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1256 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1257 | u8 shift = index & 3; |
| 1258 | |
| 1259 | index &= ~3; |
| 1260 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1261 | generic_ocp_read(tp, index, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1262 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1263 | data = __le32_to_cpu(tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1264 | data >>= (shift * 8); |
| 1265 | data &= 0xff; |
| 1266 | |
| 1267 | return (u8)data; |
| 1268 | } |
| 1269 | |
| 1270 | static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data) |
| 1271 | { |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1272 | u32 mask = 0xff; |
| 1273 | __le32 tmp; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1274 | u16 byen = BYTE_EN_BYTE; |
| 1275 | u8 shift = index & 3; |
| 1276 | |
| 1277 | data &= mask; |
| 1278 | |
| 1279 | if (index & 3) { |
| 1280 | byen <<= shift; |
| 1281 | mask <<= (shift * 8); |
| 1282 | data <<= (shift * 8); |
| 1283 | index &= ~3; |
| 1284 | } |
| 1285 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1286 | tmp = __cpu_to_le32(data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1287 | |
hayeswang | c8826de | 2013-07-31 17:21:26 +0800 | [diff] [blame] | 1288 | generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1291 | static u16 ocp_reg_read(struct r8152 *tp, u16 addr) |
| 1292 | { |
| 1293 | u16 ocp_base, ocp_index; |
| 1294 | |
| 1295 | ocp_base = addr & 0xf000; |
| 1296 | if (ocp_base != tp->ocp_base) { |
| 1297 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 1298 | tp->ocp_base = ocp_base; |
| 1299 | } |
| 1300 | |
| 1301 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 1302 | return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index); |
| 1303 | } |
| 1304 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 1305 | static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data) |
| 1306 | { |
| 1307 | u16 ocp_base, ocp_index; |
| 1308 | |
| 1309 | ocp_base = addr & 0xf000; |
| 1310 | if (ocp_base != tp->ocp_base) { |
| 1311 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base); |
| 1312 | tp->ocp_base = ocp_base; |
| 1313 | } |
| 1314 | |
| 1315 | ocp_index = (addr & 0x0fff) | 0xb000; |
| 1316 | ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data); |
| 1317 | } |
| 1318 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1319 | 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] | 1320 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1321 | ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1324 | static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1325 | { |
hayeswang | ac244d3 | 2014-01-02 11:22:40 +0800 | [diff] [blame] | 1326 | return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 1329 | static void sram_write(struct r8152 *tp, u16 addr, u16 data) |
| 1330 | { |
| 1331 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 1332 | ocp_reg_write(tp, OCP_SRAM_DATA, data); |
| 1333 | } |
| 1334 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 1335 | static u16 sram_read(struct r8152 *tp, u16 addr) |
| 1336 | { |
| 1337 | ocp_reg_write(tp, OCP_SRAM_ADDR, addr); |
| 1338 | return ocp_reg_read(tp, OCP_SRAM_DATA); |
| 1339 | } |
| 1340 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1341 | static int read_mii_word(struct net_device *netdev, int phy_id, int reg) |
| 1342 | { |
| 1343 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1344 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1345 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 1346 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1347 | return -ENODEV; |
| 1348 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1349 | if (phy_id != R8152_PHY_ID) |
| 1350 | return -EINVAL; |
| 1351 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1352 | ret = r8152_mdio_read(tp, reg); |
| 1353 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1354 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static |
| 1358 | void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val) |
| 1359 | { |
| 1360 | struct r8152 *tp = netdev_priv(netdev); |
| 1361 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 1362 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1363 | return; |
| 1364 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1365 | if (phy_id != R8152_PHY_ID) |
| 1366 | return; |
| 1367 | |
| 1368 | r8152_mdio_write(tp, reg, val); |
| 1369 | } |
| 1370 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1371 | static int |
| 1372 | 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] | 1373 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1374 | static int rtl8152_set_mac_address(struct net_device *netdev, void *p) |
| 1375 | { |
| 1376 | struct r8152 *tp = netdev_priv(netdev); |
| 1377 | struct sockaddr *addr = p; |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1378 | int ret = -EADDRNOTAVAIL; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1379 | |
| 1380 | if (!is_valid_ether_addr(addr->sa_data)) |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1381 | goto out1; |
| 1382 | |
| 1383 | ret = usb_autopm_get_interface(tp->intf); |
| 1384 | if (ret < 0) |
| 1385 | goto out1; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1386 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1387 | mutex_lock(&tp->control); |
| 1388 | |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1389 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); |
| 1390 | |
| 1391 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 1392 | pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data); |
| 1393 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 1394 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 1395 | mutex_unlock(&tp->control); |
| 1396 | |
hayeswang | ea6a711 | 2014-10-02 17:03:12 +0800 | [diff] [blame] | 1397 | usb_autopm_put_interface(tp->intf); |
| 1398 | out1: |
| 1399 | return ret; |
hayeswang | 8ba789a | 2014-09-04 16:15:41 +0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1402 | /* Devices containing proper chips can support a persistent |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1403 | * host system provided MAC address. |
| 1404 | * Examples of this are Dell TB15 and Dell WD15 docks |
| 1405 | */ |
| 1406 | static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa) |
| 1407 | { |
| 1408 | acpi_status status; |
| 1409 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1410 | union acpi_object *obj; |
| 1411 | int ret = -EINVAL; |
| 1412 | u32 ocp_data; |
| 1413 | unsigned char buf[6]; |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1414 | char *mac_obj_name; |
| 1415 | acpi_object_type mac_obj_type; |
| 1416 | int mac_strlen; |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1417 | |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1418 | if (test_bit(LENOVO_MACPASSTHRU, &tp->flags)) { |
| 1419 | mac_obj_name = "\\MACA"; |
| 1420 | mac_obj_type = ACPI_TYPE_STRING; |
| 1421 | mac_strlen = 0x16; |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1422 | } else { |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1423 | /* test for -AD variant of RTL8153 */ |
| 1424 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 1425 | if ((ocp_data & AD_MASK) == 0x1000) { |
| 1426 | /* test for MAC address pass-through bit */ |
| 1427 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE); |
| 1428 | if ((ocp_data & PASS_THRU_MASK) != 1) { |
| 1429 | netif_dbg(tp, probe, tp->netdev, |
| 1430 | "No efuse for RTL8153-AD MAC pass through\n"); |
| 1431 | return -ENODEV; |
| 1432 | } |
| 1433 | } else { |
| 1434 | /* test for RTL8153-BND and RTL8153-BD */ |
| 1435 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); |
| 1436 | if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) { |
| 1437 | netif_dbg(tp, probe, tp->netdev, |
| 1438 | "Invalid variant for MAC pass through\n"); |
| 1439 | return -ENODEV; |
| 1440 | } |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1441 | } |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1442 | |
| 1443 | mac_obj_name = "\\_SB.AMAC"; |
| 1444 | mac_obj_type = ACPI_TYPE_BUFFER; |
| 1445 | mac_strlen = 0x17; |
Mario Limonciello | 9c27369 | 2018-12-11 08:16:14 -0600 | [diff] [blame] | 1446 | } |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1447 | |
| 1448 | /* returns _AUXMAC_#AABBCCDDEEFF# */ |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1449 | status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1450 | obj = (union acpi_object *)buffer.pointer; |
| 1451 | if (!ACPI_SUCCESS(status)) |
| 1452 | return -ENODEV; |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1453 | if (obj->type != mac_obj_type || obj->string.length != mac_strlen) { |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1454 | netif_warn(tp, probe, tp->netdev, |
hayeswang | 53700f0 | 2016-09-01 17:01:42 +0800 | [diff] [blame] | 1455 | "Invalid buffer for pass-thru MAC addr: (%d, %d)\n", |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1456 | obj->type, obj->string.length); |
| 1457 | goto amacout; |
| 1458 | } |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 1459 | |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1460 | if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 || |
| 1461 | strncmp(obj->string.pointer + 0x15, "#", 1) != 0) { |
| 1462 | netif_warn(tp, probe, tp->netdev, |
| 1463 | "Invalid header when reading pass-thru MAC addr\n"); |
| 1464 | goto amacout; |
| 1465 | } |
| 1466 | ret = hex2bin(buf, obj->string.pointer + 9, 6); |
| 1467 | if (!(ret == 0 && is_valid_ether_addr(buf))) { |
| 1468 | netif_warn(tp, probe, tp->netdev, |
hayeswang | 53700f0 | 2016-09-01 17:01:42 +0800 | [diff] [blame] | 1469 | "Invalid MAC for pass-thru MAC addr: %d, %pM\n", |
| 1470 | ret, buf); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1471 | ret = -EINVAL; |
| 1472 | goto amacout; |
| 1473 | } |
| 1474 | memcpy(sa->sa_data, buf, 6); |
Mario Limonciello | 34ee32c | 2016-07-11 19:58:04 -0500 | [diff] [blame] | 1475 | netif_info(tp, probe, tp->netdev, |
| 1476 | "Using pass-thru MAC addr %pM\n", sa->sa_data); |
| 1477 | |
| 1478 | amacout: |
| 1479 | kfree(obj); |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1483 | static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa) |
| 1484 | { |
| 1485 | struct net_device *dev = tp->netdev; |
| 1486 | int ret; |
| 1487 | |
Crag.Wang | a6cbcb7 | 2019-04-22 13:03:43 +0800 | [diff] [blame] | 1488 | sa->sa_family = dev->type; |
| 1489 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1490 | if (tp->version == RTL_VER_01) { |
| 1491 | ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data); |
| 1492 | } else { |
| 1493 | /* if device doesn't support MAC pass through this will |
| 1494 | * be expected to be non-zero |
| 1495 | */ |
| 1496 | ret = vendor_mac_passthru_addr_read(tp, sa); |
| 1497 | if (ret < 0) |
| 1498 | ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data); |
| 1499 | } |
| 1500 | |
| 1501 | if (ret < 0) { |
| 1502 | netif_err(tp, probe, dev, "Get ether addr fail\n"); |
| 1503 | } else if (!is_valid_ether_addr(sa->sa_data)) { |
| 1504 | netif_err(tp, probe, dev, "Invalid ether addr %pM\n", |
| 1505 | sa->sa_data); |
| 1506 | eth_hw_addr_random(dev); |
| 1507 | ether_addr_copy(sa->sa_data, dev->dev_addr); |
| 1508 | netif_info(tp, probe, dev, "Random ether addr %pM\n", |
| 1509 | sa->sa_data); |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | return ret; |
| 1514 | } |
| 1515 | |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1516 | static int set_ethernet_addr(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1517 | { |
| 1518 | struct net_device *dev = tp->netdev; |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1519 | struct sockaddr sa; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1520 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1521 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1522 | ret = determine_ethernet_addr(tp, &sa); |
| 1523 | if (ret < 0) |
| 1524 | return ret; |
hayeswang | 8a91c82 | 2014-02-18 21:49:01 +0800 | [diff] [blame] | 1525 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 1526 | if (tp->version == RTL_VER_01) |
| 1527 | ether_addr_copy(dev->dev_addr, sa.sa_data); |
| 1528 | else |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1529 | ret = rtl8152_set_mac_address(dev, &sa); |
hayeswang | 179bb6d | 2014-09-04 16:15:42 +0800 | [diff] [blame] | 1530 | |
| 1531 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1534 | static void read_bulk_callback(struct urb *urb) |
| 1535 | { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1536 | struct net_device *netdev; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1537 | int status = urb->status; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1538 | struct rx_agg *agg; |
| 1539 | struct r8152 *tp; |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1540 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1541 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1542 | agg = urb->context; |
| 1543 | if (!agg) |
| 1544 | return; |
| 1545 | |
| 1546 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1547 | if (!tp) |
| 1548 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1549 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1550 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1551 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1552 | |
| 1553 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1554 | return; |
| 1555 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1556 | netdev = tp->netdev; |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 1557 | |
| 1558 | /* When link down, the driver would cancel all bulks. */ |
| 1559 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1560 | if (!netif_carrier_ok(netdev)) |
| 1561 | return; |
| 1562 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1563 | usb_mark_last_busy(tp->udev); |
| 1564 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1565 | switch (status) { |
| 1566 | case 0: |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1567 | if (urb->actual_length < ETH_ZLEN) |
| 1568 | break; |
| 1569 | |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1570 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1571 | list_add_tail(&agg->list, &tp->rx_done); |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1572 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1573 | napi_schedule(&tp->napi); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1574 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1575 | case -ESHUTDOWN: |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1576 | rtl_set_unplug(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1577 | netif_device_detach(tp->netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1578 | return; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1579 | case -ENOENT: |
| 1580 | return; /* the urb is in unlink state */ |
| 1581 | case -ETIME: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1582 | if (net_ratelimit()) |
| 1583 | netdev_warn(netdev, "maybe reset is needed?\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1584 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1585 | default: |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1586 | if (net_ratelimit()) |
| 1587 | netdev_warn(netdev, "Rx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1588 | break; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 1591 | r8152_submit_rx(tp, agg, GFP_ATOMIC); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static void write_bulk_callback(struct urb *urb) |
| 1595 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1596 | struct net_device_stats *stats; |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1597 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1598 | struct tx_agg *agg; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1599 | struct r8152 *tp; |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1600 | unsigned long flags; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1601 | int status = urb->status; |
| 1602 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1603 | agg = urb->context; |
| 1604 | if (!agg) |
| 1605 | return; |
| 1606 | |
| 1607 | tp = agg->context; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1608 | if (!tp) |
| 1609 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1610 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1611 | netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 1612 | stats = &netdev->stats; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1613 | if (status) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1614 | if (net_ratelimit()) |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1615 | netdev_warn(netdev, "Tx status %d\n", status); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1616 | stats->tx_errors += agg->skb_num; |
| 1617 | } else { |
| 1618 | stats->tx_packets += agg->skb_num; |
| 1619 | stats->tx_bytes += agg->skb_len; |
| 1620 | } |
| 1621 | |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1622 | spin_lock_irqsave(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1623 | list_add_tail(&agg->list, &tp->tx_free); |
Sebastian Andrzej Siewior | ed7aa30 | 2018-06-20 21:31:20 +0200 | [diff] [blame] | 1624 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1625 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 1626 | usb_autopm_put_interface_async(tp->intf); |
| 1627 | |
hayeswang | d104eaf | 2014-03-06 15:07:17 +0800 | [diff] [blame] | 1628 | if (!netif_carrier_ok(netdev)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 1629 | return; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1630 | |
| 1631 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1632 | return; |
| 1633 | |
| 1634 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1635 | return; |
| 1636 | |
| 1637 | if (!skb_queue_empty(&tp->tx_queue)) |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 1638 | tasklet_schedule(&tp->tx_tl); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1639 | } |
| 1640 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1641 | static void intr_callback(struct urb *urb) |
| 1642 | { |
| 1643 | struct r8152 *tp; |
hayeswang | 500b6d7 | 2013-11-20 17:30:57 +0800 | [diff] [blame] | 1644 | __le16 *d; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1645 | int status = urb->status; |
| 1646 | int res; |
| 1647 | |
| 1648 | tp = urb->context; |
| 1649 | if (!tp) |
| 1650 | return; |
| 1651 | |
| 1652 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 1653 | return; |
| 1654 | |
| 1655 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 1656 | return; |
| 1657 | |
| 1658 | switch (status) { |
| 1659 | case 0: /* success */ |
| 1660 | break; |
| 1661 | case -ECONNRESET: /* unlink */ |
| 1662 | case -ESHUTDOWN: |
| 1663 | netif_device_detach(tp->netdev); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 1664 | /* fall through */ |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1665 | case -ENOENT: |
hayeswang | d59c876 | 2014-10-31 13:35:57 +0800 | [diff] [blame] | 1666 | case -EPROTO: |
| 1667 | netif_info(tp, intr, tp->netdev, |
| 1668 | "Stop submitting intr, status %d\n", status); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1669 | return; |
| 1670 | case -EOVERFLOW: |
| 1671 | netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n"); |
| 1672 | goto resubmit; |
| 1673 | /* -EPIPE: should clear the halt */ |
| 1674 | default: |
| 1675 | netif_info(tp, intr, tp->netdev, "intr status %d\n", status); |
| 1676 | goto resubmit; |
| 1677 | } |
| 1678 | |
| 1679 | d = urb->transfer_buffer; |
| 1680 | if (INTR_LINK & __le16_to_cpu(d[0])) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1681 | if (!netif_carrier_ok(tp->netdev)) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1682 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1683 | schedule_delayed_work(&tp->schedule, 0); |
| 1684 | } |
| 1685 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 1686 | if (netif_carrier_ok(tp->netdev)) { |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 1687 | netif_stop_queue(tp->netdev); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1688 | set_bit(RTL8152_LINK_CHG, &tp->flags); |
| 1689 | schedule_delayed_work(&tp->schedule, 0); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | resubmit: |
| 1694 | res = usb_submit_urb(urb, GFP_ATOMIC); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1695 | if (res == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 1696 | rtl_set_unplug(tp); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1697 | netif_device_detach(tp->netdev); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1698 | } else if (res) { |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1699 | netif_err(tp, intr, tp->netdev, |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 1700 | "can't resubmit intr, status %d\n", res); |
hayeswang | 6761049 | 2014-10-30 11:46:40 +0800 | [diff] [blame] | 1701 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1702 | } |
| 1703 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1704 | static inline void *rx_agg_align(void *data) |
| 1705 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1706 | return (void *)ALIGN((uintptr_t)data, RX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static inline void *tx_agg_align(void *data) |
| 1710 | { |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1711 | return (void *)ALIGN((uintptr_t)data, TX_ALIGN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1712 | } |
| 1713 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1714 | static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg) |
| 1715 | { |
| 1716 | list_del(&agg->info_list); |
| 1717 | |
| 1718 | usb_free_urb(agg->urb); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 1719 | put_page(agg->page); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1720 | kfree(agg); |
| 1721 | |
| 1722 | atomic_dec(&tp->rx_count); |
| 1723 | } |
| 1724 | |
| 1725 | static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags) |
| 1726 | { |
| 1727 | struct net_device *netdev = tp->netdev; |
| 1728 | int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1729 | unsigned int order = get_order(tp->rx_buf_sz); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1730 | struct rx_agg *rx_agg; |
| 1731 | unsigned long flags; |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1732 | |
| 1733 | rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node); |
| 1734 | if (!rx_agg) |
| 1735 | return NULL; |
| 1736 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 1737 | rx_agg->page = alloc_pages(mflags | __GFP_COMP, order); |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1738 | if (!rx_agg->page) |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1739 | goto free_rx; |
| 1740 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1741 | rx_agg->buffer = page_address(rx_agg->page); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1742 | |
| 1743 | rx_agg->urb = usb_alloc_urb(0, mflags); |
| 1744 | if (!rx_agg->urb) |
| 1745 | goto free_buf; |
| 1746 | |
| 1747 | rx_agg->context = tp; |
| 1748 | |
| 1749 | INIT_LIST_HEAD(&rx_agg->list); |
| 1750 | INIT_LIST_HEAD(&rx_agg->info_list); |
| 1751 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 1752 | list_add_tail(&rx_agg->info_list, &tp->rx_info); |
| 1753 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1754 | |
| 1755 | atomic_inc(&tp->rx_count); |
| 1756 | |
| 1757 | return rx_agg; |
| 1758 | |
| 1759 | free_buf: |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 1760 | __free_pages(rx_agg->page, order); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1761 | free_rx: |
| 1762 | kfree(rx_agg); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1766 | static void free_all_mem(struct r8152 *tp) |
| 1767 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1768 | struct rx_agg *agg, *agg_next; |
| 1769 | unsigned long flags; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1770 | int i; |
| 1771 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1772 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1773 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1774 | list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list) |
| 1775 | free_rx_agg(tp, agg); |
| 1776 | |
| 1777 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 1778 | |
| 1779 | WARN_ON(atomic_read(&tp->rx_count)); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1780 | |
| 1781 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1782 | usb_free_urb(tp->tx_info[i].urb); |
| 1783 | tp->tx_info[i].urb = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1784 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1785 | kfree(tp->tx_info[i].buffer); |
| 1786 | tp->tx_info[i].buffer = NULL; |
| 1787 | tp->tx_info[i].head = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1788 | } |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1789 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1790 | usb_free_urb(tp->intr_urb); |
| 1791 | tp->intr_urb = NULL; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1792 | |
hayeswang | 9629e3c | 2014-01-15 10:42:15 +0800 | [diff] [blame] | 1793 | kfree(tp->intr_buff); |
| 1794 | tp->intr_buff = NULL; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | static int alloc_all_mem(struct r8152 *tp) |
| 1798 | { |
| 1799 | struct net_device *netdev = tp->netdev; |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1800 | struct usb_interface *intf = tp->intf; |
| 1801 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1802 | struct usb_host_endpoint *ep_intr = alt->endpoint + 2; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1803 | int node, i; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1804 | |
| 1805 | node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1; |
| 1806 | |
| 1807 | spin_lock_init(&tp->rx_lock); |
| 1808 | spin_lock_init(&tp->tx_lock); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1809 | INIT_LIST_HEAD(&tp->rx_info); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1810 | INIT_LIST_HEAD(&tp->tx_free); |
hayeswang | 98d068a | 2017-03-14 14:15:20 +0800 | [diff] [blame] | 1811 | INIT_LIST_HEAD(&tp->rx_done); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1812 | skb_queue_head_init(&tp->tx_queue); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 1813 | skb_queue_head_init(&tp->rx_queue); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1814 | atomic_set(&tp->rx_count, 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1815 | |
| 1816 | for (i = 0; i < RTL8152_MAX_RX; i++) { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1817 | if (!alloc_rx_agg(tp, GFP_KERNEL)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1818 | goto err1; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | for (i = 0; i < RTL8152_MAX_TX; i++) { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 1822 | struct urb *urb; |
| 1823 | u8 *buf; |
| 1824 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1825 | buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1826 | if (!buf) |
| 1827 | goto err1; |
| 1828 | |
| 1829 | if (buf != tx_agg_align(buf)) { |
| 1830 | kfree(buf); |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 1831 | buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL, |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 1832 | node); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1833 | if (!buf) |
| 1834 | goto err1; |
| 1835 | } |
| 1836 | |
| 1837 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1838 | if (!urb) { |
| 1839 | kfree(buf); |
| 1840 | goto err1; |
| 1841 | } |
| 1842 | |
| 1843 | INIT_LIST_HEAD(&tp->tx_info[i].list); |
| 1844 | tp->tx_info[i].context = tp; |
| 1845 | tp->tx_info[i].urb = urb; |
| 1846 | tp->tx_info[i].buffer = buf; |
| 1847 | tp->tx_info[i].head = tx_agg_align(buf); |
| 1848 | |
| 1849 | list_add_tail(&tp->tx_info[i].list, &tp->tx_free); |
| 1850 | } |
| 1851 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1852 | tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1853 | if (!tp->intr_urb) |
| 1854 | goto err1; |
| 1855 | |
| 1856 | tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL); |
| 1857 | if (!tp->intr_buff) |
| 1858 | goto err1; |
| 1859 | |
| 1860 | tp->intr_interval = (int)ep_intr->desc.bInterval; |
| 1861 | usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3), |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1862 | tp->intr_buff, INTBUFSIZE, intr_callback, |
| 1863 | tp, tp->intr_interval); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 1864 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 1865 | return 0; |
| 1866 | |
| 1867 | err1: |
| 1868 | free_all_mem(tp); |
| 1869 | return -ENOMEM; |
| 1870 | } |
| 1871 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1872 | static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp) |
| 1873 | { |
| 1874 | struct tx_agg *agg = NULL; |
| 1875 | unsigned long flags; |
| 1876 | |
hayeswang | 21949ab | 2014-03-07 11:04:35 +0800 | [diff] [blame] | 1877 | if (list_empty(&tp->tx_free)) |
| 1878 | return NULL; |
| 1879 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 1880 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 1881 | if (!list_empty(&tp->tx_free)) { |
| 1882 | struct list_head *cursor; |
| 1883 | |
| 1884 | cursor = tp->tx_free.next; |
| 1885 | list_del_init(cursor); |
| 1886 | agg = list_entry(cursor, struct tx_agg, list); |
| 1887 | } |
| 1888 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
| 1889 | |
| 1890 | return agg; |
| 1891 | } |
| 1892 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1893 | /* r8152_csum_workaround() |
Prashant Malani | c01ebd6 | 2019-09-30 12:38:18 -0700 | [diff] [blame] | 1894 | * The hw limits the value of the transport offset. When the offset is out of |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1895 | * range, calculate the checksum by sw. |
| 1896 | */ |
| 1897 | static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb, |
| 1898 | struct sk_buff_head *list) |
| 1899 | { |
| 1900 | if (skb_shinfo(skb)->gso_size) { |
| 1901 | netdev_features_t features = tp->netdev->features; |
| 1902 | struct sk_buff_head seg_list; |
| 1903 | struct sk_buff *segs, *nskb; |
| 1904 | |
hayeswang | a91d45f | 2014-07-11 16:48:27 +0800 | [diff] [blame] | 1905 | features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6); |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1906 | segs = skb_gso_segment(skb, features); |
| 1907 | if (IS_ERR(segs) || !segs) |
| 1908 | goto drop; |
| 1909 | |
| 1910 | __skb_queue_head_init(&seg_list); |
| 1911 | |
| 1912 | do { |
| 1913 | nskb = segs; |
| 1914 | segs = segs->next; |
| 1915 | nskb->next = NULL; |
| 1916 | __skb_queue_tail(&seg_list, nskb); |
| 1917 | } while (segs); |
| 1918 | |
| 1919 | skb_queue_splice(&seg_list, list); |
| 1920 | dev_kfree_skb(skb); |
| 1921 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1922 | if (skb_checksum_help(skb) < 0) |
| 1923 | goto drop; |
| 1924 | |
| 1925 | __skb_queue_head(list, skb); |
| 1926 | } else { |
| 1927 | struct net_device_stats *stats; |
| 1928 | |
| 1929 | drop: |
| 1930 | stats = &tp->netdev->stats; |
| 1931 | stats->tx_dropped++; |
| 1932 | dev_kfree_skb(skb); |
| 1933 | } |
| 1934 | } |
| 1935 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 1936 | /* msdn_giant_send_check() |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1937 | * According to the document of microsoft, the TCP Pseudo Header excludes the |
| 1938 | * packet length for IPv6 TCP large packets. |
| 1939 | */ |
| 1940 | static int msdn_giant_send_check(struct sk_buff *skb) |
| 1941 | { |
| 1942 | const struct ipv6hdr *ipv6h; |
| 1943 | struct tcphdr *th; |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1944 | int ret; |
| 1945 | |
| 1946 | ret = skb_cow_head(skb, 0); |
| 1947 | if (ret) |
| 1948 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1949 | |
| 1950 | ipv6h = ipv6_hdr(skb); |
| 1951 | th = tcp_hdr(skb); |
| 1952 | |
| 1953 | th->check = 0; |
| 1954 | th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0); |
| 1955 | |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 1956 | return ret; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1957 | } |
| 1958 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1959 | static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb) |
| 1960 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1961 | if (skb_vlan_tag_present(skb)) { |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1962 | u32 opts2; |
| 1963 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1964 | opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb)); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 1965 | desc->opts2 |= cpu_to_le32(opts2); |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb) |
| 1970 | { |
| 1971 | u32 opts2 = le32_to_cpu(desc->opts2); |
| 1972 | |
| 1973 | if (opts2 & RX_VLAN_TAG) |
| 1974 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
| 1975 | swab16(opts2 & 0xffff)); |
| 1976 | } |
| 1977 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1978 | static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, |
| 1979 | struct sk_buff *skb, u32 len, u32 transport_offset) |
| 1980 | { |
| 1981 | u32 mss = skb_shinfo(skb)->gso_size; |
| 1982 | u32 opts1, opts2 = 0; |
| 1983 | int ret = TX_CSUM_SUCCESS; |
| 1984 | |
| 1985 | WARN_ON_ONCE(len > TX_LEN_MAX); |
| 1986 | |
| 1987 | opts1 = len | TX_FS | TX_LS; |
| 1988 | |
| 1989 | if (mss) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 1990 | if (transport_offset > GTTCPHO_MAX) { |
| 1991 | netif_warn(tp, tx_err, tp->netdev, |
| 1992 | "Invalid transport offset 0x%x for TSO\n", |
| 1993 | transport_offset); |
| 1994 | ret = TX_CSUM_TSO; |
| 1995 | goto unavailable; |
| 1996 | } |
| 1997 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 1998 | switch (vlan_get_protocol(skb)) { |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 1999 | case htons(ETH_P_IP): |
| 2000 | opts1 |= GTSENDV4; |
| 2001 | break; |
| 2002 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2003 | case htons(ETH_P_IPV6): |
hayeswang | fcb308d | 2014-03-11 10:20:32 +0800 | [diff] [blame] | 2004 | if (msdn_giant_send_check(skb)) { |
| 2005 | ret = TX_CSUM_TSO; |
| 2006 | goto unavailable; |
| 2007 | } |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2008 | opts1 |= GTSENDV6; |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2009 | break; |
| 2010 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2011 | default: |
| 2012 | WARN_ON_ONCE(1); |
| 2013 | break; |
| 2014 | } |
| 2015 | |
| 2016 | opts1 |= transport_offset << GTTCPHO_SHIFT; |
| 2017 | opts2 |= min(mss, MSS_MAX) << MSS_SHIFT; |
| 2018 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2019 | u8 ip_protocol; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2020 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2021 | if (transport_offset > TCPHO_MAX) { |
| 2022 | netif_warn(tp, tx_err, tp->netdev, |
| 2023 | "Invalid transport offset 0x%x\n", |
| 2024 | transport_offset); |
| 2025 | ret = TX_CSUM_NONE; |
| 2026 | goto unavailable; |
| 2027 | } |
| 2028 | |
hayeswang | 6e74d17 | 2015-02-06 11:30:50 +0800 | [diff] [blame] | 2029 | switch (vlan_get_protocol(skb)) { |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2030 | case htons(ETH_P_IP): |
| 2031 | opts2 |= IPV4_CS; |
| 2032 | ip_protocol = ip_hdr(skb)->protocol; |
| 2033 | break; |
| 2034 | |
| 2035 | case htons(ETH_P_IPV6): |
| 2036 | opts2 |= IPV6_CS; |
| 2037 | ip_protocol = ipv6_hdr(skb)->nexthdr; |
| 2038 | break; |
| 2039 | |
| 2040 | default: |
| 2041 | ip_protocol = IPPROTO_RAW; |
| 2042 | break; |
| 2043 | } |
| 2044 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2045 | if (ip_protocol == IPPROTO_TCP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2046 | opts2 |= TCP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2047 | else if (ip_protocol == IPPROTO_UDP) |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2048 | opts2 |= UDP_CS; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2049 | else |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2050 | WARN_ON_ONCE(1); |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2051 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2052 | opts2 |= transport_offset << TCPHO_SHIFT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2053 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2054 | |
| 2055 | desc->opts2 = cpu_to_le32(opts2); |
| 2056 | desc->opts1 = cpu_to_le32(opts1); |
| 2057 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2058 | unavailable: |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2059 | return ret; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 2060 | } |
| 2061 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2062 | static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg) |
| 2063 | { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2064 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2065 | int remain, ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2066 | u8 *tx_data; |
| 2067 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2068 | __skb_queue_head_init(&skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2069 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2070 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2071 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2072 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2073 | tx_data = agg->head; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2074 | agg->skb_num = 0; |
| 2075 | agg->skb_len = 0; |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 2076 | remain = agg_buf_sz; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2077 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2078 | while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) { |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2079 | struct tx_desc *tx_desc; |
| 2080 | struct sk_buff *skb; |
| 2081 | unsigned int len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2082 | u32 offset; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2083 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2084 | skb = __skb_dequeue(&skb_head); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2085 | if (!skb) |
| 2086 | break; |
| 2087 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2088 | len = skb->len + sizeof(*tx_desc); |
| 2089 | |
| 2090 | if (len > remain) { |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2091 | __skb_queue_head(&skb_head, skb); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2092 | break; |
| 2093 | } |
| 2094 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2095 | tx_data = tx_agg_align(tx_data); |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2096 | tx_desc = (struct tx_desc *)tx_data; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2097 | |
| 2098 | offset = (u32)skb_transport_offset(skb); |
| 2099 | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2100 | if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) { |
| 2101 | r8152_csum_workaround(tp, skb, &skb_head); |
| 2102 | continue; |
| 2103 | } |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2104 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2105 | rtl_tx_vlan_tag(tx_desc, skb); |
| 2106 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2107 | tx_data += sizeof(*tx_desc); |
| 2108 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2109 | len = skb->len; |
| 2110 | if (skb_copy_bits(skb, 0, tx_data, len) < 0) { |
| 2111 | struct net_device_stats *stats = &tp->netdev->stats; |
| 2112 | |
| 2113 | stats->tx_dropped++; |
| 2114 | dev_kfree_skb_any(skb); |
| 2115 | tx_data -= sizeof(*tx_desc); |
| 2116 | continue; |
| 2117 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2118 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2119 | tx_data += len; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2120 | agg->skb_len += len; |
Eric Dumazet | 4c27bf3 | 2018-02-25 19:12:10 -0800 | [diff] [blame] | 2121 | agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 2122 | |
| 2123 | dev_kfree_skb_any(skb); |
| 2124 | |
hayeswang | 52aec12 | 2014-09-02 10:27:52 +0800 | [diff] [blame] | 2125 | 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] | 2126 | |
| 2127 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 2128 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2129 | } |
| 2130 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2131 | if (!skb_queue_empty(&skb_head)) { |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2132 | spin_lock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2133 | skb_queue_splice(&skb_head, tx_queue); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2134 | spin_unlock(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2135 | } |
| 2136 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2137 | netif_tx_lock(tp->netdev); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2138 | |
| 2139 | if (netif_queue_stopped(tp->netdev) && |
| 2140 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) |
| 2141 | netif_wake_queue(tp->netdev); |
| 2142 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2143 | netif_tx_unlock(tp->netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2144 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2145 | ret = usb_autopm_get_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2146 | if (ret < 0) |
| 2147 | goto out_tx_fill; |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2148 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2149 | usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2), |
| 2150 | agg->head, (int)(tx_data - (u8 *)agg->head), |
| 2151 | (usb_complete_t)write_bulk_callback, agg); |
| 2152 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2153 | ret = usb_submit_urb(agg->urb, GFP_ATOMIC); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2154 | if (ret < 0) |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2155 | usb_autopm_put_interface_async(tp->intf); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 2156 | |
| 2157 | out_tx_fill: |
| 2158 | return ret; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2159 | } |
| 2160 | |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2161 | static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc) |
| 2162 | { |
| 2163 | u8 checksum = CHECKSUM_NONE; |
| 2164 | u32 opts2, opts3; |
| 2165 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 2166 | if (!(tp->netdev->features & NETIF_F_RXCSUM)) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2167 | goto return_result; |
| 2168 | |
| 2169 | opts2 = le32_to_cpu(rx_desc->opts2); |
| 2170 | opts3 = le32_to_cpu(rx_desc->opts3); |
| 2171 | |
| 2172 | if (opts2 & RD_IPV4_CS) { |
| 2173 | if (opts3 & IPF) |
| 2174 | checksum = CHECKSUM_NONE; |
Hayes Wang | ea6499e | 2018-02-02 16:43:35 +0800 | [diff] [blame] | 2175 | else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 2176 | checksum = CHECKSUM_UNNECESSARY; |
| 2177 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2178 | checksum = CHECKSUM_UNNECESSARY; |
Mark Lord | b9a321b | 2016-10-30 19:28:27 -0400 | [diff] [blame] | 2179 | } else if (opts2 & RD_IPV6_CS) { |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 2180 | if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF)) |
| 2181 | checksum = CHECKSUM_UNNECESSARY; |
| 2182 | else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF)) |
| 2183 | checksum = CHECKSUM_UNNECESSARY; |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | return_result: |
| 2187 | return checksum; |
| 2188 | } |
| 2189 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2190 | static inline bool rx_count_exceed(struct r8152 *tp) |
| 2191 | { |
| 2192 | return atomic_read(&tp->rx_count) > RTL8152_MAX_RX; |
| 2193 | } |
| 2194 | |
| 2195 | static inline int agg_offset(struct rx_agg *agg, void *addr) |
| 2196 | { |
| 2197 | return (int)(addr - agg->buffer); |
| 2198 | } |
| 2199 | |
| 2200 | static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags) |
| 2201 | { |
| 2202 | struct rx_agg *agg, *agg_next, *agg_free = NULL; |
| 2203 | unsigned long flags; |
| 2204 | |
| 2205 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2206 | |
| 2207 | list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) { |
| 2208 | if (page_count(agg->page) == 1) { |
| 2209 | if (!agg_free) { |
| 2210 | list_del_init(&agg->list); |
| 2211 | agg_free = agg; |
| 2212 | continue; |
| 2213 | } |
| 2214 | if (rx_count_exceed(tp)) { |
| 2215 | list_del_init(&agg->list); |
| 2216 | free_rx_agg(tp, agg); |
| 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2223 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 2224 | if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending) |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2225 | agg_free = alloc_rx_agg(tp, mflags); |
| 2226 | |
| 2227 | return agg_free; |
| 2228 | } |
| 2229 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2230 | static int rx_bottom(struct r8152 *tp, int budget) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2231 | { |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 2232 | unsigned long flags; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2233 | struct list_head *cursor, *next, rx_queue; |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 2234 | int ret = 0, work_done = 0; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 2235 | struct napi_struct *napi = &tp->napi; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2236 | |
| 2237 | if (!skb_queue_empty(&tp->rx_queue)) { |
| 2238 | while (work_done < budget) { |
| 2239 | struct sk_buff *skb = __skb_dequeue(&tp->rx_queue); |
| 2240 | struct net_device *netdev = tp->netdev; |
| 2241 | struct net_device_stats *stats = &netdev->stats; |
| 2242 | unsigned int pkt_len; |
| 2243 | |
| 2244 | if (!skb) |
| 2245 | break; |
| 2246 | |
| 2247 | pkt_len = skb->len; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 2248 | napi_gro_receive(napi, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2249 | work_done++; |
| 2250 | stats->rx_packets++; |
| 2251 | stats->rx_bytes += pkt_len; |
| 2252 | } |
| 2253 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2254 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2255 | if (list_empty(&tp->rx_done)) |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2256 | goto out1; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2257 | |
| 2258 | INIT_LIST_HEAD(&rx_queue); |
hayeswang | a5a4f46 | 2013-08-16 16:09:34 +0800 | [diff] [blame] | 2259 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2260 | list_splice_init(&tp->rx_done, &rx_queue); |
| 2261 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2262 | |
| 2263 | list_for_each_safe(cursor, next, &rx_queue) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2264 | struct rx_desc *rx_desc; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2265 | struct rx_agg *agg, *agg_free; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2266 | int len_used = 0; |
| 2267 | struct urb *urb; |
| 2268 | u8 *rx_data; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2269 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2270 | list_del_init(cursor); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2271 | |
| 2272 | agg = list_entry(cursor, struct rx_agg, list); |
| 2273 | urb = agg->urb; |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 2274 | if (urb->actual_length < ETH_ZLEN) |
| 2275 | goto submit; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2276 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2277 | agg_free = rtl_get_free_rx(tp, GFP_ATOMIC); |
| 2278 | |
Hayes Wang | d55d708 | 2019-08-13 11:42:07 +0800 | [diff] [blame] | 2279 | rx_desc = agg->buffer; |
| 2280 | rx_data = agg->buffer; |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2281 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2282 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2283 | while (urb->actual_length > len_used) { |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2284 | struct net_device *netdev = tp->netdev; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2285 | struct net_device_stats *stats = &netdev->stats; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2286 | unsigned int pkt_len, rx_frag_head_sz; |
hayeswang | 43a4478 | 2013-08-16 16:09:36 +0800 | [diff] [blame] | 2287 | struct sk_buff *skb; |
| 2288 | |
hayeswang | 7454445 | 2017-06-09 17:11:47 +0800 | [diff] [blame] | 2289 | /* limite the skb numbers for rx_queue */ |
| 2290 | if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000)) |
| 2291 | break; |
| 2292 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2293 | pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2294 | if (pkt_len < ETH_ZLEN) |
| 2295 | break; |
| 2296 | |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2297 | len_used += pkt_len; |
| 2298 | if (urb->actual_length < len_used) |
| 2299 | break; |
| 2300 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2301 | pkt_len -= ETH_FCS_LEN; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2302 | rx_data += sizeof(struct rx_desc); |
| 2303 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 2304 | if (!agg_free || tp->rx_copybreak > pkt_len) |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2305 | rx_frag_head_sz = pkt_len; |
| 2306 | else |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 2307 | rx_frag_head_sz = tp->rx_copybreak; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2308 | |
| 2309 | skb = napi_alloc_skb(napi, rx_frag_head_sz); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2310 | if (!skb) { |
| 2311 | stats->rx_dropped++; |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 2312 | goto find_next_rx; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2313 | } |
hayeswang | 565cab0 | 2014-03-07 11:04:38 +0800 | [diff] [blame] | 2314 | |
| 2315 | skb->ip_summed = r8152_rx_csum(tp, rx_desc); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2316 | memcpy(skb->data, rx_data, rx_frag_head_sz); |
| 2317 | skb_put(skb, rx_frag_head_sz); |
| 2318 | pkt_len -= rx_frag_head_sz; |
| 2319 | rx_data += rx_frag_head_sz; |
| 2320 | if (pkt_len) { |
| 2321 | skb_add_rx_frag(skb, 0, agg->page, |
| 2322 | agg_offset(agg, rx_data), |
| 2323 | pkt_len, |
| 2324 | SKB_DATA_ALIGN(pkt_len)); |
| 2325 | get_page(agg->page); |
| 2326 | } |
| 2327 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2328 | skb->protocol = eth_type_trans(skb, netdev); |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2329 | rtl_rx_vlan_tag(rx_desc, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2330 | if (work_done < budget) { |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2331 | work_done++; |
| 2332 | stats->rx_packets++; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2333 | stats->rx_bytes += skb->len; |
Hayes Wang | 6636fb3 | 2019-08-19 11:15:19 +0800 | [diff] [blame] | 2334 | napi_gro_receive(napi, skb); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2335 | } else { |
| 2336 | __skb_queue_tail(&tp->rx_queue, skb); |
| 2337 | } |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2338 | |
hayeswang | 5e2f748 | 2014-03-07 11:04:37 +0800 | [diff] [blame] | 2339 | find_next_rx: |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2340 | rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2341 | rx_desc = (struct rx_desc *)rx_data; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2342 | len_used = agg_offset(agg, rx_data); |
hayeswang | 7937f9e | 2013-11-20 17:30:54 +0800 | [diff] [blame] | 2343 | len_used += sizeof(struct rx_desc); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2344 | } |
| 2345 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2346 | WARN_ON(!agg_free && page_count(agg->page) > 1); |
| 2347 | |
| 2348 | if (agg_free) { |
| 2349 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2350 | if (page_count(agg->page) == 1) { |
| 2351 | list_add(&agg_free->list, &tp->rx_used); |
| 2352 | } else { |
| 2353 | list_add_tail(&agg->list, &tp->rx_used); |
| 2354 | agg = agg_free; |
| 2355 | urb = agg->urb; |
| 2356 | } |
| 2357 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2358 | } |
| 2359 | |
hayeswang | 0de98f6 | 2013-08-16 16:09:35 +0800 | [diff] [blame] | 2360 | submit: |
hayeswang | e1a2ca9 | 2015-02-06 11:30:45 +0800 | [diff] [blame] | 2361 | if (!ret) { |
| 2362 | ret = r8152_submit_rx(tp, agg, GFP_ATOMIC); |
| 2363 | } else { |
| 2364 | urb->actual_length = 0; |
| 2365 | list_add_tail(&agg->list, next); |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | if (!list_empty(&rx_queue)) { |
| 2370 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2371 | list_splice_tail(&rx_queue, &tp->rx_done); |
| 2372 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2373 | } |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2374 | |
| 2375 | out1: |
| 2376 | return work_done; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | static void tx_bottom(struct r8152 *tp) |
| 2380 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2381 | int res; |
| 2382 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2383 | do { |
Prashant Malani | c01ebd6 | 2019-09-30 12:38:18 -0700 | [diff] [blame] | 2384 | struct net_device *netdev = tp->netdev; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2385 | struct tx_agg *agg; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2386 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2387 | if (skb_queue_empty(&tp->tx_queue)) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2388 | break; |
| 2389 | |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2390 | agg = r8152_get_tx_agg(tp); |
| 2391 | if (!agg) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2392 | break; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2393 | |
| 2394 | res = r8152_tx_agg_fill(tp, agg); |
Prashant Malani | c01ebd6 | 2019-09-30 12:38:18 -0700 | [diff] [blame] | 2395 | if (!res) |
| 2396 | continue; |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2397 | |
Prashant Malani | c01ebd6 | 2019-09-30 12:38:18 -0700 | [diff] [blame] | 2398 | if (res == -ENODEV) { |
| 2399 | rtl_set_unplug(tp); |
| 2400 | netif_device_detach(netdev); |
| 2401 | } else { |
| 2402 | struct net_device_stats *stats = &netdev->stats; |
| 2403 | unsigned long flags; |
hayeswang | 05e0f1a | 2014-03-06 15:07:18 +0800 | [diff] [blame] | 2404 | |
Prashant Malani | c01ebd6 | 2019-09-30 12:38:18 -0700 | [diff] [blame] | 2405 | netif_warn(tp, tx_err, netdev, |
| 2406 | "failed tx_urb %d\n", res); |
| 2407 | stats->tx_dropped += agg->skb_num; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 2408 | |
Prashant Malani | c01ebd6 | 2019-09-30 12:38:18 -0700 | [diff] [blame] | 2409 | spin_lock_irqsave(&tp->tx_lock, flags); |
| 2410 | list_add_tail(&agg->list, &tp->tx_free); |
| 2411 | spin_unlock_irqrestore(&tp->tx_lock, flags); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2412 | } |
hayeswang | b1379d9 | 2013-08-16 16:09:37 +0800 | [diff] [blame] | 2413 | } while (res == 0); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2414 | } |
| 2415 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2416 | static void bottom_half(unsigned long data) |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2417 | { |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2418 | struct r8152 *tp; |
| 2419 | |
| 2420 | tp = (struct r8152 *)data; |
| 2421 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2422 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2423 | return; |
| 2424 | |
| 2425 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 2426 | return; |
| 2427 | |
hayeswang | 7559fb2f | 2013-08-16 16:09:38 +0800 | [diff] [blame] | 2428 | /* When link down, the driver would cancel all bulks. */ |
| 2429 | /* This avoid the re-submitting bulk */ |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2430 | if (!netif_carrier_ok(tp->netdev)) |
| 2431 | return; |
| 2432 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2433 | clear_bit(SCHEDULE_TASKLET, &tp->flags); |
hayeswang | 9451a11 | 2014-11-12 10:05:04 +0800 | [diff] [blame] | 2434 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2435 | tx_bottom(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2436 | } |
| 2437 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2438 | static int r8152_poll(struct napi_struct *napi, int budget) |
| 2439 | { |
| 2440 | struct r8152 *tp = container_of(napi, struct r8152, napi); |
| 2441 | int work_done; |
| 2442 | |
| 2443 | work_done = rx_bottom(tp, budget); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2444 | |
| 2445 | if (work_done < budget) { |
hayeswang | a3307f9 | 2017-06-09 17:11:48 +0800 | [diff] [blame] | 2446 | if (!napi_complete_done(napi, work_done)) |
| 2447 | goto out; |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2448 | if (!list_empty(&tp->rx_done)) |
| 2449 | napi_schedule(napi); |
| 2450 | } |
| 2451 | |
hayeswang | a3307f9 | 2017-06-09 17:11:48 +0800 | [diff] [blame] | 2452 | out: |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2453 | return work_done; |
| 2454 | } |
| 2455 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2456 | static |
| 2457 | int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags) |
| 2458 | { |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2459 | int ret; |
| 2460 | |
hayeswang | ef827a5 | 2015-01-09 10:26:36 +0800 | [diff] [blame] | 2461 | /* The rx would be stopped, so skip submitting */ |
| 2462 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || |
| 2463 | !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev)) |
| 2464 | return 0; |
| 2465 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2466 | 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] | 2467 | agg->buffer, tp->rx_buf_sz, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2468 | (usb_complete_t)read_bulk_callback, agg); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2469 | |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2470 | ret = usb_submit_urb(agg->urb, mem_flags); |
| 2471 | if (ret == -ENODEV) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 2472 | rtl_set_unplug(tp); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2473 | netif_device_detach(tp->netdev); |
| 2474 | } else if (ret) { |
| 2475 | struct urb *urb = agg->urb; |
| 2476 | unsigned long flags; |
| 2477 | |
| 2478 | urb->actual_length = 0; |
| 2479 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2480 | list_add_tail(&agg->list, &tp->rx_done); |
| 2481 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2482 | |
| 2483 | netif_err(tp, rx_err, tp->netdev, |
| 2484 | "Couldn't submit rx[%p], ret = %d\n", agg, ret); |
| 2485 | |
| 2486 | napi_schedule(&tp->napi); |
hayeswang | a0fccd4 | 2014-11-20 10:29:05 +0800 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2492 | static void rtl_drop_queued_tx(struct r8152 *tp) |
| 2493 | { |
| 2494 | struct net_device_stats *stats = &tp->netdev->stats; |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2495 | struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue; |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2496 | struct sk_buff *skb; |
| 2497 | |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2498 | if (skb_queue_empty(tx_queue)) |
| 2499 | return; |
| 2500 | |
| 2501 | __skb_queue_head_init(&skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 2502 | spin_lock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2503 | skb_queue_splice_init(tx_queue, &skb_head); |
hayeswang | 2685d41 | 2014-03-07 11:04:34 +0800 | [diff] [blame] | 2504 | spin_unlock_bh(&tx_queue->lock); |
hayeswang | d84130a | 2014-02-18 21:49:02 +0800 | [diff] [blame] | 2505 | |
| 2506 | while ((skb = __skb_dequeue(&skb_head))) { |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2507 | dev_kfree_skb(skb); |
| 2508 | stats->tx_dropped++; |
| 2509 | } |
| 2510 | } |
| 2511 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2512 | static void rtl8152_tx_timeout(struct net_device *netdev) |
| 2513 | { |
| 2514 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2515 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 2516 | netif_warn(tp, tx_err, netdev, "Tx timeout\n"); |
hayeswang | 37608f3 | 2015-07-29 20:39:09 +0800 | [diff] [blame] | 2517 | |
| 2518 | usb_queue_reset_device(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | static void rtl8152_set_rx_mode(struct net_device *netdev) |
| 2522 | { |
| 2523 | struct r8152 *tp = netdev_priv(netdev); |
| 2524 | |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 2525 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2526 | set_bit(RTL8152_SET_RX_MODE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 2527 | schedule_delayed_work(&tp->schedule, 0); |
| 2528 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | static void _rtl8152_set_rx_mode(struct net_device *netdev) |
| 2532 | { |
| 2533 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2534 | u32 mc_filter[2]; /* Multicast hash filter */ |
| 2535 | __le32 tmp[2]; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2536 | u32 ocp_data; |
| 2537 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2538 | netif_stop_queue(netdev); |
| 2539 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2540 | ocp_data &= ~RCR_ACPT_ALL; |
| 2541 | ocp_data |= RCR_AB | RCR_APM; |
| 2542 | |
| 2543 | if (netdev->flags & IFF_PROMISC) { |
| 2544 | /* Unconditionally log net taps. */ |
| 2545 | netif_notice(tp, link, netdev, "Promiscuous mode enabled\n"); |
| 2546 | ocp_data |= RCR_AM | RCR_AAP; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2547 | mc_filter[1] = 0xffffffff; |
| 2548 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2549 | } else if ((netdev_mc_count(netdev) > multicast_filter_limit) || |
| 2550 | (netdev->flags & IFF_ALLMULTI)) { |
| 2551 | /* Too many to filter perfectly -- accept all multicasts. */ |
| 2552 | ocp_data |= RCR_AM; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2553 | mc_filter[1] = 0xffffffff; |
| 2554 | mc_filter[0] = 0xffffffff; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2555 | } else { |
| 2556 | struct netdev_hw_addr *ha; |
| 2557 | |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2558 | mc_filter[1] = 0; |
| 2559 | mc_filter[0] = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2560 | netdev_for_each_mc_addr(ha, netdev) { |
| 2561 | int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2562 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2563 | mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31); |
| 2564 | ocp_data |= RCR_AM; |
| 2565 | } |
| 2566 | } |
| 2567 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2568 | tmp[0] = __cpu_to_le32(swab32(mc_filter[1])); |
| 2569 | tmp[1] = __cpu_to_le32(swab32(mc_filter[0])); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2570 | |
hayeswang | 31787f5 | 2013-07-31 17:21:25 +0800 | [diff] [blame] | 2571 | pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2572 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2573 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 2576 | static netdev_features_t |
| 2577 | rtl8152_features_check(struct sk_buff *skb, struct net_device *dev, |
| 2578 | netdev_features_t features) |
| 2579 | { |
| 2580 | u32 mss = skb_shinfo(skb)->gso_size; |
| 2581 | int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX; |
| 2582 | int offset = skb_transport_offset(skb); |
| 2583 | |
| 2584 | if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset) |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 2585 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 2586 | else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz) |
| 2587 | features &= ~NETIF_F_GSO_MASK; |
| 2588 | |
| 2589 | return features; |
| 2590 | } |
| 2591 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2592 | static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb, |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2593 | struct net_device *netdev) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2594 | { |
| 2595 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2596 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2597 | skb_tx_timestamp(skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2598 | |
hayeswang | 6159878 | 2013-11-20 17:30:55 +0800 | [diff] [blame] | 2599 | skb_queue_tail(&tp->tx_queue, skb); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2600 | |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2601 | if (!list_empty(&tp->tx_free)) { |
| 2602 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) { |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2603 | set_bit(SCHEDULE_TASKLET, &tp->flags); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2604 | schedule_delayed_work(&tp->schedule, 0); |
| 2605 | } else { |
| 2606 | usb_mark_last_busy(tp->udev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 2607 | tasklet_schedule(&tp->tx_tl); |
hayeswang | 0c3121f | 2014-03-07 11:04:36 +0800 | [diff] [blame] | 2608 | } |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2609 | } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) { |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2610 | netif_stop_queue(netdev); |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2611 | } |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2612 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2613 | return NETDEV_TX_OK; |
| 2614 | } |
| 2615 | |
| 2616 | static void r8152b_reset_packet_filter(struct r8152 *tp) |
| 2617 | { |
| 2618 | u32 ocp_data; |
| 2619 | |
| 2620 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC); |
| 2621 | ocp_data &= ~FMC_FCR_MCU_EN; |
| 2622 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2623 | ocp_data |= FMC_FCR_MCU_EN; |
| 2624 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data); |
| 2625 | } |
| 2626 | |
| 2627 | static void rtl8152_nic_reset(struct r8152 *tp) |
| 2628 | { |
| 2629 | int i; |
| 2630 | |
| 2631 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST); |
| 2632 | |
| 2633 | for (i = 0; i < 1000; i++) { |
| 2634 | if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST)) |
| 2635 | break; |
hayeswang | b209af9 | 2014-08-25 15:53:00 +0800 | [diff] [blame] | 2636 | usleep_range(100, 400); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2640 | static void set_tx_qlen(struct r8152 *tp) |
| 2641 | { |
| 2642 | struct net_device *netdev = tp->netdev; |
| 2643 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 2644 | 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] | 2645 | sizeof(struct tx_desc)); |
hayeswang | dd1b119 | 2013-11-20 17:30:56 +0800 | [diff] [blame] | 2646 | } |
| 2647 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2648 | static inline u8 rtl8152_get_speed(struct r8152 *tp) |
| 2649 | { |
| 2650 | return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS); |
| 2651 | } |
| 2652 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2653 | static void rtl_set_eee_plus(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2654 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2655 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2656 | u8 speed; |
| 2657 | |
| 2658 | speed = rtl8152_get_speed(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2659 | if (speed & _10bps) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2660 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2661 | ocp_data |= EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2662 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2663 | } else { |
| 2664 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2665 | ocp_data &= ~EEEP_CR_EEEP_TX; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2666 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data); |
| 2667 | } |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2668 | } |
| 2669 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2670 | static void rxdy_gated_en(struct r8152 *tp, bool enable) |
| 2671 | { |
| 2672 | u32 ocp_data; |
| 2673 | |
| 2674 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1); |
| 2675 | if (enable) |
| 2676 | ocp_data |= RXDY_GATED_EN; |
| 2677 | else |
| 2678 | ocp_data &= ~RXDY_GATED_EN; |
| 2679 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data); |
| 2680 | } |
| 2681 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2682 | static int rtl_start_rx(struct r8152 *tp) |
| 2683 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2684 | struct rx_agg *agg, *agg_next; |
| 2685 | struct list_head tmp_list; |
| 2686 | unsigned long flags; |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2687 | int ret = 0, i = 0; |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2688 | |
| 2689 | INIT_LIST_HEAD(&tmp_list); |
| 2690 | |
| 2691 | spin_lock_irqsave(&tp->rx_lock, flags); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2692 | |
| 2693 | INIT_LIST_HEAD(&tp->rx_done); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2694 | INIT_LIST_HEAD(&tp->rx_used); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2695 | |
| 2696 | list_splice_init(&tp->rx_info, &tmp_list); |
| 2697 | |
| 2698 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2699 | |
| 2700 | list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { |
| 2701 | INIT_LIST_HEAD(&agg->list); |
| 2702 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2703 | /* Only RTL8152_MAX_RX rx_agg need to be submitted. */ |
| 2704 | if (++i > RTL8152_MAX_RX) { |
| 2705 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2706 | list_add_tail(&agg->list, &tp->rx_used); |
| 2707 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2708 | } else if (unlikely(ret < 0)) { |
| 2709 | spin_lock_irqsave(&tp->rx_lock, flags); |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2710 | list_add_tail(&agg->list, &tp->rx_done); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2711 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2712 | } else { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2713 | ret = r8152_submit_rx(tp, agg, GFP_KERNEL); |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2714 | } |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2715 | } |
| 2716 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2717 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2718 | WARN_ON(!list_empty(&tp->rx_info)); |
| 2719 | list_splice(&tmp_list, &tp->rx_info); |
| 2720 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | 7bcf4f6 | 2014-11-20 10:29:06 +0800 | [diff] [blame] | 2721 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2722 | return ret; |
| 2723 | } |
| 2724 | |
| 2725 | static int rtl_stop_rx(struct r8152 *tp) |
| 2726 | { |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2727 | struct rx_agg *agg, *agg_next; |
| 2728 | struct list_head tmp_list; |
| 2729 | unsigned long flags; |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2730 | |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2731 | INIT_LIST_HEAD(&tmp_list); |
| 2732 | |
| 2733 | /* The usb_kill_urb() couldn't be used in atomic. |
| 2734 | * Therefore, move the list of rx_info to a tmp one. |
| 2735 | * Then, list_for_each_entry_safe could be used without |
| 2736 | * spin lock. |
| 2737 | */ |
| 2738 | |
| 2739 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2740 | list_splice_init(&tp->rx_info, &tmp_list); |
| 2741 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
| 2742 | |
Hayes Wang | 47922fc | 2019-08-13 11:42:08 +0800 | [diff] [blame] | 2743 | list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) { |
| 2744 | /* At least RTL8152_MAX_RX rx_agg have the page_count being |
| 2745 | * equal to 1, so the other ones could be freed safely. |
| 2746 | */ |
| 2747 | if (page_count(agg->page) > 1) |
| 2748 | free_rx_agg(tp, agg); |
| 2749 | else |
| 2750 | usb_kill_urb(agg->urb); |
| 2751 | } |
Hayes Wang | 252df8b | 2019-08-13 11:42:06 +0800 | [diff] [blame] | 2752 | |
| 2753 | /* Move back the list of temp to the rx_info */ |
| 2754 | spin_lock_irqsave(&tp->rx_lock, flags); |
| 2755 | WARN_ON(!list_empty(&tp->rx_info)); |
| 2756 | list_splice(&tmp_list, &tp->rx_info); |
| 2757 | spin_unlock_irqrestore(&tp->rx_lock, flags); |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2758 | |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 2759 | while (!skb_queue_empty(&tp->rx_queue)) |
| 2760 | dev_kfree_skb(__skb_dequeue(&tp->rx_queue)); |
| 2761 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2762 | return 0; |
| 2763 | } |
| 2764 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 2765 | static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp) |
| 2766 | { |
| 2767 | ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN, |
| 2768 | OWN_UPDATE | OWN_CLEAR); |
| 2769 | } |
| 2770 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2771 | static int rtl_enable(struct r8152 *tp) |
| 2772 | { |
| 2773 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2774 | |
| 2775 | r8152b_reset_packet_filter(tp); |
| 2776 | |
| 2777 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR); |
| 2778 | ocp_data |= CR_RE | CR_TE; |
| 2779 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data); |
| 2780 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 2781 | switch (tp->version) { |
| 2782 | case RTL_VER_08: |
| 2783 | case RTL_VER_09: |
| 2784 | r8153b_rx_agg_chg_indicate(tp); |
| 2785 | break; |
| 2786 | default: |
| 2787 | break; |
| 2788 | } |
| 2789 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2790 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2791 | |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 2792 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2795 | static int rtl8152_enable(struct r8152 *tp) |
| 2796 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2797 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2798 | return -ENODEV; |
| 2799 | |
hayeswang | 507605a | 2014-01-02 11:22:43 +0800 | [diff] [blame] | 2800 | set_tx_qlen(tp); |
| 2801 | rtl_set_eee_plus(tp); |
| 2802 | |
| 2803 | return rtl_enable(tp); |
| 2804 | } |
| 2805 | |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2806 | static void r8153_set_rx_early_timeout(struct r8152 *tp) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2807 | { |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2808 | u32 ocp_data = tp->coalesce / 8; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2809 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2810 | switch (tp->version) { |
| 2811 | case RTL_VER_03: |
| 2812 | case RTL_VER_04: |
| 2813 | case RTL_VER_05: |
| 2814 | case RTL_VER_06: |
| 2815 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 2816 | ocp_data); |
| 2817 | break; |
| 2818 | |
| 2819 | case RTL_VER_08: |
| 2820 | case RTL_VER_09: |
| 2821 | /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout |
| 2822 | * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns. |
| 2823 | */ |
| 2824 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, |
| 2825 | 128 / 8); |
| 2826 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR, |
| 2827 | ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2828 | break; |
| 2829 | |
| 2830 | default: |
| 2831 | break; |
| 2832 | } |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | static void r8153_set_rx_early_size(struct r8152 *tp) |
| 2836 | { |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 2837 | u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2838 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2839 | switch (tp->version) { |
| 2840 | case RTL_VER_03: |
| 2841 | case RTL_VER_04: |
| 2842 | case RTL_VER_05: |
| 2843 | case RTL_VER_06: |
| 2844 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 2845 | ocp_data / 4); |
| 2846 | break; |
| 2847 | case RTL_VER_08: |
| 2848 | case RTL_VER_09: |
| 2849 | ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, |
| 2850 | ocp_data / 8); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 2851 | break; |
| 2852 | default: |
| 2853 | WARN_ON_ONCE(1); |
| 2854 | break; |
| 2855 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | static int rtl8153_enable(struct r8152 *tp) |
| 2859 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2860 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 2861 | return -ENODEV; |
| 2862 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2863 | set_tx_qlen(tp); |
| 2864 | rtl_set_eee_plus(tp); |
hayeswang | 464ec10 | 2015-02-12 14:33:46 +0800 | [diff] [blame] | 2865 | r8153_set_rx_early_timeout(tp); |
| 2866 | r8153_set_rx_early_size(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2867 | |
| 2868 | return rtl_enable(tp); |
| 2869 | } |
| 2870 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2871 | static void rtl_disable(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2872 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2873 | u32 ocp_data; |
| 2874 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2875 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2876 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2877 | rtl_drop_queued_tx(tp); |
| 2878 | return; |
| 2879 | } |
| 2880 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2881 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2882 | ocp_data &= ~RCR_ACPT_ALL; |
| 2883 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2884 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2885 | rtl_drop_queued_tx(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2886 | |
| 2887 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 2888 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2889 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2890 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2891 | |
| 2892 | for (i = 0; i < 1000; i++) { |
| 2893 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2894 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 2895 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2896 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | for (i = 0; i < 1000; i++) { |
| 2900 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 2901 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2902 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2905 | rtl_stop_rx(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2906 | |
| 2907 | rtl8152_nic_reset(tp); |
| 2908 | } |
| 2909 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2910 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 2911 | { |
| 2912 | u32 ocp_data; |
| 2913 | |
| 2914 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 2915 | if (enable) |
| 2916 | ocp_data |= POWER_CUT; |
| 2917 | else |
| 2918 | ocp_data &= ~POWER_CUT; |
| 2919 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 2920 | |
| 2921 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 2922 | ocp_data &= ~RESUME_INDICATE; |
| 2923 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2924 | } |
| 2925 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2926 | static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) |
| 2927 | { |
| 2928 | u32 ocp_data; |
| 2929 | |
| 2930 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2931 | if (enable) |
| 2932 | ocp_data |= CPCR_RX_VLAN; |
| 2933 | else |
| 2934 | ocp_data &= ~CPCR_RX_VLAN; |
| 2935 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2936 | } |
| 2937 | |
| 2938 | static int rtl8152_set_features(struct net_device *dev, |
| 2939 | netdev_features_t features) |
| 2940 | { |
| 2941 | netdev_features_t changed = features ^ dev->features; |
| 2942 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2943 | int ret; |
| 2944 | |
| 2945 | ret = usb_autopm_get_interface(tp->intf); |
| 2946 | if (ret < 0) |
| 2947 | goto out; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2948 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2949 | mutex_lock(&tp->control); |
| 2950 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2951 | if (changed & NETIF_F_HW_VLAN_CTAG_RX) { |
| 2952 | if (features & NETIF_F_HW_VLAN_CTAG_RX) |
| 2953 | rtl_rx_vlan_en(tp, true); |
| 2954 | else |
| 2955 | rtl_rx_vlan_en(tp, false); |
| 2956 | } |
| 2957 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2958 | mutex_unlock(&tp->control); |
| 2959 | |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2960 | usb_autopm_put_interface(tp->intf); |
| 2961 | |
| 2962 | out: |
| 2963 | return ret; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2964 | } |
| 2965 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2966 | #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) |
| 2967 | |
| 2968 | static u32 __rtl_get_wol(struct r8152 *tp) |
| 2969 | { |
| 2970 | u32 ocp_data; |
| 2971 | u32 wolopts = 0; |
| 2972 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2973 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2974 | if (ocp_data & LINK_ON_WAKE_EN) |
| 2975 | wolopts |= WAKE_PHY; |
| 2976 | |
| 2977 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2978 | if (ocp_data & UWF_EN) |
| 2979 | wolopts |= WAKE_UCAST; |
| 2980 | if (ocp_data & BWF_EN) |
| 2981 | wolopts |= WAKE_BCAST; |
| 2982 | if (ocp_data & MWF_EN) |
| 2983 | wolopts |= WAKE_MCAST; |
| 2984 | |
| 2985 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2986 | if (ocp_data & MAGIC_EN) |
| 2987 | wolopts |= WAKE_MAGIC; |
| 2988 | |
| 2989 | return wolopts; |
| 2990 | } |
| 2991 | |
| 2992 | static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) |
| 2993 | { |
| 2994 | u32 ocp_data; |
| 2995 | |
| 2996 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 2997 | |
| 2998 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2999 | ocp_data &= ~LINK_ON_WAKE_EN; |
| 3000 | if (wolopts & WAKE_PHY) |
| 3001 | ocp_data |= LINK_ON_WAKE_EN; |
| 3002 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3003 | |
| 3004 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
hayeswang | 92f7d07 | 2016-07-06 17:35:59 +0800 | [diff] [blame] | 3005 | ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3006 | if (wolopts & WAKE_UCAST) |
| 3007 | ocp_data |= UWF_EN; |
| 3008 | if (wolopts & WAKE_BCAST) |
| 3009 | ocp_data |= BWF_EN; |
| 3010 | if (wolopts & WAKE_MCAST) |
| 3011 | ocp_data |= MWF_EN; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3012 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); |
| 3013 | |
| 3014 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3015 | |
| 3016 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 3017 | ocp_data &= ~MAGIC_EN; |
| 3018 | if (wolopts & WAKE_MAGIC) |
| 3019 | ocp_data |= MAGIC_EN; |
| 3020 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 3021 | |
| 3022 | if (wolopts & WAKE_ANY) |
| 3023 | device_set_wakeup_enable(&tp->udev->dev, true); |
| 3024 | else |
| 3025 | device_set_wakeup_enable(&tp->udev->dev, false); |
| 3026 | } |
| 3027 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3028 | static void r8153_mac_clk_spd(struct r8152 *tp, bool enable) |
| 3029 | { |
| 3030 | /* MAC clock speed down */ |
| 3031 | if (enable) { |
| 3032 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, |
| 3033 | ALDPS_SPDWN_RATIO); |
| 3034 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, |
| 3035 | EEE_SPDWN_RATIO); |
| 3036 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, |
| 3037 | PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | |
| 3038 | U1U2_SPDWN_EN | L1_SPDWN_EN); |
| 3039 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, |
| 3040 | PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | |
| 3041 | TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN | |
| 3042 | TP1000_SPDWN_EN); |
| 3043 | } else { |
| 3044 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0); |
| 3045 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0); |
| 3046 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0); |
| 3047 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0); |
| 3048 | } |
| 3049 | } |
| 3050 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3051 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
| 3052 | { |
| 3053 | u8 u1u2[8]; |
| 3054 | |
| 3055 | if (enable) |
| 3056 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 3057 | else |
| 3058 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 3059 | |
| 3060 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 3061 | } |
| 3062 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3063 | static void r8153b_u1u2en(struct r8152 *tp, bool enable) |
| 3064 | { |
| 3065 | u32 ocp_data; |
| 3066 | |
| 3067 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); |
| 3068 | if (enable) |
| 3069 | ocp_data |= LPM_U1U2_EN; |
| 3070 | else |
| 3071 | ocp_data &= ~LPM_U1U2_EN; |
| 3072 | |
| 3073 | ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); |
| 3074 | } |
| 3075 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3076 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
| 3077 | { |
| 3078 | u32 ocp_data; |
| 3079 | |
| 3080 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3081 | if (enable) |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3082 | ocp_data |= U2P3_ENABLE; |
| 3083 | else |
| 3084 | ocp_data &= ~U2P3_ENABLE; |
| 3085 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 3086 | } |
| 3087 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3088 | static void r8153b_ups_flags(struct r8152 *tp) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3089 | { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3090 | u32 ups_flags = 0; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3091 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3092 | if (tp->ups_info.green) |
| 3093 | ups_flags |= UPS_FLAGS_EN_GREEN; |
| 3094 | |
| 3095 | if (tp->ups_info.aldps) |
| 3096 | ups_flags |= UPS_FLAGS_EN_ALDPS; |
| 3097 | |
| 3098 | if (tp->ups_info.eee) |
| 3099 | ups_flags |= UPS_FLAGS_EN_EEE; |
| 3100 | |
| 3101 | if (tp->ups_info.flow_control) |
| 3102 | ups_flags |= UPS_FLAGS_EN_FLOW_CTR; |
| 3103 | |
| 3104 | if (tp->ups_info.eee_ckdiv) |
| 3105 | ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; |
| 3106 | |
| 3107 | if (tp->ups_info.eee_cmod_lv) |
| 3108 | ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN; |
| 3109 | |
| 3110 | if (tp->ups_info._10m_ckdiv) |
| 3111 | ups_flags |= UPS_FLAGS_EN_10M_CKDIV; |
| 3112 | |
| 3113 | if (tp->ups_info.eee_plloff_100) |
| 3114 | ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; |
| 3115 | |
| 3116 | if (tp->ups_info.eee_plloff_giga) |
| 3117 | ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; |
| 3118 | |
| 3119 | if (tp->ups_info._250m_ckdiv) |
| 3120 | ups_flags |= UPS_FLAGS_250M_CKDIV; |
| 3121 | |
| 3122 | if (tp->ups_info.ctap_short_off) |
| 3123 | ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS; |
| 3124 | |
| 3125 | switch (tp->ups_info.speed_duplex) { |
| 3126 | case NWAY_10M_HALF: |
| 3127 | ups_flags |= ups_flags_speed(1); |
| 3128 | break; |
| 3129 | case NWAY_10M_FULL: |
| 3130 | ups_flags |= ups_flags_speed(2); |
| 3131 | break; |
| 3132 | case NWAY_100M_HALF: |
| 3133 | ups_flags |= ups_flags_speed(3); |
| 3134 | break; |
| 3135 | case NWAY_100M_FULL: |
| 3136 | ups_flags |= ups_flags_speed(4); |
| 3137 | break; |
| 3138 | case NWAY_1000M_FULL: |
| 3139 | ups_flags |= ups_flags_speed(5); |
| 3140 | break; |
| 3141 | case FORCE_10M_HALF: |
| 3142 | ups_flags |= ups_flags_speed(6); |
| 3143 | break; |
| 3144 | case FORCE_10M_FULL: |
| 3145 | ups_flags |= ups_flags_speed(7); |
| 3146 | break; |
| 3147 | case FORCE_100M_HALF: |
| 3148 | ups_flags |= ups_flags_speed(8); |
| 3149 | break; |
| 3150 | case FORCE_100M_FULL: |
| 3151 | ups_flags |= ups_flags_speed(9); |
| 3152 | break; |
| 3153 | default: |
| 3154 | break; |
| 3155 | } |
| 3156 | |
| 3157 | ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | static void r8153b_green_en(struct r8152 *tp, bool enable) |
| 3161 | { |
| 3162 | u16 data; |
| 3163 | |
| 3164 | if (enable) { |
| 3165 | sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ |
| 3166 | sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ |
| 3167 | sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ |
| 3168 | } else { |
| 3169 | sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ |
| 3170 | sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ |
| 3171 | sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ |
| 3172 | } |
| 3173 | |
| 3174 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 3175 | data |= GREEN_ETH_EN; |
| 3176 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 3177 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3178 | tp->ups_info.green = enable; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3179 | } |
| 3180 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 3181 | static u16 r8153_phy_status(struct r8152 *tp, u16 desired) |
| 3182 | { |
| 3183 | u16 data; |
| 3184 | int i; |
| 3185 | |
| 3186 | for (i = 0; i < 500; i++) { |
| 3187 | data = ocp_reg_read(tp, OCP_PHY_STATUS); |
| 3188 | data &= PHY_STAT_MASK; |
| 3189 | if (desired) { |
| 3190 | if (data == desired) |
| 3191 | break; |
| 3192 | } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || |
| 3193 | data == PHY_STAT_EXT_INIT) { |
| 3194 | break; |
| 3195 | } |
| 3196 | |
| 3197 | msleep(20); |
| 3198 | } |
| 3199 | |
| 3200 | return data; |
| 3201 | } |
| 3202 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3203 | static void r8153b_ups_en(struct r8152 *tp, bool enable) |
| 3204 | { |
| 3205 | u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 3206 | |
| 3207 | if (enable) { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3208 | r8153b_ups_flags(tp); |
| 3209 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3210 | ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; |
| 3211 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3212 | |
| 3213 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 3214 | ocp_data |= BIT(0); |
| 3215 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 3216 | } else { |
| 3217 | u16 data; |
| 3218 | |
| 3219 | ocp_data &= ~(UPS_EN | USP_PREWAKE); |
| 3220 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3221 | |
| 3222 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 3223 | ocp_data &= ~BIT(0); |
| 3224 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 3225 | |
| 3226 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 3227 | ocp_data &= ~PCUT_STATUS; |
| 3228 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 3229 | |
| 3230 | data = r8153_phy_status(tp, 0); |
| 3231 | |
| 3232 | switch (data) { |
| 3233 | case PHY_STAT_PWRDN: |
| 3234 | case PHY_STAT_EXT_INIT: |
| 3235 | r8153b_green_en(tp, |
| 3236 | test_bit(GREEN_ETHERNET, &tp->flags)); |
| 3237 | |
| 3238 | data = r8152_mdio_read(tp, MII_BMCR); |
| 3239 | data &= ~BMCR_PDOWN; |
| 3240 | data |= BMCR_RESET; |
| 3241 | r8152_mdio_write(tp, MII_BMCR, data); |
| 3242 | |
| 3243 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 3244 | /* fall through */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3245 | |
| 3246 | default: |
| 3247 | if (data != PHY_STAT_LAN_ON) |
| 3248 | netif_warn(tp, link, tp->netdev, |
| 3249 | "PHY not ready"); |
| 3250 | break; |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3255 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
| 3256 | { |
| 3257 | u32 ocp_data; |
| 3258 | |
| 3259 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 3260 | if (enable) |
| 3261 | ocp_data |= PWR_EN | PHASE2_EN; |
| 3262 | else |
| 3263 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 3264 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3265 | |
| 3266 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 3267 | ocp_data &= ~PCUT_STATUS; |
| 3268 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 3269 | } |
| 3270 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3271 | static void r8153b_power_cut_en(struct r8152 *tp, bool enable) |
| 3272 | { |
| 3273 | u32 ocp_data; |
| 3274 | |
| 3275 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 3276 | if (enable) |
| 3277 | ocp_data |= PWR_EN | PHASE2_EN; |
| 3278 | else |
| 3279 | ocp_data &= ~PWR_EN; |
| 3280 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3281 | |
| 3282 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 3283 | ocp_data &= ~PCUT_STATUS; |
| 3284 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 3285 | } |
| 3286 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3287 | static void r8153_queue_wake(struct r8152 *tp, bool enable) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3288 | { |
| 3289 | u32 ocp_data; |
| 3290 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3291 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3292 | if (enable) |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3293 | ocp_data |= UPCOMING_RUNTIME_D3; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3294 | else |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3295 | ocp_data &= ~UPCOMING_RUNTIME_D3; |
| 3296 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3297 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3298 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); |
| 3299 | ocp_data &= ~LINK_CHG_EVENT; |
| 3300 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); |
| 3301 | |
| 3302 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 3303 | ocp_data &= ~LINK_CHANGE_FLAG; |
| 3304 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3305 | } |
| 3306 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 3307 | static bool rtl_can_wakeup(struct r8152 *tp) |
| 3308 | { |
| 3309 | struct usb_device *udev = tp->udev; |
| 3310 | |
| 3311 | return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); |
| 3312 | } |
| 3313 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3314 | static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) |
| 3315 | { |
| 3316 | if (enable) { |
| 3317 | u32 ocp_data; |
| 3318 | |
| 3319 | __rtl_set_wol(tp, WAKE_ANY); |
| 3320 | |
| 3321 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3322 | |
| 3323 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3324 | ocp_data |= LINK_OFF_WAKE_EN; |
| 3325 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3326 | |
| 3327 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3328 | } else { |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3329 | u32 ocp_data; |
| 3330 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3331 | __rtl_set_wol(tp, tp->saved_wolopts); |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3332 | |
| 3333 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3334 | |
| 3335 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3336 | ocp_data &= ~LINK_OFF_WAKE_EN; |
| 3337 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3338 | |
| 3339 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3340 | } |
| 3341 | } |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3342 | |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3343 | static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) |
| 3344 | { |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3345 | if (enable) { |
| 3346 | r8153_u1u2en(tp, false); |
| 3347 | r8153_u2p3en(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3348 | r8153_mac_clk_spd(tp, true); |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 3349 | rtl_runtime_suspend_enable(tp, true); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3350 | } else { |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 3351 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3352 | r8153_mac_clk_spd(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3353 | |
| 3354 | switch (tp->version) { |
| 3355 | case RTL_VER_03: |
| 3356 | case RTL_VER_04: |
| 3357 | break; |
| 3358 | case RTL_VER_05: |
| 3359 | case RTL_VER_06: |
| 3360 | default: |
| 3361 | r8153_u2p3en(tp, true); |
| 3362 | break; |
| 3363 | } |
| 3364 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3365 | r8153_u1u2en(tp, true); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3366 | } |
| 3367 | } |
| 3368 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3369 | static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) |
| 3370 | { |
| 3371 | if (enable) { |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3372 | r8153_queue_wake(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3373 | r8153b_u1u2en(tp, false); |
| 3374 | r8153_u2p3en(tp, false); |
| 3375 | rtl_runtime_suspend_enable(tp, true); |
| 3376 | r8153b_ups_en(tp, true); |
| 3377 | } else { |
| 3378 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3379 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3380 | rtl_runtime_suspend_enable(tp, false); |
| 3381 | r8153_u2p3en(tp, true); |
| 3382 | r8153b_u1u2en(tp, true); |
| 3383 | } |
| 3384 | } |
| 3385 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3386 | static void r8153_teredo_off(struct r8152 *tp) |
| 3387 | { |
| 3388 | u32 ocp_data; |
| 3389 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3390 | switch (tp->version) { |
| 3391 | case RTL_VER_01: |
| 3392 | case RTL_VER_02: |
| 3393 | case RTL_VER_03: |
| 3394 | case RTL_VER_04: |
| 3395 | case RTL_VER_05: |
| 3396 | case RTL_VER_06: |
| 3397 | case RTL_VER_07: |
| 3398 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 3399 | ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | |
| 3400 | OOB_TEREDO_EN); |
| 3401 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 3402 | break; |
| 3403 | |
| 3404 | case RTL_VER_08: |
| 3405 | case RTL_VER_09: |
| 3406 | /* The bit 0 ~ 7 are relative with teredo settings. They are |
| 3407 | * W1C (write 1 to clear), so set all 1 to disable it. |
| 3408 | */ |
| 3409 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); |
| 3410 | break; |
| 3411 | |
| 3412 | default: |
| 3413 | break; |
| 3414 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3415 | |
| 3416 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); |
| 3417 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); |
| 3418 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); |
| 3419 | } |
| 3420 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3421 | static void rtl_reset_bmu(struct r8152 *tp) |
| 3422 | { |
| 3423 | u32 ocp_data; |
| 3424 | |
| 3425 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); |
| 3426 | ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); |
| 3427 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3428 | ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; |
| 3429 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3430 | } |
| 3431 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3432 | /* Clear the bp to stop the firmware before loading a new one */ |
| 3433 | static void rtl_clear_bp(struct r8152 *tp, u16 type) |
| 3434 | { |
| 3435 | switch (tp->version) { |
| 3436 | case RTL_VER_01: |
| 3437 | case RTL_VER_02: |
| 3438 | case RTL_VER_07: |
| 3439 | break; |
| 3440 | case RTL_VER_03: |
| 3441 | case RTL_VER_04: |
| 3442 | case RTL_VER_05: |
| 3443 | case RTL_VER_06: |
| 3444 | ocp_write_byte(tp, type, PLA_BP_EN, 0); |
| 3445 | break; |
| 3446 | case RTL_VER_08: |
| 3447 | case RTL_VER_09: |
| 3448 | default: |
| 3449 | if (type == MCU_TYPE_USB) { |
| 3450 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BP2_EN, 0); |
| 3451 | |
| 3452 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0); |
| 3453 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0); |
| 3454 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0); |
| 3455 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0); |
| 3456 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0); |
| 3457 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0); |
| 3458 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0); |
| 3459 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0); |
| 3460 | } else { |
| 3461 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0); |
| 3462 | } |
| 3463 | break; |
| 3464 | } |
| 3465 | |
| 3466 | ocp_write_word(tp, type, PLA_BP_0, 0); |
| 3467 | ocp_write_word(tp, type, PLA_BP_1, 0); |
| 3468 | ocp_write_word(tp, type, PLA_BP_2, 0); |
| 3469 | ocp_write_word(tp, type, PLA_BP_3, 0); |
| 3470 | ocp_write_word(tp, type, PLA_BP_4, 0); |
| 3471 | ocp_write_word(tp, type, PLA_BP_5, 0); |
| 3472 | ocp_write_word(tp, type, PLA_BP_6, 0); |
| 3473 | ocp_write_word(tp, type, PLA_BP_7, 0); |
| 3474 | |
| 3475 | /* wait 3 ms to make sure the firmware is stopped */ |
| 3476 | usleep_range(3000, 6000); |
| 3477 | ocp_write_word(tp, type, PLA_BP_BA, 0); |
| 3478 | } |
| 3479 | |
Hayes Wang | 470e391 | 2019-10-21 11:41:12 +0800 | [diff] [blame] | 3480 | static int r8153_patch_request(struct r8152 *tp, bool request) |
| 3481 | { |
| 3482 | u16 data; |
| 3483 | int i; |
| 3484 | |
| 3485 | data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); |
| 3486 | if (request) |
| 3487 | data |= PATCH_REQUEST; |
| 3488 | else |
| 3489 | data &= ~PATCH_REQUEST; |
| 3490 | ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); |
| 3491 | |
| 3492 | for (i = 0; request && i < 5000; i++) { |
| 3493 | usleep_range(1000, 2000); |
| 3494 | if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY) |
| 3495 | break; |
| 3496 | } |
| 3497 | |
| 3498 | if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { |
| 3499 | netif_err(tp, drv, tp->netdev, "patch request fail\n"); |
| 3500 | r8153_patch_request(tp, false); |
| 3501 | return -ETIME; |
| 3502 | } else { |
| 3503 | return 0; |
| 3504 | } |
| 3505 | } |
| 3506 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3507 | static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key) |
| 3508 | { |
| 3509 | if (r8153_patch_request(tp, true)) { |
| 3510 | dev_err(&tp->intf->dev, "patch request fail\n"); |
| 3511 | return -ETIME; |
| 3512 | } |
| 3513 | |
| 3514 | sram_write(tp, key_addr, patch_key); |
| 3515 | sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK); |
| 3516 | |
| 3517 | return 0; |
| 3518 | } |
| 3519 | |
| 3520 | static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr) |
| 3521 | { |
| 3522 | u16 data; |
| 3523 | |
| 3524 | sram_write(tp, 0x0000, 0x0000); |
| 3525 | |
| 3526 | data = ocp_reg_read(tp, OCP_PHY_LOCK); |
| 3527 | data &= ~PATCH_LOCK; |
| 3528 | ocp_reg_write(tp, OCP_PHY_LOCK, data); |
| 3529 | |
| 3530 | sram_write(tp, key_addr, 0x0000); |
| 3531 | |
| 3532 | r8153_patch_request(tp, false); |
| 3533 | |
| 3534 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base); |
| 3535 | |
| 3536 | return 0; |
| 3537 | } |
| 3538 | |
| 3539 | static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy) |
| 3540 | { |
| 3541 | u32 length; |
| 3542 | u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start; |
| 3543 | bool rc = false; |
| 3544 | |
| 3545 | switch (tp->version) { |
| 3546 | case RTL_VER_04: |
| 3547 | case RTL_VER_05: |
| 3548 | case RTL_VER_06: |
| 3549 | fw_reg = 0xa014; |
| 3550 | ba_reg = 0xa012; |
| 3551 | patch_en_addr = 0xa01a; |
| 3552 | mode_reg = 0xb820; |
| 3553 | bp_start = 0xa000; |
| 3554 | break; |
| 3555 | default: |
| 3556 | goto out; |
| 3557 | } |
| 3558 | |
| 3559 | fw_offset = __le16_to_cpu(phy->fw_offset); |
| 3560 | if (fw_offset < sizeof(*phy)) { |
| 3561 | dev_err(&tp->intf->dev, "fw_offset too small\n"); |
| 3562 | goto out; |
| 3563 | } |
| 3564 | |
| 3565 | length = __le32_to_cpu(phy->blk_hdr.length); |
| 3566 | if (length < fw_offset) { |
| 3567 | dev_err(&tp->intf->dev, "invalid fw_offset\n"); |
| 3568 | goto out; |
| 3569 | } |
| 3570 | |
| 3571 | length -= __le16_to_cpu(phy->fw_offset); |
| 3572 | if (!length || (length & 1)) { |
| 3573 | dev_err(&tp->intf->dev, "invalid block length\n"); |
| 3574 | goto out; |
| 3575 | } |
| 3576 | |
| 3577 | if (__le16_to_cpu(phy->fw_reg) != fw_reg) { |
| 3578 | dev_err(&tp->intf->dev, "invalid register to load firmware\n"); |
| 3579 | goto out; |
| 3580 | } |
| 3581 | |
| 3582 | if (__le16_to_cpu(phy->ba_reg) != ba_reg) { |
| 3583 | dev_err(&tp->intf->dev, "invalid base address register\n"); |
| 3584 | goto out; |
| 3585 | } |
| 3586 | |
| 3587 | if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) { |
| 3588 | dev_err(&tp->intf->dev, |
| 3589 | "invalid patch mode enabled register\n"); |
| 3590 | goto out; |
| 3591 | } |
| 3592 | |
| 3593 | if (__le16_to_cpu(phy->mode_reg) != mode_reg) { |
| 3594 | dev_err(&tp->intf->dev, |
| 3595 | "invalid register to switch the mode\n"); |
| 3596 | goto out; |
| 3597 | } |
| 3598 | |
| 3599 | if (__le16_to_cpu(phy->bp_start) != bp_start) { |
| 3600 | dev_err(&tp->intf->dev, |
| 3601 | "invalid start register of break point\n"); |
| 3602 | goto out; |
| 3603 | } |
| 3604 | |
| 3605 | if (__le16_to_cpu(phy->bp_num) > 4) { |
| 3606 | dev_err(&tp->intf->dev, "invalid break point number\n"); |
| 3607 | goto out; |
| 3608 | } |
| 3609 | |
| 3610 | rc = true; |
| 3611 | out: |
| 3612 | return rc; |
| 3613 | } |
| 3614 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3615 | static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac) |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3616 | { |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3617 | u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3618 | bool rc = false; |
| 3619 | u32 length, type; |
| 3620 | int i, max_bp; |
| 3621 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3622 | type = __le32_to_cpu(mac->blk_hdr.type); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3623 | if (type == RTL_FW_PLA) { |
| 3624 | switch (tp->version) { |
| 3625 | case RTL_VER_01: |
| 3626 | case RTL_VER_02: |
| 3627 | case RTL_VER_07: |
| 3628 | fw_reg = 0xf800; |
| 3629 | bp_ba_addr = PLA_BP_BA; |
| 3630 | bp_en_addr = 0; |
| 3631 | bp_start = PLA_BP_0; |
| 3632 | max_bp = 8; |
| 3633 | break; |
| 3634 | case RTL_VER_03: |
| 3635 | case RTL_VER_04: |
| 3636 | case RTL_VER_05: |
| 3637 | case RTL_VER_06: |
| 3638 | case RTL_VER_08: |
| 3639 | case RTL_VER_09: |
| 3640 | fw_reg = 0xf800; |
| 3641 | bp_ba_addr = PLA_BP_BA; |
| 3642 | bp_en_addr = PLA_BP_EN; |
| 3643 | bp_start = PLA_BP_0; |
| 3644 | max_bp = 8; |
| 3645 | break; |
| 3646 | default: |
| 3647 | goto out; |
| 3648 | } |
| 3649 | } else if (type == RTL_FW_USB) { |
| 3650 | switch (tp->version) { |
| 3651 | case RTL_VER_03: |
| 3652 | case RTL_VER_04: |
| 3653 | case RTL_VER_05: |
| 3654 | case RTL_VER_06: |
| 3655 | fw_reg = 0xf800; |
| 3656 | bp_ba_addr = USB_BP_BA; |
| 3657 | bp_en_addr = USB_BP_EN; |
| 3658 | bp_start = USB_BP_0; |
| 3659 | max_bp = 8; |
| 3660 | break; |
| 3661 | case RTL_VER_08: |
| 3662 | case RTL_VER_09: |
| 3663 | fw_reg = 0xe600; |
| 3664 | bp_ba_addr = USB_BP_BA; |
| 3665 | bp_en_addr = USB_BP2_EN; |
| 3666 | bp_start = USB_BP_0; |
| 3667 | max_bp = 16; |
| 3668 | break; |
| 3669 | case RTL_VER_01: |
| 3670 | case RTL_VER_02: |
| 3671 | case RTL_VER_07: |
| 3672 | default: |
| 3673 | goto out; |
| 3674 | } |
| 3675 | } else { |
| 3676 | goto out; |
| 3677 | } |
| 3678 | |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3679 | fw_offset = __le16_to_cpu(mac->fw_offset); |
| 3680 | if (fw_offset < sizeof(*mac)) { |
| 3681 | dev_err(&tp->intf->dev, "fw_offset too small\n"); |
| 3682 | goto out; |
| 3683 | } |
| 3684 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3685 | length = __le32_to_cpu(mac->blk_hdr.length); |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3686 | if (length < fw_offset) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3687 | dev_err(&tp->intf->dev, "invalid fw_offset\n"); |
| 3688 | goto out; |
| 3689 | } |
| 3690 | |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3691 | length -= fw_offset; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3692 | if (length < 4 || (length & 3)) { |
| 3693 | dev_err(&tp->intf->dev, "invalid block length\n"); |
| 3694 | goto out; |
| 3695 | } |
| 3696 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3697 | if (__le16_to_cpu(mac->fw_reg) != fw_reg) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3698 | dev_err(&tp->intf->dev, "invalid register to load firmware\n"); |
| 3699 | goto out; |
| 3700 | } |
| 3701 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3702 | if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3703 | dev_err(&tp->intf->dev, "invalid base address register\n"); |
| 3704 | goto out; |
| 3705 | } |
| 3706 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3707 | if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3708 | dev_err(&tp->intf->dev, "invalid enabled mask register\n"); |
| 3709 | goto out; |
| 3710 | } |
| 3711 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3712 | if (__le16_to_cpu(mac->bp_start) != bp_start) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3713 | dev_err(&tp->intf->dev, |
| 3714 | "invalid start register of break point\n"); |
| 3715 | goto out; |
| 3716 | } |
| 3717 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3718 | if (__le16_to_cpu(mac->bp_num) > max_bp) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3719 | dev_err(&tp->intf->dev, "invalid break point number\n"); |
| 3720 | goto out; |
| 3721 | } |
| 3722 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3723 | for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) { |
| 3724 | if (mac->bp[i]) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3725 | dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i); |
| 3726 | goto out; |
| 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | rc = true; |
| 3731 | out: |
| 3732 | return rc; |
| 3733 | } |
| 3734 | |
| 3735 | /* Verify the checksum for the firmware file. It is calculated from the version |
| 3736 | * field to the end of the file. Compare the result with the checksum field to |
| 3737 | * make sure the file is correct. |
| 3738 | */ |
| 3739 | static long rtl8152_fw_verify_checksum(struct r8152 *tp, |
| 3740 | struct fw_header *fw_hdr, size_t size) |
| 3741 | { |
| 3742 | unsigned char checksum[sizeof(fw_hdr->checksum)]; |
| 3743 | struct crypto_shash *alg; |
| 3744 | struct shash_desc *sdesc; |
| 3745 | size_t len; |
| 3746 | long rc; |
| 3747 | |
| 3748 | alg = crypto_alloc_shash("sha256", 0, 0); |
| 3749 | if (IS_ERR(alg)) { |
| 3750 | rc = PTR_ERR(alg); |
| 3751 | goto out; |
| 3752 | } |
| 3753 | |
| 3754 | if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) { |
| 3755 | rc = -EFAULT; |
| 3756 | dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n", |
| 3757 | crypto_shash_digestsize(alg)); |
| 3758 | goto free_shash; |
| 3759 | } |
| 3760 | |
| 3761 | len = sizeof(*sdesc) + crypto_shash_descsize(alg); |
| 3762 | sdesc = kmalloc(len, GFP_KERNEL); |
| 3763 | if (!sdesc) { |
| 3764 | rc = -ENOMEM; |
| 3765 | goto free_shash; |
| 3766 | } |
| 3767 | sdesc->tfm = alg; |
| 3768 | |
| 3769 | len = size - sizeof(fw_hdr->checksum); |
| 3770 | rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum); |
| 3771 | kfree(sdesc); |
| 3772 | if (rc) |
| 3773 | goto free_shash; |
| 3774 | |
| 3775 | if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) { |
| 3776 | dev_err(&tp->intf->dev, "checksum fail\n"); |
| 3777 | rc = -EFAULT; |
| 3778 | } |
| 3779 | |
| 3780 | free_shash: |
| 3781 | crypto_free_shash(alg); |
| 3782 | out: |
| 3783 | return rc; |
| 3784 | } |
| 3785 | |
| 3786 | static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw) |
| 3787 | { |
| 3788 | const struct firmware *fw = rtl_fw->fw; |
| 3789 | struct fw_header *fw_hdr = (struct fw_header *)fw->data; |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3790 | struct fw_mac *pla = NULL, *usb = NULL; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3791 | struct fw_phy_patch_key *start = NULL; |
| 3792 | struct fw_phy_nc *phy_nc = NULL; |
| 3793 | struct fw_block *stop = NULL; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3794 | long ret = -EFAULT; |
| 3795 | int i; |
| 3796 | |
| 3797 | if (fw->size < sizeof(*fw_hdr)) { |
| 3798 | dev_err(&tp->intf->dev, "file too small\n"); |
| 3799 | goto fail; |
| 3800 | } |
| 3801 | |
| 3802 | ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size); |
| 3803 | if (ret) |
| 3804 | goto fail; |
| 3805 | |
| 3806 | ret = -EFAULT; |
| 3807 | |
| 3808 | for (i = sizeof(*fw_hdr); i < fw->size;) { |
| 3809 | struct fw_block *block = (struct fw_block *)&fw->data[i]; |
| 3810 | u32 type; |
| 3811 | |
| 3812 | if ((i + sizeof(*block)) > fw->size) |
| 3813 | goto fail; |
| 3814 | |
| 3815 | type = __le32_to_cpu(block->type); |
| 3816 | switch (type) { |
| 3817 | case RTL_FW_END: |
| 3818 | if (__le32_to_cpu(block->length) != sizeof(*block)) |
| 3819 | goto fail; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3820 | goto fw_end; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3821 | case RTL_FW_PLA: |
| 3822 | if (pla) { |
| 3823 | dev_err(&tp->intf->dev, |
| 3824 | "multiple PLA firmware encountered"); |
| 3825 | goto fail; |
| 3826 | } |
| 3827 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3828 | pla = (struct fw_mac *)block; |
| 3829 | if (!rtl8152_is_fw_mac_ok(tp, pla)) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3830 | dev_err(&tp->intf->dev, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3831 | "check PLA firmware failed\n"); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3832 | goto fail; |
| 3833 | } |
| 3834 | break; |
| 3835 | case RTL_FW_USB: |
| 3836 | if (usb) { |
| 3837 | dev_err(&tp->intf->dev, |
| 3838 | "multiple USB firmware encountered"); |
| 3839 | goto fail; |
| 3840 | } |
| 3841 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3842 | usb = (struct fw_mac *)block; |
| 3843 | if (!rtl8152_is_fw_mac_ok(tp, usb)) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3844 | dev_err(&tp->intf->dev, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3845 | "check USB firmware failed\n"); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3846 | goto fail; |
| 3847 | } |
| 3848 | break; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3849 | case RTL_FW_PHY_START: |
| 3850 | if (start || phy_nc || stop) { |
| 3851 | dev_err(&tp->intf->dev, |
| 3852 | "check PHY_START fail\n"); |
| 3853 | goto fail; |
| 3854 | } |
| 3855 | |
| 3856 | if (__le32_to_cpu(block->length) != sizeof(*start)) { |
| 3857 | dev_err(&tp->intf->dev, |
| 3858 | "Invalid length for PHY_START\n"); |
| 3859 | goto fail; |
| 3860 | } |
| 3861 | |
| 3862 | start = (struct fw_phy_patch_key *)block; |
| 3863 | break; |
| 3864 | case RTL_FW_PHY_STOP: |
| 3865 | if (stop || !start) { |
| 3866 | dev_err(&tp->intf->dev, |
| 3867 | "Check PHY_STOP fail\n"); |
| 3868 | goto fail; |
| 3869 | } |
| 3870 | |
| 3871 | if (__le32_to_cpu(block->length) != sizeof(*block)) { |
| 3872 | dev_err(&tp->intf->dev, |
| 3873 | "Invalid length for PHY_STOP\n"); |
| 3874 | goto fail; |
| 3875 | } |
| 3876 | |
| 3877 | stop = block; |
| 3878 | break; |
| 3879 | case RTL_FW_PHY_NC: |
| 3880 | if (!start || stop) { |
| 3881 | dev_err(&tp->intf->dev, |
| 3882 | "check PHY_NC fail\n"); |
| 3883 | goto fail; |
| 3884 | } |
| 3885 | |
| 3886 | if (phy_nc) { |
| 3887 | dev_err(&tp->intf->dev, |
| 3888 | "multiple PHY NC encountered\n"); |
| 3889 | goto fail; |
| 3890 | } |
| 3891 | |
| 3892 | phy_nc = (struct fw_phy_nc *)block; |
| 3893 | if (!rtl8152_is_fw_phy_nc_ok(tp, phy_nc)) { |
| 3894 | dev_err(&tp->intf->dev, |
| 3895 | "check PHY NC firmware failed\n"); |
| 3896 | goto fail; |
| 3897 | } |
| 3898 | |
| 3899 | break; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3900 | default: |
| 3901 | dev_warn(&tp->intf->dev, "Unknown type %u is found\n", |
| 3902 | type); |
| 3903 | break; |
| 3904 | } |
| 3905 | |
| 3906 | /* next block */ |
| 3907 | i += ALIGN(__le32_to_cpu(block->length), 8); |
| 3908 | } |
| 3909 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3910 | fw_end: |
| 3911 | if ((phy_nc || start) && !stop) { |
| 3912 | dev_err(&tp->intf->dev, "without PHY_STOP\n"); |
| 3913 | goto fail; |
| 3914 | } |
| 3915 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3916 | return 0; |
| 3917 | fail: |
| 3918 | return ret; |
| 3919 | } |
| 3920 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3921 | static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy) |
| 3922 | { |
| 3923 | u16 mode_reg, bp_index; |
| 3924 | u32 length, i, num; |
| 3925 | __le16 *data; |
| 3926 | |
| 3927 | mode_reg = __le16_to_cpu(phy->mode_reg); |
| 3928 | sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre)); |
| 3929 | sram_write(tp, __le16_to_cpu(phy->ba_reg), |
| 3930 | __le16_to_cpu(phy->ba_data)); |
| 3931 | |
| 3932 | length = __le32_to_cpu(phy->blk_hdr.length); |
| 3933 | length -= __le16_to_cpu(phy->fw_offset); |
| 3934 | num = length / 2; |
| 3935 | data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); |
| 3936 | |
| 3937 | ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); |
| 3938 | for (i = 0; i < num; i++) |
| 3939 | ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); |
| 3940 | |
| 3941 | sram_write(tp, __le16_to_cpu(phy->patch_en_addr), |
| 3942 | __le16_to_cpu(phy->patch_en_value)); |
| 3943 | |
| 3944 | bp_index = __le16_to_cpu(phy->bp_start); |
| 3945 | num = __le16_to_cpu(phy->bp_num); |
| 3946 | for (i = 0; i < num; i++) { |
| 3947 | sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i])); |
| 3948 | bp_index += 2; |
| 3949 | } |
| 3950 | |
| 3951 | sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post)); |
| 3952 | |
| 3953 | dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); |
| 3954 | } |
| 3955 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3956 | static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac) |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3957 | { |
| 3958 | u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg; |
| 3959 | u32 length; |
| 3960 | u8 *data; |
| 3961 | int i; |
| 3962 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3963 | switch (__le32_to_cpu(mac->blk_hdr.type)) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3964 | case RTL_FW_PLA: |
| 3965 | type = MCU_TYPE_PLA; |
| 3966 | break; |
| 3967 | case RTL_FW_USB: |
| 3968 | type = MCU_TYPE_USB; |
| 3969 | break; |
| 3970 | default: |
| 3971 | return; |
| 3972 | } |
| 3973 | |
| 3974 | rtl_clear_bp(tp, type); |
| 3975 | |
| 3976 | /* Enable backup/restore of MACDBG. This is required after clearing PLA |
| 3977 | * break points and before applying the PLA firmware. |
| 3978 | */ |
| 3979 | if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA && |
| 3980 | !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) { |
| 3981 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM); |
| 3982 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM); |
| 3983 | } |
| 3984 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3985 | length = __le32_to_cpu(mac->blk_hdr.length); |
| 3986 | length -= __le16_to_cpu(mac->fw_offset); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3987 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3988 | data = (u8 *)mac; |
| 3989 | data += __le16_to_cpu(mac->fw_offset); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3990 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3991 | generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data, |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3992 | type); |
| 3993 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3994 | ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr), |
| 3995 | __le16_to_cpu(mac->bp_ba_value)); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3996 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3997 | bp_index = __le16_to_cpu(mac->bp_start); |
| 3998 | bp_num = __le16_to_cpu(mac->bp_num); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3999 | for (i = 0; i < bp_num; i++) { |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4000 | ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i])); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4001 | bp_index += 2; |
| 4002 | } |
| 4003 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4004 | bp_en_addr = __le16_to_cpu(mac->bp_en_addr); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4005 | if (bp_en_addr) |
| 4006 | ocp_write_word(tp, type, bp_en_addr, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4007 | __le16_to_cpu(mac->bp_en_value)); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4008 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4009 | fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4010 | if (fw_ver_reg) |
| 4011 | ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4012 | mac->fw_ver_data); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4013 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4014 | dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | static void rtl8152_apply_firmware(struct r8152 *tp) |
| 4018 | { |
| 4019 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
Hayes Wang | 8e484eb | 2019-10-23 21:24:44 +0800 | [diff] [blame] | 4020 | const struct firmware *fw; |
| 4021 | struct fw_header *fw_hdr; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 4022 | struct fw_phy_patch_key *key; |
| 4023 | u16 key_addr = 0; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4024 | int i; |
| 4025 | |
| 4026 | if (IS_ERR_OR_NULL(rtl_fw->fw)) |
| 4027 | return; |
| 4028 | |
Hayes Wang | 8e484eb | 2019-10-23 21:24:44 +0800 | [diff] [blame] | 4029 | fw = rtl_fw->fw; |
| 4030 | fw_hdr = (struct fw_header *)fw->data; |
| 4031 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4032 | if (rtl_fw->pre_fw) |
| 4033 | rtl_fw->pre_fw(tp); |
| 4034 | |
| 4035 | for (i = offsetof(struct fw_header, blocks); i < fw->size;) { |
| 4036 | struct fw_block *block = (struct fw_block *)&fw->data[i]; |
| 4037 | |
| 4038 | switch (__le32_to_cpu(block->type)) { |
| 4039 | case RTL_FW_END: |
| 4040 | goto post_fw; |
| 4041 | case RTL_FW_PLA: |
| 4042 | case RTL_FW_USB: |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4043 | rtl8152_fw_mac_apply(tp, (struct fw_mac *)block); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4044 | break; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 4045 | case RTL_FW_PHY_START: |
| 4046 | key = (struct fw_phy_patch_key *)block; |
| 4047 | key_addr = __le16_to_cpu(key->key_reg); |
| 4048 | r8153_pre_ram_code(tp, key_addr, |
| 4049 | __le16_to_cpu(key->key_data)); |
| 4050 | break; |
| 4051 | case RTL_FW_PHY_STOP: |
| 4052 | WARN_ON(!key_addr); |
| 4053 | r8153_post_ram_code(tp, key_addr); |
| 4054 | break; |
| 4055 | case RTL_FW_PHY_NC: |
| 4056 | rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block); |
| 4057 | break; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4058 | default: |
| 4059 | break; |
| 4060 | } |
| 4061 | |
| 4062 | i += ALIGN(__le32_to_cpu(block->length), 8); |
| 4063 | } |
| 4064 | |
| 4065 | post_fw: |
| 4066 | if (rtl_fw->post_fw) |
| 4067 | rtl_fw->post_fw(tp); |
| 4068 | |
| 4069 | strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE); |
| 4070 | dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version); |
| 4071 | } |
| 4072 | |
| 4073 | static void rtl8152_release_firmware(struct r8152 *tp) |
| 4074 | { |
| 4075 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
| 4076 | |
| 4077 | if (!IS_ERR_OR_NULL(rtl_fw->fw)) { |
| 4078 | release_firmware(rtl_fw->fw); |
| 4079 | rtl_fw->fw = NULL; |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | static int rtl8152_request_firmware(struct r8152 *tp) |
| 4084 | { |
| 4085 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
| 4086 | long rc; |
| 4087 | |
| 4088 | if (rtl_fw->fw || !rtl_fw->fw_name) { |
| 4089 | dev_info(&tp->intf->dev, "skip request firmware\n"); |
| 4090 | rc = 0; |
| 4091 | goto result; |
| 4092 | } |
| 4093 | |
| 4094 | rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev); |
| 4095 | if (rc < 0) |
| 4096 | goto result; |
| 4097 | |
| 4098 | rc = rtl8152_check_firmware(tp, rtl_fw); |
| 4099 | if (rc < 0) |
| 4100 | release_firmware(rtl_fw->fw); |
| 4101 | |
| 4102 | result: |
| 4103 | if (rc) { |
| 4104 | rtl_fw->fw = ERR_PTR(rc); |
| 4105 | |
| 4106 | dev_warn(&tp->intf->dev, |
| 4107 | "unable to load firmware patch %s (%ld)\n", |
| 4108 | rtl_fw->fw_name, rc); |
| 4109 | } |
| 4110 | |
| 4111 | return rc; |
| 4112 | } |
| 4113 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4114 | static void r8152_aldps_en(struct r8152 *tp, bool enable) |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4115 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4116 | if (enable) { |
| 4117 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 4118 | LINKENA | DIS_SDSAVE); |
| 4119 | } else { |
| 4120 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | |
| 4121 | DIS_SDSAVE); |
| 4122 | msleep(20); |
| 4123 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4124 | } |
| 4125 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4126 | static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) |
| 4127 | { |
| 4128 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); |
| 4129 | ocp_reg_write(tp, OCP_EEE_DATA, reg); |
| 4130 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); |
| 4131 | } |
| 4132 | |
| 4133 | static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) |
| 4134 | { |
| 4135 | u16 data; |
| 4136 | |
| 4137 | r8152_mmd_indirect(tp, dev, reg); |
| 4138 | data = ocp_reg_read(tp, OCP_EEE_DATA); |
| 4139 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 4140 | |
| 4141 | return data; |
| 4142 | } |
| 4143 | |
| 4144 | static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) |
| 4145 | { |
| 4146 | r8152_mmd_indirect(tp, dev, reg); |
| 4147 | ocp_reg_write(tp, OCP_EEE_DATA, data); |
| 4148 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 4149 | } |
| 4150 | |
| 4151 | static void r8152_eee_en(struct r8152 *tp, bool enable) |
| 4152 | { |
| 4153 | u16 config1, config2, config3; |
| 4154 | u32 ocp_data; |
| 4155 | |
| 4156 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 4157 | config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; |
| 4158 | config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); |
| 4159 | config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; |
| 4160 | |
| 4161 | if (enable) { |
| 4162 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 4163 | config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; |
| 4164 | config1 |= sd_rise_time(1); |
| 4165 | config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; |
| 4166 | config3 |= fast_snr(42); |
| 4167 | } else { |
| 4168 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 4169 | config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | |
| 4170 | RX_QUIET_EN); |
| 4171 | config1 |= sd_rise_time(7); |
| 4172 | config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); |
| 4173 | config3 |= fast_snr(511); |
| 4174 | } |
| 4175 | |
| 4176 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 4177 | ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); |
| 4178 | ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); |
| 4179 | ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); |
| 4180 | } |
| 4181 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4182 | static void r8153_eee_en(struct r8152 *tp, bool enable) |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4183 | { |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4184 | u32 ocp_data; |
| 4185 | u16 config; |
| 4186 | |
| 4187 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 4188 | config = ocp_reg_read(tp, OCP_EEE_CFG); |
| 4189 | |
| 4190 | if (enable) { |
| 4191 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 4192 | config |= EEE10_EN; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 4193 | } else { |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4194 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 4195 | config &= ~EEE10_EN; |
| 4196 | } |
| 4197 | |
| 4198 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 4199 | ocp_reg_write(tp, OCP_EEE_CFG, config); |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4200 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4201 | tp->ups_info.eee = enable; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | static void rtl_eee_enable(struct r8152 *tp, bool enable) |
| 4205 | { |
| 4206 | switch (tp->version) { |
| 4207 | case RTL_VER_01: |
| 4208 | case RTL_VER_02: |
| 4209 | case RTL_VER_07: |
| 4210 | if (enable) { |
| 4211 | r8152_eee_en(tp, true); |
| 4212 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, |
| 4213 | tp->eee_adv); |
| 4214 | } else { |
| 4215 | r8152_eee_en(tp, false); |
| 4216 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); |
| 4217 | } |
| 4218 | break; |
| 4219 | case RTL_VER_03: |
| 4220 | case RTL_VER_04: |
| 4221 | case RTL_VER_05: |
| 4222 | case RTL_VER_06: |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4223 | case RTL_VER_08: |
| 4224 | case RTL_VER_09: |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4225 | if (enable) { |
| 4226 | r8153_eee_en(tp, true); |
| 4227 | ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); |
| 4228 | } else { |
| 4229 | r8153_eee_en(tp, false); |
| 4230 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
| 4231 | } |
| 4232 | break; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4233 | default: |
| 4234 | break; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 4235 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | static void r8152b_enable_fc(struct r8152 *tp) |
| 4239 | { |
| 4240 | u16 anar; |
| 4241 | |
| 4242 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 4243 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 4244 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4245 | |
| 4246 | tp->ups_info.flow_control = true; |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4247 | } |
| 4248 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4249 | static void rtl8152_disable(struct r8152 *tp) |
| 4250 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4251 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4252 | rtl_disable(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4253 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4254 | } |
| 4255 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4256 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 4257 | { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4258 | rtl8152_apply_firmware(tp); |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4259 | rtl_eee_enable(tp, tp->eee_en); |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 4260 | r8152_aldps_en(tp, true); |
| 4261 | r8152b_enable_fc(tp); |
hayeswang | f0cbe0a | 2014-02-18 21:49:03 +0800 | [diff] [blame] | 4262 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4263 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4264 | } |
| 4265 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4266 | static void wait_oob_link_list_ready(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4267 | { |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 4268 | u32 ocp_data; |
| 4269 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4270 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4271 | for (i = 0; i < 1000; i++) { |
| 4272 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4273 | if (ocp_data & LINK_LIST_READY) |
| 4274 | break; |
| 4275 | usleep_range(1000, 2000); |
| 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | static void r8152b_exit_oob(struct r8152 *tp) |
| 4280 | { |
| 4281 | u32 ocp_data; |
| 4282 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4283 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4284 | ocp_data &= ~RCR_ACPT_ALL; |
| 4285 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4286 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4287 | rxdy_gated_en(tp, true); |
hayeswang | da9bd11 | 2014-02-18 21:49:08 +0800 | [diff] [blame] | 4288 | r8153_teredo_off(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4289 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 4290 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 4291 | |
| 4292 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4293 | ocp_data &= ~NOW_IS_OOB; |
| 4294 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4295 | |
| 4296 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4297 | ocp_data &= ~MCU_BORW_EN; |
| 4298 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4299 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4300 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4301 | |
| 4302 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4303 | ocp_data |= RE_INIT_LL; |
| 4304 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4305 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4306 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4307 | |
| 4308 | rtl8152_nic_reset(tp); |
| 4309 | |
| 4310 | /* rx share fifo credit full threshold */ |
| 4311 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 4312 | |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 4313 | if (tp->udev->speed == USB_SPEED_FULL || |
| 4314 | tp->udev->speed == USB_SPEED_LOW) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4315 | /* rx share fifo credit near full threshold */ |
| 4316 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 4317 | RXFIFO_THR2_FULL); |
| 4318 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 4319 | RXFIFO_THR3_FULL); |
| 4320 | } else { |
| 4321 | /* rx share fifo credit near full threshold */ |
| 4322 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 4323 | RXFIFO_THR2_HIGH); |
| 4324 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 4325 | RXFIFO_THR3_HIGH); |
| 4326 | } |
| 4327 | |
| 4328 | /* TX share fifo free credit full threshold */ |
| 4329 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 4330 | |
| 4331 | ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD); |
hayeswang | 8e1f51b | 2014-01-02 11:22:41 +0800 | [diff] [blame] | 4332 | 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] | 4333 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 4334 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 4335 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4336 | 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] | 4337 | |
| 4338 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 4339 | |
| 4340 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 4341 | ocp_data |= TCR0_AUTO_FIFO; |
| 4342 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 4343 | } |
| 4344 | |
| 4345 | static void r8152b_enter_oob(struct r8152 *tp) |
| 4346 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 4347 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4348 | |
| 4349 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4350 | ocp_data &= ~NOW_IS_OOB; |
| 4351 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4352 | |
| 4353 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 4354 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 4355 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 4356 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4357 | rtl_disable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4358 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4359 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4360 | |
| 4361 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4362 | ocp_data |= RE_INIT_LL; |
| 4363 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4364 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4365 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4366 | |
| 4367 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 4368 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4369 | rtl_rx_vlan_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4370 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4371 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4372 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4373 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4374 | |
| 4375 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4376 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 4377 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4378 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4379 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4380 | |
| 4381 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4382 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 4383 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4384 | } |
| 4385 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4386 | static int r8153_pre_firmware_1(struct r8152 *tp) |
| 4387 | { |
| 4388 | int i; |
| 4389 | |
| 4390 | /* Wait till the WTD timer is ready. It would take at most 104 ms. */ |
| 4391 | for (i = 0; i < 104; i++) { |
| 4392 | u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL); |
| 4393 | |
| 4394 | if (!(ocp_data & WTD1_EN)) |
| 4395 | break; |
| 4396 | usleep_range(1000, 2000); |
| 4397 | } |
| 4398 | |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
| 4402 | static int r8153_post_firmware_1(struct r8152 *tp) |
| 4403 | { |
| 4404 | /* set USB_BP_4 to support USB_SPEED_SUPER only */ |
| 4405 | if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) |
| 4406 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY); |
| 4407 | |
| 4408 | /* reset UPHY timer to 36 ms */ |
| 4409 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); |
| 4410 | |
| 4411 | return 0; |
| 4412 | } |
| 4413 | |
| 4414 | static int r8153_pre_firmware_2(struct r8152 *tp) |
| 4415 | { |
| 4416 | u32 ocp_data; |
| 4417 | |
| 4418 | r8153_pre_firmware_1(tp); |
| 4419 | |
| 4420 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); |
| 4421 | ocp_data &= ~FW_FIX_SUSPEND; |
| 4422 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); |
| 4423 | |
| 4424 | return 0; |
| 4425 | } |
| 4426 | |
| 4427 | static int r8153_post_firmware_2(struct r8152 *tp) |
| 4428 | { |
| 4429 | u32 ocp_data; |
| 4430 | |
| 4431 | /* enable bp0 if support USB_SPEED_SUPER only */ |
| 4432 | if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) { |
| 4433 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); |
| 4434 | ocp_data |= BIT(0); |
| 4435 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); |
| 4436 | } |
| 4437 | |
| 4438 | /* reset UPHY timer to 36 ms */ |
| 4439 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); |
| 4440 | |
| 4441 | /* enable U3P3 check, set the counter to 4 */ |
| 4442 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4); |
| 4443 | |
| 4444 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); |
| 4445 | ocp_data |= FW_FIX_SUSPEND; |
| 4446 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); |
| 4447 | |
| 4448 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 4449 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 4450 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 4451 | |
| 4452 | return 0; |
| 4453 | } |
| 4454 | |
| 4455 | static int r8153_post_firmware_3(struct r8152 *tp) |
| 4456 | { |
| 4457 | u32 ocp_data; |
| 4458 | |
| 4459 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 4460 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 4461 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 4462 | |
| 4463 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); |
| 4464 | ocp_data |= FW_IP_RESET_EN; |
| 4465 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); |
| 4466 | |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
| 4470 | static int r8153b_pre_firmware_1(struct r8152 *tp) |
| 4471 | { |
| 4472 | /* enable fc timer and set timer to 1 second. */ |
| 4473 | ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, |
| 4474 | CTRL_TIMER_EN | (1000 / 8)); |
| 4475 | |
| 4476 | return 0; |
| 4477 | } |
| 4478 | |
| 4479 | static int r8153b_post_firmware_1(struct r8152 *tp) |
| 4480 | { |
| 4481 | u32 ocp_data; |
| 4482 | |
| 4483 | /* enable bp0 for RTL8153-BND */ |
| 4484 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); |
| 4485 | if (ocp_data & BND_MASK) { |
| 4486 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); |
| 4487 | ocp_data |= BIT(0); |
| 4488 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); |
| 4489 | } |
| 4490 | |
| 4491 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); |
| 4492 | ocp_data |= FLOW_CTRL_PATCH_OPT; |
| 4493 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); |
| 4494 | |
| 4495 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); |
| 4496 | ocp_data |= FC_PATCH_TASK; |
| 4497 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); |
| 4498 | |
| 4499 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); |
| 4500 | ocp_data |= FW_IP_RESET_EN; |
| 4501 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); |
| 4502 | |
| 4503 | return 0; |
| 4504 | } |
| 4505 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4506 | static void r8153_aldps_en(struct r8152 *tp, bool enable) |
| 4507 | { |
| 4508 | u16 data; |
| 4509 | |
| 4510 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4511 | if (enable) { |
| 4512 | data |= EN_ALDPS; |
| 4513 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 4514 | } else { |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 4515 | int i; |
| 4516 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4517 | data &= ~EN_ALDPS; |
| 4518 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 4519 | for (i = 0; i < 20; i++) { |
| 4520 | usleep_range(1000, 2000); |
| 4521 | if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) |
| 4522 | break; |
| 4523 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4524 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4525 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4526 | tp->ups_info.aldps = enable; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4527 | } |
| 4528 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4529 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 4530 | { |
| 4531 | u32 ocp_data; |
| 4532 | u16 data; |
| 4533 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 4534 | /* disable ALDPS before updating the PHY parameters */ |
| 4535 | r8153_aldps_en(tp, false); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 4536 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 4537 | /* disable EEE before updating the PHY parameters */ |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4538 | rtl_eee_enable(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4539 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4540 | rtl8152_apply_firmware(tp); |
| 4541 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4542 | if (tp->version == RTL_VER_03) { |
| 4543 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 4544 | data &= ~CTAP_SHORT_EN; |
| 4545 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 4546 | } |
| 4547 | |
| 4548 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4549 | data |= EEE_CLKDIV_EN; |
| 4550 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 4551 | |
| 4552 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 4553 | data |= EN_10M_BGOFF; |
| 4554 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 4555 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4556 | data |= EN_10M_PLLOFF; |
| 4557 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 4558 | sram_write(tp, SRAM_IMPEDANCE, 0x0b13); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4559 | |
| 4560 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 4561 | ocp_data |= PFM_PWM_SWITCH; |
| 4562 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 4563 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 4564 | /* Enable LPF corner auto tune */ |
| 4565 | sram_write(tp, SRAM_LPF_CFG, 0xf70f); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4566 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 4567 | /* Adjust 10M Amplitude */ |
| 4568 | sram_write(tp, SRAM_10M_AMP1, 0x00af); |
| 4569 | sram_write(tp, SRAM_10M_AMP2, 0x0208); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4570 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4571 | if (tp->eee_en) |
| 4572 | rtl_eee_enable(tp, true); |
hayeswang | af0287e | 2016-09-20 16:22:08 +0800 | [diff] [blame] | 4573 | |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 4574 | r8153_aldps_en(tp, true); |
| 4575 | r8152b_enable_fc(tp); |
| 4576 | |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 4577 | switch (tp->version) { |
| 4578 | case RTL_VER_03: |
| 4579 | case RTL_VER_04: |
| 4580 | break; |
| 4581 | case RTL_VER_05: |
| 4582 | case RTL_VER_06: |
| 4583 | default: |
| 4584 | r8153_u2p3en(tp, true); |
| 4585 | break; |
| 4586 | } |
| 4587 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4588 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4589 | } |
| 4590 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4591 | static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) |
| 4592 | { |
| 4593 | u32 ocp_data; |
| 4594 | |
| 4595 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); |
| 4596 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); |
| 4597 | ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ |
| 4598 | ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); |
| 4599 | |
| 4600 | return ocp_data; |
| 4601 | } |
| 4602 | |
| 4603 | static void r8153b_hw_phy_cfg(struct r8152 *tp) |
| 4604 | { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4605 | u32 ocp_data; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4606 | u16 data; |
| 4607 | |
| 4608 | /* disable ALDPS before updating the PHY parameters */ |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4609 | r8153_aldps_en(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4610 | |
| 4611 | /* disable EEE before updating the PHY parameters */ |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4612 | rtl_eee_enable(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4613 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4614 | rtl8152_apply_firmware(tp); |
| 4615 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4616 | r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); |
| 4617 | |
| 4618 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 4619 | data |= R_TUNE_EN; |
| 4620 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 4621 | data = ocp_reg_read(tp, OCP_NCTL_CFG); |
| 4622 | data |= PGA_RETURN_EN; |
| 4623 | ocp_reg_write(tp, OCP_NCTL_CFG, data); |
| 4624 | |
| 4625 | /* ADC Bias Calibration: |
| 4626 | * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake |
| 4627 | * bit (bit3) to rebuild the real 16-bit data. Write the data to the |
| 4628 | * ADC ioffset. |
| 4629 | */ |
| 4630 | ocp_data = r8152_efuse_read(tp, 0x7d); |
| 4631 | data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); |
| 4632 | if (data != 0xffff) |
| 4633 | ocp_reg_write(tp, OCP_ADC_IOFFSET, data); |
| 4634 | |
| 4635 | /* ups mode tx-link-pulse timing adjustment: |
| 4636 | * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] |
| 4637 | * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt |
| 4638 | */ |
| 4639 | ocp_data = ocp_reg_read(tp, 0xc426); |
| 4640 | ocp_data &= 0x3fff; |
| 4641 | if (ocp_data) { |
| 4642 | u32 swr_cnt_1ms_ini; |
| 4643 | |
| 4644 | swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; |
| 4645 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); |
| 4646 | ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; |
| 4647 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); |
| 4648 | } |
| 4649 | |
| 4650 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 4651 | ocp_data |= PFM_PWM_SWITCH; |
| 4652 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 4653 | |
| 4654 | /* Advnace EEE */ |
| 4655 | if (!r8153_patch_request(tp, true)) { |
| 4656 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4657 | data |= EEE_CLKDIV_EN; |
| 4658 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4659 | tp->ups_info.eee_ckdiv = true; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4660 | |
| 4661 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 4662 | data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; |
| 4663 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4664 | tp->ups_info.eee_cmod_lv = true; |
| 4665 | tp->ups_info._10m_ckdiv = true; |
| 4666 | tp->ups_info.eee_plloff_giga = true; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4667 | |
| 4668 | ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); |
| 4669 | ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4670 | tp->ups_info._250m_ckdiv = true; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4671 | |
| 4672 | r8153_patch_request(tp, false); |
| 4673 | } |
| 4674 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4675 | if (tp->eee_en) |
| 4676 | rtl_eee_enable(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4677 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4678 | r8153_aldps_en(tp, true); |
| 4679 | r8152b_enable_fc(tp); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4680 | r8153_u2p3en(tp, true); |
| 4681 | |
| 4682 | set_bit(PHY_RESET, &tp->flags); |
| 4683 | } |
| 4684 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4685 | static void r8153_first_init(struct r8152 *tp) |
| 4686 | { |
| 4687 | u32 ocp_data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4688 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 4689 | r8153_mac_clk_spd(tp, false); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4690 | rxdy_gated_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4691 | r8153_teredo_off(tp); |
| 4692 | |
| 4693 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4694 | ocp_data &= ~RCR_ACPT_ALL; |
| 4695 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4696 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4697 | rtl8152_nic_reset(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 4698 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4699 | |
| 4700 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4701 | ocp_data &= ~NOW_IS_OOB; |
| 4702 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4703 | |
| 4704 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4705 | ocp_data &= ~MCU_BORW_EN; |
| 4706 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4707 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4708 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4709 | |
| 4710 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4711 | ocp_data |= RE_INIT_LL; |
| 4712 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4713 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4714 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4715 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4716 | 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] | 4717 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 4718 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 4719 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 4720 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4721 | |
| 4722 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 4723 | ocp_data |= TCR0_AUTO_FIFO; |
| 4724 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 4725 | |
| 4726 | rtl8152_nic_reset(tp); |
| 4727 | |
| 4728 | /* rx share fifo credit full threshold */ |
| 4729 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 4730 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 4731 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 4732 | /* TX share fifo free credit full threshold */ |
| 4733 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | static void r8153_enter_oob(struct r8152 *tp) |
| 4737 | { |
| 4738 | u32 ocp_data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4739 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 4740 | r8153_mac_clk_spd(tp, true); |
| 4741 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4742 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4743 | ocp_data &= ~NOW_IS_OOB; |
| 4744 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4745 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4746 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 4747 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4748 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4749 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4750 | |
| 4751 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4752 | ocp_data |= RE_INIT_LL; |
| 4753 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4754 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4755 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4756 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 4757 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 4758 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4759 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4760 | switch (tp->version) { |
| 4761 | case RTL_VER_03: |
| 4762 | case RTL_VER_04: |
| 4763 | case RTL_VER_05: |
| 4764 | case RTL_VER_06: |
| 4765 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 4766 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 4767 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 4768 | break; |
| 4769 | |
| 4770 | case RTL_VER_08: |
| 4771 | case RTL_VER_09: |
| 4772 | /* Clear teredo wake event. bit[15:8] is the teredo wakeup |
| 4773 | * type. Set it to zero. bits[7:0] are the W1C bits about |
| 4774 | * the events. Set them to all 1 to clear them. |
| 4775 | */ |
| 4776 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); |
| 4777 | break; |
| 4778 | |
| 4779 | default: |
| 4780 | break; |
| 4781 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4782 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4783 | rtl_rx_vlan_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4784 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4785 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4786 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4787 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4788 | |
| 4789 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4790 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 4791 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4792 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4793 | rxdy_gated_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4794 | |
| 4795 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4796 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 4797 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4798 | } |
| 4799 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4800 | static void rtl8153_disable(struct r8152 *tp) |
| 4801 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4802 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4803 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 4804 | rtl_reset_bmu(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4805 | r8153_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4806 | } |
| 4807 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4808 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, |
| 4809 | u32 advertising) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4810 | { |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4811 | u16 bmcr; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4812 | int ret = 0; |
| 4813 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4814 | if (autoneg == AUTONEG_DISABLE) { |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4815 | if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL) |
| 4816 | return -EINVAL; |
| 4817 | |
| 4818 | switch (speed) { |
| 4819 | case SPEED_10: |
| 4820 | bmcr = BMCR_SPEED10; |
| 4821 | if (duplex == DUPLEX_FULL) { |
| 4822 | bmcr |= BMCR_FULLDPLX; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4823 | tp->ups_info.speed_duplex = FORCE_10M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4824 | } else { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4825 | tp->ups_info.speed_duplex = FORCE_10M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4826 | } |
| 4827 | break; |
| 4828 | case SPEED_100: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4829 | bmcr = BMCR_SPEED100; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4830 | if (duplex == DUPLEX_FULL) { |
| 4831 | bmcr |= BMCR_FULLDPLX; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4832 | tp->ups_info.speed_duplex = FORCE_100M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4833 | } else { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4834 | tp->ups_info.speed_duplex = FORCE_100M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4835 | } |
| 4836 | break; |
| 4837 | case SPEED_1000: |
| 4838 | if (tp->mii.supports_gmii) { |
| 4839 | bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4840 | tp->ups_info.speed_duplex = NWAY_1000M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4841 | break; |
| 4842 | } |
| 4843 | /* fall through */ |
| 4844 | default: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4845 | ret = -EINVAL; |
| 4846 | goto out; |
| 4847 | } |
| 4848 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4849 | if (duplex == DUPLEX_FULL) |
| 4850 | tp->mii.full_duplex = 1; |
| 4851 | else |
| 4852 | tp->mii.full_duplex = 0; |
| 4853 | |
| 4854 | tp->mii.force_media = 1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4855 | } else { |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4856 | u16 anar, tmp1; |
| 4857 | u32 support; |
| 4858 | |
| 4859 | support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | |
| 4860 | RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; |
| 4861 | |
| 4862 | if (tp->mii.supports_gmii) |
| 4863 | support |= RTL_ADVERTISED_1000_FULL; |
| 4864 | |
| 4865 | if (!(advertising & support)) |
| 4866 | return -EINVAL; |
| 4867 | |
| 4868 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 4869 | tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 4870 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
| 4871 | if (advertising & RTL_ADVERTISED_10_HALF) { |
| 4872 | tmp1 |= ADVERTISE_10HALF; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4873 | tp->ups_info.speed_duplex = NWAY_10M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4874 | } |
| 4875 | if (advertising & RTL_ADVERTISED_10_FULL) { |
| 4876 | tmp1 |= ADVERTISE_10FULL; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4877 | tp->ups_info.speed_duplex = NWAY_10M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | if (advertising & RTL_ADVERTISED_100_HALF) { |
| 4881 | tmp1 |= ADVERTISE_100HALF; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4882 | tp->ups_info.speed_duplex = NWAY_100M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4883 | } |
| 4884 | if (advertising & RTL_ADVERTISED_100_FULL) { |
| 4885 | tmp1 |= ADVERTISE_100FULL; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4886 | tp->ups_info.speed_duplex = NWAY_100M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | if (anar != tmp1) { |
| 4890 | r8152_mdio_write(tp, MII_ADVERTISE, tmp1); |
| 4891 | tp->mii.advertising = tmp1; |
| 4892 | } |
| 4893 | |
| 4894 | if (tp->mii.supports_gmii) { |
| 4895 | u16 gbcr; |
| 4896 | |
| 4897 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 4898 | tmp1 = gbcr & ~(ADVERTISE_1000FULL | |
| 4899 | ADVERTISE_1000HALF); |
| 4900 | |
| 4901 | if (advertising & RTL_ADVERTISED_1000_FULL) { |
| 4902 | tmp1 |= ADVERTISE_1000FULL; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4903 | tp->ups_info.speed_duplex = NWAY_1000M_FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4904 | } |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4905 | |
| 4906 | if (gbcr != tmp1) |
| 4907 | r8152_mdio_write(tp, MII_CTRL1000, tmp1); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4911 | |
| 4912 | tp->mii.force_media = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 4915 | if (test_and_clear_bit(PHY_RESET, &tp->flags)) |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4916 | bmcr |= BMCR_RESET; |
| 4917 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4918 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 4919 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 4920 | if (bmcr & BMCR_RESET) { |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4921 | int i; |
| 4922 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4923 | for (i = 0; i < 50; i++) { |
| 4924 | msleep(20); |
| 4925 | if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) |
| 4926 | break; |
| 4927 | } |
| 4928 | } |
| 4929 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4930 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4931 | return ret; |
| 4932 | } |
| 4933 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4934 | static void rtl8152_up(struct r8152 *tp) |
| 4935 | { |
| 4936 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4937 | return; |
| 4938 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4939 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4940 | r8152b_exit_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4941 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4942 | } |
| 4943 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4944 | static void rtl8152_down(struct r8152 *tp) |
| 4945 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4946 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 4947 | rtl_drop_queued_tx(tp); |
| 4948 | return; |
| 4949 | } |
| 4950 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4951 | r8152_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4952 | r8152_aldps_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4953 | r8152b_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4954 | r8152_aldps_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4957 | static void rtl8153_up(struct r8152 *tp) |
| 4958 | { |
| 4959 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4960 | return; |
| 4961 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 4962 | r8153_u1u2en(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 4963 | r8153_u2p3en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4964 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4965 | r8153_first_init(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4966 | r8153_aldps_en(tp, true); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 4967 | |
| 4968 | switch (tp->version) { |
| 4969 | case RTL_VER_03: |
| 4970 | case RTL_VER_04: |
| 4971 | break; |
| 4972 | case RTL_VER_05: |
| 4973 | case RTL_VER_06: |
| 4974 | default: |
| 4975 | r8153_u2p3en(tp, true); |
| 4976 | break; |
| 4977 | } |
| 4978 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 4979 | r8153_u1u2en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4980 | } |
| 4981 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4982 | static void rtl8153_down(struct r8152 *tp) |
| 4983 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4984 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 4985 | rtl_drop_queued_tx(tp); |
| 4986 | return; |
| 4987 | } |
| 4988 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4989 | r8153_u1u2en(tp, false); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 4990 | r8153_u2p3en(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 4991 | r8153_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4992 | r8153_aldps_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4993 | r8153_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4994 | r8153_aldps_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4995 | } |
| 4996 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4997 | static void rtl8153b_up(struct r8152 *tp) |
| 4998 | { |
| 4999 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5000 | return; |
| 5001 | |
| 5002 | r8153b_u1u2en(tp, false); |
| 5003 | r8153_u2p3en(tp, false); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5004 | r8153_aldps_en(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5005 | |
| 5006 | r8153_first_init(tp); |
| 5007 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); |
| 5008 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5009 | r8153_aldps_en(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5010 | r8153_u2p3en(tp, true); |
| 5011 | r8153b_u1u2en(tp, true); |
| 5012 | } |
| 5013 | |
| 5014 | static void rtl8153b_down(struct r8152 *tp) |
| 5015 | { |
| 5016 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 5017 | rtl_drop_queued_tx(tp); |
| 5018 | return; |
| 5019 | } |
| 5020 | |
| 5021 | r8153b_u1u2en(tp, false); |
| 5022 | r8153_u2p3en(tp, false); |
| 5023 | r8153b_power_cut_en(tp, false); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5024 | r8153_aldps_en(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5025 | r8153_enter_oob(tp); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5026 | r8153_aldps_en(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5027 | } |
| 5028 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5029 | static bool rtl8152_in_nway(struct r8152 *tp) |
| 5030 | { |
| 5031 | u16 nway_state; |
| 5032 | |
| 5033 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); |
| 5034 | tp->ocp_base = 0x2000; |
| 5035 | ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ |
| 5036 | nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); |
| 5037 | |
| 5038 | /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ |
| 5039 | if (nway_state & 0xc000) |
| 5040 | return false; |
| 5041 | else |
| 5042 | return true; |
| 5043 | } |
| 5044 | |
| 5045 | static bool rtl8153_in_nway(struct r8152 *tp) |
| 5046 | { |
| 5047 | u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; |
| 5048 | |
| 5049 | if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) |
| 5050 | return false; |
| 5051 | else |
| 5052 | return true; |
| 5053 | } |
| 5054 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5055 | static void set_carrier(struct r8152 *tp) |
| 5056 | { |
| 5057 | struct net_device *netdev = tp->netdev; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5058 | struct napi_struct *napi = &tp->napi; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5059 | u8 speed; |
| 5060 | |
| 5061 | speed = rtl8152_get_speed(tp); |
| 5062 | |
| 5063 | if (speed & LINK_STATUS) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 5064 | if (!netif_carrier_ok(netdev)) { |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5065 | tp->rtl_ops.enable(tp); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5066 | netif_stop_queue(netdev); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5067 | napi_disable(napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5068 | netif_carrier_on(netdev); |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 5069 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 5070 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 5071 | _rtl8152_set_rx_mode(netdev); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 5072 | napi_enable(&tp->napi); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5073 | netif_wake_queue(netdev); |
| 5074 | netif_info(tp, link, netdev, "carrier on\n"); |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 5075 | } else if (netif_queue_stopped(netdev) && |
| 5076 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { |
| 5077 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5078 | } |
| 5079 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 5080 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5081 | netif_carrier_off(netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5082 | tasklet_disable(&tp->tx_tl); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5083 | napi_disable(napi); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5084 | tp->rtl_ops.disable(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5085 | napi_enable(napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5086 | tasklet_enable(&tp->tx_tl); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5087 | netif_info(tp, link, netdev, "carrier off\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5088 | } |
| 5089 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | static void rtl_work_func_t(struct work_struct *work) |
| 5093 | { |
| 5094 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 5095 | |
hayeswang | a1f83fe | 2014-11-12 10:05:05 +0800 | [diff] [blame] | 5096 | /* If the device is unplugged or !netif_running(), the workqueue |
| 5097 | * doesn't need to wake the device, and could return directly. |
| 5098 | */ |
| 5099 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) |
| 5100 | return; |
| 5101 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5102 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5103 | return; |
| 5104 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5105 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 5106 | goto out1; |
| 5107 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5108 | if (!mutex_trylock(&tp->control)) { |
| 5109 | schedule_delayed_work(&tp->schedule, 0); |
| 5110 | goto out1; |
| 5111 | } |
| 5112 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 5113 | if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 5114 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5115 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 5116 | if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5117 | _rtl8152_set_rx_mode(tp->netdev); |
| 5118 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5119 | /* don't schedule tasket before linking */ |
| 5120 | if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 5121 | netif_carrier_ok(tp->netdev)) |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5122 | tasklet_schedule(&tp->tx_tl); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 5123 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5124 | mutex_unlock(&tp->control); |
| 5125 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5126 | out1: |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5127 | usb_autopm_put_interface(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5128 | } |
| 5129 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5130 | static void rtl_hw_phy_work_func_t(struct work_struct *work) |
| 5131 | { |
| 5132 | struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); |
| 5133 | |
| 5134 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5135 | return; |
| 5136 | |
| 5137 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5138 | return; |
| 5139 | |
| 5140 | mutex_lock(&tp->control); |
| 5141 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5142 | if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) { |
| 5143 | tp->rtl_fw.retry = false; |
| 5144 | tp->rtl_fw.fw = NULL; |
| 5145 | |
| 5146 | /* Delay execution in case request_firmware() is not ready yet. |
| 5147 | */ |
| 5148 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10); |
| 5149 | goto ignore_once; |
| 5150 | } |
| 5151 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5152 | tp->rtl_ops.hw_phy_cfg(tp); |
| 5153 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5154 | rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex, |
| 5155 | tp->advertising); |
hayeswang | 9d21c0d | 2016-06-13 17:49:37 +0800 | [diff] [blame] | 5156 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5157 | ignore_once: |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5158 | mutex_unlock(&tp->control); |
| 5159 | |
| 5160 | usb_autopm_put_interface(tp->intf); |
| 5161 | } |
| 5162 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 5163 | #ifdef CONFIG_PM_SLEEP |
| 5164 | static int rtl_notifier(struct notifier_block *nb, unsigned long action, |
| 5165 | void *data) |
| 5166 | { |
| 5167 | struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); |
| 5168 | |
| 5169 | switch (action) { |
| 5170 | case PM_HIBERNATION_PREPARE: |
| 5171 | case PM_SUSPEND_PREPARE: |
| 5172 | usb_autopm_get_interface(tp->intf); |
| 5173 | break; |
| 5174 | |
| 5175 | case PM_POST_HIBERNATION: |
| 5176 | case PM_POST_SUSPEND: |
| 5177 | usb_autopm_put_interface(tp->intf); |
| 5178 | break; |
| 5179 | |
| 5180 | case PM_POST_RESTORE: |
| 5181 | case PM_RESTORE_PREPARE: |
| 5182 | default: |
| 5183 | break; |
| 5184 | } |
| 5185 | |
| 5186 | return NOTIFY_DONE; |
| 5187 | } |
| 5188 | #endif |
| 5189 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5190 | static int rtl8152_open(struct net_device *netdev) |
| 5191 | { |
| 5192 | struct r8152 *tp = netdev_priv(netdev); |
| 5193 | int res = 0; |
| 5194 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5195 | if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) { |
| 5196 | cancel_delayed_work_sync(&tp->hw_phy_work); |
| 5197 | rtl_hw_phy_work_func_t(&tp->hw_phy_work.work); |
| 5198 | } |
| 5199 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5200 | res = alloc_all_mem(tp); |
| 5201 | if (res) |
| 5202 | goto out; |
| 5203 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5204 | res = usb_autopm_get_interface(tp->intf); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5205 | if (res < 0) |
| 5206 | goto out_free; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5207 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5208 | mutex_lock(&tp->control); |
| 5209 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5210 | tp->rtl_ops.up(tp); |
| 5211 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 5212 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5213 | netif_start_queue(netdev); |
| 5214 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 5215 | |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5216 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 5217 | if (res) { |
| 5218 | if (res == -ENODEV) |
| 5219 | netif_device_detach(tp->netdev); |
| 5220 | netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", |
| 5221 | res); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5222 | goto out_unlock; |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5223 | } |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5224 | napi_enable(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5225 | tasklet_enable(&tp->tx_tl); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5226 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5227 | mutex_unlock(&tp->control); |
| 5228 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5229 | usb_autopm_put_interface(tp->intf); |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 5230 | #ifdef CONFIG_PM_SLEEP |
| 5231 | tp->pm_notifier.notifier_call = rtl_notifier; |
| 5232 | register_pm_notifier(&tp->pm_notifier); |
| 5233 | #endif |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5234 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5235 | |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5236 | out_unlock: |
| 5237 | mutex_unlock(&tp->control); |
| 5238 | usb_autopm_put_interface(tp->intf); |
| 5239 | out_free: |
| 5240 | free_all_mem(tp); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5241 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5242 | return res; |
| 5243 | } |
| 5244 | |
| 5245 | static int rtl8152_close(struct net_device *netdev) |
| 5246 | { |
| 5247 | struct r8152 *tp = netdev_priv(netdev); |
| 5248 | int res = 0; |
| 5249 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 5250 | #ifdef CONFIG_PM_SLEEP |
| 5251 | unregister_pm_notifier(&tp->pm_notifier); |
| 5252 | #endif |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5253 | tasklet_disable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5254 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5255 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5256 | cancel_delayed_work_sync(&tp->schedule); |
Prashant Malani | 8481141 | 2019-11-20 11:40:21 -0800 | [diff] [blame] | 5257 | napi_disable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5258 | netif_stop_queue(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5259 | |
| 5260 | res = usb_autopm_get_interface(tp->intf); |
hayeswang | 53543db | 2015-02-06 11:30:48 +0800 | [diff] [blame] | 5261 | if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5262 | rtl_drop_queued_tx(tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5263 | rtl_stop_rx(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5264 | } else { |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5265 | mutex_lock(&tp->control); |
| 5266 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5267 | tp->rtl_ops.down(tp); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5268 | |
| 5269 | mutex_unlock(&tp->control); |
| 5270 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5271 | usb_autopm_put_interface(tp->intf); |
| 5272 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5273 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5274 | free_all_mem(tp); |
| 5275 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5276 | return res; |
| 5277 | } |
| 5278 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5279 | static void rtl_tally_reset(struct r8152 *tp) |
| 5280 | { |
| 5281 | u32 ocp_data; |
| 5282 | |
| 5283 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); |
| 5284 | ocp_data |= TALLY_RESET; |
| 5285 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); |
| 5286 | } |
| 5287 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5288 | static void r8152b_init(struct r8152 *tp) |
| 5289 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5290 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5291 | u16 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5292 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5293 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5294 | return; |
| 5295 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5296 | data = r8152_mdio_read(tp, MII_BMCR); |
| 5297 | if (data & BMCR_PDOWN) { |
| 5298 | data &= ~BMCR_PDOWN; |
| 5299 | r8152_mdio_write(tp, MII_BMCR, data); |
| 5300 | } |
| 5301 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 5302 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 5303 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5304 | if (tp->version == RTL_VER_01) { |
| 5305 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 5306 | ocp_data &= ~LED_MODE_MASK; |
| 5307 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 5308 | } |
| 5309 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 5310 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5311 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5312 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 5313 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 5314 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 5315 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 5316 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 5317 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 5318 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 5319 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 5320 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 5321 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 5322 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5323 | rtl_tally_reset(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5324 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5325 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5326 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 5327 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5328 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 5329 | } |
| 5330 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5331 | static void r8153_init(struct r8152 *tp) |
| 5332 | { |
| 5333 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5334 | u16 data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5335 | int i; |
| 5336 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5337 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5338 | return; |
| 5339 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5340 | r8153_u1u2en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5341 | |
| 5342 | for (i = 0; i < 500; i++) { |
| 5343 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 5344 | AUTOLOAD_DONE) |
| 5345 | break; |
| 5346 | msleep(20); |
| 5347 | } |
| 5348 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 5349 | data = r8153_phy_status(tp, 0); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5350 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5351 | if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || |
| 5352 | tp->version == RTL_VER_05) |
| 5353 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 5354 | |
| 5355 | data = r8152_mdio_read(tp, MII_BMCR); |
| 5356 | if (data & BMCR_PDOWN) { |
| 5357 | data &= ~BMCR_PDOWN; |
| 5358 | r8152_mdio_write(tp, MII_BMCR, data); |
| 5359 | } |
| 5360 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 5361 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5362 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5363 | r8153_u2p3en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5364 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5365 | if (tp->version == RTL_VER_04) { |
| 5366 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); |
| 5367 | ocp_data &= ~pwd_dn_scale_mask; |
| 5368 | ocp_data |= pwd_dn_scale(96); |
| 5369 | ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); |
| 5370 | |
| 5371 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 5372 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 5373 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 5374 | } else if (tp->version == RTL_VER_05) { |
| 5375 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); |
| 5376 | ocp_data &= ~ECM_ALDPS; |
| 5377 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); |
| 5378 | |
| 5379 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 5380 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 5381 | ocp_data &= ~DYNAMIC_BURST; |
| 5382 | else |
| 5383 | ocp_data |= DYNAMIC_BURST; |
| 5384 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 5385 | } else if (tp->version == RTL_VER_06) { |
| 5386 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 5387 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 5388 | ocp_data &= ~DYNAMIC_BURST; |
| 5389 | else |
| 5390 | ocp_data |= DYNAMIC_BURST; |
| 5391 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame^] | 5392 | |
| 5393 | r8153_queue_wake(tp, false); |
| 5394 | |
| 5395 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 5396 | if (rtl8152_get_speed(tp) & LINK_STATUS) |
| 5397 | ocp_data |= CUR_LINK_OK; |
| 5398 | else |
| 5399 | ocp_data &= ~CUR_LINK_OK; |
| 5400 | ocp_data |= POLL_LINK_CHG; |
| 5401 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); |
| 5405 | ocp_data |= EP4_FULL_FC; |
| 5406 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); |
| 5407 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5408 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 5409 | ocp_data &= ~TIMER11_EN; |
| 5410 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 5411 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5412 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 5413 | ocp_data &= ~LED_MODE_MASK; |
| 5414 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 5415 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5416 | ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; |
Oliver Neukum | 2b84af94a | 2016-05-02 13:06:14 +0200 | [diff] [blame] | 5417 | if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5418 | ocp_data |= LPM_TIMER_500MS; |
hayeswang | 34203e2 | 2015-02-06 11:30:46 +0800 | [diff] [blame] | 5419 | else |
| 5420 | ocp_data |= LPM_TIMER_500US; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5421 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 5422 | |
| 5423 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 5424 | ocp_data &= ~SEN_VAL_MASK; |
| 5425 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 5426 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 5427 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5428 | ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); |
| 5429 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5430 | r8153_power_cut_en(tp, false); |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame^] | 5431 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5432 | r8153_u1u2en(tp, true); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 5433 | r8153_mac_clk_spd(tp, false); |
hayeswang | ee4761c | 2017-06-09 17:11:39 +0800 | [diff] [blame] | 5434 | usb_enable_lpm(tp->udev); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5435 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 5436 | /* rx aggregation */ |
| 5437 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 5438 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 5439 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 5440 | ocp_data |= RX_AGG_DISABLE; |
| 5441 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 5442 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5443 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5444 | rtl_tally_reset(tp); |
hayeswang | 49d1034 | 2017-06-09 17:11:44 +0800 | [diff] [blame] | 5445 | |
| 5446 | switch (tp->udev->speed) { |
| 5447 | case USB_SPEED_SUPER: |
| 5448 | case USB_SPEED_SUPER_PLUS: |
| 5449 | tp->coalesce = COALESCE_SUPER; |
| 5450 | break; |
| 5451 | case USB_SPEED_HIGH: |
| 5452 | tp->coalesce = COALESCE_HIGH; |
| 5453 | break; |
| 5454 | default: |
| 5455 | tp->coalesce = COALESCE_SLOW; |
| 5456 | break; |
| 5457 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5458 | } |
| 5459 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5460 | static void r8153b_init(struct r8152 *tp) |
| 5461 | { |
| 5462 | u32 ocp_data; |
| 5463 | u16 data; |
| 5464 | int i; |
| 5465 | |
| 5466 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5467 | return; |
| 5468 | |
| 5469 | r8153b_u1u2en(tp, false); |
| 5470 | |
| 5471 | for (i = 0; i < 500; i++) { |
| 5472 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 5473 | AUTOLOAD_DONE) |
| 5474 | break; |
| 5475 | msleep(20); |
| 5476 | } |
| 5477 | |
| 5478 | data = r8153_phy_status(tp, 0); |
| 5479 | |
| 5480 | data = r8152_mdio_read(tp, MII_BMCR); |
| 5481 | if (data & BMCR_PDOWN) { |
| 5482 | data &= ~BMCR_PDOWN; |
| 5483 | r8152_mdio_write(tp, MII_BMCR, data); |
| 5484 | } |
| 5485 | |
| 5486 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
| 5487 | |
| 5488 | r8153_u2p3en(tp, false); |
| 5489 | |
| 5490 | /* MSC timer = 0xfff * 8ms = 32760 ms */ |
| 5491 | ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); |
| 5492 | |
| 5493 | /* U1/U2/L1 idle timer. 500 us */ |
| 5494 | ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); |
| 5495 | |
| 5496 | r8153b_power_cut_en(tp, false); |
| 5497 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 5498 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5499 | rtl_runtime_suspend_enable(tp, false); |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame^] | 5500 | |
| 5501 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 5502 | if (rtl8152_get_speed(tp) & LINK_STATUS) |
| 5503 | ocp_data |= CUR_LINK_OK; |
| 5504 | else |
| 5505 | ocp_data &= ~CUR_LINK_OK; |
| 5506 | ocp_data |= POLL_LINK_CHG; |
| 5507 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5508 | r8153b_u1u2en(tp, true); |
| 5509 | usb_enable_lpm(tp->udev); |
| 5510 | |
| 5511 | /* MAC clock speed down */ |
| 5512 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); |
| 5513 | ocp_data |= MAC_CLK_SPDWN_EN; |
| 5514 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); |
| 5515 | |
| 5516 | set_bit(GREEN_ETHERNET, &tp->flags); |
| 5517 | |
| 5518 | /* rx aggregation */ |
| 5519 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 5520 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 5521 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 5522 | |
| 5523 | rtl_tally_reset(tp); |
| 5524 | |
| 5525 | tp->coalesce = 15000; /* 15 us */ |
| 5526 | } |
| 5527 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5528 | static int rtl8152_pre_reset(struct usb_interface *intf) |
| 5529 | { |
| 5530 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5531 | struct net_device *netdev; |
| 5532 | |
| 5533 | if (!tp) |
| 5534 | return 0; |
| 5535 | |
| 5536 | netdev = tp->netdev; |
| 5537 | if (!netif_running(netdev)) |
| 5538 | return 0; |
| 5539 | |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5540 | netif_stop_queue(netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5541 | tasklet_disable(&tp->tx_tl); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5542 | clear_bit(WORK_ENABLE, &tp->flags); |
| 5543 | usb_kill_urb(tp->intr_urb); |
| 5544 | cancel_delayed_work_sync(&tp->schedule); |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 5545 | napi_disable(&tp->napi); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5546 | if (netif_carrier_ok(netdev)) { |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5547 | mutex_lock(&tp->control); |
| 5548 | tp->rtl_ops.disable(tp); |
| 5549 | mutex_unlock(&tp->control); |
| 5550 | } |
| 5551 | |
| 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | static int rtl8152_post_reset(struct usb_interface *intf) |
| 5556 | { |
| 5557 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5558 | struct net_device *netdev; |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 5559 | struct sockaddr sa; |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5560 | |
| 5561 | if (!tp) |
| 5562 | return 0; |
| 5563 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 5564 | /* reset the MAC adddress in case of policy change */ |
| 5565 | if (determine_ethernet_addr(tp, &sa) >= 0) { |
| 5566 | rtnl_lock(); |
| 5567 | dev_set_mac_address (tp->netdev, &sa, NULL); |
| 5568 | rtnl_unlock(); |
| 5569 | } |
| 5570 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5571 | netdev = tp->netdev; |
| 5572 | if (!netif_running(netdev)) |
| 5573 | return 0; |
| 5574 | |
| 5575 | set_bit(WORK_ENABLE, &tp->flags); |
| 5576 | if (netif_carrier_ok(netdev)) { |
| 5577 | mutex_lock(&tp->control); |
| 5578 | tp->rtl_ops.enable(tp); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 5579 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 5580 | _rtl8152_set_rx_mode(netdev); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5581 | mutex_unlock(&tp->control); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | napi_enable(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5585 | tasklet_enable(&tp->tx_tl); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5586 | netif_wake_queue(netdev); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 5587 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5588 | |
hayeswang | 7489bda | 2017-01-26 09:38:34 +0800 | [diff] [blame] | 5589 | if (!list_empty(&tp->rx_done)) |
| 5590 | napi_schedule(&tp->napi); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5591 | |
| 5592 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5595 | static bool delay_autosuspend(struct r8152 *tp) |
| 5596 | { |
| 5597 | bool sw_linking = !!netif_carrier_ok(tp->netdev); |
| 5598 | bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); |
| 5599 | |
| 5600 | /* This means a linking change occurs and the driver doesn't detect it, |
| 5601 | * yet. If the driver has disabled tx/rx and hw is linking on, the |
| 5602 | * device wouldn't wake up by receiving any packet. |
| 5603 | */ |
| 5604 | if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) |
| 5605 | return true; |
| 5606 | |
| 5607 | /* If the linking down is occurred by nway, the device may miss the |
| 5608 | * linking change event. And it wouldn't wake when linking on. |
| 5609 | */ |
| 5610 | if (!sw_linking && tp->rtl_ops.in_nway(tp)) |
| 5611 | return true; |
hayeswang | 6a0b76c | 2017-01-23 14:18:43 +0800 | [diff] [blame] | 5612 | else if (!skb_queue_empty(&tp->tx_queue)) |
| 5613 | return true; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5614 | else |
| 5615 | return false; |
| 5616 | } |
| 5617 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5618 | static int rtl8152_runtime_resume(struct r8152 *tp) |
| 5619 | { |
| 5620 | struct net_device *netdev = tp->netdev; |
| 5621 | |
| 5622 | if (netif_running(netdev) && netdev->flags & IFF_UP) { |
| 5623 | struct napi_struct *napi = &tp->napi; |
| 5624 | |
| 5625 | tp->rtl_ops.autosuspend_en(tp, false); |
| 5626 | napi_disable(napi); |
| 5627 | set_bit(WORK_ENABLE, &tp->flags); |
| 5628 | |
| 5629 | if (netif_carrier_ok(netdev)) { |
| 5630 | if (rtl8152_get_speed(tp) & LINK_STATUS) { |
| 5631 | rtl_start_rx(tp); |
| 5632 | } else { |
| 5633 | netif_carrier_off(netdev); |
| 5634 | tp->rtl_ops.disable(tp); |
| 5635 | netif_info(tp, link, netdev, "linking down\n"); |
| 5636 | } |
| 5637 | } |
| 5638 | |
| 5639 | napi_enable(napi); |
| 5640 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5641 | smp_mb__after_atomic(); |
| 5642 | |
| 5643 | if (!list_empty(&tp->rx_done)) |
| 5644 | napi_schedule(&tp->napi); |
| 5645 | |
| 5646 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 5647 | } else { |
| 5648 | if (netdev->flags & IFF_UP) |
| 5649 | tp->rtl_ops.autosuspend_en(tp, false); |
| 5650 | |
| 5651 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5652 | } |
| 5653 | |
| 5654 | return 0; |
| 5655 | } |
| 5656 | |
| 5657 | static int rtl8152_system_resume(struct r8152 *tp) |
| 5658 | { |
| 5659 | struct net_device *netdev = tp->netdev; |
| 5660 | |
| 5661 | netif_device_attach(netdev); |
| 5662 | |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 5663 | if (netif_running(netdev) && (netdev->flags & IFF_UP)) { |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5664 | tp->rtl_ops.up(tp); |
| 5665 | netif_carrier_off(netdev); |
| 5666 | set_bit(WORK_ENABLE, &tp->flags); |
| 5667 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 5668 | } |
| 5669 | |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 5673 | static int rtl8152_runtime_suspend(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5674 | { |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 5675 | struct net_device *netdev = tp->netdev; |
| 5676 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5677 | |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 5678 | set_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5679 | smp_mb__after_atomic(); |
| 5680 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5681 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5682 | u32 rcr = 0; |
| 5683 | |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5684 | if (netif_carrier_ok(netdev)) { |
| 5685 | u32 ocp_data; |
| 5686 | |
| 5687 | rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 5688 | ocp_data = rcr & ~RCR_ACPT_ALL; |
| 5689 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 5690 | rxdy_gated_en(tp, true); |
| 5691 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, |
| 5692 | PLA_OOB_CTRL); |
| 5693 | if (!(ocp_data & RXFIFO_EMPTY)) { |
| 5694 | rxdy_gated_en(tp, false); |
| 5695 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 5696 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5697 | smp_mb__after_atomic(); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5698 | ret = -EBUSY; |
| 5699 | goto out1; |
| 5700 | } |
| 5701 | } |
| 5702 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5703 | clear_bit(WORK_ENABLE, &tp->flags); |
| 5704 | usb_kill_urb(tp->intr_urb); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5705 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5706 | tp->rtl_ops.autosuspend_en(tp, true); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5707 | |
| 5708 | if (netif_carrier_ok(netdev)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5709 | struct napi_struct *napi = &tp->napi; |
| 5710 | |
| 5711 | napi_disable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5712 | rtl_stop_rx(tp); |
| 5713 | rxdy_gated_en(tp, false); |
| 5714 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5715 | napi_enable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5716 | } |
hayeswang | bd88298 | 2017-06-13 15:14:40 +0800 | [diff] [blame] | 5717 | |
| 5718 | if (delay_autosuspend(tp)) { |
| 5719 | rtl8152_runtime_resume(tp); |
| 5720 | ret = -EBUSY; |
| 5721 | } |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 5722 | } |
| 5723 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5724 | out1: |
| 5725 | return ret; |
| 5726 | } |
| 5727 | |
| 5728 | static int rtl8152_system_suspend(struct r8152 *tp) |
| 5729 | { |
| 5730 | struct net_device *netdev = tp->netdev; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5731 | |
| 5732 | netif_device_detach(netdev); |
| 5733 | |
hayeswang | e3bd1a8 | 2014-10-29 11:12:17 +0800 | [diff] [blame] | 5734 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5735 | struct napi_struct *napi = &tp->napi; |
| 5736 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5737 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 5738 | usb_kill_urb(tp->intr_urb); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5739 | tasklet_disable(&tp->tx_tl); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5740 | napi_disable(napi); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5741 | cancel_delayed_work_sync(&tp->schedule); |
| 5742 | tp->rtl_ops.down(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5743 | napi_enable(napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5744 | tasklet_enable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5745 | } |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5746 | |
zhong jiang | f741917 | 2018-08-09 09:39:13 +0800 | [diff] [blame] | 5747 | return 0; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 5751 | { |
| 5752 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5753 | int ret; |
| 5754 | |
| 5755 | mutex_lock(&tp->control); |
| 5756 | |
| 5757 | if (PMSG_IS_AUTO(message)) |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 5758 | ret = rtl8152_runtime_suspend(tp); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5759 | else |
| 5760 | ret = rtl8152_system_suspend(tp); |
| 5761 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5762 | mutex_unlock(&tp->control); |
| 5763 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 5764 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | static int rtl8152_resume(struct usb_interface *intf) |
| 5768 | { |
| 5769 | struct r8152 *tp = usb_get_intfdata(intf); |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5770 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5771 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5772 | mutex_lock(&tp->control); |
| 5773 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5774 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) |
| 5775 | ret = rtl8152_runtime_resume(tp); |
| 5776 | else |
| 5777 | ret = rtl8152_system_resume(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5778 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5779 | mutex_unlock(&tp->control); |
| 5780 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5781 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5782 | } |
| 5783 | |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 5784 | static int rtl8152_reset_resume(struct usb_interface *intf) |
| 5785 | { |
| 5786 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5787 | |
| 5788 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
hayeswang | befb2de | 2017-06-09 17:11:45 +0800 | [diff] [blame] | 5789 | tp->rtl_ops.init(tp); |
| 5790 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
Kai-Heng Feng | a54cdee | 2019-10-04 20:51:04 +0800 | [diff] [blame] | 5791 | set_ethernet_addr(tp); |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 5792 | return rtl8152_resume(intf); |
| 5793 | } |
| 5794 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5795 | static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 5796 | { |
| 5797 | struct r8152 *tp = netdev_priv(dev); |
| 5798 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5799 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5800 | return; |
| 5801 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 5802 | if (!rtl_can_wakeup(tp)) { |
| 5803 | wol->supported = 0; |
| 5804 | wol->wolopts = 0; |
| 5805 | } else { |
| 5806 | mutex_lock(&tp->control); |
| 5807 | wol->supported = WAKE_ANY; |
| 5808 | wol->wolopts = __rtl_get_wol(tp); |
| 5809 | mutex_unlock(&tp->control); |
| 5810 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5811 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5812 | usb_autopm_put_interface(tp->intf); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 5816 | { |
| 5817 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5818 | int ret; |
| 5819 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 5820 | if (!rtl_can_wakeup(tp)) |
| 5821 | return -EOPNOTSUPP; |
| 5822 | |
Florian Fainelli | f2750df | 2018-09-28 16:18:54 -0700 | [diff] [blame] | 5823 | if (wol->wolopts & ~WAKE_ANY) |
| 5824 | return -EINVAL; |
| 5825 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5826 | ret = usb_autopm_get_interface(tp->intf); |
| 5827 | if (ret < 0) |
| 5828 | goto out_set_wol; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5829 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5830 | mutex_lock(&tp->control); |
| 5831 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5832 | __rtl_set_wol(tp, wol->wolopts); |
| 5833 | tp->saved_wolopts = wol->wolopts & WAKE_ANY; |
| 5834 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5835 | mutex_unlock(&tp->control); |
| 5836 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5837 | usb_autopm_put_interface(tp->intf); |
| 5838 | |
| 5839 | out_set_wol: |
| 5840 | return ret; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5841 | } |
| 5842 | |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 5843 | static u32 rtl8152_get_msglevel(struct net_device *dev) |
| 5844 | { |
| 5845 | struct r8152 *tp = netdev_priv(dev); |
| 5846 | |
| 5847 | return tp->msg_enable; |
| 5848 | } |
| 5849 | |
| 5850 | static void rtl8152_set_msglevel(struct net_device *dev, u32 value) |
| 5851 | { |
| 5852 | struct r8152 *tp = netdev_priv(dev); |
| 5853 | |
| 5854 | tp->msg_enable = value; |
| 5855 | } |
| 5856 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5857 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 5858 | struct ethtool_drvinfo *info) |
| 5859 | { |
| 5860 | struct r8152 *tp = netdev_priv(netdev); |
| 5861 | |
hayeswang | b0b46c7 | 2014-08-26 10:08:23 +0800 | [diff] [blame] | 5862 | strlcpy(info->driver, MODULENAME, sizeof(info->driver)); |
| 5863 | strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5864 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5865 | if (!IS_ERR_OR_NULL(tp->rtl_fw.fw)) |
| 5866 | strlcpy(info->fw_version, tp->rtl_fw.version, |
| 5867 | sizeof(info->fw_version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | static |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5871 | int rtl8152_get_link_ksettings(struct net_device *netdev, |
| 5872 | struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5873 | { |
| 5874 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5875 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5876 | |
| 5877 | if (!tp->mii.mdio_read) |
| 5878 | return -EOPNOTSUPP; |
| 5879 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5880 | ret = usb_autopm_get_interface(tp->intf); |
| 5881 | if (ret < 0) |
| 5882 | goto out; |
| 5883 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5884 | mutex_lock(&tp->control); |
| 5885 | |
yuval.shaia@oracle.com | 82c01a8 | 2017-06-04 20:22:00 +0300 | [diff] [blame] | 5886 | mii_ethtool_get_link_ksettings(&tp->mii, cmd); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5887 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5888 | mutex_unlock(&tp->control); |
| 5889 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5890 | usb_autopm_put_interface(tp->intf); |
| 5891 | |
| 5892 | out: |
| 5893 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5896 | static int rtl8152_set_link_ksettings(struct net_device *dev, |
| 5897 | const struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5898 | { |
| 5899 | struct r8152 *tp = netdev_priv(dev); |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5900 | u32 advertising = 0; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5901 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5902 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5903 | ret = usb_autopm_get_interface(tp->intf); |
| 5904 | if (ret < 0) |
| 5905 | goto out; |
| 5906 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5907 | if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 5908 | cmd->link_modes.advertising)) |
| 5909 | advertising |= RTL_ADVERTISED_10_HALF; |
| 5910 | |
| 5911 | if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 5912 | cmd->link_modes.advertising)) |
| 5913 | advertising |= RTL_ADVERTISED_10_FULL; |
| 5914 | |
| 5915 | if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 5916 | cmd->link_modes.advertising)) |
| 5917 | advertising |= RTL_ADVERTISED_100_HALF; |
| 5918 | |
| 5919 | if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 5920 | cmd->link_modes.advertising)) |
| 5921 | advertising |= RTL_ADVERTISED_100_FULL; |
| 5922 | |
| 5923 | if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 5924 | cmd->link_modes.advertising)) |
| 5925 | advertising |= RTL_ADVERTISED_1000_HALF; |
| 5926 | |
| 5927 | if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 5928 | cmd->link_modes.advertising)) |
| 5929 | advertising |= RTL_ADVERTISED_1000_FULL; |
| 5930 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5931 | mutex_lock(&tp->control); |
| 5932 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5933 | ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5934 | cmd->base.duplex, advertising); |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 5935 | if (!ret) { |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5936 | tp->autoneg = cmd->base.autoneg; |
| 5937 | tp->speed = cmd->base.speed; |
| 5938 | tp->duplex = cmd->base.duplex; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5939 | tp->advertising = advertising; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 5940 | } |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5941 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5942 | mutex_unlock(&tp->control); |
| 5943 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5944 | usb_autopm_put_interface(tp->intf); |
| 5945 | |
| 5946 | out: |
| 5947 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5950 | static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { |
| 5951 | "tx_packets", |
| 5952 | "rx_packets", |
| 5953 | "tx_errors", |
| 5954 | "rx_errors", |
| 5955 | "rx_missed", |
| 5956 | "align_errors", |
| 5957 | "tx_single_collisions", |
| 5958 | "tx_multi_collisions", |
| 5959 | "rx_unicast", |
| 5960 | "rx_broadcast", |
| 5961 | "rx_multicast", |
| 5962 | "tx_aborted", |
| 5963 | "tx_underrun", |
| 5964 | }; |
| 5965 | |
| 5966 | static int rtl8152_get_sset_count(struct net_device *dev, int sset) |
| 5967 | { |
| 5968 | switch (sset) { |
| 5969 | case ETH_SS_STATS: |
| 5970 | return ARRAY_SIZE(rtl8152_gstrings); |
| 5971 | default: |
| 5972 | return -EOPNOTSUPP; |
| 5973 | } |
| 5974 | } |
| 5975 | |
| 5976 | static void rtl8152_get_ethtool_stats(struct net_device *dev, |
| 5977 | struct ethtool_stats *stats, u64 *data) |
| 5978 | { |
| 5979 | struct r8152 *tp = netdev_priv(dev); |
| 5980 | struct tally_counter tally; |
| 5981 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 5982 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5983 | return; |
| 5984 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5985 | generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); |
| 5986 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 5987 | usb_autopm_put_interface(tp->intf); |
| 5988 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5989 | data[0] = le64_to_cpu(tally.tx_packets); |
| 5990 | data[1] = le64_to_cpu(tally.rx_packets); |
| 5991 | data[2] = le64_to_cpu(tally.tx_errors); |
| 5992 | data[3] = le32_to_cpu(tally.rx_errors); |
| 5993 | data[4] = le16_to_cpu(tally.rx_missed); |
| 5994 | data[5] = le16_to_cpu(tally.align_errors); |
| 5995 | data[6] = le32_to_cpu(tally.tx_one_collision); |
| 5996 | data[7] = le32_to_cpu(tally.tx_multi_collision); |
| 5997 | data[8] = le64_to_cpu(tally.rx_unicast); |
| 5998 | data[9] = le64_to_cpu(tally.rx_broadcast); |
| 5999 | data[10] = le32_to_cpu(tally.rx_multicast); |
| 6000 | data[11] = le16_to_cpu(tally.tx_aborted); |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 6001 | data[12] = le16_to_cpu(tally.tx_underrun); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 6002 | } |
| 6003 | |
| 6004 | static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 6005 | { |
| 6006 | switch (stringset) { |
| 6007 | case ETH_SS_STATS: |
| 6008 | memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings)); |
| 6009 | break; |
| 6010 | } |
| 6011 | } |
| 6012 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6013 | static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 6014 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6015 | u32 lp, adv, supported = 0; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6016 | u16 val; |
| 6017 | |
| 6018 | val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
| 6019 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 6020 | |
| 6021 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
| 6022 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6023 | |
| 6024 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
| 6025 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6026 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6027 | eee->eee_enabled = tp->eee_en; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6028 | eee->eee_active = !!(supported & adv & lp); |
| 6029 | eee->supported = supported; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6030 | eee->advertised = tp->eee_adv; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6031 | eee->lp_advertised = lp; |
| 6032 | |
| 6033 | return 0; |
| 6034 | } |
| 6035 | |
| 6036 | static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 6037 | { |
| 6038 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 6039 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6040 | tp->eee_en = eee->eee_enabled; |
| 6041 | tp->eee_adv = val; |
| 6042 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 6043 | rtl_eee_enable(tp, tp->eee_en); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6044 | |
| 6045 | return 0; |
| 6046 | } |
| 6047 | |
| 6048 | static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 6049 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6050 | u32 lp, adv, supported = 0; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6051 | u16 val; |
| 6052 | |
| 6053 | val = ocp_reg_read(tp, OCP_EEE_ABLE); |
| 6054 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 6055 | |
| 6056 | val = ocp_reg_read(tp, OCP_EEE_ADV); |
| 6057 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6058 | |
| 6059 | val = ocp_reg_read(tp, OCP_EEE_LPABLE); |
| 6060 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6061 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6062 | eee->eee_enabled = tp->eee_en; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6063 | eee->eee_active = !!(supported & adv & lp); |
| 6064 | eee->supported = supported; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6065 | eee->advertised = tp->eee_adv; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6066 | eee->lp_advertised = lp; |
| 6067 | |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6071 | static int |
| 6072 | rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) |
| 6073 | { |
| 6074 | struct r8152 *tp = netdev_priv(net); |
| 6075 | int ret; |
| 6076 | |
| 6077 | ret = usb_autopm_get_interface(tp->intf); |
| 6078 | if (ret < 0) |
| 6079 | goto out; |
| 6080 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6081 | mutex_lock(&tp->control); |
| 6082 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6083 | ret = tp->rtl_ops.eee_get(tp, edata); |
| 6084 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6085 | mutex_unlock(&tp->control); |
| 6086 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6087 | usb_autopm_put_interface(tp->intf); |
| 6088 | |
| 6089 | out: |
| 6090 | return ret; |
| 6091 | } |
| 6092 | |
| 6093 | static int |
| 6094 | rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) |
| 6095 | { |
| 6096 | struct r8152 *tp = netdev_priv(net); |
| 6097 | int ret; |
| 6098 | |
| 6099 | ret = usb_autopm_get_interface(tp->intf); |
| 6100 | if (ret < 0) |
| 6101 | goto out; |
| 6102 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6103 | mutex_lock(&tp->control); |
| 6104 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6105 | ret = tp->rtl_ops.eee_set(tp, edata); |
hayeswang | 9d31a7b | 2014-10-06 10:36:04 +0800 | [diff] [blame] | 6106 | if (!ret) |
| 6107 | ret = mii_nway_restart(&tp->mii); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6108 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6109 | mutex_unlock(&tp->control); |
| 6110 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6111 | usb_autopm_put_interface(tp->intf); |
| 6112 | |
| 6113 | out: |
| 6114 | return ret; |
| 6115 | } |
| 6116 | |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 6117 | static int rtl8152_nway_reset(struct net_device *dev) |
| 6118 | { |
| 6119 | struct r8152 *tp = netdev_priv(dev); |
| 6120 | int ret; |
| 6121 | |
| 6122 | ret = usb_autopm_get_interface(tp->intf); |
| 6123 | if (ret < 0) |
| 6124 | goto out; |
| 6125 | |
| 6126 | mutex_lock(&tp->control); |
| 6127 | |
| 6128 | ret = mii_nway_restart(&tp->mii); |
| 6129 | |
| 6130 | mutex_unlock(&tp->control); |
| 6131 | |
| 6132 | usb_autopm_put_interface(tp->intf); |
| 6133 | |
| 6134 | out: |
| 6135 | return ret; |
| 6136 | } |
| 6137 | |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6138 | static int rtl8152_get_coalesce(struct net_device *netdev, |
| 6139 | struct ethtool_coalesce *coalesce) |
| 6140 | { |
| 6141 | struct r8152 *tp = netdev_priv(netdev); |
| 6142 | |
| 6143 | switch (tp->version) { |
| 6144 | case RTL_VER_01: |
| 6145 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6146 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6147 | return -EOPNOTSUPP; |
| 6148 | default: |
| 6149 | break; |
| 6150 | } |
| 6151 | |
| 6152 | coalesce->rx_coalesce_usecs = tp->coalesce; |
| 6153 | |
| 6154 | return 0; |
| 6155 | } |
| 6156 | |
| 6157 | static int rtl8152_set_coalesce(struct net_device *netdev, |
| 6158 | struct ethtool_coalesce *coalesce) |
| 6159 | { |
| 6160 | struct r8152 *tp = netdev_priv(netdev); |
| 6161 | int ret; |
| 6162 | |
| 6163 | switch (tp->version) { |
| 6164 | case RTL_VER_01: |
| 6165 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6166 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6167 | return -EOPNOTSUPP; |
| 6168 | default: |
| 6169 | break; |
| 6170 | } |
| 6171 | |
| 6172 | if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) |
| 6173 | return -EINVAL; |
| 6174 | |
| 6175 | ret = usb_autopm_get_interface(tp->intf); |
| 6176 | if (ret < 0) |
| 6177 | return ret; |
| 6178 | |
| 6179 | mutex_lock(&tp->control); |
| 6180 | |
| 6181 | if (tp->coalesce != coalesce->rx_coalesce_usecs) { |
| 6182 | tp->coalesce = coalesce->rx_coalesce_usecs; |
| 6183 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 6184 | if (netif_running(netdev) && netif_carrier_ok(netdev)) { |
| 6185 | netif_stop_queue(netdev); |
| 6186 | napi_disable(&tp->napi); |
| 6187 | tp->rtl_ops.disable(tp); |
| 6188 | tp->rtl_ops.enable(tp); |
| 6189 | rtl_start_rx(tp); |
| 6190 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 6191 | _rtl8152_set_rx_mode(netdev); |
| 6192 | napi_enable(&tp->napi); |
| 6193 | netif_wake_queue(netdev); |
| 6194 | } |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6195 | } |
| 6196 | |
| 6197 | mutex_unlock(&tp->control); |
| 6198 | |
| 6199 | usb_autopm_put_interface(tp->intf); |
| 6200 | |
| 6201 | return ret; |
| 6202 | } |
| 6203 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6204 | static int rtl8152_get_tunable(struct net_device *netdev, |
| 6205 | const struct ethtool_tunable *tunable, void *d) |
| 6206 | { |
| 6207 | struct r8152 *tp = netdev_priv(netdev); |
| 6208 | |
| 6209 | switch (tunable->id) { |
| 6210 | case ETHTOOL_RX_COPYBREAK: |
| 6211 | *(u32 *)d = tp->rx_copybreak; |
| 6212 | break; |
| 6213 | default: |
| 6214 | return -EOPNOTSUPP; |
| 6215 | } |
| 6216 | |
| 6217 | return 0; |
| 6218 | } |
| 6219 | |
| 6220 | static int rtl8152_set_tunable(struct net_device *netdev, |
| 6221 | const struct ethtool_tunable *tunable, |
| 6222 | const void *d) |
| 6223 | { |
| 6224 | struct r8152 *tp = netdev_priv(netdev); |
| 6225 | u32 val; |
| 6226 | |
| 6227 | switch (tunable->id) { |
| 6228 | case ETHTOOL_RX_COPYBREAK: |
| 6229 | val = *(u32 *)d; |
| 6230 | if (val < ETH_ZLEN) { |
| 6231 | netif_err(tp, rx_err, netdev, |
| 6232 | "Invalid rx copy break value\n"); |
| 6233 | return -EINVAL; |
| 6234 | } |
| 6235 | |
| 6236 | if (tp->rx_copybreak != val) { |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 6237 | if (netdev->flags & IFF_UP) { |
| 6238 | mutex_lock(&tp->control); |
| 6239 | napi_disable(&tp->napi); |
| 6240 | tp->rx_copybreak = val; |
| 6241 | napi_enable(&tp->napi); |
| 6242 | mutex_unlock(&tp->control); |
| 6243 | } else { |
| 6244 | tp->rx_copybreak = val; |
| 6245 | } |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6246 | } |
| 6247 | break; |
| 6248 | default: |
| 6249 | return -EOPNOTSUPP; |
| 6250 | } |
| 6251 | |
| 6252 | return 0; |
| 6253 | } |
| 6254 | |
| 6255 | static void rtl8152_get_ringparam(struct net_device *netdev, |
| 6256 | struct ethtool_ringparam *ring) |
| 6257 | { |
| 6258 | struct r8152 *tp = netdev_priv(netdev); |
| 6259 | |
| 6260 | ring->rx_max_pending = RTL8152_RX_MAX_PENDING; |
| 6261 | ring->rx_pending = tp->rx_pending; |
| 6262 | } |
| 6263 | |
| 6264 | static int rtl8152_set_ringparam(struct net_device *netdev, |
| 6265 | struct ethtool_ringparam *ring) |
| 6266 | { |
| 6267 | struct r8152 *tp = netdev_priv(netdev); |
| 6268 | |
| 6269 | if (ring->rx_pending < (RTL8152_MAX_RX * 2)) |
| 6270 | return -EINVAL; |
| 6271 | |
| 6272 | if (tp->rx_pending != ring->rx_pending) { |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 6273 | if (netdev->flags & IFF_UP) { |
| 6274 | mutex_lock(&tp->control); |
| 6275 | napi_disable(&tp->napi); |
| 6276 | tp->rx_pending = ring->rx_pending; |
| 6277 | napi_enable(&tp->napi); |
| 6278 | mutex_unlock(&tp->control); |
| 6279 | } else { |
| 6280 | tp->rx_pending = ring->rx_pending; |
| 6281 | } |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | return 0; |
| 6285 | } |
| 6286 | |
Julia Lawall | 407a471 | 2016-09-01 00:21:22 +0200 | [diff] [blame] | 6287 | static const struct ethtool_ops ops = { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6288 | .get_drvinfo = rtl8152_get_drvinfo, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6289 | .get_link = ethtool_op_get_link, |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 6290 | .nway_reset = rtl8152_nway_reset, |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 6291 | .get_msglevel = rtl8152_get_msglevel, |
| 6292 | .set_msglevel = rtl8152_set_msglevel, |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 6293 | .get_wol = rtl8152_get_wol, |
| 6294 | .set_wol = rtl8152_set_wol, |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 6295 | .get_strings = rtl8152_get_strings, |
| 6296 | .get_sset_count = rtl8152_get_sset_count, |
| 6297 | .get_ethtool_stats = rtl8152_get_ethtool_stats, |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6298 | .get_coalesce = rtl8152_get_coalesce, |
| 6299 | .set_coalesce = rtl8152_set_coalesce, |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6300 | .get_eee = rtl_ethtool_get_eee, |
| 6301 | .set_eee = rtl_ethtool_set_eee, |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 6302 | .get_link_ksettings = rtl8152_get_link_ksettings, |
| 6303 | .set_link_ksettings = rtl8152_set_link_ksettings, |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6304 | .get_tunable = rtl8152_get_tunable, |
| 6305 | .set_tunable = rtl8152_set_tunable, |
| 6306 | .get_ringparam = rtl8152_get_ringparam, |
| 6307 | .set_ringparam = rtl8152_set_ringparam, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6308 | }; |
| 6309 | |
| 6310 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 6311 | { |
| 6312 | struct r8152 *tp = netdev_priv(netdev); |
| 6313 | struct mii_ioctl_data *data = if_mii(rq); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6314 | int res; |
| 6315 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 6316 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6317 | return -ENODEV; |
| 6318 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6319 | res = usb_autopm_get_interface(tp->intf); |
| 6320 | if (res < 0) |
| 6321 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6322 | |
| 6323 | switch (cmd) { |
| 6324 | case SIOCGMIIPHY: |
| 6325 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 6326 | break; |
| 6327 | |
| 6328 | case SIOCGMIIREG: |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6329 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6330 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6331 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6332 | break; |
| 6333 | |
| 6334 | case SIOCSMIIREG: |
| 6335 | if (!capable(CAP_NET_ADMIN)) { |
| 6336 | res = -EPERM; |
| 6337 | break; |
| 6338 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6339 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6340 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6341 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6342 | break; |
| 6343 | |
| 6344 | default: |
| 6345 | res = -EOPNOTSUPP; |
| 6346 | } |
| 6347 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6348 | usb_autopm_put_interface(tp->intf); |
| 6349 | |
| 6350 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6351 | return res; |
| 6352 | } |
| 6353 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6354 | static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) |
| 6355 | { |
| 6356 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 6357 | int ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6358 | |
| 6359 | switch (tp->version) { |
| 6360 | case RTL_VER_01: |
| 6361 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6362 | case RTL_VER_07: |
Jarod Wilson | a52ad51 | 2016-10-07 22:04:34 -0400 | [diff] [blame] | 6363 | dev->mtu = new_mtu; |
| 6364 | return 0; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6365 | default: |
| 6366 | break; |
| 6367 | } |
| 6368 | |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 6369 | ret = usb_autopm_get_interface(tp->intf); |
| 6370 | if (ret < 0) |
| 6371 | return ret; |
| 6372 | |
| 6373 | mutex_lock(&tp->control); |
| 6374 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6375 | dev->mtu = new_mtu; |
| 6376 | |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 6377 | if (netif_running(dev)) { |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 6378 | u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 6379 | |
| 6380 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms); |
| 6381 | |
| 6382 | if (netif_carrier_ok(dev)) |
| 6383 | r8153_set_rx_early_size(tp); |
| 6384 | } |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 6385 | |
| 6386 | mutex_unlock(&tp->control); |
| 6387 | |
| 6388 | usb_autopm_put_interface(tp->intf); |
| 6389 | |
| 6390 | return ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6391 | } |
| 6392 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6393 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 6394 | .ndo_open = rtl8152_open, |
| 6395 | .ndo_stop = rtl8152_close, |
| 6396 | .ndo_do_ioctl = rtl8152_ioctl, |
| 6397 | .ndo_start_xmit = rtl8152_start_xmit, |
| 6398 | .ndo_tx_timeout = rtl8152_tx_timeout, |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6399 | .ndo_set_features = rtl8152_set_features, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6400 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 6401 | .ndo_set_mac_address = rtl8152_set_mac_address, |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6402 | .ndo_change_mtu = rtl8152_change_mtu, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6403 | .ndo_validate_addr = eth_validate_addr, |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 6404 | .ndo_features_check = rtl8152_features_check, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6405 | }; |
| 6406 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 6407 | static void rtl8152_unload(struct r8152 *tp) |
| 6408 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 6409 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6410 | return; |
| 6411 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 6412 | if (tp->version != RTL_VER_01) |
| 6413 | r8152_power_cut_en(tp, true); |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 6414 | } |
| 6415 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 6416 | static void rtl8153_unload(struct r8152 *tp) |
| 6417 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 6418 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6419 | return; |
| 6420 | |
hayeswang | 49be172 | 2014-10-01 13:25:11 +0800 | [diff] [blame] | 6421 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 6422 | } |
| 6423 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6424 | static void rtl8153b_unload(struct r8152 *tp) |
| 6425 | { |
| 6426 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6427 | return; |
| 6428 | |
| 6429 | r8153b_power_cut_en(tp, false); |
| 6430 | } |
| 6431 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6432 | static int rtl_ops_init(struct r8152 *tp) |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6433 | { |
| 6434 | struct rtl_ops *ops = &tp->rtl_ops; |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6435 | int ret = 0; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6436 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6437 | switch (tp->version) { |
| 6438 | case RTL_VER_01: |
| 6439 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6440 | case RTL_VER_07: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6441 | ops->init = r8152b_init; |
| 6442 | ops->enable = rtl8152_enable; |
| 6443 | ops->disable = rtl8152_disable; |
| 6444 | ops->up = rtl8152_up; |
| 6445 | ops->down = rtl8152_down; |
| 6446 | ops->unload = rtl8152_unload; |
| 6447 | ops->eee_get = r8152_get_eee; |
| 6448 | ops->eee_set = r8152_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 6449 | ops->in_nway = rtl8152_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6450 | ops->hw_phy_cfg = r8152b_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 6451 | ops->autosuspend_en = rtl_runtime_suspend_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 6452 | tp->rx_buf_sz = 16 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6453 | tp->eee_en = true; |
| 6454 | tp->eee_adv = MDIO_EEE_100TX; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 6455 | break; |
| 6456 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6457 | case RTL_VER_03: |
| 6458 | case RTL_VER_04: |
| 6459 | case RTL_VER_05: |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 6460 | case RTL_VER_06: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6461 | ops->init = r8153_init; |
| 6462 | ops->enable = rtl8153_enable; |
| 6463 | ops->disable = rtl8153_disable; |
| 6464 | ops->up = rtl8153_up; |
| 6465 | ops->down = rtl8153_down; |
| 6466 | ops->unload = rtl8153_unload; |
| 6467 | ops->eee_get = r8153_get_eee; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 6468 | ops->eee_set = r8152_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 6469 | ops->in_nway = rtl8153_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6470 | ops->hw_phy_cfg = r8153_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 6471 | ops->autosuspend_en = rtl8153_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 6472 | tp->rx_buf_sz = 32 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6473 | tp->eee_en = true; |
| 6474 | tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6475 | break; |
| 6476 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6477 | case RTL_VER_08: |
| 6478 | case RTL_VER_09: |
| 6479 | ops->init = r8153b_init; |
| 6480 | ops->enable = rtl8153_enable; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 6481 | ops->disable = rtl8153_disable; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6482 | ops->up = rtl8153b_up; |
| 6483 | ops->down = rtl8153b_down; |
| 6484 | ops->unload = rtl8153b_unload; |
| 6485 | ops->eee_get = r8153_get_eee; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 6486 | ops->eee_set = r8152_set_eee; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6487 | ops->in_nway = rtl8153_in_nway; |
| 6488 | ops->hw_phy_cfg = r8153b_hw_phy_cfg; |
| 6489 | ops->autosuspend_en = rtl8153b_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 6490 | tp->rx_buf_sz = 32 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6491 | tp->eee_en = true; |
| 6492 | tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6493 | break; |
| 6494 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6495 | default: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6496 | ret = -ENODEV; |
| 6497 | netif_err(tp, probe, tp->netdev, "Unknown Device\n"); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6498 | break; |
| 6499 | } |
| 6500 | |
| 6501 | return ret; |
| 6502 | } |
| 6503 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6504 | #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw" |
| 6505 | #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw" |
| 6506 | #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw" |
| 6507 | #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw" |
| 6508 | |
| 6509 | MODULE_FIRMWARE(FIRMWARE_8153A_2); |
| 6510 | MODULE_FIRMWARE(FIRMWARE_8153A_3); |
| 6511 | MODULE_FIRMWARE(FIRMWARE_8153A_4); |
| 6512 | MODULE_FIRMWARE(FIRMWARE_8153B_2); |
| 6513 | |
| 6514 | static int rtl_fw_init(struct r8152 *tp) |
| 6515 | { |
| 6516 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
| 6517 | |
| 6518 | switch (tp->version) { |
| 6519 | case RTL_VER_04: |
| 6520 | rtl_fw->fw_name = FIRMWARE_8153A_2; |
| 6521 | rtl_fw->pre_fw = r8153_pre_firmware_1; |
| 6522 | rtl_fw->post_fw = r8153_post_firmware_1; |
| 6523 | break; |
| 6524 | case RTL_VER_05: |
| 6525 | rtl_fw->fw_name = FIRMWARE_8153A_3; |
| 6526 | rtl_fw->pre_fw = r8153_pre_firmware_2; |
| 6527 | rtl_fw->post_fw = r8153_post_firmware_2; |
| 6528 | break; |
| 6529 | case RTL_VER_06: |
| 6530 | rtl_fw->fw_name = FIRMWARE_8153A_4; |
| 6531 | rtl_fw->post_fw = r8153_post_firmware_3; |
| 6532 | break; |
| 6533 | case RTL_VER_09: |
| 6534 | rtl_fw->fw_name = FIRMWARE_8153B_2; |
| 6535 | rtl_fw->pre_fw = r8153b_pre_firmware_1; |
| 6536 | rtl_fw->post_fw = r8153b_post_firmware_1; |
| 6537 | break; |
| 6538 | default: |
| 6539 | break; |
| 6540 | } |
| 6541 | |
| 6542 | return 0; |
| 6543 | } |
| 6544 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6545 | static u8 rtl_get_version(struct usb_interface *intf) |
| 6546 | { |
| 6547 | struct usb_device *udev = interface_to_usbdev(intf); |
| 6548 | u32 ocp_data = 0; |
| 6549 | __le32 *tmp; |
| 6550 | u8 version; |
| 6551 | int ret; |
| 6552 | |
| 6553 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 6554 | if (!tmp) |
| 6555 | return 0; |
| 6556 | |
| 6557 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 6558 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 6559 | PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500); |
| 6560 | if (ret > 0) |
| 6561 | ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; |
| 6562 | |
| 6563 | kfree(tmp); |
| 6564 | |
| 6565 | switch (ocp_data) { |
| 6566 | case 0x4c00: |
| 6567 | version = RTL_VER_01; |
| 6568 | break; |
| 6569 | case 0x4c10: |
| 6570 | version = RTL_VER_02; |
| 6571 | break; |
| 6572 | case 0x5c00: |
| 6573 | version = RTL_VER_03; |
| 6574 | break; |
| 6575 | case 0x5c10: |
| 6576 | version = RTL_VER_04; |
| 6577 | break; |
| 6578 | case 0x5c20: |
| 6579 | version = RTL_VER_05; |
| 6580 | break; |
| 6581 | case 0x5c30: |
| 6582 | version = RTL_VER_06; |
| 6583 | break; |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6584 | case 0x4800: |
| 6585 | version = RTL_VER_07; |
| 6586 | break; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6587 | case 0x6000: |
| 6588 | version = RTL_VER_08; |
| 6589 | break; |
| 6590 | case 0x6010: |
| 6591 | version = RTL_VER_09; |
| 6592 | break; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6593 | default: |
| 6594 | version = RTL_VER_UNKNOWN; |
| 6595 | dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data); |
| 6596 | break; |
| 6597 | } |
| 6598 | |
Oliver Neukum | eb3c28c | 2017-06-12 13:56:51 +0200 | [diff] [blame] | 6599 | dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); |
| 6600 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6601 | return version; |
| 6602 | } |
| 6603 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6604 | static int rtl8152_probe(struct usb_interface *intf, |
| 6605 | const struct usb_device_id *id) |
| 6606 | { |
| 6607 | struct usb_device *udev = interface_to_usbdev(intf); |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6608 | u8 version = rtl_get_version(intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6609 | struct r8152 *tp; |
| 6610 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6611 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6612 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6613 | if (version == RTL_VER_UNKNOWN) |
| 6614 | return -ENODEV; |
| 6615 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 6616 | if (udev->actconfig->desc.bConfigurationValue != 1) { |
| 6617 | usb_driver_set_configuration(udev, 1); |
| 6618 | return -ENODEV; |
| 6619 | } |
| 6620 | |
Johan Hovold | 86f3f4c | 2020-01-14 09:27:29 +0100 | [diff] [blame] | 6621 | if (intf->cur_altsetting->desc.bNumEndpoints < 3) |
| 6622 | return -ENODEV; |
| 6623 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 6624 | usb_reset_device(udev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6625 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 6626 | if (!netdev) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 6627 | dev_err(&intf->dev, "Out of memory\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6628 | return -ENOMEM; |
| 6629 | } |
| 6630 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6631 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6632 | tp = netdev_priv(netdev); |
| 6633 | tp->msg_enable = 0x7FFF; |
| 6634 | |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 6635 | tp->udev = udev; |
| 6636 | tp->netdev = netdev; |
| 6637 | tp->intf = intf; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6638 | tp->version = version; |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 6639 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6640 | switch (version) { |
| 6641 | case RTL_VER_01: |
| 6642 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6643 | case RTL_VER_07: |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6644 | tp->mii.supports_gmii = 0; |
| 6645 | break; |
| 6646 | default: |
| 6647 | tp->mii.supports_gmii = 1; |
| 6648 | break; |
| 6649 | } |
| 6650 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6651 | ret = rtl_ops_init(tp); |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 6652 | if (ret) |
| 6653 | goto out; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6654 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6655 | rtl_fw_init(tp); |
| 6656 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6657 | mutex_init(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6658 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6659 | 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] | 6660 | tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp); |
| 6661 | tasklet_disable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6662 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6663 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 6664 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 6665 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 6666 | netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 6667 | NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6668 | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | |
| 6669 | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 6670 | netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 6671 | NETIF_F_TSO | NETIF_F_FRAGLIST | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6672 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | |
hayeswang | ccc39fa | 2015-02-06 11:30:49 +0800 | [diff] [blame] | 6673 | NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6674 | netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | |
| 6675 | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | |
| 6676 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 6677 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 6678 | if (tp->version == RTL_VER_01) { |
| 6679 | netdev->features &= ~NETIF_F_RXCSUM; |
| 6680 | netdev->hw_features &= ~NETIF_F_RXCSUM; |
| 6681 | } |
| 6682 | |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 6683 | if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO && |
| 6684 | le16_to_cpu(udev->descriptor.idProduct) == 0x3082) |
| 6685 | set_bit(LENOVO_MACPASSTHRU, &tp->flags); |
| 6686 | |
Kai-Heng Feng | 176eb61 | 2018-08-20 12:43:51 +0800 | [diff] [blame] | 6687 | if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && |
Prashant Malani | 151ea09 | 2019-10-02 14:09:33 -0700 | [diff] [blame] | 6688 | (!strcmp(udev->serial, "000001000000") || |
| 6689 | !strcmp(udev->serial, "000002000000"))) { |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 6690 | dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); |
| 6691 | set_bit(DELL_TB_RX_AGG_BUG, &tp->flags); |
| 6692 | } |
| 6693 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 6694 | netdev->ethtool_ops = &ops; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 6695 | netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6696 | |
Jarod Wilson | f77f0ae | 2016-10-20 13:55:17 -0400 | [diff] [blame] | 6697 | /* MTU range: 68 - 1500 or 9194 */ |
| 6698 | netdev->min_mtu = ETH_MIN_MTU; |
| 6699 | switch (tp->version) { |
| 6700 | case RTL_VER_01: |
| 6701 | case RTL_VER_02: |
| 6702 | netdev->max_mtu = ETH_DATA_LEN; |
| 6703 | break; |
| 6704 | default: |
| 6705 | netdev->max_mtu = RTL8153_MAX_MTU; |
| 6706 | break; |
| 6707 | } |
| 6708 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6709 | tp->mii.dev = netdev; |
| 6710 | tp->mii.mdio_read = read_mii_word; |
| 6711 | tp->mii.mdio_write = write_mii_word; |
| 6712 | tp->mii.phy_id_mask = 0x3f; |
| 6713 | tp->mii.reg_num_mask = 0x1f; |
| 6714 | tp->mii.phy_id = R8152_PHY_ID; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6715 | |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 6716 | tp->autoneg = AUTONEG_ENABLE; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 6717 | tp->speed = SPEED_100; |
| 6718 | tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | |
| 6719 | RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; |
| 6720 | if (tp->mii.supports_gmii) { |
| 6721 | tp->speed = SPEED_1000; |
| 6722 | tp->advertising |= RTL_ADVERTISED_1000_FULL; |
| 6723 | } |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 6724 | tp->duplex = DUPLEX_FULL; |
| 6725 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6726 | tp->rx_copybreak = RTL8152_RXFG_HEADSZ; |
| 6727 | tp->rx_pending = 10 * RTL8152_MAX_RX; |
| 6728 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6729 | intf->needs_remote_wakeup = 1; |
| 6730 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6731 | tp->rtl_ops.init(tp); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6732 | #if IS_BUILTIN(CONFIG_USB_RTL8152) |
| 6733 | /* Retry in case request_firmware() is not ready yet. */ |
| 6734 | tp->rtl_fw.retry = true; |
| 6735 | #endif |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6736 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6737 | set_ethernet_addr(tp); |
| 6738 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6739 | usb_set_intfdata(intf, tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 6740 | netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6741 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6742 | ret = register_netdev(netdev); |
| 6743 | if (ret != 0) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 6744 | netif_err(tp, probe, netdev, "couldn't register the device\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6745 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 6748 | if (!rtl_can_wakeup(tp)) |
| 6749 | __rtl_set_wol(tp, 0); |
| 6750 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 6751 | tp->saved_wolopts = __rtl_get_wol(tp); |
| 6752 | if (tp->saved_wolopts) |
| 6753 | device_set_wakeup_enable(&udev->dev, true); |
| 6754 | else |
| 6755 | device_set_wakeup_enable(&udev->dev, false); |
| 6756 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 6757 | netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6758 | |
| 6759 | return 0; |
| 6760 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6761 | out1: |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 6762 | tasklet_kill(&tp->tx_tl); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6763 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6764 | out: |
| 6765 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6766 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6767 | } |
| 6768 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6769 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 6770 | { |
| 6771 | struct r8152 *tp = usb_get_intfdata(intf); |
| 6772 | |
| 6773 | usb_set_intfdata(intf, NULL); |
| 6774 | if (tp) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 6775 | rtl_set_unplug(tp); |
hayeswang | f561de3 | 2014-09-30 16:48:01 +0800 | [diff] [blame] | 6776 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6777 | unregister_netdev(tp->netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 6778 | tasklet_kill(&tp->tx_tl); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6779 | cancel_delayed_work_sync(&tp->hw_phy_work); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6780 | tp->rtl_ops.unload(tp); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6781 | rtl8152_release_firmware(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6782 | free_netdev(tp->netdev); |
| 6783 | } |
| 6784 | } |
| 6785 | |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 6786 | #define REALTEK_USB_DEVICE(vend, prod) \ |
| 6787 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
| 6788 | USB_DEVICE_ID_MATCH_INT_CLASS, \ |
| 6789 | .idVendor = (vend), \ |
| 6790 | .idProduct = (prod), \ |
| 6791 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC \ |
| 6792 | }, \ |
| 6793 | { \ |
| 6794 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \ |
| 6795 | USB_DEVICE_ID_MATCH_DEVICE, \ |
| 6796 | .idVendor = (vend), \ |
| 6797 | .idProduct = (prod), \ |
| 6798 | .bInterfaceClass = USB_CLASS_COMM, \ |
| 6799 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ |
| 6800 | .bInterfaceProtocol = USB_CDC_PROTO_NONE |
| 6801 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6802 | /* table of devices that work with this driver */ |
Arvind Yadav | 9b4355f | 2017-08-08 21:28:05 +0530 | [diff] [blame] | 6803 | static const struct usb_device_id rtl8152_table[] = { |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6804 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 6805 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)}, |
| 6806 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)}, |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 6807 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)}, |
| 6808 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 6809 | {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)}, |
Vasily Titskiy | 1006da1 | 2015-05-06 10:31:21 -0400 | [diff] [blame] | 6810 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)}, |
hayeswang | d248caf | 2016-10-18 11:41:48 +0800 | [diff] [blame] | 6811 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)}, |
| 6812 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)}, |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 6813 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082)}, |
hayeswang | d248caf | 2016-10-18 11:41:48 +0800 | [diff] [blame] | 6814 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)}, |
| 6815 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)}, |
| 6816 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)}, |
Kazutoshi Noguchi | b306053 | 2019-10-21 00:03:07 +0900 | [diff] [blame] | 6817 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)}, |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 6818 | {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)}, |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 6819 | {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)}, |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 6820 | {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)}, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6821 | {} |
| 6822 | }; |
| 6823 | |
| 6824 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 6825 | |
| 6826 | static struct usb_driver rtl8152_driver = { |
| 6827 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6828 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6829 | .probe = rtl8152_probe, |
| 6830 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6831 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6832 | .resume = rtl8152_resume, |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 6833 | .reset_resume = rtl8152_reset_resume, |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 6834 | .pre_reset = rtl8152_pre_reset, |
| 6835 | .post_reset = rtl8152_post_reset, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6836 | .supports_autosuspend = 1, |
hayeswang | a634782 | 2014-02-18 21:49:10 +0800 | [diff] [blame] | 6837 | .disable_hub_initiated_lpm = 1, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6838 | }; |
| 6839 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 6840 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6841 | |
| 6842 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 6843 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 6844 | MODULE_LICENSE("GPL"); |
Grant Grundler | c961e87 | 2016-07-14 11:27:16 -0700 | [diff] [blame] | 6845 | MODULE_VERSION(DRIVER_VERSION); |