blob: 7ee7b67b7a08cd3579d4c973305a5b89b92a0be1 [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
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020091#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#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
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200347static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200348static struct {
349 u32 msg_enable;
350} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Francois Romieu07d3f512007-02-21 22:40:46 +0100352enum rtl_registers {
353 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100354 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100355 MAR0 = 8, /* Multicast filter. */
356 CounterAddrLow = 0x10,
357 CounterAddrHigh = 0x14,
358 TxDescStartAddrLow = 0x20,
359 TxDescStartAddrHigh = 0x24,
360 TxHDescStartAddrLow = 0x28,
361 TxHDescStartAddrHigh = 0x2c,
362 FLASH = 0x30,
363 ERSR = 0x36,
364 ChipCmd = 0x37,
365 TxPoll = 0x38,
366 IntrMask = 0x3c,
367 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700368
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800369 TxConfig = 0x40,
370#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
371#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
372
373 RxConfig = 0x44,
374#define RX128_INT_EN (1 << 15) /* 8111c and later */
375#define RX_MULTI_EN (1 << 14) /* 8111c only */
376#define RXCFG_FIFO_SHIFT 13
377 /* No threshold before first PCI xfer */
378#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000379#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800380#define RXCFG_DMA_SHIFT 8
381 /* Unlimited maximum PCI burst. */
382#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700383
Francois Romieu07d3f512007-02-21 22:40:46 +0100384 RxMissed = 0x4c,
385 Cfg9346 = 0x50,
386 Config0 = 0x51,
387 Config1 = 0x52,
388 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200389#define PME_SIGNAL (1 << 5) /* 8168c and later */
390
Francois Romieu07d3f512007-02-21 22:40:46 +0100391 Config3 = 0x54,
392 Config4 = 0x55,
393 Config5 = 0x56,
394 MultiIntr = 0x5c,
395 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100396 PHYstatus = 0x6c,
397 RxMaxSize = 0xda,
398 CPlusCmd = 0xe0,
399 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300400
401#define RTL_COALESCE_MASK 0x0f
402#define RTL_COALESCE_SHIFT 4
403#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
404#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
405
Francois Romieu07d3f512007-02-21 22:40:46 +0100406 RxDescAddrLow = 0xe4,
407 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000408 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
409
410#define NoEarlyTx 0x3f /* Max value : no early transmit. */
411
412 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
413
414#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800415#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000416
Francois Romieu07d3f512007-02-21 22:40:46 +0100417 FuncEvent = 0xf0,
418 FuncEventMask = 0xf4,
419 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800420 IBCR0 = 0xf8,
421 IBCR2 = 0xf9,
422 IBIMR0 = 0xfa,
423 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100424 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
Francois Romieuf162a5d2008-06-01 22:37:49 +0200427enum rtl8110_registers {
428 TBICSR = 0x64,
429 TBI_ANAR = 0x68,
430 TBI_LPAR = 0x6a,
431};
432
433enum rtl8168_8101_registers {
434 CSIDR = 0x64,
435 CSIAR = 0x68,
436#define CSIAR_FLAG 0x80000000
437#define CSIAR_WRITE_CMD 0x80000000
438#define CSIAR_BYTE_ENABLE 0x0f
439#define CSIAR_BYTE_ENABLE_SHIFT 12
440#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800441#define CSIAR_FUNC_CARD 0x00000000
442#define CSIAR_FUNC_SDIO 0x00010000
443#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800444#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000445 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200446 EPHYAR = 0x80,
447#define EPHYAR_FLAG 0x80000000
448#define EPHYAR_WRITE_CMD 0x80000000
449#define EPHYAR_REG_MASK 0x1f
450#define EPHYAR_REG_SHIFT 16
451#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800452 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800453#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800454#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200455 DBG_REG = 0xd1,
456#define FIX_NAK_1 (1 << 4)
457#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800458 TWSI = 0xd2,
459 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800460#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800461#define TX_EMPTY (1 << 5)
462#define RX_EMPTY (1 << 4)
463#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800464#define EN_NDP (1 << 3)
465#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800466#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000467 EFUSEAR = 0xdc,
468#define EFUSEAR_FLAG 0x80000000
469#define EFUSEAR_WRITE_CMD 0x80000000
470#define EFUSEAR_READ_CMD 0x00000000
471#define EFUSEAR_REG_MASK 0x03ff
472#define EFUSEAR_REG_SHIFT 8
473#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800474 MISC_1 = 0xf2,
475#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200476};
477
françois romieuc0e45c12011-01-03 15:08:04 +0000478enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800479 LED_FREQ = 0x1a,
480 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000481 ERIDR = 0x70,
482 ERIAR = 0x74,
483#define ERIAR_FLAG 0x80000000
484#define ERIAR_WRITE_CMD 0x80000000
485#define ERIAR_READ_CMD 0x00000000
486#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000487#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800488#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
489#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
490#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800491#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800492#define ERIAR_MASK_SHIFT 12
493#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
494#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800495#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800496#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800497#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000498 EPHY_RXER_NUM = 0x7c,
499 OCPDR = 0xb0, /* OCP GPHY access */
500#define OCPDR_WRITE_CMD 0x80000000
501#define OCPDR_READ_CMD 0x00000000
502#define OCPDR_REG_MASK 0x7f
503#define OCPDR_GPHY_REG_SHIFT 16
504#define OCPDR_DATA_MASK 0xffff
505 OCPAR = 0xb4,
506#define OCPAR_FLAG 0x80000000
507#define OCPAR_GPHY_WRITE_CMD 0x8000f060
508#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800509 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000510 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
511 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200512#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800513#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800514#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800515#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800516#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000517};
518
Francois Romieu07d3f512007-02-21 22:40:46 +0100519enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100521 SYSErr = 0x8000,
522 PCSTimeout = 0x4000,
523 SWInt = 0x0100,
524 TxDescUnavail = 0x0080,
525 RxFIFOOver = 0x0040,
526 LinkChg = 0x0020,
527 RxOverflow = 0x0010,
528 TxErr = 0x0008,
529 TxOK = 0x0004,
530 RxErr = 0x0002,
531 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400534 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200535 RxFOVF = (1 << 23),
536 RxRWT = (1 << 22),
537 RxRES = (1 << 21),
538 RxRUNT = (1 << 20),
539 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800542 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100543 CmdReset = 0x10,
544 CmdRxEnb = 0x08,
545 CmdTxEnb = 0x04,
546 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Francois Romieu275391a2007-02-23 23:50:28 +0100548 /* TXPoll register p.5 */
549 HPQ = 0x80, /* Poll cmd on the high prio queue */
550 NPQ = 0x40, /* Poll cmd on the low prio queue */
551 FSWInt = 0x01, /* Forced software interrupt */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100554 Cfg9346_Lock = 0x00,
555 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100558 AcceptErr = 0x20,
559 AcceptRunt = 0x10,
560 AcceptBroadcast = 0x08,
561 AcceptMulticast = 0x04,
562 AcceptMyPhys = 0x02,
563 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200564#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* TxConfigBits */
567 TxInterFrameGapShift = 24,
568 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
569
Francois Romieu5d06a992006-02-23 00:47:58 +0100570 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200571 LEDS1 = (1 << 7),
572 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200573 Speed_down = (1 << 4),
574 MEMMAP = (1 << 3),
575 IOMAP = (1 << 2),
576 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100577 PMEnable = (1 << 0), /* Power Management Enable */
578
Francois Romieu6dccd162007-02-13 23:38:05 +0100579 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000580 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000581 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100582 PCI_Clock_66MHz = 0x01,
583 PCI_Clock_33MHz = 0x00,
584
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100585 /* Config3 register p.25 */
586 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
587 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200588 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800589 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200590 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100591
Francois Romieud58d46b2011-05-03 16:38:29 +0200592 /* Config4 register */
593 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
594
Francois Romieu5d06a992006-02-23 00:47:58 +0100595 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100596 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
597 MWF = (1 << 5), /* Accept Multicast wakeup frame */
598 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200599 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100600 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100601 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000602 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* TBICSR p.28 */
605 TBIReset = 0x80000000,
606 TBILoopback = 0x40000000,
607 TBINwEnable = 0x20000000,
608 TBINwRestart = 0x10000000,
609 TBILinkOk = 0x02000000,
610 TBINwComplete = 0x01000000,
611
612 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200613 EnableBist = (1 << 15), // 8168 8101
614 Mac_dbgo_oe = (1 << 14), // 8168 8101
615 Normal_mode = (1 << 13), // unused
616 Force_half_dup = (1 << 12), // 8168 8101
617 Force_rxflow_en = (1 << 11), // 8168 8101
618 Force_txflow_en = (1 << 10), // 8168 8101
619 Cxpl_dbg_sel = (1 << 9), // 8168 8101
620 ASF = (1 << 8), // 8168 8101
621 PktCntrDisable = (1 << 7), // 8168 8101
622 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 RxVlan = (1 << 6),
624 RxChkSum = (1 << 5),
625 PCIDAC = (1 << 4),
626 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100627 INTT_0 = 0x0000, // 8168
628 INTT_1 = 0x0001, // 8168
629 INTT_2 = 0x0002, // 8168
630 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100633 TBI_Enable = 0x80,
634 TxFlowCtrl = 0x40,
635 RxFlowCtrl = 0x20,
636 _1000bpsF = 0x10,
637 _100bps = 0x08,
638 _10bps = 0x04,
639 LinkStatus = 0x02,
640 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100643 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200644
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200645 /* ResetCounterCommand */
646 CounterReset = 0x1,
647
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200648 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100649 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800650
651 /* magic enable v2 */
652 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653};
654
Francois Romieu2b7b4312011-04-18 22:53:24 -0700655enum rtl_desc_bit {
656 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
658 RingEnd = (1 << 30), /* End of descriptor ring */
659 FirstFrag = (1 << 29), /* First segment of a packet */
660 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700661};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663/* Generic case. */
664enum rtl_tx_desc_bit {
665 /* First doubleword. */
666 TD_LSO = (1 << 27), /* Large Send Offload */
667#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Francois Romieu2b7b4312011-04-18 22:53:24 -0700669 /* Second doubleword. */
670 TxVlanTag = (1 << 17), /* Add VLAN tag */
671};
672
673/* 8169, 8168b and 810x except 8102e. */
674enum rtl_tx_desc_bit_0 {
675 /* First doubleword. */
676#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
677 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
678 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
679 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
680};
681
682/* 8102e, 8168c and beyond. */
683enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800684 /* First doubleword. */
685 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800686 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800687#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800688#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800689
Francois Romieu2b7b4312011-04-18 22:53:24 -0700690 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800691#define TCPHO_SHIFT 18
692#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700693#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800694 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
695 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700696 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
697 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
698};
699
Francois Romieu2b7b4312011-04-18 22:53:24 -0700700enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Rx private */
702 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500703 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705#define RxProtoUDP (PID1)
706#define RxProtoTCP (PID0)
707#define RxProtoIP (PID1 | PID0)
708#define RxProtoMask RxProtoIP
709
710 IPFail = (1 << 16), /* IP checksum failed */
711 UDPFail = (1 << 15), /* UDP/IP checksum failed */
712 TCPFail = (1 << 14), /* TCP/IP checksum failed */
713 RxVlanTag = (1 << 16), /* VLAN tag available */
714};
715
716#define RsvdMask 0x3fffc000
717
718struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200719 __le32 opts1;
720 __le32 opts2;
721 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722};
723
724struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200725 __le32 opts1;
726 __le32 opts2;
727 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728};
729
730struct ring_info {
731 struct sk_buff *skb;
732 u32 len;
733 u8 __pad[sizeof(void *) - sizeof(u32)];
734};
735
Ivan Vecera355423d2009-02-06 21:49:57 -0800736struct rtl8169_counters {
737 __le64 tx_packets;
738 __le64 rx_packets;
739 __le64 tx_errors;
740 __le32 rx_errors;
741 __le16 rx_missed;
742 __le16 align_errors;
743 __le32 tx_one_collision;
744 __le32 tx_multi_collision;
745 __le64 rx_unicast;
746 __le64 rx_broadcast;
747 __le32 rx_multicast;
748 __le16 tx_aborted;
749 __le16 tx_underun;
750};
751
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200752struct rtl8169_tc_offsets {
753 bool inited;
754 __le64 tx_errors;
755 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200756 __le16 tx_aborted;
757};
758
Francois Romieuda78dbf2012-01-26 14:18:23 +0100759enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100760 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100761 RTL_FLAG_TASK_SLOW_PENDING,
762 RTL_FLAG_TASK_RESET_PENDING,
763 RTL_FLAG_TASK_PHY_PENDING,
764 RTL_FLAG_MAX
765};
766
Junchang Wang8027aa22012-03-04 23:30:32 +0100767struct rtl8169_stats {
768 u64 packets;
769 u64 bytes;
770 struct u64_stats_sync syncp;
771};
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773struct rtl8169_private {
774 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200775 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000776 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700777 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200778 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700779 u16 txd_version;
780 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
782 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100784 struct rtl8169_stats rx_stats;
785 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
787 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
788 dma_addr_t TxPhyAddr;
789 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000790 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct timer_list timer;
793 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100794
795 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300796 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000797
798 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200799 void (*write)(struct rtl8169_private *, int, int);
800 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000801 } mdio_ops;
802
françois romieu065c27c2011-01-03 15:08:12 +0000803 struct pll_power_ops {
804 void (*down)(struct rtl8169_private *);
805 void (*up)(struct rtl8169_private *);
806 } pll_power_ops;
807
Francois Romieud58d46b2011-05-03 16:38:29 +0200808 struct jumbo_ops {
809 void (*enable)(struct rtl8169_private *);
810 void (*disable)(struct rtl8169_private *);
811 } jumbo_ops;
812
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800813 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200814 void (*write)(struct rtl8169_private *, int, int);
815 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800816 } csi_ops;
817
Oliver Neukum54405cd2011-01-06 21:55:13 +0100818 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100819 int (*get_link_ksettings)(struct net_device *,
820 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000821 void (*phy_reset_enable)(struct rtl8169_private *tp);
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200822 void (*hw_start)(struct rtl8169_private *tp);
françois romieu4da19632011-01-03 15:07:55 +0000823 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200824 unsigned int (*link_ok)(struct rtl8169_private *tp);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800825 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800826 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100827
828 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100829 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
830 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100831 struct work_struct work;
832 } wk;
833
Francois Romieuccdffb92008-07-26 14:26:06 +0200834 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200835 dma_addr_t counters_phys_addr;
836 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200837 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000838 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400839 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000840
Francois Romieub6ffd972011-06-17 17:00:05 +0200841 struct rtl_fw {
842 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200843
844#define RTL_VER_SIZE 32
845
846 char version[RTL_VER_SIZE];
847
848 struct rtl_fw_phy_action {
849 __le32 *code;
850 size_t size;
851 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200852 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300853#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800854
855 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856};
857
Ralf Baechle979b6c12005-06-13 14:30:40 -0700858MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700861MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200862module_param_named(debug, debug.msg_enable, int, 0);
863MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864MODULE_LICENSE("GPL");
865MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000866MODULE_FIRMWARE(FIRMWARE_8168D_1);
867MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000868MODULE_FIRMWARE(FIRMWARE_8168E_1);
869MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400870MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800871MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800872MODULE_FIRMWARE(FIRMWARE_8168F_1);
873MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800874MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800875MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800876MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800877MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000878MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000879MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000880MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800881MODULE_FIRMWARE(FIRMWARE_8168H_1);
882MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200883MODULE_FIRMWARE(FIRMWARE_8107E_1);
884MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100886static inline struct device *tp_to_dev(struct rtl8169_private *tp)
887{
888 return &tp->pci_dev->dev;
889}
890
Francois Romieuda78dbf2012-01-26 14:18:23 +0100891static void rtl_lock_work(struct rtl8169_private *tp)
892{
893 mutex_lock(&tp->wk.mutex);
894}
895
896static void rtl_unlock_work(struct rtl8169_private *tp)
897{
898 mutex_unlock(&tp->wk.mutex);
899}
900
Heiner Kallweitcb732002018-03-20 07:45:35 +0100901static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200902{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100903 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800904 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200905}
906
Francois Romieuffc46952012-07-06 14:19:23 +0200907struct rtl_cond {
908 bool (*check)(struct rtl8169_private *);
909 const char *msg;
910};
911
912static void rtl_udelay(unsigned int d)
913{
914 udelay(d);
915}
916
917static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
918 void (*delay)(unsigned int), unsigned int d, int n,
919 bool high)
920{
921 int i;
922
923 for (i = 0; i < n; i++) {
924 delay(d);
925 if (c->check(tp) == high)
926 return true;
927 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200928 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
929 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200930 return false;
931}
932
933static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
934 const struct rtl_cond *c,
935 unsigned int d, int n)
936{
937 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
938}
939
940static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
941 const struct rtl_cond *c,
942 unsigned int d, int n)
943{
944 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
945}
946
947static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
948 const struct rtl_cond *c,
949 unsigned int d, int n)
950{
951 return rtl_loop_wait(tp, c, msleep, d, n, true);
952}
953
954static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
955 const struct rtl_cond *c,
956 unsigned int d, int n)
957{
958 return rtl_loop_wait(tp, c, msleep, d, n, false);
959}
960
961#define DECLARE_RTL_COND(name) \
962static bool name ## _check(struct rtl8169_private *); \
963 \
964static const struct rtl_cond name = { \
965 .check = name ## _check, \
966 .msg = #name \
967}; \
968 \
969static bool name ## _check(struct rtl8169_private *tp)
970
Hayes Wangc5583862012-07-02 17:23:22 +0800971static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
972{
973 if (reg & 0xffff0001) {
974 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
975 return true;
976 }
977 return false;
978}
979
980DECLARE_RTL_COND(rtl_ocp_gphy_cond)
981{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200982 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800983}
984
985static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
986{
Hayes Wangc5583862012-07-02 17:23:22 +0800987 if (rtl_ocp_reg_failure(tp, reg))
988 return;
989
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200990 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800991
992 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
993}
994
995static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
996{
Hayes Wangc5583862012-07-02 17:23:22 +0800997 if (rtl_ocp_reg_failure(tp, reg))
998 return 0;
999
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001000 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001001
1002 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001003 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +08001004}
1005
Hayes Wangc5583862012-07-02 17:23:22 +08001006static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1007{
Hayes Wangc5583862012-07-02 17:23:22 +08001008 if (rtl_ocp_reg_failure(tp, reg))
1009 return;
1010
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001011 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001012}
1013
1014static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1015{
Hayes Wangc5583862012-07-02 17:23:22 +08001016 if (rtl_ocp_reg_failure(tp, reg))
1017 return 0;
1018
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001019 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +08001020
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001021 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001022}
1023
1024#define OCP_STD_PHY_BASE 0xa400
1025
1026static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1027{
1028 if (reg == 0x1f) {
1029 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1030 return;
1031 }
1032
1033 if (tp->ocp_base != OCP_STD_PHY_BASE)
1034 reg -= 0x10;
1035
1036 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1037}
1038
1039static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1040{
1041 if (tp->ocp_base != OCP_STD_PHY_BASE)
1042 reg -= 0x10;
1043
1044 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1045}
1046
hayeswangeee37862013-04-01 22:23:38 +00001047static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1048{
1049 if (reg == 0x1f) {
1050 tp->ocp_base = value << 4;
1051 return;
1052 }
1053
1054 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1055}
1056
1057static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1058{
1059 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1060}
1061
Francois Romieuffc46952012-07-06 14:19:23 +02001062DECLARE_RTL_COND(rtl_phyar_cond)
1063{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001064 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +02001065}
1066
Francois Romieu24192212012-07-06 20:19:42 +02001067static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001069 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Francois Romieuffc46952012-07-06 14:19:23 +02001071 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001072 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001073 * According to hardware specs a 20us delay is required after write
1074 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001075 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001076 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Francois Romieu24192212012-07-06 20:19:42 +02001079static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Francois Romieuffc46952012-07-06 14:19:23 +02001081 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001083 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Francois Romieuffc46952012-07-06 14:19:23 +02001085 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001086 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +02001087
Timo Teräs81a95f02010-06-09 17:31:48 -07001088 /*
1089 * According to hardware specs a 20us delay is required after read
1090 * complete indication, but before sending next command.
1091 */
1092 udelay(20);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return value;
1095}
1096
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001097DECLARE_RTL_COND(rtl_ocpar_cond)
1098{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001099 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001100}
1101
Francois Romieu24192212012-07-06 20:19:42 +02001102static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001103{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001104 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1105 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1106 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001107
Francois Romieuffc46952012-07-06 14:19:23 +02001108 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001109}
1110
Francois Romieu24192212012-07-06 20:19:42 +02001111static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001112{
Francois Romieu24192212012-07-06 20:19:42 +02001113 r8168dp_1_mdio_access(tp, reg,
1114 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001115}
1116
Francois Romieu24192212012-07-06 20:19:42 +02001117static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001118{
Francois Romieu24192212012-07-06 20:19:42 +02001119 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001120
1121 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001122 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1123 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +00001124
Francois Romieuffc46952012-07-06 14:19:23 +02001125 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001126 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001127}
1128
françois romieue6de30d2011-01-03 15:08:37 +00001129#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1130
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001131static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001132{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001133 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001134}
1135
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001136static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00001137{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001138 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +00001139}
1140
Francois Romieu24192212012-07-06 20:19:42 +02001141static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001142{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001143 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001144
Francois Romieu24192212012-07-06 20:19:42 +02001145 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001146
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001147 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001148}
1149
Francois Romieu24192212012-07-06 20:19:42 +02001150static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001151{
1152 int value;
1153
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001154 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001155
Francois Romieu24192212012-07-06 20:19:42 +02001156 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001157
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001158 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001159
1160 return value;
1161}
1162
françois romieu4da19632011-01-03 15:07:55 +00001163static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001164{
Francois Romieu24192212012-07-06 20:19:42 +02001165 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001166}
1167
françois romieu4da19632011-01-03 15:07:55 +00001168static int rtl_readphy(struct rtl8169_private *tp, int location)
1169{
Francois Romieu24192212012-07-06 20:19:42 +02001170 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001171}
1172
1173static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1174{
1175 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1176}
1177
Chun-Hao Lin76564422014-10-01 23:17:17 +08001178static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001179{
1180 int val;
1181
françois romieu4da19632011-01-03 15:07:55 +00001182 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001183 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001184}
1185
Francois Romieuccdffb92008-07-26 14:26:06 +02001186static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1187 int val)
1188{
1189 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001190
françois romieu4da19632011-01-03 15:07:55 +00001191 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001192}
1193
1194static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1195{
1196 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001197
françois romieu4da19632011-01-03 15:07:55 +00001198 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001199}
1200
Francois Romieuffc46952012-07-06 14:19:23 +02001201DECLARE_RTL_COND(rtl_ephyar_cond)
1202{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001203 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001204}
1205
Francois Romieufdf6fc02012-07-06 22:40:38 +02001206static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001207{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001208 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001209 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1210
Francois Romieuffc46952012-07-06 14:19:23 +02001211 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1212
1213 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001214}
1215
Francois Romieufdf6fc02012-07-06 22:40:38 +02001216static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001217{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001218 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001219
Francois Romieuffc46952012-07-06 14:19:23 +02001220 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001221 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001222}
1223
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001224DECLARE_RTL_COND(rtl_eriar_cond)
1225{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001226 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001227}
1228
Francois Romieufdf6fc02012-07-06 22:40:38 +02001229static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1230 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001231{
Hayes Wang133ac402011-07-06 15:58:05 +08001232 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001233 RTL_W32(tp, ERIDR, val);
1234 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001235
Francois Romieuffc46952012-07-06 14:19:23 +02001236 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001237}
1238
Francois Romieufdf6fc02012-07-06 22:40:38 +02001239static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001240{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001241 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001242
Francois Romieuffc46952012-07-06 14:19:23 +02001243 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001244 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001245}
1246
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001247static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001249{
1250 u32 val;
1251
Francois Romieufdf6fc02012-07-06 22:40:38 +02001252 val = rtl_eri_read(tp, addr, type);
1253 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001254}
1255
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001256static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1257{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001258 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001259 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001260 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001261}
1262
1263static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1264{
1265 return rtl_eri_read(tp, reg, ERIAR_OOB);
1266}
1267
1268static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1269{
1270 switch (tp->mac_version) {
1271 case RTL_GIGA_MAC_VER_27:
1272 case RTL_GIGA_MAC_VER_28:
1273 case RTL_GIGA_MAC_VER_31:
1274 return r8168dp_ocp_read(tp, mask, reg);
1275 case RTL_GIGA_MAC_VER_49:
1276 case RTL_GIGA_MAC_VER_50:
1277 case RTL_GIGA_MAC_VER_51:
1278 return r8168ep_ocp_read(tp, mask, reg);
1279 default:
1280 BUG();
1281 return ~0;
1282 }
1283}
1284
1285static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1286 u32 data)
1287{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001288 RTL_W32(tp, OCPDR, data);
1289 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001290 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1291}
1292
1293static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1294 u32 data)
1295{
1296 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1297 data, ERIAR_OOB);
1298}
1299
1300static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1301{
1302 switch (tp->mac_version) {
1303 case RTL_GIGA_MAC_VER_27:
1304 case RTL_GIGA_MAC_VER_28:
1305 case RTL_GIGA_MAC_VER_31:
1306 r8168dp_ocp_write(tp, mask, reg, data);
1307 break;
1308 case RTL_GIGA_MAC_VER_49:
1309 case RTL_GIGA_MAC_VER_50:
1310 case RTL_GIGA_MAC_VER_51:
1311 r8168ep_ocp_write(tp, mask, reg, data);
1312 break;
1313 default:
1314 BUG();
1315 break;
1316 }
1317}
1318
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001319static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1320{
1321 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1322
1323 ocp_write(tp, 0x1, 0x30, 0x00000001);
1324}
1325
1326#define OOB_CMD_RESET 0x00
1327#define OOB_CMD_DRIVER_START 0x05
1328#define OOB_CMD_DRIVER_STOP 0x06
1329
1330static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1331{
1332 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1333}
1334
1335DECLARE_RTL_COND(rtl_ocp_read_cond)
1336{
1337 u16 reg;
1338
1339 reg = rtl8168_get_ocp_reg(tp);
1340
1341 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1342}
1343
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001344DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1345{
1346 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1347}
1348
1349DECLARE_RTL_COND(rtl_ocp_tx_cond)
1350{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001351 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001352}
1353
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001354static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1355{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001356 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001357 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001358 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1359 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001360}
1361
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001362static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001363{
1364 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001365 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1366}
1367
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001368static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1369{
1370 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1371 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1372 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1373}
1374
1375static void rtl8168_driver_start(struct rtl8169_private *tp)
1376{
1377 switch (tp->mac_version) {
1378 case RTL_GIGA_MAC_VER_27:
1379 case RTL_GIGA_MAC_VER_28:
1380 case RTL_GIGA_MAC_VER_31:
1381 rtl8168dp_driver_start(tp);
1382 break;
1383 case RTL_GIGA_MAC_VER_49:
1384 case RTL_GIGA_MAC_VER_50:
1385 case RTL_GIGA_MAC_VER_51:
1386 rtl8168ep_driver_start(tp);
1387 break;
1388 default:
1389 BUG();
1390 break;
1391 }
1392}
1393
1394static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1395{
1396 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1397 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1398}
1399
1400static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1401{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001402 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001403 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1404 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1405 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1406}
1407
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001408static void rtl8168_driver_stop(struct rtl8169_private *tp)
1409{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001410 switch (tp->mac_version) {
1411 case RTL_GIGA_MAC_VER_27:
1412 case RTL_GIGA_MAC_VER_28:
1413 case RTL_GIGA_MAC_VER_31:
1414 rtl8168dp_driver_stop(tp);
1415 break;
1416 case RTL_GIGA_MAC_VER_49:
1417 case RTL_GIGA_MAC_VER_50:
1418 case RTL_GIGA_MAC_VER_51:
1419 rtl8168ep_driver_stop(tp);
1420 break;
1421 default:
1422 BUG();
1423 break;
1424 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001425}
1426
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001427static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001428{
1429 u16 reg = rtl8168_get_ocp_reg(tp);
1430
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001431 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001432}
1433
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001434static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001435{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001436 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001437}
1438
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001439static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001440{
1441 switch (tp->mac_version) {
1442 case RTL_GIGA_MAC_VER_27:
1443 case RTL_GIGA_MAC_VER_28:
1444 case RTL_GIGA_MAC_VER_31:
1445 return r8168dp_check_dash(tp);
1446 case RTL_GIGA_MAC_VER_49:
1447 case RTL_GIGA_MAC_VER_50:
1448 case RTL_GIGA_MAC_VER_51:
1449 return r8168ep_check_dash(tp);
1450 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001451 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001452 }
1453}
1454
françois romieuc28aa382011-08-02 03:53:43 +00001455struct exgmac_reg {
1456 u16 addr;
1457 u16 mask;
1458 u32 val;
1459};
1460
Francois Romieufdf6fc02012-07-06 22:40:38 +02001461static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001462 const struct exgmac_reg *r, int len)
1463{
1464 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001465 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001466 r++;
1467 }
1468}
1469
Francois Romieuffc46952012-07-06 14:19:23 +02001470DECLARE_RTL_COND(rtl_efusear_cond)
1471{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001472 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001473}
1474
Francois Romieufdf6fc02012-07-06 22:40:38 +02001475static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001476{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001477 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001478
Francois Romieuffc46952012-07-06 14:19:23 +02001479 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001480 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001481}
1482
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001483static u16 rtl_get_events(struct rtl8169_private *tp)
1484{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001485 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001486}
1487
1488static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1489{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001490 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001491 mmiowb();
1492}
1493
1494static void rtl_irq_disable(struct rtl8169_private *tp)
1495{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001496 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001497 mmiowb();
1498}
1499
Francois Romieu3e990ff2012-01-26 12:50:01 +01001500static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1501{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001502 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001503}
1504
Francois Romieuda78dbf2012-01-26 14:18:23 +01001505#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1506#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1507#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1508
1509static void rtl_irq_enable_all(struct rtl8169_private *tp)
1510{
1511 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1512}
1513
françois romieu811fd302011-12-04 20:30:45 +00001514static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001516 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001517 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001518 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
françois romieu4da19632011-01-03 15:07:55 +00001521static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001523 return RTL_R32(tp, TBICSR) & TBIReset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
françois romieu4da19632011-01-03 15:07:55 +00001526static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
françois romieu4da19632011-01-03 15:07:55 +00001528 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001531static unsigned int rtl8169_tbi_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001533 return RTL_R32(tp, TBICSR) & TBILinkOk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001536static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001538 return RTL_R8(tp, PHYstatus) & LinkStatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
françois romieu4da19632011-01-03 15:07:55 +00001541static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001543 RTL_W32(tp, TBICSR, RTL_R32(tp, TBICSR) | TBIReset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
françois romieu4da19632011-01-03 15:07:55 +00001546static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 unsigned int val;
1549
françois romieu4da19632011-01-03 15:07:55 +00001550 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1551 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Hayes Wang70090422011-07-06 15:58:06 +08001554static void rtl_link_chg_patch(struct rtl8169_private *tp)
1555{
Hayes Wang70090422011-07-06 15:58:06 +08001556 struct net_device *dev = tp->dev;
1557
1558 if (!netif_running(dev))
1559 return;
1560
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001561 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1562 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001563 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001564 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1565 ERIAR_EXGMAC);
1566 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1567 ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001568 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001569 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1570 ERIAR_EXGMAC);
1571 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1572 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001573 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001574 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1575 ERIAR_EXGMAC);
1576 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1577 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001578 }
1579 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001580 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001581 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001582 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001583 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001584 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1585 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001586 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001587 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1588 ERIAR_EXGMAC);
1589 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1590 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001591 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001592 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1593 ERIAR_EXGMAC);
1594 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1595 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001596 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001597 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001598 if (RTL_R8(tp, PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001599 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1600 ERIAR_EXGMAC);
1601 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1602 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001603 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001604 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1605 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001606 }
Hayes Wang70090422011-07-06 15:58:06 +08001607 }
1608}
1609
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001610static void rtl8169_check_link_status(struct net_device *dev,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001611 struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001613 struct device *d = tp_to_dev(tp);
1614
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001615 if (tp->link_ok(tp)) {
Hayes Wang70090422011-07-06 15:58:06 +08001616 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001617 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001618 pm_request_resume(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001620 if (net_ratelimit())
1621 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001622 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001624 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001625 pm_runtime_idle(d);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001629#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1630
1631static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1632{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001633 u8 options;
1634 u32 wolopts = 0;
1635
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001636 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001637 if (!(options & PMEnable))
1638 return 0;
1639
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001640 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001641 if (options & LinkUp)
1642 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001643 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001644 case RTL_GIGA_MAC_VER_34:
1645 case RTL_GIGA_MAC_VER_35:
1646 case RTL_GIGA_MAC_VER_36:
1647 case RTL_GIGA_MAC_VER_37:
1648 case RTL_GIGA_MAC_VER_38:
1649 case RTL_GIGA_MAC_VER_40:
1650 case RTL_GIGA_MAC_VER_41:
1651 case RTL_GIGA_MAC_VER_42:
1652 case RTL_GIGA_MAC_VER_43:
1653 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001654 case RTL_GIGA_MAC_VER_45:
1655 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001656 case RTL_GIGA_MAC_VER_47:
1657 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001658 case RTL_GIGA_MAC_VER_49:
1659 case RTL_GIGA_MAC_VER_50:
1660 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001661 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1662 wolopts |= WAKE_MAGIC;
1663 break;
1664 default:
1665 if (options & MagicPacket)
1666 wolopts |= WAKE_MAGIC;
1667 break;
1668 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001669
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001670 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001671 if (options & UWF)
1672 wolopts |= WAKE_UCAST;
1673 if (options & BWF)
1674 wolopts |= WAKE_BCAST;
1675 if (options & MWF)
1676 wolopts |= WAKE_MCAST;
1677
1678 return wolopts;
1679}
1680
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001681static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1682{
1683 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001684 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001685
1686 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001687
Francois Romieuda78dbf2012-01-26 14:18:23 +01001688 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001689
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001690 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001691 if (pm_runtime_active(d))
1692 wol->wolopts = __rtl8169_get_wol(tp);
1693 else
1694 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001695
Francois Romieuda78dbf2012-01-26 14:18:23 +01001696 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001697
1698 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001699}
1700
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001701static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001702{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001703 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001704 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001705 u32 opt;
1706 u16 reg;
1707 u8 mask;
1708 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001709 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001710 { WAKE_UCAST, Config5, UWF },
1711 { WAKE_BCAST, Config5, BWF },
1712 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001713 { WAKE_ANY, Config5, LanWake },
1714 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001715 };
Francois Romieu851e6022012-04-17 11:10:11 +02001716 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001717
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001718 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001719
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001720 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001721 case RTL_GIGA_MAC_VER_34:
1722 case RTL_GIGA_MAC_VER_35:
1723 case RTL_GIGA_MAC_VER_36:
1724 case RTL_GIGA_MAC_VER_37:
1725 case RTL_GIGA_MAC_VER_38:
1726 case RTL_GIGA_MAC_VER_40:
1727 case RTL_GIGA_MAC_VER_41:
1728 case RTL_GIGA_MAC_VER_42:
1729 case RTL_GIGA_MAC_VER_43:
1730 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001731 case RTL_GIGA_MAC_VER_45:
1732 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001733 case RTL_GIGA_MAC_VER_47:
1734 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001735 case RTL_GIGA_MAC_VER_49:
1736 case RTL_GIGA_MAC_VER_50:
1737 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001738 tmp = ARRAY_SIZE(cfg) - 1;
1739 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001740 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001741 0x0dc,
1742 ERIAR_MASK_0100,
1743 MagicPacket_v2,
1744 0x0000,
1745 ERIAR_EXGMAC);
1746 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001747 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001748 0x0dc,
1749 ERIAR_MASK_0100,
1750 0x0000,
1751 MagicPacket_v2,
1752 ERIAR_EXGMAC);
1753 break;
1754 default:
1755 tmp = ARRAY_SIZE(cfg);
1756 break;
1757 }
1758
1759 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001760 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001761 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001762 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001763 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001764 }
1765
Francois Romieu851e6022012-04-17 11:10:11 +02001766 switch (tp->mac_version) {
1767 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001768 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001769 if (wolopts)
1770 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001771 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001772 break;
1773 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001774 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001775 if (wolopts)
1776 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001777 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001778 break;
1779 }
1780
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001781 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001782}
1783
1784static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1785{
1786 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001787 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001788
1789 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001790
Francois Romieuda78dbf2012-01-26 14:18:23 +01001791 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001792
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001793 if (pm_runtime_active(d))
1794 __rtl8169_set_wol(tp, wol->wolopts);
1795 else
1796 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001797
1798 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001799
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001800 device_set_wakeup_enable(d, wol->wolopts);
françois romieuea809072010-11-08 13:23:58 +00001801
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001802 pm_runtime_put_noidle(d);
1803
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001804 return 0;
1805}
1806
Francois Romieu31bd2042011-04-26 18:58:59 +02001807static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1808{
Francois Romieu85bffe62011-04-27 08:22:39 +02001809 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812static void rtl8169_get_drvinfo(struct net_device *dev,
1813 struct ethtool_drvinfo *info)
1814{
1815 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001816 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Rick Jones68aad782011-11-07 13:29:27 +00001818 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1819 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1820 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001821 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001822 if (!IS_ERR_OR_NULL(rtl_fw))
1823 strlcpy(info->fw_version, rtl_fw->version,
1824 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827static int rtl8169_get_regs_len(struct net_device *dev)
1828{
1829 return R8169_REGS_SIZE;
1830}
1831
1832static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001833 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int ret = 0;
1837 u32 reg;
1838
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001839 reg = RTL_R32(tp, TBICSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1841 (duplex == DUPLEX_FULL)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001842 RTL_W32(tp, TBICSR, reg & ~(TBINwEnable | TBINwRestart));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else if (autoneg == AUTONEG_ENABLE)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001844 RTL_W32(tp, TBICSR, reg | TBINwEnable | TBINwRestart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001846 netif_warn(tp, link, dev,
1847 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ret = -EOPNOTSUPP;
1849 }
1850
1851 return ret;
1852}
1853
1854static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001855 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001858 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001859 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Hayes Wang716b50a2011-02-22 17:26:18 +08001861 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001864 int auto_nego;
1865
françois romieu4da19632011-01-03 15:07:55 +00001866 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001867 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1868 ADVERTISE_100HALF | ADVERTISE_100FULL);
1869
1870 if (adv & ADVERTISED_10baseT_Half)
1871 auto_nego |= ADVERTISE_10HALF;
1872 if (adv & ADVERTISED_10baseT_Full)
1873 auto_nego |= ADVERTISE_10FULL;
1874 if (adv & ADVERTISED_100baseT_Half)
1875 auto_nego |= ADVERTISE_100HALF;
1876 if (adv & ADVERTISED_100baseT_Full)
1877 auto_nego |= ADVERTISE_100FULL;
1878
françois romieu3577aa12009-05-19 10:46:48 +00001879 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1880
françois romieu4da19632011-01-03 15:07:55 +00001881 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001882 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1883
1884 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001885 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001886 if (adv & ADVERTISED_1000baseT_Half)
1887 giga_ctrl |= ADVERTISE_1000HALF;
1888 if (adv & ADVERTISED_1000baseT_Full)
1889 giga_ctrl |= ADVERTISE_1000FULL;
1890 } else if (adv & (ADVERTISED_1000baseT_Half |
1891 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001892 netif_info(tp, link, dev,
1893 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001894 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
françois romieu3577aa12009-05-19 10:46:48 +00001897 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001898
françois romieu4da19632011-01-03 15:07:55 +00001899 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1900 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001901 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001902 if (speed == SPEED_10)
1903 bmcr = 0;
1904 else if (speed == SPEED_100)
1905 bmcr = BMCR_SPEED100;
1906 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001907 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001908
1909 if (duplex == DUPLEX_FULL)
1910 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001911 }
1912
françois romieu4da19632011-01-03 15:07:55 +00001913 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001914
Francois Romieucecb5fd2011-04-01 10:21:07 +02001915 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1916 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001917 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001918 rtl_writephy(tp, 0x17, 0x2138);
1919 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001920 } else {
françois romieu4da19632011-01-03 15:07:55 +00001921 rtl_writephy(tp, 0x17, 0x2108);
1922 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001923 }
1924 }
1925
Oliver Neukum54405cd2011-01-06 21:55:13 +01001926 rc = 0;
1927out:
1928 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001932 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct rtl8169_private *tp = netdev_priv(dev);
1935 int ret;
1936
Oliver Neukum54405cd2011-01-06 21:55:13 +01001937 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001938 if (ret < 0)
1939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Francois Romieu4876cc12011-03-11 21:07:11 +01001941 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08001942 (advertising & ADVERTISED_1000baseT_Full) &&
1943 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001945 }
1946out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return ret;
1948}
1949
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001950static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1951 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Francois Romieud58d46b2011-05-03 16:38:29 +02001953 struct rtl8169_private *tp = netdev_priv(dev);
1954
Francois Romieu2b7b4312011-04-18 22:53:24 -07001955 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001956 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Francois Romieud58d46b2011-05-03 16:38:29 +02001958 if (dev->mtu > JUMBO_1K &&
1959 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1960 features &= ~NETIF_F_IP_CSUM;
1961
Michał Mirosław350fb322011-04-08 06:35:56 +00001962 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Francois Romieuda78dbf2012-01-26 14:18:23 +01001965static void __rtl8169_set_features(struct net_device *dev,
1966 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001969 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001971 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001972 if (features & NETIF_F_RXALL)
1973 rx_config |= (AcceptErr | AcceptRunt);
1974 else
1975 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001977 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001978
hayeswang929a0312014-09-16 11:40:47 +08001979 if (features & NETIF_F_RXCSUM)
1980 tp->cp_cmd |= RxChkSum;
1981 else
1982 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001983
hayeswang929a0312014-09-16 11:40:47 +08001984 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1985 tp->cp_cmd |= RxVlan;
1986 else
1987 tp->cp_cmd &= ~RxVlan;
1988
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001989 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) & ~(RxVlan | RxChkSum);
hayeswang929a0312014-09-16 11:40:47 +08001990
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001991 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1992 RTL_R16(tp, CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001993}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Francois Romieuda78dbf2012-01-26 14:18:23 +01001995static int rtl8169_set_features(struct net_device *dev,
1996 netdev_features_t features)
1997{
1998 struct rtl8169_private *tp = netdev_priv(dev);
1999
hayeswang929a0312014-09-16 11:40:47 +08002000 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2001
Francois Romieuda78dbf2012-01-26 14:18:23 +01002002 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002003 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002004 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002005 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 return 0;
2008}
2009
Francois Romieuda78dbf2012-01-26 14:18:23 +01002010
Kirill Smelkov810f4892012-11-10 21:11:02 +04002011static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002013 return (skb_vlan_tag_present(skb)) ?
2014 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Francois Romieu7a8fc772011-03-01 17:18:33 +01002017static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Francois Romieu7a8fc772011-03-01 17:18:33 +01002021 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002022 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002025static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2026 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002030 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002032 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002034 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002036 status = RTL_R32(tp, TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002037 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2038 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002040 cmd->base.speed = SPEED_1000;
2041 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2042
2043 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2044 supported);
2045 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2046 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002047
2048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002051static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2052 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002056 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2057
2058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002061static int rtl8169_get_link_ksettings(struct net_device *dev,
2062 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002065 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Francois Romieuda78dbf2012-01-26 14:18:23 +01002067 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002068 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002069 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Francois Romieuccdffb92008-07-26 14:26:06 +02002071 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002074static int rtl8169_set_link_ksettings(struct net_device *dev,
2075 const struct ethtool_link_ksettings *cmd)
2076{
2077 struct rtl8169_private *tp = netdev_priv(dev);
2078 int rc;
2079 u32 advertising;
2080
2081 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2082 cmd->link_modes.advertising))
2083 return -EINVAL;
2084
2085 del_timer_sync(&tp->timer);
2086
2087 rtl_lock_work(tp);
2088 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2089 cmd->base.duplex, advertising);
2090 rtl_unlock_work(tp);
2091
2092 return rc;
2093}
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2096 void *p)
2097{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002098 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002099 u32 __iomem *data = tp->mmio_addr;
2100 u32 *dw = p;
2101 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Francois Romieuda78dbf2012-01-26 14:18:23 +01002103 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002104 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2105 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002106 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002109static u32 rtl8169_get_msglevel(struct net_device *dev)
2110{
2111 struct rtl8169_private *tp = netdev_priv(dev);
2112
2113 return tp->msg_enable;
2114}
2115
2116static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2117{
2118 struct rtl8169_private *tp = netdev_priv(dev);
2119
2120 tp->msg_enable = value;
2121}
2122
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002123static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2124 "tx_packets",
2125 "rx_packets",
2126 "tx_errors",
2127 "rx_errors",
2128 "rx_missed",
2129 "align_errors",
2130 "tx_single_collisions",
2131 "tx_multi_collisions",
2132 "unicast",
2133 "broadcast",
2134 "multicast",
2135 "tx_aborted",
2136 "tx_underrun",
2137};
2138
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002139static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002140{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002141 switch (sset) {
2142 case ETH_SS_STATS:
2143 return ARRAY_SIZE(rtl8169_gstrings);
2144 default:
2145 return -EOPNOTSUPP;
2146 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002147}
2148
Corinna Vinschen42020322015-09-10 10:47:35 +02002149DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002150{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002151 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002152}
2153
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002154static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002155{
Corinna Vinschen42020322015-09-10 10:47:35 +02002156 dma_addr_t paddr = tp->counters_phys_addr;
2157 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002158
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002159 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
2160 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002161 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002162 RTL_W32(tp, CounterAddrLow, cmd);
2163 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02002164
Francois Romieua78e9362018-01-26 01:53:26 +01002165 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002166}
2167
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002168static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002169{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002170 /*
2171 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2172 * tally counters.
2173 */
2174 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2175 return true;
2176
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002177 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002178}
2179
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002180static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002181{
Ivan Vecera355423d2009-02-06 21:49:57 -08002182 /*
2183 * Some chips are unable to dump tally counters when the receiver
2184 * is disabled.
2185 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002186 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002187 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002188
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002189 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002190}
2191
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002192static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002193{
Corinna Vinschen42020322015-09-10 10:47:35 +02002194 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002195 bool ret = false;
2196
2197 /*
2198 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2199 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2200 * reset by a power cycle, while the counter values collected by the
2201 * driver are reset at every driver unload/load cycle.
2202 *
2203 * To make sure the HW values returned by @get_stats64 match the SW
2204 * values, we collect the initial values at first open(*) and use them
2205 * as offsets to normalize the values returned by @get_stats64.
2206 *
2207 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2208 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2209 * set at open time by rtl_hw_start.
2210 */
2211
2212 if (tp->tc_offset.inited)
2213 return true;
2214
2215 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002216 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002217 ret = true;
2218
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002219 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002220 ret = true;
2221
Corinna Vinschen42020322015-09-10 10:47:35 +02002222 tp->tc_offset.tx_errors = counters->tx_errors;
2223 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2224 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002225 tp->tc_offset.inited = true;
2226
2227 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002228}
2229
Ivan Vecera355423d2009-02-06 21:49:57 -08002230static void rtl8169_get_ethtool_stats(struct net_device *dev,
2231 struct ethtool_stats *stats, u64 *data)
2232{
2233 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01002234 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02002235 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002236
2237 ASSERT_RTNL();
2238
Chun-Hao Line0636232016-07-29 16:37:55 +08002239 pm_runtime_get_noresume(d);
2240
2241 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02002242 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08002243
2244 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002245
Corinna Vinschen42020322015-09-10 10:47:35 +02002246 data[0] = le64_to_cpu(counters->tx_packets);
2247 data[1] = le64_to_cpu(counters->rx_packets);
2248 data[2] = le64_to_cpu(counters->tx_errors);
2249 data[3] = le32_to_cpu(counters->rx_errors);
2250 data[4] = le16_to_cpu(counters->rx_missed);
2251 data[5] = le16_to_cpu(counters->align_errors);
2252 data[6] = le32_to_cpu(counters->tx_one_collision);
2253 data[7] = le32_to_cpu(counters->tx_multi_collision);
2254 data[8] = le64_to_cpu(counters->rx_unicast);
2255 data[9] = le64_to_cpu(counters->rx_broadcast);
2256 data[10] = le32_to_cpu(counters->rx_multicast);
2257 data[11] = le16_to_cpu(counters->tx_aborted);
2258 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002259}
2260
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002261static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2262{
2263 switch(stringset) {
2264 case ETH_SS_STATS:
2265 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2266 break;
2267 }
2268}
2269
Florian Fainellif0903ea2016-12-03 12:01:19 -08002270static int rtl8169_nway_reset(struct net_device *dev)
2271{
2272 struct rtl8169_private *tp = netdev_priv(dev);
2273
2274 return mii_nway_restart(&tp->mii);
2275}
2276
Francois Romieu50970832017-10-27 13:24:49 +03002277/*
2278 * Interrupt coalescing
2279 *
2280 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2281 * > 8169, 8168 and 810x line of chipsets
2282 *
2283 * 8169, 8168, and 8136(810x) serial chipsets support it.
2284 *
2285 * > 2 - the Tx timer unit at gigabit speed
2286 *
2287 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2288 * (0xe0) bit 1 and bit 0.
2289 *
2290 * For 8169
2291 * bit[1:0] \ speed 1000M 100M 10M
2292 * 0 0 320ns 2.56us 40.96us
2293 * 0 1 2.56us 20.48us 327.7us
2294 * 1 0 5.12us 40.96us 655.4us
2295 * 1 1 10.24us 81.92us 1.31ms
2296 *
2297 * For the other
2298 * bit[1:0] \ speed 1000M 100M 10M
2299 * 0 0 5us 2.56us 40.96us
2300 * 0 1 40us 20.48us 327.7us
2301 * 1 0 80us 40.96us 655.4us
2302 * 1 1 160us 81.92us 1.31ms
2303 */
2304
2305/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2306struct rtl_coalesce_scale {
2307 /* Rx / Tx */
2308 u32 nsecs[2];
2309};
2310
2311/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2312struct rtl_coalesce_info {
2313 u32 speed;
2314 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2315};
2316
2317/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2318#define rxtx_x1822(r, t) { \
2319 {{(r), (t)}}, \
2320 {{(r)*8, (t)*8}}, \
2321 {{(r)*8*2, (t)*8*2}}, \
2322 {{(r)*8*2*2, (t)*8*2*2}}, \
2323}
2324static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2325 /* speed delays: rx00 tx00 */
2326 { SPEED_10, rxtx_x1822(40960, 40960) },
2327 { SPEED_100, rxtx_x1822( 2560, 2560) },
2328 { SPEED_1000, rxtx_x1822( 320, 320) },
2329 { 0 },
2330};
2331
2332static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2333 /* speed delays: rx00 tx00 */
2334 { SPEED_10, rxtx_x1822(40960, 40960) },
2335 { SPEED_100, rxtx_x1822( 2560, 2560) },
2336 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2337 { 0 },
2338};
2339#undef rxtx_x1822
2340
2341/* get rx/tx scale vector corresponding to current speed */
2342static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2343{
2344 struct rtl8169_private *tp = netdev_priv(dev);
2345 struct ethtool_link_ksettings ecmd;
2346 const struct rtl_coalesce_info *ci;
2347 int rc;
2348
2349 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2350 if (rc < 0)
2351 return ERR_PTR(rc);
2352
2353 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2354 if (ecmd.base.speed == ci->speed) {
2355 return ci;
2356 }
2357 }
2358
2359 return ERR_PTR(-ELNRNG);
2360}
2361
2362static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2363{
2364 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002365 const struct rtl_coalesce_info *ci;
2366 const struct rtl_coalesce_scale *scale;
2367 struct {
2368 u32 *max_frames;
2369 u32 *usecs;
2370 } coal_settings [] = {
2371 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2372 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2373 }, *p = coal_settings;
2374 int i;
2375 u16 w;
2376
2377 memset(ec, 0, sizeof(*ec));
2378
2379 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2380 ci = rtl_coalesce_info(dev);
2381 if (IS_ERR(ci))
2382 return PTR_ERR(ci);
2383
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002384 scale = &ci->scalev[RTL_R16(tp, CPlusCmd) & 3];
Francois Romieu50970832017-10-27 13:24:49 +03002385
2386 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002387 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03002388 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2389 w >>= RTL_COALESCE_SHIFT;
2390 *p->usecs = w & RTL_COALESCE_MASK;
2391 }
2392
2393 for (i = 0; i < 2; i++) {
2394 p = coal_settings + i;
2395 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2396
2397 /*
2398 * ethtool_coalesce says it is illegal to set both usecs and
2399 * max_frames to 0.
2400 */
2401 if (!*p->usecs && !*p->max_frames)
2402 *p->max_frames = 1;
2403 }
2404
2405 return 0;
2406}
2407
2408/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2409static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2410 struct net_device *dev, u32 nsec, u16 *cp01)
2411{
2412 const struct rtl_coalesce_info *ci;
2413 u16 i;
2414
2415 ci = rtl_coalesce_info(dev);
2416 if (IS_ERR(ci))
2417 return ERR_CAST(ci);
2418
2419 for (i = 0; i < 4; i++) {
2420 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2421 ci->scalev[i].nsecs[1]);
2422 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2423 *cp01 = i;
2424 return &ci->scalev[i];
2425 }
2426 }
2427
2428 return ERR_PTR(-EINVAL);
2429}
2430
2431static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2432{
2433 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03002434 const struct rtl_coalesce_scale *scale;
2435 struct {
2436 u32 frames;
2437 u32 usecs;
2438 } coal_settings [] = {
2439 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2440 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2441 }, *p = coal_settings;
2442 u16 w = 0, cp01;
2443 int i;
2444
2445 scale = rtl_coalesce_choose_scale(dev,
2446 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2447 if (IS_ERR(scale))
2448 return PTR_ERR(scale);
2449
2450 for (i = 0; i < 2; i++, p++) {
2451 u32 units;
2452
2453 /*
2454 * accept max_frames=1 we returned in rtl_get_coalesce.
2455 * accept it not only when usecs=0 because of e.g. the following scenario:
2456 *
2457 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2458 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2459 * - then user does `ethtool -C eth0 rx-usecs 100`
2460 *
2461 * since ethtool sends to kernel whole ethtool_coalesce
2462 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2463 * we'll reject it below in `frames % 4 != 0`.
2464 */
2465 if (p->frames == 1) {
2466 p->frames = 0;
2467 }
2468
2469 units = p->usecs * 1000 / scale->nsecs[i];
2470 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2471 return -EINVAL;
2472
2473 w <<= RTL_COALESCE_SHIFT;
2474 w |= units;
2475 w <<= RTL_COALESCE_SHIFT;
2476 w |= p->frames >> 2;
2477 }
2478
2479 rtl_lock_work(tp);
2480
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002481 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002482
2483 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002484 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2485 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002486
2487 rtl_unlock_work(tp);
2488
2489 return 0;
2490}
2491
Jeff Garzik7282d492006-09-13 14:30:00 -04002492static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 .get_drvinfo = rtl8169_get_drvinfo,
2494 .get_regs_len = rtl8169_get_regs_len,
2495 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002496 .get_coalesce = rtl_get_coalesce,
2497 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002498 .get_msglevel = rtl8169_get_msglevel,
2499 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002501 .get_wol = rtl8169_get_wol,
2502 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002503 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002504 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002505 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002506 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002507 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002508 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002509 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510};
2511
Francois Romieu07d3f512007-02-21 22:40:46 +01002512static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002513 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Francois Romieu0e485152007-02-20 00:00:26 +01002515 /*
2516 * The driver currently handles the 8168Bf and the 8168Be identically
2517 * but they can be identified more specifically through the test below
2518 * if needed:
2519 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002520 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002521 *
2522 * Same thing for the 8101Eb and the 8101Ec:
2523 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002524 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002525 */
Francois Romieu37441002011-06-17 22:58:54 +02002526 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002528 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 int mac_version;
2530 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002531 /* 8168EP family. */
2532 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2533 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2534 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2535
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002536 /* 8168H family. */
2537 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2538 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2539
Hayes Wangc5583862012-07-02 17:23:22 +08002540 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002541 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002542 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002543 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2544 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2545
Hayes Wangc2218922011-09-06 16:55:18 +08002546 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002547 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002548 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2549 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2550
hayeswang01dc7fe2011-03-21 01:50:28 +00002551 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002552 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002553 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2554 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2555 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2556
Francois Romieu5b538df2008-07-20 16:22:45 +02002557 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002558 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2559 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002560 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002561
françois romieue6de30d2011-01-03 15:08:37 +00002562 /* 8168DP family. */
2563 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2564 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002565 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002566
Francois Romieuef808d52008-06-29 13:10:54 +02002567 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002568 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002569 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002570 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002571 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002572 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2573 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002574 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002575 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002576 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002577
2578 /* 8168B family. */
2579 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2580 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2581 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2582 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2583
2584 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002585 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2586 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002587 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002588 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002589 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2590 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2591 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002592 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2593 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2594 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2595 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2596 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2597 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002598 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002599 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002600 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002601 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2602 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002603 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2604 /* FIXME: where did these entries come from ? -- FR */
2605 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2606 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2607
2608 /* 8110 family. */
2609 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2610 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2611 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2612 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2613 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2614 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2615
Jean Delvaref21b75e2009-05-26 20:54:48 -07002616 /* Catch-all */
2617 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002618 };
2619 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 u32 reg;
2621
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002622 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002623 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 p++;
2625 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002626
2627 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2628 netif_notice(tp, probe, dev,
2629 "unknown MAC, using family default\n");
2630 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002631 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2632 tp->mac_version = tp->mii.supports_gmii ?
2633 RTL_GIGA_MAC_VER_42 :
2634 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002635 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2636 tp->mac_version = tp->mii.supports_gmii ?
2637 RTL_GIGA_MAC_VER_45 :
2638 RTL_GIGA_MAC_VER_47;
2639 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2640 tp->mac_version = tp->mii.supports_gmii ?
2641 RTL_GIGA_MAC_VER_46 :
2642 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2647{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002648 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649}
2650
Francois Romieu867763c2007-08-17 18:21:58 +02002651struct phy_reg {
2652 u16 reg;
2653 u16 val;
2654};
2655
françois romieu4da19632011-01-03 15:07:55 +00002656static void rtl_writephy_batch(struct rtl8169_private *tp,
2657 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002658{
2659 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002660 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002661 regs++;
2662 }
2663}
2664
françois romieubca03d52011-01-03 15:07:31 +00002665#define PHY_READ 0x00000000
2666#define PHY_DATA_OR 0x10000000
2667#define PHY_DATA_AND 0x20000000
2668#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002669#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002670#define PHY_CLEAR_READCOUNT 0x70000000
2671#define PHY_WRITE 0x80000000
2672#define PHY_READCOUNT_EQ_SKIP 0x90000000
2673#define PHY_COMP_EQ_SKIPN 0xa0000000
2674#define PHY_COMP_NEQ_SKIPN 0xb0000000
2675#define PHY_WRITE_PREVIOUS 0xc0000000
2676#define PHY_SKIPN 0xd0000000
2677#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002678
Hayes Wang960aee62011-06-18 11:37:48 +02002679struct fw_info {
2680 u32 magic;
2681 char version[RTL_VER_SIZE];
2682 __le32 fw_start;
2683 __le32 fw_len;
2684 u8 chksum;
2685} __packed;
2686
Francois Romieu1c361ef2011-06-17 17:16:24 +02002687#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2688
2689static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002690{
Francois Romieub6ffd972011-06-17 17:00:05 +02002691 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002692 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002693 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2694 char *version = rtl_fw->version;
2695 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002696
Francois Romieu1c361ef2011-06-17 17:16:24 +02002697 if (fw->size < FW_OPCODE_SIZE)
2698 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002699
2700 if (!fw_info->magic) {
2701 size_t i, size, start;
2702 u8 checksum = 0;
2703
2704 if (fw->size < sizeof(*fw_info))
2705 goto out;
2706
2707 for (i = 0; i < fw->size; i++)
2708 checksum += fw->data[i];
2709 if (checksum != 0)
2710 goto out;
2711
2712 start = le32_to_cpu(fw_info->fw_start);
2713 if (start > fw->size)
2714 goto out;
2715
2716 size = le32_to_cpu(fw_info->fw_len);
2717 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2718 goto out;
2719
2720 memcpy(version, fw_info->version, RTL_VER_SIZE);
2721
2722 pa->code = (__le32 *)(fw->data + start);
2723 pa->size = size;
2724 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002725 if (fw->size % FW_OPCODE_SIZE)
2726 goto out;
2727
2728 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2729
2730 pa->code = (__le32 *)fw->data;
2731 pa->size = fw->size / FW_OPCODE_SIZE;
2732 }
2733 version[RTL_VER_SIZE - 1] = 0;
2734
2735 rc = true;
2736out:
2737 return rc;
2738}
2739
Francois Romieufd112f22011-06-18 00:10:29 +02002740static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2741 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002742{
Francois Romieufd112f22011-06-18 00:10:29 +02002743 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002744 size_t index;
2745
Francois Romieu1c361ef2011-06-17 17:16:24 +02002746 for (index = 0; index < pa->size; index++) {
2747 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002748 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002749
hayeswang42b82dc2011-01-10 02:07:25 +00002750 switch(action & 0xf0000000) {
2751 case PHY_READ:
2752 case PHY_DATA_OR:
2753 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002754 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002755 case PHY_CLEAR_READCOUNT:
2756 case PHY_WRITE:
2757 case PHY_WRITE_PREVIOUS:
2758 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002759 break;
2760
hayeswang42b82dc2011-01-10 02:07:25 +00002761 case PHY_BJMPN:
2762 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002763 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002764 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002765 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002766 }
2767 break;
2768 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002769 if (index + 2 >= pa->size) {
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_COMP_EQ_SKIPN:
2776 case PHY_COMP_NEQ_SKIPN:
2777 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002778 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002779 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002780 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002781 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002782 }
2783 break;
2784
hayeswang42b82dc2011-01-10 02:07:25 +00002785 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002786 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002787 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002788 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002789 }
2790 }
Francois Romieufd112f22011-06-18 00:10:29 +02002791 rc = true;
2792out:
2793 return rc;
2794}
françois romieubca03d52011-01-03 15:07:31 +00002795
Francois Romieufd112f22011-06-18 00:10:29 +02002796static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2797{
2798 struct net_device *dev = tp->dev;
2799 int rc = -EINVAL;
2800
2801 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002802 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002803 goto out;
2804 }
2805
2806 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2807 rc = 0;
2808out:
2809 return rc;
2810}
2811
2812static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2813{
2814 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002815 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002816 u32 predata, count;
2817 size_t index;
2818
2819 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002820 org.write = ops->write;
2821 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002822
Francois Romieu1c361ef2011-06-17 17:16:24 +02002823 for (index = 0; index < pa->size; ) {
2824 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002825 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002826 u32 regno = (action & 0x0fff0000) >> 16;
2827
2828 if (!action)
2829 break;
françois romieubca03d52011-01-03 15:07:31 +00002830
2831 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002832 case PHY_READ:
2833 predata = rtl_readphy(tp, regno);
2834 count++;
2835 index++;
françois romieubca03d52011-01-03 15:07:31 +00002836 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002837 case PHY_DATA_OR:
2838 predata |= data;
2839 index++;
2840 break;
2841 case PHY_DATA_AND:
2842 predata &= data;
2843 index++;
2844 break;
2845 case PHY_BJMPN:
2846 index -= regno;
2847 break;
hayeswangeee37862013-04-01 22:23:38 +00002848 case PHY_MDIO_CHG:
2849 if (data == 0) {
2850 ops->write = org.write;
2851 ops->read = org.read;
2852 } else if (data == 1) {
2853 ops->write = mac_mcu_write;
2854 ops->read = mac_mcu_read;
2855 }
2856
hayeswang42b82dc2011-01-10 02:07:25 +00002857 index++;
2858 break;
2859 case PHY_CLEAR_READCOUNT:
2860 count = 0;
2861 index++;
2862 break;
2863 case PHY_WRITE:
2864 rtl_writephy(tp, regno, data);
2865 index++;
2866 break;
2867 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002868 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002869 break;
2870 case PHY_COMP_EQ_SKIPN:
2871 if (predata == data)
2872 index += regno;
2873 index++;
2874 break;
2875 case PHY_COMP_NEQ_SKIPN:
2876 if (predata != data)
2877 index += regno;
2878 index++;
2879 break;
2880 case PHY_WRITE_PREVIOUS:
2881 rtl_writephy(tp, regno, predata);
2882 index++;
2883 break;
2884 case PHY_SKIPN:
2885 index += regno + 1;
2886 break;
2887 case PHY_DELAY_MS:
2888 mdelay(data);
2889 index++;
2890 break;
2891
françois romieubca03d52011-01-03 15:07:31 +00002892 default:
2893 BUG();
2894 }
2895 }
hayeswangeee37862013-04-01 22:23:38 +00002896
2897 ops->write = org.write;
2898 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002899}
2900
françois romieuf1e02ed2011-01-13 13:07:53 +00002901static void rtl_release_firmware(struct rtl8169_private *tp)
2902{
Francois Romieub6ffd972011-06-17 17:00:05 +02002903 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2904 release_firmware(tp->rtl_fw->fw);
2905 kfree(tp->rtl_fw);
2906 }
2907 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002908}
2909
François Romieu953a12c2011-04-24 17:38:48 +02002910static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002911{
Francois Romieub6ffd972011-06-17 17:00:05 +02002912 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002913
2914 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002915 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002916 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002917}
2918
2919static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2920{
2921 if (rtl_readphy(tp, reg) != val)
2922 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2923 else
2924 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002925}
2926
françois romieu4da19632011-01-03 15:07:55 +00002927static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002929 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002930 { 0x1f, 0x0001 },
2931 { 0x06, 0x006e },
2932 { 0x08, 0x0708 },
2933 { 0x15, 0x4000 },
2934 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
françois romieu0b9b5712009-08-10 19:44:56 +00002936 { 0x1f, 0x0001 },
2937 { 0x03, 0x00a1 },
2938 { 0x02, 0x0008 },
2939 { 0x01, 0x0120 },
2940 { 0x00, 0x1000 },
2941 { 0x04, 0x0800 },
2942 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
françois romieu0b9b5712009-08-10 19:44:56 +00002944 { 0x03, 0xff41 },
2945 { 0x02, 0xdf60 },
2946 { 0x01, 0x0140 },
2947 { 0x00, 0x0077 },
2948 { 0x04, 0x7800 },
2949 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
françois romieu0b9b5712009-08-10 19:44:56 +00002951 { 0x03, 0x802f },
2952 { 0x02, 0x4f02 },
2953 { 0x01, 0x0409 },
2954 { 0x00, 0xf0f9 },
2955 { 0x04, 0x9800 },
2956 { 0x04, 0x9000 },
2957
2958 { 0x03, 0xdf01 },
2959 { 0x02, 0xdf20 },
2960 { 0x01, 0xff95 },
2961 { 0x00, 0xba00 },
2962 { 0x04, 0xa800 },
2963 { 0x04, 0xa000 },
2964
2965 { 0x03, 0xff41 },
2966 { 0x02, 0xdf20 },
2967 { 0x01, 0x0140 },
2968 { 0x00, 0x00bb },
2969 { 0x04, 0xb800 },
2970 { 0x04, 0xb000 },
2971
2972 { 0x03, 0xdf41 },
2973 { 0x02, 0xdc60 },
2974 { 0x01, 0x6340 },
2975 { 0x00, 0x007d },
2976 { 0x04, 0xd800 },
2977 { 0x04, 0xd000 },
2978
2979 { 0x03, 0xdf01 },
2980 { 0x02, 0xdf20 },
2981 { 0x01, 0x100a },
2982 { 0x00, 0xa0ff },
2983 { 0x04, 0xf800 },
2984 { 0x04, 0xf000 },
2985
2986 { 0x1f, 0x0000 },
2987 { 0x0b, 0x0000 },
2988 { 0x00, 0x9200 }
2989 };
2990
françois romieu4da19632011-01-03 15:07:55 +00002991 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
françois romieu4da19632011-01-03 15:07:55 +00002994static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002995{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002996 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002997 { 0x1f, 0x0002 },
2998 { 0x01, 0x90d0 },
2999 { 0x1f, 0x0000 }
3000 };
3001
françois romieu4da19632011-01-03 15:07:55 +00003002 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003003}
3004
françois romieu4da19632011-01-03 15:07:55 +00003005static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003006{
3007 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003008
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003009 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3010 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003011 return;
3012
françois romieu4da19632011-01-03 15:07:55 +00003013 rtl_writephy(tp, 0x1f, 0x0001);
3014 rtl_writephy(tp, 0x10, 0xf01b);
3015 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003016}
3017
françois romieu4da19632011-01-03 15:07:55 +00003018static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003019{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003020 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003021 { 0x1f, 0x0001 },
3022 { 0x04, 0x0000 },
3023 { 0x03, 0x00a1 },
3024 { 0x02, 0x0008 },
3025 { 0x01, 0x0120 },
3026 { 0x00, 0x1000 },
3027 { 0x04, 0x0800 },
3028 { 0x04, 0x9000 },
3029 { 0x03, 0x802f },
3030 { 0x02, 0x4f02 },
3031 { 0x01, 0x0409 },
3032 { 0x00, 0xf099 },
3033 { 0x04, 0x9800 },
3034 { 0x04, 0xa000 },
3035 { 0x03, 0xdf01 },
3036 { 0x02, 0xdf20 },
3037 { 0x01, 0xff95 },
3038 { 0x00, 0xba00 },
3039 { 0x04, 0xa800 },
3040 { 0x04, 0xf000 },
3041 { 0x03, 0xdf01 },
3042 { 0x02, 0xdf20 },
3043 { 0x01, 0x101a },
3044 { 0x00, 0xa0ff },
3045 { 0x04, 0xf800 },
3046 { 0x04, 0x0000 },
3047 { 0x1f, 0x0000 },
3048
3049 { 0x1f, 0x0001 },
3050 { 0x10, 0xf41b },
3051 { 0x14, 0xfb54 },
3052 { 0x18, 0xf5c7 },
3053 { 0x1f, 0x0000 },
3054
3055 { 0x1f, 0x0001 },
3056 { 0x17, 0x0cc0 },
3057 { 0x1f, 0x0000 }
3058 };
3059
françois romieu4da19632011-01-03 15:07:55 +00003060 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003061
françois romieu4da19632011-01-03 15:07:55 +00003062 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003063}
3064
françois romieu4da19632011-01-03 15:07:55 +00003065static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003066{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003067 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003068 { 0x1f, 0x0001 },
3069 { 0x04, 0x0000 },
3070 { 0x03, 0x00a1 },
3071 { 0x02, 0x0008 },
3072 { 0x01, 0x0120 },
3073 { 0x00, 0x1000 },
3074 { 0x04, 0x0800 },
3075 { 0x04, 0x9000 },
3076 { 0x03, 0x802f },
3077 { 0x02, 0x4f02 },
3078 { 0x01, 0x0409 },
3079 { 0x00, 0xf099 },
3080 { 0x04, 0x9800 },
3081 { 0x04, 0xa000 },
3082 { 0x03, 0xdf01 },
3083 { 0x02, 0xdf20 },
3084 { 0x01, 0xff95 },
3085 { 0x00, 0xba00 },
3086 { 0x04, 0xa800 },
3087 { 0x04, 0xf000 },
3088 { 0x03, 0xdf01 },
3089 { 0x02, 0xdf20 },
3090 { 0x01, 0x101a },
3091 { 0x00, 0xa0ff },
3092 { 0x04, 0xf800 },
3093 { 0x04, 0x0000 },
3094 { 0x1f, 0x0000 },
3095
3096 { 0x1f, 0x0001 },
3097 { 0x0b, 0x8480 },
3098 { 0x1f, 0x0000 },
3099
3100 { 0x1f, 0x0001 },
3101 { 0x18, 0x67c7 },
3102 { 0x04, 0x2000 },
3103 { 0x03, 0x002f },
3104 { 0x02, 0x4360 },
3105 { 0x01, 0x0109 },
3106 { 0x00, 0x3022 },
3107 { 0x04, 0x2800 },
3108 { 0x1f, 0x0000 },
3109
3110 { 0x1f, 0x0001 },
3111 { 0x17, 0x0cc0 },
3112 { 0x1f, 0x0000 }
3113 };
3114
françois romieu4da19632011-01-03 15:07:55 +00003115 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003116}
3117
françois romieu4da19632011-01-03 15:07:55 +00003118static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003119{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003120 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003121 { 0x10, 0xf41b },
3122 { 0x1f, 0x0000 }
3123 };
3124
françois romieu4da19632011-01-03 15:07:55 +00003125 rtl_writephy(tp, 0x1f, 0x0001);
3126 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003127
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003129}
3130
françois romieu4da19632011-01-03 15:07:55 +00003131static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003132{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003133 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003134 { 0x1f, 0x0001 },
3135 { 0x10, 0xf41b },
3136 { 0x1f, 0x0000 }
3137 };
3138
françois romieu4da19632011-01-03 15:07:55 +00003139 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003140}
3141
françois romieu4da19632011-01-03 15:07:55 +00003142static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003143{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003144 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003145 { 0x1f, 0x0000 },
3146 { 0x1d, 0x0f00 },
3147 { 0x1f, 0x0002 },
3148 { 0x0c, 0x1ec8 },
3149 { 0x1f, 0x0000 }
3150 };
3151
françois romieu4da19632011-01-03 15:07:55 +00003152 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003153}
3154
françois romieu4da19632011-01-03 15:07:55 +00003155static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003156{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003157 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003158 { 0x1f, 0x0001 },
3159 { 0x1d, 0x3d98 },
3160 { 0x1f, 0x0000 }
3161 };
3162
françois romieu4da19632011-01-03 15:07:55 +00003163 rtl_writephy(tp, 0x1f, 0x0000);
3164 rtl_patchphy(tp, 0x14, 1 << 5);
3165 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003166
françois romieu4da19632011-01-03 15:07:55 +00003167 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003168}
3169
françois romieu4da19632011-01-03 15:07:55 +00003170static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003171{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003172 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003173 { 0x1f, 0x0001 },
3174 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003175 { 0x1f, 0x0002 },
3176 { 0x00, 0x88d4 },
3177 { 0x01, 0x82b1 },
3178 { 0x03, 0x7002 },
3179 { 0x08, 0x9e30 },
3180 { 0x09, 0x01f0 },
3181 { 0x0a, 0x5500 },
3182 { 0x0c, 0x00c8 },
3183 { 0x1f, 0x0003 },
3184 { 0x12, 0xc096 },
3185 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003186 { 0x1f, 0x0000 },
3187 { 0x1f, 0x0000 },
3188 { 0x09, 0x2000 },
3189 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003190 };
3191
françois romieu4da19632011-01-03 15:07:55 +00003192 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003193
françois romieu4da19632011-01-03 15:07:55 +00003194 rtl_patchphy(tp, 0x14, 1 << 5);
3195 rtl_patchphy(tp, 0x0d, 1 << 5);
3196 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003197}
3198
françois romieu4da19632011-01-03 15:07:55 +00003199static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003200{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003201 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003202 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003203 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003204 { 0x03, 0x802f },
3205 { 0x02, 0x4f02 },
3206 { 0x01, 0x0409 },
3207 { 0x00, 0xf099 },
3208 { 0x04, 0x9800 },
3209 { 0x04, 0x9000 },
3210 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003211 { 0x1f, 0x0002 },
3212 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003213 { 0x06, 0x0761 },
3214 { 0x1f, 0x0003 },
3215 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003216 { 0x1f, 0x0000 }
3217 };
3218
françois romieu4da19632011-01-03 15:07:55 +00003219 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003220
françois romieu4da19632011-01-03 15:07:55 +00003221 rtl_patchphy(tp, 0x16, 1 << 0);
3222 rtl_patchphy(tp, 0x14, 1 << 5);
3223 rtl_patchphy(tp, 0x0d, 1 << 5);
3224 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003225}
3226
françois romieu4da19632011-01-03 15:07:55 +00003227static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003228{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003229 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003230 { 0x1f, 0x0001 },
3231 { 0x12, 0x2300 },
3232 { 0x1d, 0x3d98 },
3233 { 0x1f, 0x0002 },
3234 { 0x0c, 0x7eb8 },
3235 { 0x06, 0x5461 },
3236 { 0x1f, 0x0003 },
3237 { 0x16, 0x0f0a },
3238 { 0x1f, 0x0000 }
3239 };
3240
françois romieu4da19632011-01-03 15:07:55 +00003241 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003242
françois romieu4da19632011-01-03 15:07:55 +00003243 rtl_patchphy(tp, 0x16, 1 << 0);
3244 rtl_patchphy(tp, 0x14, 1 << 5);
3245 rtl_patchphy(tp, 0x0d, 1 << 5);
3246 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003247}
3248
françois romieu4da19632011-01-03 15:07:55 +00003249static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003250{
françois romieu4da19632011-01-03 15:07:55 +00003251 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003252}
3253
françois romieubca03d52011-01-03 15:07:31 +00003254static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003255{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003256 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003257 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003258 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003259 { 0x06, 0x4064 },
3260 { 0x07, 0x2863 },
3261 { 0x08, 0x059c },
3262 { 0x09, 0x26b4 },
3263 { 0x0a, 0x6a19 },
3264 { 0x0b, 0xdcc8 },
3265 { 0x10, 0xf06d },
3266 { 0x14, 0x7f68 },
3267 { 0x18, 0x7fd9 },
3268 { 0x1c, 0xf0ff },
3269 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003270 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003271 { 0x12, 0xf49f },
3272 { 0x13, 0x070b },
3273 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003274 { 0x14, 0x94c0 },
3275
3276 /*
3277 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003278 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003279 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003280 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003281 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003282 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003283 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003284 { 0x06, 0x5561 },
3285
3286 /*
3287 * Can not link to 1Gbps with bad cable
3288 * Decrease SNR threshold form 21.07dB to 19.04dB
3289 */
3290 { 0x1f, 0x0001 },
3291 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003292
3293 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003294 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003295 };
3296
françois romieu4da19632011-01-03 15:07:55 +00003297 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003298
françois romieubca03d52011-01-03 15:07:31 +00003299 /*
3300 * Rx Error Issue
3301 * Fine Tune Switching regulator parameter
3302 */
françois romieu4da19632011-01-03 15:07:55 +00003303 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003304 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3305 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003306
Francois Romieufdf6fc02012-07-06 22:40:38 +02003307 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003308 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003309 { 0x1f, 0x0002 },
3310 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003311 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003312 { 0x05, 0x8330 },
3313 { 0x06, 0x669a },
3314 { 0x1f, 0x0002 }
3315 };
3316 int val;
3317
françois romieu4da19632011-01-03 15:07:55 +00003318 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003319
françois romieu4da19632011-01-03 15:07:55 +00003320 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003321
3322 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003323 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003324 0x0065, 0x0066, 0x0067, 0x0068,
3325 0x0069, 0x006a, 0x006b, 0x006c
3326 };
3327 int i;
3328
françois romieu4da19632011-01-03 15:07:55 +00003329 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003330
3331 val &= 0xff00;
3332 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003333 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003334 }
3335 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003336 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003337 { 0x1f, 0x0002 },
3338 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003339 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003340 { 0x05, 0x8330 },
3341 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003342 };
3343
françois romieu4da19632011-01-03 15:07:55 +00003344 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003345 }
3346
françois romieubca03d52011-01-03 15:07:31 +00003347 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003348 rtl_writephy(tp, 0x1f, 0x0002);
3349 rtl_patchphy(tp, 0x0d, 0x0300);
3350 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003351
françois romieubca03d52011-01-03 15:07:31 +00003352 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003353 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003354 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3355 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003356
françois romieu4da19632011-01-03 15:07:55 +00003357 rtl_writephy(tp, 0x1f, 0x0005);
3358 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003359
3360 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003361
françois romieu4da19632011-01-03 15:07:55 +00003362 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003363}
3364
françois romieubca03d52011-01-03 15:07:31 +00003365static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003366{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003367 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003368 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003369 { 0x1f, 0x0001 },
3370 { 0x06, 0x4064 },
3371 { 0x07, 0x2863 },
3372 { 0x08, 0x059c },
3373 { 0x09, 0x26b4 },
3374 { 0x0a, 0x6a19 },
3375 { 0x0b, 0xdcc8 },
3376 { 0x10, 0xf06d },
3377 { 0x14, 0x7f68 },
3378 { 0x18, 0x7fd9 },
3379 { 0x1c, 0xf0ff },
3380 { 0x1d, 0x3d9c },
3381 { 0x1f, 0x0003 },
3382 { 0x12, 0xf49f },
3383 { 0x13, 0x070b },
3384 { 0x1a, 0x05ad },
3385 { 0x14, 0x94c0 },
3386
françois romieubca03d52011-01-03 15:07:31 +00003387 /*
3388 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003389 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003390 */
françois romieudaf9df62009-10-07 12:44:20 +00003391 { 0x1f, 0x0002 },
3392 { 0x06, 0x5561 },
3393 { 0x1f, 0x0005 },
3394 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003395 { 0x06, 0x5561 },
3396
3397 /*
3398 * Can not link to 1Gbps with bad cable
3399 * Decrease SNR threshold form 21.07dB to 19.04dB
3400 */
3401 { 0x1f, 0x0001 },
3402 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003403
3404 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003405 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003406 };
3407
françois romieu4da19632011-01-03 15:07:55 +00003408 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003409
Francois Romieufdf6fc02012-07-06 22:40:38 +02003410 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003411 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003412 { 0x1f, 0x0002 },
3413 { 0x05, 0x669a },
3414 { 0x1f, 0x0005 },
3415 { 0x05, 0x8330 },
3416 { 0x06, 0x669a },
3417
3418 { 0x1f, 0x0002 }
3419 };
3420 int val;
3421
françois romieu4da19632011-01-03 15:07:55 +00003422 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003423
françois romieu4da19632011-01-03 15:07:55 +00003424 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003425 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003426 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003427 0x0065, 0x0066, 0x0067, 0x0068,
3428 0x0069, 0x006a, 0x006b, 0x006c
3429 };
3430 int i;
3431
françois romieu4da19632011-01-03 15:07:55 +00003432 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003433
3434 val &= 0xff00;
3435 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003436 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003437 }
3438 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003439 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003440 { 0x1f, 0x0002 },
3441 { 0x05, 0x2642 },
3442 { 0x1f, 0x0005 },
3443 { 0x05, 0x8330 },
3444 { 0x06, 0x2642 }
3445 };
3446
françois romieu4da19632011-01-03 15:07:55 +00003447 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003448 }
3449
françois romieubca03d52011-01-03 15:07:31 +00003450 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003451 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003452 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3453 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003454
françois romieubca03d52011-01-03 15:07:31 +00003455 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003456 rtl_writephy(tp, 0x1f, 0x0002);
3457 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003458
françois romieu4da19632011-01-03 15:07:55 +00003459 rtl_writephy(tp, 0x1f, 0x0005);
3460 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003461
3462 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003463
françois romieu4da19632011-01-03 15:07:55 +00003464 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003465}
3466
françois romieu4da19632011-01-03 15:07:55 +00003467static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003468{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003469 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003470 { 0x1f, 0x0002 },
3471 { 0x10, 0x0008 },
3472 { 0x0d, 0x006c },
3473
3474 { 0x1f, 0x0000 },
3475 { 0x0d, 0xf880 },
3476
3477 { 0x1f, 0x0001 },
3478 { 0x17, 0x0cc0 },
3479
3480 { 0x1f, 0x0001 },
3481 { 0x0b, 0xa4d8 },
3482 { 0x09, 0x281c },
3483 { 0x07, 0x2883 },
3484 { 0x0a, 0x6b35 },
3485 { 0x1d, 0x3da4 },
3486 { 0x1c, 0xeffd },
3487 { 0x14, 0x7f52 },
3488 { 0x18, 0x7fc6 },
3489 { 0x08, 0x0601 },
3490 { 0x06, 0x4063 },
3491 { 0x10, 0xf074 },
3492 { 0x1f, 0x0003 },
3493 { 0x13, 0x0789 },
3494 { 0x12, 0xf4bd },
3495 { 0x1a, 0x04fd },
3496 { 0x14, 0x84b0 },
3497 { 0x1f, 0x0000 },
3498 { 0x00, 0x9200 },
3499
3500 { 0x1f, 0x0005 },
3501 { 0x01, 0x0340 },
3502 { 0x1f, 0x0001 },
3503 { 0x04, 0x4000 },
3504 { 0x03, 0x1d21 },
3505 { 0x02, 0x0c32 },
3506 { 0x01, 0x0200 },
3507 { 0x00, 0x5554 },
3508 { 0x04, 0x4800 },
3509 { 0x04, 0x4000 },
3510 { 0x04, 0xf000 },
3511 { 0x03, 0xdf01 },
3512 { 0x02, 0xdf20 },
3513 { 0x01, 0x101a },
3514 { 0x00, 0xa0ff },
3515 { 0x04, 0xf800 },
3516 { 0x04, 0xf000 },
3517 { 0x1f, 0x0000 },
3518
3519 { 0x1f, 0x0007 },
3520 { 0x1e, 0x0023 },
3521 { 0x16, 0x0000 },
3522 { 0x1f, 0x0000 }
3523 };
3524
françois romieu4da19632011-01-03 15:07:55 +00003525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003526}
3527
françois romieue6de30d2011-01-03 15:08:37 +00003528static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3529{
3530 static const struct phy_reg phy_reg_init[] = {
3531 { 0x1f, 0x0001 },
3532 { 0x17, 0x0cc0 },
3533
3534 { 0x1f, 0x0007 },
3535 { 0x1e, 0x002d },
3536 { 0x18, 0x0040 },
3537 { 0x1f, 0x0000 }
3538 };
3539
3540 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3541 rtl_patchphy(tp, 0x0d, 1 << 5);
3542}
3543
Hayes Wang70090422011-07-06 15:58:06 +08003544static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003545{
3546 static const struct phy_reg phy_reg_init[] = {
3547 /* Enable Delay cap */
3548 { 0x1f, 0x0005 },
3549 { 0x05, 0x8b80 },
3550 { 0x06, 0xc896 },
3551 { 0x1f, 0x0000 },
3552
3553 /* Channel estimation fine tune */
3554 { 0x1f, 0x0001 },
3555 { 0x0b, 0x6c20 },
3556 { 0x07, 0x2872 },
3557 { 0x1c, 0xefff },
3558 { 0x1f, 0x0003 },
3559 { 0x14, 0x6420 },
3560 { 0x1f, 0x0000 },
3561
3562 /* Update PFM & 10M TX idle timer */
3563 { 0x1f, 0x0007 },
3564 { 0x1e, 0x002f },
3565 { 0x15, 0x1919 },
3566 { 0x1f, 0x0000 },
3567
3568 { 0x1f, 0x0007 },
3569 { 0x1e, 0x00ac },
3570 { 0x18, 0x0006 },
3571 { 0x1f, 0x0000 }
3572 };
3573
Francois Romieu15ecd032011-04-27 13:52:22 -07003574 rtl_apply_firmware(tp);
3575
hayeswang01dc7fe2011-03-21 01:50:28 +00003576 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3577
3578 /* DCO enable for 10M IDLE Power */
3579 rtl_writephy(tp, 0x1f, 0x0007);
3580 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003581 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003582 rtl_writephy(tp, 0x1f, 0x0000);
3583
3584 /* For impedance matching */
3585 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003586 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003587 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003588
3589 /* PHY auto speed down */
3590 rtl_writephy(tp, 0x1f, 0x0007);
3591 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003592 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003593 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003594 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003595
3596 rtl_writephy(tp, 0x1f, 0x0005);
3597 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003598 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003599 rtl_writephy(tp, 0x1f, 0x0000);
3600
3601 rtl_writephy(tp, 0x1f, 0x0005);
3602 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003603 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003604 rtl_writephy(tp, 0x1f, 0x0007);
3605 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003606 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003607 rtl_writephy(tp, 0x1f, 0x0006);
3608 rtl_writephy(tp, 0x00, 0x5a00);
3609 rtl_writephy(tp, 0x1f, 0x0000);
3610 rtl_writephy(tp, 0x0d, 0x0007);
3611 rtl_writephy(tp, 0x0e, 0x003c);
3612 rtl_writephy(tp, 0x0d, 0x4007);
3613 rtl_writephy(tp, 0x0e, 0x0000);
3614 rtl_writephy(tp, 0x0d, 0x0000);
3615}
3616
françois romieu9ecb9aa2012-12-07 11:20:21 +00003617static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3618{
3619 const u16 w[] = {
3620 addr[0] | (addr[1] << 8),
3621 addr[2] | (addr[3] << 8),
3622 addr[4] | (addr[5] << 8)
3623 };
3624 const struct exgmac_reg e[] = {
3625 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3626 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3627 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3628 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3629 };
3630
3631 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3632}
3633
Hayes Wang70090422011-07-06 15:58:06 +08003634static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3635{
3636 static const struct phy_reg phy_reg_init[] = {
3637 /* Enable Delay cap */
3638 { 0x1f, 0x0004 },
3639 { 0x1f, 0x0007 },
3640 { 0x1e, 0x00ac },
3641 { 0x18, 0x0006 },
3642 { 0x1f, 0x0002 },
3643 { 0x1f, 0x0000 },
3644 { 0x1f, 0x0000 },
3645
3646 /* Channel estimation fine tune */
3647 { 0x1f, 0x0003 },
3648 { 0x09, 0xa20f },
3649 { 0x1f, 0x0000 },
3650 { 0x1f, 0x0000 },
3651
3652 /* Green Setting */
3653 { 0x1f, 0x0005 },
3654 { 0x05, 0x8b5b },
3655 { 0x06, 0x9222 },
3656 { 0x05, 0x8b6d },
3657 { 0x06, 0x8000 },
3658 { 0x05, 0x8b76 },
3659 { 0x06, 0x8000 },
3660 { 0x1f, 0x0000 }
3661 };
3662
3663 rtl_apply_firmware(tp);
3664
3665 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3666
3667 /* For 4-corner performance improve */
3668 rtl_writephy(tp, 0x1f, 0x0005);
3669 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003670 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003671 rtl_writephy(tp, 0x1f, 0x0000);
3672
3673 /* PHY auto speed down */
3674 rtl_writephy(tp, 0x1f, 0x0004);
3675 rtl_writephy(tp, 0x1f, 0x0007);
3676 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003678 rtl_writephy(tp, 0x1f, 0x0002);
3679 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003680 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003681
3682 /* improve 10M EEE waveform */
3683 rtl_writephy(tp, 0x1f, 0x0005);
3684 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003685 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003686 rtl_writephy(tp, 0x1f, 0x0000);
3687
3688 /* Improve 2-pair detection performance */
3689 rtl_writephy(tp, 0x1f, 0x0005);
3690 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003692 rtl_writephy(tp, 0x1f, 0x0000);
3693
3694 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003695 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003696 rtl_writephy(tp, 0x1f, 0x0005);
3697 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003698 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003699 rtl_writephy(tp, 0x1f, 0x0004);
3700 rtl_writephy(tp, 0x1f, 0x0007);
3701 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003702 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003703 rtl_writephy(tp, 0x1f, 0x0002);
3704 rtl_writephy(tp, 0x1f, 0x0000);
3705 rtl_writephy(tp, 0x0d, 0x0007);
3706 rtl_writephy(tp, 0x0e, 0x003c);
3707 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003708 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003709 rtl_writephy(tp, 0x0d, 0x0000);
3710
3711 /* Green feature */
3712 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003713 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3714 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003715 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003716 rtl_writephy(tp, 0x1f, 0x0005);
3717 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3718 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003719
françois romieu9ecb9aa2012-12-07 11:20:21 +00003720 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3721 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003722}
3723
Hayes Wang5f886e02012-03-30 14:33:03 +08003724static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3725{
3726 /* For 4-corner performance improve */
3727 rtl_writephy(tp, 0x1f, 0x0005);
3728 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003729 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003730 rtl_writephy(tp, 0x1f, 0x0000);
3731
3732 /* PHY auto speed down */
3733 rtl_writephy(tp, 0x1f, 0x0007);
3734 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003735 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003736 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003737 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003738
3739 /* Improve 10M EEE waveform */
3740 rtl_writephy(tp, 0x1f, 0x0005);
3741 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003742 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003743 rtl_writephy(tp, 0x1f, 0x0000);
3744}
3745
Hayes Wangc2218922011-09-06 16:55:18 +08003746static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3747{
3748 static const struct phy_reg phy_reg_init[] = {
3749 /* Channel estimation fine tune */
3750 { 0x1f, 0x0003 },
3751 { 0x09, 0xa20f },
3752 { 0x1f, 0x0000 },
3753
3754 /* Modify green table for giga & fnet */
3755 { 0x1f, 0x0005 },
3756 { 0x05, 0x8b55 },
3757 { 0x06, 0x0000 },
3758 { 0x05, 0x8b5e },
3759 { 0x06, 0x0000 },
3760 { 0x05, 0x8b67 },
3761 { 0x06, 0x0000 },
3762 { 0x05, 0x8b70 },
3763 { 0x06, 0x0000 },
3764 { 0x1f, 0x0000 },
3765 { 0x1f, 0x0007 },
3766 { 0x1e, 0x0078 },
3767 { 0x17, 0x0000 },
3768 { 0x19, 0x00fb },
3769 { 0x1f, 0x0000 },
3770
3771 /* Modify green table for 10M */
3772 { 0x1f, 0x0005 },
3773 { 0x05, 0x8b79 },
3774 { 0x06, 0xaa00 },
3775 { 0x1f, 0x0000 },
3776
3777 /* Disable hiimpedance detection (RTCT) */
3778 { 0x1f, 0x0003 },
3779 { 0x01, 0x328a },
3780 { 0x1f, 0x0000 }
3781 };
3782
3783 rtl_apply_firmware(tp);
3784
3785 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3786
Hayes Wang5f886e02012-03-30 14:33:03 +08003787 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003788
3789 /* Improve 2-pair detection performance */
3790 rtl_writephy(tp, 0x1f, 0x0005);
3791 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003792 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003793 rtl_writephy(tp, 0x1f, 0x0000);
3794}
3795
3796static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3797{
3798 rtl_apply_firmware(tp);
3799
Hayes Wang5f886e02012-03-30 14:33:03 +08003800 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003801}
3802
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003803static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3804{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003805 static const struct phy_reg phy_reg_init[] = {
3806 /* Channel estimation fine tune */
3807 { 0x1f, 0x0003 },
3808 { 0x09, 0xa20f },
3809 { 0x1f, 0x0000 },
3810
3811 /* Modify green table for giga & fnet */
3812 { 0x1f, 0x0005 },
3813 { 0x05, 0x8b55 },
3814 { 0x06, 0x0000 },
3815 { 0x05, 0x8b5e },
3816 { 0x06, 0x0000 },
3817 { 0x05, 0x8b67 },
3818 { 0x06, 0x0000 },
3819 { 0x05, 0x8b70 },
3820 { 0x06, 0x0000 },
3821 { 0x1f, 0x0000 },
3822 { 0x1f, 0x0007 },
3823 { 0x1e, 0x0078 },
3824 { 0x17, 0x0000 },
3825 { 0x19, 0x00aa },
3826 { 0x1f, 0x0000 },
3827
3828 /* Modify green table for 10M */
3829 { 0x1f, 0x0005 },
3830 { 0x05, 0x8b79 },
3831 { 0x06, 0xaa00 },
3832 { 0x1f, 0x0000 },
3833
3834 /* Disable hiimpedance detection (RTCT) */
3835 { 0x1f, 0x0003 },
3836 { 0x01, 0x328a },
3837 { 0x1f, 0x0000 }
3838 };
3839
3840
3841 rtl_apply_firmware(tp);
3842
3843 rtl8168f_hw_phy_config(tp);
3844
3845 /* Improve 2-pair detection performance */
3846 rtl_writephy(tp, 0x1f, 0x0005);
3847 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003848 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003849 rtl_writephy(tp, 0x1f, 0x0000);
3850
3851 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3852
3853 /* Modify green table for giga */
3854 rtl_writephy(tp, 0x1f, 0x0005);
3855 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003856 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003857 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003858 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003859 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003860 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003861 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003862 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003863 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003864 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003865 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003866 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003867 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003868 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003869 rtl_writephy(tp, 0x1f, 0x0000);
3870
3871 /* uc same-seed solution */
3872 rtl_writephy(tp, 0x1f, 0x0005);
3873 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003874 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003875 rtl_writephy(tp, 0x1f, 0x0000);
3876
3877 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003878 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003879 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, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003882 rtl_writephy(tp, 0x1f, 0x0004);
3883 rtl_writephy(tp, 0x1f, 0x0007);
3884 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003885 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003886 rtl_writephy(tp, 0x1f, 0x0000);
3887 rtl_writephy(tp, 0x0d, 0x0007);
3888 rtl_writephy(tp, 0x0e, 0x003c);
3889 rtl_writephy(tp, 0x0d, 0x4007);
3890 rtl_writephy(tp, 0x0e, 0x0000);
3891 rtl_writephy(tp, 0x0d, 0x0000);
3892
3893 /* Green feature */
3894 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003895 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3896 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003897 rtl_writephy(tp, 0x1f, 0x0000);
3898}
3899
Hayes Wangc5583862012-07-02 17:23:22 +08003900static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3901{
Hayes Wangc5583862012-07-02 17:23:22 +08003902 rtl_apply_firmware(tp);
3903
hayeswang41f44d12013-04-01 22:23:36 +00003904 rtl_writephy(tp, 0x1f, 0x0a46);
3905 if (rtl_readphy(tp, 0x10) & 0x0100) {
3906 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003907 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003908 } else {
3909 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003910 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003911 }
Hayes Wangc5583862012-07-02 17:23:22 +08003912
hayeswang41f44d12013-04-01 22:23:36 +00003913 rtl_writephy(tp, 0x1f, 0x0a46);
3914 if (rtl_readphy(tp, 0x13) & 0x0100) {
3915 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003916 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003917 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003918 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003919 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003920 }
Hayes Wangc5583862012-07-02 17:23:22 +08003921
hayeswang41f44d12013-04-01 22:23:36 +00003922 /* Enable PHY auto speed down */
3923 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003924 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003925
hayeswangfe7524c2013-04-01 22:23:37 +00003926 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003927 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003928 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003929 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003930 rtl_writephy(tp, 0x1f, 0x0a43);
3931 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003932 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3933 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003934
hayeswang41f44d12013-04-01 22:23:36 +00003935 /* EEE auto-fallback function */
3936 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003937 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003938
hayeswang41f44d12013-04-01 22:23:36 +00003939 /* Enable UC LPF tune function */
3940 rtl_writephy(tp, 0x1f, 0x0a43);
3941 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003942 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003943
3944 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003946
hayeswangfe7524c2013-04-01 22:23:37 +00003947 /* Improve SWR Efficiency */
3948 rtl_writephy(tp, 0x1f, 0x0bcd);
3949 rtl_writephy(tp, 0x14, 0x5065);
3950 rtl_writephy(tp, 0x14, 0xd065);
3951 rtl_writephy(tp, 0x1f, 0x0bc8);
3952 rtl_writephy(tp, 0x11, 0x5655);
3953 rtl_writephy(tp, 0x1f, 0x0bcd);
3954 rtl_writephy(tp, 0x14, 0x1065);
3955 rtl_writephy(tp, 0x14, 0x9065);
3956 rtl_writephy(tp, 0x14, 0x1065);
3957
David Chang1bac1072013-11-27 15:48:36 +08003958 /* Check ALDPS bit, disable it if enabled */
3959 rtl_writephy(tp, 0x1f, 0x0a43);
3960 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003961 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003962
hayeswang41f44d12013-04-01 22:23:36 +00003963 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003964}
3965
hayeswang57538c42013-04-01 22:23:40 +00003966static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3967{
3968 rtl_apply_firmware(tp);
3969}
3970
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003971static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3972{
3973 u16 dout_tapbin;
3974 u32 data;
3975
3976 rtl_apply_firmware(tp);
3977
3978 /* CHN EST parameters adjust - giga master */
3979 rtl_writephy(tp, 0x1f, 0x0a43);
3980 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003981 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003982 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003983 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003984 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003985 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003986 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003987 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003988 rtl_writephy(tp, 0x1f, 0x0000);
3989
3990 /* CHN EST parameters adjust - giga slave */
3991 rtl_writephy(tp, 0x1f, 0x0a43);
3992 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003993 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003994 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003995 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003996 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003997 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003998 rtl_writephy(tp, 0x1f, 0x0000);
3999
4000 /* CHN EST parameters adjust - fnet */
4001 rtl_writephy(tp, 0x1f, 0x0a43);
4002 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004003 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004004 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004005 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004006 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004007 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004008 rtl_writephy(tp, 0x1f, 0x0000);
4009
4010 /* enable R-tune & PGA-retune function */
4011 dout_tapbin = 0;
4012 rtl_writephy(tp, 0x1f, 0x0a46);
4013 data = rtl_readphy(tp, 0x13);
4014 data &= 3;
4015 data <<= 2;
4016 dout_tapbin |= data;
4017 data = rtl_readphy(tp, 0x12);
4018 data &= 0xc000;
4019 data >>= 14;
4020 dout_tapbin |= data;
4021 dout_tapbin = ~(dout_tapbin^0x08);
4022 dout_tapbin <<= 12;
4023 dout_tapbin &= 0xf000;
4024 rtl_writephy(tp, 0x1f, 0x0a43);
4025 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004026 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004027 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004028 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004029 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004030 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004031 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004032 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004033
4034 rtl_writephy(tp, 0x1f, 0x0a43);
4035 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004036 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004037 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004038 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004039 rtl_writephy(tp, 0x1f, 0x0000);
4040
4041 /* enable GPHY 10M */
4042 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004043 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004044 rtl_writephy(tp, 0x1f, 0x0000);
4045
4046 /* SAR ADC performance */
4047 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004048 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004049 rtl_writephy(tp, 0x1f, 0x0000);
4050
4051 rtl_writephy(tp, 0x1f, 0x0a43);
4052 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004053 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004054 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004055 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004056 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004057 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004058 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004059 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004060 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004061 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004062 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004063 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004064 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004065 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004066 rtl_writephy(tp, 0x1f, 0x0000);
4067
4068 /* disable phy pfm mode */
4069 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004070 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004071 rtl_writephy(tp, 0x1f, 0x0000);
4072
4073 /* Check ALDPS bit, disable it if enabled */
4074 rtl_writephy(tp, 0x1f, 0x0a43);
4075 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004076 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004077
4078 rtl_writephy(tp, 0x1f, 0x0000);
4079}
4080
4081static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4082{
4083 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4084 u16 rlen;
4085 u32 data;
4086
4087 rtl_apply_firmware(tp);
4088
4089 /* CHIN EST parameter update */
4090 rtl_writephy(tp, 0x1f, 0x0a43);
4091 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004092 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004093 rtl_writephy(tp, 0x1f, 0x0000);
4094
4095 /* enable R-tune & PGA-retune function */
4096 rtl_writephy(tp, 0x1f, 0x0a43);
4097 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004098 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004099 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004100 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004101 rtl_writephy(tp, 0x1f, 0x0000);
4102
4103 /* enable GPHY 10M */
4104 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004105 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004106 rtl_writephy(tp, 0x1f, 0x0000);
4107
4108 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4109 data = r8168_mac_ocp_read(tp, 0xdd02);
4110 ioffset_p3 = ((data & 0x80)>>7);
4111 ioffset_p3 <<= 3;
4112
4113 data = r8168_mac_ocp_read(tp, 0xdd00);
4114 ioffset_p3 |= ((data & (0xe000))>>13);
4115 ioffset_p2 = ((data & (0x1e00))>>9);
4116 ioffset_p1 = ((data & (0x01e0))>>5);
4117 ioffset_p0 = ((data & 0x0010)>>4);
4118 ioffset_p0 <<= 3;
4119 ioffset_p0 |= (data & (0x07));
4120 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4121
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004122 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004123 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004124 rtl_writephy(tp, 0x1f, 0x0bcf);
4125 rtl_writephy(tp, 0x16, data);
4126 rtl_writephy(tp, 0x1f, 0x0000);
4127 }
4128
4129 /* Modify rlen (TX LPF corner frequency) level */
4130 rtl_writephy(tp, 0x1f, 0x0bcd);
4131 data = rtl_readphy(tp, 0x16);
4132 data &= 0x000f;
4133 rlen = 0;
4134 if (data > 3)
4135 rlen = data - 3;
4136 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4137 rtl_writephy(tp, 0x17, data);
4138 rtl_writephy(tp, 0x1f, 0x0bcd);
4139 rtl_writephy(tp, 0x1f, 0x0000);
4140
4141 /* disable phy pfm mode */
4142 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004143 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004144 rtl_writephy(tp, 0x1f, 0x0000);
4145
4146 /* Check ALDPS bit, disable it if enabled */
4147 rtl_writephy(tp, 0x1f, 0x0a43);
4148 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004149 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004150
4151 rtl_writephy(tp, 0x1f, 0x0000);
4152}
4153
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004154static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4155{
4156 /* Enable PHY auto speed down */
4157 rtl_writephy(tp, 0x1f, 0x0a44);
4158 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4159 rtl_writephy(tp, 0x1f, 0x0000);
4160
4161 /* patch 10M & ALDPS */
4162 rtl_writephy(tp, 0x1f, 0x0bcc);
4163 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4164 rtl_writephy(tp, 0x1f, 0x0a44);
4165 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4166 rtl_writephy(tp, 0x1f, 0x0a43);
4167 rtl_writephy(tp, 0x13, 0x8084);
4168 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4169 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4170 rtl_writephy(tp, 0x1f, 0x0000);
4171
4172 /* Enable EEE auto-fallback function */
4173 rtl_writephy(tp, 0x1f, 0x0a4b);
4174 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4175 rtl_writephy(tp, 0x1f, 0x0000);
4176
4177 /* Enable UC LPF tune function */
4178 rtl_writephy(tp, 0x1f, 0x0a43);
4179 rtl_writephy(tp, 0x13, 0x8012);
4180 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4181 rtl_writephy(tp, 0x1f, 0x0000);
4182
4183 /* set rg_sel_sdm_rate */
4184 rtl_writephy(tp, 0x1f, 0x0c42);
4185 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4186 rtl_writephy(tp, 0x1f, 0x0000);
4187
4188 /* Check ALDPS bit, disable it if enabled */
4189 rtl_writephy(tp, 0x1f, 0x0a43);
4190 if (rtl_readphy(tp, 0x10) & 0x0004)
4191 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4192
4193 rtl_writephy(tp, 0x1f, 0x0000);
4194}
4195
4196static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4197{
4198 /* patch 10M & ALDPS */
4199 rtl_writephy(tp, 0x1f, 0x0bcc);
4200 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4201 rtl_writephy(tp, 0x1f, 0x0a44);
4202 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4203 rtl_writephy(tp, 0x1f, 0x0a43);
4204 rtl_writephy(tp, 0x13, 0x8084);
4205 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4206 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4207 rtl_writephy(tp, 0x1f, 0x0000);
4208
4209 /* Enable UC LPF tune function */
4210 rtl_writephy(tp, 0x1f, 0x0a43);
4211 rtl_writephy(tp, 0x13, 0x8012);
4212 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4213 rtl_writephy(tp, 0x1f, 0x0000);
4214
4215 /* Set rg_sel_sdm_rate */
4216 rtl_writephy(tp, 0x1f, 0x0c42);
4217 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4218 rtl_writephy(tp, 0x1f, 0x0000);
4219
4220 /* Channel estimation parameters */
4221 rtl_writephy(tp, 0x1f, 0x0a43);
4222 rtl_writephy(tp, 0x13, 0x80f3);
4223 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4224 rtl_writephy(tp, 0x13, 0x80f0);
4225 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4226 rtl_writephy(tp, 0x13, 0x80ef);
4227 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4228 rtl_writephy(tp, 0x13, 0x80f6);
4229 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4230 rtl_writephy(tp, 0x13, 0x80ec);
4231 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4232 rtl_writephy(tp, 0x13, 0x80ed);
4233 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4234 rtl_writephy(tp, 0x13, 0x80f2);
4235 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4236 rtl_writephy(tp, 0x13, 0x80f4);
4237 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4238 rtl_writephy(tp, 0x1f, 0x0a43);
4239 rtl_writephy(tp, 0x13, 0x8110);
4240 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4241 rtl_writephy(tp, 0x13, 0x810f);
4242 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4243 rtl_writephy(tp, 0x13, 0x8111);
4244 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4245 rtl_writephy(tp, 0x13, 0x8113);
4246 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4247 rtl_writephy(tp, 0x13, 0x8115);
4248 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4249 rtl_writephy(tp, 0x13, 0x810e);
4250 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4251 rtl_writephy(tp, 0x13, 0x810c);
4252 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4253 rtl_writephy(tp, 0x13, 0x810b);
4254 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4255 rtl_writephy(tp, 0x1f, 0x0a43);
4256 rtl_writephy(tp, 0x13, 0x80d1);
4257 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4258 rtl_writephy(tp, 0x13, 0x80cd);
4259 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4260 rtl_writephy(tp, 0x13, 0x80d3);
4261 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4262 rtl_writephy(tp, 0x13, 0x80d5);
4263 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4264 rtl_writephy(tp, 0x13, 0x80d7);
4265 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4266
4267 /* Force PWM-mode */
4268 rtl_writephy(tp, 0x1f, 0x0bcd);
4269 rtl_writephy(tp, 0x14, 0x5065);
4270 rtl_writephy(tp, 0x14, 0xd065);
4271 rtl_writephy(tp, 0x1f, 0x0bc8);
4272 rtl_writephy(tp, 0x12, 0x00ed);
4273 rtl_writephy(tp, 0x1f, 0x0bcd);
4274 rtl_writephy(tp, 0x14, 0x1065);
4275 rtl_writephy(tp, 0x14, 0x9065);
4276 rtl_writephy(tp, 0x14, 0x1065);
4277 rtl_writephy(tp, 0x1f, 0x0000);
4278
4279 /* Check ALDPS bit, disable it if enabled */
4280 rtl_writephy(tp, 0x1f, 0x0a43);
4281 if (rtl_readphy(tp, 0x10) & 0x0004)
4282 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4283
4284 rtl_writephy(tp, 0x1f, 0x0000);
4285}
4286
françois romieu4da19632011-01-03 15:07:55 +00004287static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004288{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004289 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004290 { 0x1f, 0x0003 },
4291 { 0x08, 0x441d },
4292 { 0x01, 0x9100 },
4293 { 0x1f, 0x0000 }
4294 };
4295
françois romieu4da19632011-01-03 15:07:55 +00004296 rtl_writephy(tp, 0x1f, 0x0000);
4297 rtl_patchphy(tp, 0x11, 1 << 12);
4298 rtl_patchphy(tp, 0x19, 1 << 13);
4299 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004300
françois romieu4da19632011-01-03 15:07:55 +00004301 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004302}
4303
Hayes Wang5a5e4442011-02-22 17:26:21 +08004304static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4305{
4306 static const struct phy_reg phy_reg_init[] = {
4307 { 0x1f, 0x0005 },
4308 { 0x1a, 0x0000 },
4309 { 0x1f, 0x0000 },
4310
4311 { 0x1f, 0x0004 },
4312 { 0x1c, 0x0000 },
4313 { 0x1f, 0x0000 },
4314
4315 { 0x1f, 0x0001 },
4316 { 0x15, 0x7701 },
4317 { 0x1f, 0x0000 }
4318 };
4319
4320 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004321 rtl_writephy(tp, 0x1f, 0x0000);
4322 rtl_writephy(tp, 0x18, 0x0310);
4323 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004324
François Romieu953a12c2011-04-24 17:38:48 +02004325 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004326
4327 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4328}
4329
Hayes Wang7e18dca2012-03-30 14:33:02 +08004330static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4331{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004332 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004333 rtl_writephy(tp, 0x1f, 0x0000);
4334 rtl_writephy(tp, 0x18, 0x0310);
4335 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004336
4337 rtl_apply_firmware(tp);
4338
4339 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004340 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004341 rtl_writephy(tp, 0x1f, 0x0004);
4342 rtl_writephy(tp, 0x10, 0x401f);
4343 rtl_writephy(tp, 0x19, 0x7030);
4344 rtl_writephy(tp, 0x1f, 0x0000);
4345}
4346
Hayes Wang5598bfe2012-07-02 17:23:21 +08004347static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4348{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004349 static const struct phy_reg phy_reg_init[] = {
4350 { 0x1f, 0x0004 },
4351 { 0x10, 0xc07f },
4352 { 0x19, 0x7030 },
4353 { 0x1f, 0x0000 }
4354 };
4355
4356 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004357 rtl_writephy(tp, 0x1f, 0x0000);
4358 rtl_writephy(tp, 0x18, 0x0310);
4359 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004360
4361 rtl_apply_firmware(tp);
4362
Francois Romieufdf6fc02012-07-06 22:40:38 +02004363 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004364 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4365
Francois Romieufdf6fc02012-07-06 22:40:38 +02004366 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004367}
4368
Francois Romieu5615d9f2007-08-17 17:50:46 +02004369static void rtl_hw_phy_config(struct net_device *dev)
4370{
4371 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004372
4373 rtl8169_print_mac_version(tp);
4374
4375 switch (tp->mac_version) {
4376 case RTL_GIGA_MAC_VER_01:
4377 break;
4378 case RTL_GIGA_MAC_VER_02:
4379 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004380 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004381 break;
4382 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004383 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004384 break;
françois romieu2e9558562009-08-10 19:44:19 +00004385 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004386 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004387 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004388 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004389 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004390 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004391 case RTL_GIGA_MAC_VER_07:
4392 case RTL_GIGA_MAC_VER_08:
4393 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004394 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004395 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004396 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004397 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004398 break;
4399 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004400 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004401 break;
4402 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004403 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004404 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004405 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004406 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004407 break;
4408 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004409 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004410 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004411 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004412 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004413 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004414 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004415 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004416 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004417 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004418 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004419 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004420 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004421 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004422 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004423 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004424 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004425 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004426 break;
4427 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004428 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004429 break;
4430 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004431 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004432 break;
françois romieue6de30d2011-01-03 15:08:37 +00004433 case RTL_GIGA_MAC_VER_28:
4434 rtl8168d_4_hw_phy_config(tp);
4435 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004436 case RTL_GIGA_MAC_VER_29:
4437 case RTL_GIGA_MAC_VER_30:
4438 rtl8105e_hw_phy_config(tp);
4439 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004440 case RTL_GIGA_MAC_VER_31:
4441 /* None. */
4442 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004443 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004444 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004445 rtl8168e_1_hw_phy_config(tp);
4446 break;
4447 case RTL_GIGA_MAC_VER_34:
4448 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004449 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004450 case RTL_GIGA_MAC_VER_35:
4451 rtl8168f_1_hw_phy_config(tp);
4452 break;
4453 case RTL_GIGA_MAC_VER_36:
4454 rtl8168f_2_hw_phy_config(tp);
4455 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004456
Hayes Wang7e18dca2012-03-30 14:33:02 +08004457 case RTL_GIGA_MAC_VER_37:
4458 rtl8402_hw_phy_config(tp);
4459 break;
4460
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004461 case RTL_GIGA_MAC_VER_38:
4462 rtl8411_hw_phy_config(tp);
4463 break;
4464
Hayes Wang5598bfe2012-07-02 17:23:21 +08004465 case RTL_GIGA_MAC_VER_39:
4466 rtl8106e_hw_phy_config(tp);
4467 break;
4468
Hayes Wangc5583862012-07-02 17:23:22 +08004469 case RTL_GIGA_MAC_VER_40:
4470 rtl8168g_1_hw_phy_config(tp);
4471 break;
hayeswang57538c42013-04-01 22:23:40 +00004472 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004473 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004474 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004475 rtl8168g_2_hw_phy_config(tp);
4476 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004477 case RTL_GIGA_MAC_VER_45:
4478 case RTL_GIGA_MAC_VER_47:
4479 rtl8168h_1_hw_phy_config(tp);
4480 break;
4481 case RTL_GIGA_MAC_VER_46:
4482 case RTL_GIGA_MAC_VER_48:
4483 rtl8168h_2_hw_phy_config(tp);
4484 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004485
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004486 case RTL_GIGA_MAC_VER_49:
4487 rtl8168ep_1_hw_phy_config(tp);
4488 break;
4489 case RTL_GIGA_MAC_VER_50:
4490 case RTL_GIGA_MAC_VER_51:
4491 rtl8168ep_2_hw_phy_config(tp);
4492 break;
4493
Hayes Wangc5583862012-07-02 17:23:22 +08004494 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004495 default:
4496 break;
4497 }
4498}
4499
Francois Romieuda78dbf2012-01-26 14:18:23 +01004500static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 struct timer_list *timer = &tp->timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4504
Francois Romieubcf0bf92006-07-26 23:14:13 +02004505 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
françois romieu4da19632011-01-03 15:07:55 +00004507 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004508 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 * A busy loop could burn quite a few cycles on nowadays CPU.
4510 * Let's delay the execution of the timer for a few ticks.
4511 */
4512 timeout = HZ/10;
4513 goto out_mod_timer;
4514 }
4515
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004516 if (tp->link_ok(tp))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004517 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004519 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
françois romieu4da19632011-01-03 15:07:55 +00004521 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
4523out_mod_timer:
4524 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004525}
4526
4527static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4528{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004529 if (!test_and_set_bit(flag, tp->wk.flags))
4530 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004531}
4532
Kees Cook9de36cc2017-10-25 03:53:12 -07004533static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004534{
Kees Cook9de36cc2017-10-25 03:53:12 -07004535 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004536
Francois Romieu98ddf982012-01-31 10:47:34 +01004537 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538}
4539
Francois Romieuffc46952012-07-06 14:19:23 +02004540DECLARE_RTL_COND(rtl_phy_reset_cond)
4541{
4542 return tp->phy_reset_pending(tp);
4543}
4544
Francois Romieubf793292006-11-01 00:53:05 +01004545static void rtl8169_phy_reset(struct net_device *dev,
4546 struct rtl8169_private *tp)
4547{
françois romieu4da19632011-01-03 15:07:55 +00004548 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004549 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004550}
4551
David S. Miller8decf862011-09-22 03:23:13 -04004552static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4553{
David S. Miller8decf862011-09-22 03:23:13 -04004554 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004555 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004556}
4557
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004558static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004560 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004561
Marcus Sundberg773328942008-07-10 21:28:08 +02004562 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4563 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004564 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004565 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004566
Francois Romieu6dccd162007-02-13 23:38:05 +01004567 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4568
4569 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4570 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004571
Francois Romieubcf0bf92006-07-26 23:14:13 +02004572 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004573 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004574 RTL_W8(tp, 0x82, 0x01);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004575 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004576 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004577 }
4578
Francois Romieubf793292006-11-01 00:53:05 +01004579 rtl8169_phy_reset(dev, tp);
4580
Oliver Neukum54405cd2011-01-06 21:55:13 +01004581 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004582 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4583 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4584 (tp->mii.supports_gmii ?
4585 ADVERTISED_1000baseT_Half |
4586 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004587
David S. Miller8decf862011-09-22 03:23:13 -04004588 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004589 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004590}
4591
Francois Romieu773d2022007-01-31 23:47:43 +01004592static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4593{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004594 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004595
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004596 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004597
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004598 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4599 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004600
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004601 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4602 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004603
françois romieu9ecb9aa2012-12-07 11:20:21 +00004604 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4605 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004606
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004607 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004608
Francois Romieuda78dbf2012-01-26 14:18:23 +01004609 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004610}
4611
4612static int rtl_set_mac_address(struct net_device *dev, void *p)
4613{
4614 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004615 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004616 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004617
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004618 ret = eth_mac_addr(dev, p);
4619 if (ret)
4620 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004621
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004622 pm_runtime_get_noresume(d);
4623
4624 if (pm_runtime_active(d))
4625 rtl_rar_set(tp, dev->dev_addr);
4626
4627 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004628
4629 return 0;
4630}
4631
Francois Romieu5f787a12006-08-17 13:02:36 +02004632static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4633{
4634 struct rtl8169_private *tp = netdev_priv(dev);
4635 struct mii_ioctl_data *data = if_mii(ifr);
4636
Francois Romieu8b4ab282008-11-19 22:05:25 -08004637 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4638}
Francois Romieu5f787a12006-08-17 13:02:36 +02004639
Francois Romieucecb5fd2011-04-01 10:21:07 +02004640static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4641 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004642{
Francois Romieu5f787a12006-08-17 13:02:36 +02004643 switch (cmd) {
4644 case SIOCGMIIPHY:
4645 data->phy_id = 32; /* Internal PHY */
4646 return 0;
4647
4648 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004649 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004650 return 0;
4651
4652 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004653 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004654 return 0;
4655 }
4656 return -EOPNOTSUPP;
4657}
4658
Francois Romieu8b4ab282008-11-19 22:05:25 -08004659static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4660{
4661 return -EOPNOTSUPP;
4662}
4663
Bill Pembertonbaf63292012-12-03 09:23:28 -05004664static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004665{
4666 struct mdio_ops *ops = &tp->mdio_ops;
4667
4668 switch (tp->mac_version) {
4669 case RTL_GIGA_MAC_VER_27:
4670 ops->write = r8168dp_1_mdio_write;
4671 ops->read = r8168dp_1_mdio_read;
4672 break;
françois romieue6de30d2011-01-03 15:08:37 +00004673 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004674 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004675 ops->write = r8168dp_2_mdio_write;
4676 ops->read = r8168dp_2_mdio_read;
4677 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004678 case RTL_GIGA_MAC_VER_40:
4679 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004680 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004681 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004682 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004683 case RTL_GIGA_MAC_VER_45:
4684 case RTL_GIGA_MAC_VER_46:
4685 case RTL_GIGA_MAC_VER_47:
4686 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004687 case RTL_GIGA_MAC_VER_49:
4688 case RTL_GIGA_MAC_VER_50:
4689 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004690 ops->write = r8168g_mdio_write;
4691 ops->read = r8168g_mdio_read;
4692 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004693 default:
4694 ops->write = r8169_mdio_write;
4695 ops->read = r8169_mdio_read;
4696 break;
4697 }
4698}
4699
hayeswange2409d82013-03-31 17:02:04 +00004700static void rtl_speed_down(struct rtl8169_private *tp)
4701{
4702 u32 adv;
4703 int lpa;
4704
4705 rtl_writephy(tp, 0x1f, 0x0000);
4706 lpa = rtl_readphy(tp, MII_LPA);
4707
4708 if (lpa & (LPA_10HALF | LPA_10FULL))
4709 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4710 else if (lpa & (LPA_100HALF | LPA_100FULL))
4711 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4712 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4713 else
4714 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4715 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4716 (tp->mii.supports_gmii ?
4717 ADVERTISED_1000baseT_Half |
4718 ADVERTISED_1000baseT_Full : 0);
4719
4720 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4721 adv);
4722}
4723
David S. Miller1805b2f2011-10-24 18:18:09 -04004724static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4725{
David S. Miller1805b2f2011-10-24 18:18:09 -04004726 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004727 case RTL_GIGA_MAC_VER_25:
4728 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004729 case RTL_GIGA_MAC_VER_29:
4730 case RTL_GIGA_MAC_VER_30:
4731 case RTL_GIGA_MAC_VER_32:
4732 case RTL_GIGA_MAC_VER_33:
4733 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004734 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004735 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004736 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004737 case RTL_GIGA_MAC_VER_40:
4738 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004739 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004740 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004741 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004742 case RTL_GIGA_MAC_VER_45:
4743 case RTL_GIGA_MAC_VER_46:
4744 case RTL_GIGA_MAC_VER_47:
4745 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004746 case RTL_GIGA_MAC_VER_49:
4747 case RTL_GIGA_MAC_VER_50:
4748 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004749 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004750 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4751 break;
4752 default:
4753 break;
4754 }
4755}
4756
4757static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4758{
4759 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4760 return false;
4761
hayeswange2409d82013-03-31 17:02:04 +00004762 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004763 rtl_wol_suspend_quirk(tp);
4764
4765 return true;
4766}
4767
françois romieu065c27c2011-01-03 15:08:12 +00004768static void r810x_phy_power_down(struct rtl8169_private *tp)
4769{
4770 rtl_writephy(tp, 0x1f, 0x0000);
4771 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4772}
4773
4774static void r810x_phy_power_up(struct rtl8169_private *tp)
4775{
4776 rtl_writephy(tp, 0x1f, 0x0000);
4777 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4778}
4779
4780static void r810x_pll_power_down(struct rtl8169_private *tp)
4781{
David S. Miller1805b2f2011-10-24 18:18:09 -04004782 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004783 return;
françois romieu065c27c2011-01-03 15:08:12 +00004784
4785 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004786
4787 switch (tp->mac_version) {
4788 case RTL_GIGA_MAC_VER_07:
4789 case RTL_GIGA_MAC_VER_08:
4790 case RTL_GIGA_MAC_VER_09:
4791 case RTL_GIGA_MAC_VER_10:
4792 case RTL_GIGA_MAC_VER_13:
4793 case RTL_GIGA_MAC_VER_16:
4794 break;
4795 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004796 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004797 break;
4798 }
françois romieu065c27c2011-01-03 15:08:12 +00004799}
4800
4801static void r810x_pll_power_up(struct rtl8169_private *tp)
4802{
4803 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004804
4805 switch (tp->mac_version) {
4806 case RTL_GIGA_MAC_VER_07:
4807 case RTL_GIGA_MAC_VER_08:
4808 case RTL_GIGA_MAC_VER_09:
4809 case RTL_GIGA_MAC_VER_10:
4810 case RTL_GIGA_MAC_VER_13:
4811 case RTL_GIGA_MAC_VER_16:
4812 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004813 case RTL_GIGA_MAC_VER_47:
4814 case RTL_GIGA_MAC_VER_48:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004815 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004816 break;
Hayes Wang00042992012-03-30 14:33:00 +08004817 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004818 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
Hayes Wang00042992012-03-30 14:33:00 +08004819 break;
4820 }
françois romieu065c27c2011-01-03 15:08:12 +00004821}
4822
4823static void r8168_phy_power_up(struct rtl8169_private *tp)
4824{
4825 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004826 switch (tp->mac_version) {
4827 case RTL_GIGA_MAC_VER_11:
4828 case RTL_GIGA_MAC_VER_12:
4829 case RTL_GIGA_MAC_VER_17:
4830 case RTL_GIGA_MAC_VER_18:
4831 case RTL_GIGA_MAC_VER_19:
4832 case RTL_GIGA_MAC_VER_20:
4833 case RTL_GIGA_MAC_VER_21:
4834 case RTL_GIGA_MAC_VER_22:
4835 case RTL_GIGA_MAC_VER_23:
4836 case RTL_GIGA_MAC_VER_24:
4837 case RTL_GIGA_MAC_VER_25:
4838 case RTL_GIGA_MAC_VER_26:
4839 case RTL_GIGA_MAC_VER_27:
4840 case RTL_GIGA_MAC_VER_28:
4841 case RTL_GIGA_MAC_VER_31:
4842 rtl_writephy(tp, 0x0e, 0x0000);
4843 break;
4844 default:
4845 break;
4846 }
françois romieu065c27c2011-01-03 15:08:12 +00004847 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4848}
4849
4850static void r8168_phy_power_down(struct rtl8169_private *tp)
4851{
4852 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004853 switch (tp->mac_version) {
4854 case RTL_GIGA_MAC_VER_32:
4855 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004856 case RTL_GIGA_MAC_VER_40:
4857 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004858 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4859 break;
4860
4861 case RTL_GIGA_MAC_VER_11:
4862 case RTL_GIGA_MAC_VER_12:
4863 case RTL_GIGA_MAC_VER_17:
4864 case RTL_GIGA_MAC_VER_18:
4865 case RTL_GIGA_MAC_VER_19:
4866 case RTL_GIGA_MAC_VER_20:
4867 case RTL_GIGA_MAC_VER_21:
4868 case RTL_GIGA_MAC_VER_22:
4869 case RTL_GIGA_MAC_VER_23:
4870 case RTL_GIGA_MAC_VER_24:
4871 case RTL_GIGA_MAC_VER_25:
4872 case RTL_GIGA_MAC_VER_26:
4873 case RTL_GIGA_MAC_VER_27:
4874 case RTL_GIGA_MAC_VER_28:
4875 case RTL_GIGA_MAC_VER_31:
4876 rtl_writephy(tp, 0x0e, 0x0200);
4877 default:
4878 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4879 break;
4880 }
françois romieu065c27c2011-01-03 15:08:12 +00004881}
4882
4883static void r8168_pll_power_down(struct rtl8169_private *tp)
4884{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004885 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004886 return;
4887
Francois Romieucecb5fd2011-04-01 10:21:07 +02004888 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4889 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004890 (RTL_R16(tp, CPlusCmd) & ASF)) {
françois romieu065c27c2011-01-03 15:08:12 +00004891 return;
4892 }
4893
hayeswang01dc7fe2011-03-21 01:50:28 +00004894 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4895 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004896 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004897
David S. Miller1805b2f2011-10-24 18:18:09 -04004898 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004899 return;
françois romieu065c27c2011-01-03 15:08:12 +00004900
4901 r8168_phy_power_down(tp);
4902
4903 switch (tp->mac_version) {
4904 case RTL_GIGA_MAC_VER_25:
4905 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004906 case RTL_GIGA_MAC_VER_27:
4907 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004908 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004909 case RTL_GIGA_MAC_VER_32:
4910 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004911 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004912 case RTL_GIGA_MAC_VER_45:
4913 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004914 case RTL_GIGA_MAC_VER_50:
4915 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004916 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004917 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004918 case RTL_GIGA_MAC_VER_40:
4919 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004920 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004921 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004922 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004923 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004924 break;
françois romieu065c27c2011-01-03 15:08:12 +00004925 }
4926}
4927
4928static void r8168_pll_power_up(struct rtl8169_private *tp)
4929{
françois romieu065c27c2011-01-03 15:08:12 +00004930 switch (tp->mac_version) {
4931 case RTL_GIGA_MAC_VER_25:
4932 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004933 case RTL_GIGA_MAC_VER_27:
4934 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004935 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004936 case RTL_GIGA_MAC_VER_32:
4937 case RTL_GIGA_MAC_VER_33:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004938 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004939 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004940 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004941 case RTL_GIGA_MAC_VER_45:
4942 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004943 case RTL_GIGA_MAC_VER_50:
4944 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004945 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004946 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004947 case RTL_GIGA_MAC_VER_40:
4948 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004949 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004950 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004951 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004952 0x00000000, ERIAR_EXGMAC);
4953 break;
françois romieu065c27c2011-01-03 15:08:12 +00004954 }
4955
4956 r8168_phy_power_up(tp);
4957}
4958
Francois Romieud58d46b2011-05-03 16:38:29 +02004959static void rtl_generic_op(struct rtl8169_private *tp,
4960 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004961{
4962 if (op)
4963 op(tp);
4964}
4965
4966static void rtl_pll_power_down(struct rtl8169_private *tp)
4967{
Francois Romieud58d46b2011-05-03 16:38:29 +02004968 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004969}
4970
4971static void rtl_pll_power_up(struct rtl8169_private *tp)
4972{
Francois Romieud58d46b2011-05-03 16:38:29 +02004973 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004974}
4975
Bill Pembertonbaf63292012-12-03 09:23:28 -05004976static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004977{
4978 struct pll_power_ops *ops = &tp->pll_power_ops;
4979
4980 switch (tp->mac_version) {
4981 case RTL_GIGA_MAC_VER_07:
4982 case RTL_GIGA_MAC_VER_08:
4983 case RTL_GIGA_MAC_VER_09:
4984 case RTL_GIGA_MAC_VER_10:
4985 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004986 case RTL_GIGA_MAC_VER_29:
4987 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004988 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004989 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004990 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004991 case RTL_GIGA_MAC_VER_47:
4992 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004993 ops->down = r810x_pll_power_down;
4994 ops->up = r810x_pll_power_up;
4995 break;
4996
4997 case RTL_GIGA_MAC_VER_11:
4998 case RTL_GIGA_MAC_VER_12:
4999 case RTL_GIGA_MAC_VER_17:
5000 case RTL_GIGA_MAC_VER_18:
5001 case RTL_GIGA_MAC_VER_19:
5002 case RTL_GIGA_MAC_VER_20:
5003 case RTL_GIGA_MAC_VER_21:
5004 case RTL_GIGA_MAC_VER_22:
5005 case RTL_GIGA_MAC_VER_23:
5006 case RTL_GIGA_MAC_VER_24:
5007 case RTL_GIGA_MAC_VER_25:
5008 case RTL_GIGA_MAC_VER_26:
5009 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005010 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005011 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005012 case RTL_GIGA_MAC_VER_32:
5013 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005014 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005015 case RTL_GIGA_MAC_VER_35:
5016 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005017 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005018 case RTL_GIGA_MAC_VER_40:
5019 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005020 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005021 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005022 case RTL_GIGA_MAC_VER_45:
5023 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005024 case RTL_GIGA_MAC_VER_49:
5025 case RTL_GIGA_MAC_VER_50:
5026 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005027 ops->down = r8168_pll_power_down;
5028 ops->up = r8168_pll_power_up;
5029 break;
5030
5031 default:
5032 ops->down = NULL;
5033 ops->up = NULL;
5034 break;
5035 }
5036}
5037
Hayes Wange542a222011-07-06 15:58:04 +08005038static void rtl_init_rxcfg(struct rtl8169_private *tp)
5039{
Hayes Wange542a222011-07-06 15:58:04 +08005040 switch (tp->mac_version) {
5041 case RTL_GIGA_MAC_VER_01:
5042 case RTL_GIGA_MAC_VER_02:
5043 case RTL_GIGA_MAC_VER_03:
5044 case RTL_GIGA_MAC_VER_04:
5045 case RTL_GIGA_MAC_VER_05:
5046 case RTL_GIGA_MAC_VER_06:
5047 case RTL_GIGA_MAC_VER_10:
5048 case RTL_GIGA_MAC_VER_11:
5049 case RTL_GIGA_MAC_VER_12:
5050 case RTL_GIGA_MAC_VER_13:
5051 case RTL_GIGA_MAC_VER_14:
5052 case RTL_GIGA_MAC_VER_15:
5053 case RTL_GIGA_MAC_VER_16:
5054 case RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005055 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005056 break;
5057 case RTL_GIGA_MAC_VER_18:
5058 case RTL_GIGA_MAC_VER_19:
5059 case RTL_GIGA_MAC_VER_20:
5060 case RTL_GIGA_MAC_VER_21:
5061 case RTL_GIGA_MAC_VER_22:
5062 case RTL_GIGA_MAC_VER_23:
5063 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005064 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005065 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005066 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005067 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005068 case RTL_GIGA_MAC_VER_40:
5069 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005070 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005071 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005072 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005073 case RTL_GIGA_MAC_VER_45:
5074 case RTL_GIGA_MAC_VER_46:
5075 case RTL_GIGA_MAC_VER_47:
5076 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005077 case RTL_GIGA_MAC_VER_49:
5078 case RTL_GIGA_MAC_VER_50:
5079 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005080 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005081 break;
Hayes Wange542a222011-07-06 15:58:04 +08005082 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005083 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08005084 break;
5085 }
5086}
5087
Hayes Wang92fc43b2011-07-06 15:58:03 +08005088static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5089{
Timo Teräs9fba0812013-01-15 21:01:24 +00005090 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005091}
5092
Francois Romieud58d46b2011-05-03 16:38:29 +02005093static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5094{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005095 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005096 rtl_generic_op(tp, tp->jumbo_ops.enable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005097 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005098}
5099
5100static void rtl_hw_jumbo_disable(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.disable);
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 r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5108{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005109 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5110 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01005111 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005112}
5113
5114static void r8168c_hw_jumbo_disable(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 Kallweit8d98aa32018-04-16 21:38:27 +02005118 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005119}
5120
5121static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5122{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005123 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005124}
5125
5126static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5127{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005128 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02005129}
5130
5131static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5132{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005133 RTL_W8(tp, MaxTxPacketSize, 0x3f);
5134 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
5135 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01005136 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005137}
5138
5139static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5140{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005141 RTL_W8(tp, MaxTxPacketSize, 0x0c);
5142 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
5143 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005144 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005145}
5146
5147static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5148{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005149 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005150 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005151}
5152
5153static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5154{
Heiner Kallweitcb732002018-03-20 07:45:35 +01005155 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005156 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005157}
5158
5159static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5160{
Francois Romieud58d46b2011-05-03 16:38:29 +02005161 r8168b_0_hw_jumbo_enable(tp);
5162
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005163 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02005164}
5165
5166static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5167{
Francois Romieud58d46b2011-05-03 16:38:29 +02005168 r8168b_0_hw_jumbo_disable(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
Bill Pembertonbaf63292012-12-03 09:23:28 -05005173static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005174{
5175 struct jumbo_ops *ops = &tp->jumbo_ops;
5176
5177 switch (tp->mac_version) {
5178 case RTL_GIGA_MAC_VER_11:
5179 ops->disable = r8168b_0_hw_jumbo_disable;
5180 ops->enable = r8168b_0_hw_jumbo_enable;
5181 break;
5182 case RTL_GIGA_MAC_VER_12:
5183 case RTL_GIGA_MAC_VER_17:
5184 ops->disable = r8168b_1_hw_jumbo_disable;
5185 ops->enable = r8168b_1_hw_jumbo_enable;
5186 break;
5187 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5188 case RTL_GIGA_MAC_VER_19:
5189 case RTL_GIGA_MAC_VER_20:
5190 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5191 case RTL_GIGA_MAC_VER_22:
5192 case RTL_GIGA_MAC_VER_23:
5193 case RTL_GIGA_MAC_VER_24:
5194 case RTL_GIGA_MAC_VER_25:
5195 case RTL_GIGA_MAC_VER_26:
5196 ops->disable = r8168c_hw_jumbo_disable;
5197 ops->enable = r8168c_hw_jumbo_enable;
5198 break;
5199 case RTL_GIGA_MAC_VER_27:
5200 case RTL_GIGA_MAC_VER_28:
5201 ops->disable = r8168dp_hw_jumbo_disable;
5202 ops->enable = r8168dp_hw_jumbo_enable;
5203 break;
5204 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5205 case RTL_GIGA_MAC_VER_32:
5206 case RTL_GIGA_MAC_VER_33:
5207 case RTL_GIGA_MAC_VER_34:
5208 ops->disable = r8168e_hw_jumbo_disable;
5209 ops->enable = r8168e_hw_jumbo_enable;
5210 break;
5211
5212 /*
5213 * No action needed for jumbo frames with 8169.
5214 * No jumbo for 810x at all.
5215 */
Hayes Wangc5583862012-07-02 17:23:22 +08005216 case RTL_GIGA_MAC_VER_40:
5217 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005218 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005219 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005220 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005221 case RTL_GIGA_MAC_VER_45:
5222 case RTL_GIGA_MAC_VER_46:
5223 case RTL_GIGA_MAC_VER_47:
5224 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005225 case RTL_GIGA_MAC_VER_49:
5226 case RTL_GIGA_MAC_VER_50:
5227 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005228 default:
5229 ops->disable = NULL;
5230 ops->enable = NULL;
5231 break;
5232 }
5233}
5234
Francois Romieuffc46952012-07-06 14:19:23 +02005235DECLARE_RTL_COND(rtl_chipcmd_cond)
5236{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005237 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02005238}
5239
Francois Romieu6f43adc2011-04-29 15:05:51 +02005240static void rtl_hw_reset(struct rtl8169_private *tp)
5241{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005242 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005243
Francois Romieuffc46952012-07-06 14:19:23 +02005244 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005245}
5246
Francois Romieub6ffd972011-06-17 17:00:05 +02005247static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5248{
5249 struct rtl_fw *rtl_fw;
5250 const char *name;
5251 int rc = -ENOMEM;
5252
5253 name = rtl_lookup_firmware_name(tp);
5254 if (!name)
5255 goto out_no_firmware;
5256
5257 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5258 if (!rtl_fw)
5259 goto err_warn;
5260
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005261 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02005262 if (rc < 0)
5263 goto err_free;
5264
Francois Romieufd112f22011-06-18 00:10:29 +02005265 rc = rtl_check_firmware(tp, rtl_fw);
5266 if (rc < 0)
5267 goto err_release_firmware;
5268
Francois Romieub6ffd972011-06-17 17:00:05 +02005269 tp->rtl_fw = rtl_fw;
5270out:
5271 return;
5272
Francois Romieufd112f22011-06-18 00:10:29 +02005273err_release_firmware:
5274 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005275err_free:
5276 kfree(rtl_fw);
5277err_warn:
5278 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5279 name, rc);
5280out_no_firmware:
5281 tp->rtl_fw = NULL;
5282 goto out;
5283}
5284
François Romieu953a12c2011-04-24 17:38:48 +02005285static void rtl_request_firmware(struct rtl8169_private *tp)
5286{
Francois Romieub6ffd972011-06-17 17:00:05 +02005287 if (IS_ERR(tp->rtl_fw))
5288 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005289}
5290
Hayes Wang92fc43b2011-07-06 15:58:03 +08005291static void rtl_rx_close(struct rtl8169_private *tp)
5292{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005293 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005294}
5295
Francois Romieuffc46952012-07-06 14:19:23 +02005296DECLARE_RTL_COND(rtl_npq_cond)
5297{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005298 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02005299}
5300
5301DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5302{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005303 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02005304}
5305
françois romieue6de30d2011-01-03 15:08:37 +00005306static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307{
5308 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005309 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Hayes Wang92fc43b2011-07-06 15:58:03 +08005311 rtl_rx_close(tp);
5312
Hayes Wang5d2e1952011-02-22 17:26:22 +08005313 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005314 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5315 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005316 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005317 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005318 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5319 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5320 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5321 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5322 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5323 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5324 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5325 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5326 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5327 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5328 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5329 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005330 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5331 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5333 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005334 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005335 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005336 } else {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005337 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005338 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005339 }
5340
Hayes Wang92fc43b2011-07-06 15:58:03 +08005341 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Francois Romieu7f796d832007-06-11 23:04:41 +02005344static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005345{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005346 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005347 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01005348 (InterFrameGap << TxInterFrameGapShift));
5349}
5350
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005351static void rtl_hw_start(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005353 tp->hw_start(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005354 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005355}
5356
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005357static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005358{
5359 /*
5360 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5361 * register to be written before TxDescAddrLow to work.
5362 * Switching from MMIO to I/O access fixes the issue as well.
5363 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005364 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
5365 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
5366 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
5367 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005368}
5369
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005370static u16 rtl_rw_cpluscmd(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005371{
5372 u16 cmd;
5373
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005374 cmd = RTL_R16(tp, CPlusCmd);
5375 RTL_W16(tp, CPlusCmd, cmd);
Francois Romieu7f796d832007-06-11 23:04:41 +02005376 return cmd;
5377}
5378
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005379static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02005380{
5381 /* Low hurts. Let's disable the filtering. */
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005382 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005383}
5384
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005385static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01005386{
Francois Romieu37441002011-06-17 22:58:54 +02005387 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005388 u32 mac_version;
5389 u32 clk;
5390 u32 val;
5391 } cfg2_info [] = {
5392 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5393 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5394 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5395 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005396 };
5397 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005398 unsigned int i;
5399 u32 clk;
5400
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005401 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005402 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005403 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005404 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01005405 break;
5406 }
5407 }
5408}
5409
Francois Romieue6b763e2012-03-08 09:35:39 +01005410static void rtl_set_rx_mode(struct net_device *dev)
5411{
5412 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01005413 u32 mc_filter[2]; /* Multicast hash filter */
5414 int rx_mode;
5415 u32 tmp = 0;
5416
5417 if (dev->flags & IFF_PROMISC) {
5418 /* Unconditionally log net taps. */
5419 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5420 rx_mode =
5421 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5422 AcceptAllPhys;
5423 mc_filter[1] = mc_filter[0] = 0xffffffff;
5424 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5425 (dev->flags & IFF_ALLMULTI)) {
5426 /* Too many to filter perfectly -- accept all multicasts. */
5427 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5428 mc_filter[1] = mc_filter[0] = 0xffffffff;
5429 } else {
5430 struct netdev_hw_addr *ha;
5431
5432 rx_mode = AcceptBroadcast | AcceptMyPhys;
5433 mc_filter[1] = mc_filter[0] = 0;
5434 netdev_for_each_mc_addr(ha, dev) {
5435 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5436 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5437 rx_mode |= AcceptMulticast;
5438 }
5439 }
5440
5441 if (dev->features & NETIF_F_RXALL)
5442 rx_mode |= (AcceptErr | AcceptRunt);
5443
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005444 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01005445
5446 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5447 u32 data = mc_filter[0];
5448
5449 mc_filter[0] = swab32(mc_filter[1]);
5450 mc_filter[1] = swab32(data);
5451 }
5452
Nathan Walp04817762012-11-01 12:08:47 +00005453 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5454 mc_filter[1] = mc_filter[0] = 0xffffffff;
5455
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005456 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
5457 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01005458
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005459 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01005460}
5461
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005462static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005463{
Francois Romieu9cb427b2006-11-02 00:10:16 +01005464 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005465 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) | PCIMulRW);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005466 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005467 }
5468
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005469 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005470 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5471 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5472 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5473 tp->mac_version == RTL_GIGA_MAC_VER_04)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005474 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005475
Hayes Wange542a222011-07-06 15:58:04 +08005476 rtl_init_rxcfg(tp);
5477
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005478 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005480 rtl_set_rx_max_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
Francois Romieucecb5fd2011-04-01 10:21:07 +02005482 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5483 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5484 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5485 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005486 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005488 tp->cp_cmd |= rtl_rw_cpluscmd(tp) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005489
Francois Romieucecb5fd2011-04-01 10:21:07 +02005490 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5491 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005492 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005494 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
5496
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005497 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005498
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005499 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01005500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 /*
5502 * Undocumented corner. Supposedly:
5503 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5504 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005505 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005507 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005508
Francois Romieucecb5fd2011-04-01 10:21:07 +02005509 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5510 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5511 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5512 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005513 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieuc946b302007-10-04 00:42:50 +02005514 rtl_set_rx_tx_config_registers(tp);
5515 }
5516
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005517 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005518
5519 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005520 RTL_R8(tp, IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005522 RTL_W32(tp, RxMissed, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005524 rtl_set_rx_mode(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
5526 /* no early-rx interrupts */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005527 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005528}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005530static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5531{
5532 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005533 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005534}
5535
5536static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5537{
Francois Romieu52989f02012-07-06 13:37:00 +02005538 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005539}
5540
5541static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005542{
5543 u32 csi;
5544
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005545 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5546 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005547}
5548
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005549static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005550{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005551 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005552}
5553
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005554static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005555{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005556 rtl_csi_access_enable(tp, 0x27000000);
5557}
5558
Francois Romieuffc46952012-07-06 14:19:23 +02005559DECLARE_RTL_COND(rtl_csiar_cond)
5560{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005561 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02005562}
5563
Francois Romieu52989f02012-07-06 13:37:00 +02005564static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005565{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005566 RTL_W32(tp, CSIDR, value);
5567 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005568 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5569
Francois Romieuffc46952012-07-06 14:19:23 +02005570 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005571}
5572
Francois Romieu52989f02012-07-06 13:37:00 +02005573static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005574{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005575 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) |
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005576 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5577
Francois Romieuffc46952012-07-06 14:19:23 +02005578 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005579 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005580}
5581
Francois Romieu52989f02012-07-06 13:37:00 +02005582static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005583{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005584 RTL_W32(tp, CSIDR, value);
5585 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005586 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5587 CSIAR_FUNC_NIC);
5588
Francois Romieuffc46952012-07-06 14:19:23 +02005589 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005590}
5591
Francois Romieu52989f02012-07-06 13:37:00 +02005592static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005593{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005594 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
Hayes Wang7e18dca2012-03-30 14:33:02 +08005595 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5596
Francois Romieuffc46952012-07-06 14:19:23 +02005597 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005598 RTL_R32(tp, CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005599}
5600
hayeswang45dd95c2013-07-08 17:09:01 +08005601static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5602{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005603 RTL_W32(tp, CSIDR, value);
5604 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
hayeswang45dd95c2013-07-08 17:09:01 +08005605 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5606 CSIAR_FUNC_NIC2);
5607
5608 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5609}
5610
5611static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5612{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005613 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
hayeswang45dd95c2013-07-08 17:09:01 +08005614 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5615
5616 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005617 RTL_R32(tp, CSIDR) : ~0;
hayeswang45dd95c2013-07-08 17:09:01 +08005618}
5619
Bill Pembertonbaf63292012-12-03 09:23:28 -05005620static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005621{
5622 struct csi_ops *ops = &tp->csi_ops;
5623
5624 switch (tp->mac_version) {
5625 case RTL_GIGA_MAC_VER_01:
5626 case RTL_GIGA_MAC_VER_02:
5627 case RTL_GIGA_MAC_VER_03:
5628 case RTL_GIGA_MAC_VER_04:
5629 case RTL_GIGA_MAC_VER_05:
5630 case RTL_GIGA_MAC_VER_06:
5631 case RTL_GIGA_MAC_VER_10:
5632 case RTL_GIGA_MAC_VER_11:
5633 case RTL_GIGA_MAC_VER_12:
5634 case RTL_GIGA_MAC_VER_13:
5635 case RTL_GIGA_MAC_VER_14:
5636 case RTL_GIGA_MAC_VER_15:
5637 case RTL_GIGA_MAC_VER_16:
5638 case RTL_GIGA_MAC_VER_17:
5639 ops->write = NULL;
5640 ops->read = NULL;
5641 break;
5642
Hayes Wang7e18dca2012-03-30 14:33:02 +08005643 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005644 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005645 ops->write = r8402_csi_write;
5646 ops->read = r8402_csi_read;
5647 break;
5648
hayeswang45dd95c2013-07-08 17:09:01 +08005649 case RTL_GIGA_MAC_VER_44:
5650 ops->write = r8411_csi_write;
5651 ops->read = r8411_csi_read;
5652 break;
5653
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005654 default:
5655 ops->write = r8169_csi_write;
5656 ops->read = r8169_csi_read;
5657 break;
5658 }
Francois Romieudacf8152008-08-02 20:44:13 +02005659}
5660
5661struct ephy_info {
5662 unsigned int offset;
5663 u16 mask;
5664 u16 bits;
5665};
5666
Francois Romieufdf6fc02012-07-06 22:40:38 +02005667static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5668 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005669{
5670 u16 w;
5671
5672 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005673 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5674 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005675 e++;
5676 }
5677}
5678
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005679static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02005680{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005681 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005682 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005683}
5684
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005685static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005686{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005687 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08005688 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005689}
5690
hayeswangb51ecea2014-07-09 14:52:51 +08005691static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5692{
hayeswangb51ecea2014-07-09 14:52:51 +08005693 u8 data;
5694
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005695 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08005696
5697 if (enable)
5698 data |= Rdy_to_L23;
5699 else
5700 data &= ~Rdy_to_L23;
5701
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005702 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08005703}
5704
Francois Romieub726e492008-06-28 12:22:59 +02005705#define R8168_CPCMD_QUIRK_MASK (\
5706 EnableBist | \
5707 Mac_dbgo_oe | \
5708 Force_half_dup | \
5709 Force_rxflow_en | \
5710 Force_txflow_en | \
5711 Cxpl_dbg_sel | \
5712 ASF | \
5713 PktCntrDisable | \
5714 Mac_dbgo_sel)
5715
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005716static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005717{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005718 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005719
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005720 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieub726e492008-06-28 12:22:59 +02005721
françois romieufaf1e782013-02-27 13:01:57 +00005722 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005723 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00005724 PCI_EXP_DEVCTL_NOSNOOP_EN);
5725 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005726}
5727
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005728static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005729{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005730 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005731
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005732 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005733
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005734 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005735}
5736
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005737static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005738{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005739 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02005740
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005741 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02005742
françois romieufaf1e782013-02-27 13:01:57 +00005743 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005744 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02005745
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005746 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005747
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005748 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005749}
5750
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005751static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005752{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005753 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005754 { 0x01, 0, 0x0001 },
5755 { 0x02, 0x0800, 0x1000 },
5756 { 0x03, 0, 0x0042 },
5757 { 0x06, 0x0080, 0x0000 },
5758 { 0x07, 0, 0x2000 }
5759 };
5760
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005761 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005762
Francois Romieufdf6fc02012-07-06 22:40:38 +02005763 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005764
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005765 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005766}
5767
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005768static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005769{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005770 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005771
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005772 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02005773
françois romieufaf1e782013-02-27 13:01:57 +00005774 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005775 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02005776
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005777 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieuef3386f2008-06-29 12:24:30 +02005778}
5779
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005780static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005781{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005782 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005783
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005784 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005785
5786 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005787 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005788
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005789 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005790
françois romieufaf1e782013-02-27 13:01:57 +00005791 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005792 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005793
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005794 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005795}
5796
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005797static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005798{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005799 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005800 { 0x02, 0x0800, 0x1000 },
5801 { 0x03, 0, 0x0002 },
5802 { 0x06, 0x0080, 0x0000 }
5803 };
5804
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005805 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005806
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005807 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02005808
Francois Romieufdf6fc02012-07-06 22:40:38 +02005809 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005810
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005811 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005812}
5813
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005814static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005815{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005816 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005817 { 0x01, 0, 0x0001 },
5818 { 0x03, 0x0400, 0x0220 }
5819 };
5820
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005821 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005822
Francois Romieufdf6fc02012-07-06 22:40:38 +02005823 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005824
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005825 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005826}
5827
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005828static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005829{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005830 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005831}
5832
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005833static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005834{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005835 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005836
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005837 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005838}
5839
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005840static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005841{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005842 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005843
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005844 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005845
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005846 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005847
françois romieufaf1e782013-02-27 13:01:57 +00005848 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005849 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02005850
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005851 RTL_W16(tp, CPlusCmd, RTL_R16(tp, CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu5b538df2008-07-20 16:22:45 +02005852}
5853
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005854static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005855{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005856 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005857
françois romieufaf1e782013-02-27 13:01:57 +00005858 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005859 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00005860
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005861 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00005862
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005863 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005864}
5865
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005866static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005867{
5868 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005869 { 0x0b, 0x0000, 0x0048 },
5870 { 0x19, 0x0020, 0x0050 },
5871 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00005872 };
françois romieue6de30d2011-01-03 15:08:37 +00005873
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005874 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005875
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005876 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00005877
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005878 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00005879
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08005880 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00005881
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005882 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005883}
5884
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005885static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005886{
Hayes Wang70090422011-07-06 15:58:06 +08005887 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005888 { 0x00, 0x0200, 0x0100 },
5889 { 0x00, 0x0000, 0x0004 },
5890 { 0x06, 0x0002, 0x0001 },
5891 { 0x06, 0x0000, 0x0030 },
5892 { 0x07, 0x0000, 0x2000 },
5893 { 0x00, 0x0000, 0x0020 },
5894 { 0x03, 0x5800, 0x2000 },
5895 { 0x03, 0x0000, 0x0001 },
5896 { 0x01, 0x0800, 0x1000 },
5897 { 0x07, 0x0000, 0x4000 },
5898 { 0x1e, 0x0000, 0x2000 },
5899 { 0x19, 0xffff, 0xfe6c },
5900 { 0x0a, 0x0000, 0x0040 }
5901 };
5902
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005903 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005904
Francois Romieufdf6fc02012-07-06 22:40:38 +02005905 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005906
françois romieufaf1e782013-02-27 13:01:57 +00005907 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005908 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005909
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005910 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005911
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005912 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005913
5914 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005915 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5916 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005917
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005918 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005919}
5920
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005921static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005922{
5923 static const struct ephy_info e_info_8168e_2[] = {
5924 { 0x09, 0x0000, 0x0080 },
5925 { 0x19, 0x0000, 0x0224 }
5926 };
5927
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005928 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005929
Francois Romieufdf6fc02012-07-06 22:40:38 +02005930 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005931
françois romieufaf1e782013-02-27 13:01:57 +00005932 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005933 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005934
Francois Romieufdf6fc02012-07-06 22:40:38 +02005935 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5936 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5937 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5938 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5939 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5940 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005941 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5942 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005943
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005944 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005945
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005946 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005947
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005948 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5949 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005950
5951 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005952 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005953
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005954 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5955 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5956 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005957}
5958
Hayes Wang5f886e02012-03-30 14:33:03 +08005959static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005960{
Hayes Wang5f886e02012-03-30 14:33:03 +08005961 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005962
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005963 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005964
Francois Romieufdf6fc02012-07-06 22:40:38 +02005965 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5966 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5967 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5968 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005969 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5970 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5971 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5972 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005973 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5974 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005975
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005976 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005977
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005978 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005979
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005980 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5981 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5982 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5983 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5984 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005985}
5986
Hayes Wang5f886e02012-03-30 14:33:03 +08005987static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5988{
Hayes Wang5f886e02012-03-30 14:33:03 +08005989 static const struct ephy_info e_info_8168f_1[] = {
5990 { 0x06, 0x00c0, 0x0020 },
5991 { 0x08, 0x0001, 0x0002 },
5992 { 0x09, 0x0000, 0x0080 },
5993 { 0x19, 0x0000, 0x0224 }
5994 };
5995
5996 rtl_hw_start_8168f(tp);
5997
Francois Romieufdf6fc02012-07-06 22:40:38 +02005998 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005999
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006000 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006001
6002 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006003 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08006004}
6005
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006006static void rtl_hw_start_8411(struct rtl8169_private *tp)
6007{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006008 static const struct ephy_info e_info_8168f_1[] = {
6009 { 0x06, 0x00c0, 0x0020 },
6010 { 0x0f, 0xffff, 0x5200 },
6011 { 0x1e, 0x0000, 0x4000 },
6012 { 0x19, 0x0000, 0x0224 }
6013 };
6014
6015 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006016 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006017
Francois Romieufdf6fc02012-07-06 22:40:38 +02006018 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006019
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006020 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006021}
6022
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006023static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006024{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006025 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00006026
Hayes Wangc5583862012-07-02 17:23:22 +08006027 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6028 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6029 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6030 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6031
6032 rtl_csi_access_enable_1(tp);
6033
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006034 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08006035
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006036 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6037 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006038 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006039
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006040 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6041 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08006042
6043 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6044 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6045
6046 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006047 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08006048
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006049 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6050 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006051
6052 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006053}
6054
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006055static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6056{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006057 static const struct ephy_info e_info_8168g_1[] = {
6058 { 0x00, 0x0000, 0x0008 },
6059 { 0x0c, 0x37d0, 0x0820 },
6060 { 0x1e, 0x0000, 0x0001 },
6061 { 0x19, 0x8000, 0x0000 }
6062 };
6063
6064 rtl_hw_start_8168g(tp);
6065
6066 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006067 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6068 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006069 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6070}
6071
hayeswang57538c42013-04-01 22:23:40 +00006072static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6073{
hayeswang57538c42013-04-01 22:23:40 +00006074 static const struct ephy_info e_info_8168g_2[] = {
6075 { 0x00, 0x0000, 0x0008 },
6076 { 0x0c, 0x3df0, 0x0200 },
6077 { 0x19, 0xffff, 0xfc00 },
6078 { 0x1e, 0xffff, 0x20eb }
6079 };
6080
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006081 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006082
6083 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006084 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6085 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00006086 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6087}
6088
hayeswang45dd95c2013-07-08 17:09:01 +08006089static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6090{
hayeswang45dd95c2013-07-08 17:09:01 +08006091 static const struct ephy_info e_info_8411_2[] = {
6092 { 0x00, 0x0000, 0x0008 },
6093 { 0x0c, 0x3df0, 0x0200 },
6094 { 0x0f, 0xffff, 0x5200 },
6095 { 0x19, 0x0020, 0x0000 },
6096 { 0x1e, 0x0000, 0x2000 }
6097 };
6098
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006099 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006100
6101 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006102 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6103 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang45dd95c2013-07-08 17:09:01 +08006104 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6105}
6106
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006107static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6108{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006109 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006110 u32 data;
6111 static const struct ephy_info e_info_8168h_1[] = {
6112 { 0x1e, 0x0800, 0x0001 },
6113 { 0x1d, 0x0000, 0x0800 },
6114 { 0x05, 0xffff, 0x2089 },
6115 { 0x06, 0xffff, 0x5881 },
6116 { 0x04, 0xffff, 0x154a },
6117 { 0x01, 0xffff, 0x068b }
6118 };
6119
6120 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006121 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6122 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006123 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6124
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006125 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006126
6127 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6128 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6129 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6130 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6131
6132 rtl_csi_access_enable_1(tp);
6133
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006134 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006135
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006136 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6137 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006138
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006139 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006140
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006141 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006142
6143 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6144
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006145 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6146 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006147
6148 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6149 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6150
6151 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006152 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006153
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006154 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6155 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006156
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006157 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006158
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006159 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006160
6161 rtl_pcie_state_l2l3_enable(tp, false);
6162
6163 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006164 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006165 rtl_writephy(tp, 0x1f, 0x0000);
6166 if (rg_saw_cnt > 0) {
6167 u16 sw_cnt_1ms_ini;
6168
6169 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6170 sw_cnt_1ms_ini &= 0x0fff;
6171 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006172 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006173 data |= sw_cnt_1ms_ini;
6174 r8168_mac_ocp_write(tp, 0xd412, data);
6175 }
6176
6177 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006178 data &= ~0xf0;
6179 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006180 r8168_mac_ocp_write(tp, 0xe056, data);
6181
6182 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006183 data &= ~0x6000;
6184 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006185 r8168_mac_ocp_write(tp, 0xe052, data);
6186
6187 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006188 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006189 data |= 0x017f;
6190 r8168_mac_ocp_write(tp, 0xe0d6, data);
6191
6192 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006193 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006194 data |= 0x047f;
6195 r8168_mac_ocp_write(tp, 0xd420, data);
6196
6197 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6198 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6199 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6200 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6201}
6202
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006203static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6204{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006205 rtl8168ep_stop_cmac(tp);
6206
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006207 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006208
6209 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6210 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6211 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6212 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6213
6214 rtl_csi_access_enable_1(tp);
6215
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006216 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006217
6218 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6219 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6220
6221 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6222
6223 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6224
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006225 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
6226 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006227
6228 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6229 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6230
6231 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006232 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006233
6234 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6235
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006236 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006237
6238 rtl_pcie_state_l2l3_enable(tp, false);
6239}
6240
6241static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6242{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006243 static const struct ephy_info e_info_8168ep_1[] = {
6244 { 0x00, 0xffff, 0x10ab },
6245 { 0x06, 0xffff, 0xf030 },
6246 { 0x08, 0xffff, 0x2006 },
6247 { 0x0d, 0xffff, 0x1666 },
6248 { 0x0c, 0x3ff0, 0x0000 }
6249 };
6250
6251 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006252 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6253 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006254 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6255
6256 rtl_hw_start_8168ep(tp);
6257}
6258
6259static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6260{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006261 static const struct ephy_info e_info_8168ep_2[] = {
6262 { 0x00, 0xffff, 0x10a3 },
6263 { 0x19, 0xffff, 0xfc00 },
6264 { 0x1e, 0xffff, 0x20ea }
6265 };
6266
6267 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006268 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6269 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006270 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6271
6272 rtl_hw_start_8168ep(tp);
6273
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006274 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6275 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006276}
6277
6278static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6279{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006280 u32 data;
6281 static const struct ephy_info e_info_8168ep_3[] = {
6282 { 0x00, 0xffff, 0x10a3 },
6283 { 0x19, 0xffff, 0x7c00 },
6284 { 0x1e, 0xffff, 0x20eb },
6285 { 0x0d, 0xffff, 0x1666 }
6286 };
6287
6288 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006289 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
6290 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006291 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6292
6293 rtl_hw_start_8168ep(tp);
6294
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006295 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
6296 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006297
6298 data = r8168_mac_ocp_read(tp, 0xd3e2);
6299 data &= 0xf000;
6300 data |= 0x0271;
6301 r8168_mac_ocp_write(tp, 0xd3e2, data);
6302
6303 data = r8168_mac_ocp_read(tp, 0xd3e4);
6304 data &= 0xff00;
6305 r8168_mac_ocp_write(tp, 0xd3e4, data);
6306
6307 data = r8168_mac_ocp_read(tp, 0xe860);
6308 data |= 0x0080;
6309 r8168_mac_ocp_write(tp, 0xe860, data);
6310}
6311
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006312static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006313{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006314 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu2dd99532007-06-11 23:22:52 +02006315
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006316 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006317
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006318 rtl_set_rx_max_size(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006319
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006320 tp->cp_cmd |= RTL_R16(tp, CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006321
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006322 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02006323
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006324 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01006325
6326 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006327 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006328 tp->event_slow |= RxFIFOOver | PCSTimeout;
6329 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006330 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006331
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006332 rtl_set_rx_tx_desc_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006333
hayeswang1a964642013-04-01 22:23:41 +00006334 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006335
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006336 RTL_R8(tp, IntrMask);
Francois Romieu2dd99532007-06-11 23:22:52 +02006337
Francois Romieu219a1e92008-06-28 11:58:39 +02006338 switch (tp->mac_version) {
6339 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006340 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006341 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006342
6343 case RTL_GIGA_MAC_VER_12:
6344 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006345 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006346 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006347
6348 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006349 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006350 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006351
6352 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006353 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006354 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006355
6356 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006357 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006358 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006359
Francois Romieu197ff762008-06-28 13:16:02 +02006360 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006361 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006362 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006363
Francois Romieu6fb07052008-06-29 11:54:28 +02006364 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006365 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006366 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006367
Francois Romieuef3386f2008-06-29 12:24:30 +02006368 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006369 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006370 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006371
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006372 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006373 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006374 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006375
Francois Romieu5b538df2008-07-20 16:22:45 +02006376 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006377 case RTL_GIGA_MAC_VER_26:
6378 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006379 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006380 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006381
françois romieue6de30d2011-01-03 15:08:37 +00006382 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006383 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006384 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006385
hayeswang4804b3b2011-03-21 01:50:29 +00006386 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006387 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006388 break;
6389
hayeswang01dc7fe2011-03-21 01:50:28 +00006390 case RTL_GIGA_MAC_VER_32:
6391 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006392 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006393 break;
6394 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006395 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006396 break;
françois romieue6de30d2011-01-03 15:08:37 +00006397
Hayes Wangc2218922011-09-06 16:55:18 +08006398 case RTL_GIGA_MAC_VER_35:
6399 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006400 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006401 break;
6402
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006403 case RTL_GIGA_MAC_VER_38:
6404 rtl_hw_start_8411(tp);
6405 break;
6406
Hayes Wangc5583862012-07-02 17:23:22 +08006407 case RTL_GIGA_MAC_VER_40:
6408 case RTL_GIGA_MAC_VER_41:
6409 rtl_hw_start_8168g_1(tp);
6410 break;
hayeswang57538c42013-04-01 22:23:40 +00006411 case RTL_GIGA_MAC_VER_42:
6412 rtl_hw_start_8168g_2(tp);
6413 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006414
hayeswang45dd95c2013-07-08 17:09:01 +08006415 case RTL_GIGA_MAC_VER_44:
6416 rtl_hw_start_8411_2(tp);
6417 break;
6418
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006419 case RTL_GIGA_MAC_VER_45:
6420 case RTL_GIGA_MAC_VER_46:
6421 rtl_hw_start_8168h_1(tp);
6422 break;
6423
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006424 case RTL_GIGA_MAC_VER_49:
6425 rtl_hw_start_8168ep_1(tp);
6426 break;
6427
6428 case RTL_GIGA_MAC_VER_50:
6429 rtl_hw_start_8168ep_2(tp);
6430 break;
6431
6432 case RTL_GIGA_MAC_VER_51:
6433 rtl_hw_start_8168ep_3(tp);
6434 break;
6435
Francois Romieu219a1e92008-06-28 11:58:39 +02006436 default:
6437 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006438 tp->dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006439 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006440 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006441
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006442 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
hayeswang1a964642013-04-01 22:23:41 +00006443
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006444 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu0e485152007-02-20 00:00:26 +01006445
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006446 rtl_set_rx_mode(tp->dev);
Francois Romieub8363902008-06-01 12:31:57 +02006447
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006448 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006449}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
Francois Romieu2857ffb2008-08-02 21:08:49 +02006451#define R810X_CPCMD_QUIRK_MASK (\
6452 EnableBist | \
6453 Mac_dbgo_oe | \
6454 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006455 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006456 Force_txflow_en | \
6457 Cxpl_dbg_sel | \
6458 ASF | \
6459 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006460 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006461
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006462static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006463{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006464 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006465 { 0x01, 0, 0x6e65 },
6466 { 0x02, 0, 0x091f },
6467 { 0x03, 0, 0xc2f9 },
6468 { 0x06, 0, 0xafb5 },
6469 { 0x07, 0, 0x0e00 },
6470 { 0x19, 0, 0xec80 },
6471 { 0x01, 0, 0x2e65 },
6472 { 0x01, 0, 0x6e65 }
6473 };
6474 u8 cfg1;
6475
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006476 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006477
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006478 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006479
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006480 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006481
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006482 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02006483 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006484 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006485
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006486 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006487 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006488 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006489
Francois Romieufdf6fc02012-07-06 22:40:38 +02006490 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006491}
6492
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006493static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006494{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006495 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006496
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006497 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006498
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006499 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
6500 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006501}
6502
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006503static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006504{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006505 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006506
Francois Romieufdf6fc02012-07-06 22:40:38 +02006507 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006508}
6509
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006510static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006511{
6512 static const struct ephy_info e_info_8105e_1[] = {
6513 { 0x07, 0, 0x4000 },
6514 { 0x19, 0, 0x0200 },
6515 { 0x19, 0, 0x0020 },
6516 { 0x1e, 0, 0x2000 },
6517 { 0x03, 0, 0x0001 },
6518 { 0x19, 0, 0x0100 },
6519 { 0x19, 0, 0x0004 },
6520 { 0x0a, 0, 0x0020 }
6521 };
6522
Francois Romieucecb5fd2011-04-01 10:21:07 +02006523 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006524 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006525
Francois Romieucecb5fd2011-04-01 10:21:07 +02006526 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006527 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006528
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006529 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6530 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006531
Francois Romieufdf6fc02012-07-06 22:40:38 +02006532 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006533
6534 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006535}
6536
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006537static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006538{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006539 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006540 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006541}
6542
Hayes Wang7e18dca2012-03-30 14:33:02 +08006543static void rtl_hw_start_8402(struct rtl8169_private *tp)
6544{
Hayes Wang7e18dca2012-03-30 14:33:02 +08006545 static const struct ephy_info e_info_8402[] = {
6546 { 0x19, 0xffff, 0xff64 },
6547 { 0x1e, 0, 0x4000 }
6548 };
6549
6550 rtl_csi_access_enable_2(tp);
6551
6552 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006553 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006554
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006555 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
6556 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006557
Francois Romieufdf6fc02012-07-06 22:40:38 +02006558 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006559
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02006560 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006561
Francois Romieufdf6fc02012-07-06 22:40:38 +02006562 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6563 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006564 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6565 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006566 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6567 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006568 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006569
6570 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006571}
6572
Hayes Wang5598bfe2012-07-02 17:23:21 +08006573static void rtl_hw_start_8106(struct rtl8169_private *tp)
6574{
Hayes Wang5598bfe2012-07-02 17:23:21 +08006575 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006576 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006577
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006578 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
6579 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
6580 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006581
6582 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006583}
6584
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006585static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01006586{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006587 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6588 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006589
Francois Romieucecb5fd2011-04-01 10:21:07 +02006590 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006591 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006592 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006593 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006594
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006595 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006596
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006597 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00006598
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006599 rtl_set_rx_max_size(tp);
hayeswang1a964642013-04-01 22:23:41 +00006600
6601 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006602 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00006603
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006604 rtl_set_rx_tx_desc_registers(tp);
hayeswang1a964642013-04-01 22:23:41 +00006605
6606 rtl_set_rx_tx_config_registers(tp);
6607
Francois Romieu2857ffb2008-08-02 21:08:49 +02006608 switch (tp->mac_version) {
6609 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006610 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006611 break;
6612
6613 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006614 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006615 break;
6616
6617 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006618 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006619 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006620
6621 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006622 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006623 break;
6624 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006625 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006626 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006627
6628 case RTL_GIGA_MAC_VER_37:
6629 rtl_hw_start_8402(tp);
6630 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006631
6632 case RTL_GIGA_MAC_VER_39:
6633 rtl_hw_start_8106(tp);
6634 break;
hayeswang58152cd2013-04-01 22:23:42 +00006635 case RTL_GIGA_MAC_VER_43:
6636 rtl_hw_start_8168g_2(tp);
6637 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006638 case RTL_GIGA_MAC_VER_47:
6639 case RTL_GIGA_MAC_VER_48:
6640 rtl_hw_start_8168h_1(tp);
6641 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006642 }
6643
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006644 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006645
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006646 RTL_W16(tp, IntrMitigate, 0x0000);
Francois Romieucdf1a602007-06-11 23:29:50 +02006647
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006648 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006649
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006650 rtl_set_rx_mode(tp->dev);
Francois Romieucdf1a602007-06-11 23:29:50 +02006651
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006652 RTL_R8(tp, IntrMask);
hayeswang1a964642013-04-01 22:23:41 +00006653
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006654 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655}
6656
6657static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6658{
Francois Romieud58d46b2011-05-03 16:38:29 +02006659 struct rtl8169_private *tp = netdev_priv(dev);
6660
Francois Romieud58d46b2011-05-03 16:38:29 +02006661 if (new_mtu > ETH_DATA_LEN)
6662 rtl_hw_jumbo_enable(tp);
6663 else
6664 rtl_hw_jumbo_disable(tp);
6665
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006667 netdev_update_features(dev);
6668
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670}
6671
6672static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6673{
Al Viro95e09182007-12-22 18:55:39 +00006674 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6676}
6677
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006678static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6679 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006681 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
6682 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006683
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006684 kfree(*data_buff);
6685 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 rtl8169_make_unusable_by_asic(desc);
6687}
6688
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006689static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690{
6691 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6692
Alexander Duycka0750132014-12-11 15:02:17 -08006693 /* Force memory writes to complete before releasing descriptor */
6694 dma_wmb();
6695
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006696 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697}
6698
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006699static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006701 return (void *)ALIGN((long)data, 16);
6702}
6703
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006704static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6705 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006706{
6707 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006709 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02006710 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006712 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006713 if (!data)
6714 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006715
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006716 if (rtl8169_align(data) != data) {
6717 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006718 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006719 if (!data)
6720 return NULL;
6721 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006722
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006723 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006724 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006725 if (unlikely(dma_mapping_error(d, mapping))) {
6726 if (net_ratelimit())
6727 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006728 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Heiner Kallweitd731af72018-04-17 23:26:41 +02006731 desc->addr = cpu_to_le64(mapping);
6732 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006733 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006734
6735err_out:
6736 kfree(data);
6737 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738}
6739
6740static void rtl8169_rx_clear(struct rtl8169_private *tp)
6741{
Francois Romieu07d3f512007-02-21 22:40:46 +01006742 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
6744 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006745 if (tp->Rx_databuff[i]) {
6746 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 tp->RxDescArray + i);
6748 }
6749 }
6750}
6751
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006752static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006754 desc->opts1 |= cpu_to_le32(RingEnd);
6755}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006756
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006757static int rtl8169_rx_fill(struct rtl8169_private *tp)
6758{
6759 unsigned int i;
6760
6761 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006762 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006763
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006764 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006765 if (!data) {
6766 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006767 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006768 }
6769 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006772 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6773 return 0;
6774
6775err_out:
6776 rtl8169_rx_clear(tp);
6777 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778}
6779
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006780static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 rtl8169_init_ring_indexes(tp);
6783
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006784 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
6785 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006787 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788}
6789
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006790static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 struct TxDesc *desc)
6792{
6793 unsigned int len = tx_skb->len;
6794
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006795 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6796
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 desc->opts1 = 0x00;
6798 desc->opts2 = 0x00;
6799 desc->addr = 0x00;
6800 tx_skb->len = 0;
6801}
6802
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006803static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6804 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805{
6806 unsigned int i;
6807
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006808 for (i = 0; i < n; i++) {
6809 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 struct ring_info *tx_skb = tp->tx_skb + entry;
6811 unsigned int len = tx_skb->len;
6812
6813 if (len) {
6814 struct sk_buff *skb = tx_skb->skb;
6815
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006816 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 tp->TxDescArray + entry);
6818 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006819 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 tx_skb->skb = NULL;
6821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 }
6823 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006824}
6825
6826static void rtl8169_tx_clear(struct rtl8169_private *tp)
6827{
6828 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 tp->cur_tx = tp->dirty_tx = 0;
6830}
6831
Francois Romieu4422bcd2012-01-26 11:23:32 +01006832static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833{
David Howellsc4028952006-11-22 14:57:56 +00006834 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006835 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
Francois Romieuda78dbf2012-01-26 14:18:23 +01006837 napi_disable(&tp->napi);
6838 netif_stop_queue(dev);
6839 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
françois romieuc7c2c392011-12-04 20:30:52 +00006841 rtl8169_hw_reset(tp);
6842
Francois Romieu56de4142011-03-15 17:29:31 +01006843 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006844 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01006845
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006847 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Francois Romieuda78dbf2012-01-26 14:18:23 +01006849 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006850 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01006851 netif_wake_queue(dev);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006852 rtl8169_check_link_status(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853}
6854
6855static void rtl8169_tx_timeout(struct net_device *dev)
6856{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006857 struct rtl8169_private *tp = netdev_priv(dev);
6858
6859 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
6862static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006863 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864{
6865 struct skb_shared_info *info = skb_shinfo(skb);
6866 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006867 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006868 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
6870 entry = tp->cur_tx;
6871 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006872 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 dma_addr_t mapping;
6874 u32 status, len;
6875 void *addr;
6876
6877 entry = (entry + 1) % NUM_TX_DESC;
6878
6879 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006880 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006881 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006882 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006883 if (unlikely(dma_mapping_error(d, mapping))) {
6884 if (net_ratelimit())
6885 netif_err(tp, drv, tp->dev,
6886 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006887 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
Francois Romieucecb5fd2011-04-01 10:21:07 +02006890 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006891 status = opts[0] | len |
6892 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
6894 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006895 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 txd->addr = cpu_to_le64(mapping);
6897
6898 tp->tx_skb[entry].len = len;
6899 }
6900
6901 if (cur_frag) {
6902 tp->tx_skb[entry].skb = skb;
6903 txd->opts1 |= cpu_to_le32(LastFrag);
6904 }
6905
6906 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006907
6908err_out:
6909 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6910 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911}
6912
françois romieub423e9a2013-05-18 01:24:46 +00006913static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6914{
6915 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6916}
6917
hayeswange9746042014-07-11 16:25:58 +08006918static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6919 struct net_device *dev);
6920/* r8169_csum_workaround()
6921 * The hw limites the value the transport offset. When the offset is out of the
6922 * range, calculate the checksum by sw.
6923 */
6924static void r8169_csum_workaround(struct rtl8169_private *tp,
6925 struct sk_buff *skb)
6926{
6927 if (skb_shinfo(skb)->gso_size) {
6928 netdev_features_t features = tp->dev->features;
6929 struct sk_buff *segs, *nskb;
6930
6931 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6932 segs = skb_gso_segment(skb, features);
6933 if (IS_ERR(segs) || !segs)
6934 goto drop;
6935
6936 do {
6937 nskb = segs;
6938 segs = segs->next;
6939 nskb->next = NULL;
6940 rtl8169_start_xmit(nskb, tp->dev);
6941 } while (segs);
6942
Alexander Duyckeb781392015-05-01 10:34:44 -07006943 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006944 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6945 if (skb_checksum_help(skb) < 0)
6946 goto drop;
6947
6948 rtl8169_start_xmit(skb, tp->dev);
6949 } else {
6950 struct net_device_stats *stats;
6951
6952drop:
6953 stats = &tp->dev->stats;
6954 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006955 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006956 }
6957}
6958
6959/* msdn_giant_send_check()
6960 * According to the document of microsoft, the TCP Pseudo Header excludes the
6961 * packet length for IPv6 TCP large packets.
6962 */
6963static int msdn_giant_send_check(struct sk_buff *skb)
6964{
6965 const struct ipv6hdr *ipv6h;
6966 struct tcphdr *th;
6967 int ret;
6968
6969 ret = skb_cow_head(skb, 0);
6970 if (ret)
6971 return ret;
6972
6973 ipv6h = ipv6_hdr(skb);
6974 th = tcp_hdr(skb);
6975
6976 th->check = 0;
6977 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6978
6979 return ret;
6980}
6981
6982static inline __be16 get_protocol(struct sk_buff *skb)
6983{
6984 __be16 protocol;
6985
6986 if (skb->protocol == htons(ETH_P_8021Q))
6987 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6988 else
6989 protocol = skb->protocol;
6990
6991 return protocol;
6992}
6993
hayeswang5888d3f2014-07-11 16:25:56 +08006994static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6995 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996{
Michał Mirosław350fb322011-04-08 06:35:56 +00006997 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
Francois Romieu2b7b4312011-04-18 22:53:24 -07006999 if (mss) {
7000 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007001 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7002 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7003 const struct iphdr *ip = ip_hdr(skb);
7004
7005 if (ip->protocol == IPPROTO_TCP)
7006 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7007 else if (ip->protocol == IPPROTO_UDP)
7008 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7009 else
7010 WARN_ON_ONCE(1);
7011 }
7012
7013 return true;
7014}
7015
7016static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7017 struct sk_buff *skb, u32 *opts)
7018{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007019 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007020 u32 mss = skb_shinfo(skb)->gso_size;
7021
7022 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007023 if (transport_offset > GTTCPHO_MAX) {
7024 netif_warn(tp, tx_err, tp->dev,
7025 "Invalid transport offset 0x%x for TSO\n",
7026 transport_offset);
7027 return false;
7028 }
7029
7030 switch (get_protocol(skb)) {
7031 case htons(ETH_P_IP):
7032 opts[0] |= TD1_GTSENV4;
7033 break;
7034
7035 case htons(ETH_P_IPV6):
7036 if (msdn_giant_send_check(skb))
7037 return false;
7038
7039 opts[0] |= TD1_GTSENV6;
7040 break;
7041
7042 default:
7043 WARN_ON_ONCE(1);
7044 break;
7045 }
7046
hayeswangbdfa4ed2014-07-11 16:25:57 +08007047 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007048 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007049 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007050 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
françois romieub423e9a2013-05-18 01:24:46 +00007052 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007053 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007054
hayeswange9746042014-07-11 16:25:58 +08007055 if (transport_offset > TCPHO_MAX) {
7056 netif_warn(tp, tx_err, tp->dev,
7057 "Invalid transport offset 0x%x\n",
7058 transport_offset);
7059 return false;
7060 }
7061
7062 switch (get_protocol(skb)) {
7063 case htons(ETH_P_IP):
7064 opts[1] |= TD1_IPv4_CS;
7065 ip_protocol = ip_hdr(skb)->protocol;
7066 break;
7067
7068 case htons(ETH_P_IPV6):
7069 opts[1] |= TD1_IPv6_CS;
7070 ip_protocol = ipv6_hdr(skb)->nexthdr;
7071 break;
7072
7073 default:
7074 ip_protocol = IPPROTO_RAW;
7075 break;
7076 }
7077
7078 if (ip_protocol == IPPROTO_TCP)
7079 opts[1] |= TD1_TCP_CS;
7080 else if (ip_protocol == IPPROTO_UDP)
7081 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007082 else
7083 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007084
7085 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007086 } else {
7087 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007088 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 }
hayeswang5888d3f2014-07-11 16:25:56 +08007090
françois romieub423e9a2013-05-18 01:24:46 +00007091 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092}
7093
Stephen Hemminger613573252009-08-31 19:50:58 +00007094static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7095 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096{
7097 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007098 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007100 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 dma_addr_t mapping;
7102 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007103 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007104 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007105
Julien Ducourthial477206a2012-05-09 00:00:06 +02007106 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007107 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007108 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 }
7110
7111 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007112 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113
françois romieub423e9a2013-05-18 01:24:46 +00007114 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7115 opts[0] = DescOwn;
7116
hayeswange9746042014-07-11 16:25:58 +08007117 if (!tp->tso_csum(tp, skb, opts)) {
7118 r8169_csum_workaround(tp, skb);
7119 return NETDEV_TX_OK;
7120 }
françois romieub423e9a2013-05-18 01:24:46 +00007121
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007122 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007123 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007124 if (unlikely(dma_mapping_error(d, mapping))) {
7125 if (net_ratelimit())
7126 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007127 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
7130 tp->tx_skb[entry].len = len;
7131 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
Francois Romieu2b7b4312011-04-18 22:53:24 -07007133 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007134 if (frags < 0)
7135 goto err_dma_1;
7136 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007137 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007138 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007139 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007140 tp->tx_skb[entry].skb = skb;
7141 }
7142
Francois Romieu2b7b4312011-04-18 22:53:24 -07007143 txd->opts2 = cpu_to_le32(opts[1]);
7144
Richard Cochran5047fb52012-03-10 07:29:42 +00007145 skb_tx_timestamp(skb);
7146
Alexander Duycka0750132014-12-11 15:02:17 -08007147 /* Force memory writes to complete before releasing descriptor */
7148 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Francois Romieucecb5fd2011-04-01 10:21:07 +02007150 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007151 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 txd->opts1 = cpu_to_le32(status);
7153
Alexander Duycka0750132014-12-11 15:02:17 -08007154 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007155 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
Alexander Duycka0750132014-12-11 15:02:17 -08007157 tp->cur_tx += frags + 1;
7158
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007159 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160
David S. Miller87cda7c2015-02-22 15:54:29 -05007161 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007162
David S. Miller87cda7c2015-02-22 15:54:29 -05007163 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007164 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7165 * not miss a ring update when it notices a stopped queue.
7166 */
7167 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007169 /* Sync with rtl_tx:
7170 * - publish queue status and cur_tx ring index (write barrier)
7171 * - refresh dirty_tx ring index (read barrier).
7172 * May the current thread have a pessimistic view of the ring
7173 * status and forget to wake up queue, a racing rtl_tx thread
7174 * can't.
7175 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007176 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007177 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 netif_wake_queue(dev);
7179 }
7180
Stephen Hemminger613573252009-08-31 19:50:58 +00007181 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007183err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007184 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007185err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007186 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007187 dev->stats.tx_dropped++;
7188 return NETDEV_TX_OK;
7189
7190err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007192 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007193 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194}
7195
7196static void rtl8169_pcierr_interrupt(struct net_device *dev)
7197{
7198 struct rtl8169_private *tp = netdev_priv(dev);
7199 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 u16 pci_status, pci_cmd;
7201
7202 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7203 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7204
Joe Perchesbf82c182010-02-09 11:49:50 +00007205 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7206 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207
7208 /*
7209 * The recovery sequence below admits a very elaborated explanation:
7210 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007211 * - I did not see what else could be done;
7212 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 *
7214 * Feel free to adjust to your needs.
7215 */
Francois Romieua27993f2006-12-18 00:04:19 +01007216 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007217 pci_cmd &= ~PCI_COMMAND_PARITY;
7218 else
7219 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7220
7221 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
7223 pci_write_config_word(pdev, PCI_STATUS,
7224 pci_status & (PCI_STATUS_DETECTED_PARITY |
7225 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7226 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7227
7228 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007229 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007230 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007232 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 }
7235
françois romieue6de30d2011-01-03 15:08:37 +00007236 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007237
Francois Romieu98ddf982012-01-31 10:47:34 +01007238 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239}
7240
Francois Romieuda78dbf2012-01-26 14:18:23 +01007241static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242{
7243 unsigned int dirty_tx, tx_left;
7244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 dirty_tx = tp->dirty_tx;
7246 smp_rmb();
7247 tx_left = tp->cur_tx - dirty_tx;
7248
7249 while (tx_left > 0) {
7250 unsigned int entry = dirty_tx % NUM_TX_DESC;
7251 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 u32 status;
7253
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7255 if (status & DescOwn)
7256 break;
7257
Alexander Duycka0750132014-12-11 15:02:17 -08007258 /* This barrier is needed to keep us from reading
7259 * any other fields out of the Tx descriptor until
7260 * we know the status of DescOwn
7261 */
7262 dma_rmb();
7263
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007264 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007265 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007267 u64_stats_update_begin(&tp->tx_stats.syncp);
7268 tp->tx_stats.packets++;
7269 tp->tx_stats.bytes += tx_skb->skb->len;
7270 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007271 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 tx_skb->skb = NULL;
7273 }
7274 dirty_tx++;
7275 tx_left--;
7276 }
7277
7278 if (tp->dirty_tx != dirty_tx) {
7279 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007280 /* Sync with rtl8169_start_xmit:
7281 * - publish dirty_tx ring index (write barrier)
7282 * - refresh cur_tx ring index and queue status (read barrier)
7283 * May the current thread miss the stopped queue condition,
7284 * a racing xmit thread can only have a right view of the
7285 * ring status.
7286 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007287 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007289 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 netif_wake_queue(dev);
7291 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007292 /*
7293 * 8168 hack: TxPoll requests are lost when the Tx packets are
7294 * too close. Let's kick an extra TxPoll request when a burst
7295 * of start_xmit activity is detected (if it is not detected,
7296 * it is slow enough). -- FR
7297 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007298 if (tp->cur_tx != dirty_tx)
7299 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 }
7301}
7302
Francois Romieu126fa4b2005-05-12 20:09:17 -04007303static inline int rtl8169_fragmented_frame(u32 status)
7304{
7305 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7306}
7307
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007308static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 u32 status = opts1 & RxProtoMask;
7311
7312 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007313 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 skb->ip_summed = CHECKSUM_UNNECESSARY;
7315 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007316 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317}
7318
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007319static struct sk_buff *rtl8169_try_rx_copy(void *data,
7320 struct rtl8169_private *tp,
7321 int pkt_size,
7322 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007324 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01007325 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007327 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007328 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007329 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007330 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007331 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02007332 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007333 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7334
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007335 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
7337
Francois Romieuda78dbf2012-01-26 14:18:23 +01007338static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339{
7340 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007341 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344
Timo Teräs9fba0812013-01-15 21:01:24 +00007345 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007347 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 u32 status;
7349
David S. Miller8decf862011-09-22 03:23:13 -04007350 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 if (status & DescOwn)
7352 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007353
7354 /* This barrier is needed to keep us from reading
7355 * any other fields out of the Rx descriptor until
7356 * we know the status of DescOwn
7357 */
7358 dma_rmb();
7359
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007360 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007361 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7362 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007363 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007365 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007367 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007368 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007369 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007370 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007371 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007372 if ((status & (RxRUNT | RxCRC)) &&
7373 !(status & (RxRWT | RxFOVF)) &&
7374 (dev->features & NETIF_F_RXALL))
7375 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007377 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007378 dma_addr_t addr;
7379 int pkt_size;
7380
7381process_pkt:
7382 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007383 if (likely(!(dev->features & NETIF_F_RXFCS)))
7384 pkt_size = (status & 0x00003fff) - 4;
7385 else
7386 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Francois Romieu126fa4b2005-05-12 20:09:17 -04007388 /*
7389 * The driver does not support incoming fragmented
7390 * frames. They are seen as a symptom of over-mtu
7391 * sized frames.
7392 */
7393 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007394 dev->stats.rx_dropped++;
7395 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007396 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007397 }
7398
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007399 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7400 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007401 if (!skb) {
7402 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007403 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
7405
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007406 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 skb_put(skb, pkt_size);
7408 skb->protocol = eth_type_trans(skb, dev);
7409
Francois Romieu7a8fc772011-03-01 17:18:33 +01007410 rtl8169_rx_vlan_tag(desc, skb);
7411
françois romieu39174292015-11-11 23:35:18 +01007412 if (skb->pkt_type == PACKET_MULTICAST)
7413 dev->stats.multicast++;
7414
Francois Romieu56de4142011-03-15 17:29:31 +01007415 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
Junchang Wang8027aa22012-03-04 23:30:32 +01007417 u64_stats_update_begin(&tp->rx_stats.syncp);
7418 tp->rx_stats.packets++;
7419 tp->rx_stats.bytes += pkt_size;
7420 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 }
françois romieuce11ff52013-01-24 13:30:06 +00007422release_descriptor:
7423 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02007424 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 }
7426
7427 count = cur_rx - tp->cur_rx;
7428 tp->cur_rx = cur_rx;
7429
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 return count;
7431}
7432
Francois Romieu07d3f512007-02-21 22:40:46 +01007433static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434{
Francois Romieu07d3f512007-02-21 22:40:46 +01007435 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007438 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007440 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007441 if (status && status != 0xffff) {
7442 status &= RTL_EVENT_NAPI | tp->event_slow;
7443 if (status) {
7444 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007445
Francois Romieuda78dbf2012-01-26 14:18:23 +01007446 rtl_irq_disable(tp);
Heiner Kallweit9a899a32018-04-17 23:21:01 +02007447 napi_schedule_irqoff(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450 return IRQ_RETVAL(handled);
7451}
7452
Francois Romieuda78dbf2012-01-26 14:18:23 +01007453/*
7454 * Workqueue context.
7455 */
7456static void rtl_slow_event_work(struct rtl8169_private *tp)
7457{
7458 struct net_device *dev = tp->dev;
7459 u16 status;
7460
7461 status = rtl_get_events(tp) & tp->event_slow;
7462 rtl_ack_events(tp, status);
7463
7464 if (unlikely(status & RxFIFOOver)) {
7465 switch (tp->mac_version) {
7466 /* Work around for rx fifo overflow */
7467 case RTL_GIGA_MAC_VER_11:
7468 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007469 /* XXX - Hack alert. See rtl_task(). */
7470 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007471 default:
7472 break;
7473 }
7474 }
7475
7476 if (unlikely(status & SYSErr))
7477 rtl8169_pcierr_interrupt(dev);
7478
7479 if (status & LinkChg)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007480 rtl8169_check_link_status(dev, tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007481
françois romieu7dbb4912012-06-09 10:53:16 +00007482 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007483}
7484
Francois Romieu4422bcd2012-01-26 11:23:32 +01007485static void rtl_task(struct work_struct *work)
7486{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007487 static const struct {
7488 int bitnr;
7489 void (*action)(struct rtl8169_private *);
7490 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007491 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007492 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7493 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7494 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7495 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007496 struct rtl8169_private *tp =
7497 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007498 struct net_device *dev = tp->dev;
7499 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007500
Francois Romieuda78dbf2012-01-26 14:18:23 +01007501 rtl_lock_work(tp);
7502
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007503 if (!netif_running(dev) ||
7504 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007505 goto out_unlock;
7506
7507 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7508 bool pending;
7509
Francois Romieuda78dbf2012-01-26 14:18:23 +01007510 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007511 if (pending)
7512 rtl_work[i].action(tp);
7513 }
7514
7515out_unlock:
7516 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007517}
7518
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007519static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007521 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7522 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007523 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7524 int work_done= 0;
7525 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
Francois Romieuda78dbf2012-01-26 14:18:23 +01007527 status = rtl_get_events(tp);
7528 rtl_ack_events(tp, status & ~tp->event_slow);
7529
7530 if (status & RTL_EVENT_NAPI_RX)
7531 work_done = rtl_rx(dev, tp, (u32) budget);
7532
7533 if (status & RTL_EVENT_NAPI_TX)
7534 rtl_tx(dev, tp);
7535
7536 if (status & tp->event_slow) {
7537 enable_mask &= ~tp->event_slow;
7538
7539 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007542 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007543 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007544
Francois Romieuda78dbf2012-01-26 14:18:23 +01007545 rtl_irq_enable(tp, enable_mask);
7546 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 }
7548
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007549 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007552static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02007553{
7554 struct rtl8169_private *tp = netdev_priv(dev);
7555
7556 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7557 return;
7558
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007559 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
7560 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02007561}
7562
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563static void rtl8169_down(struct net_device *dev)
7564{
7565 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
Francois Romieu4876cc12011-03-11 21:07:11 +01007567 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007569 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007570 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
Hayes Wang92fc43b2011-07-06 15:58:03 +08007572 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007573 /*
7574 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007575 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7576 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007577 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007578 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007581 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 rtl8169_tx_clear(tp);
7584
7585 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007586
7587 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588}
7589
7590static int rtl8169_close(struct net_device *dev)
7591{
7592 struct rtl8169_private *tp = netdev_priv(dev);
7593 struct pci_dev *pdev = tp->pci_dev;
7594
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007595 pm_runtime_get_sync(&pdev->dev);
7596
Francois Romieucecb5fd2011-04-01 10:21:07 +02007597 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007598 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08007599
Francois Romieuda78dbf2012-01-26 14:18:23 +01007600 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007601 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007602
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007604 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605
Lekensteyn4ea72442013-07-22 09:53:30 +02007606 cancel_work_sync(&tp->wk.work);
7607
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007608 pci_free_irq(pdev, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007610 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7611 tp->RxPhyAddr);
7612 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7613 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 tp->TxDescArray = NULL;
7615 tp->RxDescArray = NULL;
7616
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007617 pm_runtime_put_sync(&pdev->dev);
7618
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 return 0;
7620}
7621
Francois Romieudc1c00c2012-03-08 10:06:18 +01007622#ifdef CONFIG_NET_POLL_CONTROLLER
7623static void rtl8169_netpoll(struct net_device *dev)
7624{
7625 struct rtl8169_private *tp = netdev_priv(dev);
7626
Heiner Kallweit29274992018-02-28 20:43:38 +01007627 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), dev);
Francois Romieudc1c00c2012-03-08 10:06:18 +01007628}
7629#endif
7630
Francois Romieudf43ac72012-03-08 09:48:40 +01007631static int rtl_open(struct net_device *dev)
7632{
7633 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007634 struct pci_dev *pdev = tp->pci_dev;
7635 int retval = -ENOMEM;
7636
7637 pm_runtime_get_sync(&pdev->dev);
7638
7639 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007640 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007641 * dma_alloc_coherent provides more.
7642 */
7643 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7644 &tp->TxPhyAddr, GFP_KERNEL);
7645 if (!tp->TxDescArray)
7646 goto err_pm_runtime_put;
7647
7648 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7649 &tp->RxPhyAddr, GFP_KERNEL);
7650 if (!tp->RxDescArray)
7651 goto err_free_tx_0;
7652
Heiner Kallweitb1127e62018-04-17 23:23:35 +02007653 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007654 if (retval < 0)
7655 goto err_free_rx_1;
7656
7657 INIT_WORK(&tp->wk.work, rtl_task);
7658
7659 smp_mb();
7660
7661 rtl_request_firmware(tp);
7662
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007663 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7664 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007665 if (retval < 0)
7666 goto err_release_fw_2;
7667
7668 rtl_lock_work(tp);
7669
7670 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7671
7672 napi_enable(&tp->napi);
7673
7674 rtl8169_init_phy(dev, tp);
7675
7676 __rtl8169_set_features(dev, dev->features);
7677
7678 rtl_pll_power_up(tp);
7679
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007680 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007681
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007682 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007683 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7684
Francois Romieudf43ac72012-03-08 09:48:40 +01007685 netif_start_queue(dev);
7686
7687 rtl_unlock_work(tp);
7688
7689 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007690 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007691
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007692 rtl8169_check_link_status(dev, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01007693out:
7694 return retval;
7695
7696err_release_fw_2:
7697 rtl_release_firmware(tp);
7698 rtl8169_rx_clear(tp);
7699err_free_rx_1:
7700 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7701 tp->RxPhyAddr);
7702 tp->RxDescArray = NULL;
7703err_free_tx_0:
7704 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7705 tp->TxPhyAddr);
7706 tp->TxDescArray = NULL;
7707err_pm_runtime_put:
7708 pm_runtime_put_noidle(&pdev->dev);
7709 goto out;
7710}
7711
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007712static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007713rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714{
7715 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007716 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007717 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007718 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007719
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007720 pm_runtime_get_noresume(&pdev->dev);
7721
7722 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007723 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007724
Junchang Wang8027aa22012-03-04 23:30:32 +01007725 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007726 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007727 stats->rx_packets = tp->rx_stats.packets;
7728 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007729 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007730
Junchang Wang8027aa22012-03-04 23:30:32 +01007731 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007732 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007733 stats->tx_packets = tp->tx_stats.packets;
7734 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007735 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007736
7737 stats->rx_dropped = dev->stats.rx_dropped;
7738 stats->tx_dropped = dev->stats.tx_dropped;
7739 stats->rx_length_errors = dev->stats.rx_length_errors;
7740 stats->rx_errors = dev->stats.rx_errors;
7741 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7742 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7743 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007744 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007745
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007746 /*
7747 * Fetch additonal counter values missing in stats collected by driver
7748 * from tally counters.
7749 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007750 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007751 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007752
7753 /*
7754 * Subtract values fetched during initalization.
7755 * See rtl8169_init_counter_offsets for a description why we do that.
7756 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007757 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007758 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007759 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007760 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02007761 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007762 le16_to_cpu(tp->tc_offset.tx_aborted);
7763
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007764 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765}
7766
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007767static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007768{
françois romieu065c27c2011-01-03 15:08:12 +00007769 struct rtl8169_private *tp = netdev_priv(dev);
7770
Francois Romieu5d06a992006-02-23 00:47:58 +01007771 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007772 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007773
7774 netif_device_detach(dev);
7775 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007776
7777 rtl_lock_work(tp);
7778 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007779 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007780 rtl_unlock_work(tp);
7781
7782 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007783}
Francois Romieu5d06a992006-02-23 00:47:58 +01007784
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007785#ifdef CONFIG_PM
7786
7787static int rtl8169_suspend(struct device *device)
7788{
7789 struct pci_dev *pdev = to_pci_dev(device);
7790 struct net_device *dev = pci_get_drvdata(pdev);
7791
7792 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007793
Francois Romieu5d06a992006-02-23 00:47:58 +01007794 return 0;
7795}
7796
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007797static void __rtl8169_resume(struct net_device *dev)
7798{
françois romieu065c27c2011-01-03 15:08:12 +00007799 struct rtl8169_private *tp = netdev_priv(dev);
7800
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007801 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007802
7803 rtl_pll_power_up(tp);
7804
Artem Savkovcff4c162012-04-03 10:29:11 +00007805 rtl_lock_work(tp);
7806 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007807 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007808 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007809
Francois Romieu98ddf982012-01-31 10:47:34 +01007810 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007811}
7812
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007813static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007814{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007815 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007816 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007817 struct rtl8169_private *tp = netdev_priv(dev);
7818
7819 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007820
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007821 if (netif_running(dev))
7822 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007823
Francois Romieu5d06a992006-02-23 00:47:58 +01007824 return 0;
7825}
7826
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007827static int rtl8169_runtime_suspend(struct device *device)
7828{
7829 struct pci_dev *pdev = to_pci_dev(device);
7830 struct net_device *dev = pci_get_drvdata(pdev);
7831 struct rtl8169_private *tp = netdev_priv(dev);
7832
Heiner Kallweita92a0842018-01-08 21:39:13 +01007833 if (!tp->TxDescArray) {
7834 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007835 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007836 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007837
Francois Romieuda78dbf2012-01-26 14:18:23 +01007838 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007839 tp->saved_wolopts = __rtl8169_get_wol(tp);
7840 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007841 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007842
7843 rtl8169_net_suspend(dev);
7844
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007845 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007846 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02007847 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007848
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007849 return 0;
7850}
7851
7852static int rtl8169_runtime_resume(struct device *device)
7853{
7854 struct pci_dev *pdev = to_pci_dev(device);
7855 struct net_device *dev = pci_get_drvdata(pdev);
7856 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08007857 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007858
7859 if (!tp->TxDescArray)
7860 return 0;
7861
Francois Romieuda78dbf2012-01-26 14:18:23 +01007862 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007863 __rtl8169_set_wol(tp, tp->saved_wolopts);
7864 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007865 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007866
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007867 rtl8169_init_phy(dev, tp);
7868
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007869 __rtl8169_resume(dev);
7870
7871 return 0;
7872}
7873
7874static int rtl8169_runtime_idle(struct device *device)
7875{
7876 struct pci_dev *pdev = to_pci_dev(device);
7877 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007878
Heiner Kallweita92a0842018-01-08 21:39:13 +01007879 if (!netif_running(dev) || !netif_carrier_ok(dev))
7880 pm_schedule_suspend(device, 10000);
7881
7882 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007883}
7884
Alexey Dobriyan47145212009-12-14 18:00:08 -08007885static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007886 .suspend = rtl8169_suspend,
7887 .resume = rtl8169_resume,
7888 .freeze = rtl8169_suspend,
7889 .thaw = rtl8169_resume,
7890 .poweroff = rtl8169_suspend,
7891 .restore = rtl8169_resume,
7892 .runtime_suspend = rtl8169_runtime_suspend,
7893 .runtime_resume = rtl8169_runtime_resume,
7894 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007895};
7896
7897#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7898
7899#else /* !CONFIG_PM */
7900
7901#define RTL8169_PM_OPS NULL
7902
7903#endif /* !CONFIG_PM */
7904
David S. Miller1805b2f2011-10-24 18:18:09 -04007905static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7906{
David S. Miller1805b2f2011-10-24 18:18:09 -04007907 /* WoL fails with 8168b when the receiver is disabled. */
7908 switch (tp->mac_version) {
7909 case RTL_GIGA_MAC_VER_11:
7910 case RTL_GIGA_MAC_VER_12:
7911 case RTL_GIGA_MAC_VER_17:
7912 pci_clear_master(tp->pci_dev);
7913
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007914 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007915 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007916 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04007917 break;
7918 default:
7919 break;
7920 }
7921}
7922
Francois Romieu1765f952008-09-13 17:21:40 +02007923static void rtl_shutdown(struct pci_dev *pdev)
7924{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007925 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007926 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007927
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007928 rtl8169_net_suspend(dev);
7929
Francois Romieucecb5fd2011-04-01 10:21:07 +02007930 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007931 rtl_rar_set(tp, dev->perm_addr);
7932
Hayes Wang92fc43b2011-07-06 15:58:03 +08007933 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007934
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007935 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007936 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7937 rtl_wol_suspend_quirk(tp);
7938 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007939 }
7940
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007941 pci_wake_from_d3(pdev, true);
7942 pci_set_power_state(pdev, PCI_D3hot);
7943 }
7944}
Francois Romieu5d06a992006-02-23 00:47:58 +01007945
Bill Pembertonbaf63292012-12-03 09:23:28 -05007946static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007947{
7948 struct net_device *dev = pci_get_drvdata(pdev);
7949 struct rtl8169_private *tp = netdev_priv(dev);
7950
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007951 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007952 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007953
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007954 netif_napi_del(&tp->napi);
7955
Francois Romieue27566e2012-03-08 09:54:01 +01007956 unregister_netdev(dev);
7957
7958 rtl_release_firmware(tp);
7959
7960 if (pci_dev_run_wake(pdev))
7961 pm_runtime_get_noresume(&pdev->dev);
7962
7963 /* restore original MAC address */
7964 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007965}
7966
Francois Romieufa9c3852012-03-08 10:01:50 +01007967static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007968 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007969 .ndo_stop = rtl8169_close,
7970 .ndo_get_stats64 = rtl8169_get_stats64,
7971 .ndo_start_xmit = rtl8169_start_xmit,
7972 .ndo_tx_timeout = rtl8169_tx_timeout,
7973 .ndo_validate_addr = eth_validate_addr,
7974 .ndo_change_mtu = rtl8169_change_mtu,
7975 .ndo_fix_features = rtl8169_fix_features,
7976 .ndo_set_features = rtl8169_set_features,
7977 .ndo_set_mac_address = rtl_set_mac_address,
7978 .ndo_do_ioctl = rtl8169_ioctl,
7979 .ndo_set_rx_mode = rtl_set_rx_mode,
7980#ifdef CONFIG_NET_POLL_CONTROLLER
7981 .ndo_poll_controller = rtl8169_netpoll,
7982#endif
7983
7984};
7985
Francois Romieu31fa8b12012-03-08 10:09:40 +01007986static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007987 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007988 unsigned int region;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007989 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007990 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007991 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007992 u8 default_ver;
7993} rtl_cfg_infos [] = {
7994 [RTL_CFG_0] = {
7995 .hw_start = rtl_hw_start_8169,
7996 .region = 1,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007997 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007998 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007999 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008000 .default_ver = RTL_GIGA_MAC_VER_01,
8001 },
8002 [RTL_CFG_1] = {
8003 .hw_start = rtl_hw_start_8168,
8004 .region = 2,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008005 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008006 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008007 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008008 .default_ver = RTL_GIGA_MAC_VER_11,
8009 },
8010 [RTL_CFG_2] = {
8011 .hw_start = rtl_hw_start_8101,
8012 .region = 2,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008013 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8014 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03008015 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008016 .default_ver = RTL_GIGA_MAC_VER_13,
8017 }
8018};
8019
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008020static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01008021{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008022 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008023
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008024 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008025 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
8026 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
8027 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008028 flags = PCI_IRQ_LEGACY;
8029 } else {
8030 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008031 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008032
8033 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01008034}
8035
Hayes Wangc5583862012-07-02 17:23:22 +08008036DECLARE_RTL_COND(rtl_link_list_ready_cond)
8037{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008038 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08008039}
8040
8041DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8042{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008043 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08008044}
8045
Bill Pembertonbaf63292012-12-03 09:23:28 -05008046static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008047{
Hayes Wangc5583862012-07-02 17:23:22 +08008048 u32 data;
8049
8050 tp->ocp_base = OCP_STD_PHY_BASE;
8051
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008052 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08008053
8054 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8055 return;
8056
8057 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8058 return;
8059
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008060 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08008061 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008062 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08008063
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008064 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008065 data &= ~(1 << 14);
8066 r8168_mac_ocp_write(tp, 0xe8de, data);
8067
8068 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8069 return;
8070
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008071 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008072 data |= (1 << 15);
8073 r8168_mac_ocp_write(tp, 0xe8de, data);
8074
8075 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8076 return;
8077}
8078
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008079static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8080{
8081 rtl8168ep_stop_cmac(tp);
8082 rtl_hw_init_8168g(tp);
8083}
8084
Bill Pembertonbaf63292012-12-03 09:23:28 -05008085static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008086{
8087 switch (tp->mac_version) {
8088 case RTL_GIGA_MAC_VER_40:
8089 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008090 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008091 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008092 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008093 case RTL_GIGA_MAC_VER_45:
8094 case RTL_GIGA_MAC_VER_46:
8095 case RTL_GIGA_MAC_VER_47:
8096 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008097 rtl_hw_init_8168g(tp);
8098 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008099 case RTL_GIGA_MAC_VER_49:
8100 case RTL_GIGA_MAC_VER_50:
8101 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008102 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008103 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008104 default:
8105 break;
8106 }
8107}
8108
hayeswang929a0312014-09-16 11:40:47 +08008109static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008110{
8111 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8112 const unsigned int region = cfg->region;
8113 struct rtl8169_private *tp;
8114 struct mii_if_info *mii;
8115 struct net_device *dev;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008116 int chipset, i;
8117 int rc;
8118
8119 if (netif_msg_drv(&debug)) {
8120 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8121 MODULENAME, RTL8169_VERSION);
8122 }
8123
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008124 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8125 if (!dev)
8126 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008127
8128 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008129 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008130 tp = netdev_priv(dev);
8131 tp->dev = dev;
8132 tp->pci_dev = pdev;
8133 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8134
8135 mii = &tp->mii;
8136 mii->dev = dev;
8137 mii->mdio_read = rtl_mdio_read;
8138 mii->mdio_write = rtl_mdio_write;
8139 mii->phy_id_mask = 0x1f;
8140 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008141 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008142
8143 /* disable ASPM completely as that cause random device stop working
8144 * problems as well as full system hangs for some PCIe devices users */
8145 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8146 PCIE_LINK_STATE_CLKPM);
8147
8148 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008149 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008150 if (rc < 0) {
8151 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008152 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008153 }
8154
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008155 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008156 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8157
8158 /* make sure PCI base addr 1 is MMIO */
8159 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8160 netif_err(tp, probe, dev,
8161 "region #%d not an MMIO resource, aborting\n",
8162 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008163 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008164 }
8165
8166 /* check for weird/broken PCI region reporting */
8167 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8168 netif_err(tp, probe, dev,
8169 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008170 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008171 }
8172
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008173 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008174 if (rc < 0) {
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008175 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008176 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008177 }
8178
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008179 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01008180
8181 if (!pci_is_pcie(pdev))
8182 netif_info(tp, probe, dev, "not PCI Express\n");
8183
8184 /* Identify chip attached to board */
8185 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8186
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008187 tp->cp_cmd = 0;
8188
8189 if ((sizeof(dma_addr_t) > 4) &&
8190 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8191 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008192 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8193 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008194
8195 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8196 if (!pci_is_pcie(pdev))
8197 tp->cp_cmd |= PCIDAC;
8198 dev->features |= NETIF_F_HIGHDMA;
8199 } else {
8200 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8201 if (rc < 0) {
8202 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008203 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008204 }
8205 }
8206
Francois Romieu3b6cf252012-03-08 09:59:04 +01008207 rtl_init_rxcfg(tp);
8208
8209 rtl_irq_disable(tp);
8210
Hayes Wangc5583862012-07-02 17:23:22 +08008211 rtl_hw_initialize(tp);
8212
Francois Romieu3b6cf252012-03-08 09:59:04 +01008213 rtl_hw_reset(tp);
8214
8215 rtl_ack_events(tp, 0xffff);
8216
8217 pci_set_master(pdev);
8218
Francois Romieu3b6cf252012-03-08 09:59:04 +01008219 rtl_init_mdio_ops(tp);
8220 rtl_init_pll_power_ops(tp);
8221 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008222 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008223
8224 rtl8169_print_mac_version(tp);
8225
8226 chipset = tp->mac_version;
8227 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8228
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008229 rc = rtl_alloc_irq(tp);
8230 if (rc < 0) {
8231 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8232 return rc;
8233 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008234
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008235 /* override BIOS settings, use userspace tools to enable WOL */
8236 __rtl8169_set_wol(tp, 0);
8237
Francois Romieu3b6cf252012-03-08 09:59:04 +01008238 if (rtl_tbi_enabled(tp)) {
8239 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008240 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008241 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8242 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8243 tp->link_ok = rtl8169_tbi_link_ok;
8244 tp->do_ioctl = rtl_tbi_ioctl;
8245 } else {
8246 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008247 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008248 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8249 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8250 tp->link_ok = rtl8169_xmii_link_ok;
8251 tp->do_ioctl = rtl_xmii_ioctl;
8252 }
8253
8254 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008255 u64_stats_init(&tp->rx_stats.syncp);
8256 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008257
8258 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008259 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8260 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8261 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8262 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8263 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8264 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8265 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8266 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8267 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8268 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008269 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8270 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008271 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8272 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8273 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8274 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008275 u16 mac_addr[3];
8276
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008277 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8278 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008279
8280 if (is_valid_ether_addr((u8 *)mac_addr))
8281 rtl_rar_set(tp, (u8 *)mac_addr);
8282 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008283 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008284 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008285
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008286 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008287 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008288
Heiner Kallweit37621492018-04-17 23:20:03 +02008289 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008290
8291 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8292 * properly for all devices */
8293 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008294 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008295
8296 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008297 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8298 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008299 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8300 NETIF_F_HIGHDMA;
8301
hayeswang929a0312014-09-16 11:40:47 +08008302 tp->cp_cmd |= RxChkSum | RxVlan;
8303
8304 /*
8305 * Pretend we are using VLANs; This bypasses a nasty bug where
8306 * Interrupts stop flowing on high load on 8110SCd controllers.
8307 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008308 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008309 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008310 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008311
hayeswang5888d3f2014-07-11 16:25:56 +08008312 if (tp->txd_version == RTL_TD_0)
8313 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008314 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008315 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008316 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8317 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008318 WARN_ON_ONCE(1);
8319
Francois Romieu3b6cf252012-03-08 09:59:04 +01008320 dev->hw_features |= NETIF_F_RXALL;
8321 dev->hw_features |= NETIF_F_RXFCS;
8322
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008323 /* MTU range: 60 - hw-specific max */
8324 dev->min_mtu = ETH_ZLEN;
8325 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8326
Francois Romieu3b6cf252012-03-08 09:59:04 +01008327 tp->hw_start = cfg->hw_start;
8328 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008329 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008330
8331 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8332 ~(RxBOVF | RxFOVF) : ~0;
8333
Kees Cook9de36cc2017-10-25 03:53:12 -07008334 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008335
8336 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8337
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008338 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8339 &tp->counters_phys_addr,
8340 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008341 if (!tp->counters)
8342 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008343
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02008344 pci_set_drvdata(pdev, dev);
8345
Francois Romieu3b6cf252012-03-08 09:59:04 +01008346 rc = register_netdev(dev);
8347 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008348 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008349
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008350 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
Andy Shevchenko93a00d42018-03-01 13:27:35 +02008351 rtl_chip_infos[chipset].name, tp->mmio_addr, dev->dev_addr,
Andy Shevchenko1ef72862018-03-01 13:27:34 +02008352 (u32)(RTL_R32(tp, TxConfig) & 0x9cf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01008353 pci_irq_vector(pdev, 0));
Francois Romieu3b6cf252012-03-08 09:59:04 +01008354 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8355 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8356 "tx checksumming: %s]\n",
8357 rtl_chip_infos[chipset].jumbo_max,
8358 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8359 }
8360
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008361 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008362 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008363
Francois Romieu3b6cf252012-03-08 09:59:04 +01008364 netif_carrier_off(dev);
8365
Heiner Kallweita92a0842018-01-08 21:39:13 +01008366 if (pci_dev_run_wake(pdev))
8367 pm_runtime_put_sync(&pdev->dev);
8368
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008369 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008370}
8371
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372static struct pci_driver rtl8169_pci_driver = {
8373 .name = MODULENAME,
8374 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008375 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008376 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008377 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008378 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379};
8380
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008381module_pci_driver(rtl8169_pci_driver);