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