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 | |
Hayes Wang | f99cd20 | 2020-01-22 16:02:06 +0800 | [diff] [blame^] | 2868 | if (tp->version == RTL_VER_09) { |
| 2869 | u32 ocp_data; |
| 2870 | |
| 2871 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); |
| 2872 | ocp_data &= ~FC_PATCH_TASK; |
| 2873 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); |
| 2874 | usleep_range(1000, 2000); |
| 2875 | ocp_data |= FC_PATCH_TASK; |
| 2876 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); |
| 2877 | } |
| 2878 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 2879 | return rtl_enable(tp); |
| 2880 | } |
| 2881 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 2882 | static void rtl_disable(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2883 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2884 | u32 ocp_data; |
| 2885 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2886 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 2887 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 2888 | rtl_drop_queued_tx(tp); |
| 2889 | return; |
| 2890 | } |
| 2891 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2892 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 2893 | ocp_data &= ~RCR_ACPT_ALL; |
| 2894 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 2895 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2896 | rtl_drop_queued_tx(tp); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 2897 | |
| 2898 | for (i = 0; i < RTL8152_MAX_TX; i++) |
| 2899 | usb_kill_urb(tp->tx_info[i].urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2900 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2901 | rxdy_gated_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2902 | |
| 2903 | for (i = 0; i < 1000; i++) { |
| 2904 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 2905 | if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY) |
| 2906 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2907 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | for (i = 0; i < 1000; i++) { |
| 2911 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY) |
| 2912 | break; |
hayeswang | 8ddfa07 | 2014-09-09 11:40:28 +0800 | [diff] [blame] | 2913 | usleep_range(1000, 2000); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
hayeswang | 445f7f4 | 2014-09-23 16:31:47 +0800 | [diff] [blame] | 2916 | rtl_stop_rx(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 2917 | |
| 2918 | rtl8152_nic_reset(tp); |
| 2919 | } |
| 2920 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2921 | static void r8152_power_cut_en(struct r8152 *tp, bool enable) |
| 2922 | { |
| 2923 | u32 ocp_data; |
| 2924 | |
| 2925 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL); |
| 2926 | if (enable) |
| 2927 | ocp_data |= POWER_CUT; |
| 2928 | else |
| 2929 | ocp_data &= ~POWER_CUT; |
| 2930 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data); |
| 2931 | |
| 2932 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS); |
| 2933 | ocp_data &= ~RESUME_INDICATE; |
| 2934 | ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 2935 | } |
| 2936 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2937 | static void rtl_rx_vlan_en(struct r8152 *tp, bool enable) |
| 2938 | { |
| 2939 | u32 ocp_data; |
| 2940 | |
| 2941 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR); |
| 2942 | if (enable) |
| 2943 | ocp_data |= CPCR_RX_VLAN; |
| 2944 | else |
| 2945 | ocp_data &= ~CPCR_RX_VLAN; |
| 2946 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data); |
| 2947 | } |
| 2948 | |
| 2949 | static int rtl8152_set_features(struct net_device *dev, |
| 2950 | netdev_features_t features) |
| 2951 | { |
| 2952 | netdev_features_t changed = features ^ dev->features; |
| 2953 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2954 | int ret; |
| 2955 | |
| 2956 | ret = usb_autopm_get_interface(tp->intf); |
| 2957 | if (ret < 0) |
| 2958 | goto out; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2959 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2960 | mutex_lock(&tp->control); |
| 2961 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2962 | if (changed & NETIF_F_HW_VLAN_CTAG_RX) { |
| 2963 | if (features & NETIF_F_HW_VLAN_CTAG_RX) |
| 2964 | rtl_rx_vlan_en(tp, true); |
| 2965 | else |
| 2966 | rtl_rx_vlan_en(tp, false); |
| 2967 | } |
| 2968 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 2969 | mutex_unlock(&tp->control); |
| 2970 | |
hayeswang | 405f8a0 | 2014-10-09 18:00:24 +0800 | [diff] [blame] | 2971 | usb_autopm_put_interface(tp->intf); |
| 2972 | |
| 2973 | out: |
| 2974 | return ret; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 2975 | } |
| 2976 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2977 | #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) |
| 2978 | |
| 2979 | static u32 __rtl_get_wol(struct r8152 *tp) |
| 2980 | { |
| 2981 | u32 ocp_data; |
| 2982 | u32 wolopts = 0; |
| 2983 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 2984 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 2985 | if (ocp_data & LINK_ON_WAKE_EN) |
| 2986 | wolopts |= WAKE_PHY; |
| 2987 | |
| 2988 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
| 2989 | if (ocp_data & UWF_EN) |
| 2990 | wolopts |= WAKE_UCAST; |
| 2991 | if (ocp_data & BWF_EN) |
| 2992 | wolopts |= WAKE_BCAST; |
| 2993 | if (ocp_data & MWF_EN) |
| 2994 | wolopts |= WAKE_MCAST; |
| 2995 | |
| 2996 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 2997 | if (ocp_data & MAGIC_EN) |
| 2998 | wolopts |= WAKE_MAGIC; |
| 2999 | |
| 3000 | return wolopts; |
| 3001 | } |
| 3002 | |
| 3003 | static void __rtl_set_wol(struct r8152 *tp, u32 wolopts) |
| 3004 | { |
| 3005 | u32 ocp_data; |
| 3006 | |
| 3007 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3008 | |
| 3009 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3010 | ocp_data &= ~LINK_ON_WAKE_EN; |
| 3011 | if (wolopts & WAKE_PHY) |
| 3012 | ocp_data |= LINK_ON_WAKE_EN; |
| 3013 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3014 | |
| 3015 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5); |
hayeswang | 92f7d07 | 2016-07-06 17:35:59 +0800 | [diff] [blame] | 3016 | ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3017 | if (wolopts & WAKE_UCAST) |
| 3018 | ocp_data |= UWF_EN; |
| 3019 | if (wolopts & WAKE_BCAST) |
| 3020 | ocp_data |= BWF_EN; |
| 3021 | if (wolopts & WAKE_MCAST) |
| 3022 | ocp_data |= MWF_EN; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 3023 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data); |
| 3024 | |
| 3025 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3026 | |
| 3027 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL); |
| 3028 | ocp_data &= ~MAGIC_EN; |
| 3029 | if (wolopts & WAKE_MAGIC) |
| 3030 | ocp_data |= MAGIC_EN; |
| 3031 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data); |
| 3032 | |
| 3033 | if (wolopts & WAKE_ANY) |
| 3034 | device_set_wakeup_enable(&tp->udev->dev, true); |
| 3035 | else |
| 3036 | device_set_wakeup_enable(&tp->udev->dev, false); |
| 3037 | } |
| 3038 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3039 | static void r8153_mac_clk_spd(struct r8152 *tp, bool enable) |
| 3040 | { |
| 3041 | /* MAC clock speed down */ |
| 3042 | if (enable) { |
| 3043 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, |
| 3044 | ALDPS_SPDWN_RATIO); |
| 3045 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, |
| 3046 | EEE_SPDWN_RATIO); |
| 3047 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, |
| 3048 | PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN | |
| 3049 | U1U2_SPDWN_EN | L1_SPDWN_EN); |
| 3050 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, |
| 3051 | PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN | |
| 3052 | TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN | |
| 3053 | TP1000_SPDWN_EN); |
| 3054 | } else { |
| 3055 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0); |
| 3056 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0); |
| 3057 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0); |
| 3058 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0); |
| 3059 | } |
| 3060 | } |
| 3061 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3062 | static void r8153_u1u2en(struct r8152 *tp, bool enable) |
| 3063 | { |
| 3064 | u8 u1u2[8]; |
| 3065 | |
| 3066 | if (enable) |
| 3067 | memset(u1u2, 0xff, sizeof(u1u2)); |
| 3068 | else |
| 3069 | memset(u1u2, 0x00, sizeof(u1u2)); |
| 3070 | |
| 3071 | usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2); |
| 3072 | } |
| 3073 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3074 | static void r8153b_u1u2en(struct r8152 *tp, bool enable) |
| 3075 | { |
| 3076 | u32 ocp_data; |
| 3077 | |
| 3078 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG); |
| 3079 | if (enable) |
| 3080 | ocp_data |= LPM_U1U2_EN; |
| 3081 | else |
| 3082 | ocp_data &= ~LPM_U1U2_EN; |
| 3083 | |
| 3084 | ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data); |
| 3085 | } |
| 3086 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3087 | static void r8153_u2p3en(struct r8152 *tp, bool enable) |
| 3088 | { |
| 3089 | u32 ocp_data; |
| 3090 | |
| 3091 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3092 | if (enable) |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3093 | ocp_data |= U2P3_ENABLE; |
| 3094 | else |
| 3095 | ocp_data &= ~U2P3_ENABLE; |
| 3096 | ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data); |
| 3097 | } |
| 3098 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3099 | static void r8153b_ups_flags(struct r8152 *tp) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3100 | { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3101 | u32 ups_flags = 0; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3102 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3103 | if (tp->ups_info.green) |
| 3104 | ups_flags |= UPS_FLAGS_EN_GREEN; |
| 3105 | |
| 3106 | if (tp->ups_info.aldps) |
| 3107 | ups_flags |= UPS_FLAGS_EN_ALDPS; |
| 3108 | |
| 3109 | if (tp->ups_info.eee) |
| 3110 | ups_flags |= UPS_FLAGS_EN_EEE; |
| 3111 | |
| 3112 | if (tp->ups_info.flow_control) |
| 3113 | ups_flags |= UPS_FLAGS_EN_FLOW_CTR; |
| 3114 | |
| 3115 | if (tp->ups_info.eee_ckdiv) |
| 3116 | ups_flags |= UPS_FLAGS_EN_EEE_CKDIV; |
| 3117 | |
| 3118 | if (tp->ups_info.eee_cmod_lv) |
| 3119 | ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN; |
| 3120 | |
| 3121 | if (tp->ups_info._10m_ckdiv) |
| 3122 | ups_flags |= UPS_FLAGS_EN_10M_CKDIV; |
| 3123 | |
| 3124 | if (tp->ups_info.eee_plloff_100) |
| 3125 | ups_flags |= UPS_FLAGS_EEE_PLLOFF_100; |
| 3126 | |
| 3127 | if (tp->ups_info.eee_plloff_giga) |
| 3128 | ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA; |
| 3129 | |
| 3130 | if (tp->ups_info._250m_ckdiv) |
| 3131 | ups_flags |= UPS_FLAGS_250M_CKDIV; |
| 3132 | |
| 3133 | if (tp->ups_info.ctap_short_off) |
| 3134 | ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS; |
| 3135 | |
| 3136 | switch (tp->ups_info.speed_duplex) { |
| 3137 | case NWAY_10M_HALF: |
| 3138 | ups_flags |= ups_flags_speed(1); |
| 3139 | break; |
| 3140 | case NWAY_10M_FULL: |
| 3141 | ups_flags |= ups_flags_speed(2); |
| 3142 | break; |
| 3143 | case NWAY_100M_HALF: |
| 3144 | ups_flags |= ups_flags_speed(3); |
| 3145 | break; |
| 3146 | case NWAY_100M_FULL: |
| 3147 | ups_flags |= ups_flags_speed(4); |
| 3148 | break; |
| 3149 | case NWAY_1000M_FULL: |
| 3150 | ups_flags |= ups_flags_speed(5); |
| 3151 | break; |
| 3152 | case FORCE_10M_HALF: |
| 3153 | ups_flags |= ups_flags_speed(6); |
| 3154 | break; |
| 3155 | case FORCE_10M_FULL: |
| 3156 | ups_flags |= ups_flags_speed(7); |
| 3157 | break; |
| 3158 | case FORCE_100M_HALF: |
| 3159 | ups_flags |= ups_flags_speed(8); |
| 3160 | break; |
| 3161 | case FORCE_100M_FULL: |
| 3162 | ups_flags |= ups_flags_speed(9); |
| 3163 | break; |
| 3164 | default: |
| 3165 | break; |
| 3166 | } |
| 3167 | |
| 3168 | ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | static void r8153b_green_en(struct r8152 *tp, bool enable) |
| 3172 | { |
| 3173 | u16 data; |
| 3174 | |
| 3175 | if (enable) { |
| 3176 | sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */ |
| 3177 | sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */ |
| 3178 | sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */ |
| 3179 | } else { |
| 3180 | sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */ |
| 3181 | sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */ |
| 3182 | sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */ |
| 3183 | } |
| 3184 | |
| 3185 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 3186 | data |= GREEN_ETH_EN; |
| 3187 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 3188 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3189 | tp->ups_info.green = enable; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3190 | } |
| 3191 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 3192 | static u16 r8153_phy_status(struct r8152 *tp, u16 desired) |
| 3193 | { |
| 3194 | u16 data; |
| 3195 | int i; |
| 3196 | |
| 3197 | for (i = 0; i < 500; i++) { |
| 3198 | data = ocp_reg_read(tp, OCP_PHY_STATUS); |
| 3199 | data &= PHY_STAT_MASK; |
| 3200 | if (desired) { |
| 3201 | if (data == desired) |
| 3202 | break; |
| 3203 | } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN || |
| 3204 | data == PHY_STAT_EXT_INIT) { |
| 3205 | break; |
| 3206 | } |
| 3207 | |
| 3208 | msleep(20); |
| 3209 | } |
| 3210 | |
| 3211 | return data; |
| 3212 | } |
| 3213 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3214 | static void r8153b_ups_en(struct r8152 *tp, bool enable) |
| 3215 | { |
| 3216 | u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 3217 | |
| 3218 | if (enable) { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 3219 | r8153b_ups_flags(tp); |
| 3220 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3221 | ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN; |
| 3222 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3223 | |
| 3224 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 3225 | ocp_data |= BIT(0); |
| 3226 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 3227 | } else { |
| 3228 | u16 data; |
| 3229 | |
| 3230 | ocp_data &= ~(UPS_EN | USP_PREWAKE); |
| 3231 | ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3232 | |
| 3233 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff); |
| 3234 | ocp_data &= ~BIT(0); |
| 3235 | ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data); |
| 3236 | |
| 3237 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 3238 | ocp_data &= ~PCUT_STATUS; |
| 3239 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 3240 | |
| 3241 | data = r8153_phy_status(tp, 0); |
| 3242 | |
| 3243 | switch (data) { |
| 3244 | case PHY_STAT_PWRDN: |
| 3245 | case PHY_STAT_EXT_INIT: |
| 3246 | r8153b_green_en(tp, |
| 3247 | test_bit(GREEN_ETHERNET, &tp->flags)); |
| 3248 | |
| 3249 | data = r8152_mdio_read(tp, MII_BMCR); |
| 3250 | data &= ~BMCR_PDOWN; |
| 3251 | data |= BMCR_RESET; |
| 3252 | r8152_mdio_write(tp, MII_BMCR, data); |
| 3253 | |
| 3254 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
Gustavo A. R. Silva | 9ca7867 | 2018-06-28 13:50:48 -0500 | [diff] [blame] | 3255 | /* fall through */ |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3256 | |
| 3257 | default: |
| 3258 | if (data != PHY_STAT_LAN_ON) |
| 3259 | netif_warn(tp, link, tp->netdev, |
| 3260 | "PHY not ready"); |
| 3261 | break; |
| 3262 | } |
| 3263 | } |
| 3264 | } |
| 3265 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3266 | static void r8153_power_cut_en(struct r8152 *tp, bool enable) |
| 3267 | { |
| 3268 | u32 ocp_data; |
| 3269 | |
| 3270 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 3271 | if (enable) |
| 3272 | ocp_data |= PWR_EN | PHASE2_EN; |
| 3273 | else |
| 3274 | ocp_data &= ~(PWR_EN | PHASE2_EN); |
| 3275 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3276 | |
| 3277 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 3278 | ocp_data &= ~PCUT_STATUS; |
| 3279 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 3280 | } |
| 3281 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3282 | static void r8153b_power_cut_en(struct r8152 *tp, bool enable) |
| 3283 | { |
| 3284 | u32 ocp_data; |
| 3285 | |
| 3286 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT); |
| 3287 | if (enable) |
| 3288 | ocp_data |= PWR_EN | PHASE2_EN; |
| 3289 | else |
| 3290 | ocp_data &= ~PWR_EN; |
| 3291 | ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data); |
| 3292 | |
| 3293 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0); |
| 3294 | ocp_data &= ~PCUT_STATUS; |
| 3295 | ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data); |
| 3296 | } |
| 3297 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3298 | static void r8153_queue_wake(struct r8152 *tp, bool enable) |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3299 | { |
| 3300 | u32 ocp_data; |
| 3301 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3302 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3303 | if (enable) |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3304 | ocp_data |= UPCOMING_RUNTIME_D3; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3305 | else |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3306 | ocp_data &= ~UPCOMING_RUNTIME_D3; |
| 3307 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3308 | |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3309 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG); |
| 3310 | ocp_data &= ~LINK_CHG_EVENT; |
| 3311 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data); |
| 3312 | |
| 3313 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 3314 | ocp_data &= ~LINK_CHANGE_FLAG; |
| 3315 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3316 | } |
| 3317 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 3318 | static bool rtl_can_wakeup(struct r8152 *tp) |
| 3319 | { |
| 3320 | struct usb_device *udev = tp->udev; |
| 3321 | |
| 3322 | return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP); |
| 3323 | } |
| 3324 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3325 | static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable) |
| 3326 | { |
| 3327 | if (enable) { |
| 3328 | u32 ocp_data; |
| 3329 | |
| 3330 | __rtl_set_wol(tp, WAKE_ANY); |
| 3331 | |
| 3332 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3333 | |
| 3334 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3335 | ocp_data |= LINK_OFF_WAKE_EN; |
| 3336 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3337 | |
| 3338 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 3339 | } else { |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3340 | u32 ocp_data; |
| 3341 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3342 | __rtl_set_wol(tp, tp->saved_wolopts); |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3343 | |
| 3344 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG); |
| 3345 | |
| 3346 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34); |
| 3347 | ocp_data &= ~LINK_OFF_WAKE_EN; |
| 3348 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data); |
| 3349 | |
| 3350 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3351 | } |
| 3352 | } |
hayeswang | f95ae8a | 2016-06-30 15:33:35 +0800 | [diff] [blame] | 3353 | |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3354 | static void rtl8153_runtime_enable(struct r8152 *tp, bool enable) |
| 3355 | { |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3356 | if (enable) { |
| 3357 | r8153_u1u2en(tp, false); |
| 3358 | r8153_u2p3en(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3359 | r8153_mac_clk_spd(tp, true); |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 3360 | rtl_runtime_suspend_enable(tp, true); |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 3361 | } else { |
hayeswang | 0255275 | 2017-06-09 17:11:42 +0800 | [diff] [blame] | 3362 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 3363 | r8153_mac_clk_spd(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 3364 | |
| 3365 | switch (tp->version) { |
| 3366 | case RTL_VER_03: |
| 3367 | case RTL_VER_04: |
| 3368 | break; |
| 3369 | case RTL_VER_05: |
| 3370 | case RTL_VER_06: |
| 3371 | default: |
| 3372 | r8153_u2p3en(tp, true); |
| 3373 | break; |
| 3374 | } |
| 3375 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 3376 | r8153_u1u2en(tp, true); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 3377 | } |
| 3378 | } |
| 3379 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3380 | static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable) |
| 3381 | { |
| 3382 | if (enable) { |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3383 | r8153_queue_wake(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3384 | r8153b_u1u2en(tp, false); |
| 3385 | r8153_u2p3en(tp, false); |
| 3386 | rtl_runtime_suspend_enable(tp, true); |
| 3387 | r8153b_ups_en(tp, true); |
| 3388 | } else { |
| 3389 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 3390 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3391 | rtl_runtime_suspend_enable(tp, false); |
| 3392 | r8153_u2p3en(tp, true); |
| 3393 | r8153b_u1u2en(tp, true); |
| 3394 | } |
| 3395 | } |
| 3396 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3397 | static void r8153_teredo_off(struct r8152 *tp) |
| 3398 | { |
| 3399 | u32 ocp_data; |
| 3400 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 3401 | switch (tp->version) { |
| 3402 | case RTL_VER_01: |
| 3403 | case RTL_VER_02: |
| 3404 | case RTL_VER_03: |
| 3405 | case RTL_VER_04: |
| 3406 | case RTL_VER_05: |
| 3407 | case RTL_VER_06: |
| 3408 | case RTL_VER_07: |
| 3409 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 3410 | ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | |
| 3411 | OOB_TEREDO_EN); |
| 3412 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 3413 | break; |
| 3414 | |
| 3415 | case RTL_VER_08: |
| 3416 | case RTL_VER_09: |
| 3417 | /* The bit 0 ~ 7 are relative with teredo settings. They are |
| 3418 | * W1C (write 1 to clear), so set all 1 to disable it. |
| 3419 | */ |
| 3420 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff); |
| 3421 | break; |
| 3422 | |
| 3423 | default: |
| 3424 | break; |
| 3425 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 3426 | |
| 3427 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE); |
| 3428 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0); |
| 3429 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0); |
| 3430 | } |
| 3431 | |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 3432 | static void rtl_reset_bmu(struct r8152 *tp) |
| 3433 | { |
| 3434 | u32 ocp_data; |
| 3435 | |
| 3436 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET); |
| 3437 | ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT); |
| 3438 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3439 | ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT; |
| 3440 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data); |
| 3441 | } |
| 3442 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3443 | /* Clear the bp to stop the firmware before loading a new one */ |
| 3444 | static void rtl_clear_bp(struct r8152 *tp, u16 type) |
| 3445 | { |
| 3446 | switch (tp->version) { |
| 3447 | case RTL_VER_01: |
| 3448 | case RTL_VER_02: |
| 3449 | case RTL_VER_07: |
| 3450 | break; |
| 3451 | case RTL_VER_03: |
| 3452 | case RTL_VER_04: |
| 3453 | case RTL_VER_05: |
| 3454 | case RTL_VER_06: |
| 3455 | ocp_write_byte(tp, type, PLA_BP_EN, 0); |
| 3456 | break; |
| 3457 | case RTL_VER_08: |
| 3458 | case RTL_VER_09: |
| 3459 | default: |
| 3460 | if (type == MCU_TYPE_USB) { |
| 3461 | ocp_write_byte(tp, MCU_TYPE_USB, USB_BP2_EN, 0); |
| 3462 | |
| 3463 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0); |
| 3464 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0); |
| 3465 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0); |
| 3466 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0); |
| 3467 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0); |
| 3468 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0); |
| 3469 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0); |
| 3470 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0); |
| 3471 | } else { |
| 3472 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0); |
| 3473 | } |
| 3474 | break; |
| 3475 | } |
| 3476 | |
| 3477 | ocp_write_word(tp, type, PLA_BP_0, 0); |
| 3478 | ocp_write_word(tp, type, PLA_BP_1, 0); |
| 3479 | ocp_write_word(tp, type, PLA_BP_2, 0); |
| 3480 | ocp_write_word(tp, type, PLA_BP_3, 0); |
| 3481 | ocp_write_word(tp, type, PLA_BP_4, 0); |
| 3482 | ocp_write_word(tp, type, PLA_BP_5, 0); |
| 3483 | ocp_write_word(tp, type, PLA_BP_6, 0); |
| 3484 | ocp_write_word(tp, type, PLA_BP_7, 0); |
| 3485 | |
| 3486 | /* wait 3 ms to make sure the firmware is stopped */ |
| 3487 | usleep_range(3000, 6000); |
| 3488 | ocp_write_word(tp, type, PLA_BP_BA, 0); |
| 3489 | } |
| 3490 | |
Hayes Wang | 470e391 | 2019-10-21 11:41:12 +0800 | [diff] [blame] | 3491 | static int r8153_patch_request(struct r8152 *tp, bool request) |
| 3492 | { |
| 3493 | u16 data; |
| 3494 | int i; |
| 3495 | |
| 3496 | data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD); |
| 3497 | if (request) |
| 3498 | data |= PATCH_REQUEST; |
| 3499 | else |
| 3500 | data &= ~PATCH_REQUEST; |
| 3501 | ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data); |
| 3502 | |
| 3503 | for (i = 0; request && i < 5000; i++) { |
| 3504 | usleep_range(1000, 2000); |
| 3505 | if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY) |
| 3506 | break; |
| 3507 | } |
| 3508 | |
| 3509 | if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) { |
| 3510 | netif_err(tp, drv, tp->netdev, "patch request fail\n"); |
| 3511 | r8153_patch_request(tp, false); |
| 3512 | return -ETIME; |
| 3513 | } else { |
| 3514 | return 0; |
| 3515 | } |
| 3516 | } |
| 3517 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3518 | static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key) |
| 3519 | { |
| 3520 | if (r8153_patch_request(tp, true)) { |
| 3521 | dev_err(&tp->intf->dev, "patch request fail\n"); |
| 3522 | return -ETIME; |
| 3523 | } |
| 3524 | |
| 3525 | sram_write(tp, key_addr, patch_key); |
| 3526 | sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK); |
| 3527 | |
| 3528 | return 0; |
| 3529 | } |
| 3530 | |
| 3531 | static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr) |
| 3532 | { |
| 3533 | u16 data; |
| 3534 | |
| 3535 | sram_write(tp, 0x0000, 0x0000); |
| 3536 | |
| 3537 | data = ocp_reg_read(tp, OCP_PHY_LOCK); |
| 3538 | data &= ~PATCH_LOCK; |
| 3539 | ocp_reg_write(tp, OCP_PHY_LOCK, data); |
| 3540 | |
| 3541 | sram_write(tp, key_addr, 0x0000); |
| 3542 | |
| 3543 | r8153_patch_request(tp, false); |
| 3544 | |
| 3545 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base); |
| 3546 | |
| 3547 | return 0; |
| 3548 | } |
| 3549 | |
| 3550 | static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy) |
| 3551 | { |
| 3552 | u32 length; |
| 3553 | u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start; |
| 3554 | bool rc = false; |
| 3555 | |
| 3556 | switch (tp->version) { |
| 3557 | case RTL_VER_04: |
| 3558 | case RTL_VER_05: |
| 3559 | case RTL_VER_06: |
| 3560 | fw_reg = 0xa014; |
| 3561 | ba_reg = 0xa012; |
| 3562 | patch_en_addr = 0xa01a; |
| 3563 | mode_reg = 0xb820; |
| 3564 | bp_start = 0xa000; |
| 3565 | break; |
| 3566 | default: |
| 3567 | goto out; |
| 3568 | } |
| 3569 | |
| 3570 | fw_offset = __le16_to_cpu(phy->fw_offset); |
| 3571 | if (fw_offset < sizeof(*phy)) { |
| 3572 | dev_err(&tp->intf->dev, "fw_offset too small\n"); |
| 3573 | goto out; |
| 3574 | } |
| 3575 | |
| 3576 | length = __le32_to_cpu(phy->blk_hdr.length); |
| 3577 | if (length < fw_offset) { |
| 3578 | dev_err(&tp->intf->dev, "invalid fw_offset\n"); |
| 3579 | goto out; |
| 3580 | } |
| 3581 | |
| 3582 | length -= __le16_to_cpu(phy->fw_offset); |
| 3583 | if (!length || (length & 1)) { |
| 3584 | dev_err(&tp->intf->dev, "invalid block length\n"); |
| 3585 | goto out; |
| 3586 | } |
| 3587 | |
| 3588 | if (__le16_to_cpu(phy->fw_reg) != fw_reg) { |
| 3589 | dev_err(&tp->intf->dev, "invalid register to load firmware\n"); |
| 3590 | goto out; |
| 3591 | } |
| 3592 | |
| 3593 | if (__le16_to_cpu(phy->ba_reg) != ba_reg) { |
| 3594 | dev_err(&tp->intf->dev, "invalid base address register\n"); |
| 3595 | goto out; |
| 3596 | } |
| 3597 | |
| 3598 | if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) { |
| 3599 | dev_err(&tp->intf->dev, |
| 3600 | "invalid patch mode enabled register\n"); |
| 3601 | goto out; |
| 3602 | } |
| 3603 | |
| 3604 | if (__le16_to_cpu(phy->mode_reg) != mode_reg) { |
| 3605 | dev_err(&tp->intf->dev, |
| 3606 | "invalid register to switch the mode\n"); |
| 3607 | goto out; |
| 3608 | } |
| 3609 | |
| 3610 | if (__le16_to_cpu(phy->bp_start) != bp_start) { |
| 3611 | dev_err(&tp->intf->dev, |
| 3612 | "invalid start register of break point\n"); |
| 3613 | goto out; |
| 3614 | } |
| 3615 | |
| 3616 | if (__le16_to_cpu(phy->bp_num) > 4) { |
| 3617 | dev_err(&tp->intf->dev, "invalid break point number\n"); |
| 3618 | goto out; |
| 3619 | } |
| 3620 | |
| 3621 | rc = true; |
| 3622 | out: |
| 3623 | return rc; |
| 3624 | } |
| 3625 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3626 | 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] | 3627 | { |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3628 | 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] | 3629 | bool rc = false; |
| 3630 | u32 length, type; |
| 3631 | int i, max_bp; |
| 3632 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3633 | type = __le32_to_cpu(mac->blk_hdr.type); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3634 | if (type == RTL_FW_PLA) { |
| 3635 | switch (tp->version) { |
| 3636 | case RTL_VER_01: |
| 3637 | case RTL_VER_02: |
| 3638 | case RTL_VER_07: |
| 3639 | fw_reg = 0xf800; |
| 3640 | bp_ba_addr = PLA_BP_BA; |
| 3641 | bp_en_addr = 0; |
| 3642 | bp_start = PLA_BP_0; |
| 3643 | max_bp = 8; |
| 3644 | break; |
| 3645 | case RTL_VER_03: |
| 3646 | case RTL_VER_04: |
| 3647 | case RTL_VER_05: |
| 3648 | case RTL_VER_06: |
| 3649 | case RTL_VER_08: |
| 3650 | case RTL_VER_09: |
| 3651 | fw_reg = 0xf800; |
| 3652 | bp_ba_addr = PLA_BP_BA; |
| 3653 | bp_en_addr = PLA_BP_EN; |
| 3654 | bp_start = PLA_BP_0; |
| 3655 | max_bp = 8; |
| 3656 | break; |
| 3657 | default: |
| 3658 | goto out; |
| 3659 | } |
| 3660 | } else if (type == RTL_FW_USB) { |
| 3661 | switch (tp->version) { |
| 3662 | case RTL_VER_03: |
| 3663 | case RTL_VER_04: |
| 3664 | case RTL_VER_05: |
| 3665 | case RTL_VER_06: |
| 3666 | fw_reg = 0xf800; |
| 3667 | bp_ba_addr = USB_BP_BA; |
| 3668 | bp_en_addr = USB_BP_EN; |
| 3669 | bp_start = USB_BP_0; |
| 3670 | max_bp = 8; |
| 3671 | break; |
| 3672 | case RTL_VER_08: |
| 3673 | case RTL_VER_09: |
| 3674 | fw_reg = 0xe600; |
| 3675 | bp_ba_addr = USB_BP_BA; |
| 3676 | bp_en_addr = USB_BP2_EN; |
| 3677 | bp_start = USB_BP_0; |
| 3678 | max_bp = 16; |
| 3679 | break; |
| 3680 | case RTL_VER_01: |
| 3681 | case RTL_VER_02: |
| 3682 | case RTL_VER_07: |
| 3683 | default: |
| 3684 | goto out; |
| 3685 | } |
| 3686 | } else { |
| 3687 | goto out; |
| 3688 | } |
| 3689 | |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3690 | fw_offset = __le16_to_cpu(mac->fw_offset); |
| 3691 | if (fw_offset < sizeof(*mac)) { |
| 3692 | dev_err(&tp->intf->dev, "fw_offset too small\n"); |
| 3693 | goto out; |
| 3694 | } |
| 3695 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3696 | length = __le32_to_cpu(mac->blk_hdr.length); |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3697 | if (length < fw_offset) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3698 | dev_err(&tp->intf->dev, "invalid fw_offset\n"); |
| 3699 | goto out; |
| 3700 | } |
| 3701 | |
Hayes Wang | 5a16a3d | 2019-10-21 11:41:11 +0800 | [diff] [blame] | 3702 | length -= fw_offset; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3703 | if (length < 4 || (length & 3)) { |
| 3704 | dev_err(&tp->intf->dev, "invalid block length\n"); |
| 3705 | goto out; |
| 3706 | } |
| 3707 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3708 | if (__le16_to_cpu(mac->fw_reg) != fw_reg) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3709 | dev_err(&tp->intf->dev, "invalid register to load firmware\n"); |
| 3710 | goto out; |
| 3711 | } |
| 3712 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3713 | if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3714 | dev_err(&tp->intf->dev, "invalid base address register\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_en_addr) != bp_en_addr) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3719 | dev_err(&tp->intf->dev, "invalid enabled mask register\n"); |
| 3720 | goto out; |
| 3721 | } |
| 3722 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3723 | if (__le16_to_cpu(mac->bp_start) != bp_start) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3724 | dev_err(&tp->intf->dev, |
| 3725 | "invalid start register of break point\n"); |
| 3726 | goto out; |
| 3727 | } |
| 3728 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3729 | if (__le16_to_cpu(mac->bp_num) > max_bp) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3730 | dev_err(&tp->intf->dev, "invalid break point number\n"); |
| 3731 | goto out; |
| 3732 | } |
| 3733 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3734 | for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) { |
| 3735 | if (mac->bp[i]) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3736 | dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i); |
| 3737 | goto out; |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | rc = true; |
| 3742 | out: |
| 3743 | return rc; |
| 3744 | } |
| 3745 | |
| 3746 | /* Verify the checksum for the firmware file. It is calculated from the version |
| 3747 | * field to the end of the file. Compare the result with the checksum field to |
| 3748 | * make sure the file is correct. |
| 3749 | */ |
| 3750 | static long rtl8152_fw_verify_checksum(struct r8152 *tp, |
| 3751 | struct fw_header *fw_hdr, size_t size) |
| 3752 | { |
| 3753 | unsigned char checksum[sizeof(fw_hdr->checksum)]; |
| 3754 | struct crypto_shash *alg; |
| 3755 | struct shash_desc *sdesc; |
| 3756 | size_t len; |
| 3757 | long rc; |
| 3758 | |
| 3759 | alg = crypto_alloc_shash("sha256", 0, 0); |
| 3760 | if (IS_ERR(alg)) { |
| 3761 | rc = PTR_ERR(alg); |
| 3762 | goto out; |
| 3763 | } |
| 3764 | |
| 3765 | if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) { |
| 3766 | rc = -EFAULT; |
| 3767 | dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n", |
| 3768 | crypto_shash_digestsize(alg)); |
| 3769 | goto free_shash; |
| 3770 | } |
| 3771 | |
| 3772 | len = sizeof(*sdesc) + crypto_shash_descsize(alg); |
| 3773 | sdesc = kmalloc(len, GFP_KERNEL); |
| 3774 | if (!sdesc) { |
| 3775 | rc = -ENOMEM; |
| 3776 | goto free_shash; |
| 3777 | } |
| 3778 | sdesc->tfm = alg; |
| 3779 | |
| 3780 | len = size - sizeof(fw_hdr->checksum); |
| 3781 | rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum); |
| 3782 | kfree(sdesc); |
| 3783 | if (rc) |
| 3784 | goto free_shash; |
| 3785 | |
| 3786 | if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) { |
| 3787 | dev_err(&tp->intf->dev, "checksum fail\n"); |
| 3788 | rc = -EFAULT; |
| 3789 | } |
| 3790 | |
| 3791 | free_shash: |
| 3792 | crypto_free_shash(alg); |
| 3793 | out: |
| 3794 | return rc; |
| 3795 | } |
| 3796 | |
| 3797 | static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw) |
| 3798 | { |
| 3799 | const struct firmware *fw = rtl_fw->fw; |
| 3800 | struct fw_header *fw_hdr = (struct fw_header *)fw->data; |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3801 | struct fw_mac *pla = NULL, *usb = NULL; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3802 | struct fw_phy_patch_key *start = NULL; |
| 3803 | struct fw_phy_nc *phy_nc = NULL; |
| 3804 | struct fw_block *stop = NULL; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3805 | long ret = -EFAULT; |
| 3806 | int i; |
| 3807 | |
| 3808 | if (fw->size < sizeof(*fw_hdr)) { |
| 3809 | dev_err(&tp->intf->dev, "file too small\n"); |
| 3810 | goto fail; |
| 3811 | } |
| 3812 | |
| 3813 | ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size); |
| 3814 | if (ret) |
| 3815 | goto fail; |
| 3816 | |
| 3817 | ret = -EFAULT; |
| 3818 | |
| 3819 | for (i = sizeof(*fw_hdr); i < fw->size;) { |
| 3820 | struct fw_block *block = (struct fw_block *)&fw->data[i]; |
| 3821 | u32 type; |
| 3822 | |
| 3823 | if ((i + sizeof(*block)) > fw->size) |
| 3824 | goto fail; |
| 3825 | |
| 3826 | type = __le32_to_cpu(block->type); |
| 3827 | switch (type) { |
| 3828 | case RTL_FW_END: |
| 3829 | if (__le32_to_cpu(block->length) != sizeof(*block)) |
| 3830 | goto fail; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3831 | goto fw_end; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3832 | case RTL_FW_PLA: |
| 3833 | if (pla) { |
| 3834 | dev_err(&tp->intf->dev, |
| 3835 | "multiple PLA firmware encountered"); |
| 3836 | goto fail; |
| 3837 | } |
| 3838 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3839 | pla = (struct fw_mac *)block; |
| 3840 | if (!rtl8152_is_fw_mac_ok(tp, pla)) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3841 | dev_err(&tp->intf->dev, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3842 | "check PLA firmware failed\n"); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3843 | goto fail; |
| 3844 | } |
| 3845 | break; |
| 3846 | case RTL_FW_USB: |
| 3847 | if (usb) { |
| 3848 | dev_err(&tp->intf->dev, |
| 3849 | "multiple USB firmware encountered"); |
| 3850 | goto fail; |
| 3851 | } |
| 3852 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3853 | usb = (struct fw_mac *)block; |
| 3854 | if (!rtl8152_is_fw_mac_ok(tp, usb)) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3855 | dev_err(&tp->intf->dev, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3856 | "check USB firmware failed\n"); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3857 | goto fail; |
| 3858 | } |
| 3859 | break; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3860 | case RTL_FW_PHY_START: |
| 3861 | if (start || phy_nc || stop) { |
| 3862 | dev_err(&tp->intf->dev, |
| 3863 | "check PHY_START fail\n"); |
| 3864 | goto fail; |
| 3865 | } |
| 3866 | |
| 3867 | if (__le32_to_cpu(block->length) != sizeof(*start)) { |
| 3868 | dev_err(&tp->intf->dev, |
| 3869 | "Invalid length for PHY_START\n"); |
| 3870 | goto fail; |
| 3871 | } |
| 3872 | |
| 3873 | start = (struct fw_phy_patch_key *)block; |
| 3874 | break; |
| 3875 | case RTL_FW_PHY_STOP: |
| 3876 | if (stop || !start) { |
| 3877 | dev_err(&tp->intf->dev, |
| 3878 | "Check PHY_STOP fail\n"); |
| 3879 | goto fail; |
| 3880 | } |
| 3881 | |
| 3882 | if (__le32_to_cpu(block->length) != sizeof(*block)) { |
| 3883 | dev_err(&tp->intf->dev, |
| 3884 | "Invalid length for PHY_STOP\n"); |
| 3885 | goto fail; |
| 3886 | } |
| 3887 | |
| 3888 | stop = block; |
| 3889 | break; |
| 3890 | case RTL_FW_PHY_NC: |
| 3891 | if (!start || stop) { |
| 3892 | dev_err(&tp->intf->dev, |
| 3893 | "check PHY_NC fail\n"); |
| 3894 | goto fail; |
| 3895 | } |
| 3896 | |
| 3897 | if (phy_nc) { |
| 3898 | dev_err(&tp->intf->dev, |
| 3899 | "multiple PHY NC encountered\n"); |
| 3900 | goto fail; |
| 3901 | } |
| 3902 | |
| 3903 | phy_nc = (struct fw_phy_nc *)block; |
| 3904 | if (!rtl8152_is_fw_phy_nc_ok(tp, phy_nc)) { |
| 3905 | dev_err(&tp->intf->dev, |
| 3906 | "check PHY NC firmware failed\n"); |
| 3907 | goto fail; |
| 3908 | } |
| 3909 | |
| 3910 | break; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3911 | default: |
| 3912 | dev_warn(&tp->intf->dev, "Unknown type %u is found\n", |
| 3913 | type); |
| 3914 | break; |
| 3915 | } |
| 3916 | |
| 3917 | /* next block */ |
| 3918 | i += ALIGN(__le32_to_cpu(block->length), 8); |
| 3919 | } |
| 3920 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3921 | fw_end: |
| 3922 | if ((phy_nc || start) && !stop) { |
| 3923 | dev_err(&tp->intf->dev, "without PHY_STOP\n"); |
| 3924 | goto fail; |
| 3925 | } |
| 3926 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3927 | return 0; |
| 3928 | fail: |
| 3929 | return ret; |
| 3930 | } |
| 3931 | |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 3932 | static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy) |
| 3933 | { |
| 3934 | u16 mode_reg, bp_index; |
| 3935 | u32 length, i, num; |
| 3936 | __le16 *data; |
| 3937 | |
| 3938 | mode_reg = __le16_to_cpu(phy->mode_reg); |
| 3939 | sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre)); |
| 3940 | sram_write(tp, __le16_to_cpu(phy->ba_reg), |
| 3941 | __le16_to_cpu(phy->ba_data)); |
| 3942 | |
| 3943 | length = __le32_to_cpu(phy->blk_hdr.length); |
| 3944 | length -= __le16_to_cpu(phy->fw_offset); |
| 3945 | num = length / 2; |
| 3946 | data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset)); |
| 3947 | |
| 3948 | ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg)); |
| 3949 | for (i = 0; i < num; i++) |
| 3950 | ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i])); |
| 3951 | |
| 3952 | sram_write(tp, __le16_to_cpu(phy->patch_en_addr), |
| 3953 | __le16_to_cpu(phy->patch_en_value)); |
| 3954 | |
| 3955 | bp_index = __le16_to_cpu(phy->bp_start); |
| 3956 | num = __le16_to_cpu(phy->bp_num); |
| 3957 | for (i = 0; i < num; i++) { |
| 3958 | sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i])); |
| 3959 | bp_index += 2; |
| 3960 | } |
| 3961 | |
| 3962 | sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post)); |
| 3963 | |
| 3964 | dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info); |
| 3965 | } |
| 3966 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3967 | 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] | 3968 | { |
| 3969 | u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg; |
| 3970 | u32 length; |
| 3971 | u8 *data; |
| 3972 | int i; |
| 3973 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3974 | switch (__le32_to_cpu(mac->blk_hdr.type)) { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3975 | case RTL_FW_PLA: |
| 3976 | type = MCU_TYPE_PLA; |
| 3977 | break; |
| 3978 | case RTL_FW_USB: |
| 3979 | type = MCU_TYPE_USB; |
| 3980 | break; |
| 3981 | default: |
| 3982 | return; |
| 3983 | } |
| 3984 | |
| 3985 | rtl_clear_bp(tp, type); |
| 3986 | |
| 3987 | /* Enable backup/restore of MACDBG. This is required after clearing PLA |
| 3988 | * break points and before applying the PLA firmware. |
| 3989 | */ |
| 3990 | if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA && |
| 3991 | !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) { |
| 3992 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM); |
| 3993 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM); |
| 3994 | } |
| 3995 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3996 | length = __le32_to_cpu(mac->blk_hdr.length); |
| 3997 | length -= __le16_to_cpu(mac->fw_offset); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 3998 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 3999 | data = (u8 *)mac; |
| 4000 | data += __le16_to_cpu(mac->fw_offset); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4001 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4002 | 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] | 4003 | type); |
| 4004 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4005 | ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr), |
| 4006 | __le16_to_cpu(mac->bp_ba_value)); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4007 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4008 | bp_index = __le16_to_cpu(mac->bp_start); |
| 4009 | bp_num = __le16_to_cpu(mac->bp_num); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4010 | for (i = 0; i < bp_num; i++) { |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4011 | 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] | 4012 | bp_index += 2; |
| 4013 | } |
| 4014 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4015 | bp_en_addr = __le16_to_cpu(mac->bp_en_addr); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4016 | if (bp_en_addr) |
| 4017 | ocp_write_word(tp, type, bp_en_addr, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4018 | __le16_to_cpu(mac->bp_en_value)); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4019 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4020 | fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4021 | if (fw_ver_reg) |
| 4022 | ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg, |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4023 | mac->fw_ver_data); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4024 | |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4025 | dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | static void rtl8152_apply_firmware(struct r8152 *tp) |
| 4029 | { |
| 4030 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
Hayes Wang | 8e484eb | 2019-10-23 21:24:44 +0800 | [diff] [blame] | 4031 | const struct firmware *fw; |
| 4032 | struct fw_header *fw_hdr; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 4033 | struct fw_phy_patch_key *key; |
| 4034 | u16 key_addr = 0; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4035 | int i; |
| 4036 | |
| 4037 | if (IS_ERR_OR_NULL(rtl_fw->fw)) |
| 4038 | return; |
| 4039 | |
Hayes Wang | 8e484eb | 2019-10-23 21:24:44 +0800 | [diff] [blame] | 4040 | fw = rtl_fw->fw; |
| 4041 | fw_hdr = (struct fw_header *)fw->data; |
| 4042 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4043 | if (rtl_fw->pre_fw) |
| 4044 | rtl_fw->pre_fw(tp); |
| 4045 | |
| 4046 | for (i = offsetof(struct fw_header, blocks); i < fw->size;) { |
| 4047 | struct fw_block *block = (struct fw_block *)&fw->data[i]; |
| 4048 | |
| 4049 | switch (__le32_to_cpu(block->type)) { |
| 4050 | case RTL_FW_END: |
| 4051 | goto post_fw; |
| 4052 | case RTL_FW_PLA: |
| 4053 | case RTL_FW_USB: |
Hayes Wang | a66edaa | 2019-10-21 11:41:10 +0800 | [diff] [blame] | 4054 | rtl8152_fw_mac_apply(tp, (struct fw_mac *)block); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4055 | break; |
Hayes Wang | af14288 | 2019-10-21 11:41:13 +0800 | [diff] [blame] | 4056 | case RTL_FW_PHY_START: |
| 4057 | key = (struct fw_phy_patch_key *)block; |
| 4058 | key_addr = __le16_to_cpu(key->key_reg); |
| 4059 | r8153_pre_ram_code(tp, key_addr, |
| 4060 | __le16_to_cpu(key->key_data)); |
| 4061 | break; |
| 4062 | case RTL_FW_PHY_STOP: |
| 4063 | WARN_ON(!key_addr); |
| 4064 | r8153_post_ram_code(tp, key_addr); |
| 4065 | break; |
| 4066 | case RTL_FW_PHY_NC: |
| 4067 | rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block); |
| 4068 | break; |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4069 | default: |
| 4070 | break; |
| 4071 | } |
| 4072 | |
| 4073 | i += ALIGN(__le32_to_cpu(block->length), 8); |
| 4074 | } |
| 4075 | |
| 4076 | post_fw: |
| 4077 | if (rtl_fw->post_fw) |
| 4078 | rtl_fw->post_fw(tp); |
| 4079 | |
| 4080 | strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE); |
| 4081 | dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version); |
| 4082 | } |
| 4083 | |
| 4084 | static void rtl8152_release_firmware(struct r8152 *tp) |
| 4085 | { |
| 4086 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
| 4087 | |
| 4088 | if (!IS_ERR_OR_NULL(rtl_fw->fw)) { |
| 4089 | release_firmware(rtl_fw->fw); |
| 4090 | rtl_fw->fw = NULL; |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | static int rtl8152_request_firmware(struct r8152 *tp) |
| 4095 | { |
| 4096 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
| 4097 | long rc; |
| 4098 | |
| 4099 | if (rtl_fw->fw || !rtl_fw->fw_name) { |
| 4100 | dev_info(&tp->intf->dev, "skip request firmware\n"); |
| 4101 | rc = 0; |
| 4102 | goto result; |
| 4103 | } |
| 4104 | |
| 4105 | rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev); |
| 4106 | if (rc < 0) |
| 4107 | goto result; |
| 4108 | |
| 4109 | rc = rtl8152_check_firmware(tp, rtl_fw); |
| 4110 | if (rc < 0) |
| 4111 | release_firmware(rtl_fw->fw); |
| 4112 | |
| 4113 | result: |
| 4114 | if (rc) { |
| 4115 | rtl_fw->fw = ERR_PTR(rc); |
| 4116 | |
| 4117 | dev_warn(&tp->intf->dev, |
| 4118 | "unable to load firmware patch %s (%ld)\n", |
| 4119 | rtl_fw->fw_name, rc); |
| 4120 | } |
| 4121 | |
| 4122 | return rc; |
| 4123 | } |
| 4124 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4125 | static void r8152_aldps_en(struct r8152 *tp, bool enable) |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4126 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4127 | if (enable) { |
| 4128 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS | |
| 4129 | LINKENA | DIS_SDSAVE); |
| 4130 | } else { |
| 4131 | ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | |
| 4132 | DIS_SDSAVE); |
| 4133 | msleep(20); |
| 4134 | } |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4135 | } |
| 4136 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4137 | static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg) |
| 4138 | { |
| 4139 | ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev); |
| 4140 | ocp_reg_write(tp, OCP_EEE_DATA, reg); |
| 4141 | ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev); |
| 4142 | } |
| 4143 | |
| 4144 | static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg) |
| 4145 | { |
| 4146 | u16 data; |
| 4147 | |
| 4148 | r8152_mmd_indirect(tp, dev, reg); |
| 4149 | data = ocp_reg_read(tp, OCP_EEE_DATA); |
| 4150 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 4151 | |
| 4152 | return data; |
| 4153 | } |
| 4154 | |
| 4155 | static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data) |
| 4156 | { |
| 4157 | r8152_mmd_indirect(tp, dev, reg); |
| 4158 | ocp_reg_write(tp, OCP_EEE_DATA, data); |
| 4159 | ocp_reg_write(tp, OCP_EEE_AR, 0x0000); |
| 4160 | } |
| 4161 | |
| 4162 | static void r8152_eee_en(struct r8152 *tp, bool enable) |
| 4163 | { |
| 4164 | u16 config1, config2, config3; |
| 4165 | u32 ocp_data; |
| 4166 | |
| 4167 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 4168 | config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask; |
| 4169 | config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2); |
| 4170 | config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask; |
| 4171 | |
| 4172 | if (enable) { |
| 4173 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 4174 | config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN; |
| 4175 | config1 |= sd_rise_time(1); |
| 4176 | config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN; |
| 4177 | config3 |= fast_snr(42); |
| 4178 | } else { |
| 4179 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 4180 | config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | |
| 4181 | RX_QUIET_EN); |
| 4182 | config1 |= sd_rise_time(7); |
| 4183 | config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN); |
| 4184 | config3 |= fast_snr(511); |
| 4185 | } |
| 4186 | |
| 4187 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 4188 | ocp_reg_write(tp, OCP_EEE_CONFIG1, config1); |
| 4189 | ocp_reg_write(tp, OCP_EEE_CONFIG2, config2); |
| 4190 | ocp_reg_write(tp, OCP_EEE_CONFIG3, config3); |
| 4191 | } |
| 4192 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4193 | static void r8153_eee_en(struct r8152 *tp, bool enable) |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4194 | { |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4195 | u32 ocp_data; |
| 4196 | u16 config; |
| 4197 | |
| 4198 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR); |
| 4199 | config = ocp_reg_read(tp, OCP_EEE_CFG); |
| 4200 | |
| 4201 | if (enable) { |
| 4202 | ocp_data |= EEE_RX_EN | EEE_TX_EN; |
| 4203 | config |= EEE10_EN; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 4204 | } else { |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4205 | ocp_data &= ~(EEE_RX_EN | EEE_TX_EN); |
| 4206 | config &= ~EEE10_EN; |
| 4207 | } |
| 4208 | |
| 4209 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data); |
| 4210 | ocp_reg_write(tp, OCP_EEE_CFG, config); |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4211 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4212 | tp->ups_info.eee = enable; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | static void rtl_eee_enable(struct r8152 *tp, bool enable) |
| 4216 | { |
| 4217 | switch (tp->version) { |
| 4218 | case RTL_VER_01: |
| 4219 | case RTL_VER_02: |
| 4220 | case RTL_VER_07: |
| 4221 | if (enable) { |
| 4222 | r8152_eee_en(tp, true); |
| 4223 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, |
| 4224 | tp->eee_adv); |
| 4225 | } else { |
| 4226 | r8152_eee_en(tp, false); |
| 4227 | r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0); |
| 4228 | } |
| 4229 | break; |
| 4230 | case RTL_VER_03: |
| 4231 | case RTL_VER_04: |
| 4232 | case RTL_VER_05: |
| 4233 | case RTL_VER_06: |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4234 | case RTL_VER_08: |
| 4235 | case RTL_VER_09: |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4236 | if (enable) { |
| 4237 | r8153_eee_en(tp, true); |
| 4238 | ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv); |
| 4239 | } else { |
| 4240 | r8153_eee_en(tp, false); |
| 4241 | ocp_reg_write(tp, OCP_EEE_ADV, 0); |
| 4242 | } |
| 4243 | break; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4244 | default: |
| 4245 | break; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 4246 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | static void r8152b_enable_fc(struct r8152 *tp) |
| 4250 | { |
| 4251 | u16 anar; |
| 4252 | |
| 4253 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 4254 | anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM; |
| 4255 | r8152_mdio_write(tp, MII_ADVERTISE, anar); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4256 | |
| 4257 | tp->ups_info.flow_control = true; |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4258 | } |
| 4259 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4260 | static void rtl8152_disable(struct r8152 *tp) |
| 4261 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4262 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4263 | rtl_disable(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4264 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4265 | } |
| 4266 | |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4267 | static void r8152b_hw_phy_cfg(struct r8152 *tp) |
| 4268 | { |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4269 | rtl8152_apply_firmware(tp); |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4270 | rtl_eee_enable(tp, tp->eee_en); |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 4271 | r8152_aldps_en(tp, true); |
| 4272 | r8152b_enable_fc(tp); |
hayeswang | f0cbe0a | 2014-02-18 21:49:03 +0800 | [diff] [blame] | 4273 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4274 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 4349968 | 2014-02-18 21:48:58 +0800 | [diff] [blame] | 4275 | } |
| 4276 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4277 | static void wait_oob_link_list_ready(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4278 | { |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 4279 | u32 ocp_data; |
| 4280 | int i; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4281 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4282 | for (i = 0; i < 1000; i++) { |
| 4283 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4284 | if (ocp_data & LINK_LIST_READY) |
| 4285 | break; |
| 4286 | usleep_range(1000, 2000); |
| 4287 | } |
| 4288 | } |
| 4289 | |
| 4290 | static void r8152b_exit_oob(struct r8152 *tp) |
| 4291 | { |
| 4292 | u32 ocp_data; |
| 4293 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4294 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4295 | ocp_data &= ~RCR_ACPT_ALL; |
| 4296 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4297 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4298 | rxdy_gated_en(tp, true); |
hayeswang | da9bd11 | 2014-02-18 21:49:08 +0800 | [diff] [blame] | 4299 | r8153_teredo_off(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4300 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML); |
| 4301 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00); |
| 4302 | |
| 4303 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4304 | ocp_data &= ~NOW_IS_OOB; |
| 4305 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4306 | |
| 4307 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4308 | ocp_data &= ~MCU_BORW_EN; |
| 4309 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4310 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4311 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4312 | |
| 4313 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4314 | ocp_data |= RE_INIT_LL; |
| 4315 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4316 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4317 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4318 | |
| 4319 | rtl8152_nic_reset(tp); |
| 4320 | |
| 4321 | /* rx share fifo credit full threshold */ |
| 4322 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 4323 | |
hayeswang | a3cc465 | 2014-07-24 16:37:43 +0800 | [diff] [blame] | 4324 | if (tp->udev->speed == USB_SPEED_FULL || |
| 4325 | tp->udev->speed == USB_SPEED_LOW) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4326 | /* rx share fifo credit near full threshold */ |
| 4327 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 4328 | RXFIFO_THR2_FULL); |
| 4329 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 4330 | RXFIFO_THR3_FULL); |
| 4331 | } else { |
| 4332 | /* rx share fifo credit near full threshold */ |
| 4333 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, |
| 4334 | RXFIFO_THR2_HIGH); |
| 4335 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, |
| 4336 | RXFIFO_THR3_HIGH); |
| 4337 | } |
| 4338 | |
| 4339 | /* TX share fifo free credit full threshold */ |
| 4340 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL); |
| 4341 | |
| 4342 | 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] | 4343 | 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] | 4344 | ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA, |
| 4345 | TEST_MODE_DISABLE | TX_SIZE_ADJUST1); |
| 4346 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4347 | 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] | 4348 | |
| 4349 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 4350 | |
| 4351 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 4352 | ocp_data |= TCR0_AUTO_FIFO; |
| 4353 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 4354 | } |
| 4355 | |
| 4356 | static void r8152b_enter_oob(struct r8152 *tp) |
| 4357 | { |
hayeswang | 45f4a19 | 2014-01-06 17:08:41 +0800 | [diff] [blame] | 4358 | u32 ocp_data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4359 | |
| 4360 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4361 | ocp_data &= ~NOW_IS_OOB; |
| 4362 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4363 | |
| 4364 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB); |
| 4365 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB); |
| 4366 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB); |
| 4367 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4368 | rtl_disable(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4369 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4370 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4371 | |
| 4372 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4373 | ocp_data |= RE_INIT_LL; |
| 4374 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4375 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4376 | wait_oob_link_list_ready(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4377 | |
| 4378 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS); |
| 4379 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4380 | rtl_rx_vlan_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4381 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4382 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4383 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4384 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4385 | |
| 4386 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4387 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 4388 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4389 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4390 | rxdy_gated_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4391 | |
| 4392 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4393 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 4394 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4395 | } |
| 4396 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4397 | static int r8153_pre_firmware_1(struct r8152 *tp) |
| 4398 | { |
| 4399 | int i; |
| 4400 | |
| 4401 | /* Wait till the WTD timer is ready. It would take at most 104 ms. */ |
| 4402 | for (i = 0; i < 104; i++) { |
| 4403 | u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL); |
| 4404 | |
| 4405 | if (!(ocp_data & WTD1_EN)) |
| 4406 | break; |
| 4407 | usleep_range(1000, 2000); |
| 4408 | } |
| 4409 | |
| 4410 | return 0; |
| 4411 | } |
| 4412 | |
| 4413 | static int r8153_post_firmware_1(struct r8152 *tp) |
| 4414 | { |
| 4415 | /* set USB_BP_4 to support USB_SPEED_SUPER only */ |
| 4416 | if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) |
| 4417 | ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY); |
| 4418 | |
| 4419 | /* reset UPHY timer to 36 ms */ |
| 4420 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); |
| 4421 | |
| 4422 | return 0; |
| 4423 | } |
| 4424 | |
| 4425 | static int r8153_pre_firmware_2(struct r8152 *tp) |
| 4426 | { |
| 4427 | u32 ocp_data; |
| 4428 | |
| 4429 | r8153_pre_firmware_1(tp); |
| 4430 | |
| 4431 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); |
| 4432 | ocp_data &= ~FW_FIX_SUSPEND; |
| 4433 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data); |
| 4434 | |
| 4435 | return 0; |
| 4436 | } |
| 4437 | |
| 4438 | static int r8153_post_firmware_2(struct r8152 *tp) |
| 4439 | { |
| 4440 | u32 ocp_data; |
| 4441 | |
| 4442 | /* enable bp0 if support USB_SPEED_SUPER only */ |
| 4443 | if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) { |
| 4444 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); |
| 4445 | ocp_data |= BIT(0); |
| 4446 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); |
| 4447 | } |
| 4448 | |
| 4449 | /* reset UPHY timer to 36 ms */ |
| 4450 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16); |
| 4451 | |
| 4452 | /* enable U3P3 check, set the counter to 4 */ |
| 4453 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4); |
| 4454 | |
| 4455 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0); |
| 4456 | ocp_data |= FW_FIX_SUSPEND; |
| 4457 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, 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 | return 0; |
| 4464 | } |
| 4465 | |
| 4466 | static int r8153_post_firmware_3(struct r8152 *tp) |
| 4467 | { |
| 4468 | u32 ocp_data; |
| 4469 | |
| 4470 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 4471 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 4472 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 4473 | |
| 4474 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); |
| 4475 | ocp_data |= FW_IP_RESET_EN; |
| 4476 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); |
| 4477 | |
| 4478 | return 0; |
| 4479 | } |
| 4480 | |
| 4481 | static int r8153b_pre_firmware_1(struct r8152 *tp) |
| 4482 | { |
| 4483 | /* enable fc timer and set timer to 1 second. */ |
| 4484 | ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER, |
| 4485 | CTRL_TIMER_EN | (1000 / 8)); |
| 4486 | |
| 4487 | return 0; |
| 4488 | } |
| 4489 | |
| 4490 | static int r8153b_post_firmware_1(struct r8152 *tp) |
| 4491 | { |
| 4492 | u32 ocp_data; |
| 4493 | |
| 4494 | /* enable bp0 for RTL8153-BND */ |
| 4495 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1); |
| 4496 | if (ocp_data & BND_MASK) { |
| 4497 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN); |
| 4498 | ocp_data |= BIT(0); |
| 4499 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data); |
| 4500 | } |
| 4501 | |
| 4502 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL); |
| 4503 | ocp_data |= FLOW_CTRL_PATCH_OPT; |
| 4504 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data); |
| 4505 | |
| 4506 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK); |
| 4507 | ocp_data |= FC_PATCH_TASK; |
| 4508 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data); |
| 4509 | |
| 4510 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1); |
| 4511 | ocp_data |= FW_IP_RESET_EN; |
| 4512 | ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data); |
| 4513 | |
| 4514 | return 0; |
| 4515 | } |
| 4516 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4517 | static void r8153_aldps_en(struct r8152 *tp, bool enable) |
| 4518 | { |
| 4519 | u16 data; |
| 4520 | |
| 4521 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4522 | if (enable) { |
| 4523 | data |= EN_ALDPS; |
| 4524 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 4525 | } else { |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 4526 | int i; |
| 4527 | |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4528 | data &= ~EN_ALDPS; |
| 4529 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | 4214cc5 | 2017-06-09 17:11:46 +0800 | [diff] [blame] | 4530 | for (i = 0; i < 20; i++) { |
| 4531 | usleep_range(1000, 2000); |
| 4532 | if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100) |
| 4533 | break; |
| 4534 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4535 | } |
hayeswang | e644953 | 2016-09-20 16:22:05 +0800 | [diff] [blame] | 4536 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4537 | tp->ups_info.aldps = enable; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4538 | } |
| 4539 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4540 | static void r8153_hw_phy_cfg(struct r8152 *tp) |
| 4541 | { |
| 4542 | u32 ocp_data; |
| 4543 | u16 data; |
| 4544 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 4545 | /* disable ALDPS before updating the PHY parameters */ |
| 4546 | r8153_aldps_en(tp, false); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 4547 | |
hayeswang | d768c61 | 2016-09-20 16:22:09 +0800 | [diff] [blame] | 4548 | /* disable EEE before updating the PHY parameters */ |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4549 | rtl_eee_enable(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4550 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4551 | rtl8152_apply_firmware(tp); |
| 4552 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4553 | if (tp->version == RTL_VER_03) { |
| 4554 | data = ocp_reg_read(tp, OCP_EEE_CFG); |
| 4555 | data &= ~CTAP_SHORT_EN; |
| 4556 | ocp_reg_write(tp, OCP_EEE_CFG, data); |
| 4557 | } |
| 4558 | |
| 4559 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4560 | data |= EEE_CLKDIV_EN; |
| 4561 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
| 4562 | |
| 4563 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 4564 | data |= EN_10M_BGOFF; |
| 4565 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
| 4566 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4567 | data |= EN_10M_PLLOFF; |
| 4568 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 4569 | sram_write(tp, SRAM_IMPEDANCE, 0x0b13); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4570 | |
| 4571 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 4572 | ocp_data |= PFM_PWM_SWITCH; |
| 4573 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 4574 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 4575 | /* Enable LPF corner auto tune */ |
| 4576 | sram_write(tp, SRAM_LPF_CFG, 0xf70f); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4577 | |
hayeswang | b4d99de | 2015-01-19 17:02:46 +0800 | [diff] [blame] | 4578 | /* Adjust 10M Amplitude */ |
| 4579 | sram_write(tp, SRAM_10M_AMP1, 0x00af); |
| 4580 | sram_write(tp, SRAM_10M_AMP2, 0x0208); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4581 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4582 | if (tp->eee_en) |
| 4583 | rtl_eee_enable(tp, true); |
hayeswang | af0287e | 2016-09-20 16:22:08 +0800 | [diff] [blame] | 4584 | |
hayeswang | ef39df8 | 2016-09-20 16:22:07 +0800 | [diff] [blame] | 4585 | r8153_aldps_en(tp, true); |
| 4586 | r8152b_enable_fc(tp); |
| 4587 | |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 4588 | switch (tp->version) { |
| 4589 | case RTL_VER_03: |
| 4590 | case RTL_VER_04: |
| 4591 | break; |
| 4592 | case RTL_VER_05: |
| 4593 | case RTL_VER_06: |
| 4594 | default: |
| 4595 | r8153_u2p3en(tp, true); |
| 4596 | break; |
| 4597 | } |
| 4598 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4599 | set_bit(PHY_RESET, &tp->flags); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4600 | } |
| 4601 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4602 | static u32 r8152_efuse_read(struct r8152 *tp, u8 addr) |
| 4603 | { |
| 4604 | u32 ocp_data; |
| 4605 | |
| 4606 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr); |
| 4607 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD); |
| 4608 | ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */ |
| 4609 | ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA); |
| 4610 | |
| 4611 | return ocp_data; |
| 4612 | } |
| 4613 | |
| 4614 | static void r8153b_hw_phy_cfg(struct r8152 *tp) |
| 4615 | { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4616 | u32 ocp_data; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4617 | u16 data; |
| 4618 | |
| 4619 | /* disable ALDPS before updating the PHY parameters */ |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4620 | r8153_aldps_en(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4621 | |
| 4622 | /* disable EEE before updating the PHY parameters */ |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4623 | rtl_eee_enable(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4624 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 4625 | rtl8152_apply_firmware(tp); |
| 4626 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4627 | r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags)); |
| 4628 | |
| 4629 | data = sram_read(tp, SRAM_GREEN_CFG); |
| 4630 | data |= R_TUNE_EN; |
| 4631 | sram_write(tp, SRAM_GREEN_CFG, data); |
| 4632 | data = ocp_reg_read(tp, OCP_NCTL_CFG); |
| 4633 | data |= PGA_RETURN_EN; |
| 4634 | ocp_reg_write(tp, OCP_NCTL_CFG, data); |
| 4635 | |
| 4636 | /* ADC Bias Calibration: |
| 4637 | * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake |
| 4638 | * bit (bit3) to rebuild the real 16-bit data. Write the data to the |
| 4639 | * ADC ioffset. |
| 4640 | */ |
| 4641 | ocp_data = r8152_efuse_read(tp, 0x7d); |
| 4642 | data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7)); |
| 4643 | if (data != 0xffff) |
| 4644 | ocp_reg_write(tp, OCP_ADC_IOFFSET, data); |
| 4645 | |
| 4646 | /* ups mode tx-link-pulse timing adjustment: |
| 4647 | * rg_saw_cnt = OCP reg 0xC426 Bit[13:0] |
| 4648 | * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt |
| 4649 | */ |
| 4650 | ocp_data = ocp_reg_read(tp, 0xc426); |
| 4651 | ocp_data &= 0x3fff; |
| 4652 | if (ocp_data) { |
| 4653 | u32 swr_cnt_1ms_ini; |
| 4654 | |
| 4655 | swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK; |
| 4656 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG); |
| 4657 | ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini; |
| 4658 | ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data); |
| 4659 | } |
| 4660 | |
| 4661 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 4662 | ocp_data |= PFM_PWM_SWITCH; |
| 4663 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 4664 | |
| 4665 | /* Advnace EEE */ |
| 4666 | if (!r8153_patch_request(tp, true)) { |
| 4667 | data = ocp_reg_read(tp, OCP_POWER_CFG); |
| 4668 | data |= EEE_CLKDIV_EN; |
| 4669 | ocp_reg_write(tp, OCP_POWER_CFG, data); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4670 | tp->ups_info.eee_ckdiv = true; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4671 | |
| 4672 | data = ocp_reg_read(tp, OCP_DOWN_SPEED); |
| 4673 | data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV; |
| 4674 | ocp_reg_write(tp, OCP_DOWN_SPEED, data); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4675 | tp->ups_info.eee_cmod_lv = true; |
| 4676 | tp->ups_info._10m_ckdiv = true; |
| 4677 | tp->ups_info.eee_plloff_giga = true; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4678 | |
| 4679 | ocp_reg_write(tp, OCP_SYSCLK_CFG, 0); |
| 4680 | ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5)); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4681 | tp->ups_info._250m_ckdiv = true; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4682 | |
| 4683 | r8153_patch_request(tp, false); |
| 4684 | } |
| 4685 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 4686 | if (tp->eee_en) |
| 4687 | rtl_eee_enable(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4688 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4689 | r8153_aldps_en(tp, true); |
| 4690 | r8152b_enable_fc(tp); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4691 | r8153_u2p3en(tp, true); |
| 4692 | |
| 4693 | set_bit(PHY_RESET, &tp->flags); |
| 4694 | } |
| 4695 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4696 | static void r8153_first_init(struct r8152 *tp) |
| 4697 | { |
| 4698 | u32 ocp_data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4699 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 4700 | r8153_mac_clk_spd(tp, false); |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4701 | rxdy_gated_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4702 | r8153_teredo_off(tp); |
| 4703 | |
| 4704 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4705 | ocp_data &= ~RCR_ACPT_ALL; |
| 4706 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4707 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4708 | rtl8152_nic_reset(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 4709 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4710 | |
| 4711 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4712 | ocp_data &= ~NOW_IS_OOB; |
| 4713 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4714 | |
| 4715 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4716 | ocp_data &= ~MCU_BORW_EN; |
| 4717 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4718 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4719 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4720 | |
| 4721 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4722 | ocp_data |= RE_INIT_LL; |
| 4723 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4724 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4725 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4726 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4727 | 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] | 4728 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 4729 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 4730 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 4731 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4732 | |
| 4733 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0); |
| 4734 | ocp_data |= TCR0_AUTO_FIFO; |
| 4735 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data); |
| 4736 | |
| 4737 | rtl8152_nic_reset(tp); |
| 4738 | |
| 4739 | /* rx share fifo credit full threshold */ |
| 4740 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL); |
| 4741 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL); |
| 4742 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL); |
| 4743 | /* TX share fifo free credit full threshold */ |
| 4744 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | static void r8153_enter_oob(struct r8152 *tp) |
| 4748 | { |
| 4749 | u32 ocp_data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4750 | |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 4751 | r8153_mac_clk_spd(tp, true); |
| 4752 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4753 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4754 | ocp_data &= ~NOW_IS_OOB; |
| 4755 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4756 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4757 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 4758 | rtl_reset_bmu(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4759 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4760 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4761 | |
| 4762 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7); |
| 4763 | ocp_data |= RE_INIT_LL; |
| 4764 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data); |
| 4765 | |
Prashant Malani | 5f71c84 | 2019-10-01 01:35:57 -0700 | [diff] [blame] | 4766 | wait_oob_link_list_ready(tp); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4767 | |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 4768 | ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 4769 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4770 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4771 | switch (tp->version) { |
| 4772 | case RTL_VER_03: |
| 4773 | case RTL_VER_04: |
| 4774 | case RTL_VER_05: |
| 4775 | case RTL_VER_06: |
| 4776 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG); |
| 4777 | ocp_data &= ~TEREDO_WAKE_MASK; |
| 4778 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data); |
| 4779 | break; |
| 4780 | |
| 4781 | case RTL_VER_08: |
| 4782 | case RTL_VER_09: |
| 4783 | /* Clear teredo wake event. bit[15:8] is the teredo wakeup |
| 4784 | * type. Set it to zero. bits[7:0] are the W1C bits about |
| 4785 | * the events. Set them to all 1 to clear them. |
| 4786 | */ |
| 4787 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff); |
| 4788 | break; |
| 4789 | |
| 4790 | default: |
| 4791 | break; |
| 4792 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4793 | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 4794 | rtl_rx_vlan_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4795 | |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4796 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4797 | ocp_data |= ALDPS_PROXY_MODE; |
Kevin Lo | 59c0b47 | 2019-08-01 11:29:38 +0800 | [diff] [blame] | 4798 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4799 | |
| 4800 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL); |
| 4801 | ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB; |
| 4802 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data); |
| 4803 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4804 | rxdy_gated_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4805 | |
| 4806 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 4807 | ocp_data |= RCR_APM | RCR_AM | RCR_AB; |
| 4808 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 4809 | } |
| 4810 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4811 | static void rtl8153_disable(struct r8152 *tp) |
| 4812 | { |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4813 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4814 | rtl_disable(tp); |
hayeswang | 93fe9b1 | 2016-06-16 10:55:18 +0800 | [diff] [blame] | 4815 | rtl_reset_bmu(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4816 | r8153_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4817 | } |
| 4818 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4819 | static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex, |
| 4820 | u32 advertising) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4821 | { |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4822 | u16 bmcr; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4823 | int ret = 0; |
| 4824 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4825 | if (autoneg == AUTONEG_DISABLE) { |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4826 | if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL) |
| 4827 | return -EINVAL; |
| 4828 | |
| 4829 | switch (speed) { |
| 4830 | case SPEED_10: |
| 4831 | bmcr = BMCR_SPEED10; |
| 4832 | if (duplex == DUPLEX_FULL) { |
| 4833 | bmcr |= BMCR_FULLDPLX; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4834 | tp->ups_info.speed_duplex = FORCE_10M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4835 | } else { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4836 | tp->ups_info.speed_duplex = FORCE_10M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4837 | } |
| 4838 | break; |
| 4839 | case SPEED_100: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4840 | bmcr = BMCR_SPEED100; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4841 | if (duplex == DUPLEX_FULL) { |
| 4842 | bmcr |= BMCR_FULLDPLX; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4843 | tp->ups_info.speed_duplex = FORCE_100M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4844 | } else { |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4845 | tp->ups_info.speed_duplex = FORCE_100M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4846 | } |
| 4847 | break; |
| 4848 | case SPEED_1000: |
| 4849 | if (tp->mii.supports_gmii) { |
| 4850 | bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4851 | tp->ups_info.speed_duplex = NWAY_1000M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4852 | break; |
| 4853 | } |
| 4854 | /* fall through */ |
| 4855 | default: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4856 | ret = -EINVAL; |
| 4857 | goto out; |
| 4858 | } |
| 4859 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4860 | if (duplex == DUPLEX_FULL) |
| 4861 | tp->mii.full_duplex = 1; |
| 4862 | else |
| 4863 | tp->mii.full_duplex = 0; |
| 4864 | |
| 4865 | tp->mii.force_media = 1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4866 | } else { |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4867 | u16 anar, tmp1; |
| 4868 | u32 support; |
| 4869 | |
| 4870 | support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | |
| 4871 | RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; |
| 4872 | |
| 4873 | if (tp->mii.supports_gmii) |
| 4874 | support |= RTL_ADVERTISED_1000_FULL; |
| 4875 | |
| 4876 | if (!(advertising & support)) |
| 4877 | return -EINVAL; |
| 4878 | |
| 4879 | anar = r8152_mdio_read(tp, MII_ADVERTISE); |
| 4880 | tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL | |
| 4881 | ADVERTISE_100HALF | ADVERTISE_100FULL); |
| 4882 | if (advertising & RTL_ADVERTISED_10_HALF) { |
| 4883 | tmp1 |= ADVERTISE_10HALF; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4884 | tp->ups_info.speed_duplex = NWAY_10M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4885 | } |
| 4886 | if (advertising & RTL_ADVERTISED_10_FULL) { |
| 4887 | tmp1 |= ADVERTISE_10FULL; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4888 | tp->ups_info.speed_duplex = NWAY_10M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | if (advertising & RTL_ADVERTISED_100_HALF) { |
| 4892 | tmp1 |= ADVERTISE_100HALF; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4893 | tp->ups_info.speed_duplex = NWAY_100M_HALF; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4894 | } |
| 4895 | if (advertising & RTL_ADVERTISED_100_FULL) { |
| 4896 | tmp1 |= ADVERTISE_100FULL; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4897 | tp->ups_info.speed_duplex = NWAY_100M_FULL; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | if (anar != tmp1) { |
| 4901 | r8152_mdio_write(tp, MII_ADVERTISE, tmp1); |
| 4902 | tp->mii.advertising = tmp1; |
| 4903 | } |
| 4904 | |
| 4905 | if (tp->mii.supports_gmii) { |
| 4906 | u16 gbcr; |
| 4907 | |
| 4908 | gbcr = r8152_mdio_read(tp, MII_CTRL1000); |
| 4909 | tmp1 = gbcr & ~(ADVERTISE_1000FULL | |
| 4910 | ADVERTISE_1000HALF); |
| 4911 | |
| 4912 | if (advertising & RTL_ADVERTISED_1000_FULL) { |
| 4913 | tmp1 |= ADVERTISE_1000FULL; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 4914 | tp->ups_info.speed_duplex = NWAY_1000M_FULL; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 4915 | } |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4916 | |
| 4917 | if (gbcr != tmp1) |
| 4918 | r8152_mdio_write(tp, MII_CTRL1000, tmp1); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | bmcr = BMCR_ANENABLE | BMCR_ANRESTART; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 4922 | |
| 4923 | tp->mii.force_media = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 4926 | if (test_and_clear_bit(PHY_RESET, &tp->flags)) |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4927 | bmcr |= BMCR_RESET; |
| 4928 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4929 | r8152_mdio_write(tp, MII_BMCR, bmcr); |
| 4930 | |
hayeswang | fae5617 | 2016-06-16 14:08:29 +0800 | [diff] [blame] | 4931 | if (bmcr & BMCR_RESET) { |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4932 | int i; |
| 4933 | |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 4934 | for (i = 0; i < 50; i++) { |
| 4935 | msleep(20); |
| 4936 | if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0) |
| 4937 | break; |
| 4938 | } |
| 4939 | } |
| 4940 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4941 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4942 | return ret; |
| 4943 | } |
| 4944 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4945 | static void rtl8152_up(struct r8152 *tp) |
| 4946 | { |
| 4947 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4948 | return; |
| 4949 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4950 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4951 | r8152b_exit_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4952 | r8152_aldps_en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4953 | } |
| 4954 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4955 | static void rtl8152_down(struct r8152 *tp) |
| 4956 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4957 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 4958 | rtl_drop_queued_tx(tp); |
| 4959 | return; |
| 4960 | } |
| 4961 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 4962 | r8152_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4963 | r8152_aldps_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4964 | r8152b_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4965 | r8152_aldps_en(tp, true); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4968 | static void rtl8153_up(struct r8152 *tp) |
| 4969 | { |
| 4970 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 4971 | return; |
| 4972 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 4973 | r8153_u1u2en(tp, false); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 4974 | r8153_u2p3en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4975 | r8153_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4976 | r8153_first_init(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 4977 | r8153_aldps_en(tp, true); |
hayeswang | 3cb3234 | 2017-06-09 17:11:43 +0800 | [diff] [blame] | 4978 | |
| 4979 | switch (tp->version) { |
| 4980 | case RTL_VER_03: |
| 4981 | case RTL_VER_04: |
| 4982 | break; |
| 4983 | case RTL_VER_05: |
| 4984 | case RTL_VER_06: |
| 4985 | default: |
| 4986 | r8153_u2p3en(tp, true); |
| 4987 | break; |
| 4988 | } |
| 4989 | |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 4990 | r8153_u1u2en(tp, true); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 4991 | } |
| 4992 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 4993 | static void rtl8153_down(struct r8152 *tp) |
| 4994 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 4995 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 4996 | rtl_drop_queued_tx(tp); |
| 4997 | return; |
| 4998 | } |
| 4999 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5000 | r8153_u1u2en(tp, false); |
hayeswang | b214396 | 2015-07-24 13:54:23 +0800 | [diff] [blame] | 5001 | r8153_u2p3en(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5002 | r8153_power_cut_en(tp, false); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 5003 | r8153_aldps_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5004 | r8153_enter_oob(tp); |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 5005 | r8153_aldps_en(tp, true); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5006 | } |
| 5007 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5008 | static void rtl8153b_up(struct r8152 *tp) |
| 5009 | { |
| 5010 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5011 | return; |
| 5012 | |
| 5013 | r8153b_u1u2en(tp, false); |
| 5014 | r8153_u2p3en(tp, false); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5015 | r8153_aldps_en(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5016 | |
| 5017 | r8153_first_init(tp); |
| 5018 | ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B); |
| 5019 | |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5020 | r8153_aldps_en(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5021 | r8153_u2p3en(tp, true); |
| 5022 | r8153b_u1u2en(tp, true); |
| 5023 | } |
| 5024 | |
| 5025 | static void rtl8153b_down(struct r8152 *tp) |
| 5026 | { |
| 5027 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) { |
| 5028 | rtl_drop_queued_tx(tp); |
| 5029 | return; |
| 5030 | } |
| 5031 | |
| 5032 | r8153b_u1u2en(tp, false); |
| 5033 | r8153_u2p3en(tp, false); |
| 5034 | r8153b_power_cut_en(tp, false); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5035 | r8153_aldps_en(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5036 | r8153_enter_oob(tp); |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 5037 | r8153_aldps_en(tp, true); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5038 | } |
| 5039 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5040 | static bool rtl8152_in_nway(struct r8152 *tp) |
| 5041 | { |
| 5042 | u16 nway_state; |
| 5043 | |
| 5044 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000); |
| 5045 | tp->ocp_base = 0x2000; |
| 5046 | ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */ |
| 5047 | nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a); |
| 5048 | |
| 5049 | /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */ |
| 5050 | if (nway_state & 0xc000) |
| 5051 | return false; |
| 5052 | else |
| 5053 | return true; |
| 5054 | } |
| 5055 | |
| 5056 | static bool rtl8153_in_nway(struct r8152 *tp) |
| 5057 | { |
| 5058 | u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff; |
| 5059 | |
| 5060 | if (phy_state == TXDIS_STATE || phy_state == ABD_STATE) |
| 5061 | return false; |
| 5062 | else |
| 5063 | return true; |
| 5064 | } |
| 5065 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5066 | static void set_carrier(struct r8152 *tp) |
| 5067 | { |
| 5068 | struct net_device *netdev = tp->netdev; |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5069 | struct napi_struct *napi = &tp->napi; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5070 | u8 speed; |
| 5071 | |
| 5072 | speed = rtl8152_get_speed(tp); |
| 5073 | |
| 5074 | if (speed & LINK_STATUS) { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 5075 | if (!netif_carrier_ok(netdev)) { |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5076 | tp->rtl_ops.enable(tp); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5077 | netif_stop_queue(netdev); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5078 | napi_disable(napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5079 | netif_carrier_on(netdev); |
hayeswang | aa2e092 | 2015-01-09 10:26:35 +0800 | [diff] [blame] | 5080 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 5081 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 5082 | _rtl8152_set_rx_mode(netdev); |
hayeswang | 41cec84 | 2015-07-24 13:54:25 +0800 | [diff] [blame] | 5083 | napi_enable(&tp->napi); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5084 | netif_wake_queue(netdev); |
| 5085 | netif_info(tp, link, netdev, "carrier on\n"); |
hayeswang | 2f25abe | 2017-03-23 19:14:19 +0800 | [diff] [blame] | 5086 | } else if (netif_queue_stopped(netdev) && |
| 5087 | skb_queue_len(&tp->tx_queue) < tp->tx_qlen) { |
| 5088 | netif_wake_queue(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5089 | } |
| 5090 | } else { |
hayeswang | 51d979f | 2015-02-06 11:30:47 +0800 | [diff] [blame] | 5091 | if (netif_carrier_ok(netdev)) { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5092 | netif_carrier_off(netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5093 | tasklet_disable(&tp->tx_tl); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5094 | napi_disable(napi); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 5095 | tp->rtl_ops.disable(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5096 | napi_enable(napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5097 | tasklet_enable(&tp->tx_tl); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5098 | netif_info(tp, link, netdev, "carrier off\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5099 | } |
| 5100 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | static void rtl_work_func_t(struct work_struct *work) |
| 5104 | { |
| 5105 | struct r8152 *tp = container_of(work, struct r8152, schedule.work); |
| 5106 | |
hayeswang | a1f83fe | 2014-11-12 10:05:05 +0800 | [diff] [blame] | 5107 | /* If the device is unplugged or !netif_running(), the workqueue |
| 5108 | * doesn't need to wake the device, and could return directly. |
| 5109 | */ |
| 5110 | if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev)) |
| 5111 | return; |
| 5112 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5113 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5114 | return; |
| 5115 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5116 | if (!test_bit(WORK_ENABLE, &tp->flags)) |
| 5117 | goto out1; |
| 5118 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5119 | if (!mutex_trylock(&tp->control)) { |
| 5120 | schedule_delayed_work(&tp->schedule, 0); |
| 5121 | goto out1; |
| 5122 | } |
| 5123 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 5124 | if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags)) |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 5125 | set_carrier(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5126 | |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 5127 | if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags)) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5128 | _rtl8152_set_rx_mode(tp->netdev); |
| 5129 | |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5130 | /* don't schedule tasket before linking */ |
| 5131 | if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) && |
hayeswang | 216a834 | 2016-01-07 17:51:11 +0800 | [diff] [blame] | 5132 | netif_carrier_ok(tp->netdev)) |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5133 | tasklet_schedule(&tp->tx_tl); |
hayeswang | aa66a5f | 2014-02-18 21:49:04 +0800 | [diff] [blame] | 5134 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5135 | mutex_unlock(&tp->control); |
| 5136 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5137 | out1: |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5138 | usb_autopm_put_interface(tp->intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5141 | static void rtl_hw_phy_work_func_t(struct work_struct *work) |
| 5142 | { |
| 5143 | struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work); |
| 5144 | |
| 5145 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5146 | return; |
| 5147 | |
| 5148 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5149 | return; |
| 5150 | |
| 5151 | mutex_lock(&tp->control); |
| 5152 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5153 | if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) { |
| 5154 | tp->rtl_fw.retry = false; |
| 5155 | tp->rtl_fw.fw = NULL; |
| 5156 | |
| 5157 | /* Delay execution in case request_firmware() is not ready yet. |
| 5158 | */ |
| 5159 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10); |
| 5160 | goto ignore_once; |
| 5161 | } |
| 5162 | |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5163 | tp->rtl_ops.hw_phy_cfg(tp); |
| 5164 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5165 | rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex, |
| 5166 | tp->advertising); |
hayeswang | 9d21c0d | 2016-06-13 17:49:37 +0800 | [diff] [blame] | 5167 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5168 | ignore_once: |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 5169 | mutex_unlock(&tp->control); |
| 5170 | |
| 5171 | usb_autopm_put_interface(tp->intf); |
| 5172 | } |
| 5173 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 5174 | #ifdef CONFIG_PM_SLEEP |
| 5175 | static int rtl_notifier(struct notifier_block *nb, unsigned long action, |
| 5176 | void *data) |
| 5177 | { |
| 5178 | struct r8152 *tp = container_of(nb, struct r8152, pm_notifier); |
| 5179 | |
| 5180 | switch (action) { |
| 5181 | case PM_HIBERNATION_PREPARE: |
| 5182 | case PM_SUSPEND_PREPARE: |
| 5183 | usb_autopm_get_interface(tp->intf); |
| 5184 | break; |
| 5185 | |
| 5186 | case PM_POST_HIBERNATION: |
| 5187 | case PM_POST_SUSPEND: |
| 5188 | usb_autopm_put_interface(tp->intf); |
| 5189 | break; |
| 5190 | |
| 5191 | case PM_POST_RESTORE: |
| 5192 | case PM_RESTORE_PREPARE: |
| 5193 | default: |
| 5194 | break; |
| 5195 | } |
| 5196 | |
| 5197 | return NOTIFY_DONE; |
| 5198 | } |
| 5199 | #endif |
| 5200 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5201 | static int rtl8152_open(struct net_device *netdev) |
| 5202 | { |
| 5203 | struct r8152 *tp = netdev_priv(netdev); |
| 5204 | int res = 0; |
| 5205 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5206 | if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) { |
| 5207 | cancel_delayed_work_sync(&tp->hw_phy_work); |
| 5208 | rtl_hw_phy_work_func_t(&tp->hw_phy_work.work); |
| 5209 | } |
| 5210 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5211 | res = alloc_all_mem(tp); |
| 5212 | if (res) |
| 5213 | goto out; |
| 5214 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5215 | res = usb_autopm_get_interface(tp->intf); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5216 | if (res < 0) |
| 5217 | goto out_free; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5218 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5219 | mutex_lock(&tp->control); |
| 5220 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5221 | tp->rtl_ops.up(tp); |
| 5222 | |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 5223 | netif_carrier_off(netdev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5224 | netif_start_queue(netdev); |
| 5225 | set_bit(WORK_ENABLE, &tp->flags); |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 5226 | |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5227 | res = usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
| 5228 | if (res) { |
| 5229 | if (res == -ENODEV) |
| 5230 | netif_device_detach(tp->netdev); |
| 5231 | netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n", |
| 5232 | res); |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5233 | goto out_unlock; |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5234 | } |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5235 | napi_enable(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5236 | tasklet_enable(&tp->tx_tl); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5237 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5238 | mutex_unlock(&tp->control); |
| 5239 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5240 | usb_autopm_put_interface(tp->intf); |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 5241 | #ifdef CONFIG_PM_SLEEP |
| 5242 | tp->pm_notifier.notifier_call = rtl_notifier; |
| 5243 | register_pm_notifier(&tp->pm_notifier); |
| 5244 | #endif |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5245 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5246 | |
Guenter Roeck | ca0a753 | 2016-11-09 19:51:25 -0800 | [diff] [blame] | 5247 | out_unlock: |
| 5248 | mutex_unlock(&tp->control); |
| 5249 | usb_autopm_put_interface(tp->intf); |
| 5250 | out_free: |
| 5251 | free_all_mem(tp); |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5252 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5253 | return res; |
| 5254 | } |
| 5255 | |
| 5256 | static int rtl8152_close(struct net_device *netdev) |
| 5257 | { |
| 5258 | struct r8152 *tp = netdev_priv(netdev); |
| 5259 | int res = 0; |
| 5260 | |
hayeswang | 5ee3c60 | 2016-01-07 17:12:17 +0800 | [diff] [blame] | 5261 | #ifdef CONFIG_PM_SLEEP |
| 5262 | unregister_pm_notifier(&tp->pm_notifier); |
| 5263 | #endif |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5264 | tasklet_disable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5265 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 3d55f44 | 2014-02-06 11:55:48 +0800 | [diff] [blame] | 5266 | usb_kill_urb(tp->intr_urb); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5267 | cancel_delayed_work_sync(&tp->schedule); |
Prashant Malani | 8481141 | 2019-11-20 11:40:21 -0800 | [diff] [blame] | 5268 | napi_disable(&tp->napi); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5269 | netif_stop_queue(netdev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5270 | |
| 5271 | res = usb_autopm_get_interface(tp->intf); |
hayeswang | 53543db | 2015-02-06 11:30:48 +0800 | [diff] [blame] | 5272 | if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) { |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5273 | rtl_drop_queued_tx(tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 5274 | rtl_stop_rx(tp); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5275 | } else { |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5276 | mutex_lock(&tp->control); |
| 5277 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5278 | tp->rtl_ops.down(tp); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5279 | |
| 5280 | mutex_unlock(&tp->control); |
| 5281 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5282 | usb_autopm_put_interface(tp->intf); |
| 5283 | } |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5284 | |
hayeswang | 7e9da48 | 2014-02-18 21:49:05 +0800 | [diff] [blame] | 5285 | free_all_mem(tp); |
| 5286 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5287 | return res; |
| 5288 | } |
| 5289 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5290 | static void rtl_tally_reset(struct r8152 *tp) |
| 5291 | { |
| 5292 | u32 ocp_data; |
| 5293 | |
| 5294 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY); |
| 5295 | ocp_data |= TALLY_RESET; |
| 5296 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data); |
| 5297 | } |
| 5298 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5299 | static void r8152b_init(struct r8152 *tp) |
| 5300 | { |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5301 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5302 | u16 data; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5303 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5304 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5305 | return; |
| 5306 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5307 | data = r8152_mdio_read(tp, MII_BMCR); |
| 5308 | if (data & BMCR_PDOWN) { |
| 5309 | data &= ~BMCR_PDOWN; |
| 5310 | r8152_mdio_write(tp, MII_BMCR, data); |
| 5311 | } |
| 5312 | |
hayeswang | cda9fb0 | 2016-01-07 17:51:12 +0800 | [diff] [blame] | 5313 | r8152_aldps_en(tp, false); |
hayeswang | d70b113 | 2014-09-19 15:17:18 +0800 | [diff] [blame] | 5314 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5315 | if (tp->version == RTL_VER_01) { |
| 5316 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 5317 | ocp_data &= ~LED_MODE_MASK; |
| 5318 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 5319 | } |
| 5320 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 5321 | r8152_power_cut_en(tp, false); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5322 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5323 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR); |
| 5324 | ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH; |
| 5325 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data); |
| 5326 | ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL); |
| 5327 | ocp_data &= ~MCU_CLK_RATIO_MASK; |
| 5328 | ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN; |
| 5329 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data); |
| 5330 | ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK | |
| 5331 | SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK; |
| 5332 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data); |
| 5333 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5334 | rtl_tally_reset(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5335 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 5336 | /* enable rx aggregation */ |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5337 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
hayeswang | e90fba8 | 2015-07-31 11:23:39 +0800 | [diff] [blame] | 5338 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5339 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 5340 | } |
| 5341 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5342 | static void r8153_init(struct r8152 *tp) |
| 5343 | { |
| 5344 | u32 ocp_data; |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5345 | u16 data; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5346 | int i; |
| 5347 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 5348 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5349 | return; |
| 5350 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5351 | r8153_u1u2en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5352 | |
| 5353 | for (i = 0; i < 500; i++) { |
| 5354 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 5355 | AUTOLOAD_DONE) |
| 5356 | break; |
| 5357 | msleep(20); |
| 5358 | } |
| 5359 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 5360 | data = r8153_phy_status(tp, 0); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5361 | |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5362 | if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 || |
| 5363 | tp->version == RTL_VER_05) |
| 5364 | ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L); |
| 5365 | |
| 5366 | data = r8152_mdio_read(tp, MII_BMCR); |
| 5367 | if (data & BMCR_PDOWN) { |
| 5368 | data &= ~BMCR_PDOWN; |
| 5369 | r8152_mdio_write(tp, MII_BMCR, data); |
| 5370 | } |
| 5371 | |
hayeswang | c564b87 | 2017-06-09 17:11:38 +0800 | [diff] [blame] | 5372 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
hayeswang | 2dd436d | 2016-09-20 16:22:06 +0800 | [diff] [blame] | 5373 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5374 | r8153_u2p3en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5375 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5376 | if (tp->version == RTL_VER_04) { |
| 5377 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2); |
| 5378 | ocp_data &= ~pwd_dn_scale_mask; |
| 5379 | ocp_data |= pwd_dn_scale(96); |
| 5380 | ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data); |
| 5381 | |
| 5382 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY); |
| 5383 | ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND; |
| 5384 | ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data); |
| 5385 | } else if (tp->version == RTL_VER_05) { |
| 5386 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0); |
| 5387 | ocp_data &= ~ECM_ALDPS; |
| 5388 | ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data); |
| 5389 | |
| 5390 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 5391 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 5392 | ocp_data &= ~DYNAMIC_BURST; |
| 5393 | else |
| 5394 | ocp_data |= DYNAMIC_BURST; |
| 5395 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 5396 | } else if (tp->version == RTL_VER_06) { |
| 5397 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1); |
| 5398 | if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0) |
| 5399 | ocp_data &= ~DYNAMIC_BURST; |
| 5400 | else |
| 5401 | ocp_data |= DYNAMIC_BURST; |
| 5402 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data); |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame] | 5403 | |
| 5404 | r8153_queue_wake(tp, false); |
| 5405 | |
| 5406 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 5407 | if (rtl8152_get_speed(tp) & LINK_STATUS) |
| 5408 | ocp_data |= CUR_LINK_OK; |
| 5409 | else |
| 5410 | ocp_data &= ~CUR_LINK_OK; |
| 5411 | ocp_data |= POLL_LINK_CHG; |
| 5412 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2); |
| 5416 | ocp_data |= EP4_FULL_FC; |
| 5417 | ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data); |
| 5418 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5419 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL); |
| 5420 | ocp_data &= ~TIMER11_EN; |
| 5421 | ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data); |
| 5422 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5423 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE); |
| 5424 | ocp_data &= ~LED_MODE_MASK; |
| 5425 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data); |
| 5426 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5427 | ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM; |
Oliver Neukum | 2b84af94a | 2016-05-02 13:06:14 +0200 | [diff] [blame] | 5428 | if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER) |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5429 | ocp_data |= LPM_TIMER_500MS; |
hayeswang | 34203e2 | 2015-02-06 11:30:46 +0800 | [diff] [blame] | 5430 | else |
| 5431 | ocp_data |= LPM_TIMER_500US; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5432 | ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data); |
| 5433 | |
| 5434 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2); |
| 5435 | ocp_data &= ~SEN_VAL_MASK; |
| 5436 | ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE; |
| 5437 | ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data); |
| 5438 | |
hayeswang | 65bab84 | 2015-02-12 16:20:46 +0800 | [diff] [blame] | 5439 | ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001); |
| 5440 | |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5441 | r8153_power_cut_en(tp, false); |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame] | 5442 | rtl_runtime_suspend_enable(tp, false); |
hayeswang | b970272 | 2014-02-18 21:49:00 +0800 | [diff] [blame] | 5443 | r8153_u1u2en(tp, true); |
hayeswang | 134f98b | 2017-06-09 17:11:40 +0800 | [diff] [blame] | 5444 | r8153_mac_clk_spd(tp, false); |
hayeswang | ee4761c | 2017-06-09 17:11:39 +0800 | [diff] [blame] | 5445 | usb_enable_lpm(tp->udev); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5446 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 5447 | /* rx aggregation */ |
| 5448 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 5449 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 5450 | if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags)) |
| 5451 | ocp_data |= RX_AGG_DISABLE; |
| 5452 | |
hayeswang | e31f636 | 2017-06-09 17:11:41 +0800 | [diff] [blame] | 5453 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5454 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5455 | rtl_tally_reset(tp); |
hayeswang | 49d1034 | 2017-06-09 17:11:44 +0800 | [diff] [blame] | 5456 | |
| 5457 | switch (tp->udev->speed) { |
| 5458 | case USB_SPEED_SUPER: |
| 5459 | case USB_SPEED_SUPER_PLUS: |
| 5460 | tp->coalesce = COALESCE_SUPER; |
| 5461 | break; |
| 5462 | case USB_SPEED_HIGH: |
| 5463 | tp->coalesce = COALESCE_HIGH; |
| 5464 | break; |
| 5465 | default: |
| 5466 | tp->coalesce = COALESCE_SLOW; |
| 5467 | break; |
| 5468 | } |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 5469 | } |
| 5470 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5471 | static void r8153b_init(struct r8152 *tp) |
| 5472 | { |
| 5473 | u32 ocp_data; |
| 5474 | u16 data; |
| 5475 | int i; |
| 5476 | |
| 5477 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 5478 | return; |
| 5479 | |
| 5480 | r8153b_u1u2en(tp, false); |
| 5481 | |
| 5482 | for (i = 0; i < 500; i++) { |
| 5483 | if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) & |
| 5484 | AUTOLOAD_DONE) |
| 5485 | break; |
| 5486 | msleep(20); |
| 5487 | } |
| 5488 | |
| 5489 | data = r8153_phy_status(tp, 0); |
| 5490 | |
| 5491 | data = r8152_mdio_read(tp, MII_BMCR); |
| 5492 | if (data & BMCR_PDOWN) { |
| 5493 | data &= ~BMCR_PDOWN; |
| 5494 | r8152_mdio_write(tp, MII_BMCR, data); |
| 5495 | } |
| 5496 | |
| 5497 | data = r8153_phy_status(tp, PHY_STAT_LAN_ON); |
| 5498 | |
| 5499 | r8153_u2p3en(tp, false); |
| 5500 | |
| 5501 | /* MSC timer = 0xfff * 8ms = 32760 ms */ |
| 5502 | ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff); |
| 5503 | |
| 5504 | /* U1/U2/L1 idle timer. 500 us */ |
| 5505 | ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500); |
| 5506 | |
| 5507 | r8153b_power_cut_en(tp, false); |
| 5508 | r8153b_ups_en(tp, false); |
Hayes Wang | 13e04fbf | 2019-07-01 15:53:19 +0800 | [diff] [blame] | 5509 | r8153_queue_wake(tp, false); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5510 | rtl_runtime_suspend_enable(tp, false); |
Hayes Wang | a391427 | 2020-01-22 16:02:05 +0800 | [diff] [blame] | 5511 | |
| 5512 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS); |
| 5513 | if (rtl8152_get_speed(tp) & LINK_STATUS) |
| 5514 | ocp_data |= CUR_LINK_OK; |
| 5515 | else |
| 5516 | ocp_data &= ~CUR_LINK_OK; |
| 5517 | ocp_data |= POLL_LINK_CHG; |
| 5518 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data); |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 5519 | r8153b_u1u2en(tp, true); |
| 5520 | usb_enable_lpm(tp->udev); |
| 5521 | |
| 5522 | /* MAC clock speed down */ |
| 5523 | ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2); |
| 5524 | ocp_data |= MAC_CLK_SPDWN_EN; |
| 5525 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data); |
| 5526 | |
| 5527 | set_bit(GREEN_ETHERNET, &tp->flags); |
| 5528 | |
| 5529 | /* rx aggregation */ |
| 5530 | ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL); |
| 5531 | ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN); |
| 5532 | ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data); |
| 5533 | |
| 5534 | rtl_tally_reset(tp); |
| 5535 | |
| 5536 | tp->coalesce = 15000; /* 15 us */ |
| 5537 | } |
| 5538 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5539 | static int rtl8152_pre_reset(struct usb_interface *intf) |
| 5540 | { |
| 5541 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5542 | struct net_device *netdev; |
| 5543 | |
| 5544 | if (!tp) |
| 5545 | return 0; |
| 5546 | |
| 5547 | netdev = tp->netdev; |
| 5548 | if (!netif_running(netdev)) |
| 5549 | return 0; |
| 5550 | |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5551 | netif_stop_queue(netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5552 | tasklet_disable(&tp->tx_tl); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5553 | clear_bit(WORK_ENABLE, &tp->flags); |
| 5554 | usb_kill_urb(tp->intr_urb); |
| 5555 | cancel_delayed_work_sync(&tp->schedule); |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 5556 | napi_disable(&tp->napi); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5557 | if (netif_carrier_ok(netdev)) { |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5558 | mutex_lock(&tp->control); |
| 5559 | tp->rtl_ops.disable(tp); |
| 5560 | mutex_unlock(&tp->control); |
| 5561 | } |
| 5562 | |
| 5563 | return 0; |
| 5564 | } |
| 5565 | |
| 5566 | static int rtl8152_post_reset(struct usb_interface *intf) |
| 5567 | { |
| 5568 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5569 | struct net_device *netdev; |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 5570 | struct sockaddr sa; |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5571 | |
| 5572 | if (!tp) |
| 5573 | return 0; |
| 5574 | |
Mario Limonciello | 2576627 | 2019-04-04 13:46:53 -0500 | [diff] [blame] | 5575 | /* reset the MAC adddress in case of policy change */ |
| 5576 | if (determine_ethernet_addr(tp, &sa) >= 0) { |
| 5577 | rtnl_lock(); |
| 5578 | dev_set_mac_address (tp->netdev, &sa, NULL); |
| 5579 | rtnl_unlock(); |
| 5580 | } |
| 5581 | |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5582 | netdev = tp->netdev; |
| 5583 | if (!netif_running(netdev)) |
| 5584 | return 0; |
| 5585 | |
| 5586 | set_bit(WORK_ENABLE, &tp->flags); |
| 5587 | if (netif_carrier_ok(netdev)) { |
| 5588 | mutex_lock(&tp->control); |
| 5589 | tp->rtl_ops.enable(tp); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 5590 | rtl_start_rx(tp); |
Hayes Wang | aece477 | 2018-02-02 16:43:36 +0800 | [diff] [blame] | 5591 | _rtl8152_set_rx_mode(netdev); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5592 | mutex_unlock(&tp->control); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | napi_enable(&tp->napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5596 | tasklet_enable(&tp->tx_tl); |
hayeswang | de9bf29 | 2017-01-26 09:38:32 +0800 | [diff] [blame] | 5597 | netif_wake_queue(netdev); |
hayeswang | 2c561b2 | 2017-01-20 14:33:55 +0800 | [diff] [blame] | 5598 | usb_submit_urb(tp->intr_urb, GFP_KERNEL); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5599 | |
hayeswang | 7489bda | 2017-01-26 09:38:34 +0800 | [diff] [blame] | 5600 | if (!list_empty(&tp->rx_done)) |
| 5601 | napi_schedule(&tp->napi); |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 5602 | |
| 5603 | return 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5606 | static bool delay_autosuspend(struct r8152 *tp) |
| 5607 | { |
| 5608 | bool sw_linking = !!netif_carrier_ok(tp->netdev); |
| 5609 | bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS); |
| 5610 | |
| 5611 | /* This means a linking change occurs and the driver doesn't detect it, |
| 5612 | * yet. If the driver has disabled tx/rx and hw is linking on, the |
| 5613 | * device wouldn't wake up by receiving any packet. |
| 5614 | */ |
| 5615 | if (work_busy(&tp->schedule.work) || sw_linking != hw_linking) |
| 5616 | return true; |
| 5617 | |
| 5618 | /* If the linking down is occurred by nway, the device may miss the |
| 5619 | * linking change event. And it wouldn't wake when linking on. |
| 5620 | */ |
| 5621 | if (!sw_linking && tp->rtl_ops.in_nway(tp)) |
| 5622 | return true; |
hayeswang | 6a0b76c | 2017-01-23 14:18:43 +0800 | [diff] [blame] | 5623 | else if (!skb_queue_empty(&tp->tx_queue)) |
| 5624 | return true; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 5625 | else |
| 5626 | return false; |
| 5627 | } |
| 5628 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5629 | static int rtl8152_runtime_resume(struct r8152 *tp) |
| 5630 | { |
| 5631 | struct net_device *netdev = tp->netdev; |
| 5632 | |
| 5633 | if (netif_running(netdev) && netdev->flags & IFF_UP) { |
| 5634 | struct napi_struct *napi = &tp->napi; |
| 5635 | |
| 5636 | tp->rtl_ops.autosuspend_en(tp, false); |
| 5637 | napi_disable(napi); |
| 5638 | set_bit(WORK_ENABLE, &tp->flags); |
| 5639 | |
| 5640 | if (netif_carrier_ok(netdev)) { |
| 5641 | if (rtl8152_get_speed(tp) & LINK_STATUS) { |
| 5642 | rtl_start_rx(tp); |
| 5643 | } else { |
| 5644 | netif_carrier_off(netdev); |
| 5645 | tp->rtl_ops.disable(tp); |
| 5646 | netif_info(tp, link, netdev, "linking down\n"); |
| 5647 | } |
| 5648 | } |
| 5649 | |
| 5650 | napi_enable(napi); |
| 5651 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5652 | smp_mb__after_atomic(); |
| 5653 | |
| 5654 | if (!list_empty(&tp->rx_done)) |
| 5655 | napi_schedule(&tp->napi); |
| 5656 | |
| 5657 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 5658 | } else { |
| 5659 | if (netdev->flags & IFF_UP) |
| 5660 | tp->rtl_ops.autosuspend_en(tp, false); |
| 5661 | |
| 5662 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5663 | } |
| 5664 | |
| 5665 | return 0; |
| 5666 | } |
| 5667 | |
| 5668 | static int rtl8152_system_resume(struct r8152 *tp) |
| 5669 | { |
| 5670 | struct net_device *netdev = tp->netdev; |
| 5671 | |
| 5672 | netif_device_attach(netdev); |
| 5673 | |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 5674 | if (netif_running(netdev) && (netdev->flags & IFF_UP)) { |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5675 | tp->rtl_ops.up(tp); |
| 5676 | netif_carrier_off(netdev); |
| 5677 | set_bit(WORK_ENABLE, &tp->flags); |
| 5678 | usb_submit_urb(tp->intr_urb, GFP_NOIO); |
| 5679 | } |
| 5680 | |
| 5681 | return 0; |
| 5682 | } |
| 5683 | |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 5684 | static int rtl8152_runtime_suspend(struct r8152 *tp) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5685 | { |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 5686 | struct net_device *netdev = tp->netdev; |
| 5687 | int ret = 0; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5688 | |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 5689 | set_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5690 | smp_mb__after_atomic(); |
| 5691 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5692 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5693 | u32 rcr = 0; |
| 5694 | |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5695 | if (netif_carrier_ok(netdev)) { |
| 5696 | u32 ocp_data; |
| 5697 | |
| 5698 | rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR); |
| 5699 | ocp_data = rcr & ~RCR_ACPT_ALL; |
| 5700 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data); |
| 5701 | rxdy_gated_en(tp, true); |
| 5702 | ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, |
| 5703 | PLA_OOB_CTRL); |
| 5704 | if (!(ocp_data & RXFIFO_EMPTY)) { |
| 5705 | rxdy_gated_en(tp, false); |
| 5706 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | 26afec3 | 2017-01-26 09:38:31 +0800 | [diff] [blame] | 5707 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
| 5708 | smp_mb__after_atomic(); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5709 | ret = -EBUSY; |
| 5710 | goto out1; |
| 5711 | } |
| 5712 | } |
| 5713 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5714 | clear_bit(WORK_ENABLE, &tp->flags); |
| 5715 | usb_kill_urb(tp->intr_urb); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5716 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5717 | tp->rtl_ops.autosuspend_en(tp, true); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5718 | |
| 5719 | if (netif_carrier_ok(netdev)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5720 | struct napi_struct *napi = &tp->napi; |
| 5721 | |
| 5722 | napi_disable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5723 | rtl_stop_rx(tp); |
| 5724 | rxdy_gated_en(tp, false); |
| 5725 | ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5726 | napi_enable(napi); |
hayeswang | 75dc692 | 2017-01-10 17:04:07 +0800 | [diff] [blame] | 5727 | } |
hayeswang | bd88298 | 2017-06-13 15:14:40 +0800 | [diff] [blame] | 5728 | |
| 5729 | if (delay_autosuspend(tp)) { |
| 5730 | rtl8152_runtime_resume(tp); |
| 5731 | ret = -EBUSY; |
| 5732 | } |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 5733 | } |
| 5734 | |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5735 | out1: |
| 5736 | return ret; |
| 5737 | } |
| 5738 | |
| 5739 | static int rtl8152_system_suspend(struct r8152 *tp) |
| 5740 | { |
| 5741 | struct net_device *netdev = tp->netdev; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5742 | |
| 5743 | netif_device_detach(netdev); |
| 5744 | |
hayeswang | e3bd1a8 | 2014-10-29 11:12:17 +0800 | [diff] [blame] | 5745 | if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) { |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5746 | struct napi_struct *napi = &tp->napi; |
| 5747 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5748 | clear_bit(WORK_ENABLE, &tp->flags); |
hayeswang | 40a8291 | 2013-08-14 20:54:40 +0800 | [diff] [blame] | 5749 | usb_kill_urb(tp->intr_urb); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5750 | tasklet_disable(&tp->tx_tl); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5751 | napi_disable(napi); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5752 | cancel_delayed_work_sync(&tp->schedule); |
| 5753 | tp->rtl_ops.down(tp); |
hayeswang | ce594e9 | 2017-03-16 14:32:22 +0800 | [diff] [blame] | 5754 | napi_enable(napi); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 5755 | tasklet_enable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5756 | } |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5757 | |
zhong jiang | f741917 | 2018-08-09 09:39:13 +0800 | [diff] [blame] | 5758 | return 0; |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message) |
| 5762 | { |
| 5763 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5764 | int ret; |
| 5765 | |
| 5766 | mutex_lock(&tp->control); |
| 5767 | |
| 5768 | if (PMSG_IS_AUTO(message)) |
hayeswang | a9c54ad | 2017-01-25 13:41:45 +0800 | [diff] [blame] | 5769 | ret = rtl8152_runtime_suspend(tp); |
hayeswang | 8fb2806 | 2017-01-10 17:04:06 +0800 | [diff] [blame] | 5770 | else |
| 5771 | ret = rtl8152_system_suspend(tp); |
| 5772 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5773 | mutex_unlock(&tp->control); |
| 5774 | |
hayeswang | 6cc69f2 | 2014-10-17 16:55:08 +0800 | [diff] [blame] | 5775 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | static int rtl8152_resume(struct usb_interface *intf) |
| 5779 | { |
| 5780 | struct r8152 *tp = usb_get_intfdata(intf); |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5781 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5782 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5783 | mutex_lock(&tp->control); |
| 5784 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5785 | if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) |
| 5786 | ret = rtl8152_runtime_resume(tp); |
| 5787 | else |
| 5788 | ret = rtl8152_system_resume(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5789 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5790 | mutex_unlock(&tp->control); |
| 5791 | |
hayeswang | 21cbd0e | 2017-06-13 15:14:39 +0800 | [diff] [blame] | 5792 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 5795 | static int rtl8152_reset_resume(struct usb_interface *intf) |
| 5796 | { |
| 5797 | struct r8152 *tp = usb_get_intfdata(intf); |
| 5798 | |
| 5799 | clear_bit(SELECTIVE_SUSPEND, &tp->flags); |
hayeswang | befb2de | 2017-06-09 17:11:45 +0800 | [diff] [blame] | 5800 | tp->rtl_ops.init(tp); |
| 5801 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
Kai-Heng Feng | a54cdee | 2019-10-04 20:51:04 +0800 | [diff] [blame] | 5802 | set_ethernet_addr(tp); |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 5803 | return rtl8152_resume(intf); |
| 5804 | } |
| 5805 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5806 | static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 5807 | { |
| 5808 | struct r8152 *tp = netdev_priv(dev); |
| 5809 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5810 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5811 | return; |
| 5812 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 5813 | if (!rtl_can_wakeup(tp)) { |
| 5814 | wol->supported = 0; |
| 5815 | wol->wolopts = 0; |
| 5816 | } else { |
| 5817 | mutex_lock(&tp->control); |
| 5818 | wol->supported = WAKE_ANY; |
| 5819 | wol->wolopts = __rtl_get_wol(tp); |
| 5820 | mutex_unlock(&tp->control); |
| 5821 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5822 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5823 | usb_autopm_put_interface(tp->intf); |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) |
| 5827 | { |
| 5828 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5829 | int ret; |
| 5830 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 5831 | if (!rtl_can_wakeup(tp)) |
| 5832 | return -EOPNOTSUPP; |
| 5833 | |
Florian Fainelli | f2750df | 2018-09-28 16:18:54 -0700 | [diff] [blame] | 5834 | if (wol->wolopts & ~WAKE_ANY) |
| 5835 | return -EINVAL; |
| 5836 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5837 | ret = usb_autopm_get_interface(tp->intf); |
| 5838 | if (ret < 0) |
| 5839 | goto out_set_wol; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5840 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5841 | mutex_lock(&tp->control); |
| 5842 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5843 | __rtl_set_wol(tp, wol->wolopts); |
| 5844 | tp->saved_wolopts = wol->wolopts & WAKE_ANY; |
| 5845 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5846 | mutex_unlock(&tp->control); |
| 5847 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5848 | usb_autopm_put_interface(tp->intf); |
| 5849 | |
| 5850 | out_set_wol: |
| 5851 | return ret; |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 5852 | } |
| 5853 | |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 5854 | static u32 rtl8152_get_msglevel(struct net_device *dev) |
| 5855 | { |
| 5856 | struct r8152 *tp = netdev_priv(dev); |
| 5857 | |
| 5858 | return tp->msg_enable; |
| 5859 | } |
| 5860 | |
| 5861 | static void rtl8152_set_msglevel(struct net_device *dev, u32 value) |
| 5862 | { |
| 5863 | struct r8152 *tp = netdev_priv(dev); |
| 5864 | |
| 5865 | tp->msg_enable = value; |
| 5866 | } |
| 5867 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5868 | static void rtl8152_get_drvinfo(struct net_device *netdev, |
| 5869 | struct ethtool_drvinfo *info) |
| 5870 | { |
| 5871 | struct r8152 *tp = netdev_priv(netdev); |
| 5872 | |
hayeswang | b0b46c7 | 2014-08-26 10:08:23 +0800 | [diff] [blame] | 5873 | strlcpy(info->driver, MODULENAME, sizeof(info->driver)); |
| 5874 | strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5875 | usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info)); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 5876 | if (!IS_ERR_OR_NULL(tp->rtl_fw.fw)) |
| 5877 | strlcpy(info->fw_version, tp->rtl_fw.version, |
| 5878 | sizeof(info->fw_version)); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5879 | } |
| 5880 | |
| 5881 | static |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5882 | int rtl8152_get_link_ksettings(struct net_device *netdev, |
| 5883 | struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5884 | { |
| 5885 | struct r8152 *tp = netdev_priv(netdev); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5886 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5887 | |
| 5888 | if (!tp->mii.mdio_read) |
| 5889 | return -EOPNOTSUPP; |
| 5890 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5891 | ret = usb_autopm_get_interface(tp->intf); |
| 5892 | if (ret < 0) |
| 5893 | goto out; |
| 5894 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5895 | mutex_lock(&tp->control); |
| 5896 | |
yuval.shaia@oracle.com | 82c01a8 | 2017-06-04 20:22:00 +0300 | [diff] [blame] | 5897 | mii_ethtool_get_link_ksettings(&tp->mii, cmd); |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5898 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5899 | mutex_unlock(&tp->control); |
| 5900 | |
hayeswang | 8d4a4d7 | 2014-10-09 18:00:25 +0800 | [diff] [blame] | 5901 | usb_autopm_put_interface(tp->intf); |
| 5902 | |
| 5903 | out: |
| 5904 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5907 | static int rtl8152_set_link_ksettings(struct net_device *dev, |
| 5908 | const struct ethtool_link_ksettings *cmd) |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5909 | { |
| 5910 | struct r8152 *tp = netdev_priv(dev); |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5911 | u32 advertising = 0; |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5912 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5913 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5914 | ret = usb_autopm_get_interface(tp->intf); |
| 5915 | if (ret < 0) |
| 5916 | goto out; |
| 5917 | |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5918 | if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 5919 | cmd->link_modes.advertising)) |
| 5920 | advertising |= RTL_ADVERTISED_10_HALF; |
| 5921 | |
| 5922 | if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 5923 | cmd->link_modes.advertising)) |
| 5924 | advertising |= RTL_ADVERTISED_10_FULL; |
| 5925 | |
| 5926 | if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 5927 | cmd->link_modes.advertising)) |
| 5928 | advertising |= RTL_ADVERTISED_100_HALF; |
| 5929 | |
| 5930 | if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 5931 | cmd->link_modes.advertising)) |
| 5932 | advertising |= RTL_ADVERTISED_100_FULL; |
| 5933 | |
| 5934 | if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 5935 | cmd->link_modes.advertising)) |
| 5936 | advertising |= RTL_ADVERTISED_1000_HALF; |
| 5937 | |
| 5938 | if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 5939 | cmd->link_modes.advertising)) |
| 5940 | advertising |= RTL_ADVERTISED_1000_FULL; |
| 5941 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5942 | mutex_lock(&tp->control); |
| 5943 | |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5944 | ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed, |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5945 | cmd->base.duplex, advertising); |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 5946 | if (!ret) { |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 5947 | tp->autoneg = cmd->base.autoneg; |
| 5948 | tp->speed = cmd->base.speed; |
| 5949 | tp->duplex = cmd->base.duplex; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 5950 | tp->advertising = advertising; |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 5951 | } |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5952 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 5953 | mutex_unlock(&tp->control); |
| 5954 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 5955 | usb_autopm_put_interface(tp->intf); |
| 5956 | |
| 5957 | out: |
| 5958 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5961 | static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = { |
| 5962 | "tx_packets", |
| 5963 | "rx_packets", |
| 5964 | "tx_errors", |
| 5965 | "rx_errors", |
| 5966 | "rx_missed", |
| 5967 | "align_errors", |
| 5968 | "tx_single_collisions", |
| 5969 | "tx_multi_collisions", |
| 5970 | "rx_unicast", |
| 5971 | "rx_broadcast", |
| 5972 | "rx_multicast", |
| 5973 | "tx_aborted", |
| 5974 | "tx_underrun", |
| 5975 | }; |
| 5976 | |
| 5977 | static int rtl8152_get_sset_count(struct net_device *dev, int sset) |
| 5978 | { |
| 5979 | switch (sset) { |
| 5980 | case ETH_SS_STATS: |
| 5981 | return ARRAY_SIZE(rtl8152_gstrings); |
| 5982 | default: |
| 5983 | return -EOPNOTSUPP; |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | static void rtl8152_get_ethtool_stats(struct net_device *dev, |
| 5988 | struct ethtool_stats *stats, u64 *data) |
| 5989 | { |
| 5990 | struct r8152 *tp = netdev_priv(dev); |
| 5991 | struct tally_counter tally; |
| 5992 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 5993 | if (usb_autopm_get_interface(tp->intf) < 0) |
| 5994 | return; |
| 5995 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 5996 | generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA); |
| 5997 | |
hayeswang | 0b03024 | 2014-07-08 14:49:28 +0800 | [diff] [blame] | 5998 | usb_autopm_put_interface(tp->intf); |
| 5999 | |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 6000 | data[0] = le64_to_cpu(tally.tx_packets); |
| 6001 | data[1] = le64_to_cpu(tally.rx_packets); |
| 6002 | data[2] = le64_to_cpu(tally.tx_errors); |
| 6003 | data[3] = le32_to_cpu(tally.rx_errors); |
| 6004 | data[4] = le16_to_cpu(tally.rx_missed); |
| 6005 | data[5] = le16_to_cpu(tally.align_errors); |
| 6006 | data[6] = le32_to_cpu(tally.tx_one_collision); |
| 6007 | data[7] = le32_to_cpu(tally.tx_multi_collision); |
| 6008 | data[8] = le64_to_cpu(tally.rx_unicast); |
| 6009 | data[9] = le64_to_cpu(tally.rx_broadcast); |
| 6010 | data[10] = le32_to_cpu(tally.rx_multicast); |
| 6011 | data[11] = le16_to_cpu(tally.tx_aborted); |
hayeswang | f37119c | 2014-10-28 14:05:51 +0800 | [diff] [blame] | 6012 | data[12] = le16_to_cpu(tally.tx_underrun); |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 6013 | } |
| 6014 | |
| 6015 | static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 6016 | { |
| 6017 | switch (stringset) { |
| 6018 | case ETH_SS_STATS: |
| 6019 | memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings)); |
| 6020 | break; |
| 6021 | } |
| 6022 | } |
| 6023 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6024 | static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 6025 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6026 | u32 lp, adv, supported = 0; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6027 | u16 val; |
| 6028 | |
| 6029 | val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
| 6030 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 6031 | |
| 6032 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
| 6033 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6034 | |
| 6035 | val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
| 6036 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6037 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6038 | eee->eee_enabled = tp->eee_en; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6039 | eee->eee_active = !!(supported & adv & lp); |
| 6040 | eee->supported = supported; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6041 | eee->advertised = tp->eee_adv; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6042 | eee->lp_advertised = lp; |
| 6043 | |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
| 6047 | static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 6048 | { |
| 6049 | u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised); |
| 6050 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6051 | tp->eee_en = eee->eee_enabled; |
| 6052 | tp->eee_adv = val; |
| 6053 | |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 6054 | rtl_eee_enable(tp, tp->eee_en); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6055 | |
| 6056 | return 0; |
| 6057 | } |
| 6058 | |
| 6059 | static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee) |
| 6060 | { |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6061 | u32 lp, adv, supported = 0; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6062 | u16 val; |
| 6063 | |
| 6064 | val = ocp_reg_read(tp, OCP_EEE_ABLE); |
| 6065 | supported = mmd_eee_cap_to_ethtool_sup_t(val); |
| 6066 | |
| 6067 | val = ocp_reg_read(tp, OCP_EEE_ADV); |
| 6068 | adv = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6069 | |
| 6070 | val = ocp_reg_read(tp, OCP_EEE_LPABLE); |
| 6071 | lp = mmd_eee_adv_to_ethtool_adv_t(val); |
| 6072 | |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6073 | eee->eee_enabled = tp->eee_en; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6074 | eee->eee_active = !!(supported & adv & lp); |
| 6075 | eee->supported = supported; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6076 | eee->advertised = tp->eee_adv; |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6077 | eee->lp_advertised = lp; |
| 6078 | |
| 6079 | return 0; |
| 6080 | } |
| 6081 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6082 | static int |
| 6083 | rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata) |
| 6084 | { |
| 6085 | struct r8152 *tp = netdev_priv(net); |
| 6086 | int ret; |
| 6087 | |
| 6088 | ret = usb_autopm_get_interface(tp->intf); |
| 6089 | if (ret < 0) |
| 6090 | goto out; |
| 6091 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6092 | mutex_lock(&tp->control); |
| 6093 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6094 | ret = tp->rtl_ops.eee_get(tp, edata); |
| 6095 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6096 | mutex_unlock(&tp->control); |
| 6097 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6098 | usb_autopm_put_interface(tp->intf); |
| 6099 | |
| 6100 | out: |
| 6101 | return ret; |
| 6102 | } |
| 6103 | |
| 6104 | static int |
| 6105 | rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata) |
| 6106 | { |
| 6107 | struct r8152 *tp = netdev_priv(net); |
| 6108 | int ret; |
| 6109 | |
| 6110 | ret = usb_autopm_get_interface(tp->intf); |
| 6111 | if (ret < 0) |
| 6112 | goto out; |
| 6113 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6114 | mutex_lock(&tp->control); |
| 6115 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6116 | ret = tp->rtl_ops.eee_set(tp, edata); |
hayeswang | 9d31a7b | 2014-10-06 10:36:04 +0800 | [diff] [blame] | 6117 | if (!ret) |
| 6118 | ret = mii_nway_restart(&tp->mii); |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6119 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6120 | mutex_unlock(&tp->control); |
| 6121 | |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6122 | usb_autopm_put_interface(tp->intf); |
| 6123 | |
| 6124 | out: |
| 6125 | return ret; |
| 6126 | } |
| 6127 | |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 6128 | static int rtl8152_nway_reset(struct net_device *dev) |
| 6129 | { |
| 6130 | struct r8152 *tp = netdev_priv(dev); |
| 6131 | int ret; |
| 6132 | |
| 6133 | ret = usb_autopm_get_interface(tp->intf); |
| 6134 | if (ret < 0) |
| 6135 | goto out; |
| 6136 | |
| 6137 | mutex_lock(&tp->control); |
| 6138 | |
| 6139 | ret = mii_nway_restart(&tp->mii); |
| 6140 | |
| 6141 | mutex_unlock(&tp->control); |
| 6142 | |
| 6143 | usb_autopm_put_interface(tp->intf); |
| 6144 | |
| 6145 | out: |
| 6146 | return ret; |
| 6147 | } |
| 6148 | |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6149 | static int rtl8152_get_coalesce(struct net_device *netdev, |
| 6150 | struct ethtool_coalesce *coalesce) |
| 6151 | { |
| 6152 | struct r8152 *tp = netdev_priv(netdev); |
| 6153 | |
| 6154 | switch (tp->version) { |
| 6155 | case RTL_VER_01: |
| 6156 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6157 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6158 | return -EOPNOTSUPP; |
| 6159 | default: |
| 6160 | break; |
| 6161 | } |
| 6162 | |
| 6163 | coalesce->rx_coalesce_usecs = tp->coalesce; |
| 6164 | |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
| 6168 | static int rtl8152_set_coalesce(struct net_device *netdev, |
| 6169 | struct ethtool_coalesce *coalesce) |
| 6170 | { |
| 6171 | struct r8152 *tp = netdev_priv(netdev); |
| 6172 | int ret; |
| 6173 | |
| 6174 | switch (tp->version) { |
| 6175 | case RTL_VER_01: |
| 6176 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6177 | case RTL_VER_07: |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6178 | return -EOPNOTSUPP; |
| 6179 | default: |
| 6180 | break; |
| 6181 | } |
| 6182 | |
| 6183 | if (coalesce->rx_coalesce_usecs > COALESCE_SLOW) |
| 6184 | return -EINVAL; |
| 6185 | |
| 6186 | ret = usb_autopm_get_interface(tp->intf); |
| 6187 | if (ret < 0) |
| 6188 | return ret; |
| 6189 | |
| 6190 | mutex_lock(&tp->control); |
| 6191 | |
| 6192 | if (tp->coalesce != coalesce->rx_coalesce_usecs) { |
| 6193 | tp->coalesce = coalesce->rx_coalesce_usecs; |
| 6194 | |
Hayes Wang | 9fae541 | 2019-07-03 15:11:56 +0800 | [diff] [blame] | 6195 | if (netif_running(netdev) && netif_carrier_ok(netdev)) { |
| 6196 | netif_stop_queue(netdev); |
| 6197 | napi_disable(&tp->napi); |
| 6198 | tp->rtl_ops.disable(tp); |
| 6199 | tp->rtl_ops.enable(tp); |
| 6200 | rtl_start_rx(tp); |
| 6201 | clear_bit(RTL8152_SET_RX_MODE, &tp->flags); |
| 6202 | _rtl8152_set_rx_mode(netdev); |
| 6203 | napi_enable(&tp->napi); |
| 6204 | netif_wake_queue(netdev); |
| 6205 | } |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | mutex_unlock(&tp->control); |
| 6209 | |
| 6210 | usb_autopm_put_interface(tp->intf); |
| 6211 | |
| 6212 | return ret; |
| 6213 | } |
| 6214 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6215 | static int rtl8152_get_tunable(struct net_device *netdev, |
| 6216 | const struct ethtool_tunable *tunable, void *d) |
| 6217 | { |
| 6218 | struct r8152 *tp = netdev_priv(netdev); |
| 6219 | |
| 6220 | switch (tunable->id) { |
| 6221 | case ETHTOOL_RX_COPYBREAK: |
| 6222 | *(u32 *)d = tp->rx_copybreak; |
| 6223 | break; |
| 6224 | default: |
| 6225 | return -EOPNOTSUPP; |
| 6226 | } |
| 6227 | |
| 6228 | return 0; |
| 6229 | } |
| 6230 | |
| 6231 | static int rtl8152_set_tunable(struct net_device *netdev, |
| 6232 | const struct ethtool_tunable *tunable, |
| 6233 | const void *d) |
| 6234 | { |
| 6235 | struct r8152 *tp = netdev_priv(netdev); |
| 6236 | u32 val; |
| 6237 | |
| 6238 | switch (tunable->id) { |
| 6239 | case ETHTOOL_RX_COPYBREAK: |
| 6240 | val = *(u32 *)d; |
| 6241 | if (val < ETH_ZLEN) { |
| 6242 | netif_err(tp, rx_err, netdev, |
| 6243 | "Invalid rx copy break value\n"); |
| 6244 | return -EINVAL; |
| 6245 | } |
| 6246 | |
| 6247 | if (tp->rx_copybreak != val) { |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 6248 | if (netdev->flags & IFF_UP) { |
| 6249 | mutex_lock(&tp->control); |
| 6250 | napi_disable(&tp->napi); |
| 6251 | tp->rx_copybreak = val; |
| 6252 | napi_enable(&tp->napi); |
| 6253 | mutex_unlock(&tp->control); |
| 6254 | } else { |
| 6255 | tp->rx_copybreak = val; |
| 6256 | } |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6257 | } |
| 6258 | break; |
| 6259 | default: |
| 6260 | return -EOPNOTSUPP; |
| 6261 | } |
| 6262 | |
| 6263 | return 0; |
| 6264 | } |
| 6265 | |
| 6266 | static void rtl8152_get_ringparam(struct net_device *netdev, |
| 6267 | struct ethtool_ringparam *ring) |
| 6268 | { |
| 6269 | struct r8152 *tp = netdev_priv(netdev); |
| 6270 | |
| 6271 | ring->rx_max_pending = RTL8152_RX_MAX_PENDING; |
| 6272 | ring->rx_pending = tp->rx_pending; |
| 6273 | } |
| 6274 | |
| 6275 | static int rtl8152_set_ringparam(struct net_device *netdev, |
| 6276 | struct ethtool_ringparam *ring) |
| 6277 | { |
| 6278 | struct r8152 *tp = netdev_priv(netdev); |
| 6279 | |
| 6280 | if (ring->rx_pending < (RTL8152_MAX_RX * 2)) |
| 6281 | return -EINVAL; |
| 6282 | |
| 6283 | if (tp->rx_pending != ring->rx_pending) { |
Hayes Wang | 5b1d9c1 | 2019-11-22 16:21:09 +0800 | [diff] [blame] | 6284 | if (netdev->flags & IFF_UP) { |
| 6285 | mutex_lock(&tp->control); |
| 6286 | napi_disable(&tp->napi); |
| 6287 | tp->rx_pending = ring->rx_pending; |
| 6288 | napi_enable(&tp->napi); |
| 6289 | mutex_unlock(&tp->control); |
| 6290 | } else { |
| 6291 | tp->rx_pending = ring->rx_pending; |
| 6292 | } |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6293 | } |
| 6294 | |
| 6295 | return 0; |
| 6296 | } |
| 6297 | |
Julia Lawall | 407a471 | 2016-09-01 00:21:22 +0200 | [diff] [blame] | 6298 | static const struct ethtool_ops ops = { |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6299 | .get_drvinfo = rtl8152_get_drvinfo, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6300 | .get_link = ethtool_op_get_link, |
hayeswang | 8884f50 | 2014-10-28 14:05:52 +0800 | [diff] [blame] | 6301 | .nway_reset = rtl8152_nway_reset, |
hayeswang | a5ec27c | 2014-02-18 21:49:11 +0800 | [diff] [blame] | 6302 | .get_msglevel = rtl8152_get_msglevel, |
| 6303 | .set_msglevel = rtl8152_set_msglevel, |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 6304 | .get_wol = rtl8152_get_wol, |
| 6305 | .set_wol = rtl8152_set_wol, |
hayeswang | 4f1d4d5 | 2014-03-11 16:24:19 +0800 | [diff] [blame] | 6306 | .get_strings = rtl8152_get_strings, |
| 6307 | .get_sset_count = rtl8152_get_sset_count, |
| 6308 | .get_ethtool_stats = rtl8152_get_ethtool_stats, |
hayeswang | efb3dd8 | 2015-02-12 14:33:48 +0800 | [diff] [blame] | 6309 | .get_coalesce = rtl8152_get_coalesce, |
| 6310 | .set_coalesce = rtl8152_set_coalesce, |
hayeswang | df35d28 | 2014-09-25 20:54:02 +0800 | [diff] [blame] | 6311 | .get_eee = rtl_ethtool_get_eee, |
| 6312 | .set_eee = rtl_ethtool_set_eee, |
Philippe Reynes | 06144dc | 2017-03-12 22:41:58 +0100 | [diff] [blame] | 6313 | .get_link_ksettings = rtl8152_get_link_ksettings, |
| 6314 | .set_link_ksettings = rtl8152_set_link_ksettings, |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6315 | .get_tunable = rtl8152_get_tunable, |
| 6316 | .set_tunable = rtl8152_set_tunable, |
| 6317 | .get_ringparam = rtl8152_get_ringparam, |
| 6318 | .set_ringparam = rtl8152_set_ringparam, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6319 | }; |
| 6320 | |
| 6321 | static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) |
| 6322 | { |
| 6323 | struct r8152 *tp = netdev_priv(netdev); |
| 6324 | struct mii_ioctl_data *data = if_mii(rq); |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6325 | int res; |
| 6326 | |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 6327 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6328 | return -ENODEV; |
| 6329 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6330 | res = usb_autopm_get_interface(tp->intf); |
| 6331 | if (res < 0) |
| 6332 | goto out; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6333 | |
| 6334 | switch (cmd) { |
| 6335 | case SIOCGMIIPHY: |
| 6336 | data->phy_id = R8152_PHY_ID; /* Internal PHY */ |
| 6337 | break; |
| 6338 | |
| 6339 | case SIOCGMIIREG: |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6340 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6341 | data->val_out = r8152_mdio_read(tp, data->reg_num); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6342 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6343 | break; |
| 6344 | |
| 6345 | case SIOCSMIIREG: |
| 6346 | if (!capable(CAP_NET_ADMIN)) { |
| 6347 | res = -EPERM; |
| 6348 | break; |
| 6349 | } |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6350 | mutex_lock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6351 | r8152_mdio_write(tp, data->reg_num, data->val_in); |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6352 | mutex_unlock(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6353 | break; |
| 6354 | |
| 6355 | default: |
| 6356 | res = -EOPNOTSUPP; |
| 6357 | } |
| 6358 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6359 | usb_autopm_put_interface(tp->intf); |
| 6360 | |
| 6361 | out: |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6362 | return res; |
| 6363 | } |
| 6364 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6365 | static int rtl8152_change_mtu(struct net_device *dev, int new_mtu) |
| 6366 | { |
| 6367 | struct r8152 *tp = netdev_priv(dev); |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 6368 | int ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6369 | |
| 6370 | switch (tp->version) { |
| 6371 | case RTL_VER_01: |
| 6372 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6373 | case RTL_VER_07: |
Jarod Wilson | a52ad51 | 2016-10-07 22:04:34 -0400 | [diff] [blame] | 6374 | dev->mtu = new_mtu; |
| 6375 | return 0; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6376 | default: |
| 6377 | break; |
| 6378 | } |
| 6379 | |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 6380 | ret = usb_autopm_get_interface(tp->intf); |
| 6381 | if (ret < 0) |
| 6382 | return ret; |
| 6383 | |
| 6384 | mutex_lock(&tp->control); |
| 6385 | |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6386 | dev->mtu = new_mtu; |
| 6387 | |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 6388 | if (netif_running(dev)) { |
hayeswang | b65c0c9 | 2017-06-21 11:25:18 +0800 | [diff] [blame] | 6389 | u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; |
hayeswang | 210c4f7 | 2017-03-20 16:13:44 +0800 | [diff] [blame] | 6390 | |
| 6391 | ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms); |
| 6392 | |
| 6393 | if (netif_carrier_ok(dev)) |
| 6394 | r8153_set_rx_early_size(tp); |
| 6395 | } |
hayeswang | 396e2e2 | 2015-02-12 14:33:47 +0800 | [diff] [blame] | 6396 | |
| 6397 | mutex_unlock(&tp->control); |
| 6398 | |
| 6399 | usb_autopm_put_interface(tp->intf); |
| 6400 | |
| 6401 | return ret; |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6402 | } |
| 6403 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6404 | static const struct net_device_ops rtl8152_netdev_ops = { |
| 6405 | .ndo_open = rtl8152_open, |
| 6406 | .ndo_stop = rtl8152_close, |
| 6407 | .ndo_do_ioctl = rtl8152_ioctl, |
| 6408 | .ndo_start_xmit = rtl8152_start_xmit, |
| 6409 | .ndo_tx_timeout = rtl8152_tx_timeout, |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6410 | .ndo_set_features = rtl8152_set_features, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6411 | .ndo_set_rx_mode = rtl8152_set_rx_mode, |
| 6412 | .ndo_set_mac_address = rtl8152_set_mac_address, |
hayeswang | 69b4b7a | 2014-07-10 10:58:54 +0800 | [diff] [blame] | 6413 | .ndo_change_mtu = rtl8152_change_mtu, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6414 | .ndo_validate_addr = eth_validate_addr, |
hayeswang | a5e3125 | 2015-01-06 17:41:58 +0800 | [diff] [blame] | 6415 | .ndo_features_check = rtl8152_features_check, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6416 | }; |
| 6417 | |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 6418 | static void rtl8152_unload(struct r8152 *tp) |
| 6419 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 6420 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6421 | return; |
| 6422 | |
hayeswang | 00a5e36 | 2014-02-18 21:48:59 +0800 | [diff] [blame] | 6423 | if (tp->version != RTL_VER_01) |
| 6424 | r8152_power_cut_en(tp, true); |
hayeswang | e3fe0b1 | 2014-01-02 11:22:39 +0800 | [diff] [blame] | 6425 | } |
| 6426 | |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 6427 | static void rtl8153_unload(struct r8152 *tp) |
| 6428 | { |
hayeswang | 6871438 | 2014-04-11 17:54:31 +0800 | [diff] [blame] | 6429 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6430 | return; |
| 6431 | |
hayeswang | 49be172 | 2014-10-01 13:25:11 +0800 | [diff] [blame] | 6432 | r8153_power_cut_en(tp, false); |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 6433 | } |
| 6434 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6435 | static void rtl8153b_unload(struct r8152 *tp) |
| 6436 | { |
| 6437 | if (test_bit(RTL8152_UNPLUG, &tp->flags)) |
| 6438 | return; |
| 6439 | |
| 6440 | r8153b_power_cut_en(tp, false); |
| 6441 | } |
| 6442 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6443 | static int rtl_ops_init(struct r8152 *tp) |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6444 | { |
| 6445 | struct rtl_ops *ops = &tp->rtl_ops; |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6446 | int ret = 0; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6447 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6448 | switch (tp->version) { |
| 6449 | case RTL_VER_01: |
| 6450 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6451 | case RTL_VER_07: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6452 | ops->init = r8152b_init; |
| 6453 | ops->enable = rtl8152_enable; |
| 6454 | ops->disable = rtl8152_disable; |
| 6455 | ops->up = rtl8152_up; |
| 6456 | ops->down = rtl8152_down; |
| 6457 | ops->unload = rtl8152_unload; |
| 6458 | ops->eee_get = r8152_get_eee; |
| 6459 | ops->eee_set = r8152_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 6460 | ops->in_nway = rtl8152_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6461 | ops->hw_phy_cfg = r8152b_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 6462 | ops->autosuspend_en = rtl_runtime_suspend_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 6463 | tp->rx_buf_sz = 16 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6464 | tp->eee_en = true; |
| 6465 | tp->eee_adv = MDIO_EEE_100TX; |
hayeswang | 43779f8 | 2014-01-02 11:25:10 +0800 | [diff] [blame] | 6466 | break; |
| 6467 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6468 | case RTL_VER_03: |
| 6469 | case RTL_VER_04: |
| 6470 | case RTL_VER_05: |
hayeswang | fb02eb4 | 2015-07-22 15:27:41 +0800 | [diff] [blame] | 6471 | case RTL_VER_06: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6472 | ops->init = r8153_init; |
| 6473 | ops->enable = rtl8153_enable; |
| 6474 | ops->disable = rtl8153_disable; |
| 6475 | ops->up = rtl8153_up; |
| 6476 | ops->down = rtl8153_down; |
| 6477 | ops->unload = rtl8153_unload; |
| 6478 | ops->eee_get = r8153_get_eee; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 6479 | ops->eee_set = r8152_set_eee; |
hayeswang | 2dd49e0 | 2015-09-07 11:57:44 +0800 | [diff] [blame] | 6480 | ops->in_nway = rtl8153_in_nway; |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6481 | ops->hw_phy_cfg = r8153_hw_phy_cfg; |
hayeswang | 2609af1 | 2016-07-05 16:11:46 +0800 | [diff] [blame] | 6482 | ops->autosuspend_en = rtl8153_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 6483 | tp->rx_buf_sz = 32 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6484 | tp->eee_en = true; |
| 6485 | tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6486 | break; |
| 6487 | |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6488 | case RTL_VER_08: |
| 6489 | case RTL_VER_09: |
| 6490 | ops->init = r8153b_init; |
| 6491 | ops->enable = rtl8153_enable; |
Hayes Wang | 0e5b36b | 2019-09-05 10:46:20 +0800 | [diff] [blame] | 6492 | ops->disable = rtl8153_disable; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6493 | ops->up = rtl8153b_up; |
| 6494 | ops->down = rtl8153b_down; |
| 6495 | ops->unload = rtl8153b_unload; |
| 6496 | ops->eee_get = r8153_get_eee; |
Hayes Wang | e7bde56 | 2019-08-23 15:33:41 +0800 | [diff] [blame] | 6497 | ops->eee_set = r8152_set_eee; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6498 | ops->in_nway = rtl8153_in_nway; |
| 6499 | ops->hw_phy_cfg = r8153b_hw_phy_cfg; |
| 6500 | ops->autosuspend_en = rtl8153b_runtime_enable; |
Hayes Wang | ec5791c | 2019-08-13 11:42:05 +0800 | [diff] [blame] | 6501 | tp->rx_buf_sz = 32 * 1024; |
Hayes Wang | f4a93be | 2019-08-23 15:33:40 +0800 | [diff] [blame] | 6502 | tp->eee_en = true; |
| 6503 | tp->eee_adv = MDIO_EEE_1000T | MDIO_EEE_100TX; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6504 | break; |
| 6505 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6506 | default: |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6507 | ret = -ENODEV; |
| 6508 | netif_err(tp, probe, tp->netdev, "Unknown Device\n"); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6509 | break; |
| 6510 | } |
| 6511 | |
| 6512 | return ret; |
| 6513 | } |
| 6514 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6515 | #define FIRMWARE_8153A_2 "rtl_nic/rtl8153a-2.fw" |
| 6516 | #define FIRMWARE_8153A_3 "rtl_nic/rtl8153a-3.fw" |
| 6517 | #define FIRMWARE_8153A_4 "rtl_nic/rtl8153a-4.fw" |
| 6518 | #define FIRMWARE_8153B_2 "rtl_nic/rtl8153b-2.fw" |
| 6519 | |
| 6520 | MODULE_FIRMWARE(FIRMWARE_8153A_2); |
| 6521 | MODULE_FIRMWARE(FIRMWARE_8153A_3); |
| 6522 | MODULE_FIRMWARE(FIRMWARE_8153A_4); |
| 6523 | MODULE_FIRMWARE(FIRMWARE_8153B_2); |
| 6524 | |
| 6525 | static int rtl_fw_init(struct r8152 *tp) |
| 6526 | { |
| 6527 | struct rtl_fw *rtl_fw = &tp->rtl_fw; |
| 6528 | |
| 6529 | switch (tp->version) { |
| 6530 | case RTL_VER_04: |
| 6531 | rtl_fw->fw_name = FIRMWARE_8153A_2; |
| 6532 | rtl_fw->pre_fw = r8153_pre_firmware_1; |
| 6533 | rtl_fw->post_fw = r8153_post_firmware_1; |
| 6534 | break; |
| 6535 | case RTL_VER_05: |
| 6536 | rtl_fw->fw_name = FIRMWARE_8153A_3; |
| 6537 | rtl_fw->pre_fw = r8153_pre_firmware_2; |
| 6538 | rtl_fw->post_fw = r8153_post_firmware_2; |
| 6539 | break; |
| 6540 | case RTL_VER_06: |
| 6541 | rtl_fw->fw_name = FIRMWARE_8153A_4; |
| 6542 | rtl_fw->post_fw = r8153_post_firmware_3; |
| 6543 | break; |
| 6544 | case RTL_VER_09: |
| 6545 | rtl_fw->fw_name = FIRMWARE_8153B_2; |
| 6546 | rtl_fw->pre_fw = r8153b_pre_firmware_1; |
| 6547 | rtl_fw->post_fw = r8153b_post_firmware_1; |
| 6548 | break; |
| 6549 | default: |
| 6550 | break; |
| 6551 | } |
| 6552 | |
| 6553 | return 0; |
| 6554 | } |
| 6555 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6556 | static u8 rtl_get_version(struct usb_interface *intf) |
| 6557 | { |
| 6558 | struct usb_device *udev = interface_to_usbdev(intf); |
| 6559 | u32 ocp_data = 0; |
| 6560 | __le32 *tmp; |
| 6561 | u8 version; |
| 6562 | int ret; |
| 6563 | |
| 6564 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); |
| 6565 | if (!tmp) |
| 6566 | return 0; |
| 6567 | |
| 6568 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 6569 | RTL8152_REQ_GET_REGS, RTL8152_REQT_READ, |
| 6570 | PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500); |
| 6571 | if (ret > 0) |
| 6572 | ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK; |
| 6573 | |
| 6574 | kfree(tmp); |
| 6575 | |
| 6576 | switch (ocp_data) { |
| 6577 | case 0x4c00: |
| 6578 | version = RTL_VER_01; |
| 6579 | break; |
| 6580 | case 0x4c10: |
| 6581 | version = RTL_VER_02; |
| 6582 | break; |
| 6583 | case 0x5c00: |
| 6584 | version = RTL_VER_03; |
| 6585 | break; |
| 6586 | case 0x5c10: |
| 6587 | version = RTL_VER_04; |
| 6588 | break; |
| 6589 | case 0x5c20: |
| 6590 | version = RTL_VER_05; |
| 6591 | break; |
| 6592 | case 0x5c30: |
| 6593 | version = RTL_VER_06; |
| 6594 | break; |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6595 | case 0x4800: |
| 6596 | version = RTL_VER_07; |
| 6597 | break; |
hayeswang | 65b82d6 | 2017-06-15 14:44:03 +0800 | [diff] [blame] | 6598 | case 0x6000: |
| 6599 | version = RTL_VER_08; |
| 6600 | break; |
| 6601 | case 0x6010: |
| 6602 | version = RTL_VER_09; |
| 6603 | break; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6604 | default: |
| 6605 | version = RTL_VER_UNKNOWN; |
| 6606 | dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data); |
| 6607 | break; |
| 6608 | } |
| 6609 | |
Oliver Neukum | eb3c28c | 2017-06-12 13:56:51 +0200 | [diff] [blame] | 6610 | dev_dbg(&intf->dev, "Detected version 0x%04x\n", version); |
| 6611 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6612 | return version; |
| 6613 | } |
| 6614 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6615 | static int rtl8152_probe(struct usb_interface *intf, |
| 6616 | const struct usb_device_id *id) |
| 6617 | { |
| 6618 | struct usb_device *udev = interface_to_usbdev(intf); |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6619 | u8 version = rtl_get_version(intf); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6620 | struct r8152 *tp; |
| 6621 | struct net_device *netdev; |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6622 | int ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6623 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6624 | if (version == RTL_VER_UNKNOWN) |
| 6625 | return -ENODEV; |
| 6626 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 6627 | if (udev->actconfig->desc.bConfigurationValue != 1) { |
| 6628 | usb_driver_set_configuration(udev, 1); |
| 6629 | return -ENODEV; |
| 6630 | } |
| 6631 | |
Johan Hovold | 86f3f4c | 2020-01-14 09:27:29 +0100 | [diff] [blame] | 6632 | if (intf->cur_altsetting->desc.bNumEndpoints < 3) |
| 6633 | return -ENODEV; |
| 6634 | |
hayeswang | 10c3271 | 2014-03-04 20:47:48 +0800 | [diff] [blame] | 6635 | usb_reset_device(udev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6636 | netdev = alloc_etherdev(sizeof(struct r8152)); |
| 6637 | if (!netdev) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 6638 | dev_err(&intf->dev, "Out of memory\n"); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6639 | return -ENOMEM; |
| 6640 | } |
| 6641 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6642 | SET_NETDEV_DEV(netdev, &intf->dev); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6643 | tp = netdev_priv(netdev); |
| 6644 | tp->msg_enable = 0x7FFF; |
| 6645 | |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 6646 | tp->udev = udev; |
| 6647 | tp->netdev = netdev; |
| 6648 | tp->intf = intf; |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6649 | tp->version = version; |
hayeswang | e3ad412 | 2014-01-06 17:08:42 +0800 | [diff] [blame] | 6650 | |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6651 | switch (version) { |
| 6652 | case RTL_VER_01: |
| 6653 | case RTL_VER_02: |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6654 | case RTL_VER_07: |
hayeswang | 33928ee | 2017-03-17 11:20:13 +0800 | [diff] [blame] | 6655 | tp->mii.supports_gmii = 0; |
| 6656 | break; |
| 6657 | default: |
| 6658 | tp->mii.supports_gmii = 1; |
| 6659 | break; |
| 6660 | } |
| 6661 | |
hayeswang | 55b6547 | 2014-11-06 12:47:39 +0800 | [diff] [blame] | 6662 | ret = rtl_ops_init(tp); |
hayeswang | 31ca1de | 2014-01-06 17:08:43 +0800 | [diff] [blame] | 6663 | if (ret) |
| 6664 | goto out; |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6665 | |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6666 | rtl_fw_init(tp); |
| 6667 | |
hayeswang | b540327 | 2014-10-09 18:00:26 +0800 | [diff] [blame] | 6668 | mutex_init(&tp->control); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6669 | INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6670 | 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] | 6671 | tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp); |
| 6672 | tasklet_disable(&tp->tx_tl); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6673 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6674 | netdev->netdev_ops = &rtl8152_netdev_ops; |
| 6675 | netdev->watchdog_timeo = RTL8152_TX_TIMEOUT; |
hayeswang | 5bd2388 | 2013-08-14 20:54:39 +0800 | [diff] [blame] | 6676 | |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 6677 | netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 6678 | NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6679 | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX | |
| 6680 | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 6681 | netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG | |
hayeswang | 6128d1bb | 2014-03-07 11:04:40 +0800 | [diff] [blame] | 6682 | NETIF_F_TSO | NETIF_F_FRAGLIST | |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6683 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6 | |
hayeswang | ccc39fa | 2015-02-06 11:30:49 +0800 | [diff] [blame] | 6684 | NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX; |
hayeswang | c555429 | 2014-09-12 10:43:11 +0800 | [diff] [blame] | 6685 | netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO | |
| 6686 | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | |
| 6687 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6; |
hayeswang | db8515e | 2014-03-06 15:07:16 +0800 | [diff] [blame] | 6688 | |
hayeswang | 19c0f40 | 2017-01-11 16:25:34 +0800 | [diff] [blame] | 6689 | if (tp->version == RTL_VER_01) { |
| 6690 | netdev->features &= ~NETIF_F_RXCSUM; |
| 6691 | netdev->hw_features &= ~NETIF_F_RXCSUM; |
| 6692 | } |
| 6693 | |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 6694 | if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO && |
| 6695 | le16_to_cpu(udev->descriptor.idProduct) == 0x3082) |
| 6696 | set_bit(LENOVO_MACPASSTHRU, &tp->flags); |
| 6697 | |
Kai-Heng Feng | 176eb61 | 2018-08-20 12:43:51 +0800 | [diff] [blame] | 6698 | if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial && |
Prashant Malani | 151ea09 | 2019-10-02 14:09:33 -0700 | [diff] [blame] | 6699 | (!strcmp(udev->serial, "000001000000") || |
| 6700 | !strcmp(udev->serial, "000002000000"))) { |
Kai-Heng Feng | 0b16551 | 2018-01-16 16:46:27 +0800 | [diff] [blame] | 6701 | dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation"); |
| 6702 | set_bit(DELL_TB_RX_AGG_BUG, &tp->flags); |
| 6703 | } |
| 6704 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 6705 | netdev->ethtool_ops = &ops; |
hayeswang | 60c8907 | 2014-03-07 11:04:39 +0800 | [diff] [blame] | 6706 | netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6707 | |
Jarod Wilson | f77f0ae | 2016-10-20 13:55:17 -0400 | [diff] [blame] | 6708 | /* MTU range: 68 - 1500 or 9194 */ |
| 6709 | netdev->min_mtu = ETH_MIN_MTU; |
| 6710 | switch (tp->version) { |
| 6711 | case RTL_VER_01: |
| 6712 | case RTL_VER_02: |
| 6713 | netdev->max_mtu = ETH_DATA_LEN; |
| 6714 | break; |
| 6715 | default: |
| 6716 | netdev->max_mtu = RTL8153_MAX_MTU; |
| 6717 | break; |
| 6718 | } |
| 6719 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6720 | tp->mii.dev = netdev; |
| 6721 | tp->mii.mdio_read = read_mii_word; |
| 6722 | tp->mii.mdio_write = write_mii_word; |
| 6723 | tp->mii.phy_id_mask = 0x3f; |
| 6724 | tp->mii.reg_num_mask = 0x1f; |
| 6725 | tp->mii.phy_id = R8152_PHY_ID; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6726 | |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 6727 | tp->autoneg = AUTONEG_ENABLE; |
Hayes Wang | 771efed | 2019-09-02 19:52:28 +0800 | [diff] [blame] | 6728 | tp->speed = SPEED_100; |
| 6729 | tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL | |
| 6730 | RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL; |
| 6731 | if (tp->mii.supports_gmii) { |
| 6732 | tp->speed = SPEED_1000; |
| 6733 | tp->advertising |= RTL_ADVERTISED_1000_FULL; |
| 6734 | } |
hayeswang | aa7e26b | 2016-06-13 17:49:38 +0800 | [diff] [blame] | 6735 | tp->duplex = DUPLEX_FULL; |
| 6736 | |
Hayes Wang | e4a5017 | 2019-08-13 11:42:09 +0800 | [diff] [blame] | 6737 | tp->rx_copybreak = RTL8152_RXFG_HEADSZ; |
| 6738 | tp->rx_pending = 10 * RTL8152_MAX_RX; |
| 6739 | |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6740 | intf->needs_remote_wakeup = 1; |
| 6741 | |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6742 | tp->rtl_ops.init(tp); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6743 | #if IS_BUILTIN(CONFIG_USB_RTL8152) |
| 6744 | /* Retry in case request_firmware() is not ready yet. */ |
| 6745 | tp->rtl_fw.retry = true; |
| 6746 | #endif |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6747 | queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6748 | set_ethernet_addr(tp); |
| 6749 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6750 | usb_set_intfdata(intf, tp); |
hayeswang | d823ab6 | 2015-01-12 12:06:23 +0800 | [diff] [blame] | 6751 | netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6752 | |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6753 | ret = register_netdev(netdev); |
| 6754 | if (ret != 0) { |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 6755 | netif_err(tp, probe, netdev, "couldn't register the device\n"); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6756 | goto out1; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6757 | } |
| 6758 | |
hayeswang | 7daed8d | 2015-07-24 13:54:24 +0800 | [diff] [blame] | 6759 | if (!rtl_can_wakeup(tp)) |
| 6760 | __rtl_set_wol(tp, 0); |
| 6761 | |
hayeswang | 21ff2e8 | 2014-02-18 21:49:06 +0800 | [diff] [blame] | 6762 | tp->saved_wolopts = __rtl_get_wol(tp); |
| 6763 | if (tp->saved_wolopts) |
| 6764 | device_set_wakeup_enable(&udev->dev, true); |
| 6765 | else |
| 6766 | device_set_wakeup_enable(&udev->dev, false); |
| 6767 | |
Hayes Wang | 4a8deae | 2014-01-07 11:18:22 +0800 | [diff] [blame] | 6768 | netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6769 | |
| 6770 | return 0; |
| 6771 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6772 | out1: |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 6773 | tasklet_kill(&tp->tx_tl); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6774 | usb_set_intfdata(intf, NULL); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6775 | out: |
| 6776 | free_netdev(netdev); |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6777 | return ret; |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6780 | static void rtl8152_disconnect(struct usb_interface *intf) |
| 6781 | { |
| 6782 | struct r8152 *tp = usb_get_intfdata(intf); |
| 6783 | |
| 6784 | usb_set_intfdata(intf, NULL); |
| 6785 | if (tp) { |
Hayes Wang | ffa9fec | 2019-07-04 17:36:32 +0800 | [diff] [blame] | 6786 | rtl_set_unplug(tp); |
hayeswang | f561de3 | 2014-09-30 16:48:01 +0800 | [diff] [blame] | 6787 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6788 | unregister_netdev(tp->netdev); |
Hayes Wang | d2187f8 | 2019-08-19 14:40:36 +0800 | [diff] [blame] | 6789 | tasklet_kill(&tp->tx_tl); |
hayeswang | a028a9e | 2016-06-13 17:49:36 +0800 | [diff] [blame] | 6790 | cancel_delayed_work_sync(&tp->hw_phy_work); |
hayeswang | c81229c | 2014-01-02 11:22:42 +0800 | [diff] [blame] | 6791 | tp->rtl_ops.unload(tp); |
Hayes Wang | 9370f2d | 2019-10-16 11:02:42 +0800 | [diff] [blame] | 6792 | rtl8152_release_firmware(tp); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6793 | free_netdev(tp->netdev); |
| 6794 | } |
| 6795 | } |
| 6796 | |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 6797 | #define REALTEK_USB_DEVICE(vend, prod) \ |
| 6798 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ |
| 6799 | USB_DEVICE_ID_MATCH_INT_CLASS, \ |
| 6800 | .idVendor = (vend), \ |
| 6801 | .idProduct = (prod), \ |
| 6802 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC \ |
| 6803 | }, \ |
| 6804 | { \ |
| 6805 | .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \ |
| 6806 | USB_DEVICE_ID_MATCH_DEVICE, \ |
| 6807 | .idVendor = (vend), \ |
| 6808 | .idProduct = (prod), \ |
| 6809 | .bInterfaceClass = USB_CLASS_COMM, \ |
| 6810 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \ |
| 6811 | .bInterfaceProtocol = USB_CDC_PROTO_NONE |
| 6812 | |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6813 | /* table of devices that work with this driver */ |
Arvind Yadav | 9b4355f | 2017-08-08 21:28:05 +0530 | [diff] [blame] | 6814 | static const struct usb_device_id rtl8152_table[] = { |
hayeswang | c27b32c | 2017-06-15 14:44:02 +0800 | [diff] [blame] | 6815 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 6816 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)}, |
| 6817 | {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)}, |
René Rebe | d5b07cc | 2017-03-28 07:56:51 +0200 | [diff] [blame] | 6818 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)}, |
| 6819 | {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)}, |
hayeswang | d9a28c5 | 2014-12-04 10:43:11 +0800 | [diff] [blame] | 6820 | {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)}, |
Vasily Titskiy | 1006da1 | 2015-05-06 10:31:21 -0400 | [diff] [blame] | 6821 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)}, |
hayeswang | d248caf | 2016-10-18 11:41:48 +0800 | [diff] [blame] | 6822 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)}, |
| 6823 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)}, |
Kai-Heng Feng | 9647722 | 2019-11-05 19:24:52 +0800 | [diff] [blame] | 6824 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3082)}, |
hayeswang | d248caf | 2016-10-18 11:41:48 +0800 | [diff] [blame] | 6825 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)}, |
| 6826 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)}, |
| 6827 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)}, |
Kazutoshi Noguchi | b306053 | 2019-10-21 00:03:07 +0900 | [diff] [blame] | 6828 | {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)}, |
Grant Grundler | 9084104 | 2017-09-28 11:35:00 -0700 | [diff] [blame] | 6829 | {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)}, |
Zheng Liu | d065c3c1 | 2015-07-07 13:54:12 -0700 | [diff] [blame] | 6830 | {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)}, |
Ran Wang | 9d11b06 | 2017-10-23 18:10:23 +0800 | [diff] [blame] | 6831 | {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)}, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6832 | {} |
| 6833 | }; |
| 6834 | |
| 6835 | MODULE_DEVICE_TABLE(usb, rtl8152_table); |
| 6836 | |
| 6837 | static struct usb_driver rtl8152_driver = { |
| 6838 | .name = MODULENAME, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6839 | .id_table = rtl8152_table, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6840 | .probe = rtl8152_probe, |
| 6841 | .disconnect = rtl8152_disconnect, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6842 | .suspend = rtl8152_suspend, |
hayeswang | ebc2ec48 | 2013-08-14 20:54:38 +0800 | [diff] [blame] | 6843 | .resume = rtl8152_resume, |
hayeswang | 7ec2541 | 2016-01-04 14:38:46 +0800 | [diff] [blame] | 6844 | .reset_resume = rtl8152_reset_resume, |
hayeswang | e501139 | 2015-07-29 20:39:08 +0800 | [diff] [blame] | 6845 | .pre_reset = rtl8152_pre_reset, |
| 6846 | .post_reset = rtl8152_post_reset, |
hayeswang | 9a4be1b | 2014-02-18 21:49:07 +0800 | [diff] [blame] | 6847 | .supports_autosuspend = 1, |
hayeswang | a634782 | 2014-02-18 21:49:10 +0800 | [diff] [blame] | 6848 | .disable_hub_initiated_lpm = 1, |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6849 | }; |
| 6850 | |
Sachin Kamat | b4236daa | 2013-05-16 17:48:08 +0000 | [diff] [blame] | 6851 | module_usb_driver(rtl8152_driver); |
hayeswang | ac718b6 | 2013-05-02 16:01:25 +0000 | [diff] [blame] | 6852 | |
| 6853 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 6854 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 6855 | MODULE_LICENSE("GPL"); |
Grant Grundler | c961e87 | 2016-07-14 11:27:16 -0700 | [diff] [blame] | 6856 | MODULE_VERSION(DRIVER_VERSION); |