blob: d943a00dbccfc79b6883f6f4ec3e306a3ea08d65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040028#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080029#include <linux/ipv6.h>
30#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080042#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080043#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080046#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080047#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080048#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080049#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000050#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000051#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000052#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080053#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
54#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
55#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
56#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000057
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020058#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070059 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060
Julien Ducourthial477206a2012-05-09 00:00:06 +020061#define TX_SLOTS_AVAIL(tp) \
62 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
63
64/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
65#define TX_FRAGS_READY_FOR(tp,nr_frags) \
66 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Michal Schmidtaee77e42012-09-09 13:55:26 +000072#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
74
75#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020076#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000078#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
80#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
81
82#define RTL8169_TX_TIMEOUT (6*HZ)
83#define RTL8169_PHY_TIMEOUT (10*HZ)
84
85/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020086#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
87#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
88#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
89#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
90#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
91#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020094 RTL_GIGA_MAC_VER_01 = 0,
95 RTL_GIGA_MAC_VER_02,
96 RTL_GIGA_MAC_VER_03,
97 RTL_GIGA_MAC_VER_04,
98 RTL_GIGA_MAC_VER_05,
99 RTL_GIGA_MAC_VER_06,
100 RTL_GIGA_MAC_VER_07,
101 RTL_GIGA_MAC_VER_08,
102 RTL_GIGA_MAC_VER_09,
103 RTL_GIGA_MAC_VER_10,
104 RTL_GIGA_MAC_VER_11,
105 RTL_GIGA_MAC_VER_12,
106 RTL_GIGA_MAC_VER_13,
107 RTL_GIGA_MAC_VER_14,
108 RTL_GIGA_MAC_VER_15,
109 RTL_GIGA_MAC_VER_16,
110 RTL_GIGA_MAC_VER_17,
111 RTL_GIGA_MAC_VER_18,
112 RTL_GIGA_MAC_VER_19,
113 RTL_GIGA_MAC_VER_20,
114 RTL_GIGA_MAC_VER_21,
115 RTL_GIGA_MAC_VER_22,
116 RTL_GIGA_MAC_VER_23,
117 RTL_GIGA_MAC_VER_24,
118 RTL_GIGA_MAC_VER_25,
119 RTL_GIGA_MAC_VER_26,
120 RTL_GIGA_MAC_VER_27,
121 RTL_GIGA_MAC_VER_28,
122 RTL_GIGA_MAC_VER_29,
123 RTL_GIGA_MAC_VER_30,
124 RTL_GIGA_MAC_VER_31,
125 RTL_GIGA_MAC_VER_32,
126 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800127 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800128 RTL_GIGA_MAC_VER_35,
129 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800130 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800131 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800132 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800133 RTL_GIGA_MAC_VER_40,
134 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000135 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000136 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800137 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800138 RTL_GIGA_MAC_VER_45,
139 RTL_GIGA_MAC_VER_46,
140 RTL_GIGA_MAC_VER_47,
141 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800142 RTL_GIGA_MAC_VER_49,
143 RTL_GIGA_MAC_VER_50,
144 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200145 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Francois Romieu2b7b4312011-04-18 22:53:24 -0700148enum rtl_tx_desc_version {
149 RTL_TD_0 = 0,
150 RTL_TD_1 = 1,
151};
152
Francois Romieud58d46b2011-05-03 16:38:29 +0200153#define JUMBO_1K ETH_DATA_LEN
154#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
155#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
156#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
157#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
158
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200159#define _R(NAME,TD,FW,SZ) { \
Francois Romieud58d46b2011-05-03 16:38:29 +0200160 .name = NAME, \
161 .txd_version = TD, \
162 .fw_name = FW, \
163 .jumbo_max = SZ, \
Francois Romieud58d46b2011-05-03 16:38:29 +0200164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800166static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700168 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 u16 jumbo_max;
Francois Romieu85bffe62011-04-27 08:22:39 +0200171} rtl_chip_infos[] = {
172 /* PCI devices. */
173 [RTL_GIGA_MAC_VER_01] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200174 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_02] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200176 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_03] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200178 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 [RTL_GIGA_MAC_VER_04] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200180 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200181 [RTL_GIGA_MAC_VER_05] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200182 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200183 [RTL_GIGA_MAC_VER_06] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200184 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 /* PCI-E devices. */
186 [RTL_GIGA_MAC_VER_07] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200187 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_08] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200189 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_09] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200191 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_10] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_11] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200195 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_12] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200197 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_13] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_14] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200201 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_15] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200203 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_16] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200205 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_17] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200207 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_18] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200209 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_19] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_20] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200213 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_21] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200215 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_22] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200217 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_23] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200219 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_24] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200221 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_25] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_26] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200225 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_27] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200227 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_28] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200229 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_29] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200231 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_30] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_31] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200235 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_32] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200237 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_33] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200239 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800242 [RTL_GIGA_MAC_VER_35] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200243 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
Hayes Wangc2218922011-09-06 16:55:18 +0800244 [RTL_GIGA_MAC_VER_36] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200245 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800246 [RTL_GIGA_MAC_VER_37] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200247 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800248 [RTL_GIGA_MAC_VER_38] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200249 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800250 [RTL_GIGA_MAC_VER_39] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200251 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
Hayes Wangc5583862012-07-02 17:23:22 +0800252 [RTL_GIGA_MAC_VER_40] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200253 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
Hayes Wangc5583862012-07-02 17:23:22 +0800254 [RTL_GIGA_MAC_VER_41] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200255 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
hayeswang57538c42013-04-01 22:23:40 +0000256 [RTL_GIGA_MAC_VER_42] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200257 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
hayeswang58152cd2013-04-01 22:23:42 +0000258 [RTL_GIGA_MAC_VER_43] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200259 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
hayeswang45dd95c2013-07-08 17:09:01 +0800260 [RTL_GIGA_MAC_VER_44] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200261 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800262 [RTL_GIGA_MAC_VER_45] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200263 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800264 [RTL_GIGA_MAC_VER_46] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200265 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800266 [RTL_GIGA_MAC_VER_47] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200267 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800268 [RTL_GIGA_MAC_VER_48] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200269 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800270 [RTL_GIGA_MAC_VER_49] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200271 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800272 [RTL_GIGA_MAC_VER_50] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200273 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800274 [RTL_GIGA_MAC_VER_51] =
Heiner Kallweit6ed0e082018-04-17 23:36:12 +0200275 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277#undef _R
278
Francois Romieubcf0bf92006-07-26 23:14:13 +0200279enum cfg_version {
280 RTL_CFG_0 = 0x00,
281 RTL_CFG_1,
282 RTL_CFG_2
283};
284
Benoit Taine9baa3c32014-08-08 15:56:03 +0200285static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200286 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200287 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800288 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200289 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100290 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200291 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200292 { PCI_VENDOR_ID_DLINK, 0x4300,
293 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200294 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000295 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200296 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200297 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
298 { PCI_VENDOR_ID_LINKSYS, 0x1032,
299 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100300 { 0x0001, 0x8168,
301 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 {0,},
303};
304
305MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
306
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200307static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200308static struct {
309 u32 msg_enable;
310} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Francois Romieu07d3f512007-02-21 22:40:46 +0100312enum rtl_registers {
313 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100314 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100315 MAR0 = 8, /* Multicast filter. */
316 CounterAddrLow = 0x10,
317 CounterAddrHigh = 0x14,
318 TxDescStartAddrLow = 0x20,
319 TxDescStartAddrHigh = 0x24,
320 TxHDescStartAddrLow = 0x28,
321 TxHDescStartAddrHigh = 0x2c,
322 FLASH = 0x30,
323 ERSR = 0x36,
324 ChipCmd = 0x37,
325 TxPoll = 0x38,
326 IntrMask = 0x3c,
327 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700328
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800329 TxConfig = 0x40,
330#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
331#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
332
333 RxConfig = 0x44,
334#define RX128_INT_EN (1 << 15) /* 8111c and later */
335#define RX_MULTI_EN (1 << 14) /* 8111c only */
336#define RXCFG_FIFO_SHIFT 13
337 /* No threshold before first PCI xfer */
338#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000339#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800340#define RXCFG_DMA_SHIFT 8
341 /* Unlimited maximum PCI burst. */
342#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700343
Francois Romieu07d3f512007-02-21 22:40:46 +0100344 RxMissed = 0x4c,
345 Cfg9346 = 0x50,
346 Config0 = 0x51,
347 Config1 = 0x52,
348 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200349#define PME_SIGNAL (1 << 5) /* 8168c and later */
350
Francois Romieu07d3f512007-02-21 22:40:46 +0100351 Config3 = 0x54,
352 Config4 = 0x55,
353 Config5 = 0x56,
354 MultiIntr = 0x5c,
355 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 PHYstatus = 0x6c,
357 RxMaxSize = 0xda,
358 CPlusCmd = 0xe0,
359 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300360
361#define RTL_COALESCE_MASK 0x0f
362#define RTL_COALESCE_SHIFT 4
363#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
364#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
365
Francois Romieu07d3f512007-02-21 22:40:46 +0100366 RxDescAddrLow = 0xe4,
367 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000368 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
369
370#define NoEarlyTx 0x3f /* Max value : no early transmit. */
371
372 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
373
374#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800375#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000376
Francois Romieu07d3f512007-02-21 22:40:46 +0100377 FuncEvent = 0xf0,
378 FuncEventMask = 0xf4,
379 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800380 IBCR0 = 0xf8,
381 IBCR2 = 0xf9,
382 IBIMR0 = 0xfa,
383 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100384 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387enum rtl8168_8101_registers {
388 CSIDR = 0x64,
389 CSIAR = 0x68,
390#define CSIAR_FLAG 0x80000000
391#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200392#define CSIAR_BYTE_ENABLE 0x0000f000
393#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000394 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200395 EPHYAR = 0x80,
396#define EPHYAR_FLAG 0x80000000
397#define EPHYAR_WRITE_CMD 0x80000000
398#define EPHYAR_REG_MASK 0x1f
399#define EPHYAR_REG_SHIFT 16
400#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800401 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800402#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800403#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200404 DBG_REG = 0xd1,
405#define FIX_NAK_1 (1 << 4)
406#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800407 TWSI = 0xd2,
408 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800409#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800410#define TX_EMPTY (1 << 5)
411#define RX_EMPTY (1 << 4)
412#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800413#define EN_NDP (1 << 3)
414#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800415#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000416 EFUSEAR = 0xdc,
417#define EFUSEAR_FLAG 0x80000000
418#define EFUSEAR_WRITE_CMD 0x80000000
419#define EFUSEAR_READ_CMD 0x00000000
420#define EFUSEAR_REG_MASK 0x03ff
421#define EFUSEAR_REG_SHIFT 8
422#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800423 MISC_1 = 0xf2,
424#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200425};
426
françois romieuc0e45c12011-01-03 15:08:04 +0000427enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800428 LED_FREQ = 0x1a,
429 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000430 ERIDR = 0x70,
431 ERIAR = 0x74,
432#define ERIAR_FLAG 0x80000000
433#define ERIAR_WRITE_CMD 0x80000000
434#define ERIAR_READ_CMD 0x00000000
435#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000436#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800437#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
438#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
439#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800440#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800441#define ERIAR_MASK_SHIFT 12
442#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
443#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800444#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800445#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800446#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000447 EPHY_RXER_NUM = 0x7c,
448 OCPDR = 0xb0, /* OCP GPHY access */
449#define OCPDR_WRITE_CMD 0x80000000
450#define OCPDR_READ_CMD 0x00000000
451#define OCPDR_REG_MASK 0x7f
452#define OCPDR_GPHY_REG_SHIFT 16
453#define OCPDR_DATA_MASK 0xffff
454 OCPAR = 0xb4,
455#define OCPAR_FLAG 0x80000000
456#define OCPAR_GPHY_WRITE_CMD 0x8000f060
457#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800458 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000459 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
460 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200461#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800462#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800463#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800464#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800465#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000466};
467
Francois Romieu07d3f512007-02-21 22:40:46 +0100468enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100470 SYSErr = 0x8000,
471 PCSTimeout = 0x4000,
472 SWInt = 0x0100,
473 TxDescUnavail = 0x0080,
474 RxFIFOOver = 0x0040,
475 LinkChg = 0x0020,
476 RxOverflow = 0x0010,
477 TxErr = 0x0008,
478 TxOK = 0x0004,
479 RxErr = 0x0002,
480 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400483 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200484 RxFOVF = (1 << 23),
485 RxRWT = (1 << 22),
486 RxRES = (1 << 21),
487 RxRUNT = (1 << 20),
488 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800491 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100492 CmdReset = 0x10,
493 CmdRxEnb = 0x08,
494 CmdTxEnb = 0x04,
495 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Francois Romieu275391a2007-02-23 23:50:28 +0100497 /* TXPoll register p.5 */
498 HPQ = 0x80, /* Poll cmd on the high prio queue */
499 NPQ = 0x40, /* Poll cmd on the low prio queue */
500 FSWInt = 0x01, /* Forced software interrupt */
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100503 Cfg9346_Lock = 0x00,
504 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100507 AcceptErr = 0x20,
508 AcceptRunt = 0x10,
509 AcceptBroadcast = 0x08,
510 AcceptMulticast = 0x04,
511 AcceptMyPhys = 0x02,
512 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200513#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* TxConfigBits */
516 TxInterFrameGapShift = 24,
517 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
518
Francois Romieu5d06a992006-02-23 00:47:58 +0100519 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200520 LEDS1 = (1 << 7),
521 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200522 Speed_down = (1 << 4),
523 MEMMAP = (1 << 3),
524 IOMAP = (1 << 2),
525 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100526 PMEnable = (1 << 0), /* Power Management Enable */
527
Francois Romieu6dccd162007-02-13 23:38:05 +0100528 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000529 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000530 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100531 PCI_Clock_66MHz = 0x01,
532 PCI_Clock_33MHz = 0x00,
533
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100534 /* Config3 register p.25 */
535 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
536 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200537 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800538 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200539 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100540
Francois Romieud58d46b2011-05-03 16:38:29 +0200541 /* Config4 register */
542 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
543
Francois Romieu5d06a992006-02-23 00:47:58 +0100544 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100545 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
546 MWF = (1 << 5), /* Accept Multicast wakeup frame */
547 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200548 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100549 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100550 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000551 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200554 EnableBist = (1 << 15), // 8168 8101
555 Mac_dbgo_oe = (1 << 14), // 8168 8101
556 Normal_mode = (1 << 13), // unused
557 Force_half_dup = (1 << 12), // 8168 8101
558 Force_rxflow_en = (1 << 11), // 8168 8101
559 Force_txflow_en = (1 << 10), // 8168 8101
560 Cxpl_dbg_sel = (1 << 9), // 8168 8101
561 ASF = (1 << 8), // 8168 8101
562 PktCntrDisable = (1 << 7), // 8168 8101
563 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 RxVlan = (1 << 6),
565 RxChkSum = (1 << 5),
566 PCIDAC = (1 << 4),
567 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200568#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100569 INTT_0 = 0x0000, // 8168
570 INTT_1 = 0x0001, // 8168
571 INTT_2 = 0x0002, // 8168
572 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100575 TBI_Enable = 0x80,
576 TxFlowCtrl = 0x40,
577 RxFlowCtrl = 0x20,
578 _1000bpsF = 0x10,
579 _100bps = 0x08,
580 _10bps = 0x04,
581 LinkStatus = 0x02,
582 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100585 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200586
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200587 /* ResetCounterCommand */
588 CounterReset = 0x1,
589
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200590 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100591 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800592
593 /* magic enable v2 */
594 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595};
596
Francois Romieu2b7b4312011-04-18 22:53:24 -0700597enum rtl_desc_bit {
598 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
600 RingEnd = (1 << 30), /* End of descriptor ring */
601 FirstFrag = (1 << 29), /* First segment of a packet */
602 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700603};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Francois Romieu2b7b4312011-04-18 22:53:24 -0700605/* Generic case. */
606enum rtl_tx_desc_bit {
607 /* First doubleword. */
608 TD_LSO = (1 << 27), /* Large Send Offload */
609#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Francois Romieu2b7b4312011-04-18 22:53:24 -0700611 /* Second doubleword. */
612 TxVlanTag = (1 << 17), /* Add VLAN tag */
613};
614
615/* 8169, 8168b and 810x except 8102e. */
616enum rtl_tx_desc_bit_0 {
617 /* First doubleword. */
618#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
619 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
620 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
621 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
622};
623
624/* 8102e, 8168c and beyond. */
625enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800626 /* First doubleword. */
627 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800628 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800629#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800630#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800631
Francois Romieu2b7b4312011-04-18 22:53:24 -0700632 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800633#define TCPHO_SHIFT 18
634#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700635#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800636 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
637 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700638 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
639 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
640};
641
Francois Romieu2b7b4312011-04-18 22:53:24 -0700642enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* Rx private */
644 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500645 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647#define RxProtoUDP (PID1)
648#define RxProtoTCP (PID0)
649#define RxProtoIP (PID1 | PID0)
650#define RxProtoMask RxProtoIP
651
652 IPFail = (1 << 16), /* IP checksum failed */
653 UDPFail = (1 << 15), /* UDP/IP checksum failed */
654 TCPFail = (1 << 14), /* TCP/IP checksum failed */
655 RxVlanTag = (1 << 16), /* VLAN tag available */
656};
657
658#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200659#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200662 __le32 opts1;
663 __le32 opts2;
664 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665};
666
667struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200668 __le32 opts1;
669 __le32 opts2;
670 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671};
672
673struct ring_info {
674 struct sk_buff *skb;
675 u32 len;
676 u8 __pad[sizeof(void *) - sizeof(u32)];
677};
678
Ivan Vecera355423d2009-02-06 21:49:57 -0800679struct rtl8169_counters {
680 __le64 tx_packets;
681 __le64 rx_packets;
682 __le64 tx_errors;
683 __le32 rx_errors;
684 __le16 rx_missed;
685 __le16 align_errors;
686 __le32 tx_one_collision;
687 __le32 tx_multi_collision;
688 __le64 rx_unicast;
689 __le64 rx_broadcast;
690 __le32 rx_multicast;
691 __le16 tx_aborted;
692 __le16 tx_underun;
693};
694
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200695struct rtl8169_tc_offsets {
696 bool inited;
697 __le64 tx_errors;
698 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200699 __le16 tx_aborted;
700};
701
Francois Romieuda78dbf2012-01-26 14:18:23 +0100702enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100703 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100704 RTL_FLAG_TASK_SLOW_PENDING,
705 RTL_FLAG_TASK_RESET_PENDING,
706 RTL_FLAG_TASK_PHY_PENDING,
707 RTL_FLAG_MAX
708};
709
Junchang Wang8027aa22012-03-04 23:30:32 +0100710struct rtl8169_stats {
711 u64 packets;
712 u64 bytes;
713 struct u64_stats_sync syncp;
714};
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716struct rtl8169_private {
717 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200718 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000719 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700720 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200721 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700722 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
724 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100726 struct rtl8169_stats rx_stats;
727 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
729 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
730 dma_addr_t TxPhyAddr;
731 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000732 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct timer_list timer;
735 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100736
737 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300738 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000739
740 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200741 void (*write)(struct rtl8169_private *, int, int);
742 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000743 } mdio_ops;
744
Francois Romieud58d46b2011-05-03 16:38:29 +0200745 struct jumbo_ops {
746 void (*enable)(struct rtl8169_private *);
747 void (*disable)(struct rtl8169_private *);
748 } jumbo_ops;
749
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200750 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800751 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100752
753 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100754 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
755 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100756 struct work_struct work;
757 } wk;
758
Francois Romieuccdffb92008-07-26 14:26:06 +0200759 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200760 dma_addr_t counters_phys_addr;
761 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200762 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000763 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000764
Francois Romieub6ffd972011-06-17 17:00:05 +0200765 struct rtl_fw {
766 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200767
768#define RTL_VER_SIZE 32
769
770 char version[RTL_VER_SIZE];
771
772 struct rtl_fw_phy_action {
773 __le32 *code;
774 size_t size;
775 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200776 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300777#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800778
779 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780};
781
Ralf Baechle979b6c12005-06-13 14:30:40 -0700782MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700785MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200786module_param_named(debug, debug.msg_enable, int, 0);
787MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788MODULE_LICENSE("GPL");
789MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000790MODULE_FIRMWARE(FIRMWARE_8168D_1);
791MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000792MODULE_FIRMWARE(FIRMWARE_8168E_1);
793MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400794MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800795MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800796MODULE_FIRMWARE(FIRMWARE_8168F_1);
797MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800798MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800799MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800800MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800801MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000802MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000803MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000804MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800805MODULE_FIRMWARE(FIRMWARE_8168H_1);
806MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200807MODULE_FIRMWARE(FIRMWARE_8107E_1);
808MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100810static inline struct device *tp_to_dev(struct rtl8169_private *tp)
811{
812 return &tp->pci_dev->dev;
813}
814
Francois Romieuda78dbf2012-01-26 14:18:23 +0100815static void rtl_lock_work(struct rtl8169_private *tp)
816{
817 mutex_lock(&tp->wk.mutex);
818}
819
820static void rtl_unlock_work(struct rtl8169_private *tp)
821{
822 mutex_unlock(&tp->wk.mutex);
823}
824
Heiner Kallweitcb732002018-03-20 07:45:35 +0100825static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200826{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100827 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800828 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200829}
830
Francois Romieuffc46952012-07-06 14:19:23 +0200831struct rtl_cond {
832 bool (*check)(struct rtl8169_private *);
833 const char *msg;
834};
835
836static void rtl_udelay(unsigned int d)
837{
838 udelay(d);
839}
840
841static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
842 void (*delay)(unsigned int), unsigned int d, int n,
843 bool high)
844{
845 int i;
846
847 for (i = 0; i < n; i++) {
848 delay(d);
849 if (c->check(tp) == high)
850 return true;
851 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200852 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
853 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200854 return false;
855}
856
857static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
858 const struct rtl_cond *c,
859 unsigned int d, int n)
860{
861 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
862}
863
864static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
865 const struct rtl_cond *c,
866 unsigned int d, int n)
867{
868 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
869}
870
871static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
872 const struct rtl_cond *c,
873 unsigned int d, int n)
874{
875 return rtl_loop_wait(tp, c, msleep, d, n, true);
876}
877
878static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
879 const struct rtl_cond *c,
880 unsigned int d, int n)
881{
882 return rtl_loop_wait(tp, c, msleep, d, n, false);
883}
884
885#define DECLARE_RTL_COND(name) \
886static bool name ## _check(struct rtl8169_private *); \
887 \
888static const struct rtl_cond name = { \
889 .check = name ## _check, \
890 .msg = #name \
891}; \
892 \
893static bool name ## _check(struct rtl8169_private *tp)
894
Hayes Wangc5583862012-07-02 17:23:22 +0800895static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
896{
897 if (reg & 0xffff0001) {
898 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
899 return true;
900 }
901 return false;
902}
903
904DECLARE_RTL_COND(rtl_ocp_gphy_cond)
905{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200906 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800907}
908
909static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
910{
Hayes Wangc5583862012-07-02 17:23:22 +0800911 if (rtl_ocp_reg_failure(tp, reg))
912 return;
913
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200914 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800915
916 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
917}
918
919static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
920{
Hayes Wangc5583862012-07-02 17:23:22 +0800921 if (rtl_ocp_reg_failure(tp, reg))
922 return 0;
923
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200924 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800925
926 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200927 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800928}
929
Hayes Wangc5583862012-07-02 17:23:22 +0800930static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
931{
Hayes Wangc5583862012-07-02 17:23:22 +0800932 if (rtl_ocp_reg_failure(tp, reg))
933 return;
934
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200935 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800936}
937
938static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
939{
Hayes Wangc5583862012-07-02 17:23:22 +0800940 if (rtl_ocp_reg_failure(tp, reg))
941 return 0;
942
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200943 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800944
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200945 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800946}
947
948#define OCP_STD_PHY_BASE 0xa400
949
950static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
951{
952 if (reg == 0x1f) {
953 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
954 return;
955 }
956
957 if (tp->ocp_base != OCP_STD_PHY_BASE)
958 reg -= 0x10;
959
960 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
961}
962
963static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
964{
965 if (tp->ocp_base != OCP_STD_PHY_BASE)
966 reg -= 0x10;
967
968 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
969}
970
hayeswangeee37862013-04-01 22:23:38 +0000971static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
972{
973 if (reg == 0x1f) {
974 tp->ocp_base = value << 4;
975 return;
976 }
977
978 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
979}
980
981static int mac_mcu_read(struct rtl8169_private *tp, int reg)
982{
983 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
984}
985
Francois Romieuffc46952012-07-06 14:19:23 +0200986DECLARE_RTL_COND(rtl_phyar_cond)
987{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200988 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200989}
990
Francois Romieu24192212012-07-06 20:19:42 +0200991static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200993 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Francois Romieuffc46952012-07-06 14:19:23 +0200995 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700996 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700997 * According to hardware specs a 20us delay is required after write
998 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700999 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001000 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Francois Romieu24192212012-07-06 20:19:42 +02001003static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Francois Romieuffc46952012-07-06 14:19:23 +02001005 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001007 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Francois Romieuffc46952012-07-06 14:19:23 +02001009 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001010 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001011
Timo Teräs81a95f02010-06-09 17:31:48 -07001012 /*
1013 * According to hardware specs a 20us delay is required after read
1014 * complete indication, but before sending next command.
1015 */
1016 udelay(20);
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return value;
1019}
1020
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001021DECLARE_RTL_COND(rtl_ocpar_cond)
1022{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001023 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001024}
1025
Francois Romieu24192212012-07-06 20:19:42 +02001026static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001027{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001028 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1029 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1030 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001031
Francois Romieuffc46952012-07-06 14:19:23 +02001032 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001033}
1034
Francois Romieu24192212012-07-06 20:19:42 +02001035static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001036{
Francois Romieu24192212012-07-06 20:19:42 +02001037 r8168dp_1_mdio_access(tp, reg,
1038 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001039}
1040
Francois Romieu24192212012-07-06 20:19:42 +02001041static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001042{
Francois Romieu24192212012-07-06 20:19:42 +02001043 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001044
1045 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001046 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1047 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001048
Francois Romieuffc46952012-07-06 14:19:23 +02001049 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001050 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001051}
1052
françois romieue6de30d2011-01-03 15:08:37 +00001053#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1054
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001055static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001056{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001057 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001058}
1059
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001060static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001061{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001062 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001063}
1064
Francois Romieu24192212012-07-06 20:19:42 +02001065static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001066{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001067 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001068
Francois Romieu24192212012-07-06 20:19:42 +02001069 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001070
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001071 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001072}
1073
Francois Romieu24192212012-07-06 20:19:42 +02001074static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001075{
1076 int value;
1077
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001078 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001079
Francois Romieu24192212012-07-06 20:19:42 +02001080 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001081
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001082 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001083
1084 return value;
1085}
1086
françois romieu4da19632011-01-03 15:07:55 +00001087static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001088{
Francois Romieu24192212012-07-06 20:19:42 +02001089 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001090}
1091
françois romieu4da19632011-01-03 15:07:55 +00001092static int rtl_readphy(struct rtl8169_private *tp, int location)
1093{
Francois Romieu24192212012-07-06 20:19:42 +02001094 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001095}
1096
1097static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1098{
1099 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1100}
1101
Chun-Hao Lin76564422014-10-01 23:17:17 +08001102static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001103{
1104 int val;
1105
françois romieu4da19632011-01-03 15:07:55 +00001106 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001107 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001108}
1109
Francois Romieuccdffb92008-07-26 14:26:06 +02001110static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1111 int val)
1112{
1113 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001114
françois romieu4da19632011-01-03 15:07:55 +00001115 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001116}
1117
1118static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1119{
1120 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001121
françois romieu4da19632011-01-03 15:07:55 +00001122 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001123}
1124
Francois Romieuffc46952012-07-06 14:19:23 +02001125DECLARE_RTL_COND(rtl_ephyar_cond)
1126{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001127 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001128}
1129
Francois Romieufdf6fc02012-07-06 22:40:38 +02001130static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001131{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001132 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001133 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1134
Francois Romieuffc46952012-07-06 14:19:23 +02001135 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1136
1137 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001138}
1139
Francois Romieufdf6fc02012-07-06 22:40:38 +02001140static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001141{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001142 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001143
Francois Romieuffc46952012-07-06 14:19:23 +02001144 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001145 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001146}
1147
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001148DECLARE_RTL_COND(rtl_eriar_cond)
1149{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001150 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001151}
1152
Francois Romieufdf6fc02012-07-06 22:40:38 +02001153static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1154 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001155{
Hayes Wang133ac402011-07-06 15:58:05 +08001156 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001157 RTL_W32(tp, ERIDR, val);
1158 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001159
Francois Romieuffc46952012-07-06 14:19:23 +02001160 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001161}
1162
Francois Romieufdf6fc02012-07-06 22:40:38 +02001163static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001164{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001165 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001166
Francois Romieuffc46952012-07-06 14:19:23 +02001167 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001168 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001169}
1170
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001171static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001172 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001173{
1174 u32 val;
1175
Francois Romieufdf6fc02012-07-06 22:40:38 +02001176 val = rtl_eri_read(tp, addr, type);
1177 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001178}
1179
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001180static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1181{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001182 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001183 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001184 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001185}
1186
1187static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1188{
1189 return rtl_eri_read(tp, reg, ERIAR_OOB);
1190}
1191
1192static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1193{
1194 switch (tp->mac_version) {
1195 case RTL_GIGA_MAC_VER_27:
1196 case RTL_GIGA_MAC_VER_28:
1197 case RTL_GIGA_MAC_VER_31:
1198 return r8168dp_ocp_read(tp, mask, reg);
1199 case RTL_GIGA_MAC_VER_49:
1200 case RTL_GIGA_MAC_VER_50:
1201 case RTL_GIGA_MAC_VER_51:
1202 return r8168ep_ocp_read(tp, mask, reg);
1203 default:
1204 BUG();
1205 return ~0;
1206 }
1207}
1208
1209static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1210 u32 data)
1211{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001212 RTL_W32(tp, OCPDR, data);
1213 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001214 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1215}
1216
1217static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1218 u32 data)
1219{
1220 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1221 data, ERIAR_OOB);
1222}
1223
1224static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1225{
1226 switch (tp->mac_version) {
1227 case RTL_GIGA_MAC_VER_27:
1228 case RTL_GIGA_MAC_VER_28:
1229 case RTL_GIGA_MAC_VER_31:
1230 r8168dp_ocp_write(tp, mask, reg, data);
1231 break;
1232 case RTL_GIGA_MAC_VER_49:
1233 case RTL_GIGA_MAC_VER_50:
1234 case RTL_GIGA_MAC_VER_51:
1235 r8168ep_ocp_write(tp, mask, reg, data);
1236 break;
1237 default:
1238 BUG();
1239 break;
1240 }
1241}
1242
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001243static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1244{
1245 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1246
1247 ocp_write(tp, 0x1, 0x30, 0x00000001);
1248}
1249
1250#define OOB_CMD_RESET 0x00
1251#define OOB_CMD_DRIVER_START 0x05
1252#define OOB_CMD_DRIVER_STOP 0x06
1253
1254static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1255{
1256 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1257}
1258
1259DECLARE_RTL_COND(rtl_ocp_read_cond)
1260{
1261 u16 reg;
1262
1263 reg = rtl8168_get_ocp_reg(tp);
1264
1265 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1266}
1267
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001268DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1269{
1270 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1271}
1272
1273DECLARE_RTL_COND(rtl_ocp_tx_cond)
1274{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001275 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001276}
1277
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001278static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1279{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001280 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001281 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001282 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1283 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001284}
1285
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001286static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001287{
1288 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001289 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1290}
1291
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001292static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1293{
1294 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1295 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1296 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1297}
1298
1299static void rtl8168_driver_start(struct rtl8169_private *tp)
1300{
1301 switch (tp->mac_version) {
1302 case RTL_GIGA_MAC_VER_27:
1303 case RTL_GIGA_MAC_VER_28:
1304 case RTL_GIGA_MAC_VER_31:
1305 rtl8168dp_driver_start(tp);
1306 break;
1307 case RTL_GIGA_MAC_VER_49:
1308 case RTL_GIGA_MAC_VER_50:
1309 case RTL_GIGA_MAC_VER_51:
1310 rtl8168ep_driver_start(tp);
1311 break;
1312 default:
1313 BUG();
1314 break;
1315 }
1316}
1317
1318static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1319{
1320 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1321 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1322}
1323
1324static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1325{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001326 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001327 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1328 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1329 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1330}
1331
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001332static void rtl8168_driver_stop(struct rtl8169_private *tp)
1333{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001334 switch (tp->mac_version) {
1335 case RTL_GIGA_MAC_VER_27:
1336 case RTL_GIGA_MAC_VER_28:
1337 case RTL_GIGA_MAC_VER_31:
1338 rtl8168dp_driver_stop(tp);
1339 break;
1340 case RTL_GIGA_MAC_VER_49:
1341 case RTL_GIGA_MAC_VER_50:
1342 case RTL_GIGA_MAC_VER_51:
1343 rtl8168ep_driver_stop(tp);
1344 break;
1345 default:
1346 BUG();
1347 break;
1348 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001349}
1350
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001351static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001352{
1353 u16 reg = rtl8168_get_ocp_reg(tp);
1354
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001355 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001356}
1357
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001358static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001359{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001360 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001361}
1362
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001363static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001364{
1365 switch (tp->mac_version) {
1366 case RTL_GIGA_MAC_VER_27:
1367 case RTL_GIGA_MAC_VER_28:
1368 case RTL_GIGA_MAC_VER_31:
1369 return r8168dp_check_dash(tp);
1370 case RTL_GIGA_MAC_VER_49:
1371 case RTL_GIGA_MAC_VER_50:
1372 case RTL_GIGA_MAC_VER_51:
1373 return r8168ep_check_dash(tp);
1374 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001375 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001376 }
1377}
1378
françois romieuc28aa382011-08-02 03:53:43 +00001379struct exgmac_reg {
1380 u16 addr;
1381 u16 mask;
1382 u32 val;
1383};
1384
Francois Romieufdf6fc02012-07-06 22:40:38 +02001385static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001386 const struct exgmac_reg *r, int len)
1387{
1388 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001389 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001390 r++;
1391 }
1392}
1393
Francois Romieuffc46952012-07-06 14:19:23 +02001394DECLARE_RTL_COND(rtl_efusear_cond)
1395{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001396 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001397}
1398
Francois Romieufdf6fc02012-07-06 22:40:38 +02001399static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001400{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001401 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001402
Francois Romieuffc46952012-07-06 14:19:23 +02001403 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001404 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001405}
1406
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001407static u16 rtl_get_events(struct rtl8169_private *tp)
1408{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001409 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001410}
1411
1412static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1413{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001414 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001415 mmiowb();
1416}
1417
1418static void rtl_irq_disable(struct rtl8169_private *tp)
1419{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001420 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001421 mmiowb();
1422}
1423
Francois Romieu3e990ff2012-01-26 12:50:01 +01001424static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1425{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001426 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001427}
1428
Francois Romieuda78dbf2012-01-26 14:18:23 +01001429#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1430#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1431#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1432
1433static void rtl_irq_enable_all(struct rtl8169_private *tp)
1434{
1435 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1436}
1437
françois romieu811fd302011-12-04 20:30:45 +00001438static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001440 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001441 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001442 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
françois romieu4da19632011-01-03 15:07:55 +00001445static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
françois romieu4da19632011-01-03 15:07:55 +00001447 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001450static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001452 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
françois romieu4da19632011-01-03 15:07:55 +00001455static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 unsigned int val;
1458
françois romieu4da19632011-01-03 15:07:55 +00001459 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1460 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Hayes Wang70090422011-07-06 15:58:06 +08001463static void rtl_link_chg_patch(struct rtl8169_private *tp)
1464{
Hayes Wang70090422011-07-06 15:58:06 +08001465 struct net_device *dev = tp->dev;
1466
1467 if (!netif_running(dev))
1468 return;
1469
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001470 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1471 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001472 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001473 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1474 ERIAR_EXGMAC);
1475 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1476 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001477 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001478 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1479 ERIAR_EXGMAC);
1480 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1481 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001482 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001483 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1484 ERIAR_EXGMAC);
1485 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1486 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001487 }
1488 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001489 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001490 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001491 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001492 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001493 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1494 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001495 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001496 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1497 ERIAR_EXGMAC);
1498 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1499 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001500 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001501 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1502 ERIAR_EXGMAC);
1503 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1504 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001505 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001506 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001507 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001508 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1509 ERIAR_EXGMAC);
1510 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1511 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001512 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001513 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1514 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001515 }
Hayes Wang70090422011-07-06 15:58:06 +08001516 }
1517}
1518
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001519static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001520 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001522 struct device *d = tp_to_dev(tp);
1523
Heiner Kallweite3972862018-06-29 08:07:04 +02001524 if (rtl8169_xmii_link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001525 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001526 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001527 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001529 if (net_ratelimit())
1530 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001533 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001534 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001538#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1539
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001540/* Currently we only enable WoL if explicitly told by userspace to circumvent
1541 * issues on certain platforms, see commit bde135a672bf ("r8169: only enable
1542 * PCI wakeups when WOL is active"). Let's keep __rtl8169_get_wol() for the
1543 * case that we want to respect BIOS settings again.
1544 */
1545#if 0
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001546static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1547{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001548 u8 options;
1549 u32 wolopts = 0;
1550
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001551 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001552 if (!(options & PMEnable))
1553 return 0;
1554
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001555 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001556 if (options & LinkUp)
1557 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001558 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001559 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1560 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001561 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1562 wolopts |= WAKE_MAGIC;
1563 break;
1564 default:
1565 if (options & MagicPacket)
1566 wolopts |= WAKE_MAGIC;
1567 break;
1568 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001569
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001570 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001571 if (options & UWF)
1572 wolopts |= WAKE_UCAST;
1573 if (options & BWF)
1574 wolopts |= WAKE_BCAST;
1575 if (options & MWF)
1576 wolopts |= WAKE_MCAST;
1577
1578 return wolopts;
1579}
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001580#endif
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001581
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001582static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1583{
1584 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001585
Francois Romieuda78dbf2012-01-26 14:18:23 +01001586 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001587 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001588 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001589 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001590}
1591
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001592static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001593{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001594 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001595 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001596 u32 opt;
1597 u16 reg;
1598 u8 mask;
1599 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001600 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001601 { WAKE_UCAST, Config5, UWF },
1602 { WAKE_BCAST, Config5, BWF },
1603 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001604 { WAKE_ANY, Config5, LanWake },
1605 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001606 };
Francois Romieu851e6022012-04-17 11:10:11 +02001607 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001608
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001609 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001610
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001611 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001612 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1613 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001614 tmp = ARRAY_SIZE(cfg) - 1;
1615 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001616 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001617 0x0dc,
1618 ERIAR_MASK_0100,
1619 MagicPacket_v2,
1620 0x0000,
1621 ERIAR_EXGMAC);
1622 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001623 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001624 0x0dc,
1625 ERIAR_MASK_0100,
1626 0x0000,
1627 MagicPacket_v2,
1628 ERIAR_EXGMAC);
1629 break;
1630 default:
1631 tmp = ARRAY_SIZE(cfg);
1632 break;
1633 }
1634
1635 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001636 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001637 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001638 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001639 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001640 }
1641
Francois Romieu851e6022012-04-17 11:10:11 +02001642 switch (tp->mac_version) {
1643 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001644 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001645 if (wolopts)
1646 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001647 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001648 break;
1649 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001650 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001651 if (wolopts)
1652 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001653 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001654 break;
1655 }
1656
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001657 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001658}
1659
1660static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1661{
1662 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001663 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001664
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001665 if (wol->wolopts & ~WAKE_ANY)
1666 return -EINVAL;
1667
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001668 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001669
Francois Romieuda78dbf2012-01-26 14:18:23 +01001670 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001671
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001672 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001673
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001674 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001675 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001676
1677 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001678
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001679 device_set_wakeup_enable(d, tp->saved_wolopts);
françois romieuea809072010-11-08 13:23:58 +00001680
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001681 pm_runtime_put_noidle(d);
1682
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001683 return 0;
1684}
1685
Francois Romieu31bd2042011-04-26 18:58:59 +02001686static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1687{
Francois Romieu85bffe62011-04-27 08:22:39 +02001688 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691static void rtl8169_get_drvinfo(struct net_device *dev,
1692 struct ethtool_drvinfo *info)
1693{
1694 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001695 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Rick Jones68aad782011-11-07 13:29:27 +00001697 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1698 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1699 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001700 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001701 if (!IS_ERR_OR_NULL(rtl_fw))
1702 strlcpy(info->fw_version, rtl_fw->version,
1703 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706static int rtl8169_get_regs_len(struct net_device *dev)
1707{
1708 return R8169_REGS_SIZE;
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001712 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001715 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001716 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Hayes Wang716b50a2011-02-22 17:26:18 +08001718 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001721 int auto_nego;
1722
françois romieu4da19632011-01-03 15:07:55 +00001723 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001724 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1725 ADVERTISE_100HALF | ADVERTISE_100FULL);
1726
1727 if (adv & ADVERTISED_10baseT_Half)
1728 auto_nego |= ADVERTISE_10HALF;
1729 if (adv & ADVERTISED_10baseT_Full)
1730 auto_nego |= ADVERTISE_10FULL;
1731 if (adv & ADVERTISED_100baseT_Half)
1732 auto_nego |= ADVERTISE_100HALF;
1733 if (adv & ADVERTISED_100baseT_Full)
1734 auto_nego |= ADVERTISE_100FULL;
1735
françois romieu3577aa12009-05-19 10:46:48 +00001736 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1737
françois romieu4da19632011-01-03 15:07:55 +00001738 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001739 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1740
1741 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001742 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001743 if (adv & ADVERTISED_1000baseT_Half)
1744 giga_ctrl |= ADVERTISE_1000HALF;
1745 if (adv & ADVERTISED_1000baseT_Full)
1746 giga_ctrl |= ADVERTISE_1000FULL;
1747 } else if (adv & (ADVERTISED_1000baseT_Half |
1748 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001749 netif_info(tp, link, dev,
1750 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001751 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
françois romieu3577aa12009-05-19 10:46:48 +00001754 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001755
françois romieu4da19632011-01-03 15:07:55 +00001756 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1757 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001758 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001759 if (speed == SPEED_10)
1760 bmcr = 0;
1761 else if (speed == SPEED_100)
1762 bmcr = BMCR_SPEED100;
1763 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001764 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001765
1766 if (duplex == DUPLEX_FULL)
1767 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001768 }
1769
françois romieu4da19632011-01-03 15:07:55 +00001770 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001771
Francois Romieucecb5fd2011-04-01 10:21:07 +02001772 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1773 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001774 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001775 rtl_writephy(tp, 0x17, 0x2138);
1776 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001777 } else {
françois romieu4da19632011-01-03 15:07:55 +00001778 rtl_writephy(tp, 0x17, 0x2108);
1779 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001780 }
1781 }
1782
Oliver Neukum54405cd2011-01-06 21:55:13 +01001783 rc = 0;
1784out:
1785 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
1788static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001789 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct rtl8169_private *tp = netdev_priv(dev);
1792 int ret;
1793
Heiner Kallweite3972862018-06-29 08:07:04 +02001794 ret = rtl8169_set_speed_xmii(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001795 if (ret < 0)
1796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Francois Romieu4876cc12011-03-11 21:07:11 +01001798 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001799 (advertising & ADVERTISED_1000baseT_Full) &&
1800 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001802 }
1803out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return ret;
1805}
1806
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001807static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1808 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Francois Romieud58d46b2011-05-03 16:38:29 +02001810 struct rtl8169_private *tp = netdev_priv(dev);
1811
Francois Romieu2b7b4312011-04-18 22:53:24 -07001812 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001813 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Francois Romieud58d46b2011-05-03 16:38:29 +02001815 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001816 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001817 features &= ~NETIF_F_IP_CSUM;
1818
Michał Mirosław350fb322011-04-08 06:35:56 +00001819 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Heiner Kallweita3984572018-04-28 22:19:15 +02001822static int rtl8169_set_features(struct net_device *dev,
1823 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001826 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Heiner Kallweita3984572018-04-28 22:19:15 +02001828 rtl_lock_work(tp);
1829
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001830 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001831 if (features & NETIF_F_RXALL)
1832 rx_config |= (AcceptErr | AcceptRunt);
1833 else
1834 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001836 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001837
hayeswang929a0312014-09-16 11:40:47 +08001838 if (features & NETIF_F_RXCSUM)
1839 tp->cp_cmd |= RxChkSum;
1840 else
1841 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001842
hayeswang929a0312014-09-16 11:40:47 +08001843 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1844 tp->cp_cmd |= RxVlan;
1845 else
1846 tp->cp_cmd &= ~RxVlan;
1847
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001848 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1849 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Francois Romieuda78dbf2012-01-26 14:18:23 +01001851 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 return 0;
1854}
1855
Kirill Smelkov810f4892012-11-10 21:11:02 +04001856static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001858 return (skb_vlan_tag_present(skb)) ?
1859 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Francois Romieu7a8fc772011-03-01 17:18:33 +01001862static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Francois Romieu7a8fc772011-03-01 17:18:33 +01001866 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001867 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Heiner Kallweite3972862018-06-29 08:07:04 +02001870static int rtl8169_get_link_ksettings(struct net_device *dev,
1871 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001875 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
1876
1877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01001880static int rtl8169_set_link_ksettings(struct net_device *dev,
1881 const struct ethtool_link_ksettings *cmd)
1882{
1883 struct rtl8169_private *tp = netdev_priv(dev);
1884 int rc;
1885 u32 advertising;
1886
1887 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
1888 cmd->link_modes.advertising))
1889 return -EINVAL;
1890
1891 del_timer_sync(&tp->timer);
1892
1893 rtl_lock_work(tp);
1894 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
1895 cmd->base.duplex, advertising);
1896 rtl_unlock_work(tp);
1897
1898 return rc;
1899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1902 void *p)
1903{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001904 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001905 u32 __iomem *data = tp->mmio_addr;
1906 u32 *dw = p;
1907 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Francois Romieuda78dbf2012-01-26 14:18:23 +01001909 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001910 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1911 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001912 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001915static u32 rtl8169_get_msglevel(struct net_device *dev)
1916{
1917 struct rtl8169_private *tp = netdev_priv(dev);
1918
1919 return tp->msg_enable;
1920}
1921
1922static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1923{
1924 struct rtl8169_private *tp = netdev_priv(dev);
1925
1926 tp->msg_enable = value;
1927}
1928
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001929static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1930 "tx_packets",
1931 "rx_packets",
1932 "tx_errors",
1933 "rx_errors",
1934 "rx_missed",
1935 "align_errors",
1936 "tx_single_collisions",
1937 "tx_multi_collisions",
1938 "unicast",
1939 "broadcast",
1940 "multicast",
1941 "tx_aborted",
1942 "tx_underrun",
1943};
1944
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001945static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001946{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001947 switch (sset) {
1948 case ETH_SS_STATS:
1949 return ARRAY_SIZE(rtl8169_gstrings);
1950 default:
1951 return -EOPNOTSUPP;
1952 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001953}
1954
Corinna Vinschen42020322015-09-10 10:47:35 +02001955DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001956{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001957 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001958}
1959
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001960static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001961{
Corinna Vinschen42020322015-09-10 10:47:35 +02001962 dma_addr_t paddr = tp->counters_phys_addr;
1963 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001964
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001965 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1966 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001967 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001968 RTL_W32(tp, CounterAddrLow, cmd);
1969 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001970
Francois Romieua78e9362018-01-26 01:53:26 +01001971 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001972}
1973
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001974static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001975{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001976 /*
1977 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1978 * tally counters.
1979 */
1980 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1981 return true;
1982
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001983 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001984}
1985
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001986static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001987{
Ivan Vecera355423d2009-02-06 21:49:57 -08001988 /*
1989 * Some chips are unable to dump tally counters when the receiver
1990 * is disabled.
1991 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001992 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001993 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001994
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001995 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001996}
1997
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001998static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001999{
Corinna Vinschen42020322015-09-10 10:47:35 +02002000 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002001 bool ret = false;
2002
2003 /*
2004 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2005 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2006 * reset by a power cycle, while the counter values collected by the
2007 * driver are reset at every driver unload/load cycle.
2008 *
2009 * To make sure the HW values returned by @get_stats64 match the SW
2010 * values, we collect the initial values at first open(*) and use them
2011 * as offsets to normalize the values returned by @get_stats64.
2012 *
2013 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2014 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2015 * set at open time by rtl_hw_start.
2016 */
2017
2018 if (tp->tc_offset.inited)
2019 return true;
2020
2021 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002022 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002023 ret = true;
2024
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002025 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002026 ret = true;
2027
Corinna Vinschen42020322015-09-10 10:47:35 +02002028 tp->tc_offset.tx_errors = counters->tx_errors;
2029 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2030 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002031 tp->tc_offset.inited = true;
2032
2033 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002034}
2035
Ivan Vecera355423d2009-02-06 21:49:57 -08002036static void rtl8169_get_ethtool_stats(struct net_device *dev,
2037 struct ethtool_stats *stats, u64 *data)
2038{
2039 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002040 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002041 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002042
2043 ASSERT_RTNL();
2044
Chun-Hao Line0636232016-07-29 16:37:55 +08002045 pm_runtime_get_noresume(d);
2046
2047 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002048 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002049
2050 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002051
Corinna Vinschen42020322015-09-10 10:47:35 +02002052 data[0] = le64_to_cpu(counters->tx_packets);
2053 data[1] = le64_to_cpu(counters->rx_packets);
2054 data[2] = le64_to_cpu(counters->tx_errors);
2055 data[3] = le32_to_cpu(counters->rx_errors);
2056 data[4] = le16_to_cpu(counters->rx_missed);
2057 data[5] = le16_to_cpu(counters->align_errors);
2058 data[6] = le32_to_cpu(counters->tx_one_collision);
2059 data[7] = le32_to_cpu(counters->tx_multi_collision);
2060 data[8] = le64_to_cpu(counters->rx_unicast);
2061 data[9] = le64_to_cpu(counters->rx_broadcast);
2062 data[10] = le32_to_cpu(counters->rx_multicast);
2063 data[11] = le16_to_cpu(counters->tx_aborted);
2064 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002065}
2066
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002067static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2068{
2069 switch(stringset) {
2070 case ETH_SS_STATS:
2071 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2072 break;
2073 }
2074}
2075
Florian Fainellif0903ea2016-12-03 12:01:19 -08002076static int rtl8169_nway_reset(struct net_device *dev)
2077{
2078 struct rtl8169_private *tp = netdev_priv(dev);
2079
2080 return mii_nway_restart(&tp->mii);
2081}
2082
Francois Romieu50970832017-10-27 13:24:49 +03002083/*
2084 * Interrupt coalescing
2085 *
2086 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2087 * > 8169, 8168 and 810x line of chipsets
2088 *
2089 * 8169, 8168, and 8136(810x) serial chipsets support it.
2090 *
2091 * > 2 - the Tx timer unit at gigabit speed
2092 *
2093 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2094 * (0xe0) bit 1 and bit 0.
2095 *
2096 * For 8169
2097 * bit[1:0] \ speed 1000M 100M 10M
2098 * 0 0 320ns 2.56us 40.96us
2099 * 0 1 2.56us 20.48us 327.7us
2100 * 1 0 5.12us 40.96us 655.4us
2101 * 1 1 10.24us 81.92us 1.31ms
2102 *
2103 * For the other
2104 * bit[1:0] \ speed 1000M 100M 10M
2105 * 0 0 5us 2.56us 40.96us
2106 * 0 1 40us 20.48us 327.7us
2107 * 1 0 80us 40.96us 655.4us
2108 * 1 1 160us 81.92us 1.31ms
2109 */
2110
2111/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2112struct rtl_coalesce_scale {
2113 /* Rx / Tx */
2114 u32 nsecs[2];
2115};
2116
2117/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2118struct rtl_coalesce_info {
2119 u32 speed;
2120 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2121};
2122
2123/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2124#define rxtx_x1822(r, t) { \
2125 {{(r), (t)}}, \
2126 {{(r)*8, (t)*8}}, \
2127 {{(r)*8*2, (t)*8*2}}, \
2128 {{(r)*8*2*2, (t)*8*2*2}}, \
2129}
2130static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2131 /* speed delays: rx00 tx00 */
2132 { SPEED_10, rxtx_x1822(40960, 40960) },
2133 { SPEED_100, rxtx_x1822( 2560, 2560) },
2134 { SPEED_1000, rxtx_x1822( 320, 320) },
2135 { 0 },
2136};
2137
2138static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2139 /* speed delays: rx00 tx00 */
2140 { SPEED_10, rxtx_x1822(40960, 40960) },
2141 { SPEED_100, rxtx_x1822( 2560, 2560) },
2142 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2143 { 0 },
2144};
2145#undef rxtx_x1822
2146
2147/* get rx/tx scale vector corresponding to current speed */
2148static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2149{
2150 struct rtl8169_private *tp = netdev_priv(dev);
2151 struct ethtool_link_ksettings ecmd;
2152 const struct rtl_coalesce_info *ci;
2153 int rc;
2154
2155 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2156 if (rc < 0)
2157 return ERR_PTR(rc);
2158
2159 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2160 if (ecmd.base.speed == ci->speed) {
2161 return ci;
2162 }
2163 }
2164
2165 return ERR_PTR(-ELNRNG);
2166}
2167
2168static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2169{
2170 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002171 const struct rtl_coalesce_info *ci;
2172 const struct rtl_coalesce_scale *scale;
2173 struct {
2174 u32 *max_frames;
2175 u32 *usecs;
2176 } coal_settings [] = {
2177 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2178 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2179 }, *p = coal_settings;
2180 int i;
2181 u16 w;
2182
2183 memset(ec, 0, sizeof(*ec));
2184
2185 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2186 ci = rtl_coalesce_info(dev);
2187 if (IS_ERR(ci))
2188 return PTR_ERR(ci);
2189
Heiner Kallweit0ae09742018-04-28 22:19:26 +02002190 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03002191
2192 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002193 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002194 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2195 w >>= RTL_COALESCE_SHIFT;
2196 *p->usecs = w & RTL_COALESCE_MASK;
2197 }
2198
2199 for (i = 0; i < 2; i++) {
2200 p = coal_settings + i;
2201 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2202
2203 /*
2204 * ethtool_coalesce says it is illegal to set both usecs and
2205 * max_frames to 0.
2206 */
2207 if (!*p->usecs && !*p->max_frames)
2208 *p->max_frames = 1;
2209 }
2210
2211 return 0;
2212}
2213
2214/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2215static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2216 struct net_device *dev, u32 nsec, u16 *cp01)
2217{
2218 const struct rtl_coalesce_info *ci;
2219 u16 i;
2220
2221 ci = rtl_coalesce_info(dev);
2222 if (IS_ERR(ci))
2223 return ERR_CAST(ci);
2224
2225 for (i = 0; i < 4; i++) {
2226 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2227 ci->scalev[i].nsecs[1]);
2228 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2229 *cp01 = i;
2230 return &ci->scalev[i];
2231 }
2232 }
2233
2234 return ERR_PTR(-EINVAL);
2235}
2236
2237static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2238{
2239 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002240 const struct rtl_coalesce_scale *scale;
2241 struct {
2242 u32 frames;
2243 u32 usecs;
2244 } coal_settings [] = {
2245 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2246 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2247 }, *p = coal_settings;
2248 u16 w = 0, cp01;
2249 int i;
2250
2251 scale = rtl_coalesce_choose_scale(dev,
2252 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2253 if (IS_ERR(scale))
2254 return PTR_ERR(scale);
2255
2256 for (i = 0; i < 2; i++, p++) {
2257 u32 units;
2258
2259 /*
2260 * accept max_frames=1 we returned in rtl_get_coalesce.
2261 * accept it not only when usecs=0 because of e.g. the following scenario:
2262 *
2263 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2264 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2265 * - then user does `ethtool -C eth0 rx-usecs 100`
2266 *
2267 * since ethtool sends to kernel whole ethtool_coalesce
2268 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2269 * we'll reject it below in `frames % 4 != 0`.
2270 */
2271 if (p->frames == 1) {
2272 p->frames = 0;
2273 }
2274
2275 units = p->usecs * 1000 / scale->nsecs[i];
2276 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2277 return -EINVAL;
2278
2279 w <<= RTL_COALESCE_SHIFT;
2280 w |= units;
2281 w <<= RTL_COALESCE_SHIFT;
2282 w |= p->frames >> 2;
2283 }
2284
2285 rtl_lock_work(tp);
2286
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002287 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002288
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002289 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002290 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2291 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002292
2293 rtl_unlock_work(tp);
2294
2295 return 0;
2296}
2297
Jeff Garzik7282d492006-09-13 14:30:00 -04002298static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 .get_drvinfo = rtl8169_get_drvinfo,
2300 .get_regs_len = rtl8169_get_regs_len,
2301 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002302 .get_coalesce = rtl_get_coalesce,
2303 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002304 .get_msglevel = rtl8169_get_msglevel,
2305 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002307 .get_wol = rtl8169_get_wol,
2308 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002309 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002310 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002311 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002312 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002313 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002314 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002315 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316};
2317
Francois Romieu07d3f512007-02-21 22:40:46 +01002318static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002319 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Francois Romieu0e485152007-02-20 00:00:26 +01002321 /*
2322 * The driver currently handles the 8168Bf and the 8168Be identically
2323 * but they can be identified more specifically through the test below
2324 * if needed:
2325 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002326 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002327 *
2328 * Same thing for the 8101Eb and the 8101Ec:
2329 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002330 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002331 */
Francois Romieu37441002011-06-17 22:58:54 +02002332 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002334 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 int mac_version;
2336 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002337 /* 8168EP family. */
2338 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2339 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2340 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2341
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002342 /* 8168H family. */
2343 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2344 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2345
Hayes Wangc5583862012-07-02 17:23:22 +08002346 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002347 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002348 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002349 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2350 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2351
Hayes Wangc2218922011-09-06 16:55:18 +08002352 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002353 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002354 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2355 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2356
hayeswang01dc7fe2011-03-21 01:50:28 +00002357 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002358 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002359 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2360 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2361
Francois Romieu5b538df2008-07-20 16:22:45 +02002362 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002363 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002364 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002365
françois romieue6de30d2011-01-03 15:08:37 +00002366 /* 8168DP family. */
2367 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2368 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002369 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002370
Francois Romieuef808d52008-06-29 13:10:54 +02002371 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002372 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002373 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002374 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002375 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2376 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002377 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002378 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002379
2380 /* 8168B family. */
2381 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002382 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2383 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2384
2385 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002386 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002387 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002388 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2389 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002390 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2391 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2392 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2393 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002394 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002395 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002396 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002397 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2398 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002399 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2400 /* FIXME: where did these entries come from ? -- FR */
2401 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2402 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2403
2404 /* 8110 family. */
2405 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2406 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2407 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2408 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2409 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2410 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2411
Jean Delvaref21b75e2009-05-26 20:54:48 -07002412 /* Catch-all */
2413 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002414 };
2415 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 u32 reg;
2417
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002418 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002419 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 p++;
2421 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002422
2423 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002424 dev_notice(tp_to_dev(tp),
2425 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002426 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002427 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2428 tp->mac_version = tp->mii.supports_gmii ?
2429 RTL_GIGA_MAC_VER_42 :
2430 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002431 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2432 tp->mac_version = tp->mii.supports_gmii ?
2433 RTL_GIGA_MAC_VER_45 :
2434 RTL_GIGA_MAC_VER_47;
2435 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2436 tp->mac_version = tp->mii.supports_gmii ?
2437 RTL_GIGA_MAC_VER_46 :
2438 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2443{
Heiner Kallweit49d17512018-06-28 20:36:15 +02002444 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Francois Romieu867763c2007-08-17 18:21:58 +02002447struct phy_reg {
2448 u16 reg;
2449 u16 val;
2450};
2451
françois romieu4da19632011-01-03 15:07:55 +00002452static void rtl_writephy_batch(struct rtl8169_private *tp,
2453 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002454{
2455 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002456 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002457 regs++;
2458 }
2459}
2460
françois romieubca03d52011-01-03 15:07:31 +00002461#define PHY_READ 0x00000000
2462#define PHY_DATA_OR 0x10000000
2463#define PHY_DATA_AND 0x20000000
2464#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002465#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002466#define PHY_CLEAR_READCOUNT 0x70000000
2467#define PHY_WRITE 0x80000000
2468#define PHY_READCOUNT_EQ_SKIP 0x90000000
2469#define PHY_COMP_EQ_SKIPN 0xa0000000
2470#define PHY_COMP_NEQ_SKIPN 0xb0000000
2471#define PHY_WRITE_PREVIOUS 0xc0000000
2472#define PHY_SKIPN 0xd0000000
2473#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002474
Hayes Wang960aee62011-06-18 11:37:48 +02002475struct fw_info {
2476 u32 magic;
2477 char version[RTL_VER_SIZE];
2478 __le32 fw_start;
2479 __le32 fw_len;
2480 u8 chksum;
2481} __packed;
2482
Francois Romieu1c361ef2011-06-17 17:16:24 +02002483#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2484
2485static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002486{
Francois Romieub6ffd972011-06-17 17:00:05 +02002487 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002488 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002489 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2490 char *version = rtl_fw->version;
2491 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002492
Francois Romieu1c361ef2011-06-17 17:16:24 +02002493 if (fw->size < FW_OPCODE_SIZE)
2494 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002495
2496 if (!fw_info->magic) {
2497 size_t i, size, start;
2498 u8 checksum = 0;
2499
2500 if (fw->size < sizeof(*fw_info))
2501 goto out;
2502
2503 for (i = 0; i < fw->size; i++)
2504 checksum += fw->data[i];
2505 if (checksum != 0)
2506 goto out;
2507
2508 start = le32_to_cpu(fw_info->fw_start);
2509 if (start > fw->size)
2510 goto out;
2511
2512 size = le32_to_cpu(fw_info->fw_len);
2513 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2514 goto out;
2515
2516 memcpy(version, fw_info->version, RTL_VER_SIZE);
2517
2518 pa->code = (__le32 *)(fw->data + start);
2519 pa->size = size;
2520 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002521 if (fw->size % FW_OPCODE_SIZE)
2522 goto out;
2523
2524 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2525
2526 pa->code = (__le32 *)fw->data;
2527 pa->size = fw->size / FW_OPCODE_SIZE;
2528 }
2529 version[RTL_VER_SIZE - 1] = 0;
2530
2531 rc = true;
2532out:
2533 return rc;
2534}
2535
Francois Romieufd112f22011-06-18 00:10:29 +02002536static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2537 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002538{
Francois Romieufd112f22011-06-18 00:10:29 +02002539 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002540 size_t index;
2541
Francois Romieu1c361ef2011-06-17 17:16:24 +02002542 for (index = 0; index < pa->size; index++) {
2543 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002544 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002545
hayeswang42b82dc2011-01-10 02:07:25 +00002546 switch(action & 0xf0000000) {
2547 case PHY_READ:
2548 case PHY_DATA_OR:
2549 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002550 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002551 case PHY_CLEAR_READCOUNT:
2552 case PHY_WRITE:
2553 case PHY_WRITE_PREVIOUS:
2554 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002555 break;
2556
hayeswang42b82dc2011-01-10 02:07:25 +00002557 case PHY_BJMPN:
2558 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002559 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002560 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002561 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002562 }
2563 break;
2564 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002565 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002566 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002567 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002568 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002569 }
2570 break;
2571 case PHY_COMP_EQ_SKIPN:
2572 case PHY_COMP_NEQ_SKIPN:
2573 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002574 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002575 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002576 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002577 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002578 }
2579 break;
2580
hayeswang42b82dc2011-01-10 02:07:25 +00002581 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002582 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002583 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002584 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002585 }
2586 }
Francois Romieufd112f22011-06-18 00:10:29 +02002587 rc = true;
2588out:
2589 return rc;
2590}
françois romieubca03d52011-01-03 15:07:31 +00002591
Francois Romieufd112f22011-06-18 00:10:29 +02002592static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2593{
2594 struct net_device *dev = tp->dev;
2595 int rc = -EINVAL;
2596
2597 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002598 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002599 goto out;
2600 }
2601
2602 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2603 rc = 0;
2604out:
2605 return rc;
2606}
2607
2608static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2609{
2610 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002611 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002612 u32 predata, count;
2613 size_t index;
2614
2615 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002616 org.write = ops->write;
2617 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002618
Francois Romieu1c361ef2011-06-17 17:16:24 +02002619 for (index = 0; index < pa->size; ) {
2620 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002621 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002622 u32 regno = (action & 0x0fff0000) >> 16;
2623
2624 if (!action)
2625 break;
françois romieubca03d52011-01-03 15:07:31 +00002626
2627 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002628 case PHY_READ:
2629 predata = rtl_readphy(tp, regno);
2630 count++;
2631 index++;
françois romieubca03d52011-01-03 15:07:31 +00002632 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002633 case PHY_DATA_OR:
2634 predata |= data;
2635 index++;
2636 break;
2637 case PHY_DATA_AND:
2638 predata &= data;
2639 index++;
2640 break;
2641 case PHY_BJMPN:
2642 index -= regno;
2643 break;
hayeswangeee37862013-04-01 22:23:38 +00002644 case PHY_MDIO_CHG:
2645 if (data == 0) {
2646 ops->write = org.write;
2647 ops->read = org.read;
2648 } else if (data == 1) {
2649 ops->write = mac_mcu_write;
2650 ops->read = mac_mcu_read;
2651 }
2652
hayeswang42b82dc2011-01-10 02:07:25 +00002653 index++;
2654 break;
2655 case PHY_CLEAR_READCOUNT:
2656 count = 0;
2657 index++;
2658 break;
2659 case PHY_WRITE:
2660 rtl_writephy(tp, regno, data);
2661 index++;
2662 break;
2663 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002664 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002665 break;
2666 case PHY_COMP_EQ_SKIPN:
2667 if (predata == data)
2668 index += regno;
2669 index++;
2670 break;
2671 case PHY_COMP_NEQ_SKIPN:
2672 if (predata != data)
2673 index += regno;
2674 index++;
2675 break;
2676 case PHY_WRITE_PREVIOUS:
2677 rtl_writephy(tp, regno, predata);
2678 index++;
2679 break;
2680 case PHY_SKIPN:
2681 index += regno + 1;
2682 break;
2683 case PHY_DELAY_MS:
2684 mdelay(data);
2685 index++;
2686 break;
2687
françois romieubca03d52011-01-03 15:07:31 +00002688 default:
2689 BUG();
2690 }
2691 }
hayeswangeee37862013-04-01 22:23:38 +00002692
2693 ops->write = org.write;
2694 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002695}
2696
françois romieuf1e02ed2011-01-13 13:07:53 +00002697static void rtl_release_firmware(struct rtl8169_private *tp)
2698{
Francois Romieub6ffd972011-06-17 17:00:05 +02002699 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2700 release_firmware(tp->rtl_fw->fw);
2701 kfree(tp->rtl_fw);
2702 }
2703 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002704}
2705
François Romieu953a12c2011-04-24 17:38:48 +02002706static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002707{
Francois Romieub6ffd972011-06-17 17:00:05 +02002708 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002709
2710 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002711 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002712 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002713}
2714
2715static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2716{
2717 if (rtl_readphy(tp, reg) != val)
2718 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2719 else
2720 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002721}
2722
françois romieu4da19632011-01-03 15:07:55 +00002723static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002725 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002726 { 0x1f, 0x0001 },
2727 { 0x06, 0x006e },
2728 { 0x08, 0x0708 },
2729 { 0x15, 0x4000 },
2730 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
françois romieu0b9b5712009-08-10 19:44:56 +00002732 { 0x1f, 0x0001 },
2733 { 0x03, 0x00a1 },
2734 { 0x02, 0x0008 },
2735 { 0x01, 0x0120 },
2736 { 0x00, 0x1000 },
2737 { 0x04, 0x0800 },
2738 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
françois romieu0b9b5712009-08-10 19:44:56 +00002740 { 0x03, 0xff41 },
2741 { 0x02, 0xdf60 },
2742 { 0x01, 0x0140 },
2743 { 0x00, 0x0077 },
2744 { 0x04, 0x7800 },
2745 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
françois romieu0b9b5712009-08-10 19:44:56 +00002747 { 0x03, 0x802f },
2748 { 0x02, 0x4f02 },
2749 { 0x01, 0x0409 },
2750 { 0x00, 0xf0f9 },
2751 { 0x04, 0x9800 },
2752 { 0x04, 0x9000 },
2753
2754 { 0x03, 0xdf01 },
2755 { 0x02, 0xdf20 },
2756 { 0x01, 0xff95 },
2757 { 0x00, 0xba00 },
2758 { 0x04, 0xa800 },
2759 { 0x04, 0xa000 },
2760
2761 { 0x03, 0xff41 },
2762 { 0x02, 0xdf20 },
2763 { 0x01, 0x0140 },
2764 { 0x00, 0x00bb },
2765 { 0x04, 0xb800 },
2766 { 0x04, 0xb000 },
2767
2768 { 0x03, 0xdf41 },
2769 { 0x02, 0xdc60 },
2770 { 0x01, 0x6340 },
2771 { 0x00, 0x007d },
2772 { 0x04, 0xd800 },
2773 { 0x04, 0xd000 },
2774
2775 { 0x03, 0xdf01 },
2776 { 0x02, 0xdf20 },
2777 { 0x01, 0x100a },
2778 { 0x00, 0xa0ff },
2779 { 0x04, 0xf800 },
2780 { 0x04, 0xf000 },
2781
2782 { 0x1f, 0x0000 },
2783 { 0x0b, 0x0000 },
2784 { 0x00, 0x9200 }
2785 };
2786
françois romieu4da19632011-01-03 15:07:55 +00002787 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
françois romieu4da19632011-01-03 15:07:55 +00002790static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002791{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002792 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002793 { 0x1f, 0x0002 },
2794 { 0x01, 0x90d0 },
2795 { 0x1f, 0x0000 }
2796 };
2797
françois romieu4da19632011-01-03 15:07:55 +00002798 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002799}
2800
françois romieu4da19632011-01-03 15:07:55 +00002801static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002802{
2803 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002804
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002805 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2806 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002807 return;
2808
françois romieu4da19632011-01-03 15:07:55 +00002809 rtl_writephy(tp, 0x1f, 0x0001);
2810 rtl_writephy(tp, 0x10, 0xf01b);
2811 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002812}
2813
françois romieu4da19632011-01-03 15:07:55 +00002814static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002815{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002816 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002817 { 0x1f, 0x0001 },
2818 { 0x04, 0x0000 },
2819 { 0x03, 0x00a1 },
2820 { 0x02, 0x0008 },
2821 { 0x01, 0x0120 },
2822 { 0x00, 0x1000 },
2823 { 0x04, 0x0800 },
2824 { 0x04, 0x9000 },
2825 { 0x03, 0x802f },
2826 { 0x02, 0x4f02 },
2827 { 0x01, 0x0409 },
2828 { 0x00, 0xf099 },
2829 { 0x04, 0x9800 },
2830 { 0x04, 0xa000 },
2831 { 0x03, 0xdf01 },
2832 { 0x02, 0xdf20 },
2833 { 0x01, 0xff95 },
2834 { 0x00, 0xba00 },
2835 { 0x04, 0xa800 },
2836 { 0x04, 0xf000 },
2837 { 0x03, 0xdf01 },
2838 { 0x02, 0xdf20 },
2839 { 0x01, 0x101a },
2840 { 0x00, 0xa0ff },
2841 { 0x04, 0xf800 },
2842 { 0x04, 0x0000 },
2843 { 0x1f, 0x0000 },
2844
2845 { 0x1f, 0x0001 },
2846 { 0x10, 0xf41b },
2847 { 0x14, 0xfb54 },
2848 { 0x18, 0xf5c7 },
2849 { 0x1f, 0x0000 },
2850
2851 { 0x1f, 0x0001 },
2852 { 0x17, 0x0cc0 },
2853 { 0x1f, 0x0000 }
2854 };
2855
françois romieu4da19632011-01-03 15:07:55 +00002856 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002857
françois romieu4da19632011-01-03 15:07:55 +00002858 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002859}
2860
françois romieu4da19632011-01-03 15:07:55 +00002861static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002862{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002863 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002864 { 0x1f, 0x0001 },
2865 { 0x04, 0x0000 },
2866 { 0x03, 0x00a1 },
2867 { 0x02, 0x0008 },
2868 { 0x01, 0x0120 },
2869 { 0x00, 0x1000 },
2870 { 0x04, 0x0800 },
2871 { 0x04, 0x9000 },
2872 { 0x03, 0x802f },
2873 { 0x02, 0x4f02 },
2874 { 0x01, 0x0409 },
2875 { 0x00, 0xf099 },
2876 { 0x04, 0x9800 },
2877 { 0x04, 0xa000 },
2878 { 0x03, 0xdf01 },
2879 { 0x02, 0xdf20 },
2880 { 0x01, 0xff95 },
2881 { 0x00, 0xba00 },
2882 { 0x04, 0xa800 },
2883 { 0x04, 0xf000 },
2884 { 0x03, 0xdf01 },
2885 { 0x02, 0xdf20 },
2886 { 0x01, 0x101a },
2887 { 0x00, 0xa0ff },
2888 { 0x04, 0xf800 },
2889 { 0x04, 0x0000 },
2890 { 0x1f, 0x0000 },
2891
2892 { 0x1f, 0x0001 },
2893 { 0x0b, 0x8480 },
2894 { 0x1f, 0x0000 },
2895
2896 { 0x1f, 0x0001 },
2897 { 0x18, 0x67c7 },
2898 { 0x04, 0x2000 },
2899 { 0x03, 0x002f },
2900 { 0x02, 0x4360 },
2901 { 0x01, 0x0109 },
2902 { 0x00, 0x3022 },
2903 { 0x04, 0x2800 },
2904 { 0x1f, 0x0000 },
2905
2906 { 0x1f, 0x0001 },
2907 { 0x17, 0x0cc0 },
2908 { 0x1f, 0x0000 }
2909 };
2910
françois romieu4da19632011-01-03 15:07:55 +00002911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002912}
2913
françois romieu4da19632011-01-03 15:07:55 +00002914static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002915{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002916 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002917 { 0x10, 0xf41b },
2918 { 0x1f, 0x0000 }
2919 };
2920
françois romieu4da19632011-01-03 15:07:55 +00002921 rtl_writephy(tp, 0x1f, 0x0001);
2922 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002923
françois romieu4da19632011-01-03 15:07:55 +00002924 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002925}
2926
françois romieu4da19632011-01-03 15:07:55 +00002927static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002928{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002929 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002930 { 0x1f, 0x0001 },
2931 { 0x10, 0xf41b },
2932 { 0x1f, 0x0000 }
2933 };
2934
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002936}
2937
françois romieu4da19632011-01-03 15:07:55 +00002938static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002939{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002940 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002941 { 0x1f, 0x0000 },
2942 { 0x1d, 0x0f00 },
2943 { 0x1f, 0x0002 },
2944 { 0x0c, 0x1ec8 },
2945 { 0x1f, 0x0000 }
2946 };
2947
françois romieu4da19632011-01-03 15:07:55 +00002948 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002949}
2950
françois romieu4da19632011-01-03 15:07:55 +00002951static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002952{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002953 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002954 { 0x1f, 0x0001 },
2955 { 0x1d, 0x3d98 },
2956 { 0x1f, 0x0000 }
2957 };
2958
françois romieu4da19632011-01-03 15:07:55 +00002959 rtl_writephy(tp, 0x1f, 0x0000);
2960 rtl_patchphy(tp, 0x14, 1 << 5);
2961 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002962
françois romieu4da19632011-01-03 15:07:55 +00002963 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002964}
2965
françois romieu4da19632011-01-03 15:07:55 +00002966static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002967{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002968 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002969 { 0x1f, 0x0001 },
2970 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002971 { 0x1f, 0x0002 },
2972 { 0x00, 0x88d4 },
2973 { 0x01, 0x82b1 },
2974 { 0x03, 0x7002 },
2975 { 0x08, 0x9e30 },
2976 { 0x09, 0x01f0 },
2977 { 0x0a, 0x5500 },
2978 { 0x0c, 0x00c8 },
2979 { 0x1f, 0x0003 },
2980 { 0x12, 0xc096 },
2981 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002982 { 0x1f, 0x0000 },
2983 { 0x1f, 0x0000 },
2984 { 0x09, 0x2000 },
2985 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002986 };
2987
françois romieu4da19632011-01-03 15:07:55 +00002988 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002989
françois romieu4da19632011-01-03 15:07:55 +00002990 rtl_patchphy(tp, 0x14, 1 << 5);
2991 rtl_patchphy(tp, 0x0d, 1 << 5);
2992 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002993}
2994
françois romieu4da19632011-01-03 15:07:55 +00002995static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002996{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002997 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002998 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002999 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003000 { 0x03, 0x802f },
3001 { 0x02, 0x4f02 },
3002 { 0x01, 0x0409 },
3003 { 0x00, 0xf099 },
3004 { 0x04, 0x9800 },
3005 { 0x04, 0x9000 },
3006 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003007 { 0x1f, 0x0002 },
3008 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003009 { 0x06, 0x0761 },
3010 { 0x1f, 0x0003 },
3011 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003012 { 0x1f, 0x0000 }
3013 };
3014
françois romieu4da19632011-01-03 15:07:55 +00003015 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003016
françois romieu4da19632011-01-03 15:07:55 +00003017 rtl_patchphy(tp, 0x16, 1 << 0);
3018 rtl_patchphy(tp, 0x14, 1 << 5);
3019 rtl_patchphy(tp, 0x0d, 1 << 5);
3020 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003021}
3022
françois romieu4da19632011-01-03 15:07:55 +00003023static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003024{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003025 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003026 { 0x1f, 0x0001 },
3027 { 0x12, 0x2300 },
3028 { 0x1d, 0x3d98 },
3029 { 0x1f, 0x0002 },
3030 { 0x0c, 0x7eb8 },
3031 { 0x06, 0x5461 },
3032 { 0x1f, 0x0003 },
3033 { 0x16, 0x0f0a },
3034 { 0x1f, 0x0000 }
3035 };
3036
françois romieu4da19632011-01-03 15:07:55 +00003037 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003038
françois romieu4da19632011-01-03 15:07:55 +00003039 rtl_patchphy(tp, 0x16, 1 << 0);
3040 rtl_patchphy(tp, 0x14, 1 << 5);
3041 rtl_patchphy(tp, 0x0d, 1 << 5);
3042 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003043}
3044
françois romieu4da19632011-01-03 15:07:55 +00003045static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003046{
françois romieu4da19632011-01-03 15:07:55 +00003047 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003048}
3049
françois romieubca03d52011-01-03 15:07:31 +00003050static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003051{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003052 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003053 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003054 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003055 { 0x06, 0x4064 },
3056 { 0x07, 0x2863 },
3057 { 0x08, 0x059c },
3058 { 0x09, 0x26b4 },
3059 { 0x0a, 0x6a19 },
3060 { 0x0b, 0xdcc8 },
3061 { 0x10, 0xf06d },
3062 { 0x14, 0x7f68 },
3063 { 0x18, 0x7fd9 },
3064 { 0x1c, 0xf0ff },
3065 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003066 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003067 { 0x12, 0xf49f },
3068 { 0x13, 0x070b },
3069 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003070 { 0x14, 0x94c0 },
3071
3072 /*
3073 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003074 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003075 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003076 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003077 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003078 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003079 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003080 { 0x06, 0x5561 },
3081
3082 /*
3083 * Can not link to 1Gbps with bad cable
3084 * Decrease SNR threshold form 21.07dB to 19.04dB
3085 */
3086 { 0x1f, 0x0001 },
3087 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003088
3089 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003090 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003091 };
3092
françois romieu4da19632011-01-03 15:07:55 +00003093 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003094
françois romieubca03d52011-01-03 15:07:31 +00003095 /*
3096 * Rx Error Issue
3097 * Fine Tune Switching regulator parameter
3098 */
françois romieu4da19632011-01-03 15:07:55 +00003099 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003100 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3101 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003102
Francois Romieufdf6fc02012-07-06 22:40:38 +02003103 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003104 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003105 { 0x1f, 0x0002 },
3106 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003107 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003108 { 0x05, 0x8330 },
3109 { 0x06, 0x669a },
3110 { 0x1f, 0x0002 }
3111 };
3112 int val;
3113
françois romieu4da19632011-01-03 15:07:55 +00003114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003115
françois romieu4da19632011-01-03 15:07:55 +00003116 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003117
3118 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003119 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003120 0x0065, 0x0066, 0x0067, 0x0068,
3121 0x0069, 0x006a, 0x006b, 0x006c
3122 };
3123 int i;
3124
françois romieu4da19632011-01-03 15:07:55 +00003125 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003126
3127 val &= 0xff00;
3128 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003129 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003130 }
3131 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003132 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003133 { 0x1f, 0x0002 },
3134 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003135 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003136 { 0x05, 0x8330 },
3137 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003138 };
3139
françois romieu4da19632011-01-03 15:07:55 +00003140 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003141 }
3142
françois romieubca03d52011-01-03 15:07:31 +00003143 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003144 rtl_writephy(tp, 0x1f, 0x0002);
3145 rtl_patchphy(tp, 0x0d, 0x0300);
3146 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003147
françois romieubca03d52011-01-03 15:07:31 +00003148 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003149 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003150 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3151 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003152
françois romieu4da19632011-01-03 15:07:55 +00003153 rtl_writephy(tp, 0x1f, 0x0005);
3154 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003155
3156 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003157
françois romieu4da19632011-01-03 15:07:55 +00003158 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003159}
3160
françois romieubca03d52011-01-03 15:07:31 +00003161static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003162{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003163 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003164 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003165 { 0x1f, 0x0001 },
3166 { 0x06, 0x4064 },
3167 { 0x07, 0x2863 },
3168 { 0x08, 0x059c },
3169 { 0x09, 0x26b4 },
3170 { 0x0a, 0x6a19 },
3171 { 0x0b, 0xdcc8 },
3172 { 0x10, 0xf06d },
3173 { 0x14, 0x7f68 },
3174 { 0x18, 0x7fd9 },
3175 { 0x1c, 0xf0ff },
3176 { 0x1d, 0x3d9c },
3177 { 0x1f, 0x0003 },
3178 { 0x12, 0xf49f },
3179 { 0x13, 0x070b },
3180 { 0x1a, 0x05ad },
3181 { 0x14, 0x94c0 },
3182
françois romieubca03d52011-01-03 15:07:31 +00003183 /*
3184 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003185 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003186 */
françois romieudaf9df62009-10-07 12:44:20 +00003187 { 0x1f, 0x0002 },
3188 { 0x06, 0x5561 },
3189 { 0x1f, 0x0005 },
3190 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003191 { 0x06, 0x5561 },
3192
3193 /*
3194 * Can not link to 1Gbps with bad cable
3195 * Decrease SNR threshold form 21.07dB to 19.04dB
3196 */
3197 { 0x1f, 0x0001 },
3198 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003199
3200 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003201 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003202 };
3203
françois romieu4da19632011-01-03 15:07:55 +00003204 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003205
Francois Romieufdf6fc02012-07-06 22:40:38 +02003206 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003207 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003208 { 0x1f, 0x0002 },
3209 { 0x05, 0x669a },
3210 { 0x1f, 0x0005 },
3211 { 0x05, 0x8330 },
3212 { 0x06, 0x669a },
3213
3214 { 0x1f, 0x0002 }
3215 };
3216 int val;
3217
françois romieu4da19632011-01-03 15:07:55 +00003218 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003219
françois romieu4da19632011-01-03 15:07:55 +00003220 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003221 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003222 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003223 0x0065, 0x0066, 0x0067, 0x0068,
3224 0x0069, 0x006a, 0x006b, 0x006c
3225 };
3226 int i;
3227
françois romieu4da19632011-01-03 15:07:55 +00003228 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003229
3230 val &= 0xff00;
3231 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003232 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003233 }
3234 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003235 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003236 { 0x1f, 0x0002 },
3237 { 0x05, 0x2642 },
3238 { 0x1f, 0x0005 },
3239 { 0x05, 0x8330 },
3240 { 0x06, 0x2642 }
3241 };
3242
françois romieu4da19632011-01-03 15:07:55 +00003243 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003244 }
3245
françois romieubca03d52011-01-03 15:07:31 +00003246 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003247 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003248 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3249 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003250
françois romieubca03d52011-01-03 15:07:31 +00003251 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003252 rtl_writephy(tp, 0x1f, 0x0002);
3253 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003254
françois romieu4da19632011-01-03 15:07:55 +00003255 rtl_writephy(tp, 0x1f, 0x0005);
3256 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003257
3258 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003259
françois romieu4da19632011-01-03 15:07:55 +00003260 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003261}
3262
françois romieu4da19632011-01-03 15:07:55 +00003263static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003264{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003265 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003266 { 0x1f, 0x0002 },
3267 { 0x10, 0x0008 },
3268 { 0x0d, 0x006c },
3269
3270 { 0x1f, 0x0000 },
3271 { 0x0d, 0xf880 },
3272
3273 { 0x1f, 0x0001 },
3274 { 0x17, 0x0cc0 },
3275
3276 { 0x1f, 0x0001 },
3277 { 0x0b, 0xa4d8 },
3278 { 0x09, 0x281c },
3279 { 0x07, 0x2883 },
3280 { 0x0a, 0x6b35 },
3281 { 0x1d, 0x3da4 },
3282 { 0x1c, 0xeffd },
3283 { 0x14, 0x7f52 },
3284 { 0x18, 0x7fc6 },
3285 { 0x08, 0x0601 },
3286 { 0x06, 0x4063 },
3287 { 0x10, 0xf074 },
3288 { 0x1f, 0x0003 },
3289 { 0x13, 0x0789 },
3290 { 0x12, 0xf4bd },
3291 { 0x1a, 0x04fd },
3292 { 0x14, 0x84b0 },
3293 { 0x1f, 0x0000 },
3294 { 0x00, 0x9200 },
3295
3296 { 0x1f, 0x0005 },
3297 { 0x01, 0x0340 },
3298 { 0x1f, 0x0001 },
3299 { 0x04, 0x4000 },
3300 { 0x03, 0x1d21 },
3301 { 0x02, 0x0c32 },
3302 { 0x01, 0x0200 },
3303 { 0x00, 0x5554 },
3304 { 0x04, 0x4800 },
3305 { 0x04, 0x4000 },
3306 { 0x04, 0xf000 },
3307 { 0x03, 0xdf01 },
3308 { 0x02, 0xdf20 },
3309 { 0x01, 0x101a },
3310 { 0x00, 0xa0ff },
3311 { 0x04, 0xf800 },
3312 { 0x04, 0xf000 },
3313 { 0x1f, 0x0000 },
3314
3315 { 0x1f, 0x0007 },
3316 { 0x1e, 0x0023 },
3317 { 0x16, 0x0000 },
3318 { 0x1f, 0x0000 }
3319 };
3320
françois romieu4da19632011-01-03 15:07:55 +00003321 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003322}
3323
françois romieue6de30d2011-01-03 15:08:37 +00003324static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3325{
3326 static const struct phy_reg phy_reg_init[] = {
3327 { 0x1f, 0x0001 },
3328 { 0x17, 0x0cc0 },
3329
3330 { 0x1f, 0x0007 },
3331 { 0x1e, 0x002d },
3332 { 0x18, 0x0040 },
3333 { 0x1f, 0x0000 }
3334 };
3335
3336 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3337 rtl_patchphy(tp, 0x0d, 1 << 5);
3338}
3339
Hayes Wang70090422011-07-06 15:58:06 +08003340static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003341{
3342 static const struct phy_reg phy_reg_init[] = {
3343 /* Enable Delay cap */
3344 { 0x1f, 0x0005 },
3345 { 0x05, 0x8b80 },
3346 { 0x06, 0xc896 },
3347 { 0x1f, 0x0000 },
3348
3349 /* Channel estimation fine tune */
3350 { 0x1f, 0x0001 },
3351 { 0x0b, 0x6c20 },
3352 { 0x07, 0x2872 },
3353 { 0x1c, 0xefff },
3354 { 0x1f, 0x0003 },
3355 { 0x14, 0x6420 },
3356 { 0x1f, 0x0000 },
3357
3358 /* Update PFM & 10M TX idle timer */
3359 { 0x1f, 0x0007 },
3360 { 0x1e, 0x002f },
3361 { 0x15, 0x1919 },
3362 { 0x1f, 0x0000 },
3363
3364 { 0x1f, 0x0007 },
3365 { 0x1e, 0x00ac },
3366 { 0x18, 0x0006 },
3367 { 0x1f, 0x0000 }
3368 };
3369
Francois Romieu15ecd032011-04-27 13:52:22 -07003370 rtl_apply_firmware(tp);
3371
hayeswang01dc7fe2011-03-21 01:50:28 +00003372 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3373
3374 /* DCO enable for 10M IDLE Power */
3375 rtl_writephy(tp, 0x1f, 0x0007);
3376 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003377 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003378 rtl_writephy(tp, 0x1f, 0x0000);
3379
3380 /* For impedance matching */
3381 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003382 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003383 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003384
3385 /* PHY auto speed down */
3386 rtl_writephy(tp, 0x1f, 0x0007);
3387 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003388 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003389 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003390 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003391
3392 rtl_writephy(tp, 0x1f, 0x0005);
3393 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003394 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003395 rtl_writephy(tp, 0x1f, 0x0000);
3396
3397 rtl_writephy(tp, 0x1f, 0x0005);
3398 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003399 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003400 rtl_writephy(tp, 0x1f, 0x0007);
3401 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003402 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003403 rtl_writephy(tp, 0x1f, 0x0006);
3404 rtl_writephy(tp, 0x00, 0x5a00);
3405 rtl_writephy(tp, 0x1f, 0x0000);
3406 rtl_writephy(tp, 0x0d, 0x0007);
3407 rtl_writephy(tp, 0x0e, 0x003c);
3408 rtl_writephy(tp, 0x0d, 0x4007);
3409 rtl_writephy(tp, 0x0e, 0x0000);
3410 rtl_writephy(tp, 0x0d, 0x0000);
3411}
3412
françois romieu9ecb9aa2012-12-07 11:20:21 +00003413static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3414{
3415 const u16 w[] = {
3416 addr[0] | (addr[1] << 8),
3417 addr[2] | (addr[3] << 8),
3418 addr[4] | (addr[5] << 8)
3419 };
3420 const struct exgmac_reg e[] = {
3421 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3422 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3423 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3424 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3425 };
3426
3427 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3428}
3429
Hayes Wang70090422011-07-06 15:58:06 +08003430static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3431{
3432 static const struct phy_reg phy_reg_init[] = {
3433 /* Enable Delay cap */
3434 { 0x1f, 0x0004 },
3435 { 0x1f, 0x0007 },
3436 { 0x1e, 0x00ac },
3437 { 0x18, 0x0006 },
3438 { 0x1f, 0x0002 },
3439 { 0x1f, 0x0000 },
3440 { 0x1f, 0x0000 },
3441
3442 /* Channel estimation fine tune */
3443 { 0x1f, 0x0003 },
3444 { 0x09, 0xa20f },
3445 { 0x1f, 0x0000 },
3446 { 0x1f, 0x0000 },
3447
3448 /* Green Setting */
3449 { 0x1f, 0x0005 },
3450 { 0x05, 0x8b5b },
3451 { 0x06, 0x9222 },
3452 { 0x05, 0x8b6d },
3453 { 0x06, 0x8000 },
3454 { 0x05, 0x8b76 },
3455 { 0x06, 0x8000 },
3456 { 0x1f, 0x0000 }
3457 };
3458
3459 rtl_apply_firmware(tp);
3460
3461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3462
3463 /* For 4-corner performance improve */
3464 rtl_writephy(tp, 0x1f, 0x0005);
3465 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003466 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003467 rtl_writephy(tp, 0x1f, 0x0000);
3468
3469 /* PHY auto speed down */
3470 rtl_writephy(tp, 0x1f, 0x0004);
3471 rtl_writephy(tp, 0x1f, 0x0007);
3472 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003473 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003474 rtl_writephy(tp, 0x1f, 0x0002);
3475 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003476 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003477
3478 /* improve 10M EEE waveform */
3479 rtl_writephy(tp, 0x1f, 0x0005);
3480 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003481 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003482 rtl_writephy(tp, 0x1f, 0x0000);
3483
3484 /* Improve 2-pair detection performance */
3485 rtl_writephy(tp, 0x1f, 0x0005);
3486 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003487 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003488 rtl_writephy(tp, 0x1f, 0x0000);
3489
3490 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003491 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003492 rtl_writephy(tp, 0x1f, 0x0005);
3493 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003494 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003495 rtl_writephy(tp, 0x1f, 0x0004);
3496 rtl_writephy(tp, 0x1f, 0x0007);
3497 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003498 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003499 rtl_writephy(tp, 0x1f, 0x0002);
3500 rtl_writephy(tp, 0x1f, 0x0000);
3501 rtl_writephy(tp, 0x0d, 0x0007);
3502 rtl_writephy(tp, 0x0e, 0x003c);
3503 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003504 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003505 rtl_writephy(tp, 0x0d, 0x0000);
3506
3507 /* Green feature */
3508 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003509 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3510 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003511 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003512 rtl_writephy(tp, 0x1f, 0x0005);
3513 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3514 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003515
françois romieu9ecb9aa2012-12-07 11:20:21 +00003516 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3517 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003518}
3519
Hayes Wang5f886e02012-03-30 14:33:03 +08003520static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3521{
3522 /* For 4-corner performance improve */
3523 rtl_writephy(tp, 0x1f, 0x0005);
3524 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003525 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003526 rtl_writephy(tp, 0x1f, 0x0000);
3527
3528 /* PHY auto speed down */
3529 rtl_writephy(tp, 0x1f, 0x0007);
3530 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003531 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003532 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003533 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003534
3535 /* Improve 10M EEE waveform */
3536 rtl_writephy(tp, 0x1f, 0x0005);
3537 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003538 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003539 rtl_writephy(tp, 0x1f, 0x0000);
3540}
3541
Hayes Wangc2218922011-09-06 16:55:18 +08003542static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3543{
3544 static const struct phy_reg phy_reg_init[] = {
3545 /* Channel estimation fine tune */
3546 { 0x1f, 0x0003 },
3547 { 0x09, 0xa20f },
3548 { 0x1f, 0x0000 },
3549
3550 /* Modify green table for giga & fnet */
3551 { 0x1f, 0x0005 },
3552 { 0x05, 0x8b55 },
3553 { 0x06, 0x0000 },
3554 { 0x05, 0x8b5e },
3555 { 0x06, 0x0000 },
3556 { 0x05, 0x8b67 },
3557 { 0x06, 0x0000 },
3558 { 0x05, 0x8b70 },
3559 { 0x06, 0x0000 },
3560 { 0x1f, 0x0000 },
3561 { 0x1f, 0x0007 },
3562 { 0x1e, 0x0078 },
3563 { 0x17, 0x0000 },
3564 { 0x19, 0x00fb },
3565 { 0x1f, 0x0000 },
3566
3567 /* Modify green table for 10M */
3568 { 0x1f, 0x0005 },
3569 { 0x05, 0x8b79 },
3570 { 0x06, 0xaa00 },
3571 { 0x1f, 0x0000 },
3572
3573 /* Disable hiimpedance detection (RTCT) */
3574 { 0x1f, 0x0003 },
3575 { 0x01, 0x328a },
3576 { 0x1f, 0x0000 }
3577 };
3578
3579 rtl_apply_firmware(tp);
3580
3581 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3582
Hayes Wang5f886e02012-03-30 14:33:03 +08003583 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003584
3585 /* Improve 2-pair detection performance */
3586 rtl_writephy(tp, 0x1f, 0x0005);
3587 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003588 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003589 rtl_writephy(tp, 0x1f, 0x0000);
3590}
3591
3592static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3593{
3594 rtl_apply_firmware(tp);
3595
Hayes Wang5f886e02012-03-30 14:33:03 +08003596 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003597}
3598
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003599static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3600{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003601 static const struct phy_reg phy_reg_init[] = {
3602 /* Channel estimation fine tune */
3603 { 0x1f, 0x0003 },
3604 { 0x09, 0xa20f },
3605 { 0x1f, 0x0000 },
3606
3607 /* Modify green table for giga & fnet */
3608 { 0x1f, 0x0005 },
3609 { 0x05, 0x8b55 },
3610 { 0x06, 0x0000 },
3611 { 0x05, 0x8b5e },
3612 { 0x06, 0x0000 },
3613 { 0x05, 0x8b67 },
3614 { 0x06, 0x0000 },
3615 { 0x05, 0x8b70 },
3616 { 0x06, 0x0000 },
3617 { 0x1f, 0x0000 },
3618 { 0x1f, 0x0007 },
3619 { 0x1e, 0x0078 },
3620 { 0x17, 0x0000 },
3621 { 0x19, 0x00aa },
3622 { 0x1f, 0x0000 },
3623
3624 /* Modify green table for 10M */
3625 { 0x1f, 0x0005 },
3626 { 0x05, 0x8b79 },
3627 { 0x06, 0xaa00 },
3628 { 0x1f, 0x0000 },
3629
3630 /* Disable hiimpedance detection (RTCT) */
3631 { 0x1f, 0x0003 },
3632 { 0x01, 0x328a },
3633 { 0x1f, 0x0000 }
3634 };
3635
3636
3637 rtl_apply_firmware(tp);
3638
3639 rtl8168f_hw_phy_config(tp);
3640
3641 /* Improve 2-pair detection performance */
3642 rtl_writephy(tp, 0x1f, 0x0005);
3643 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003644 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003645 rtl_writephy(tp, 0x1f, 0x0000);
3646
3647 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3648
3649 /* Modify green table for giga */
3650 rtl_writephy(tp, 0x1f, 0x0005);
3651 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003652 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003653 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003654 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003655 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003656 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003657 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003658 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003659 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003660 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003661 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003662 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003663 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003664 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003665 rtl_writephy(tp, 0x1f, 0x0000);
3666
3667 /* uc same-seed solution */
3668 rtl_writephy(tp, 0x1f, 0x0005);
3669 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003670 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003671 rtl_writephy(tp, 0x1f, 0x0000);
3672
3673 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003674 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003675 rtl_writephy(tp, 0x1f, 0x0005);
3676 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003678 rtl_writephy(tp, 0x1f, 0x0004);
3679 rtl_writephy(tp, 0x1f, 0x0007);
3680 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003681 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003682 rtl_writephy(tp, 0x1f, 0x0000);
3683 rtl_writephy(tp, 0x0d, 0x0007);
3684 rtl_writephy(tp, 0x0e, 0x003c);
3685 rtl_writephy(tp, 0x0d, 0x4007);
3686 rtl_writephy(tp, 0x0e, 0x0000);
3687 rtl_writephy(tp, 0x0d, 0x0000);
3688
3689 /* Green feature */
3690 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3692 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003693 rtl_writephy(tp, 0x1f, 0x0000);
3694}
3695
Hayes Wangc5583862012-07-02 17:23:22 +08003696static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3697{
Hayes Wangc5583862012-07-02 17:23:22 +08003698 rtl_apply_firmware(tp);
3699
hayeswang41f44d12013-04-01 22:23:36 +00003700 rtl_writephy(tp, 0x1f, 0x0a46);
3701 if (rtl_readphy(tp, 0x10) & 0x0100) {
3702 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003703 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003704 } else {
3705 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003706 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003707 }
Hayes Wangc5583862012-07-02 17:23:22 +08003708
hayeswang41f44d12013-04-01 22:23:36 +00003709 rtl_writephy(tp, 0x1f, 0x0a46);
3710 if (rtl_readphy(tp, 0x13) & 0x0100) {
3711 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003712 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003713 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003714 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003715 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003716 }
Hayes Wangc5583862012-07-02 17:23:22 +08003717
hayeswang41f44d12013-04-01 22:23:36 +00003718 /* Enable PHY auto speed down */
3719 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003720 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003721
hayeswangfe7524c2013-04-01 22:23:37 +00003722 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003723 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003724 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003725 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003726 rtl_writephy(tp, 0x1f, 0x0a43);
3727 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003728 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3729 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003730
hayeswang41f44d12013-04-01 22:23:36 +00003731 /* EEE auto-fallback function */
3732 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003733 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003734
hayeswang41f44d12013-04-01 22:23:36 +00003735 /* Enable UC LPF tune function */
3736 rtl_writephy(tp, 0x1f, 0x0a43);
3737 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003738 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003739
3740 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003741 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003742
hayeswangfe7524c2013-04-01 22:23:37 +00003743 /* Improve SWR Efficiency */
3744 rtl_writephy(tp, 0x1f, 0x0bcd);
3745 rtl_writephy(tp, 0x14, 0x5065);
3746 rtl_writephy(tp, 0x14, 0xd065);
3747 rtl_writephy(tp, 0x1f, 0x0bc8);
3748 rtl_writephy(tp, 0x11, 0x5655);
3749 rtl_writephy(tp, 0x1f, 0x0bcd);
3750 rtl_writephy(tp, 0x14, 0x1065);
3751 rtl_writephy(tp, 0x14, 0x9065);
3752 rtl_writephy(tp, 0x14, 0x1065);
3753
David Chang1bac1072013-11-27 15:48:36 +08003754 /* Check ALDPS bit, disable it if enabled */
3755 rtl_writephy(tp, 0x1f, 0x0a43);
3756 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003757 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003758
hayeswang41f44d12013-04-01 22:23:36 +00003759 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003760}
3761
hayeswang57538c42013-04-01 22:23:40 +00003762static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3763{
3764 rtl_apply_firmware(tp);
3765}
3766
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003767static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3768{
3769 u16 dout_tapbin;
3770 u32 data;
3771
3772 rtl_apply_firmware(tp);
3773
3774 /* CHN EST parameters adjust - giga master */
3775 rtl_writephy(tp, 0x1f, 0x0a43);
3776 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003777 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003778 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003779 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003780 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003781 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003782 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003783 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003784 rtl_writephy(tp, 0x1f, 0x0000);
3785
3786 /* CHN EST parameters adjust - giga slave */
3787 rtl_writephy(tp, 0x1f, 0x0a43);
3788 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003789 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003790 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003791 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003792 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003793 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003794 rtl_writephy(tp, 0x1f, 0x0000);
3795
3796 /* CHN EST parameters adjust - fnet */
3797 rtl_writephy(tp, 0x1f, 0x0a43);
3798 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003799 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003800 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003801 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003802 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003803 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003804 rtl_writephy(tp, 0x1f, 0x0000);
3805
3806 /* enable R-tune & PGA-retune function */
3807 dout_tapbin = 0;
3808 rtl_writephy(tp, 0x1f, 0x0a46);
3809 data = rtl_readphy(tp, 0x13);
3810 data &= 3;
3811 data <<= 2;
3812 dout_tapbin |= data;
3813 data = rtl_readphy(tp, 0x12);
3814 data &= 0xc000;
3815 data >>= 14;
3816 dout_tapbin |= data;
3817 dout_tapbin = ~(dout_tapbin^0x08);
3818 dout_tapbin <<= 12;
3819 dout_tapbin &= 0xf000;
3820 rtl_writephy(tp, 0x1f, 0x0a43);
3821 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003822 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003823 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003824 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003825 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003826 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003827 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003828 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003829
3830 rtl_writephy(tp, 0x1f, 0x0a43);
3831 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003832 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003833 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003834 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003835 rtl_writephy(tp, 0x1f, 0x0000);
3836
3837 /* enable GPHY 10M */
3838 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003839 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003840 rtl_writephy(tp, 0x1f, 0x0000);
3841
3842 /* SAR ADC performance */
3843 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003844 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003845 rtl_writephy(tp, 0x1f, 0x0000);
3846
3847 rtl_writephy(tp, 0x1f, 0x0a43);
3848 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003849 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003850 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003851 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003852 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003853 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003854 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003855 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003856 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003857 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003858 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003859 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003860 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003861 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003862 rtl_writephy(tp, 0x1f, 0x0000);
3863
3864 /* disable phy pfm mode */
3865 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003866 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003867 rtl_writephy(tp, 0x1f, 0x0000);
3868
3869 /* Check ALDPS bit, disable it if enabled */
3870 rtl_writephy(tp, 0x1f, 0x0a43);
3871 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003872 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003873
3874 rtl_writephy(tp, 0x1f, 0x0000);
3875}
3876
3877static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3878{
3879 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3880 u16 rlen;
3881 u32 data;
3882
3883 rtl_apply_firmware(tp);
3884
3885 /* CHIN EST parameter update */
3886 rtl_writephy(tp, 0x1f, 0x0a43);
3887 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003888 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003889 rtl_writephy(tp, 0x1f, 0x0000);
3890
3891 /* enable R-tune & PGA-retune function */
3892 rtl_writephy(tp, 0x1f, 0x0a43);
3893 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003894 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003895 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003896 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003897 rtl_writephy(tp, 0x1f, 0x0000);
3898
3899 /* enable GPHY 10M */
3900 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003901 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003902 rtl_writephy(tp, 0x1f, 0x0000);
3903
3904 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3905 data = r8168_mac_ocp_read(tp, 0xdd02);
3906 ioffset_p3 = ((data & 0x80)>>7);
3907 ioffset_p3 <<= 3;
3908
3909 data = r8168_mac_ocp_read(tp, 0xdd00);
3910 ioffset_p3 |= ((data & (0xe000))>>13);
3911 ioffset_p2 = ((data & (0x1e00))>>9);
3912 ioffset_p1 = ((data & (0x01e0))>>5);
3913 ioffset_p0 = ((data & 0x0010)>>4);
3914 ioffset_p0 <<= 3;
3915 ioffset_p0 |= (data & (0x07));
3916 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3917
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003918 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003919 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003920 rtl_writephy(tp, 0x1f, 0x0bcf);
3921 rtl_writephy(tp, 0x16, data);
3922 rtl_writephy(tp, 0x1f, 0x0000);
3923 }
3924
3925 /* Modify rlen (TX LPF corner frequency) level */
3926 rtl_writephy(tp, 0x1f, 0x0bcd);
3927 data = rtl_readphy(tp, 0x16);
3928 data &= 0x000f;
3929 rlen = 0;
3930 if (data > 3)
3931 rlen = data - 3;
3932 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3933 rtl_writephy(tp, 0x17, data);
3934 rtl_writephy(tp, 0x1f, 0x0bcd);
3935 rtl_writephy(tp, 0x1f, 0x0000);
3936
3937 /* disable phy pfm mode */
3938 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003939 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003940 rtl_writephy(tp, 0x1f, 0x0000);
3941
3942 /* Check ALDPS bit, disable it if enabled */
3943 rtl_writephy(tp, 0x1f, 0x0a43);
3944 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003946
3947 rtl_writephy(tp, 0x1f, 0x0000);
3948}
3949
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003950static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3951{
3952 /* Enable PHY auto speed down */
3953 rtl_writephy(tp, 0x1f, 0x0a44);
3954 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3955 rtl_writephy(tp, 0x1f, 0x0000);
3956
3957 /* patch 10M & ALDPS */
3958 rtl_writephy(tp, 0x1f, 0x0bcc);
3959 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3960 rtl_writephy(tp, 0x1f, 0x0a44);
3961 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3962 rtl_writephy(tp, 0x1f, 0x0a43);
3963 rtl_writephy(tp, 0x13, 0x8084);
3964 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3965 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3966 rtl_writephy(tp, 0x1f, 0x0000);
3967
3968 /* Enable EEE auto-fallback function */
3969 rtl_writephy(tp, 0x1f, 0x0a4b);
3970 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3971 rtl_writephy(tp, 0x1f, 0x0000);
3972
3973 /* Enable UC LPF tune function */
3974 rtl_writephy(tp, 0x1f, 0x0a43);
3975 rtl_writephy(tp, 0x13, 0x8012);
3976 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3977 rtl_writephy(tp, 0x1f, 0x0000);
3978
3979 /* set rg_sel_sdm_rate */
3980 rtl_writephy(tp, 0x1f, 0x0c42);
3981 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3982 rtl_writephy(tp, 0x1f, 0x0000);
3983
3984 /* Check ALDPS bit, disable it if enabled */
3985 rtl_writephy(tp, 0x1f, 0x0a43);
3986 if (rtl_readphy(tp, 0x10) & 0x0004)
3987 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3988
3989 rtl_writephy(tp, 0x1f, 0x0000);
3990}
3991
3992static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3993{
3994 /* patch 10M & ALDPS */
3995 rtl_writephy(tp, 0x1f, 0x0bcc);
3996 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3997 rtl_writephy(tp, 0x1f, 0x0a44);
3998 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3999 rtl_writephy(tp, 0x1f, 0x0a43);
4000 rtl_writephy(tp, 0x13, 0x8084);
4001 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4002 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4003 rtl_writephy(tp, 0x1f, 0x0000);
4004
4005 /* Enable UC LPF tune function */
4006 rtl_writephy(tp, 0x1f, 0x0a43);
4007 rtl_writephy(tp, 0x13, 0x8012);
4008 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4009 rtl_writephy(tp, 0x1f, 0x0000);
4010
4011 /* Set rg_sel_sdm_rate */
4012 rtl_writephy(tp, 0x1f, 0x0c42);
4013 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4014 rtl_writephy(tp, 0x1f, 0x0000);
4015
4016 /* Channel estimation parameters */
4017 rtl_writephy(tp, 0x1f, 0x0a43);
4018 rtl_writephy(tp, 0x13, 0x80f3);
4019 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4020 rtl_writephy(tp, 0x13, 0x80f0);
4021 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4022 rtl_writephy(tp, 0x13, 0x80ef);
4023 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4024 rtl_writephy(tp, 0x13, 0x80f6);
4025 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4026 rtl_writephy(tp, 0x13, 0x80ec);
4027 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4028 rtl_writephy(tp, 0x13, 0x80ed);
4029 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4030 rtl_writephy(tp, 0x13, 0x80f2);
4031 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4032 rtl_writephy(tp, 0x13, 0x80f4);
4033 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4034 rtl_writephy(tp, 0x1f, 0x0a43);
4035 rtl_writephy(tp, 0x13, 0x8110);
4036 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4037 rtl_writephy(tp, 0x13, 0x810f);
4038 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4039 rtl_writephy(tp, 0x13, 0x8111);
4040 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4041 rtl_writephy(tp, 0x13, 0x8113);
4042 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4043 rtl_writephy(tp, 0x13, 0x8115);
4044 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4045 rtl_writephy(tp, 0x13, 0x810e);
4046 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4047 rtl_writephy(tp, 0x13, 0x810c);
4048 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4049 rtl_writephy(tp, 0x13, 0x810b);
4050 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4051 rtl_writephy(tp, 0x1f, 0x0a43);
4052 rtl_writephy(tp, 0x13, 0x80d1);
4053 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4054 rtl_writephy(tp, 0x13, 0x80cd);
4055 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4056 rtl_writephy(tp, 0x13, 0x80d3);
4057 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4058 rtl_writephy(tp, 0x13, 0x80d5);
4059 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4060 rtl_writephy(tp, 0x13, 0x80d7);
4061 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4062
4063 /* Force PWM-mode */
4064 rtl_writephy(tp, 0x1f, 0x0bcd);
4065 rtl_writephy(tp, 0x14, 0x5065);
4066 rtl_writephy(tp, 0x14, 0xd065);
4067 rtl_writephy(tp, 0x1f, 0x0bc8);
4068 rtl_writephy(tp, 0x12, 0x00ed);
4069 rtl_writephy(tp, 0x1f, 0x0bcd);
4070 rtl_writephy(tp, 0x14, 0x1065);
4071 rtl_writephy(tp, 0x14, 0x9065);
4072 rtl_writephy(tp, 0x14, 0x1065);
4073 rtl_writephy(tp, 0x1f, 0x0000);
4074
4075 /* Check ALDPS bit, disable it if enabled */
4076 rtl_writephy(tp, 0x1f, 0x0a43);
4077 if (rtl_readphy(tp, 0x10) & 0x0004)
4078 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4079
4080 rtl_writephy(tp, 0x1f, 0x0000);
4081}
4082
françois romieu4da19632011-01-03 15:07:55 +00004083static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004084{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004085 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004086 { 0x1f, 0x0003 },
4087 { 0x08, 0x441d },
4088 { 0x01, 0x9100 },
4089 { 0x1f, 0x0000 }
4090 };
4091
françois romieu4da19632011-01-03 15:07:55 +00004092 rtl_writephy(tp, 0x1f, 0x0000);
4093 rtl_patchphy(tp, 0x11, 1 << 12);
4094 rtl_patchphy(tp, 0x19, 1 << 13);
4095 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004096
françois romieu4da19632011-01-03 15:07:55 +00004097 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004098}
4099
Hayes Wang5a5e4442011-02-22 17:26:21 +08004100static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4101{
4102 static const struct phy_reg phy_reg_init[] = {
4103 { 0x1f, 0x0005 },
4104 { 0x1a, 0x0000 },
4105 { 0x1f, 0x0000 },
4106
4107 { 0x1f, 0x0004 },
4108 { 0x1c, 0x0000 },
4109 { 0x1f, 0x0000 },
4110
4111 { 0x1f, 0x0001 },
4112 { 0x15, 0x7701 },
4113 { 0x1f, 0x0000 }
4114 };
4115
4116 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004117 rtl_writephy(tp, 0x1f, 0x0000);
4118 rtl_writephy(tp, 0x18, 0x0310);
4119 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004120
François Romieu953a12c2011-04-24 17:38:48 +02004121 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004122
4123 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4124}
4125
Hayes Wang7e18dca2012-03-30 14:33:02 +08004126static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4127{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004128 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004129 rtl_writephy(tp, 0x1f, 0x0000);
4130 rtl_writephy(tp, 0x18, 0x0310);
4131 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004132
4133 rtl_apply_firmware(tp);
4134
4135 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004136 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004137 rtl_writephy(tp, 0x1f, 0x0004);
4138 rtl_writephy(tp, 0x10, 0x401f);
4139 rtl_writephy(tp, 0x19, 0x7030);
4140 rtl_writephy(tp, 0x1f, 0x0000);
4141}
4142
Hayes Wang5598bfe2012-07-02 17:23:21 +08004143static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4144{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004145 static const struct phy_reg phy_reg_init[] = {
4146 { 0x1f, 0x0004 },
4147 { 0x10, 0xc07f },
4148 { 0x19, 0x7030 },
4149 { 0x1f, 0x0000 }
4150 };
4151
4152 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004153 rtl_writephy(tp, 0x1f, 0x0000);
4154 rtl_writephy(tp, 0x18, 0x0310);
4155 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004156
4157 rtl_apply_firmware(tp);
4158
Francois Romieufdf6fc02012-07-06 22:40:38 +02004159 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004160 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4161
Francois Romieufdf6fc02012-07-06 22:40:38 +02004162 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004163}
4164
Francois Romieu5615d9f2007-08-17 17:50:46 +02004165static void rtl_hw_phy_config(struct net_device *dev)
4166{
4167 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004168
4169 rtl8169_print_mac_version(tp);
4170
4171 switch (tp->mac_version) {
4172 case RTL_GIGA_MAC_VER_01:
4173 break;
4174 case RTL_GIGA_MAC_VER_02:
4175 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004176 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004177 break;
4178 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004179 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004180 break;
françois romieu2e9558562009-08-10 19:44:19 +00004181 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004182 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004183 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004184 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004185 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004186 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004187 case RTL_GIGA_MAC_VER_07:
4188 case RTL_GIGA_MAC_VER_08:
4189 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004190 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004191 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004192 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004193 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004194 break;
4195 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004196 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004197 break;
4198 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004199 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004200 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004201 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004202 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004203 break;
4204 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004205 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004206 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004207 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004208 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004209 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004210 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004211 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004212 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004213 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004214 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004215 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004216 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004217 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004218 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004219 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004220 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004221 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004222 break;
4223 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004224 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004225 break;
4226 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004227 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004228 break;
françois romieue6de30d2011-01-03 15:08:37 +00004229 case RTL_GIGA_MAC_VER_28:
4230 rtl8168d_4_hw_phy_config(tp);
4231 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004232 case RTL_GIGA_MAC_VER_29:
4233 case RTL_GIGA_MAC_VER_30:
4234 rtl8105e_hw_phy_config(tp);
4235 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004236 case RTL_GIGA_MAC_VER_31:
4237 /* None. */
4238 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004239 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004240 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004241 rtl8168e_1_hw_phy_config(tp);
4242 break;
4243 case RTL_GIGA_MAC_VER_34:
4244 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004245 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004246 case RTL_GIGA_MAC_VER_35:
4247 rtl8168f_1_hw_phy_config(tp);
4248 break;
4249 case RTL_GIGA_MAC_VER_36:
4250 rtl8168f_2_hw_phy_config(tp);
4251 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004252
Hayes Wang7e18dca2012-03-30 14:33:02 +08004253 case RTL_GIGA_MAC_VER_37:
4254 rtl8402_hw_phy_config(tp);
4255 break;
4256
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004257 case RTL_GIGA_MAC_VER_38:
4258 rtl8411_hw_phy_config(tp);
4259 break;
4260
Hayes Wang5598bfe2012-07-02 17:23:21 +08004261 case RTL_GIGA_MAC_VER_39:
4262 rtl8106e_hw_phy_config(tp);
4263 break;
4264
Hayes Wangc5583862012-07-02 17:23:22 +08004265 case RTL_GIGA_MAC_VER_40:
4266 rtl8168g_1_hw_phy_config(tp);
4267 break;
hayeswang57538c42013-04-01 22:23:40 +00004268 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004269 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004270 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004271 rtl8168g_2_hw_phy_config(tp);
4272 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004273 case RTL_GIGA_MAC_VER_45:
4274 case RTL_GIGA_MAC_VER_47:
4275 rtl8168h_1_hw_phy_config(tp);
4276 break;
4277 case RTL_GIGA_MAC_VER_46:
4278 case RTL_GIGA_MAC_VER_48:
4279 rtl8168h_2_hw_phy_config(tp);
4280 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004281
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004282 case RTL_GIGA_MAC_VER_49:
4283 rtl8168ep_1_hw_phy_config(tp);
4284 break;
4285 case RTL_GIGA_MAC_VER_50:
4286 case RTL_GIGA_MAC_VER_51:
4287 rtl8168ep_2_hw_phy_config(tp);
4288 break;
4289
Hayes Wangc5583862012-07-02 17:23:22 +08004290 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004291 default:
4292 break;
4293 }
4294}
4295
Francois Romieuda78dbf2012-01-26 14:18:23 +01004296static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4300
Heiner Kallweite3972862018-06-29 08:07:04 +02004301 if (rtl8169_xmii_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004302 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 * A busy loop could burn quite a few cycles on nowadays CPU.
4304 * Let's delay the execution of the timer for a few ticks.
4305 */
4306 timeout = HZ/10;
4307 goto out_mod_timer;
4308 }
4309
Heiner Kallweite3972862018-06-29 08:07:04 +02004310 if (rtl8169_xmii_link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004311 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004313 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Heiner Kallweite3972862018-06-29 08:07:04 +02004315 rtl8169_xmii_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317out_mod_timer:
4318 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004319}
4320
4321static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4322{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004323 if (!test_and_set_bit(flag, tp->wk.flags))
4324 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004325}
4326
Kees Cook9de36cc2017-10-25 03:53:12 -07004327static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004328{
Kees Cook9de36cc2017-10-25 03:53:12 -07004329 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004330
Francois Romieu98ddf982012-01-31 10:47:34 +01004331 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
4333
Francois Romieuffc46952012-07-06 14:19:23 +02004334DECLARE_RTL_COND(rtl_phy_reset_cond)
4335{
Heiner Kallweite3972862018-06-29 08:07:04 +02004336 return rtl8169_xmii_reset_pending(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004337}
4338
Francois Romieubf793292006-11-01 00:53:05 +01004339static void rtl8169_phy_reset(struct net_device *dev,
4340 struct rtl8169_private *tp)
4341{
Heiner Kallweite3972862018-06-29 08:07:04 +02004342 rtl8169_xmii_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004343 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004344}
4345
David S. Miller8decf862011-09-22 03:23:13 -04004346static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4347{
David S. Miller8decf862011-09-22 03:23:13 -04004348 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02004349 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004350}
4351
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004352static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004354 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004355
Marcus Sundberg773328942008-07-10 21:28:08 +02004356 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004357 netif_dbg(tp, drv, dev,
4358 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004359 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004360 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004361
Francois Romieu6dccd162007-02-13 23:38:05 +01004362 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4363
4364 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4365 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004366
Francois Romieubcf0bf92006-07-26 23:14:13 +02004367 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004368 netif_dbg(tp, drv, dev,
4369 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004370 RTL_W8(tp, 0x82, 0x01);
Heiner Kallweit49d17512018-06-28 20:36:15 +02004371 netif_dbg(tp, drv, dev,
4372 "Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004373 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004374 }
4375
Francois Romieubf793292006-11-01 00:53:05 +01004376 rtl8169_phy_reset(dev, tp);
4377
Oliver Neukum54405cd2011-01-06 21:55:13 +01004378 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004379 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4380 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4381 (tp->mii.supports_gmii ?
4382 ADVERTISED_1000baseT_Half |
4383 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004384}
4385
Francois Romieu773d2022007-01-31 23:47:43 +01004386static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4387{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004388 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004389
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004390 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004391
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004392 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4393 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004394
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004395 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4396 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004397
françois romieu9ecb9aa2012-12-07 11:20:21 +00004398 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4399 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004400
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004401 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004402
Francois Romieuda78dbf2012-01-26 14:18:23 +01004403 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004404}
4405
4406static int rtl_set_mac_address(struct net_device *dev, void *p)
4407{
4408 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004409 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004410 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004411
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004412 ret = eth_mac_addr(dev, p);
4413 if (ret)
4414 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004415
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004416 pm_runtime_get_noresume(d);
4417
4418 if (pm_runtime_active(d))
4419 rtl_rar_set(tp, dev->dev_addr);
4420
4421 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004422
4423 return 0;
4424}
4425
Francois Romieucecb5fd2011-04-01 10:21:07 +02004426static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4427 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004428{
Francois Romieu5f787a12006-08-17 13:02:36 +02004429 switch (cmd) {
4430 case SIOCGMIIPHY:
4431 data->phy_id = 32; /* Internal PHY */
4432 return 0;
4433
4434 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004435 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004436 return 0;
4437
4438 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004439 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004440 return 0;
4441 }
4442 return -EOPNOTSUPP;
4443}
4444
Heiner Kallweite3972862018-06-29 08:07:04 +02004445static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004446{
Heiner Kallweite3972862018-06-29 08:07:04 +02004447 struct rtl8169_private *tp = netdev_priv(dev);
4448 struct mii_ioctl_data *data = if_mii(ifr);
4449
4450 return netif_running(dev) ? rtl_xmii_ioctl(tp, data, cmd) : -ENODEV;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004451}
4452
Bill Pembertonbaf63292012-12-03 09:23:28 -05004453static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004454{
4455 struct mdio_ops *ops = &tp->mdio_ops;
4456
4457 switch (tp->mac_version) {
4458 case RTL_GIGA_MAC_VER_27:
4459 ops->write = r8168dp_1_mdio_write;
4460 ops->read = r8168dp_1_mdio_read;
4461 break;
françois romieue6de30d2011-01-03 15:08:37 +00004462 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004463 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004464 ops->write = r8168dp_2_mdio_write;
4465 ops->read = r8168dp_2_mdio_read;
4466 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004467 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004468 ops->write = r8168g_mdio_write;
4469 ops->read = r8168g_mdio_read;
4470 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004471 default:
4472 ops->write = r8169_mdio_write;
4473 ops->read = r8169_mdio_read;
4474 break;
4475 }
4476}
4477
hayeswange2409d82013-03-31 17:02:04 +00004478static void rtl_speed_down(struct rtl8169_private *tp)
4479{
4480 u32 adv;
4481 int lpa;
4482
4483 rtl_writephy(tp, 0x1f, 0x0000);
4484 lpa = rtl_readphy(tp, MII_LPA);
4485
4486 if (lpa & (LPA_10HALF | LPA_10FULL))
4487 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4488 else if (lpa & (LPA_100HALF | LPA_100FULL))
4489 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4490 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4491 else
4492 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4493 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4494 (tp->mii.supports_gmii ?
4495 ADVERTISED_1000baseT_Half |
4496 ADVERTISED_1000baseT_Full : 0);
4497
4498 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4499 adv);
4500}
4501
David S. Miller1805b2f2011-10-24 18:18:09 -04004502static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4503{
David S. Miller1805b2f2011-10-24 18:18:09 -04004504 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004505 case RTL_GIGA_MAC_VER_25:
4506 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004507 case RTL_GIGA_MAC_VER_29:
4508 case RTL_GIGA_MAC_VER_30:
4509 case RTL_GIGA_MAC_VER_32:
4510 case RTL_GIGA_MAC_VER_33:
4511 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004512 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004513 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004514 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4515 break;
4516 default:
4517 break;
4518 }
4519}
4520
4521static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4522{
Heiner Kallweitfe87bef2018-06-24 18:40:23 +02004523 if (!netif_running(tp->dev) || !tp->saved_wolopts)
David S. Miller1805b2f2011-10-24 18:18:09 -04004524 return false;
4525
hayeswange2409d82013-03-31 17:02:04 +00004526 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004527 rtl_wol_suspend_quirk(tp);
4528
4529 return true;
4530}
4531
françois romieu065c27c2011-01-03 15:08:12 +00004532static void r8168_phy_power_up(struct rtl8169_private *tp)
4533{
4534 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004535 switch (tp->mac_version) {
4536 case RTL_GIGA_MAC_VER_11:
4537 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004538 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004539 case RTL_GIGA_MAC_VER_31:
4540 rtl_writephy(tp, 0x0e, 0x0000);
4541 break;
4542 default:
4543 break;
4544 }
françois romieu065c27c2011-01-03 15:08:12 +00004545 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
David S. Millerb2d6cee2018-05-11 20:53:22 -04004546
4547 /* give MAC/PHY some time to resume */
4548 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004549}
4550
4551static void r8168_phy_power_down(struct rtl8169_private *tp)
4552{
4553 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004554 switch (tp->mac_version) {
4555 case RTL_GIGA_MAC_VER_32:
4556 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004557 case RTL_GIGA_MAC_VER_40:
4558 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004559 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4560 break;
4561
4562 case RTL_GIGA_MAC_VER_11:
4563 case RTL_GIGA_MAC_VER_12:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004564 case RTL_GIGA_MAC_VER_17 ... RTL_GIGA_MAC_VER_28:
hayeswang01dc7fe2011-03-21 01:50:28 +00004565 case RTL_GIGA_MAC_VER_31:
4566 rtl_writephy(tp, 0x0e, 0x0200);
4567 default:
4568 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4569 break;
4570 }
françois romieu065c27c2011-01-03 15:08:12 +00004571}
4572
4573static void r8168_pll_power_down(struct rtl8169_private *tp)
4574{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004575 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004576 return;
4577
hayeswang01dc7fe2011-03-21 01:50:28 +00004578 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4579 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004580 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004581
David S. Miller1805b2f2011-10-24 18:18:09 -04004582 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004583 return;
françois romieu065c27c2011-01-03 15:08:12 +00004584
4585 r8168_phy_power_down(tp);
4586
4587 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004588 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004589 case RTL_GIGA_MAC_VER_37:
4590 case RTL_GIGA_MAC_VER_39:
4591 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004592 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004593 case RTL_GIGA_MAC_VER_45:
4594 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004595 case RTL_GIGA_MAC_VER_47:
4596 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004597 case RTL_GIGA_MAC_VER_50:
4598 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004599 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004600 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004601 case RTL_GIGA_MAC_VER_40:
4602 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004603 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004604 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004605 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004606 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004607 break;
françois romieu065c27c2011-01-03 15:08:12 +00004608 }
4609}
4610
4611static void r8168_pll_power_up(struct rtl8169_private *tp)
4612{
françois romieu065c27c2011-01-03 15:08:12 +00004613 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004614 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004615 case RTL_GIGA_MAC_VER_37:
4616 case RTL_GIGA_MAC_VER_39:
4617 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004618 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004619 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004620 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004621 case RTL_GIGA_MAC_VER_45:
4622 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004623 case RTL_GIGA_MAC_VER_47:
4624 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004625 case RTL_GIGA_MAC_VER_50:
4626 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004627 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004628 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004629 case RTL_GIGA_MAC_VER_40:
4630 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004631 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004632 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004633 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004634 0x00000000, ERIAR_EXGMAC);
4635 break;
françois romieu065c27c2011-01-03 15:08:12 +00004636 }
4637
4638 r8168_phy_power_up(tp);
4639}
4640
françois romieu065c27c2011-01-03 15:08:12 +00004641static void rtl_pll_power_down(struct rtl8169_private *tp)
4642{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004643 switch (tp->mac_version) {
4644 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4645 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4646 break;
4647 default:
4648 r8168_pll_power_down(tp);
4649 }
françois romieu065c27c2011-01-03 15:08:12 +00004650}
4651
4652static void rtl_pll_power_up(struct rtl8169_private *tp)
4653{
françois romieu065c27c2011-01-03 15:08:12 +00004654 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004655 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4656 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004657 break;
françois romieu065c27c2011-01-03 15:08:12 +00004658 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004659 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004660 }
4661}
4662
Hayes Wange542a222011-07-06 15:58:04 +08004663static void rtl_init_rxcfg(struct rtl8169_private *tp)
4664{
Hayes Wange542a222011-07-06 15:58:04 +08004665 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004666 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4667 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004668 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004669 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004670 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004671 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004672 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004673 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004674 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004675 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004676 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004677 break;
Hayes Wange542a222011-07-06 15:58:04 +08004678 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004679 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004680 break;
4681 }
4682}
4683
Hayes Wang92fc43b2011-07-06 15:58:03 +08004684static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4685{
Timo Teräs9fba0812013-01-15 21:01:24 +00004686 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004687}
4688
Francois Romieud58d46b2011-05-03 16:38:29 +02004689static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4690{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004691 if (tp->jumbo_ops.enable) {
4692 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4693 tp->jumbo_ops.enable(tp);
4694 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4695 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004696}
4697
4698static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4699{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004700 if (tp->jumbo_ops.disable) {
4701 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4702 tp->jumbo_ops.disable(tp);
4703 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4704 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004705}
4706
4707static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4708{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004709 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4710 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004711 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004712}
4713
4714static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4715{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004716 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4717 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004718 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004719}
4720
4721static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4722{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004723 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004724}
4725
4726static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4727{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004728 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004729}
4730
4731static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4732{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004733 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4734 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4735 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004736 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004737}
4738
4739static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4740{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004741 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4742 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4743 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004744 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004745}
4746
4747static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4748{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004749 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004750 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004751}
4752
4753static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4754{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004755 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004756 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004757}
4758
4759static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4760{
Francois Romieud58d46b2011-05-03 16:38:29 +02004761 r8168b_0_hw_jumbo_enable(tp);
4762
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004763 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004764}
4765
4766static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4767{
Francois Romieud58d46b2011-05-03 16:38:29 +02004768 r8168b_0_hw_jumbo_disable(tp);
4769
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004770 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004771}
4772
Bill Pembertonbaf63292012-12-03 09:23:28 -05004773static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004774{
4775 struct jumbo_ops *ops = &tp->jumbo_ops;
4776
4777 switch (tp->mac_version) {
4778 case RTL_GIGA_MAC_VER_11:
4779 ops->disable = r8168b_0_hw_jumbo_disable;
4780 ops->enable = r8168b_0_hw_jumbo_enable;
4781 break;
4782 case RTL_GIGA_MAC_VER_12:
4783 case RTL_GIGA_MAC_VER_17:
4784 ops->disable = r8168b_1_hw_jumbo_disable;
4785 ops->enable = r8168b_1_hw_jumbo_enable;
4786 break;
4787 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4788 case RTL_GIGA_MAC_VER_19:
4789 case RTL_GIGA_MAC_VER_20:
4790 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4791 case RTL_GIGA_MAC_VER_22:
4792 case RTL_GIGA_MAC_VER_23:
4793 case RTL_GIGA_MAC_VER_24:
4794 case RTL_GIGA_MAC_VER_25:
4795 case RTL_GIGA_MAC_VER_26:
4796 ops->disable = r8168c_hw_jumbo_disable;
4797 ops->enable = r8168c_hw_jumbo_enable;
4798 break;
4799 case RTL_GIGA_MAC_VER_27:
4800 case RTL_GIGA_MAC_VER_28:
4801 ops->disable = r8168dp_hw_jumbo_disable;
4802 ops->enable = r8168dp_hw_jumbo_enable;
4803 break;
4804 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4805 case RTL_GIGA_MAC_VER_32:
4806 case RTL_GIGA_MAC_VER_33:
4807 case RTL_GIGA_MAC_VER_34:
4808 ops->disable = r8168e_hw_jumbo_disable;
4809 ops->enable = r8168e_hw_jumbo_enable;
4810 break;
4811
4812 /*
4813 * No action needed for jumbo frames with 8169.
4814 * No jumbo for 810x at all.
4815 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004816 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004817 default:
4818 ops->disable = NULL;
4819 ops->enable = NULL;
4820 break;
4821 }
4822}
4823
Francois Romieuffc46952012-07-06 14:19:23 +02004824DECLARE_RTL_COND(rtl_chipcmd_cond)
4825{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004826 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004827}
4828
Francois Romieu6f43adc2011-04-29 15:05:51 +02004829static void rtl_hw_reset(struct rtl8169_private *tp)
4830{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004831 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004832
Francois Romieuffc46952012-07-06 14:19:23 +02004833 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004834}
4835
Francois Romieub6ffd972011-06-17 17:00:05 +02004836static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4837{
4838 struct rtl_fw *rtl_fw;
4839 const char *name;
4840 int rc = -ENOMEM;
4841
4842 name = rtl_lookup_firmware_name(tp);
4843 if (!name)
4844 goto out_no_firmware;
4845
4846 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4847 if (!rtl_fw)
4848 goto err_warn;
4849
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004850 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004851 if (rc < 0)
4852 goto err_free;
4853
Francois Romieufd112f22011-06-18 00:10:29 +02004854 rc = rtl_check_firmware(tp, rtl_fw);
4855 if (rc < 0)
4856 goto err_release_firmware;
4857
Francois Romieub6ffd972011-06-17 17:00:05 +02004858 tp->rtl_fw = rtl_fw;
4859out:
4860 return;
4861
Francois Romieufd112f22011-06-18 00:10:29 +02004862err_release_firmware:
4863 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004864err_free:
4865 kfree(rtl_fw);
4866err_warn:
4867 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4868 name, rc);
4869out_no_firmware:
4870 tp->rtl_fw = NULL;
4871 goto out;
4872}
4873
François Romieu953a12c2011-04-24 17:38:48 +02004874static void rtl_request_firmware(struct rtl8169_private *tp)
4875{
Francois Romieub6ffd972011-06-17 17:00:05 +02004876 if (IS_ERR(tp->rtl_fw))
4877 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004878}
4879
Hayes Wang92fc43b2011-07-06 15:58:03 +08004880static void rtl_rx_close(struct rtl8169_private *tp)
4881{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004882 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004883}
4884
Francois Romieuffc46952012-07-06 14:19:23 +02004885DECLARE_RTL_COND(rtl_npq_cond)
4886{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004887 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004888}
4889
4890DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4891{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004892 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004893}
4894
françois romieue6de30d2011-01-03 15:08:37 +00004895static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
4897 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004898 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Hayes Wang92fc43b2011-07-06 15:58:03 +08004900 rtl_rx_close(tp);
4901
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004902 switch (tp->mac_version) {
4903 case RTL_GIGA_MAC_VER_27:
4904 case RTL_GIGA_MAC_VER_28:
4905 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004906 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004907 break;
4908 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4909 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004910 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004911 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004912 break;
4913 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004914 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004915 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004916 break;
françois romieue6de30d2011-01-03 15:08:37 +00004917 }
4918
Hayes Wang92fc43b2011-07-06 15:58:03 +08004919 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920}
4921
Francois Romieu7f796d832007-06-11 23:04:41 +02004922static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004923{
Francois Romieu9cb427b2006-11-02 00:10:16 +01004924 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004925 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01004926 (InterFrameGap << TxInterFrameGapShift));
4927}
4928
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004929static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004931 /* Low hurts. Let's disable the filtering. */
4932 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004933}
4934
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004935static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004936{
4937 /*
4938 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4939 * register to be written before TxDescAddrLow to work.
4940 * Switching from MMIO to I/O access fixes the issue as well.
4941 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004942 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4943 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4944 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4945 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004946}
4947
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004948static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004949{
Francois Romieu37441002011-06-17 22:58:54 +02004950 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004951 u32 mac_version;
4952 u32 clk;
4953 u32 val;
4954 } cfg2_info [] = {
4955 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4956 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4957 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4958 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004959 };
4960 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004961 unsigned int i;
4962 u32 clk;
4963
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004964 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004965 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004966 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004967 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004968 break;
4969 }
4970 }
4971}
4972
Francois Romieue6b763e2012-03-08 09:35:39 +01004973static void rtl_set_rx_mode(struct net_device *dev)
4974{
4975 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004976 u32 mc_filter[2]; /* Multicast hash filter */
4977 int rx_mode;
4978 u32 tmp = 0;
4979
4980 if (dev->flags & IFF_PROMISC) {
4981 /* Unconditionally log net taps. */
4982 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4983 rx_mode =
4984 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4985 AcceptAllPhys;
4986 mc_filter[1] = mc_filter[0] = 0xffffffff;
4987 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4988 (dev->flags & IFF_ALLMULTI)) {
4989 /* Too many to filter perfectly -- accept all multicasts. */
4990 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4991 mc_filter[1] = mc_filter[0] = 0xffffffff;
4992 } else {
4993 struct netdev_hw_addr *ha;
4994
4995 rx_mode = AcceptBroadcast | AcceptMyPhys;
4996 mc_filter[1] = mc_filter[0] = 0;
4997 netdev_for_each_mc_addr(ha, dev) {
4998 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4999 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5000 rx_mode |= AcceptMulticast;
5001 }
5002 }
5003
5004 if (dev->features & NETIF_F_RXALL)
5005 rx_mode |= (AcceptErr | AcceptRunt);
5006
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005007 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005008
5009 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5010 u32 data = mc_filter[0];
5011
5012 mc_filter[0] = swab32(mc_filter[1]);
5013 mc_filter[1] = swab32(data);
5014 }
5015
Nathan Walp04817762012-11-01 12:08:47 +00005016 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5017 mc_filter[1] = mc_filter[0] = 0xffffffff;
5018
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005019 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5020 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005021
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005022 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005023}
5024
Heiner Kallweit52f85602018-05-19 10:29:33 +02005025static void rtl_hw_start(struct rtl8169_private *tp)
5026{
5027 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
5028
5029 tp->hw_start(tp);
5030
5031 rtl_set_rx_max_size(tp);
5032 rtl_set_rx_tx_desc_registers(tp);
5033 rtl_set_rx_tx_config_registers(tp);
5034 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
5035
5036 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5037 RTL_R8(tp, IntrMask);
5038 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5039 rtl_set_rx_mode(tp->dev);
5040 /* no early-rx interrupts */
5041 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5042 rtl_irq_enable_all(tp);
5043}
5044
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005045static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005046{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005047 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005048 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005049
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005050 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005052 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005053
Francois Romieucecb5fd2011-04-01 10:21:07 +02005054 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5055 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02005056 netif_dbg(tp, drv, tp->dev,
5057 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005058 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
5060
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005061 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005062
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005063 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 /*
5066 * Undocumented corner. Supposedly:
5067 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5068 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005069 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005071 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01005072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Francois Romieuffc46952012-07-06 14:19:23 +02005074DECLARE_RTL_COND(rtl_csiar_cond)
5075{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005076 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005077}
5078
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005079static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005080{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005081 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5082
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005083 RTL_W32(tp, CSIDR, value);
5084 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005085 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005086
Francois Romieuffc46952012-07-06 14:19:23 +02005087 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005088}
5089
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005090static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005091{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005092 u32 func = PCI_FUNC(tp->pci_dev->devfn);
5093
5094 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
5095 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005096
Francois Romieuffc46952012-07-06 14:19:23 +02005097 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005098 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005099}
5100
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005101static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005102{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005103 struct pci_dev *pdev = tp->pci_dev;
5104 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005105
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005106 /* According to Realtek the value at config space address 0x070f
5107 * controls the L0s/L1 entrance latency. We try standard ECAM access
5108 * first and if it fails fall back to CSI.
5109 */
5110 if (pdev->cfg_size > 0x070f &&
5111 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
5112 return;
5113
5114 netdev_notice_once(tp->dev,
5115 "No native access to PCI extended config space, falling back to CSI\n");
5116 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5117 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005118}
5119
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005120static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08005121{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02005122 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02005123}
5124
5125struct ephy_info {
5126 unsigned int offset;
5127 u16 mask;
5128 u16 bits;
5129};
5130
Francois Romieufdf6fc02012-07-06 22:40:38 +02005131static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5132 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005133{
5134 u16 w;
5135
5136 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005137 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5138 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005139 e++;
5140 }
5141}
5142
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005143static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005144{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005145 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005146 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005147}
5148
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005149static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005150{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005151 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005152 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005153}
5154
hayeswangb51ecea2014-07-09 14:52:51 +08005155static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5156{
hayeswangb51ecea2014-07-09 14:52:51 +08005157 u8 data;
5158
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005159 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005160
5161 if (enable)
5162 data |= Rdy_to_L23;
5163 else
5164 data &= ~Rdy_to_L23;
5165
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005166 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005167}
5168
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005169static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
5170{
5171 if (enable) {
5172 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
5173 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
5174 } else {
5175 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5176 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5177 }
5178}
5179
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005180static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005181{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005182 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005183
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005184 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005185 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02005186
françois romieufaf1e782013-02-27 13:01:57 +00005187 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005188 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005189 PCI_EXP_DEVCTL_NOSNOOP_EN);
5190 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005191}
5192
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005193static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005194{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005195 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005196
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005197 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005198
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005199 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005200}
5201
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005202static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005203{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005204 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005205
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005206 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005207
françois romieufaf1e782013-02-27 13:01:57 +00005208 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005209 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005210
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005211 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005212
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005213 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005214 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02005215}
5216
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005217static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005218{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005219 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005220 { 0x01, 0, 0x0001 },
5221 { 0x02, 0x0800, 0x1000 },
5222 { 0x03, 0, 0x0042 },
5223 { 0x06, 0x0080, 0x0000 },
5224 { 0x07, 0, 0x2000 }
5225 };
5226
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005227 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005228
Francois Romieufdf6fc02012-07-06 22:40:38 +02005229 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005230
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005231 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005232}
5233
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005234static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005235{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005236 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005237
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005238 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005239
françois romieufaf1e782013-02-27 13:01:57 +00005240 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005241 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005242
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005243 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005244 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02005245}
5246
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005247static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005248{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005249 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005250
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005251 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005252
5253 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005254 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005255
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005256 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005257
françois romieufaf1e782013-02-27 13:01:57 +00005258 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005259 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005260
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005261 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005262 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005263}
5264
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005265static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005266{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005267 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005268 { 0x02, 0x0800, 0x1000 },
5269 { 0x03, 0, 0x0002 },
5270 { 0x06, 0x0080, 0x0000 }
5271 };
5272
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005273 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005274
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005275 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005276
Francois Romieufdf6fc02012-07-06 22:40:38 +02005277 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005278
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005279 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005280}
5281
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005282static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005283{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005284 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005285 { 0x01, 0, 0x0001 },
5286 { 0x03, 0x0400, 0x0220 }
5287 };
5288
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005289 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005290
Francois Romieufdf6fc02012-07-06 22:40:38 +02005291 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005292
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005293 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005294}
5295
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005296static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005297{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005298 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005299}
5300
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005301static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005302{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005303 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005304
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005305 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005306}
5307
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005308static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005309{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005310 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005311
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005312 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005313
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005314 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005315
françois romieufaf1e782013-02-27 13:01:57 +00005316 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005317 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005318
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005319 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005320 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02005321}
5322
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005323static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005324{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005325 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005326
françois romieufaf1e782013-02-27 13:01:57 +00005327 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005328 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005329
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005330 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005331
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005332 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005333}
5334
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005335static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005336{
5337 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005338 { 0x0b, 0x0000, 0x0048 },
5339 { 0x19, 0x0020, 0x0050 },
5340 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005341 };
françois romieue6de30d2011-01-03 15:08:37 +00005342
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005343 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005344
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005345 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005346
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005347 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005348
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005349 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005350
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005351 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005352}
5353
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005354static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005355{
Hayes Wang70090422011-07-06 15:58:06 +08005356 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005357 { 0x00, 0x0200, 0x0100 },
5358 { 0x00, 0x0000, 0x0004 },
5359 { 0x06, 0x0002, 0x0001 },
5360 { 0x06, 0x0000, 0x0030 },
5361 { 0x07, 0x0000, 0x2000 },
5362 { 0x00, 0x0000, 0x0020 },
5363 { 0x03, 0x5800, 0x2000 },
5364 { 0x03, 0x0000, 0x0001 },
5365 { 0x01, 0x0800, 0x1000 },
5366 { 0x07, 0x0000, 0x4000 },
5367 { 0x1e, 0x0000, 0x2000 },
5368 { 0x19, 0xffff, 0xfe6c },
5369 { 0x0a, 0x0000, 0x0040 }
5370 };
5371
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005372 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005373
Francois Romieufdf6fc02012-07-06 22:40:38 +02005374 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005375
françois romieufaf1e782013-02-27 13:01:57 +00005376 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005377 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005378
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005379 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005380
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005381 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005382
5383 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005384 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5385 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005386
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005387 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005388}
5389
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005390static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005391{
5392 static const struct ephy_info e_info_8168e_2[] = {
5393 { 0x09, 0x0000, 0x0080 },
5394 { 0x19, 0x0000, 0x0224 }
5395 };
5396
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005397 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005398
Francois Romieufdf6fc02012-07-06 22:40:38 +02005399 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005400
françois romieufaf1e782013-02-27 13:01:57 +00005401 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005402 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005403
Francois Romieufdf6fc02012-07-06 22:40:38 +02005404 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5405 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5406 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5407 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5408 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5409 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005410 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5411 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005412
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005413 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005414
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005415 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005416
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005417 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5418 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005419
5420 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005421 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005422
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005423 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5424 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5425 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005426
5427 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005428}
5429
Hayes Wang5f886e02012-03-30 14:33:03 +08005430static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005431{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005432 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005433
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005434 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005435
Francois Romieufdf6fc02012-07-06 22:40:38 +02005436 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5437 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5438 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5439 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005440 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5441 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5442 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5443 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005444 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5445 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005446
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005447 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005448
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005449 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005450
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005451 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5452 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5453 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5454 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5455 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005456}
5457
Hayes Wang5f886e02012-03-30 14:33:03 +08005458static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5459{
Hayes Wang5f886e02012-03-30 14:33:03 +08005460 static const struct ephy_info e_info_8168f_1[] = {
5461 { 0x06, 0x00c0, 0x0020 },
5462 { 0x08, 0x0001, 0x0002 },
5463 { 0x09, 0x0000, 0x0080 },
5464 { 0x19, 0x0000, 0x0224 }
5465 };
5466
5467 rtl_hw_start_8168f(tp);
5468
Francois Romieufdf6fc02012-07-06 22:40:38 +02005469 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005470
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005471 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005472
5473 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005474 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005475}
5476
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005477static void rtl_hw_start_8411(struct rtl8169_private *tp)
5478{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005479 static const struct ephy_info e_info_8168f_1[] = {
5480 { 0x06, 0x00c0, 0x0020 },
5481 { 0x0f, 0xffff, 0x5200 },
5482 { 0x1e, 0x0000, 0x4000 },
5483 { 0x19, 0x0000, 0x0224 }
5484 };
5485
5486 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005487 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005488
Francois Romieufdf6fc02012-07-06 22:40:38 +02005489 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005490
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005491 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005492}
5493
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005494static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005495{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005496 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005497
Hayes Wangc5583862012-07-02 17:23:22 +08005498 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5499 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5500 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5501 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5502
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005503 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005504
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005505 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005506
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005507 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5508 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005509 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005510
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005511 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5512 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005513
5514 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5515 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5516
5517 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005518 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005519
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005520 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5521 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005522
5523 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005524}
5525
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005526static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5527{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005528 static const struct ephy_info e_info_8168g_1[] = {
5529 { 0x00, 0x0000, 0x0008 },
5530 { 0x0c, 0x37d0, 0x0820 },
5531 { 0x1e, 0x0000, 0x0001 },
5532 { 0x19, 0x8000, 0x0000 }
5533 };
5534
5535 rtl_hw_start_8168g(tp);
5536
5537 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005538 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005539 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005540 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005541}
5542
hayeswang57538c42013-04-01 22:23:40 +00005543static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5544{
hayeswang57538c42013-04-01 22:23:40 +00005545 static const struct ephy_info e_info_8168g_2[] = {
5546 { 0x00, 0x0000, 0x0008 },
5547 { 0x0c, 0x3df0, 0x0200 },
5548 { 0x19, 0xffff, 0xfc00 },
5549 { 0x1e, 0xffff, 0x20eb }
5550 };
5551
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005552 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005553
5554 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005555 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5556 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005557 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5558}
5559
hayeswang45dd95c2013-07-08 17:09:01 +08005560static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5561{
hayeswang45dd95c2013-07-08 17:09:01 +08005562 static const struct ephy_info e_info_8411_2[] = {
5563 { 0x00, 0x0000, 0x0008 },
5564 { 0x0c, 0x3df0, 0x0200 },
5565 { 0x0f, 0xffff, 0x5200 },
5566 { 0x19, 0x0020, 0x0000 },
5567 { 0x1e, 0x0000, 0x2000 }
5568 };
5569
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005570 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005571
5572 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005573 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005574 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005575 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005576}
5577
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005578static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5579{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005580 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005581 u32 data;
5582 static const struct ephy_info e_info_8168h_1[] = {
5583 { 0x1e, 0x0800, 0x0001 },
5584 { 0x1d, 0x0000, 0x0800 },
5585 { 0x05, 0xffff, 0x2089 },
5586 { 0x06, 0xffff, 0x5881 },
5587 { 0x04, 0xffff, 0x154a },
5588 { 0x01, 0xffff, 0x068b }
5589 };
5590
5591 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005592 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005593 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5594
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005595 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005596
5597 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5598 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5599 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5600 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5601
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005602 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005603
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005604 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005605
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005606 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5607 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005608
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005609 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005610
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005611 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005612
5613 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5614
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005615 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5616 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005617
5618 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5619 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5620
5621 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005622 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005623
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005624 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5625 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005626
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005627 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005628
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005629 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005630
5631 rtl_pcie_state_l2l3_enable(tp, false);
5632
5633 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005634 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005635 rtl_writephy(tp, 0x1f, 0x0000);
5636 if (rg_saw_cnt > 0) {
5637 u16 sw_cnt_1ms_ini;
5638
5639 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5640 sw_cnt_1ms_ini &= 0x0fff;
5641 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005642 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005643 data |= sw_cnt_1ms_ini;
5644 r8168_mac_ocp_write(tp, 0xd412, data);
5645 }
5646
5647 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005648 data &= ~0xf0;
5649 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005650 r8168_mac_ocp_write(tp, 0xe056, data);
5651
5652 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005653 data &= ~0x6000;
5654 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005655 r8168_mac_ocp_write(tp, 0xe052, data);
5656
5657 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005658 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005659 data |= 0x017f;
5660 r8168_mac_ocp_write(tp, 0xe0d6, data);
5661
5662 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005663 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005664 data |= 0x047f;
5665 r8168_mac_ocp_write(tp, 0xd420, data);
5666
5667 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5668 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5669 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5670 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005671
5672 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005673}
5674
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005675static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5676{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005677 rtl8168ep_stop_cmac(tp);
5678
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005679 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005680
5681 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5682 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5683 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5684 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5685
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005686 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005687
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005688 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005689
5690 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5691 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5692
5693 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5694
5695 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5696
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005697 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5698 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005699
5700 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5701 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5702
5703 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005704 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005705
5706 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5707
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005708 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005709
5710 rtl_pcie_state_l2l3_enable(tp, false);
5711}
5712
5713static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5714{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005715 static const struct ephy_info e_info_8168ep_1[] = {
5716 { 0x00, 0xffff, 0x10ab },
5717 { 0x06, 0xffff, 0xf030 },
5718 { 0x08, 0xffff, 0x2006 },
5719 { 0x0d, 0xffff, 0x1666 },
5720 { 0x0c, 0x3ff0, 0x0000 }
5721 };
5722
5723 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005724 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005725 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5726
5727 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005728
5729 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005730}
5731
5732static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5733{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005734 static const struct ephy_info e_info_8168ep_2[] = {
5735 { 0x00, 0xffff, 0x10a3 },
5736 { 0x19, 0xffff, 0xfc00 },
5737 { 0x1e, 0xffff, 0x20ea }
5738 };
5739
5740 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005741 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005742 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5743
5744 rtl_hw_start_8168ep(tp);
5745
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005746 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5747 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005748
5749 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005750}
5751
5752static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5753{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005754 u32 data;
5755 static const struct ephy_info e_info_8168ep_3[] = {
5756 { 0x00, 0xffff, 0x10a3 },
5757 { 0x19, 0xffff, 0x7c00 },
5758 { 0x1e, 0xffff, 0x20eb },
5759 { 0x0d, 0xffff, 0x1666 }
5760 };
5761
5762 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005763 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005764 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5765
5766 rtl_hw_start_8168ep(tp);
5767
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005768 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5769 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005770
5771 data = r8168_mac_ocp_read(tp, 0xd3e2);
5772 data &= 0xf000;
5773 data |= 0x0271;
5774 r8168_mac_ocp_write(tp, 0xd3e2, data);
5775
5776 data = r8168_mac_ocp_read(tp, 0xd3e4);
5777 data &= 0xff00;
5778 r8168_mac_ocp_write(tp, 0xd3e4, data);
5779
5780 data = r8168_mac_ocp_read(tp, 0xe860);
5781 data |= 0x0080;
5782 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005783
5784 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005785}
5786
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005787static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005788{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005789 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005790
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005791 tp->cp_cmd &= ~INTT_MASK;
5792 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005793 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005794
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005795 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005796
5797 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005798 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005799 tp->event_slow |= RxFIFOOver | PCSTimeout;
5800 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005801 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005802
Francois Romieu219a1e92008-06-28 11:58:39 +02005803 switch (tp->mac_version) {
5804 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005805 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005806 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005807
5808 case RTL_GIGA_MAC_VER_12:
5809 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005810 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005811 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005812
5813 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005814 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005815 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005816
5817 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005818 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005819 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005820
5821 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005822 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005823 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005824
Francois Romieu197ff762008-06-28 13:16:02 +02005825 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005826 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005827 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005828
Francois Romieu6fb07052008-06-29 11:54:28 +02005829 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005830 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005831 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005832
Francois Romieuef3386f2008-06-29 12:24:30 +02005833 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005834 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005835 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005836
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005837 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005838 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005839 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005840
Francois Romieu5b538df2008-07-20 16:22:45 +02005841 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005842 case RTL_GIGA_MAC_VER_26:
5843 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005844 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005845 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005846
françois romieue6de30d2011-01-03 15:08:37 +00005847 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005848 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005849 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005850
hayeswang4804b3b2011-03-21 01:50:29 +00005851 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005852 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005853 break;
5854
hayeswang01dc7fe2011-03-21 01:50:28 +00005855 case RTL_GIGA_MAC_VER_32:
5856 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005857 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005858 break;
5859 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005860 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005861 break;
françois romieue6de30d2011-01-03 15:08:37 +00005862
Hayes Wangc2218922011-09-06 16:55:18 +08005863 case RTL_GIGA_MAC_VER_35:
5864 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005865 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005866 break;
5867
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005868 case RTL_GIGA_MAC_VER_38:
5869 rtl_hw_start_8411(tp);
5870 break;
5871
Hayes Wangc5583862012-07-02 17:23:22 +08005872 case RTL_GIGA_MAC_VER_40:
5873 case RTL_GIGA_MAC_VER_41:
5874 rtl_hw_start_8168g_1(tp);
5875 break;
hayeswang57538c42013-04-01 22:23:40 +00005876 case RTL_GIGA_MAC_VER_42:
5877 rtl_hw_start_8168g_2(tp);
5878 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005879
hayeswang45dd95c2013-07-08 17:09:01 +08005880 case RTL_GIGA_MAC_VER_44:
5881 rtl_hw_start_8411_2(tp);
5882 break;
5883
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005884 case RTL_GIGA_MAC_VER_45:
5885 case RTL_GIGA_MAC_VER_46:
5886 rtl_hw_start_8168h_1(tp);
5887 break;
5888
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005889 case RTL_GIGA_MAC_VER_49:
5890 rtl_hw_start_8168ep_1(tp);
5891 break;
5892
5893 case RTL_GIGA_MAC_VER_50:
5894 rtl_hw_start_8168ep_2(tp);
5895 break;
5896
5897 case RTL_GIGA_MAC_VER_51:
5898 rtl_hw_start_8168ep_3(tp);
5899 break;
5900
Francois Romieu219a1e92008-06-28 11:58:39 +02005901 default:
Heiner Kallweit49d17512018-06-28 20:36:15 +02005902 netif_err(tp, drv, tp->dev,
5903 "unknown chipset (mac_version = %d)\n",
5904 tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005905 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005906 }
Francois Romieu07ce4062007-02-23 23:36:39 +01005907}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005909static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005910{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005911 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005912 { 0x01, 0, 0x6e65 },
5913 { 0x02, 0, 0x091f },
5914 { 0x03, 0, 0xc2f9 },
5915 { 0x06, 0, 0xafb5 },
5916 { 0x07, 0, 0x0e00 },
5917 { 0x19, 0, 0xec80 },
5918 { 0x01, 0, 0x2e65 },
5919 { 0x01, 0, 0x6e65 }
5920 };
5921 u8 cfg1;
5922
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005923 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005924
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005925 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005926
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005927 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005928
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005929 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005930 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005931 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005932
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005933 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005934 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005935 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005936
Francois Romieufdf6fc02012-07-06 22:40:38 +02005937 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005938}
5939
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005940static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005941{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005942 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005943
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005944 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005945
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005946 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5947 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005948}
5949
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005950static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005951{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005952 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005953
Francois Romieufdf6fc02012-07-06 22:40:38 +02005954 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005955}
5956
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005957static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005958{
5959 static const struct ephy_info e_info_8105e_1[] = {
5960 { 0x07, 0, 0x4000 },
5961 { 0x19, 0, 0x0200 },
5962 { 0x19, 0, 0x0020 },
5963 { 0x1e, 0, 0x2000 },
5964 { 0x03, 0, 0x0001 },
5965 { 0x19, 0, 0x0100 },
5966 { 0x19, 0, 0x0004 },
5967 { 0x0a, 0, 0x0020 }
5968 };
5969
Francois Romieucecb5fd2011-04-01 10:21:07 +02005970 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005971 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005972
Francois Romieucecb5fd2011-04-01 10:21:07 +02005973 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005974 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005975
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005976 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5977 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005978
Francois Romieufdf6fc02012-07-06 22:40:38 +02005979 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005980
5981 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005982}
5983
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005984static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005985{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005986 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005987 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005988}
5989
Hayes Wang7e18dca2012-03-30 14:33:02 +08005990static void rtl_hw_start_8402(struct rtl8169_private *tp)
5991{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005992 static const struct ephy_info e_info_8402[] = {
5993 { 0x19, 0xffff, 0xff64 },
5994 { 0x1e, 0, 0x4000 }
5995 };
5996
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005997 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005998
5999 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006000 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006001
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006002 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6003 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006004
Francois Romieufdf6fc02012-07-06 22:40:38 +02006005 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006006
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006007 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006008
Francois Romieufdf6fc02012-07-06 22:40:38 +02006009 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6010 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006011 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6012 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006013 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6014 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006015 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006016
6017 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006018}
6019
Hayes Wang5598bfe2012-07-02 17:23:21 +08006020static void rtl_hw_start_8106(struct rtl8169_private *tp)
6021{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006022 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006023 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006024
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006025 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6026 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6027 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006028
6029 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006030}
6031
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006032static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006033{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006034 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6035 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006036
Francois Romieucecb5fd2011-04-01 10:21:07 +02006037 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006038 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006039 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006040 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006041
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006042 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006043
Heiner Kallweit12d42c52018-04-28 22:19:30 +02006044 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006045 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006046
Francois Romieu2857ffb2008-08-02 21:08:49 +02006047 switch (tp->mac_version) {
6048 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006049 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006050 break;
6051
6052 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006053 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006054 break;
6055
6056 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006057 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006058 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006059
6060 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006061 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006062 break;
6063 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006064 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006065 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006066
6067 case RTL_GIGA_MAC_VER_37:
6068 rtl_hw_start_8402(tp);
6069 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006070
6071 case RTL_GIGA_MAC_VER_39:
6072 rtl_hw_start_8106(tp);
6073 break;
hayeswang58152cd2013-04-01 22:23:42 +00006074 case RTL_GIGA_MAC_VER_43:
6075 rtl_hw_start_8168g_2(tp);
6076 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006077 case RTL_GIGA_MAC_VER_47:
6078 case RTL_GIGA_MAC_VER_48:
6079 rtl_hw_start_8168h_1(tp);
6080 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006081 }
6082
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006083 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084}
6085
6086static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6087{
Francois Romieud58d46b2011-05-03 16:38:29 +02006088 struct rtl8169_private *tp = netdev_priv(dev);
6089
Francois Romieud58d46b2011-05-03 16:38:29 +02006090 if (new_mtu > ETH_DATA_LEN)
6091 rtl_hw_jumbo_enable(tp);
6092 else
6093 rtl_hw_jumbo_disable(tp);
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006096 netdev_update_features(dev);
6097
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099}
6100
6101static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6102{
Al Viro95e09182007-12-22 18:55:39 +00006103 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6105}
6106
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006107static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6108 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006110 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6111 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006112
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006113 kfree(*data_buff);
6114 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 rtl8169_make_unusable_by_asic(desc);
6116}
6117
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006118static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119{
6120 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6121
Alexander Duycka0750132014-12-11 15:02:17 -08006122 /* Force memory writes to complete before releasing descriptor */
6123 dma_wmb();
6124
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006125 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126}
6127
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006128static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006130 return (void *)ALIGN((long)data, 16);
6131}
6132
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006133static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6134 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006135{
6136 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006138 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006139 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006141 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006142 if (!data)
6143 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006144
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006145 if (rtl8169_align(data) != data) {
6146 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006147 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006148 if (!data)
6149 return NULL;
6150 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006151
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006152 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006153 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006154 if (unlikely(dma_mapping_error(d, mapping))) {
6155 if (net_ratelimit())
6156 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006157 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Heiner Kallweitd731af72018-04-17 23:26:41 +02006160 desc->addr = cpu_to_le64(mapping);
6161 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006162 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006163
6164err_out:
6165 kfree(data);
6166 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167}
6168
6169static void rtl8169_rx_clear(struct rtl8169_private *tp)
6170{
Francois Romieu07d3f512007-02-21 22:40:46 +01006171 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
6173 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006174 if (tp->Rx_databuff[i]) {
6175 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 tp->RxDescArray + i);
6177 }
6178 }
6179}
6180
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006181static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006183 desc->opts1 |= cpu_to_le32(RingEnd);
6184}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006185
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006186static int rtl8169_rx_fill(struct rtl8169_private *tp)
6187{
6188 unsigned int i;
6189
6190 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006191 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006192
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006193 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006194 if (!data) {
6195 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006196 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006197 }
6198 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006201 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6202 return 0;
6203
6204err_out:
6205 rtl8169_rx_clear(tp);
6206 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207}
6208
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006209static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 rtl8169_init_ring_indexes(tp);
6212
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006213 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6214 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006216 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006219static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 struct TxDesc *desc)
6221{
6222 unsigned int len = tx_skb->len;
6223
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006224 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 desc->opts1 = 0x00;
6227 desc->opts2 = 0x00;
6228 desc->addr = 0x00;
6229 tx_skb->len = 0;
6230}
6231
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006232static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6233 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234{
6235 unsigned int i;
6236
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006237 for (i = 0; i < n; i++) {
6238 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 struct ring_info *tx_skb = tp->tx_skb + entry;
6240 unsigned int len = tx_skb->len;
6241
6242 if (len) {
6243 struct sk_buff *skb = tx_skb->skb;
6244
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006245 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 tp->TxDescArray + entry);
6247 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006248 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 tx_skb->skb = NULL;
6250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 }
6252 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006253}
6254
6255static void rtl8169_tx_clear(struct rtl8169_private *tp)
6256{
6257 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 tp->cur_tx = tp->dirty_tx = 0;
6259}
6260
Francois Romieu4422bcd2012-01-26 11:23:32 +01006261static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262{
David Howellsc4028952006-11-22 14:57:56 +00006263 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006264 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Francois Romieuda78dbf2012-01-26 14:18:23 +01006266 napi_disable(&tp->napi);
6267 netif_stop_queue(dev);
6268 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
françois romieuc7c2c392011-12-04 20:30:52 +00006270 rtl8169_hw_reset(tp);
6271
Francois Romieu56de4142011-03-15 17:29:31 +01006272 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006273 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006274
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006276 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277
Francois Romieuda78dbf2012-01-26 14:18:23 +01006278 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006279 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006280 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006281 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282}
6283
6284static void rtl8169_tx_timeout(struct net_device *dev)
6285{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006286 struct rtl8169_private *tp = netdev_priv(dev);
6287
6288 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289}
6290
6291static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006292 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
6294 struct skb_shared_info *info = skb_shinfo(skb);
6295 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006296 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006297 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
6299 entry = tp->cur_tx;
6300 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006301 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 dma_addr_t mapping;
6303 u32 status, len;
6304 void *addr;
6305
6306 entry = (entry + 1) % NUM_TX_DESC;
6307
6308 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006309 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006310 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006311 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006312 if (unlikely(dma_mapping_error(d, mapping))) {
6313 if (net_ratelimit())
6314 netif_err(tp, drv, tp->dev,
6315 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006316 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Francois Romieucecb5fd2011-04-01 10:21:07 +02006319 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006320 status = opts[0] | len |
6321 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
6323 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006324 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 txd->addr = cpu_to_le64(mapping);
6326
6327 tp->tx_skb[entry].len = len;
6328 }
6329
6330 if (cur_frag) {
6331 tp->tx_skb[entry].skb = skb;
6332 txd->opts1 |= cpu_to_le32(LastFrag);
6333 }
6334
6335 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006336
6337err_out:
6338 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6339 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340}
6341
françois romieub423e9a2013-05-18 01:24:46 +00006342static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6343{
6344 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6345}
6346
hayeswange9746042014-07-11 16:25:58 +08006347static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6348 struct net_device *dev);
6349/* r8169_csum_workaround()
6350 * The hw limites the value the transport offset. When the offset is out of the
6351 * range, calculate the checksum by sw.
6352 */
6353static void r8169_csum_workaround(struct rtl8169_private *tp,
6354 struct sk_buff *skb)
6355{
6356 if (skb_shinfo(skb)->gso_size) {
6357 netdev_features_t features = tp->dev->features;
6358 struct sk_buff *segs, *nskb;
6359
6360 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6361 segs = skb_gso_segment(skb, features);
6362 if (IS_ERR(segs) || !segs)
6363 goto drop;
6364
6365 do {
6366 nskb = segs;
6367 segs = segs->next;
6368 nskb->next = NULL;
6369 rtl8169_start_xmit(nskb, tp->dev);
6370 } while (segs);
6371
Alexander Duyckeb781392015-05-01 10:34:44 -07006372 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006373 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6374 if (skb_checksum_help(skb) < 0)
6375 goto drop;
6376
6377 rtl8169_start_xmit(skb, tp->dev);
6378 } else {
6379 struct net_device_stats *stats;
6380
6381drop:
6382 stats = &tp->dev->stats;
6383 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006384 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006385 }
6386}
6387
6388/* msdn_giant_send_check()
6389 * According to the document of microsoft, the TCP Pseudo Header excludes the
6390 * packet length for IPv6 TCP large packets.
6391 */
6392static int msdn_giant_send_check(struct sk_buff *skb)
6393{
6394 const struct ipv6hdr *ipv6h;
6395 struct tcphdr *th;
6396 int ret;
6397
6398 ret = skb_cow_head(skb, 0);
6399 if (ret)
6400 return ret;
6401
6402 ipv6h = ipv6_hdr(skb);
6403 th = tcp_hdr(skb);
6404
6405 th->check = 0;
6406 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6407
6408 return ret;
6409}
6410
hayeswang5888d3f2014-07-11 16:25:56 +08006411static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6412 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413{
Michał Mirosław350fb322011-04-08 06:35:56 +00006414 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
Francois Romieu2b7b4312011-04-18 22:53:24 -07006416 if (mss) {
6417 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006418 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6419 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6420 const struct iphdr *ip = ip_hdr(skb);
6421
6422 if (ip->protocol == IPPROTO_TCP)
6423 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6424 else if (ip->protocol == IPPROTO_UDP)
6425 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6426 else
6427 WARN_ON_ONCE(1);
6428 }
6429
6430 return true;
6431}
6432
6433static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6434 struct sk_buff *skb, u32 *opts)
6435{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006436 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006437 u32 mss = skb_shinfo(skb)->gso_size;
6438
6439 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006440 if (transport_offset > GTTCPHO_MAX) {
6441 netif_warn(tp, tx_err, tp->dev,
6442 "Invalid transport offset 0x%x for TSO\n",
6443 transport_offset);
6444 return false;
6445 }
6446
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006447 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006448 case htons(ETH_P_IP):
6449 opts[0] |= TD1_GTSENV4;
6450 break;
6451
6452 case htons(ETH_P_IPV6):
6453 if (msdn_giant_send_check(skb))
6454 return false;
6455
6456 opts[0] |= TD1_GTSENV6;
6457 break;
6458
6459 default:
6460 WARN_ON_ONCE(1);
6461 break;
6462 }
6463
hayeswangbdfa4ed2014-07-11 16:25:57 +08006464 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006465 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006466 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006467 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468
françois romieub423e9a2013-05-18 01:24:46 +00006469 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006470 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006471
hayeswange9746042014-07-11 16:25:58 +08006472 if (transport_offset > TCPHO_MAX) {
6473 netif_warn(tp, tx_err, tp->dev,
6474 "Invalid transport offset 0x%x\n",
6475 transport_offset);
6476 return false;
6477 }
6478
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006479 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006480 case htons(ETH_P_IP):
6481 opts[1] |= TD1_IPv4_CS;
6482 ip_protocol = ip_hdr(skb)->protocol;
6483 break;
6484
6485 case htons(ETH_P_IPV6):
6486 opts[1] |= TD1_IPv6_CS;
6487 ip_protocol = ipv6_hdr(skb)->nexthdr;
6488 break;
6489
6490 default:
6491 ip_protocol = IPPROTO_RAW;
6492 break;
6493 }
6494
6495 if (ip_protocol == IPPROTO_TCP)
6496 opts[1] |= TD1_TCP_CS;
6497 else if (ip_protocol == IPPROTO_UDP)
6498 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006499 else
6500 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006501
6502 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006503 } else {
6504 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006505 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 }
hayeswang5888d3f2014-07-11 16:25:56 +08006507
françois romieub423e9a2013-05-18 01:24:46 +00006508 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509}
6510
Stephen Hemminger613573252009-08-31 19:50:58 +00006511static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6512 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513{
6514 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006515 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006517 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 dma_addr_t mapping;
6519 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006520 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006521 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006522
Julien Ducourthial477206a2012-05-09 00:00:06 +02006523 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006524 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006525 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 }
6527
6528 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006529 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
françois romieub423e9a2013-05-18 01:24:46 +00006531 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6532 opts[0] = DescOwn;
6533
hayeswange9746042014-07-11 16:25:58 +08006534 if (!tp->tso_csum(tp, skb, opts)) {
6535 r8169_csum_workaround(tp, skb);
6536 return NETDEV_TX_OK;
6537 }
françois romieub423e9a2013-05-18 01:24:46 +00006538
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006539 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006540 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006541 if (unlikely(dma_mapping_error(d, mapping))) {
6542 if (net_ratelimit())
6543 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006544 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
6547 tp->tx_skb[entry].len = len;
6548 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
Francois Romieu2b7b4312011-04-18 22:53:24 -07006550 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006551 if (frags < 0)
6552 goto err_dma_1;
6553 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006554 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006555 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006556 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006557 tp->tx_skb[entry].skb = skb;
6558 }
6559
Francois Romieu2b7b4312011-04-18 22:53:24 -07006560 txd->opts2 = cpu_to_le32(opts[1]);
6561
Richard Cochran5047fb52012-03-10 07:29:42 +00006562 skb_tx_timestamp(skb);
6563
Alexander Duycka0750132014-12-11 15:02:17 -08006564 /* Force memory writes to complete before releasing descriptor */
6565 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566
Francois Romieucecb5fd2011-04-01 10:21:07 +02006567 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006568 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 txd->opts1 = cpu_to_le32(status);
6570
Alexander Duycka0750132014-12-11 15:02:17 -08006571 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006572 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
Alexander Duycka0750132014-12-11 15:02:17 -08006574 tp->cur_tx += frags + 1;
6575
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006576 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
David S. Miller87cda7c2015-02-22 15:54:29 -05006578 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006579
David S. Miller87cda7c2015-02-22 15:54:29 -05006580 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006581 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6582 * not miss a ring update when it notices a stopped queue.
6583 */
6584 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006586 /* Sync with rtl_tx:
6587 * - publish queue status and cur_tx ring index (write barrier)
6588 * - refresh dirty_tx ring index (read barrier).
6589 * May the current thread have a pessimistic view of the ring
6590 * status and forget to wake up queue, a racing rtl_tx thread
6591 * can't.
6592 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006593 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006594 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 netif_wake_queue(dev);
6596 }
6597
Stephen Hemminger613573252009-08-31 19:50:58 +00006598 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006600err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006601 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006602err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006603 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006604 dev->stats.tx_dropped++;
6605 return NETDEV_TX_OK;
6606
6607err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006609 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006610 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611}
6612
6613static void rtl8169_pcierr_interrupt(struct net_device *dev)
6614{
6615 struct rtl8169_private *tp = netdev_priv(dev);
6616 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 u16 pci_status, pci_cmd;
6618
6619 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6620 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6621
Joe Perchesbf82c182010-02-09 11:49:50 +00006622 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6623 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
6625 /*
6626 * The recovery sequence below admits a very elaborated explanation:
6627 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006628 * - I did not see what else could be done;
6629 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 *
6631 * Feel free to adjust to your needs.
6632 */
Francois Romieua27993f2006-12-18 00:04:19 +01006633 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006634 pci_cmd &= ~PCI_COMMAND_PARITY;
6635 else
6636 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6637
6638 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
6640 pci_write_config_word(pdev, PCI_STATUS,
6641 pci_status & (PCI_STATUS_DETECTED_PARITY |
6642 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6643 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6644
6645 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006646 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006647 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006649 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 }
6652
françois romieue6de30d2011-01-03 15:08:37 +00006653 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006654
Francois Romieu98ddf982012-01-31 10:47:34 +01006655 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656}
6657
Francois Romieuda78dbf2012-01-26 14:18:23 +01006658static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659{
6660 unsigned int dirty_tx, tx_left;
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 dirty_tx = tp->dirty_tx;
6663 smp_rmb();
6664 tx_left = tp->cur_tx - dirty_tx;
6665
6666 while (tx_left > 0) {
6667 unsigned int entry = dirty_tx % NUM_TX_DESC;
6668 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 u32 status;
6670
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6672 if (status & DescOwn)
6673 break;
6674
Alexander Duycka0750132014-12-11 15:02:17 -08006675 /* This barrier is needed to keep us from reading
6676 * any other fields out of the Tx descriptor until
6677 * we know the status of DescOwn
6678 */
6679 dma_rmb();
6680
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006681 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006682 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006684 u64_stats_update_begin(&tp->tx_stats.syncp);
6685 tp->tx_stats.packets++;
6686 tp->tx_stats.bytes += tx_skb->skb->len;
6687 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006688 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 tx_skb->skb = NULL;
6690 }
6691 dirty_tx++;
6692 tx_left--;
6693 }
6694
6695 if (tp->dirty_tx != dirty_tx) {
6696 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006697 /* Sync with rtl8169_start_xmit:
6698 * - publish dirty_tx ring index (write barrier)
6699 * - refresh cur_tx ring index and queue status (read barrier)
6700 * May the current thread miss the stopped queue condition,
6701 * a racing xmit thread can only have a right view of the
6702 * ring status.
6703 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006704 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006706 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 netif_wake_queue(dev);
6708 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006709 /*
6710 * 8168 hack: TxPoll requests are lost when the Tx packets are
6711 * too close. Let's kick an extra TxPoll request when a burst
6712 * of start_xmit activity is detected (if it is not detected,
6713 * it is slow enough). -- FR
6714 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006715 if (tp->cur_tx != dirty_tx)
6716 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 }
6718}
6719
Francois Romieu126fa4b2005-05-12 20:09:17 -04006720static inline int rtl8169_fragmented_frame(u32 status)
6721{
6722 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6723}
6724
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006725static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 u32 status = opts1 & RxProtoMask;
6728
6729 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006730 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 skb->ip_summed = CHECKSUM_UNNECESSARY;
6732 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006733 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734}
6735
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006736static struct sk_buff *rtl8169_try_rx_copy(void *data,
6737 struct rtl8169_private *tp,
6738 int pkt_size,
6739 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006741 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006742 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006744 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006745 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006746 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006747 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006748 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006749 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006750 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6751
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006752 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753}
6754
Francois Romieuda78dbf2012-01-26 14:18:23 +01006755static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756{
6757 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006758 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
Timo Teräs9fba0812013-01-15 21:01:24 +00006762 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006764 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 u32 status;
6766
Heiner Kallweit62028062018-04-17 23:30:29 +02006767 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 if (status & DescOwn)
6769 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006770
6771 /* This barrier is needed to keep us from reading
6772 * any other fields out of the Rx descriptor until
6773 * we know the status of DescOwn
6774 */
6775 dma_rmb();
6776
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006777 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006778 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6779 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006780 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006782 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006784 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006785 /* RxFOVF is a reserved bit on later chip versions */
6786 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6787 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006788 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006789 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006790 } else if (status & (RxRUNT | RxCRC) &&
6791 !(status & RxRWT) &&
6792 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006793 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006796 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006797 dma_addr_t addr;
6798 int pkt_size;
6799
6800process_pkt:
6801 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006802 if (likely(!(dev->features & NETIF_F_RXFCS)))
6803 pkt_size = (status & 0x00003fff) - 4;
6804 else
6805 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Francois Romieu126fa4b2005-05-12 20:09:17 -04006807 /*
6808 * The driver does not support incoming fragmented
6809 * frames. They are seen as a symptom of over-mtu
6810 * sized frames.
6811 */
6812 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006813 dev->stats.rx_dropped++;
6814 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006815 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006816 }
6817
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006818 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6819 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006820 if (!skb) {
6821 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006822 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 }
6824
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006825 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 skb_put(skb, pkt_size);
6827 skb->protocol = eth_type_trans(skb, dev);
6828
Francois Romieu7a8fc772011-03-01 17:18:33 +01006829 rtl8169_rx_vlan_tag(desc, skb);
6830
françois romieu39174292015-11-11 23:35:18 +01006831 if (skb->pkt_type == PACKET_MULTICAST)
6832 dev->stats.multicast++;
6833
Francois Romieu56de4142011-03-15 17:29:31 +01006834 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Junchang Wang8027aa22012-03-04 23:30:32 +01006836 u64_stats_update_begin(&tp->rx_stats.syncp);
6837 tp->rx_stats.packets++;
6838 tp->rx_stats.bytes += pkt_size;
6839 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 }
françois romieuce11ff52013-01-24 13:30:06 +00006841release_descriptor:
6842 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006843 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 }
6845
6846 count = cur_rx - tp->cur_rx;
6847 tp->cur_rx = cur_rx;
6848
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 return count;
6850}
6851
Francois Romieu07d3f512007-02-21 22:40:46 +01006852static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006854 struct rtl8169_private *tp = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006856 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006858 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006859 if (status && status != 0xffff) {
6860 status &= RTL_EVENT_NAPI | tp->event_slow;
6861 if (status) {
6862 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006863
Francois Romieuda78dbf2012-01-26 14:18:23 +01006864 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02006865 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 return IRQ_RETVAL(handled);
6869}
6870
Francois Romieuda78dbf2012-01-26 14:18:23 +01006871/*
6872 * Workqueue context.
6873 */
6874static void rtl_slow_event_work(struct rtl8169_private *tp)
6875{
6876 struct net_device *dev = tp->dev;
6877 u16 status;
6878
6879 status = rtl_get_events(tp) & tp->event_slow;
6880 rtl_ack_events(tp, status);
6881
6882 if (unlikely(status & RxFIFOOver)) {
6883 switch (tp->mac_version) {
6884 /* Work around for rx fifo overflow */
6885 case RTL_GIGA_MAC_VER_11:
6886 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006887 /* XXX - Hack alert. See rtl_task(). */
6888 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006889 default:
6890 break;
6891 }
6892 }
6893
6894 if (unlikely(status & SYSErr))
6895 rtl8169_pcierr_interrupt(dev);
6896
6897 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006898 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006899
françois romieu7dbb4912012-06-09 10:53:16 +00006900 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006901}
6902
Francois Romieu4422bcd2012-01-26 11:23:32 +01006903static void rtl_task(struct work_struct *work)
6904{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006905 static const struct {
6906 int bitnr;
6907 void (*action)(struct rtl8169_private *);
6908 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006909 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006910 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6911 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6912 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6913 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006914 struct rtl8169_private *tp =
6915 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006916 struct net_device *dev = tp->dev;
6917 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006918
Francois Romieuda78dbf2012-01-26 14:18:23 +01006919 rtl_lock_work(tp);
6920
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006921 if (!netif_running(dev) ||
6922 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006923 goto out_unlock;
6924
6925 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6926 bool pending;
6927
Francois Romieuda78dbf2012-01-26 14:18:23 +01006928 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006929 if (pending)
6930 rtl_work[i].action(tp);
6931 }
6932
6933out_unlock:
6934 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006935}
6936
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006937static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006939 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6940 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006941 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6942 int work_done= 0;
6943 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
Francois Romieuda78dbf2012-01-26 14:18:23 +01006945 status = rtl_get_events(tp);
6946 rtl_ack_events(tp, status & ~tp->event_slow);
6947
6948 if (status & RTL_EVENT_NAPI_RX)
6949 work_done = rtl_rx(dev, tp, (u32) budget);
6950
6951 if (status & RTL_EVENT_NAPI_TX)
6952 rtl_tx(dev, tp);
6953
6954 if (status & tp->event_slow) {
6955 enable_mask &= ~tp->event_slow;
6956
6957 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006960 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006961 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00006962
Francois Romieuda78dbf2012-01-26 14:18:23 +01006963 rtl_irq_enable(tp, enable_mask);
6964 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 }
6966
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006967 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006970static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006971{
6972 struct rtl8169_private *tp = netdev_priv(dev);
6973
6974 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6975 return;
6976
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006977 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6978 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006979}
6980
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981static void rtl8169_down(struct net_device *dev)
6982{
6983 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
Francois Romieu4876cc12011-03-11 21:07:11 +01006985 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006987 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006988 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
Hayes Wang92fc43b2011-07-06 15:58:03 +08006990 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006991 /*
6992 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006993 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6994 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006995 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006996 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006999 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 rtl8169_tx_clear(tp);
7002
7003 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007004
7005 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006}
7007
7008static int rtl8169_close(struct net_device *dev)
7009{
7010 struct rtl8169_private *tp = netdev_priv(dev);
7011 struct pci_dev *pdev = tp->pci_dev;
7012
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007013 pm_runtime_get_sync(&pdev->dev);
7014
Francois Romieucecb5fd2011-04-01 10:21:07 +02007015 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007016 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007017
Francois Romieuda78dbf2012-01-26 14:18:23 +01007018 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007019 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007020
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007022 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
Lekensteyn4ea72442013-07-22 09:53:30 +02007024 cancel_work_sync(&tp->wk.work);
7025
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007026 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007028 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7029 tp->RxPhyAddr);
7030 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7031 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 tp->TxDescArray = NULL;
7033 tp->RxDescArray = NULL;
7034
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007035 pm_runtime_put_sync(&pdev->dev);
7036
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 return 0;
7038}
7039
Francois Romieudc1c00c2012-03-08 10:06:18 +01007040#ifdef CONFIG_NET_POLL_CONTROLLER
7041static void rtl8169_netpoll(struct net_device *dev)
7042{
7043 struct rtl8169_private *tp = netdev_priv(dev);
7044
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03007045 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007046}
7047#endif
7048
Francois Romieudf43ac72012-03-08 09:48:40 +01007049static int rtl_open(struct net_device *dev)
7050{
7051 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007052 struct pci_dev *pdev = tp->pci_dev;
7053 int retval = -ENOMEM;
7054
7055 pm_runtime_get_sync(&pdev->dev);
7056
7057 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007058 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007059 * dma_alloc_coherent provides more.
7060 */
7061 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7062 &tp->TxPhyAddr, GFP_KERNEL);
7063 if (!tp->TxDescArray)
7064 goto err_pm_runtime_put;
7065
7066 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7067 &tp->RxPhyAddr, GFP_KERNEL);
7068 if (!tp->RxDescArray)
7069 goto err_free_tx_0;
7070
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007071 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007072 if (retval < 0)
7073 goto err_free_rx_1;
7074
7075 INIT_WORK(&tp->wk.work, rtl_task);
7076
7077 smp_mb();
7078
7079 rtl_request_firmware(tp);
7080
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02007081 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007082 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007083 if (retval < 0)
7084 goto err_release_fw_2;
7085
7086 rtl_lock_work(tp);
7087
7088 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7089
7090 napi_enable(&tp->napi);
7091
7092 rtl8169_init_phy(dev, tp);
7093
Francois Romieudf43ac72012-03-08 09:48:40 +01007094 rtl_pll_power_up(tp);
7095
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007096 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007097
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007098 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007099 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7100
Francois Romieudf43ac72012-03-08 09:48:40 +01007101 netif_start_queue(dev);
7102
7103 rtl_unlock_work(tp);
7104
Heiner Kallweita92a0842018-01-08 21:39:13 +01007105 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007106
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007107 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007108out:
7109 return retval;
7110
7111err_release_fw_2:
7112 rtl_release_firmware(tp);
7113 rtl8169_rx_clear(tp);
7114err_free_rx_1:
7115 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7116 tp->RxPhyAddr);
7117 tp->RxDescArray = NULL;
7118err_free_tx_0:
7119 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7120 tp->TxPhyAddr);
7121 tp->TxDescArray = NULL;
7122err_pm_runtime_put:
7123 pm_runtime_put_noidle(&pdev->dev);
7124 goto out;
7125}
7126
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007127static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007128rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129{
7130 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007131 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007132 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007133 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007135 pm_runtime_get_noresume(&pdev->dev);
7136
7137 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007138 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007139
Junchang Wang8027aa22012-03-04 23:30:32 +01007140 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007141 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007142 stats->rx_packets = tp->rx_stats.packets;
7143 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007144 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007145
Junchang Wang8027aa22012-03-04 23:30:32 +01007146 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007147 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007148 stats->tx_packets = tp->tx_stats.packets;
7149 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007150 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007151
7152 stats->rx_dropped = dev->stats.rx_dropped;
7153 stats->tx_dropped = dev->stats.tx_dropped;
7154 stats->rx_length_errors = dev->stats.rx_length_errors;
7155 stats->rx_errors = dev->stats.rx_errors;
7156 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7157 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7158 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007159 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007160
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007161 /*
7162 * Fetch additonal counter values missing in stats collected by driver
7163 * from tally counters.
7164 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007165 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007166 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007167
7168 /*
7169 * Subtract values fetched during initalization.
7170 * See rtl8169_init_counter_offsets for a description why we do that.
7171 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007172 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007173 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007174 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007175 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007176 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007177 le16_to_cpu(tp->tc_offset.tx_aborted);
7178
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007179 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180}
7181
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007182static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007183{
françois romieu065c27c2011-01-03 15:08:12 +00007184 struct rtl8169_private *tp = netdev_priv(dev);
7185
Francois Romieu5d06a992006-02-23 00:47:58 +01007186 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007187 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007188
7189 netif_device_detach(dev);
7190 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007191
7192 rtl_lock_work(tp);
7193 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007194 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007195 rtl_unlock_work(tp);
7196
7197 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007198}
Francois Romieu5d06a992006-02-23 00:47:58 +01007199
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007200#ifdef CONFIG_PM
7201
7202static int rtl8169_suspend(struct device *device)
7203{
7204 struct pci_dev *pdev = to_pci_dev(device);
7205 struct net_device *dev = pci_get_drvdata(pdev);
7206
7207 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007208
Francois Romieu5d06a992006-02-23 00:47:58 +01007209 return 0;
7210}
7211
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007212static void __rtl8169_resume(struct net_device *dev)
7213{
françois romieu065c27c2011-01-03 15:08:12 +00007214 struct rtl8169_private *tp = netdev_priv(dev);
7215
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007216 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007217
7218 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02007219 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00007220
Artem Savkovcff4c162012-04-03 10:29:11 +00007221 rtl_lock_work(tp);
7222 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007223 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007224 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007225
Francois Romieu98ddf982012-01-31 10:47:34 +01007226 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007227}
7228
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007229static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007230{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007231 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007232 struct net_device *dev = pci_get_drvdata(pdev);
7233
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007234 if (netif_running(dev))
7235 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007236
Francois Romieu5d06a992006-02-23 00:47:58 +01007237 return 0;
7238}
7239
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007240static int rtl8169_runtime_suspend(struct device *device)
7241{
7242 struct pci_dev *pdev = to_pci_dev(device);
7243 struct net_device *dev = pci_get_drvdata(pdev);
7244 struct rtl8169_private *tp = netdev_priv(dev);
7245
Heiner Kallweita92a0842018-01-08 21:39:13 +01007246 if (!tp->TxDescArray) {
7247 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007248 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007249 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007250
Francois Romieuda78dbf2012-01-26 14:18:23 +01007251 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007252 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007253 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007254
7255 rtl8169_net_suspend(dev);
7256
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007257 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007258 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007259 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007260
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007261 return 0;
7262}
7263
7264static int rtl8169_runtime_resume(struct device *device)
7265{
7266 struct pci_dev *pdev = to_pci_dev(device);
7267 struct net_device *dev = pci_get_drvdata(pdev);
7268 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007269 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007270
7271 if (!tp->TxDescArray)
7272 return 0;
7273
Francois Romieuda78dbf2012-01-26 14:18:23 +01007274 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007275 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007276 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007277
7278 __rtl8169_resume(dev);
7279
7280 return 0;
7281}
7282
7283static int rtl8169_runtime_idle(struct device *device)
7284{
7285 struct pci_dev *pdev = to_pci_dev(device);
7286 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007287
Heiner Kallweita92a0842018-01-08 21:39:13 +01007288 if (!netif_running(dev) || !netif_carrier_ok(dev))
7289 pm_schedule_suspend(device, 10000);
7290
7291 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007292}
7293
Alexey Dobriyan47145212009-12-14 18:00:08 -08007294static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007295 .suspend = rtl8169_suspend,
7296 .resume = rtl8169_resume,
7297 .freeze = rtl8169_suspend,
7298 .thaw = rtl8169_resume,
7299 .poweroff = rtl8169_suspend,
7300 .restore = rtl8169_resume,
7301 .runtime_suspend = rtl8169_runtime_suspend,
7302 .runtime_resume = rtl8169_runtime_resume,
7303 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007304};
7305
7306#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7307
7308#else /* !CONFIG_PM */
7309
7310#define RTL8169_PM_OPS NULL
7311
7312#endif /* !CONFIG_PM */
7313
David S. Miller1805b2f2011-10-24 18:18:09 -04007314static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7315{
David S. Miller1805b2f2011-10-24 18:18:09 -04007316 /* WoL fails with 8168b when the receiver is disabled. */
7317 switch (tp->mac_version) {
7318 case RTL_GIGA_MAC_VER_11:
7319 case RTL_GIGA_MAC_VER_12:
7320 case RTL_GIGA_MAC_VER_17:
7321 pci_clear_master(tp->pci_dev);
7322
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007323 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007324 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007325 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007326 break;
7327 default:
7328 break;
7329 }
7330}
7331
Francois Romieu1765f952008-09-13 17:21:40 +02007332static void rtl_shutdown(struct pci_dev *pdev)
7333{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007334 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007335 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007336
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007337 rtl8169_net_suspend(dev);
7338
Francois Romieucecb5fd2011-04-01 10:21:07 +02007339 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007340 rtl_rar_set(tp, dev->perm_addr);
7341
Hayes Wang92fc43b2011-07-06 15:58:03 +08007342 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007343
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007344 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02007345 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007346 rtl_wol_suspend_quirk(tp);
7347 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007348 }
7349
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007350 pci_wake_from_d3(pdev, true);
7351 pci_set_power_state(pdev, PCI_D3hot);
7352 }
7353}
Francois Romieu5d06a992006-02-23 00:47:58 +01007354
Bill Pembertonbaf63292012-12-03 09:23:28 -05007355static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007356{
7357 struct net_device *dev = pci_get_drvdata(pdev);
7358 struct rtl8169_private *tp = netdev_priv(dev);
7359
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007360 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007361 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007362
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007363 netif_napi_del(&tp->napi);
7364
Francois Romieue27566e2012-03-08 09:54:01 +01007365 unregister_netdev(dev);
7366
7367 rtl_release_firmware(tp);
7368
7369 if (pci_dev_run_wake(pdev))
7370 pm_runtime_get_noresume(&pdev->dev);
7371
7372 /* restore original MAC address */
7373 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007374}
7375
Francois Romieufa9c3852012-03-08 10:01:50 +01007376static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007377 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007378 .ndo_stop = rtl8169_close,
7379 .ndo_get_stats64 = rtl8169_get_stats64,
7380 .ndo_start_xmit = rtl8169_start_xmit,
7381 .ndo_tx_timeout = rtl8169_tx_timeout,
7382 .ndo_validate_addr = eth_validate_addr,
7383 .ndo_change_mtu = rtl8169_change_mtu,
7384 .ndo_fix_features = rtl8169_fix_features,
7385 .ndo_set_features = rtl8169_set_features,
7386 .ndo_set_mac_address = rtl_set_mac_address,
7387 .ndo_do_ioctl = rtl8169_ioctl,
7388 .ndo_set_rx_mode = rtl_set_rx_mode,
7389#ifdef CONFIG_NET_POLL_CONTROLLER
7390 .ndo_poll_controller = rtl8169_netpoll,
7391#endif
7392
7393};
7394
Francois Romieu31fa8b12012-03-08 10:09:40 +01007395static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007396 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007397 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007398 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007399 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007400 u8 default_ver;
7401} rtl_cfg_infos [] = {
7402 [RTL_CFG_0] = {
7403 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007404 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007405 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007406 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007407 .default_ver = RTL_GIGA_MAC_VER_01,
7408 },
7409 [RTL_CFG_1] = {
7410 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007411 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007412 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007413 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007414 .default_ver = RTL_GIGA_MAC_VER_11,
7415 },
7416 [RTL_CFG_2] = {
7417 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007418 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7419 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007420 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007421 .default_ver = RTL_GIGA_MAC_VER_13,
7422 }
7423};
7424
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007425static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007426{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007427 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007428
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007429 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007430 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7431 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7432 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007433 flags = PCI_IRQ_LEGACY;
7434 } else {
7435 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007436 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007437
7438 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007439}
7440
Hayes Wangc5583862012-07-02 17:23:22 +08007441DECLARE_RTL_COND(rtl_link_list_ready_cond)
7442{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007443 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007444}
7445
7446DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7447{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007448 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007449}
7450
Bill Pembertonbaf63292012-12-03 09:23:28 -05007451static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007452{
Hayes Wangc5583862012-07-02 17:23:22 +08007453 u32 data;
7454
7455 tp->ocp_base = OCP_STD_PHY_BASE;
7456
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007457 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007458
7459 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7460 return;
7461
7462 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7463 return;
7464
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007465 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007466 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007467 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007468
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007469 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007470 data &= ~(1 << 14);
7471 r8168_mac_ocp_write(tp, 0xe8de, data);
7472
7473 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7474 return;
7475
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007476 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007477 data |= (1 << 15);
7478 r8168_mac_ocp_write(tp, 0xe8de, data);
7479
7480 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7481 return;
7482}
7483
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007484static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7485{
7486 rtl8168ep_stop_cmac(tp);
7487 rtl_hw_init_8168g(tp);
7488}
7489
Bill Pembertonbaf63292012-12-03 09:23:28 -05007490static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007491{
7492 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007493 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007494 rtl_hw_init_8168g(tp);
7495 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007496 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007497 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007498 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007499 default:
7500 break;
7501 }
7502}
7503
hayeswang929a0312014-09-16 11:40:47 +08007504static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007505{
7506 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007507 struct rtl8169_private *tp;
7508 struct mii_if_info *mii;
7509 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007510 int chipset, region, i;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007511 int rc;
7512
7513 if (netif_msg_drv(&debug)) {
7514 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7515 MODULENAME, RTL8169_VERSION);
7516 }
7517
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007518 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7519 if (!dev)
7520 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007521
7522 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007523 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007524 tp = netdev_priv(dev);
7525 tp->dev = dev;
7526 tp->pci_dev = pdev;
7527 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7528
7529 mii = &tp->mii;
7530 mii->dev = dev;
7531 mii->mdio_read = rtl_mdio_read;
7532 mii->mdio_write = rtl_mdio_write;
7533 mii->phy_id_mask = 0x1f;
7534 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007535 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007536
Francois Romieu3b6cf252012-03-08 09:59:04 +01007537 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007538 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007539 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007540 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007541 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007542 }
7543
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007544 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007545 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007546
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007547 /* use first MMIO region */
7548 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7549 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007550 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007551 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007552 }
7553
7554 /* check for weird/broken PCI region reporting */
7555 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007556 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007557 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007558 }
7559
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007560 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007561 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007562 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007563 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007564 }
7565
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007566 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007567
7568 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007569 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007570
7571 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007572 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007573
Heiner Kallweite3972862018-06-29 08:07:04 +02007574 if (rtl_tbi_enabled(tp)) {
7575 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7576 return -ENODEV;
7577 }
7578
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007579 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007580
7581 if ((sizeof(dma_addr_t) > 4) &&
7582 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7583 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007584 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7585 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007586
7587 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7588 if (!pci_is_pcie(pdev))
7589 tp->cp_cmd |= PCIDAC;
7590 dev->features |= NETIF_F_HIGHDMA;
7591 } else {
7592 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7593 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007594 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007595 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007596 }
7597 }
7598
Francois Romieu3b6cf252012-03-08 09:59:04 +01007599 rtl_init_rxcfg(tp);
7600
7601 rtl_irq_disable(tp);
7602
Hayes Wangc5583862012-07-02 17:23:22 +08007603 rtl_hw_initialize(tp);
7604
Francois Romieu3b6cf252012-03-08 09:59:04 +01007605 rtl_hw_reset(tp);
7606
7607 rtl_ack_events(tp, 0xffff);
7608
7609 pci_set_master(pdev);
7610
Francois Romieu3b6cf252012-03-08 09:59:04 +01007611 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007612 rtl_init_jumbo_ops(tp);
7613
7614 rtl8169_print_mac_version(tp);
7615
7616 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007617
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007618 rc = rtl_alloc_irq(tp);
7619 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007620 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007621 return rc;
7622 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007623
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007624 /* override BIOS settings, use userspace tools to enable WOL */
7625 __rtl8169_set_wol(tp, 0);
7626
Francois Romieu3b6cf252012-03-08 09:59:04 +01007627 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007628 u64_stats_init(&tp->rx_stats.syncp);
7629 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007630
7631 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007632 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007633 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007634 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7635 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007636 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007637 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007638
Heiner Kallweit353af852018-05-02 21:39:59 +02007639 if (is_valid_ether_addr(mac_addr))
7640 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007641 break;
7642 default:
7643 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007644 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007645 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007646 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007647
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007648 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007649 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007650
Heiner Kallweit37621492018-04-17 23:20:03 +02007651 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007652
7653 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7654 * properly for all devices */
7655 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007656 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007657
7658 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007659 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7660 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007661 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7662 NETIF_F_HIGHDMA;
7663
hayeswang929a0312014-09-16 11:40:47 +08007664 tp->cp_cmd |= RxChkSum | RxVlan;
7665
7666 /*
7667 * Pretend we are using VLANs; This bypasses a nasty bug where
7668 * Interrupts stop flowing on high load on 8110SCd controllers.
7669 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007670 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007671 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007672 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007673
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007674 switch (rtl_chip_infos[chipset].txd_version) {
7675 case RTL_TD_0:
hayeswang5888d3f2014-07-11 16:25:56 +08007676 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007677 break;
7678 case RTL_TD_1:
hayeswang5888d3f2014-07-11 16:25:56 +08007679 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007680 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007681 break;
7682 default:
hayeswang5888d3f2014-07-11 16:25:56 +08007683 WARN_ON_ONCE(1);
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007684 }
hayeswang5888d3f2014-07-11 16:25:56 +08007685
Francois Romieu3b6cf252012-03-08 09:59:04 +01007686 dev->hw_features |= NETIF_F_RXALL;
7687 dev->hw_features |= NETIF_F_RXFCS;
7688
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007689 /* MTU range: 60 - hw-specific max */
7690 dev->min_mtu = ETH_ZLEN;
7691 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7692
Francois Romieu3b6cf252012-03-08 09:59:04 +01007693 tp->hw_start = cfg->hw_start;
7694 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007695 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007696
Kees Cook9de36cc2017-10-25 03:53:12 -07007697 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007698
7699 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7700
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007701 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7702 &tp->counters_phys_addr,
7703 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007704 if (!tp->counters)
7705 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007706
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007707 pci_set_drvdata(pdev, dev);
7708
Francois Romieu3b6cf252012-03-08 09:59:04 +01007709 rc = register_netdev(dev);
7710 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007711 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007712
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007713 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7714 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007715 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007716 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007717 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7718 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7719 "tx checksumming: %s]\n",
7720 rtl_chip_infos[chipset].jumbo_max,
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02007721 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007722 }
7723
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007724 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007725 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007726
Francois Romieu3b6cf252012-03-08 09:59:04 +01007727 netif_carrier_off(dev);
7728
Heiner Kallweita92a0842018-01-08 21:39:13 +01007729 if (pci_dev_run_wake(pdev))
7730 pm_runtime_put_sync(&pdev->dev);
7731
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007732 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007733}
7734
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735static struct pci_driver rtl8169_pci_driver = {
7736 .name = MODULENAME,
7737 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007738 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007739 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007740 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007741 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742};
7743
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007744module_pci_driver(rtl8169_pci_driver);