blob: 3c7d90d3a9cb13a9944f1cb2ce0b1f9363159221 [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
Francois Romieu9c14cea2008-07-05 00:21:15 +020087#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000088#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000094#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000107#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800143 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800146 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800147 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800148 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000151 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000152 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800153 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
Francois Romieud58d46b2011-05-03 16:38:29 +0200169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700185 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 u16 jumbo_max;
188 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200256 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200261 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800264 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800270 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800282 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318#undef _R
319
Francois Romieubcf0bf92006-07-26 23:14:13 +0200320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
Benoit Taine9baa3c32014-08-08 15:56:03 +0200326static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100341 { 0x0001, 0x8168,
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 {0,},
344};
345
346MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000348static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200349static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200350static struct {
351 u32 msg_enable;
352} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francois Romieu07d3f512007-02-21 22:40:46 +0100354enum rtl_registers {
355 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100356 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
364 FLASH = 0x30,
365 ERSR = 0x36,
366 ChipCmd = 0x37,
367 TxPoll = 0x38,
368 IntrMask = 0x3c,
369 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700370
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371 TxConfig = 0x40,
372#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
374
375 RxConfig = 0x44,
376#define RX128_INT_EN (1 << 15) /* 8111c and later */
377#define RX_MULTI_EN (1 << 14) /* 8111c only */
378#define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000381#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700385
Francois Romieu07d3f512007-02-21 22:40:46 +0100386 RxMissed = 0x4c,
387 Cfg9346 = 0x50,
388 Config0 = 0x51,
389 Config1 = 0x52,
390 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200391#define PME_SIGNAL (1 << 5) /* 8168c and later */
392
Francois Romieu07d3f512007-02-21 22:40:46 +0100393 Config3 = 0x54,
394 Config4 = 0x55,
395 Config5 = 0x56,
396 MultiIntr = 0x5c,
397 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 PHYstatus = 0x6c,
399 RxMaxSize = 0xda,
400 CPlusCmd = 0xe0,
401 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300402
403#define RTL_COALESCE_MASK 0x0f
404#define RTL_COALESCE_SHIFT 4
405#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
406#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
407
Francois Romieu07d3f512007-02-21 22:40:46 +0100408 RxDescAddrLow = 0xe4,
409 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000410 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
411
412#define NoEarlyTx 0x3f /* Max value : no early transmit. */
413
414 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
415
416#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800417#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000418
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncEvent = 0xf0,
420 FuncEventMask = 0xf4,
421 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800422 IBCR0 = 0xf8,
423 IBCR2 = 0xf9,
424 IBIMR0 = 0xfa,
425 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100426 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429enum rtl8110_registers {
430 TBICSR = 0x64,
431 TBI_ANAR = 0x68,
432 TBI_LPAR = 0x6a,
433};
434
435enum rtl8168_8101_registers {
436 CSIDR = 0x64,
437 CSIAR = 0x68,
438#define CSIAR_FLAG 0x80000000
439#define CSIAR_WRITE_CMD 0x80000000
440#define CSIAR_BYTE_ENABLE 0x0f
441#define CSIAR_BYTE_ENABLE_SHIFT 12
442#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800443#define CSIAR_FUNC_CARD 0x00000000
444#define CSIAR_FUNC_SDIO 0x00010000
445#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800446#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000447 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 EPHYAR = 0x80,
449#define EPHYAR_FLAG 0x80000000
450#define EPHYAR_WRITE_CMD 0x80000000
451#define EPHYAR_REG_MASK 0x1f
452#define EPHYAR_REG_SHIFT 16
453#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800454 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800456#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200457 DBG_REG = 0xd1,
458#define FIX_NAK_1 (1 << 4)
459#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800460 TWSI = 0xd2,
461 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800462#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800463#define TX_EMPTY (1 << 5)
464#define RX_EMPTY (1 << 4)
465#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800466#define EN_NDP (1 << 3)
467#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800468#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000469 EFUSEAR = 0xdc,
470#define EFUSEAR_FLAG 0x80000000
471#define EFUSEAR_WRITE_CMD 0x80000000
472#define EFUSEAR_READ_CMD 0x00000000
473#define EFUSEAR_REG_MASK 0x03ff
474#define EFUSEAR_REG_SHIFT 8
475#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800476 MISC_1 = 0xf2,
477#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478};
479
françois romieuc0e45c12011-01-03 15:08:04 +0000480enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800481 LED_FREQ = 0x1a,
482 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000483 ERIDR = 0x70,
484 ERIAR = 0x74,
485#define ERIAR_FLAG 0x80000000
486#define ERIAR_WRITE_CMD 0x80000000
487#define ERIAR_READ_CMD 0x00000000
488#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000489#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800490#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
491#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
492#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800493#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800494#define ERIAR_MASK_SHIFT 12
495#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
496#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800497#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800498#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800499#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000500 EPHY_RXER_NUM = 0x7c,
501 OCPDR = 0xb0, /* OCP GPHY access */
502#define OCPDR_WRITE_CMD 0x80000000
503#define OCPDR_READ_CMD 0x00000000
504#define OCPDR_REG_MASK 0x7f
505#define OCPDR_GPHY_REG_SHIFT 16
506#define OCPDR_DATA_MASK 0xffff
507 OCPAR = 0xb4,
508#define OCPAR_FLAG 0x80000000
509#define OCPAR_GPHY_WRITE_CMD 0x8000f060
510#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800511 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000512 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
513 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200514#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800515#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800516#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800517#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800518#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000519};
520
Francois Romieu07d3f512007-02-21 22:40:46 +0100521enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100523 SYSErr = 0x8000,
524 PCSTimeout = 0x4000,
525 SWInt = 0x0100,
526 TxDescUnavail = 0x0080,
527 RxFIFOOver = 0x0040,
528 LinkChg = 0x0020,
529 RxOverflow = 0x0010,
530 TxErr = 0x0008,
531 TxOK = 0x0004,
532 RxErr = 0x0002,
533 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400536 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200537 RxFOVF = (1 << 23),
538 RxRWT = (1 << 22),
539 RxRES = (1 << 21),
540 RxRUNT = (1 << 20),
541 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800544 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100545 CmdReset = 0x10,
546 CmdRxEnb = 0x08,
547 CmdTxEnb = 0x04,
548 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Francois Romieu275391a2007-02-23 23:50:28 +0100550 /* TXPoll register p.5 */
551 HPQ = 0x80, /* Poll cmd on the high prio queue */
552 NPQ = 0x40, /* Poll cmd on the low prio queue */
553 FSWInt = 0x01, /* Forced software interrupt */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100556 Cfg9346_Lock = 0x00,
557 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100560 AcceptErr = 0x20,
561 AcceptRunt = 0x10,
562 AcceptBroadcast = 0x08,
563 AcceptMulticast = 0x04,
564 AcceptMyPhys = 0x02,
565 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200566#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* TxConfigBits */
569 TxInterFrameGapShift = 24,
570 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
571
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200573 LEDS1 = (1 << 7),
574 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200575 Speed_down = (1 << 4),
576 MEMMAP = (1 << 3),
577 IOMAP = (1 << 2),
578 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100579 PMEnable = (1 << 0), /* Power Management Enable */
580
Francois Romieu6dccd162007-02-13 23:38:05 +0100581 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000582 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000583 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100584 PCI_Clock_66MHz = 0x01,
585 PCI_Clock_33MHz = 0x00,
586
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100587 /* Config3 register p.25 */
588 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
589 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200590 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800591 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200592 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100593
Francois Romieud58d46b2011-05-03 16:38:29 +0200594 /* Config4 register */
595 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
596
Francois Romieu5d06a992006-02-23 00:47:58 +0100597 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100598 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
599 MWF = (1 << 5), /* Accept Multicast wakeup frame */
600 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200601 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100602 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100603 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000604 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* TBICSR p.28 */
607 TBIReset = 0x80000000,
608 TBILoopback = 0x40000000,
609 TBINwEnable = 0x20000000,
610 TBINwRestart = 0x10000000,
611 TBILinkOk = 0x02000000,
612 TBINwComplete = 0x01000000,
613
614 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200615 EnableBist = (1 << 15), // 8168 8101
616 Mac_dbgo_oe = (1 << 14), // 8168 8101
617 Normal_mode = (1 << 13), // unused
618 Force_half_dup = (1 << 12), // 8168 8101
619 Force_rxflow_en = (1 << 11), // 8168 8101
620 Force_txflow_en = (1 << 10), // 8168 8101
621 Cxpl_dbg_sel = (1 << 9), // 8168 8101
622 ASF = (1 << 8), // 8168 8101
623 PktCntrDisable = (1 << 7), // 8168 8101
624 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 RxVlan = (1 << 6),
626 RxChkSum = (1 << 5),
627 PCIDAC = (1 << 4),
628 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100629 INTT_0 = 0x0000, // 8168
630 INTT_1 = 0x0001, // 8168
631 INTT_2 = 0x0002, // 8168
632 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100635 TBI_Enable = 0x80,
636 TxFlowCtrl = 0x40,
637 RxFlowCtrl = 0x20,
638 _1000bpsF = 0x10,
639 _100bps = 0x08,
640 _10bps = 0x04,
641 LinkStatus = 0x02,
642 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100645 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200646
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200647 /* ResetCounterCommand */
648 CounterReset = 0x1,
649
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200650 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100651 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800652
653 /* magic enable v2 */
654 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
Francois Romieu2b7b4312011-04-18 22:53:24 -0700657enum rtl_desc_bit {
658 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
660 RingEnd = (1 << 30), /* End of descriptor ring */
661 FirstFrag = (1 << 29), /* First segment of a packet */
662 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Francois Romieu2b7b4312011-04-18 22:53:24 -0700665/* Generic case. */
666enum rtl_tx_desc_bit {
667 /* First doubleword. */
668 TD_LSO = (1 << 27), /* Large Send Offload */
669#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Francois Romieu2b7b4312011-04-18 22:53:24 -0700671 /* Second doubleword. */
672 TxVlanTag = (1 << 17), /* Add VLAN tag */
673};
674
675/* 8169, 8168b and 810x except 8102e. */
676enum rtl_tx_desc_bit_0 {
677 /* First doubleword. */
678#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
679 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
680 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
681 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
682};
683
684/* 8102e, 8168c and beyond. */
685enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800686 /* First doubleword. */
687 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800688 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800689#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800690#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800691
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800693#define TCPHO_SHIFT 18
694#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700695#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800696 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
697 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700698 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
699 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
700};
701
Francois Romieu2b7b4312011-04-18 22:53:24 -0700702enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Rx private */
704 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500705 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707#define RxProtoUDP (PID1)
708#define RxProtoTCP (PID0)
709#define RxProtoIP (PID1 | PID0)
710#define RxProtoMask RxProtoIP
711
712 IPFail = (1 << 16), /* IP checksum failed */
713 UDPFail = (1 << 15), /* UDP/IP checksum failed */
714 TCPFail = (1 << 14), /* TCP/IP checksum failed */
715 RxVlanTag = (1 << 16), /* VLAN tag available */
716};
717
718#define RsvdMask 0x3fffc000
719
720struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200721 __le32 opts1;
722 __le32 opts2;
723 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
726struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200727 __le32 opts1;
728 __le32 opts2;
729 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
732struct ring_info {
733 struct sk_buff *skb;
734 u32 len;
735 u8 __pad[sizeof(void *) - sizeof(u32)];
736};
737
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200738enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200739 RTL_FEATURE_WOL = (1 << 0),
740 RTL_FEATURE_MSI = (1 << 1),
741 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200742};
743
Ivan Vecera355423d2009-02-06 21:49:57 -0800744struct rtl8169_counters {
745 __le64 tx_packets;
746 __le64 rx_packets;
747 __le64 tx_errors;
748 __le32 rx_errors;
749 __le16 rx_missed;
750 __le16 align_errors;
751 __le32 tx_one_collision;
752 __le32 tx_multi_collision;
753 __le64 rx_unicast;
754 __le64 rx_broadcast;
755 __le32 rx_multicast;
756 __le16 tx_aborted;
757 __le16 tx_underun;
758};
759
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200760struct rtl8169_tc_offsets {
761 bool inited;
762 __le64 tx_errors;
763 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200764 __le16 tx_aborted;
765};
766
Francois Romieuda78dbf2012-01-26 14:18:23 +0100767enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100768 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100769 RTL_FLAG_TASK_SLOW_PENDING,
770 RTL_FLAG_TASK_RESET_PENDING,
771 RTL_FLAG_TASK_PHY_PENDING,
772 RTL_FLAG_MAX
773};
774
Junchang Wang8027aa22012-03-04 23:30:32 +0100775struct rtl8169_stats {
776 u64 packets;
777 u64 bytes;
778 struct u64_stats_sync syncp;
779};
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781struct rtl8169_private {
782 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200783 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000784 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700785 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200786 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700787 u16 txd_version;
788 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
790 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100792 struct rtl8169_stats rx_stats;
793 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
795 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
796 dma_addr_t TxPhyAddr;
797 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000798 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct timer_list timer;
801 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100802
803 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300804 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000805
806 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200807 void (*write)(struct rtl8169_private *, int, int);
808 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000809 } mdio_ops;
810
françois romieu065c27c2011-01-03 15:08:12 +0000811 struct pll_power_ops {
812 void (*down)(struct rtl8169_private *);
813 void (*up)(struct rtl8169_private *);
814 } pll_power_ops;
815
Francois Romieud58d46b2011-05-03 16:38:29 +0200816 struct jumbo_ops {
817 void (*enable)(struct rtl8169_private *);
818 void (*disable)(struct rtl8169_private *);
819 } jumbo_ops;
820
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800821 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200822 void (*write)(struct rtl8169_private *, int, int);
823 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800824 } csi_ops;
825
Oliver Neukum54405cd2011-01-06 21:55:13 +0100826 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100827 int (*get_link_ksettings)(struct net_device *,
828 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000829 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100830 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000831 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800833 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800834 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100835
836 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100837 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
838 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100839 struct work_struct work;
840 } wk;
841
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200842 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200843
844 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200845 dma_addr_t counters_phys_addr;
846 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200847 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000848 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400849 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000850
Francois Romieub6ffd972011-06-17 17:00:05 +0200851 struct rtl_fw {
852 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200853
854#define RTL_VER_SIZE 32
855
856 char version[RTL_VER_SIZE];
857
858 struct rtl_fw_phy_action {
859 __le32 *code;
860 size_t size;
861 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200862 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300863#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800864
865 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
867
Ralf Baechle979b6c12005-06-13 14:30:40 -0700868MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700871MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200872module_param_named(debug, debug.msg_enable, int, 0);
873MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874MODULE_LICENSE("GPL");
875MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000876MODULE_FIRMWARE(FIRMWARE_8168D_1);
877MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000878MODULE_FIRMWARE(FIRMWARE_8168E_1);
879MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400880MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800881MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800882MODULE_FIRMWARE(FIRMWARE_8168F_1);
883MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800884MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800885MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800886MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800887MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000888MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000889MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000890MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800891MODULE_FIRMWARE(FIRMWARE_8168H_1);
892MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200893MODULE_FIRMWARE(FIRMWARE_8107E_1);
894MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Francois Romieuda78dbf2012-01-26 14:18:23 +0100896static void rtl_lock_work(struct rtl8169_private *tp)
897{
898 mutex_lock(&tp->wk.mutex);
899}
900
901static void rtl_unlock_work(struct rtl8169_private *tp)
902{
903 mutex_unlock(&tp->wk.mutex);
904}
905
Francois Romieud58d46b2011-05-03 16:38:29 +0200906static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
907{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800908 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
909 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200910}
911
Francois Romieuffc46952012-07-06 14:19:23 +0200912struct rtl_cond {
913 bool (*check)(struct rtl8169_private *);
914 const char *msg;
915};
916
917static void rtl_udelay(unsigned int d)
918{
919 udelay(d);
920}
921
922static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
923 void (*delay)(unsigned int), unsigned int d, int n,
924 bool high)
925{
926 int i;
927
928 for (i = 0; i < n; i++) {
929 delay(d);
930 if (c->check(tp) == high)
931 return true;
932 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200933 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
934 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200935 return false;
936}
937
938static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
939 const struct rtl_cond *c,
940 unsigned int d, int n)
941{
942 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
943}
944
945static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
946 const struct rtl_cond *c,
947 unsigned int d, int n)
948{
949 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
950}
951
952static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
953 const struct rtl_cond *c,
954 unsigned int d, int n)
955{
956 return rtl_loop_wait(tp, c, msleep, d, n, true);
957}
958
959static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
960 const struct rtl_cond *c,
961 unsigned int d, int n)
962{
963 return rtl_loop_wait(tp, c, msleep, d, n, false);
964}
965
966#define DECLARE_RTL_COND(name) \
967static bool name ## _check(struct rtl8169_private *); \
968 \
969static const struct rtl_cond name = { \
970 .check = name ## _check, \
971 .msg = #name \
972}; \
973 \
974static bool name ## _check(struct rtl8169_private *tp)
975
Hayes Wangc5583862012-07-02 17:23:22 +0800976static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
977{
978 if (reg & 0xffff0001) {
979 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
980 return true;
981 }
982 return false;
983}
984
985DECLARE_RTL_COND(rtl_ocp_gphy_cond)
986{
987 void __iomem *ioaddr = tp->mmio_addr;
988
989 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
990}
991
992static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
993{
994 void __iomem *ioaddr = tp->mmio_addr;
995
996 if (rtl_ocp_reg_failure(tp, reg))
997 return;
998
999 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1000
1001 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1002}
1003
1004static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1005{
1006 void __iomem *ioaddr = tp->mmio_addr;
1007
1008 if (rtl_ocp_reg_failure(tp, reg))
1009 return 0;
1010
1011 RTL_W32(GPHY_OCP, reg << 15);
1012
1013 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1014 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1015}
1016
Hayes Wangc5583862012-07-02 17:23:22 +08001017static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1018{
1019 void __iomem *ioaddr = tp->mmio_addr;
1020
1021 if (rtl_ocp_reg_failure(tp, reg))
1022 return;
1023
1024 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001025}
1026
1027static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1028{
1029 void __iomem *ioaddr = tp->mmio_addr;
1030
1031 if (rtl_ocp_reg_failure(tp, reg))
1032 return 0;
1033
1034 RTL_W32(OCPDR, reg << 15);
1035
Hayes Wang3a83ad12012-07-11 20:31:56 +08001036 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001037}
1038
1039#define OCP_STD_PHY_BASE 0xa400
1040
1041static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1042{
1043 if (reg == 0x1f) {
1044 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1045 return;
1046 }
1047
1048 if (tp->ocp_base != OCP_STD_PHY_BASE)
1049 reg -= 0x10;
1050
1051 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1052}
1053
1054static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1055{
1056 if (tp->ocp_base != OCP_STD_PHY_BASE)
1057 reg -= 0x10;
1058
1059 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1060}
1061
hayeswangeee37862013-04-01 22:23:38 +00001062static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1063{
1064 if (reg == 0x1f) {
1065 tp->ocp_base = value << 4;
1066 return;
1067 }
1068
1069 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1070}
1071
1072static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1073{
1074 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1075}
1076
Francois Romieuffc46952012-07-06 14:19:23 +02001077DECLARE_RTL_COND(rtl_phyar_cond)
1078{
1079 void __iomem *ioaddr = tp->mmio_addr;
1080
1081 return RTL_R32(PHYAR) & 0x80000000;
1082}
1083
Francois Romieu24192212012-07-06 20:19:42 +02001084static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Francois Romieu24192212012-07-06 20:19:42 +02001086 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Francois Romieu24192212012-07-06 20:19:42 +02001088 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Francois Romieuffc46952012-07-06 14:19:23 +02001090 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001091 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001092 * According to hardware specs a 20us delay is required after write
1093 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001094 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001095 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Francois Romieu24192212012-07-06 20:19:42 +02001098static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Francois Romieu24192212012-07-06 20:19:42 +02001100 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001101 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Francois Romieu24192212012-07-06 20:19:42 +02001103 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Francois Romieuffc46952012-07-06 14:19:23 +02001105 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1106 RTL_R32(PHYAR) & 0xffff : ~0;
1107
Timo Teräs81a95f02010-06-09 17:31:48 -07001108 /*
1109 * According to hardware specs a 20us delay is required after read
1110 * complete indication, but before sending next command.
1111 */
1112 udelay(20);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return value;
1115}
1116
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001117DECLARE_RTL_COND(rtl_ocpar_cond)
1118{
1119 void __iomem *ioaddr = tp->mmio_addr;
1120
1121 return RTL_R32(OCPAR) & OCPAR_FLAG;
1122}
1123
Francois Romieu24192212012-07-06 20:19:42 +02001124static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001125{
Francois Romieu24192212012-07-06 20:19:42 +02001126 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001127
Francois Romieu24192212012-07-06 20:19:42 +02001128 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001129 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1130 RTL_W32(EPHY_RXER_NUM, 0);
1131
Francois Romieuffc46952012-07-06 14:19:23 +02001132 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001133}
1134
Francois Romieu24192212012-07-06 20:19:42 +02001135static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001136{
Francois Romieu24192212012-07-06 20:19:42 +02001137 r8168dp_1_mdio_access(tp, reg,
1138 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001139}
1140
Francois Romieu24192212012-07-06 20:19:42 +02001141static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001142{
Francois Romieu24192212012-07-06 20:19:42 +02001143 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001144
Francois Romieu24192212012-07-06 20:19:42 +02001145 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001146
1147 mdelay(1);
1148 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1149 RTL_W32(EPHY_RXER_NUM, 0);
1150
Francois Romieuffc46952012-07-06 14:19:23 +02001151 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1152 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001153}
1154
françois romieue6de30d2011-01-03 15:08:37 +00001155#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1156
1157static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1158{
1159 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1160}
1161
1162static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1163{
1164 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1165}
1166
Francois Romieu24192212012-07-06 20:19:42 +02001167static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001168{
Francois Romieu24192212012-07-06 20:19:42 +02001169 void __iomem *ioaddr = tp->mmio_addr;
1170
françois romieue6de30d2011-01-03 15:08:37 +00001171 r8168dp_2_mdio_start(ioaddr);
1172
Francois Romieu24192212012-07-06 20:19:42 +02001173 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001174
1175 r8168dp_2_mdio_stop(ioaddr);
1176}
1177
Francois Romieu24192212012-07-06 20:19:42 +02001178static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001179{
Francois Romieu24192212012-07-06 20:19:42 +02001180 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001181 int value;
1182
1183 r8168dp_2_mdio_start(ioaddr);
1184
Francois Romieu24192212012-07-06 20:19:42 +02001185 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001186
1187 r8168dp_2_mdio_stop(ioaddr);
1188
1189 return value;
1190}
1191
françois romieu4da19632011-01-03 15:07:55 +00001192static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001193{
Francois Romieu24192212012-07-06 20:19:42 +02001194 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001195}
1196
françois romieu4da19632011-01-03 15:07:55 +00001197static int rtl_readphy(struct rtl8169_private *tp, int location)
1198{
Francois Romieu24192212012-07-06 20:19:42 +02001199 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001200}
1201
1202static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1203{
1204 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1205}
1206
Chun-Hao Lin76564422014-10-01 23:17:17 +08001207static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001208{
1209 int val;
1210
françois romieu4da19632011-01-03 15:07:55 +00001211 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001212 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001213}
1214
Francois Romieuccdffb92008-07-26 14:26:06 +02001215static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1216 int val)
1217{
1218 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001219
françois romieu4da19632011-01-03 15:07:55 +00001220 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001221}
1222
1223static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1224{
1225 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001226
françois romieu4da19632011-01-03 15:07:55 +00001227 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001228}
1229
Francois Romieuffc46952012-07-06 14:19:23 +02001230DECLARE_RTL_COND(rtl_ephyar_cond)
1231{
1232 void __iomem *ioaddr = tp->mmio_addr;
1233
1234 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1235}
1236
Francois Romieufdf6fc02012-07-06 22:40:38 +02001237static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001238{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001239 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001240
1241 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1242 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1243
Francois Romieuffc46952012-07-06 14:19:23 +02001244 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1245
1246 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001247}
1248
Francois Romieufdf6fc02012-07-06 22:40:38 +02001249static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001250{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001251 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001252
1253 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1254
Francois Romieuffc46952012-07-06 14:19:23 +02001255 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1256 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001257}
1258
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001259DECLARE_RTL_COND(rtl_eriar_cond)
1260{
1261 void __iomem *ioaddr = tp->mmio_addr;
1262
1263 return RTL_R32(ERIAR) & ERIAR_FLAG;
1264}
1265
Francois Romieufdf6fc02012-07-06 22:40:38 +02001266static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1267 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001268{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001269 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001270
1271 BUG_ON((addr & 3) || (mask == 0));
1272 RTL_W32(ERIDR, val);
1273 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1274
Francois Romieuffc46952012-07-06 14:19:23 +02001275 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001276}
1277
Francois Romieufdf6fc02012-07-06 22:40:38 +02001278static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001279{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001280 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001281
1282 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1283
Francois Romieuffc46952012-07-06 14:19:23 +02001284 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1285 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001286}
1287
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001288static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001289 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001290{
1291 u32 val;
1292
Francois Romieufdf6fc02012-07-06 22:40:38 +02001293 val = rtl_eri_read(tp, addr, type);
1294 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001295}
1296
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001297static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1298{
1299 void __iomem *ioaddr = tp->mmio_addr;
1300
1301 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1302 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1303 RTL_R32(OCPDR) : ~0;
1304}
1305
1306static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1307{
1308 return rtl_eri_read(tp, reg, ERIAR_OOB);
1309}
1310
1311static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1312{
1313 switch (tp->mac_version) {
1314 case RTL_GIGA_MAC_VER_27:
1315 case RTL_GIGA_MAC_VER_28:
1316 case RTL_GIGA_MAC_VER_31:
1317 return r8168dp_ocp_read(tp, mask, reg);
1318 case RTL_GIGA_MAC_VER_49:
1319 case RTL_GIGA_MAC_VER_50:
1320 case RTL_GIGA_MAC_VER_51:
1321 return r8168ep_ocp_read(tp, mask, reg);
1322 default:
1323 BUG();
1324 return ~0;
1325 }
1326}
1327
1328static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1329 u32 data)
1330{
1331 void __iomem *ioaddr = tp->mmio_addr;
1332
1333 RTL_W32(OCPDR, data);
1334 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1335 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1336}
1337
1338static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1339 u32 data)
1340{
1341 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1342 data, ERIAR_OOB);
1343}
1344
1345static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1346{
1347 switch (tp->mac_version) {
1348 case RTL_GIGA_MAC_VER_27:
1349 case RTL_GIGA_MAC_VER_28:
1350 case RTL_GIGA_MAC_VER_31:
1351 r8168dp_ocp_write(tp, mask, reg, data);
1352 break;
1353 case RTL_GIGA_MAC_VER_49:
1354 case RTL_GIGA_MAC_VER_50:
1355 case RTL_GIGA_MAC_VER_51:
1356 r8168ep_ocp_write(tp, mask, reg, data);
1357 break;
1358 default:
1359 BUG();
1360 break;
1361 }
1362}
1363
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001364static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1365{
1366 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1367
1368 ocp_write(tp, 0x1, 0x30, 0x00000001);
1369}
1370
1371#define OOB_CMD_RESET 0x00
1372#define OOB_CMD_DRIVER_START 0x05
1373#define OOB_CMD_DRIVER_STOP 0x06
1374
1375static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1376{
1377 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1378}
1379
1380DECLARE_RTL_COND(rtl_ocp_read_cond)
1381{
1382 u16 reg;
1383
1384 reg = rtl8168_get_ocp_reg(tp);
1385
1386 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1387}
1388
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001389DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1390{
1391 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1392}
1393
1394DECLARE_RTL_COND(rtl_ocp_tx_cond)
1395{
1396 void __iomem *ioaddr = tp->mmio_addr;
1397
1398 return RTL_R8(IBISR0) & 0x02;
1399}
1400
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001401static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1402{
1403 void __iomem *ioaddr = tp->mmio_addr;
1404
1405 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1406 rtl_msleep_loop_wait_low(tp, &rtl_ocp_tx_cond, 50, 2000);
1407 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1408 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1409}
1410
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001411static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001412{
1413 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001414 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1415}
1416
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001417static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1418{
1419 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1420 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1421 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1422}
1423
1424static void rtl8168_driver_start(struct rtl8169_private *tp)
1425{
1426 switch (tp->mac_version) {
1427 case RTL_GIGA_MAC_VER_27:
1428 case RTL_GIGA_MAC_VER_28:
1429 case RTL_GIGA_MAC_VER_31:
1430 rtl8168dp_driver_start(tp);
1431 break;
1432 case RTL_GIGA_MAC_VER_49:
1433 case RTL_GIGA_MAC_VER_50:
1434 case RTL_GIGA_MAC_VER_51:
1435 rtl8168ep_driver_start(tp);
1436 break;
1437 default:
1438 BUG();
1439 break;
1440 }
1441}
1442
1443static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1444{
1445 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1446 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1447}
1448
1449static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1450{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001451 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001452 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1453 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1454 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1455}
1456
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001457static void rtl8168_driver_stop(struct rtl8169_private *tp)
1458{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001459 switch (tp->mac_version) {
1460 case RTL_GIGA_MAC_VER_27:
1461 case RTL_GIGA_MAC_VER_28:
1462 case RTL_GIGA_MAC_VER_31:
1463 rtl8168dp_driver_stop(tp);
1464 break;
1465 case RTL_GIGA_MAC_VER_49:
1466 case RTL_GIGA_MAC_VER_50:
1467 case RTL_GIGA_MAC_VER_51:
1468 rtl8168ep_driver_stop(tp);
1469 break;
1470 default:
1471 BUG();
1472 break;
1473 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001474}
1475
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001476static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001477{
1478 u16 reg = rtl8168_get_ocp_reg(tp);
1479
1480 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1481}
1482
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001483static int r8168ep_check_dash(struct rtl8169_private *tp)
1484{
1485 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1486}
1487
1488static int r8168_check_dash(struct rtl8169_private *tp)
1489{
1490 switch (tp->mac_version) {
1491 case RTL_GIGA_MAC_VER_27:
1492 case RTL_GIGA_MAC_VER_28:
1493 case RTL_GIGA_MAC_VER_31:
1494 return r8168dp_check_dash(tp);
1495 case RTL_GIGA_MAC_VER_49:
1496 case RTL_GIGA_MAC_VER_50:
1497 case RTL_GIGA_MAC_VER_51:
1498 return r8168ep_check_dash(tp);
1499 default:
1500 return 0;
1501 }
1502}
1503
françois romieuc28aa382011-08-02 03:53:43 +00001504struct exgmac_reg {
1505 u16 addr;
1506 u16 mask;
1507 u32 val;
1508};
1509
Francois Romieufdf6fc02012-07-06 22:40:38 +02001510static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001511 const struct exgmac_reg *r, int len)
1512{
1513 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001514 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001515 r++;
1516 }
1517}
1518
Francois Romieuffc46952012-07-06 14:19:23 +02001519DECLARE_RTL_COND(rtl_efusear_cond)
1520{
1521 void __iomem *ioaddr = tp->mmio_addr;
1522
1523 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1524}
1525
Francois Romieufdf6fc02012-07-06 22:40:38 +02001526static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001527{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001528 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001529
1530 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1531
Francois Romieuffc46952012-07-06 14:19:23 +02001532 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1533 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001534}
1535
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001536static u16 rtl_get_events(struct rtl8169_private *tp)
1537{
1538 void __iomem *ioaddr = tp->mmio_addr;
1539
1540 return RTL_R16(IntrStatus);
1541}
1542
1543static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1544{
1545 void __iomem *ioaddr = tp->mmio_addr;
1546
1547 RTL_W16(IntrStatus, bits);
1548 mmiowb();
1549}
1550
1551static void rtl_irq_disable(struct rtl8169_private *tp)
1552{
1553 void __iomem *ioaddr = tp->mmio_addr;
1554
1555 RTL_W16(IntrMask, 0);
1556 mmiowb();
1557}
1558
Francois Romieu3e990ff2012-01-26 12:50:01 +01001559static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1560{
1561 void __iomem *ioaddr = tp->mmio_addr;
1562
1563 RTL_W16(IntrMask, bits);
1564}
1565
Francois Romieuda78dbf2012-01-26 14:18:23 +01001566#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1567#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1568#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1569
1570static void rtl_irq_enable_all(struct rtl8169_private *tp)
1571{
1572 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1573}
1574
françois romieu811fd302011-12-04 20:30:45 +00001575static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
françois romieu811fd302011-12-04 20:30:45 +00001577 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001579 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001580 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001581 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
françois romieu4da19632011-01-03 15:07:55 +00001584static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
françois romieu4da19632011-01-03 15:07:55 +00001586 void __iomem *ioaddr = tp->mmio_addr;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return RTL_R32(TBICSR) & TBIReset;
1589}
1590
françois romieu4da19632011-01-03 15:07:55 +00001591static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
françois romieu4da19632011-01-03 15:07:55 +00001593 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1597{
1598 return RTL_R32(TBICSR) & TBILinkOk;
1599}
1600
1601static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1602{
1603 return RTL_R8(PHYstatus) & LinkStatus;
1604}
1605
françois romieu4da19632011-01-03 15:07:55 +00001606static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
françois romieu4da19632011-01-03 15:07:55 +00001608 void __iomem *ioaddr = tp->mmio_addr;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1611}
1612
françois romieu4da19632011-01-03 15:07:55 +00001613static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 unsigned int val;
1616
françois romieu4da19632011-01-03 15:07:55 +00001617 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1618 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Hayes Wang70090422011-07-06 15:58:06 +08001621static void rtl_link_chg_patch(struct rtl8169_private *tp)
1622{
1623 void __iomem *ioaddr = tp->mmio_addr;
1624 struct net_device *dev = tp->dev;
1625
1626 if (!netif_running(dev))
1627 return;
1628
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001629 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1630 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001631 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001632 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1633 ERIAR_EXGMAC);
1634 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1635 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001636 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001637 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1638 ERIAR_EXGMAC);
1639 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1640 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001641 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001642 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1643 ERIAR_EXGMAC);
1644 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1645 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001646 }
1647 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001648 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001649 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001650 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001651 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001652 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1653 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1654 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001655 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1656 ERIAR_EXGMAC);
1657 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1658 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001659 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001660 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1661 ERIAR_EXGMAC);
1662 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1663 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001664 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001665 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1666 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001667 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1668 ERIAR_EXGMAC);
1669 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1670 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001671 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001672 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1673 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001674 }
Hayes Wang70090422011-07-06 15:58:06 +08001675 }
1676}
1677
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001678static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001679 struct rtl8169_private *tp,
1680 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001683 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001684 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001685 if (pm)
1686 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001688 if (net_ratelimit())
1689 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001692 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001693 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001694 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001698static void rtl8169_check_link_status(struct net_device *dev,
1699 struct rtl8169_private *tp,
1700 void __iomem *ioaddr)
1701{
1702 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1703}
1704
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001705#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1706
1707static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1708{
1709 void __iomem *ioaddr = tp->mmio_addr;
1710 u8 options;
1711 u32 wolopts = 0;
1712
1713 options = RTL_R8(Config1);
1714 if (!(options & PMEnable))
1715 return 0;
1716
1717 options = RTL_R8(Config3);
1718 if (options & LinkUp)
1719 wolopts |= WAKE_PHY;
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 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1739 wolopts |= WAKE_MAGIC;
1740 break;
1741 default:
1742 if (options & MagicPacket)
1743 wolopts |= WAKE_MAGIC;
1744 break;
1745 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001746
1747 options = RTL_R8(Config5);
1748 if (options & UWF)
1749 wolopts |= WAKE_UCAST;
1750 if (options & BWF)
1751 wolopts |= WAKE_BCAST;
1752 if (options & MWF)
1753 wolopts |= WAKE_MCAST;
1754
1755 return wolopts;
1756}
1757
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001758static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1759{
1760 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001761 struct device *d = &tp->pci_dev->dev;
1762
1763 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001764
Francois Romieuda78dbf2012-01-26 14:18:23 +01001765 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001766
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001767 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001768 if (pm_runtime_active(d))
1769 wol->wolopts = __rtl8169_get_wol(tp);
1770 else
1771 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001772
Francois Romieuda78dbf2012-01-26 14:18:23 +01001773 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001774
1775 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001776}
1777
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001778static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001779{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001780 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001781 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001782 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001783 u32 opt;
1784 u16 reg;
1785 u8 mask;
1786 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001787 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001788 { WAKE_UCAST, Config5, UWF },
1789 { WAKE_BCAST, Config5, BWF },
1790 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001791 { WAKE_ANY, Config5, LanWake },
1792 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001793 };
Francois Romieu851e6022012-04-17 11:10:11 +02001794 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001795
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001796 RTL_W8(Cfg9346, Cfg9346_Unlock);
1797
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001798 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001799 case RTL_GIGA_MAC_VER_34:
1800 case RTL_GIGA_MAC_VER_35:
1801 case RTL_GIGA_MAC_VER_36:
1802 case RTL_GIGA_MAC_VER_37:
1803 case RTL_GIGA_MAC_VER_38:
1804 case RTL_GIGA_MAC_VER_40:
1805 case RTL_GIGA_MAC_VER_41:
1806 case RTL_GIGA_MAC_VER_42:
1807 case RTL_GIGA_MAC_VER_43:
1808 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001809 case RTL_GIGA_MAC_VER_45:
1810 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001811 case RTL_GIGA_MAC_VER_47:
1812 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001813 case RTL_GIGA_MAC_VER_49:
1814 case RTL_GIGA_MAC_VER_50:
1815 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001816 tmp = ARRAY_SIZE(cfg) - 1;
1817 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001818 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001819 0x0dc,
1820 ERIAR_MASK_0100,
1821 MagicPacket_v2,
1822 0x0000,
1823 ERIAR_EXGMAC);
1824 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001825 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001826 0x0dc,
1827 ERIAR_MASK_0100,
1828 0x0000,
1829 MagicPacket_v2,
1830 ERIAR_EXGMAC);
1831 break;
1832 default:
1833 tmp = ARRAY_SIZE(cfg);
1834 break;
1835 }
1836
1837 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001838 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001839 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001840 options |= cfg[i].mask;
1841 RTL_W8(cfg[i].reg, options);
1842 }
1843
Francois Romieu851e6022012-04-17 11:10:11 +02001844 switch (tp->mac_version) {
1845 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1846 options = RTL_R8(Config1) & ~PMEnable;
1847 if (wolopts)
1848 options |= PMEnable;
1849 RTL_W8(Config1, options);
1850 break;
1851 default:
Francois Romieud387b422012-04-17 11:12:01 +02001852 options = RTL_R8(Config2) & ~PME_SIGNAL;
1853 if (wolopts)
1854 options |= PME_SIGNAL;
1855 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001856 break;
1857 }
1858
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001859 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001860}
1861
1862static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1863{
1864 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001865 struct device *d = &tp->pci_dev->dev;
1866
1867 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001868
Francois Romieuda78dbf2012-01-26 14:18:23 +01001869 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001870
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001871 if (wol->wolopts)
1872 tp->features |= RTL_FEATURE_WOL;
1873 else
1874 tp->features &= ~RTL_FEATURE_WOL;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001875 if (pm_runtime_active(d))
1876 __rtl8169_set_wol(tp, wol->wolopts);
1877 else
1878 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001879
1880 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001881
françois romieuea809072010-11-08 13:23:58 +00001882 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1883
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001884 pm_runtime_put_noidle(d);
1885
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001886 return 0;
1887}
1888
Francois Romieu31bd2042011-04-26 18:58:59 +02001889static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1890{
Francois Romieu85bffe62011-04-27 08:22:39 +02001891 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001892}
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894static void rtl8169_get_drvinfo(struct net_device *dev,
1895 struct ethtool_drvinfo *info)
1896{
1897 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001898 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Rick Jones68aad782011-11-07 13:29:27 +00001900 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1901 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1902 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001903 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001904 if (!IS_ERR_OR_NULL(rtl_fw))
1905 strlcpy(info->fw_version, rtl_fw->version,
1906 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909static int rtl8169_get_regs_len(struct net_device *dev)
1910{
1911 return R8169_REGS_SIZE;
1912}
1913
1914static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001915 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 struct rtl8169_private *tp = netdev_priv(dev);
1918 void __iomem *ioaddr = tp->mmio_addr;
1919 int ret = 0;
1920 u32 reg;
1921
1922 reg = RTL_R32(TBICSR);
1923 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1924 (duplex == DUPLEX_FULL)) {
1925 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1926 } else if (autoneg == AUTONEG_ENABLE)
1927 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1928 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001929 netif_warn(tp, link, dev,
1930 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ret = -EOPNOTSUPP;
1932 }
1933
1934 return ret;
1935}
1936
1937static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001938 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001941 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001942 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Hayes Wang716b50a2011-02-22 17:26:18 +08001944 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001947 int auto_nego;
1948
françois romieu4da19632011-01-03 15:07:55 +00001949 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001950 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1951 ADVERTISE_100HALF | ADVERTISE_100FULL);
1952
1953 if (adv & ADVERTISED_10baseT_Half)
1954 auto_nego |= ADVERTISE_10HALF;
1955 if (adv & ADVERTISED_10baseT_Full)
1956 auto_nego |= ADVERTISE_10FULL;
1957 if (adv & ADVERTISED_100baseT_Half)
1958 auto_nego |= ADVERTISE_100HALF;
1959 if (adv & ADVERTISED_100baseT_Full)
1960 auto_nego |= ADVERTISE_100FULL;
1961
françois romieu3577aa12009-05-19 10:46:48 +00001962 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1963
françois romieu4da19632011-01-03 15:07:55 +00001964 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001965 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1966
1967 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001968 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001969 if (adv & ADVERTISED_1000baseT_Half)
1970 giga_ctrl |= ADVERTISE_1000HALF;
1971 if (adv & ADVERTISED_1000baseT_Full)
1972 giga_ctrl |= ADVERTISE_1000FULL;
1973 } else if (adv & (ADVERTISED_1000baseT_Half |
1974 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001975 netif_info(tp, link, dev,
1976 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001977 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
françois romieu3577aa12009-05-19 10:46:48 +00001980 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001981
françois romieu4da19632011-01-03 15:07:55 +00001982 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1983 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001984 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001985 if (speed == SPEED_10)
1986 bmcr = 0;
1987 else if (speed == SPEED_100)
1988 bmcr = BMCR_SPEED100;
1989 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001990 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001991
1992 if (duplex == DUPLEX_FULL)
1993 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001994 }
1995
françois romieu4da19632011-01-03 15:07:55 +00001996 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001997
Francois Romieucecb5fd2011-04-01 10:21:07 +02001998 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1999 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00002000 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00002001 rtl_writephy(tp, 0x17, 0x2138);
2002 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00002003 } else {
françois romieu4da19632011-01-03 15:07:55 +00002004 rtl_writephy(tp, 0x17, 0x2108);
2005 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00002006 }
2007 }
2008
Oliver Neukum54405cd2011-01-06 21:55:13 +01002009 rc = 0;
2010out:
2011 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01002015 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct rtl8169_private *tp = netdev_priv(dev);
2018 int ret;
2019
Oliver Neukum54405cd2011-01-06 21:55:13 +01002020 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002021 if (ret < 0)
2022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Francois Romieu4876cc12011-03-11 21:07:11 +01002024 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002025 (advertising & ADVERTISED_1000baseT_Full) &&
2026 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002028 }
2029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return ret;
2031}
2032
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002033static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2034 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Francois Romieud58d46b2011-05-03 16:38:29 +02002036 struct rtl8169_private *tp = netdev_priv(dev);
2037
Francois Romieu2b7b4312011-04-18 22:53:24 -07002038 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002039 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Francois Romieud58d46b2011-05-03 16:38:29 +02002041 if (dev->mtu > JUMBO_1K &&
2042 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2043 features &= ~NETIF_F_IP_CSUM;
2044
Michał Mirosław350fb322011-04-08 06:35:56 +00002045 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Francois Romieuda78dbf2012-01-26 14:18:23 +01002048static void __rtl8169_set_features(struct net_device *dev,
2049 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002052 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002053 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
hayeswang929a0312014-09-16 11:40:47 +08002055 rx_config = RTL_R32(RxConfig);
2056 if (features & NETIF_F_RXALL)
2057 rx_config |= (AcceptErr | AcceptRunt);
2058 else
2059 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
hayeswang929a0312014-09-16 11:40:47 +08002061 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002062
hayeswang929a0312014-09-16 11:40:47 +08002063 if (features & NETIF_F_RXCSUM)
2064 tp->cp_cmd |= RxChkSum;
2065 else
2066 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002067
hayeswang929a0312014-09-16 11:40:47 +08002068 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2069 tp->cp_cmd |= RxVlan;
2070 else
2071 tp->cp_cmd &= ~RxVlan;
2072
2073 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2074
2075 RTL_W16(CPlusCmd, tp->cp_cmd);
2076 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Francois Romieuda78dbf2012-01-26 14:18:23 +01002079static int rtl8169_set_features(struct net_device *dev,
2080 netdev_features_t features)
2081{
2082 struct rtl8169_private *tp = netdev_priv(dev);
2083
hayeswang929a0312014-09-16 11:40:47 +08002084 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2085
Francois Romieuda78dbf2012-01-26 14:18:23 +01002086 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002087 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002088 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002089 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 return 0;
2092}
2093
Francois Romieuda78dbf2012-01-26 14:18:23 +01002094
Kirill Smelkov810f4892012-11-10 21:11:02 +04002095static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002097 return (skb_vlan_tag_present(skb)) ?
2098 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Francois Romieu7a8fc772011-03-01 17:18:33 +01002101static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Francois Romieu7a8fc772011-03-01 17:18:33 +01002105 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002106 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002109static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2110 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
2112 struct rtl8169_private *tp = netdev_priv(dev);
2113 void __iomem *ioaddr = tp->mmio_addr;
2114 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002115 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002117 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002119 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 status = RTL_R32(TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002122 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2123 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002125 cmd->base.speed = SPEED_1000;
2126 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2127
2128 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2129 supported);
2130 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2131 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002132
2133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002136static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2137 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002141 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2142
2143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002146static int rtl8169_get_link_ksettings(struct net_device *dev,
2147 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002150 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Francois Romieuda78dbf2012-01-26 14:18:23 +01002152 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002153 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002154 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Francois Romieuccdffb92008-07-26 14:26:06 +02002156 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002159static int rtl8169_set_link_ksettings(struct net_device *dev,
2160 const struct ethtool_link_ksettings *cmd)
2161{
2162 struct rtl8169_private *tp = netdev_priv(dev);
2163 int rc;
2164 u32 advertising;
2165
2166 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2167 cmd->link_modes.advertising))
2168 return -EINVAL;
2169
2170 del_timer_sync(&tp->timer);
2171
2172 rtl_lock_work(tp);
2173 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2174 cmd->base.duplex, advertising);
2175 rtl_unlock_work(tp);
2176
2177 return rc;
2178}
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2181 void *p)
2182{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002183 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002184 u32 __iomem *data = tp->mmio_addr;
2185 u32 *dw = p;
2186 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Francois Romieuda78dbf2012-01-26 14:18:23 +01002188 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002189 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2190 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002191 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002194static u32 rtl8169_get_msglevel(struct net_device *dev)
2195{
2196 struct rtl8169_private *tp = netdev_priv(dev);
2197
2198 return tp->msg_enable;
2199}
2200
2201static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2202{
2203 struct rtl8169_private *tp = netdev_priv(dev);
2204
2205 tp->msg_enable = value;
2206}
2207
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002208static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2209 "tx_packets",
2210 "rx_packets",
2211 "tx_errors",
2212 "rx_errors",
2213 "rx_missed",
2214 "align_errors",
2215 "tx_single_collisions",
2216 "tx_multi_collisions",
2217 "unicast",
2218 "broadcast",
2219 "multicast",
2220 "tx_aborted",
2221 "tx_underrun",
2222};
2223
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002224static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002225{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002226 switch (sset) {
2227 case ETH_SS_STATS:
2228 return ARRAY_SIZE(rtl8169_gstrings);
2229 default:
2230 return -EOPNOTSUPP;
2231 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002232}
2233
Corinna Vinschen42020322015-09-10 10:47:35 +02002234DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002235{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002236 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002237
Corinna Vinschen42020322015-09-10 10:47:35 +02002238 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002239}
2240
Corinna Vinschen42020322015-09-10 10:47:35 +02002241static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002242{
2243 struct rtl8169_private *tp = netdev_priv(dev);
2244 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002245 dma_addr_t paddr = tp->counters_phys_addr;
2246 u32 cmd;
2247 bool ret;
2248
2249 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
2250 cmd = (u64)paddr & DMA_BIT_MASK(32);
2251 RTL_W32(CounterAddrLow, cmd);
2252 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2253
2254 ret = rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002255
2256 RTL_W32(CounterAddrLow, 0);
2257 RTL_W32(CounterAddrHigh, 0);
2258
Corinna Vinschen42020322015-09-10 10:47:35 +02002259 return ret;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002260}
2261
2262static bool rtl8169_reset_counters(struct net_device *dev)
2263{
2264 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002265
2266 /*
2267 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2268 * tally counters.
2269 */
2270 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2271 return true;
2272
Corinna Vinschen42020322015-09-10 10:47:35 +02002273 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002274}
2275
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002276static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002277{
2278 struct rtl8169_private *tp = netdev_priv(dev);
2279 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002280
Ivan Vecera355423d2009-02-06 21:49:57 -08002281 /*
2282 * Some chips are unable to dump tally counters when the receiver
2283 * is disabled.
2284 */
2285 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002286 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002287
Corinna Vinschen42020322015-09-10 10:47:35 +02002288 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002289}
2290
2291static bool rtl8169_init_counter_offsets(struct net_device *dev)
2292{
2293 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002294 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002295 bool ret = false;
2296
2297 /*
2298 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2299 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2300 * reset by a power cycle, while the counter values collected by the
2301 * driver are reset at every driver unload/load cycle.
2302 *
2303 * To make sure the HW values returned by @get_stats64 match the SW
2304 * values, we collect the initial values at first open(*) and use them
2305 * as offsets to normalize the values returned by @get_stats64.
2306 *
2307 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2308 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2309 * set at open time by rtl_hw_start.
2310 */
2311
2312 if (tp->tc_offset.inited)
2313 return true;
2314
2315 /* If both, reset and update fail, propagate to caller. */
2316 if (rtl8169_reset_counters(dev))
2317 ret = true;
2318
2319 if (rtl8169_update_counters(dev))
2320 ret = true;
2321
Corinna Vinschen42020322015-09-10 10:47:35 +02002322 tp->tc_offset.tx_errors = counters->tx_errors;
2323 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2324 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002325 tp->tc_offset.inited = true;
2326
2327 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002328}
2329
Ivan Vecera355423d2009-02-06 21:49:57 -08002330static void rtl8169_get_ethtool_stats(struct net_device *dev,
2331 struct ethtool_stats *stats, u64 *data)
2332{
2333 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002334 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002335 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002336
2337 ASSERT_RTNL();
2338
Chun-Hao Line0636232016-07-29 16:37:55 +08002339 pm_runtime_get_noresume(d);
2340
2341 if (pm_runtime_active(d))
2342 rtl8169_update_counters(dev);
2343
2344 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002345
Corinna Vinschen42020322015-09-10 10:47:35 +02002346 data[0] = le64_to_cpu(counters->tx_packets);
2347 data[1] = le64_to_cpu(counters->rx_packets);
2348 data[2] = le64_to_cpu(counters->tx_errors);
2349 data[3] = le32_to_cpu(counters->rx_errors);
2350 data[4] = le16_to_cpu(counters->rx_missed);
2351 data[5] = le16_to_cpu(counters->align_errors);
2352 data[6] = le32_to_cpu(counters->tx_one_collision);
2353 data[7] = le32_to_cpu(counters->tx_multi_collision);
2354 data[8] = le64_to_cpu(counters->rx_unicast);
2355 data[9] = le64_to_cpu(counters->rx_broadcast);
2356 data[10] = le32_to_cpu(counters->rx_multicast);
2357 data[11] = le16_to_cpu(counters->tx_aborted);
2358 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002359}
2360
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002361static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2362{
2363 switch(stringset) {
2364 case ETH_SS_STATS:
2365 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2366 break;
2367 }
2368}
2369
Florian Fainellif0903ea2016-12-03 12:01:19 -08002370static int rtl8169_nway_reset(struct net_device *dev)
2371{
2372 struct rtl8169_private *tp = netdev_priv(dev);
2373
2374 return mii_nway_restart(&tp->mii);
2375}
2376
Francois Romieu50970832017-10-27 13:24:49 +03002377/*
2378 * Interrupt coalescing
2379 *
2380 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2381 * > 8169, 8168 and 810x line of chipsets
2382 *
2383 * 8169, 8168, and 8136(810x) serial chipsets support it.
2384 *
2385 * > 2 - the Tx timer unit at gigabit speed
2386 *
2387 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2388 * (0xe0) bit 1 and bit 0.
2389 *
2390 * For 8169
2391 * bit[1:0] \ speed 1000M 100M 10M
2392 * 0 0 320ns 2.56us 40.96us
2393 * 0 1 2.56us 20.48us 327.7us
2394 * 1 0 5.12us 40.96us 655.4us
2395 * 1 1 10.24us 81.92us 1.31ms
2396 *
2397 * For the other
2398 * bit[1:0] \ speed 1000M 100M 10M
2399 * 0 0 5us 2.56us 40.96us
2400 * 0 1 40us 20.48us 327.7us
2401 * 1 0 80us 40.96us 655.4us
2402 * 1 1 160us 81.92us 1.31ms
2403 */
2404
2405/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2406struct rtl_coalesce_scale {
2407 /* Rx / Tx */
2408 u32 nsecs[2];
2409};
2410
2411/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2412struct rtl_coalesce_info {
2413 u32 speed;
2414 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2415};
2416
2417/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2418#define rxtx_x1822(r, t) { \
2419 {{(r), (t)}}, \
2420 {{(r)*8, (t)*8}}, \
2421 {{(r)*8*2, (t)*8*2}}, \
2422 {{(r)*8*2*2, (t)*8*2*2}}, \
2423}
2424static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2425 /* speed delays: rx00 tx00 */
2426 { SPEED_10, rxtx_x1822(40960, 40960) },
2427 { SPEED_100, rxtx_x1822( 2560, 2560) },
2428 { SPEED_1000, rxtx_x1822( 320, 320) },
2429 { 0 },
2430};
2431
2432static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2433 /* speed delays: rx00 tx00 */
2434 { SPEED_10, rxtx_x1822(40960, 40960) },
2435 { SPEED_100, rxtx_x1822( 2560, 2560) },
2436 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2437 { 0 },
2438};
2439#undef rxtx_x1822
2440
2441/* get rx/tx scale vector corresponding to current speed */
2442static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2443{
2444 struct rtl8169_private *tp = netdev_priv(dev);
2445 struct ethtool_link_ksettings ecmd;
2446 const struct rtl_coalesce_info *ci;
2447 int rc;
2448
2449 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2450 if (rc < 0)
2451 return ERR_PTR(rc);
2452
2453 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2454 if (ecmd.base.speed == ci->speed) {
2455 return ci;
2456 }
2457 }
2458
2459 return ERR_PTR(-ELNRNG);
2460}
2461
2462static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2463{
2464 struct rtl8169_private *tp = netdev_priv(dev);
2465 void __iomem *ioaddr = tp->mmio_addr;
2466 const struct rtl_coalesce_info *ci;
2467 const struct rtl_coalesce_scale *scale;
2468 struct {
2469 u32 *max_frames;
2470 u32 *usecs;
2471 } coal_settings [] = {
2472 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2473 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2474 }, *p = coal_settings;
2475 int i;
2476 u16 w;
2477
2478 memset(ec, 0, sizeof(*ec));
2479
2480 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2481 ci = rtl_coalesce_info(dev);
2482 if (IS_ERR(ci))
2483 return PTR_ERR(ci);
2484
2485 scale = &ci->scalev[RTL_R16(CPlusCmd) & 3];
2486
2487 /* read IntrMitigate and adjust according to scale */
2488 for (w = RTL_R16(IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2489 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2490 w >>= RTL_COALESCE_SHIFT;
2491 *p->usecs = w & RTL_COALESCE_MASK;
2492 }
2493
2494 for (i = 0; i < 2; i++) {
2495 p = coal_settings + i;
2496 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2497
2498 /*
2499 * ethtool_coalesce says it is illegal to set both usecs and
2500 * max_frames to 0.
2501 */
2502 if (!*p->usecs && !*p->max_frames)
2503 *p->max_frames = 1;
2504 }
2505
2506 return 0;
2507}
2508
2509/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2510static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2511 struct net_device *dev, u32 nsec, u16 *cp01)
2512{
2513 const struct rtl_coalesce_info *ci;
2514 u16 i;
2515
2516 ci = rtl_coalesce_info(dev);
2517 if (IS_ERR(ci))
2518 return ERR_CAST(ci);
2519
2520 for (i = 0; i < 4; i++) {
2521 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2522 ci->scalev[i].nsecs[1]);
2523 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2524 *cp01 = i;
2525 return &ci->scalev[i];
2526 }
2527 }
2528
2529 return ERR_PTR(-EINVAL);
2530}
2531
2532static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2533{
2534 struct rtl8169_private *tp = netdev_priv(dev);
2535 void __iomem *ioaddr = tp->mmio_addr;
2536 const struct rtl_coalesce_scale *scale;
2537 struct {
2538 u32 frames;
2539 u32 usecs;
2540 } coal_settings [] = {
2541 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2542 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2543 }, *p = coal_settings;
2544 u16 w = 0, cp01;
2545 int i;
2546
2547 scale = rtl_coalesce_choose_scale(dev,
2548 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2549 if (IS_ERR(scale))
2550 return PTR_ERR(scale);
2551
2552 for (i = 0; i < 2; i++, p++) {
2553 u32 units;
2554
2555 /*
2556 * accept max_frames=1 we returned in rtl_get_coalesce.
2557 * accept it not only when usecs=0 because of e.g. the following scenario:
2558 *
2559 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2560 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2561 * - then user does `ethtool -C eth0 rx-usecs 100`
2562 *
2563 * since ethtool sends to kernel whole ethtool_coalesce
2564 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2565 * we'll reject it below in `frames % 4 != 0`.
2566 */
2567 if (p->frames == 1) {
2568 p->frames = 0;
2569 }
2570
2571 units = p->usecs * 1000 / scale->nsecs[i];
2572 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2573 return -EINVAL;
2574
2575 w <<= RTL_COALESCE_SHIFT;
2576 w |= units;
2577 w <<= RTL_COALESCE_SHIFT;
2578 w |= p->frames >> 2;
2579 }
2580
2581 rtl_lock_work(tp);
2582
2583 RTL_W16(IntrMitigate, swab16(w));
2584
2585 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2586 RTL_W16(CPlusCmd, tp->cp_cmd);
2587 RTL_R16(CPlusCmd);
2588
2589 rtl_unlock_work(tp);
2590
2591 return 0;
2592}
2593
Jeff Garzik7282d492006-09-13 14:30:00 -04002594static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 .get_drvinfo = rtl8169_get_drvinfo,
2596 .get_regs_len = rtl8169_get_regs_len,
2597 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002598 .get_coalesce = rtl_get_coalesce,
2599 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002600 .get_msglevel = rtl8169_get_msglevel,
2601 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002603 .get_wol = rtl8169_get_wol,
2604 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002605 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002606 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002607 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002608 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002609 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002610 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002611 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612};
2613
Francois Romieu07d3f512007-02-21 22:40:46 +01002614static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002615 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Francois Romieu5d320a22011-05-08 17:47:36 +02002617 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002618 /*
2619 * The driver currently handles the 8168Bf and the 8168Be identically
2620 * but they can be identified more specifically through the test below
2621 * if needed:
2622 *
2623 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002624 *
2625 * Same thing for the 8101Eb and the 8101Ec:
2626 *
2627 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002628 */
Francois Romieu37441002011-06-17 22:58:54 +02002629 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002631 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 int mac_version;
2633 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002634 /* 8168EP family. */
2635 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2636 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2637 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2638
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002639 /* 8168H family. */
2640 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2641 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2642
Hayes Wangc5583862012-07-02 17:23:22 +08002643 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002644 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002645 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002646 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2647 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2648
Hayes Wangc2218922011-09-06 16:55:18 +08002649 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002650 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002651 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2652 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2653
hayeswang01dc7fe2011-03-21 01:50:28 +00002654 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002655 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002656 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2657 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2658 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2659
Francois Romieu5b538df2008-07-20 16:22:45 +02002660 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002661 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2662 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002663 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002664
françois romieue6de30d2011-01-03 15:08:37 +00002665 /* 8168DP family. */
2666 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2667 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002668 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002669
Francois Romieuef808d52008-06-29 13:10:54 +02002670 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002671 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002672 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002673 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002674 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002675 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2676 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002677 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002678 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002679 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002680
2681 /* 8168B family. */
2682 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2683 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2684 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2685 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2686
2687 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002688 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2689 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002690 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002691 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002692 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2693 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2694 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002695 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2696 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2697 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2698 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2699 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2700 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002701 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002702 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002703 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002704 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2705 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002706 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2707 /* FIXME: where did these entries come from ? -- FR */
2708 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2709 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2710
2711 /* 8110 family. */
2712 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2713 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2714 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2715 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2716 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2717 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2718
Jean Delvaref21b75e2009-05-26 20:54:48 -07002719 /* Catch-all */
2720 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002721 };
2722 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 u32 reg;
2724
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002725 reg = RTL_R32(TxConfig);
2726 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 p++;
2728 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002729
2730 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2731 netif_notice(tp, probe, dev,
2732 "unknown MAC, using family default\n");
2733 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002734 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2735 tp->mac_version = tp->mii.supports_gmii ?
2736 RTL_GIGA_MAC_VER_42 :
2737 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002738 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2739 tp->mac_version = tp->mii.supports_gmii ?
2740 RTL_GIGA_MAC_VER_45 :
2741 RTL_GIGA_MAC_VER_47;
2742 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2743 tp->mac_version = tp->mii.supports_gmii ?
2744 RTL_GIGA_MAC_VER_46 :
2745 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2750{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002751 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
Francois Romieu867763c2007-08-17 18:21:58 +02002754struct phy_reg {
2755 u16 reg;
2756 u16 val;
2757};
2758
françois romieu4da19632011-01-03 15:07:55 +00002759static void rtl_writephy_batch(struct rtl8169_private *tp,
2760 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002761{
2762 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002763 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002764 regs++;
2765 }
2766}
2767
françois romieubca03d52011-01-03 15:07:31 +00002768#define PHY_READ 0x00000000
2769#define PHY_DATA_OR 0x10000000
2770#define PHY_DATA_AND 0x20000000
2771#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002772#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002773#define PHY_CLEAR_READCOUNT 0x70000000
2774#define PHY_WRITE 0x80000000
2775#define PHY_READCOUNT_EQ_SKIP 0x90000000
2776#define PHY_COMP_EQ_SKIPN 0xa0000000
2777#define PHY_COMP_NEQ_SKIPN 0xb0000000
2778#define PHY_WRITE_PREVIOUS 0xc0000000
2779#define PHY_SKIPN 0xd0000000
2780#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002781
Hayes Wang960aee62011-06-18 11:37:48 +02002782struct fw_info {
2783 u32 magic;
2784 char version[RTL_VER_SIZE];
2785 __le32 fw_start;
2786 __le32 fw_len;
2787 u8 chksum;
2788} __packed;
2789
Francois Romieu1c361ef2011-06-17 17:16:24 +02002790#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2791
2792static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002793{
Francois Romieub6ffd972011-06-17 17:00:05 +02002794 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002795 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002796 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2797 char *version = rtl_fw->version;
2798 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002799
Francois Romieu1c361ef2011-06-17 17:16:24 +02002800 if (fw->size < FW_OPCODE_SIZE)
2801 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002802
2803 if (!fw_info->magic) {
2804 size_t i, size, start;
2805 u8 checksum = 0;
2806
2807 if (fw->size < sizeof(*fw_info))
2808 goto out;
2809
2810 for (i = 0; i < fw->size; i++)
2811 checksum += fw->data[i];
2812 if (checksum != 0)
2813 goto out;
2814
2815 start = le32_to_cpu(fw_info->fw_start);
2816 if (start > fw->size)
2817 goto out;
2818
2819 size = le32_to_cpu(fw_info->fw_len);
2820 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2821 goto out;
2822
2823 memcpy(version, fw_info->version, RTL_VER_SIZE);
2824
2825 pa->code = (__le32 *)(fw->data + start);
2826 pa->size = size;
2827 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002828 if (fw->size % FW_OPCODE_SIZE)
2829 goto out;
2830
2831 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2832
2833 pa->code = (__le32 *)fw->data;
2834 pa->size = fw->size / FW_OPCODE_SIZE;
2835 }
2836 version[RTL_VER_SIZE - 1] = 0;
2837
2838 rc = true;
2839out:
2840 return rc;
2841}
2842
Francois Romieufd112f22011-06-18 00:10:29 +02002843static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2844 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002845{
Francois Romieufd112f22011-06-18 00:10:29 +02002846 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002847 size_t index;
2848
Francois Romieu1c361ef2011-06-17 17:16:24 +02002849 for (index = 0; index < pa->size; index++) {
2850 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002851 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002852
hayeswang42b82dc2011-01-10 02:07:25 +00002853 switch(action & 0xf0000000) {
2854 case PHY_READ:
2855 case PHY_DATA_OR:
2856 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002857 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002858 case PHY_CLEAR_READCOUNT:
2859 case PHY_WRITE:
2860 case PHY_WRITE_PREVIOUS:
2861 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002862 break;
2863
hayeswang42b82dc2011-01-10 02:07:25 +00002864 case PHY_BJMPN:
2865 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002866 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002867 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002868 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002869 }
2870 break;
2871 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002872 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002873 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002874 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002875 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002876 }
2877 break;
2878 case PHY_COMP_EQ_SKIPN:
2879 case PHY_COMP_NEQ_SKIPN:
2880 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002881 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002882 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002883 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002884 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002885 }
2886 break;
2887
hayeswang42b82dc2011-01-10 02:07:25 +00002888 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002889 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002890 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002891 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002892 }
2893 }
Francois Romieufd112f22011-06-18 00:10:29 +02002894 rc = true;
2895out:
2896 return rc;
2897}
françois romieubca03d52011-01-03 15:07:31 +00002898
Francois Romieufd112f22011-06-18 00:10:29 +02002899static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2900{
2901 struct net_device *dev = tp->dev;
2902 int rc = -EINVAL;
2903
2904 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002905 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002906 goto out;
2907 }
2908
2909 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2910 rc = 0;
2911out:
2912 return rc;
2913}
2914
2915static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2916{
2917 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002918 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002919 u32 predata, count;
2920 size_t index;
2921
2922 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002923 org.write = ops->write;
2924 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002925
Francois Romieu1c361ef2011-06-17 17:16:24 +02002926 for (index = 0; index < pa->size; ) {
2927 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002928 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002929 u32 regno = (action & 0x0fff0000) >> 16;
2930
2931 if (!action)
2932 break;
françois romieubca03d52011-01-03 15:07:31 +00002933
2934 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002935 case PHY_READ:
2936 predata = rtl_readphy(tp, regno);
2937 count++;
2938 index++;
françois romieubca03d52011-01-03 15:07:31 +00002939 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002940 case PHY_DATA_OR:
2941 predata |= data;
2942 index++;
2943 break;
2944 case PHY_DATA_AND:
2945 predata &= data;
2946 index++;
2947 break;
2948 case PHY_BJMPN:
2949 index -= regno;
2950 break;
hayeswangeee37862013-04-01 22:23:38 +00002951 case PHY_MDIO_CHG:
2952 if (data == 0) {
2953 ops->write = org.write;
2954 ops->read = org.read;
2955 } else if (data == 1) {
2956 ops->write = mac_mcu_write;
2957 ops->read = mac_mcu_read;
2958 }
2959
hayeswang42b82dc2011-01-10 02:07:25 +00002960 index++;
2961 break;
2962 case PHY_CLEAR_READCOUNT:
2963 count = 0;
2964 index++;
2965 break;
2966 case PHY_WRITE:
2967 rtl_writephy(tp, regno, data);
2968 index++;
2969 break;
2970 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002971 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002972 break;
2973 case PHY_COMP_EQ_SKIPN:
2974 if (predata == data)
2975 index += regno;
2976 index++;
2977 break;
2978 case PHY_COMP_NEQ_SKIPN:
2979 if (predata != data)
2980 index += regno;
2981 index++;
2982 break;
2983 case PHY_WRITE_PREVIOUS:
2984 rtl_writephy(tp, regno, predata);
2985 index++;
2986 break;
2987 case PHY_SKIPN:
2988 index += regno + 1;
2989 break;
2990 case PHY_DELAY_MS:
2991 mdelay(data);
2992 index++;
2993 break;
2994
françois romieubca03d52011-01-03 15:07:31 +00002995 default:
2996 BUG();
2997 }
2998 }
hayeswangeee37862013-04-01 22:23:38 +00002999
3000 ops->write = org.write;
3001 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00003002}
3003
françois romieuf1e02ed2011-01-13 13:07:53 +00003004static void rtl_release_firmware(struct rtl8169_private *tp)
3005{
Francois Romieub6ffd972011-06-17 17:00:05 +02003006 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
3007 release_firmware(tp->rtl_fw->fw);
3008 kfree(tp->rtl_fw);
3009 }
3010 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00003011}
3012
François Romieu953a12c2011-04-24 17:38:48 +02003013static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00003014{
Francois Romieub6ffd972011-06-17 17:00:05 +02003015 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00003016
3017 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01003018 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02003019 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02003020}
3021
3022static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
3023{
3024 if (rtl_readphy(tp, reg) != val)
3025 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
3026 else
3027 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00003028}
3029
françois romieu4da19632011-01-03 15:07:55 +00003030static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003032 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00003033 { 0x1f, 0x0001 },
3034 { 0x06, 0x006e },
3035 { 0x08, 0x0708 },
3036 { 0x15, 0x4000 },
3037 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
françois romieu0b9b5712009-08-10 19:44:56 +00003039 { 0x1f, 0x0001 },
3040 { 0x03, 0x00a1 },
3041 { 0x02, 0x0008 },
3042 { 0x01, 0x0120 },
3043 { 0x00, 0x1000 },
3044 { 0x04, 0x0800 },
3045 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
françois romieu0b9b5712009-08-10 19:44:56 +00003047 { 0x03, 0xff41 },
3048 { 0x02, 0xdf60 },
3049 { 0x01, 0x0140 },
3050 { 0x00, 0x0077 },
3051 { 0x04, 0x7800 },
3052 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
françois romieu0b9b5712009-08-10 19:44:56 +00003054 { 0x03, 0x802f },
3055 { 0x02, 0x4f02 },
3056 { 0x01, 0x0409 },
3057 { 0x00, 0xf0f9 },
3058 { 0x04, 0x9800 },
3059 { 0x04, 0x9000 },
3060
3061 { 0x03, 0xdf01 },
3062 { 0x02, 0xdf20 },
3063 { 0x01, 0xff95 },
3064 { 0x00, 0xba00 },
3065 { 0x04, 0xa800 },
3066 { 0x04, 0xa000 },
3067
3068 { 0x03, 0xff41 },
3069 { 0x02, 0xdf20 },
3070 { 0x01, 0x0140 },
3071 { 0x00, 0x00bb },
3072 { 0x04, 0xb800 },
3073 { 0x04, 0xb000 },
3074
3075 { 0x03, 0xdf41 },
3076 { 0x02, 0xdc60 },
3077 { 0x01, 0x6340 },
3078 { 0x00, 0x007d },
3079 { 0x04, 0xd800 },
3080 { 0x04, 0xd000 },
3081
3082 { 0x03, 0xdf01 },
3083 { 0x02, 0xdf20 },
3084 { 0x01, 0x100a },
3085 { 0x00, 0xa0ff },
3086 { 0x04, 0xf800 },
3087 { 0x04, 0xf000 },
3088
3089 { 0x1f, 0x0000 },
3090 { 0x0b, 0x0000 },
3091 { 0x00, 0x9200 }
3092 };
3093
françois romieu4da19632011-01-03 15:07:55 +00003094 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
françois romieu4da19632011-01-03 15:07:55 +00003097static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003098{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003099 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003100 { 0x1f, 0x0002 },
3101 { 0x01, 0x90d0 },
3102 { 0x1f, 0x0000 }
3103 };
3104
françois romieu4da19632011-01-03 15:07:55 +00003105 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003106}
3107
françois romieu4da19632011-01-03 15:07:55 +00003108static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003109{
3110 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003111
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003112 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3113 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003114 return;
3115
françois romieu4da19632011-01-03 15:07:55 +00003116 rtl_writephy(tp, 0x1f, 0x0001);
3117 rtl_writephy(tp, 0x10, 0xf01b);
3118 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003119}
3120
françois romieu4da19632011-01-03 15:07:55 +00003121static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003122{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003123 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003124 { 0x1f, 0x0001 },
3125 { 0x04, 0x0000 },
3126 { 0x03, 0x00a1 },
3127 { 0x02, 0x0008 },
3128 { 0x01, 0x0120 },
3129 { 0x00, 0x1000 },
3130 { 0x04, 0x0800 },
3131 { 0x04, 0x9000 },
3132 { 0x03, 0x802f },
3133 { 0x02, 0x4f02 },
3134 { 0x01, 0x0409 },
3135 { 0x00, 0xf099 },
3136 { 0x04, 0x9800 },
3137 { 0x04, 0xa000 },
3138 { 0x03, 0xdf01 },
3139 { 0x02, 0xdf20 },
3140 { 0x01, 0xff95 },
3141 { 0x00, 0xba00 },
3142 { 0x04, 0xa800 },
3143 { 0x04, 0xf000 },
3144 { 0x03, 0xdf01 },
3145 { 0x02, 0xdf20 },
3146 { 0x01, 0x101a },
3147 { 0x00, 0xa0ff },
3148 { 0x04, 0xf800 },
3149 { 0x04, 0x0000 },
3150 { 0x1f, 0x0000 },
3151
3152 { 0x1f, 0x0001 },
3153 { 0x10, 0xf41b },
3154 { 0x14, 0xfb54 },
3155 { 0x18, 0xf5c7 },
3156 { 0x1f, 0x0000 },
3157
3158 { 0x1f, 0x0001 },
3159 { 0x17, 0x0cc0 },
3160 { 0x1f, 0x0000 }
3161 };
3162
françois romieu4da19632011-01-03 15:07:55 +00003163 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003164
françois romieu4da19632011-01-03 15:07:55 +00003165 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003166}
3167
françois romieu4da19632011-01-03 15:07:55 +00003168static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003169{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003170 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003171 { 0x1f, 0x0001 },
3172 { 0x04, 0x0000 },
3173 { 0x03, 0x00a1 },
3174 { 0x02, 0x0008 },
3175 { 0x01, 0x0120 },
3176 { 0x00, 0x1000 },
3177 { 0x04, 0x0800 },
3178 { 0x04, 0x9000 },
3179 { 0x03, 0x802f },
3180 { 0x02, 0x4f02 },
3181 { 0x01, 0x0409 },
3182 { 0x00, 0xf099 },
3183 { 0x04, 0x9800 },
3184 { 0x04, 0xa000 },
3185 { 0x03, 0xdf01 },
3186 { 0x02, 0xdf20 },
3187 { 0x01, 0xff95 },
3188 { 0x00, 0xba00 },
3189 { 0x04, 0xa800 },
3190 { 0x04, 0xf000 },
3191 { 0x03, 0xdf01 },
3192 { 0x02, 0xdf20 },
3193 { 0x01, 0x101a },
3194 { 0x00, 0xa0ff },
3195 { 0x04, 0xf800 },
3196 { 0x04, 0x0000 },
3197 { 0x1f, 0x0000 },
3198
3199 { 0x1f, 0x0001 },
3200 { 0x0b, 0x8480 },
3201 { 0x1f, 0x0000 },
3202
3203 { 0x1f, 0x0001 },
3204 { 0x18, 0x67c7 },
3205 { 0x04, 0x2000 },
3206 { 0x03, 0x002f },
3207 { 0x02, 0x4360 },
3208 { 0x01, 0x0109 },
3209 { 0x00, 0x3022 },
3210 { 0x04, 0x2800 },
3211 { 0x1f, 0x0000 },
3212
3213 { 0x1f, 0x0001 },
3214 { 0x17, 0x0cc0 },
3215 { 0x1f, 0x0000 }
3216 };
3217
françois romieu4da19632011-01-03 15:07:55 +00003218 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003219}
3220
françois romieu4da19632011-01-03 15:07:55 +00003221static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003222{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003223 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003224 { 0x10, 0xf41b },
3225 { 0x1f, 0x0000 }
3226 };
3227
françois romieu4da19632011-01-03 15:07:55 +00003228 rtl_writephy(tp, 0x1f, 0x0001);
3229 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003230
françois romieu4da19632011-01-03 15:07:55 +00003231 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003232}
3233
françois romieu4da19632011-01-03 15:07:55 +00003234static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003235{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003236 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003237 { 0x1f, 0x0001 },
3238 { 0x10, 0xf41b },
3239 { 0x1f, 0x0000 }
3240 };
3241
françois romieu4da19632011-01-03 15:07:55 +00003242 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003243}
3244
françois romieu4da19632011-01-03 15:07:55 +00003245static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003246{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003247 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003248 { 0x1f, 0x0000 },
3249 { 0x1d, 0x0f00 },
3250 { 0x1f, 0x0002 },
3251 { 0x0c, 0x1ec8 },
3252 { 0x1f, 0x0000 }
3253 };
3254
françois romieu4da19632011-01-03 15:07:55 +00003255 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003256}
3257
françois romieu4da19632011-01-03 15:07:55 +00003258static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003259{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003260 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003261 { 0x1f, 0x0001 },
3262 { 0x1d, 0x3d98 },
3263 { 0x1f, 0x0000 }
3264 };
3265
françois romieu4da19632011-01-03 15:07:55 +00003266 rtl_writephy(tp, 0x1f, 0x0000);
3267 rtl_patchphy(tp, 0x14, 1 << 5);
3268 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003269
françois romieu4da19632011-01-03 15:07:55 +00003270 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003271}
3272
françois romieu4da19632011-01-03 15:07:55 +00003273static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003274{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003275 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003276 { 0x1f, 0x0001 },
3277 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003278 { 0x1f, 0x0002 },
3279 { 0x00, 0x88d4 },
3280 { 0x01, 0x82b1 },
3281 { 0x03, 0x7002 },
3282 { 0x08, 0x9e30 },
3283 { 0x09, 0x01f0 },
3284 { 0x0a, 0x5500 },
3285 { 0x0c, 0x00c8 },
3286 { 0x1f, 0x0003 },
3287 { 0x12, 0xc096 },
3288 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003289 { 0x1f, 0x0000 },
3290 { 0x1f, 0x0000 },
3291 { 0x09, 0x2000 },
3292 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003293 };
3294
françois romieu4da19632011-01-03 15:07:55 +00003295 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003296
françois romieu4da19632011-01-03 15:07:55 +00003297 rtl_patchphy(tp, 0x14, 1 << 5);
3298 rtl_patchphy(tp, 0x0d, 1 << 5);
3299 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003300}
3301
françois romieu4da19632011-01-03 15:07:55 +00003302static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003303{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003304 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003305 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003306 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003307 { 0x03, 0x802f },
3308 { 0x02, 0x4f02 },
3309 { 0x01, 0x0409 },
3310 { 0x00, 0xf099 },
3311 { 0x04, 0x9800 },
3312 { 0x04, 0x9000 },
3313 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003314 { 0x1f, 0x0002 },
3315 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003316 { 0x06, 0x0761 },
3317 { 0x1f, 0x0003 },
3318 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003319 { 0x1f, 0x0000 }
3320 };
3321
françois romieu4da19632011-01-03 15:07:55 +00003322 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003323
françois romieu4da19632011-01-03 15:07:55 +00003324 rtl_patchphy(tp, 0x16, 1 << 0);
3325 rtl_patchphy(tp, 0x14, 1 << 5);
3326 rtl_patchphy(tp, 0x0d, 1 << 5);
3327 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003328}
3329
françois romieu4da19632011-01-03 15:07:55 +00003330static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003331{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003332 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003333 { 0x1f, 0x0001 },
3334 { 0x12, 0x2300 },
3335 { 0x1d, 0x3d98 },
3336 { 0x1f, 0x0002 },
3337 { 0x0c, 0x7eb8 },
3338 { 0x06, 0x5461 },
3339 { 0x1f, 0x0003 },
3340 { 0x16, 0x0f0a },
3341 { 0x1f, 0x0000 }
3342 };
3343
françois romieu4da19632011-01-03 15:07:55 +00003344 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003345
françois romieu4da19632011-01-03 15:07:55 +00003346 rtl_patchphy(tp, 0x16, 1 << 0);
3347 rtl_patchphy(tp, 0x14, 1 << 5);
3348 rtl_patchphy(tp, 0x0d, 1 << 5);
3349 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003350}
3351
françois romieu4da19632011-01-03 15:07:55 +00003352static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003353{
françois romieu4da19632011-01-03 15:07:55 +00003354 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003355}
3356
françois romieubca03d52011-01-03 15:07:31 +00003357static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003358{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003359 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003360 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003361 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003362 { 0x06, 0x4064 },
3363 { 0x07, 0x2863 },
3364 { 0x08, 0x059c },
3365 { 0x09, 0x26b4 },
3366 { 0x0a, 0x6a19 },
3367 { 0x0b, 0xdcc8 },
3368 { 0x10, 0xf06d },
3369 { 0x14, 0x7f68 },
3370 { 0x18, 0x7fd9 },
3371 { 0x1c, 0xf0ff },
3372 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003373 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003374 { 0x12, 0xf49f },
3375 { 0x13, 0x070b },
3376 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003377 { 0x14, 0x94c0 },
3378
3379 /*
3380 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003381 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003382 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003383 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003384 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003385 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003386 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003387 { 0x06, 0x5561 },
3388
3389 /*
3390 * Can not link to 1Gbps with bad cable
3391 * Decrease SNR threshold form 21.07dB to 19.04dB
3392 */
3393 { 0x1f, 0x0001 },
3394 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003395
3396 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003397 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003398 };
3399
françois romieu4da19632011-01-03 15:07:55 +00003400 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003401
françois romieubca03d52011-01-03 15:07:31 +00003402 /*
3403 * Rx Error Issue
3404 * Fine Tune Switching regulator parameter
3405 */
françois romieu4da19632011-01-03 15:07:55 +00003406 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003407 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3408 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
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 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003414 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003415 { 0x05, 0x8330 },
3416 { 0x06, 0x669a },
3417 { 0x1f, 0x0002 }
3418 };
3419 int val;
3420
françois romieu4da19632011-01-03 15:07:55 +00003421 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003422
françois romieu4da19632011-01-03 15:07:55 +00003423 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003424
3425 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -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, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003442 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003443 { 0x05, 0x8330 },
3444 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003445 };
3446
françois romieu4da19632011-01-03 15:07:55 +00003447 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003448 }
3449
françois romieubca03d52011-01-03 15:07:31 +00003450 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003451 rtl_writephy(tp, 0x1f, 0x0002);
3452 rtl_patchphy(tp, 0x0d, 0x0300);
3453 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003454
françois romieubca03d52011-01-03 15:07:31 +00003455 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003456 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003457 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3458 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003459
françois romieu4da19632011-01-03 15:07:55 +00003460 rtl_writephy(tp, 0x1f, 0x0005);
3461 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003462
3463 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003464
françois romieu4da19632011-01-03 15:07:55 +00003465 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003466}
3467
françois romieubca03d52011-01-03 15:07:31 +00003468static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003469{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003470 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003471 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003472 { 0x1f, 0x0001 },
3473 { 0x06, 0x4064 },
3474 { 0x07, 0x2863 },
3475 { 0x08, 0x059c },
3476 { 0x09, 0x26b4 },
3477 { 0x0a, 0x6a19 },
3478 { 0x0b, 0xdcc8 },
3479 { 0x10, 0xf06d },
3480 { 0x14, 0x7f68 },
3481 { 0x18, 0x7fd9 },
3482 { 0x1c, 0xf0ff },
3483 { 0x1d, 0x3d9c },
3484 { 0x1f, 0x0003 },
3485 { 0x12, 0xf49f },
3486 { 0x13, 0x070b },
3487 { 0x1a, 0x05ad },
3488 { 0x14, 0x94c0 },
3489
françois romieubca03d52011-01-03 15:07:31 +00003490 /*
3491 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003492 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003493 */
françois romieudaf9df62009-10-07 12:44:20 +00003494 { 0x1f, 0x0002 },
3495 { 0x06, 0x5561 },
3496 { 0x1f, 0x0005 },
3497 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003498 { 0x06, 0x5561 },
3499
3500 /*
3501 * Can not link to 1Gbps with bad cable
3502 * Decrease SNR threshold form 21.07dB to 19.04dB
3503 */
3504 { 0x1f, 0x0001 },
3505 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003506
3507 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003508 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003509 };
3510
françois romieu4da19632011-01-03 15:07:55 +00003511 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003512
Francois Romieufdf6fc02012-07-06 22:40:38 +02003513 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003514 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003515 { 0x1f, 0x0002 },
3516 { 0x05, 0x669a },
3517 { 0x1f, 0x0005 },
3518 { 0x05, 0x8330 },
3519 { 0x06, 0x669a },
3520
3521 { 0x1f, 0x0002 }
3522 };
3523 int val;
3524
françois romieu4da19632011-01-03 15:07:55 +00003525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003526
françois romieu4da19632011-01-03 15:07:55 +00003527 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003528 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003529 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003530 0x0065, 0x0066, 0x0067, 0x0068,
3531 0x0069, 0x006a, 0x006b, 0x006c
3532 };
3533 int i;
3534
françois romieu4da19632011-01-03 15:07:55 +00003535 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003536
3537 val &= 0xff00;
3538 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003539 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003540 }
3541 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003542 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003543 { 0x1f, 0x0002 },
3544 { 0x05, 0x2642 },
3545 { 0x1f, 0x0005 },
3546 { 0x05, 0x8330 },
3547 { 0x06, 0x2642 }
3548 };
3549
françois romieu4da19632011-01-03 15:07:55 +00003550 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003551 }
3552
françois romieubca03d52011-01-03 15:07:31 +00003553 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003554 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003555 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3556 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003557
françois romieubca03d52011-01-03 15:07:31 +00003558 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003559 rtl_writephy(tp, 0x1f, 0x0002);
3560 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003561
françois romieu4da19632011-01-03 15:07:55 +00003562 rtl_writephy(tp, 0x1f, 0x0005);
3563 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003564
3565 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003566
françois romieu4da19632011-01-03 15:07:55 +00003567 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003568}
3569
françois romieu4da19632011-01-03 15:07:55 +00003570static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003571{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003572 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003573 { 0x1f, 0x0002 },
3574 { 0x10, 0x0008 },
3575 { 0x0d, 0x006c },
3576
3577 { 0x1f, 0x0000 },
3578 { 0x0d, 0xf880 },
3579
3580 { 0x1f, 0x0001 },
3581 { 0x17, 0x0cc0 },
3582
3583 { 0x1f, 0x0001 },
3584 { 0x0b, 0xa4d8 },
3585 { 0x09, 0x281c },
3586 { 0x07, 0x2883 },
3587 { 0x0a, 0x6b35 },
3588 { 0x1d, 0x3da4 },
3589 { 0x1c, 0xeffd },
3590 { 0x14, 0x7f52 },
3591 { 0x18, 0x7fc6 },
3592 { 0x08, 0x0601 },
3593 { 0x06, 0x4063 },
3594 { 0x10, 0xf074 },
3595 { 0x1f, 0x0003 },
3596 { 0x13, 0x0789 },
3597 { 0x12, 0xf4bd },
3598 { 0x1a, 0x04fd },
3599 { 0x14, 0x84b0 },
3600 { 0x1f, 0x0000 },
3601 { 0x00, 0x9200 },
3602
3603 { 0x1f, 0x0005 },
3604 { 0x01, 0x0340 },
3605 { 0x1f, 0x0001 },
3606 { 0x04, 0x4000 },
3607 { 0x03, 0x1d21 },
3608 { 0x02, 0x0c32 },
3609 { 0x01, 0x0200 },
3610 { 0x00, 0x5554 },
3611 { 0x04, 0x4800 },
3612 { 0x04, 0x4000 },
3613 { 0x04, 0xf000 },
3614 { 0x03, 0xdf01 },
3615 { 0x02, 0xdf20 },
3616 { 0x01, 0x101a },
3617 { 0x00, 0xa0ff },
3618 { 0x04, 0xf800 },
3619 { 0x04, 0xf000 },
3620 { 0x1f, 0x0000 },
3621
3622 { 0x1f, 0x0007 },
3623 { 0x1e, 0x0023 },
3624 { 0x16, 0x0000 },
3625 { 0x1f, 0x0000 }
3626 };
3627
françois romieu4da19632011-01-03 15:07:55 +00003628 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003629}
3630
françois romieue6de30d2011-01-03 15:08:37 +00003631static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3632{
3633 static const struct phy_reg phy_reg_init[] = {
3634 { 0x1f, 0x0001 },
3635 { 0x17, 0x0cc0 },
3636
3637 { 0x1f, 0x0007 },
3638 { 0x1e, 0x002d },
3639 { 0x18, 0x0040 },
3640 { 0x1f, 0x0000 }
3641 };
3642
3643 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3644 rtl_patchphy(tp, 0x0d, 1 << 5);
3645}
3646
Hayes Wang70090422011-07-06 15:58:06 +08003647static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003648{
3649 static const struct phy_reg phy_reg_init[] = {
3650 /* Enable Delay cap */
3651 { 0x1f, 0x0005 },
3652 { 0x05, 0x8b80 },
3653 { 0x06, 0xc896 },
3654 { 0x1f, 0x0000 },
3655
3656 /* Channel estimation fine tune */
3657 { 0x1f, 0x0001 },
3658 { 0x0b, 0x6c20 },
3659 { 0x07, 0x2872 },
3660 { 0x1c, 0xefff },
3661 { 0x1f, 0x0003 },
3662 { 0x14, 0x6420 },
3663 { 0x1f, 0x0000 },
3664
3665 /* Update PFM & 10M TX idle timer */
3666 { 0x1f, 0x0007 },
3667 { 0x1e, 0x002f },
3668 { 0x15, 0x1919 },
3669 { 0x1f, 0x0000 },
3670
3671 { 0x1f, 0x0007 },
3672 { 0x1e, 0x00ac },
3673 { 0x18, 0x0006 },
3674 { 0x1f, 0x0000 }
3675 };
3676
Francois Romieu15ecd032011-04-27 13:52:22 -07003677 rtl_apply_firmware(tp);
3678
hayeswang01dc7fe2011-03-21 01:50:28 +00003679 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3680
3681 /* DCO enable for 10M IDLE Power */
3682 rtl_writephy(tp, 0x1f, 0x0007);
3683 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003684 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003685 rtl_writephy(tp, 0x1f, 0x0000);
3686
3687 /* For impedance matching */
3688 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003689 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003690 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003691
3692 /* PHY auto speed down */
3693 rtl_writephy(tp, 0x1f, 0x0007);
3694 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003695 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003696 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003697 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003698
3699 rtl_writephy(tp, 0x1f, 0x0005);
3700 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003701 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003702 rtl_writephy(tp, 0x1f, 0x0000);
3703
3704 rtl_writephy(tp, 0x1f, 0x0005);
3705 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003706 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003707 rtl_writephy(tp, 0x1f, 0x0007);
3708 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003709 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003710 rtl_writephy(tp, 0x1f, 0x0006);
3711 rtl_writephy(tp, 0x00, 0x5a00);
3712 rtl_writephy(tp, 0x1f, 0x0000);
3713 rtl_writephy(tp, 0x0d, 0x0007);
3714 rtl_writephy(tp, 0x0e, 0x003c);
3715 rtl_writephy(tp, 0x0d, 0x4007);
3716 rtl_writephy(tp, 0x0e, 0x0000);
3717 rtl_writephy(tp, 0x0d, 0x0000);
3718}
3719
françois romieu9ecb9aa2012-12-07 11:20:21 +00003720static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3721{
3722 const u16 w[] = {
3723 addr[0] | (addr[1] << 8),
3724 addr[2] | (addr[3] << 8),
3725 addr[4] | (addr[5] << 8)
3726 };
3727 const struct exgmac_reg e[] = {
3728 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3729 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3730 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3731 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3732 };
3733
3734 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3735}
3736
Hayes Wang70090422011-07-06 15:58:06 +08003737static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3738{
3739 static const struct phy_reg phy_reg_init[] = {
3740 /* Enable Delay cap */
3741 { 0x1f, 0x0004 },
3742 { 0x1f, 0x0007 },
3743 { 0x1e, 0x00ac },
3744 { 0x18, 0x0006 },
3745 { 0x1f, 0x0002 },
3746 { 0x1f, 0x0000 },
3747 { 0x1f, 0x0000 },
3748
3749 /* Channel estimation fine tune */
3750 { 0x1f, 0x0003 },
3751 { 0x09, 0xa20f },
3752 { 0x1f, 0x0000 },
3753 { 0x1f, 0x0000 },
3754
3755 /* Green Setting */
3756 { 0x1f, 0x0005 },
3757 { 0x05, 0x8b5b },
3758 { 0x06, 0x9222 },
3759 { 0x05, 0x8b6d },
3760 { 0x06, 0x8000 },
3761 { 0x05, 0x8b76 },
3762 { 0x06, 0x8000 },
3763 { 0x1f, 0x0000 }
3764 };
3765
3766 rtl_apply_firmware(tp);
3767
3768 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3769
3770 /* For 4-corner performance improve */
3771 rtl_writephy(tp, 0x1f, 0x0005);
3772 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003773 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003774 rtl_writephy(tp, 0x1f, 0x0000);
3775
3776 /* PHY auto speed down */
3777 rtl_writephy(tp, 0x1f, 0x0004);
3778 rtl_writephy(tp, 0x1f, 0x0007);
3779 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003780 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003781 rtl_writephy(tp, 0x1f, 0x0002);
3782 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003783 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003784
3785 /* improve 10M EEE waveform */
3786 rtl_writephy(tp, 0x1f, 0x0005);
3787 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003788 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003789 rtl_writephy(tp, 0x1f, 0x0000);
3790
3791 /* Improve 2-pair detection performance */
3792 rtl_writephy(tp, 0x1f, 0x0005);
3793 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003794 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003795 rtl_writephy(tp, 0x1f, 0x0000);
3796
3797 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003798 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003799 rtl_writephy(tp, 0x1f, 0x0005);
3800 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003801 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003802 rtl_writephy(tp, 0x1f, 0x0004);
3803 rtl_writephy(tp, 0x1f, 0x0007);
3804 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003805 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003806 rtl_writephy(tp, 0x1f, 0x0002);
3807 rtl_writephy(tp, 0x1f, 0x0000);
3808 rtl_writephy(tp, 0x0d, 0x0007);
3809 rtl_writephy(tp, 0x0e, 0x003c);
3810 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003811 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003812 rtl_writephy(tp, 0x0d, 0x0000);
3813
3814 /* Green feature */
3815 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003816 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3817 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003818 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003819 rtl_writephy(tp, 0x1f, 0x0005);
3820 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3821 rtl_writephy(tp, 0x1f, 0x0000);
3822 /* soft-reset phy */
3823 rtl_writephy(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE | BMCR_ANRESTART);
hayeswange0c07552012-10-23 20:24:03 +00003824
françois romieu9ecb9aa2012-12-07 11:20:21 +00003825 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3826 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003827}
3828
Hayes Wang5f886e02012-03-30 14:33:03 +08003829static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3830{
3831 /* For 4-corner performance improve */
3832 rtl_writephy(tp, 0x1f, 0x0005);
3833 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003834 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003835 rtl_writephy(tp, 0x1f, 0x0000);
3836
3837 /* PHY auto speed down */
3838 rtl_writephy(tp, 0x1f, 0x0007);
3839 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003840 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003841 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003842 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003843
3844 /* Improve 10M EEE waveform */
3845 rtl_writephy(tp, 0x1f, 0x0005);
3846 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003847 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003848 rtl_writephy(tp, 0x1f, 0x0000);
3849}
3850
Hayes Wangc2218922011-09-06 16:55:18 +08003851static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3852{
3853 static const struct phy_reg phy_reg_init[] = {
3854 /* Channel estimation fine tune */
3855 { 0x1f, 0x0003 },
3856 { 0x09, 0xa20f },
3857 { 0x1f, 0x0000 },
3858
3859 /* Modify green table for giga & fnet */
3860 { 0x1f, 0x0005 },
3861 { 0x05, 0x8b55 },
3862 { 0x06, 0x0000 },
3863 { 0x05, 0x8b5e },
3864 { 0x06, 0x0000 },
3865 { 0x05, 0x8b67 },
3866 { 0x06, 0x0000 },
3867 { 0x05, 0x8b70 },
3868 { 0x06, 0x0000 },
3869 { 0x1f, 0x0000 },
3870 { 0x1f, 0x0007 },
3871 { 0x1e, 0x0078 },
3872 { 0x17, 0x0000 },
3873 { 0x19, 0x00fb },
3874 { 0x1f, 0x0000 },
3875
3876 /* Modify green table for 10M */
3877 { 0x1f, 0x0005 },
3878 { 0x05, 0x8b79 },
3879 { 0x06, 0xaa00 },
3880 { 0x1f, 0x0000 },
3881
3882 /* Disable hiimpedance detection (RTCT) */
3883 { 0x1f, 0x0003 },
3884 { 0x01, 0x328a },
3885 { 0x1f, 0x0000 }
3886 };
3887
3888 rtl_apply_firmware(tp);
3889
3890 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3891
Hayes Wang5f886e02012-03-30 14:33:03 +08003892 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003893
3894 /* Improve 2-pair detection performance */
3895 rtl_writephy(tp, 0x1f, 0x0005);
3896 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003897 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003898 rtl_writephy(tp, 0x1f, 0x0000);
3899}
3900
3901static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3902{
3903 rtl_apply_firmware(tp);
3904
Hayes Wang5f886e02012-03-30 14:33:03 +08003905 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003906}
3907
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003908static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3909{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003910 static const struct phy_reg phy_reg_init[] = {
3911 /* Channel estimation fine tune */
3912 { 0x1f, 0x0003 },
3913 { 0x09, 0xa20f },
3914 { 0x1f, 0x0000 },
3915
3916 /* Modify green table for giga & fnet */
3917 { 0x1f, 0x0005 },
3918 { 0x05, 0x8b55 },
3919 { 0x06, 0x0000 },
3920 { 0x05, 0x8b5e },
3921 { 0x06, 0x0000 },
3922 { 0x05, 0x8b67 },
3923 { 0x06, 0x0000 },
3924 { 0x05, 0x8b70 },
3925 { 0x06, 0x0000 },
3926 { 0x1f, 0x0000 },
3927 { 0x1f, 0x0007 },
3928 { 0x1e, 0x0078 },
3929 { 0x17, 0x0000 },
3930 { 0x19, 0x00aa },
3931 { 0x1f, 0x0000 },
3932
3933 /* Modify green table for 10M */
3934 { 0x1f, 0x0005 },
3935 { 0x05, 0x8b79 },
3936 { 0x06, 0xaa00 },
3937 { 0x1f, 0x0000 },
3938
3939 /* Disable hiimpedance detection (RTCT) */
3940 { 0x1f, 0x0003 },
3941 { 0x01, 0x328a },
3942 { 0x1f, 0x0000 }
3943 };
3944
3945
3946 rtl_apply_firmware(tp);
3947
3948 rtl8168f_hw_phy_config(tp);
3949
3950 /* Improve 2-pair detection performance */
3951 rtl_writephy(tp, 0x1f, 0x0005);
3952 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003953 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003954 rtl_writephy(tp, 0x1f, 0x0000);
3955
3956 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3957
3958 /* Modify green table for giga */
3959 rtl_writephy(tp, 0x1f, 0x0005);
3960 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003961 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003962 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003963 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003964 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003965 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003966 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003967 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003968 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003969 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003970 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003971 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003972 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003973 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003974 rtl_writephy(tp, 0x1f, 0x0000);
3975
3976 /* uc same-seed solution */
3977 rtl_writephy(tp, 0x1f, 0x0005);
3978 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003979 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003980 rtl_writephy(tp, 0x1f, 0x0000);
3981
3982 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003983 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003984 rtl_writephy(tp, 0x1f, 0x0005);
3985 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003986 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003987 rtl_writephy(tp, 0x1f, 0x0004);
3988 rtl_writephy(tp, 0x1f, 0x0007);
3989 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003990 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003991 rtl_writephy(tp, 0x1f, 0x0000);
3992 rtl_writephy(tp, 0x0d, 0x0007);
3993 rtl_writephy(tp, 0x0e, 0x003c);
3994 rtl_writephy(tp, 0x0d, 0x4007);
3995 rtl_writephy(tp, 0x0e, 0x0000);
3996 rtl_writephy(tp, 0x0d, 0x0000);
3997
3998 /* Green feature */
3999 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004000 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
4001 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004002 rtl_writephy(tp, 0x1f, 0x0000);
4003}
4004
Hayes Wangc5583862012-07-02 17:23:22 +08004005static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
4006{
Hayes Wangc5583862012-07-02 17:23:22 +08004007 rtl_apply_firmware(tp);
4008
hayeswang41f44d12013-04-01 22:23:36 +00004009 rtl_writephy(tp, 0x1f, 0x0a46);
4010 if (rtl_readphy(tp, 0x10) & 0x0100) {
4011 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004012 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00004013 } else {
4014 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004015 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004016 }
Hayes Wangc5583862012-07-02 17:23:22 +08004017
hayeswang41f44d12013-04-01 22:23:36 +00004018 rtl_writephy(tp, 0x1f, 0x0a46);
4019 if (rtl_readphy(tp, 0x13) & 0x0100) {
4020 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004021 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004022 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00004023 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004024 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00004025 }
Hayes Wangc5583862012-07-02 17:23:22 +08004026
hayeswang41f44d12013-04-01 22:23:36 +00004027 /* Enable PHY auto speed down */
4028 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004029 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004030
hayeswangfe7524c2013-04-01 22:23:37 +00004031 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004032 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004033 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004034 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004035 rtl_writephy(tp, 0x1f, 0x0a43);
4036 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004037 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4038 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004039
hayeswang41f44d12013-04-01 22:23:36 +00004040 /* EEE auto-fallback function */
4041 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004042 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004043
hayeswang41f44d12013-04-01 22:23:36 +00004044 /* Enable UC LPF tune function */
4045 rtl_writephy(tp, 0x1f, 0x0a43);
4046 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004047 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004048
4049 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004050 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00004051
hayeswangfe7524c2013-04-01 22:23:37 +00004052 /* Improve SWR Efficiency */
4053 rtl_writephy(tp, 0x1f, 0x0bcd);
4054 rtl_writephy(tp, 0x14, 0x5065);
4055 rtl_writephy(tp, 0x14, 0xd065);
4056 rtl_writephy(tp, 0x1f, 0x0bc8);
4057 rtl_writephy(tp, 0x11, 0x5655);
4058 rtl_writephy(tp, 0x1f, 0x0bcd);
4059 rtl_writephy(tp, 0x14, 0x1065);
4060 rtl_writephy(tp, 0x14, 0x9065);
4061 rtl_writephy(tp, 0x14, 0x1065);
4062
David Chang1bac1072013-11-27 15:48:36 +08004063 /* Check ALDPS bit, disable it if enabled */
4064 rtl_writephy(tp, 0x1f, 0x0a43);
4065 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004066 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08004067
hayeswang41f44d12013-04-01 22:23:36 +00004068 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004069}
4070
hayeswang57538c42013-04-01 22:23:40 +00004071static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
4072{
4073 rtl_apply_firmware(tp);
4074}
4075
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004076static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
4077{
4078 u16 dout_tapbin;
4079 u32 data;
4080
4081 rtl_apply_firmware(tp);
4082
4083 /* CHN EST parameters adjust - giga master */
4084 rtl_writephy(tp, 0x1f, 0x0a43);
4085 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004086 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004087 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004088 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004089 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004090 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004091 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004092 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004093 rtl_writephy(tp, 0x1f, 0x0000);
4094
4095 /* CHN EST parameters adjust - giga slave */
4096 rtl_writephy(tp, 0x1f, 0x0a43);
4097 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004098 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004099 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004100 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004101 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004102 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004103 rtl_writephy(tp, 0x1f, 0x0000);
4104
4105 /* CHN EST parameters adjust - fnet */
4106 rtl_writephy(tp, 0x1f, 0x0a43);
4107 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004108 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004109 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004110 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004111 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004112 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004113 rtl_writephy(tp, 0x1f, 0x0000);
4114
4115 /* enable R-tune & PGA-retune function */
4116 dout_tapbin = 0;
4117 rtl_writephy(tp, 0x1f, 0x0a46);
4118 data = rtl_readphy(tp, 0x13);
4119 data &= 3;
4120 data <<= 2;
4121 dout_tapbin |= data;
4122 data = rtl_readphy(tp, 0x12);
4123 data &= 0xc000;
4124 data >>= 14;
4125 dout_tapbin |= data;
4126 dout_tapbin = ~(dout_tapbin^0x08);
4127 dout_tapbin <<= 12;
4128 dout_tapbin &= 0xf000;
4129 rtl_writephy(tp, 0x1f, 0x0a43);
4130 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004131 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004132 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004133 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004134 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004135 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004136 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004137 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004138
4139 rtl_writephy(tp, 0x1f, 0x0a43);
4140 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004141 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004142 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004143 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004144 rtl_writephy(tp, 0x1f, 0x0000);
4145
4146 /* enable GPHY 10M */
4147 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004148 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004149 rtl_writephy(tp, 0x1f, 0x0000);
4150
4151 /* SAR ADC performance */
4152 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004153 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004154 rtl_writephy(tp, 0x1f, 0x0000);
4155
4156 rtl_writephy(tp, 0x1f, 0x0a43);
4157 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004158 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004159 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004160 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004161 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004162 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004163 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004164 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004165 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004166 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004167 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004168 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004169 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004170 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004171 rtl_writephy(tp, 0x1f, 0x0000);
4172
4173 /* disable phy pfm mode */
4174 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004175 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004176 rtl_writephy(tp, 0x1f, 0x0000);
4177
4178 /* Check ALDPS bit, disable it if enabled */
4179 rtl_writephy(tp, 0x1f, 0x0a43);
4180 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004181 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004182
4183 rtl_writephy(tp, 0x1f, 0x0000);
4184}
4185
4186static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4187{
4188 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4189 u16 rlen;
4190 u32 data;
4191
4192 rtl_apply_firmware(tp);
4193
4194 /* CHIN EST parameter update */
4195 rtl_writephy(tp, 0x1f, 0x0a43);
4196 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004197 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004198 rtl_writephy(tp, 0x1f, 0x0000);
4199
4200 /* enable R-tune & PGA-retune function */
4201 rtl_writephy(tp, 0x1f, 0x0a43);
4202 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004203 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004204 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004205 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004206 rtl_writephy(tp, 0x1f, 0x0000);
4207
4208 /* enable GPHY 10M */
4209 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004210 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004211 rtl_writephy(tp, 0x1f, 0x0000);
4212
4213 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4214 data = r8168_mac_ocp_read(tp, 0xdd02);
4215 ioffset_p3 = ((data & 0x80)>>7);
4216 ioffset_p3 <<= 3;
4217
4218 data = r8168_mac_ocp_read(tp, 0xdd00);
4219 ioffset_p3 |= ((data & (0xe000))>>13);
4220 ioffset_p2 = ((data & (0x1e00))>>9);
4221 ioffset_p1 = ((data & (0x01e0))>>5);
4222 ioffset_p0 = ((data & 0x0010)>>4);
4223 ioffset_p0 <<= 3;
4224 ioffset_p0 |= (data & (0x07));
4225 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4226
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004227 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004228 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004229 rtl_writephy(tp, 0x1f, 0x0bcf);
4230 rtl_writephy(tp, 0x16, data);
4231 rtl_writephy(tp, 0x1f, 0x0000);
4232 }
4233
4234 /* Modify rlen (TX LPF corner frequency) level */
4235 rtl_writephy(tp, 0x1f, 0x0bcd);
4236 data = rtl_readphy(tp, 0x16);
4237 data &= 0x000f;
4238 rlen = 0;
4239 if (data > 3)
4240 rlen = data - 3;
4241 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4242 rtl_writephy(tp, 0x17, data);
4243 rtl_writephy(tp, 0x1f, 0x0bcd);
4244 rtl_writephy(tp, 0x1f, 0x0000);
4245
4246 /* disable phy pfm mode */
4247 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004248 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004249 rtl_writephy(tp, 0x1f, 0x0000);
4250
4251 /* Check ALDPS bit, disable it if enabled */
4252 rtl_writephy(tp, 0x1f, 0x0a43);
4253 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004254 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004255
4256 rtl_writephy(tp, 0x1f, 0x0000);
4257}
4258
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004259static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4260{
4261 /* Enable PHY auto speed down */
4262 rtl_writephy(tp, 0x1f, 0x0a44);
4263 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4264 rtl_writephy(tp, 0x1f, 0x0000);
4265
4266 /* patch 10M & ALDPS */
4267 rtl_writephy(tp, 0x1f, 0x0bcc);
4268 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4269 rtl_writephy(tp, 0x1f, 0x0a44);
4270 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4271 rtl_writephy(tp, 0x1f, 0x0a43);
4272 rtl_writephy(tp, 0x13, 0x8084);
4273 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4274 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4275 rtl_writephy(tp, 0x1f, 0x0000);
4276
4277 /* Enable EEE auto-fallback function */
4278 rtl_writephy(tp, 0x1f, 0x0a4b);
4279 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4280 rtl_writephy(tp, 0x1f, 0x0000);
4281
4282 /* Enable UC LPF tune function */
4283 rtl_writephy(tp, 0x1f, 0x0a43);
4284 rtl_writephy(tp, 0x13, 0x8012);
4285 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4286 rtl_writephy(tp, 0x1f, 0x0000);
4287
4288 /* set rg_sel_sdm_rate */
4289 rtl_writephy(tp, 0x1f, 0x0c42);
4290 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4291 rtl_writephy(tp, 0x1f, 0x0000);
4292
4293 /* Check ALDPS bit, disable it if enabled */
4294 rtl_writephy(tp, 0x1f, 0x0a43);
4295 if (rtl_readphy(tp, 0x10) & 0x0004)
4296 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4297
4298 rtl_writephy(tp, 0x1f, 0x0000);
4299}
4300
4301static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4302{
4303 /* patch 10M & ALDPS */
4304 rtl_writephy(tp, 0x1f, 0x0bcc);
4305 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4306 rtl_writephy(tp, 0x1f, 0x0a44);
4307 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4308 rtl_writephy(tp, 0x1f, 0x0a43);
4309 rtl_writephy(tp, 0x13, 0x8084);
4310 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4311 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4312 rtl_writephy(tp, 0x1f, 0x0000);
4313
4314 /* Enable UC LPF tune function */
4315 rtl_writephy(tp, 0x1f, 0x0a43);
4316 rtl_writephy(tp, 0x13, 0x8012);
4317 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4318 rtl_writephy(tp, 0x1f, 0x0000);
4319
4320 /* Set rg_sel_sdm_rate */
4321 rtl_writephy(tp, 0x1f, 0x0c42);
4322 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4323 rtl_writephy(tp, 0x1f, 0x0000);
4324
4325 /* Channel estimation parameters */
4326 rtl_writephy(tp, 0x1f, 0x0a43);
4327 rtl_writephy(tp, 0x13, 0x80f3);
4328 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4329 rtl_writephy(tp, 0x13, 0x80f0);
4330 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4331 rtl_writephy(tp, 0x13, 0x80ef);
4332 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4333 rtl_writephy(tp, 0x13, 0x80f6);
4334 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4335 rtl_writephy(tp, 0x13, 0x80ec);
4336 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4337 rtl_writephy(tp, 0x13, 0x80ed);
4338 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4339 rtl_writephy(tp, 0x13, 0x80f2);
4340 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4341 rtl_writephy(tp, 0x13, 0x80f4);
4342 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4343 rtl_writephy(tp, 0x1f, 0x0a43);
4344 rtl_writephy(tp, 0x13, 0x8110);
4345 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4346 rtl_writephy(tp, 0x13, 0x810f);
4347 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4348 rtl_writephy(tp, 0x13, 0x8111);
4349 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4350 rtl_writephy(tp, 0x13, 0x8113);
4351 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4352 rtl_writephy(tp, 0x13, 0x8115);
4353 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4354 rtl_writephy(tp, 0x13, 0x810e);
4355 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4356 rtl_writephy(tp, 0x13, 0x810c);
4357 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4358 rtl_writephy(tp, 0x13, 0x810b);
4359 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4360 rtl_writephy(tp, 0x1f, 0x0a43);
4361 rtl_writephy(tp, 0x13, 0x80d1);
4362 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4363 rtl_writephy(tp, 0x13, 0x80cd);
4364 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4365 rtl_writephy(tp, 0x13, 0x80d3);
4366 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4367 rtl_writephy(tp, 0x13, 0x80d5);
4368 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4369 rtl_writephy(tp, 0x13, 0x80d7);
4370 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4371
4372 /* Force PWM-mode */
4373 rtl_writephy(tp, 0x1f, 0x0bcd);
4374 rtl_writephy(tp, 0x14, 0x5065);
4375 rtl_writephy(tp, 0x14, 0xd065);
4376 rtl_writephy(tp, 0x1f, 0x0bc8);
4377 rtl_writephy(tp, 0x12, 0x00ed);
4378 rtl_writephy(tp, 0x1f, 0x0bcd);
4379 rtl_writephy(tp, 0x14, 0x1065);
4380 rtl_writephy(tp, 0x14, 0x9065);
4381 rtl_writephy(tp, 0x14, 0x1065);
4382 rtl_writephy(tp, 0x1f, 0x0000);
4383
4384 /* Check ALDPS bit, disable it if enabled */
4385 rtl_writephy(tp, 0x1f, 0x0a43);
4386 if (rtl_readphy(tp, 0x10) & 0x0004)
4387 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4388
4389 rtl_writephy(tp, 0x1f, 0x0000);
4390}
4391
françois romieu4da19632011-01-03 15:07:55 +00004392static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004393{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004394 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004395 { 0x1f, 0x0003 },
4396 { 0x08, 0x441d },
4397 { 0x01, 0x9100 },
4398 { 0x1f, 0x0000 }
4399 };
4400
françois romieu4da19632011-01-03 15:07:55 +00004401 rtl_writephy(tp, 0x1f, 0x0000);
4402 rtl_patchphy(tp, 0x11, 1 << 12);
4403 rtl_patchphy(tp, 0x19, 1 << 13);
4404 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004405
françois romieu4da19632011-01-03 15:07:55 +00004406 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004407}
4408
Hayes Wang5a5e4442011-02-22 17:26:21 +08004409static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4410{
4411 static const struct phy_reg phy_reg_init[] = {
4412 { 0x1f, 0x0005 },
4413 { 0x1a, 0x0000 },
4414 { 0x1f, 0x0000 },
4415
4416 { 0x1f, 0x0004 },
4417 { 0x1c, 0x0000 },
4418 { 0x1f, 0x0000 },
4419
4420 { 0x1f, 0x0001 },
4421 { 0x15, 0x7701 },
4422 { 0x1f, 0x0000 }
4423 };
4424
4425 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004426 rtl_writephy(tp, 0x1f, 0x0000);
4427 rtl_writephy(tp, 0x18, 0x0310);
4428 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004429
François Romieu953a12c2011-04-24 17:38:48 +02004430 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004431
4432 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4433}
4434
Hayes Wang7e18dca2012-03-30 14:33:02 +08004435static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4436{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004437 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004438 rtl_writephy(tp, 0x1f, 0x0000);
4439 rtl_writephy(tp, 0x18, 0x0310);
4440 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004441
4442 rtl_apply_firmware(tp);
4443
4444 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004445 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004446 rtl_writephy(tp, 0x1f, 0x0004);
4447 rtl_writephy(tp, 0x10, 0x401f);
4448 rtl_writephy(tp, 0x19, 0x7030);
4449 rtl_writephy(tp, 0x1f, 0x0000);
4450}
4451
Hayes Wang5598bfe2012-07-02 17:23:21 +08004452static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4453{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004454 static const struct phy_reg phy_reg_init[] = {
4455 { 0x1f, 0x0004 },
4456 { 0x10, 0xc07f },
4457 { 0x19, 0x7030 },
4458 { 0x1f, 0x0000 }
4459 };
4460
4461 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004462 rtl_writephy(tp, 0x1f, 0x0000);
4463 rtl_writephy(tp, 0x18, 0x0310);
4464 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004465
4466 rtl_apply_firmware(tp);
4467
Francois Romieufdf6fc02012-07-06 22:40:38 +02004468 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004469 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4470
Francois Romieufdf6fc02012-07-06 22:40:38 +02004471 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004472}
4473
Francois Romieu5615d9f2007-08-17 17:50:46 +02004474static void rtl_hw_phy_config(struct net_device *dev)
4475{
4476 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004477
4478 rtl8169_print_mac_version(tp);
4479
4480 switch (tp->mac_version) {
4481 case RTL_GIGA_MAC_VER_01:
4482 break;
4483 case RTL_GIGA_MAC_VER_02:
4484 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004485 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004486 break;
4487 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004488 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004489 break;
françois romieu2e9558562009-08-10 19:44:19 +00004490 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004491 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004492 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004493 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004494 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004495 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004496 case RTL_GIGA_MAC_VER_07:
4497 case RTL_GIGA_MAC_VER_08:
4498 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004499 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004500 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004501 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004502 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004503 break;
4504 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004505 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004506 break;
4507 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004508 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004509 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004510 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004511 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004512 break;
4513 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004514 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004515 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004516 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004517 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004518 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004519 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004520 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004521 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004522 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004523 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004524 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004525 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004526 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004527 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004528 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004529 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004530 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004531 break;
4532 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004533 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004534 break;
4535 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004536 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004537 break;
françois romieue6de30d2011-01-03 15:08:37 +00004538 case RTL_GIGA_MAC_VER_28:
4539 rtl8168d_4_hw_phy_config(tp);
4540 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004541 case RTL_GIGA_MAC_VER_29:
4542 case RTL_GIGA_MAC_VER_30:
4543 rtl8105e_hw_phy_config(tp);
4544 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004545 case RTL_GIGA_MAC_VER_31:
4546 /* None. */
4547 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004548 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004549 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004550 rtl8168e_1_hw_phy_config(tp);
4551 break;
4552 case RTL_GIGA_MAC_VER_34:
4553 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004554 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004555 case RTL_GIGA_MAC_VER_35:
4556 rtl8168f_1_hw_phy_config(tp);
4557 break;
4558 case RTL_GIGA_MAC_VER_36:
4559 rtl8168f_2_hw_phy_config(tp);
4560 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004561
Hayes Wang7e18dca2012-03-30 14:33:02 +08004562 case RTL_GIGA_MAC_VER_37:
4563 rtl8402_hw_phy_config(tp);
4564 break;
4565
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004566 case RTL_GIGA_MAC_VER_38:
4567 rtl8411_hw_phy_config(tp);
4568 break;
4569
Hayes Wang5598bfe2012-07-02 17:23:21 +08004570 case RTL_GIGA_MAC_VER_39:
4571 rtl8106e_hw_phy_config(tp);
4572 break;
4573
Hayes Wangc5583862012-07-02 17:23:22 +08004574 case RTL_GIGA_MAC_VER_40:
4575 rtl8168g_1_hw_phy_config(tp);
4576 break;
hayeswang57538c42013-04-01 22:23:40 +00004577 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004578 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004579 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004580 rtl8168g_2_hw_phy_config(tp);
4581 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004582 case RTL_GIGA_MAC_VER_45:
4583 case RTL_GIGA_MAC_VER_47:
4584 rtl8168h_1_hw_phy_config(tp);
4585 break;
4586 case RTL_GIGA_MAC_VER_46:
4587 case RTL_GIGA_MAC_VER_48:
4588 rtl8168h_2_hw_phy_config(tp);
4589 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004590
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004591 case RTL_GIGA_MAC_VER_49:
4592 rtl8168ep_1_hw_phy_config(tp);
4593 break;
4594 case RTL_GIGA_MAC_VER_50:
4595 case RTL_GIGA_MAC_VER_51:
4596 rtl8168ep_2_hw_phy_config(tp);
4597 break;
4598
Hayes Wangc5583862012-07-02 17:23:22 +08004599 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004600 default:
4601 break;
4602 }
4603}
4604
Francois Romieuda78dbf2012-01-26 14:18:23 +01004605static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 struct timer_list *timer = &tp->timer;
4608 void __iomem *ioaddr = tp->mmio_addr;
4609 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4610
Francois Romieubcf0bf92006-07-26 23:14:13 +02004611 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
françois romieu4da19632011-01-03 15:07:55 +00004613 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004614 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 * A busy loop could burn quite a few cycles on nowadays CPU.
4616 * Let's delay the execution of the timer for a few ticks.
4617 */
4618 timeout = HZ/10;
4619 goto out_mod_timer;
4620 }
4621
4622 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004625 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
françois romieu4da19632011-01-03 15:07:55 +00004627 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
4629out_mod_timer:
4630 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004631}
4632
4633static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4634{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004635 if (!test_and_set_bit(flag, tp->wk.flags))
4636 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004637}
4638
Kees Cook9de36cc2017-10-25 03:53:12 -07004639static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004640{
Kees Cook9de36cc2017-10-25 03:53:12 -07004641 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004642
Francois Romieu98ddf982012-01-31 10:47:34 +01004643 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644}
4645
Francois Romieuffc46952012-07-06 14:19:23 +02004646DECLARE_RTL_COND(rtl_phy_reset_cond)
4647{
4648 return tp->phy_reset_pending(tp);
4649}
4650
Francois Romieubf793292006-11-01 00:53:05 +01004651static void rtl8169_phy_reset(struct net_device *dev,
4652 struct rtl8169_private *tp)
4653{
françois romieu4da19632011-01-03 15:07:55 +00004654 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004655 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004656}
4657
David S. Miller8decf862011-09-22 03:23:13 -04004658static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4659{
4660 void __iomem *ioaddr = tp->mmio_addr;
4661
4662 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4663 (RTL_R8(PHYstatus) & TBI_Enable);
4664}
4665
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004666static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004668 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004669
Francois Romieu5615d9f2007-08-17 17:50:46 +02004670 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004671
Marcus Sundberg773328942008-07-10 21:28:08 +02004672 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4673 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4674 RTL_W8(0x82, 0x01);
4675 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004676
Francois Romieu6dccd162007-02-13 23:38:05 +01004677 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4678
4679 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4680 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004681
Francois Romieubcf0bf92006-07-26 23:14:13 +02004682 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004683 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4684 RTL_W8(0x82, 0x01);
4685 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004686 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004687 }
4688
Francois Romieubf793292006-11-01 00:53:05 +01004689 rtl8169_phy_reset(dev, tp);
4690
Oliver Neukum54405cd2011-01-06 21:55:13 +01004691 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004692 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4693 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4694 (tp->mii.supports_gmii ?
4695 ADVERTISED_1000baseT_Half |
4696 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004697
David S. Miller8decf862011-09-22 03:23:13 -04004698 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004699 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004700}
4701
Francois Romieu773d2022007-01-31 23:47:43 +01004702static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4703{
4704 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004705
Francois Romieuda78dbf2012-01-26 14:18:23 +01004706 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004707
4708 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004709
françois romieu9ecb9aa2012-12-07 11:20:21 +00004710 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2bf2010-04-26 11:42:58 +00004711 RTL_R32(MAC4);
4712
françois romieu9ecb9aa2012-12-07 11:20:21 +00004713 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2bf2010-04-26 11:42:58 +00004714 RTL_R32(MAC0);
4715
françois romieu9ecb9aa2012-12-07 11:20:21 +00004716 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4717 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004718
Francois Romieu773d2022007-01-31 23:47:43 +01004719 RTL_W8(Cfg9346, Cfg9346_Lock);
4720
Francois Romieuda78dbf2012-01-26 14:18:23 +01004721 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004722}
4723
4724static int rtl_set_mac_address(struct net_device *dev, void *p)
4725{
4726 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004727 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004728 struct sockaddr *addr = p;
4729
4730 if (!is_valid_ether_addr(addr->sa_data))
4731 return -EADDRNOTAVAIL;
4732
4733 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4734
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004735 pm_runtime_get_noresume(d);
4736
4737 if (pm_runtime_active(d))
4738 rtl_rar_set(tp, dev->dev_addr);
4739
4740 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004741
4742 return 0;
4743}
4744
Francois Romieu5f787a12006-08-17 13:02:36 +02004745static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4746{
4747 struct rtl8169_private *tp = netdev_priv(dev);
4748 struct mii_ioctl_data *data = if_mii(ifr);
4749
Francois Romieu8b4ab282008-11-19 22:05:25 -08004750 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4751}
Francois Romieu5f787a12006-08-17 13:02:36 +02004752
Francois Romieucecb5fd2011-04-01 10:21:07 +02004753static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4754 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004755{
Francois Romieu5f787a12006-08-17 13:02:36 +02004756 switch (cmd) {
4757 case SIOCGMIIPHY:
4758 data->phy_id = 32; /* Internal PHY */
4759 return 0;
4760
4761 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004762 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004763 return 0;
4764
4765 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004766 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004767 return 0;
4768 }
4769 return -EOPNOTSUPP;
4770}
4771
Francois Romieu8b4ab282008-11-19 22:05:25 -08004772static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4773{
4774 return -EOPNOTSUPP;
4775}
4776
Bill Pembertonbaf63292012-12-03 09:23:28 -05004777static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004778{
4779 struct mdio_ops *ops = &tp->mdio_ops;
4780
4781 switch (tp->mac_version) {
4782 case RTL_GIGA_MAC_VER_27:
4783 ops->write = r8168dp_1_mdio_write;
4784 ops->read = r8168dp_1_mdio_read;
4785 break;
françois romieue6de30d2011-01-03 15:08:37 +00004786 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004787 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004788 ops->write = r8168dp_2_mdio_write;
4789 ops->read = r8168dp_2_mdio_read;
4790 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004791 case RTL_GIGA_MAC_VER_40:
4792 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004793 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004794 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004795 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004796 case RTL_GIGA_MAC_VER_45:
4797 case RTL_GIGA_MAC_VER_46:
4798 case RTL_GIGA_MAC_VER_47:
4799 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004800 case RTL_GIGA_MAC_VER_49:
4801 case RTL_GIGA_MAC_VER_50:
4802 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004803 ops->write = r8168g_mdio_write;
4804 ops->read = r8168g_mdio_read;
4805 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004806 default:
4807 ops->write = r8169_mdio_write;
4808 ops->read = r8169_mdio_read;
4809 break;
4810 }
4811}
4812
hayeswange2409d82013-03-31 17:02:04 +00004813static void rtl_speed_down(struct rtl8169_private *tp)
4814{
4815 u32 adv;
4816 int lpa;
4817
4818 rtl_writephy(tp, 0x1f, 0x0000);
4819 lpa = rtl_readphy(tp, MII_LPA);
4820
4821 if (lpa & (LPA_10HALF | LPA_10FULL))
4822 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4823 else if (lpa & (LPA_100HALF | LPA_100FULL))
4824 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4825 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4826 else
4827 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4828 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4829 (tp->mii.supports_gmii ?
4830 ADVERTISED_1000baseT_Half |
4831 ADVERTISED_1000baseT_Full : 0);
4832
4833 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4834 adv);
4835}
4836
David S. Miller1805b2f2011-10-24 18:18:09 -04004837static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4838{
4839 void __iomem *ioaddr = tp->mmio_addr;
4840
4841 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004842 case RTL_GIGA_MAC_VER_25:
4843 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004844 case RTL_GIGA_MAC_VER_29:
4845 case RTL_GIGA_MAC_VER_30:
4846 case RTL_GIGA_MAC_VER_32:
4847 case RTL_GIGA_MAC_VER_33:
4848 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004849 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004850 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004851 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004852 case RTL_GIGA_MAC_VER_40:
4853 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004854 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004855 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004856 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004857 case RTL_GIGA_MAC_VER_45:
4858 case RTL_GIGA_MAC_VER_46:
4859 case RTL_GIGA_MAC_VER_47:
4860 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004861 case RTL_GIGA_MAC_VER_49:
4862 case RTL_GIGA_MAC_VER_50:
4863 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004864 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4865 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4866 break;
4867 default:
4868 break;
4869 }
4870}
4871
4872static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4873{
4874 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4875 return false;
4876
hayeswange2409d82013-03-31 17:02:04 +00004877 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004878 rtl_wol_suspend_quirk(tp);
4879
4880 return true;
4881}
4882
françois romieu065c27c2011-01-03 15:08:12 +00004883static void r810x_phy_power_down(struct rtl8169_private *tp)
4884{
4885 rtl_writephy(tp, 0x1f, 0x0000);
4886 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4887}
4888
4889static void r810x_phy_power_up(struct rtl8169_private *tp)
4890{
4891 rtl_writephy(tp, 0x1f, 0x0000);
4892 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4893}
4894
4895static void r810x_pll_power_down(struct rtl8169_private *tp)
4896{
Hayes Wang00042992012-03-30 14:33:00 +08004897 void __iomem *ioaddr = tp->mmio_addr;
4898
David S. Miller1805b2f2011-10-24 18:18:09 -04004899 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004900 return;
françois romieu065c27c2011-01-03 15:08:12 +00004901
4902 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004903
4904 switch (tp->mac_version) {
4905 case RTL_GIGA_MAC_VER_07:
4906 case RTL_GIGA_MAC_VER_08:
4907 case RTL_GIGA_MAC_VER_09:
4908 case RTL_GIGA_MAC_VER_10:
4909 case RTL_GIGA_MAC_VER_13:
4910 case RTL_GIGA_MAC_VER_16:
4911 break;
4912 default:
4913 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4914 break;
4915 }
françois romieu065c27c2011-01-03 15:08:12 +00004916}
4917
4918static void r810x_pll_power_up(struct rtl8169_private *tp)
4919{
Hayes Wang00042992012-03-30 14:33:00 +08004920 void __iomem *ioaddr = tp->mmio_addr;
4921
françois romieu065c27c2011-01-03 15:08:12 +00004922 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004923
4924 switch (tp->mac_version) {
4925 case RTL_GIGA_MAC_VER_07:
4926 case RTL_GIGA_MAC_VER_08:
4927 case RTL_GIGA_MAC_VER_09:
4928 case RTL_GIGA_MAC_VER_10:
4929 case RTL_GIGA_MAC_VER_13:
4930 case RTL_GIGA_MAC_VER_16:
4931 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004932 case RTL_GIGA_MAC_VER_47:
4933 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004934 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004935 break;
Hayes Wang00042992012-03-30 14:33:00 +08004936 default:
4937 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4938 break;
4939 }
françois romieu065c27c2011-01-03 15:08:12 +00004940}
4941
4942static void r8168_phy_power_up(struct rtl8169_private *tp)
4943{
4944 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004945 switch (tp->mac_version) {
4946 case RTL_GIGA_MAC_VER_11:
4947 case RTL_GIGA_MAC_VER_12:
4948 case RTL_GIGA_MAC_VER_17:
4949 case RTL_GIGA_MAC_VER_18:
4950 case RTL_GIGA_MAC_VER_19:
4951 case RTL_GIGA_MAC_VER_20:
4952 case RTL_GIGA_MAC_VER_21:
4953 case RTL_GIGA_MAC_VER_22:
4954 case RTL_GIGA_MAC_VER_23:
4955 case RTL_GIGA_MAC_VER_24:
4956 case RTL_GIGA_MAC_VER_25:
4957 case RTL_GIGA_MAC_VER_26:
4958 case RTL_GIGA_MAC_VER_27:
4959 case RTL_GIGA_MAC_VER_28:
4960 case RTL_GIGA_MAC_VER_31:
4961 rtl_writephy(tp, 0x0e, 0x0000);
4962 break;
4963 default:
4964 break;
4965 }
françois romieu065c27c2011-01-03 15:08:12 +00004966 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4967}
4968
4969static void r8168_phy_power_down(struct rtl8169_private *tp)
4970{
4971 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004972 switch (tp->mac_version) {
4973 case RTL_GIGA_MAC_VER_32:
4974 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004975 case RTL_GIGA_MAC_VER_40:
4976 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004977 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4978 break;
4979
4980 case RTL_GIGA_MAC_VER_11:
4981 case RTL_GIGA_MAC_VER_12:
4982 case RTL_GIGA_MAC_VER_17:
4983 case RTL_GIGA_MAC_VER_18:
4984 case RTL_GIGA_MAC_VER_19:
4985 case RTL_GIGA_MAC_VER_20:
4986 case RTL_GIGA_MAC_VER_21:
4987 case RTL_GIGA_MAC_VER_22:
4988 case RTL_GIGA_MAC_VER_23:
4989 case RTL_GIGA_MAC_VER_24:
4990 case RTL_GIGA_MAC_VER_25:
4991 case RTL_GIGA_MAC_VER_26:
4992 case RTL_GIGA_MAC_VER_27:
4993 case RTL_GIGA_MAC_VER_28:
4994 case RTL_GIGA_MAC_VER_31:
4995 rtl_writephy(tp, 0x0e, 0x0200);
4996 default:
4997 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4998 break;
4999 }
françois romieu065c27c2011-01-03 15:08:12 +00005000}
5001
5002static void r8168_pll_power_down(struct rtl8169_private *tp)
5003{
5004 void __iomem *ioaddr = tp->mmio_addr;
5005
Francois Romieucecb5fd2011-04-01 10:21:07 +02005006 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5007 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005008 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
5009 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5010 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5011 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08005012 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00005013 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08005014 }
françois romieu065c27c2011-01-03 15:08:12 +00005015
Francois Romieucecb5fd2011-04-01 10:21:07 +02005016 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
5017 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00005018 (RTL_R16(CPlusCmd) & ASF)) {
5019 return;
5020 }
5021
hayeswang01dc7fe2011-03-21 01:50:28 +00005022 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
5023 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02005024 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00005025
David S. Miller1805b2f2011-10-24 18:18:09 -04005026 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00005027 return;
françois romieu065c27c2011-01-03 15:08:12 +00005028
5029 r8168_phy_power_down(tp);
5030
5031 switch (tp->mac_version) {
5032 case RTL_GIGA_MAC_VER_25:
5033 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005034 case RTL_GIGA_MAC_VER_27:
5035 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005036 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005037 case RTL_GIGA_MAC_VER_32:
5038 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005039 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005040 case RTL_GIGA_MAC_VER_45:
5041 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005042 case RTL_GIGA_MAC_VER_50:
5043 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005044 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5045 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005046 case RTL_GIGA_MAC_VER_40:
5047 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005048 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005049 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005050 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005051 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00005052 break;
françois romieu065c27c2011-01-03 15:08:12 +00005053 }
5054}
5055
5056static void r8168_pll_power_up(struct rtl8169_private *tp)
5057{
5058 void __iomem *ioaddr = tp->mmio_addr;
5059
françois romieu065c27c2011-01-03 15:08:12 +00005060 switch (tp->mac_version) {
5061 case RTL_GIGA_MAC_VER_25:
5062 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005063 case RTL_GIGA_MAC_VER_27:
5064 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005065 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005066 case RTL_GIGA_MAC_VER_32:
5067 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00005068 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5069 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005070 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005071 case RTL_GIGA_MAC_VER_45:
5072 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005073 case RTL_GIGA_MAC_VER_50:
5074 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005075 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005076 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005077 case RTL_GIGA_MAC_VER_40:
5078 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005079 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005080 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005081 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005082 0x00000000, ERIAR_EXGMAC);
5083 break;
françois romieu065c27c2011-01-03 15:08:12 +00005084 }
5085
5086 r8168_phy_power_up(tp);
5087}
5088
Francois Romieud58d46b2011-05-03 16:38:29 +02005089static void rtl_generic_op(struct rtl8169_private *tp,
5090 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00005091{
5092 if (op)
5093 op(tp);
5094}
5095
5096static void rtl_pll_power_down(struct rtl8169_private *tp)
5097{
Francois Romieud58d46b2011-05-03 16:38:29 +02005098 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00005099}
5100
5101static void rtl_pll_power_up(struct rtl8169_private *tp)
5102{
Francois Romieud58d46b2011-05-03 16:38:29 +02005103 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00005104}
5105
Bill Pembertonbaf63292012-12-03 09:23:28 -05005106static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00005107{
5108 struct pll_power_ops *ops = &tp->pll_power_ops;
5109
5110 switch (tp->mac_version) {
5111 case RTL_GIGA_MAC_VER_07:
5112 case RTL_GIGA_MAC_VER_08:
5113 case RTL_GIGA_MAC_VER_09:
5114 case RTL_GIGA_MAC_VER_10:
5115 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08005116 case RTL_GIGA_MAC_VER_29:
5117 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005118 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08005119 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00005120 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005121 case RTL_GIGA_MAC_VER_47:
5122 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005123 ops->down = r810x_pll_power_down;
5124 ops->up = r810x_pll_power_up;
5125 break;
5126
5127 case RTL_GIGA_MAC_VER_11:
5128 case RTL_GIGA_MAC_VER_12:
5129 case RTL_GIGA_MAC_VER_17:
5130 case RTL_GIGA_MAC_VER_18:
5131 case RTL_GIGA_MAC_VER_19:
5132 case RTL_GIGA_MAC_VER_20:
5133 case RTL_GIGA_MAC_VER_21:
5134 case RTL_GIGA_MAC_VER_22:
5135 case RTL_GIGA_MAC_VER_23:
5136 case RTL_GIGA_MAC_VER_24:
5137 case RTL_GIGA_MAC_VER_25:
5138 case RTL_GIGA_MAC_VER_26:
5139 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005140 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005141 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005142 case RTL_GIGA_MAC_VER_32:
5143 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005144 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005145 case RTL_GIGA_MAC_VER_35:
5146 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005147 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005148 case RTL_GIGA_MAC_VER_40:
5149 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005150 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005151 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005152 case RTL_GIGA_MAC_VER_45:
5153 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005154 case RTL_GIGA_MAC_VER_49:
5155 case RTL_GIGA_MAC_VER_50:
5156 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005157 ops->down = r8168_pll_power_down;
5158 ops->up = r8168_pll_power_up;
5159 break;
5160
5161 default:
5162 ops->down = NULL;
5163 ops->up = NULL;
5164 break;
5165 }
5166}
5167
Hayes Wange542a222011-07-06 15:58:04 +08005168static void rtl_init_rxcfg(struct rtl8169_private *tp)
5169{
5170 void __iomem *ioaddr = tp->mmio_addr;
5171
5172 switch (tp->mac_version) {
5173 case RTL_GIGA_MAC_VER_01:
5174 case RTL_GIGA_MAC_VER_02:
5175 case RTL_GIGA_MAC_VER_03:
5176 case RTL_GIGA_MAC_VER_04:
5177 case RTL_GIGA_MAC_VER_05:
5178 case RTL_GIGA_MAC_VER_06:
5179 case RTL_GIGA_MAC_VER_10:
5180 case RTL_GIGA_MAC_VER_11:
5181 case RTL_GIGA_MAC_VER_12:
5182 case RTL_GIGA_MAC_VER_13:
5183 case RTL_GIGA_MAC_VER_14:
5184 case RTL_GIGA_MAC_VER_15:
5185 case RTL_GIGA_MAC_VER_16:
5186 case RTL_GIGA_MAC_VER_17:
5187 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5188 break;
5189 case RTL_GIGA_MAC_VER_18:
5190 case RTL_GIGA_MAC_VER_19:
5191 case RTL_GIGA_MAC_VER_20:
5192 case RTL_GIGA_MAC_VER_21:
5193 case RTL_GIGA_MAC_VER_22:
5194 case RTL_GIGA_MAC_VER_23:
5195 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005196 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005197 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08005198 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5199 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005200 case RTL_GIGA_MAC_VER_40:
5201 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005202 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005203 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005204 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005205 case RTL_GIGA_MAC_VER_45:
5206 case RTL_GIGA_MAC_VER_46:
5207 case RTL_GIGA_MAC_VER_47:
5208 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005209 case RTL_GIGA_MAC_VER_49:
5210 case RTL_GIGA_MAC_VER_50:
5211 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02005212 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005213 break;
Hayes Wange542a222011-07-06 15:58:04 +08005214 default:
5215 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5216 break;
5217 }
5218}
5219
Hayes Wang92fc43b2011-07-06 15:58:03 +08005220static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5221{
Timo Teräs9fba0812013-01-15 21:01:24 +00005222 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005223}
5224
Francois Romieud58d46b2011-05-03 16:38:29 +02005225static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5226{
françois romieu9c5028e2012-03-02 04:43:14 +00005227 void __iomem *ioaddr = tp->mmio_addr;
5228
5229 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005230 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005231 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005232}
5233
5234static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5235{
françois romieu9c5028e2012-03-02 04:43:14 +00005236 void __iomem *ioaddr = tp->mmio_addr;
5237
5238 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005239 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005240 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005241}
5242
5243static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5244{
5245 void __iomem *ioaddr = tp->mmio_addr;
5246
5247 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5248 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005249 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005250}
5251
5252static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5253{
5254 void __iomem *ioaddr = tp->mmio_addr;
5255
5256 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5257 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5258 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5259}
5260
5261static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5262{
5263 void __iomem *ioaddr = tp->mmio_addr;
5264
5265 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5266}
5267
5268static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5269{
5270 void __iomem *ioaddr = tp->mmio_addr;
5271
5272 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5273}
5274
5275static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5276{
5277 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005278
5279 RTL_W8(MaxTxPacketSize, 0x3f);
5280 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5281 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005282 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005283}
5284
5285static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5286{
5287 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005288
5289 RTL_W8(MaxTxPacketSize, 0x0c);
5290 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5291 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005292 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005293}
5294
5295static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5296{
5297 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005298 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005299}
5300
5301static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5302{
5303 rtl_tx_performance_tweak(tp->pci_dev,
5304 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5305}
5306
5307static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5308{
5309 void __iomem *ioaddr = tp->mmio_addr;
5310
5311 r8168b_0_hw_jumbo_enable(tp);
5312
5313 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5314}
5315
5316static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5317{
5318 void __iomem *ioaddr = tp->mmio_addr;
5319
5320 r8168b_0_hw_jumbo_disable(tp);
5321
5322 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5323}
5324
Bill Pembertonbaf63292012-12-03 09:23:28 -05005325static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005326{
5327 struct jumbo_ops *ops = &tp->jumbo_ops;
5328
5329 switch (tp->mac_version) {
5330 case RTL_GIGA_MAC_VER_11:
5331 ops->disable = r8168b_0_hw_jumbo_disable;
5332 ops->enable = r8168b_0_hw_jumbo_enable;
5333 break;
5334 case RTL_GIGA_MAC_VER_12:
5335 case RTL_GIGA_MAC_VER_17:
5336 ops->disable = r8168b_1_hw_jumbo_disable;
5337 ops->enable = r8168b_1_hw_jumbo_enable;
5338 break;
5339 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5340 case RTL_GIGA_MAC_VER_19:
5341 case RTL_GIGA_MAC_VER_20:
5342 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5343 case RTL_GIGA_MAC_VER_22:
5344 case RTL_GIGA_MAC_VER_23:
5345 case RTL_GIGA_MAC_VER_24:
5346 case RTL_GIGA_MAC_VER_25:
5347 case RTL_GIGA_MAC_VER_26:
5348 ops->disable = r8168c_hw_jumbo_disable;
5349 ops->enable = r8168c_hw_jumbo_enable;
5350 break;
5351 case RTL_GIGA_MAC_VER_27:
5352 case RTL_GIGA_MAC_VER_28:
5353 ops->disable = r8168dp_hw_jumbo_disable;
5354 ops->enable = r8168dp_hw_jumbo_enable;
5355 break;
5356 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5357 case RTL_GIGA_MAC_VER_32:
5358 case RTL_GIGA_MAC_VER_33:
5359 case RTL_GIGA_MAC_VER_34:
5360 ops->disable = r8168e_hw_jumbo_disable;
5361 ops->enable = r8168e_hw_jumbo_enable;
5362 break;
5363
5364 /*
5365 * No action needed for jumbo frames with 8169.
5366 * No jumbo for 810x at all.
5367 */
Hayes Wangc5583862012-07-02 17:23:22 +08005368 case RTL_GIGA_MAC_VER_40:
5369 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005370 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005371 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005372 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005373 case RTL_GIGA_MAC_VER_45:
5374 case RTL_GIGA_MAC_VER_46:
5375 case RTL_GIGA_MAC_VER_47:
5376 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005377 case RTL_GIGA_MAC_VER_49:
5378 case RTL_GIGA_MAC_VER_50:
5379 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005380 default:
5381 ops->disable = NULL;
5382 ops->enable = NULL;
5383 break;
5384 }
5385}
5386
Francois Romieuffc46952012-07-06 14:19:23 +02005387DECLARE_RTL_COND(rtl_chipcmd_cond)
5388{
5389 void __iomem *ioaddr = tp->mmio_addr;
5390
5391 return RTL_R8(ChipCmd) & CmdReset;
5392}
5393
Francois Romieu6f43adc2011-04-29 15:05:51 +02005394static void rtl_hw_reset(struct rtl8169_private *tp)
5395{
5396 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005397
Francois Romieu6f43adc2011-04-29 15:05:51 +02005398 RTL_W8(ChipCmd, CmdReset);
5399
Francois Romieuffc46952012-07-06 14:19:23 +02005400 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005401}
5402
Francois Romieub6ffd972011-06-17 17:00:05 +02005403static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5404{
5405 struct rtl_fw *rtl_fw;
5406 const char *name;
5407 int rc = -ENOMEM;
5408
5409 name = rtl_lookup_firmware_name(tp);
5410 if (!name)
5411 goto out_no_firmware;
5412
5413 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5414 if (!rtl_fw)
5415 goto err_warn;
5416
5417 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5418 if (rc < 0)
5419 goto err_free;
5420
Francois Romieufd112f22011-06-18 00:10:29 +02005421 rc = rtl_check_firmware(tp, rtl_fw);
5422 if (rc < 0)
5423 goto err_release_firmware;
5424
Francois Romieub6ffd972011-06-17 17:00:05 +02005425 tp->rtl_fw = rtl_fw;
5426out:
5427 return;
5428
Francois Romieufd112f22011-06-18 00:10:29 +02005429err_release_firmware:
5430 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005431err_free:
5432 kfree(rtl_fw);
5433err_warn:
5434 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5435 name, rc);
5436out_no_firmware:
5437 tp->rtl_fw = NULL;
5438 goto out;
5439}
5440
François Romieu953a12c2011-04-24 17:38:48 +02005441static void rtl_request_firmware(struct rtl8169_private *tp)
5442{
Francois Romieub6ffd972011-06-17 17:00:05 +02005443 if (IS_ERR(tp->rtl_fw))
5444 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005445}
5446
Hayes Wang92fc43b2011-07-06 15:58:03 +08005447static void rtl_rx_close(struct rtl8169_private *tp)
5448{
5449 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005450
Francois Romieu1687b562011-07-19 17:21:29 +02005451 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005452}
5453
Francois Romieuffc46952012-07-06 14:19:23 +02005454DECLARE_RTL_COND(rtl_npq_cond)
5455{
5456 void __iomem *ioaddr = tp->mmio_addr;
5457
5458 return RTL_R8(TxPoll) & NPQ;
5459}
5460
5461DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5462{
5463 void __iomem *ioaddr = tp->mmio_addr;
5464
5465 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5466}
5467
françois romieue6de30d2011-01-03 15:08:37 +00005468static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469{
françois romieue6de30d2011-01-03 15:08:37 +00005470 void __iomem *ioaddr = tp->mmio_addr;
5471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005473 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Hayes Wang92fc43b2011-07-06 15:58:03 +08005475 rtl_rx_close(tp);
5476
Hayes Wang5d2e1952011-02-22 17:26:22 +08005477 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005478 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5479 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005480 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005481 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005482 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5483 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5484 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5485 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5486 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5487 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5488 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5489 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5490 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5491 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5492 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5493 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005494 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5495 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5496 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5497 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005498 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005499 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005500 } else {
5501 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5502 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005503 }
5504
Hayes Wang92fc43b2011-07-06 15:58:03 +08005505 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506}
5507
Francois Romieu7f796d832007-06-11 23:04:41 +02005508static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005509{
5510 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005511
5512 /* Set DMA burst size and Interframe Gap Time */
5513 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5514 (InterFrameGap << TxInterFrameGapShift));
5515}
5516
Francois Romieu07ce4062007-02-23 23:36:39 +01005517static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
5519 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Francois Romieu07ce4062007-02-23 23:36:39 +01005521 tp->hw_start(dev);
5522
Francois Romieuda78dbf2012-01-26 14:18:23 +01005523 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005524}
5525
Francois Romieu7f796d832007-06-11 23:04:41 +02005526static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5527 void __iomem *ioaddr)
5528{
5529 /*
5530 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5531 * register to be written before TxDescAddrLow to work.
5532 * Switching from MMIO to I/O access fixes the issue as well.
5533 */
5534 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005535 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005536 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005537 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005538}
5539
5540static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5541{
5542 u16 cmd;
5543
5544 cmd = RTL_R16(CPlusCmd);
5545 RTL_W16(CPlusCmd, cmd);
5546 return cmd;
5547}
5548
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005549static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005550{
5551 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005552 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005553}
5554
Francois Romieu6dccd162007-02-13 23:38:05 +01005555static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5556{
Francois Romieu37441002011-06-17 22:58:54 +02005557 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005558 u32 mac_version;
5559 u32 clk;
5560 u32 val;
5561 } cfg2_info [] = {
5562 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5563 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5564 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5565 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005566 };
5567 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005568 unsigned int i;
5569 u32 clk;
5570
5571 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005572 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005573 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5574 RTL_W32(0x7c, p->val);
5575 break;
5576 }
5577 }
5578}
5579
Francois Romieue6b763e2012-03-08 09:35:39 +01005580static void rtl_set_rx_mode(struct net_device *dev)
5581{
5582 struct rtl8169_private *tp = netdev_priv(dev);
5583 void __iomem *ioaddr = tp->mmio_addr;
5584 u32 mc_filter[2]; /* Multicast hash filter */
5585 int rx_mode;
5586 u32 tmp = 0;
5587
5588 if (dev->flags & IFF_PROMISC) {
5589 /* Unconditionally log net taps. */
5590 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5591 rx_mode =
5592 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5593 AcceptAllPhys;
5594 mc_filter[1] = mc_filter[0] = 0xffffffff;
5595 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5596 (dev->flags & IFF_ALLMULTI)) {
5597 /* Too many to filter perfectly -- accept all multicasts. */
5598 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5599 mc_filter[1] = mc_filter[0] = 0xffffffff;
5600 } else {
5601 struct netdev_hw_addr *ha;
5602
5603 rx_mode = AcceptBroadcast | AcceptMyPhys;
5604 mc_filter[1] = mc_filter[0] = 0;
5605 netdev_for_each_mc_addr(ha, dev) {
5606 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5607 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5608 rx_mode |= AcceptMulticast;
5609 }
5610 }
5611
5612 if (dev->features & NETIF_F_RXALL)
5613 rx_mode |= (AcceptErr | AcceptRunt);
5614
5615 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5616
5617 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5618 u32 data = mc_filter[0];
5619
5620 mc_filter[0] = swab32(mc_filter[1]);
5621 mc_filter[1] = swab32(data);
5622 }
5623
Nathan Walp04817762012-11-01 12:08:47 +00005624 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5625 mc_filter[1] = mc_filter[0] = 0xffffffff;
5626
Francois Romieue6b763e2012-03-08 09:35:39 +01005627 RTL_W32(MAR0 + 4, mc_filter[1]);
5628 RTL_W32(MAR0 + 0, mc_filter[0]);
5629
5630 RTL_W32(RxConfig, tmp);
5631}
5632
Francois Romieu07ce4062007-02-23 23:36:39 +01005633static void rtl_hw_start_8169(struct net_device *dev)
5634{
5635 struct rtl8169_private *tp = netdev_priv(dev);
5636 void __iomem *ioaddr = tp->mmio_addr;
5637 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005638
Francois Romieu9cb427b2006-11-02 00:10:16 +01005639 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5640 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5641 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5642 }
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005645 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5646 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5647 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5648 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005649 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5650
Hayes Wange542a222011-07-06 15:58:04 +08005651 rtl_init_rxcfg(tp);
5652
françois romieuf0298f82011-01-03 15:07:42 +00005653 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005655 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Francois Romieucecb5fd2011-04-01 10:21:07 +02005657 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5658 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5659 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5660 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005661 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Francois Romieu7f796d832007-06-11 23:04:41 +02005663 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005664
Francois Romieucecb5fd2011-04-01 10:21:07 +02005665 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5666 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005667 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005669 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
5671
Francois Romieubcf0bf92006-07-26 23:14:13 +02005672 RTL_W16(CPlusCmd, tp->cp_cmd);
5673
Francois Romieu6dccd162007-02-13 23:38:05 +01005674 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 /*
5677 * Undocumented corner. Supposedly:
5678 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5679 */
5680 RTL_W16(IntrMitigate, 0x0000);
5681
Francois Romieu7f796d832007-06-11 23:04:41 +02005682 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005683
Francois Romieucecb5fd2011-04-01 10:21:07 +02005684 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5685 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5686 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5687 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005688 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5689 rtl_set_rx_tx_config_registers(tp);
5690 }
5691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005693
5694 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5695 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 RTL_W32(RxMissed, 0);
5698
Francois Romieu07ce4062007-02-23 23:36:39 +01005699 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
5701 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005702 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005705static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5706{
5707 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005708 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005709}
5710
5711static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5712{
Francois Romieu52989f02012-07-06 13:37:00 +02005713 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005714}
5715
5716static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005717{
5718 u32 csi;
5719
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005720 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5721 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005722}
5723
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005724static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005725{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005726 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005727}
5728
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005729static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005730{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005731 rtl_csi_access_enable(tp, 0x27000000);
5732}
5733
Francois Romieuffc46952012-07-06 14:19:23 +02005734DECLARE_RTL_COND(rtl_csiar_cond)
5735{
5736 void __iomem *ioaddr = tp->mmio_addr;
5737
5738 return RTL_R32(CSIAR) & CSIAR_FLAG;
5739}
5740
Francois Romieu52989f02012-07-06 13:37:00 +02005741static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005742{
Francois Romieu52989f02012-07-06 13:37:00 +02005743 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005744
5745 RTL_W32(CSIDR, value);
5746 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5747 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5748
Francois Romieuffc46952012-07-06 14:19:23 +02005749 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005750}
5751
Francois Romieu52989f02012-07-06 13:37:00 +02005752static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005753{
Francois Romieu52989f02012-07-06 13:37:00 +02005754 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005755
5756 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5757 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5758
Francois Romieuffc46952012-07-06 14:19:23 +02005759 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5760 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005761}
5762
Francois Romieu52989f02012-07-06 13:37:00 +02005763static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005764{
Francois Romieu52989f02012-07-06 13:37:00 +02005765 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005766
5767 RTL_W32(CSIDR, value);
5768 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5769 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5770 CSIAR_FUNC_NIC);
5771
Francois Romieuffc46952012-07-06 14:19:23 +02005772 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005773}
5774
Francois Romieu52989f02012-07-06 13:37:00 +02005775static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005776{
Francois Romieu52989f02012-07-06 13:37:00 +02005777 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005778
5779 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5780 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5781
Francois Romieuffc46952012-07-06 14:19:23 +02005782 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5783 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005784}
5785
hayeswang45dd95c2013-07-08 17:09:01 +08005786static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5787{
5788 void __iomem *ioaddr = tp->mmio_addr;
5789
5790 RTL_W32(CSIDR, value);
5791 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5792 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5793 CSIAR_FUNC_NIC2);
5794
5795 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5796}
5797
5798static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5799{
5800 void __iomem *ioaddr = tp->mmio_addr;
5801
5802 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5803 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5804
5805 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5806 RTL_R32(CSIDR) : ~0;
5807}
5808
Bill Pembertonbaf63292012-12-03 09:23:28 -05005809static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005810{
5811 struct csi_ops *ops = &tp->csi_ops;
5812
5813 switch (tp->mac_version) {
5814 case RTL_GIGA_MAC_VER_01:
5815 case RTL_GIGA_MAC_VER_02:
5816 case RTL_GIGA_MAC_VER_03:
5817 case RTL_GIGA_MAC_VER_04:
5818 case RTL_GIGA_MAC_VER_05:
5819 case RTL_GIGA_MAC_VER_06:
5820 case RTL_GIGA_MAC_VER_10:
5821 case RTL_GIGA_MAC_VER_11:
5822 case RTL_GIGA_MAC_VER_12:
5823 case RTL_GIGA_MAC_VER_13:
5824 case RTL_GIGA_MAC_VER_14:
5825 case RTL_GIGA_MAC_VER_15:
5826 case RTL_GIGA_MAC_VER_16:
5827 case RTL_GIGA_MAC_VER_17:
5828 ops->write = NULL;
5829 ops->read = NULL;
5830 break;
5831
Hayes Wang7e18dca2012-03-30 14:33:02 +08005832 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005833 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005834 ops->write = r8402_csi_write;
5835 ops->read = r8402_csi_read;
5836 break;
5837
hayeswang45dd95c2013-07-08 17:09:01 +08005838 case RTL_GIGA_MAC_VER_44:
5839 ops->write = r8411_csi_write;
5840 ops->read = r8411_csi_read;
5841 break;
5842
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005843 default:
5844 ops->write = r8169_csi_write;
5845 ops->read = r8169_csi_read;
5846 break;
5847 }
Francois Romieudacf8152008-08-02 20:44:13 +02005848}
5849
5850struct ephy_info {
5851 unsigned int offset;
5852 u16 mask;
5853 u16 bits;
5854};
5855
Francois Romieufdf6fc02012-07-06 22:40:38 +02005856static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5857 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005858{
5859 u16 w;
5860
5861 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005862 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5863 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005864 e++;
5865 }
5866}
5867
Francois Romieub726e492008-06-28 12:22:59 +02005868static void rtl_disable_clock_request(struct pci_dev *pdev)
5869{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005870 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5871 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005872}
5873
françois romieue6de30d2011-01-03 15:08:37 +00005874static void rtl_enable_clock_request(struct pci_dev *pdev)
5875{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005876 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5877 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005878}
5879
hayeswangb51ecea2014-07-09 14:52:51 +08005880static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5881{
5882 void __iomem *ioaddr = tp->mmio_addr;
5883 u8 data;
5884
5885 data = RTL_R8(Config3);
5886
5887 if (enable)
5888 data |= Rdy_to_L23;
5889 else
5890 data &= ~Rdy_to_L23;
5891
5892 RTL_W8(Config3, data);
5893}
5894
Francois Romieub726e492008-06-28 12:22:59 +02005895#define R8168_CPCMD_QUIRK_MASK (\
5896 EnableBist | \
5897 Mac_dbgo_oe | \
5898 Force_half_dup | \
5899 Force_rxflow_en | \
5900 Force_txflow_en | \
5901 Cxpl_dbg_sel | \
5902 ASF | \
5903 PktCntrDisable | \
5904 Mac_dbgo_sel)
5905
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005906static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005907{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005908 void __iomem *ioaddr = tp->mmio_addr;
5909 struct pci_dev *pdev = tp->pci_dev;
5910
Francois Romieub726e492008-06-28 12:22:59 +02005911 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5912
5913 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5914
françois romieufaf1e782013-02-27 13:01:57 +00005915 if (tp->dev->mtu <= ETH_DATA_LEN) {
5916 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5917 PCI_EXP_DEVCTL_NOSNOOP_EN);
5918 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005919}
5920
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005921static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005922{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005923 void __iomem *ioaddr = tp->mmio_addr;
5924
5925 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005926
françois romieuf0298f82011-01-03 15:07:42 +00005927 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005928
5929 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005930}
5931
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005932static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005933{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005934 void __iomem *ioaddr = tp->mmio_addr;
5935 struct pci_dev *pdev = tp->pci_dev;
5936
Francois Romieub726e492008-06-28 12:22:59 +02005937 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5938
5939 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5940
françois romieufaf1e782013-02-27 13:01:57 +00005941 if (tp->dev->mtu <= ETH_DATA_LEN)
5942 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005943
5944 rtl_disable_clock_request(pdev);
5945
5946 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005947}
5948
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005949static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005950{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005951 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005952 { 0x01, 0, 0x0001 },
5953 { 0x02, 0x0800, 0x1000 },
5954 { 0x03, 0, 0x0042 },
5955 { 0x06, 0x0080, 0x0000 },
5956 { 0x07, 0, 0x2000 }
5957 };
5958
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005959 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005960
Francois Romieufdf6fc02012-07-06 22:40:38 +02005961 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005962
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005963 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005964}
5965
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005966static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005967{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005968 void __iomem *ioaddr = tp->mmio_addr;
5969 struct pci_dev *pdev = tp->pci_dev;
5970
5971 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005972
5973 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5974
françois romieufaf1e782013-02-27 13:01:57 +00005975 if (tp->dev->mtu <= ETH_DATA_LEN)
5976 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005977
5978 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5979}
5980
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005981static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005982{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005983 void __iomem *ioaddr = tp->mmio_addr;
5984 struct pci_dev *pdev = tp->pci_dev;
5985
5986 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005987
5988 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5989
5990 /* Magic. */
5991 RTL_W8(DBG_REG, 0x20);
5992
françois romieuf0298f82011-01-03 15:07:42 +00005993 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005994
françois romieufaf1e782013-02-27 13:01:57 +00005995 if (tp->dev->mtu <= ETH_DATA_LEN)
5996 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005997
5998 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5999}
6000
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006001static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02006002{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006003 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006004 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02006005 { 0x02, 0x0800, 0x1000 },
6006 { 0x03, 0, 0x0002 },
6007 { 0x06, 0x0080, 0x0000 }
6008 };
6009
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006010 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02006011
6012 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
6013
Francois Romieufdf6fc02012-07-06 22:40:38 +02006014 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02006015
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006016 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006017}
6018
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006019static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02006020{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006021 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02006022 { 0x01, 0, 0x0001 },
6023 { 0x03, 0x0400, 0x0220 }
6024 };
6025
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006026 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02006027
Francois Romieufdf6fc02012-07-06 22:40:38 +02006028 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02006029
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006030 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006031}
6032
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006033static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02006034{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006035 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02006036}
6037
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006038static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02006039{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006040 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006041
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006042 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006043}
6044
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006045static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02006046{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006047 void __iomem *ioaddr = tp->mmio_addr;
6048 struct pci_dev *pdev = tp->pci_dev;
6049
6050 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02006051
6052 rtl_disable_clock_request(pdev);
6053
françois romieuf0298f82011-01-03 15:07:42 +00006054 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02006055
françois romieufaf1e782013-02-27 13:01:57 +00006056 if (tp->dev->mtu <= ETH_DATA_LEN)
6057 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02006058
6059 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6060}
6061
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006062static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00006063{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006064 void __iomem *ioaddr = tp->mmio_addr;
6065 struct pci_dev *pdev = tp->pci_dev;
6066
6067 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006068
françois romieufaf1e782013-02-27 13:01:57 +00006069 if (tp->dev->mtu <= ETH_DATA_LEN)
6070 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00006071
6072 RTL_W8(MaxTxPacketSize, TxPacketMax);
6073
6074 rtl_disable_clock_request(pdev);
6075}
6076
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006077static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00006078{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006079 void __iomem *ioaddr = tp->mmio_addr;
6080 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00006081 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006082 { 0x0b, 0x0000, 0x0048 },
6083 { 0x19, 0x0020, 0x0050 },
6084 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00006085 };
françois romieue6de30d2011-01-03 15:08:37 +00006086
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006087 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00006088
6089 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6090
6091 RTL_W8(MaxTxPacketSize, TxPacketMax);
6092
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006093 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00006094
6095 rtl_enable_clock_request(pdev);
6096}
6097
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006098static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00006099{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006100 void __iomem *ioaddr = tp->mmio_addr;
6101 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006102 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00006103 { 0x00, 0x0200, 0x0100 },
6104 { 0x00, 0x0000, 0x0004 },
6105 { 0x06, 0x0002, 0x0001 },
6106 { 0x06, 0x0000, 0x0030 },
6107 { 0x07, 0x0000, 0x2000 },
6108 { 0x00, 0x0000, 0x0020 },
6109 { 0x03, 0x5800, 0x2000 },
6110 { 0x03, 0x0000, 0x0001 },
6111 { 0x01, 0x0800, 0x1000 },
6112 { 0x07, 0x0000, 0x4000 },
6113 { 0x1e, 0x0000, 0x2000 },
6114 { 0x19, 0xffff, 0xfe6c },
6115 { 0x0a, 0x0000, 0x0040 }
6116 };
6117
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006118 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006119
Francois Romieufdf6fc02012-07-06 22:40:38 +02006120 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00006121
françois romieufaf1e782013-02-27 13:01:57 +00006122 if (tp->dev->mtu <= ETH_DATA_LEN)
6123 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00006124
6125 RTL_W8(MaxTxPacketSize, TxPacketMax);
6126
6127 rtl_disable_clock_request(pdev);
6128
6129 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02006130 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6131 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00006132
Francois Romieucecb5fd2011-04-01 10:21:07 +02006133 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00006134}
6135
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006136static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08006137{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006138 void __iomem *ioaddr = tp->mmio_addr;
6139 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006140 static const struct ephy_info e_info_8168e_2[] = {
6141 { 0x09, 0x0000, 0x0080 },
6142 { 0x19, 0x0000, 0x0224 }
6143 };
6144
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006145 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006146
Francois Romieufdf6fc02012-07-06 22:40:38 +02006147 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08006148
françois romieufaf1e782013-02-27 13:01:57 +00006149 if (tp->dev->mtu <= ETH_DATA_LEN)
6150 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08006151
Francois Romieufdf6fc02012-07-06 22:40:38 +02006152 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6153 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6154 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6155 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6156 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6157 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006158 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6159 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08006160
Hayes Wang3090bd92011-09-06 16:55:15 +08006161 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08006162
Francois Romieu4521e1a92012-11-01 16:46:28 +00006163 rtl_disable_clock_request(pdev);
6164
Hayes Wang70090422011-07-06 15:58:06 +08006165 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6166 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6167
6168 /* Adjust EEE LED frequency */
6169 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6170
6171 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6172 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006173 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08006174}
6175
Hayes Wang5f886e02012-03-30 14:33:03 +08006176static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08006177{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006178 void __iomem *ioaddr = tp->mmio_addr;
6179 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08006180
Hayes Wang5f886e02012-03-30 14:33:03 +08006181 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006182
6183 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6184
Francois Romieufdf6fc02012-07-06 22:40:38 +02006185 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6186 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6187 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6188 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006189 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6190 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6191 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6192 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006193 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6194 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006195
6196 RTL_W8(MaxTxPacketSize, EarlySize);
6197
Francois Romieu4521e1a92012-11-01 16:46:28 +00006198 rtl_disable_clock_request(pdev);
6199
Hayes Wangc2218922011-09-06 16:55:18 +08006200 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6201 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08006202 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006203 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6204 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006205}
6206
Hayes Wang5f886e02012-03-30 14:33:03 +08006207static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6208{
6209 void __iomem *ioaddr = tp->mmio_addr;
6210 static const struct ephy_info e_info_8168f_1[] = {
6211 { 0x06, 0x00c0, 0x0020 },
6212 { 0x08, 0x0001, 0x0002 },
6213 { 0x09, 0x0000, 0x0080 },
6214 { 0x19, 0x0000, 0x0224 }
6215 };
6216
6217 rtl_hw_start_8168f(tp);
6218
Francois Romieufdf6fc02012-07-06 22:40:38 +02006219 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006220
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006221 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006222
6223 /* Adjust EEE LED frequency */
6224 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6225}
6226
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006227static void rtl_hw_start_8411(struct rtl8169_private *tp)
6228{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006229 static const struct ephy_info e_info_8168f_1[] = {
6230 { 0x06, 0x00c0, 0x0020 },
6231 { 0x0f, 0xffff, 0x5200 },
6232 { 0x1e, 0x0000, 0x4000 },
6233 { 0x19, 0x0000, 0x0224 }
6234 };
6235
6236 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006237 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006238
Francois Romieufdf6fc02012-07-06 22:40:38 +02006239 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006240
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006241 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006242}
6243
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006244static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006245{
6246 void __iomem *ioaddr = tp->mmio_addr;
6247 struct pci_dev *pdev = tp->pci_dev;
6248
hayeswangbeb330a2013-04-01 22:23:39 +00006249 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6250
Hayes Wangc5583862012-07-02 17:23:22 +08006251 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6252 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6253 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6254 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6255
6256 rtl_csi_access_enable_1(tp);
6257
6258 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6259
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006260 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6261 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006262 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006263
Francois Romieu4521e1a92012-11-01 16:46:28 +00006264 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006265 RTL_W8(MaxTxPacketSize, EarlySize);
6266
6267 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6268 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6269
6270 /* Adjust EEE LED frequency */
6271 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6272
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006273 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6274 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006275
6276 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006277}
6278
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006279static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6280{
6281 void __iomem *ioaddr = tp->mmio_addr;
6282 static const struct ephy_info e_info_8168g_1[] = {
6283 { 0x00, 0x0000, 0x0008 },
6284 { 0x0c, 0x37d0, 0x0820 },
6285 { 0x1e, 0x0000, 0x0001 },
6286 { 0x19, 0x8000, 0x0000 }
6287 };
6288
6289 rtl_hw_start_8168g(tp);
6290
6291 /* disable aspm and clock request before access ephy */
6292 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6293 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6294 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6295}
6296
hayeswang57538c42013-04-01 22:23:40 +00006297static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6298{
6299 void __iomem *ioaddr = tp->mmio_addr;
6300 static const struct ephy_info e_info_8168g_2[] = {
6301 { 0x00, 0x0000, 0x0008 },
6302 { 0x0c, 0x3df0, 0x0200 },
6303 { 0x19, 0xffff, 0xfc00 },
6304 { 0x1e, 0xffff, 0x20eb }
6305 };
6306
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006307 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006308
6309 /* disable aspm and clock request before access ephy */
6310 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6311 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6312 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6313}
6314
hayeswang45dd95c2013-07-08 17:09:01 +08006315static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6316{
6317 void __iomem *ioaddr = tp->mmio_addr;
6318 static const struct ephy_info e_info_8411_2[] = {
6319 { 0x00, 0x0000, 0x0008 },
6320 { 0x0c, 0x3df0, 0x0200 },
6321 { 0x0f, 0xffff, 0x5200 },
6322 { 0x19, 0x0020, 0x0000 },
6323 { 0x1e, 0x0000, 0x2000 }
6324 };
6325
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006326 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006327
6328 /* disable aspm and clock request before access ephy */
6329 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6330 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6331 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6332}
6333
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006334static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6335{
6336 void __iomem *ioaddr = tp->mmio_addr;
6337 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006338 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006339 u32 data;
6340 static const struct ephy_info e_info_8168h_1[] = {
6341 { 0x1e, 0x0800, 0x0001 },
6342 { 0x1d, 0x0000, 0x0800 },
6343 { 0x05, 0xffff, 0x2089 },
6344 { 0x06, 0xffff, 0x5881 },
6345 { 0x04, 0xffff, 0x154a },
6346 { 0x01, 0xffff, 0x068b }
6347 };
6348
6349 /* disable aspm and clock request before access ephy */
6350 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6351 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6352 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6353
6354 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6355
6356 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6357 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6358 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6359 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6360
6361 rtl_csi_access_enable_1(tp);
6362
6363 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6364
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006365 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6366 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006367
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006368 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006369
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006370 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006371
6372 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6373
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006374 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6375 RTL_W8(MaxTxPacketSize, EarlySize);
6376
6377 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6378 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6379
6380 /* Adjust EEE LED frequency */
6381 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6382
6383 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006384 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006385
6386 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6387
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006388 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006389
6390 rtl_pcie_state_l2l3_enable(tp, false);
6391
6392 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006393 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006394 rtl_writephy(tp, 0x1f, 0x0000);
6395 if (rg_saw_cnt > 0) {
6396 u16 sw_cnt_1ms_ini;
6397
6398 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6399 sw_cnt_1ms_ini &= 0x0fff;
6400 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006401 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006402 data |= sw_cnt_1ms_ini;
6403 r8168_mac_ocp_write(tp, 0xd412, data);
6404 }
6405
6406 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006407 data &= ~0xf0;
6408 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006409 r8168_mac_ocp_write(tp, 0xe056, data);
6410
6411 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006412 data &= ~0x6000;
6413 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006414 r8168_mac_ocp_write(tp, 0xe052, data);
6415
6416 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006417 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006418 data |= 0x017f;
6419 r8168_mac_ocp_write(tp, 0xe0d6, data);
6420
6421 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006422 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006423 data |= 0x047f;
6424 r8168_mac_ocp_write(tp, 0xd420, data);
6425
6426 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6427 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6428 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6429 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6430}
6431
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006432static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6433{
6434 void __iomem *ioaddr = tp->mmio_addr;
6435 struct pci_dev *pdev = tp->pci_dev;
6436
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006437 rtl8168ep_stop_cmac(tp);
6438
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006439 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6440
6441 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6442 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6443 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6444 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6445
6446 rtl_csi_access_enable_1(tp);
6447
6448 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6449
6450 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6451 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6452
6453 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6454
6455 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6456
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006457 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6458 RTL_W8(MaxTxPacketSize, EarlySize);
6459
6460 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6461 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6462
6463 /* Adjust EEE LED frequency */
6464 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6465
6466 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6467
6468 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6469
6470 rtl_pcie_state_l2l3_enable(tp, false);
6471}
6472
6473static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6474{
6475 void __iomem *ioaddr = tp->mmio_addr;
6476 static const struct ephy_info e_info_8168ep_1[] = {
6477 { 0x00, 0xffff, 0x10ab },
6478 { 0x06, 0xffff, 0xf030 },
6479 { 0x08, 0xffff, 0x2006 },
6480 { 0x0d, 0xffff, 0x1666 },
6481 { 0x0c, 0x3ff0, 0x0000 }
6482 };
6483
6484 /* disable aspm and clock request before access ephy */
6485 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6486 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6487 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6488
6489 rtl_hw_start_8168ep(tp);
6490}
6491
6492static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6493{
6494 void __iomem *ioaddr = tp->mmio_addr;
6495 static const struct ephy_info e_info_8168ep_2[] = {
6496 { 0x00, 0xffff, 0x10a3 },
6497 { 0x19, 0xffff, 0xfc00 },
6498 { 0x1e, 0xffff, 0x20ea }
6499 };
6500
6501 /* disable aspm and clock request before access ephy */
6502 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6503 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6504 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6505
6506 rtl_hw_start_8168ep(tp);
6507
6508 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006509 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006510}
6511
6512static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6513{
6514 void __iomem *ioaddr = tp->mmio_addr;
6515 u32 data;
6516 static const struct ephy_info e_info_8168ep_3[] = {
6517 { 0x00, 0xffff, 0x10a3 },
6518 { 0x19, 0xffff, 0x7c00 },
6519 { 0x1e, 0xffff, 0x20eb },
6520 { 0x0d, 0xffff, 0x1666 }
6521 };
6522
6523 /* disable aspm and clock request before access ephy */
6524 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6525 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6526 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6527
6528 rtl_hw_start_8168ep(tp);
6529
6530 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006531 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006532
6533 data = r8168_mac_ocp_read(tp, 0xd3e2);
6534 data &= 0xf000;
6535 data |= 0x0271;
6536 r8168_mac_ocp_write(tp, 0xd3e2, data);
6537
6538 data = r8168_mac_ocp_read(tp, 0xd3e4);
6539 data &= 0xff00;
6540 r8168_mac_ocp_write(tp, 0xd3e4, data);
6541
6542 data = r8168_mac_ocp_read(tp, 0xe860);
6543 data |= 0x0080;
6544 r8168_mac_ocp_write(tp, 0xe860, data);
6545}
6546
Francois Romieu07ce4062007-02-23 23:36:39 +01006547static void rtl_hw_start_8168(struct net_device *dev)
6548{
Francois Romieu2dd99532007-06-11 23:22:52 +02006549 struct rtl8169_private *tp = netdev_priv(dev);
6550 void __iomem *ioaddr = tp->mmio_addr;
6551
6552 RTL_W8(Cfg9346, Cfg9346_Unlock);
6553
françois romieuf0298f82011-01-03 15:07:42 +00006554 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006555
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006556 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006557
Francois Romieu0e485152007-02-20 00:00:26 +01006558 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006559
6560 RTL_W16(CPlusCmd, tp->cp_cmd);
6561
Francois Romieu0e485152007-02-20 00:00:26 +01006562 RTL_W16(IntrMitigate, 0x5151);
6563
6564 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006565 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006566 tp->event_slow |= RxFIFOOver | PCSTimeout;
6567 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006568 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006569
6570 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6571
hayeswang1a964642013-04-01 22:23:41 +00006572 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006573
6574 RTL_R8(IntrMask);
6575
Francois Romieu219a1e92008-06-28 11:58:39 +02006576 switch (tp->mac_version) {
6577 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006578 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006579 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006580
6581 case RTL_GIGA_MAC_VER_12:
6582 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006583 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006584 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006585
6586 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006587 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006588 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006589
6590 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006591 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006592 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006593
6594 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006595 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006596 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006597
Francois Romieu197ff762008-06-28 13:16:02 +02006598 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006599 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006600 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006601
Francois Romieu6fb07052008-06-29 11:54:28 +02006602 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006603 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006604 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006605
Francois Romieuef3386f2008-06-29 12:24:30 +02006606 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006607 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006608 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006609
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006610 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006611 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006612 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006613
Francois Romieu5b538df2008-07-20 16:22:45 +02006614 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006615 case RTL_GIGA_MAC_VER_26:
6616 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006617 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006618 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006619
françois romieue6de30d2011-01-03 15:08:37 +00006620 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006621 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006622 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006623
hayeswang4804b3b2011-03-21 01:50:29 +00006624 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006625 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006626 break;
6627
hayeswang01dc7fe2011-03-21 01:50:28 +00006628 case RTL_GIGA_MAC_VER_32:
6629 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006630 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006631 break;
6632 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006633 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006634 break;
françois romieue6de30d2011-01-03 15:08:37 +00006635
Hayes Wangc2218922011-09-06 16:55:18 +08006636 case RTL_GIGA_MAC_VER_35:
6637 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006638 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006639 break;
6640
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006641 case RTL_GIGA_MAC_VER_38:
6642 rtl_hw_start_8411(tp);
6643 break;
6644
Hayes Wangc5583862012-07-02 17:23:22 +08006645 case RTL_GIGA_MAC_VER_40:
6646 case RTL_GIGA_MAC_VER_41:
6647 rtl_hw_start_8168g_1(tp);
6648 break;
hayeswang57538c42013-04-01 22:23:40 +00006649 case RTL_GIGA_MAC_VER_42:
6650 rtl_hw_start_8168g_2(tp);
6651 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006652
hayeswang45dd95c2013-07-08 17:09:01 +08006653 case RTL_GIGA_MAC_VER_44:
6654 rtl_hw_start_8411_2(tp);
6655 break;
6656
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006657 case RTL_GIGA_MAC_VER_45:
6658 case RTL_GIGA_MAC_VER_46:
6659 rtl_hw_start_8168h_1(tp);
6660 break;
6661
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006662 case RTL_GIGA_MAC_VER_49:
6663 rtl_hw_start_8168ep_1(tp);
6664 break;
6665
6666 case RTL_GIGA_MAC_VER_50:
6667 rtl_hw_start_8168ep_2(tp);
6668 break;
6669
6670 case RTL_GIGA_MAC_VER_51:
6671 rtl_hw_start_8168ep_3(tp);
6672 break;
6673
Francois Romieu219a1e92008-06-28 11:58:39 +02006674 default:
6675 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6676 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006677 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006678 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006679
hayeswang1a964642013-04-01 22:23:41 +00006680 RTL_W8(Cfg9346, Cfg9346_Lock);
6681
Francois Romieu0e485152007-02-20 00:00:26 +01006682 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6683
hayeswang1a964642013-04-01 22:23:41 +00006684 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006685
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006686 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Francois Romieu2857ffb2008-08-02 21:08:49 +02006689#define R810X_CPCMD_QUIRK_MASK (\
6690 EnableBist | \
6691 Mac_dbgo_oe | \
6692 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006693 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006694 Force_txflow_en | \
6695 Cxpl_dbg_sel | \
6696 ASF | \
6697 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006698 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006699
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006700static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006701{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006702 void __iomem *ioaddr = tp->mmio_addr;
6703 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006704 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006705 { 0x01, 0, 0x6e65 },
6706 { 0x02, 0, 0x091f },
6707 { 0x03, 0, 0xc2f9 },
6708 { 0x06, 0, 0xafb5 },
6709 { 0x07, 0, 0x0e00 },
6710 { 0x19, 0, 0xec80 },
6711 { 0x01, 0, 0x2e65 },
6712 { 0x01, 0, 0x6e65 }
6713 };
6714 u8 cfg1;
6715
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006716 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006717
6718 RTL_W8(DBG_REG, FIX_NAK_1);
6719
6720 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6721
6722 RTL_W8(Config1,
6723 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6724 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6725
6726 cfg1 = RTL_R8(Config1);
6727 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6728 RTL_W8(Config1, cfg1 & ~LEDS0);
6729
Francois Romieufdf6fc02012-07-06 22:40:38 +02006730 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006731}
6732
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006733static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006734{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006735 void __iomem *ioaddr = tp->mmio_addr;
6736 struct pci_dev *pdev = tp->pci_dev;
6737
6738 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006739
6740 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6741
6742 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6743 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006744}
6745
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006746static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006747{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006748 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006749
Francois Romieufdf6fc02012-07-06 22:40:38 +02006750 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006751}
6752
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006753static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006754{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006755 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006756 static const struct ephy_info e_info_8105e_1[] = {
6757 { 0x07, 0, 0x4000 },
6758 { 0x19, 0, 0x0200 },
6759 { 0x19, 0, 0x0020 },
6760 { 0x1e, 0, 0x2000 },
6761 { 0x03, 0, 0x0001 },
6762 { 0x19, 0, 0x0100 },
6763 { 0x19, 0, 0x0004 },
6764 { 0x0a, 0, 0x0020 }
6765 };
6766
Francois Romieucecb5fd2011-04-01 10:21:07 +02006767 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006768 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6769
Francois Romieucecb5fd2011-04-01 10:21:07 +02006770 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006771 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6772
6773 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006774 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006775
Francois Romieufdf6fc02012-07-06 22:40:38 +02006776 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006777
6778 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006779}
6780
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006781static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006782{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006783 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006784 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006785}
6786
Hayes Wang7e18dca2012-03-30 14:33:02 +08006787static void rtl_hw_start_8402(struct rtl8169_private *tp)
6788{
6789 void __iomem *ioaddr = tp->mmio_addr;
6790 static const struct ephy_info e_info_8402[] = {
6791 { 0x19, 0xffff, 0xff64 },
6792 { 0x1e, 0, 0x4000 }
6793 };
6794
6795 rtl_csi_access_enable_2(tp);
6796
6797 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6798 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6799
6800 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6801 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6802
Francois Romieufdf6fc02012-07-06 22:40:38 +02006803 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006804
6805 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6806
Francois Romieufdf6fc02012-07-06 22:40:38 +02006807 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6808 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006809 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6810 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006811 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6812 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006813 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006814
6815 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006816}
6817
Hayes Wang5598bfe2012-07-02 17:23:21 +08006818static void rtl_hw_start_8106(struct rtl8169_private *tp)
6819{
6820 void __iomem *ioaddr = tp->mmio_addr;
6821
6822 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6823 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6824
Francois Romieu4521e1a92012-11-01 16:46:28 +00006825 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006826 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6827 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006828
6829 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006830}
6831
Francois Romieu07ce4062007-02-23 23:36:39 +01006832static void rtl_hw_start_8101(struct net_device *dev)
6833{
Francois Romieucdf1a602007-06-11 23:29:50 +02006834 struct rtl8169_private *tp = netdev_priv(dev);
6835 void __iomem *ioaddr = tp->mmio_addr;
6836 struct pci_dev *pdev = tp->pci_dev;
6837
Francois Romieuda78dbf2012-01-26 14:18:23 +01006838 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6839 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006840
Francois Romieucecb5fd2011-04-01 10:21:07 +02006841 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006842 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006843 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6844 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006845
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006846 RTL_W8(Cfg9346, Cfg9346_Unlock);
6847
hayeswang1a964642013-04-01 22:23:41 +00006848 RTL_W8(MaxTxPacketSize, TxPacketMax);
6849
6850 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6851
6852 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6853 RTL_W16(CPlusCmd, tp->cp_cmd);
6854
6855 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6856
6857 rtl_set_rx_tx_config_registers(tp);
6858
Francois Romieu2857ffb2008-08-02 21:08:49 +02006859 switch (tp->mac_version) {
6860 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006861 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006862 break;
6863
6864 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006865 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006866 break;
6867
6868 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006869 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006870 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006871
6872 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006873 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006874 break;
6875 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006876 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006877 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006878
6879 case RTL_GIGA_MAC_VER_37:
6880 rtl_hw_start_8402(tp);
6881 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006882
6883 case RTL_GIGA_MAC_VER_39:
6884 rtl_hw_start_8106(tp);
6885 break;
hayeswang58152cd2013-04-01 22:23:42 +00006886 case RTL_GIGA_MAC_VER_43:
6887 rtl_hw_start_8168g_2(tp);
6888 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006889 case RTL_GIGA_MAC_VER_47:
6890 case RTL_GIGA_MAC_VER_48:
6891 rtl_hw_start_8168h_1(tp);
6892 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006893 }
6894
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006895 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006896
Francois Romieucdf1a602007-06-11 23:29:50 +02006897 RTL_W16(IntrMitigate, 0x0000);
6898
Francois Romieucdf1a602007-06-11 23:29:50 +02006899 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006900
Francois Romieucdf1a602007-06-11 23:29:50 +02006901 rtl_set_rx_mode(dev);
6902
hayeswang1a964642013-04-01 22:23:41 +00006903 RTL_R8(IntrMask);
6904
Francois Romieucdf1a602007-06-11 23:29:50 +02006905 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906}
6907
6908static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6909{
Francois Romieud58d46b2011-05-03 16:38:29 +02006910 struct rtl8169_private *tp = netdev_priv(dev);
6911
Francois Romieud58d46b2011-05-03 16:38:29 +02006912 if (new_mtu > ETH_DATA_LEN)
6913 rtl_hw_jumbo_enable(tp);
6914 else
6915 rtl_hw_jumbo_disable(tp);
6916
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006918 netdev_update_features(dev);
6919
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921}
6922
6923static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6924{
Al Viro95e09182007-12-22 18:55:39 +00006925 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6927}
6928
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006929static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6930 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006932 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006933 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006934
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006935 kfree(*data_buff);
6936 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 rtl8169_make_unusable_by_asic(desc);
6938}
6939
6940static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6941{
6942 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6943
Alexander Duycka0750132014-12-11 15:02:17 -08006944 /* Force memory writes to complete before releasing descriptor */
6945 dma_wmb();
6946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6948}
6949
6950static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6951 u32 rx_buf_sz)
6952{
6953 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 rtl8169_mark_to_asic(desc, rx_buf_sz);
6955}
6956
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006957static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006959 return (void *)ALIGN((long)data, 16);
6960}
6961
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006962static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6963 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006964{
6965 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006967 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006968 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006969 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006971 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6972 if (!data)
6973 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006974
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006975 if (rtl8169_align(data) != data) {
6976 kfree(data);
6977 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6978 if (!data)
6979 return NULL;
6980 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006981
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006982 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006983 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006984 if (unlikely(dma_mapping_error(d, mapping))) {
6985 if (net_ratelimit())
6986 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006987 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
6990 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006991 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006992
6993err_out:
6994 kfree(data);
6995 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996}
6997
6998static void rtl8169_rx_clear(struct rtl8169_private *tp)
6999{
Francois Romieu07d3f512007-02-21 22:40:46 +01007000 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
7002 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007003 if (tp->Rx_databuff[i]) {
7004 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 tp->RxDescArray + i);
7006 }
7007 }
7008}
7009
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007010static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007012 desc->opts1 |= cpu_to_le32(RingEnd);
7013}
Francois Romieu5b0384f2006-08-16 16:00:01 +02007014
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007015static int rtl8169_rx_fill(struct rtl8169_private *tp)
7016{
7017 unsigned int i;
7018
7019 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007020 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02007021
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007022 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02007024
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007025 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007026 if (!data) {
7027 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007028 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007029 }
7030 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007033 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7034 return 0;
7035
7036err_out:
7037 rtl8169_rx_clear(tp);
7038 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039}
7040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041static int rtl8169_init_ring(struct net_device *dev)
7042{
7043 struct rtl8169_private *tp = netdev_priv(dev);
7044
7045 rtl8169_init_ring_indexes(tp);
7046
7047 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007048 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007050 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051}
7052
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007053static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 struct TxDesc *desc)
7055{
7056 unsigned int len = tx_skb->len;
7057
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007058 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7059
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 desc->opts1 = 0x00;
7061 desc->opts2 = 0x00;
7062 desc->addr = 0x00;
7063 tx_skb->len = 0;
7064}
7065
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007066static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7067 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068{
7069 unsigned int i;
7070
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007071 for (i = 0; i < n; i++) {
7072 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 struct ring_info *tx_skb = tp->tx_skb + entry;
7074 unsigned int len = tx_skb->len;
7075
7076 if (len) {
7077 struct sk_buff *skb = tx_skb->skb;
7078
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007079 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 tp->TxDescArray + entry);
7081 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007082 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 tx_skb->skb = NULL;
7084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 }
7086 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007087}
7088
7089static void rtl8169_tx_clear(struct rtl8169_private *tp)
7090{
7091 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 tp->cur_tx = tp->dirty_tx = 0;
7093}
7094
Francois Romieu4422bcd2012-01-26 11:23:32 +01007095static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096{
David Howellsc4028952006-11-22 14:57:56 +00007097 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01007098 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Francois Romieuda78dbf2012-01-26 14:18:23 +01007100 napi_disable(&tp->napi);
7101 netif_stop_queue(dev);
7102 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
françois romieuc7c2c392011-12-04 20:30:52 +00007104 rtl8169_hw_reset(tp);
7105
Francois Romieu56de4142011-03-15 17:29:31 +01007106 for (i = 0; i < NUM_RX_DESC; i++)
7107 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7108
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00007110 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Francois Romieuda78dbf2012-01-26 14:18:23 +01007112 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01007113 rtl_hw_start(dev);
7114 netif_wake_queue(dev);
7115 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116}
7117
7118static void rtl8169_tx_timeout(struct net_device *dev)
7119{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007120 struct rtl8169_private *tp = netdev_priv(dev);
7121
7122 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123}
7124
7125static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07007126 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127{
7128 struct skb_shared_info *info = skb_shinfo(skb);
7129 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007130 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007131 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133 entry = tp->cur_tx;
7134 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007135 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 dma_addr_t mapping;
7137 u32 status, len;
7138 void *addr;
7139
7140 entry = (entry + 1) % NUM_TX_DESC;
7141
7142 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00007143 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00007144 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007145 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007146 if (unlikely(dma_mapping_error(d, mapping))) {
7147 if (net_ratelimit())
7148 netif_err(tp, drv, tp->dev,
7149 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007150 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Francois Romieucecb5fd2011-04-01 10:21:07 +02007153 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007154 status = opts[0] | len |
7155 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
7157 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07007158 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 txd->addr = cpu_to_le64(mapping);
7160
7161 tp->tx_skb[entry].len = len;
7162 }
7163
7164 if (cur_frag) {
7165 tp->tx_skb[entry].skb = skb;
7166 txd->opts1 |= cpu_to_le32(LastFrag);
7167 }
7168
7169 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007170
7171err_out:
7172 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7173 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174}
7175
françois romieub423e9a2013-05-18 01:24:46 +00007176static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7177{
7178 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7179}
7180
hayeswange9746042014-07-11 16:25:58 +08007181static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7182 struct net_device *dev);
7183/* r8169_csum_workaround()
7184 * The hw limites the value the transport offset. When the offset is out of the
7185 * range, calculate the checksum by sw.
7186 */
7187static void r8169_csum_workaround(struct rtl8169_private *tp,
7188 struct sk_buff *skb)
7189{
7190 if (skb_shinfo(skb)->gso_size) {
7191 netdev_features_t features = tp->dev->features;
7192 struct sk_buff *segs, *nskb;
7193
7194 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7195 segs = skb_gso_segment(skb, features);
7196 if (IS_ERR(segs) || !segs)
7197 goto drop;
7198
7199 do {
7200 nskb = segs;
7201 segs = segs->next;
7202 nskb->next = NULL;
7203 rtl8169_start_xmit(nskb, tp->dev);
7204 } while (segs);
7205
Alexander Duyckeb781392015-05-01 10:34:44 -07007206 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007207 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7208 if (skb_checksum_help(skb) < 0)
7209 goto drop;
7210
7211 rtl8169_start_xmit(skb, tp->dev);
7212 } else {
7213 struct net_device_stats *stats;
7214
7215drop:
7216 stats = &tp->dev->stats;
7217 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007218 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007219 }
7220}
7221
7222/* msdn_giant_send_check()
7223 * According to the document of microsoft, the TCP Pseudo Header excludes the
7224 * packet length for IPv6 TCP large packets.
7225 */
7226static int msdn_giant_send_check(struct sk_buff *skb)
7227{
7228 const struct ipv6hdr *ipv6h;
7229 struct tcphdr *th;
7230 int ret;
7231
7232 ret = skb_cow_head(skb, 0);
7233 if (ret)
7234 return ret;
7235
7236 ipv6h = ipv6_hdr(skb);
7237 th = tcp_hdr(skb);
7238
7239 th->check = 0;
7240 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7241
7242 return ret;
7243}
7244
7245static inline __be16 get_protocol(struct sk_buff *skb)
7246{
7247 __be16 protocol;
7248
7249 if (skb->protocol == htons(ETH_P_8021Q))
7250 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7251 else
7252 protocol = skb->protocol;
7253
7254 return protocol;
7255}
7256
hayeswang5888d3f2014-07-11 16:25:56 +08007257static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7258 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259{
Michał Mirosław350fb322011-04-08 06:35:56 +00007260 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
Francois Romieu2b7b4312011-04-18 22:53:24 -07007262 if (mss) {
7263 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007264 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7265 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7266 const struct iphdr *ip = ip_hdr(skb);
7267
7268 if (ip->protocol == IPPROTO_TCP)
7269 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7270 else if (ip->protocol == IPPROTO_UDP)
7271 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7272 else
7273 WARN_ON_ONCE(1);
7274 }
7275
7276 return true;
7277}
7278
7279static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7280 struct sk_buff *skb, u32 *opts)
7281{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007282 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007283 u32 mss = skb_shinfo(skb)->gso_size;
7284
7285 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007286 if (transport_offset > GTTCPHO_MAX) {
7287 netif_warn(tp, tx_err, tp->dev,
7288 "Invalid transport offset 0x%x for TSO\n",
7289 transport_offset);
7290 return false;
7291 }
7292
7293 switch (get_protocol(skb)) {
7294 case htons(ETH_P_IP):
7295 opts[0] |= TD1_GTSENV4;
7296 break;
7297
7298 case htons(ETH_P_IPV6):
7299 if (msdn_giant_send_check(skb))
7300 return false;
7301
7302 opts[0] |= TD1_GTSENV6;
7303 break;
7304
7305 default:
7306 WARN_ON_ONCE(1);
7307 break;
7308 }
7309
hayeswangbdfa4ed2014-07-11 16:25:57 +08007310 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007311 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007312 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007313 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
françois romieub423e9a2013-05-18 01:24:46 +00007315 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007316 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007317
hayeswange9746042014-07-11 16:25:58 +08007318 if (transport_offset > TCPHO_MAX) {
7319 netif_warn(tp, tx_err, tp->dev,
7320 "Invalid transport offset 0x%x\n",
7321 transport_offset);
7322 return false;
7323 }
7324
7325 switch (get_protocol(skb)) {
7326 case htons(ETH_P_IP):
7327 opts[1] |= TD1_IPv4_CS;
7328 ip_protocol = ip_hdr(skb)->protocol;
7329 break;
7330
7331 case htons(ETH_P_IPV6):
7332 opts[1] |= TD1_IPv6_CS;
7333 ip_protocol = ipv6_hdr(skb)->nexthdr;
7334 break;
7335
7336 default:
7337 ip_protocol = IPPROTO_RAW;
7338 break;
7339 }
7340
7341 if (ip_protocol == IPPROTO_TCP)
7342 opts[1] |= TD1_TCP_CS;
7343 else if (ip_protocol == IPPROTO_UDP)
7344 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007345 else
7346 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007347
7348 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007349 } else {
7350 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007351 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 }
hayeswang5888d3f2014-07-11 16:25:56 +08007353
françois romieub423e9a2013-05-18 01:24:46 +00007354 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355}
7356
Stephen Hemminger613573252009-08-31 19:50:58 +00007357static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7358 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359{
7360 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007361 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 struct TxDesc *txd = tp->TxDescArray + entry;
7363 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007364 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 dma_addr_t mapping;
7366 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007367 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007368 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007369
Julien Ducourthial477206a2012-05-09 00:00:06 +02007370 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007371 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007372 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 }
7374
7375 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007376 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
françois romieub423e9a2013-05-18 01:24:46 +00007378 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7379 opts[0] = DescOwn;
7380
hayeswange9746042014-07-11 16:25:58 +08007381 if (!tp->tso_csum(tp, skb, opts)) {
7382 r8169_csum_workaround(tp, skb);
7383 return NETDEV_TX_OK;
7384 }
françois romieub423e9a2013-05-18 01:24:46 +00007385
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007386 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007387 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007388 if (unlikely(dma_mapping_error(d, mapping))) {
7389 if (net_ratelimit())
7390 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007391 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
7394 tp->tx_skb[entry].len = len;
7395 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Francois Romieu2b7b4312011-04-18 22:53:24 -07007397 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007398 if (frags < 0)
7399 goto err_dma_1;
7400 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007401 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007402 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007403 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007404 tp->tx_skb[entry].skb = skb;
7405 }
7406
Francois Romieu2b7b4312011-04-18 22:53:24 -07007407 txd->opts2 = cpu_to_le32(opts[1]);
7408
Richard Cochran5047fb52012-03-10 07:29:42 +00007409 skb_tx_timestamp(skb);
7410
Alexander Duycka0750132014-12-11 15:02:17 -08007411 /* Force memory writes to complete before releasing descriptor */
7412 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Francois Romieucecb5fd2011-04-01 10:21:07 +02007414 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007415 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 txd->opts1 = cpu_to_le32(status);
7417
Alexander Duycka0750132014-12-11 15:02:17 -08007418 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007419 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420
Alexander Duycka0750132014-12-11 15:02:17 -08007421 tp->cur_tx += frags + 1;
7422
David S. Miller87cda7c2015-02-22 15:54:29 -05007423 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424
David S. Miller87cda7c2015-02-22 15:54:29 -05007425 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007426
David S. Miller87cda7c2015-02-22 15:54:29 -05007427 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007428 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7429 * not miss a ring update when it notices a stopped queue.
7430 */
7431 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007433 /* Sync with rtl_tx:
7434 * - publish queue status and cur_tx ring index (write barrier)
7435 * - refresh dirty_tx ring index (read barrier).
7436 * May the current thread have a pessimistic view of the ring
7437 * status and forget to wake up queue, a racing rtl_tx thread
7438 * can't.
7439 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007440 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007441 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 netif_wake_queue(dev);
7443 }
7444
Stephen Hemminger613573252009-08-31 19:50:58 +00007445 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007447err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007448 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007449err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007450 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007451 dev->stats.tx_dropped++;
7452 return NETDEV_TX_OK;
7453
7454err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007456 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007457 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458}
7459
7460static void rtl8169_pcierr_interrupt(struct net_device *dev)
7461{
7462 struct rtl8169_private *tp = netdev_priv(dev);
7463 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 u16 pci_status, pci_cmd;
7465
7466 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7467 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7468
Joe Perchesbf82c182010-02-09 11:49:50 +00007469 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7470 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471
7472 /*
7473 * The recovery sequence below admits a very elaborated explanation:
7474 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007475 * - I did not see what else could be done;
7476 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 *
7478 * Feel free to adjust to your needs.
7479 */
Francois Romieua27993f2006-12-18 00:04:19 +01007480 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007481 pci_cmd &= ~PCI_COMMAND_PARITY;
7482 else
7483 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7484
7485 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486
7487 pci_write_config_word(pdev, PCI_STATUS,
7488 pci_status & (PCI_STATUS_DETECTED_PARITY |
7489 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7490 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7491
7492 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007493 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007494 void __iomem *ioaddr = tp->mmio_addr;
7495
Joe Perchesbf82c182010-02-09 11:49:50 +00007496 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 tp->cp_cmd &= ~PCIDAC;
7498 RTL_W16(CPlusCmd, tp->cp_cmd);
7499 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 }
7501
françois romieue6de30d2011-01-03 15:08:37 +00007502 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007503
Francois Romieu98ddf982012-01-31 10:47:34 +01007504 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505}
7506
Francois Romieuda78dbf2012-01-26 14:18:23 +01007507static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
7509 unsigned int dirty_tx, tx_left;
7510
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 dirty_tx = tp->dirty_tx;
7512 smp_rmb();
7513 tx_left = tp->cur_tx - dirty_tx;
7514
7515 while (tx_left > 0) {
7516 unsigned int entry = dirty_tx % NUM_TX_DESC;
7517 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 u32 status;
7519
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7521 if (status & DescOwn)
7522 break;
7523
Alexander Duycka0750132014-12-11 15:02:17 -08007524 /* This barrier is needed to keep us from reading
7525 * any other fields out of the Tx descriptor until
7526 * we know the status of DescOwn
7527 */
7528 dma_rmb();
7529
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007530 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7531 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007533 u64_stats_update_begin(&tp->tx_stats.syncp);
7534 tp->tx_stats.packets++;
7535 tp->tx_stats.bytes += tx_skb->skb->len;
7536 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007537 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 tx_skb->skb = NULL;
7539 }
7540 dirty_tx++;
7541 tx_left--;
7542 }
7543
7544 if (tp->dirty_tx != dirty_tx) {
7545 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007546 /* Sync with rtl8169_start_xmit:
7547 * - publish dirty_tx ring index (write barrier)
7548 * - refresh cur_tx ring index and queue status (read barrier)
7549 * May the current thread miss the stopped queue condition,
7550 * a racing xmit thread can only have a right view of the
7551 * ring status.
7552 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007553 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007555 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 netif_wake_queue(dev);
7557 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007558 /*
7559 * 8168 hack: TxPoll requests are lost when the Tx packets are
7560 * too close. Let's kick an extra TxPoll request when a burst
7561 * of start_xmit activity is detected (if it is not detected,
7562 * it is slow enough). -- FR
7563 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007564 if (tp->cur_tx != dirty_tx) {
7565 void __iomem *ioaddr = tp->mmio_addr;
7566
Francois Romieud78ae2d2007-08-26 20:08:19 +02007567 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 }
7570}
7571
Francois Romieu126fa4b2005-05-12 20:09:17 -04007572static inline int rtl8169_fragmented_frame(u32 status)
7573{
7574 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7575}
7576
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007577static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 u32 status = opts1 & RxProtoMask;
7580
7581 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007582 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 skb->ip_summed = CHECKSUM_UNNECESSARY;
7584 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007585 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586}
7587
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007588static struct sk_buff *rtl8169_try_rx_copy(void *data,
7589 struct rtl8169_private *tp,
7590 int pkt_size,
7591 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007593 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007594 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007596 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007597 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007598 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007599 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007600 if (skb)
7601 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007602 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7603
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007604 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605}
7606
Francois Romieuda78dbf2012-01-26 14:18:23 +01007607static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608{
7609 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007610 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
Timo Teräs9fba0812013-01-15 21:01:24 +00007614 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007616 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 u32 status;
7618
David S. Miller8decf862011-09-22 03:23:13 -04007619 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 if (status & DescOwn)
7621 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007622
7623 /* This barrier is needed to keep us from reading
7624 * any other fields out of the Rx descriptor until
7625 * we know the status of DescOwn
7626 */
7627 dma_rmb();
7628
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007629 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007630 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7631 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007632 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007634 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007636 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007637 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007638 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007639 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007640 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007641 if ((status & (RxRUNT | RxCRC)) &&
7642 !(status & (RxRWT | RxFOVF)) &&
7643 (dev->features & NETIF_F_RXALL))
7644 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007646 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007647 dma_addr_t addr;
7648 int pkt_size;
7649
7650process_pkt:
7651 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007652 if (likely(!(dev->features & NETIF_F_RXFCS)))
7653 pkt_size = (status & 0x00003fff) - 4;
7654 else
7655 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
Francois Romieu126fa4b2005-05-12 20:09:17 -04007657 /*
7658 * The driver does not support incoming fragmented
7659 * frames. They are seen as a symptom of over-mtu
7660 * sized frames.
7661 */
7662 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007663 dev->stats.rx_dropped++;
7664 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007665 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007666 }
7667
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007668 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7669 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007670 if (!skb) {
7671 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007672 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 }
7674
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007675 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 skb_put(skb, pkt_size);
7677 skb->protocol = eth_type_trans(skb, dev);
7678
Francois Romieu7a8fc772011-03-01 17:18:33 +01007679 rtl8169_rx_vlan_tag(desc, skb);
7680
françois romieu39174292015-11-11 23:35:18 +01007681 if (skb->pkt_type == PACKET_MULTICAST)
7682 dev->stats.multicast++;
7683
Francois Romieu56de4142011-03-15 17:29:31 +01007684 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685
Junchang Wang8027aa22012-03-04 23:30:32 +01007686 u64_stats_update_begin(&tp->rx_stats.syncp);
7687 tp->rx_stats.packets++;
7688 tp->rx_stats.bytes += pkt_size;
7689 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 }
françois romieuce11ff52013-01-24 13:30:06 +00007691release_descriptor:
7692 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007693 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 }
7695
7696 count = cur_rx - tp->cur_rx;
7697 tp->cur_rx = cur_rx;
7698
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 return count;
7700}
7701
Francois Romieu07d3f512007-02-21 22:40:46 +01007702static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703{
Francois Romieu07d3f512007-02-21 22:40:46 +01007704 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007707 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007709 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007710 if (status && status != 0xffff) {
7711 status &= RTL_EVENT_NAPI | tp->event_slow;
7712 if (status) {
7713 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007714
Francois Romieuda78dbf2012-01-26 14:18:23 +01007715 rtl_irq_disable(tp);
7716 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007719 return IRQ_RETVAL(handled);
7720}
7721
Francois Romieuda78dbf2012-01-26 14:18:23 +01007722/*
7723 * Workqueue context.
7724 */
7725static void rtl_slow_event_work(struct rtl8169_private *tp)
7726{
7727 struct net_device *dev = tp->dev;
7728 u16 status;
7729
7730 status = rtl_get_events(tp) & tp->event_slow;
7731 rtl_ack_events(tp, status);
7732
7733 if (unlikely(status & RxFIFOOver)) {
7734 switch (tp->mac_version) {
7735 /* Work around for rx fifo overflow */
7736 case RTL_GIGA_MAC_VER_11:
7737 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007738 /* XXX - Hack alert. See rtl_task(). */
7739 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007740 default:
7741 break;
7742 }
7743 }
7744
7745 if (unlikely(status & SYSErr))
7746 rtl8169_pcierr_interrupt(dev);
7747
7748 if (status & LinkChg)
7749 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7750
françois romieu7dbb4912012-06-09 10:53:16 +00007751 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007752}
7753
Francois Romieu4422bcd2012-01-26 11:23:32 +01007754static void rtl_task(struct work_struct *work)
7755{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007756 static const struct {
7757 int bitnr;
7758 void (*action)(struct rtl8169_private *);
7759 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007760 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007761 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7762 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7763 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7764 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007765 struct rtl8169_private *tp =
7766 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007767 struct net_device *dev = tp->dev;
7768 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007769
Francois Romieuda78dbf2012-01-26 14:18:23 +01007770 rtl_lock_work(tp);
7771
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007772 if (!netif_running(dev) ||
7773 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007774 goto out_unlock;
7775
7776 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7777 bool pending;
7778
Francois Romieuda78dbf2012-01-26 14:18:23 +01007779 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007780 if (pending)
7781 rtl_work[i].action(tp);
7782 }
7783
7784out_unlock:
7785 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007786}
7787
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007788static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007790 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7791 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007792 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7793 int work_done= 0;
7794 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795
Francois Romieuda78dbf2012-01-26 14:18:23 +01007796 status = rtl_get_events(tp);
7797 rtl_ack_events(tp, status & ~tp->event_slow);
7798
7799 if (status & RTL_EVENT_NAPI_RX)
7800 work_done = rtl_rx(dev, tp, (u32) budget);
7801
7802 if (status & RTL_EVENT_NAPI_TX)
7803 rtl_tx(dev, tp);
7804
7805 if (status & tp->event_slow) {
7806 enable_mask &= ~tp->event_slow;
7807
7808 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007811 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007812 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007813
Francois Romieuda78dbf2012-01-26 14:18:23 +01007814 rtl_irq_enable(tp, enable_mask);
7815 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 }
7817
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007818 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
Francois Romieu523a6092008-09-10 22:28:56 +02007821static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7822{
7823 struct rtl8169_private *tp = netdev_priv(dev);
7824
7825 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7826 return;
7827
7828 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7829 RTL_W32(RxMissed, 0);
7830}
7831
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832static void rtl8169_down(struct net_device *dev)
7833{
7834 struct rtl8169_private *tp = netdev_priv(dev);
7835 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
Francois Romieu4876cc12011-03-11 21:07:11 +01007837 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007839 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007840 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841
Hayes Wang92fc43b2011-07-06 15:58:03 +08007842 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007843 /*
7844 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007845 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7846 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007847 */
Francois Romieu523a6092008-09-10 22:28:56 +02007848 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007851 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 rtl8169_tx_clear(tp);
7854
7855 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007856
7857 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858}
7859
7860static int rtl8169_close(struct net_device *dev)
7861{
7862 struct rtl8169_private *tp = netdev_priv(dev);
7863 struct pci_dev *pdev = tp->pci_dev;
7864
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007865 pm_runtime_get_sync(&pdev->dev);
7866
Francois Romieucecb5fd2011-04-01 10:21:07 +02007867 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007868 rtl8169_update_counters(dev);
7869
Francois Romieuda78dbf2012-01-26 14:18:23 +01007870 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007871 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007872
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007874 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Lekensteyn4ea72442013-07-22 09:53:30 +02007876 cancel_work_sync(&tp->wk.work);
7877
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007878 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007880 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7881 tp->RxPhyAddr);
7882 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7883 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 tp->TxDescArray = NULL;
7885 tp->RxDescArray = NULL;
7886
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007887 pm_runtime_put_sync(&pdev->dev);
7888
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 return 0;
7890}
7891
Francois Romieudc1c00c2012-03-08 10:06:18 +01007892#ifdef CONFIG_NET_POLL_CONTROLLER
7893static void rtl8169_netpoll(struct net_device *dev)
7894{
7895 struct rtl8169_private *tp = netdev_priv(dev);
7896
7897 rtl8169_interrupt(tp->pci_dev->irq, dev);
7898}
7899#endif
7900
Francois Romieudf43ac72012-03-08 09:48:40 +01007901static int rtl_open(struct net_device *dev)
7902{
7903 struct rtl8169_private *tp = netdev_priv(dev);
7904 void __iomem *ioaddr = tp->mmio_addr;
7905 struct pci_dev *pdev = tp->pci_dev;
7906 int retval = -ENOMEM;
7907
7908 pm_runtime_get_sync(&pdev->dev);
7909
7910 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007911 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007912 * dma_alloc_coherent provides more.
7913 */
7914 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7915 &tp->TxPhyAddr, GFP_KERNEL);
7916 if (!tp->TxDescArray)
7917 goto err_pm_runtime_put;
7918
7919 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7920 &tp->RxPhyAddr, GFP_KERNEL);
7921 if (!tp->RxDescArray)
7922 goto err_free_tx_0;
7923
7924 retval = rtl8169_init_ring(dev);
7925 if (retval < 0)
7926 goto err_free_rx_1;
7927
7928 INIT_WORK(&tp->wk.work, rtl_task);
7929
7930 smp_mb();
7931
7932 rtl_request_firmware(tp);
7933
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007934 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007935 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7936 dev->name, dev);
7937 if (retval < 0)
7938 goto err_release_fw_2;
7939
7940 rtl_lock_work(tp);
7941
7942 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7943
7944 napi_enable(&tp->napi);
7945
7946 rtl8169_init_phy(dev, tp);
7947
7948 __rtl8169_set_features(dev, dev->features);
7949
7950 rtl_pll_power_up(tp);
7951
7952 rtl_hw_start(dev);
7953
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007954 if (!rtl8169_init_counter_offsets(dev))
7955 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7956
Francois Romieudf43ac72012-03-08 09:48:40 +01007957 netif_start_queue(dev);
7958
7959 rtl_unlock_work(tp);
7960
7961 tp->saved_wolopts = 0;
7962 pm_runtime_put_noidle(&pdev->dev);
7963
7964 rtl8169_check_link_status(dev, tp, ioaddr);
7965out:
7966 return retval;
7967
7968err_release_fw_2:
7969 rtl_release_firmware(tp);
7970 rtl8169_rx_clear(tp);
7971err_free_rx_1:
7972 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7973 tp->RxPhyAddr);
7974 tp->RxDescArray = NULL;
7975err_free_tx_0:
7976 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7977 tp->TxPhyAddr);
7978 tp->TxDescArray = NULL;
7979err_pm_runtime_put:
7980 pm_runtime_put_noidle(&pdev->dev);
7981 goto out;
7982}
7983
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007984static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007985rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007986{
7987 struct rtl8169_private *tp = netdev_priv(dev);
7988 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007989 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007990 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007991 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007993 pm_runtime_get_noresume(&pdev->dev);
7994
7995 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007996 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007997
Junchang Wang8027aa22012-03-04 23:30:32 +01007998 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007999 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01008000 stats->rx_packets = tp->rx_stats.packets;
8001 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07008002 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01008003
Junchang Wang8027aa22012-03-04 23:30:32 +01008004 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07008005 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01008006 stats->tx_packets = tp->tx_stats.packets;
8007 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07008008 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01008009
8010 stats->rx_dropped = dev->stats.rx_dropped;
8011 stats->tx_dropped = dev->stats.tx_dropped;
8012 stats->rx_length_errors = dev->stats.rx_length_errors;
8013 stats->rx_errors = dev->stats.rx_errors;
8014 stats->rx_crc_errors = dev->stats.rx_crc_errors;
8015 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
8016 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02008017 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01008018
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008019 /*
8020 * Fetch additonal counter values missing in stats collected by driver
8021 * from tally counters.
8022 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008023 if (pm_runtime_active(&pdev->dev))
8024 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008025
8026 /*
8027 * Subtract values fetched during initalization.
8028 * See rtl8169_init_counter_offsets for a description why we do that.
8029 */
Corinna Vinschen42020322015-09-10 10:47:35 +02008030 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008031 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02008032 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008033 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02008034 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008035 le16_to_cpu(tp->tc_offset.tx_aborted);
8036
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008037 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038}
8039
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008040static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01008041{
françois romieu065c27c2011-01-03 15:08:12 +00008042 struct rtl8169_private *tp = netdev_priv(dev);
8043
Francois Romieu5d06a992006-02-23 00:47:58 +01008044 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008045 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01008046
8047 netif_device_detach(dev);
8048 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008049
8050 rtl_lock_work(tp);
8051 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008052 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008053 rtl_unlock_work(tp);
8054
8055 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008056}
Francois Romieu5d06a992006-02-23 00:47:58 +01008057
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008058#ifdef CONFIG_PM
8059
8060static int rtl8169_suspend(struct device *device)
8061{
8062 struct pci_dev *pdev = to_pci_dev(device);
8063 struct net_device *dev = pci_get_drvdata(pdev);
8064
8065 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02008066
Francois Romieu5d06a992006-02-23 00:47:58 +01008067 return 0;
8068}
8069
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008070static void __rtl8169_resume(struct net_device *dev)
8071{
françois romieu065c27c2011-01-03 15:08:12 +00008072 struct rtl8169_private *tp = netdev_priv(dev);
8073
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008074 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00008075
8076 rtl_pll_power_up(tp);
8077
Artem Savkovcff4c162012-04-03 10:29:11 +00008078 rtl_lock_work(tp);
8079 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008080 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00008081 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008082
Francois Romieu98ddf982012-01-31 10:47:34 +01008083 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008084}
8085
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008086static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01008087{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008088 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01008089 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008090 struct rtl8169_private *tp = netdev_priv(dev);
8091
8092 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01008093
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008094 if (netif_running(dev))
8095 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01008096
Francois Romieu5d06a992006-02-23 00:47:58 +01008097 return 0;
8098}
8099
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008100static int rtl8169_runtime_suspend(struct device *device)
8101{
8102 struct pci_dev *pdev = to_pci_dev(device);
8103 struct net_device *dev = pci_get_drvdata(pdev);
8104 struct rtl8169_private *tp = netdev_priv(dev);
8105
8106 if (!tp->TxDescArray)
8107 return 0;
8108
Francois Romieuda78dbf2012-01-26 14:18:23 +01008109 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008110 tp->saved_wolopts = __rtl8169_get_wol(tp);
8111 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008112 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008113
8114 rtl8169_net_suspend(dev);
8115
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008116 /* Update counters before going runtime suspend */
8117 rtl8169_rx_missed(dev, tp->mmio_addr);
8118 rtl8169_update_counters(dev);
8119
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008120 return 0;
8121}
8122
8123static int rtl8169_runtime_resume(struct device *device)
8124{
8125 struct pci_dev *pdev = to_pci_dev(device);
8126 struct net_device *dev = pci_get_drvdata(pdev);
8127 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08008128 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008129
8130 if (!tp->TxDescArray)
8131 return 0;
8132
Francois Romieuda78dbf2012-01-26 14:18:23 +01008133 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008134 __rtl8169_set_wol(tp, tp->saved_wolopts);
8135 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01008136 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008137
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008138 rtl8169_init_phy(dev, tp);
8139
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008140 __rtl8169_resume(dev);
8141
8142 return 0;
8143}
8144
8145static int rtl8169_runtime_idle(struct device *device)
8146{
8147 struct pci_dev *pdev = to_pci_dev(device);
8148 struct net_device *dev = pci_get_drvdata(pdev);
8149 struct rtl8169_private *tp = netdev_priv(dev);
8150
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00008151 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008152}
8153
Alexey Dobriyan47145212009-12-14 18:00:08 -08008154static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02008155 .suspend = rtl8169_suspend,
8156 .resume = rtl8169_resume,
8157 .freeze = rtl8169_suspend,
8158 .thaw = rtl8169_resume,
8159 .poweroff = rtl8169_suspend,
8160 .restore = rtl8169_resume,
8161 .runtime_suspend = rtl8169_runtime_suspend,
8162 .runtime_resume = rtl8169_runtime_resume,
8163 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008164};
8165
8166#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8167
8168#else /* !CONFIG_PM */
8169
8170#define RTL8169_PM_OPS NULL
8171
8172#endif /* !CONFIG_PM */
8173
David S. Miller1805b2f2011-10-24 18:18:09 -04008174static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8175{
8176 void __iomem *ioaddr = tp->mmio_addr;
8177
8178 /* WoL fails with 8168b when the receiver is disabled. */
8179 switch (tp->mac_version) {
8180 case RTL_GIGA_MAC_VER_11:
8181 case RTL_GIGA_MAC_VER_12:
8182 case RTL_GIGA_MAC_VER_17:
8183 pci_clear_master(tp->pci_dev);
8184
8185 RTL_W8(ChipCmd, CmdRxEnb);
8186 /* PCI commit */
8187 RTL_R8(ChipCmd);
8188 break;
8189 default:
8190 break;
8191 }
8192}
8193
Francois Romieu1765f952008-09-13 17:21:40 +02008194static void rtl_shutdown(struct pci_dev *pdev)
8195{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008196 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00008197 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00008198 struct device *d = &pdev->dev;
8199
8200 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02008201
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008202 rtl8169_net_suspend(dev);
8203
Francois Romieucecb5fd2011-04-01 10:21:07 +02008204 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08008205 rtl_rar_set(tp, dev->perm_addr);
8206
Hayes Wang92fc43b2011-07-06 15:58:03 +08008207 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00008208
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008209 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04008210 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8211 rtl_wol_suspend_quirk(tp);
8212 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00008213 }
8214
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008215 pci_wake_from_d3(pdev, true);
8216 pci_set_power_state(pdev, PCI_D3hot);
8217 }
françois romieu2a15cd22012-03-06 01:14:12 +00008218
8219 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008220}
Francois Romieu5d06a992006-02-23 00:47:58 +01008221
Bill Pembertonbaf63292012-12-03 09:23:28 -05008222static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008223{
8224 struct net_device *dev = pci_get_drvdata(pdev);
8225 struct rtl8169_private *tp = netdev_priv(dev);
8226
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008227 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8228 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008229 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8230 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8231 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8232 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008233 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01008234 rtl8168_driver_stop(tp);
8235 }
8236
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008237 netif_napi_del(&tp->napi);
8238
Francois Romieue27566e2012-03-08 09:54:01 +01008239 unregister_netdev(dev);
8240
8241 rtl_release_firmware(tp);
8242
8243 if (pci_dev_run_wake(pdev))
8244 pm_runtime_get_noresume(&pdev->dev);
8245
8246 /* restore original MAC address */
8247 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008248}
8249
Francois Romieufa9c3852012-03-08 10:01:50 +01008250static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008251 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008252 .ndo_stop = rtl8169_close,
8253 .ndo_get_stats64 = rtl8169_get_stats64,
8254 .ndo_start_xmit = rtl8169_start_xmit,
8255 .ndo_tx_timeout = rtl8169_tx_timeout,
8256 .ndo_validate_addr = eth_validate_addr,
8257 .ndo_change_mtu = rtl8169_change_mtu,
8258 .ndo_fix_features = rtl8169_fix_features,
8259 .ndo_set_features = rtl8169_set_features,
8260 .ndo_set_mac_address = rtl_set_mac_address,
8261 .ndo_do_ioctl = rtl8169_ioctl,
8262 .ndo_set_rx_mode = rtl_set_rx_mode,
8263#ifdef CONFIG_NET_POLL_CONTROLLER
8264 .ndo_poll_controller = rtl8169_netpoll,
8265#endif
8266
8267};
8268
Francois Romieu31fa8b12012-03-08 10:09:40 +01008269static const struct rtl_cfg_info {
8270 void (*hw_start)(struct net_device *);
8271 unsigned int region;
8272 unsigned int align;
8273 u16 event_slow;
8274 unsigned features;
Francois Romieu50970832017-10-27 13:24:49 +03008275 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008276 u8 default_ver;
8277} rtl_cfg_infos [] = {
8278 [RTL_CFG_0] = {
8279 .hw_start = rtl_hw_start_8169,
8280 .region = 1,
8281 .align = 0,
8282 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8283 .features = RTL_FEATURE_GMII,
Francois Romieu50970832017-10-27 13:24:49 +03008284 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008285 .default_ver = RTL_GIGA_MAC_VER_01,
8286 },
8287 [RTL_CFG_1] = {
8288 .hw_start = rtl_hw_start_8168,
8289 .region = 2,
8290 .align = 8,
8291 .event_slow = SYSErr | LinkChg | RxOverflow,
8292 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008293 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008294 .default_ver = RTL_GIGA_MAC_VER_11,
8295 },
8296 [RTL_CFG_2] = {
8297 .hw_start = rtl_hw_start_8101,
8298 .region = 2,
8299 .align = 8,
8300 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8301 PCSTimeout,
8302 .features = RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008303 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008304 .default_ver = RTL_GIGA_MAC_VER_13,
8305 }
8306};
8307
8308/* Cfg9346_Unlock assumed. */
8309static unsigned rtl_try_msi(struct rtl8169_private *tp,
8310 const struct rtl_cfg_info *cfg)
8311{
8312 void __iomem *ioaddr = tp->mmio_addr;
8313 unsigned msi = 0;
8314 u8 cfg2;
8315
8316 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8317 if (cfg->features & RTL_FEATURE_MSI) {
8318 if (pci_enable_msi(tp->pci_dev)) {
8319 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8320 } else {
8321 cfg2 |= MSIEnable;
8322 msi = RTL_FEATURE_MSI;
8323 }
8324 }
8325 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8326 RTL_W8(Config2, cfg2);
8327 return msi;
8328}
8329
Hayes Wangc5583862012-07-02 17:23:22 +08008330DECLARE_RTL_COND(rtl_link_list_ready_cond)
8331{
8332 void __iomem *ioaddr = tp->mmio_addr;
8333
8334 return RTL_R8(MCU) & LINK_LIST_RDY;
8335}
8336
8337DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8338{
8339 void __iomem *ioaddr = tp->mmio_addr;
8340
8341 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8342}
8343
Bill Pembertonbaf63292012-12-03 09:23:28 -05008344static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008345{
8346 void __iomem *ioaddr = tp->mmio_addr;
8347 u32 data;
8348
8349 tp->ocp_base = OCP_STD_PHY_BASE;
8350
8351 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8352
8353 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8354 return;
8355
8356 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8357 return;
8358
8359 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8360 msleep(1);
8361 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8362
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008363 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008364 data &= ~(1 << 14);
8365 r8168_mac_ocp_write(tp, 0xe8de, data);
8366
8367 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8368 return;
8369
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008370 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008371 data |= (1 << 15);
8372 r8168_mac_ocp_write(tp, 0xe8de, data);
8373
8374 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8375 return;
8376}
8377
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008378static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8379{
8380 rtl8168ep_stop_cmac(tp);
8381 rtl_hw_init_8168g(tp);
8382}
8383
Bill Pembertonbaf63292012-12-03 09:23:28 -05008384static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008385{
8386 switch (tp->mac_version) {
8387 case RTL_GIGA_MAC_VER_40:
8388 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008389 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008390 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008391 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008392 case RTL_GIGA_MAC_VER_45:
8393 case RTL_GIGA_MAC_VER_46:
8394 case RTL_GIGA_MAC_VER_47:
8395 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008396 rtl_hw_init_8168g(tp);
8397 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008398 case RTL_GIGA_MAC_VER_49:
8399 case RTL_GIGA_MAC_VER_50:
8400 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008401 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008402 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008403 default:
8404 break;
8405 }
8406}
8407
hayeswang929a0312014-09-16 11:40:47 +08008408static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008409{
8410 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8411 const unsigned int region = cfg->region;
8412 struct rtl8169_private *tp;
8413 struct mii_if_info *mii;
8414 struct net_device *dev;
8415 void __iomem *ioaddr;
8416 int chipset, i;
8417 int rc;
8418
8419 if (netif_msg_drv(&debug)) {
8420 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8421 MODULENAME, RTL8169_VERSION);
8422 }
8423
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008424 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8425 if (!dev)
8426 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008427
8428 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008429 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008430 tp = netdev_priv(dev);
8431 tp->dev = dev;
8432 tp->pci_dev = pdev;
8433 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8434
8435 mii = &tp->mii;
8436 mii->dev = dev;
8437 mii->mdio_read = rtl_mdio_read;
8438 mii->mdio_write = rtl_mdio_write;
8439 mii->phy_id_mask = 0x1f;
8440 mii->reg_num_mask = 0x1f;
8441 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8442
8443 /* disable ASPM completely as that cause random device stop working
8444 * problems as well as full system hangs for some PCIe devices users */
8445 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8446 PCIE_LINK_STATE_CLKPM);
8447
8448 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008449 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008450 if (rc < 0) {
8451 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008452 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008453 }
8454
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008455 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008456 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8457
8458 /* make sure PCI base addr 1 is MMIO */
8459 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8460 netif_err(tp, probe, dev,
8461 "region #%d not an MMIO resource, aborting\n",
8462 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008463 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008464 }
8465
8466 /* check for weird/broken PCI region reporting */
8467 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8468 netif_err(tp, probe, dev,
8469 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008470 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008471 }
8472
8473 rc = pci_request_regions(pdev, MODULENAME);
8474 if (rc < 0) {
8475 netif_err(tp, probe, dev, "could not request regions\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008476 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008477 }
8478
Francois Romieu3b6cf252012-03-08 09:59:04 +01008479 /* ioremap MMIO region */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008480 ioaddr = devm_ioremap(&pdev->dev, pci_resource_start(pdev, region),
8481 R8169_REGS_SIZE);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008482 if (!ioaddr) {
8483 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008484 return -EIO;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008485 }
8486 tp->mmio_addr = ioaddr;
8487
8488 if (!pci_is_pcie(pdev))
8489 netif_info(tp, probe, dev, "not PCI Express\n");
8490
8491 /* Identify chip attached to board */
8492 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8493
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008494 tp->cp_cmd = 0;
8495
8496 if ((sizeof(dma_addr_t) > 4) &&
8497 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8498 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008499 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8500 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008501
8502 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8503 if (!pci_is_pcie(pdev))
8504 tp->cp_cmd |= PCIDAC;
8505 dev->features |= NETIF_F_HIGHDMA;
8506 } else {
8507 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8508 if (rc < 0) {
8509 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008510 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008511 }
8512 }
8513
Francois Romieu3b6cf252012-03-08 09:59:04 +01008514 rtl_init_rxcfg(tp);
8515
8516 rtl_irq_disable(tp);
8517
Hayes Wangc5583862012-07-02 17:23:22 +08008518 rtl_hw_initialize(tp);
8519
Francois Romieu3b6cf252012-03-08 09:59:04 +01008520 rtl_hw_reset(tp);
8521
8522 rtl_ack_events(tp, 0xffff);
8523
8524 pci_set_master(pdev);
8525
Francois Romieu3b6cf252012-03-08 09:59:04 +01008526 rtl_init_mdio_ops(tp);
8527 rtl_init_pll_power_ops(tp);
8528 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008529 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008530
8531 rtl8169_print_mac_version(tp);
8532
8533 chipset = tp->mac_version;
8534 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8535
8536 RTL_W8(Cfg9346, Cfg9346_Unlock);
8537 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02008538 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008539 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008540 case RTL_GIGA_MAC_VER_34:
8541 case RTL_GIGA_MAC_VER_35:
8542 case RTL_GIGA_MAC_VER_36:
8543 case RTL_GIGA_MAC_VER_37:
8544 case RTL_GIGA_MAC_VER_38:
8545 case RTL_GIGA_MAC_VER_40:
8546 case RTL_GIGA_MAC_VER_41:
8547 case RTL_GIGA_MAC_VER_42:
8548 case RTL_GIGA_MAC_VER_43:
8549 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008550 case RTL_GIGA_MAC_VER_45:
8551 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008552 case RTL_GIGA_MAC_VER_47:
8553 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008554 case RTL_GIGA_MAC_VER_49:
8555 case RTL_GIGA_MAC_VER_50:
8556 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008557 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8558 tp->features |= RTL_FEATURE_WOL;
8559 if ((RTL_R8(Config3) & LinkUp) != 0)
8560 tp->features |= RTL_FEATURE_WOL;
8561 break;
8562 default:
8563 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8564 tp->features |= RTL_FEATURE_WOL;
8565 break;
8566 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008567 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8568 tp->features |= RTL_FEATURE_WOL;
8569 tp->features |= rtl_try_msi(tp, cfg);
8570 RTL_W8(Cfg9346, Cfg9346_Lock);
8571
8572 if (rtl_tbi_enabled(tp)) {
8573 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008574 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008575 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8576 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8577 tp->link_ok = rtl8169_tbi_link_ok;
8578 tp->do_ioctl = rtl_tbi_ioctl;
8579 } else {
8580 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008581 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008582 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8583 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8584 tp->link_ok = rtl8169_xmii_link_ok;
8585 tp->do_ioctl = rtl_xmii_ioctl;
8586 }
8587
8588 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008589 u64_stats_init(&tp->rx_stats.syncp);
8590 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008591
8592 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008593 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8594 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8595 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8596 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8597 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8598 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8599 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8600 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8601 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8602 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008603 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8604 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008605 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8606 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8607 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8608 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008609 u16 mac_addr[3];
8610
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008611 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8612 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008613
8614 if (is_valid_ether_addr((u8 *)mac_addr))
8615 rtl_rar_set(tp, (u8 *)mac_addr);
8616 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008617 for (i = 0; i < ETH_ALEN; i++)
8618 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008619
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008620 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008621 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008622
8623 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8624
8625 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8626 * properly for all devices */
8627 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008628 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008629
8630 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008631 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8632 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008633 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8634 NETIF_F_HIGHDMA;
8635
hayeswang929a0312014-09-16 11:40:47 +08008636 tp->cp_cmd |= RxChkSum | RxVlan;
8637
8638 /*
8639 * Pretend we are using VLANs; This bypasses a nasty bug where
8640 * Interrupts stop flowing on high load on 8110SCd controllers.
8641 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008642 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008643 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008644 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008645
hayeswang5888d3f2014-07-11 16:25:56 +08008646 if (tp->txd_version == RTL_TD_0)
8647 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008648 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008649 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008650 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8651 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008652 WARN_ON_ONCE(1);
8653
Francois Romieu3b6cf252012-03-08 09:59:04 +01008654 dev->hw_features |= NETIF_F_RXALL;
8655 dev->hw_features |= NETIF_F_RXFCS;
8656
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008657 /* MTU range: 60 - hw-specific max */
8658 dev->min_mtu = ETH_ZLEN;
8659 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8660
Francois Romieu3b6cf252012-03-08 09:59:04 +01008661 tp->hw_start = cfg->hw_start;
8662 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008663 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008664
8665 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8666 ~(RxBOVF | RxFOVF) : ~0;
8667
Kees Cook9de36cc2017-10-25 03:53:12 -07008668 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008669
8670 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8671
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008672 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8673 &tp->counters_phys_addr,
8674 GFP_KERNEL);
Corinna Vinschen42020322015-09-10 10:47:35 +02008675 if (!tp->counters) {
8676 rc = -ENOMEM;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008677 goto err_out_msi_5;
Corinna Vinschen42020322015-09-10 10:47:35 +02008678 }
8679
Francois Romieu3b6cf252012-03-08 09:59:04 +01008680 rc = register_netdev(dev);
8681 if (rc < 0)
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008682 goto err_out_msi_5;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008683
8684 pci_set_drvdata(pdev, dev);
8685
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008686 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8687 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8688 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008689 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8690 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8691 "tx checksumming: %s]\n",
8692 rtl_chip_infos[chipset].jumbo_max,
8693 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8694 }
8695
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008696 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8697 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008698 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8699 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8700 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8701 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008702 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008703 rtl8168_driver_start(tp);
8704 }
8705
Francois Romieu3b6cf252012-03-08 09:59:04 +01008706 if (pci_dev_run_wake(pdev))
8707 pm_runtime_put_noidle(&pdev->dev);
8708
8709 netif_carrier_off(dev);
8710
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008711 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008712
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008713err_out_msi_5:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008714 netif_napi_del(&tp->napi);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008715
8716 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008717}
8718
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719static struct pci_driver rtl8169_pci_driver = {
8720 .name = MODULENAME,
8721 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008722 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008723 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008724 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008725 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726};
8727
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008728module_pci_driver(rtl8169_pci_driver);