blob: 8c303617cc37897c4b4c807a1db4074c6e1093ef [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>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Michal Schmidtaee77e42012-09-09 13:55:26 +000087#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
89
90#define R8169_REGS_SIZE 256
91#define R8169_NAPI_WEIGHT 64
92#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000093#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
95#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
96
97#define RTL8169_TX_TIMEOUT (6*HZ)
98#define RTL8169_PHY_TIMEOUT (10*HZ)
99
100/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200101#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
102#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
103#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
104#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
105#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
106#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200109 RTL_GIGA_MAC_VER_01 = 0,
110 RTL_GIGA_MAC_VER_02,
111 RTL_GIGA_MAC_VER_03,
112 RTL_GIGA_MAC_VER_04,
113 RTL_GIGA_MAC_VER_05,
114 RTL_GIGA_MAC_VER_06,
115 RTL_GIGA_MAC_VER_07,
116 RTL_GIGA_MAC_VER_08,
117 RTL_GIGA_MAC_VER_09,
118 RTL_GIGA_MAC_VER_10,
119 RTL_GIGA_MAC_VER_11,
120 RTL_GIGA_MAC_VER_12,
121 RTL_GIGA_MAC_VER_13,
122 RTL_GIGA_MAC_VER_14,
123 RTL_GIGA_MAC_VER_15,
124 RTL_GIGA_MAC_VER_16,
125 RTL_GIGA_MAC_VER_17,
126 RTL_GIGA_MAC_VER_18,
127 RTL_GIGA_MAC_VER_19,
128 RTL_GIGA_MAC_VER_20,
129 RTL_GIGA_MAC_VER_21,
130 RTL_GIGA_MAC_VER_22,
131 RTL_GIGA_MAC_VER_23,
132 RTL_GIGA_MAC_VER_24,
133 RTL_GIGA_MAC_VER_25,
134 RTL_GIGA_MAC_VER_26,
135 RTL_GIGA_MAC_VER_27,
136 RTL_GIGA_MAC_VER_28,
137 RTL_GIGA_MAC_VER_29,
138 RTL_GIGA_MAC_VER_30,
139 RTL_GIGA_MAC_VER_31,
140 RTL_GIGA_MAC_VER_32,
141 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800142 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800143 RTL_GIGA_MAC_VER_35,
144 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800145 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800146 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800147 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800148 RTL_GIGA_MAC_VER_40,
149 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000150 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000151 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800152 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800153 RTL_GIGA_MAC_VER_45,
154 RTL_GIGA_MAC_VER_46,
155 RTL_GIGA_MAC_VER_47,
156 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800157 RTL_GIGA_MAC_VER_49,
158 RTL_GIGA_MAC_VER_50,
159 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200160 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Francois Romieu2b7b4312011-04-18 22:53:24 -0700163enum rtl_tx_desc_version {
164 RTL_TD_0 = 0,
165 RTL_TD_1 = 1,
166};
167
Francois Romieud58d46b2011-05-03 16:38:29 +0200168#define JUMBO_1K ETH_DATA_LEN
169#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
170#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
171#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
172#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
173
174#define _R(NAME,TD,FW,SZ,B) { \
175 .name = NAME, \
176 .txd_version = TD, \
177 .fw_name = FW, \
178 .jumbo_max = SZ, \
179 .jumbo_tx_csum = B \
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800182static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700184 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200186 u16 jumbo_max;
187 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200188} rtl_chip_infos[] = {
189 /* PCI devices. */
190 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 /* PCI-E devices. */
203 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200222 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800224 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200232 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200240 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
241 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200242 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200243 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
244 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200246 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200247 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200249 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
251 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200252 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200253 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
254 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200255 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200256 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200257 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200258 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
259 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200260 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200261 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
262 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800263 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200264 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
265 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800266 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200267 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
268 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800269 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200270 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
271 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800272 [RTL_GIGA_MAC_VER_37] =
273 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
274 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800275 [RTL_GIGA_MAC_VER_38] =
276 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
277 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800278 [RTL_GIGA_MAC_VER_39] =
279 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
280 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800281 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000282 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800283 JUMBO_9K, false),
284 [RTL_GIGA_MAC_VER_41] =
285 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000286 [RTL_GIGA_MAC_VER_42] =
287 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
288 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000289 [RTL_GIGA_MAC_VER_43] =
290 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
291 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800292 [RTL_GIGA_MAC_VER_44] =
293 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
294 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800295 [RTL_GIGA_MAC_VER_45] =
296 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
297 JUMBO_9K, false),
298 [RTL_GIGA_MAC_VER_46] =
299 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
300 JUMBO_9K, false),
301 [RTL_GIGA_MAC_VER_47] =
302 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
303 JUMBO_1K, false),
304 [RTL_GIGA_MAC_VER_48] =
305 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
306 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800307 [RTL_GIGA_MAC_VER_49] =
308 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
309 JUMBO_9K, false),
310 [RTL_GIGA_MAC_VER_50] =
311 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
312 JUMBO_9K, false),
313 [RTL_GIGA_MAC_VER_51] =
314 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
315 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317#undef _R
318
Francois Romieubcf0bf92006-07-26 23:14:13 +0200319enum cfg_version {
320 RTL_CFG_0 = 0x00,
321 RTL_CFG_1,
322 RTL_CFG_2
323};
324
Benoit Taine9baa3c32014-08-08 15:56:03 +0200325static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200326 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200332 { PCI_VENDOR_ID_DLINK, 0x4300,
333 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200334 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200336 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200337 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
338 { PCI_VENDOR_ID_LINKSYS, 0x1032,
339 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100340 { 0x0001, 0x8168,
341 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 {0,},
343};
344
345MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
346
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000347static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200348static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200349static struct {
350 u32 msg_enable;
351} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Francois Romieu07d3f512007-02-21 22:40:46 +0100353enum rtl_registers {
354 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100355 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 MAR0 = 8, /* Multicast filter. */
357 CounterAddrLow = 0x10,
358 CounterAddrHigh = 0x14,
359 TxDescStartAddrLow = 0x20,
360 TxDescStartAddrHigh = 0x24,
361 TxHDescStartAddrLow = 0x28,
362 TxHDescStartAddrHigh = 0x2c,
363 FLASH = 0x30,
364 ERSR = 0x36,
365 ChipCmd = 0x37,
366 TxPoll = 0x38,
367 IntrMask = 0x3c,
368 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700369
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800370 TxConfig = 0x40,
371#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
372#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
373
374 RxConfig = 0x44,
375#define RX128_INT_EN (1 << 15) /* 8111c and later */
376#define RX_MULTI_EN (1 << 14) /* 8111c only */
377#define RXCFG_FIFO_SHIFT 13
378 /* No threshold before first PCI xfer */
379#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000380#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800381#define RXCFG_DMA_SHIFT 8
382 /* Unlimited maximum PCI burst. */
383#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700384
Francois Romieu07d3f512007-02-21 22:40:46 +0100385 RxMissed = 0x4c,
386 Cfg9346 = 0x50,
387 Config0 = 0x51,
388 Config1 = 0x52,
389 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200390#define PME_SIGNAL (1 << 5) /* 8168c and later */
391
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 Config3 = 0x54,
393 Config4 = 0x55,
394 Config5 = 0x56,
395 MultiIntr = 0x5c,
396 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100397 PHYstatus = 0x6c,
398 RxMaxSize = 0xda,
399 CPlusCmd = 0xe0,
400 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300401
402#define RTL_COALESCE_MASK 0x0f
403#define RTL_COALESCE_SHIFT 4
404#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
405#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
406
Francois Romieu07d3f512007-02-21 22:40:46 +0100407 RxDescAddrLow = 0xe4,
408 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000409 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
410
411#define NoEarlyTx 0x3f /* Max value : no early transmit. */
412
413 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
414
415#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800416#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000417
Francois Romieu07d3f512007-02-21 22:40:46 +0100418 FuncEvent = 0xf0,
419 FuncEventMask = 0xf4,
420 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800421 IBCR0 = 0xf8,
422 IBCR2 = 0xf9,
423 IBIMR0 = 0xfa,
424 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100425 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426};
427
Francois Romieuf162a5d2008-06-01 22:37:49 +0200428enum rtl8110_registers {
429 TBICSR = 0x64,
430 TBI_ANAR = 0x68,
431 TBI_LPAR = 0x6a,
432};
433
434enum rtl8168_8101_registers {
435 CSIDR = 0x64,
436 CSIAR = 0x68,
437#define CSIAR_FLAG 0x80000000
438#define CSIAR_WRITE_CMD 0x80000000
439#define CSIAR_BYTE_ENABLE 0x0f
440#define CSIAR_BYTE_ENABLE_SHIFT 12
441#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800442#define CSIAR_FUNC_CARD 0x00000000
443#define CSIAR_FUNC_SDIO 0x00010000
444#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800445#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000446 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200447 EPHYAR = 0x80,
448#define EPHYAR_FLAG 0x80000000
449#define EPHYAR_WRITE_CMD 0x80000000
450#define EPHYAR_REG_MASK 0x1f
451#define EPHYAR_REG_SHIFT 16
452#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800453 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800454#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800455#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200456 DBG_REG = 0xd1,
457#define FIX_NAK_1 (1 << 4)
458#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800459 TWSI = 0xd2,
460 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800461#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800462#define TX_EMPTY (1 << 5)
463#define RX_EMPTY (1 << 4)
464#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800465#define EN_NDP (1 << 3)
466#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800467#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000468 EFUSEAR = 0xdc,
469#define EFUSEAR_FLAG 0x80000000
470#define EFUSEAR_WRITE_CMD 0x80000000
471#define EFUSEAR_READ_CMD 0x00000000
472#define EFUSEAR_REG_MASK 0x03ff
473#define EFUSEAR_REG_SHIFT 8
474#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800475 MISC_1 = 0xf2,
476#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200477};
478
françois romieuc0e45c12011-01-03 15:08:04 +0000479enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800480 LED_FREQ = 0x1a,
481 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000482 ERIDR = 0x70,
483 ERIAR = 0x74,
484#define ERIAR_FLAG 0x80000000
485#define ERIAR_WRITE_CMD 0x80000000
486#define ERIAR_READ_CMD 0x00000000
487#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000488#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800489#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
490#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
491#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800492#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800493#define ERIAR_MASK_SHIFT 12
494#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
495#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800496#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800497#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800498#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000499 EPHY_RXER_NUM = 0x7c,
500 OCPDR = 0xb0, /* OCP GPHY access */
501#define OCPDR_WRITE_CMD 0x80000000
502#define OCPDR_READ_CMD 0x00000000
503#define OCPDR_REG_MASK 0x7f
504#define OCPDR_GPHY_REG_SHIFT 16
505#define OCPDR_DATA_MASK 0xffff
506 OCPAR = 0xb4,
507#define OCPAR_FLAG 0x80000000
508#define OCPAR_GPHY_WRITE_CMD 0x8000f060
509#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800510 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000511 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
512 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200513#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800514#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800515#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800516#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800517#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000518};
519
Francois Romieu07d3f512007-02-21 22:40:46 +0100520enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100522 SYSErr = 0x8000,
523 PCSTimeout = 0x4000,
524 SWInt = 0x0100,
525 TxDescUnavail = 0x0080,
526 RxFIFOOver = 0x0040,
527 LinkChg = 0x0020,
528 RxOverflow = 0x0010,
529 TxErr = 0x0008,
530 TxOK = 0x0004,
531 RxErr = 0x0002,
532 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400535 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200536 RxFOVF = (1 << 23),
537 RxRWT = (1 << 22),
538 RxRES = (1 << 21),
539 RxRUNT = (1 << 20),
540 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800543 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 CmdReset = 0x10,
545 CmdRxEnb = 0x08,
546 CmdTxEnb = 0x04,
547 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Francois Romieu275391a2007-02-23 23:50:28 +0100549 /* TXPoll register p.5 */
550 HPQ = 0x80, /* Poll cmd on the high prio queue */
551 NPQ = 0x40, /* Poll cmd on the low prio queue */
552 FSWInt = 0x01, /* Forced software interrupt */
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100555 Cfg9346_Lock = 0x00,
556 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100559 AcceptErr = 0x20,
560 AcceptRunt = 0x10,
561 AcceptBroadcast = 0x08,
562 AcceptMulticast = 0x04,
563 AcceptMyPhys = 0x02,
564 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200565#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* TxConfigBits */
568 TxInterFrameGapShift = 24,
569 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
570
Francois Romieu5d06a992006-02-23 00:47:58 +0100571 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200572 LEDS1 = (1 << 7),
573 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200574 Speed_down = (1 << 4),
575 MEMMAP = (1 << 3),
576 IOMAP = (1 << 2),
577 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100578 PMEnable = (1 << 0), /* Power Management Enable */
579
Francois Romieu6dccd162007-02-13 23:38:05 +0100580 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000581 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000582 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100583 PCI_Clock_66MHz = 0x01,
584 PCI_Clock_33MHz = 0x00,
585
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100586 /* Config3 register p.25 */
587 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
588 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200589 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800590 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200591 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100592
Francois Romieud58d46b2011-05-03 16:38:29 +0200593 /* Config4 register */
594 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
595
Francois Romieu5d06a992006-02-23 00:47:58 +0100596 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100597 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
598 MWF = (1 << 5), /* Accept Multicast wakeup frame */
599 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200600 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100601 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100602 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000603 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* TBICSR p.28 */
606 TBIReset = 0x80000000,
607 TBILoopback = 0x40000000,
608 TBINwEnable = 0x20000000,
609 TBINwRestart = 0x10000000,
610 TBILinkOk = 0x02000000,
611 TBINwComplete = 0x01000000,
612
613 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200614 EnableBist = (1 << 15), // 8168 8101
615 Mac_dbgo_oe = (1 << 14), // 8168 8101
616 Normal_mode = (1 << 13), // unused
617 Force_half_dup = (1 << 12), // 8168 8101
618 Force_rxflow_en = (1 << 11), // 8168 8101
619 Force_txflow_en = (1 << 10), // 8168 8101
620 Cxpl_dbg_sel = (1 << 9), // 8168 8101
621 ASF = (1 << 8), // 8168 8101
622 PktCntrDisable = (1 << 7), // 8168 8101
623 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 RxVlan = (1 << 6),
625 RxChkSum = (1 << 5),
626 PCIDAC = (1 << 4),
627 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100628 INTT_0 = 0x0000, // 8168
629 INTT_1 = 0x0001, // 8168
630 INTT_2 = 0x0002, // 8168
631 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100634 TBI_Enable = 0x80,
635 TxFlowCtrl = 0x40,
636 RxFlowCtrl = 0x20,
637 _1000bpsF = 0x10,
638 _100bps = 0x08,
639 _10bps = 0x04,
640 LinkStatus = 0x02,
641 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100644 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200645
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200646 /* ResetCounterCommand */
647 CounterReset = 0x1,
648
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200649 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100650 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800651
652 /* magic enable v2 */
653 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654};
655
Francois Romieu2b7b4312011-04-18 22:53:24 -0700656enum rtl_desc_bit {
657 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
659 RingEnd = (1 << 30), /* End of descriptor ring */
660 FirstFrag = (1 << 29), /* First segment of a packet */
661 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700662};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Francois Romieu2b7b4312011-04-18 22:53:24 -0700664/* Generic case. */
665enum rtl_tx_desc_bit {
666 /* First doubleword. */
667 TD_LSO = (1 << 27), /* Large Send Offload */
668#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Francois Romieu2b7b4312011-04-18 22:53:24 -0700670 /* Second doubleword. */
671 TxVlanTag = (1 << 17), /* Add VLAN tag */
672};
673
674/* 8169, 8168b and 810x except 8102e. */
675enum rtl_tx_desc_bit_0 {
676 /* First doubleword. */
677#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
678 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
679 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
680 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
681};
682
683/* 8102e, 8168c and beyond. */
684enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800685 /* First doubleword. */
686 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800687 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800688#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800689#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800690
Francois Romieu2b7b4312011-04-18 22:53:24 -0700691 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800692#define TCPHO_SHIFT 18
693#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700694#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800695 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
696 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700697 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
698 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
699};
700
Francois Romieu2b7b4312011-04-18 22:53:24 -0700701enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Rx private */
703 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500704 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706#define RxProtoUDP (PID1)
707#define RxProtoTCP (PID0)
708#define RxProtoIP (PID1 | PID0)
709#define RxProtoMask RxProtoIP
710
711 IPFail = (1 << 16), /* IP checksum failed */
712 UDPFail = (1 << 15), /* UDP/IP checksum failed */
713 TCPFail = (1 << 14), /* TCP/IP checksum failed */
714 RxVlanTag = (1 << 16), /* VLAN tag available */
715};
716
717#define RsvdMask 0x3fffc000
718
719struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200720 __le32 opts1;
721 __le32 opts2;
722 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723};
724
725struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200726 __le32 opts1;
727 __le32 opts2;
728 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729};
730
731struct ring_info {
732 struct sk_buff *skb;
733 u32 len;
734 u8 __pad[sizeof(void *) - sizeof(u32)];
735};
736
Ivan Vecera355423d2009-02-06 21:49:57 -0800737struct rtl8169_counters {
738 __le64 tx_packets;
739 __le64 rx_packets;
740 __le64 tx_errors;
741 __le32 rx_errors;
742 __le16 rx_missed;
743 __le16 align_errors;
744 __le32 tx_one_collision;
745 __le32 tx_multi_collision;
746 __le64 rx_unicast;
747 __le64 rx_broadcast;
748 __le32 rx_multicast;
749 __le16 tx_aborted;
750 __le16 tx_underun;
751};
752
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200753struct rtl8169_tc_offsets {
754 bool inited;
755 __le64 tx_errors;
756 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200757 __le16 tx_aborted;
758};
759
Francois Romieuda78dbf2012-01-26 14:18:23 +0100760enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100761 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100762 RTL_FLAG_TASK_SLOW_PENDING,
763 RTL_FLAG_TASK_RESET_PENDING,
764 RTL_FLAG_TASK_PHY_PENDING,
765 RTL_FLAG_MAX
766};
767
Junchang Wang8027aa22012-03-04 23:30:32 +0100768struct rtl8169_stats {
769 u64 packets;
770 u64 bytes;
771 struct u64_stats_sync syncp;
772};
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774struct rtl8169_private {
775 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200776 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000777 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700778 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200779 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700780 u16 txd_version;
781 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
783 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100785 struct rtl8169_stats rx_stats;
786 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
788 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
789 dma_addr_t TxPhyAddr;
790 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000791 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct timer_list timer;
794 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100795
796 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300797 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000798
799 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200800 void (*write)(struct rtl8169_private *, int, int);
801 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000802 } mdio_ops;
803
françois romieu065c27c2011-01-03 15:08:12 +0000804 struct pll_power_ops {
805 void (*down)(struct rtl8169_private *);
806 void (*up)(struct rtl8169_private *);
807 } pll_power_ops;
808
Francois Romieud58d46b2011-05-03 16:38:29 +0200809 struct jumbo_ops {
810 void (*enable)(struct rtl8169_private *);
811 void (*disable)(struct rtl8169_private *);
812 } jumbo_ops;
813
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800814 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200815 void (*write)(struct rtl8169_private *, int, int);
816 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800817 } csi_ops;
818
Oliver Neukum54405cd2011-01-06 21:55:13 +0100819 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100820 int (*get_link_ksettings)(struct net_device *,
821 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000822 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100823 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000824 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200825 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800826 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800827 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100828
829 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100830 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
831 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100832 struct work_struct work;
833 } wk;
834
Francois Romieuccdffb92008-07-26 14:26:06 +0200835 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200836 dma_addr_t counters_phys_addr;
837 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200838 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000839 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400840 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000841
Francois Romieub6ffd972011-06-17 17:00:05 +0200842 struct rtl_fw {
843 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200844
845#define RTL_VER_SIZE 32
846
847 char version[RTL_VER_SIZE];
848
849 struct rtl_fw_phy_action {
850 __le32 *code;
851 size_t size;
852 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200853 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300854#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800855
856 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
Ralf Baechle979b6c12005-06-13 14:30:40 -0700859MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700862MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200863module_param_named(debug, debug.msg_enable, int, 0);
864MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865MODULE_LICENSE("GPL");
866MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000867MODULE_FIRMWARE(FIRMWARE_8168D_1);
868MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000869MODULE_FIRMWARE(FIRMWARE_8168E_1);
870MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400871MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800872MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800873MODULE_FIRMWARE(FIRMWARE_8168F_1);
874MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800875MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800876MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800877MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800878MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000879MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000880MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000881MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800882MODULE_FIRMWARE(FIRMWARE_8168H_1);
883MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200884MODULE_FIRMWARE(FIRMWARE_8107E_1);
885MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100887static inline struct device *tp_to_dev(struct rtl8169_private *tp)
888{
889 return &tp->pci_dev->dev;
890}
891
Francois Romieuda78dbf2012-01-26 14:18:23 +0100892static void rtl_lock_work(struct rtl8169_private *tp)
893{
894 mutex_lock(&tp->wk.mutex);
895}
896
897static void rtl_unlock_work(struct rtl8169_private *tp)
898{
899 mutex_unlock(&tp->wk.mutex);
900}
901
Heiner Kallweitcb732002018-03-20 07:45:35 +0100902static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200903{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100904 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800905 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200906}
907
Francois Romieuffc46952012-07-06 14:19:23 +0200908struct rtl_cond {
909 bool (*check)(struct rtl8169_private *);
910 const char *msg;
911};
912
913static void rtl_udelay(unsigned int d)
914{
915 udelay(d);
916}
917
918static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
919 void (*delay)(unsigned int), unsigned int d, int n,
920 bool high)
921{
922 int i;
923
924 for (i = 0; i < n; i++) {
925 delay(d);
926 if (c->check(tp) == high)
927 return true;
928 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200929 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
930 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200931 return false;
932}
933
934static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
935 const struct rtl_cond *c,
936 unsigned int d, int n)
937{
938 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
939}
940
941static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
942 const struct rtl_cond *c,
943 unsigned int d, int n)
944{
945 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
946}
947
948static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
949 const struct rtl_cond *c,
950 unsigned int d, int n)
951{
952 return rtl_loop_wait(tp, c, msleep, d, n, true);
953}
954
955static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
956 const struct rtl_cond *c,
957 unsigned int d, int n)
958{
959 return rtl_loop_wait(tp, c, msleep, d, n, false);
960}
961
962#define DECLARE_RTL_COND(name) \
963static bool name ## _check(struct rtl8169_private *); \
964 \
965static const struct rtl_cond name = { \
966 .check = name ## _check, \
967 .msg = #name \
968}; \
969 \
970static bool name ## _check(struct rtl8169_private *tp)
971
Hayes Wangc5583862012-07-02 17:23:22 +0800972static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
973{
974 if (reg & 0xffff0001) {
975 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
976 return true;
977 }
978 return false;
979}
980
981DECLARE_RTL_COND(rtl_ocp_gphy_cond)
982{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200983 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800984}
985
986static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
987{
Hayes Wangc5583862012-07-02 17:23:22 +0800988 if (rtl_ocp_reg_failure(tp, reg))
989 return;
990
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800992
993 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
994}
995
996static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
997{
Hayes Wangc5583862012-07-02 17:23:22 +0800998 if (rtl_ocp_reg_failure(tp, reg))
999 return 0;
1000
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001001 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001002
1003 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001004 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +08001005}
1006
Hayes Wangc5583862012-07-02 17:23:22 +08001007static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1008{
Hayes Wangc5583862012-07-02 17:23:22 +08001009 if (rtl_ocp_reg_failure(tp, reg))
1010 return;
1011
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001012 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001013}
1014
1015static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1016{
Hayes Wangc5583862012-07-02 17:23:22 +08001017 if (rtl_ocp_reg_failure(tp, reg))
1018 return 0;
1019
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001020 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001021
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001022 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001023}
1024
1025#define OCP_STD_PHY_BASE 0xa400
1026
1027static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1028{
1029 if (reg == 0x1f) {
1030 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1031 return;
1032 }
1033
1034 if (tp->ocp_base != OCP_STD_PHY_BASE)
1035 reg -= 0x10;
1036
1037 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1038}
1039
1040static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1041{
1042 if (tp->ocp_base != OCP_STD_PHY_BASE)
1043 reg -= 0x10;
1044
1045 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1046}
1047
hayeswangeee37862013-04-01 22:23:38 +00001048static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1049{
1050 if (reg == 0x1f) {
1051 tp->ocp_base = value << 4;
1052 return;
1053 }
1054
1055 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1056}
1057
1058static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1059{
1060 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1061}
1062
Francois Romieuffc46952012-07-06 14:19:23 +02001063DECLARE_RTL_COND(rtl_phyar_cond)
1064{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001065 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001066}
1067
Francois Romieu24192212012-07-06 20:19:42 +02001068static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001070 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Francois Romieuffc46952012-07-06 14:19:23 +02001072 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001073 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001074 * According to hardware specs a 20us delay is required after write
1075 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001076 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001077 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Francois Romieu24192212012-07-06 20:19:42 +02001080static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Francois Romieuffc46952012-07-06 14:19:23 +02001082 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001084 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Francois Romieuffc46952012-07-06 14:19:23 +02001086 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001087 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001088
Timo Teräs81a95f02010-06-09 17:31:48 -07001089 /*
1090 * According to hardware specs a 20us delay is required after read
1091 * complete indication, but before sending next command.
1092 */
1093 udelay(20);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return value;
1096}
1097
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001098DECLARE_RTL_COND(rtl_ocpar_cond)
1099{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001100 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001101}
1102
Francois Romieu24192212012-07-06 20:19:42 +02001103static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001104{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001105 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1106 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1107 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001108
Francois Romieuffc46952012-07-06 14:19:23 +02001109 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001110}
1111
Francois Romieu24192212012-07-06 20:19:42 +02001112static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001113{
Francois Romieu24192212012-07-06 20:19:42 +02001114 r8168dp_1_mdio_access(tp, reg,
1115 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001116}
1117
Francois Romieu24192212012-07-06 20:19:42 +02001118static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001119{
Francois Romieu24192212012-07-06 20:19:42 +02001120 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001121
1122 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001123 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1124 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001125
Francois Romieuffc46952012-07-06 14:19:23 +02001126 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001127 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001128}
1129
françois romieue6de30d2011-01-03 15:08:37 +00001130#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1131
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001132static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001133{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001134 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001135}
1136
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001137static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001138{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001139 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001140}
1141
Francois Romieu24192212012-07-06 20:19:42 +02001142static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001143{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001144 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001145
Francois Romieu24192212012-07-06 20:19:42 +02001146 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001147
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001148 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001149}
1150
Francois Romieu24192212012-07-06 20:19:42 +02001151static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001152{
1153 int value;
1154
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001155 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001156
Francois Romieu24192212012-07-06 20:19:42 +02001157 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001158
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001159 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001160
1161 return value;
1162}
1163
françois romieu4da19632011-01-03 15:07:55 +00001164static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001165{
Francois Romieu24192212012-07-06 20:19:42 +02001166 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001167}
1168
françois romieu4da19632011-01-03 15:07:55 +00001169static int rtl_readphy(struct rtl8169_private *tp, int location)
1170{
Francois Romieu24192212012-07-06 20:19:42 +02001171 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001172}
1173
1174static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1175{
1176 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1177}
1178
Chun-Hao Lin76564422014-10-01 23:17:17 +08001179static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001180{
1181 int val;
1182
françois romieu4da19632011-01-03 15:07:55 +00001183 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001184 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001185}
1186
Francois Romieuccdffb92008-07-26 14:26:06 +02001187static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1188 int val)
1189{
1190 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001191
françois romieu4da19632011-01-03 15:07:55 +00001192 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001193}
1194
1195static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1196{
1197 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001198
françois romieu4da19632011-01-03 15:07:55 +00001199 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001200}
1201
Francois Romieuffc46952012-07-06 14:19:23 +02001202DECLARE_RTL_COND(rtl_ephyar_cond)
1203{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001204 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001205}
1206
Francois Romieufdf6fc02012-07-06 22:40:38 +02001207static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001208{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001209 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001210 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1211
Francois Romieuffc46952012-07-06 14:19:23 +02001212 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1213
1214 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001215}
1216
Francois Romieufdf6fc02012-07-06 22:40:38 +02001217static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001218{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001219 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001220
Francois Romieuffc46952012-07-06 14:19:23 +02001221 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001222 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001223}
1224
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001225DECLARE_RTL_COND(rtl_eriar_cond)
1226{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001227 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001228}
1229
Francois Romieufdf6fc02012-07-06 22:40:38 +02001230static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1231 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001232{
Hayes Wang133ac402011-07-06 15:58:05 +08001233 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001234 RTL_W32(tp, ERIDR, val);
1235 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001236
Francois Romieuffc46952012-07-06 14:19:23 +02001237 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001238}
1239
Francois Romieufdf6fc02012-07-06 22:40:38 +02001240static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001241{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001242 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001243
Francois Romieuffc46952012-07-06 14:19:23 +02001244 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001245 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001246}
1247
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001248static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001249 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001250{
1251 u32 val;
1252
Francois Romieufdf6fc02012-07-06 22:40:38 +02001253 val = rtl_eri_read(tp, addr, type);
1254 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001255}
1256
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001257static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1258{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001259 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001260 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001261 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001262}
1263
1264static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1265{
1266 return rtl_eri_read(tp, reg, ERIAR_OOB);
1267}
1268
1269static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1270{
1271 switch (tp->mac_version) {
1272 case RTL_GIGA_MAC_VER_27:
1273 case RTL_GIGA_MAC_VER_28:
1274 case RTL_GIGA_MAC_VER_31:
1275 return r8168dp_ocp_read(tp, mask, reg);
1276 case RTL_GIGA_MAC_VER_49:
1277 case RTL_GIGA_MAC_VER_50:
1278 case RTL_GIGA_MAC_VER_51:
1279 return r8168ep_ocp_read(tp, mask, reg);
1280 default:
1281 BUG();
1282 return ~0;
1283 }
1284}
1285
1286static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1287 u32 data)
1288{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001289 RTL_W32(tp, OCPDR, data);
1290 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001291 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1292}
1293
1294static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1295 u32 data)
1296{
1297 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1298 data, ERIAR_OOB);
1299}
1300
1301static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1302{
1303 switch (tp->mac_version) {
1304 case RTL_GIGA_MAC_VER_27:
1305 case RTL_GIGA_MAC_VER_28:
1306 case RTL_GIGA_MAC_VER_31:
1307 r8168dp_ocp_write(tp, mask, reg, data);
1308 break;
1309 case RTL_GIGA_MAC_VER_49:
1310 case RTL_GIGA_MAC_VER_50:
1311 case RTL_GIGA_MAC_VER_51:
1312 r8168ep_ocp_write(tp, mask, reg, data);
1313 break;
1314 default:
1315 BUG();
1316 break;
1317 }
1318}
1319
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001320static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1321{
1322 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1323
1324 ocp_write(tp, 0x1, 0x30, 0x00000001);
1325}
1326
1327#define OOB_CMD_RESET 0x00
1328#define OOB_CMD_DRIVER_START 0x05
1329#define OOB_CMD_DRIVER_STOP 0x06
1330
1331static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1332{
1333 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1334}
1335
1336DECLARE_RTL_COND(rtl_ocp_read_cond)
1337{
1338 u16 reg;
1339
1340 reg = rtl8168_get_ocp_reg(tp);
1341
1342 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1343}
1344
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001345DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1346{
1347 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1348}
1349
1350DECLARE_RTL_COND(rtl_ocp_tx_cond)
1351{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001352 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001353}
1354
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001355static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1356{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001357 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001358 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001359 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1360 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001361}
1362
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001363static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001364{
1365 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001366 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1367}
1368
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001369static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1370{
1371 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1372 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1373 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1374}
1375
1376static void rtl8168_driver_start(struct rtl8169_private *tp)
1377{
1378 switch (tp->mac_version) {
1379 case RTL_GIGA_MAC_VER_27:
1380 case RTL_GIGA_MAC_VER_28:
1381 case RTL_GIGA_MAC_VER_31:
1382 rtl8168dp_driver_start(tp);
1383 break;
1384 case RTL_GIGA_MAC_VER_49:
1385 case RTL_GIGA_MAC_VER_50:
1386 case RTL_GIGA_MAC_VER_51:
1387 rtl8168ep_driver_start(tp);
1388 break;
1389 default:
1390 BUG();
1391 break;
1392 }
1393}
1394
1395static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1396{
1397 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1398 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1399}
1400
1401static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1402{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001403 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001404 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1405 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1406 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1407}
1408
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001409static void rtl8168_driver_stop(struct rtl8169_private *tp)
1410{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001411 switch (tp->mac_version) {
1412 case RTL_GIGA_MAC_VER_27:
1413 case RTL_GIGA_MAC_VER_28:
1414 case RTL_GIGA_MAC_VER_31:
1415 rtl8168dp_driver_stop(tp);
1416 break;
1417 case RTL_GIGA_MAC_VER_49:
1418 case RTL_GIGA_MAC_VER_50:
1419 case RTL_GIGA_MAC_VER_51:
1420 rtl8168ep_driver_stop(tp);
1421 break;
1422 default:
1423 BUG();
1424 break;
1425 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001426}
1427
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001428static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001429{
1430 u16 reg = rtl8168_get_ocp_reg(tp);
1431
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001432 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001433}
1434
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001435static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001436{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001437 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001438}
1439
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001440static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001441{
1442 switch (tp->mac_version) {
1443 case RTL_GIGA_MAC_VER_27:
1444 case RTL_GIGA_MAC_VER_28:
1445 case RTL_GIGA_MAC_VER_31:
1446 return r8168dp_check_dash(tp);
1447 case RTL_GIGA_MAC_VER_49:
1448 case RTL_GIGA_MAC_VER_50:
1449 case RTL_GIGA_MAC_VER_51:
1450 return r8168ep_check_dash(tp);
1451 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001452 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001453 }
1454}
1455
françois romieuc28aa382011-08-02 03:53:43 +00001456struct exgmac_reg {
1457 u16 addr;
1458 u16 mask;
1459 u32 val;
1460};
1461
Francois Romieufdf6fc02012-07-06 22:40:38 +02001462static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001463 const struct exgmac_reg *r, int len)
1464{
1465 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001466 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001467 r++;
1468 }
1469}
1470
Francois Romieuffc46952012-07-06 14:19:23 +02001471DECLARE_RTL_COND(rtl_efusear_cond)
1472{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001473 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001474}
1475
Francois Romieufdf6fc02012-07-06 22:40:38 +02001476static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001477{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001478 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001479
Francois Romieuffc46952012-07-06 14:19:23 +02001480 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001481 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001482}
1483
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001484static u16 rtl_get_events(struct rtl8169_private *tp)
1485{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001486 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001487}
1488
1489static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1490{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001491 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001492 mmiowb();
1493}
1494
1495static void rtl_irq_disable(struct rtl8169_private *tp)
1496{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001497 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001498 mmiowb();
1499}
1500
Francois Romieu3e990ff2012-01-26 12:50:01 +01001501static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1502{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001503 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001504}
1505
Francois Romieuda78dbf2012-01-26 14:18:23 +01001506#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1507#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1508#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1509
1510static void rtl_irq_enable_all(struct rtl8169_private *tp)
1511{
1512 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1513}
1514
françois romieu811fd302011-12-04 20:30:45 +00001515static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001517 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001518 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001519 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
françois romieu4da19632011-01-03 15:07:55 +00001522static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001524 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
françois romieu4da19632011-01-03 15:07:55 +00001527static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
françois romieu4da19632011-01-03 15:07:55 +00001529 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001532static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001534 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001537static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001539 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
françois romieu4da19632011-01-03 15:07:55 +00001542static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001544 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
françois romieu4da19632011-01-03 15:07:55 +00001547static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 unsigned int val;
1550
françois romieu4da19632011-01-03 15:07:55 +00001551 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1552 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Hayes Wang70090422011-07-06 15:58:06 +08001555static void rtl_link_chg_patch(struct rtl8169_private *tp)
1556{
Hayes Wang70090422011-07-06 15:58:06 +08001557 struct net_device *dev = tp->dev;
1558
1559 if (!netif_running(dev))
1560 return;
1561
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001562 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1563 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001564 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001565 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1566 ERIAR_EXGMAC);
1567 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1568 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001569 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001570 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1571 ERIAR_EXGMAC);
1572 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1573 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001574 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001575 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1576 ERIAR_EXGMAC);
1577 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1578 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001579 }
1580 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001581 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001582 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001583 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001584 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001585 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1586 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001587 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001588 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1589 ERIAR_EXGMAC);
1590 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1591 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001592 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001593 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1594 ERIAR_EXGMAC);
1595 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1596 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001597 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001598 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001599 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001600 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1601 ERIAR_EXGMAC);
1602 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1603 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001604 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001605 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1606 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001607 }
Hayes Wang70090422011-07-06 15:58:06 +08001608 }
1609}
1610
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001611static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001612 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001614 struct device *d = tp_to_dev(tp);
1615
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001616 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001617 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001618 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001619 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001621 if (net_ratelimit())
1622 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001623 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001625 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001626 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001630#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1631
1632static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1633{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001634 u8 options;
1635 u32 wolopts = 0;
1636
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001637 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001638 if (!(options & PMEnable))
1639 return 0;
1640
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001641 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001642 if (options & LinkUp)
1643 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001644 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001645 case RTL_GIGA_MAC_VER_34:
1646 case RTL_GIGA_MAC_VER_35:
1647 case RTL_GIGA_MAC_VER_36:
1648 case RTL_GIGA_MAC_VER_37:
1649 case RTL_GIGA_MAC_VER_38:
1650 case RTL_GIGA_MAC_VER_40:
1651 case RTL_GIGA_MAC_VER_41:
1652 case RTL_GIGA_MAC_VER_42:
1653 case RTL_GIGA_MAC_VER_43:
1654 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001655 case RTL_GIGA_MAC_VER_45:
1656 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001657 case RTL_GIGA_MAC_VER_47:
1658 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001659 case RTL_GIGA_MAC_VER_49:
1660 case RTL_GIGA_MAC_VER_50:
1661 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001662 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1663 wolopts |= WAKE_MAGIC;
1664 break;
1665 default:
1666 if (options & MagicPacket)
1667 wolopts |= WAKE_MAGIC;
1668 break;
1669 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001670
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001671 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001672 if (options & UWF)
1673 wolopts |= WAKE_UCAST;
1674 if (options & BWF)
1675 wolopts |= WAKE_BCAST;
1676 if (options & MWF)
1677 wolopts |= WAKE_MCAST;
1678
1679 return wolopts;
1680}
1681
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001682static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1683{
1684 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001685 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001686
1687 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001688
Francois Romieuda78dbf2012-01-26 14:18:23 +01001689 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001690
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001691 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001692 if (pm_runtime_active(d))
1693 wol->wolopts = __rtl8169_get_wol(tp);
1694 else
1695 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001696
Francois Romieuda78dbf2012-01-26 14:18:23 +01001697 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001698
1699 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001700}
1701
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001702static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001703{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001704 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001705 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001706 u32 opt;
1707 u16 reg;
1708 u8 mask;
1709 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001710 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001711 { WAKE_UCAST, Config5, UWF },
1712 { WAKE_BCAST, Config5, BWF },
1713 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001714 { WAKE_ANY, Config5, LanWake },
1715 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001716 };
Francois Romieu851e6022012-04-17 11:10:11 +02001717 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001718
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001719 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001720
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001721 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001722 case RTL_GIGA_MAC_VER_34:
1723 case RTL_GIGA_MAC_VER_35:
1724 case RTL_GIGA_MAC_VER_36:
1725 case RTL_GIGA_MAC_VER_37:
1726 case RTL_GIGA_MAC_VER_38:
1727 case RTL_GIGA_MAC_VER_40:
1728 case RTL_GIGA_MAC_VER_41:
1729 case RTL_GIGA_MAC_VER_42:
1730 case RTL_GIGA_MAC_VER_43:
1731 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001732 case RTL_GIGA_MAC_VER_45:
1733 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001734 case RTL_GIGA_MAC_VER_47:
1735 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001736 case RTL_GIGA_MAC_VER_49:
1737 case RTL_GIGA_MAC_VER_50:
1738 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001739 tmp = ARRAY_SIZE(cfg) - 1;
1740 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001741 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001742 0x0dc,
1743 ERIAR_MASK_0100,
1744 MagicPacket_v2,
1745 0x0000,
1746 ERIAR_EXGMAC);
1747 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001748 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001749 0x0dc,
1750 ERIAR_MASK_0100,
1751 0x0000,
1752 MagicPacket_v2,
1753 ERIAR_EXGMAC);
1754 break;
1755 default:
1756 tmp = ARRAY_SIZE(cfg);
1757 break;
1758 }
1759
1760 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001761 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001762 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001763 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001764 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001765 }
1766
Francois Romieu851e6022012-04-17 11:10:11 +02001767 switch (tp->mac_version) {
1768 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001769 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001770 if (wolopts)
1771 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001772 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001773 break;
1774 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001775 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001776 if (wolopts)
1777 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001778 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001779 break;
1780 }
1781
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001782 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001783}
1784
1785static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1786{
1787 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001788 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001789
1790 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001791
Francois Romieuda78dbf2012-01-26 14:18:23 +01001792 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001793
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001794 if (pm_runtime_active(d))
1795 __rtl8169_set_wol(tp, wol->wolopts);
1796 else
1797 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001798
1799 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001800
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001801 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001802
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001803 pm_runtime_put_noidle(d);
1804
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001805 return 0;
1806}
1807
Francois Romieu31bd2042011-04-26 18:58:59 +02001808static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1809{
Francois Romieu85bffe62011-04-27 08:22:39 +02001810 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813static void rtl8169_get_drvinfo(struct net_device *dev,
1814 struct ethtool_drvinfo *info)
1815{
1816 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001817 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Rick Jones68aad782011-11-07 13:29:27 +00001819 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1820 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1821 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001822 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001823 if (!IS_ERR_OR_NULL(rtl_fw))
1824 strlcpy(info->fw_version, rtl_fw->version,
1825 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828static int rtl8169_get_regs_len(struct net_device *dev)
1829{
1830 return R8169_REGS_SIZE;
1831}
1832
1833static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001834 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 int ret = 0;
1838 u32 reg;
1839
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001840 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1842 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001843 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001845 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001847 netif_warn(tp, link, dev,
1848 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 ret = -EOPNOTSUPP;
1850 }
1851
1852 return ret;
1853}
1854
1855static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001856 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001859 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001860 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Hayes Wang716b50a2011-02-22 17:26:18 +08001862 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001865 int auto_nego;
1866
françois romieu4da19632011-01-03 15:07:55 +00001867 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001868 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1869 ADVERTISE_100HALF | ADVERTISE_100FULL);
1870
1871 if (adv & ADVERTISED_10baseT_Half)
1872 auto_nego |= ADVERTISE_10HALF;
1873 if (adv & ADVERTISED_10baseT_Full)
1874 auto_nego |= ADVERTISE_10FULL;
1875 if (adv & ADVERTISED_100baseT_Half)
1876 auto_nego |= ADVERTISE_100HALF;
1877 if (adv & ADVERTISED_100baseT_Full)
1878 auto_nego |= ADVERTISE_100FULL;
1879
françois romieu3577aa12009-05-19 10:46:48 +00001880 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1881
françois romieu4da19632011-01-03 15:07:55 +00001882 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001883 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1884
1885 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001886 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001887 if (adv & ADVERTISED_1000baseT_Half)
1888 giga_ctrl |= ADVERTISE_1000HALF;
1889 if (adv & ADVERTISED_1000baseT_Full)
1890 giga_ctrl |= ADVERTISE_1000FULL;
1891 } else if (adv & (ADVERTISED_1000baseT_Half |
1892 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001893 netif_info(tp, link, dev,
1894 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001895 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
françois romieu3577aa12009-05-19 10:46:48 +00001898 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001899
françois romieu4da19632011-01-03 15:07:55 +00001900 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1901 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001902 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001903 if (speed == SPEED_10)
1904 bmcr = 0;
1905 else if (speed == SPEED_100)
1906 bmcr = BMCR_SPEED100;
1907 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001908 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001909
1910 if (duplex == DUPLEX_FULL)
1911 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001912 }
1913
françois romieu4da19632011-01-03 15:07:55 +00001914 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001915
Francois Romieucecb5fd2011-04-01 10:21:07 +02001916 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1917 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001918 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001919 rtl_writephy(tp, 0x17, 0x2138);
1920 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001921 } else {
françois romieu4da19632011-01-03 15:07:55 +00001922 rtl_writephy(tp, 0x17, 0x2108);
1923 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001924 }
1925 }
1926
Oliver Neukum54405cd2011-01-06 21:55:13 +01001927 rc = 0;
1928out:
1929 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
1932static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001933 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct rtl8169_private *tp = netdev_priv(dev);
1936 int ret;
1937
Oliver Neukum54405cd2011-01-06 21:55:13 +01001938 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001939 if (ret < 0)
1940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Francois Romieu4876cc12011-03-11 21:07:11 +01001942 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001943 (advertising & ADVERTISED_1000baseT_Full) &&
1944 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001946 }
1947out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return ret;
1949}
1950
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001951static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1952 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Francois Romieud58d46b2011-05-03 16:38:29 +02001954 struct rtl8169_private *tp = netdev_priv(dev);
1955
Francois Romieu2b7b4312011-04-18 22:53:24 -07001956 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001957 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Francois Romieud58d46b2011-05-03 16:38:29 +02001959 if (dev->mtu > JUMBO_1K &&
1960 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1961 features &= ~NETIF_F_IP_CSUM;
1962
Michał Mirosław350fb322011-04-08 06:35:56 +00001963 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Francois Romieuda78dbf2012-01-26 14:18:23 +01001966static void __rtl8169_set_features(struct net_device *dev,
1967 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001970 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001972 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001973 if (features & NETIF_F_RXALL)
1974 rx_config |= (AcceptErr | AcceptRunt);
1975 else
1976 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001978 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001979
hayeswang929a0312014-09-16 11:40:47 +08001980 if (features & NETIF_F_RXCSUM)
1981 tp->cp_cmd |= RxChkSum;
1982 else
1983 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001984
hayeswang929a0312014-09-16 11:40:47 +08001985 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1986 tp->cp_cmd |= RxVlan;
1987 else
1988 tp->cp_cmd &= ~RxVlan;
1989
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001990 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) & ~(RxVlan | RxChkSum);
hayeswang929a0312014-09-16 11:40:47 +08001991
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001992 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1993 RTL_R16(tp, CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Francois Romieuda78dbf2012-01-26 14:18:23 +01001996static int rtl8169_set_features(struct net_device *dev,
1997 netdev_features_t features)
1998{
1999 struct rtl8169_private *tp = netdev_priv(dev);
2000
hayeswang929a0312014-09-16 11:40:47 +08002001 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2002
Francois Romieuda78dbf2012-01-26 14:18:23 +01002003 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002004 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002005 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002006 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 return 0;
2009}
2010
Francois Romieuda78dbf2012-01-26 14:18:23 +01002011
Kirill Smelkov810f4892012-11-10 21:11:02 +04002012static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002014 return (skb_vlan_tag_present(skb)) ?
2015 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Francois Romieu7a8fc772011-03-01 17:18:33 +01002018static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Francois Romieu7a8fc772011-03-01 17:18:33 +01002022 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002023 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002026static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2027 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002031 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002033 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002035 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002037 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002038 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2039 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002041 cmd->base.speed = SPEED_1000;
2042 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2043
2044 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2045 supported);
2046 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2047 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002048
2049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002052static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2053 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002057 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2058
2059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002062static int rtl8169_get_link_ksettings(struct net_device *dev,
2063 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002066 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Francois Romieuda78dbf2012-01-26 14:18:23 +01002068 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002069 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002070 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Francois Romieuccdffb92008-07-26 14:26:06 +02002072 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002075static int rtl8169_set_link_ksettings(struct net_device *dev,
2076 const struct ethtool_link_ksettings *cmd)
2077{
2078 struct rtl8169_private *tp = netdev_priv(dev);
2079 int rc;
2080 u32 advertising;
2081
2082 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2083 cmd->link_modes.advertising))
2084 return -EINVAL;
2085
2086 del_timer_sync(&tp->timer);
2087
2088 rtl_lock_work(tp);
2089 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2090 cmd->base.duplex, advertising);
2091 rtl_unlock_work(tp);
2092
2093 return rc;
2094}
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2097 void *p)
2098{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002099 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002100 u32 __iomem *data = tp->mmio_addr;
2101 u32 *dw = p;
2102 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Francois Romieuda78dbf2012-01-26 14:18:23 +01002104 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002105 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2106 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002107 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002110static u32 rtl8169_get_msglevel(struct net_device *dev)
2111{
2112 struct rtl8169_private *tp = netdev_priv(dev);
2113
2114 return tp->msg_enable;
2115}
2116
2117static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2118{
2119 struct rtl8169_private *tp = netdev_priv(dev);
2120
2121 tp->msg_enable = value;
2122}
2123
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002124static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2125 "tx_packets",
2126 "rx_packets",
2127 "tx_errors",
2128 "rx_errors",
2129 "rx_missed",
2130 "align_errors",
2131 "tx_single_collisions",
2132 "tx_multi_collisions",
2133 "unicast",
2134 "broadcast",
2135 "multicast",
2136 "tx_aborted",
2137 "tx_underrun",
2138};
2139
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002140static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002141{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002142 switch (sset) {
2143 case ETH_SS_STATS:
2144 return ARRAY_SIZE(rtl8169_gstrings);
2145 default:
2146 return -EOPNOTSUPP;
2147 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002148}
2149
Corinna Vinschen42020322015-09-10 10:47:35 +02002150DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002151{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002152 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002153}
2154
Corinna Vinschen42020322015-09-10 10:47:35 +02002155static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002156{
2157 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002158 dma_addr_t paddr = tp->counters_phys_addr;
2159 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002160
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002161 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2162 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002163 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002164 RTL_W32(tp, CounterAddrLow, cmd);
2165 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002166
Francois Romieua78e9362018-01-26 01:53:26 +01002167 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002168}
2169
2170static bool rtl8169_reset_counters(struct net_device *dev)
2171{
2172 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002173
2174 /*
2175 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2176 * tally counters.
2177 */
2178 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2179 return true;
2180
Corinna Vinschen42020322015-09-10 10:47:35 +02002181 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002182}
2183
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002184static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002185{
2186 struct rtl8169_private *tp = netdev_priv(dev);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002187
Ivan Vecera355423d2009-02-06 21:49:57 -08002188 /*
2189 * Some chips are unable to dump tally counters when the receiver
2190 * is disabled.
2191 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002192 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002193 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002194
Corinna Vinschen42020322015-09-10 10:47:35 +02002195 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002196}
2197
2198static bool rtl8169_init_counter_offsets(struct net_device *dev)
2199{
2200 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002201 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002202 bool ret = false;
2203
2204 /*
2205 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2206 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2207 * reset by a power cycle, while the counter values collected by the
2208 * driver are reset at every driver unload/load cycle.
2209 *
2210 * To make sure the HW values returned by @get_stats64 match the SW
2211 * values, we collect the initial values at first open(*) and use them
2212 * as offsets to normalize the values returned by @get_stats64.
2213 *
2214 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2215 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2216 * set at open time by rtl_hw_start.
2217 */
2218
2219 if (tp->tc_offset.inited)
2220 return true;
2221
2222 /* If both, reset and update fail, propagate to caller. */
2223 if (rtl8169_reset_counters(dev))
2224 ret = true;
2225
2226 if (rtl8169_update_counters(dev))
2227 ret = true;
2228
Corinna Vinschen42020322015-09-10 10:47:35 +02002229 tp->tc_offset.tx_errors = counters->tx_errors;
2230 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2231 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002232 tp->tc_offset.inited = true;
2233
2234 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002235}
2236
Ivan Vecera355423d2009-02-06 21:49:57 -08002237static void rtl8169_get_ethtool_stats(struct net_device *dev,
2238 struct ethtool_stats *stats, u64 *data)
2239{
2240 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002241 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002242 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002243
2244 ASSERT_RTNL();
2245
Chun-Hao Line0636232016-07-29 16:37:55 +08002246 pm_runtime_get_noresume(d);
2247
2248 if (pm_runtime_active(d))
2249 rtl8169_update_counters(dev);
2250
2251 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002252
Corinna Vinschen42020322015-09-10 10:47:35 +02002253 data[0] = le64_to_cpu(counters->tx_packets);
2254 data[1] = le64_to_cpu(counters->rx_packets);
2255 data[2] = le64_to_cpu(counters->tx_errors);
2256 data[3] = le32_to_cpu(counters->rx_errors);
2257 data[4] = le16_to_cpu(counters->rx_missed);
2258 data[5] = le16_to_cpu(counters->align_errors);
2259 data[6] = le32_to_cpu(counters->tx_one_collision);
2260 data[7] = le32_to_cpu(counters->tx_multi_collision);
2261 data[8] = le64_to_cpu(counters->rx_unicast);
2262 data[9] = le64_to_cpu(counters->rx_broadcast);
2263 data[10] = le32_to_cpu(counters->rx_multicast);
2264 data[11] = le16_to_cpu(counters->tx_aborted);
2265 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002266}
2267
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002268static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2269{
2270 switch(stringset) {
2271 case ETH_SS_STATS:
2272 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2273 break;
2274 }
2275}
2276
Florian Fainellif0903ea2016-12-03 12:01:19 -08002277static int rtl8169_nway_reset(struct net_device *dev)
2278{
2279 struct rtl8169_private *tp = netdev_priv(dev);
2280
2281 return mii_nway_restart(&tp->mii);
2282}
2283
Francois Romieu50970832017-10-27 13:24:49 +03002284/*
2285 * Interrupt coalescing
2286 *
2287 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2288 * > 8169, 8168 and 810x line of chipsets
2289 *
2290 * 8169, 8168, and 8136(810x) serial chipsets support it.
2291 *
2292 * > 2 - the Tx timer unit at gigabit speed
2293 *
2294 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2295 * (0xe0) bit 1 and bit 0.
2296 *
2297 * For 8169
2298 * bit[1:0] \ speed 1000M 100M 10M
2299 * 0 0 320ns 2.56us 40.96us
2300 * 0 1 2.56us 20.48us 327.7us
2301 * 1 0 5.12us 40.96us 655.4us
2302 * 1 1 10.24us 81.92us 1.31ms
2303 *
2304 * For the other
2305 * bit[1:0] \ speed 1000M 100M 10M
2306 * 0 0 5us 2.56us 40.96us
2307 * 0 1 40us 20.48us 327.7us
2308 * 1 0 80us 40.96us 655.4us
2309 * 1 1 160us 81.92us 1.31ms
2310 */
2311
2312/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2313struct rtl_coalesce_scale {
2314 /* Rx / Tx */
2315 u32 nsecs[2];
2316};
2317
2318/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2319struct rtl_coalesce_info {
2320 u32 speed;
2321 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2322};
2323
2324/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2325#define rxtx_x1822(r, t) { \
2326 {{(r), (t)}}, \
2327 {{(r)*8, (t)*8}}, \
2328 {{(r)*8*2, (t)*8*2}}, \
2329 {{(r)*8*2*2, (t)*8*2*2}}, \
2330}
2331static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2332 /* speed delays: rx00 tx00 */
2333 { SPEED_10, rxtx_x1822(40960, 40960) },
2334 { SPEED_100, rxtx_x1822( 2560, 2560) },
2335 { SPEED_1000, rxtx_x1822( 320, 320) },
2336 { 0 },
2337};
2338
2339static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2340 /* speed delays: rx00 tx00 */
2341 { SPEED_10, rxtx_x1822(40960, 40960) },
2342 { SPEED_100, rxtx_x1822( 2560, 2560) },
2343 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2344 { 0 },
2345};
2346#undef rxtx_x1822
2347
2348/* get rx/tx scale vector corresponding to current speed */
2349static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2350{
2351 struct rtl8169_private *tp = netdev_priv(dev);
2352 struct ethtool_link_ksettings ecmd;
2353 const struct rtl_coalesce_info *ci;
2354 int rc;
2355
2356 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2357 if (rc < 0)
2358 return ERR_PTR(rc);
2359
2360 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2361 if (ecmd.base.speed == ci->speed) {
2362 return ci;
2363 }
2364 }
2365
2366 return ERR_PTR(-ELNRNG);
2367}
2368
2369static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2370{
2371 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002372 const struct rtl_coalesce_info *ci;
2373 const struct rtl_coalesce_scale *scale;
2374 struct {
2375 u32 *max_frames;
2376 u32 *usecs;
2377 } coal_settings [] = {
2378 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2379 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2380 }, *p = coal_settings;
2381 int i;
2382 u16 w;
2383
2384 memset(ec, 0, sizeof(*ec));
2385
2386 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2387 ci = rtl_coalesce_info(dev);
2388 if (IS_ERR(ci))
2389 return PTR_ERR(ci);
2390
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002391 scale = &ci->scalev[RTL_R16(tp, CPlusCmd) & 3];
Francois Romieu50970832017-10-27 13:24:49 +03002392
2393 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002394 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002395 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2396 w >>= RTL_COALESCE_SHIFT;
2397 *p->usecs = w & RTL_COALESCE_MASK;
2398 }
2399
2400 for (i = 0; i < 2; i++) {
2401 p = coal_settings + i;
2402 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2403
2404 /*
2405 * ethtool_coalesce says it is illegal to set both usecs and
2406 * max_frames to 0.
2407 */
2408 if (!*p->usecs && !*p->max_frames)
2409 *p->max_frames = 1;
2410 }
2411
2412 return 0;
2413}
2414
2415/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2416static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2417 struct net_device *dev, u32 nsec, u16 *cp01)
2418{
2419 const struct rtl_coalesce_info *ci;
2420 u16 i;
2421
2422 ci = rtl_coalesce_info(dev);
2423 if (IS_ERR(ci))
2424 return ERR_CAST(ci);
2425
2426 for (i = 0; i < 4; i++) {
2427 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2428 ci->scalev[i].nsecs[1]);
2429 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2430 *cp01 = i;
2431 return &ci->scalev[i];
2432 }
2433 }
2434
2435 return ERR_PTR(-EINVAL);
2436}
2437
2438static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2439{
2440 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002441 const struct rtl_coalesce_scale *scale;
2442 struct {
2443 u32 frames;
2444 u32 usecs;
2445 } coal_settings [] = {
2446 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2447 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2448 }, *p = coal_settings;
2449 u16 w = 0, cp01;
2450 int i;
2451
2452 scale = rtl_coalesce_choose_scale(dev,
2453 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2454 if (IS_ERR(scale))
2455 return PTR_ERR(scale);
2456
2457 for (i = 0; i < 2; i++, p++) {
2458 u32 units;
2459
2460 /*
2461 * accept max_frames=1 we returned in rtl_get_coalesce.
2462 * accept it not only when usecs=0 because of e.g. the following scenario:
2463 *
2464 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2465 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2466 * - then user does `ethtool -C eth0 rx-usecs 100`
2467 *
2468 * since ethtool sends to kernel whole ethtool_coalesce
2469 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2470 * we'll reject it below in `frames % 4 != 0`.
2471 */
2472 if (p->frames == 1) {
2473 p->frames = 0;
2474 }
2475
2476 units = p->usecs * 1000 / scale->nsecs[i];
2477 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2478 return -EINVAL;
2479
2480 w <<= RTL_COALESCE_SHIFT;
2481 w |= units;
2482 w <<= RTL_COALESCE_SHIFT;
2483 w |= p->frames >> 2;
2484 }
2485
2486 rtl_lock_work(tp);
2487
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002488 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002489
2490 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002491 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2492 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002493
2494 rtl_unlock_work(tp);
2495
2496 return 0;
2497}
2498
Jeff Garzik7282d492006-09-13 14:30:00 -04002499static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .get_drvinfo = rtl8169_get_drvinfo,
2501 .get_regs_len = rtl8169_get_regs_len,
2502 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002503 .get_coalesce = rtl_get_coalesce,
2504 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002505 .get_msglevel = rtl8169_get_msglevel,
2506 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002508 .get_wol = rtl8169_get_wol,
2509 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002510 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002511 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002512 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002513 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002514 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002515 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002516 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517};
2518
Francois Romieu07d3f512007-02-21 22:40:46 +01002519static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002520 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Francois Romieu0e485152007-02-20 00:00:26 +01002522 /*
2523 * The driver currently handles the 8168Bf and the 8168Be identically
2524 * but they can be identified more specifically through the test below
2525 * if needed:
2526 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002527 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002528 *
2529 * Same thing for the 8101Eb and the 8101Ec:
2530 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002531 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002532 */
Francois Romieu37441002011-06-17 22:58:54 +02002533 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002535 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 int mac_version;
2537 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002538 /* 8168EP family. */
2539 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2540 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2541 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2542
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002543 /* 8168H family. */
2544 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2545 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2546
Hayes Wangc5583862012-07-02 17:23:22 +08002547 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002548 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002549 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002550 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2551 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2552
Hayes Wangc2218922011-09-06 16:55:18 +08002553 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002554 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002555 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2556 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2557
hayeswang01dc7fe2011-03-21 01:50:28 +00002558 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002559 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002560 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2561 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2562 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2563
Francois Romieu5b538df2008-07-20 16:22:45 +02002564 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002565 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2566 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002567 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002568
françois romieue6de30d2011-01-03 15:08:37 +00002569 /* 8168DP family. */
2570 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2571 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002572 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002573
Francois Romieuef808d52008-06-29 13:10:54 +02002574 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002575 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002576 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002577 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002578 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002579 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2580 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002581 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002582 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002583 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002584
2585 /* 8168B family. */
2586 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2587 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2588 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2589 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2590
2591 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002592 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2593 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002594 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002595 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002596 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2597 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2598 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002599 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2600 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2601 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2602 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2603 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2604 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002605 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002606 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002607 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002608 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2609 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002610 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2611 /* FIXME: where did these entries come from ? -- FR */
2612 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2613 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2614
2615 /* 8110 family. */
2616 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2617 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2618 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2619 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2620 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2621 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2622
Jean Delvaref21b75e2009-05-26 20:54:48 -07002623 /* Catch-all */
2624 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002625 };
2626 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 u32 reg;
2628
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002629 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002630 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 p++;
2632 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002633
2634 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2635 netif_notice(tp, probe, dev,
2636 "unknown MAC, using family default\n");
2637 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002638 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2639 tp->mac_version = tp->mii.supports_gmii ?
2640 RTL_GIGA_MAC_VER_42 :
2641 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002642 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2643 tp->mac_version = tp->mii.supports_gmii ?
2644 RTL_GIGA_MAC_VER_45 :
2645 RTL_GIGA_MAC_VER_47;
2646 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2647 tp->mac_version = tp->mii.supports_gmii ?
2648 RTL_GIGA_MAC_VER_46 :
2649 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2654{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002655 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Francois Romieu867763c2007-08-17 18:21:58 +02002658struct phy_reg {
2659 u16 reg;
2660 u16 val;
2661};
2662
françois romieu4da19632011-01-03 15:07:55 +00002663static void rtl_writephy_batch(struct rtl8169_private *tp,
2664 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002665{
2666 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002668 regs++;
2669 }
2670}
2671
françois romieubca03d52011-01-03 15:07:31 +00002672#define PHY_READ 0x00000000
2673#define PHY_DATA_OR 0x10000000
2674#define PHY_DATA_AND 0x20000000
2675#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002676#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002677#define PHY_CLEAR_READCOUNT 0x70000000
2678#define PHY_WRITE 0x80000000
2679#define PHY_READCOUNT_EQ_SKIP 0x90000000
2680#define PHY_COMP_EQ_SKIPN 0xa0000000
2681#define PHY_COMP_NEQ_SKIPN 0xb0000000
2682#define PHY_WRITE_PREVIOUS 0xc0000000
2683#define PHY_SKIPN 0xd0000000
2684#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002685
Hayes Wang960aee62011-06-18 11:37:48 +02002686struct fw_info {
2687 u32 magic;
2688 char version[RTL_VER_SIZE];
2689 __le32 fw_start;
2690 __le32 fw_len;
2691 u8 chksum;
2692} __packed;
2693
Francois Romieu1c361ef2011-06-17 17:16:24 +02002694#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2695
2696static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002697{
Francois Romieub6ffd972011-06-17 17:00:05 +02002698 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002699 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002700 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2701 char *version = rtl_fw->version;
2702 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002703
Francois Romieu1c361ef2011-06-17 17:16:24 +02002704 if (fw->size < FW_OPCODE_SIZE)
2705 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002706
2707 if (!fw_info->magic) {
2708 size_t i, size, start;
2709 u8 checksum = 0;
2710
2711 if (fw->size < sizeof(*fw_info))
2712 goto out;
2713
2714 for (i = 0; i < fw->size; i++)
2715 checksum += fw->data[i];
2716 if (checksum != 0)
2717 goto out;
2718
2719 start = le32_to_cpu(fw_info->fw_start);
2720 if (start > fw->size)
2721 goto out;
2722
2723 size = le32_to_cpu(fw_info->fw_len);
2724 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2725 goto out;
2726
2727 memcpy(version, fw_info->version, RTL_VER_SIZE);
2728
2729 pa->code = (__le32 *)(fw->data + start);
2730 pa->size = size;
2731 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002732 if (fw->size % FW_OPCODE_SIZE)
2733 goto out;
2734
2735 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2736
2737 pa->code = (__le32 *)fw->data;
2738 pa->size = fw->size / FW_OPCODE_SIZE;
2739 }
2740 version[RTL_VER_SIZE - 1] = 0;
2741
2742 rc = true;
2743out:
2744 return rc;
2745}
2746
Francois Romieufd112f22011-06-18 00:10:29 +02002747static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2748 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002749{
Francois Romieufd112f22011-06-18 00:10:29 +02002750 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002751 size_t index;
2752
Francois Romieu1c361ef2011-06-17 17:16:24 +02002753 for (index = 0; index < pa->size; index++) {
2754 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002755 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002756
hayeswang42b82dc2011-01-10 02:07:25 +00002757 switch(action & 0xf0000000) {
2758 case PHY_READ:
2759 case PHY_DATA_OR:
2760 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002761 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002762 case PHY_CLEAR_READCOUNT:
2763 case PHY_WRITE:
2764 case PHY_WRITE_PREVIOUS:
2765 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002766 break;
2767
hayeswang42b82dc2011-01-10 02:07:25 +00002768 case PHY_BJMPN:
2769 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002770 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002771 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002772 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002773 }
2774 break;
2775 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002776 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002777 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002778 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002779 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002780 }
2781 break;
2782 case PHY_COMP_EQ_SKIPN:
2783 case PHY_COMP_NEQ_SKIPN:
2784 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002785 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002786 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002787 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002788 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002789 }
2790 break;
2791
hayeswang42b82dc2011-01-10 02:07:25 +00002792 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002793 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002794 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002795 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002796 }
2797 }
Francois Romieufd112f22011-06-18 00:10:29 +02002798 rc = true;
2799out:
2800 return rc;
2801}
françois romieubca03d52011-01-03 15:07:31 +00002802
Francois Romieufd112f22011-06-18 00:10:29 +02002803static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2804{
2805 struct net_device *dev = tp->dev;
2806 int rc = -EINVAL;
2807
2808 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002809 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002810 goto out;
2811 }
2812
2813 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2814 rc = 0;
2815out:
2816 return rc;
2817}
2818
2819static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2820{
2821 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002822 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002823 u32 predata, count;
2824 size_t index;
2825
2826 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002827 org.write = ops->write;
2828 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002829
Francois Romieu1c361ef2011-06-17 17:16:24 +02002830 for (index = 0; index < pa->size; ) {
2831 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002832 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002833 u32 regno = (action & 0x0fff0000) >> 16;
2834
2835 if (!action)
2836 break;
françois romieubca03d52011-01-03 15:07:31 +00002837
2838 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002839 case PHY_READ:
2840 predata = rtl_readphy(tp, regno);
2841 count++;
2842 index++;
françois romieubca03d52011-01-03 15:07:31 +00002843 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002844 case PHY_DATA_OR:
2845 predata |= data;
2846 index++;
2847 break;
2848 case PHY_DATA_AND:
2849 predata &= data;
2850 index++;
2851 break;
2852 case PHY_BJMPN:
2853 index -= regno;
2854 break;
hayeswangeee37862013-04-01 22:23:38 +00002855 case PHY_MDIO_CHG:
2856 if (data == 0) {
2857 ops->write = org.write;
2858 ops->read = org.read;
2859 } else if (data == 1) {
2860 ops->write = mac_mcu_write;
2861 ops->read = mac_mcu_read;
2862 }
2863
hayeswang42b82dc2011-01-10 02:07:25 +00002864 index++;
2865 break;
2866 case PHY_CLEAR_READCOUNT:
2867 count = 0;
2868 index++;
2869 break;
2870 case PHY_WRITE:
2871 rtl_writephy(tp, regno, data);
2872 index++;
2873 break;
2874 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002875 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002876 break;
2877 case PHY_COMP_EQ_SKIPN:
2878 if (predata == data)
2879 index += regno;
2880 index++;
2881 break;
2882 case PHY_COMP_NEQ_SKIPN:
2883 if (predata != data)
2884 index += regno;
2885 index++;
2886 break;
2887 case PHY_WRITE_PREVIOUS:
2888 rtl_writephy(tp, regno, predata);
2889 index++;
2890 break;
2891 case PHY_SKIPN:
2892 index += regno + 1;
2893 break;
2894 case PHY_DELAY_MS:
2895 mdelay(data);
2896 index++;
2897 break;
2898
françois romieubca03d52011-01-03 15:07:31 +00002899 default:
2900 BUG();
2901 }
2902 }
hayeswangeee37862013-04-01 22:23:38 +00002903
2904 ops->write = org.write;
2905 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002906}
2907
françois romieuf1e02ed2011-01-13 13:07:53 +00002908static void rtl_release_firmware(struct rtl8169_private *tp)
2909{
Francois Romieub6ffd972011-06-17 17:00:05 +02002910 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2911 release_firmware(tp->rtl_fw->fw);
2912 kfree(tp->rtl_fw);
2913 }
2914 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002915}
2916
François Romieu953a12c2011-04-24 17:38:48 +02002917static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002918{
Francois Romieub6ffd972011-06-17 17:00:05 +02002919 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002920
2921 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002922 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002923 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002924}
2925
2926static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2927{
2928 if (rtl_readphy(tp, reg) != val)
2929 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2930 else
2931 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002932}
2933
françois romieu4da19632011-01-03 15:07:55 +00002934static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002936 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002937 { 0x1f, 0x0001 },
2938 { 0x06, 0x006e },
2939 { 0x08, 0x0708 },
2940 { 0x15, 0x4000 },
2941 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
françois romieu0b9b5712009-08-10 19:44:56 +00002943 { 0x1f, 0x0001 },
2944 { 0x03, 0x00a1 },
2945 { 0x02, 0x0008 },
2946 { 0x01, 0x0120 },
2947 { 0x00, 0x1000 },
2948 { 0x04, 0x0800 },
2949 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
françois romieu0b9b5712009-08-10 19:44:56 +00002951 { 0x03, 0xff41 },
2952 { 0x02, 0xdf60 },
2953 { 0x01, 0x0140 },
2954 { 0x00, 0x0077 },
2955 { 0x04, 0x7800 },
2956 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
françois romieu0b9b5712009-08-10 19:44:56 +00002958 { 0x03, 0x802f },
2959 { 0x02, 0x4f02 },
2960 { 0x01, 0x0409 },
2961 { 0x00, 0xf0f9 },
2962 { 0x04, 0x9800 },
2963 { 0x04, 0x9000 },
2964
2965 { 0x03, 0xdf01 },
2966 { 0x02, 0xdf20 },
2967 { 0x01, 0xff95 },
2968 { 0x00, 0xba00 },
2969 { 0x04, 0xa800 },
2970 { 0x04, 0xa000 },
2971
2972 { 0x03, 0xff41 },
2973 { 0x02, 0xdf20 },
2974 { 0x01, 0x0140 },
2975 { 0x00, 0x00bb },
2976 { 0x04, 0xb800 },
2977 { 0x04, 0xb000 },
2978
2979 { 0x03, 0xdf41 },
2980 { 0x02, 0xdc60 },
2981 { 0x01, 0x6340 },
2982 { 0x00, 0x007d },
2983 { 0x04, 0xd800 },
2984 { 0x04, 0xd000 },
2985
2986 { 0x03, 0xdf01 },
2987 { 0x02, 0xdf20 },
2988 { 0x01, 0x100a },
2989 { 0x00, 0xa0ff },
2990 { 0x04, 0xf800 },
2991 { 0x04, 0xf000 },
2992
2993 { 0x1f, 0x0000 },
2994 { 0x0b, 0x0000 },
2995 { 0x00, 0x9200 }
2996 };
2997
françois romieu4da19632011-01-03 15:07:55 +00002998 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
françois romieu4da19632011-01-03 15:07:55 +00003001static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003002{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003003 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003004 { 0x1f, 0x0002 },
3005 { 0x01, 0x90d0 },
3006 { 0x1f, 0x0000 }
3007 };
3008
françois romieu4da19632011-01-03 15:07:55 +00003009 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003010}
3011
françois romieu4da19632011-01-03 15:07:55 +00003012static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003013{
3014 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003015
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003016 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3017 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003018 return;
3019
françois romieu4da19632011-01-03 15:07:55 +00003020 rtl_writephy(tp, 0x1f, 0x0001);
3021 rtl_writephy(tp, 0x10, 0xf01b);
3022 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003023}
3024
françois romieu4da19632011-01-03 15:07:55 +00003025static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003026{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003027 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003028 { 0x1f, 0x0001 },
3029 { 0x04, 0x0000 },
3030 { 0x03, 0x00a1 },
3031 { 0x02, 0x0008 },
3032 { 0x01, 0x0120 },
3033 { 0x00, 0x1000 },
3034 { 0x04, 0x0800 },
3035 { 0x04, 0x9000 },
3036 { 0x03, 0x802f },
3037 { 0x02, 0x4f02 },
3038 { 0x01, 0x0409 },
3039 { 0x00, 0xf099 },
3040 { 0x04, 0x9800 },
3041 { 0x04, 0xa000 },
3042 { 0x03, 0xdf01 },
3043 { 0x02, 0xdf20 },
3044 { 0x01, 0xff95 },
3045 { 0x00, 0xba00 },
3046 { 0x04, 0xa800 },
3047 { 0x04, 0xf000 },
3048 { 0x03, 0xdf01 },
3049 { 0x02, 0xdf20 },
3050 { 0x01, 0x101a },
3051 { 0x00, 0xa0ff },
3052 { 0x04, 0xf800 },
3053 { 0x04, 0x0000 },
3054 { 0x1f, 0x0000 },
3055
3056 { 0x1f, 0x0001 },
3057 { 0x10, 0xf41b },
3058 { 0x14, 0xfb54 },
3059 { 0x18, 0xf5c7 },
3060 { 0x1f, 0x0000 },
3061
3062 { 0x1f, 0x0001 },
3063 { 0x17, 0x0cc0 },
3064 { 0x1f, 0x0000 }
3065 };
3066
françois romieu4da19632011-01-03 15:07:55 +00003067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003068
françois romieu4da19632011-01-03 15:07:55 +00003069 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003070}
3071
françois romieu4da19632011-01-03 15:07:55 +00003072static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003073{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003074 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003075 { 0x1f, 0x0001 },
3076 { 0x04, 0x0000 },
3077 { 0x03, 0x00a1 },
3078 { 0x02, 0x0008 },
3079 { 0x01, 0x0120 },
3080 { 0x00, 0x1000 },
3081 { 0x04, 0x0800 },
3082 { 0x04, 0x9000 },
3083 { 0x03, 0x802f },
3084 { 0x02, 0x4f02 },
3085 { 0x01, 0x0409 },
3086 { 0x00, 0xf099 },
3087 { 0x04, 0x9800 },
3088 { 0x04, 0xa000 },
3089 { 0x03, 0xdf01 },
3090 { 0x02, 0xdf20 },
3091 { 0x01, 0xff95 },
3092 { 0x00, 0xba00 },
3093 { 0x04, 0xa800 },
3094 { 0x04, 0xf000 },
3095 { 0x03, 0xdf01 },
3096 { 0x02, 0xdf20 },
3097 { 0x01, 0x101a },
3098 { 0x00, 0xa0ff },
3099 { 0x04, 0xf800 },
3100 { 0x04, 0x0000 },
3101 { 0x1f, 0x0000 },
3102
3103 { 0x1f, 0x0001 },
3104 { 0x0b, 0x8480 },
3105 { 0x1f, 0x0000 },
3106
3107 { 0x1f, 0x0001 },
3108 { 0x18, 0x67c7 },
3109 { 0x04, 0x2000 },
3110 { 0x03, 0x002f },
3111 { 0x02, 0x4360 },
3112 { 0x01, 0x0109 },
3113 { 0x00, 0x3022 },
3114 { 0x04, 0x2800 },
3115 { 0x1f, 0x0000 },
3116
3117 { 0x1f, 0x0001 },
3118 { 0x17, 0x0cc0 },
3119 { 0x1f, 0x0000 }
3120 };
3121
françois romieu4da19632011-01-03 15:07:55 +00003122 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003123}
3124
françois romieu4da19632011-01-03 15:07:55 +00003125static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003126{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003127 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003128 { 0x10, 0xf41b },
3129 { 0x1f, 0x0000 }
3130 };
3131
françois romieu4da19632011-01-03 15:07:55 +00003132 rtl_writephy(tp, 0x1f, 0x0001);
3133 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003134
françois romieu4da19632011-01-03 15:07:55 +00003135 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003136}
3137
françois romieu4da19632011-01-03 15:07:55 +00003138static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003139{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003140 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003141 { 0x1f, 0x0001 },
3142 { 0x10, 0xf41b },
3143 { 0x1f, 0x0000 }
3144 };
3145
françois romieu4da19632011-01-03 15:07:55 +00003146 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003147}
3148
françois romieu4da19632011-01-03 15:07:55 +00003149static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003150{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003151 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003152 { 0x1f, 0x0000 },
3153 { 0x1d, 0x0f00 },
3154 { 0x1f, 0x0002 },
3155 { 0x0c, 0x1ec8 },
3156 { 0x1f, 0x0000 }
3157 };
3158
françois romieu4da19632011-01-03 15:07:55 +00003159 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003160}
3161
françois romieu4da19632011-01-03 15:07:55 +00003162static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003163{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003164 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003165 { 0x1f, 0x0001 },
3166 { 0x1d, 0x3d98 },
3167 { 0x1f, 0x0000 }
3168 };
3169
françois romieu4da19632011-01-03 15:07:55 +00003170 rtl_writephy(tp, 0x1f, 0x0000);
3171 rtl_patchphy(tp, 0x14, 1 << 5);
3172 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003173
françois romieu4da19632011-01-03 15:07:55 +00003174 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003175}
3176
françois romieu4da19632011-01-03 15:07:55 +00003177static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003178{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003179 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003180 { 0x1f, 0x0001 },
3181 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003182 { 0x1f, 0x0002 },
3183 { 0x00, 0x88d4 },
3184 { 0x01, 0x82b1 },
3185 { 0x03, 0x7002 },
3186 { 0x08, 0x9e30 },
3187 { 0x09, 0x01f0 },
3188 { 0x0a, 0x5500 },
3189 { 0x0c, 0x00c8 },
3190 { 0x1f, 0x0003 },
3191 { 0x12, 0xc096 },
3192 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003193 { 0x1f, 0x0000 },
3194 { 0x1f, 0x0000 },
3195 { 0x09, 0x2000 },
3196 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003197 };
3198
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003200
françois romieu4da19632011-01-03 15:07:55 +00003201 rtl_patchphy(tp, 0x14, 1 << 5);
3202 rtl_patchphy(tp, 0x0d, 1 << 5);
3203 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003204}
3205
françois romieu4da19632011-01-03 15:07:55 +00003206static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003207{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003208 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003209 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003210 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003211 { 0x03, 0x802f },
3212 { 0x02, 0x4f02 },
3213 { 0x01, 0x0409 },
3214 { 0x00, 0xf099 },
3215 { 0x04, 0x9800 },
3216 { 0x04, 0x9000 },
3217 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003218 { 0x1f, 0x0002 },
3219 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003220 { 0x06, 0x0761 },
3221 { 0x1f, 0x0003 },
3222 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003223 { 0x1f, 0x0000 }
3224 };
3225
françois romieu4da19632011-01-03 15:07:55 +00003226 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003227
françois romieu4da19632011-01-03 15:07:55 +00003228 rtl_patchphy(tp, 0x16, 1 << 0);
3229 rtl_patchphy(tp, 0x14, 1 << 5);
3230 rtl_patchphy(tp, 0x0d, 1 << 5);
3231 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003232}
3233
françois romieu4da19632011-01-03 15:07:55 +00003234static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003235{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003236 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003237 { 0x1f, 0x0001 },
3238 { 0x12, 0x2300 },
3239 { 0x1d, 0x3d98 },
3240 { 0x1f, 0x0002 },
3241 { 0x0c, 0x7eb8 },
3242 { 0x06, 0x5461 },
3243 { 0x1f, 0x0003 },
3244 { 0x16, 0x0f0a },
3245 { 0x1f, 0x0000 }
3246 };
3247
françois romieu4da19632011-01-03 15:07:55 +00003248 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003249
françois romieu4da19632011-01-03 15:07:55 +00003250 rtl_patchphy(tp, 0x16, 1 << 0);
3251 rtl_patchphy(tp, 0x14, 1 << 5);
3252 rtl_patchphy(tp, 0x0d, 1 << 5);
3253 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003254}
3255
françois romieu4da19632011-01-03 15:07:55 +00003256static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003257{
françois romieu4da19632011-01-03 15:07:55 +00003258 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003259}
3260
françois romieubca03d52011-01-03 15:07:31 +00003261static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003262{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003263 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003264 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003265 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003266 { 0x06, 0x4064 },
3267 { 0x07, 0x2863 },
3268 { 0x08, 0x059c },
3269 { 0x09, 0x26b4 },
3270 { 0x0a, 0x6a19 },
3271 { 0x0b, 0xdcc8 },
3272 { 0x10, 0xf06d },
3273 { 0x14, 0x7f68 },
3274 { 0x18, 0x7fd9 },
3275 { 0x1c, 0xf0ff },
3276 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003277 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003278 { 0x12, 0xf49f },
3279 { 0x13, 0x070b },
3280 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003281 { 0x14, 0x94c0 },
3282
3283 /*
3284 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003285 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003286 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003287 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003288 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003289 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003290 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003291 { 0x06, 0x5561 },
3292
3293 /*
3294 * Can not link to 1Gbps with bad cable
3295 * Decrease SNR threshold form 21.07dB to 19.04dB
3296 */
3297 { 0x1f, 0x0001 },
3298 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003299
3300 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003301 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003302 };
3303
françois romieu4da19632011-01-03 15:07:55 +00003304 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003305
françois romieubca03d52011-01-03 15:07:31 +00003306 /*
3307 * Rx Error Issue
3308 * Fine Tune Switching regulator parameter
3309 */
françois romieu4da19632011-01-03 15:07:55 +00003310 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003311 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3312 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003313
Francois Romieufdf6fc02012-07-06 22:40:38 +02003314 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003315 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003316 { 0x1f, 0x0002 },
3317 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003318 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003319 { 0x05, 0x8330 },
3320 { 0x06, 0x669a },
3321 { 0x1f, 0x0002 }
3322 };
3323 int val;
3324
françois romieu4da19632011-01-03 15:07:55 +00003325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003326
françois romieu4da19632011-01-03 15:07:55 +00003327 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003328
3329 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003330 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003331 0x0065, 0x0066, 0x0067, 0x0068,
3332 0x0069, 0x006a, 0x006b, 0x006c
3333 };
3334 int i;
3335
françois romieu4da19632011-01-03 15:07:55 +00003336 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003337
3338 val &= 0xff00;
3339 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003340 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003341 }
3342 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003343 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003344 { 0x1f, 0x0002 },
3345 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003346 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003347 { 0x05, 0x8330 },
3348 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003349 };
3350
françois romieu4da19632011-01-03 15:07:55 +00003351 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003352 }
3353
françois romieubca03d52011-01-03 15:07:31 +00003354 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003355 rtl_writephy(tp, 0x1f, 0x0002);
3356 rtl_patchphy(tp, 0x0d, 0x0300);
3357 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003358
françois romieubca03d52011-01-03 15:07:31 +00003359 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003360 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003361 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3362 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003363
françois romieu4da19632011-01-03 15:07:55 +00003364 rtl_writephy(tp, 0x1f, 0x0005);
3365 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003366
3367 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003368
françois romieu4da19632011-01-03 15:07:55 +00003369 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003370}
3371
françois romieubca03d52011-01-03 15:07:31 +00003372static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003373{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003374 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003375 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003376 { 0x1f, 0x0001 },
3377 { 0x06, 0x4064 },
3378 { 0x07, 0x2863 },
3379 { 0x08, 0x059c },
3380 { 0x09, 0x26b4 },
3381 { 0x0a, 0x6a19 },
3382 { 0x0b, 0xdcc8 },
3383 { 0x10, 0xf06d },
3384 { 0x14, 0x7f68 },
3385 { 0x18, 0x7fd9 },
3386 { 0x1c, 0xf0ff },
3387 { 0x1d, 0x3d9c },
3388 { 0x1f, 0x0003 },
3389 { 0x12, 0xf49f },
3390 { 0x13, 0x070b },
3391 { 0x1a, 0x05ad },
3392 { 0x14, 0x94c0 },
3393
françois romieubca03d52011-01-03 15:07:31 +00003394 /*
3395 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003396 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003397 */
françois romieudaf9df62009-10-07 12:44:20 +00003398 { 0x1f, 0x0002 },
3399 { 0x06, 0x5561 },
3400 { 0x1f, 0x0005 },
3401 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003402 { 0x06, 0x5561 },
3403
3404 /*
3405 * Can not link to 1Gbps with bad cable
3406 * Decrease SNR threshold form 21.07dB to 19.04dB
3407 */
3408 { 0x1f, 0x0001 },
3409 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003410
3411 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003412 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003413 };
3414
françois romieu4da19632011-01-03 15:07:55 +00003415 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003416
Francois Romieufdf6fc02012-07-06 22:40:38 +02003417 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003418 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003419 { 0x1f, 0x0002 },
3420 { 0x05, 0x669a },
3421 { 0x1f, 0x0005 },
3422 { 0x05, 0x8330 },
3423 { 0x06, 0x669a },
3424
3425 { 0x1f, 0x0002 }
3426 };
3427 int val;
3428
françois romieu4da19632011-01-03 15:07:55 +00003429 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003430
françois romieu4da19632011-01-03 15:07:55 +00003431 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003432 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003433 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003434 0x0065, 0x0066, 0x0067, 0x0068,
3435 0x0069, 0x006a, 0x006b, 0x006c
3436 };
3437 int i;
3438
françois romieu4da19632011-01-03 15:07:55 +00003439 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003440
3441 val &= 0xff00;
3442 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003443 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003444 }
3445 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003446 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003447 { 0x1f, 0x0002 },
3448 { 0x05, 0x2642 },
3449 { 0x1f, 0x0005 },
3450 { 0x05, 0x8330 },
3451 { 0x06, 0x2642 }
3452 };
3453
françois romieu4da19632011-01-03 15:07:55 +00003454 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003455 }
3456
françois romieubca03d52011-01-03 15:07:31 +00003457 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003458 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003459 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3460 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003461
françois romieubca03d52011-01-03 15:07:31 +00003462 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003463 rtl_writephy(tp, 0x1f, 0x0002);
3464 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003465
françois romieu4da19632011-01-03 15:07:55 +00003466 rtl_writephy(tp, 0x1f, 0x0005);
3467 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003468
3469 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003470
françois romieu4da19632011-01-03 15:07:55 +00003471 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003472}
3473
françois romieu4da19632011-01-03 15:07:55 +00003474static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003475{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003476 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003477 { 0x1f, 0x0002 },
3478 { 0x10, 0x0008 },
3479 { 0x0d, 0x006c },
3480
3481 { 0x1f, 0x0000 },
3482 { 0x0d, 0xf880 },
3483
3484 { 0x1f, 0x0001 },
3485 { 0x17, 0x0cc0 },
3486
3487 { 0x1f, 0x0001 },
3488 { 0x0b, 0xa4d8 },
3489 { 0x09, 0x281c },
3490 { 0x07, 0x2883 },
3491 { 0x0a, 0x6b35 },
3492 { 0x1d, 0x3da4 },
3493 { 0x1c, 0xeffd },
3494 { 0x14, 0x7f52 },
3495 { 0x18, 0x7fc6 },
3496 { 0x08, 0x0601 },
3497 { 0x06, 0x4063 },
3498 { 0x10, 0xf074 },
3499 { 0x1f, 0x0003 },
3500 { 0x13, 0x0789 },
3501 { 0x12, 0xf4bd },
3502 { 0x1a, 0x04fd },
3503 { 0x14, 0x84b0 },
3504 { 0x1f, 0x0000 },
3505 { 0x00, 0x9200 },
3506
3507 { 0x1f, 0x0005 },
3508 { 0x01, 0x0340 },
3509 { 0x1f, 0x0001 },
3510 { 0x04, 0x4000 },
3511 { 0x03, 0x1d21 },
3512 { 0x02, 0x0c32 },
3513 { 0x01, 0x0200 },
3514 { 0x00, 0x5554 },
3515 { 0x04, 0x4800 },
3516 { 0x04, 0x4000 },
3517 { 0x04, 0xf000 },
3518 { 0x03, 0xdf01 },
3519 { 0x02, 0xdf20 },
3520 { 0x01, 0x101a },
3521 { 0x00, 0xa0ff },
3522 { 0x04, 0xf800 },
3523 { 0x04, 0xf000 },
3524 { 0x1f, 0x0000 },
3525
3526 { 0x1f, 0x0007 },
3527 { 0x1e, 0x0023 },
3528 { 0x16, 0x0000 },
3529 { 0x1f, 0x0000 }
3530 };
3531
françois romieu4da19632011-01-03 15:07:55 +00003532 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003533}
3534
françois romieue6de30d2011-01-03 15:08:37 +00003535static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3536{
3537 static const struct phy_reg phy_reg_init[] = {
3538 { 0x1f, 0x0001 },
3539 { 0x17, 0x0cc0 },
3540
3541 { 0x1f, 0x0007 },
3542 { 0x1e, 0x002d },
3543 { 0x18, 0x0040 },
3544 { 0x1f, 0x0000 }
3545 };
3546
3547 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3548 rtl_patchphy(tp, 0x0d, 1 << 5);
3549}
3550
Hayes Wang70090422011-07-06 15:58:06 +08003551static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003552{
3553 static const struct phy_reg phy_reg_init[] = {
3554 /* Enable Delay cap */
3555 { 0x1f, 0x0005 },
3556 { 0x05, 0x8b80 },
3557 { 0x06, 0xc896 },
3558 { 0x1f, 0x0000 },
3559
3560 /* Channel estimation fine tune */
3561 { 0x1f, 0x0001 },
3562 { 0x0b, 0x6c20 },
3563 { 0x07, 0x2872 },
3564 { 0x1c, 0xefff },
3565 { 0x1f, 0x0003 },
3566 { 0x14, 0x6420 },
3567 { 0x1f, 0x0000 },
3568
3569 /* Update PFM & 10M TX idle timer */
3570 { 0x1f, 0x0007 },
3571 { 0x1e, 0x002f },
3572 { 0x15, 0x1919 },
3573 { 0x1f, 0x0000 },
3574
3575 { 0x1f, 0x0007 },
3576 { 0x1e, 0x00ac },
3577 { 0x18, 0x0006 },
3578 { 0x1f, 0x0000 }
3579 };
3580
Francois Romieu15ecd032011-04-27 13:52:22 -07003581 rtl_apply_firmware(tp);
3582
hayeswang01dc7fe2011-03-21 01:50:28 +00003583 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3584
3585 /* DCO enable for 10M IDLE Power */
3586 rtl_writephy(tp, 0x1f, 0x0007);
3587 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003588 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003589 rtl_writephy(tp, 0x1f, 0x0000);
3590
3591 /* For impedance matching */
3592 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003593 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003594 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003595
3596 /* PHY auto speed down */
3597 rtl_writephy(tp, 0x1f, 0x0007);
3598 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003599 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003600 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003601 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003602
3603 rtl_writephy(tp, 0x1f, 0x0005);
3604 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003605 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003606 rtl_writephy(tp, 0x1f, 0x0000);
3607
3608 rtl_writephy(tp, 0x1f, 0x0005);
3609 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003610 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003611 rtl_writephy(tp, 0x1f, 0x0007);
3612 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003613 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003614 rtl_writephy(tp, 0x1f, 0x0006);
3615 rtl_writephy(tp, 0x00, 0x5a00);
3616 rtl_writephy(tp, 0x1f, 0x0000);
3617 rtl_writephy(tp, 0x0d, 0x0007);
3618 rtl_writephy(tp, 0x0e, 0x003c);
3619 rtl_writephy(tp, 0x0d, 0x4007);
3620 rtl_writephy(tp, 0x0e, 0x0000);
3621 rtl_writephy(tp, 0x0d, 0x0000);
3622}
3623
françois romieu9ecb9aa2012-12-07 11:20:21 +00003624static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3625{
3626 const u16 w[] = {
3627 addr[0] | (addr[1] << 8),
3628 addr[2] | (addr[3] << 8),
3629 addr[4] | (addr[5] << 8)
3630 };
3631 const struct exgmac_reg e[] = {
3632 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3633 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3634 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3635 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3636 };
3637
3638 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3639}
3640
Hayes Wang70090422011-07-06 15:58:06 +08003641static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3642{
3643 static const struct phy_reg phy_reg_init[] = {
3644 /* Enable Delay cap */
3645 { 0x1f, 0x0004 },
3646 { 0x1f, 0x0007 },
3647 { 0x1e, 0x00ac },
3648 { 0x18, 0x0006 },
3649 { 0x1f, 0x0002 },
3650 { 0x1f, 0x0000 },
3651 { 0x1f, 0x0000 },
3652
3653 /* Channel estimation fine tune */
3654 { 0x1f, 0x0003 },
3655 { 0x09, 0xa20f },
3656 { 0x1f, 0x0000 },
3657 { 0x1f, 0x0000 },
3658
3659 /* Green Setting */
3660 { 0x1f, 0x0005 },
3661 { 0x05, 0x8b5b },
3662 { 0x06, 0x9222 },
3663 { 0x05, 0x8b6d },
3664 { 0x06, 0x8000 },
3665 { 0x05, 0x8b76 },
3666 { 0x06, 0x8000 },
3667 { 0x1f, 0x0000 }
3668 };
3669
3670 rtl_apply_firmware(tp);
3671
3672 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3673
3674 /* For 4-corner performance improve */
3675 rtl_writephy(tp, 0x1f, 0x0005);
3676 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003678 rtl_writephy(tp, 0x1f, 0x0000);
3679
3680 /* PHY auto speed down */
3681 rtl_writephy(tp, 0x1f, 0x0004);
3682 rtl_writephy(tp, 0x1f, 0x0007);
3683 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003684 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003685 rtl_writephy(tp, 0x1f, 0x0002);
3686 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003687 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003688
3689 /* improve 10M EEE waveform */
3690 rtl_writephy(tp, 0x1f, 0x0005);
3691 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003692 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003693 rtl_writephy(tp, 0x1f, 0x0000);
3694
3695 /* Improve 2-pair detection performance */
3696 rtl_writephy(tp, 0x1f, 0x0005);
3697 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003698 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003699 rtl_writephy(tp, 0x1f, 0x0000);
3700
3701 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003702 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003703 rtl_writephy(tp, 0x1f, 0x0005);
3704 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003705 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003706 rtl_writephy(tp, 0x1f, 0x0004);
3707 rtl_writephy(tp, 0x1f, 0x0007);
3708 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003709 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003710 rtl_writephy(tp, 0x1f, 0x0002);
3711 rtl_writephy(tp, 0x1f, 0x0000);
3712 rtl_writephy(tp, 0x0d, 0x0007);
3713 rtl_writephy(tp, 0x0e, 0x003c);
3714 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003715 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003716 rtl_writephy(tp, 0x0d, 0x0000);
3717
3718 /* Green feature */
3719 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003720 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3721 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003722 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003723 rtl_writephy(tp, 0x1f, 0x0005);
3724 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3725 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003726
françois romieu9ecb9aa2012-12-07 11:20:21 +00003727 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3728 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003729}
3730
Hayes Wang5f886e02012-03-30 14:33:03 +08003731static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3732{
3733 /* For 4-corner performance improve */
3734 rtl_writephy(tp, 0x1f, 0x0005);
3735 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003736 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003737 rtl_writephy(tp, 0x1f, 0x0000);
3738
3739 /* PHY auto speed down */
3740 rtl_writephy(tp, 0x1f, 0x0007);
3741 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003742 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003743 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003744 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003745
3746 /* Improve 10M EEE waveform */
3747 rtl_writephy(tp, 0x1f, 0x0005);
3748 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003749 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003750 rtl_writephy(tp, 0x1f, 0x0000);
3751}
3752
Hayes Wangc2218922011-09-06 16:55:18 +08003753static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3754{
3755 static const struct phy_reg phy_reg_init[] = {
3756 /* Channel estimation fine tune */
3757 { 0x1f, 0x0003 },
3758 { 0x09, 0xa20f },
3759 { 0x1f, 0x0000 },
3760
3761 /* Modify green table for giga & fnet */
3762 { 0x1f, 0x0005 },
3763 { 0x05, 0x8b55 },
3764 { 0x06, 0x0000 },
3765 { 0x05, 0x8b5e },
3766 { 0x06, 0x0000 },
3767 { 0x05, 0x8b67 },
3768 { 0x06, 0x0000 },
3769 { 0x05, 0x8b70 },
3770 { 0x06, 0x0000 },
3771 { 0x1f, 0x0000 },
3772 { 0x1f, 0x0007 },
3773 { 0x1e, 0x0078 },
3774 { 0x17, 0x0000 },
3775 { 0x19, 0x00fb },
3776 { 0x1f, 0x0000 },
3777
3778 /* Modify green table for 10M */
3779 { 0x1f, 0x0005 },
3780 { 0x05, 0x8b79 },
3781 { 0x06, 0xaa00 },
3782 { 0x1f, 0x0000 },
3783
3784 /* Disable hiimpedance detection (RTCT) */
3785 { 0x1f, 0x0003 },
3786 { 0x01, 0x328a },
3787 { 0x1f, 0x0000 }
3788 };
3789
3790 rtl_apply_firmware(tp);
3791
3792 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3793
Hayes Wang5f886e02012-03-30 14:33:03 +08003794 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003795
3796 /* Improve 2-pair detection performance */
3797 rtl_writephy(tp, 0x1f, 0x0005);
3798 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003799 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003800 rtl_writephy(tp, 0x1f, 0x0000);
3801}
3802
3803static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3804{
3805 rtl_apply_firmware(tp);
3806
Hayes Wang5f886e02012-03-30 14:33:03 +08003807 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003808}
3809
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003810static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3811{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003812 static const struct phy_reg phy_reg_init[] = {
3813 /* Channel estimation fine tune */
3814 { 0x1f, 0x0003 },
3815 { 0x09, 0xa20f },
3816 { 0x1f, 0x0000 },
3817
3818 /* Modify green table for giga & fnet */
3819 { 0x1f, 0x0005 },
3820 { 0x05, 0x8b55 },
3821 { 0x06, 0x0000 },
3822 { 0x05, 0x8b5e },
3823 { 0x06, 0x0000 },
3824 { 0x05, 0x8b67 },
3825 { 0x06, 0x0000 },
3826 { 0x05, 0x8b70 },
3827 { 0x06, 0x0000 },
3828 { 0x1f, 0x0000 },
3829 { 0x1f, 0x0007 },
3830 { 0x1e, 0x0078 },
3831 { 0x17, 0x0000 },
3832 { 0x19, 0x00aa },
3833 { 0x1f, 0x0000 },
3834
3835 /* Modify green table for 10M */
3836 { 0x1f, 0x0005 },
3837 { 0x05, 0x8b79 },
3838 { 0x06, 0xaa00 },
3839 { 0x1f, 0x0000 },
3840
3841 /* Disable hiimpedance detection (RTCT) */
3842 { 0x1f, 0x0003 },
3843 { 0x01, 0x328a },
3844 { 0x1f, 0x0000 }
3845 };
3846
3847
3848 rtl_apply_firmware(tp);
3849
3850 rtl8168f_hw_phy_config(tp);
3851
3852 /* Improve 2-pair detection performance */
3853 rtl_writephy(tp, 0x1f, 0x0005);
3854 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003855 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003856 rtl_writephy(tp, 0x1f, 0x0000);
3857
3858 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3859
3860 /* Modify green table for giga */
3861 rtl_writephy(tp, 0x1f, 0x0005);
3862 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003863 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003864 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003865 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003866 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003867 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003868 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003869 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003870 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003871 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003872 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003873 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003874 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003875 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003876 rtl_writephy(tp, 0x1f, 0x0000);
3877
3878 /* uc same-seed solution */
3879 rtl_writephy(tp, 0x1f, 0x0005);
3880 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003881 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003882 rtl_writephy(tp, 0x1f, 0x0000);
3883
3884 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003885 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003886 rtl_writephy(tp, 0x1f, 0x0005);
3887 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003888 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003889 rtl_writephy(tp, 0x1f, 0x0004);
3890 rtl_writephy(tp, 0x1f, 0x0007);
3891 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003892 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003893 rtl_writephy(tp, 0x1f, 0x0000);
3894 rtl_writephy(tp, 0x0d, 0x0007);
3895 rtl_writephy(tp, 0x0e, 0x003c);
3896 rtl_writephy(tp, 0x0d, 0x4007);
3897 rtl_writephy(tp, 0x0e, 0x0000);
3898 rtl_writephy(tp, 0x0d, 0x0000);
3899
3900 /* Green feature */
3901 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003902 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3903 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003904 rtl_writephy(tp, 0x1f, 0x0000);
3905}
3906
Hayes Wangc5583862012-07-02 17:23:22 +08003907static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3908{
Hayes Wangc5583862012-07-02 17:23:22 +08003909 rtl_apply_firmware(tp);
3910
hayeswang41f44d12013-04-01 22:23:36 +00003911 rtl_writephy(tp, 0x1f, 0x0a46);
3912 if (rtl_readphy(tp, 0x10) & 0x0100) {
3913 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003914 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003915 } else {
3916 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003917 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003918 }
Hayes Wangc5583862012-07-02 17:23:22 +08003919
hayeswang41f44d12013-04-01 22:23:36 +00003920 rtl_writephy(tp, 0x1f, 0x0a46);
3921 if (rtl_readphy(tp, 0x13) & 0x0100) {
3922 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003923 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003924 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003925 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003926 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003927 }
Hayes Wangc5583862012-07-02 17:23:22 +08003928
hayeswang41f44d12013-04-01 22:23:36 +00003929 /* Enable PHY auto speed down */
3930 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003931 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003932
hayeswangfe7524c2013-04-01 22:23:37 +00003933 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003934 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003935 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003936 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003937 rtl_writephy(tp, 0x1f, 0x0a43);
3938 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003939 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3940 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003941
hayeswang41f44d12013-04-01 22:23:36 +00003942 /* EEE auto-fallback function */
3943 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003944 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003945
hayeswang41f44d12013-04-01 22:23:36 +00003946 /* Enable UC LPF tune function */
3947 rtl_writephy(tp, 0x1f, 0x0a43);
3948 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003949 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003950
3951 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003952 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003953
hayeswangfe7524c2013-04-01 22:23:37 +00003954 /* Improve SWR Efficiency */
3955 rtl_writephy(tp, 0x1f, 0x0bcd);
3956 rtl_writephy(tp, 0x14, 0x5065);
3957 rtl_writephy(tp, 0x14, 0xd065);
3958 rtl_writephy(tp, 0x1f, 0x0bc8);
3959 rtl_writephy(tp, 0x11, 0x5655);
3960 rtl_writephy(tp, 0x1f, 0x0bcd);
3961 rtl_writephy(tp, 0x14, 0x1065);
3962 rtl_writephy(tp, 0x14, 0x9065);
3963 rtl_writephy(tp, 0x14, 0x1065);
3964
David Chang1bac1072013-11-27 15:48:36 +08003965 /* Check ALDPS bit, disable it if enabled */
3966 rtl_writephy(tp, 0x1f, 0x0a43);
3967 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003968 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003969
hayeswang41f44d12013-04-01 22:23:36 +00003970 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003971}
3972
hayeswang57538c42013-04-01 22:23:40 +00003973static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3974{
3975 rtl_apply_firmware(tp);
3976}
3977
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003978static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3979{
3980 u16 dout_tapbin;
3981 u32 data;
3982
3983 rtl_apply_firmware(tp);
3984
3985 /* CHN EST parameters adjust - giga master */
3986 rtl_writephy(tp, 0x1f, 0x0a43);
3987 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003988 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003989 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003990 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003991 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003992 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003993 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003994 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003995 rtl_writephy(tp, 0x1f, 0x0000);
3996
3997 /* CHN EST parameters adjust - giga slave */
3998 rtl_writephy(tp, 0x1f, 0x0a43);
3999 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004000 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004001 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004002 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004003 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004004 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004005 rtl_writephy(tp, 0x1f, 0x0000);
4006
4007 /* CHN EST parameters adjust - fnet */
4008 rtl_writephy(tp, 0x1f, 0x0a43);
4009 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004010 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004011 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004012 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004013 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004014 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004015 rtl_writephy(tp, 0x1f, 0x0000);
4016
4017 /* enable R-tune & PGA-retune function */
4018 dout_tapbin = 0;
4019 rtl_writephy(tp, 0x1f, 0x0a46);
4020 data = rtl_readphy(tp, 0x13);
4021 data &= 3;
4022 data <<= 2;
4023 dout_tapbin |= data;
4024 data = rtl_readphy(tp, 0x12);
4025 data &= 0xc000;
4026 data >>= 14;
4027 dout_tapbin |= data;
4028 dout_tapbin = ~(dout_tapbin^0x08);
4029 dout_tapbin <<= 12;
4030 dout_tapbin &= 0xf000;
4031 rtl_writephy(tp, 0x1f, 0x0a43);
4032 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004033 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004034 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004035 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004036 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004037 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004038 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004039 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004040
4041 rtl_writephy(tp, 0x1f, 0x0a43);
4042 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004043 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004044 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004045 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004046 rtl_writephy(tp, 0x1f, 0x0000);
4047
4048 /* enable GPHY 10M */
4049 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004050 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004051 rtl_writephy(tp, 0x1f, 0x0000);
4052
4053 /* SAR ADC performance */
4054 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004055 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004056 rtl_writephy(tp, 0x1f, 0x0000);
4057
4058 rtl_writephy(tp, 0x1f, 0x0a43);
4059 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004060 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004061 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004062 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004063 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004064 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004065 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004066 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004067 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004068 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004069 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004070 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004071 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004072 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004073 rtl_writephy(tp, 0x1f, 0x0000);
4074
4075 /* disable phy pfm mode */
4076 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004077 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004078 rtl_writephy(tp, 0x1f, 0x0000);
4079
4080 /* Check ALDPS bit, disable it if enabled */
4081 rtl_writephy(tp, 0x1f, 0x0a43);
4082 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004083 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004084
4085 rtl_writephy(tp, 0x1f, 0x0000);
4086}
4087
4088static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4089{
4090 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4091 u16 rlen;
4092 u32 data;
4093
4094 rtl_apply_firmware(tp);
4095
4096 /* CHIN EST parameter update */
4097 rtl_writephy(tp, 0x1f, 0x0a43);
4098 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004099 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004100 rtl_writephy(tp, 0x1f, 0x0000);
4101
4102 /* enable R-tune & PGA-retune function */
4103 rtl_writephy(tp, 0x1f, 0x0a43);
4104 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004105 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004106 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004107 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004108 rtl_writephy(tp, 0x1f, 0x0000);
4109
4110 /* enable GPHY 10M */
4111 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004112 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004113 rtl_writephy(tp, 0x1f, 0x0000);
4114
4115 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4116 data = r8168_mac_ocp_read(tp, 0xdd02);
4117 ioffset_p3 = ((data & 0x80)>>7);
4118 ioffset_p3 <<= 3;
4119
4120 data = r8168_mac_ocp_read(tp, 0xdd00);
4121 ioffset_p3 |= ((data & (0xe000))>>13);
4122 ioffset_p2 = ((data & (0x1e00))>>9);
4123 ioffset_p1 = ((data & (0x01e0))>>5);
4124 ioffset_p0 = ((data & 0x0010)>>4);
4125 ioffset_p0 <<= 3;
4126 ioffset_p0 |= (data & (0x07));
4127 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4128
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004129 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004130 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004131 rtl_writephy(tp, 0x1f, 0x0bcf);
4132 rtl_writephy(tp, 0x16, data);
4133 rtl_writephy(tp, 0x1f, 0x0000);
4134 }
4135
4136 /* Modify rlen (TX LPF corner frequency) level */
4137 rtl_writephy(tp, 0x1f, 0x0bcd);
4138 data = rtl_readphy(tp, 0x16);
4139 data &= 0x000f;
4140 rlen = 0;
4141 if (data > 3)
4142 rlen = data - 3;
4143 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4144 rtl_writephy(tp, 0x17, data);
4145 rtl_writephy(tp, 0x1f, 0x0bcd);
4146 rtl_writephy(tp, 0x1f, 0x0000);
4147
4148 /* disable phy pfm mode */
4149 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004150 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004151 rtl_writephy(tp, 0x1f, 0x0000);
4152
4153 /* Check ALDPS bit, disable it if enabled */
4154 rtl_writephy(tp, 0x1f, 0x0a43);
4155 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004156 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004157
4158 rtl_writephy(tp, 0x1f, 0x0000);
4159}
4160
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004161static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4162{
4163 /* Enable PHY auto speed down */
4164 rtl_writephy(tp, 0x1f, 0x0a44);
4165 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4166 rtl_writephy(tp, 0x1f, 0x0000);
4167
4168 /* patch 10M & ALDPS */
4169 rtl_writephy(tp, 0x1f, 0x0bcc);
4170 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4171 rtl_writephy(tp, 0x1f, 0x0a44);
4172 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4173 rtl_writephy(tp, 0x1f, 0x0a43);
4174 rtl_writephy(tp, 0x13, 0x8084);
4175 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4176 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4177 rtl_writephy(tp, 0x1f, 0x0000);
4178
4179 /* Enable EEE auto-fallback function */
4180 rtl_writephy(tp, 0x1f, 0x0a4b);
4181 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4182 rtl_writephy(tp, 0x1f, 0x0000);
4183
4184 /* Enable UC LPF tune function */
4185 rtl_writephy(tp, 0x1f, 0x0a43);
4186 rtl_writephy(tp, 0x13, 0x8012);
4187 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4188 rtl_writephy(tp, 0x1f, 0x0000);
4189
4190 /* set rg_sel_sdm_rate */
4191 rtl_writephy(tp, 0x1f, 0x0c42);
4192 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4193 rtl_writephy(tp, 0x1f, 0x0000);
4194
4195 /* Check ALDPS bit, disable it if enabled */
4196 rtl_writephy(tp, 0x1f, 0x0a43);
4197 if (rtl_readphy(tp, 0x10) & 0x0004)
4198 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4199
4200 rtl_writephy(tp, 0x1f, 0x0000);
4201}
4202
4203static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4204{
4205 /* patch 10M & ALDPS */
4206 rtl_writephy(tp, 0x1f, 0x0bcc);
4207 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4208 rtl_writephy(tp, 0x1f, 0x0a44);
4209 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4210 rtl_writephy(tp, 0x1f, 0x0a43);
4211 rtl_writephy(tp, 0x13, 0x8084);
4212 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4213 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4214 rtl_writephy(tp, 0x1f, 0x0000);
4215
4216 /* Enable UC LPF tune function */
4217 rtl_writephy(tp, 0x1f, 0x0a43);
4218 rtl_writephy(tp, 0x13, 0x8012);
4219 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4220 rtl_writephy(tp, 0x1f, 0x0000);
4221
4222 /* Set rg_sel_sdm_rate */
4223 rtl_writephy(tp, 0x1f, 0x0c42);
4224 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4225 rtl_writephy(tp, 0x1f, 0x0000);
4226
4227 /* Channel estimation parameters */
4228 rtl_writephy(tp, 0x1f, 0x0a43);
4229 rtl_writephy(tp, 0x13, 0x80f3);
4230 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4231 rtl_writephy(tp, 0x13, 0x80f0);
4232 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4233 rtl_writephy(tp, 0x13, 0x80ef);
4234 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4235 rtl_writephy(tp, 0x13, 0x80f6);
4236 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4237 rtl_writephy(tp, 0x13, 0x80ec);
4238 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4239 rtl_writephy(tp, 0x13, 0x80ed);
4240 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4241 rtl_writephy(tp, 0x13, 0x80f2);
4242 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4243 rtl_writephy(tp, 0x13, 0x80f4);
4244 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4245 rtl_writephy(tp, 0x1f, 0x0a43);
4246 rtl_writephy(tp, 0x13, 0x8110);
4247 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4248 rtl_writephy(tp, 0x13, 0x810f);
4249 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4250 rtl_writephy(tp, 0x13, 0x8111);
4251 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4252 rtl_writephy(tp, 0x13, 0x8113);
4253 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4254 rtl_writephy(tp, 0x13, 0x8115);
4255 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4256 rtl_writephy(tp, 0x13, 0x810e);
4257 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4258 rtl_writephy(tp, 0x13, 0x810c);
4259 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4260 rtl_writephy(tp, 0x13, 0x810b);
4261 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4262 rtl_writephy(tp, 0x1f, 0x0a43);
4263 rtl_writephy(tp, 0x13, 0x80d1);
4264 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4265 rtl_writephy(tp, 0x13, 0x80cd);
4266 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4267 rtl_writephy(tp, 0x13, 0x80d3);
4268 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4269 rtl_writephy(tp, 0x13, 0x80d5);
4270 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4271 rtl_writephy(tp, 0x13, 0x80d7);
4272 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4273
4274 /* Force PWM-mode */
4275 rtl_writephy(tp, 0x1f, 0x0bcd);
4276 rtl_writephy(tp, 0x14, 0x5065);
4277 rtl_writephy(tp, 0x14, 0xd065);
4278 rtl_writephy(tp, 0x1f, 0x0bc8);
4279 rtl_writephy(tp, 0x12, 0x00ed);
4280 rtl_writephy(tp, 0x1f, 0x0bcd);
4281 rtl_writephy(tp, 0x14, 0x1065);
4282 rtl_writephy(tp, 0x14, 0x9065);
4283 rtl_writephy(tp, 0x14, 0x1065);
4284 rtl_writephy(tp, 0x1f, 0x0000);
4285
4286 /* Check ALDPS bit, disable it if enabled */
4287 rtl_writephy(tp, 0x1f, 0x0a43);
4288 if (rtl_readphy(tp, 0x10) & 0x0004)
4289 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4290
4291 rtl_writephy(tp, 0x1f, 0x0000);
4292}
4293
françois romieu4da19632011-01-03 15:07:55 +00004294static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004295{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004296 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004297 { 0x1f, 0x0003 },
4298 { 0x08, 0x441d },
4299 { 0x01, 0x9100 },
4300 { 0x1f, 0x0000 }
4301 };
4302
françois romieu4da19632011-01-03 15:07:55 +00004303 rtl_writephy(tp, 0x1f, 0x0000);
4304 rtl_patchphy(tp, 0x11, 1 << 12);
4305 rtl_patchphy(tp, 0x19, 1 << 13);
4306 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004307
françois romieu4da19632011-01-03 15:07:55 +00004308 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004309}
4310
Hayes Wang5a5e4442011-02-22 17:26:21 +08004311static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4312{
4313 static const struct phy_reg phy_reg_init[] = {
4314 { 0x1f, 0x0005 },
4315 { 0x1a, 0x0000 },
4316 { 0x1f, 0x0000 },
4317
4318 { 0x1f, 0x0004 },
4319 { 0x1c, 0x0000 },
4320 { 0x1f, 0x0000 },
4321
4322 { 0x1f, 0x0001 },
4323 { 0x15, 0x7701 },
4324 { 0x1f, 0x0000 }
4325 };
4326
4327 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004328 rtl_writephy(tp, 0x1f, 0x0000);
4329 rtl_writephy(tp, 0x18, 0x0310);
4330 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004331
François Romieu953a12c2011-04-24 17:38:48 +02004332 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004333
4334 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4335}
4336
Hayes Wang7e18dca2012-03-30 14:33:02 +08004337static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4338{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004339 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004340 rtl_writephy(tp, 0x1f, 0x0000);
4341 rtl_writephy(tp, 0x18, 0x0310);
4342 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004343
4344 rtl_apply_firmware(tp);
4345
4346 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004347 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004348 rtl_writephy(tp, 0x1f, 0x0004);
4349 rtl_writephy(tp, 0x10, 0x401f);
4350 rtl_writephy(tp, 0x19, 0x7030);
4351 rtl_writephy(tp, 0x1f, 0x0000);
4352}
4353
Hayes Wang5598bfe2012-07-02 17:23:21 +08004354static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4355{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004356 static const struct phy_reg phy_reg_init[] = {
4357 { 0x1f, 0x0004 },
4358 { 0x10, 0xc07f },
4359 { 0x19, 0x7030 },
4360 { 0x1f, 0x0000 }
4361 };
4362
4363 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004364 rtl_writephy(tp, 0x1f, 0x0000);
4365 rtl_writephy(tp, 0x18, 0x0310);
4366 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004367
4368 rtl_apply_firmware(tp);
4369
Francois Romieufdf6fc02012-07-06 22:40:38 +02004370 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004371 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4372
Francois Romieufdf6fc02012-07-06 22:40:38 +02004373 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004374}
4375
Francois Romieu5615d9f2007-08-17 17:50:46 +02004376static void rtl_hw_phy_config(struct net_device *dev)
4377{
4378 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004379
4380 rtl8169_print_mac_version(tp);
4381
4382 switch (tp->mac_version) {
4383 case RTL_GIGA_MAC_VER_01:
4384 break;
4385 case RTL_GIGA_MAC_VER_02:
4386 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004387 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004388 break;
4389 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004390 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004391 break;
françois romieu2e9558562009-08-10 19:44:19 +00004392 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004393 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004394 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004395 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004396 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004397 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004398 case RTL_GIGA_MAC_VER_07:
4399 case RTL_GIGA_MAC_VER_08:
4400 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004401 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004402 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004403 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004404 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004405 break;
4406 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004407 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004408 break;
4409 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004410 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004411 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004412 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004413 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004414 break;
4415 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004416 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004417 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004418 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004419 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004420 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004421 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004422 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004423 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004424 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004425 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004426 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004427 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004428 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004429 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004430 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004431 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004432 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004433 break;
4434 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004435 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004436 break;
4437 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004438 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004439 break;
françois romieue6de30d2011-01-03 15:08:37 +00004440 case RTL_GIGA_MAC_VER_28:
4441 rtl8168d_4_hw_phy_config(tp);
4442 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004443 case RTL_GIGA_MAC_VER_29:
4444 case RTL_GIGA_MAC_VER_30:
4445 rtl8105e_hw_phy_config(tp);
4446 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004447 case RTL_GIGA_MAC_VER_31:
4448 /* None. */
4449 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004450 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004451 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004452 rtl8168e_1_hw_phy_config(tp);
4453 break;
4454 case RTL_GIGA_MAC_VER_34:
4455 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004456 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004457 case RTL_GIGA_MAC_VER_35:
4458 rtl8168f_1_hw_phy_config(tp);
4459 break;
4460 case RTL_GIGA_MAC_VER_36:
4461 rtl8168f_2_hw_phy_config(tp);
4462 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004463
Hayes Wang7e18dca2012-03-30 14:33:02 +08004464 case RTL_GIGA_MAC_VER_37:
4465 rtl8402_hw_phy_config(tp);
4466 break;
4467
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004468 case RTL_GIGA_MAC_VER_38:
4469 rtl8411_hw_phy_config(tp);
4470 break;
4471
Hayes Wang5598bfe2012-07-02 17:23:21 +08004472 case RTL_GIGA_MAC_VER_39:
4473 rtl8106e_hw_phy_config(tp);
4474 break;
4475
Hayes Wangc5583862012-07-02 17:23:22 +08004476 case RTL_GIGA_MAC_VER_40:
4477 rtl8168g_1_hw_phy_config(tp);
4478 break;
hayeswang57538c42013-04-01 22:23:40 +00004479 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004480 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004481 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004482 rtl8168g_2_hw_phy_config(tp);
4483 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004484 case RTL_GIGA_MAC_VER_45:
4485 case RTL_GIGA_MAC_VER_47:
4486 rtl8168h_1_hw_phy_config(tp);
4487 break;
4488 case RTL_GIGA_MAC_VER_46:
4489 case RTL_GIGA_MAC_VER_48:
4490 rtl8168h_2_hw_phy_config(tp);
4491 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004492
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004493 case RTL_GIGA_MAC_VER_49:
4494 rtl8168ep_1_hw_phy_config(tp);
4495 break;
4496 case RTL_GIGA_MAC_VER_50:
4497 case RTL_GIGA_MAC_VER_51:
4498 rtl8168ep_2_hw_phy_config(tp);
4499 break;
4500
Hayes Wangc5583862012-07-02 17:23:22 +08004501 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004502 default:
4503 break;
4504 }
4505}
4506
Francois Romieuda78dbf2012-01-26 14:18:23 +01004507static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4511
Francois Romieubcf0bf92006-07-26 23:14:13 +02004512 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
françois romieu4da19632011-01-03 15:07:55 +00004514 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004515 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 * A busy loop could burn quite a few cycles on nowadays CPU.
4517 * Let's delay the execution of the timer for a few ticks.
4518 */
4519 timeout = HZ/10;
4520 goto out_mod_timer;
4521 }
4522
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004523 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004526 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
françois romieu4da19632011-01-03 15:07:55 +00004528 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530out_mod_timer:
4531 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004532}
4533
4534static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4535{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004536 if (!test_and_set_bit(flag, tp->wk.flags))
4537 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004538}
4539
Kees Cook9de36cc2017-10-25 03:53:12 -07004540static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004541{
Kees Cook9de36cc2017-10-25 03:53:12 -07004542 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004543
Francois Romieu98ddf982012-01-31 10:47:34 +01004544 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Francois Romieuffc46952012-07-06 14:19:23 +02004547DECLARE_RTL_COND(rtl_phy_reset_cond)
4548{
4549 return tp->phy_reset_pending(tp);
4550}
4551
Francois Romieubf793292006-11-01 00:53:05 +01004552static void rtl8169_phy_reset(struct net_device *dev,
4553 struct rtl8169_private *tp)
4554{
françois romieu4da19632011-01-03 15:07:55 +00004555 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004556 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004557}
4558
David S. Miller8decf862011-09-22 03:23:13 -04004559static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4560{
David S. Miller8decf862011-09-22 03:23:13 -04004561 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004562 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004563}
4564
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004565static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004567 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004568
Marcus Sundberg773328942008-07-10 21:28:08 +02004569 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4570 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004571 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004572 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004573
Francois Romieu6dccd162007-02-13 23:38:05 +01004574 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4575
4576 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4577 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004578
Francois Romieubcf0bf92006-07-26 23:14:13 +02004579 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004580 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004581 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004582 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004583 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004584 }
4585
Francois Romieubf793292006-11-01 00:53:05 +01004586 rtl8169_phy_reset(dev, tp);
4587
Oliver Neukum54405cd2011-01-06 21:55:13 +01004588 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004589 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4590 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4591 (tp->mii.supports_gmii ?
4592 ADVERTISED_1000baseT_Half |
4593 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004594
David S. Miller8decf862011-09-22 03:23:13 -04004595 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004596 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004597}
4598
Francois Romieu773d2022007-01-31 23:47:43 +01004599static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4600{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004601 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004602
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004603 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004604
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004605 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4606 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004607
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004608 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4609 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004610
françois romieu9ecb9aa2012-12-07 11:20:21 +00004611 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4612 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004613
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004614 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004615
Francois Romieuda78dbf2012-01-26 14:18:23 +01004616 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004617}
4618
4619static int rtl_set_mac_address(struct net_device *dev, void *p)
4620{
4621 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004622 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004623 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004624
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004625 ret = eth_mac_addr(dev, p);
4626 if (ret)
4627 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004628
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004629 pm_runtime_get_noresume(d);
4630
4631 if (pm_runtime_active(d))
4632 rtl_rar_set(tp, dev->dev_addr);
4633
4634 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004635
4636 return 0;
4637}
4638
Francois Romieu5f787a12006-08-17 13:02:36 +02004639static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4640{
4641 struct rtl8169_private *tp = netdev_priv(dev);
4642 struct mii_ioctl_data *data = if_mii(ifr);
4643
Francois Romieu8b4ab282008-11-19 22:05:25 -08004644 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4645}
Francois Romieu5f787a12006-08-17 13:02:36 +02004646
Francois Romieucecb5fd2011-04-01 10:21:07 +02004647static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4648 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004649{
Francois Romieu5f787a12006-08-17 13:02:36 +02004650 switch (cmd) {
4651 case SIOCGMIIPHY:
4652 data->phy_id = 32; /* Internal PHY */
4653 return 0;
4654
4655 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004656 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004657 return 0;
4658
4659 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004660 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004661 return 0;
4662 }
4663 return -EOPNOTSUPP;
4664}
4665
Francois Romieu8b4ab282008-11-19 22:05:25 -08004666static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4667{
4668 return -EOPNOTSUPP;
4669}
4670
Bill Pembertonbaf63292012-12-03 09:23:28 -05004671static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004672{
4673 struct mdio_ops *ops = &tp->mdio_ops;
4674
4675 switch (tp->mac_version) {
4676 case RTL_GIGA_MAC_VER_27:
4677 ops->write = r8168dp_1_mdio_write;
4678 ops->read = r8168dp_1_mdio_read;
4679 break;
françois romieue6de30d2011-01-03 15:08:37 +00004680 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004681 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004682 ops->write = r8168dp_2_mdio_write;
4683 ops->read = r8168dp_2_mdio_read;
4684 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004685 case RTL_GIGA_MAC_VER_40:
4686 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004687 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004688 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004689 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004690 case RTL_GIGA_MAC_VER_45:
4691 case RTL_GIGA_MAC_VER_46:
4692 case RTL_GIGA_MAC_VER_47:
4693 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004694 case RTL_GIGA_MAC_VER_49:
4695 case RTL_GIGA_MAC_VER_50:
4696 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004697 ops->write = r8168g_mdio_write;
4698 ops->read = r8168g_mdio_read;
4699 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004700 default:
4701 ops->write = r8169_mdio_write;
4702 ops->read = r8169_mdio_read;
4703 break;
4704 }
4705}
4706
hayeswange2409d82013-03-31 17:02:04 +00004707static void rtl_speed_down(struct rtl8169_private *tp)
4708{
4709 u32 adv;
4710 int lpa;
4711
4712 rtl_writephy(tp, 0x1f, 0x0000);
4713 lpa = rtl_readphy(tp, MII_LPA);
4714
4715 if (lpa & (LPA_10HALF | LPA_10FULL))
4716 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4717 else if (lpa & (LPA_100HALF | LPA_100FULL))
4718 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4719 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4720 else
4721 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4722 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4723 (tp->mii.supports_gmii ?
4724 ADVERTISED_1000baseT_Half |
4725 ADVERTISED_1000baseT_Full : 0);
4726
4727 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4728 adv);
4729}
4730
David S. Miller1805b2f2011-10-24 18:18:09 -04004731static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4732{
David S. Miller1805b2f2011-10-24 18:18:09 -04004733 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004734 case RTL_GIGA_MAC_VER_25:
4735 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004736 case RTL_GIGA_MAC_VER_29:
4737 case RTL_GIGA_MAC_VER_30:
4738 case RTL_GIGA_MAC_VER_32:
4739 case RTL_GIGA_MAC_VER_33:
4740 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004741 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004742 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004743 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004744 case RTL_GIGA_MAC_VER_40:
4745 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004746 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004747 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004748 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004749 case RTL_GIGA_MAC_VER_45:
4750 case RTL_GIGA_MAC_VER_46:
4751 case RTL_GIGA_MAC_VER_47:
4752 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004753 case RTL_GIGA_MAC_VER_49:
4754 case RTL_GIGA_MAC_VER_50:
4755 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004756 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004757 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4758 break;
4759 default:
4760 break;
4761 }
4762}
4763
4764static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4765{
4766 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4767 return false;
4768
hayeswange2409d82013-03-31 17:02:04 +00004769 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004770 rtl_wol_suspend_quirk(tp);
4771
4772 return true;
4773}
4774
françois romieu065c27c2011-01-03 15:08:12 +00004775static void r810x_phy_power_down(struct rtl8169_private *tp)
4776{
4777 rtl_writephy(tp, 0x1f, 0x0000);
4778 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4779}
4780
4781static void r810x_phy_power_up(struct rtl8169_private *tp)
4782{
4783 rtl_writephy(tp, 0x1f, 0x0000);
4784 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4785}
4786
4787static void r810x_pll_power_down(struct rtl8169_private *tp)
4788{
David S. Miller1805b2f2011-10-24 18:18:09 -04004789 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004790 return;
françois romieu065c27c2011-01-03 15:08:12 +00004791
4792 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004793
4794 switch (tp->mac_version) {
4795 case RTL_GIGA_MAC_VER_07:
4796 case RTL_GIGA_MAC_VER_08:
4797 case RTL_GIGA_MAC_VER_09:
4798 case RTL_GIGA_MAC_VER_10:
4799 case RTL_GIGA_MAC_VER_13:
4800 case RTL_GIGA_MAC_VER_16:
4801 break;
4802 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004803 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004804 break;
4805 }
françois romieu065c27c2011-01-03 15:08:12 +00004806}
4807
4808static void r810x_pll_power_up(struct rtl8169_private *tp)
4809{
4810 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004811
4812 switch (tp->mac_version) {
4813 case RTL_GIGA_MAC_VER_07:
4814 case RTL_GIGA_MAC_VER_08:
4815 case RTL_GIGA_MAC_VER_09:
4816 case RTL_GIGA_MAC_VER_10:
4817 case RTL_GIGA_MAC_VER_13:
4818 case RTL_GIGA_MAC_VER_16:
4819 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004820 case RTL_GIGA_MAC_VER_47:
4821 case RTL_GIGA_MAC_VER_48:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004822 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004823 break;
Hayes Wang00042992012-03-30 14:33:00 +08004824 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004825 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004826 break;
4827 }
françois romieu065c27c2011-01-03 15:08:12 +00004828}
4829
4830static void r8168_phy_power_up(struct rtl8169_private *tp)
4831{
4832 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004833 switch (tp->mac_version) {
4834 case RTL_GIGA_MAC_VER_11:
4835 case RTL_GIGA_MAC_VER_12:
4836 case RTL_GIGA_MAC_VER_17:
4837 case RTL_GIGA_MAC_VER_18:
4838 case RTL_GIGA_MAC_VER_19:
4839 case RTL_GIGA_MAC_VER_20:
4840 case RTL_GIGA_MAC_VER_21:
4841 case RTL_GIGA_MAC_VER_22:
4842 case RTL_GIGA_MAC_VER_23:
4843 case RTL_GIGA_MAC_VER_24:
4844 case RTL_GIGA_MAC_VER_25:
4845 case RTL_GIGA_MAC_VER_26:
4846 case RTL_GIGA_MAC_VER_27:
4847 case RTL_GIGA_MAC_VER_28:
4848 case RTL_GIGA_MAC_VER_31:
4849 rtl_writephy(tp, 0x0e, 0x0000);
4850 break;
4851 default:
4852 break;
4853 }
françois romieu065c27c2011-01-03 15:08:12 +00004854 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4855}
4856
4857static void r8168_phy_power_down(struct rtl8169_private *tp)
4858{
4859 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004860 switch (tp->mac_version) {
4861 case RTL_GIGA_MAC_VER_32:
4862 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004863 case RTL_GIGA_MAC_VER_40:
4864 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004865 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4866 break;
4867
4868 case RTL_GIGA_MAC_VER_11:
4869 case RTL_GIGA_MAC_VER_12:
4870 case RTL_GIGA_MAC_VER_17:
4871 case RTL_GIGA_MAC_VER_18:
4872 case RTL_GIGA_MAC_VER_19:
4873 case RTL_GIGA_MAC_VER_20:
4874 case RTL_GIGA_MAC_VER_21:
4875 case RTL_GIGA_MAC_VER_22:
4876 case RTL_GIGA_MAC_VER_23:
4877 case RTL_GIGA_MAC_VER_24:
4878 case RTL_GIGA_MAC_VER_25:
4879 case RTL_GIGA_MAC_VER_26:
4880 case RTL_GIGA_MAC_VER_27:
4881 case RTL_GIGA_MAC_VER_28:
4882 case RTL_GIGA_MAC_VER_31:
4883 rtl_writephy(tp, 0x0e, 0x0200);
4884 default:
4885 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4886 break;
4887 }
françois romieu065c27c2011-01-03 15:08:12 +00004888}
4889
4890static void r8168_pll_power_down(struct rtl8169_private *tp)
4891{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004892 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004893 return;
4894
Francois Romieucecb5fd2011-04-01 10:21:07 +02004895 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4896 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004897 (RTL_R16(tp, CPlusCmd) & ASF)) {
françois romieu065c27c2011-01-03 15:08:12 +00004898 return;
4899 }
4900
hayeswang01dc7fe2011-03-21 01:50:28 +00004901 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4902 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004903 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004904
David S. Miller1805b2f2011-10-24 18:18:09 -04004905 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004906 return;
françois romieu065c27c2011-01-03 15:08:12 +00004907
4908 r8168_phy_power_down(tp);
4909
4910 switch (tp->mac_version) {
4911 case RTL_GIGA_MAC_VER_25:
4912 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004913 case RTL_GIGA_MAC_VER_27:
4914 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004915 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004916 case RTL_GIGA_MAC_VER_32:
4917 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004918 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004919 case RTL_GIGA_MAC_VER_45:
4920 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004921 case RTL_GIGA_MAC_VER_50:
4922 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004923 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004924 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004925 case RTL_GIGA_MAC_VER_40:
4926 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004927 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004928 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004929 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004930 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004931 break;
françois romieu065c27c2011-01-03 15:08:12 +00004932 }
4933}
4934
4935static void r8168_pll_power_up(struct rtl8169_private *tp)
4936{
françois romieu065c27c2011-01-03 15:08:12 +00004937 switch (tp->mac_version) {
4938 case RTL_GIGA_MAC_VER_25:
4939 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004940 case RTL_GIGA_MAC_VER_27:
4941 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004942 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004943 case RTL_GIGA_MAC_VER_32:
4944 case RTL_GIGA_MAC_VER_33:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004945 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004946 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004947 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004948 case RTL_GIGA_MAC_VER_45:
4949 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004950 case RTL_GIGA_MAC_VER_50:
4951 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004952 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004953 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004954 case RTL_GIGA_MAC_VER_40:
4955 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004956 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004957 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004958 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004959 0x00000000, ERIAR_EXGMAC);
4960 break;
françois romieu065c27c2011-01-03 15:08:12 +00004961 }
4962
4963 r8168_phy_power_up(tp);
4964}
4965
Francois Romieud58d46b2011-05-03 16:38:29 +02004966static void rtl_generic_op(struct rtl8169_private *tp,
4967 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004968{
4969 if (op)
4970 op(tp);
4971}
4972
4973static void rtl_pll_power_down(struct rtl8169_private *tp)
4974{
Francois Romieud58d46b2011-05-03 16:38:29 +02004975 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004976}
4977
4978static void rtl_pll_power_up(struct rtl8169_private *tp)
4979{
Francois Romieud58d46b2011-05-03 16:38:29 +02004980 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004981}
4982
Bill Pembertonbaf63292012-12-03 09:23:28 -05004983static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004984{
4985 struct pll_power_ops *ops = &tp->pll_power_ops;
4986
4987 switch (tp->mac_version) {
4988 case RTL_GIGA_MAC_VER_07:
4989 case RTL_GIGA_MAC_VER_08:
4990 case RTL_GIGA_MAC_VER_09:
4991 case RTL_GIGA_MAC_VER_10:
4992 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004993 case RTL_GIGA_MAC_VER_29:
4994 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004995 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004996 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004997 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004998 case RTL_GIGA_MAC_VER_47:
4999 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005000 ops->down = r810x_pll_power_down;
5001 ops->up = r810x_pll_power_up;
5002 break;
5003
5004 case RTL_GIGA_MAC_VER_11:
5005 case RTL_GIGA_MAC_VER_12:
5006 case RTL_GIGA_MAC_VER_17:
5007 case RTL_GIGA_MAC_VER_18:
5008 case RTL_GIGA_MAC_VER_19:
5009 case RTL_GIGA_MAC_VER_20:
5010 case RTL_GIGA_MAC_VER_21:
5011 case RTL_GIGA_MAC_VER_22:
5012 case RTL_GIGA_MAC_VER_23:
5013 case RTL_GIGA_MAC_VER_24:
5014 case RTL_GIGA_MAC_VER_25:
5015 case RTL_GIGA_MAC_VER_26:
5016 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005017 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005018 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005019 case RTL_GIGA_MAC_VER_32:
5020 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005021 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005022 case RTL_GIGA_MAC_VER_35:
5023 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005024 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005025 case RTL_GIGA_MAC_VER_40:
5026 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005027 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005028 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005029 case RTL_GIGA_MAC_VER_45:
5030 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005031 case RTL_GIGA_MAC_VER_49:
5032 case RTL_GIGA_MAC_VER_50:
5033 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005034 ops->down = r8168_pll_power_down;
5035 ops->up = r8168_pll_power_up;
5036 break;
5037
5038 default:
5039 ops->down = NULL;
5040 ops->up = NULL;
5041 break;
5042 }
5043}
5044
Hayes Wange542a222011-07-06 15:58:04 +08005045static void rtl_init_rxcfg(struct rtl8169_private *tp)
5046{
Hayes Wange542a222011-07-06 15:58:04 +08005047 switch (tp->mac_version) {
5048 case RTL_GIGA_MAC_VER_01:
5049 case RTL_GIGA_MAC_VER_02:
5050 case RTL_GIGA_MAC_VER_03:
5051 case RTL_GIGA_MAC_VER_04:
5052 case RTL_GIGA_MAC_VER_05:
5053 case RTL_GIGA_MAC_VER_06:
5054 case RTL_GIGA_MAC_VER_10:
5055 case RTL_GIGA_MAC_VER_11:
5056 case RTL_GIGA_MAC_VER_12:
5057 case RTL_GIGA_MAC_VER_13:
5058 case RTL_GIGA_MAC_VER_14:
5059 case RTL_GIGA_MAC_VER_15:
5060 case RTL_GIGA_MAC_VER_16:
5061 case RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005062 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005063 break;
5064 case RTL_GIGA_MAC_VER_18:
5065 case RTL_GIGA_MAC_VER_19:
5066 case RTL_GIGA_MAC_VER_20:
5067 case RTL_GIGA_MAC_VER_21:
5068 case RTL_GIGA_MAC_VER_22:
5069 case RTL_GIGA_MAC_VER_23:
5070 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005071 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005072 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005073 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005074 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005075 case RTL_GIGA_MAC_VER_40:
5076 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005077 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005078 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005079 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005080 case RTL_GIGA_MAC_VER_45:
5081 case RTL_GIGA_MAC_VER_46:
5082 case RTL_GIGA_MAC_VER_47:
5083 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005084 case RTL_GIGA_MAC_VER_49:
5085 case RTL_GIGA_MAC_VER_50:
5086 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005087 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005088 break;
Hayes Wange542a222011-07-06 15:58:04 +08005089 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005090 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005091 break;
5092 }
5093}
5094
Hayes Wang92fc43b2011-07-06 15:58:03 +08005095static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5096{
Timo Teräs9fba0812013-01-15 21:01:24 +00005097 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005098}
5099
Francois Romieud58d46b2011-05-03 16:38:29 +02005100static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5101{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005102 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005103 rtl_generic_op(tp, tp->jumbo_ops.enable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005104 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005105}
5106
5107static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5108{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005109 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005110 rtl_generic_op(tp, tp->jumbo_ops.disable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005111 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005112}
5113
5114static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5115{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005116 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5117 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01005118 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005119}
5120
5121static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5122{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005123 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5124 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005125 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005126}
5127
5128static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5129{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005130 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005131}
5132
5133static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5134{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005135 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005136}
5137
5138static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5139{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005140 RTL_W8(tp, MaxTxPacketSize, 0x3f);
5141 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5142 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01005143 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005144}
5145
5146static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5147{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005148 RTL_W8(tp, MaxTxPacketSize, 0x0c);
5149 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5150 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005151 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005152}
5153
5154static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5155{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005156 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005157 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005158}
5159
5160static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5161{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005162 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005163 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005164}
5165
5166static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5167{
Francois Romieud58d46b2011-05-03 16:38:29 +02005168 r8168b_0_hw_jumbo_enable(tp);
5169
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005170 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005171}
5172
5173static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5174{
Francois Romieud58d46b2011-05-03 16:38:29 +02005175 r8168b_0_hw_jumbo_disable(tp);
5176
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005177 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005178}
5179
Bill Pembertonbaf63292012-12-03 09:23:28 -05005180static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005181{
5182 struct jumbo_ops *ops = &tp->jumbo_ops;
5183
5184 switch (tp->mac_version) {
5185 case RTL_GIGA_MAC_VER_11:
5186 ops->disable = r8168b_0_hw_jumbo_disable;
5187 ops->enable = r8168b_0_hw_jumbo_enable;
5188 break;
5189 case RTL_GIGA_MAC_VER_12:
5190 case RTL_GIGA_MAC_VER_17:
5191 ops->disable = r8168b_1_hw_jumbo_disable;
5192 ops->enable = r8168b_1_hw_jumbo_enable;
5193 break;
5194 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5195 case RTL_GIGA_MAC_VER_19:
5196 case RTL_GIGA_MAC_VER_20:
5197 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5198 case RTL_GIGA_MAC_VER_22:
5199 case RTL_GIGA_MAC_VER_23:
5200 case RTL_GIGA_MAC_VER_24:
5201 case RTL_GIGA_MAC_VER_25:
5202 case RTL_GIGA_MAC_VER_26:
5203 ops->disable = r8168c_hw_jumbo_disable;
5204 ops->enable = r8168c_hw_jumbo_enable;
5205 break;
5206 case RTL_GIGA_MAC_VER_27:
5207 case RTL_GIGA_MAC_VER_28:
5208 ops->disable = r8168dp_hw_jumbo_disable;
5209 ops->enable = r8168dp_hw_jumbo_enable;
5210 break;
5211 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5212 case RTL_GIGA_MAC_VER_32:
5213 case RTL_GIGA_MAC_VER_33:
5214 case RTL_GIGA_MAC_VER_34:
5215 ops->disable = r8168e_hw_jumbo_disable;
5216 ops->enable = r8168e_hw_jumbo_enable;
5217 break;
5218
5219 /*
5220 * No action needed for jumbo frames with 8169.
5221 * No jumbo for 810x at all.
5222 */
Hayes Wangc5583862012-07-02 17:23:22 +08005223 case RTL_GIGA_MAC_VER_40:
5224 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005225 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005226 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005227 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005228 case RTL_GIGA_MAC_VER_45:
5229 case RTL_GIGA_MAC_VER_46:
5230 case RTL_GIGA_MAC_VER_47:
5231 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005232 case RTL_GIGA_MAC_VER_49:
5233 case RTL_GIGA_MAC_VER_50:
5234 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005235 default:
5236 ops->disable = NULL;
5237 ops->enable = NULL;
5238 break;
5239 }
5240}
5241
Francois Romieuffc46952012-07-06 14:19:23 +02005242DECLARE_RTL_COND(rtl_chipcmd_cond)
5243{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005244 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02005245}
5246
Francois Romieu6f43adc2011-04-29 15:05:51 +02005247static void rtl_hw_reset(struct rtl8169_private *tp)
5248{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005249 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005250
Francois Romieuffc46952012-07-06 14:19:23 +02005251 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005252}
5253
Francois Romieub6ffd972011-06-17 17:00:05 +02005254static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5255{
5256 struct rtl_fw *rtl_fw;
5257 const char *name;
5258 int rc = -ENOMEM;
5259
5260 name = rtl_lookup_firmware_name(tp);
5261 if (!name)
5262 goto out_no_firmware;
5263
5264 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5265 if (!rtl_fw)
5266 goto err_warn;
5267
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005268 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02005269 if (rc < 0)
5270 goto err_free;
5271
Francois Romieufd112f22011-06-18 00:10:29 +02005272 rc = rtl_check_firmware(tp, rtl_fw);
5273 if (rc < 0)
5274 goto err_release_firmware;
5275
Francois Romieub6ffd972011-06-17 17:00:05 +02005276 tp->rtl_fw = rtl_fw;
5277out:
5278 return;
5279
Francois Romieufd112f22011-06-18 00:10:29 +02005280err_release_firmware:
5281 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005282err_free:
5283 kfree(rtl_fw);
5284err_warn:
5285 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5286 name, rc);
5287out_no_firmware:
5288 tp->rtl_fw = NULL;
5289 goto out;
5290}
5291
François Romieu953a12c2011-04-24 17:38:48 +02005292static void rtl_request_firmware(struct rtl8169_private *tp)
5293{
Francois Romieub6ffd972011-06-17 17:00:05 +02005294 if (IS_ERR(tp->rtl_fw))
5295 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005296}
5297
Hayes Wang92fc43b2011-07-06 15:58:03 +08005298static void rtl_rx_close(struct rtl8169_private *tp)
5299{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005300 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005301}
5302
Francois Romieuffc46952012-07-06 14:19:23 +02005303DECLARE_RTL_COND(rtl_npq_cond)
5304{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005305 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005306}
5307
5308DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5309{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005310 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005311}
5312
françois romieue6de30d2011-01-03 15:08:37 +00005313static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314{
5315 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005316 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
Hayes Wang92fc43b2011-07-06 15:58:03 +08005318 rtl_rx_close(tp);
5319
Hayes Wang5d2e1952011-02-22 17:26:22 +08005320 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005321 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5322 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005323 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005324 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005325 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5326 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5327 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5328 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5330 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5333 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5334 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5335 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5336 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005337 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5338 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5339 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5340 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005341 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005342 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005343 } else {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005344 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005345 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005346 }
5347
Hayes Wang92fc43b2011-07-06 15:58:03 +08005348 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
Francois Romieu7f796d832007-06-11 23:04:41 +02005351static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005352{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005353 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005354 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005355 (InterFrameGap << TxInterFrameGapShift));
5356}
5357
Francois Romieu07ce4062007-02-23 23:36:39 +01005358static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
5360 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Francois Romieu07ce4062007-02-23 23:36:39 +01005362 tp->hw_start(dev);
5363
Francois Romieuda78dbf2012-01-26 14:18:23 +01005364 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005365}
5366
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005367static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005368{
5369 /*
5370 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5371 * register to be written before TxDescAddrLow to work.
5372 * Switching from MMIO to I/O access fixes the issue as well.
5373 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005374 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5375 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5376 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5377 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005378}
5379
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005380static u16 rtl_rw_cpluscmd(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005381{
5382 u16 cmd;
5383
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005384 cmd = RTL_R16(tp, CPlusCmd);
5385 RTL_W16(tp, CPlusCmd, cmd);
Francois Romieu7f796d832007-06-11 23:04:41 +02005386 return cmd;
5387}
5388
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005389static void rtl_set_rx_max_size(struct rtl8169_private *tp, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005390{
5391 /* Low hurts. Let's disable the filtering. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005392 RTL_W16(tp, RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005393}
5394
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005395static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005396{
Francois Romieu37441002011-06-17 22:58:54 +02005397 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005398 u32 mac_version;
5399 u32 clk;
5400 u32 val;
5401 } cfg2_info [] = {
5402 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5403 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5404 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5405 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005406 };
5407 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005408 unsigned int i;
5409 u32 clk;
5410
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005411 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005412 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005413 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005414 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005415 break;
5416 }
5417 }
5418}
5419
Francois Romieue6b763e2012-03-08 09:35:39 +01005420static void rtl_set_rx_mode(struct net_device *dev)
5421{
5422 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005423 u32 mc_filter[2]; /* Multicast hash filter */
5424 int rx_mode;
5425 u32 tmp = 0;
5426
5427 if (dev->flags & IFF_PROMISC) {
5428 /* Unconditionally log net taps. */
5429 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5430 rx_mode =
5431 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5432 AcceptAllPhys;
5433 mc_filter[1] = mc_filter[0] = 0xffffffff;
5434 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5435 (dev->flags & IFF_ALLMULTI)) {
5436 /* Too many to filter perfectly -- accept all multicasts. */
5437 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5438 mc_filter[1] = mc_filter[0] = 0xffffffff;
5439 } else {
5440 struct netdev_hw_addr *ha;
5441
5442 rx_mode = AcceptBroadcast | AcceptMyPhys;
5443 mc_filter[1] = mc_filter[0] = 0;
5444 netdev_for_each_mc_addr(ha, dev) {
5445 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5446 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5447 rx_mode |= AcceptMulticast;
5448 }
5449 }
5450
5451 if (dev->features & NETIF_F_RXALL)
5452 rx_mode |= (AcceptErr | AcceptRunt);
5453
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005454 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005455
5456 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5457 u32 data = mc_filter[0];
5458
5459 mc_filter[0] = swab32(mc_filter[1]);
5460 mc_filter[1] = swab32(data);
5461 }
5462
Nathan Walp04817762012-11-01 12:08:47 +00005463 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5464 mc_filter[1] = mc_filter[0] = 0xffffffff;
5465
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005466 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5467 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005468
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005469 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005470}
5471
Francois Romieu07ce4062007-02-23 23:36:39 +01005472static void rtl_hw_start_8169(struct net_device *dev)
5473{
5474 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu07ce4062007-02-23 23:36:39 +01005475 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005476
Francois Romieu9cb427b2006-11-02 00:10:16 +01005477 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005478 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) | PCIMulRW);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005479 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5480 }
5481
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005482 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005483 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5484 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5485 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5486 tp->mac_version == RTL_GIGA_MAC_VER_04)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005487 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005488
Hayes Wange542a222011-07-06 15:58:04 +08005489 rtl_init_rxcfg(tp);
5490
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005491 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005493 rtl_set_rx_max_size(tp, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
Francois Romieucecb5fd2011-04-01 10:21:07 +02005495 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5496 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5497 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5498 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005499 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005501 tp->cp_cmd |= rtl_rw_cpluscmd(tp) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005502
Francois Romieucecb5fd2011-04-01 10:21:07 +02005503 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5504 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005505 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005507 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005510 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005511
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005512 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /*
5515 * Undocumented corner. Supposedly:
5516 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5517 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005518 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005520 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005521
Francois Romieucecb5fd2011-04-01 10:21:07 +02005522 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5523 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5524 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5525 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005526 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieuc946b302007-10-04 00:42:50 +02005527 rtl_set_rx_tx_config_registers(tp);
5528 }
5529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005530 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005531
5532 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005533 RTL_R8(tp, IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005535 RTL_W32(tp, RxMissed, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Francois Romieu07ce4062007-02-23 23:36:39 +01005537 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539 /* no early-rx interrupts */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005540 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005541}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005543static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5544{
5545 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005546 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005547}
5548
5549static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5550{
Francois Romieu52989f02012-07-06 13:37:00 +02005551 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005552}
5553
5554static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005555{
5556 u32 csi;
5557
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005558 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5559 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005560}
5561
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005562static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005563{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005564 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005565}
5566
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005567static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005568{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005569 rtl_csi_access_enable(tp, 0x27000000);
5570}
5571
Francois Romieuffc46952012-07-06 14:19:23 +02005572DECLARE_RTL_COND(rtl_csiar_cond)
5573{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005574 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005575}
5576
Francois Romieu52989f02012-07-06 13:37:00 +02005577static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005578{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005579 RTL_W32(tp, CSIDR, value);
5580 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005581 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5582
Francois Romieuffc46952012-07-06 14:19:23 +02005583 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005584}
5585
Francois Romieu52989f02012-07-06 13:37:00 +02005586static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005587{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005588 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005589 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5590
Francois Romieuffc46952012-07-06 14:19:23 +02005591 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005592 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005593}
5594
Francois Romieu52989f02012-07-06 13:37:00 +02005595static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005596{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005597 RTL_W32(tp, CSIDR, value);
5598 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005599 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5600 CSIAR_FUNC_NIC);
5601
Francois Romieuffc46952012-07-06 14:19:23 +02005602 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005603}
5604
Francois Romieu52989f02012-07-06 13:37:00 +02005605static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005606{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005607 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005608 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5609
Francois Romieuffc46952012-07-06 14:19:23 +02005610 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005611 RTL_R32(tp, CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005612}
5613
hayeswang45dd95c2013-07-08 17:09:01 +08005614static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5615{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005616 RTL_W32(tp, CSIDR, value);
5617 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
hayeswang45dd95c2013-07-08 17:09:01 +08005618 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5619 CSIAR_FUNC_NIC2);
5620
5621 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5622}
5623
5624static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5625{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005626 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
hayeswang45dd95c2013-07-08 17:09:01 +08005627 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5628
5629 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005630 RTL_R32(tp, CSIDR) : ~0;
hayeswang45dd95c2013-07-08 17:09:01 +08005631}
5632
Bill Pembertonbaf63292012-12-03 09:23:28 -05005633static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005634{
5635 struct csi_ops *ops = &tp->csi_ops;
5636
5637 switch (tp->mac_version) {
5638 case RTL_GIGA_MAC_VER_01:
5639 case RTL_GIGA_MAC_VER_02:
5640 case RTL_GIGA_MAC_VER_03:
5641 case RTL_GIGA_MAC_VER_04:
5642 case RTL_GIGA_MAC_VER_05:
5643 case RTL_GIGA_MAC_VER_06:
5644 case RTL_GIGA_MAC_VER_10:
5645 case RTL_GIGA_MAC_VER_11:
5646 case RTL_GIGA_MAC_VER_12:
5647 case RTL_GIGA_MAC_VER_13:
5648 case RTL_GIGA_MAC_VER_14:
5649 case RTL_GIGA_MAC_VER_15:
5650 case RTL_GIGA_MAC_VER_16:
5651 case RTL_GIGA_MAC_VER_17:
5652 ops->write = NULL;
5653 ops->read = NULL;
5654 break;
5655
Hayes Wang7e18dca2012-03-30 14:33:02 +08005656 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005657 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005658 ops->write = r8402_csi_write;
5659 ops->read = r8402_csi_read;
5660 break;
5661
hayeswang45dd95c2013-07-08 17:09:01 +08005662 case RTL_GIGA_MAC_VER_44:
5663 ops->write = r8411_csi_write;
5664 ops->read = r8411_csi_read;
5665 break;
5666
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005667 default:
5668 ops->write = r8169_csi_write;
5669 ops->read = r8169_csi_read;
5670 break;
5671 }
Francois Romieudacf8152008-08-02 20:44:13 +02005672}
5673
5674struct ephy_info {
5675 unsigned int offset;
5676 u16 mask;
5677 u16 bits;
5678};
5679
Francois Romieufdf6fc02012-07-06 22:40:38 +02005680static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5681 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005682{
5683 u16 w;
5684
5685 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005686 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5687 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005688 e++;
5689 }
5690}
5691
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005692static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005693{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005694 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005695 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005696}
5697
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005698static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005699{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005700 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005701 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005702}
5703
hayeswangb51ecea2014-07-09 14:52:51 +08005704static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5705{
hayeswangb51ecea2014-07-09 14:52:51 +08005706 u8 data;
5707
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005708 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005709
5710 if (enable)
5711 data |= Rdy_to_L23;
5712 else
5713 data &= ~Rdy_to_L23;
5714
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005715 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005716}
5717
Francois Romieub726e492008-06-28 12:22:59 +02005718#define R8168_CPCMD_QUIRK_MASK (\
5719 EnableBist | \
5720 Mac_dbgo_oe | \
5721 Force_half_dup | \
5722 Force_rxflow_en | \
5723 Force_txflow_en | \
5724 Cxpl_dbg_sel | \
5725 ASF | \
5726 PktCntrDisable | \
5727 Mac_dbgo_sel)
5728
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005729static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005730{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005731 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005732
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005733 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieub726e492008-06-28 12:22:59 +02005734
françois romieufaf1e782013-02-27 13:01:57 +00005735 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005736 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005737 PCI_EXP_DEVCTL_NOSNOOP_EN);
5738 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005739}
5740
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005741static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005742{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005743 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005744
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005745 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005746
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005747 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005748}
5749
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005750static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005751{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005752 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005753
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005754 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005755
françois romieufaf1e782013-02-27 13:01:57 +00005756 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005757 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005758
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005759 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005760
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005761 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005762}
5763
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005764static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005765{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005766 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005767 { 0x01, 0, 0x0001 },
5768 { 0x02, 0x0800, 0x1000 },
5769 { 0x03, 0, 0x0042 },
5770 { 0x06, 0x0080, 0x0000 },
5771 { 0x07, 0, 0x2000 }
5772 };
5773
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005774 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005775
Francois Romieufdf6fc02012-07-06 22:40:38 +02005776 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005777
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005778 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005779}
5780
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005781static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005782{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005783 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005784
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005785 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005786
françois romieufaf1e782013-02-27 13:01:57 +00005787 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005788 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005789
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005790 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieuef3386f2008-06-29 12:24:30 +02005791}
5792
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005793static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005794{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005795 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005796
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005797 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005798
5799 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005800 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005801
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005802 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005803
françois romieufaf1e782013-02-27 13:01:57 +00005804 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005805 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005806
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005807 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005808}
5809
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005810static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005811{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005812 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005813 { 0x02, 0x0800, 0x1000 },
5814 { 0x03, 0, 0x0002 },
5815 { 0x06, 0x0080, 0x0000 }
5816 };
5817
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005818 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005819
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005820 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005821
Francois Romieufdf6fc02012-07-06 22:40:38 +02005822 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005823
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005824 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005825}
5826
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005827static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005828{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005829 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005830 { 0x01, 0, 0x0001 },
5831 { 0x03, 0x0400, 0x0220 }
5832 };
5833
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005834 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005835
Francois Romieufdf6fc02012-07-06 22:40:38 +02005836 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005837
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005838 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005839}
5840
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005841static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005842{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005843 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005844}
5845
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005846static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005847{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005848 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005849
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005850 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005851}
5852
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005853static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005854{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005855 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005856
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005857 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005858
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005859 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005860
françois romieufaf1e782013-02-27 13:01:57 +00005861 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005862 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005863
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005864 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu5b538df2008-07-20 16:22:45 +02005865}
5866
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005867static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005868{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005869 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005870
françois romieufaf1e782013-02-27 13:01:57 +00005871 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005872 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005873
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005874 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005875
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005876 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005877}
5878
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005879static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005880{
5881 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005882 { 0x0b, 0x0000, 0x0048 },
5883 { 0x19, 0x0020, 0x0050 },
5884 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005885 };
françois romieue6de30d2011-01-03 15:08:37 +00005886
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005887 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005888
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005889 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005890
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005891 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005892
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005893 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005894
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005895 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005896}
5897
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005898static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005899{
Hayes Wang70090422011-07-06 15:58:06 +08005900 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005901 { 0x00, 0x0200, 0x0100 },
5902 { 0x00, 0x0000, 0x0004 },
5903 { 0x06, 0x0002, 0x0001 },
5904 { 0x06, 0x0000, 0x0030 },
5905 { 0x07, 0x0000, 0x2000 },
5906 { 0x00, 0x0000, 0x0020 },
5907 { 0x03, 0x5800, 0x2000 },
5908 { 0x03, 0x0000, 0x0001 },
5909 { 0x01, 0x0800, 0x1000 },
5910 { 0x07, 0x0000, 0x4000 },
5911 { 0x1e, 0x0000, 0x2000 },
5912 { 0x19, 0xffff, 0xfe6c },
5913 { 0x0a, 0x0000, 0x0040 }
5914 };
5915
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005916 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005917
Francois Romieufdf6fc02012-07-06 22:40:38 +02005918 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005919
françois romieufaf1e782013-02-27 13:01:57 +00005920 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005921 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005922
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005923 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005924
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005925 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005926
5927 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005928 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5929 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005930
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005931 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005932}
5933
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005934static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005935{
5936 static const struct ephy_info e_info_8168e_2[] = {
5937 { 0x09, 0x0000, 0x0080 },
5938 { 0x19, 0x0000, 0x0224 }
5939 };
5940
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005941 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005942
Francois Romieufdf6fc02012-07-06 22:40:38 +02005943 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005944
françois romieufaf1e782013-02-27 13:01:57 +00005945 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005946 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005947
Francois Romieufdf6fc02012-07-06 22:40:38 +02005948 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5949 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5950 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5951 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5952 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5953 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005954 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5955 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005956
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005957 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005958
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005959 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005960
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005961 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5962 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005963
5964 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005965 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005966
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005967 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5968 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5969 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005970}
5971
Hayes Wang5f886e02012-03-30 14:33:03 +08005972static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005973{
Hayes Wang5f886e02012-03-30 14:33:03 +08005974 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005975
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005976 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005977
Francois Romieufdf6fc02012-07-06 22:40:38 +02005978 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5979 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5980 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5981 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005982 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5983 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5984 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5985 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005986 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5987 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005988
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005989 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005990
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005991 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005992
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005993 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5994 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5995 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5996 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5997 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005998}
5999
Hayes Wang5f886e02012-03-30 14:33:03 +08006000static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6001{
Hayes Wang5f886e02012-03-30 14:33:03 +08006002 static const struct ephy_info e_info_8168f_1[] = {
6003 { 0x06, 0x00c0, 0x0020 },
6004 { 0x08, 0x0001, 0x0002 },
6005 { 0x09, 0x0000, 0x0080 },
6006 { 0x19, 0x0000, 0x0224 }
6007 };
6008
6009 rtl_hw_start_8168f(tp);
6010
Francois Romieufdf6fc02012-07-06 22:40:38 +02006011 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006012
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006013 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006014
6015 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006016 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08006017}
6018
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006019static void rtl_hw_start_8411(struct rtl8169_private *tp)
6020{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006021 static const struct ephy_info e_info_8168f_1[] = {
6022 { 0x06, 0x00c0, 0x0020 },
6023 { 0x0f, 0xffff, 0x5200 },
6024 { 0x1e, 0x0000, 0x4000 },
6025 { 0x19, 0x0000, 0x0224 }
6026 };
6027
6028 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006029 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006030
Francois Romieufdf6fc02012-07-06 22:40:38 +02006031 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006032
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006033 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006034}
6035
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006036static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006037{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006038 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00006039
Hayes Wangc5583862012-07-02 17:23:22 +08006040 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6041 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6042 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6043 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6044
6045 rtl_csi_access_enable_1(tp);
6046
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006047 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08006048
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006049 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6050 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006051 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006052
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006053 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6054 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08006055
6056 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6057 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6058
6059 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006060 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08006061
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006062 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6063 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006064
6065 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006066}
6067
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006068static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6069{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006070 static const struct ephy_info e_info_8168g_1[] = {
6071 { 0x00, 0x0000, 0x0008 },
6072 { 0x0c, 0x37d0, 0x0820 },
6073 { 0x1e, 0x0000, 0x0001 },
6074 { 0x19, 0x8000, 0x0000 }
6075 };
6076
6077 rtl_hw_start_8168g(tp);
6078
6079 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006080 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6081 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006082 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6083}
6084
hayeswang57538c42013-04-01 22:23:40 +00006085static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6086{
hayeswang57538c42013-04-01 22:23:40 +00006087 static const struct ephy_info e_info_8168g_2[] = {
6088 { 0x00, 0x0000, 0x0008 },
6089 { 0x0c, 0x3df0, 0x0200 },
6090 { 0x19, 0xffff, 0xfc00 },
6091 { 0x1e, 0xffff, 0x20eb }
6092 };
6093
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006094 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006095
6096 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006097 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6098 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00006099 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6100}
6101
hayeswang45dd95c2013-07-08 17:09:01 +08006102static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6103{
hayeswang45dd95c2013-07-08 17:09:01 +08006104 static const struct ephy_info e_info_8411_2[] = {
6105 { 0x00, 0x0000, 0x0008 },
6106 { 0x0c, 0x3df0, 0x0200 },
6107 { 0x0f, 0xffff, 0x5200 },
6108 { 0x19, 0x0020, 0x0000 },
6109 { 0x1e, 0x0000, 0x2000 }
6110 };
6111
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006112 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006113
6114 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006115 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6116 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08006117 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6118}
6119
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006120static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6121{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006122 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006123 u32 data;
6124 static const struct ephy_info e_info_8168h_1[] = {
6125 { 0x1e, 0x0800, 0x0001 },
6126 { 0x1d, 0x0000, 0x0800 },
6127 { 0x05, 0xffff, 0x2089 },
6128 { 0x06, 0xffff, 0x5881 },
6129 { 0x04, 0xffff, 0x154a },
6130 { 0x01, 0xffff, 0x068b }
6131 };
6132
6133 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006134 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6135 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006136 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6137
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006138 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006139
6140 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6141 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6142 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6143 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6144
6145 rtl_csi_access_enable_1(tp);
6146
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006147 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006148
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006149 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6150 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006151
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006152 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006153
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006154 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006155
6156 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6157
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006158 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6159 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006160
6161 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6162 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6163
6164 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006165 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006166
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006167 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6168 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006169
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006170 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006171
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006172 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006173
6174 rtl_pcie_state_l2l3_enable(tp, false);
6175
6176 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006177 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006178 rtl_writephy(tp, 0x1f, 0x0000);
6179 if (rg_saw_cnt > 0) {
6180 u16 sw_cnt_1ms_ini;
6181
6182 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6183 sw_cnt_1ms_ini &= 0x0fff;
6184 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006185 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006186 data |= sw_cnt_1ms_ini;
6187 r8168_mac_ocp_write(tp, 0xd412, data);
6188 }
6189
6190 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006191 data &= ~0xf0;
6192 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006193 r8168_mac_ocp_write(tp, 0xe056, data);
6194
6195 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006196 data &= ~0x6000;
6197 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006198 r8168_mac_ocp_write(tp, 0xe052, data);
6199
6200 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006201 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006202 data |= 0x017f;
6203 r8168_mac_ocp_write(tp, 0xe0d6, data);
6204
6205 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006206 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006207 data |= 0x047f;
6208 r8168_mac_ocp_write(tp, 0xd420, data);
6209
6210 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6211 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6212 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6213 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6214}
6215
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006216static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6217{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006218 rtl8168ep_stop_cmac(tp);
6219
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006220 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006221
6222 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6223 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6224 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6225 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6226
6227 rtl_csi_access_enable_1(tp);
6228
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006229 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006230
6231 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6232 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6233
6234 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6235
6236 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6237
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006238 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6239 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006240
6241 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6242 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6243
6244 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006245 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006246
6247 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6248
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006249 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006250
6251 rtl_pcie_state_l2l3_enable(tp, false);
6252}
6253
6254static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6255{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006256 static const struct ephy_info e_info_8168ep_1[] = {
6257 { 0x00, 0xffff, 0x10ab },
6258 { 0x06, 0xffff, 0xf030 },
6259 { 0x08, 0xffff, 0x2006 },
6260 { 0x0d, 0xffff, 0x1666 },
6261 { 0x0c, 0x3ff0, 0x0000 }
6262 };
6263
6264 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006265 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6266 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006267 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6268
6269 rtl_hw_start_8168ep(tp);
6270}
6271
6272static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6273{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006274 static const struct ephy_info e_info_8168ep_2[] = {
6275 { 0x00, 0xffff, 0x10a3 },
6276 { 0x19, 0xffff, 0xfc00 },
6277 { 0x1e, 0xffff, 0x20ea }
6278 };
6279
6280 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006281 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6282 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006283 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6284
6285 rtl_hw_start_8168ep(tp);
6286
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006287 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6288 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006289}
6290
6291static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6292{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006293 u32 data;
6294 static const struct ephy_info e_info_8168ep_3[] = {
6295 { 0x00, 0xffff, 0x10a3 },
6296 { 0x19, 0xffff, 0x7c00 },
6297 { 0x1e, 0xffff, 0x20eb },
6298 { 0x0d, 0xffff, 0x1666 }
6299 };
6300
6301 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006302 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6303 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006304 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6305
6306 rtl_hw_start_8168ep(tp);
6307
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006308 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6309 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006310
6311 data = r8168_mac_ocp_read(tp, 0xd3e2);
6312 data &= 0xf000;
6313 data |= 0x0271;
6314 r8168_mac_ocp_write(tp, 0xd3e2, data);
6315
6316 data = r8168_mac_ocp_read(tp, 0xd3e4);
6317 data &= 0xff00;
6318 r8168_mac_ocp_write(tp, 0xd3e4, data);
6319
6320 data = r8168_mac_ocp_read(tp, 0xe860);
6321 data |= 0x0080;
6322 r8168_mac_ocp_write(tp, 0xe860, data);
6323}
6324
Francois Romieu07ce4062007-02-23 23:36:39 +01006325static void rtl_hw_start_8168(struct net_device *dev)
6326{
Francois Romieu2dd99532007-06-11 23:22:52 +02006327 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu2dd99532007-06-11 23:22:52 +02006328
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006329 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu2dd99532007-06-11 23:22:52 +02006330
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006331 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006332
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006333 rtl_set_rx_max_size(tp, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006334
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006335 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006336
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006337 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02006338
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006339 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01006340
6341 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006342 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006343 tp->event_slow |= RxFIFOOver | PCSTimeout;
6344 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006345 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006346
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006347 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006348
hayeswang1a964642013-04-01 22:23:41 +00006349 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006350
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006351 RTL_R8(tp, IntrMask);
Francois Romieu2dd99532007-06-11 23:22:52 +02006352
Francois Romieu219a1e92008-06-28 11:58:39 +02006353 switch (tp->mac_version) {
6354 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006355 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006356 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006357
6358 case RTL_GIGA_MAC_VER_12:
6359 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006360 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006361 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006362
6363 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006364 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006365 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006366
6367 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006368 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006369 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006370
6371 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006372 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006373 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006374
Francois Romieu197ff762008-06-28 13:16:02 +02006375 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006376 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006377 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006378
Francois Romieu6fb07052008-06-29 11:54:28 +02006379 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006380 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006381 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006382
Francois Romieuef3386f2008-06-29 12:24:30 +02006383 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006384 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006385 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006386
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006387 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006388 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006389 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006390
Francois Romieu5b538df2008-07-20 16:22:45 +02006391 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006392 case RTL_GIGA_MAC_VER_26:
6393 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006394 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006395 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006396
françois romieue6de30d2011-01-03 15:08:37 +00006397 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006398 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006399 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006400
hayeswang4804b3b2011-03-21 01:50:29 +00006401 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006402 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006403 break;
6404
hayeswang01dc7fe2011-03-21 01:50:28 +00006405 case RTL_GIGA_MAC_VER_32:
6406 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006407 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006408 break;
6409 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006410 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006411 break;
françois romieue6de30d2011-01-03 15:08:37 +00006412
Hayes Wangc2218922011-09-06 16:55:18 +08006413 case RTL_GIGA_MAC_VER_35:
6414 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006415 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006416 break;
6417
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006418 case RTL_GIGA_MAC_VER_38:
6419 rtl_hw_start_8411(tp);
6420 break;
6421
Hayes Wangc5583862012-07-02 17:23:22 +08006422 case RTL_GIGA_MAC_VER_40:
6423 case RTL_GIGA_MAC_VER_41:
6424 rtl_hw_start_8168g_1(tp);
6425 break;
hayeswang57538c42013-04-01 22:23:40 +00006426 case RTL_GIGA_MAC_VER_42:
6427 rtl_hw_start_8168g_2(tp);
6428 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006429
hayeswang45dd95c2013-07-08 17:09:01 +08006430 case RTL_GIGA_MAC_VER_44:
6431 rtl_hw_start_8411_2(tp);
6432 break;
6433
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006434 case RTL_GIGA_MAC_VER_45:
6435 case RTL_GIGA_MAC_VER_46:
6436 rtl_hw_start_8168h_1(tp);
6437 break;
6438
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006439 case RTL_GIGA_MAC_VER_49:
6440 rtl_hw_start_8168ep_1(tp);
6441 break;
6442
6443 case RTL_GIGA_MAC_VER_50:
6444 rtl_hw_start_8168ep_2(tp);
6445 break;
6446
6447 case RTL_GIGA_MAC_VER_51:
6448 rtl_hw_start_8168ep_3(tp);
6449 break;
6450
Francois Romieu219a1e92008-06-28 11:58:39 +02006451 default:
6452 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6453 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006454 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006455 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006456
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006457 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
hayeswang1a964642013-04-01 22:23:41 +00006458
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006459 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu0e485152007-02-20 00:00:26 +01006460
hayeswang1a964642013-04-01 22:23:41 +00006461 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006462
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006463 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006464}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465
Francois Romieu2857ffb2008-08-02 21:08:49 +02006466#define R810X_CPCMD_QUIRK_MASK (\
6467 EnableBist | \
6468 Mac_dbgo_oe | \
6469 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006470 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006471 Force_txflow_en | \
6472 Cxpl_dbg_sel | \
6473 ASF | \
6474 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006475 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006476
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006477static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006478{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006479 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006480 { 0x01, 0, 0x6e65 },
6481 { 0x02, 0, 0x091f },
6482 { 0x03, 0, 0xc2f9 },
6483 { 0x06, 0, 0xafb5 },
6484 { 0x07, 0, 0x0e00 },
6485 { 0x19, 0, 0xec80 },
6486 { 0x01, 0, 0x2e65 },
6487 { 0x01, 0, 0x6e65 }
6488 };
6489 u8 cfg1;
6490
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006491 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006492
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006493 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006494
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006495 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006496
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006497 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006498 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006499 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006500
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006501 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006502 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006503 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006504
Francois Romieufdf6fc02012-07-06 22:40:38 +02006505 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006506}
6507
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006508static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006509{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006510 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006511
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006512 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006513
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006514 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6515 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006516}
6517
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006518static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006519{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006520 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006521
Francois Romieufdf6fc02012-07-06 22:40:38 +02006522 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006523}
6524
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006525static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006526{
6527 static const struct ephy_info e_info_8105e_1[] = {
6528 { 0x07, 0, 0x4000 },
6529 { 0x19, 0, 0x0200 },
6530 { 0x19, 0, 0x0020 },
6531 { 0x1e, 0, 0x2000 },
6532 { 0x03, 0, 0x0001 },
6533 { 0x19, 0, 0x0100 },
6534 { 0x19, 0, 0x0004 },
6535 { 0x0a, 0, 0x0020 }
6536 };
6537
Francois Romieucecb5fd2011-04-01 10:21:07 +02006538 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006539 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006540
Francois Romieucecb5fd2011-04-01 10:21:07 +02006541 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006542 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006543
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006544 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6545 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006546
Francois Romieufdf6fc02012-07-06 22:40:38 +02006547 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006548
6549 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006550}
6551
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006552static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006553{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006554 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006555 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006556}
6557
Hayes Wang7e18dca2012-03-30 14:33:02 +08006558static void rtl_hw_start_8402(struct rtl8169_private *tp)
6559{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006560 static const struct ephy_info e_info_8402[] = {
6561 { 0x19, 0xffff, 0xff64 },
6562 { 0x1e, 0, 0x4000 }
6563 };
6564
6565 rtl_csi_access_enable_2(tp);
6566
6567 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006568 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006569
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006570 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6571 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006572
Francois Romieufdf6fc02012-07-06 22:40:38 +02006573 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006574
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006575 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006576
Francois Romieufdf6fc02012-07-06 22:40:38 +02006577 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6578 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006579 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6580 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006581 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6582 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006583 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006584
6585 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006586}
6587
Hayes Wang5598bfe2012-07-02 17:23:21 +08006588static void rtl_hw_start_8106(struct rtl8169_private *tp)
6589{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006590 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006591 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006592
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006593 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6594 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6595 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006596
6597 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006598}
6599
Francois Romieu07ce4062007-02-23 23:36:39 +01006600static void rtl_hw_start_8101(struct net_device *dev)
6601{
Francois Romieucdf1a602007-06-11 23:29:50 +02006602 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieucdf1a602007-06-11 23:29:50 +02006603 struct pci_dev *pdev = tp->pci_dev;
6604
Francois Romieuda78dbf2012-01-26 14:18:23 +01006605 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6606 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006607
Francois Romieucecb5fd2011-04-01 10:21:07 +02006608 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006609 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006610 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6611 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006612
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006613 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006614
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006615 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006616
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006617 rtl_set_rx_max_size(tp, rx_buf_sz);
hayeswang1a964642013-04-01 22:23:41 +00006618
6619 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006620 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006621
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006622 rtl_set_rx_tx_desc_registers(tp);
hayeswang1a964642013-04-01 22:23:41 +00006623
6624 rtl_set_rx_tx_config_registers(tp);
6625
Francois Romieu2857ffb2008-08-02 21:08:49 +02006626 switch (tp->mac_version) {
6627 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006628 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006629 break;
6630
6631 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006632 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006633 break;
6634
6635 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006636 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006637 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006638
6639 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006640 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006641 break;
6642 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006643 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006644 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006645
6646 case RTL_GIGA_MAC_VER_37:
6647 rtl_hw_start_8402(tp);
6648 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006649
6650 case RTL_GIGA_MAC_VER_39:
6651 rtl_hw_start_8106(tp);
6652 break;
hayeswang58152cd2013-04-01 22:23:42 +00006653 case RTL_GIGA_MAC_VER_43:
6654 rtl_hw_start_8168g_2(tp);
6655 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006656 case RTL_GIGA_MAC_VER_47:
6657 case RTL_GIGA_MAC_VER_48:
6658 rtl_hw_start_8168h_1(tp);
6659 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006660 }
6661
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006662 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006663
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006664 RTL_W16(tp, IntrMitigate, 0x0000);
Francois Romieucdf1a602007-06-11 23:29:50 +02006665
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006666 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006667
Francois Romieucdf1a602007-06-11 23:29:50 +02006668 rtl_set_rx_mode(dev);
6669
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006670 RTL_R8(tp, IntrMask);
hayeswang1a964642013-04-01 22:23:41 +00006671
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006672 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673}
6674
6675static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6676{
Francois Romieud58d46b2011-05-03 16:38:29 +02006677 struct rtl8169_private *tp = netdev_priv(dev);
6678
Francois Romieud58d46b2011-05-03 16:38:29 +02006679 if (new_mtu > ETH_DATA_LEN)
6680 rtl_hw_jumbo_enable(tp);
6681 else
6682 rtl_hw_jumbo_disable(tp);
6683
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006685 netdev_update_features(dev);
6686
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688}
6689
6690static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6691{
Al Viro95e09182007-12-22 18:55:39 +00006692 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6694}
6695
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006696static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6697 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006699 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006700 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006701
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006702 kfree(*data_buff);
6703 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 rtl8169_make_unusable_by_asic(desc);
6705}
6706
6707static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6708{
6709 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6710
Alexander Duycka0750132014-12-11 15:02:17 -08006711 /* Force memory writes to complete before releasing descriptor */
6712 dma_wmb();
6713
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6715}
6716
6717static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6718 u32 rx_buf_sz)
6719{
6720 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 rtl8169_mark_to_asic(desc, rx_buf_sz);
6722}
6723
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006724static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006726 return (void *)ALIGN((long)data, 16);
6727}
6728
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006729static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6730 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006731{
6732 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006734 struct device *d = tp_to_dev(tp);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006735 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006736 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006738 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6739 if (!data)
6740 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006741
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006742 if (rtl8169_align(data) != data) {
6743 kfree(data);
6744 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6745 if (!data)
6746 return NULL;
6747 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006748
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006749 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006750 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006751 if (unlikely(dma_mapping_error(d, mapping))) {
6752 if (net_ratelimit())
6753 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006754 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
6757 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006758 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006759
6760err_out:
6761 kfree(data);
6762 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763}
6764
6765static void rtl8169_rx_clear(struct rtl8169_private *tp)
6766{
Francois Romieu07d3f512007-02-21 22:40:46 +01006767 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
6769 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006770 if (tp->Rx_databuff[i]) {
6771 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 tp->RxDescArray + i);
6773 }
6774 }
6775}
6776
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006777static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006779 desc->opts1 |= cpu_to_le32(RingEnd);
6780}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006781
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006782static int rtl8169_rx_fill(struct rtl8169_private *tp)
6783{
6784 unsigned int i;
6785
6786 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006787 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006788
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006789 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006791
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006792 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006793 if (!data) {
6794 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006795 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006796 }
6797 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006800 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6801 return 0;
6802
6803err_out:
6804 rtl8169_rx_clear(tp);
6805 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806}
6807
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808static int rtl8169_init_ring(struct net_device *dev)
6809{
6810 struct rtl8169_private *tp = netdev_priv(dev);
6811
6812 rtl8169_init_ring_indexes(tp);
6813
6814 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006815 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006817 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818}
6819
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006820static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 struct TxDesc *desc)
6822{
6823 unsigned int len = tx_skb->len;
6824
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006825 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6826
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 desc->opts1 = 0x00;
6828 desc->opts2 = 0x00;
6829 desc->addr = 0x00;
6830 tx_skb->len = 0;
6831}
6832
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006833static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6834 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835{
6836 unsigned int i;
6837
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006838 for (i = 0; i < n; i++) {
6839 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 struct ring_info *tx_skb = tp->tx_skb + entry;
6841 unsigned int len = tx_skb->len;
6842
6843 if (len) {
6844 struct sk_buff *skb = tx_skb->skb;
6845
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006846 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 tp->TxDescArray + entry);
6848 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006849 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 tx_skb->skb = NULL;
6851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 }
6853 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006854}
6855
6856static void rtl8169_tx_clear(struct rtl8169_private *tp)
6857{
6858 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 tp->cur_tx = tp->dirty_tx = 0;
6860}
6861
Francois Romieu4422bcd2012-01-26 11:23:32 +01006862static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863{
David Howellsc4028952006-11-22 14:57:56 +00006864 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006865 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Francois Romieuda78dbf2012-01-26 14:18:23 +01006867 napi_disable(&tp->napi);
6868 netif_stop_queue(dev);
6869 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870
françois romieuc7c2c392011-12-04 20:30:52 +00006871 rtl8169_hw_reset(tp);
6872
Francois Romieu56de4142011-03-15 17:29:31 +01006873 for (i = 0; i < NUM_RX_DESC; i++)
6874 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6875
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006877 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
Francois Romieuda78dbf2012-01-26 14:18:23 +01006879 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006880 rtl_hw_start(dev);
6881 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006882 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883}
6884
6885static void rtl8169_tx_timeout(struct net_device *dev)
6886{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006887 struct rtl8169_private *tp = netdev_priv(dev);
6888
6889 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890}
6891
6892static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006893 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894{
6895 struct skb_shared_info *info = skb_shinfo(skb);
6896 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006897 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006898 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
6900 entry = tp->cur_tx;
6901 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006902 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 dma_addr_t mapping;
6904 u32 status, len;
6905 void *addr;
6906
6907 entry = (entry + 1) % NUM_TX_DESC;
6908
6909 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006910 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006911 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006912 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006913 if (unlikely(dma_mapping_error(d, mapping))) {
6914 if (net_ratelimit())
6915 netif_err(tp, drv, tp->dev,
6916 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006917 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
Francois Romieucecb5fd2011-04-01 10:21:07 +02006920 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006921 status = opts[0] | len |
6922 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
6924 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006925 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 txd->addr = cpu_to_le64(mapping);
6927
6928 tp->tx_skb[entry].len = len;
6929 }
6930
6931 if (cur_frag) {
6932 tp->tx_skb[entry].skb = skb;
6933 txd->opts1 |= cpu_to_le32(LastFrag);
6934 }
6935
6936 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006937
6938err_out:
6939 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6940 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941}
6942
françois romieub423e9a2013-05-18 01:24:46 +00006943static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6944{
6945 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6946}
6947
hayeswange9746042014-07-11 16:25:58 +08006948static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6949 struct net_device *dev);
6950/* r8169_csum_workaround()
6951 * The hw limites the value the transport offset. When the offset is out of the
6952 * range, calculate the checksum by sw.
6953 */
6954static void r8169_csum_workaround(struct rtl8169_private *tp,
6955 struct sk_buff *skb)
6956{
6957 if (skb_shinfo(skb)->gso_size) {
6958 netdev_features_t features = tp->dev->features;
6959 struct sk_buff *segs, *nskb;
6960
6961 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6962 segs = skb_gso_segment(skb, features);
6963 if (IS_ERR(segs) || !segs)
6964 goto drop;
6965
6966 do {
6967 nskb = segs;
6968 segs = segs->next;
6969 nskb->next = NULL;
6970 rtl8169_start_xmit(nskb, tp->dev);
6971 } while (segs);
6972
Alexander Duyckeb781392015-05-01 10:34:44 -07006973 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006974 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6975 if (skb_checksum_help(skb) < 0)
6976 goto drop;
6977
6978 rtl8169_start_xmit(skb, tp->dev);
6979 } else {
6980 struct net_device_stats *stats;
6981
6982drop:
6983 stats = &tp->dev->stats;
6984 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006985 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006986 }
6987}
6988
6989/* msdn_giant_send_check()
6990 * According to the document of microsoft, the TCP Pseudo Header excludes the
6991 * packet length for IPv6 TCP large packets.
6992 */
6993static int msdn_giant_send_check(struct sk_buff *skb)
6994{
6995 const struct ipv6hdr *ipv6h;
6996 struct tcphdr *th;
6997 int ret;
6998
6999 ret = skb_cow_head(skb, 0);
7000 if (ret)
7001 return ret;
7002
7003 ipv6h = ipv6_hdr(skb);
7004 th = tcp_hdr(skb);
7005
7006 th->check = 0;
7007 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7008
7009 return ret;
7010}
7011
7012static inline __be16 get_protocol(struct sk_buff *skb)
7013{
7014 __be16 protocol;
7015
7016 if (skb->protocol == htons(ETH_P_8021Q))
7017 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7018 else
7019 protocol = skb->protocol;
7020
7021 return protocol;
7022}
7023
hayeswang5888d3f2014-07-11 16:25:56 +08007024static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7025 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026{
Michał Mirosław350fb322011-04-08 06:35:56 +00007027 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
Francois Romieu2b7b4312011-04-18 22:53:24 -07007029 if (mss) {
7030 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007031 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7032 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7033 const struct iphdr *ip = ip_hdr(skb);
7034
7035 if (ip->protocol == IPPROTO_TCP)
7036 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7037 else if (ip->protocol == IPPROTO_UDP)
7038 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7039 else
7040 WARN_ON_ONCE(1);
7041 }
7042
7043 return true;
7044}
7045
7046static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7047 struct sk_buff *skb, u32 *opts)
7048{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007049 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007050 u32 mss = skb_shinfo(skb)->gso_size;
7051
7052 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007053 if (transport_offset > GTTCPHO_MAX) {
7054 netif_warn(tp, tx_err, tp->dev,
7055 "Invalid transport offset 0x%x for TSO\n",
7056 transport_offset);
7057 return false;
7058 }
7059
7060 switch (get_protocol(skb)) {
7061 case htons(ETH_P_IP):
7062 opts[0] |= TD1_GTSENV4;
7063 break;
7064
7065 case htons(ETH_P_IPV6):
7066 if (msdn_giant_send_check(skb))
7067 return false;
7068
7069 opts[0] |= TD1_GTSENV6;
7070 break;
7071
7072 default:
7073 WARN_ON_ONCE(1);
7074 break;
7075 }
7076
hayeswangbdfa4ed2014-07-11 16:25:57 +08007077 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007078 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007079 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007080 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
françois romieub423e9a2013-05-18 01:24:46 +00007082 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007083 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007084
hayeswange9746042014-07-11 16:25:58 +08007085 if (transport_offset > TCPHO_MAX) {
7086 netif_warn(tp, tx_err, tp->dev,
7087 "Invalid transport offset 0x%x\n",
7088 transport_offset);
7089 return false;
7090 }
7091
7092 switch (get_protocol(skb)) {
7093 case htons(ETH_P_IP):
7094 opts[1] |= TD1_IPv4_CS;
7095 ip_protocol = ip_hdr(skb)->protocol;
7096 break;
7097
7098 case htons(ETH_P_IPV6):
7099 opts[1] |= TD1_IPv6_CS;
7100 ip_protocol = ipv6_hdr(skb)->nexthdr;
7101 break;
7102
7103 default:
7104 ip_protocol = IPPROTO_RAW;
7105 break;
7106 }
7107
7108 if (ip_protocol == IPPROTO_TCP)
7109 opts[1] |= TD1_TCP_CS;
7110 else if (ip_protocol == IPPROTO_UDP)
7111 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007112 else
7113 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007114
7115 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007116 } else {
7117 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007118 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 }
hayeswang5888d3f2014-07-11 16:25:56 +08007120
françois romieub423e9a2013-05-18 01:24:46 +00007121 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122}
7123
Stephen Hemminger613573252009-08-31 19:50:58 +00007124static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7125 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126{
7127 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007128 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007130 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 dma_addr_t mapping;
7132 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007133 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007134 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007135
Julien Ducourthial477206a2012-05-09 00:00:06 +02007136 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007137 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007138 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 }
7140
7141 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007142 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143
françois romieub423e9a2013-05-18 01:24:46 +00007144 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7145 opts[0] = DescOwn;
7146
hayeswange9746042014-07-11 16:25:58 +08007147 if (!tp->tso_csum(tp, skb, opts)) {
7148 r8169_csum_workaround(tp, skb);
7149 return NETDEV_TX_OK;
7150 }
françois romieub423e9a2013-05-18 01:24:46 +00007151
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007152 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007153 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007154 if (unlikely(dma_mapping_error(d, mapping))) {
7155 if (net_ratelimit())
7156 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007157 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
7160 tp->tx_skb[entry].len = len;
7161 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
Francois Romieu2b7b4312011-04-18 22:53:24 -07007163 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007164 if (frags < 0)
7165 goto err_dma_1;
7166 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007167 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007168 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007169 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007170 tp->tx_skb[entry].skb = skb;
7171 }
7172
Francois Romieu2b7b4312011-04-18 22:53:24 -07007173 txd->opts2 = cpu_to_le32(opts[1]);
7174
Richard Cochran5047fb52012-03-10 07:29:42 +00007175 skb_tx_timestamp(skb);
7176
Alexander Duycka0750132014-12-11 15:02:17 -08007177 /* Force memory writes to complete before releasing descriptor */
7178 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
Francois Romieucecb5fd2011-04-01 10:21:07 +02007180 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007181 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 txd->opts1 = cpu_to_le32(status);
7183
Alexander Duycka0750132014-12-11 15:02:17 -08007184 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007185 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
Alexander Duycka0750132014-12-11 15:02:17 -08007187 tp->cur_tx += frags + 1;
7188
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007189 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190
David S. Miller87cda7c2015-02-22 15:54:29 -05007191 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007192
David S. Miller87cda7c2015-02-22 15:54:29 -05007193 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007194 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7195 * not miss a ring update when it notices a stopped queue.
7196 */
7197 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007199 /* Sync with rtl_tx:
7200 * - publish queue status and cur_tx ring index (write barrier)
7201 * - refresh dirty_tx ring index (read barrier).
7202 * May the current thread have a pessimistic view of the ring
7203 * status and forget to wake up queue, a racing rtl_tx thread
7204 * can't.
7205 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007206 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007207 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 netif_wake_queue(dev);
7209 }
7210
Stephen Hemminger613573252009-08-31 19:50:58 +00007211 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007213err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007214 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007215err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007216 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007217 dev->stats.tx_dropped++;
7218 return NETDEV_TX_OK;
7219
7220err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007222 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007223 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224}
7225
7226static void rtl8169_pcierr_interrupt(struct net_device *dev)
7227{
7228 struct rtl8169_private *tp = netdev_priv(dev);
7229 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 u16 pci_status, pci_cmd;
7231
7232 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7233 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7234
Joe Perchesbf82c182010-02-09 11:49:50 +00007235 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7236 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238 /*
7239 * The recovery sequence below admits a very elaborated explanation:
7240 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007241 * - I did not see what else could be done;
7242 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 *
7244 * Feel free to adjust to your needs.
7245 */
Francois Romieua27993f2006-12-18 00:04:19 +01007246 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007247 pci_cmd &= ~PCI_COMMAND_PARITY;
7248 else
7249 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7250
7251 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252
7253 pci_write_config_word(pdev, PCI_STATUS,
7254 pci_status & (PCI_STATUS_DETECTED_PARITY |
7255 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7256 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7257
7258 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007259 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007260 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007262 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 }
7265
françois romieue6de30d2011-01-03 15:08:37 +00007266 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007267
Francois Romieu98ddf982012-01-31 10:47:34 +01007268 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269}
7270
Francois Romieuda78dbf2012-01-26 14:18:23 +01007271static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272{
7273 unsigned int dirty_tx, tx_left;
7274
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 dirty_tx = tp->dirty_tx;
7276 smp_rmb();
7277 tx_left = tp->cur_tx - dirty_tx;
7278
7279 while (tx_left > 0) {
7280 unsigned int entry = dirty_tx % NUM_TX_DESC;
7281 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 u32 status;
7283
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7285 if (status & DescOwn)
7286 break;
7287
Alexander Duycka0750132014-12-11 15:02:17 -08007288 /* This barrier is needed to keep us from reading
7289 * any other fields out of the Tx descriptor until
7290 * we know the status of DescOwn
7291 */
7292 dma_rmb();
7293
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007294 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007295 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007297 u64_stats_update_begin(&tp->tx_stats.syncp);
7298 tp->tx_stats.packets++;
7299 tp->tx_stats.bytes += tx_skb->skb->len;
7300 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007301 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 tx_skb->skb = NULL;
7303 }
7304 dirty_tx++;
7305 tx_left--;
7306 }
7307
7308 if (tp->dirty_tx != dirty_tx) {
7309 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007310 /* Sync with rtl8169_start_xmit:
7311 * - publish dirty_tx ring index (write barrier)
7312 * - refresh cur_tx ring index and queue status (read barrier)
7313 * May the current thread miss the stopped queue condition,
7314 * a racing xmit thread can only have a right view of the
7315 * ring status.
7316 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007317 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007319 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 netif_wake_queue(dev);
7321 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007322 /*
7323 * 8168 hack: TxPoll requests are lost when the Tx packets are
7324 * too close. Let's kick an extra TxPoll request when a burst
7325 * of start_xmit activity is detected (if it is not detected,
7326 * it is slow enough). -- FR
7327 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007328 if (tp->cur_tx != dirty_tx)
7329 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 }
7331}
7332
Francois Romieu126fa4b2005-05-12 20:09:17 -04007333static inline int rtl8169_fragmented_frame(u32 status)
7334{
7335 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7336}
7337
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007338static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 u32 status = opts1 & RxProtoMask;
7341
7342 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007343 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 skb->ip_summed = CHECKSUM_UNNECESSARY;
7345 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007346 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347}
7348
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007349static struct sk_buff *rtl8169_try_rx_copy(void *data,
7350 struct rtl8169_private *tp,
7351 int pkt_size,
7352 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007354 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007355 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007357 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007358 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007359 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007360 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007361 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02007362 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007363 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7364
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007365 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366}
7367
Francois Romieuda78dbf2012-01-26 14:18:23 +01007368static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369{
7370 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007371 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Timo Teräs9fba0812013-01-15 21:01:24 +00007375 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007377 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 u32 status;
7379
David S. Miller8decf862011-09-22 03:23:13 -04007380 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 if (status & DescOwn)
7382 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007383
7384 /* This barrier is needed to keep us from reading
7385 * any other fields out of the Rx descriptor until
7386 * we know the status of DescOwn
7387 */
7388 dma_rmb();
7389
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007390 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007391 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7392 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007393 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007395 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007397 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007398 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007399 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007400 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007401 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007402 if ((status & (RxRUNT | RxCRC)) &&
7403 !(status & (RxRWT | RxFOVF)) &&
7404 (dev->features & NETIF_F_RXALL))
7405 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007407 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007408 dma_addr_t addr;
7409 int pkt_size;
7410
7411process_pkt:
7412 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007413 if (likely(!(dev->features & NETIF_F_RXFCS)))
7414 pkt_size = (status & 0x00003fff) - 4;
7415 else
7416 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
Francois Romieu126fa4b2005-05-12 20:09:17 -04007418 /*
7419 * The driver does not support incoming fragmented
7420 * frames. They are seen as a symptom of over-mtu
7421 * sized frames.
7422 */
7423 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007424 dev->stats.rx_dropped++;
7425 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007426 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007427 }
7428
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007429 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7430 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007431 if (!skb) {
7432 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007433 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 }
7435
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007436 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 skb_put(skb, pkt_size);
7438 skb->protocol = eth_type_trans(skb, dev);
7439
Francois Romieu7a8fc772011-03-01 17:18:33 +01007440 rtl8169_rx_vlan_tag(desc, skb);
7441
françois romieu39174292015-11-11 23:35:18 +01007442 if (skb->pkt_type == PACKET_MULTICAST)
7443 dev->stats.multicast++;
7444
Francois Romieu56de4142011-03-15 17:29:31 +01007445 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
Junchang Wang8027aa22012-03-04 23:30:32 +01007447 u64_stats_update_begin(&tp->rx_stats.syncp);
7448 tp->rx_stats.packets++;
7449 tp->rx_stats.bytes += pkt_size;
7450 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 }
françois romieuce11ff52013-01-24 13:30:06 +00007452release_descriptor:
7453 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007454 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 }
7456
7457 count = cur_rx - tp->cur_rx;
7458 tp->cur_rx = cur_rx;
7459
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 return count;
7461}
7462
Francois Romieu07d3f512007-02-21 22:40:46 +01007463static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464{
Francois Romieu07d3f512007-02-21 22:40:46 +01007465 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007468 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007470 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007471 if (status && status != 0xffff) {
7472 status &= RTL_EVENT_NAPI | tp->event_slow;
7473 if (status) {
7474 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007475
Francois Romieuda78dbf2012-01-26 14:18:23 +01007476 rtl_irq_disable(tp);
7477 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 return IRQ_RETVAL(handled);
7481}
7482
Francois Romieuda78dbf2012-01-26 14:18:23 +01007483/*
7484 * Workqueue context.
7485 */
7486static void rtl_slow_event_work(struct rtl8169_private *tp)
7487{
7488 struct net_device *dev = tp->dev;
7489 u16 status;
7490
7491 status = rtl_get_events(tp) & tp->event_slow;
7492 rtl_ack_events(tp, status);
7493
7494 if (unlikely(status & RxFIFOOver)) {
7495 switch (tp->mac_version) {
7496 /* Work around for rx fifo overflow */
7497 case RTL_GIGA_MAC_VER_11:
7498 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007499 /* XXX - Hack alert. See rtl_task(). */
7500 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007501 default:
7502 break;
7503 }
7504 }
7505
7506 if (unlikely(status & SYSErr))
7507 rtl8169_pcierr_interrupt(dev);
7508
7509 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007510 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007511
françois romieu7dbb4912012-06-09 10:53:16 +00007512 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007513}
7514
Francois Romieu4422bcd2012-01-26 11:23:32 +01007515static void rtl_task(struct work_struct *work)
7516{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007517 static const struct {
7518 int bitnr;
7519 void (*action)(struct rtl8169_private *);
7520 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007521 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007522 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7523 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7524 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7525 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007526 struct rtl8169_private *tp =
7527 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007528 struct net_device *dev = tp->dev;
7529 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007530
Francois Romieuda78dbf2012-01-26 14:18:23 +01007531 rtl_lock_work(tp);
7532
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007533 if (!netif_running(dev) ||
7534 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007535 goto out_unlock;
7536
7537 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7538 bool pending;
7539
Francois Romieuda78dbf2012-01-26 14:18:23 +01007540 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007541 if (pending)
7542 rtl_work[i].action(tp);
7543 }
7544
7545out_unlock:
7546 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007547}
7548
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007549static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007551 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7552 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007553 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7554 int work_done= 0;
7555 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556
Francois Romieuda78dbf2012-01-26 14:18:23 +01007557 status = rtl_get_events(tp);
7558 rtl_ack_events(tp, status & ~tp->event_slow);
7559
7560 if (status & RTL_EVENT_NAPI_RX)
7561 work_done = rtl_rx(dev, tp, (u32) budget);
7562
7563 if (status & RTL_EVENT_NAPI_TX)
7564 rtl_tx(dev, tp);
7565
7566 if (status & tp->event_slow) {
7567 enable_mask &= ~tp->event_slow;
7568
7569 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007572 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007573 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007574
Francois Romieuda78dbf2012-01-26 14:18:23 +01007575 rtl_irq_enable(tp, enable_mask);
7576 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 }
7578
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007579 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007582static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007583{
7584 struct rtl8169_private *tp = netdev_priv(dev);
7585
7586 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7587 return;
7588
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007589 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7590 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007591}
7592
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593static void rtl8169_down(struct net_device *dev)
7594{
7595 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596
Francois Romieu4876cc12011-03-11 21:07:11 +01007597 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007599 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007600 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601
Hayes Wang92fc43b2011-07-06 15:58:03 +08007602 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007603 /*
7604 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007605 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7606 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007607 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007608 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007611 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 rtl8169_tx_clear(tp);
7614
7615 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007616
7617 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618}
7619
7620static int rtl8169_close(struct net_device *dev)
7621{
7622 struct rtl8169_private *tp = netdev_priv(dev);
7623 struct pci_dev *pdev = tp->pci_dev;
7624
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007625 pm_runtime_get_sync(&pdev->dev);
7626
Francois Romieucecb5fd2011-04-01 10:21:07 +02007627 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007628 rtl8169_update_counters(dev);
7629
Francois Romieuda78dbf2012-01-26 14:18:23 +01007630 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007631 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007632
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007634 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635
Lekensteyn4ea72442013-07-22 09:53:30 +02007636 cancel_work_sync(&tp->wk.work);
7637
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007638 pci_free_irq(pdev, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007640 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7641 tp->RxPhyAddr);
7642 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7643 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 tp->TxDescArray = NULL;
7645 tp->RxDescArray = NULL;
7646
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007647 pm_runtime_put_sync(&pdev->dev);
7648
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 return 0;
7650}
7651
Francois Romieudc1c00c2012-03-08 10:06:18 +01007652#ifdef CONFIG_NET_POLL_CONTROLLER
7653static void rtl8169_netpoll(struct net_device *dev)
7654{
7655 struct rtl8169_private *tp = netdev_priv(dev);
7656
Heiner Kallweit29274992018-02-28 20:43:38 +01007657 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007658}
7659#endif
7660
Francois Romieudf43ac72012-03-08 09:48:40 +01007661static int rtl_open(struct net_device *dev)
7662{
7663 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007664 struct pci_dev *pdev = tp->pci_dev;
7665 int retval = -ENOMEM;
7666
7667 pm_runtime_get_sync(&pdev->dev);
7668
7669 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007670 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007671 * dma_alloc_coherent provides more.
7672 */
7673 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7674 &tp->TxPhyAddr, GFP_KERNEL);
7675 if (!tp->TxDescArray)
7676 goto err_pm_runtime_put;
7677
7678 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7679 &tp->RxPhyAddr, GFP_KERNEL);
7680 if (!tp->RxDescArray)
7681 goto err_free_tx_0;
7682
7683 retval = rtl8169_init_ring(dev);
7684 if (retval < 0)
7685 goto err_free_rx_1;
7686
7687 INIT_WORK(&tp->wk.work, rtl_task);
7688
7689 smp_mb();
7690
7691 rtl_request_firmware(tp);
7692
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007693 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7694 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007695 if (retval < 0)
7696 goto err_release_fw_2;
7697
7698 rtl_lock_work(tp);
7699
7700 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7701
7702 napi_enable(&tp->napi);
7703
7704 rtl8169_init_phy(dev, tp);
7705
7706 __rtl8169_set_features(dev, dev->features);
7707
7708 rtl_pll_power_up(tp);
7709
7710 rtl_hw_start(dev);
7711
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007712 if (!rtl8169_init_counter_offsets(dev))
7713 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7714
Francois Romieudf43ac72012-03-08 09:48:40 +01007715 netif_start_queue(dev);
7716
7717 rtl_unlock_work(tp);
7718
7719 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007720 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007721
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007722 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007723out:
7724 return retval;
7725
7726err_release_fw_2:
7727 rtl_release_firmware(tp);
7728 rtl8169_rx_clear(tp);
7729err_free_rx_1:
7730 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7731 tp->RxPhyAddr);
7732 tp->RxDescArray = NULL;
7733err_free_tx_0:
7734 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7735 tp->TxPhyAddr);
7736 tp->TxDescArray = NULL;
7737err_pm_runtime_put:
7738 pm_runtime_put_noidle(&pdev->dev);
7739 goto out;
7740}
7741
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007742static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007743rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744{
7745 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007746 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007747 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007748 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007750 pm_runtime_get_noresume(&pdev->dev);
7751
7752 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007753 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007754
Junchang Wang8027aa22012-03-04 23:30:32 +01007755 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007756 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007757 stats->rx_packets = tp->rx_stats.packets;
7758 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007759 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007760
Junchang Wang8027aa22012-03-04 23:30:32 +01007761 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007762 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007763 stats->tx_packets = tp->tx_stats.packets;
7764 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007765 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007766
7767 stats->rx_dropped = dev->stats.rx_dropped;
7768 stats->tx_dropped = dev->stats.tx_dropped;
7769 stats->rx_length_errors = dev->stats.rx_length_errors;
7770 stats->rx_errors = dev->stats.rx_errors;
7771 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7772 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7773 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007774 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007775
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007776 /*
7777 * Fetch additonal counter values missing in stats collected by driver
7778 * from tally counters.
7779 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007780 if (pm_runtime_active(&pdev->dev))
7781 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007782
7783 /*
7784 * Subtract values fetched during initalization.
7785 * See rtl8169_init_counter_offsets for a description why we do that.
7786 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007787 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007788 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007789 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007790 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007791 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007792 le16_to_cpu(tp->tc_offset.tx_aborted);
7793
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007794 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795}
7796
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007797static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007798{
françois romieu065c27c2011-01-03 15:08:12 +00007799 struct rtl8169_private *tp = netdev_priv(dev);
7800
Francois Romieu5d06a992006-02-23 00:47:58 +01007801 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007802 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007803
7804 netif_device_detach(dev);
7805 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007806
7807 rtl_lock_work(tp);
7808 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007809 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007810 rtl_unlock_work(tp);
7811
7812 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007813}
Francois Romieu5d06a992006-02-23 00:47:58 +01007814
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007815#ifdef CONFIG_PM
7816
7817static int rtl8169_suspend(struct device *device)
7818{
7819 struct pci_dev *pdev = to_pci_dev(device);
7820 struct net_device *dev = pci_get_drvdata(pdev);
7821
7822 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007823
Francois Romieu5d06a992006-02-23 00:47:58 +01007824 return 0;
7825}
7826
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007827static void __rtl8169_resume(struct net_device *dev)
7828{
françois romieu065c27c2011-01-03 15:08:12 +00007829 struct rtl8169_private *tp = netdev_priv(dev);
7830
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007831 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007832
7833 rtl_pll_power_up(tp);
7834
Artem Savkovcff4c162012-04-03 10:29:11 +00007835 rtl_lock_work(tp);
7836 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007837 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007838 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007839
Francois Romieu98ddf982012-01-31 10:47:34 +01007840 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007841}
7842
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007843static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007844{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007845 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007846 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007847 struct rtl8169_private *tp = netdev_priv(dev);
7848
7849 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007850
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007851 if (netif_running(dev))
7852 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007853
Francois Romieu5d06a992006-02-23 00:47:58 +01007854 return 0;
7855}
7856
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007857static int rtl8169_runtime_suspend(struct device *device)
7858{
7859 struct pci_dev *pdev = to_pci_dev(device);
7860 struct net_device *dev = pci_get_drvdata(pdev);
7861 struct rtl8169_private *tp = netdev_priv(dev);
7862
Heiner Kallweita92a0842018-01-08 21:39:13 +01007863 if (!tp->TxDescArray) {
7864 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007865 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007866 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007867
Francois Romieuda78dbf2012-01-26 14:18:23 +01007868 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007869 tp->saved_wolopts = __rtl8169_get_wol(tp);
7870 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007871 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007872
7873 rtl8169_net_suspend(dev);
7874
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007875 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007876 rtl8169_rx_missed(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007877 rtl8169_update_counters(dev);
7878
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007879 return 0;
7880}
7881
7882static int rtl8169_runtime_resume(struct device *device)
7883{
7884 struct pci_dev *pdev = to_pci_dev(device);
7885 struct net_device *dev = pci_get_drvdata(pdev);
7886 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007887 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007888
7889 if (!tp->TxDescArray)
7890 return 0;
7891
Francois Romieuda78dbf2012-01-26 14:18:23 +01007892 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007893 __rtl8169_set_wol(tp, tp->saved_wolopts);
7894 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007895 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007896
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007897 rtl8169_init_phy(dev, tp);
7898
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007899 __rtl8169_resume(dev);
7900
7901 return 0;
7902}
7903
7904static int rtl8169_runtime_idle(struct device *device)
7905{
7906 struct pci_dev *pdev = to_pci_dev(device);
7907 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007908
Heiner Kallweita92a0842018-01-08 21:39:13 +01007909 if (!netif_running(dev) || !netif_carrier_ok(dev))
7910 pm_schedule_suspend(device, 10000);
7911
7912 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007913}
7914
Alexey Dobriyan47145212009-12-14 18:00:08 -08007915static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007916 .suspend = rtl8169_suspend,
7917 .resume = rtl8169_resume,
7918 .freeze = rtl8169_suspend,
7919 .thaw = rtl8169_resume,
7920 .poweroff = rtl8169_suspend,
7921 .restore = rtl8169_resume,
7922 .runtime_suspend = rtl8169_runtime_suspend,
7923 .runtime_resume = rtl8169_runtime_resume,
7924 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007925};
7926
7927#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7928
7929#else /* !CONFIG_PM */
7930
7931#define RTL8169_PM_OPS NULL
7932
7933#endif /* !CONFIG_PM */
7934
David S. Miller1805b2f2011-10-24 18:18:09 -04007935static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7936{
David S. Miller1805b2f2011-10-24 18:18:09 -04007937 /* WoL fails with 8168b when the receiver is disabled. */
7938 switch (tp->mac_version) {
7939 case RTL_GIGA_MAC_VER_11:
7940 case RTL_GIGA_MAC_VER_12:
7941 case RTL_GIGA_MAC_VER_17:
7942 pci_clear_master(tp->pci_dev);
7943
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007944 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007945 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007946 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007947 break;
7948 default:
7949 break;
7950 }
7951}
7952
Francois Romieu1765f952008-09-13 17:21:40 +02007953static void rtl_shutdown(struct pci_dev *pdev)
7954{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007955 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007956 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007957
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007958 rtl8169_net_suspend(dev);
7959
Francois Romieucecb5fd2011-04-01 10:21:07 +02007960 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007961 rtl_rar_set(tp, dev->perm_addr);
7962
Hayes Wang92fc43b2011-07-06 15:58:03 +08007963 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007964
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007965 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007966 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7967 rtl_wol_suspend_quirk(tp);
7968 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007969 }
7970
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007971 pci_wake_from_d3(pdev, true);
7972 pci_set_power_state(pdev, PCI_D3hot);
7973 }
7974}
Francois Romieu5d06a992006-02-23 00:47:58 +01007975
Bill Pembertonbaf63292012-12-03 09:23:28 -05007976static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007977{
7978 struct net_device *dev = pci_get_drvdata(pdev);
7979 struct rtl8169_private *tp = netdev_priv(dev);
7980
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007981 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007982 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007983
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007984 netif_napi_del(&tp->napi);
7985
Francois Romieue27566e2012-03-08 09:54:01 +01007986 unregister_netdev(dev);
7987
7988 rtl_release_firmware(tp);
7989
7990 if (pci_dev_run_wake(pdev))
7991 pm_runtime_get_noresume(&pdev->dev);
7992
7993 /* restore original MAC address */
7994 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007995}
7996
Francois Romieufa9c3852012-03-08 10:01:50 +01007997static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007998 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007999 .ndo_stop = rtl8169_close,
8000 .ndo_get_stats64 = rtl8169_get_stats64,
8001 .ndo_start_xmit = rtl8169_start_xmit,
8002 .ndo_tx_timeout = rtl8169_tx_timeout,
8003 .ndo_validate_addr = eth_validate_addr,
8004 .ndo_change_mtu = rtl8169_change_mtu,
8005 .ndo_fix_features = rtl8169_fix_features,
8006 .ndo_set_features = rtl8169_set_features,
8007 .ndo_set_mac_address = rtl_set_mac_address,
8008 .ndo_do_ioctl = rtl8169_ioctl,
8009 .ndo_set_rx_mode = rtl_set_rx_mode,
8010#ifdef CONFIG_NET_POLL_CONTROLLER
8011 .ndo_poll_controller = rtl8169_netpoll,
8012#endif
8013
8014};
8015
Francois Romieu31fa8b12012-03-08 10:09:40 +01008016static const struct rtl_cfg_info {
8017 void (*hw_start)(struct net_device *);
8018 unsigned int region;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008019 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008020 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03008021 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008022 u8 default_ver;
8023} rtl_cfg_infos [] = {
8024 [RTL_CFG_0] = {
8025 .hw_start = rtl_hw_start_8169,
8026 .region = 1,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008027 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008028 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008029 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008030 .default_ver = RTL_GIGA_MAC_VER_01,
8031 },
8032 [RTL_CFG_1] = {
8033 .hw_start = rtl_hw_start_8168,
8034 .region = 2,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008035 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008036 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008037 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008038 .default_ver = RTL_GIGA_MAC_VER_11,
8039 },
8040 [RTL_CFG_2] = {
8041 .hw_start = rtl_hw_start_8101,
8042 .region = 2,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008043 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8044 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03008045 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008046 .default_ver = RTL_GIGA_MAC_VER_13,
8047 }
8048};
8049
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008050static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01008051{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008052 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008053
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008054 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008055 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
8056 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
8057 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008058 flags = PCI_IRQ_LEGACY;
8059 } else {
8060 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008061 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008062
8063 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01008064}
8065
Hayes Wangc5583862012-07-02 17:23:22 +08008066DECLARE_RTL_COND(rtl_link_list_ready_cond)
8067{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008068 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08008069}
8070
8071DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8072{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008073 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08008074}
8075
Bill Pembertonbaf63292012-12-03 09:23:28 -05008076static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008077{
Hayes Wangc5583862012-07-02 17:23:22 +08008078 u32 data;
8079
8080 tp->ocp_base = OCP_STD_PHY_BASE;
8081
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008082 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08008083
8084 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8085 return;
8086
8087 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8088 return;
8089
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008090 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08008091 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008092 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08008093
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008094 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008095 data &= ~(1 << 14);
8096 r8168_mac_ocp_write(tp, 0xe8de, data);
8097
8098 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8099 return;
8100
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008101 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008102 data |= (1 << 15);
8103 r8168_mac_ocp_write(tp, 0xe8de, data);
8104
8105 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8106 return;
8107}
8108
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008109static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8110{
8111 rtl8168ep_stop_cmac(tp);
8112 rtl_hw_init_8168g(tp);
8113}
8114
Bill Pembertonbaf63292012-12-03 09:23:28 -05008115static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008116{
8117 switch (tp->mac_version) {
8118 case RTL_GIGA_MAC_VER_40:
8119 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008120 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008121 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008122 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008123 case RTL_GIGA_MAC_VER_45:
8124 case RTL_GIGA_MAC_VER_46:
8125 case RTL_GIGA_MAC_VER_47:
8126 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008127 rtl_hw_init_8168g(tp);
8128 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008129 case RTL_GIGA_MAC_VER_49:
8130 case RTL_GIGA_MAC_VER_50:
8131 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008132 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008133 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008134 default:
8135 break;
8136 }
8137}
8138
hayeswang929a0312014-09-16 11:40:47 +08008139static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008140{
8141 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8142 const unsigned int region = cfg->region;
8143 struct rtl8169_private *tp;
8144 struct mii_if_info *mii;
8145 struct net_device *dev;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008146 int chipset, i;
8147 int rc;
8148
8149 if (netif_msg_drv(&debug)) {
8150 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8151 MODULENAME, RTL8169_VERSION);
8152 }
8153
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008154 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8155 if (!dev)
8156 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008157
8158 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008159 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008160 tp = netdev_priv(dev);
8161 tp->dev = dev;
8162 tp->pci_dev = pdev;
8163 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8164
8165 mii = &tp->mii;
8166 mii->dev = dev;
8167 mii->mdio_read = rtl_mdio_read;
8168 mii->mdio_write = rtl_mdio_write;
8169 mii->phy_id_mask = 0x1f;
8170 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008171 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008172
8173 /* disable ASPM completely as that cause random device stop working
8174 * problems as well as full system hangs for some PCIe devices users */
8175 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8176 PCIE_LINK_STATE_CLKPM);
8177
8178 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008179 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008180 if (rc < 0) {
8181 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008182 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008183 }
8184
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008185 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008186 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8187
8188 /* make sure PCI base addr 1 is MMIO */
8189 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8190 netif_err(tp, probe, dev,
8191 "region #%d not an MMIO resource, aborting\n",
8192 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008193 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008194 }
8195
8196 /* check for weird/broken PCI region reporting */
8197 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8198 netif_err(tp, probe, dev,
8199 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008200 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008201 }
8202
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008203 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008204 if (rc < 0) {
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008205 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008206 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008207 }
8208
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008209 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01008210
8211 if (!pci_is_pcie(pdev))
8212 netif_info(tp, probe, dev, "not PCI Express\n");
8213
8214 /* Identify chip attached to board */
8215 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8216
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008217 tp->cp_cmd = 0;
8218
8219 if ((sizeof(dma_addr_t) > 4) &&
8220 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8221 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008222 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8223 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008224
8225 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8226 if (!pci_is_pcie(pdev))
8227 tp->cp_cmd |= PCIDAC;
8228 dev->features |= NETIF_F_HIGHDMA;
8229 } else {
8230 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8231 if (rc < 0) {
8232 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008233 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008234 }
8235 }
8236
Francois Romieu3b6cf252012-03-08 09:59:04 +01008237 rtl_init_rxcfg(tp);
8238
8239 rtl_irq_disable(tp);
8240
Hayes Wangc5583862012-07-02 17:23:22 +08008241 rtl_hw_initialize(tp);
8242
Francois Romieu3b6cf252012-03-08 09:59:04 +01008243 rtl_hw_reset(tp);
8244
8245 rtl_ack_events(tp, 0xffff);
8246
8247 pci_set_master(pdev);
8248
Francois Romieu3b6cf252012-03-08 09:59:04 +01008249 rtl_init_mdio_ops(tp);
8250 rtl_init_pll_power_ops(tp);
8251 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008252 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008253
8254 rtl8169_print_mac_version(tp);
8255
8256 chipset = tp->mac_version;
8257 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8258
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008259 rc = rtl_alloc_irq(tp);
8260 if (rc < 0) {
8261 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8262 return rc;
8263 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008264
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008265 /* override BIOS settings, use userspace tools to enable WOL */
8266 __rtl8169_set_wol(tp, 0);
8267
Francois Romieu3b6cf252012-03-08 09:59:04 +01008268 if (rtl_tbi_enabled(tp)) {
8269 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008270 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008271 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8272 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8273 tp->link_ok = rtl8169_tbi_link_ok;
8274 tp->do_ioctl = rtl_tbi_ioctl;
8275 } else {
8276 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008277 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008278 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8279 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8280 tp->link_ok = rtl8169_xmii_link_ok;
8281 tp->do_ioctl = rtl_xmii_ioctl;
8282 }
8283
8284 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008285 u64_stats_init(&tp->rx_stats.syncp);
8286 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008287
8288 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008289 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8290 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8291 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8292 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8293 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8294 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8295 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8296 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8297 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8298 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008299 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8300 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008301 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8302 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8303 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8304 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008305 u16 mac_addr[3];
8306
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008307 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8308 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008309
8310 if (is_valid_ether_addr((u8 *)mac_addr))
8311 rtl_rar_set(tp, (u8 *)mac_addr);
8312 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008313 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008314 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008315
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008316 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008317 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008318
8319 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8320
8321 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8322 * properly for all devices */
8323 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008324 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008325
8326 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008327 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8328 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008329 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8330 NETIF_F_HIGHDMA;
8331
hayeswang929a0312014-09-16 11:40:47 +08008332 tp->cp_cmd |= RxChkSum | RxVlan;
8333
8334 /*
8335 * Pretend we are using VLANs; This bypasses a nasty bug where
8336 * Interrupts stop flowing on high load on 8110SCd controllers.
8337 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008338 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008339 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008340 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008341
hayeswang5888d3f2014-07-11 16:25:56 +08008342 if (tp->txd_version == RTL_TD_0)
8343 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008344 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008345 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008346 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8347 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008348 WARN_ON_ONCE(1);
8349
Francois Romieu3b6cf252012-03-08 09:59:04 +01008350 dev->hw_features |= NETIF_F_RXALL;
8351 dev->hw_features |= NETIF_F_RXFCS;
8352
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008353 /* MTU range: 60 - hw-specific max */
8354 dev->min_mtu = ETH_ZLEN;
8355 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8356
Francois Romieu3b6cf252012-03-08 09:59:04 +01008357 tp->hw_start = cfg->hw_start;
8358 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008359 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008360
8361 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8362 ~(RxBOVF | RxFOVF) : ~0;
8363
Kees Cook9de36cc2017-10-25 03:53:12 -07008364 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008365
8366 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8367
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008368 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8369 &tp->counters_phys_addr,
8370 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008371 if (!tp->counters)
8372 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008373
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02008374 pci_set_drvdata(pdev, dev);
8375
Francois Romieu3b6cf252012-03-08 09:59:04 +01008376 rc = register_netdev(dev);
8377 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008378 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008379
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008380 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008381 rtl_chip_infos[chipset].name, tp->mmio_addr, dev->dev_addr,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008382 (u32)(RTL_R32(tp, TxConfig) & 0x9cf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01008383 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01008384 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8385 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8386 "tx checksumming: %s]\n",
8387 rtl_chip_infos[chipset].jumbo_max,
8388 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8389 }
8390
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008391 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008392 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008393
Francois Romieu3b6cf252012-03-08 09:59:04 +01008394 netif_carrier_off(dev);
8395
Heiner Kallweita92a0842018-01-08 21:39:13 +01008396 if (pci_dev_run_wake(pdev))
8397 pm_runtime_put_sync(&pdev->dev);
8398
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008399 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008400}
8401
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402static struct pci_driver rtl8169_pci_driver = {
8403 .name = MODULENAME,
8404 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008405 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008406 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008407 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008408 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409};
8410
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008411module_pci_driver(rtl8169_pci_driver);