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