1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * r8169.c: RealTek 8169/8168/8101 ethernet driver. 4 * 5 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw> 6 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com> 7 * Copyright (c) a lot of people too. Please respect their work. 8 * 9 * See MAINTAINERS file for support contact information. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/pci.h> 14 #include <linux/netdevice.h> 15 #include <linux/etherdevice.h> 16 #include <linux/clk.h> 17 #include <linux/delay.h> 18 #include <linux/ethtool.h> 19 #include <linux/phy.h> 20 #include <linux/if_vlan.h> 21 #include <linux/in.h> 22 #include <linux/io.h> 23 #include <linux/ip.h> 24 #include <linux/tcp.h> 25 #include <linux/interrupt.h> 26 #include <linux/dma-mapping.h> 27 #include <linux/pm_runtime.h> 28 #include <linux/bitfield.h> 29 #include <linux/prefetch.h> 30 #include <linux/ipv6.h> 31 #include <linux/unaligned.h> 32 #include <net/ip6_checksum.h> 33 #include <net/netdev_queues.h> 34 35 #include "r8169.h" 36 #include "r8169_firmware.h" 37 38 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw" 39 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw" 40 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw" 41 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw" 42 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw" 43 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw" 44 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw" 45 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw" 46 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw" 47 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw" 48 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw" 49 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw" 50 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw" 51 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw" 52 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw" 53 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw" 54 #define FIRMWARE_8168FP_3 "rtl_nic/rtl8168fp-3.fw" 55 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw" 56 #define FIRMWARE_8125A_3 "rtl_nic/rtl8125a-3.fw" 57 #define FIRMWARE_8125B_2 "rtl_nic/rtl8125b-2.fw" 58 #define FIRMWARE_8125D_1 "rtl_nic/rtl8125d-1.fw" 59 #define FIRMWARE_8125D_2 "rtl_nic/rtl8125d-2.fw" 60 #define FIRMWARE_8125K_1 "rtl_nic/rtl8125k-1.fw" 61 #define FIRMWARE_8125BP_2 "rtl_nic/rtl8125bp-2.fw" 62 #define FIRMWARE_9151A_1 "rtl_nic/rtl9151a-1.fw" 63 #define FIRMWARE_8126A_2 "rtl_nic/rtl8126a-2.fw" 64 #define FIRMWARE_8126A_3 "rtl_nic/rtl8126a-3.fw" 65 #define FIRMWARE_8127A_1 "rtl_nic/rtl8127a-1.fw" 66 67 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */ 68 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */ 69 70 #define R8169_REGS_SIZE 256 71 #define R8169_RX_BUF_SIZE (SZ_16K - 1) 72 #define NUM_TX_DESC 256 /* Number of Tx descriptor registers */ 73 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */ 74 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc)) 75 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc)) 76 #define R8169_TX_STOP_THRS (MAX_SKB_FRAGS + 1) 77 #define R8169_TX_START_THRS (2 * R8169_TX_STOP_THRS) 78 79 #define OCP_STD_PHY_BASE 0xa400 80 81 #define RTL_CFG_NO_GBIT 1 82 83 /* write/read MMIO register */ 84 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg)) 85 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg)) 86 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg)) 87 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg)) 88 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg)) 89 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg)) 90 91 #define JUMBO_4K (4 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN) 92 #define JUMBO_6K (6 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN) 93 #define JUMBO_7K (7 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN) 94 #define JUMBO_9K (9 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN) 95 #define JUMBO_16K (SZ_16K - VLAN_ETH_HLEN - ETH_FCS_LEN) 96 97 static const struct rtl_chip_info { 98 u16 mask; 99 u16 val; 100 enum mac_version mac_version; 101 const char *name; 102 const char *fw_name; 103 } rtl_chip_infos[] = { 104 /* 8127A family. */ 105 { 0x7cf, 0x6c9, RTL_GIGA_MAC_VER_80, "RTL8127A", FIRMWARE_8127A_1 }, 106 107 /* 8126A family. */ 108 { 0x7cf, 0x64a, RTL_GIGA_MAC_VER_70, "RTL8126A", FIRMWARE_8126A_3 }, 109 { 0x7cf, 0x649, RTL_GIGA_MAC_VER_70, "RTL8126A", FIRMWARE_8126A_2 }, 110 111 /* 8125BP family. */ 112 { 0x7cf, 0x681, RTL_GIGA_MAC_VER_66, "RTL8125BP", FIRMWARE_8125BP_2 }, 113 114 /* 8125D family. */ 115 { 0x7cf, 0x68b, RTL_GIGA_MAC_VER_64, "RTL9151A", FIRMWARE_9151A_1 }, 116 { 0x7cf, 0x68a, RTL_GIGA_MAC_VER_64, "RTL8125K", FIRMWARE_8125K_1 }, 117 { 0x7cf, 0x689, RTL_GIGA_MAC_VER_64, "RTL8125D", FIRMWARE_8125D_2 }, 118 { 0x7cf, 0x688, RTL_GIGA_MAC_VER_64, "RTL8125D", FIRMWARE_8125D_1 }, 119 120 /* 8125B family. */ 121 { 0x7cf, 0x641, RTL_GIGA_MAC_VER_63, "RTL8125B", FIRMWARE_8125B_2 }, 122 123 /* 8125A family. */ 124 { 0x7cf, 0x609, RTL_GIGA_MAC_VER_61, "RTL8125A", FIRMWARE_8125A_3 }, 125 126 /* RTL8117 */ 127 { 0x7cf, 0x54b, RTL_GIGA_MAC_VER_52, "RTL8168fp/RTL8117" }, 128 { 0x7cf, 0x54a, RTL_GIGA_MAC_VER_52, "RTL8168fp/RTL8117", 129 FIRMWARE_8168FP_3 }, 130 131 /* 8168EP family. */ 132 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51, "RTL8168ep/8111ep" }, 133 134 /* 8168H family. */ 135 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46, "RTL8168h/8111h", 136 FIRMWARE_8168H_2 }, 137 /* Realtek calls it RTL8168M, but it's handled like RTL8168H */ 138 { 0x7cf, 0x6c0, RTL_GIGA_MAC_VER_46, "RTL8168M", FIRMWARE_8168H_2 }, 139 140 /* 8168G family. */ 141 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44, "RTL8411b", FIRMWARE_8411_2 }, 142 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42, "RTL8168gu/8111gu", 143 FIRMWARE_8168G_3 }, 144 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40, "RTL8168g/8111g", 145 FIRMWARE_8168G_2 }, 146 147 /* 8168F family. */ 148 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38, "RTL8411", FIRMWARE_8411_1 }, 149 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36, "RTL8168f/8111f", 150 FIRMWARE_8168F_2 }, 151 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35, "RTL8168f/8111f", 152 FIRMWARE_8168F_1 }, 153 154 /* 8168E family. */ 155 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34, "RTL8168evl/8111evl", 156 FIRMWARE_8168E_3 }, 157 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32, "RTL8168e/8111e", 158 FIRMWARE_8168E_1 }, 159 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33, "RTL8168e/8111e", 160 FIRMWARE_8168E_2 }, 161 162 /* 8168D family. */ 163 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25, "RTL8168d/8111d", 164 FIRMWARE_8168D_1 }, 165 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26, "RTL8168d/8111d", 166 FIRMWARE_8168D_2 }, 167 168 /* 8168DP family. */ 169 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28, "RTL8168dp/8111dp" }, 170 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31, "RTL8168dp/8111dp" }, 171 172 /* 8168C family. */ 173 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23, "RTL8168cp/8111cp" }, 174 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18, "RTL8168cp/8111cp" }, 175 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24, "RTL8168cp/8111cp" }, 176 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19, "RTL8168c/8111c" }, 177 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20, "RTL8168c/8111c" }, 178 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21, "RTL8168c/8111c" }, 179 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22, "RTL8168c/8111c" }, 180 181 /* 8168B family. */ 182 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17, "RTL8168b/8111b" }, 183 /* This one is very old and rare, support has been removed. 184 * { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11, "RTL8168b/8111b" }, 185 */ 186 187 /* 8101 family. */ 188 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39, "RTL8106e", FIRMWARE_8106E_1 }, 189 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37, "RTL8402", FIRMWARE_8402_1 }, 190 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29, "RTL8105e", FIRMWARE_8105E_1 }, 191 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30, "RTL8105e", FIRMWARE_8105E_1 }, 192 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08, "RTL8102e" }, 193 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08, "RTL8102e" }, 194 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07, "RTL8102e" }, 195 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07, "RTL8102e" }, 196 { 0x7cf, 0x240, RTL_GIGA_MAC_VER_14, "RTL8401" }, 197 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09, "RTL8102e/RTL8103e" }, 198 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09, "RTL8102e/RTL8103e" }, 199 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_10, "RTL8101e/RTL8100e" }, 200 201 /* 8110 family. */ 202 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06, "RTL8169sc/8110sc" }, 203 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05, "RTL8169sc/8110sc" }, 204 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04, "RTL8169sb/8110sb" }, 205 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03, "RTL8110s" }, 206 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02, "RTL8169s" }, 207 208 /* Catch-all */ 209 { 0x000, 0x000, RTL_GIGA_MAC_NONE } 210 }; 211 212 static const struct pci_device_id rtl8169_pci_tbl[] = { 213 { PCI_VDEVICE(REALTEK, 0x2502) }, 214 { PCI_VDEVICE(REALTEK, 0x2600) }, 215 { PCI_VDEVICE(REALTEK, 0x8129) }, 216 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT }, 217 { PCI_VDEVICE(REALTEK, 0x8161) }, 218 { PCI_VDEVICE(REALTEK, 0x8162) }, 219 { PCI_VDEVICE(REALTEK, 0x8167) }, 220 { PCI_VDEVICE(REALTEK, 0x8168) }, 221 { PCI_VDEVICE(NCUBE, 0x8168) }, 222 { PCI_VDEVICE(REALTEK, 0x8169) }, 223 { PCI_VDEVICE(DLINK, 0x4300) }, 224 { PCI_VDEVICE(DLINK, 0x4302) }, 225 { PCI_VDEVICE(AT, 0xc107) }, 226 { PCI_VDEVICE(USR, 0x0116) }, 227 { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 }, 228 { 0x0001, 0x8168, PCI_ANY_ID, 0x2410 }, 229 { PCI_VDEVICE(REALTEK, 0x8125) }, 230 { PCI_VDEVICE(REALTEK, 0x8126) }, 231 { PCI_VDEVICE(REALTEK, 0x8127) }, 232 { PCI_VDEVICE(REALTEK, 0x3000) }, 233 { PCI_VDEVICE(REALTEK, 0x5000) }, 234 { PCI_VDEVICE(REALTEK, 0x0e10) }, 235 {} 236 }; 237 238 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl); 239 240 enum rtl_registers { 241 MAC0 = 0, /* Ethernet hardware address. */ 242 MAC4 = 4, 243 MAR0 = 8, /* Multicast filter. */ 244 CounterAddrLow = 0x10, 245 CounterAddrHigh = 0x14, 246 TxDescStartAddrLow = 0x20, 247 TxDescStartAddrHigh = 0x24, 248 TxHDescStartAddrLow = 0x28, 249 TxHDescStartAddrHigh = 0x2c, 250 FLASH = 0x30, 251 ERSR = 0x36, 252 ChipCmd = 0x37, 253 TxPoll = 0x38, 254 IntrMask = 0x3c, 255 IntrStatus = 0x3e, 256 257 TxConfig = 0x40, 258 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */ 259 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */ 260 261 RxConfig = 0x44, 262 #define RX128_INT_EN (1 << 15) /* 8111c and later */ 263 #define RX_MULTI_EN (1 << 14) /* 8111c only */ 264 #define RXCFG_FIFO_SHIFT 13 265 /* No threshold before first PCI xfer */ 266 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT) 267 #define RX_EARLY_OFF (1 << 11) 268 #define RX_PAUSE_SLOT_ON (1 << 11) /* 8125b and later */ 269 #define RXCFG_DMA_SHIFT 8 270 /* Unlimited maximum PCI burst. */ 271 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT) 272 273 Cfg9346 = 0x50, 274 Config0 = 0x51, 275 Config1 = 0x52, 276 Config2 = 0x53, 277 #define PME_SIGNAL (1 << 5) /* 8168c and later */ 278 279 Config3 = 0x54, 280 Config4 = 0x55, 281 Config5 = 0x56, 282 PHYAR = 0x60, 283 PHYstatus = 0x6c, 284 RxMaxSize = 0xda, 285 CPlusCmd = 0xe0, 286 IntrMitigate = 0xe2, 287 288 #define RTL_COALESCE_TX_USECS GENMASK(15, 12) 289 #define RTL_COALESCE_TX_FRAMES GENMASK(11, 8) 290 #define RTL_COALESCE_RX_USECS GENMASK(7, 4) 291 #define RTL_COALESCE_RX_FRAMES GENMASK(3, 0) 292 293 #define RTL_COALESCE_T_MAX 0x0fU 294 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_T_MAX * 4) 295 296 RxDescAddrLow = 0xe4, 297 RxDescAddrHigh = 0xe8, 298 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */ 299 300 #define NoEarlyTx 0x3f /* Max value : no early transmit. */ 301 302 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */ 303 304 #define TxPacketMax (8064 >> 7) 305 #define EarlySize 0x27 306 307 FuncEvent = 0xf0, 308 FuncEventMask = 0xf4, 309 FuncPresetState = 0xf8, 310 IBCR0 = 0xf8, 311 IBCR2 = 0xf9, 312 IBIMR0 = 0xfa, 313 IBISR0 = 0xfb, 314 FuncForceEvent = 0xfc, 315 }; 316 317 enum rtl8168_8101_registers { 318 CSIDR = 0x64, 319 CSIAR = 0x68, 320 #define CSIAR_FLAG 0x80000000 321 #define CSIAR_WRITE_CMD 0x80000000 322 #define CSIAR_BYTE_ENABLE 0x0000f000 323 #define CSIAR_ADDR_MASK 0x00000fff 324 PMCH = 0x6f, 325 #define D3COLD_NO_PLL_DOWN BIT(7) 326 #define D3HOT_NO_PLL_DOWN BIT(6) 327 #define D3_NO_PLL_DOWN (BIT(7) | BIT(6)) 328 EPHYAR = 0x80, 329 #define EPHYAR_FLAG 0x80000000 330 #define EPHYAR_WRITE_CMD 0x80000000 331 #define EPHYAR_REG_MASK 0x1f 332 #define EPHYAR_REG_SHIFT 16 333 #define EPHYAR_DATA_MASK 0xffff 334 DLLPR = 0xd0, 335 #define PFM_EN (1 << 6) 336 #define TX_10M_PS_EN (1 << 7) 337 DBG_REG = 0xd1, 338 #define FIX_NAK_1 (1 << 4) 339 #define FIX_NAK_2 (1 << 3) 340 TWSI = 0xd2, 341 MCU = 0xd3, 342 #define NOW_IS_OOB (1 << 7) 343 #define TX_EMPTY (1 << 5) 344 #define RX_EMPTY (1 << 4) 345 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY) 346 #define EN_NDP (1 << 3) 347 #define EN_OOB_RESET (1 << 2) 348 #define LINK_LIST_RDY (1 << 1) 349 EFUSEAR = 0xdc, 350 #define EFUSEAR_FLAG 0x80000000 351 #define EFUSEAR_WRITE_CMD 0x80000000 352 #define EFUSEAR_READ_CMD 0x00000000 353 #define EFUSEAR_REG_MASK 0x03ff 354 #define EFUSEAR_REG_SHIFT 8 355 #define EFUSEAR_DATA_MASK 0xff 356 MISC_1 = 0xf2, 357 #define PFM_D3COLD_EN (1 << 6) 358 }; 359 360 enum rtl8168_registers { 361 LED_CTRL = 0x18, 362 LED_FREQ = 0x1a, 363 EEE_LED = 0x1b, 364 ERIDR = 0x70, 365 ERIAR = 0x74, 366 #define ERIAR_FLAG 0x80000000 367 #define ERIAR_WRITE_CMD 0x80000000 368 #define ERIAR_READ_CMD 0x00000000 369 #define ERIAR_ADDR_BYTE_ALIGN 4 370 #define ERIAR_TYPE_SHIFT 16 371 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT) 372 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT) 373 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT) 374 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT) 375 #define ERIAR_MASK_SHIFT 12 376 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT) 377 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT) 378 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT) 379 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT) 380 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT) 381 EPHY_RXER_NUM = 0x7c, 382 OCPDR = 0xb0, /* OCP GPHY access */ 383 #define OCPDR_WRITE_CMD 0x80000000 384 #define OCPDR_READ_CMD 0x00000000 385 #define OCPDR_REG_MASK 0x7f 386 #define OCPDR_GPHY_REG_SHIFT 16 387 #define OCPDR_DATA_MASK 0xffff 388 OCPAR = 0xb4, 389 #define OCPAR_FLAG 0x80000000 390 #define OCPAR_GPHY_WRITE_CMD 0x8000f060 391 #define OCPAR_GPHY_READ_CMD 0x0000f060 392 GPHY_OCP = 0xb8, 393 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */ 394 MISC = 0xf0, /* 8168e only. */ 395 #define TXPLA_RST (1 << 29) 396 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */ 397 #define PWM_EN (1 << 22) 398 #define RXDV_GATED_EN (1 << 19) 399 #define EARLY_TALLY_EN (1 << 16) 400 }; 401 402 enum rtl8125_registers { 403 LEDSEL0 = 0x18, 404 INT_CFG0_8125 = 0x34, 405 #define INT_CFG0_ENABLE_8125 BIT(0) 406 #define INT_CFG0_CLKREQEN BIT(3) 407 IntrMask_8125 = 0x38, 408 IntrStatus_8125 = 0x3c, 409 INT_CFG1_8125 = 0x7a, 410 LEDSEL2 = 0x84, 411 LEDSEL1 = 0x86, 412 TxPoll_8125 = 0x90, 413 LEDSEL3 = 0x96, 414 MAC0_BKP = 0x19e0, 415 RSS_CTRL_8125 = 0x4500, 416 Q_NUM_CTRL_8125 = 0x4800, 417 EEE_TXIDLE_TIMER_8125 = 0x6048, 418 }; 419 420 #define LEDSEL_MASK_8125 0x23f 421 422 #define RX_VLAN_INNER_8125 BIT(22) 423 #define RX_VLAN_OUTER_8125 BIT(23) 424 #define RX_VLAN_8125 (RX_VLAN_INNER_8125 | RX_VLAN_OUTER_8125) 425 426 #define RX_FETCH_DFLT_8125 (8 << 27) 427 428 enum rtl_register_content { 429 /* InterruptStatusBits */ 430 SYSErr = 0x8000, 431 PCSTimeout = 0x4000, 432 SWInt = 0x0100, 433 TxDescUnavail = 0x0080, 434 RxFIFOOver = 0x0040, 435 LinkChg = 0x0020, 436 RxOverflow = 0x0010, 437 TxErr = 0x0008, 438 TxOK = 0x0004, 439 RxErr = 0x0002, 440 RxOK = 0x0001, 441 442 /* RxStatusDesc */ 443 RxRWT = (1 << 22), 444 RxRES = (1 << 21), 445 RxRUNT = (1 << 20), 446 RxCRC = (1 << 19), 447 448 /* ChipCmdBits */ 449 StopReq = 0x80, 450 CmdReset = 0x10, 451 CmdRxEnb = 0x08, 452 CmdTxEnb = 0x04, 453 RxBufEmpty = 0x01, 454 455 /* TXPoll register p.5 */ 456 HPQ = 0x80, /* Poll cmd on the high prio queue */ 457 NPQ = 0x40, /* Poll cmd on the low prio queue */ 458 FSWInt = 0x01, /* Forced software interrupt */ 459 460 /* Cfg9346Bits */ 461 Cfg9346_Lock = 0x00, 462 Cfg9346_Unlock = 0xc0, 463 464 /* rx_mode_bits */ 465 AcceptErr = 0x20, 466 AcceptRunt = 0x10, 467 #define RX_CONFIG_ACCEPT_ERR_MASK 0x30 468 AcceptBroadcast = 0x08, 469 AcceptMulticast = 0x04, 470 AcceptMyPhys = 0x02, 471 AcceptAllPhys = 0x01, 472 #define RX_CONFIG_ACCEPT_OK_MASK 0x0f 473 #define RX_CONFIG_ACCEPT_MASK 0x3f 474 475 /* TxConfigBits */ 476 TxInterFrameGapShift = 24, 477 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */ 478 479 /* Config1 register p.24 */ 480 LEDS1 = (1 << 7), 481 LEDS0 = (1 << 6), 482 Speed_down = (1 << 4), 483 MEMMAP = (1 << 3), 484 IOMAP = (1 << 2), 485 VPD = (1 << 1), 486 PMEnable = (1 << 0), /* Power Management Enable */ 487 488 /* Config2 register p. 25 */ 489 ClkReqEn = (1 << 7), /* Clock Request Enable */ 490 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */ 491 PCI_Clock_66MHz = 0x01, 492 PCI_Clock_33MHz = 0x00, 493 494 /* Config3 register p.25 */ 495 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */ 496 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */ 497 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */ 498 Rdy_to_L23 = (1 << 1), /* L23 Enable */ 499 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */ 500 501 /* Config4 register */ 502 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */ 503 504 /* Config5 register p.27 */ 505 BWF = (1 << 6), /* Accept Broadcast wakeup frame */ 506 MWF = (1 << 5), /* Accept Multicast wakeup frame */ 507 UWF = (1 << 4), /* Accept Unicast wakeup frame */ 508 Spi_en = (1 << 3), 509 LanWake = (1 << 1), /* LanWake enable/disable */ 510 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */ 511 ASPM_en = (1 << 0), /* ASPM enable */ 512 513 /* CPlusCmd p.31 */ 514 EnableBist = (1 << 15), // 8168 8101 515 Mac_dbgo_oe = (1 << 14), // 8168 8101 516 EnAnaPLL = (1 << 14), // 8169 517 Normal_mode = (1 << 13), // unused 518 Force_half_dup = (1 << 12), // 8168 8101 519 Force_rxflow_en = (1 << 11), // 8168 8101 520 Force_txflow_en = (1 << 10), // 8168 8101 521 Cxpl_dbg_sel = (1 << 9), // 8168 8101 522 ASF = (1 << 8), // 8168 8101 523 PktCntrDisable = (1 << 7), // 8168 8101 524 Mac_dbgo_sel = 0x001c, // 8168 525 RxVlan = (1 << 6), 526 RxChkSum = (1 << 5), 527 PCIDAC = (1 << 4), 528 PCIMulRW = (1 << 3), 529 #define INTT_MASK GENMASK(1, 0) 530 #define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK) 531 532 /* rtl8169_PHYstatus */ 533 TBI_Enable = 0x80, 534 TxFlowCtrl = 0x40, 535 RxFlowCtrl = 0x20, 536 _1000bpsF = 0x10, 537 _100bps = 0x08, 538 _10bps = 0x04, 539 LinkStatus = 0x02, 540 FullDup = 0x01, 541 542 /* ResetCounterCommand */ 543 CounterReset = 0x1, 544 545 /* DumpCounterCommand */ 546 CounterDump = 0x8, 547 548 /* magic enable v2 */ 549 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */ 550 }; 551 552 enum rtl_desc_bit { 553 /* First doubleword. */ 554 DescOwn = (1 << 31), /* Descriptor is owned by NIC */ 555 RingEnd = (1 << 30), /* End of descriptor ring */ 556 FirstFrag = (1 << 29), /* First segment of a packet */ 557 LastFrag = (1 << 28), /* Final segment of a packet */ 558 }; 559 560 /* Generic case. */ 561 enum rtl_tx_desc_bit { 562 /* First doubleword. */ 563 TD_LSO = (1 << 27), /* Large Send Offload */ 564 #define TD_MSS_MAX 0x07ffu /* MSS value */ 565 566 /* Second doubleword. */ 567 TxVlanTag = (1 << 17), /* Add VLAN tag */ 568 }; 569 570 /* 8169, 8168b and 810x except 8102e. */ 571 enum rtl_tx_desc_bit_0 { 572 /* First doubleword. */ 573 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */ 574 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */ 575 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */ 576 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */ 577 }; 578 579 /* 8102e, 8168c and beyond. */ 580 enum rtl_tx_desc_bit_1 { 581 /* First doubleword. */ 582 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */ 583 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */ 584 #define GTTCPHO_SHIFT 18 585 #define GTTCPHO_MAX 0x7f 586 587 /* Second doubleword. */ 588 #define TCPHO_SHIFT 18 589 #define TCPHO_MAX 0x3ff 590 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */ 591 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */ 592 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */ 593 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */ 594 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */ 595 }; 596 597 enum rtl_rx_desc_bit { 598 /* Rx private */ 599 PID1 = (1 << 18), /* Protocol ID bit 1/2 */ 600 PID0 = (1 << 17), /* Protocol ID bit 0/2 */ 601 602 #define RxProtoUDP (PID1) 603 #define RxProtoTCP (PID0) 604 #define RxProtoIP (PID1 | PID0) 605 #define RxProtoMask RxProtoIP 606 607 IPFail = (1 << 16), /* IP checksum failed */ 608 UDPFail = (1 << 15), /* UDP/IP checksum failed */ 609 TCPFail = (1 << 14), /* TCP/IP checksum failed */ 610 611 #define RxCSFailMask (IPFail | UDPFail | TCPFail) 612 613 RxVlanTag = (1 << 16), /* VLAN tag available */ 614 }; 615 616 #define RTL_GSO_MAX_SIZE_V1 32000 617 #define RTL_GSO_MAX_SEGS_V1 24 618 #define RTL_GSO_MAX_SIZE_V2 64000 619 #define RTL_GSO_MAX_SEGS_V2 64 620 621 struct TxDesc { 622 __le32 opts1; 623 __le32 opts2; 624 __le64 addr; 625 }; 626 627 struct RxDesc { 628 __le32 opts1; 629 __le32 opts2; 630 __le64 addr; 631 }; 632 633 struct ring_info { 634 struct sk_buff *skb; 635 u32 len; 636 }; 637 638 struct rtl8169_counters { 639 __le64 tx_packets; 640 __le64 rx_packets; 641 __le64 tx_errors; 642 __le32 rx_errors; 643 __le16 rx_missed; 644 __le16 align_errors; 645 __le32 tx_one_collision; 646 __le32 tx_multi_collision; 647 __le64 rx_unicast; 648 __le64 rx_broadcast; 649 __le32 rx_multicast; 650 __le16 tx_aborted; 651 __le16 tx_underrun; 652 /* new since RTL8125 */ 653 __le64 tx_octets; 654 __le64 rx_octets; 655 __le64 rx_multicast64; 656 __le64 tx_unicast64; 657 __le64 tx_broadcast64; 658 __le64 tx_multicast64; 659 __le32 tx_pause_on; 660 __le32 tx_pause_off; 661 __le32 tx_pause_all; 662 __le32 tx_deferred; 663 __le32 tx_late_collision; 664 __le32 tx_all_collision; 665 __le32 tx_aborted32; 666 __le32 align_errors32; 667 __le32 rx_frame_too_long; 668 __le32 rx_runt; 669 __le32 rx_pause_on; 670 __le32 rx_pause_off; 671 __le32 rx_pause_all; 672 __le32 rx_unknown_opcode; 673 __le32 rx_mac_error; 674 __le32 tx_underrun32; 675 __le32 rx_mac_missed; 676 __le32 rx_tcam_dropped; 677 __le32 tdu; 678 __le32 rdu; 679 }; 680 681 struct rtl8169_tc_offsets { 682 bool inited; 683 __le64 tx_errors; 684 __le32 tx_multi_collision; 685 __le16 tx_aborted; 686 __le16 rx_missed; 687 }; 688 689 enum rtl_flag { 690 RTL_FLAG_TASK_RESET_PENDING, 691 RTL_FLAG_TASK_TX_TIMEOUT, 692 RTL_FLAG_MAX 693 }; 694 695 enum rtl_dash_type { 696 RTL_DASH_NONE, 697 RTL_DASH_DP, 698 RTL_DASH_EP, 699 RTL_DASH_25_BP, 700 }; 701 702 struct rtl8169_private { 703 void __iomem *mmio_addr; /* memory map physical address */ 704 struct pci_dev *pci_dev; 705 struct net_device *dev; 706 struct phy_device *phydev; 707 struct napi_struct napi; 708 enum mac_version mac_version; 709 enum rtl_dash_type dash_type; 710 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */ 711 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */ 712 u32 dirty_tx; 713 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */ 714 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */ 715 dma_addr_t TxPhyAddr; 716 dma_addr_t RxPhyAddr; 717 struct page *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */ 718 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */ 719 u16 cp_cmd; 720 u16 tx_lpi_timer; 721 u32 irq_mask; 722 int irq; 723 struct clk *clk; 724 725 struct { 726 DECLARE_BITMAP(flags, RTL_FLAG_MAX); 727 struct work_struct work; 728 } wk; 729 730 raw_spinlock_t mac_ocp_lock; 731 struct mutex led_lock; /* serialize LED ctrl RMW access */ 732 733 unsigned supports_gmii:1; 734 unsigned aspm_manageable:1; 735 unsigned dash_enabled:1; 736 dma_addr_t counters_phys_addr; 737 struct rtl8169_counters *counters; 738 struct rtl8169_tc_offsets tc_offset; 739 u32 saved_wolopts; 740 741 const char *fw_name; 742 struct rtl_fw *rtl_fw; 743 744 struct r8169_led_classdev *leds; 745 746 u32 ocp_base; 747 }; 748 749 typedef void (*rtl_generic_fct)(struct rtl8169_private *tp); 750 751 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>"); 752 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver"); 753 MODULE_SOFTDEP("pre: realtek"); 754 MODULE_LICENSE("GPL"); 755 MODULE_FIRMWARE(FIRMWARE_8168D_1); 756 MODULE_FIRMWARE(FIRMWARE_8168D_2); 757 MODULE_FIRMWARE(FIRMWARE_8168E_1); 758 MODULE_FIRMWARE(FIRMWARE_8168E_2); 759 MODULE_FIRMWARE(FIRMWARE_8168E_3); 760 MODULE_FIRMWARE(FIRMWARE_8105E_1); 761 MODULE_FIRMWARE(FIRMWARE_8168F_1); 762 MODULE_FIRMWARE(FIRMWARE_8168F_2); 763 MODULE_FIRMWARE(FIRMWARE_8402_1); 764 MODULE_FIRMWARE(FIRMWARE_8411_1); 765 MODULE_FIRMWARE(FIRMWARE_8411_2); 766 MODULE_FIRMWARE(FIRMWARE_8106E_1); 767 MODULE_FIRMWARE(FIRMWARE_8106E_2); 768 MODULE_FIRMWARE(FIRMWARE_8168G_2); 769 MODULE_FIRMWARE(FIRMWARE_8168G_3); 770 MODULE_FIRMWARE(FIRMWARE_8168H_2); 771 MODULE_FIRMWARE(FIRMWARE_8168FP_3); 772 MODULE_FIRMWARE(FIRMWARE_8107E_2); 773 MODULE_FIRMWARE(FIRMWARE_8125A_3); 774 MODULE_FIRMWARE(FIRMWARE_8125B_2); 775 MODULE_FIRMWARE(FIRMWARE_8125D_1); 776 MODULE_FIRMWARE(FIRMWARE_8125D_2); 777 MODULE_FIRMWARE(FIRMWARE_8125K_1); 778 MODULE_FIRMWARE(FIRMWARE_8125BP_2); 779 MODULE_FIRMWARE(FIRMWARE_9151A_1); 780 MODULE_FIRMWARE(FIRMWARE_8126A_2); 781 MODULE_FIRMWARE(FIRMWARE_8126A_3); 782 MODULE_FIRMWARE(FIRMWARE_8127A_1); 783 784 static inline struct device *tp_to_dev(struct rtl8169_private *tp) 785 { 786 return &tp->pci_dev->dev; 787 } 788 789 static void rtl_lock_config_regs(struct rtl8169_private *tp) 790 { 791 RTL_W8(tp, Cfg9346, Cfg9346_Lock); 792 } 793 794 static void rtl_unlock_config_regs(struct rtl8169_private *tp) 795 { 796 RTL_W8(tp, Cfg9346, Cfg9346_Unlock); 797 } 798 799 static void rtl_pci_commit(struct rtl8169_private *tp) 800 { 801 /* Read an arbitrary register to commit a preceding PCI write */ 802 RTL_R8(tp, ChipCmd); 803 } 804 805 static void rtl_mod_config2(struct rtl8169_private *tp, u8 clear, u8 set) 806 { 807 u8 val; 808 809 val = RTL_R8(tp, Config2); 810 RTL_W8(tp, Config2, (val & ~clear) | set); 811 } 812 813 static void rtl_mod_config5(struct rtl8169_private *tp, u8 clear, u8 set) 814 { 815 u8 val; 816 817 val = RTL_R8(tp, Config5); 818 RTL_W8(tp, Config5, (val & ~clear) | set); 819 } 820 821 static void r8169_mod_reg8_cond(struct rtl8169_private *tp, int reg, 822 u8 bits, bool cond) 823 { 824 u8 val, old_val; 825 826 old_val = RTL_R8(tp, reg); 827 if (cond) 828 val = old_val | bits; 829 else 830 val = old_val & ~bits; 831 if (val != old_val) 832 RTL_W8(tp, reg, val); 833 } 834 835 static bool rtl_is_8125(struct rtl8169_private *tp) 836 { 837 return tp->mac_version >= RTL_GIGA_MAC_VER_61; 838 } 839 840 static bool rtl_is_8168evl_up(struct rtl8169_private *tp) 841 { 842 return tp->mac_version >= RTL_GIGA_MAC_VER_34 && 843 tp->mac_version != RTL_GIGA_MAC_VER_39 && 844 tp->mac_version <= RTL_GIGA_MAC_VER_52; 845 } 846 847 static bool rtl_supports_eee(struct rtl8169_private *tp) 848 { 849 return tp->mac_version >= RTL_GIGA_MAC_VER_34 && 850 tp->mac_version != RTL_GIGA_MAC_VER_37 && 851 tp->mac_version != RTL_GIGA_MAC_VER_39; 852 } 853 854 static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg) 855 { 856 int i; 857 858 for (i = 0; i < ETH_ALEN; i++) 859 mac[i] = RTL_R8(tp, reg + i); 860 } 861 862 struct rtl_cond { 863 bool (*check)(struct rtl8169_private *); 864 const char *msg; 865 }; 866 867 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c, 868 unsigned long usecs, int n, bool high) 869 { 870 int i; 871 872 for (i = 0; i < n; i++) { 873 if (c->check(tp) == high) 874 return true; 875 fsleep(usecs); 876 } 877 878 if (net_ratelimit()) 879 netdev_err(tp->dev, "%s == %d (loop: %d, delay: %lu).\n", 880 c->msg, !high, n, usecs); 881 return false; 882 } 883 884 static bool rtl_loop_wait_high(struct rtl8169_private *tp, 885 const struct rtl_cond *c, 886 unsigned long d, int n) 887 { 888 return rtl_loop_wait(tp, c, d, n, true); 889 } 890 891 static bool rtl_loop_wait_low(struct rtl8169_private *tp, 892 const struct rtl_cond *c, 893 unsigned long d, int n) 894 { 895 return rtl_loop_wait(tp, c, d, n, false); 896 } 897 898 #define DECLARE_RTL_COND(name) \ 899 static bool name ## _check(struct rtl8169_private *); \ 900 \ 901 static const struct rtl_cond name = { \ 902 .check = name ## _check, \ 903 .msg = #name \ 904 }; \ 905 \ 906 static bool name ## _check(struct rtl8169_private *tp) 907 908 int rtl8168_led_mod_ctrl(struct rtl8169_private *tp, u16 mask, u16 val) 909 { 910 struct device *dev = tp_to_dev(tp); 911 int ret; 912 913 ret = pm_runtime_resume_and_get(dev); 914 if (ret < 0) 915 return ret; 916 917 mutex_lock(&tp->led_lock); 918 RTL_W16(tp, LED_CTRL, (RTL_R16(tp, LED_CTRL) & ~mask) | val); 919 mutex_unlock(&tp->led_lock); 920 921 pm_runtime_put_sync(dev); 922 923 return 0; 924 } 925 926 int rtl8168_get_led_mode(struct rtl8169_private *tp) 927 { 928 struct device *dev = tp_to_dev(tp); 929 int ret; 930 931 ret = pm_runtime_resume_and_get(dev); 932 if (ret < 0) 933 return ret; 934 935 ret = RTL_R16(tp, LED_CTRL); 936 937 pm_runtime_put_sync(dev); 938 939 return ret; 940 } 941 942 static int rtl8125_get_led_reg(int index) 943 { 944 static const int led_regs[] = { LEDSEL0, LEDSEL1, LEDSEL2, LEDSEL3 }; 945 946 return led_regs[index]; 947 } 948 949 int rtl8125_set_led_mode(struct rtl8169_private *tp, int index, u16 mode) 950 { 951 int reg = rtl8125_get_led_reg(index); 952 struct device *dev = tp_to_dev(tp); 953 int ret; 954 u16 val; 955 956 ret = pm_runtime_resume_and_get(dev); 957 if (ret < 0) 958 return ret; 959 960 mutex_lock(&tp->led_lock); 961 val = RTL_R16(tp, reg) & ~LEDSEL_MASK_8125; 962 RTL_W16(tp, reg, val | mode); 963 mutex_unlock(&tp->led_lock); 964 965 pm_runtime_put_sync(dev); 966 967 return 0; 968 } 969 970 int rtl8125_get_led_mode(struct rtl8169_private *tp, int index) 971 { 972 int reg = rtl8125_get_led_reg(index); 973 struct device *dev = tp_to_dev(tp); 974 int ret; 975 976 ret = pm_runtime_resume_and_get(dev); 977 if (ret < 0) 978 return ret; 979 980 ret = RTL_R16(tp, reg); 981 982 pm_runtime_put_sync(dev); 983 984 return ret; 985 } 986 987 void r8169_get_led_name(struct rtl8169_private *tp, int idx, 988 char *buf, int buf_len) 989 { 990 struct pci_dev *pdev = tp->pci_dev; 991 char pdom[8], pfun[8]; 992 int domain; 993 994 domain = pci_domain_nr(pdev->bus); 995 if (domain) 996 snprintf(pdom, sizeof(pdom), "P%d", domain); 997 else 998 pdom[0] = '\0'; 999 1000 if (pdev->multifunction) 1001 snprintf(pfun, sizeof(pfun), "f%d", PCI_FUNC(pdev->devfn)); 1002 else 1003 pfun[0] = '\0'; 1004 1005 snprintf(buf, buf_len, "en%sp%ds%d%s-%d::lan", pdom, pdev->bus->number, 1006 PCI_SLOT(pdev->devfn), pfun, idx); 1007 } 1008 1009 static void r8168fp_adjust_ocp_cmd(struct rtl8169_private *tp, u32 *cmd, int type) 1010 { 1011 /* based on RTL8168FP_OOBMAC_BASE in vendor driver */ 1012 if (type == ERIAR_OOB && tp->mac_version == RTL_GIGA_MAC_VER_52) 1013 *cmd |= 0xf70 << 18; 1014 } 1015 1016 DECLARE_RTL_COND(rtl_eriar_cond) 1017 { 1018 return RTL_R32(tp, ERIAR) & ERIAR_FLAG; 1019 } 1020 1021 static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask, 1022 u32 val, int type) 1023 { 1024 u32 cmd = ERIAR_WRITE_CMD | type | mask | addr; 1025 1026 if (WARN(addr & 3 || !mask, "addr: 0x%x, mask: 0x%08x\n", addr, mask)) 1027 return; 1028 1029 RTL_W32(tp, ERIDR, val); 1030 r8168fp_adjust_ocp_cmd(tp, &cmd, type); 1031 RTL_W32(tp, ERIAR, cmd); 1032 1033 rtl_loop_wait_low(tp, &rtl_eriar_cond, 100, 100); 1034 } 1035 1036 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask, 1037 u32 val) 1038 { 1039 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC); 1040 } 1041 1042 static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type) 1043 { 1044 u32 cmd = ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr; 1045 1046 r8168fp_adjust_ocp_cmd(tp, &cmd, type); 1047 RTL_W32(tp, ERIAR, cmd); 1048 1049 return rtl_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ? 1050 RTL_R32(tp, ERIDR) : ~0; 1051 } 1052 1053 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr) 1054 { 1055 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC); 1056 } 1057 1058 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 p, u32 m) 1059 { 1060 u32 val = rtl_eri_read(tp, addr); 1061 1062 rtl_eri_write(tp, addr, ERIAR_MASK_1111, (val & ~m) | p); 1063 } 1064 1065 static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 p) 1066 { 1067 rtl_w0w1_eri(tp, addr, p, 0); 1068 } 1069 1070 static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 m) 1071 { 1072 rtl_w0w1_eri(tp, addr, 0, m); 1073 } 1074 1075 static bool rtl_ocp_reg_failure(u32 reg) 1076 { 1077 return WARN_ONCE(reg & 0xffff0001, "Invalid ocp reg %x!\n", reg); 1078 } 1079 1080 DECLARE_RTL_COND(rtl_ocp_gphy_cond) 1081 { 1082 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG; 1083 } 1084 1085 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data) 1086 { 1087 if (rtl_ocp_reg_failure(reg)) 1088 return; 1089 1090 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data); 1091 1092 rtl_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10); 1093 } 1094 1095 static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg) 1096 { 1097 if (rtl_ocp_reg_failure(reg)) 1098 return 0; 1099 1100 RTL_W32(tp, GPHY_OCP, reg << 15); 1101 1102 return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ? 1103 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT; 1104 } 1105 1106 static void __r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data) 1107 { 1108 if (rtl_ocp_reg_failure(reg)) 1109 return; 1110 1111 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data); 1112 } 1113 1114 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data) 1115 { 1116 unsigned long flags; 1117 1118 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); 1119 __r8168_mac_ocp_write(tp, reg, data); 1120 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); 1121 } 1122 1123 static u16 __r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg) 1124 { 1125 if (rtl_ocp_reg_failure(reg)) 1126 return 0; 1127 1128 RTL_W32(tp, OCPDR, reg << 15); 1129 1130 return RTL_R32(tp, OCPDR); 1131 } 1132 1133 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg) 1134 { 1135 unsigned long flags; 1136 u16 val; 1137 1138 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); 1139 val = __r8168_mac_ocp_read(tp, reg); 1140 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); 1141 1142 return val; 1143 } 1144 1145 static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask, 1146 u16 set) 1147 { 1148 unsigned long flags; 1149 u16 data; 1150 1151 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); 1152 data = __r8168_mac_ocp_read(tp, reg); 1153 __r8168_mac_ocp_write(tp, reg, (data & ~mask) | set); 1154 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); 1155 } 1156 1157 /* Work around a hw issue with RTL8168g PHY, the quirk disables 1158 * PHY MCU interrupts before PHY power-down. 1159 */ 1160 static void rtl8168g_phy_suspend_quirk(struct rtl8169_private *tp, int value) 1161 { 1162 switch (tp->mac_version) { 1163 case RTL_GIGA_MAC_VER_40: 1164 if (value & BMCR_RESET || !(value & BMCR_PDOWN)) 1165 rtl_eri_set_bits(tp, 0x1a8, 0xfc000000); 1166 else 1167 rtl_eri_clear_bits(tp, 0x1a8, 0xfc000000); 1168 break; 1169 default: 1170 break; 1171 } 1172 }; 1173 1174 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value) 1175 { 1176 if (reg == 0x1f) { 1177 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE; 1178 return; 1179 } 1180 1181 if (tp->ocp_base != OCP_STD_PHY_BASE) 1182 reg -= 0x10; 1183 1184 if (tp->ocp_base == OCP_STD_PHY_BASE && reg == MII_BMCR) 1185 rtl8168g_phy_suspend_quirk(tp, value); 1186 1187 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value); 1188 } 1189 1190 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg) 1191 { 1192 if (reg == 0x1f) 1193 return tp->ocp_base == OCP_STD_PHY_BASE ? 0 : tp->ocp_base >> 4; 1194 1195 if (tp->ocp_base != OCP_STD_PHY_BASE) 1196 reg -= 0x10; 1197 1198 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2); 1199 } 1200 1201 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value) 1202 { 1203 if (reg == 0x1f) { 1204 tp->ocp_base = value << 4; 1205 return; 1206 } 1207 1208 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value); 1209 } 1210 1211 static int mac_mcu_read(struct rtl8169_private *tp, int reg) 1212 { 1213 return r8168_mac_ocp_read(tp, tp->ocp_base + reg); 1214 } 1215 1216 DECLARE_RTL_COND(rtl_phyar_cond) 1217 { 1218 return RTL_R32(tp, PHYAR) & 0x80000000; 1219 } 1220 1221 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value) 1222 { 1223 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff)); 1224 1225 rtl_loop_wait_low(tp, &rtl_phyar_cond, 25, 20); 1226 /* 1227 * According to hardware specs a 20us delay is required after write 1228 * complete indication, but before sending next command. 1229 */ 1230 udelay(20); 1231 } 1232 1233 static int r8169_mdio_read(struct rtl8169_private *tp, int reg) 1234 { 1235 int value; 1236 1237 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16); 1238 1239 value = rtl_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ? 1240 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT; 1241 1242 /* 1243 * According to hardware specs a 20us delay is required after read 1244 * complete indication, but before sending next command. 1245 */ 1246 udelay(20); 1247 1248 return value; 1249 } 1250 1251 DECLARE_RTL_COND(rtl_ocpar_cond) 1252 { 1253 return RTL_R32(tp, OCPAR) & OCPAR_FLAG; 1254 } 1255 1256 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000 1257 1258 static void r8168dp_2_mdio_start(struct rtl8169_private *tp) 1259 { 1260 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT); 1261 } 1262 1263 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp) 1264 { 1265 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT); 1266 } 1267 1268 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value) 1269 { 1270 r8168dp_2_mdio_start(tp); 1271 1272 r8169_mdio_write(tp, reg, value); 1273 1274 r8168dp_2_mdio_stop(tp); 1275 } 1276 1277 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg) 1278 { 1279 int value; 1280 1281 /* Work around issue with chip reporting wrong PHY ID */ 1282 if (reg == MII_PHYSID2) 1283 return 0xc912; 1284 1285 r8168dp_2_mdio_start(tp); 1286 1287 value = r8169_mdio_read(tp, reg); 1288 1289 r8168dp_2_mdio_stop(tp); 1290 1291 return value; 1292 } 1293 1294 static void rtl_writephy(struct rtl8169_private *tp, int location, int val) 1295 { 1296 switch (tp->mac_version) { 1297 case RTL_GIGA_MAC_VER_28: 1298 case RTL_GIGA_MAC_VER_31: 1299 r8168dp_2_mdio_write(tp, location, val); 1300 break; 1301 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_LAST: 1302 r8168g_mdio_write(tp, location, val); 1303 break; 1304 default: 1305 r8169_mdio_write(tp, location, val); 1306 break; 1307 } 1308 } 1309 1310 static int rtl_readphy(struct rtl8169_private *tp, int location) 1311 { 1312 switch (tp->mac_version) { 1313 case RTL_GIGA_MAC_VER_28: 1314 case RTL_GIGA_MAC_VER_31: 1315 return r8168dp_2_mdio_read(tp, location); 1316 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_LAST: 1317 return r8168g_mdio_read(tp, location); 1318 default: 1319 return r8169_mdio_read(tp, location); 1320 } 1321 } 1322 1323 DECLARE_RTL_COND(rtl_ephyar_cond) 1324 { 1325 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG; 1326 } 1327 1328 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value) 1329 { 1330 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) | 1331 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT); 1332 1333 rtl_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100); 1334 1335 udelay(10); 1336 } 1337 1338 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr) 1339 { 1340 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT); 1341 1342 return rtl_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ? 1343 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0; 1344 } 1345 1346 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u16 reg) 1347 { 1348 RTL_W32(tp, OCPAR, 0x0fu << 12 | (reg & 0x0fff)); 1349 return rtl_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ? 1350 RTL_R32(tp, OCPDR) : ~0; 1351 } 1352 1353 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u16 reg) 1354 { 1355 return _rtl_eri_read(tp, reg, ERIAR_OOB); 1356 } 1357 1358 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, 1359 u32 data) 1360 { 1361 RTL_W32(tp, OCPDR, data); 1362 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff)); 1363 rtl_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20); 1364 } 1365 1366 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, 1367 u32 data) 1368 { 1369 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT, 1370 data, ERIAR_OOB); 1371 } 1372 1373 static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd) 1374 { 1375 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd); 1376 1377 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001); 1378 } 1379 1380 #define OOB_CMD_RESET 0x00 1381 #define OOB_CMD_DRIVER_START 0x05 1382 #define OOB_CMD_DRIVER_STOP 0x06 1383 1384 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp) 1385 { 1386 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10; 1387 } 1388 1389 DECLARE_RTL_COND(rtl_dp_ocp_read_cond) 1390 { 1391 u16 reg; 1392 1393 reg = rtl8168_get_ocp_reg(tp); 1394 1395 return r8168dp_ocp_read(tp, reg) & 0x00000800; 1396 } 1397 1398 DECLARE_RTL_COND(rtl_ep_ocp_read_cond) 1399 { 1400 return r8168ep_ocp_read(tp, 0x124) & 0x00000001; 1401 } 1402 1403 DECLARE_RTL_COND(rtl_ocp_tx_cond) 1404 { 1405 return RTL_R8(tp, IBISR0) & 0x20; 1406 } 1407 1408 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp) 1409 { 1410 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01); 1411 rtl_loop_wait_high(tp, &rtl_ocp_tx_cond, 50000, 2000); 1412 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20); 1413 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01); 1414 } 1415 1416 static void rtl8168dp_driver_start(struct rtl8169_private *tp) 1417 { 1418 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START); 1419 if (tp->dash_enabled) 1420 rtl_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10000, 10); 1421 } 1422 1423 static void rtl8168ep_driver_start(struct rtl8169_private *tp) 1424 { 1425 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START); 1426 r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01); 1427 if (tp->dash_enabled) 1428 rtl_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10000, 30); 1429 } 1430 1431 static void rtl8125bp_driver_start(struct rtl8169_private *tp) 1432 { 1433 r8168ep_ocp_write(tp, 0x01, 0x14, OOB_CMD_DRIVER_START); 1434 r8168ep_ocp_write(tp, 0x01, 0x18, 0x00); 1435 r8168ep_ocp_write(tp, 0x01, 0x10, 0x01); 1436 } 1437 1438 static void rtl8168_driver_start(struct rtl8169_private *tp) 1439 { 1440 if (tp->dash_type == RTL_DASH_DP) 1441 rtl8168dp_driver_start(tp); 1442 else if (tp->dash_type == RTL_DASH_25_BP) 1443 rtl8125bp_driver_start(tp); 1444 else 1445 rtl8168ep_driver_start(tp); 1446 } 1447 1448 static void rtl8168dp_driver_stop(struct rtl8169_private *tp) 1449 { 1450 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP); 1451 if (tp->dash_enabled) 1452 rtl_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10000, 10); 1453 } 1454 1455 static void rtl8168ep_driver_stop(struct rtl8169_private *tp) 1456 { 1457 rtl8168ep_stop_cmac(tp); 1458 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP); 1459 r8168ep_ocp_write(tp, 0x01, 0x30, r8168ep_ocp_read(tp, 0x30) | 0x01); 1460 if (tp->dash_enabled) 1461 rtl_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10000, 10); 1462 } 1463 1464 static void rtl8125bp_driver_stop(struct rtl8169_private *tp) 1465 { 1466 r8168ep_ocp_write(tp, 0x01, 0x14, OOB_CMD_DRIVER_STOP); 1467 r8168ep_ocp_write(tp, 0x01, 0x18, 0x00); 1468 r8168ep_ocp_write(tp, 0x01, 0x10, 0x01); 1469 } 1470 1471 static void rtl8168_driver_stop(struct rtl8169_private *tp) 1472 { 1473 if (tp->dash_type == RTL_DASH_DP) 1474 rtl8168dp_driver_stop(tp); 1475 else if (tp->dash_type == RTL_DASH_25_BP) 1476 rtl8125bp_driver_stop(tp); 1477 else 1478 rtl8168ep_driver_stop(tp); 1479 } 1480 1481 static bool r8168dp_check_dash(struct rtl8169_private *tp) 1482 { 1483 u16 reg = rtl8168_get_ocp_reg(tp); 1484 1485 return r8168dp_ocp_read(tp, reg) & BIT(15); 1486 } 1487 1488 static bool r8168ep_check_dash(struct rtl8169_private *tp) 1489 { 1490 return r8168ep_ocp_read(tp, 0x128) & BIT(0); 1491 } 1492 1493 static bool rtl_dash_is_enabled(struct rtl8169_private *tp) 1494 { 1495 switch (tp->dash_type) { 1496 case RTL_DASH_DP: 1497 return r8168dp_check_dash(tp); 1498 case RTL_DASH_EP: 1499 case RTL_DASH_25_BP: 1500 return r8168ep_check_dash(tp); 1501 default: 1502 return false; 1503 } 1504 } 1505 1506 static enum rtl_dash_type rtl_get_dash_type(struct rtl8169_private *tp) 1507 { 1508 switch (tp->mac_version) { 1509 case RTL_GIGA_MAC_VER_28: 1510 case RTL_GIGA_MAC_VER_31: 1511 return RTL_DASH_DP; 1512 case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_52: 1513 return RTL_DASH_EP; 1514 case RTL_GIGA_MAC_VER_66: 1515 return RTL_DASH_25_BP; 1516 default: 1517 return RTL_DASH_NONE; 1518 } 1519 } 1520 1521 static void rtl_set_d3_pll_down(struct rtl8169_private *tp, bool enable) 1522 { 1523 switch (tp->mac_version) { 1524 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_24: 1525 case RTL_GIGA_MAC_VER_28: 1526 case RTL_GIGA_MAC_VER_31: 1527 case RTL_GIGA_MAC_VER_38: 1528 break; 1529 case RTL_GIGA_MAC_VER_80: 1530 r8169_mod_reg8_cond(tp, PMCH, D3_NO_PLL_DOWN, true); 1531 break; 1532 default: 1533 r8169_mod_reg8_cond(tp, PMCH, D3HOT_NO_PLL_DOWN, true); 1534 r8169_mod_reg8_cond(tp, PMCH, D3COLD_NO_PLL_DOWN, !enable); 1535 break; 1536 } 1537 } 1538 1539 static void rtl_reset_packet_filter(struct rtl8169_private *tp) 1540 { 1541 rtl_eri_clear_bits(tp, 0xdc, BIT(0)); 1542 rtl_eri_set_bits(tp, 0xdc, BIT(0)); 1543 } 1544 1545 DECLARE_RTL_COND(rtl_efusear_cond) 1546 { 1547 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG; 1548 } 1549 1550 u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr) 1551 { 1552 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT); 1553 1554 return rtl_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ? 1555 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0; 1556 } 1557 1558 static u32 rtl_get_events(struct rtl8169_private *tp) 1559 { 1560 if (rtl_is_8125(tp)) 1561 return RTL_R32(tp, IntrStatus_8125); 1562 else 1563 return RTL_R16(tp, IntrStatus); 1564 } 1565 1566 static void rtl_ack_events(struct rtl8169_private *tp, u32 bits) 1567 { 1568 if (rtl_is_8125(tp)) 1569 RTL_W32(tp, IntrStatus_8125, bits); 1570 else 1571 RTL_W16(tp, IntrStatus, bits); 1572 } 1573 1574 static void rtl_irq_disable(struct rtl8169_private *tp) 1575 { 1576 if (rtl_is_8125(tp)) 1577 RTL_W32(tp, IntrMask_8125, 0); 1578 else 1579 RTL_W16(tp, IntrMask, 0); 1580 } 1581 1582 static void rtl_irq_enable(struct rtl8169_private *tp) 1583 { 1584 if (rtl_is_8125(tp)) 1585 RTL_W32(tp, IntrMask_8125, tp->irq_mask); 1586 else 1587 RTL_W16(tp, IntrMask, tp->irq_mask); 1588 } 1589 1590 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp) 1591 { 1592 rtl_irq_disable(tp); 1593 rtl_ack_events(tp, 0xffffffff); 1594 rtl_pci_commit(tp); 1595 } 1596 1597 static void rtl_link_chg_patch(struct rtl8169_private *tp) 1598 { 1599 struct phy_device *phydev = tp->phydev; 1600 1601 if (tp->mac_version == RTL_GIGA_MAC_VER_34 || 1602 tp->mac_version == RTL_GIGA_MAC_VER_38) { 1603 if (phydev->speed == SPEED_1000) { 1604 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011); 1605 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005); 1606 } else if (phydev->speed == SPEED_100) { 1607 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f); 1608 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005); 1609 } else { 1610 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f); 1611 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f); 1612 } 1613 rtl_reset_packet_filter(tp); 1614 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 || 1615 tp->mac_version == RTL_GIGA_MAC_VER_36) { 1616 if (phydev->speed == SPEED_1000) { 1617 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011); 1618 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005); 1619 } else { 1620 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f); 1621 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f); 1622 } 1623 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) { 1624 if (phydev->speed == SPEED_10) { 1625 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02); 1626 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a); 1627 } else { 1628 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000); 1629 } 1630 } 1631 } 1632 1633 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST) 1634 1635 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 1636 { 1637 struct rtl8169_private *tp = netdev_priv(dev); 1638 1639 wol->supported = WAKE_ANY; 1640 wol->wolopts = tp->saved_wolopts; 1641 } 1642 1643 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts) 1644 { 1645 rtl_unlock_config_regs(tp); 1646 1647 if (rtl_is_8168evl_up(tp)) { 1648 if (wolopts & WAKE_MAGIC) 1649 rtl_eri_set_bits(tp, 0x0dc, MagicPacket_v2); 1650 else 1651 rtl_eri_clear_bits(tp, 0x0dc, MagicPacket_v2); 1652 } else if (rtl_is_8125(tp)) { 1653 if (wolopts & WAKE_MAGIC) 1654 r8168_mac_ocp_modify(tp, 0xc0b6, 0, BIT(0)); 1655 else 1656 r8168_mac_ocp_modify(tp, 0xc0b6, BIT(0), 0); 1657 } else { 1658 r8169_mod_reg8_cond(tp, Config3, MagicPacket, 1659 wolopts & WAKE_MAGIC); 1660 } 1661 1662 r8169_mod_reg8_cond(tp, Config3, LinkUp, wolopts & WAKE_PHY); 1663 if (rtl_is_8125(tp)) 1664 r8168_mac_ocp_modify(tp, 0xe0c6, 0x3f, 1665 wolopts & WAKE_PHY ? 0x13 : 0); 1666 r8169_mod_reg8_cond(tp, Config5, UWF, wolopts & WAKE_UCAST); 1667 r8169_mod_reg8_cond(tp, Config5, BWF, wolopts & WAKE_BCAST); 1668 r8169_mod_reg8_cond(tp, Config5, MWF, wolopts & WAKE_MCAST); 1669 r8169_mod_reg8_cond(tp, Config5, LanWake, wolopts); 1670 1671 switch (tp->mac_version) { 1672 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06: 1673 r8169_mod_reg8_cond(tp, Config1, PMEnable, wolopts); 1674 break; 1675 case RTL_GIGA_MAC_VER_34: 1676 case RTL_GIGA_MAC_VER_37: 1677 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_LAST: 1678 r8169_mod_reg8_cond(tp, Config2, PME_SIGNAL, wolopts); 1679 break; 1680 default: 1681 break; 1682 } 1683 1684 rtl_lock_config_regs(tp); 1685 1686 device_set_wakeup_enable(tp_to_dev(tp), wolopts); 1687 1688 if (!tp->dash_enabled) { 1689 rtl_set_d3_pll_down(tp, !wolopts); 1690 tp->dev->ethtool->wol_enabled = wolopts ? 1 : 0; 1691 } 1692 } 1693 1694 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 1695 { 1696 struct rtl8169_private *tp = netdev_priv(dev); 1697 1698 if (wol->wolopts & ~WAKE_ANY) 1699 return -EINVAL; 1700 1701 tp->saved_wolopts = wol->wolopts; 1702 __rtl8169_set_wol(tp, tp->saved_wolopts); 1703 1704 return 0; 1705 } 1706 1707 static void rtl8169_get_drvinfo(struct net_device *dev, 1708 struct ethtool_drvinfo *info) 1709 { 1710 struct rtl8169_private *tp = netdev_priv(dev); 1711 struct rtl_fw *rtl_fw = tp->rtl_fw; 1712 1713 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); 1714 strscpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info)); 1715 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version)); 1716 if (rtl_fw) 1717 strscpy(info->fw_version, rtl_fw->version, 1718 sizeof(info->fw_version)); 1719 } 1720 1721 static int rtl8169_get_regs_len(struct net_device *dev) 1722 { 1723 return R8169_REGS_SIZE; 1724 } 1725 1726 static netdev_features_t rtl8169_fix_features(struct net_device *dev, 1727 netdev_features_t features) 1728 { 1729 struct rtl8169_private *tp = netdev_priv(dev); 1730 1731 if (dev->mtu > TD_MSS_MAX) 1732 features &= ~NETIF_F_ALL_TSO; 1733 1734 if (dev->mtu > ETH_DATA_LEN && 1735 tp->mac_version > RTL_GIGA_MAC_VER_06) 1736 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_ALL_TSO); 1737 1738 return features; 1739 } 1740 1741 static void rtl_set_rx_config_features(struct rtl8169_private *tp, 1742 netdev_features_t features) 1743 { 1744 u32 rx_config = RTL_R32(tp, RxConfig); 1745 1746 if (features & NETIF_F_RXALL) 1747 rx_config |= RX_CONFIG_ACCEPT_ERR_MASK; 1748 else 1749 rx_config &= ~RX_CONFIG_ACCEPT_ERR_MASK; 1750 1751 if (rtl_is_8125(tp)) { 1752 if (features & NETIF_F_HW_VLAN_CTAG_RX) 1753 rx_config |= RX_VLAN_8125; 1754 else 1755 rx_config &= ~RX_VLAN_8125; 1756 } 1757 1758 RTL_W32(tp, RxConfig, rx_config); 1759 } 1760 1761 static int rtl8169_set_features(struct net_device *dev, 1762 netdev_features_t features) 1763 { 1764 struct rtl8169_private *tp = netdev_priv(dev); 1765 1766 rtl_set_rx_config_features(tp, features); 1767 1768 if (features & NETIF_F_RXCSUM) 1769 tp->cp_cmd |= RxChkSum; 1770 else 1771 tp->cp_cmd &= ~RxChkSum; 1772 1773 if (!rtl_is_8125(tp)) { 1774 if (features & NETIF_F_HW_VLAN_CTAG_RX) 1775 tp->cp_cmd |= RxVlan; 1776 else 1777 tp->cp_cmd &= ~RxVlan; 1778 } 1779 1780 RTL_W16(tp, CPlusCmd, tp->cp_cmd); 1781 rtl_pci_commit(tp); 1782 1783 return 0; 1784 } 1785 1786 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb) 1787 { 1788 return (skb_vlan_tag_present(skb)) ? 1789 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00; 1790 } 1791 1792 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb) 1793 { 1794 u32 opts2 = le32_to_cpu(desc->opts2); 1795 1796 if (opts2 & RxVlanTag) 1797 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff)); 1798 } 1799 1800 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs, 1801 void *p) 1802 { 1803 struct rtl8169_private *tp = netdev_priv(dev); 1804 u32 __iomem *data = tp->mmio_addr; 1805 u32 *dw = p; 1806 int i; 1807 1808 for (i = 0; i < R8169_REGS_SIZE; i += 4) 1809 memcpy_fromio(dw++, data++, 4); 1810 } 1811 1812 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = { 1813 "tx_packets", 1814 "rx_packets", 1815 "tx_errors", 1816 "rx_errors", 1817 "rx_missed", 1818 "align_errors", 1819 "tx_single_collisions", 1820 "tx_multi_collisions", 1821 "unicast", 1822 "broadcast", 1823 "multicast", 1824 "tx_aborted", 1825 "tx_underrun", 1826 }; 1827 1828 static int rtl8169_get_sset_count(struct net_device *dev, int sset) 1829 { 1830 switch (sset) { 1831 case ETH_SS_STATS: 1832 return ARRAY_SIZE(rtl8169_gstrings); 1833 default: 1834 return -EOPNOTSUPP; 1835 } 1836 } 1837 1838 DECLARE_RTL_COND(rtl_counters_cond) 1839 { 1840 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump); 1841 } 1842 1843 static void rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd) 1844 { 1845 u32 cmd = lower_32_bits(tp->counters_phys_addr); 1846 1847 RTL_W32(tp, CounterAddrHigh, upper_32_bits(tp->counters_phys_addr)); 1848 rtl_pci_commit(tp); 1849 RTL_W32(tp, CounterAddrLow, cmd); 1850 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd); 1851 1852 rtl_loop_wait_low(tp, &rtl_counters_cond, 10, 1000); 1853 } 1854 1855 static void rtl8169_update_counters(struct rtl8169_private *tp) 1856 { 1857 u8 val = RTL_R8(tp, ChipCmd); 1858 1859 /* 1860 * Some chips are unable to dump tally counters when the receiver 1861 * is disabled. If 0xff chip may be in a PCI power-save state. 1862 */ 1863 if (val & CmdRxEnb && val != 0xff) 1864 rtl8169_do_counters(tp, CounterDump); 1865 } 1866 1867 static void rtl8169_init_counter_offsets(struct rtl8169_private *tp) 1868 { 1869 struct rtl8169_counters *counters = tp->counters; 1870 1871 /* 1872 * rtl8169_init_counter_offsets is called from rtl_open. On chip 1873 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only 1874 * reset by a power cycle, while the counter values collected by the 1875 * driver are reset at every driver unload/load cycle. 1876 * 1877 * To make sure the HW values returned by @get_stats64 match the SW 1878 * values, we collect the initial values at first open(*) and use them 1879 * as offsets to normalize the values returned by @get_stats64. 1880 * 1881 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one 1882 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only 1883 * set at open time by rtl_hw_start. 1884 */ 1885 1886 if (tp->tc_offset.inited) 1887 return; 1888 1889 if (tp->mac_version >= RTL_GIGA_MAC_VER_19) { 1890 rtl8169_do_counters(tp, CounterReset); 1891 } else { 1892 rtl8169_update_counters(tp); 1893 tp->tc_offset.tx_errors = counters->tx_errors; 1894 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision; 1895 tp->tc_offset.tx_aborted = counters->tx_aborted; 1896 tp->tc_offset.rx_missed = counters->rx_missed; 1897 } 1898 1899 tp->tc_offset.inited = true; 1900 } 1901 1902 static void rtl8169_get_ethtool_stats(struct net_device *dev, 1903 struct ethtool_stats *stats, u64 *data) 1904 { 1905 struct rtl8169_private *tp = netdev_priv(dev); 1906 struct rtl8169_counters *counters; 1907 1908 counters = tp->counters; 1909 rtl8169_update_counters(tp); 1910 1911 data[0] = le64_to_cpu(counters->tx_packets); 1912 data[1] = le64_to_cpu(counters->rx_packets); 1913 data[2] = le64_to_cpu(counters->tx_errors); 1914 data[3] = le32_to_cpu(counters->rx_errors); 1915 data[4] = le16_to_cpu(counters->rx_missed); 1916 data[5] = le16_to_cpu(counters->align_errors); 1917 data[6] = le32_to_cpu(counters->tx_one_collision); 1918 data[7] = le32_to_cpu(counters->tx_multi_collision); 1919 data[8] = le64_to_cpu(counters->rx_unicast); 1920 data[9] = le64_to_cpu(counters->rx_broadcast); 1921 data[10] = le32_to_cpu(counters->rx_multicast); 1922 data[11] = le16_to_cpu(counters->tx_aborted); 1923 data[12] = le16_to_cpu(counters->tx_underrun); 1924 } 1925 1926 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data) 1927 { 1928 switch(stringset) { 1929 case ETH_SS_STATS: 1930 memcpy(data, rtl8169_gstrings, sizeof(rtl8169_gstrings)); 1931 break; 1932 } 1933 } 1934 1935 /* 1936 * Interrupt coalescing 1937 * 1938 * > 1 - the availability of the IntrMitigate (0xe2) register through the 1939 * > 8169, 8168 and 810x line of chipsets 1940 * 1941 * 8169, 8168, and 8136(810x) serial chipsets support it. 1942 * 1943 * > 2 - the Tx timer unit at gigabit speed 1944 * 1945 * The unit of the timer depends on both the speed and the setting of CPlusCmd 1946 * (0xe0) bit 1 and bit 0. 1947 * 1948 * For 8169 1949 * bit[1:0] \ speed 1000M 100M 10M 1950 * 0 0 320ns 2.56us 40.96us 1951 * 0 1 2.56us 20.48us 327.7us 1952 * 1 0 5.12us 40.96us 655.4us 1953 * 1 1 10.24us 81.92us 1.31ms 1954 * 1955 * For the other 1956 * bit[1:0] \ speed 1000M 100M 10M 1957 * 0 0 5us 2.56us 40.96us 1958 * 0 1 40us 20.48us 327.7us 1959 * 1 0 80us 40.96us 655.4us 1960 * 1 1 160us 81.92us 1.31ms 1961 */ 1962 1963 /* rx/tx scale factors for all CPlusCmd[0:1] cases */ 1964 struct rtl_coalesce_info { 1965 u32 speed; 1966 u32 scale_nsecs[4]; 1967 }; 1968 1969 /* produce array with base delay *1, *8, *8*2, *8*2*2 */ 1970 #define COALESCE_DELAY(d) { (d), 8 * (d), 16 * (d), 32 * (d) } 1971 1972 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = { 1973 { SPEED_1000, COALESCE_DELAY(320) }, 1974 { SPEED_100, COALESCE_DELAY(2560) }, 1975 { SPEED_10, COALESCE_DELAY(40960) }, 1976 { 0 }, 1977 }; 1978 1979 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = { 1980 { SPEED_1000, COALESCE_DELAY(5000) }, 1981 { SPEED_100, COALESCE_DELAY(2560) }, 1982 { SPEED_10, COALESCE_DELAY(40960) }, 1983 { 0 }, 1984 }; 1985 #undef COALESCE_DELAY 1986 1987 /* get rx/tx scale vector corresponding to current speed */ 1988 static const struct rtl_coalesce_info * 1989 rtl_coalesce_info(struct rtl8169_private *tp) 1990 { 1991 const struct rtl_coalesce_info *ci; 1992 1993 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) 1994 ci = rtl_coalesce_info_8169; 1995 else 1996 ci = rtl_coalesce_info_8168_8136; 1997 1998 /* if speed is unknown assume highest one */ 1999 if (tp->phydev->speed == SPEED_UNKNOWN) 2000 return ci; 2001 2002 for (; ci->speed; ci++) { 2003 if (tp->phydev->speed == ci->speed) 2004 return ci; 2005 } 2006 2007 return ERR_PTR(-ELNRNG); 2008 } 2009 2010 static int rtl_get_coalesce(struct net_device *dev, 2011 struct ethtool_coalesce *ec, 2012 struct kernel_ethtool_coalesce *kernel_coal, 2013 struct netlink_ext_ack *extack) 2014 { 2015 struct rtl8169_private *tp = netdev_priv(dev); 2016 const struct rtl_coalesce_info *ci; 2017 u32 scale, c_us, c_fr; 2018 u16 intrmit; 2019 2020 if (rtl_is_8125(tp)) 2021 return -EOPNOTSUPP; 2022 2023 memset(ec, 0, sizeof(*ec)); 2024 2025 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */ 2026 ci = rtl_coalesce_info(tp); 2027 if (IS_ERR(ci)) 2028 return PTR_ERR(ci); 2029 2030 scale = ci->scale_nsecs[tp->cp_cmd & INTT_MASK]; 2031 2032 intrmit = RTL_R16(tp, IntrMitigate); 2033 2034 c_us = FIELD_GET(RTL_COALESCE_TX_USECS, intrmit); 2035 ec->tx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000); 2036 2037 c_fr = FIELD_GET(RTL_COALESCE_TX_FRAMES, intrmit); 2038 /* ethtool_coalesce states usecs and max_frames must not both be 0 */ 2039 ec->tx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1; 2040 2041 c_us = FIELD_GET(RTL_COALESCE_RX_USECS, intrmit); 2042 ec->rx_coalesce_usecs = DIV_ROUND_UP(c_us * scale, 1000); 2043 2044 c_fr = FIELD_GET(RTL_COALESCE_RX_FRAMES, intrmit); 2045 ec->rx_max_coalesced_frames = (c_us || c_fr) ? c_fr * 4 : 1; 2046 2047 return 0; 2048 } 2049 2050 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, usec) */ 2051 static int rtl_coalesce_choose_scale(struct rtl8169_private *tp, u32 usec, 2052 u16 *cp01) 2053 { 2054 const struct rtl_coalesce_info *ci; 2055 u16 i; 2056 2057 ci = rtl_coalesce_info(tp); 2058 if (IS_ERR(ci)) 2059 return PTR_ERR(ci); 2060 2061 for (i = 0; i < 4; i++) { 2062 if (usec <= ci->scale_nsecs[i] * RTL_COALESCE_T_MAX / 1000U) { 2063 *cp01 = i; 2064 return ci->scale_nsecs[i]; 2065 } 2066 } 2067 2068 return -ERANGE; 2069 } 2070 2071 static int rtl_set_coalesce(struct net_device *dev, 2072 struct ethtool_coalesce *ec, 2073 struct kernel_ethtool_coalesce *kernel_coal, 2074 struct netlink_ext_ack *extack) 2075 { 2076 struct rtl8169_private *tp = netdev_priv(dev); 2077 u32 tx_fr = ec->tx_max_coalesced_frames; 2078 u32 rx_fr = ec->rx_max_coalesced_frames; 2079 u32 coal_usec_max, units; 2080 u16 w = 0, cp01 = 0; 2081 int scale; 2082 2083 if (rtl_is_8125(tp)) 2084 return -EOPNOTSUPP; 2085 2086 if (rx_fr > RTL_COALESCE_FRAME_MAX || tx_fr > RTL_COALESCE_FRAME_MAX) 2087 return -ERANGE; 2088 2089 coal_usec_max = max(ec->rx_coalesce_usecs, ec->tx_coalesce_usecs); 2090 scale = rtl_coalesce_choose_scale(tp, coal_usec_max, &cp01); 2091 if (scale < 0) 2092 return scale; 2093 2094 /* Accept max_frames=1 we returned in rtl_get_coalesce. Accept it 2095 * not only when usecs=0 because of e.g. the following scenario: 2096 * 2097 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX) 2098 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1 2099 * - then user does `ethtool -C eth0 rx-usecs 100` 2100 * 2101 * Since ethtool sends to kernel whole ethtool_coalesce settings, 2102 * if we want to ignore rx_frames then it has to be set to 0. 2103 */ 2104 if (rx_fr == 1) 2105 rx_fr = 0; 2106 if (tx_fr == 1) 2107 tx_fr = 0; 2108 2109 /* HW requires time limit to be set if frame limit is set */ 2110 if ((tx_fr && !ec->tx_coalesce_usecs) || 2111 (rx_fr && !ec->rx_coalesce_usecs)) 2112 return -EINVAL; 2113 2114 w |= FIELD_PREP(RTL_COALESCE_TX_FRAMES, DIV_ROUND_UP(tx_fr, 4)); 2115 w |= FIELD_PREP(RTL_COALESCE_RX_FRAMES, DIV_ROUND_UP(rx_fr, 4)); 2116 2117 units = DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000U, scale); 2118 w |= FIELD_PREP(RTL_COALESCE_TX_USECS, units); 2119 units = DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000U, scale); 2120 w |= FIELD_PREP(RTL_COALESCE_RX_USECS, units); 2121 2122 RTL_W16(tp, IntrMitigate, w); 2123 2124 /* Meaning of PktCntrDisable bit changed from RTL8168e-vl */ 2125 if (rtl_is_8168evl_up(tp)) { 2126 if (!rx_fr && !tx_fr) 2127 /* disable packet counter */ 2128 tp->cp_cmd |= PktCntrDisable; 2129 else 2130 tp->cp_cmd &= ~PktCntrDisable; 2131 } 2132 2133 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01; 2134 RTL_W16(tp, CPlusCmd, tp->cp_cmd); 2135 rtl_pci_commit(tp); 2136 2137 return 0; 2138 } 2139 2140 static void rtl_set_eee_txidle_timer(struct rtl8169_private *tp) 2141 { 2142 unsigned int timer_val = READ_ONCE(tp->dev->mtu) + ETH_HLEN + 0x20; 2143 2144 switch (tp->mac_version) { 2145 case RTL_GIGA_MAC_VER_46: 2146 case RTL_GIGA_MAC_VER_48: 2147 tp->tx_lpi_timer = timer_val; 2148 r8168_mac_ocp_write(tp, 0xe048, timer_val); 2149 break; 2150 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_LAST: 2151 tp->tx_lpi_timer = timer_val; 2152 RTL_W16(tp, EEE_TXIDLE_TIMER_8125, timer_val); 2153 break; 2154 default: 2155 break; 2156 } 2157 } 2158 2159 static unsigned int r8169_get_tx_lpi_timer_us(struct rtl8169_private *tp) 2160 { 2161 unsigned int speed = tp->phydev->speed; 2162 unsigned int timer = tp->tx_lpi_timer; 2163 2164 if (!timer || speed == SPEED_UNKNOWN) 2165 return 0; 2166 2167 /* tx_lpi_timer value is in bytes */ 2168 return DIV_ROUND_CLOSEST(timer * BITS_PER_BYTE, speed); 2169 } 2170 2171 static int rtl8169_get_eee(struct net_device *dev, struct ethtool_keee *data) 2172 { 2173 struct rtl8169_private *tp = netdev_priv(dev); 2174 int ret; 2175 2176 if (!rtl_supports_eee(tp)) 2177 return -EOPNOTSUPP; 2178 2179 ret = phy_ethtool_get_eee(tp->phydev, data); 2180 if (ret) 2181 return ret; 2182 2183 data->tx_lpi_timer = r8169_get_tx_lpi_timer_us(tp); 2184 2185 return 0; 2186 } 2187 2188 static int rtl8169_set_eee(struct net_device *dev, struct ethtool_keee *data) 2189 { 2190 struct rtl8169_private *tp = netdev_priv(dev); 2191 2192 if (!rtl_supports_eee(tp)) 2193 return -EOPNOTSUPP; 2194 2195 return phy_ethtool_set_eee(tp->phydev, data); 2196 } 2197 2198 static void rtl8169_get_ringparam(struct net_device *dev, 2199 struct ethtool_ringparam *data, 2200 struct kernel_ethtool_ringparam *kernel_data, 2201 struct netlink_ext_ack *extack) 2202 { 2203 data->rx_max_pending = NUM_RX_DESC; 2204 data->rx_pending = NUM_RX_DESC; 2205 data->tx_max_pending = NUM_TX_DESC; 2206 data->tx_pending = NUM_TX_DESC; 2207 } 2208 2209 static void rtl8169_get_pause_stats(struct net_device *dev, 2210 struct ethtool_pause_stats *pause_stats) 2211 { 2212 struct rtl8169_private *tp = netdev_priv(dev); 2213 2214 if (!rtl_is_8125(tp)) 2215 return; 2216 2217 rtl8169_update_counters(tp); 2218 pause_stats->tx_pause_frames = le32_to_cpu(tp->counters->tx_pause_on); 2219 pause_stats->rx_pause_frames = le32_to_cpu(tp->counters->rx_pause_on); 2220 } 2221 2222 static void rtl8169_get_pauseparam(struct net_device *dev, 2223 struct ethtool_pauseparam *data) 2224 { 2225 struct rtl8169_private *tp = netdev_priv(dev); 2226 bool tx_pause, rx_pause; 2227 2228 phy_get_pause(tp->phydev, &tx_pause, &rx_pause); 2229 2230 data->autoneg = tp->phydev->autoneg; 2231 data->tx_pause = tx_pause ? 1 : 0; 2232 data->rx_pause = rx_pause ? 1 : 0; 2233 } 2234 2235 static int rtl8169_set_pauseparam(struct net_device *dev, 2236 struct ethtool_pauseparam *data) 2237 { 2238 struct rtl8169_private *tp = netdev_priv(dev); 2239 2240 if (dev->mtu > ETH_DATA_LEN) 2241 return -EOPNOTSUPP; 2242 2243 phy_set_asym_pause(tp->phydev, data->rx_pause, data->tx_pause); 2244 2245 return 0; 2246 } 2247 2248 static void rtl8169_get_eth_mac_stats(struct net_device *dev, 2249 struct ethtool_eth_mac_stats *mac_stats) 2250 { 2251 struct rtl8169_private *tp = netdev_priv(dev); 2252 2253 rtl8169_update_counters(tp); 2254 2255 mac_stats->FramesTransmittedOK = 2256 le64_to_cpu(tp->counters->tx_packets); 2257 mac_stats->SingleCollisionFrames = 2258 le32_to_cpu(tp->counters->tx_one_collision); 2259 mac_stats->MultipleCollisionFrames = 2260 le32_to_cpu(tp->counters->tx_multi_collision); 2261 mac_stats->FramesReceivedOK = 2262 le64_to_cpu(tp->counters->rx_packets); 2263 mac_stats->AlignmentErrors = 2264 le16_to_cpu(tp->counters->align_errors); 2265 mac_stats->FramesLostDueToIntMACXmitError = 2266 le64_to_cpu(tp->counters->tx_errors); 2267 mac_stats->BroadcastFramesReceivedOK = 2268 le64_to_cpu(tp->counters->rx_broadcast); 2269 mac_stats->MulticastFramesReceivedOK = 2270 le32_to_cpu(tp->counters->rx_multicast); 2271 2272 if (!rtl_is_8125(tp)) 2273 return; 2274 2275 mac_stats->AlignmentErrors = 2276 le32_to_cpu(tp->counters->align_errors32); 2277 mac_stats->OctetsTransmittedOK = 2278 le64_to_cpu(tp->counters->tx_octets); 2279 mac_stats->LateCollisions = 2280 le32_to_cpu(tp->counters->tx_late_collision); 2281 mac_stats->FramesAbortedDueToXSColls = 2282 le32_to_cpu(tp->counters->tx_aborted32); 2283 mac_stats->OctetsReceivedOK = 2284 le64_to_cpu(tp->counters->rx_octets); 2285 mac_stats->FramesLostDueToIntMACRcvError = 2286 le32_to_cpu(tp->counters->rx_mac_error); 2287 mac_stats->MulticastFramesXmittedOK = 2288 le64_to_cpu(tp->counters->tx_multicast64); 2289 mac_stats->BroadcastFramesXmittedOK = 2290 le64_to_cpu(tp->counters->tx_broadcast64); 2291 mac_stats->MulticastFramesReceivedOK = 2292 le64_to_cpu(tp->counters->rx_multicast64); 2293 mac_stats->FrameTooLongErrors = 2294 le32_to_cpu(tp->counters->rx_frame_too_long); 2295 } 2296 2297 static void rtl8169_get_eth_ctrl_stats(struct net_device *dev, 2298 struct ethtool_eth_ctrl_stats *ctrl_stats) 2299 { 2300 struct rtl8169_private *tp = netdev_priv(dev); 2301 2302 if (!rtl_is_8125(tp)) 2303 return; 2304 2305 rtl8169_update_counters(tp); 2306 2307 ctrl_stats->UnsupportedOpcodesReceived = 2308 le32_to_cpu(tp->counters->rx_unknown_opcode); 2309 } 2310 2311 static const struct ethtool_ops rtl8169_ethtool_ops = { 2312 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 2313 ETHTOOL_COALESCE_MAX_FRAMES, 2314 .get_drvinfo = rtl8169_get_drvinfo, 2315 .get_regs_len = rtl8169_get_regs_len, 2316 .get_link = ethtool_op_get_link, 2317 .get_coalesce = rtl_get_coalesce, 2318 .set_coalesce = rtl_set_coalesce, 2319 .get_regs = rtl8169_get_regs, 2320 .get_wol = rtl8169_get_wol, 2321 .set_wol = rtl8169_set_wol, 2322 .get_strings = rtl8169_get_strings, 2323 .get_sset_count = rtl8169_get_sset_count, 2324 .get_ethtool_stats = rtl8169_get_ethtool_stats, 2325 .get_ts_info = ethtool_op_get_ts_info, 2326 .nway_reset = phy_ethtool_nway_reset, 2327 .get_eee = rtl8169_get_eee, 2328 .set_eee = rtl8169_set_eee, 2329 .get_link_ksettings = phy_ethtool_get_link_ksettings, 2330 .set_link_ksettings = phy_ethtool_set_link_ksettings, 2331 .get_ringparam = rtl8169_get_ringparam, 2332 .get_pause_stats = rtl8169_get_pause_stats, 2333 .get_pauseparam = rtl8169_get_pauseparam, 2334 .set_pauseparam = rtl8169_set_pauseparam, 2335 .get_eth_mac_stats = rtl8169_get_eth_mac_stats, 2336 .get_eth_ctrl_stats = rtl8169_get_eth_ctrl_stats, 2337 }; 2338 2339 static const struct rtl_chip_info *rtl8169_get_chip_version(u16 xid, bool gmii) 2340 { 2341 /* Chips combining a 1Gbps MAC with a 100Mbps PHY */ 2342 static const struct rtl_chip_info rtl8106eus_info = { 2343 .mac_version = RTL_GIGA_MAC_VER_43, 2344 .name = "RTL8106eus", 2345 .fw_name = FIRMWARE_8106E_2, 2346 }; 2347 static const struct rtl_chip_info rtl8107e_info = { 2348 .mac_version = RTL_GIGA_MAC_VER_48, 2349 .name = "RTL8107e", 2350 .fw_name = FIRMWARE_8107E_2, 2351 }; 2352 const struct rtl_chip_info *p = rtl_chip_infos; 2353 2354 while ((xid & p->mask) != p->val) 2355 p++; 2356 2357 if (p->mac_version == RTL_GIGA_MAC_VER_42 && !gmii) 2358 return &rtl8106eus_info; 2359 if (p->mac_version == RTL_GIGA_MAC_VER_46 && !gmii) 2360 return &rtl8107e_info; 2361 2362 return p; 2363 } 2364 2365 static void rtl_release_firmware(struct rtl8169_private *tp) 2366 { 2367 if (tp->rtl_fw) { 2368 rtl_fw_release_firmware(tp->rtl_fw); 2369 kfree(tp->rtl_fw); 2370 tp->rtl_fw = NULL; 2371 } 2372 } 2373 2374 void r8169_apply_firmware(struct rtl8169_private *tp) 2375 { 2376 int val; 2377 2378 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */ 2379 if (tp->rtl_fw) { 2380 rtl_fw_write_firmware(tp, tp->rtl_fw); 2381 /* At least one firmware doesn't reset tp->ocp_base. */ 2382 tp->ocp_base = OCP_STD_PHY_BASE; 2383 2384 /* PHY soft reset may still be in progress */ 2385 phy_read_poll_timeout(tp->phydev, MII_BMCR, val, 2386 !(val & BMCR_RESET), 2387 50000, 600000, true); 2388 } 2389 } 2390 2391 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, const u8 *addr) 2392 { 2393 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, get_unaligned_le32(addr)); 2394 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, get_unaligned_le16(addr + 4)); 2395 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, get_unaligned_le16(addr) << 16); 2396 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, get_unaligned_le32(addr + 2)); 2397 } 2398 2399 u16 rtl8168h_2_get_adc_bias_ioffset(struct rtl8169_private *tp) 2400 { 2401 u16 data1, data2, ioffset; 2402 2403 r8168_mac_ocp_write(tp, 0xdd02, 0x807d); 2404 data1 = r8168_mac_ocp_read(tp, 0xdd02); 2405 data2 = r8168_mac_ocp_read(tp, 0xdd00); 2406 2407 ioffset = (data2 >> 1) & 0x7ff8; 2408 ioffset |= data2 & 0x0007; 2409 if (data1 & BIT(7)) 2410 ioffset |= BIT(15); 2411 2412 return ioffset; 2413 } 2414 2415 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag) 2416 { 2417 set_bit(flag, tp->wk.flags); 2418 if (!schedule_work(&tp->wk.work)) 2419 clear_bit(flag, tp->wk.flags); 2420 } 2421 2422 static void rtl8169_init_phy(struct rtl8169_private *tp) 2423 { 2424 r8169_hw_phy_config(tp, tp->phydev, tp->mac_version); 2425 2426 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) { 2427 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40); 2428 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08); 2429 /* set undocumented MAC Reg C+CR Offset 0x82h */ 2430 RTL_W8(tp, 0x82, 0x01); 2431 } 2432 2433 if (tp->mac_version == RTL_GIGA_MAC_VER_05 && 2434 tp->pci_dev->subsystem_vendor == PCI_VENDOR_ID_GIGABYTE && 2435 tp->pci_dev->subsystem_device == 0xe000) 2436 phy_write_paged(tp->phydev, 0x0001, 0x10, 0xf01b); 2437 2438 /* We may have called phy_speed_down before */ 2439 phy_speed_up(tp->phydev); 2440 2441 genphy_soft_reset(tp->phydev); 2442 } 2443 2444 static void rtl_rar_set(struct rtl8169_private *tp, const u8 *addr) 2445 { 2446 rtl_unlock_config_regs(tp); 2447 2448 RTL_W32(tp, MAC4, get_unaligned_le16(addr + 4)); 2449 rtl_pci_commit(tp); 2450 2451 RTL_W32(tp, MAC0, get_unaligned_le32(addr)); 2452 rtl_pci_commit(tp); 2453 2454 if (tp->mac_version == RTL_GIGA_MAC_VER_34) 2455 rtl_rar_exgmac_set(tp, addr); 2456 2457 rtl_lock_config_regs(tp); 2458 } 2459 2460 static int rtl_set_mac_address(struct net_device *dev, void *p) 2461 { 2462 struct rtl8169_private *tp = netdev_priv(dev); 2463 int ret; 2464 2465 ret = eth_mac_addr(dev, p); 2466 if (ret) 2467 return ret; 2468 2469 rtl_rar_set(tp, dev->dev_addr); 2470 2471 return 0; 2472 } 2473 2474 static void rtl_init_rxcfg(struct rtl8169_private *tp) 2475 { 2476 switch (tp->mac_version) { 2477 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06: 2478 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17: 2479 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST); 2480 break; 2481 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24: 2482 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36: 2483 case RTL_GIGA_MAC_VER_38: 2484 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST); 2485 break; 2486 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_52: 2487 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF); 2488 break; 2489 case RTL_GIGA_MAC_VER_61: 2490 RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST); 2491 break; 2492 case RTL_GIGA_MAC_VER_63 ... RTL_GIGA_MAC_VER_LAST: 2493 RTL_W32(tp, RxConfig, RX_FETCH_DFLT_8125 | RX_DMA_BURST | 2494 RX_PAUSE_SLOT_ON); 2495 break; 2496 default: 2497 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST); 2498 break; 2499 } 2500 } 2501 2502 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp) 2503 { 2504 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0; 2505 } 2506 2507 static void rtl_jumbo_config(struct rtl8169_private *tp) 2508 { 2509 bool jumbo = tp->dev->mtu > ETH_DATA_LEN; 2510 int readrq = 4096; 2511 2512 if (jumbo && tp->mac_version >= RTL_GIGA_MAC_VER_17 && 2513 tp->mac_version <= RTL_GIGA_MAC_VER_26) 2514 readrq = 512; 2515 2516 rtl_unlock_config_regs(tp); 2517 switch (tp->mac_version) { 2518 case RTL_GIGA_MAC_VER_17: 2519 r8169_mod_reg8_cond(tp, Config4, BIT(0), jumbo); 2520 break; 2521 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26: 2522 r8169_mod_reg8_cond(tp, Config3, Jumbo_En0, jumbo); 2523 r8169_mod_reg8_cond(tp, Config4, Jumbo_En1, jumbo); 2524 break; 2525 case RTL_GIGA_MAC_VER_28: 2526 r8169_mod_reg8_cond(tp, Config3, Jumbo_En0, jumbo); 2527 break; 2528 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_33: 2529 RTL_W8(tp, MaxTxPacketSize, jumbo ? 0x24 : 0x3f); 2530 r8169_mod_reg8_cond(tp, Config3, Jumbo_En0, jumbo); 2531 r8169_mod_reg8_cond(tp, Config4, BIT(0), jumbo); 2532 break; 2533 default: 2534 break; 2535 } 2536 rtl_lock_config_regs(tp); 2537 2538 if (pci_is_pcie(tp->pci_dev) && tp->supports_gmii) 2539 pcie_set_readrq(tp->pci_dev, readrq); 2540 2541 /* Chip doesn't support pause in jumbo mode */ 2542 if (jumbo) { 2543 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2544 tp->phydev->advertising); 2545 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2546 tp->phydev->advertising); 2547 phy_start_aneg(tp->phydev); 2548 } 2549 } 2550 2551 DECLARE_RTL_COND(rtl_chipcmd_cond) 2552 { 2553 return RTL_R8(tp, ChipCmd) & CmdReset; 2554 } 2555 2556 static void rtl_hw_reset(struct rtl8169_private *tp) 2557 { 2558 RTL_W8(tp, ChipCmd, CmdReset); 2559 2560 rtl_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100); 2561 } 2562 2563 static void rtl_request_firmware(struct rtl8169_private *tp) 2564 { 2565 struct rtl_fw *rtl_fw; 2566 2567 /* firmware loaded already or no firmware available */ 2568 if (tp->rtl_fw || !tp->fw_name) 2569 return; 2570 2571 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL); 2572 if (!rtl_fw) 2573 return; 2574 2575 rtl_fw->phy_write = rtl_writephy; 2576 rtl_fw->phy_read = rtl_readphy; 2577 rtl_fw->mac_mcu_write = mac_mcu_write; 2578 rtl_fw->mac_mcu_read = mac_mcu_read; 2579 rtl_fw->fw_name = tp->fw_name; 2580 rtl_fw->dev = tp_to_dev(tp); 2581 2582 if (rtl_fw_request_firmware(rtl_fw)) 2583 kfree(rtl_fw); 2584 else 2585 tp->rtl_fw = rtl_fw; 2586 } 2587 2588 static void rtl_rx_close(struct rtl8169_private *tp) 2589 { 2590 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK); 2591 } 2592 2593 DECLARE_RTL_COND(rtl_npq_cond) 2594 { 2595 return RTL_R8(tp, TxPoll) & NPQ; 2596 } 2597 2598 DECLARE_RTL_COND(rtl_txcfg_empty_cond) 2599 { 2600 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY; 2601 } 2602 2603 DECLARE_RTL_COND(rtl_rxtx_empty_cond) 2604 { 2605 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY; 2606 } 2607 2608 DECLARE_RTL_COND(rtl_rxtx_empty_cond_2) 2609 { 2610 /* IntrMitigate has new functionality on RTL8125 */ 2611 return (RTL_R16(tp, IntrMitigate) & 0x0103) == 0x0103; 2612 } 2613 2614 static void rtl_wait_txrx_fifo_empty(struct rtl8169_private *tp) 2615 { 2616 switch (tp->mac_version) { 2617 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_52: 2618 rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42); 2619 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42); 2620 break; 2621 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_61: 2622 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42); 2623 break; 2624 case RTL_GIGA_MAC_VER_63 ... RTL_GIGA_MAC_VER_LAST: 2625 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq); 2626 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42); 2627 rtl_loop_wait_high(tp, &rtl_rxtx_empty_cond_2, 100, 42); 2628 break; 2629 default: 2630 break; 2631 } 2632 } 2633 2634 static void rtl_disable_rxdvgate(struct rtl8169_private *tp) 2635 { 2636 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN); 2637 } 2638 2639 static void rtl_enable_rxdvgate(struct rtl8169_private *tp) 2640 { 2641 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN); 2642 fsleep(2000); 2643 rtl_wait_txrx_fifo_empty(tp); 2644 } 2645 2646 static void rtl_wol_enable_rx(struct rtl8169_private *tp) 2647 { 2648 if (tp->mac_version >= RTL_GIGA_MAC_VER_25) 2649 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) | 2650 AcceptBroadcast | AcceptMulticast | AcceptMyPhys); 2651 2652 if (tp->mac_version >= RTL_GIGA_MAC_VER_40) 2653 rtl_disable_rxdvgate(tp); 2654 } 2655 2656 static void rtl_prepare_power_down(struct rtl8169_private *tp) 2657 { 2658 if (tp->dash_enabled) 2659 return; 2660 2661 if (tp->mac_version == RTL_GIGA_MAC_VER_32 || 2662 tp->mac_version == RTL_GIGA_MAC_VER_33) 2663 rtl_ephy_write(tp, 0x19, 0xff64); 2664 2665 if (device_may_wakeup(tp_to_dev(tp))) { 2666 phy_speed_down(tp->phydev, false); 2667 rtl_wol_enable_rx(tp); 2668 } 2669 } 2670 2671 static void rtl_set_tx_config_registers(struct rtl8169_private *tp) 2672 { 2673 u32 val = TX_DMA_BURST << TxDMAShift | 2674 InterFrameGap << TxInterFrameGapShift; 2675 2676 if (rtl_is_8168evl_up(tp)) 2677 val |= TXCFG_AUTO_FIFO; 2678 2679 RTL_W32(tp, TxConfig, val); 2680 } 2681 2682 static void rtl_set_rx_max_size(struct rtl8169_private *tp) 2683 { 2684 /* Low hurts. Let's disable the filtering. */ 2685 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1); 2686 } 2687 2688 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp) 2689 { 2690 /* 2691 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh 2692 * register to be written before TxDescAddrLow to work. 2693 * Switching from MMIO to I/O access fixes the issue as well. 2694 */ 2695 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32); 2696 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32)); 2697 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32); 2698 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32)); 2699 } 2700 2701 static void rtl8169_set_magic_reg(struct rtl8169_private *tp) 2702 { 2703 u32 val; 2704 2705 if (tp->mac_version == RTL_GIGA_MAC_VER_05) 2706 val = 0x000fff00; 2707 else if (tp->mac_version == RTL_GIGA_MAC_VER_06) 2708 val = 0x00ffff00; 2709 else 2710 return; 2711 2712 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz) 2713 val |= 0xff; 2714 2715 RTL_W32(tp, 0x7c, val); 2716 } 2717 2718 static void rtl_set_rx_mode(struct net_device *dev) 2719 { 2720 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast; 2721 /* Multicast hash filter */ 2722 u32 mc_filter[2] = { 0xffffffff, 0xffffffff }; 2723 struct rtl8169_private *tp = netdev_priv(dev); 2724 u32 tmp; 2725 2726 if (dev->flags & IFF_PROMISC) { 2727 rx_mode |= AcceptAllPhys; 2728 } else if (!(dev->flags & IFF_MULTICAST)) { 2729 rx_mode &= ~AcceptMulticast; 2730 } else if (dev->flags & IFF_ALLMULTI || 2731 tp->mac_version == RTL_GIGA_MAC_VER_35) { 2732 /* accept all multicasts */ 2733 } else if (netdev_mc_empty(dev)) { 2734 rx_mode &= ~AcceptMulticast; 2735 } else { 2736 struct netdev_hw_addr *ha; 2737 2738 mc_filter[1] = mc_filter[0] = 0; 2739 netdev_for_each_mc_addr(ha, dev) { 2740 u32 bit_nr = eth_hw_addr_crc(ha) >> 26; 2741 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31); 2742 } 2743 2744 if (tp->mac_version > RTL_GIGA_MAC_VER_06) { 2745 tmp = mc_filter[0]; 2746 mc_filter[0] = swab32(mc_filter[1]); 2747 mc_filter[1] = swab32(tmp); 2748 } 2749 } 2750 2751 RTL_W32(tp, MAR0 + 4, mc_filter[1]); 2752 RTL_W32(tp, MAR0 + 0, mc_filter[0]); 2753 2754 tmp = RTL_R32(tp, RxConfig); 2755 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_OK_MASK) | rx_mode); 2756 } 2757 2758 DECLARE_RTL_COND(rtl_csiar_cond) 2759 { 2760 return RTL_R32(tp, CSIAR) & CSIAR_FLAG; 2761 } 2762 2763 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value) 2764 { 2765 u32 func = PCI_FUNC(tp->pci_dev->devfn); 2766 2767 RTL_W32(tp, CSIDR, value); 2768 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) | 2769 CSIAR_BYTE_ENABLE | func << 16); 2770 2771 rtl_loop_wait_low(tp, &rtl_csiar_cond, 10, 100); 2772 } 2773 2774 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr) 2775 { 2776 u32 func = PCI_FUNC(tp->pci_dev->devfn); 2777 2778 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 | 2779 CSIAR_BYTE_ENABLE); 2780 2781 return rtl_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ? 2782 RTL_R32(tp, CSIDR) : ~0; 2783 } 2784 2785 static void rtl_csi_mod(struct rtl8169_private *tp, int addr, 2786 u32 mask, u32 set) 2787 { 2788 u32 val; 2789 2790 WARN(addr % 4, "Invalid CSI address %#x\n", addr); 2791 2792 netdev_notice_once(tp->dev, 2793 "No native access to PCI extended config space, falling back to CSI\n"); 2794 2795 val = rtl_csi_read(tp, addr); 2796 rtl_csi_write(tp, addr, (val & ~mask) | set); 2797 } 2798 2799 static void rtl_disable_zrxdc_timeout(struct rtl8169_private *tp) 2800 { 2801 struct pci_dev *pdev = tp->pci_dev; 2802 int rc; 2803 u8 val; 2804 2805 #define RTL_GEN3_RELATED_OFF 0x0890 2806 #define RTL_GEN3_ZRXDC_NONCOMPL 0x1 2807 if (pdev->cfg_size > RTL_GEN3_RELATED_OFF) { 2808 rc = pci_read_config_byte(pdev, RTL_GEN3_RELATED_OFF, &val); 2809 if (rc == PCIBIOS_SUCCESSFUL) { 2810 val &= ~RTL_GEN3_ZRXDC_NONCOMPL; 2811 rc = pci_write_config_byte(pdev, RTL_GEN3_RELATED_OFF, 2812 val); 2813 if (rc == PCIBIOS_SUCCESSFUL) 2814 return; 2815 } 2816 } 2817 2818 rtl_csi_mod(tp, RTL_GEN3_RELATED_OFF, RTL_GEN3_ZRXDC_NONCOMPL, 0); 2819 } 2820 2821 static void rtl_set_aspm_entry_latency(struct rtl8169_private *tp, u8 val) 2822 { 2823 struct pci_dev *pdev = tp->pci_dev; 2824 2825 /* According to Realtek the value at config space address 0x070f 2826 * controls the L0s/L1 entrance latency. We try standard ECAM access 2827 * first and if it fails fall back to CSI. 2828 * bit 0..2: L0: 0 = 1us, 1 = 2us .. 6 = 7us, 7 = 7us (no typo) 2829 * bit 3..5: L1: 0 = 1us, 1 = 2us .. 6 = 64us, 7 = 64us 2830 */ 2831 if (pdev->cfg_size > 0x070f && 2832 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL) 2833 return; 2834 2835 rtl_csi_mod(tp, 0x070c, 0xff000000, val << 24); 2836 } 2837 2838 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp) 2839 { 2840 /* L0 7us, L1 16us */ 2841 rtl_set_aspm_entry_latency(tp, 0x27); 2842 } 2843 2844 struct ephy_info { 2845 unsigned int offset; 2846 u16 mask; 2847 u16 bits; 2848 }; 2849 2850 static void __rtl_ephy_init(struct rtl8169_private *tp, 2851 const struct ephy_info *e, int len) 2852 { 2853 u16 w; 2854 2855 while (len-- > 0) { 2856 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits; 2857 rtl_ephy_write(tp, e->offset, w); 2858 e++; 2859 } 2860 } 2861 2862 #define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a)) 2863 2864 static void rtl_disable_clock_request(struct rtl8169_private *tp) 2865 { 2866 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL, 2867 PCI_EXP_LNKCTL_CLKREQ_EN); 2868 } 2869 2870 static void rtl_enable_clock_request(struct rtl8169_private *tp) 2871 { 2872 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL, 2873 PCI_EXP_LNKCTL_CLKREQ_EN); 2874 } 2875 2876 static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp) 2877 { 2878 /* work around an issue when PCI reset occurs during L2/L3 state */ 2879 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23); 2880 } 2881 2882 static void rtl_enable_exit_l1(struct rtl8169_private *tp) 2883 { 2884 /* Bits control which events trigger ASPM L1 exit: 2885 * Bit 12: rxdv 2886 * Bit 11: ltr_msg 2887 * Bit 10: txdma_poll 2888 * Bit 9: xadm 2889 * Bit 8: pktavi 2890 * Bit 7: txpla 2891 */ 2892 switch (tp->mac_version) { 2893 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36: 2894 rtl_eri_set_bits(tp, 0xd4, 0x1f00); 2895 break; 2896 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_38: 2897 rtl_eri_set_bits(tp, 0xd4, 0x0c00); 2898 break; 2899 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_LAST: 2900 r8168_mac_ocp_modify(tp, 0xc0ac, 0, 0x1f80); 2901 break; 2902 default: 2903 break; 2904 } 2905 } 2906 2907 static void rtl_disable_exit_l1(struct rtl8169_private *tp) 2908 { 2909 switch (tp->mac_version) { 2910 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38: 2911 rtl_eri_clear_bits(tp, 0xd4, 0x1f00); 2912 break; 2913 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_LAST: 2914 r8168_mac_ocp_modify(tp, 0xc0ac, 0x1f80, 0); 2915 break; 2916 default: 2917 break; 2918 } 2919 } 2920 2921 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable) 2922 { 2923 u8 val8; 2924 2925 if (tp->mac_version < RTL_GIGA_MAC_VER_32) 2926 return; 2927 2928 /* Don't enable ASPM in the chip if OS can't control ASPM */ 2929 if (enable && tp->aspm_manageable) { 2930 /* On these chip versions ASPM can even harm 2931 * bus communication of other PCI devices. 2932 */ 2933 if (tp->mac_version == RTL_GIGA_MAC_VER_42 || 2934 tp->mac_version == RTL_GIGA_MAC_VER_43) 2935 return; 2936 2937 rtl_mod_config5(tp, 0, ASPM_en); 2938 switch (tp->mac_version) { 2939 case RTL_GIGA_MAC_VER_70: 2940 case RTL_GIGA_MAC_VER_80: 2941 val8 = RTL_R8(tp, INT_CFG0_8125) | INT_CFG0_CLKREQEN; 2942 RTL_W8(tp, INT_CFG0_8125, val8); 2943 break; 2944 default: 2945 rtl_mod_config2(tp, 0, ClkReqEn); 2946 break; 2947 } 2948 2949 switch (tp->mac_version) { 2950 case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48: 2951 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_LAST: 2952 /* reset ephy tx/rx disable timer */ 2953 r8168_mac_ocp_modify(tp, 0xe094, 0xff00, 0); 2954 /* chip can trigger L1.2 */ 2955 r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, BIT(2)); 2956 break; 2957 default: 2958 break; 2959 } 2960 } else { 2961 switch (tp->mac_version) { 2962 case RTL_GIGA_MAC_VER_46 ... RTL_GIGA_MAC_VER_48: 2963 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_LAST: 2964 r8168_mac_ocp_modify(tp, 0xe092, 0x00ff, 0); 2965 break; 2966 default: 2967 break; 2968 } 2969 2970 switch (tp->mac_version) { 2971 case RTL_GIGA_MAC_VER_70: 2972 case RTL_GIGA_MAC_VER_80: 2973 val8 = RTL_R8(tp, INT_CFG0_8125) & ~INT_CFG0_CLKREQEN; 2974 RTL_W8(tp, INT_CFG0_8125, val8); 2975 break; 2976 default: 2977 rtl_mod_config2(tp, ClkReqEn, 0); 2978 break; 2979 } 2980 rtl_mod_config5(tp, ASPM_en, 0); 2981 } 2982 } 2983 2984 static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat, 2985 u16 tx_stat, u16 rx_dyn, u16 tx_dyn) 2986 { 2987 /* Usage of dynamic vs. static FIFO is controlled by bit 2988 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known. 2989 */ 2990 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn); 2991 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn); 2992 } 2993 2994 static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp, 2995 u8 low, u8 high) 2996 { 2997 /* FIFO thresholds for pause flow control */ 2998 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low); 2999 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high); 3000 } 3001 3002 static void rtl_hw_start_8168b(struct rtl8169_private *tp) 3003 { 3004 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3005 } 3006 3007 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp) 3008 { 3009 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down); 3010 3011 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3012 3013 rtl_disable_clock_request(tp); 3014 } 3015 3016 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp) 3017 { 3018 static const struct ephy_info e_info_8168cp[] = { 3019 { 0x01, 0, 0x0001 }, 3020 { 0x02, 0x0800, 0x1000 }, 3021 { 0x03, 0, 0x0042 }, 3022 { 0x06, 0x0080, 0x0000 }, 3023 { 0x07, 0, 0x2000 } 3024 }; 3025 3026 rtl_set_def_aspm_entry_latency(tp); 3027 3028 rtl_ephy_init(tp, e_info_8168cp); 3029 3030 __rtl_hw_start_8168cp(tp); 3031 } 3032 3033 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp) 3034 { 3035 rtl_set_def_aspm_entry_latency(tp); 3036 3037 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3038 } 3039 3040 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp) 3041 { 3042 rtl_set_def_aspm_entry_latency(tp); 3043 3044 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3045 3046 /* Magic. */ 3047 RTL_W8(tp, DBG_REG, 0x20); 3048 } 3049 3050 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp) 3051 { 3052 static const struct ephy_info e_info_8168c_1[] = { 3053 { 0x02, 0x0800, 0x1000 }, 3054 { 0x03, 0, 0x0002 }, 3055 { 0x06, 0x0080, 0x0000 } 3056 }; 3057 3058 rtl_set_def_aspm_entry_latency(tp); 3059 3060 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2); 3061 3062 rtl_ephy_init(tp, e_info_8168c_1); 3063 3064 __rtl_hw_start_8168cp(tp); 3065 } 3066 3067 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp) 3068 { 3069 static const struct ephy_info e_info_8168c_2[] = { 3070 { 0x01, 0, 0x0001 }, 3071 { 0x03, 0x0400, 0x0020 } 3072 }; 3073 3074 rtl_set_def_aspm_entry_latency(tp); 3075 3076 rtl_ephy_init(tp, e_info_8168c_2); 3077 3078 __rtl_hw_start_8168cp(tp); 3079 } 3080 3081 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp) 3082 { 3083 rtl_set_def_aspm_entry_latency(tp); 3084 3085 __rtl_hw_start_8168cp(tp); 3086 } 3087 3088 static void rtl_hw_start_8168d(struct rtl8169_private *tp) 3089 { 3090 rtl_set_def_aspm_entry_latency(tp); 3091 3092 rtl_disable_clock_request(tp); 3093 } 3094 3095 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp) 3096 { 3097 static const struct ephy_info e_info_8168d_4[] = { 3098 { 0x0b, 0x0000, 0x0048 }, 3099 { 0x19, 0x0020, 0x0050 }, 3100 { 0x0c, 0x0100, 0x0020 }, 3101 { 0x10, 0x0004, 0x0000 }, 3102 }; 3103 3104 rtl_set_def_aspm_entry_latency(tp); 3105 3106 rtl_ephy_init(tp, e_info_8168d_4); 3107 3108 rtl_enable_clock_request(tp); 3109 } 3110 3111 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp) 3112 { 3113 static const struct ephy_info e_info_8168e_1[] = { 3114 { 0x00, 0x0200, 0x0100 }, 3115 { 0x00, 0x0000, 0x0004 }, 3116 { 0x06, 0x0002, 0x0001 }, 3117 { 0x06, 0x0000, 0x0030 }, 3118 { 0x07, 0x0000, 0x2000 }, 3119 { 0x00, 0x0000, 0x0020 }, 3120 { 0x03, 0x5800, 0x2000 }, 3121 { 0x03, 0x0000, 0x0001 }, 3122 { 0x01, 0x0800, 0x1000 }, 3123 { 0x07, 0x0000, 0x4000 }, 3124 { 0x1e, 0x0000, 0x2000 }, 3125 { 0x19, 0xffff, 0xfe6c }, 3126 { 0x0a, 0x0000, 0x0040 } 3127 }; 3128 3129 rtl_set_def_aspm_entry_latency(tp); 3130 3131 rtl_ephy_init(tp, e_info_8168e_1); 3132 3133 rtl_disable_clock_request(tp); 3134 3135 /* Reset tx FIFO pointer */ 3136 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST); 3137 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST); 3138 3139 rtl_mod_config5(tp, Spi_en, 0); 3140 } 3141 3142 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp) 3143 { 3144 static const struct ephy_info e_info_8168e_2[] = { 3145 { 0x09, 0x0000, 0x0080 }, 3146 { 0x19, 0x0000, 0x0224 }, 3147 { 0x00, 0x0000, 0x0004 }, 3148 { 0x0c, 0x3df0, 0x0200 }, 3149 }; 3150 3151 rtl_set_def_aspm_entry_latency(tp); 3152 3153 rtl_ephy_init(tp, e_info_8168e_2); 3154 3155 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3156 rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000); 3157 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06); 3158 rtl_eri_set_bits(tp, 0x1d0, BIT(1)); 3159 rtl_reset_packet_filter(tp); 3160 rtl_eri_set_bits(tp, 0x1b0, BIT(4)); 3161 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050); 3162 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060); 3163 3164 rtl_disable_clock_request(tp); 3165 3166 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); 3167 3168 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN); 3169 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN); 3170 rtl_mod_config5(tp, Spi_en, 0); 3171 } 3172 3173 static void rtl_hw_start_8168f(struct rtl8169_private *tp) 3174 { 3175 rtl_set_def_aspm_entry_latency(tp); 3176 3177 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3178 rtl_eri_write(tp, 0xb8, ERIAR_MASK_1111, 0x0000); 3179 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06); 3180 rtl_reset_packet_filter(tp); 3181 rtl_eri_set_bits(tp, 0x1b0, BIT(4)); 3182 rtl_eri_set_bits(tp, 0x1d0, BIT(4) | BIT(1)); 3183 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050); 3184 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060); 3185 3186 rtl_disable_clock_request(tp); 3187 3188 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); 3189 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN); 3190 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN); 3191 rtl_mod_config5(tp, Spi_en, 0); 3192 } 3193 3194 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp) 3195 { 3196 static const struct ephy_info e_info_8168f_1[] = { 3197 { 0x06, 0x00c0, 0x0020 }, 3198 { 0x08, 0x0001, 0x0002 }, 3199 { 0x09, 0x0000, 0x0080 }, 3200 { 0x19, 0x0000, 0x0224 }, 3201 { 0x00, 0x0000, 0x0008 }, 3202 { 0x0c, 0x3df0, 0x0200 }, 3203 }; 3204 3205 rtl_hw_start_8168f(tp); 3206 3207 rtl_ephy_init(tp, e_info_8168f_1); 3208 } 3209 3210 static void rtl_hw_start_8411(struct rtl8169_private *tp) 3211 { 3212 static const struct ephy_info e_info_8168f_1[] = { 3213 { 0x06, 0x00c0, 0x0020 }, 3214 { 0x0f, 0xffff, 0x5200 }, 3215 { 0x19, 0x0000, 0x0224 }, 3216 { 0x00, 0x0000, 0x0008 }, 3217 { 0x0c, 0x3df0, 0x0200 }, 3218 }; 3219 3220 rtl_hw_start_8168f(tp); 3221 rtl_pcie_state_l2l3_disable(tp); 3222 3223 rtl_ephy_init(tp, e_info_8168f_1); 3224 } 3225 3226 static void rtl_hw_start_8168g(struct rtl8169_private *tp) 3227 { 3228 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06); 3229 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48); 3230 3231 rtl_set_def_aspm_entry_latency(tp); 3232 3233 rtl_reset_packet_filter(tp); 3234 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f); 3235 3236 rtl_disable_rxdvgate(tp); 3237 3238 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3239 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000); 3240 3241 rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06); 3242 rtl_eri_clear_bits(tp, 0x1b0, BIT(12)); 3243 3244 rtl_pcie_state_l2l3_disable(tp); 3245 } 3246 3247 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp) 3248 { 3249 static const struct ephy_info e_info_8168g_1[] = { 3250 { 0x00, 0x0008, 0x0000 }, 3251 { 0x0c, 0x3ff0, 0x0820 }, 3252 { 0x1e, 0x0000, 0x0001 }, 3253 { 0x19, 0x8000, 0x0000 } 3254 }; 3255 3256 rtl_hw_start_8168g(tp); 3257 rtl_ephy_init(tp, e_info_8168g_1); 3258 } 3259 3260 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp) 3261 { 3262 static const struct ephy_info e_info_8168g_2[] = { 3263 { 0x00, 0x0008, 0x0000 }, 3264 { 0x0c, 0x3ff0, 0x0820 }, 3265 { 0x19, 0xffff, 0x7c00 }, 3266 { 0x1e, 0xffff, 0x20eb }, 3267 { 0x0d, 0xffff, 0x1666 }, 3268 { 0x00, 0xffff, 0x10a3 }, 3269 { 0x06, 0xffff, 0xf050 }, 3270 { 0x04, 0x0000, 0x0010 }, 3271 { 0x1d, 0x4000, 0x0000 }, 3272 }; 3273 3274 rtl_hw_start_8168g(tp); 3275 rtl_ephy_init(tp, e_info_8168g_2); 3276 } 3277 3278 static void rtl8411b_fix_phy_down(struct rtl8169_private *tp) 3279 { 3280 static const u16 fix_data[] = { 3281 /* 0xf800 */ 0xe008, 0xe00a, 0xe00c, 0xe00e, 0xe027, 0xe04f, 0xe05e, 0xe065, 3282 /* 0xf810 */ 0xc602, 0xbe00, 0x0000, 0xc502, 0xbd00, 0x074c, 0xc302, 0xbb00, 3283 /* 0xf820 */ 0x080a, 0x6420, 0x48c2, 0x8c20, 0xc516, 0x64a4, 0x49c0, 0xf009, 3284 /* 0xf830 */ 0x74a2, 0x8ca5, 0x74a0, 0xc50e, 0x9ca2, 0x1c11, 0x9ca0, 0xe006, 3285 /* 0xf840 */ 0x74f8, 0x48c4, 0x8cf8, 0xc404, 0xbc00, 0xc403, 0xbc00, 0x0bf2, 3286 /* 0xf850 */ 0x0c0a, 0xe434, 0xd3c0, 0x49d9, 0xf01f, 0xc526, 0x64a5, 0x1400, 3287 /* 0xf860 */ 0xf007, 0x0c01, 0x8ca5, 0x1c15, 0xc51b, 0x9ca0, 0xe013, 0xc519, 3288 /* 0xf870 */ 0x74a0, 0x48c4, 0x8ca0, 0xc516, 0x74a4, 0x48c8, 0x48ca, 0x9ca4, 3289 /* 0xf880 */ 0xc512, 0x1b00, 0x9ba0, 0x1b1c, 0x483f, 0x9ba2, 0x1b04, 0xc508, 3290 /* 0xf890 */ 0x9ba0, 0xc505, 0xbd00, 0xc502, 0xbd00, 0x0300, 0x051e, 0xe434, 3291 /* 0xf8a0 */ 0xe018, 0xe092, 0xde20, 0xd3c0, 0xc50f, 0x76a4, 0x49e3, 0xf007, 3292 /* 0xf8b0 */ 0x49c0, 0xf103, 0xc607, 0xbe00, 0xc606, 0xbe00, 0xc602, 0xbe00, 3293 /* 0xf8c0 */ 0x0c4c, 0x0c28, 0x0c2c, 0xdc00, 0xc707, 0x1d00, 0x8de2, 0x48c1, 3294 /* 0xf8d0 */ 0xc502, 0xbd00, 0x00aa, 0xe0c0, 0xc502, 0xbd00, 0x0132 3295 }; 3296 unsigned long flags; 3297 int i; 3298 3299 raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); 3300 for (i = 0; i < ARRAY_SIZE(fix_data); i++) 3301 __r8168_mac_ocp_write(tp, 0xf800 + 2 * i, fix_data[i]); 3302 raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); 3303 } 3304 3305 static void rtl_hw_start_8411_2(struct rtl8169_private *tp) 3306 { 3307 static const struct ephy_info e_info_8411_2[] = { 3308 { 0x00, 0x0008, 0x0000 }, 3309 { 0x0c, 0x37d0, 0x0820 }, 3310 { 0x1e, 0x0000, 0x0001 }, 3311 { 0x19, 0x8021, 0x0000 }, 3312 { 0x1e, 0x0000, 0x2000 }, 3313 { 0x0d, 0x0100, 0x0200 }, 3314 { 0x00, 0x0000, 0x0080 }, 3315 { 0x06, 0x0000, 0x0010 }, 3316 { 0x04, 0x0000, 0x0010 }, 3317 { 0x1d, 0x0000, 0x4000 }, 3318 }; 3319 3320 rtl_hw_start_8168g(tp); 3321 3322 rtl_ephy_init(tp, e_info_8411_2); 3323 3324 /* The following Realtek-provided magic fixes an issue with the RX unit 3325 * getting confused after the PHY having been powered-down. 3326 */ 3327 r8168_mac_ocp_write(tp, 0xFC28, 0x0000); 3328 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000); 3329 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000); 3330 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000); 3331 r8168_mac_ocp_write(tp, 0xFC30, 0x0000); 3332 r8168_mac_ocp_write(tp, 0xFC32, 0x0000); 3333 r8168_mac_ocp_write(tp, 0xFC34, 0x0000); 3334 r8168_mac_ocp_write(tp, 0xFC36, 0x0000); 3335 mdelay(3); 3336 r8168_mac_ocp_write(tp, 0xFC26, 0x0000); 3337 3338 rtl8411b_fix_phy_down(tp); 3339 3340 r8168_mac_ocp_write(tp, 0xFC26, 0x8000); 3341 3342 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743); 3343 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801); 3344 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9); 3345 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD); 3346 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25); 3347 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9); 3348 r8168_mac_ocp_write(tp, 0xFC36, 0x012D); 3349 } 3350 3351 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp) 3352 { 3353 static const struct ephy_info e_info_8168h_1[] = { 3354 { 0x1e, 0x0800, 0x0001 }, 3355 { 0x1d, 0x0000, 0x0800 }, 3356 { 0x05, 0xffff, 0x2089 }, 3357 { 0x06, 0xffff, 0x5881 }, 3358 { 0x04, 0xffff, 0x854a }, 3359 { 0x01, 0xffff, 0x068b } 3360 }; 3361 int rg_saw_cnt; 3362 3363 rtl_ephy_init(tp, e_info_8168h_1); 3364 3365 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06); 3366 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48); 3367 3368 rtl_set_def_aspm_entry_latency(tp); 3369 3370 rtl_reset_packet_filter(tp); 3371 3372 rtl_eri_set_bits(tp, 0xdc, 0x001c); 3373 3374 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87); 3375 3376 rtl_disable_rxdvgate(tp); 3377 3378 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3379 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000); 3380 3381 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN); 3382 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN); 3383 3384 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN); 3385 3386 rtl_eri_clear_bits(tp, 0x1b0, BIT(12)); 3387 3388 rtl_pcie_state_l2l3_disable(tp); 3389 3390 rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff; 3391 if (rg_saw_cnt > 0) { 3392 u16 sw_cnt_1ms_ini; 3393 3394 sw_cnt_1ms_ini = 16000000/rg_saw_cnt; 3395 sw_cnt_1ms_ini &= 0x0fff; 3396 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini); 3397 } 3398 3399 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0000); 3400 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008); 3401 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f); 3402 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f); 3403 3404 r8168_mac_ocp_write(tp, 0xe63e, 0x0001); 3405 r8168_mac_ocp_write(tp, 0xe63e, 0x0000); 3406 r8168_mac_ocp_write(tp, 0xc094, 0x0000); 3407 r8168_mac_ocp_write(tp, 0xc09e, 0x0000); 3408 } 3409 3410 static void rtl_hw_start_8168ep(struct rtl8169_private *tp) 3411 { 3412 rtl8168ep_stop_cmac(tp); 3413 3414 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06); 3415 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f); 3416 3417 rtl_set_def_aspm_entry_latency(tp); 3418 3419 rtl_reset_packet_filter(tp); 3420 3421 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87); 3422 3423 rtl_disable_rxdvgate(tp); 3424 3425 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3426 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000); 3427 3428 rtl_w0w1_eri(tp, 0x2fc, 0x01, 0x06); 3429 3430 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN); 3431 3432 rtl_pcie_state_l2l3_disable(tp); 3433 } 3434 3435 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp) 3436 { 3437 static const struct ephy_info e_info_8168ep_3[] = { 3438 { 0x00, 0x0000, 0x0080 }, 3439 { 0x0d, 0x0100, 0x0200 }, 3440 { 0x19, 0x8021, 0x0000 }, 3441 { 0x1e, 0x0000, 0x2000 }, 3442 }; 3443 3444 rtl_ephy_init(tp, e_info_8168ep_3); 3445 3446 rtl_hw_start_8168ep(tp); 3447 3448 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN); 3449 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN); 3450 3451 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271); 3452 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000); 3453 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080); 3454 } 3455 3456 static void rtl_hw_start_8117(struct rtl8169_private *tp) 3457 { 3458 static const struct ephy_info e_info_8117[] = { 3459 { 0x19, 0x0040, 0x1100 }, 3460 { 0x59, 0x0040, 0x1100 }, 3461 }; 3462 int rg_saw_cnt; 3463 3464 rtl8168ep_stop_cmac(tp); 3465 rtl_ephy_init(tp, e_info_8117); 3466 3467 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06); 3468 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f); 3469 3470 rtl_set_def_aspm_entry_latency(tp); 3471 3472 rtl_reset_packet_filter(tp); 3473 3474 rtl_eri_set_bits(tp, 0xd4, 0x0010); 3475 3476 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87); 3477 3478 rtl_disable_rxdvgate(tp); 3479 3480 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3481 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000); 3482 3483 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN); 3484 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN); 3485 3486 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN); 3487 3488 rtl_eri_clear_bits(tp, 0x1b0, BIT(12)); 3489 3490 rtl_pcie_state_l2l3_disable(tp); 3491 3492 rg_saw_cnt = phy_read_paged(tp->phydev, 0x0c42, 0x13) & 0x3fff; 3493 if (rg_saw_cnt > 0) { 3494 u16 sw_cnt_1ms_ini; 3495 3496 sw_cnt_1ms_ini = (16000000 / rg_saw_cnt) & 0x0fff; 3497 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini); 3498 } 3499 3500 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0000); 3501 r8168_mac_ocp_write(tp, 0xea80, 0x0003); 3502 r8168_mac_ocp_modify(tp, 0xe052, 0x0000, 0x0009); 3503 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f); 3504 3505 r8168_mac_ocp_write(tp, 0xe63e, 0x0001); 3506 r8168_mac_ocp_write(tp, 0xe63e, 0x0000); 3507 r8168_mac_ocp_write(tp, 0xc094, 0x0000); 3508 r8168_mac_ocp_write(tp, 0xc09e, 0x0000); 3509 3510 /* firmware is for MAC only */ 3511 r8169_apply_firmware(tp); 3512 } 3513 3514 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp) 3515 { 3516 static const struct ephy_info e_info_8102e_1[] = { 3517 { 0x01, 0, 0x6e65 }, 3518 { 0x02, 0, 0x091f }, 3519 { 0x03, 0, 0xc2f9 }, 3520 { 0x06, 0, 0xafb5 }, 3521 { 0x07, 0, 0x0e00 }, 3522 { 0x19, 0, 0xec80 }, 3523 { 0x01, 0, 0x2e65 }, 3524 { 0x01, 0, 0x6e65 } 3525 }; 3526 u8 cfg1; 3527 3528 rtl_set_def_aspm_entry_latency(tp); 3529 3530 RTL_W8(tp, DBG_REG, FIX_NAK_1); 3531 3532 RTL_W8(tp, Config1, 3533 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable); 3534 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3535 3536 cfg1 = RTL_R8(tp, Config1); 3537 if ((cfg1 & LEDS0) && (cfg1 & LEDS1)) 3538 RTL_W8(tp, Config1, cfg1 & ~LEDS0); 3539 3540 rtl_ephy_init(tp, e_info_8102e_1); 3541 } 3542 3543 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp) 3544 { 3545 rtl_set_def_aspm_entry_latency(tp); 3546 3547 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable); 3548 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3549 } 3550 3551 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp) 3552 { 3553 rtl_hw_start_8102e_2(tp); 3554 3555 rtl_ephy_write(tp, 0x03, 0xc2f9); 3556 } 3557 3558 static void rtl_hw_start_8401(struct rtl8169_private *tp) 3559 { 3560 static const struct ephy_info e_info_8401[] = { 3561 { 0x01, 0xffff, 0x6fe5 }, 3562 { 0x03, 0xffff, 0x0599 }, 3563 { 0x06, 0xffff, 0xaf25 }, 3564 { 0x07, 0xffff, 0x8e68 }, 3565 }; 3566 3567 rtl_ephy_init(tp, e_info_8401); 3568 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en); 3569 } 3570 3571 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp) 3572 { 3573 static const struct ephy_info e_info_8105e_1[] = { 3574 { 0x07, 0, 0x4000 }, 3575 { 0x19, 0, 0x0200 }, 3576 { 0x19, 0, 0x0020 }, 3577 { 0x1e, 0, 0x2000 }, 3578 { 0x03, 0, 0x0001 }, 3579 { 0x19, 0, 0x0100 }, 3580 { 0x19, 0, 0x0004 }, 3581 { 0x0a, 0, 0x0020 } 3582 }; 3583 3584 /* Force LAN exit from ASPM if Rx/Tx are not idle */ 3585 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800); 3586 3587 /* Disable Early Tally Counter */ 3588 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000); 3589 3590 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET); 3591 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN); 3592 3593 rtl_ephy_init(tp, e_info_8105e_1); 3594 3595 rtl_pcie_state_l2l3_disable(tp); 3596 } 3597 3598 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp) 3599 { 3600 rtl_hw_start_8105e_1(tp); 3601 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000); 3602 } 3603 3604 static void rtl_hw_start_8402(struct rtl8169_private *tp) 3605 { 3606 static const struct ephy_info e_info_8402[] = { 3607 { 0x19, 0xffff, 0xff64 }, 3608 { 0x1e, 0, 0x4000 } 3609 }; 3610 3611 rtl_set_def_aspm_entry_latency(tp); 3612 3613 /* Force LAN exit from ASPM if Rx/Tx are not idle */ 3614 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800); 3615 3616 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); 3617 3618 rtl_ephy_init(tp, e_info_8402); 3619 3620 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06); 3621 rtl_reset_packet_filter(tp); 3622 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000); 3623 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000); 3624 rtl_w0w1_eri(tp, 0x0d4, 0x0e00, 0xff00); 3625 3626 /* disable EEE */ 3627 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000); 3628 3629 rtl_pcie_state_l2l3_disable(tp); 3630 } 3631 3632 static void rtl_hw_start_8106(struct rtl8169_private *tp) 3633 { 3634 /* Force LAN exit from ASPM if Rx/Tx are not idle */ 3635 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800); 3636 3637 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN); 3638 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET); 3639 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN); 3640 3641 /* L0 7us, L1 32us - needed to avoid issues with link-up detection */ 3642 rtl_set_aspm_entry_latency(tp, 0x2f); 3643 3644 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000); 3645 3646 /* disable EEE */ 3647 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000); 3648 3649 rtl_pcie_state_l2l3_disable(tp); 3650 } 3651 3652 DECLARE_RTL_COND(rtl_mac_ocp_e00e_cond) 3653 { 3654 return r8168_mac_ocp_read(tp, 0xe00e) & BIT(13); 3655 } 3656 3657 static void rtl_hw_start_8125_common(struct rtl8169_private *tp) 3658 { 3659 rtl_pcie_state_l2l3_disable(tp); 3660 3661 RTL_W16(tp, 0x382, 0x221b); 3662 RTL_W32(tp, RSS_CTRL_8125, 0); 3663 RTL_W16(tp, Q_NUM_CTRL_8125, 0); 3664 3665 /* disable UPS */ 3666 r8168_mac_ocp_modify(tp, 0xd40a, 0x0010, 0x0000); 3667 3668 RTL_W8(tp, Config1, RTL_R8(tp, Config1) & ~0x10); 3669 3670 r8168_mac_ocp_write(tp, 0xc140, 0xffff); 3671 r8168_mac_ocp_write(tp, 0xc142, 0xffff); 3672 3673 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x03a9); 3674 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000); 3675 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080); 3676 3677 /* disable new tx descriptor format */ 3678 r8168_mac_ocp_modify(tp, 0xeb58, 0x0001, 0x0000); 3679 3680 if (tp->mac_version == RTL_GIGA_MAC_VER_70 || 3681 tp->mac_version == RTL_GIGA_MAC_VER_80) 3682 RTL_W8(tp, 0xD8, RTL_R8(tp, 0xD8) & ~0x02); 3683 3684 if (tp->mac_version == RTL_GIGA_MAC_VER_80) 3685 r8168_mac_ocp_modify(tp, 0xe614, 0x0f00, 0x0f00); 3686 else if (tp->mac_version == RTL_GIGA_MAC_VER_70) 3687 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0400); 3688 else if (tp->mac_version == RTL_GIGA_MAC_VER_63) 3689 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0200); 3690 else 3691 r8168_mac_ocp_modify(tp, 0xe614, 0x0700, 0x0300); 3692 3693 if (tp->mac_version == RTL_GIGA_MAC_VER_63) 3694 r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0000); 3695 else 3696 r8168_mac_ocp_modify(tp, 0xe63e, 0x0c30, 0x0020); 3697 3698 r8168_mac_ocp_modify(tp, 0xc0b4, 0x0000, 0x000c); 3699 r8168_mac_ocp_modify(tp, 0xeb6a, 0x00ff, 0x0033); 3700 r8168_mac_ocp_modify(tp, 0xeb50, 0x03e0, 0x0040); 3701 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0000); 3702 r8168_mac_ocp_modify(tp, 0xe040, 0x1000, 0x0000); 3703 r8168_mac_ocp_modify(tp, 0xea1c, 0x0003, 0x0001); 3704 if (tp->mac_version == RTL_GIGA_MAC_VER_70 || 3705 tp->mac_version == RTL_GIGA_MAC_VER_80) 3706 r8168_mac_ocp_modify(tp, 0xea1c, 0x0300, 0x0000); 3707 else 3708 r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000); 3709 r8168_mac_ocp_modify(tp, 0xe0c0, 0x4f0f, 0x4403); 3710 r8168_mac_ocp_modify(tp, 0xe052, 0x0080, 0x0068); 3711 r8168_mac_ocp_modify(tp, 0xd430, 0x0fff, 0x047f); 3712 3713 r8168_mac_ocp_modify(tp, 0xea1c, 0x0004, 0x0000); 3714 r8168_mac_ocp_modify(tp, 0xeb54, 0x0000, 0x0001); 3715 udelay(1); 3716 r8168_mac_ocp_modify(tp, 0xeb54, 0x0001, 0x0000); 3717 RTL_W16(tp, 0x1880, RTL_R16(tp, 0x1880) & ~0x0030); 3718 3719 r8168_mac_ocp_write(tp, 0xe098, 0xc302); 3720 3721 rtl_loop_wait_low(tp, &rtl_mac_ocp_e00e_cond, 1000, 10); 3722 3723 rtl_disable_rxdvgate(tp); 3724 } 3725 3726 static void rtl_hw_start_8125a_2(struct rtl8169_private *tp) 3727 { 3728 static const struct ephy_info e_info_8125a_2[] = { 3729 { 0x04, 0xffff, 0xd000 }, 3730 { 0x0a, 0xffff, 0x8653 }, 3731 { 0x23, 0xffff, 0xab66 }, 3732 { 0x20, 0xffff, 0x9455 }, 3733 { 0x21, 0xffff, 0x99ff }, 3734 { 0x29, 0xffff, 0xfe04 }, 3735 3736 { 0x44, 0xffff, 0xd000 }, 3737 { 0x4a, 0xffff, 0x8653 }, 3738 { 0x63, 0xffff, 0xab66 }, 3739 { 0x60, 0xffff, 0x9455 }, 3740 { 0x61, 0xffff, 0x99ff }, 3741 { 0x69, 0xffff, 0xfe04 }, 3742 }; 3743 3744 rtl_set_def_aspm_entry_latency(tp); 3745 rtl_ephy_init(tp, e_info_8125a_2); 3746 rtl_hw_start_8125_common(tp); 3747 } 3748 3749 static void rtl_hw_start_8125b(struct rtl8169_private *tp) 3750 { 3751 static const struct ephy_info e_info_8125b[] = { 3752 { 0x0b, 0xffff, 0xa908 }, 3753 { 0x1e, 0xffff, 0x20eb }, 3754 { 0x4b, 0xffff, 0xa908 }, 3755 { 0x5e, 0xffff, 0x20eb }, 3756 { 0x22, 0x0030, 0x0020 }, 3757 { 0x62, 0x0030, 0x0020 }, 3758 }; 3759 3760 rtl_set_def_aspm_entry_latency(tp); 3761 rtl_ephy_init(tp, e_info_8125b); 3762 rtl_hw_start_8125_common(tp); 3763 } 3764 3765 static void rtl_hw_start_8125d(struct rtl8169_private *tp) 3766 { 3767 rtl_set_def_aspm_entry_latency(tp); 3768 rtl_hw_start_8125_common(tp); 3769 } 3770 3771 static void rtl_hw_start_8126a(struct rtl8169_private *tp) 3772 { 3773 rtl_disable_zrxdc_timeout(tp); 3774 rtl_set_def_aspm_entry_latency(tp); 3775 rtl_hw_start_8125_common(tp); 3776 } 3777 3778 static void rtl_hw_start_8127a(struct rtl8169_private *tp) 3779 { 3780 rtl_set_def_aspm_entry_latency(tp); 3781 rtl_hw_start_8125_common(tp); 3782 } 3783 3784 static void rtl_hw_config(struct rtl8169_private *tp) 3785 { 3786 static const rtl_generic_fct hw_configs[] = { 3787 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1, 3788 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3, 3789 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2, 3790 [RTL_GIGA_MAC_VER_10] = NULL, 3791 [RTL_GIGA_MAC_VER_14] = rtl_hw_start_8401, 3792 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168b, 3793 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1, 3794 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1, 3795 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2, 3796 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_2, 3797 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4, 3798 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2, 3799 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3, 3800 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d, 3801 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d, 3802 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4, 3803 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1, 3804 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2, 3805 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168d, 3806 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1, 3807 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1, 3808 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2, 3809 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1, 3810 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1, 3811 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402, 3812 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411, 3813 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106, 3814 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1, 3815 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2, 3816 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2, 3817 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2, 3818 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1, 3819 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1, 3820 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3, 3821 [RTL_GIGA_MAC_VER_52] = rtl_hw_start_8117, 3822 [RTL_GIGA_MAC_VER_61] = rtl_hw_start_8125a_2, 3823 [RTL_GIGA_MAC_VER_63] = rtl_hw_start_8125b, 3824 [RTL_GIGA_MAC_VER_64] = rtl_hw_start_8125d, 3825 [RTL_GIGA_MAC_VER_66] = rtl_hw_start_8125d, 3826 [RTL_GIGA_MAC_VER_70] = rtl_hw_start_8126a, 3827 [RTL_GIGA_MAC_VER_80] = rtl_hw_start_8127a, 3828 }; 3829 3830 if (hw_configs[tp->mac_version]) 3831 hw_configs[tp->mac_version](tp); 3832 } 3833 3834 static void rtl_hw_start_8125(struct rtl8169_private *tp) 3835 { 3836 int i; 3837 3838 RTL_W8(tp, INT_CFG0_8125, 0x00); 3839 3840 /* disable interrupt coalescing */ 3841 switch (tp->mac_version) { 3842 case RTL_GIGA_MAC_VER_61: 3843 case RTL_GIGA_MAC_VER_64: 3844 case RTL_GIGA_MAC_VER_66: 3845 case RTL_GIGA_MAC_VER_80: 3846 for (i = 0xa00; i < 0xb00; i += 4) 3847 RTL_W32(tp, i, 0); 3848 if (tp->mac_version == RTL_GIGA_MAC_VER_80) 3849 RTL_W16(tp, INT_CFG1_8125, 0x0000); 3850 break; 3851 case RTL_GIGA_MAC_VER_63: 3852 case RTL_GIGA_MAC_VER_70: 3853 for (i = 0xa00; i < 0xa80; i += 4) 3854 RTL_W32(tp, i, 0); 3855 RTL_W16(tp, INT_CFG1_8125, 0x0000); 3856 break; 3857 default: 3858 break; 3859 } 3860 3861 /* enable extended tally counter */ 3862 r8168_mac_ocp_modify(tp, 0xea84, 0, BIT(1) | BIT(0)); 3863 3864 rtl_hw_config(tp); 3865 } 3866 3867 static void rtl_hw_start_8168(struct rtl8169_private *tp) 3868 { 3869 if (rtl_is_8168evl_up(tp)) 3870 RTL_W8(tp, MaxTxPacketSize, EarlySize); 3871 else 3872 RTL_W8(tp, MaxTxPacketSize, TxPacketMax); 3873 3874 rtl_hw_config(tp); 3875 3876 /* disable interrupt coalescing */ 3877 RTL_W16(tp, IntrMitigate, 0x0000); 3878 } 3879 3880 static void rtl_hw_start_8169(struct rtl8169_private *tp) 3881 { 3882 RTL_W8(tp, EarlyTxThres, NoEarlyTx); 3883 3884 tp->cp_cmd |= PCIMulRW; 3885 3886 if (tp->mac_version == RTL_GIGA_MAC_VER_02 || 3887 tp->mac_version == RTL_GIGA_MAC_VER_03) 3888 tp->cp_cmd |= EnAnaPLL; 3889 3890 RTL_W16(tp, CPlusCmd, tp->cp_cmd); 3891 3892 rtl8169_set_magic_reg(tp); 3893 3894 /* disable interrupt coalescing */ 3895 RTL_W16(tp, IntrMitigate, 0x0000); 3896 } 3897 3898 static void rtl_hw_start(struct rtl8169_private *tp) 3899 { 3900 rtl_unlock_config_regs(tp); 3901 /* disable aspm and clock request before ephy access */ 3902 rtl_hw_aspm_clkreq_enable(tp, false); 3903 RTL_W16(tp, CPlusCmd, tp->cp_cmd); 3904 3905 rtl_set_eee_txidle_timer(tp); 3906 3907 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) 3908 rtl_hw_start_8169(tp); 3909 else if (rtl_is_8125(tp)) 3910 rtl_hw_start_8125(tp); 3911 else 3912 rtl_hw_start_8168(tp); 3913 3914 rtl_enable_exit_l1(tp); 3915 rtl_hw_aspm_clkreq_enable(tp, true); 3916 rtl_set_rx_max_size(tp); 3917 rtl_set_rx_tx_desc_registers(tp); 3918 rtl_lock_config_regs(tp); 3919 3920 rtl_jumbo_config(tp); 3921 3922 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */ 3923 rtl_pci_commit(tp); 3924 3925 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb); 3926 rtl_init_rxcfg(tp); 3927 rtl_set_tx_config_registers(tp); 3928 rtl_set_rx_config_features(tp, tp->dev->features); 3929 rtl_set_rx_mode(tp->dev); 3930 rtl_irq_enable(tp); 3931 } 3932 3933 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu) 3934 { 3935 struct rtl8169_private *tp = netdev_priv(dev); 3936 3937 WRITE_ONCE(dev->mtu, new_mtu); 3938 netdev_update_features(dev); 3939 rtl_jumbo_config(tp); 3940 rtl_set_eee_txidle_timer(tp); 3941 3942 return 0; 3943 } 3944 3945 static void rtl8169_mark_to_asic(struct RxDesc *desc) 3946 { 3947 u32 eor = le32_to_cpu(desc->opts1) & RingEnd; 3948 3949 desc->opts2 = 0; 3950 /* Force memory writes to complete before releasing descriptor */ 3951 dma_wmb(); 3952 WRITE_ONCE(desc->opts1, cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE)); 3953 } 3954 3955 static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp, 3956 struct RxDesc *desc) 3957 { 3958 struct device *d = tp_to_dev(tp); 3959 int node = dev_to_node(d); 3960 dma_addr_t mapping; 3961 struct page *data; 3962 3963 data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE)); 3964 if (!data) 3965 return NULL; 3966 3967 mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE); 3968 if (unlikely(dma_mapping_error(d, mapping))) { 3969 netdev_err(tp->dev, "Failed to map RX DMA!\n"); 3970 __free_pages(data, get_order(R8169_RX_BUF_SIZE)); 3971 return NULL; 3972 } 3973 3974 desc->addr = cpu_to_le64(mapping); 3975 rtl8169_mark_to_asic(desc); 3976 3977 return data; 3978 } 3979 3980 static void rtl8169_rx_clear(struct rtl8169_private *tp) 3981 { 3982 int i; 3983 3984 for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) { 3985 dma_unmap_page(tp_to_dev(tp), 3986 le64_to_cpu(tp->RxDescArray[i].addr), 3987 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE); 3988 __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE)); 3989 tp->Rx_databuff[i] = NULL; 3990 tp->RxDescArray[i].addr = 0; 3991 tp->RxDescArray[i].opts1 = 0; 3992 } 3993 } 3994 3995 static int rtl8169_rx_fill(struct rtl8169_private *tp) 3996 { 3997 int i; 3998 3999 for (i = 0; i < NUM_RX_DESC; i++) { 4000 struct page *data; 4001 4002 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i); 4003 if (!data) { 4004 rtl8169_rx_clear(tp); 4005 return -ENOMEM; 4006 } 4007 tp->Rx_databuff[i] = data; 4008 } 4009 4010 /* mark as last descriptor in the ring */ 4011 tp->RxDescArray[NUM_RX_DESC - 1].opts1 |= cpu_to_le32(RingEnd); 4012 4013 return 0; 4014 } 4015 4016 static int rtl8169_init_ring(struct rtl8169_private *tp) 4017 { 4018 rtl8169_init_ring_indexes(tp); 4019 4020 memset(tp->tx_skb, 0, sizeof(tp->tx_skb)); 4021 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff)); 4022 4023 return rtl8169_rx_fill(tp); 4024 } 4025 4026 static void rtl8169_unmap_tx_skb(struct rtl8169_private *tp, unsigned int entry) 4027 { 4028 struct ring_info *tx_skb = tp->tx_skb + entry; 4029 struct TxDesc *desc = tp->TxDescArray + entry; 4030 4031 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), tx_skb->len, 4032 DMA_TO_DEVICE); 4033 memset(desc, 0, sizeof(*desc)); 4034 memset(tx_skb, 0, sizeof(*tx_skb)); 4035 } 4036 4037 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start, 4038 unsigned int n) 4039 { 4040 unsigned int i; 4041 4042 for (i = 0; i < n; i++) { 4043 unsigned int entry = (start + i) % NUM_TX_DESC; 4044 struct ring_info *tx_skb = tp->tx_skb + entry; 4045 unsigned int len = tx_skb->len; 4046 4047 if (len) { 4048 struct sk_buff *skb = tx_skb->skb; 4049 4050 rtl8169_unmap_tx_skb(tp, entry); 4051 if (skb) 4052 dev_consume_skb_any(skb); 4053 } 4054 } 4055 } 4056 4057 static void rtl8169_tx_clear(struct rtl8169_private *tp) 4058 { 4059 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC); 4060 netdev_reset_queue(tp->dev); 4061 } 4062 4063 static void rtl8169_cleanup(struct rtl8169_private *tp) 4064 { 4065 napi_disable(&tp->napi); 4066 4067 /* Give a racing hard_start_xmit a few cycles to complete. */ 4068 synchronize_net(); 4069 4070 /* Disable interrupts */ 4071 rtl8169_irq_mask_and_ack(tp); 4072 4073 rtl_rx_close(tp); 4074 4075 switch (tp->mac_version) { 4076 case RTL_GIGA_MAC_VER_28: 4077 case RTL_GIGA_MAC_VER_31: 4078 rtl_loop_wait_low(tp, &rtl_npq_cond, 20, 2000); 4079 break; 4080 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38: 4081 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq); 4082 rtl_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666); 4083 break; 4084 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_LAST: 4085 rtl_enable_rxdvgate(tp); 4086 fsleep(2000); 4087 break; 4088 default: 4089 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq); 4090 fsleep(100); 4091 break; 4092 } 4093 4094 rtl_hw_reset(tp); 4095 4096 rtl8169_tx_clear(tp); 4097 rtl8169_init_ring_indexes(tp); 4098 } 4099 4100 static void rtl_reset_work(struct rtl8169_private *tp) 4101 { 4102 int i; 4103 4104 netif_stop_queue(tp->dev); 4105 4106 rtl8169_cleanup(tp); 4107 4108 for (i = 0; i < NUM_RX_DESC; i++) 4109 rtl8169_mark_to_asic(tp->RxDescArray + i); 4110 4111 napi_enable(&tp->napi); 4112 rtl_hw_start(tp); 4113 } 4114 4115 static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue) 4116 { 4117 struct rtl8169_private *tp = netdev_priv(dev); 4118 4119 rtl_schedule_task(tp, RTL_FLAG_TASK_TX_TIMEOUT); 4120 } 4121 4122 static int rtl8169_tx_map(struct rtl8169_private *tp, const u32 *opts, u32 len, 4123 void *addr, unsigned int entry, bool desc_own) 4124 { 4125 struct TxDesc *txd = tp->TxDescArray + entry; 4126 struct device *d = tp_to_dev(tp); 4127 dma_addr_t mapping; 4128 u32 opts1; 4129 int ret; 4130 4131 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE); 4132 ret = dma_mapping_error(d, mapping); 4133 if (unlikely(ret)) { 4134 if (net_ratelimit()) 4135 netdev_err(tp->dev, "Failed to map TX data!\n"); 4136 return ret; 4137 } 4138 4139 txd->addr = cpu_to_le64(mapping); 4140 txd->opts2 = cpu_to_le32(opts[1]); 4141 4142 opts1 = opts[0] | len; 4143 if (entry == NUM_TX_DESC - 1) 4144 opts1 |= RingEnd; 4145 if (desc_own) 4146 opts1 |= DescOwn; 4147 txd->opts1 = cpu_to_le32(opts1); 4148 4149 tp->tx_skb[entry].len = len; 4150 4151 return 0; 4152 } 4153 4154 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb, 4155 const u32 *opts, unsigned int entry) 4156 { 4157 struct skb_shared_info *info = skb_shinfo(skb); 4158 unsigned int cur_frag; 4159 4160 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) { 4161 const skb_frag_t *frag = info->frags + cur_frag; 4162 void *addr = skb_frag_address(frag); 4163 u32 len = skb_frag_size(frag); 4164 4165 entry = (entry + 1) % NUM_TX_DESC; 4166 4167 if (unlikely(rtl8169_tx_map(tp, opts, len, addr, entry, true))) 4168 goto err_out; 4169 } 4170 4171 return 0; 4172 4173 err_out: 4174 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag); 4175 return -EIO; 4176 } 4177 4178 static bool rtl_skb_is_udp(struct sk_buff *skb) 4179 { 4180 int no = skb_network_offset(skb); 4181 struct ipv6hdr *i6h, _i6h; 4182 struct iphdr *ih, _ih; 4183 4184 switch (vlan_get_protocol(skb)) { 4185 case htons(ETH_P_IP): 4186 ih = skb_header_pointer(skb, no, sizeof(_ih), &_ih); 4187 return ih && ih->protocol == IPPROTO_UDP; 4188 case htons(ETH_P_IPV6): 4189 i6h = skb_header_pointer(skb, no, sizeof(_i6h), &_i6h); 4190 return i6h && i6h->nexthdr == IPPROTO_UDP; 4191 default: 4192 return false; 4193 } 4194 } 4195 4196 #define RTL_MIN_PATCH_LEN 47 4197 4198 /* see rtl8125_get_patch_pad_len() in r8125 vendor driver */ 4199 static unsigned int rtl8125_quirk_udp_padto(struct rtl8169_private *tp, 4200 struct sk_buff *skb) 4201 { 4202 unsigned int padto = 0, len = skb->len; 4203 4204 if (len < 128 + RTL_MIN_PATCH_LEN && rtl_skb_is_udp(skb) && 4205 skb_transport_header_was_set(skb)) { 4206 unsigned int trans_data_len = skb_tail_pointer(skb) - 4207 skb_transport_header(skb); 4208 4209 if (trans_data_len >= offsetof(struct udphdr, len) && 4210 trans_data_len < RTL_MIN_PATCH_LEN) { 4211 u16 dest = ntohs(udp_hdr(skb)->dest); 4212 4213 /* dest is a standard PTP port */ 4214 if (dest == 319 || dest == 320) 4215 padto = len + RTL_MIN_PATCH_LEN - trans_data_len; 4216 } 4217 4218 if (trans_data_len < sizeof(struct udphdr)) 4219 padto = max_t(unsigned int, padto, 4220 len + sizeof(struct udphdr) - trans_data_len); 4221 } 4222 4223 return padto; 4224 } 4225 4226 static unsigned int rtl_quirk_packet_padto(struct rtl8169_private *tp, 4227 struct sk_buff *skb) 4228 { 4229 unsigned int padto = 0; 4230 4231 switch (tp->mac_version) { 4232 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_63: 4233 padto = rtl8125_quirk_udp_padto(tp, skb); 4234 break; 4235 default: 4236 break; 4237 } 4238 4239 switch (tp->mac_version) { 4240 case RTL_GIGA_MAC_VER_34: 4241 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_LAST: 4242 padto = max_t(unsigned int, padto, ETH_ZLEN); 4243 break; 4244 default: 4245 break; 4246 } 4247 4248 return padto; 4249 } 4250 4251 static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts) 4252 { 4253 u32 mss = skb_shinfo(skb)->gso_size; 4254 4255 if (mss) { 4256 opts[0] |= TD_LSO; 4257 opts[0] |= mss << TD0_MSS_SHIFT; 4258 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 4259 const struct iphdr *ip = ip_hdr(skb); 4260 4261 if (ip->protocol == IPPROTO_TCP) 4262 opts[0] |= TD0_IP_CS | TD0_TCP_CS; 4263 else if (ip->protocol == IPPROTO_UDP) 4264 opts[0] |= TD0_IP_CS | TD0_UDP_CS; 4265 else 4266 WARN_ON_ONCE(1); 4267 } 4268 } 4269 4270 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp, 4271 struct sk_buff *skb, u32 *opts) 4272 { 4273 struct skb_shared_info *shinfo = skb_shinfo(skb); 4274 u32 mss = shinfo->gso_size; 4275 4276 if (mss) { 4277 if (shinfo->gso_type & SKB_GSO_TCPV4) { 4278 opts[0] |= TD1_GTSENV4; 4279 } else if (shinfo->gso_type & SKB_GSO_TCPV6) { 4280 if (skb_cow_head(skb, 0)) 4281 return false; 4282 4283 tcp_v6_gso_csum_prep(skb); 4284 opts[0] |= TD1_GTSENV6; 4285 } else { 4286 WARN_ON_ONCE(1); 4287 } 4288 4289 opts[0] |= skb_transport_offset(skb) << GTTCPHO_SHIFT; 4290 opts[1] |= mss << TD1_MSS_SHIFT; 4291 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 4292 u8 ip_protocol; 4293 4294 switch (vlan_get_protocol(skb)) { 4295 case htons(ETH_P_IP): 4296 opts[1] |= TD1_IPv4_CS; 4297 ip_protocol = ip_hdr(skb)->protocol; 4298 break; 4299 4300 case htons(ETH_P_IPV6): 4301 opts[1] |= TD1_IPv6_CS; 4302 ip_protocol = ipv6_hdr(skb)->nexthdr; 4303 break; 4304 4305 default: 4306 ip_protocol = IPPROTO_RAW; 4307 break; 4308 } 4309 4310 if (ip_protocol == IPPROTO_TCP) 4311 opts[1] |= TD1_TCP_CS; 4312 else if (ip_protocol == IPPROTO_UDP) 4313 opts[1] |= TD1_UDP_CS; 4314 else 4315 WARN_ON_ONCE(1); 4316 4317 opts[1] |= skb_transport_offset(skb) << TCPHO_SHIFT; 4318 } else { 4319 unsigned int padto = rtl_quirk_packet_padto(tp, skb); 4320 4321 /* skb_padto would free the skb on error */ 4322 return !__skb_put_padto(skb, padto, false); 4323 } 4324 4325 return true; 4326 } 4327 4328 static unsigned int rtl_tx_slots_avail(struct rtl8169_private *tp) 4329 { 4330 return READ_ONCE(tp->dirty_tx) + NUM_TX_DESC - READ_ONCE(tp->cur_tx); 4331 } 4332 4333 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */ 4334 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp) 4335 { 4336 switch (tp->mac_version) { 4337 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06: 4338 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17: 4339 return false; 4340 default: 4341 return true; 4342 } 4343 } 4344 4345 static void rtl8169_doorbell(struct rtl8169_private *tp) 4346 { 4347 if (rtl_is_8125(tp)) 4348 RTL_W16(tp, TxPoll_8125, BIT(0)); 4349 else 4350 RTL_W8(tp, TxPoll, NPQ); 4351 } 4352 4353 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb, 4354 struct net_device *dev) 4355 { 4356 struct rtl8169_private *tp = netdev_priv(dev); 4357 unsigned int entry = tp->cur_tx % NUM_TX_DESC; 4358 struct TxDesc *txd_first, *txd_last; 4359 bool stop_queue, door_bell; 4360 unsigned int frags; 4361 u32 opts[2]; 4362 4363 if (unlikely(!rtl_tx_slots_avail(tp))) { 4364 if (net_ratelimit()) 4365 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); 4366 netif_stop_queue(dev); 4367 return NETDEV_TX_BUSY; 4368 } 4369 4370 opts[1] = rtl8169_tx_vlan_tag(skb); 4371 opts[0] = 0; 4372 4373 if (!rtl_chip_supports_csum_v2(tp)) 4374 rtl8169_tso_csum_v1(skb, opts); 4375 else if (!rtl8169_tso_csum_v2(tp, skb, opts)) 4376 goto err_dma_0; 4377 4378 if (unlikely(rtl8169_tx_map(tp, opts, skb_headlen(skb), skb->data, 4379 entry, false))) 4380 goto err_dma_0; 4381 4382 txd_first = tp->TxDescArray + entry; 4383 4384 frags = skb_shinfo(skb)->nr_frags; 4385 if (frags) { 4386 if (rtl8169_xmit_frags(tp, skb, opts, entry)) 4387 goto err_dma_1; 4388 entry = (entry + frags) % NUM_TX_DESC; 4389 } 4390 4391 txd_last = tp->TxDescArray + entry; 4392 txd_last->opts1 |= cpu_to_le32(LastFrag); 4393 tp->tx_skb[entry].skb = skb; 4394 4395 skb_tx_timestamp(skb); 4396 4397 /* Force memory writes to complete before releasing descriptor */ 4398 dma_wmb(); 4399 4400 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more()); 4401 4402 txd_first->opts1 |= cpu_to_le32(DescOwn | FirstFrag); 4403 4404 /* rtl_tx needs to see descriptor changes before updated tp->cur_tx */ 4405 smp_wmb(); 4406 4407 WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1); 4408 4409 stop_queue = !netif_subqueue_maybe_stop(dev, 0, rtl_tx_slots_avail(tp), 4410 R8169_TX_STOP_THRS, 4411 R8169_TX_START_THRS); 4412 if (door_bell || stop_queue) 4413 rtl8169_doorbell(tp); 4414 4415 return NETDEV_TX_OK; 4416 4417 err_dma_1: 4418 rtl8169_unmap_tx_skb(tp, entry); 4419 err_dma_0: 4420 dev_kfree_skb_any(skb); 4421 dev->stats.tx_dropped++; 4422 return NETDEV_TX_OK; 4423 } 4424 4425 static unsigned int rtl_last_frag_len(struct sk_buff *skb) 4426 { 4427 struct skb_shared_info *info = skb_shinfo(skb); 4428 unsigned int nr_frags = info->nr_frags; 4429 4430 if (!nr_frags) 4431 return UINT_MAX; 4432 4433 return skb_frag_size(info->frags + nr_frags - 1); 4434 } 4435 4436 /* Workaround for hw issues with TSO on RTL8168evl */ 4437 static netdev_features_t rtl8168evl_fix_tso(struct sk_buff *skb, 4438 netdev_features_t features) 4439 { 4440 /* IPv4 header has options field */ 4441 if (vlan_get_protocol(skb) == htons(ETH_P_IP) && 4442 ip_hdrlen(skb) > sizeof(struct iphdr)) 4443 features &= ~NETIF_F_ALL_TSO; 4444 4445 /* IPv4 TCP header has options field */ 4446 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 && 4447 tcp_hdrlen(skb) > sizeof(struct tcphdr)) 4448 features &= ~NETIF_F_ALL_TSO; 4449 4450 else if (rtl_last_frag_len(skb) <= 6) 4451 features &= ~NETIF_F_ALL_TSO; 4452 4453 return features; 4454 } 4455 4456 static netdev_features_t rtl8169_features_check(struct sk_buff *skb, 4457 struct net_device *dev, 4458 netdev_features_t features) 4459 { 4460 struct rtl8169_private *tp = netdev_priv(dev); 4461 4462 if (skb_is_gso(skb)) { 4463 if (tp->mac_version == RTL_GIGA_MAC_VER_34) 4464 features = rtl8168evl_fix_tso(skb, features); 4465 4466 if (skb_transport_offset(skb) > GTTCPHO_MAX && 4467 rtl_chip_supports_csum_v2(tp)) 4468 features &= ~NETIF_F_ALL_TSO; 4469 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 4470 /* work around hw bug on some chip versions */ 4471 if (skb->len < ETH_ZLEN) 4472 features &= ~NETIF_F_CSUM_MASK; 4473 4474 if (rtl_quirk_packet_padto(tp, skb)) 4475 features &= ~NETIF_F_CSUM_MASK; 4476 4477 if (skb_transport_offset(skb) > TCPHO_MAX && 4478 rtl_chip_supports_csum_v2(tp)) 4479 features &= ~NETIF_F_CSUM_MASK; 4480 } 4481 4482 return vlan_features_check(skb, features); 4483 } 4484 4485 static void rtl8169_pcierr_interrupt(struct net_device *dev) 4486 { 4487 struct rtl8169_private *tp = netdev_priv(dev); 4488 struct pci_dev *pdev = tp->pci_dev; 4489 int pci_status_errs; 4490 u16 pci_cmd; 4491 4492 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd); 4493 4494 pci_status_errs = pci_status_get_and_clear_errors(pdev); 4495 4496 if (net_ratelimit()) 4497 netdev_err(dev, "PCI error (cmd = 0x%04x, status_errs = 0x%04x)\n", 4498 pci_cmd, pci_status_errs); 4499 4500 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING); 4501 } 4502 4503 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, 4504 int budget) 4505 { 4506 unsigned int dirty_tx, bytes_compl = 0, pkts_compl = 0; 4507 struct sk_buff *skb; 4508 4509 dirty_tx = tp->dirty_tx; 4510 4511 while (READ_ONCE(tp->cur_tx) != dirty_tx) { 4512 unsigned int entry = dirty_tx % NUM_TX_DESC; 4513 u32 status; 4514 4515 status = le32_to_cpu(READ_ONCE(tp->TxDescArray[entry].opts1)); 4516 if (status & DescOwn) 4517 break; 4518 4519 skb = tp->tx_skb[entry].skb; 4520 rtl8169_unmap_tx_skb(tp, entry); 4521 4522 if (skb) { 4523 pkts_compl++; 4524 bytes_compl += skb->len; 4525 napi_consume_skb(skb, budget); 4526 } 4527 dirty_tx++; 4528 } 4529 4530 if (tp->dirty_tx != dirty_tx) { 4531 dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl); 4532 WRITE_ONCE(tp->dirty_tx, dirty_tx); 4533 4534 netif_subqueue_completed_wake(dev, 0, pkts_compl, bytes_compl, 4535 rtl_tx_slots_avail(tp), 4536 R8169_TX_START_THRS); 4537 /* 4538 * 8168 hack: TxPoll requests are lost when the Tx packets are 4539 * too close. Let's kick an extra TxPoll request when a burst 4540 * of start_xmit activity is detected (if it is not detected, 4541 * it is slow enough). -- FR 4542 * If skb is NULL then we come here again once a tx irq is 4543 * triggered after the last fragment is marked transmitted. 4544 */ 4545 if (READ_ONCE(tp->cur_tx) != dirty_tx && skb) 4546 rtl8169_doorbell(tp); 4547 } 4548 } 4549 4550 static inline int rtl8169_fragmented_frame(u32 status) 4551 { 4552 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag); 4553 } 4554 4555 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1) 4556 { 4557 u32 status = opts1 & (RxProtoMask | RxCSFailMask); 4558 4559 if (status == RxProtoTCP || status == RxProtoUDP) 4560 skb->ip_summed = CHECKSUM_UNNECESSARY; 4561 else 4562 skb_checksum_none_assert(skb); 4563 } 4564 4565 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget) 4566 { 4567 struct device *d = tp_to_dev(tp); 4568 int count; 4569 4570 for (count = 0; count < budget; count++, tp->cur_rx++) { 4571 unsigned int pkt_size, entry = tp->cur_rx % NUM_RX_DESC; 4572 struct RxDesc *desc = tp->RxDescArray + entry; 4573 struct sk_buff *skb; 4574 const void *rx_buf; 4575 dma_addr_t addr; 4576 u32 status; 4577 4578 status = le32_to_cpu(READ_ONCE(desc->opts1)); 4579 if (status & DescOwn) 4580 break; 4581 4582 /* This barrier is needed to keep us from reading 4583 * any other fields out of the Rx descriptor until 4584 * we know the status of DescOwn 4585 */ 4586 dma_rmb(); 4587 4588 if (unlikely(status & RxRES)) { 4589 if (net_ratelimit()) 4590 netdev_warn(dev, "Rx ERROR. status = %08x\n", 4591 status); 4592 dev->stats.rx_errors++; 4593 if (status & (RxRWT | RxRUNT)) 4594 dev->stats.rx_length_errors++; 4595 if (status & RxCRC) 4596 dev->stats.rx_crc_errors++; 4597 4598 if (!(dev->features & NETIF_F_RXALL)) 4599 goto release_descriptor; 4600 else if (status & RxRWT || !(status & (RxRUNT | RxCRC))) 4601 goto release_descriptor; 4602 } 4603 4604 pkt_size = status & GENMASK(13, 0); 4605 if (likely(!(dev->features & NETIF_F_RXFCS))) 4606 pkt_size -= ETH_FCS_LEN; 4607 4608 /* The driver does not support incoming fragmented frames. 4609 * They are seen as a symptom of over-mtu sized frames. 4610 */ 4611 if (unlikely(rtl8169_fragmented_frame(status))) { 4612 dev->stats.rx_dropped++; 4613 dev->stats.rx_length_errors++; 4614 goto release_descriptor; 4615 } 4616 4617 skb = napi_alloc_skb(&tp->napi, pkt_size); 4618 if (unlikely(!skb)) { 4619 dev->stats.rx_dropped++; 4620 goto release_descriptor; 4621 } 4622 4623 addr = le64_to_cpu(desc->addr); 4624 rx_buf = page_address(tp->Rx_databuff[entry]); 4625 4626 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE); 4627 prefetch(rx_buf); 4628 skb_copy_to_linear_data(skb, rx_buf, pkt_size); 4629 skb->tail += pkt_size; 4630 skb->len = pkt_size; 4631 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE); 4632 4633 rtl8169_rx_csum(skb, status); 4634 skb->protocol = eth_type_trans(skb, dev); 4635 4636 rtl8169_rx_vlan_tag(desc, skb); 4637 4638 if (skb->pkt_type == PACKET_MULTICAST) 4639 dev->stats.multicast++; 4640 4641 napi_gro_receive(&tp->napi, skb); 4642 4643 dev_sw_netstats_rx_add(dev, pkt_size); 4644 release_descriptor: 4645 rtl8169_mark_to_asic(desc); 4646 } 4647 4648 return count; 4649 } 4650 4651 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance) 4652 { 4653 struct rtl8169_private *tp = dev_instance; 4654 u32 status = rtl_get_events(tp); 4655 4656 if ((status & 0xffff) == 0xffff || !(status & tp->irq_mask)) 4657 return IRQ_NONE; 4658 4659 /* At least RTL8168fp may unexpectedly set the SYSErr bit */ 4660 if (unlikely(status & SYSErr && 4661 tp->mac_version <= RTL_GIGA_MAC_VER_06)) { 4662 rtl8169_pcierr_interrupt(tp->dev); 4663 goto out; 4664 } 4665 4666 if (status & LinkChg) 4667 phy_mac_interrupt(tp->phydev); 4668 4669 rtl_irq_disable(tp); 4670 napi_schedule(&tp->napi); 4671 out: 4672 rtl_ack_events(tp, status); 4673 4674 return IRQ_HANDLED; 4675 } 4676 4677 static void rtl_task(struct work_struct *work) 4678 { 4679 struct rtl8169_private *tp = 4680 container_of(work, struct rtl8169_private, wk.work); 4681 int ret; 4682 4683 if (test_and_clear_bit(RTL_FLAG_TASK_TX_TIMEOUT, tp->wk.flags)) { 4684 /* if chip isn't accessible, reset bus to revive it */ 4685 if (RTL_R32(tp, TxConfig) == ~0) { 4686 ret = pci_reset_bus(tp->pci_dev); 4687 if (ret < 0) { 4688 netdev_err(tp->dev, "Can't reset secondary PCI bus, detach NIC\n"); 4689 netif_device_detach(tp->dev); 4690 return; 4691 } 4692 } 4693 4694 /* ASPM compatibility issues are a typical reason for tx timeouts */ 4695 ret = pci_disable_link_state(tp->pci_dev, PCIE_LINK_STATE_L1 | 4696 PCIE_LINK_STATE_L0S); 4697 if (!ret) 4698 netdev_warn_once(tp->dev, "ASPM disabled on Tx timeout\n"); 4699 goto reset; 4700 } 4701 4702 if (test_and_clear_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags)) { 4703 reset: 4704 rtl_reset_work(tp); 4705 netif_wake_queue(tp->dev); 4706 } 4707 } 4708 4709 static int rtl8169_poll(struct napi_struct *napi, int budget) 4710 { 4711 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi); 4712 struct net_device *dev = tp->dev; 4713 int work_done; 4714 4715 rtl_tx(dev, tp, budget); 4716 4717 work_done = rtl_rx(dev, tp, budget); 4718 4719 if (work_done < budget && napi_complete_done(napi, work_done)) 4720 rtl_irq_enable(tp); 4721 4722 return work_done; 4723 } 4724 4725 static void rtl_enable_tx_lpi(struct rtl8169_private *tp, bool enable) 4726 { 4727 if (!rtl_supports_eee(tp)) 4728 return; 4729 4730 switch (tp->mac_version) { 4731 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_52: 4732 /* Adjust EEE LED frequency */ 4733 if (tp->mac_version != RTL_GIGA_MAC_VER_38) 4734 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07); 4735 if (enable) 4736 rtl_eri_set_bits(tp, 0x1b0, 0x0003); 4737 else 4738 rtl_eri_clear_bits(tp, 0x1b0, 0x0003); 4739 break; 4740 case RTL_GIGA_MAC_VER_61: 4741 if (enable) { 4742 r8168_mac_ocp_modify(tp, 0xe040, 0, 0x0003); 4743 r8168_mac_ocp_modify(tp, 0xeb62, 0, 0x0006); 4744 } else { 4745 r8168_mac_ocp_modify(tp, 0xe040, 0x0003, 0); 4746 r8168_mac_ocp_modify(tp, 0xeb62, 0x0006, 0); 4747 } 4748 break; 4749 case RTL_GIGA_MAC_VER_63 ... RTL_GIGA_MAC_VER_LAST: 4750 if (enable) 4751 r8168_mac_ocp_modify(tp, 0xe040, 0, 0x0003); 4752 else 4753 r8168_mac_ocp_modify(tp, 0xe040, 0x0003, 0); 4754 break; 4755 default: 4756 break; 4757 } 4758 } 4759 4760 static void r8169_phylink_handler(struct net_device *ndev) 4761 { 4762 struct rtl8169_private *tp = netdev_priv(ndev); 4763 struct device *d = tp_to_dev(tp); 4764 4765 if (netif_carrier_ok(ndev)) { 4766 rtl_link_chg_patch(tp); 4767 rtl_enable_tx_lpi(tp, tp->phydev->enable_tx_lpi); 4768 pm_request_resume(d); 4769 } else { 4770 pm_runtime_idle(d); 4771 } 4772 4773 phy_print_status(tp->phydev); 4774 } 4775 4776 static int r8169_phy_connect(struct rtl8169_private *tp) 4777 { 4778 struct phy_device *phydev = tp->phydev; 4779 phy_interface_t phy_mode; 4780 int ret; 4781 4782 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII : 4783 PHY_INTERFACE_MODE_MII; 4784 4785 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler, 4786 phy_mode); 4787 if (ret) 4788 return ret; 4789 4790 if (!tp->supports_gmii) 4791 phy_set_max_speed(phydev, SPEED_100); 4792 4793 phy_attached_info(phydev); 4794 4795 return 0; 4796 } 4797 4798 static void rtl8169_down(struct rtl8169_private *tp) 4799 { 4800 disable_work_sync(&tp->wk.work); 4801 /* Clear all task flags */ 4802 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX); 4803 4804 phy_stop(tp->phydev); 4805 4806 rtl8169_update_counters(tp); 4807 4808 pci_clear_master(tp->pci_dev); 4809 rtl_pci_commit(tp); 4810 4811 rtl8169_cleanup(tp); 4812 rtl_disable_exit_l1(tp); 4813 rtl_prepare_power_down(tp); 4814 4815 if (tp->dash_type != RTL_DASH_NONE) 4816 rtl8168_driver_stop(tp); 4817 } 4818 4819 static void rtl8169_up(struct rtl8169_private *tp) 4820 { 4821 if (tp->dash_type != RTL_DASH_NONE) 4822 rtl8168_driver_start(tp); 4823 4824 pci_set_master(tp->pci_dev); 4825 phy_init_hw(tp->phydev); 4826 phy_resume(tp->phydev); 4827 rtl8169_init_phy(tp); 4828 napi_enable(&tp->napi); 4829 enable_work(&tp->wk.work); 4830 rtl_reset_work(tp); 4831 4832 phy_start(tp->phydev); 4833 } 4834 4835 static int rtl8169_close(struct net_device *dev) 4836 { 4837 struct rtl8169_private *tp = netdev_priv(dev); 4838 struct pci_dev *pdev = tp->pci_dev; 4839 4840 pm_runtime_get_sync(&pdev->dev); 4841 4842 netif_stop_queue(dev); 4843 rtl8169_down(tp); 4844 rtl8169_rx_clear(tp); 4845 4846 free_irq(tp->irq, tp); 4847 4848 phy_disconnect(tp->phydev); 4849 4850 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray, 4851 tp->RxPhyAddr); 4852 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray, 4853 tp->TxPhyAddr); 4854 tp->TxDescArray = NULL; 4855 tp->RxDescArray = NULL; 4856 4857 pm_runtime_put_sync(&pdev->dev); 4858 4859 return 0; 4860 } 4861 4862 #ifdef CONFIG_NET_POLL_CONTROLLER 4863 static void rtl8169_netpoll(struct net_device *dev) 4864 { 4865 struct rtl8169_private *tp = netdev_priv(dev); 4866 4867 rtl8169_interrupt(tp->irq, tp); 4868 } 4869 #endif 4870 4871 static int rtl_open(struct net_device *dev) 4872 { 4873 struct rtl8169_private *tp = netdev_priv(dev); 4874 struct pci_dev *pdev = tp->pci_dev; 4875 unsigned long irqflags; 4876 int retval = -ENOMEM; 4877 4878 pm_runtime_get_sync(&pdev->dev); 4879 4880 /* 4881 * Rx and Tx descriptors needs 256 bytes alignment. 4882 * dma_alloc_coherent provides more. 4883 */ 4884 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES, 4885 &tp->TxPhyAddr, GFP_KERNEL); 4886 if (!tp->TxDescArray) 4887 goto out; 4888 4889 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES, 4890 &tp->RxPhyAddr, GFP_KERNEL); 4891 if (!tp->RxDescArray) 4892 goto err_free_tx_0; 4893 4894 retval = rtl8169_init_ring(tp); 4895 if (retval < 0) 4896 goto err_free_rx_1; 4897 4898 rtl_request_firmware(tp); 4899 4900 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_NO_THREAD : IRQF_SHARED; 4901 retval = request_irq(tp->irq, rtl8169_interrupt, irqflags, dev->name, tp); 4902 if (retval < 0) 4903 goto err_release_fw_2; 4904 4905 retval = r8169_phy_connect(tp); 4906 if (retval) 4907 goto err_free_irq; 4908 4909 rtl8169_up(tp); 4910 rtl8169_init_counter_offsets(tp); 4911 netif_start_queue(dev); 4912 out: 4913 pm_runtime_put_sync(&pdev->dev); 4914 4915 return retval; 4916 4917 err_free_irq: 4918 free_irq(tp->irq, tp); 4919 err_release_fw_2: 4920 rtl_release_firmware(tp); 4921 rtl8169_rx_clear(tp); 4922 err_free_rx_1: 4923 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray, 4924 tp->RxPhyAddr); 4925 tp->RxDescArray = NULL; 4926 err_free_tx_0: 4927 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray, 4928 tp->TxPhyAddr); 4929 tp->TxDescArray = NULL; 4930 goto out; 4931 } 4932 4933 static void 4934 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) 4935 { 4936 struct rtl8169_private *tp = netdev_priv(dev); 4937 struct pci_dev *pdev = tp->pci_dev; 4938 struct rtl8169_counters *counters = tp->counters; 4939 4940 pm_runtime_get_noresume(&pdev->dev); 4941 4942 netdev_stats_to_stats64(stats, &dev->stats); 4943 dev_fetch_sw_netstats(stats, dev->tstats); 4944 4945 /* 4946 * Fetch additional counter values missing in stats collected by driver 4947 * from tally counters. 4948 */ 4949 if (pm_runtime_active(&pdev->dev)) 4950 rtl8169_update_counters(tp); 4951 4952 /* 4953 * Subtract values fetched during initalization. 4954 * See rtl8169_init_counter_offsets for a description why we do that. 4955 */ 4956 stats->tx_errors = le64_to_cpu(counters->tx_errors) - 4957 le64_to_cpu(tp->tc_offset.tx_errors); 4958 stats->collisions = le32_to_cpu(counters->tx_multi_collision) - 4959 le32_to_cpu(tp->tc_offset.tx_multi_collision); 4960 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) - 4961 le16_to_cpu(tp->tc_offset.tx_aborted); 4962 stats->rx_missed_errors = le16_to_cpu(counters->rx_missed) - 4963 le16_to_cpu(tp->tc_offset.rx_missed); 4964 4965 pm_runtime_put_noidle(&pdev->dev); 4966 } 4967 4968 static void rtl8169_net_suspend(struct rtl8169_private *tp) 4969 { 4970 netif_device_detach(tp->dev); 4971 4972 if (netif_running(tp->dev)) 4973 rtl8169_down(tp); 4974 } 4975 4976 static int rtl8169_runtime_resume(struct device *dev) 4977 { 4978 struct rtl8169_private *tp = dev_get_drvdata(dev); 4979 4980 rtl_rar_set(tp, tp->dev->dev_addr); 4981 __rtl8169_set_wol(tp, tp->saved_wolopts); 4982 4983 if (tp->TxDescArray) 4984 rtl8169_up(tp); 4985 4986 netif_device_attach(tp->dev); 4987 4988 return 0; 4989 } 4990 4991 static int rtl8169_suspend(struct device *device) 4992 { 4993 struct rtl8169_private *tp = dev_get_drvdata(device); 4994 4995 rtnl_lock(); 4996 rtl8169_net_suspend(tp); 4997 if (!device_may_wakeup(tp_to_dev(tp))) 4998 clk_disable_unprepare(tp->clk); 4999 rtnl_unlock(); 5000 5001 return 0; 5002 } 5003 5004 static int rtl8169_resume(struct device *device) 5005 { 5006 struct rtl8169_private *tp = dev_get_drvdata(device); 5007 5008 if (!device_may_wakeup(tp_to_dev(tp))) 5009 clk_prepare_enable(tp->clk); 5010 5011 /* Some chip versions may truncate packets without this initialization */ 5012 rtl_init_rxcfg(tp); 5013 5014 return rtl8169_runtime_resume(device); 5015 } 5016 5017 static int rtl8169_runtime_suspend(struct device *device) 5018 { 5019 struct rtl8169_private *tp = dev_get_drvdata(device); 5020 5021 if (!tp->TxDescArray) { 5022 netif_device_detach(tp->dev); 5023 return 0; 5024 } 5025 5026 rtnl_lock(); 5027 __rtl8169_set_wol(tp, WAKE_PHY); 5028 rtl8169_net_suspend(tp); 5029 rtnl_unlock(); 5030 5031 return 0; 5032 } 5033 5034 static int rtl8169_runtime_idle(struct device *device) 5035 { 5036 struct rtl8169_private *tp = dev_get_drvdata(device); 5037 5038 if (tp->dash_enabled) 5039 return -EBUSY; 5040 5041 if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev)) 5042 pm_schedule_suspend(device, 10000); 5043 5044 return -EBUSY; 5045 } 5046 5047 static const struct dev_pm_ops rtl8169_pm_ops = { 5048 SYSTEM_SLEEP_PM_OPS(rtl8169_suspend, rtl8169_resume) 5049 RUNTIME_PM_OPS(rtl8169_runtime_suspend, rtl8169_runtime_resume, 5050 rtl8169_runtime_idle) 5051 }; 5052 5053 static void rtl_shutdown(struct pci_dev *pdev) 5054 { 5055 struct rtl8169_private *tp = pci_get_drvdata(pdev); 5056 5057 rtnl_lock(); 5058 rtl8169_net_suspend(tp); 5059 rtnl_unlock(); 5060 5061 /* Restore original MAC address */ 5062 rtl_rar_set(tp, tp->dev->perm_addr); 5063 5064 if (system_state == SYSTEM_POWER_OFF && !tp->dash_enabled) 5065 pci_prepare_to_sleep(pdev); 5066 } 5067 5068 static void rtl_remove_one(struct pci_dev *pdev) 5069 { 5070 struct rtl8169_private *tp = pci_get_drvdata(pdev); 5071 5072 if (pci_dev_run_wake(pdev)) 5073 pm_runtime_get_noresume(&pdev->dev); 5074 5075 disable_work_sync(&tp->wk.work); 5076 5077 if (IS_ENABLED(CONFIG_R8169_LEDS)) 5078 r8169_remove_leds(tp->leds); 5079 5080 unregister_netdev(tp->dev); 5081 5082 if (tp->dash_type != RTL_DASH_NONE) 5083 rtl8168_driver_stop(tp); 5084 5085 rtl_release_firmware(tp); 5086 5087 /* restore original MAC address */ 5088 rtl_rar_set(tp, tp->dev->perm_addr); 5089 } 5090 5091 static const struct net_device_ops rtl_netdev_ops = { 5092 .ndo_open = rtl_open, 5093 .ndo_stop = rtl8169_close, 5094 .ndo_get_stats64 = rtl8169_get_stats64, 5095 .ndo_start_xmit = rtl8169_start_xmit, 5096 .ndo_features_check = rtl8169_features_check, 5097 .ndo_tx_timeout = rtl8169_tx_timeout, 5098 .ndo_validate_addr = eth_validate_addr, 5099 .ndo_change_mtu = rtl8169_change_mtu, 5100 .ndo_fix_features = rtl8169_fix_features, 5101 .ndo_set_features = rtl8169_set_features, 5102 .ndo_set_mac_address = rtl_set_mac_address, 5103 .ndo_eth_ioctl = phy_do_ioctl_running, 5104 .ndo_set_rx_mode = rtl_set_rx_mode, 5105 #ifdef CONFIG_NET_POLL_CONTROLLER 5106 .ndo_poll_controller = rtl8169_netpoll, 5107 #endif 5108 5109 }; 5110 5111 static void rtl_set_irq_mask(struct rtl8169_private *tp) 5112 { 5113 tp->irq_mask = RxOK | RxErr | TxOK | TxErr | LinkChg; 5114 5115 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) 5116 tp->irq_mask |= SYSErr | RxFIFOOver; 5117 } 5118 5119 static int rtl_alloc_irq(struct rtl8169_private *tp) 5120 { 5121 unsigned int flags; 5122 5123 switch (tp->mac_version) { 5124 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06: 5125 rtl_unlock_config_regs(tp); 5126 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable); 5127 rtl_lock_config_regs(tp); 5128 fallthrough; 5129 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_17: 5130 flags = PCI_IRQ_INTX; 5131 break; 5132 default: 5133 flags = PCI_IRQ_ALL_TYPES; 5134 break; 5135 } 5136 5137 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags); 5138 } 5139 5140 static void rtl_read_mac_address(struct rtl8169_private *tp, 5141 u8 mac_addr[ETH_ALEN]) 5142 { 5143 /* Get MAC address */ 5144 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) { 5145 u32 value; 5146 5147 value = rtl_eri_read(tp, 0xe0); 5148 put_unaligned_le32(value, mac_addr); 5149 value = rtl_eri_read(tp, 0xe4); 5150 put_unaligned_le16(value, mac_addr + 4); 5151 } else if (rtl_is_8125(tp)) { 5152 rtl_read_mac_from_reg(tp, mac_addr, MAC0_BKP); 5153 } 5154 } 5155 5156 DECLARE_RTL_COND(rtl_link_list_ready_cond) 5157 { 5158 return RTL_R8(tp, MCU) & LINK_LIST_RDY; 5159 } 5160 5161 static void r8168g_wait_ll_share_fifo_ready(struct rtl8169_private *tp) 5162 { 5163 rtl_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42); 5164 } 5165 5166 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg) 5167 { 5168 struct rtl8169_private *tp = mii_bus->priv; 5169 5170 if (phyaddr > 0) 5171 return -ENODEV; 5172 5173 return rtl_readphy(tp, phyreg); 5174 } 5175 5176 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr, 5177 int phyreg, u16 val) 5178 { 5179 struct rtl8169_private *tp = mii_bus->priv; 5180 5181 if (phyaddr > 0) 5182 return -ENODEV; 5183 5184 rtl_writephy(tp, phyreg, val); 5185 5186 return 0; 5187 } 5188 5189 static int r8169_mdio_read_reg_c45(struct mii_bus *mii_bus, int addr, 5190 int devnum, int regnum) 5191 { 5192 struct rtl8169_private *tp = mii_bus->priv; 5193 5194 if (addr > 0) 5195 return -ENODEV; 5196 5197 if (devnum == MDIO_MMD_VEND2 && regnum > MDIO_STAT2) 5198 return r8168_phy_ocp_read(tp, regnum); 5199 5200 return 0; 5201 } 5202 5203 static int r8169_mdio_write_reg_c45(struct mii_bus *mii_bus, int addr, 5204 int devnum, int regnum, u16 val) 5205 { 5206 struct rtl8169_private *tp = mii_bus->priv; 5207 5208 if (addr > 0 || devnum != MDIO_MMD_VEND2 || regnum <= MDIO_STAT2) 5209 return -ENODEV; 5210 5211 r8168_phy_ocp_write(tp, regnum, val); 5212 5213 return 0; 5214 } 5215 5216 static int r8169_mdio_register(struct rtl8169_private *tp) 5217 { 5218 struct pci_dev *pdev = tp->pci_dev; 5219 struct mii_bus *new_bus; 5220 int ret; 5221 5222 /* On some boards with this chip version the BIOS is buggy and misses 5223 * to reset the PHY page selector. This results in the PHY ID read 5224 * accessing registers on a different page, returning a more or 5225 * less random value. Fix this by resetting the page selector first. 5226 */ 5227 if (tp->mac_version == RTL_GIGA_MAC_VER_25 || 5228 tp->mac_version == RTL_GIGA_MAC_VER_26) 5229 r8169_mdio_write(tp, 0x1f, 0); 5230 5231 new_bus = devm_mdiobus_alloc(&pdev->dev); 5232 if (!new_bus) 5233 return -ENOMEM; 5234 5235 new_bus->name = "r8169"; 5236 new_bus->priv = tp; 5237 new_bus->parent = &pdev->dev; 5238 new_bus->irq[0] = PHY_MAC_INTERRUPT; 5239 new_bus->phy_mask = GENMASK(31, 1); 5240 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x-%x", 5241 pci_domain_nr(pdev->bus), pci_dev_id(pdev)); 5242 5243 new_bus->read = r8169_mdio_read_reg; 5244 new_bus->write = r8169_mdio_write_reg; 5245 5246 if (tp->mac_version >= RTL_GIGA_MAC_VER_40) { 5247 new_bus->read_c45 = r8169_mdio_read_reg_c45; 5248 new_bus->write_c45 = r8169_mdio_write_reg_c45; 5249 } 5250 5251 ret = devm_mdiobus_register(&pdev->dev, new_bus); 5252 if (ret) 5253 return ret; 5254 5255 tp->phydev = mdiobus_get_phy(new_bus, 0); 5256 if (!tp->phydev) { 5257 return -ENODEV; 5258 } else if (!tp->phydev->drv) { 5259 /* Most chip versions fail with the genphy driver. 5260 * Therefore ensure that the dedicated PHY driver is loaded. 5261 */ 5262 dev_err(&pdev->dev, "no dedicated PHY driver found for PHY ID 0x%08x, maybe realtek.ko needs to be added to initramfs?\n", 5263 tp->phydev->phy_id); 5264 return -EUNATCH; 5265 } 5266 5267 tp->phydev->mac_managed_pm = true; 5268 if (rtl_supports_eee(tp)) 5269 phy_support_eee(tp->phydev); 5270 phy_support_asym_pause(tp->phydev); 5271 5272 /* mimic behavior of r8125/r8126 vendor drivers */ 5273 if (tp->mac_version == RTL_GIGA_MAC_VER_61) 5274 phy_disable_eee_mode(tp->phydev, 5275 ETHTOOL_LINK_MODE_2500baseT_Full_BIT); 5276 5277 /* PHY will be woken up in rtl_open() */ 5278 phy_suspend(tp->phydev); 5279 5280 return 0; 5281 } 5282 5283 static void rtl_hw_init_8168g(struct rtl8169_private *tp) 5284 { 5285 rtl_enable_rxdvgate(tp); 5286 5287 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb)); 5288 msleep(1); 5289 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); 5290 5291 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0); 5292 r8168g_wait_ll_share_fifo_ready(tp); 5293 5294 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15)); 5295 r8168g_wait_ll_share_fifo_ready(tp); 5296 } 5297 5298 static void rtl_hw_init_8125(struct rtl8169_private *tp) 5299 { 5300 rtl_enable_rxdvgate(tp); 5301 5302 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb)); 5303 msleep(1); 5304 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB); 5305 5306 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0); 5307 r8168g_wait_ll_share_fifo_ready(tp); 5308 5309 r8168_mac_ocp_write(tp, 0xc0aa, 0x07d0); 5310 r8168_mac_ocp_write(tp, 0xc0a6, 0x0150); 5311 r8168_mac_ocp_write(tp, 0xc01e, 0x5555); 5312 r8168g_wait_ll_share_fifo_ready(tp); 5313 } 5314 5315 static void rtl_hw_initialize(struct rtl8169_private *tp) 5316 { 5317 switch (tp->mac_version) { 5318 case RTL_GIGA_MAC_VER_51 ... RTL_GIGA_MAC_VER_52: 5319 rtl8168ep_stop_cmac(tp); 5320 fallthrough; 5321 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48: 5322 rtl_hw_init_8168g(tp); 5323 break; 5324 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_LAST: 5325 rtl_hw_init_8125(tp); 5326 break; 5327 default: 5328 break; 5329 } 5330 } 5331 5332 static int rtl_jumbo_max(struct rtl8169_private *tp) 5333 { 5334 /* Non-GBit versions don't support jumbo frames */ 5335 if (!tp->supports_gmii) 5336 return 0; 5337 5338 switch (tp->mac_version) { 5339 /* RTL8169 */ 5340 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06: 5341 return JUMBO_7K; 5342 /* RTL8168b */ 5343 case RTL_GIGA_MAC_VER_17: 5344 return JUMBO_4K; 5345 /* RTL8168c */ 5346 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24: 5347 return JUMBO_6K; 5348 /* RTL8125/8126 */ 5349 case RTL_GIGA_MAC_VER_61 ... RTL_GIGA_MAC_VER_LAST: 5350 return JUMBO_16K; 5351 default: 5352 return JUMBO_9K; 5353 } 5354 } 5355 5356 static void rtl_init_mac_address(struct rtl8169_private *tp) 5357 { 5358 u8 mac_addr[ETH_ALEN] __aligned(2) = {}; 5359 struct net_device *dev = tp->dev; 5360 int rc; 5361 5362 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr); 5363 if (!rc) 5364 goto done; 5365 5366 rtl_read_mac_address(tp, mac_addr); 5367 if (is_valid_ether_addr(mac_addr)) 5368 goto done; 5369 5370 rtl_read_mac_from_reg(tp, mac_addr, MAC0); 5371 if (is_valid_ether_addr(mac_addr)) 5372 goto done; 5373 5374 eth_random_addr(mac_addr); 5375 dev->addr_assign_type = NET_ADDR_RANDOM; 5376 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n"); 5377 done: 5378 eth_hw_addr_set(dev, mac_addr); 5379 rtl_rar_set(tp, mac_addr); 5380 } 5381 5382 /* register is set if system vendor successfully tested ASPM 1.2 */ 5383 static bool rtl_aspm_is_safe(struct rtl8169_private *tp) 5384 { 5385 if (tp->mac_version >= RTL_GIGA_MAC_VER_46 && 5386 r8168_mac_ocp_read(tp, 0xc0b2) & 0xf) 5387 return true; 5388 5389 return false; 5390 } 5391 5392 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 5393 { 5394 const struct rtl_chip_info *chip; 5395 struct rtl8169_private *tp; 5396 int jumbo_max, region, rc; 5397 struct net_device *dev; 5398 u32 txconfig; 5399 u16 xid; 5400 5401 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp)); 5402 if (!dev) 5403 return -ENOMEM; 5404 5405 SET_NETDEV_DEV(dev, &pdev->dev); 5406 dev->netdev_ops = &rtl_netdev_ops; 5407 tp = netdev_priv(dev); 5408 tp->dev = dev; 5409 tp->pci_dev = pdev; 5410 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1; 5411 tp->ocp_base = OCP_STD_PHY_BASE; 5412 5413 raw_spin_lock_init(&tp->mac_ocp_lock); 5414 mutex_init(&tp->led_lock); 5415 5416 /* Get the *optional* external "ether_clk" used on some boards */ 5417 tp->clk = devm_clk_get_optional_enabled(&pdev->dev, "ether_clk"); 5418 if (IS_ERR(tp->clk)) 5419 return dev_err_probe(&pdev->dev, PTR_ERR(tp->clk), "failed to get ether_clk\n"); 5420 5421 /* enable device (incl. PCI PM wakeup and hotplug setup) */ 5422 rc = pcim_enable_device(pdev); 5423 if (rc < 0) 5424 return dev_err_probe(&pdev->dev, rc, "enable failure\n"); 5425 5426 if (pcim_set_mwi(pdev) < 0) 5427 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n"); 5428 5429 /* use first MMIO region */ 5430 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1; 5431 if (region < 0) 5432 return dev_err_probe(&pdev->dev, -ENODEV, "no MMIO resource found\n"); 5433 5434 tp->mmio_addr = pcim_iomap_region(pdev, region, KBUILD_MODNAME); 5435 if (IS_ERR(tp->mmio_addr)) 5436 return dev_err_probe(&pdev->dev, PTR_ERR(tp->mmio_addr), 5437 "cannot remap MMIO, aborting\n"); 5438 5439 txconfig = RTL_R32(tp, TxConfig); 5440 if (txconfig == ~0U) 5441 return dev_err_probe(&pdev->dev, -EIO, "PCI read failed\n"); 5442 5443 xid = (txconfig >> 20) & 0xfcf; 5444 5445 /* Identify chip attached to board */ 5446 chip = rtl8169_get_chip_version(xid, tp->supports_gmii); 5447 if (chip->mac_version == RTL_GIGA_MAC_NONE) 5448 return dev_err_probe(&pdev->dev, -ENODEV, 5449 "unknown chip XID %03x, contact r8169 maintainers (see MAINTAINERS file)\n", 5450 xid); 5451 tp->mac_version = chip->mac_version; 5452 tp->fw_name = chip->fw_name; 5453 5454 /* Disable ASPM L1 as that cause random device stop working 5455 * problems as well as full system hangs for some PCIe devices users. 5456 */ 5457 if (rtl_aspm_is_safe(tp)) { 5458 dev_info(&pdev->dev, "System vendor flags ASPM as safe\n"); 5459 rc = 0; 5460 } else { 5461 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L1); 5462 } 5463 tp->aspm_manageable = !rc; 5464 5465 /* Fiber mode on RTL8127AF isn't supported */ 5466 if (rtl_is_8125(tp)) { 5467 u16 data = r8168_mac_ocp_read(tp, 0xd006); 5468 5469 if ((data & 0xff) == 0x07) 5470 return dev_err_probe(&pdev->dev, -ENODEV, 5471 "Fiber mode not supported\n"); 5472 } 5473 5474 tp->dash_type = rtl_get_dash_type(tp); 5475 tp->dash_enabled = rtl_dash_is_enabled(tp); 5476 5477 tp->cp_cmd = RTL_R16(tp, CPlusCmd) & CPCMD_MASK; 5478 5479 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 && 5480 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) 5481 dev->features |= NETIF_F_HIGHDMA; 5482 5483 rtl_init_rxcfg(tp); 5484 5485 rtl8169_irq_mask_and_ack(tp); 5486 5487 rtl_hw_initialize(tp); 5488 5489 rtl_hw_reset(tp); 5490 5491 rc = rtl_alloc_irq(tp); 5492 if (rc < 0) 5493 return dev_err_probe(&pdev->dev, rc, "Can't allocate interrupt\n"); 5494 5495 tp->irq = pci_irq_vector(pdev, 0); 5496 5497 INIT_WORK(&tp->wk.work, rtl_task); 5498 disable_work(&tp->wk.work); 5499 5500 rtl_init_mac_address(tp); 5501 5502 dev->ethtool_ops = &rtl8169_ethtool_ops; 5503 5504 netif_napi_add(dev, &tp->napi, rtl8169_poll); 5505 5506 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM | 5507 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; 5508 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; 5509 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 5510 5511 /* 5512 * Pretend we are using VLANs; This bypasses a nasty bug where 5513 * Interrupts stop flowing on high load on 8110SCd controllers. 5514 */ 5515 if (tp->mac_version == RTL_GIGA_MAC_VER_05) 5516 /* Disallow toggling */ 5517 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX; 5518 5519 if (rtl_chip_supports_csum_v2(tp)) 5520 dev->hw_features |= NETIF_F_IPV6_CSUM; 5521 5522 dev->features |= dev->hw_features; 5523 5524 if (rtl_chip_supports_csum_v2(tp)) { 5525 dev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6; 5526 netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V2); 5527 netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V2); 5528 } else { 5529 dev->hw_features |= NETIF_F_SG | NETIF_F_TSO; 5530 netif_set_tso_max_size(dev, RTL_GSO_MAX_SIZE_V1); 5531 netif_set_tso_max_segs(dev, RTL_GSO_MAX_SEGS_V1); 5532 } 5533 5534 /* There has been a number of reports that using SG/TSO results in 5535 * tx timeouts. However for a lot of people SG/TSO works fine. 5536 * It's not fully clear which chip versions are affected. Vendor 5537 * drivers enable SG/TSO for certain chip versions per default, 5538 * let's mimic this here. On other chip versions users can 5539 * use ethtool to enable SG/TSO, use at own risk! 5540 */ 5541 if (tp->mac_version >= RTL_GIGA_MAC_VER_46 && 5542 tp->mac_version != RTL_GIGA_MAC_VER_61) 5543 dev->features |= dev->hw_features; 5544 5545 dev->hw_features |= NETIF_F_RXALL; 5546 dev->hw_features |= NETIF_F_RXFCS; 5547 5548 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 5549 5550 netdev_sw_irq_coalesce_default_on(dev); 5551 5552 /* configure chip for default features */ 5553 rtl8169_set_features(dev, dev->features); 5554 5555 if (!tp->dash_enabled) { 5556 rtl_set_d3_pll_down(tp, true); 5557 } else { 5558 rtl_set_d3_pll_down(tp, false); 5559 dev->ethtool->wol_enabled = 1; 5560 } 5561 5562 jumbo_max = rtl_jumbo_max(tp); 5563 if (jumbo_max) 5564 dev->max_mtu = jumbo_max; 5565 5566 rtl_set_irq_mask(tp); 5567 5568 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters), 5569 &tp->counters_phys_addr, 5570 GFP_KERNEL); 5571 if (!tp->counters) 5572 return -ENOMEM; 5573 5574 pci_set_drvdata(pdev, tp); 5575 5576 rc = r8169_mdio_register(tp); 5577 if (rc) 5578 return rc; 5579 5580 rc = register_netdev(dev); 5581 if (rc) 5582 return rc; 5583 5584 if (IS_ENABLED(CONFIG_R8169_LEDS)) { 5585 if (rtl_is_8125(tp)) 5586 tp->leds = rtl8125_init_leds(dev); 5587 else if (tp->mac_version > RTL_GIGA_MAC_VER_06) 5588 tp->leds = rtl8168_init_leds(dev); 5589 } 5590 5591 netdev_info(dev, "%s, %pM, XID %03x, IRQ %d\n", 5592 chip->name, dev->dev_addr, xid, tp->irq); 5593 5594 if (jumbo_max) 5595 netdev_info(dev, "jumbo features [frames: %d bytes, tx checksumming: %s]\n", 5596 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ? 5597 "ok" : "ko"); 5598 5599 if (tp->dash_type != RTL_DASH_NONE) { 5600 netdev_info(dev, "DASH %s\n", 5601 tp->dash_enabled ? "enabled" : "disabled"); 5602 rtl8168_driver_start(tp); 5603 } 5604 5605 if (pci_dev_run_wake(pdev)) 5606 pm_runtime_put_sync(&pdev->dev); 5607 5608 return 0; 5609 } 5610 5611 static struct pci_driver rtl8169_pci_driver = { 5612 .name = KBUILD_MODNAME, 5613 .id_table = rtl8169_pci_tbl, 5614 .probe = rtl_init_one, 5615 .remove = rtl_remove_one, 5616 .shutdown = rtl_shutdown, 5617 .driver.pm = pm_ptr(&rtl8169_pm_ops), 5618 }; 5619 5620 module_pci_driver(rtl8169_pci_driver); 5621