blob: cc51286ee51f7967e32665296e6915ce2dfa811b [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 Romieuf0e837d2005-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 {
Heiner Kallweit6c6aa152018-02-24 16:53:23 +0100739 RTL_FEATURE_GMII = (1 << 0),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200740};
741
Ivan Vecera355423d2009-02-06 21:49:57 -0800742struct rtl8169_counters {
743 __le64 tx_packets;
744 __le64 rx_packets;
745 __le64 tx_errors;
746 __le32 rx_errors;
747 __le16 rx_missed;
748 __le16 align_errors;
749 __le32 tx_one_collision;
750 __le32 tx_multi_collision;
751 __le64 rx_unicast;
752 __le64 rx_broadcast;
753 __le32 rx_multicast;
754 __le16 tx_aborted;
755 __le16 tx_underun;
756};
757
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200758struct rtl8169_tc_offsets {
759 bool inited;
760 __le64 tx_errors;
761 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200762 __le16 tx_aborted;
763};
764
Francois Romieuda78dbf2012-01-26 14:18:23 +0100765enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100766 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100767 RTL_FLAG_TASK_SLOW_PENDING,
768 RTL_FLAG_TASK_RESET_PENDING,
769 RTL_FLAG_TASK_PHY_PENDING,
770 RTL_FLAG_MAX
771};
772
Junchang Wang8027aa22012-03-04 23:30:32 +0100773struct rtl8169_stats {
774 u64 packets;
775 u64 bytes;
776 struct u64_stats_sync syncp;
777};
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779struct rtl8169_private {
780 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200781 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000782 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700783 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200784 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700785 u16 txd_version;
786 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
788 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100790 struct rtl8169_stats rx_stats;
791 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
793 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
794 dma_addr_t TxPhyAddr;
795 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000796 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct timer_list timer;
799 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100800
801 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300802 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000803
804 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200805 void (*write)(struct rtl8169_private *, int, int);
806 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000807 } mdio_ops;
808
françois romieu065c27c2011-01-03 15:08:12 +0000809 struct pll_power_ops {
810 void (*down)(struct rtl8169_private *);
811 void (*up)(struct rtl8169_private *);
812 } pll_power_ops;
813
Francois Romieud58d46b2011-05-03 16:38:29 +0200814 struct jumbo_ops {
815 void (*enable)(struct rtl8169_private *);
816 void (*disable)(struct rtl8169_private *);
817 } jumbo_ops;
818
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800819 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200820 void (*write)(struct rtl8169_private *, int, int);
821 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800822 } csi_ops;
823
Oliver Neukum54405cd2011-01-06 21:55:13 +0100824 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100825 int (*get_link_ksettings)(struct net_device *,
826 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000827 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100828 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000829 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800831 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800832 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100833
834 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100835 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
836 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100837 struct work_struct work;
838 } wk;
839
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200840 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200841
842 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200843 dma_addr_t counters_phys_addr;
844 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200845 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000846 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400847 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000848
Francois Romieub6ffd972011-06-17 17:00:05 +0200849 struct rtl_fw {
850 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200851
852#define RTL_VER_SIZE 32
853
854 char version[RTL_VER_SIZE];
855
856 struct rtl_fw_phy_action {
857 __le32 *code;
858 size_t size;
859 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200860 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300861#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800862
863 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864};
865
Ralf Baechle979b6c12005-06-13 14:30:40 -0700866MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700869MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200870module_param_named(debug, debug.msg_enable, int, 0);
871MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872MODULE_LICENSE("GPL");
873MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000874MODULE_FIRMWARE(FIRMWARE_8168D_1);
875MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000876MODULE_FIRMWARE(FIRMWARE_8168E_1);
877MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400878MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800879MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800880MODULE_FIRMWARE(FIRMWARE_8168F_1);
881MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800882MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800883MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800884MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800885MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000886MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000887MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000888MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800889MODULE_FIRMWARE(FIRMWARE_8168H_1);
890MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200891MODULE_FIRMWARE(FIRMWARE_8107E_1);
892MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Francois Romieuda78dbf2012-01-26 14:18:23 +0100894static void rtl_lock_work(struct rtl8169_private *tp)
895{
896 mutex_lock(&tp->wk.mutex);
897}
898
899static void rtl_unlock_work(struct rtl8169_private *tp)
900{
901 mutex_unlock(&tp->wk.mutex);
902}
903
Francois Romieud58d46b2011-05-03 16:38:29 +0200904static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
905{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800906 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
907 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200908}
909
Francois Romieuffc46952012-07-06 14:19:23 +0200910struct rtl_cond {
911 bool (*check)(struct rtl8169_private *);
912 const char *msg;
913};
914
915static void rtl_udelay(unsigned int d)
916{
917 udelay(d);
918}
919
920static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
921 void (*delay)(unsigned int), unsigned int d, int n,
922 bool high)
923{
924 int i;
925
926 for (i = 0; i < n; i++) {
927 delay(d);
928 if (c->check(tp) == high)
929 return true;
930 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200931 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
932 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200933 return false;
934}
935
936static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
937 const struct rtl_cond *c,
938 unsigned int d, int n)
939{
940 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
941}
942
943static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
944 const struct rtl_cond *c,
945 unsigned int d, int n)
946{
947 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
948}
949
950static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
951 const struct rtl_cond *c,
952 unsigned int d, int n)
953{
954 return rtl_loop_wait(tp, c, msleep, d, n, true);
955}
956
957static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
958 const struct rtl_cond *c,
959 unsigned int d, int n)
960{
961 return rtl_loop_wait(tp, c, msleep, d, n, false);
962}
963
964#define DECLARE_RTL_COND(name) \
965static bool name ## _check(struct rtl8169_private *); \
966 \
967static const struct rtl_cond name = { \
968 .check = name ## _check, \
969 .msg = #name \
970}; \
971 \
972static bool name ## _check(struct rtl8169_private *tp)
973
Hayes Wangc5583862012-07-02 17:23:22 +0800974static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
975{
976 if (reg & 0xffff0001) {
977 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
978 return true;
979 }
980 return false;
981}
982
983DECLARE_RTL_COND(rtl_ocp_gphy_cond)
984{
985 void __iomem *ioaddr = tp->mmio_addr;
986
987 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
988}
989
990static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
991{
992 void __iomem *ioaddr = tp->mmio_addr;
993
994 if (rtl_ocp_reg_failure(tp, reg))
995 return;
996
997 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
998
999 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1000}
1001
1002static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1003{
1004 void __iomem *ioaddr = tp->mmio_addr;
1005
1006 if (rtl_ocp_reg_failure(tp, reg))
1007 return 0;
1008
1009 RTL_W32(GPHY_OCP, reg << 15);
1010
1011 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1012 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1013}
1014
Hayes Wangc5583862012-07-02 17:23:22 +08001015static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1016{
1017 void __iomem *ioaddr = tp->mmio_addr;
1018
1019 if (rtl_ocp_reg_failure(tp, reg))
1020 return;
1021
1022 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001023}
1024
1025static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1026{
1027 void __iomem *ioaddr = tp->mmio_addr;
1028
1029 if (rtl_ocp_reg_failure(tp, reg))
1030 return 0;
1031
1032 RTL_W32(OCPDR, reg << 15);
1033
Hayes Wang3a83ad12012-07-11 20:31:56 +08001034 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001035}
1036
1037#define OCP_STD_PHY_BASE 0xa400
1038
1039static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1040{
1041 if (reg == 0x1f) {
1042 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1043 return;
1044 }
1045
1046 if (tp->ocp_base != OCP_STD_PHY_BASE)
1047 reg -= 0x10;
1048
1049 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1050}
1051
1052static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1053{
1054 if (tp->ocp_base != OCP_STD_PHY_BASE)
1055 reg -= 0x10;
1056
1057 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1058}
1059
hayeswangeee37862013-04-01 22:23:38 +00001060static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1061{
1062 if (reg == 0x1f) {
1063 tp->ocp_base = value << 4;
1064 return;
1065 }
1066
1067 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1068}
1069
1070static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1071{
1072 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1073}
1074
Francois Romieuffc46952012-07-06 14:19:23 +02001075DECLARE_RTL_COND(rtl_phyar_cond)
1076{
1077 void __iomem *ioaddr = tp->mmio_addr;
1078
1079 return RTL_R32(PHYAR) & 0x80000000;
1080}
1081
Francois Romieu24192212012-07-06 20:19:42 +02001082static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Francois Romieu24192212012-07-06 20:19:42 +02001084 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Francois Romieu24192212012-07-06 20:19:42 +02001086 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Francois Romieuffc46952012-07-06 14:19:23 +02001088 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001089 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001090 * According to hardware specs a 20us delay is required after write
1091 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001092 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001093 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Francois Romieu24192212012-07-06 20:19:42 +02001096static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Francois Romieu24192212012-07-06 20:19:42 +02001098 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001099 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Francois Romieu24192212012-07-06 20:19:42 +02001101 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Francois Romieuffc46952012-07-06 14:19:23 +02001103 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1104 RTL_R32(PHYAR) & 0xffff : ~0;
1105
Timo Teräs81a95f02010-06-09 17:31:48 -07001106 /*
1107 * According to hardware specs a 20us delay is required after read
1108 * complete indication, but before sending next command.
1109 */
1110 udelay(20);
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return value;
1113}
1114
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001115DECLARE_RTL_COND(rtl_ocpar_cond)
1116{
1117 void __iomem *ioaddr = tp->mmio_addr;
1118
1119 return RTL_R32(OCPAR) & OCPAR_FLAG;
1120}
1121
Francois Romieu24192212012-07-06 20:19:42 +02001122static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001123{
Francois Romieu24192212012-07-06 20:19:42 +02001124 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001125
Francois Romieu24192212012-07-06 20:19:42 +02001126 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001127 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1128 RTL_W32(EPHY_RXER_NUM, 0);
1129
Francois Romieuffc46952012-07-06 14:19:23 +02001130 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001131}
1132
Francois Romieu24192212012-07-06 20:19:42 +02001133static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001134{
Francois Romieu24192212012-07-06 20:19:42 +02001135 r8168dp_1_mdio_access(tp, reg,
1136 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001137}
1138
Francois Romieu24192212012-07-06 20:19:42 +02001139static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001140{
Francois Romieu24192212012-07-06 20:19:42 +02001141 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001142
Francois Romieu24192212012-07-06 20:19:42 +02001143 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001144
1145 mdelay(1);
1146 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1147 RTL_W32(EPHY_RXER_NUM, 0);
1148
Francois Romieuffc46952012-07-06 14:19:23 +02001149 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1150 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001151}
1152
françois romieue6de30d2011-01-03 15:08:37 +00001153#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1154
1155static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1156{
1157 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1158}
1159
1160static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1161{
1162 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1163}
1164
Francois Romieu24192212012-07-06 20:19:42 +02001165static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001166{
Francois Romieu24192212012-07-06 20:19:42 +02001167 void __iomem *ioaddr = tp->mmio_addr;
1168
françois romieue6de30d2011-01-03 15:08:37 +00001169 r8168dp_2_mdio_start(ioaddr);
1170
Francois Romieu24192212012-07-06 20:19:42 +02001171 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001172
1173 r8168dp_2_mdio_stop(ioaddr);
1174}
1175
Francois Romieu24192212012-07-06 20:19:42 +02001176static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001177{
Francois Romieu24192212012-07-06 20:19:42 +02001178 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001179 int value;
1180
1181 r8168dp_2_mdio_start(ioaddr);
1182
Francois Romieu24192212012-07-06 20:19:42 +02001183 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001184
1185 r8168dp_2_mdio_stop(ioaddr);
1186
1187 return value;
1188}
1189
françois romieu4da19632011-01-03 15:07:55 +00001190static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001191{
Francois Romieu24192212012-07-06 20:19:42 +02001192 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001193}
1194
françois romieu4da19632011-01-03 15:07:55 +00001195static int rtl_readphy(struct rtl8169_private *tp, int location)
1196{
Francois Romieu24192212012-07-06 20:19:42 +02001197 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001198}
1199
1200static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1201{
1202 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1203}
1204
Chun-Hao Lin76564422014-10-01 23:17:17 +08001205static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001206{
1207 int val;
1208
françois romieu4da19632011-01-03 15:07:55 +00001209 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001210 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001211}
1212
Francois Romieuccdffb92008-07-26 14:26:06 +02001213static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1214 int val)
1215{
1216 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001217
françois romieu4da19632011-01-03 15:07:55 +00001218 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001219}
1220
1221static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1222{
1223 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001224
françois romieu4da19632011-01-03 15:07:55 +00001225 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001226}
1227
Francois Romieuffc46952012-07-06 14:19:23 +02001228DECLARE_RTL_COND(rtl_ephyar_cond)
1229{
1230 void __iomem *ioaddr = tp->mmio_addr;
1231
1232 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1233}
1234
Francois Romieufdf6fc02012-07-06 22:40:38 +02001235static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001236{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001237 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001238
1239 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1240 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1241
Francois Romieuffc46952012-07-06 14:19:23 +02001242 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1243
1244 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001245}
1246
Francois Romieufdf6fc02012-07-06 22:40:38 +02001247static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001248{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001249 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001250
1251 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1252
Francois Romieuffc46952012-07-06 14:19:23 +02001253 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1254 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001255}
1256
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001257DECLARE_RTL_COND(rtl_eriar_cond)
1258{
1259 void __iomem *ioaddr = tp->mmio_addr;
1260
1261 return RTL_R32(ERIAR) & ERIAR_FLAG;
1262}
1263
Francois Romieufdf6fc02012-07-06 22:40:38 +02001264static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1265 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001266{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001267 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001268
1269 BUG_ON((addr & 3) || (mask == 0));
1270 RTL_W32(ERIDR, val);
1271 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1272
Francois Romieuffc46952012-07-06 14:19:23 +02001273 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001274}
1275
Francois Romieufdf6fc02012-07-06 22:40:38 +02001276static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001277{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001278 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001279
1280 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1281
Francois Romieuffc46952012-07-06 14:19:23 +02001282 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1283 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001284}
1285
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001286static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001287 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001288{
1289 u32 val;
1290
Francois Romieufdf6fc02012-07-06 22:40:38 +02001291 val = rtl_eri_read(tp, addr, type);
1292 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001293}
1294
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001295static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1296{
1297 void __iomem *ioaddr = tp->mmio_addr;
1298
1299 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1300 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1301 RTL_R32(OCPDR) : ~0;
1302}
1303
1304static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1305{
1306 return rtl_eri_read(tp, reg, ERIAR_OOB);
1307}
1308
1309static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1310{
1311 switch (tp->mac_version) {
1312 case RTL_GIGA_MAC_VER_27:
1313 case RTL_GIGA_MAC_VER_28:
1314 case RTL_GIGA_MAC_VER_31:
1315 return r8168dp_ocp_read(tp, mask, reg);
1316 case RTL_GIGA_MAC_VER_49:
1317 case RTL_GIGA_MAC_VER_50:
1318 case RTL_GIGA_MAC_VER_51:
1319 return r8168ep_ocp_read(tp, mask, reg);
1320 default:
1321 BUG();
1322 return ~0;
1323 }
1324}
1325
1326static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1327 u32 data)
1328{
1329 void __iomem *ioaddr = tp->mmio_addr;
1330
1331 RTL_W32(OCPDR, data);
1332 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1333 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1334}
1335
1336static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1337 u32 data)
1338{
1339 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1340 data, ERIAR_OOB);
1341}
1342
1343static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1344{
1345 switch (tp->mac_version) {
1346 case RTL_GIGA_MAC_VER_27:
1347 case RTL_GIGA_MAC_VER_28:
1348 case RTL_GIGA_MAC_VER_31:
1349 r8168dp_ocp_write(tp, mask, reg, data);
1350 break;
1351 case RTL_GIGA_MAC_VER_49:
1352 case RTL_GIGA_MAC_VER_50:
1353 case RTL_GIGA_MAC_VER_51:
1354 r8168ep_ocp_write(tp, mask, reg, data);
1355 break;
1356 default:
1357 BUG();
1358 break;
1359 }
1360}
1361
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001362static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1363{
1364 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1365
1366 ocp_write(tp, 0x1, 0x30, 0x00000001);
1367}
1368
1369#define OOB_CMD_RESET 0x00
1370#define OOB_CMD_DRIVER_START 0x05
1371#define OOB_CMD_DRIVER_STOP 0x06
1372
1373static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1374{
1375 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1376}
1377
1378DECLARE_RTL_COND(rtl_ocp_read_cond)
1379{
1380 u16 reg;
1381
1382 reg = rtl8168_get_ocp_reg(tp);
1383
1384 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1385}
1386
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001387DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1388{
1389 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1390}
1391
1392DECLARE_RTL_COND(rtl_ocp_tx_cond)
1393{
1394 void __iomem *ioaddr = tp->mmio_addr;
1395
Chunhao Lin086ca232018-01-31 01:32:36 +08001396 return RTL_R8(IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001397}
1398
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001399static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1400{
1401 void __iomem *ioaddr = tp->mmio_addr;
1402
1403 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001404 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001405 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1406 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1407}
1408
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001409static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001410{
1411 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001412 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1413}
1414
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001415static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1416{
1417 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1418 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1419 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1420}
1421
1422static void rtl8168_driver_start(struct rtl8169_private *tp)
1423{
1424 switch (tp->mac_version) {
1425 case RTL_GIGA_MAC_VER_27:
1426 case RTL_GIGA_MAC_VER_28:
1427 case RTL_GIGA_MAC_VER_31:
1428 rtl8168dp_driver_start(tp);
1429 break;
1430 case RTL_GIGA_MAC_VER_49:
1431 case RTL_GIGA_MAC_VER_50:
1432 case RTL_GIGA_MAC_VER_51:
1433 rtl8168ep_driver_start(tp);
1434 break;
1435 default:
1436 BUG();
1437 break;
1438 }
1439}
1440
1441static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1442{
1443 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1444 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1445}
1446
1447static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1448{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001449 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001450 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1451 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1452 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1453}
1454
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001455static void rtl8168_driver_stop(struct rtl8169_private *tp)
1456{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001457 switch (tp->mac_version) {
1458 case RTL_GIGA_MAC_VER_27:
1459 case RTL_GIGA_MAC_VER_28:
1460 case RTL_GIGA_MAC_VER_31:
1461 rtl8168dp_driver_stop(tp);
1462 break;
1463 case RTL_GIGA_MAC_VER_49:
1464 case RTL_GIGA_MAC_VER_50:
1465 case RTL_GIGA_MAC_VER_51:
1466 rtl8168ep_driver_stop(tp);
1467 break;
1468 default:
1469 BUG();
1470 break;
1471 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001472}
1473
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001474static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001475{
1476 u16 reg = rtl8168_get_ocp_reg(tp);
1477
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001478 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001479}
1480
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001481static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001482{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001483 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001484}
1485
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001486static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001487{
1488 switch (tp->mac_version) {
1489 case RTL_GIGA_MAC_VER_27:
1490 case RTL_GIGA_MAC_VER_28:
1491 case RTL_GIGA_MAC_VER_31:
1492 return r8168dp_check_dash(tp);
1493 case RTL_GIGA_MAC_VER_49:
1494 case RTL_GIGA_MAC_VER_50:
1495 case RTL_GIGA_MAC_VER_51:
1496 return r8168ep_check_dash(tp);
1497 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001498 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001499 }
1500}
1501
françois romieuc28aa382011-08-02 03:53:43 +00001502struct exgmac_reg {
1503 u16 addr;
1504 u16 mask;
1505 u32 val;
1506};
1507
Francois Romieufdf6fc02012-07-06 22:40:38 +02001508static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001509 const struct exgmac_reg *r, int len)
1510{
1511 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001512 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001513 r++;
1514 }
1515}
1516
Francois Romieuffc46952012-07-06 14:19:23 +02001517DECLARE_RTL_COND(rtl_efusear_cond)
1518{
1519 void __iomem *ioaddr = tp->mmio_addr;
1520
1521 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1522}
1523
Francois Romieufdf6fc02012-07-06 22:40:38 +02001524static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001525{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001526 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001527
1528 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1529
Francois Romieuffc46952012-07-06 14:19:23 +02001530 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1531 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001532}
1533
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001534static u16 rtl_get_events(struct rtl8169_private *tp)
1535{
1536 void __iomem *ioaddr = tp->mmio_addr;
1537
1538 return RTL_R16(IntrStatus);
1539}
1540
1541static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1542{
1543 void __iomem *ioaddr = tp->mmio_addr;
1544
1545 RTL_W16(IntrStatus, bits);
1546 mmiowb();
1547}
1548
1549static void rtl_irq_disable(struct rtl8169_private *tp)
1550{
1551 void __iomem *ioaddr = tp->mmio_addr;
1552
1553 RTL_W16(IntrMask, 0);
1554 mmiowb();
1555}
1556
Francois Romieu3e990ff2012-01-26 12:50:01 +01001557static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1558{
1559 void __iomem *ioaddr = tp->mmio_addr;
1560
1561 RTL_W16(IntrMask, bits);
1562}
1563
Francois Romieuda78dbf2012-01-26 14:18:23 +01001564#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1565#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1566#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1567
1568static void rtl_irq_enable_all(struct rtl8169_private *tp)
1569{
1570 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1571}
1572
françois romieu811fd302011-12-04 20:30:45 +00001573static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
françois romieu811fd302011-12-04 20:30:45 +00001575 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001577 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001578 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001579 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
françois romieu4da19632011-01-03 15:07:55 +00001582static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
françois romieu4da19632011-01-03 15:07:55 +00001584 void __iomem *ioaddr = tp->mmio_addr;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return RTL_R32(TBICSR) & TBIReset;
1587}
1588
françois romieu4da19632011-01-03 15:07:55 +00001589static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
françois romieu4da19632011-01-03 15:07:55 +00001591 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1595{
1596 return RTL_R32(TBICSR) & TBILinkOk;
1597}
1598
1599static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1600{
1601 return RTL_R8(PHYstatus) & LinkStatus;
1602}
1603
françois romieu4da19632011-01-03 15:07:55 +00001604static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
françois romieu4da19632011-01-03 15:07:55 +00001606 void __iomem *ioaddr = tp->mmio_addr;
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1609}
1610
françois romieu4da19632011-01-03 15:07:55 +00001611static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 unsigned int val;
1614
françois romieu4da19632011-01-03 15:07:55 +00001615 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1616 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Hayes Wang70090422011-07-06 15:58:06 +08001619static void rtl_link_chg_patch(struct rtl8169_private *tp)
1620{
1621 void __iomem *ioaddr = tp->mmio_addr;
1622 struct net_device *dev = tp->dev;
1623
1624 if (!netif_running(dev))
1625 return;
1626
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001627 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1628 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001629 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001630 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1631 ERIAR_EXGMAC);
1632 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1633 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001634 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001635 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1636 ERIAR_EXGMAC);
1637 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1638 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001639 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001640 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1641 ERIAR_EXGMAC);
1642 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1643 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001644 }
1645 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001646 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001647 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001648 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001649 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001650 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1651 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1652 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001653 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1654 ERIAR_EXGMAC);
1655 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1656 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001657 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001658 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1659 ERIAR_EXGMAC);
1660 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1661 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001662 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001663 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1664 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001665 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1666 ERIAR_EXGMAC);
1667 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1668 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001669 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001670 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1671 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001672 }
Hayes Wang70090422011-07-06 15:58:06 +08001673 }
1674}
1675
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001676static void rtl8169_check_link_status(struct net_device *dev,
1677 struct rtl8169_private *tp,
1678 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001681 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001682 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001683 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001685 if (net_ratelimit())
1686 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001687 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001689 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweita92a0842018-01-08 21:39:13 +01001690 pm_runtime_idle(&tp->pci_dev->dev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001694#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1695
1696static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1697{
1698 void __iomem *ioaddr = tp->mmio_addr;
1699 u8 options;
1700 u32 wolopts = 0;
1701
1702 options = RTL_R8(Config1);
1703 if (!(options & PMEnable))
1704 return 0;
1705
1706 options = RTL_R8(Config3);
1707 if (options & LinkUp)
1708 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001709 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001710 case RTL_GIGA_MAC_VER_34:
1711 case RTL_GIGA_MAC_VER_35:
1712 case RTL_GIGA_MAC_VER_36:
1713 case RTL_GIGA_MAC_VER_37:
1714 case RTL_GIGA_MAC_VER_38:
1715 case RTL_GIGA_MAC_VER_40:
1716 case RTL_GIGA_MAC_VER_41:
1717 case RTL_GIGA_MAC_VER_42:
1718 case RTL_GIGA_MAC_VER_43:
1719 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001720 case RTL_GIGA_MAC_VER_45:
1721 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001722 case RTL_GIGA_MAC_VER_47:
1723 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001724 case RTL_GIGA_MAC_VER_49:
1725 case RTL_GIGA_MAC_VER_50:
1726 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001727 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1728 wolopts |= WAKE_MAGIC;
1729 break;
1730 default:
1731 if (options & MagicPacket)
1732 wolopts |= WAKE_MAGIC;
1733 break;
1734 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001735
1736 options = RTL_R8(Config5);
1737 if (options & UWF)
1738 wolopts |= WAKE_UCAST;
1739 if (options & BWF)
1740 wolopts |= WAKE_BCAST;
1741 if (options & MWF)
1742 wolopts |= WAKE_MCAST;
1743
1744 return wolopts;
1745}
1746
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001747static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1748{
1749 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001750 struct device *d = &tp->pci_dev->dev;
1751
1752 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001753
Francois Romieuda78dbf2012-01-26 14:18:23 +01001754 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001755
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001756 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001757 if (pm_runtime_active(d))
1758 wol->wolopts = __rtl8169_get_wol(tp);
1759 else
1760 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001761
Francois Romieuda78dbf2012-01-26 14:18:23 +01001762 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001763
1764 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001765}
1766
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001767static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001768{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001769 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001770 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001771 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001772 u32 opt;
1773 u16 reg;
1774 u8 mask;
1775 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001776 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001777 { WAKE_UCAST, Config5, UWF },
1778 { WAKE_BCAST, Config5, BWF },
1779 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001780 { WAKE_ANY, Config5, LanWake },
1781 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001782 };
Francois Romieu851e6022012-04-17 11:10:11 +02001783 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001784
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001785 RTL_W8(Cfg9346, Cfg9346_Unlock);
1786
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001787 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001788 case RTL_GIGA_MAC_VER_34:
1789 case RTL_GIGA_MAC_VER_35:
1790 case RTL_GIGA_MAC_VER_36:
1791 case RTL_GIGA_MAC_VER_37:
1792 case RTL_GIGA_MAC_VER_38:
1793 case RTL_GIGA_MAC_VER_40:
1794 case RTL_GIGA_MAC_VER_41:
1795 case RTL_GIGA_MAC_VER_42:
1796 case RTL_GIGA_MAC_VER_43:
1797 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001798 case RTL_GIGA_MAC_VER_45:
1799 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001800 case RTL_GIGA_MAC_VER_47:
1801 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001802 case RTL_GIGA_MAC_VER_49:
1803 case RTL_GIGA_MAC_VER_50:
1804 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001805 tmp = ARRAY_SIZE(cfg) - 1;
1806 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001807 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001808 0x0dc,
1809 ERIAR_MASK_0100,
1810 MagicPacket_v2,
1811 0x0000,
1812 ERIAR_EXGMAC);
1813 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001814 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001815 0x0dc,
1816 ERIAR_MASK_0100,
1817 0x0000,
1818 MagicPacket_v2,
1819 ERIAR_EXGMAC);
1820 break;
1821 default:
1822 tmp = ARRAY_SIZE(cfg);
1823 break;
1824 }
1825
1826 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001827 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001828 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001829 options |= cfg[i].mask;
1830 RTL_W8(cfg[i].reg, options);
1831 }
1832
Francois Romieu851e6022012-04-17 11:10:11 +02001833 switch (tp->mac_version) {
1834 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1835 options = RTL_R8(Config1) & ~PMEnable;
1836 if (wolopts)
1837 options |= PMEnable;
1838 RTL_W8(Config1, options);
1839 break;
1840 default:
Francois Romieud387b422012-04-17 11:12:01 +02001841 options = RTL_R8(Config2) & ~PME_SIGNAL;
1842 if (wolopts)
1843 options |= PME_SIGNAL;
1844 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001845 break;
1846 }
1847
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001848 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001849}
1850
1851static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1852{
1853 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001854 struct device *d = &tp->pci_dev->dev;
1855
1856 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001857
Francois Romieuda78dbf2012-01-26 14:18:23 +01001858 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001859
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001860 if (pm_runtime_active(d))
1861 __rtl8169_set_wol(tp, wol->wolopts);
1862 else
1863 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001864
1865 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001866
françois romieuea809072010-11-08 13:23:58 +00001867 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1868
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001869 pm_runtime_put_noidle(d);
1870
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001871 return 0;
1872}
1873
Francois Romieu31bd2042011-04-26 18:58:59 +02001874static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1875{
Francois Romieu85bffe62011-04-27 08:22:39 +02001876 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879static void rtl8169_get_drvinfo(struct net_device *dev,
1880 struct ethtool_drvinfo *info)
1881{
1882 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001883 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Rick Jones68aad782011-11-07 13:29:27 +00001885 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1886 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1887 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001888 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001889 if (!IS_ERR_OR_NULL(rtl_fw))
1890 strlcpy(info->fw_version, rtl_fw->version,
1891 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
1894static int rtl8169_get_regs_len(struct net_device *dev)
1895{
1896 return R8169_REGS_SIZE;
1897}
1898
1899static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001900 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct rtl8169_private *tp = netdev_priv(dev);
1903 void __iomem *ioaddr = tp->mmio_addr;
1904 int ret = 0;
1905 u32 reg;
1906
1907 reg = RTL_R32(TBICSR);
1908 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1909 (duplex == DUPLEX_FULL)) {
1910 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1911 } else if (autoneg == AUTONEG_ENABLE)
1912 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1913 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001914 netif_warn(tp, link, dev,
1915 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 ret = -EOPNOTSUPP;
1917 }
1918
1919 return ret;
1920}
1921
1922static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001923 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001926 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001927 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Hayes Wang716b50a2011-02-22 17:26:18 +08001929 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001932 int auto_nego;
1933
françois romieu4da19632011-01-03 15:07:55 +00001934 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001935 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1936 ADVERTISE_100HALF | ADVERTISE_100FULL);
1937
1938 if (adv & ADVERTISED_10baseT_Half)
1939 auto_nego |= ADVERTISE_10HALF;
1940 if (adv & ADVERTISED_10baseT_Full)
1941 auto_nego |= ADVERTISE_10FULL;
1942 if (adv & ADVERTISED_100baseT_Half)
1943 auto_nego |= ADVERTISE_100HALF;
1944 if (adv & ADVERTISED_100baseT_Full)
1945 auto_nego |= ADVERTISE_100FULL;
1946
françois romieu3577aa12009-05-19 10:46:48 +00001947 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1948
françois romieu4da19632011-01-03 15:07:55 +00001949 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001950 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1951
1952 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001953 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001954 if (adv & ADVERTISED_1000baseT_Half)
1955 giga_ctrl |= ADVERTISE_1000HALF;
1956 if (adv & ADVERTISED_1000baseT_Full)
1957 giga_ctrl |= ADVERTISE_1000FULL;
1958 } else if (adv & (ADVERTISED_1000baseT_Half |
1959 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001960 netif_info(tp, link, dev,
1961 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001962 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
françois romieu3577aa12009-05-19 10:46:48 +00001965 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001966
françois romieu4da19632011-01-03 15:07:55 +00001967 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1968 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001969 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001970 if (speed == SPEED_10)
1971 bmcr = 0;
1972 else if (speed == SPEED_100)
1973 bmcr = BMCR_SPEED100;
1974 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001975 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001976
1977 if (duplex == DUPLEX_FULL)
1978 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001979 }
1980
françois romieu4da19632011-01-03 15:07:55 +00001981 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001982
Francois Romieucecb5fd2011-04-01 10:21:07 +02001983 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1984 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001985 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001986 rtl_writephy(tp, 0x17, 0x2138);
1987 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001988 } else {
françois romieu4da19632011-01-03 15:07:55 +00001989 rtl_writephy(tp, 0x17, 0x2108);
1990 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001991 }
1992 }
1993
Oliver Neukum54405cd2011-01-06 21:55:13 +01001994 rc = 0;
1995out:
1996 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01002000 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct rtl8169_private *tp = netdev_priv(dev);
2003 int ret;
2004
Oliver Neukum54405cd2011-01-06 21:55:13 +01002005 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002006 if (ret < 0)
2007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Francois Romieu4876cc12011-03-11 21:07:11 +01002009 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002010 (advertising & ADVERTISED_1000baseT_Full) &&
2011 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002013 }
2014out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return ret;
2016}
2017
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002018static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2019 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Francois Romieud58d46b2011-05-03 16:38:29 +02002021 struct rtl8169_private *tp = netdev_priv(dev);
2022
Francois Romieu2b7b4312011-04-18 22:53:24 -07002023 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002024 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Francois Romieud58d46b2011-05-03 16:38:29 +02002026 if (dev->mtu > JUMBO_1K &&
2027 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2028 features &= ~NETIF_F_IP_CSUM;
2029
Michał Mirosław350fb322011-04-08 06:35:56 +00002030 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Francois Romieuda78dbf2012-01-26 14:18:23 +01002033static void __rtl8169_set_features(struct net_device *dev,
2034 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002037 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002038 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
hayeswang929a0312014-09-16 11:40:47 +08002040 rx_config = RTL_R32(RxConfig);
2041 if (features & NETIF_F_RXALL)
2042 rx_config |= (AcceptErr | AcceptRunt);
2043 else
2044 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
hayeswang929a0312014-09-16 11:40:47 +08002046 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002047
hayeswang929a0312014-09-16 11:40:47 +08002048 if (features & NETIF_F_RXCSUM)
2049 tp->cp_cmd |= RxChkSum;
2050 else
2051 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002052
hayeswang929a0312014-09-16 11:40:47 +08002053 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2054 tp->cp_cmd |= RxVlan;
2055 else
2056 tp->cp_cmd &= ~RxVlan;
2057
2058 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2059
2060 RTL_W16(CPlusCmd, tp->cp_cmd);
2061 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Francois Romieuda78dbf2012-01-26 14:18:23 +01002064static int rtl8169_set_features(struct net_device *dev,
2065 netdev_features_t features)
2066{
2067 struct rtl8169_private *tp = netdev_priv(dev);
2068
hayeswang929a0312014-09-16 11:40:47 +08002069 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2070
Francois Romieuda78dbf2012-01-26 14:18:23 +01002071 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002072 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002073 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002074 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 return 0;
2077}
2078
Francois Romieuda78dbf2012-01-26 14:18:23 +01002079
Kirill Smelkov810f4892012-11-10 21:11:02 +04002080static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002082 return (skb_vlan_tag_present(skb)) ?
2083 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Francois Romieu7a8fc772011-03-01 17:18:33 +01002086static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Francois Romieu7a8fc772011-03-01 17:18:33 +01002090 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002091 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002094static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2095 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 struct rtl8169_private *tp = netdev_priv(dev);
2098 void __iomem *ioaddr = tp->mmio_addr;
2099 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002100 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002102 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002104 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 status = RTL_R32(TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002107 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2108 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002110 cmd->base.speed = SPEED_1000;
2111 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2112
2113 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2114 supported);
2115 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2116 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002117
2118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002121static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2122 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002126 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2127
2128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002131static int rtl8169_get_link_ksettings(struct net_device *dev,
2132 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002135 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Francois Romieuda78dbf2012-01-26 14:18:23 +01002137 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002138 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002139 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Francois Romieuccdffb92008-07-26 14:26:06 +02002141 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002144static int rtl8169_set_link_ksettings(struct net_device *dev,
2145 const struct ethtool_link_ksettings *cmd)
2146{
2147 struct rtl8169_private *tp = netdev_priv(dev);
2148 int rc;
2149 u32 advertising;
2150
2151 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2152 cmd->link_modes.advertising))
2153 return -EINVAL;
2154
2155 del_timer_sync(&tp->timer);
2156
2157 rtl_lock_work(tp);
2158 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2159 cmd->base.duplex, advertising);
2160 rtl_unlock_work(tp);
2161
2162 return rc;
2163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2166 void *p)
2167{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002168 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002169 u32 __iomem *data = tp->mmio_addr;
2170 u32 *dw = p;
2171 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Francois Romieuda78dbf2012-01-26 14:18:23 +01002173 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002174 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2175 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002176 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002179static u32 rtl8169_get_msglevel(struct net_device *dev)
2180{
2181 struct rtl8169_private *tp = netdev_priv(dev);
2182
2183 return tp->msg_enable;
2184}
2185
2186static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2187{
2188 struct rtl8169_private *tp = netdev_priv(dev);
2189
2190 tp->msg_enable = value;
2191}
2192
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002193static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2194 "tx_packets",
2195 "rx_packets",
2196 "tx_errors",
2197 "rx_errors",
2198 "rx_missed",
2199 "align_errors",
2200 "tx_single_collisions",
2201 "tx_multi_collisions",
2202 "unicast",
2203 "broadcast",
2204 "multicast",
2205 "tx_aborted",
2206 "tx_underrun",
2207};
2208
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002209static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002210{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002211 switch (sset) {
2212 case ETH_SS_STATS:
2213 return ARRAY_SIZE(rtl8169_gstrings);
2214 default:
2215 return -EOPNOTSUPP;
2216 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002217}
2218
Corinna Vinschen42020322015-09-10 10:47:35 +02002219DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002220{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002221 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002222
Corinna Vinschen42020322015-09-10 10:47:35 +02002223 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002224}
2225
Corinna Vinschen42020322015-09-10 10:47:35 +02002226static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002227{
2228 struct rtl8169_private *tp = netdev_priv(dev);
2229 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002230 dma_addr_t paddr = tp->counters_phys_addr;
2231 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002232
2233 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Francois Romieua78e9362018-01-26 01:53:26 +01002234 RTL_R32(CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002235 cmd = (u64)paddr & DMA_BIT_MASK(32);
2236 RTL_W32(CounterAddrLow, cmd);
2237 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2238
Francois Romieua78e9362018-01-26 01:53:26 +01002239 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002240}
2241
2242static bool rtl8169_reset_counters(struct net_device *dev)
2243{
2244 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002245
2246 /*
2247 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2248 * tally counters.
2249 */
2250 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2251 return true;
2252
Corinna Vinschen42020322015-09-10 10:47:35 +02002253 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002254}
2255
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002256static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002257{
2258 struct rtl8169_private *tp = netdev_priv(dev);
2259 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002260
Ivan Vecera355423d2009-02-06 21:49:57 -08002261 /*
2262 * Some chips are unable to dump tally counters when the receiver
2263 * is disabled.
2264 */
2265 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002266 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002267
Corinna Vinschen42020322015-09-10 10:47:35 +02002268 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002269}
2270
2271static bool rtl8169_init_counter_offsets(struct net_device *dev)
2272{
2273 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002274 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002275 bool ret = false;
2276
2277 /*
2278 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2279 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2280 * reset by a power cycle, while the counter values collected by the
2281 * driver are reset at every driver unload/load cycle.
2282 *
2283 * To make sure the HW values returned by @get_stats64 match the SW
2284 * values, we collect the initial values at first open(*) and use them
2285 * as offsets to normalize the values returned by @get_stats64.
2286 *
2287 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2288 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2289 * set at open time by rtl_hw_start.
2290 */
2291
2292 if (tp->tc_offset.inited)
2293 return true;
2294
2295 /* If both, reset and update fail, propagate to caller. */
2296 if (rtl8169_reset_counters(dev))
2297 ret = true;
2298
2299 if (rtl8169_update_counters(dev))
2300 ret = true;
2301
Corinna Vinschen42020322015-09-10 10:47:35 +02002302 tp->tc_offset.tx_errors = counters->tx_errors;
2303 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2304 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002305 tp->tc_offset.inited = true;
2306
2307 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002308}
2309
Ivan Vecera355423d2009-02-06 21:49:57 -08002310static void rtl8169_get_ethtool_stats(struct net_device *dev,
2311 struct ethtool_stats *stats, u64 *data)
2312{
2313 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002314 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002315 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002316
2317 ASSERT_RTNL();
2318
Chun-Hao Line0636232016-07-29 16:37:55 +08002319 pm_runtime_get_noresume(d);
2320
2321 if (pm_runtime_active(d))
2322 rtl8169_update_counters(dev);
2323
2324 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002325
Corinna Vinschen42020322015-09-10 10:47:35 +02002326 data[0] = le64_to_cpu(counters->tx_packets);
2327 data[1] = le64_to_cpu(counters->rx_packets);
2328 data[2] = le64_to_cpu(counters->tx_errors);
2329 data[3] = le32_to_cpu(counters->rx_errors);
2330 data[4] = le16_to_cpu(counters->rx_missed);
2331 data[5] = le16_to_cpu(counters->align_errors);
2332 data[6] = le32_to_cpu(counters->tx_one_collision);
2333 data[7] = le32_to_cpu(counters->tx_multi_collision);
2334 data[8] = le64_to_cpu(counters->rx_unicast);
2335 data[9] = le64_to_cpu(counters->rx_broadcast);
2336 data[10] = le32_to_cpu(counters->rx_multicast);
2337 data[11] = le16_to_cpu(counters->tx_aborted);
2338 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002339}
2340
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002341static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2342{
2343 switch(stringset) {
2344 case ETH_SS_STATS:
2345 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2346 break;
2347 }
2348}
2349
Florian Fainellif0903ea2016-12-03 12:01:19 -08002350static int rtl8169_nway_reset(struct net_device *dev)
2351{
2352 struct rtl8169_private *tp = netdev_priv(dev);
2353
2354 return mii_nway_restart(&tp->mii);
2355}
2356
Francois Romieu50970832017-10-27 13:24:49 +03002357/*
2358 * Interrupt coalescing
2359 *
2360 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2361 * > 8169, 8168 and 810x line of chipsets
2362 *
2363 * 8169, 8168, and 8136(810x) serial chipsets support it.
2364 *
2365 * > 2 - the Tx timer unit at gigabit speed
2366 *
2367 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2368 * (0xe0) bit 1 and bit 0.
2369 *
2370 * For 8169
2371 * bit[1:0] \ speed 1000M 100M 10M
2372 * 0 0 320ns 2.56us 40.96us
2373 * 0 1 2.56us 20.48us 327.7us
2374 * 1 0 5.12us 40.96us 655.4us
2375 * 1 1 10.24us 81.92us 1.31ms
2376 *
2377 * For the other
2378 * bit[1:0] \ speed 1000M 100M 10M
2379 * 0 0 5us 2.56us 40.96us
2380 * 0 1 40us 20.48us 327.7us
2381 * 1 0 80us 40.96us 655.4us
2382 * 1 1 160us 81.92us 1.31ms
2383 */
2384
2385/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2386struct rtl_coalesce_scale {
2387 /* Rx / Tx */
2388 u32 nsecs[2];
2389};
2390
2391/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2392struct rtl_coalesce_info {
2393 u32 speed;
2394 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2395};
2396
2397/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2398#define rxtx_x1822(r, t) { \
2399 {{(r), (t)}}, \
2400 {{(r)*8, (t)*8}}, \
2401 {{(r)*8*2, (t)*8*2}}, \
2402 {{(r)*8*2*2, (t)*8*2*2}}, \
2403}
2404static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2405 /* speed delays: rx00 tx00 */
2406 { SPEED_10, rxtx_x1822(40960, 40960) },
2407 { SPEED_100, rxtx_x1822( 2560, 2560) },
2408 { SPEED_1000, rxtx_x1822( 320, 320) },
2409 { 0 },
2410};
2411
2412static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2413 /* speed delays: rx00 tx00 */
2414 { SPEED_10, rxtx_x1822(40960, 40960) },
2415 { SPEED_100, rxtx_x1822( 2560, 2560) },
2416 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2417 { 0 },
2418};
2419#undef rxtx_x1822
2420
2421/* get rx/tx scale vector corresponding to current speed */
2422static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2423{
2424 struct rtl8169_private *tp = netdev_priv(dev);
2425 struct ethtool_link_ksettings ecmd;
2426 const struct rtl_coalesce_info *ci;
2427 int rc;
2428
2429 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2430 if (rc < 0)
2431 return ERR_PTR(rc);
2432
2433 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2434 if (ecmd.base.speed == ci->speed) {
2435 return ci;
2436 }
2437 }
2438
2439 return ERR_PTR(-ELNRNG);
2440}
2441
2442static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2443{
2444 struct rtl8169_private *tp = netdev_priv(dev);
2445 void __iomem *ioaddr = tp->mmio_addr;
2446 const struct rtl_coalesce_info *ci;
2447 const struct rtl_coalesce_scale *scale;
2448 struct {
2449 u32 *max_frames;
2450 u32 *usecs;
2451 } coal_settings [] = {
2452 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2453 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2454 }, *p = coal_settings;
2455 int i;
2456 u16 w;
2457
2458 memset(ec, 0, sizeof(*ec));
2459
2460 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2461 ci = rtl_coalesce_info(dev);
2462 if (IS_ERR(ci))
2463 return PTR_ERR(ci);
2464
2465 scale = &ci->scalev[RTL_R16(CPlusCmd) & 3];
2466
2467 /* read IntrMitigate and adjust according to scale */
2468 for (w = RTL_R16(IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2469 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2470 w >>= RTL_COALESCE_SHIFT;
2471 *p->usecs = w & RTL_COALESCE_MASK;
2472 }
2473
2474 for (i = 0; i < 2; i++) {
2475 p = coal_settings + i;
2476 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2477
2478 /*
2479 * ethtool_coalesce says it is illegal to set both usecs and
2480 * max_frames to 0.
2481 */
2482 if (!*p->usecs && !*p->max_frames)
2483 *p->max_frames = 1;
2484 }
2485
2486 return 0;
2487}
2488
2489/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2490static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2491 struct net_device *dev, u32 nsec, u16 *cp01)
2492{
2493 const struct rtl_coalesce_info *ci;
2494 u16 i;
2495
2496 ci = rtl_coalesce_info(dev);
2497 if (IS_ERR(ci))
2498 return ERR_CAST(ci);
2499
2500 for (i = 0; i < 4; i++) {
2501 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2502 ci->scalev[i].nsecs[1]);
2503 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2504 *cp01 = i;
2505 return &ci->scalev[i];
2506 }
2507 }
2508
2509 return ERR_PTR(-EINVAL);
2510}
2511
2512static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2513{
2514 struct rtl8169_private *tp = netdev_priv(dev);
2515 void __iomem *ioaddr = tp->mmio_addr;
2516 const struct rtl_coalesce_scale *scale;
2517 struct {
2518 u32 frames;
2519 u32 usecs;
2520 } coal_settings [] = {
2521 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2522 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2523 }, *p = coal_settings;
2524 u16 w = 0, cp01;
2525 int i;
2526
2527 scale = rtl_coalesce_choose_scale(dev,
2528 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2529 if (IS_ERR(scale))
2530 return PTR_ERR(scale);
2531
2532 for (i = 0; i < 2; i++, p++) {
2533 u32 units;
2534
2535 /*
2536 * accept max_frames=1 we returned in rtl_get_coalesce.
2537 * accept it not only when usecs=0 because of e.g. the following scenario:
2538 *
2539 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2540 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2541 * - then user does `ethtool -C eth0 rx-usecs 100`
2542 *
2543 * since ethtool sends to kernel whole ethtool_coalesce
2544 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2545 * we'll reject it below in `frames % 4 != 0`.
2546 */
2547 if (p->frames == 1) {
2548 p->frames = 0;
2549 }
2550
2551 units = p->usecs * 1000 / scale->nsecs[i];
2552 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2553 return -EINVAL;
2554
2555 w <<= RTL_COALESCE_SHIFT;
2556 w |= units;
2557 w <<= RTL_COALESCE_SHIFT;
2558 w |= p->frames >> 2;
2559 }
2560
2561 rtl_lock_work(tp);
2562
2563 RTL_W16(IntrMitigate, swab16(w));
2564
2565 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2566 RTL_W16(CPlusCmd, tp->cp_cmd);
2567 RTL_R16(CPlusCmd);
2568
2569 rtl_unlock_work(tp);
2570
2571 return 0;
2572}
2573
Jeff Garzik7282d492006-09-13 14:30:00 -04002574static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 .get_drvinfo = rtl8169_get_drvinfo,
2576 .get_regs_len = rtl8169_get_regs_len,
2577 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002578 .get_coalesce = rtl_get_coalesce,
2579 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002580 .get_msglevel = rtl8169_get_msglevel,
2581 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002583 .get_wol = rtl8169_get_wol,
2584 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002585 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002586 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002587 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002588 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002589 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002590 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002591 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592};
2593
Francois Romieu07d3f512007-02-21 22:40:46 +01002594static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002595 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Francois Romieu5d320a22011-05-08 17:47:36 +02002597 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002598 /*
2599 * The driver currently handles the 8168Bf and the 8168Be identically
2600 * but they can be identified more specifically through the test below
2601 * if needed:
2602 *
2603 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002604 *
2605 * Same thing for the 8101Eb and the 8101Ec:
2606 *
2607 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002608 */
Francois Romieu37441002011-06-17 22:58:54 +02002609 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002611 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 int mac_version;
2613 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002614 /* 8168EP family. */
2615 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2616 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2617 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2618
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002619 /* 8168H family. */
2620 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2621 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2622
Hayes Wangc5583862012-07-02 17:23:22 +08002623 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002624 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002625 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002626 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2627 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2628
Hayes Wangc2218922011-09-06 16:55:18 +08002629 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002630 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002631 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2632 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2633
hayeswang01dc7fe2011-03-21 01:50:28 +00002634 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002635 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002636 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2637 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2638 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2639
Francois Romieu5b538df2008-07-20 16:22:45 +02002640 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002641 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2642 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002644
françois romieue6de30d2011-01-03 15:08:37 +00002645 /* 8168DP family. */
2646 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2647 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002648 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002649
Francois Romieuef808d52008-06-29 13:10:54 +02002650 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002651 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002652 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002653 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002654 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002655 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2656 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002657 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002658 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002659 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002660
2661 /* 8168B family. */
2662 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2663 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2664 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2665 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2666
2667 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002668 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2669 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002670 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002671 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002672 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2673 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2674 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002675 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2676 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2677 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2678 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2679 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2680 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002681 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002682 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002683 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002684 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2685 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002686 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2687 /* FIXME: where did these entries come from ? -- FR */
2688 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2689 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2690
2691 /* 8110 family. */
2692 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2693 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2694 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2695 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2696 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2697 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2698
Jean Delvaref21b75e2009-05-26 20:54:48 -07002699 /* Catch-all */
2700 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002701 };
2702 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 u32 reg;
2704
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002705 reg = RTL_R32(TxConfig);
2706 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 p++;
2708 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002709
2710 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2711 netif_notice(tp, probe, dev,
2712 "unknown MAC, using family default\n");
2713 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002714 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2715 tp->mac_version = tp->mii.supports_gmii ?
2716 RTL_GIGA_MAC_VER_42 :
2717 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002718 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2719 tp->mac_version = tp->mii.supports_gmii ?
2720 RTL_GIGA_MAC_VER_45 :
2721 RTL_GIGA_MAC_VER_47;
2722 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2723 tp->mac_version = tp->mii.supports_gmii ?
2724 RTL_GIGA_MAC_VER_46 :
2725 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2730{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002731 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Francois Romieu867763c2007-08-17 18:21:58 +02002734struct phy_reg {
2735 u16 reg;
2736 u16 val;
2737};
2738
françois romieu4da19632011-01-03 15:07:55 +00002739static void rtl_writephy_batch(struct rtl8169_private *tp,
2740 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002741{
2742 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002744 regs++;
2745 }
2746}
2747
françois romieubca03d52011-01-03 15:07:31 +00002748#define PHY_READ 0x00000000
2749#define PHY_DATA_OR 0x10000000
2750#define PHY_DATA_AND 0x20000000
2751#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002752#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002753#define PHY_CLEAR_READCOUNT 0x70000000
2754#define PHY_WRITE 0x80000000
2755#define PHY_READCOUNT_EQ_SKIP 0x90000000
2756#define PHY_COMP_EQ_SKIPN 0xa0000000
2757#define PHY_COMP_NEQ_SKIPN 0xb0000000
2758#define PHY_WRITE_PREVIOUS 0xc0000000
2759#define PHY_SKIPN 0xd0000000
2760#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002761
Hayes Wang960aee62011-06-18 11:37:48 +02002762struct fw_info {
2763 u32 magic;
2764 char version[RTL_VER_SIZE];
2765 __le32 fw_start;
2766 __le32 fw_len;
2767 u8 chksum;
2768} __packed;
2769
Francois Romieu1c361ef2011-06-17 17:16:24 +02002770#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2771
2772static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002773{
Francois Romieub6ffd972011-06-17 17:00:05 +02002774 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002775 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002776 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2777 char *version = rtl_fw->version;
2778 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002779
Francois Romieu1c361ef2011-06-17 17:16:24 +02002780 if (fw->size < FW_OPCODE_SIZE)
2781 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002782
2783 if (!fw_info->magic) {
2784 size_t i, size, start;
2785 u8 checksum = 0;
2786
2787 if (fw->size < sizeof(*fw_info))
2788 goto out;
2789
2790 for (i = 0; i < fw->size; i++)
2791 checksum += fw->data[i];
2792 if (checksum != 0)
2793 goto out;
2794
2795 start = le32_to_cpu(fw_info->fw_start);
2796 if (start > fw->size)
2797 goto out;
2798
2799 size = le32_to_cpu(fw_info->fw_len);
2800 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2801 goto out;
2802
2803 memcpy(version, fw_info->version, RTL_VER_SIZE);
2804
2805 pa->code = (__le32 *)(fw->data + start);
2806 pa->size = size;
2807 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002808 if (fw->size % FW_OPCODE_SIZE)
2809 goto out;
2810
2811 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2812
2813 pa->code = (__le32 *)fw->data;
2814 pa->size = fw->size / FW_OPCODE_SIZE;
2815 }
2816 version[RTL_VER_SIZE - 1] = 0;
2817
2818 rc = true;
2819out:
2820 return rc;
2821}
2822
Francois Romieufd112f22011-06-18 00:10:29 +02002823static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2824 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002825{
Francois Romieufd112f22011-06-18 00:10:29 +02002826 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002827 size_t index;
2828
Francois Romieu1c361ef2011-06-17 17:16:24 +02002829 for (index = 0; index < pa->size; index++) {
2830 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002831 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002832
hayeswang42b82dc2011-01-10 02:07:25 +00002833 switch(action & 0xf0000000) {
2834 case PHY_READ:
2835 case PHY_DATA_OR:
2836 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002837 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002838 case PHY_CLEAR_READCOUNT:
2839 case PHY_WRITE:
2840 case PHY_WRITE_PREVIOUS:
2841 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002842 break;
2843
hayeswang42b82dc2011-01-10 02:07:25 +00002844 case PHY_BJMPN:
2845 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002846 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002847 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002848 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002849 }
2850 break;
2851 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002852 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002853 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002854 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002855 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002856 }
2857 break;
2858 case PHY_COMP_EQ_SKIPN:
2859 case PHY_COMP_NEQ_SKIPN:
2860 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002861 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002862 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002863 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002864 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002865 }
2866 break;
2867
hayeswang42b82dc2011-01-10 02:07:25 +00002868 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002869 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002870 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002871 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002872 }
2873 }
Francois Romieufd112f22011-06-18 00:10:29 +02002874 rc = true;
2875out:
2876 return rc;
2877}
françois romieubca03d52011-01-03 15:07:31 +00002878
Francois Romieufd112f22011-06-18 00:10:29 +02002879static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2880{
2881 struct net_device *dev = tp->dev;
2882 int rc = -EINVAL;
2883
2884 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002885 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002886 goto out;
2887 }
2888
2889 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2890 rc = 0;
2891out:
2892 return rc;
2893}
2894
2895static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2896{
2897 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002898 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002899 u32 predata, count;
2900 size_t index;
2901
2902 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002903 org.write = ops->write;
2904 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002905
Francois Romieu1c361ef2011-06-17 17:16:24 +02002906 for (index = 0; index < pa->size; ) {
2907 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002908 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002909 u32 regno = (action & 0x0fff0000) >> 16;
2910
2911 if (!action)
2912 break;
françois romieubca03d52011-01-03 15:07:31 +00002913
2914 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002915 case PHY_READ:
2916 predata = rtl_readphy(tp, regno);
2917 count++;
2918 index++;
françois romieubca03d52011-01-03 15:07:31 +00002919 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002920 case PHY_DATA_OR:
2921 predata |= data;
2922 index++;
2923 break;
2924 case PHY_DATA_AND:
2925 predata &= data;
2926 index++;
2927 break;
2928 case PHY_BJMPN:
2929 index -= regno;
2930 break;
hayeswangeee37862013-04-01 22:23:38 +00002931 case PHY_MDIO_CHG:
2932 if (data == 0) {
2933 ops->write = org.write;
2934 ops->read = org.read;
2935 } else if (data == 1) {
2936 ops->write = mac_mcu_write;
2937 ops->read = mac_mcu_read;
2938 }
2939
hayeswang42b82dc2011-01-10 02:07:25 +00002940 index++;
2941 break;
2942 case PHY_CLEAR_READCOUNT:
2943 count = 0;
2944 index++;
2945 break;
2946 case PHY_WRITE:
2947 rtl_writephy(tp, regno, data);
2948 index++;
2949 break;
2950 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002951 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002952 break;
2953 case PHY_COMP_EQ_SKIPN:
2954 if (predata == data)
2955 index += regno;
2956 index++;
2957 break;
2958 case PHY_COMP_NEQ_SKIPN:
2959 if (predata != data)
2960 index += regno;
2961 index++;
2962 break;
2963 case PHY_WRITE_PREVIOUS:
2964 rtl_writephy(tp, regno, predata);
2965 index++;
2966 break;
2967 case PHY_SKIPN:
2968 index += regno + 1;
2969 break;
2970 case PHY_DELAY_MS:
2971 mdelay(data);
2972 index++;
2973 break;
2974
françois romieubca03d52011-01-03 15:07:31 +00002975 default:
2976 BUG();
2977 }
2978 }
hayeswangeee37862013-04-01 22:23:38 +00002979
2980 ops->write = org.write;
2981 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002982}
2983
françois romieuf1e02ed2011-01-13 13:07:53 +00002984static void rtl_release_firmware(struct rtl8169_private *tp)
2985{
Francois Romieub6ffd972011-06-17 17:00:05 +02002986 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2987 release_firmware(tp->rtl_fw->fw);
2988 kfree(tp->rtl_fw);
2989 }
2990 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002991}
2992
François Romieu953a12c2011-04-24 17:38:48 +02002993static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002994{
Francois Romieub6ffd972011-06-17 17:00:05 +02002995 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002996
2997 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002998 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002999 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02003000}
3001
3002static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
3003{
3004 if (rtl_readphy(tp, reg) != val)
3005 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
3006 else
3007 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00003008}
3009
françois romieu4da19632011-01-03 15:07:55 +00003010static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003012 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00003013 { 0x1f, 0x0001 },
3014 { 0x06, 0x006e },
3015 { 0x08, 0x0708 },
3016 { 0x15, 0x4000 },
3017 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
françois romieu0b9b5712009-08-10 19:44:56 +00003019 { 0x1f, 0x0001 },
3020 { 0x03, 0x00a1 },
3021 { 0x02, 0x0008 },
3022 { 0x01, 0x0120 },
3023 { 0x00, 0x1000 },
3024 { 0x04, 0x0800 },
3025 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
françois romieu0b9b5712009-08-10 19:44:56 +00003027 { 0x03, 0xff41 },
3028 { 0x02, 0xdf60 },
3029 { 0x01, 0x0140 },
3030 { 0x00, 0x0077 },
3031 { 0x04, 0x7800 },
3032 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
françois romieu0b9b5712009-08-10 19:44:56 +00003034 { 0x03, 0x802f },
3035 { 0x02, 0x4f02 },
3036 { 0x01, 0x0409 },
3037 { 0x00, 0xf0f9 },
3038 { 0x04, 0x9800 },
3039 { 0x04, 0x9000 },
3040
3041 { 0x03, 0xdf01 },
3042 { 0x02, 0xdf20 },
3043 { 0x01, 0xff95 },
3044 { 0x00, 0xba00 },
3045 { 0x04, 0xa800 },
3046 { 0x04, 0xa000 },
3047
3048 { 0x03, 0xff41 },
3049 { 0x02, 0xdf20 },
3050 { 0x01, 0x0140 },
3051 { 0x00, 0x00bb },
3052 { 0x04, 0xb800 },
3053 { 0x04, 0xb000 },
3054
3055 { 0x03, 0xdf41 },
3056 { 0x02, 0xdc60 },
3057 { 0x01, 0x6340 },
3058 { 0x00, 0x007d },
3059 { 0x04, 0xd800 },
3060 { 0x04, 0xd000 },
3061
3062 { 0x03, 0xdf01 },
3063 { 0x02, 0xdf20 },
3064 { 0x01, 0x100a },
3065 { 0x00, 0xa0ff },
3066 { 0x04, 0xf800 },
3067 { 0x04, 0xf000 },
3068
3069 { 0x1f, 0x0000 },
3070 { 0x0b, 0x0000 },
3071 { 0x00, 0x9200 }
3072 };
3073
françois romieu4da19632011-01-03 15:07:55 +00003074 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
françois romieu4da19632011-01-03 15:07:55 +00003077static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003078{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003079 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003080 { 0x1f, 0x0002 },
3081 { 0x01, 0x90d0 },
3082 { 0x1f, 0x0000 }
3083 };
3084
françois romieu4da19632011-01-03 15:07:55 +00003085 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003086}
3087
françois romieu4da19632011-01-03 15:07:55 +00003088static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003089{
3090 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003091
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003092 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3093 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003094 return;
3095
françois romieu4da19632011-01-03 15:07:55 +00003096 rtl_writephy(tp, 0x1f, 0x0001);
3097 rtl_writephy(tp, 0x10, 0xf01b);
3098 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003099}
3100
françois romieu4da19632011-01-03 15:07:55 +00003101static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003102{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003103 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003104 { 0x1f, 0x0001 },
3105 { 0x04, 0x0000 },
3106 { 0x03, 0x00a1 },
3107 { 0x02, 0x0008 },
3108 { 0x01, 0x0120 },
3109 { 0x00, 0x1000 },
3110 { 0x04, 0x0800 },
3111 { 0x04, 0x9000 },
3112 { 0x03, 0x802f },
3113 { 0x02, 0x4f02 },
3114 { 0x01, 0x0409 },
3115 { 0x00, 0xf099 },
3116 { 0x04, 0x9800 },
3117 { 0x04, 0xa000 },
3118 { 0x03, 0xdf01 },
3119 { 0x02, 0xdf20 },
3120 { 0x01, 0xff95 },
3121 { 0x00, 0xba00 },
3122 { 0x04, 0xa800 },
3123 { 0x04, 0xf000 },
3124 { 0x03, 0xdf01 },
3125 { 0x02, 0xdf20 },
3126 { 0x01, 0x101a },
3127 { 0x00, 0xa0ff },
3128 { 0x04, 0xf800 },
3129 { 0x04, 0x0000 },
3130 { 0x1f, 0x0000 },
3131
3132 { 0x1f, 0x0001 },
3133 { 0x10, 0xf41b },
3134 { 0x14, 0xfb54 },
3135 { 0x18, 0xf5c7 },
3136 { 0x1f, 0x0000 },
3137
3138 { 0x1f, 0x0001 },
3139 { 0x17, 0x0cc0 },
3140 { 0x1f, 0x0000 }
3141 };
3142
françois romieu4da19632011-01-03 15:07:55 +00003143 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003144
françois romieu4da19632011-01-03 15:07:55 +00003145 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003146}
3147
françois romieu4da19632011-01-03 15:07:55 +00003148static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003149{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003150 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003151 { 0x1f, 0x0001 },
3152 { 0x04, 0x0000 },
3153 { 0x03, 0x00a1 },
3154 { 0x02, 0x0008 },
3155 { 0x01, 0x0120 },
3156 { 0x00, 0x1000 },
3157 { 0x04, 0x0800 },
3158 { 0x04, 0x9000 },
3159 { 0x03, 0x802f },
3160 { 0x02, 0x4f02 },
3161 { 0x01, 0x0409 },
3162 { 0x00, 0xf099 },
3163 { 0x04, 0x9800 },
3164 { 0x04, 0xa000 },
3165 { 0x03, 0xdf01 },
3166 { 0x02, 0xdf20 },
3167 { 0x01, 0xff95 },
3168 { 0x00, 0xba00 },
3169 { 0x04, 0xa800 },
3170 { 0x04, 0xf000 },
3171 { 0x03, 0xdf01 },
3172 { 0x02, 0xdf20 },
3173 { 0x01, 0x101a },
3174 { 0x00, 0xa0ff },
3175 { 0x04, 0xf800 },
3176 { 0x04, 0x0000 },
3177 { 0x1f, 0x0000 },
3178
3179 { 0x1f, 0x0001 },
3180 { 0x0b, 0x8480 },
3181 { 0x1f, 0x0000 },
3182
3183 { 0x1f, 0x0001 },
3184 { 0x18, 0x67c7 },
3185 { 0x04, 0x2000 },
3186 { 0x03, 0x002f },
3187 { 0x02, 0x4360 },
3188 { 0x01, 0x0109 },
3189 { 0x00, 0x3022 },
3190 { 0x04, 0x2800 },
3191 { 0x1f, 0x0000 },
3192
3193 { 0x1f, 0x0001 },
3194 { 0x17, 0x0cc0 },
3195 { 0x1f, 0x0000 }
3196 };
3197
françois romieu4da19632011-01-03 15:07:55 +00003198 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003199}
3200
françois romieu4da19632011-01-03 15:07:55 +00003201static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003202{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003203 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003204 { 0x10, 0xf41b },
3205 { 0x1f, 0x0000 }
3206 };
3207
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl_writephy(tp, 0x1f, 0x0001);
3209 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003210
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003212}
3213
françois romieu4da19632011-01-03 15:07:55 +00003214static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003215{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003216 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003217 { 0x1f, 0x0001 },
3218 { 0x10, 0xf41b },
3219 { 0x1f, 0x0000 }
3220 };
3221
françois romieu4da19632011-01-03 15:07:55 +00003222 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003223}
3224
françois romieu4da19632011-01-03 15:07:55 +00003225static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003226{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003227 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003228 { 0x1f, 0x0000 },
3229 { 0x1d, 0x0f00 },
3230 { 0x1f, 0x0002 },
3231 { 0x0c, 0x1ec8 },
3232 { 0x1f, 0x0000 }
3233 };
3234
françois romieu4da19632011-01-03 15:07:55 +00003235 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003236}
3237
françois romieu4da19632011-01-03 15:07:55 +00003238static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003239{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003240 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003241 { 0x1f, 0x0001 },
3242 { 0x1d, 0x3d98 },
3243 { 0x1f, 0x0000 }
3244 };
3245
françois romieu4da19632011-01-03 15:07:55 +00003246 rtl_writephy(tp, 0x1f, 0x0000);
3247 rtl_patchphy(tp, 0x14, 1 << 5);
3248 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003249
françois romieu4da19632011-01-03 15:07:55 +00003250 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003251}
3252
françois romieu4da19632011-01-03 15:07:55 +00003253static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003254{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003255 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003256 { 0x1f, 0x0001 },
3257 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003258 { 0x1f, 0x0002 },
3259 { 0x00, 0x88d4 },
3260 { 0x01, 0x82b1 },
3261 { 0x03, 0x7002 },
3262 { 0x08, 0x9e30 },
3263 { 0x09, 0x01f0 },
3264 { 0x0a, 0x5500 },
3265 { 0x0c, 0x00c8 },
3266 { 0x1f, 0x0003 },
3267 { 0x12, 0xc096 },
3268 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003269 { 0x1f, 0x0000 },
3270 { 0x1f, 0x0000 },
3271 { 0x09, 0x2000 },
3272 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003273 };
3274
françois romieu4da19632011-01-03 15:07:55 +00003275 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003276
françois romieu4da19632011-01-03 15:07:55 +00003277 rtl_patchphy(tp, 0x14, 1 << 5);
3278 rtl_patchphy(tp, 0x0d, 1 << 5);
3279 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003280}
3281
françois romieu4da19632011-01-03 15:07:55 +00003282static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003283{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003284 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003285 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003286 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003287 { 0x03, 0x802f },
3288 { 0x02, 0x4f02 },
3289 { 0x01, 0x0409 },
3290 { 0x00, 0xf099 },
3291 { 0x04, 0x9800 },
3292 { 0x04, 0x9000 },
3293 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003294 { 0x1f, 0x0002 },
3295 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003296 { 0x06, 0x0761 },
3297 { 0x1f, 0x0003 },
3298 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003299 { 0x1f, 0x0000 }
3300 };
3301
françois romieu4da19632011-01-03 15:07:55 +00003302 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003303
françois romieu4da19632011-01-03 15:07:55 +00003304 rtl_patchphy(tp, 0x16, 1 << 0);
3305 rtl_patchphy(tp, 0x14, 1 << 5);
3306 rtl_patchphy(tp, 0x0d, 1 << 5);
3307 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003308}
3309
françois romieu4da19632011-01-03 15:07:55 +00003310static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003311{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003312 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003313 { 0x1f, 0x0001 },
3314 { 0x12, 0x2300 },
3315 { 0x1d, 0x3d98 },
3316 { 0x1f, 0x0002 },
3317 { 0x0c, 0x7eb8 },
3318 { 0x06, 0x5461 },
3319 { 0x1f, 0x0003 },
3320 { 0x16, 0x0f0a },
3321 { 0x1f, 0x0000 }
3322 };
3323
françois romieu4da19632011-01-03 15:07:55 +00003324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003325
françois romieu4da19632011-01-03 15:07:55 +00003326 rtl_patchphy(tp, 0x16, 1 << 0);
3327 rtl_patchphy(tp, 0x14, 1 << 5);
3328 rtl_patchphy(tp, 0x0d, 1 << 5);
3329 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003330}
3331
françois romieu4da19632011-01-03 15:07:55 +00003332static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003333{
françois romieu4da19632011-01-03 15:07:55 +00003334 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003335}
3336
françois romieubca03d52011-01-03 15:07:31 +00003337static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003338{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003339 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003340 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003341 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003342 { 0x06, 0x4064 },
3343 { 0x07, 0x2863 },
3344 { 0x08, 0x059c },
3345 { 0x09, 0x26b4 },
3346 { 0x0a, 0x6a19 },
3347 { 0x0b, 0xdcc8 },
3348 { 0x10, 0xf06d },
3349 { 0x14, 0x7f68 },
3350 { 0x18, 0x7fd9 },
3351 { 0x1c, 0xf0ff },
3352 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003353 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003354 { 0x12, 0xf49f },
3355 { 0x13, 0x070b },
3356 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003357 { 0x14, 0x94c0 },
3358
3359 /*
3360 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003361 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003362 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003363 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003364 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003365 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003366 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003367 { 0x06, 0x5561 },
3368
3369 /*
3370 * Can not link to 1Gbps with bad cable
3371 * Decrease SNR threshold form 21.07dB to 19.04dB
3372 */
3373 { 0x1f, 0x0001 },
3374 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003375
3376 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003377 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003378 };
3379
françois romieu4da19632011-01-03 15:07:55 +00003380 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003381
françois romieubca03d52011-01-03 15:07:31 +00003382 /*
3383 * Rx Error Issue
3384 * Fine Tune Switching regulator parameter
3385 */
françois romieu4da19632011-01-03 15:07:55 +00003386 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003387 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3388 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003389
Francois Romieufdf6fc02012-07-06 22:40:38 +02003390 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003391 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003392 { 0x1f, 0x0002 },
3393 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003394 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003395 { 0x05, 0x8330 },
3396 { 0x06, 0x669a },
3397 { 0x1f, 0x0002 }
3398 };
3399 int val;
3400
françois romieu4da19632011-01-03 15:07:55 +00003401 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003402
françois romieu4da19632011-01-03 15:07:55 +00003403 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003404
3405 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003406 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003407 0x0065, 0x0066, 0x0067, 0x0068,
3408 0x0069, 0x006a, 0x006b, 0x006c
3409 };
3410 int i;
3411
françois romieu4da19632011-01-03 15:07:55 +00003412 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003413
3414 val &= 0xff00;
3415 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003416 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003417 }
3418 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003419 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003420 { 0x1f, 0x0002 },
3421 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003422 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003423 { 0x05, 0x8330 },
3424 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003425 };
3426
françois romieu4da19632011-01-03 15:07:55 +00003427 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003428 }
3429
françois romieubca03d52011-01-03 15:07:31 +00003430 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003431 rtl_writephy(tp, 0x1f, 0x0002);
3432 rtl_patchphy(tp, 0x0d, 0x0300);
3433 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003434
françois romieubca03d52011-01-03 15:07:31 +00003435 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003436 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003437 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3438 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003439
françois romieu4da19632011-01-03 15:07:55 +00003440 rtl_writephy(tp, 0x1f, 0x0005);
3441 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003442
3443 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003444
françois romieu4da19632011-01-03 15:07:55 +00003445 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003446}
3447
françois romieubca03d52011-01-03 15:07:31 +00003448static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003449{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003450 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003451 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003452 { 0x1f, 0x0001 },
3453 { 0x06, 0x4064 },
3454 { 0x07, 0x2863 },
3455 { 0x08, 0x059c },
3456 { 0x09, 0x26b4 },
3457 { 0x0a, 0x6a19 },
3458 { 0x0b, 0xdcc8 },
3459 { 0x10, 0xf06d },
3460 { 0x14, 0x7f68 },
3461 { 0x18, 0x7fd9 },
3462 { 0x1c, 0xf0ff },
3463 { 0x1d, 0x3d9c },
3464 { 0x1f, 0x0003 },
3465 { 0x12, 0xf49f },
3466 { 0x13, 0x070b },
3467 { 0x1a, 0x05ad },
3468 { 0x14, 0x94c0 },
3469
françois romieubca03d52011-01-03 15:07:31 +00003470 /*
3471 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003472 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003473 */
françois romieudaf9df62009-10-07 12:44:20 +00003474 { 0x1f, 0x0002 },
3475 { 0x06, 0x5561 },
3476 { 0x1f, 0x0005 },
3477 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003478 { 0x06, 0x5561 },
3479
3480 /*
3481 * Can not link to 1Gbps with bad cable
3482 * Decrease SNR threshold form 21.07dB to 19.04dB
3483 */
3484 { 0x1f, 0x0001 },
3485 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003486
3487 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003488 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003489 };
3490
françois romieu4da19632011-01-03 15:07:55 +00003491 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003492
Francois Romieufdf6fc02012-07-06 22:40:38 +02003493 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003494 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003495 { 0x1f, 0x0002 },
3496 { 0x05, 0x669a },
3497 { 0x1f, 0x0005 },
3498 { 0x05, 0x8330 },
3499 { 0x06, 0x669a },
3500
3501 { 0x1f, 0x0002 }
3502 };
3503 int val;
3504
françois romieu4da19632011-01-03 15:07:55 +00003505 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003506
françois romieu4da19632011-01-03 15:07:55 +00003507 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003508 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003509 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003510 0x0065, 0x0066, 0x0067, 0x0068,
3511 0x0069, 0x006a, 0x006b, 0x006c
3512 };
3513 int i;
3514
françois romieu4da19632011-01-03 15:07:55 +00003515 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003516
3517 val &= 0xff00;
3518 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003519 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003520 }
3521 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003522 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003523 { 0x1f, 0x0002 },
3524 { 0x05, 0x2642 },
3525 { 0x1f, 0x0005 },
3526 { 0x05, 0x8330 },
3527 { 0x06, 0x2642 }
3528 };
3529
françois romieu4da19632011-01-03 15:07:55 +00003530 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003531 }
3532
françois romieubca03d52011-01-03 15:07:31 +00003533 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003534 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003535 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3536 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003537
françois romieubca03d52011-01-03 15:07:31 +00003538 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003539 rtl_writephy(tp, 0x1f, 0x0002);
3540 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003541
françois romieu4da19632011-01-03 15:07:55 +00003542 rtl_writephy(tp, 0x1f, 0x0005);
3543 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003544
3545 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003546
françois romieu4da19632011-01-03 15:07:55 +00003547 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003548}
3549
françois romieu4da19632011-01-03 15:07:55 +00003550static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003551{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003552 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003553 { 0x1f, 0x0002 },
3554 { 0x10, 0x0008 },
3555 { 0x0d, 0x006c },
3556
3557 { 0x1f, 0x0000 },
3558 { 0x0d, 0xf880 },
3559
3560 { 0x1f, 0x0001 },
3561 { 0x17, 0x0cc0 },
3562
3563 { 0x1f, 0x0001 },
3564 { 0x0b, 0xa4d8 },
3565 { 0x09, 0x281c },
3566 { 0x07, 0x2883 },
3567 { 0x0a, 0x6b35 },
3568 { 0x1d, 0x3da4 },
3569 { 0x1c, 0xeffd },
3570 { 0x14, 0x7f52 },
3571 { 0x18, 0x7fc6 },
3572 { 0x08, 0x0601 },
3573 { 0x06, 0x4063 },
3574 { 0x10, 0xf074 },
3575 { 0x1f, 0x0003 },
3576 { 0x13, 0x0789 },
3577 { 0x12, 0xf4bd },
3578 { 0x1a, 0x04fd },
3579 { 0x14, 0x84b0 },
3580 { 0x1f, 0x0000 },
3581 { 0x00, 0x9200 },
3582
3583 { 0x1f, 0x0005 },
3584 { 0x01, 0x0340 },
3585 { 0x1f, 0x0001 },
3586 { 0x04, 0x4000 },
3587 { 0x03, 0x1d21 },
3588 { 0x02, 0x0c32 },
3589 { 0x01, 0x0200 },
3590 { 0x00, 0x5554 },
3591 { 0x04, 0x4800 },
3592 { 0x04, 0x4000 },
3593 { 0x04, 0xf000 },
3594 { 0x03, 0xdf01 },
3595 { 0x02, 0xdf20 },
3596 { 0x01, 0x101a },
3597 { 0x00, 0xa0ff },
3598 { 0x04, 0xf800 },
3599 { 0x04, 0xf000 },
3600 { 0x1f, 0x0000 },
3601
3602 { 0x1f, 0x0007 },
3603 { 0x1e, 0x0023 },
3604 { 0x16, 0x0000 },
3605 { 0x1f, 0x0000 }
3606 };
3607
françois romieu4da19632011-01-03 15:07:55 +00003608 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003609}
3610
françois romieue6de30d2011-01-03 15:08:37 +00003611static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3612{
3613 static const struct phy_reg phy_reg_init[] = {
3614 { 0x1f, 0x0001 },
3615 { 0x17, 0x0cc0 },
3616
3617 { 0x1f, 0x0007 },
3618 { 0x1e, 0x002d },
3619 { 0x18, 0x0040 },
3620 { 0x1f, 0x0000 }
3621 };
3622
3623 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3624 rtl_patchphy(tp, 0x0d, 1 << 5);
3625}
3626
Hayes Wang70090422011-07-06 15:58:06 +08003627static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003628{
3629 static const struct phy_reg phy_reg_init[] = {
3630 /* Enable Delay cap */
3631 { 0x1f, 0x0005 },
3632 { 0x05, 0x8b80 },
3633 { 0x06, 0xc896 },
3634 { 0x1f, 0x0000 },
3635
3636 /* Channel estimation fine tune */
3637 { 0x1f, 0x0001 },
3638 { 0x0b, 0x6c20 },
3639 { 0x07, 0x2872 },
3640 { 0x1c, 0xefff },
3641 { 0x1f, 0x0003 },
3642 { 0x14, 0x6420 },
3643 { 0x1f, 0x0000 },
3644
3645 /* Update PFM & 10M TX idle timer */
3646 { 0x1f, 0x0007 },
3647 { 0x1e, 0x002f },
3648 { 0x15, 0x1919 },
3649 { 0x1f, 0x0000 },
3650
3651 { 0x1f, 0x0007 },
3652 { 0x1e, 0x00ac },
3653 { 0x18, 0x0006 },
3654 { 0x1f, 0x0000 }
3655 };
3656
Francois Romieu15ecd032011-04-27 13:52:22 -07003657 rtl_apply_firmware(tp);
3658
hayeswang01dc7fe2011-03-21 01:50:28 +00003659 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3660
3661 /* DCO enable for 10M IDLE Power */
3662 rtl_writephy(tp, 0x1f, 0x0007);
3663 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003664 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003665 rtl_writephy(tp, 0x1f, 0x0000);
3666
3667 /* For impedance matching */
3668 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003669 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003670 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003671
3672 /* PHY auto speed down */
3673 rtl_writephy(tp, 0x1f, 0x0007);
3674 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003675 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003676 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003678
3679 rtl_writephy(tp, 0x1f, 0x0005);
3680 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003681 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003682 rtl_writephy(tp, 0x1f, 0x0000);
3683
3684 rtl_writephy(tp, 0x1f, 0x0005);
3685 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003686 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003687 rtl_writephy(tp, 0x1f, 0x0007);
3688 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003689 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003690 rtl_writephy(tp, 0x1f, 0x0006);
3691 rtl_writephy(tp, 0x00, 0x5a00);
3692 rtl_writephy(tp, 0x1f, 0x0000);
3693 rtl_writephy(tp, 0x0d, 0x0007);
3694 rtl_writephy(tp, 0x0e, 0x003c);
3695 rtl_writephy(tp, 0x0d, 0x4007);
3696 rtl_writephy(tp, 0x0e, 0x0000);
3697 rtl_writephy(tp, 0x0d, 0x0000);
3698}
3699
françois romieu9ecb9aa2012-12-07 11:20:21 +00003700static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3701{
3702 const u16 w[] = {
3703 addr[0] | (addr[1] << 8),
3704 addr[2] | (addr[3] << 8),
3705 addr[4] | (addr[5] << 8)
3706 };
3707 const struct exgmac_reg e[] = {
3708 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3709 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3710 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3711 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3712 };
3713
3714 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3715}
3716
Hayes Wang70090422011-07-06 15:58:06 +08003717static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3718{
3719 static const struct phy_reg phy_reg_init[] = {
3720 /* Enable Delay cap */
3721 { 0x1f, 0x0004 },
3722 { 0x1f, 0x0007 },
3723 { 0x1e, 0x00ac },
3724 { 0x18, 0x0006 },
3725 { 0x1f, 0x0002 },
3726 { 0x1f, 0x0000 },
3727 { 0x1f, 0x0000 },
3728
3729 /* Channel estimation fine tune */
3730 { 0x1f, 0x0003 },
3731 { 0x09, 0xa20f },
3732 { 0x1f, 0x0000 },
3733 { 0x1f, 0x0000 },
3734
3735 /* Green Setting */
3736 { 0x1f, 0x0005 },
3737 { 0x05, 0x8b5b },
3738 { 0x06, 0x9222 },
3739 { 0x05, 0x8b6d },
3740 { 0x06, 0x8000 },
3741 { 0x05, 0x8b76 },
3742 { 0x06, 0x8000 },
3743 { 0x1f, 0x0000 }
3744 };
3745
3746 rtl_apply_firmware(tp);
3747
3748 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3749
3750 /* For 4-corner performance improve */
3751 rtl_writephy(tp, 0x1f, 0x0005);
3752 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003753 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003754 rtl_writephy(tp, 0x1f, 0x0000);
3755
3756 /* PHY auto speed down */
3757 rtl_writephy(tp, 0x1f, 0x0004);
3758 rtl_writephy(tp, 0x1f, 0x0007);
3759 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003760 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003761 rtl_writephy(tp, 0x1f, 0x0002);
3762 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003763 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003764
3765 /* improve 10M EEE waveform */
3766 rtl_writephy(tp, 0x1f, 0x0005);
3767 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003768 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003769 rtl_writephy(tp, 0x1f, 0x0000);
3770
3771 /* Improve 2-pair detection performance */
3772 rtl_writephy(tp, 0x1f, 0x0005);
3773 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003774 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003775 rtl_writephy(tp, 0x1f, 0x0000);
3776
3777 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003778 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003779 rtl_writephy(tp, 0x1f, 0x0005);
3780 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003781 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003782 rtl_writephy(tp, 0x1f, 0x0004);
3783 rtl_writephy(tp, 0x1f, 0x0007);
3784 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003785 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003786 rtl_writephy(tp, 0x1f, 0x0002);
3787 rtl_writephy(tp, 0x1f, 0x0000);
3788 rtl_writephy(tp, 0x0d, 0x0007);
3789 rtl_writephy(tp, 0x0e, 0x003c);
3790 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003791 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003792 rtl_writephy(tp, 0x0d, 0x0000);
3793
3794 /* Green feature */
3795 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003796 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3797 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003798 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003799 rtl_writephy(tp, 0x1f, 0x0005);
3800 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3801 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003802
françois romieu9ecb9aa2012-12-07 11:20:21 +00003803 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3804 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003805}
3806
Hayes Wang5f886e02012-03-30 14:33:03 +08003807static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3808{
3809 /* For 4-corner performance improve */
3810 rtl_writephy(tp, 0x1f, 0x0005);
3811 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003812 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003813 rtl_writephy(tp, 0x1f, 0x0000);
3814
3815 /* PHY auto speed down */
3816 rtl_writephy(tp, 0x1f, 0x0007);
3817 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003818 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003819 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003820 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003821
3822 /* Improve 10M EEE waveform */
3823 rtl_writephy(tp, 0x1f, 0x0005);
3824 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003825 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003826 rtl_writephy(tp, 0x1f, 0x0000);
3827}
3828
Hayes Wangc2218922011-09-06 16:55:18 +08003829static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3830{
3831 static const struct phy_reg phy_reg_init[] = {
3832 /* Channel estimation fine tune */
3833 { 0x1f, 0x0003 },
3834 { 0x09, 0xa20f },
3835 { 0x1f, 0x0000 },
3836
3837 /* Modify green table for giga & fnet */
3838 { 0x1f, 0x0005 },
3839 { 0x05, 0x8b55 },
3840 { 0x06, 0x0000 },
3841 { 0x05, 0x8b5e },
3842 { 0x06, 0x0000 },
3843 { 0x05, 0x8b67 },
3844 { 0x06, 0x0000 },
3845 { 0x05, 0x8b70 },
3846 { 0x06, 0x0000 },
3847 { 0x1f, 0x0000 },
3848 { 0x1f, 0x0007 },
3849 { 0x1e, 0x0078 },
3850 { 0x17, 0x0000 },
3851 { 0x19, 0x00fb },
3852 { 0x1f, 0x0000 },
3853
3854 /* Modify green table for 10M */
3855 { 0x1f, 0x0005 },
3856 { 0x05, 0x8b79 },
3857 { 0x06, 0xaa00 },
3858 { 0x1f, 0x0000 },
3859
3860 /* Disable hiimpedance detection (RTCT) */
3861 { 0x1f, 0x0003 },
3862 { 0x01, 0x328a },
3863 { 0x1f, 0x0000 }
3864 };
3865
3866 rtl_apply_firmware(tp);
3867
3868 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3869
Hayes Wang5f886e02012-03-30 14:33:03 +08003870 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003871
3872 /* Improve 2-pair detection performance */
3873 rtl_writephy(tp, 0x1f, 0x0005);
3874 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003875 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003876 rtl_writephy(tp, 0x1f, 0x0000);
3877}
3878
3879static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3880{
3881 rtl_apply_firmware(tp);
3882
Hayes Wang5f886e02012-03-30 14:33:03 +08003883 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003884}
3885
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003886static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3887{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003888 static const struct phy_reg phy_reg_init[] = {
3889 /* Channel estimation fine tune */
3890 { 0x1f, 0x0003 },
3891 { 0x09, 0xa20f },
3892 { 0x1f, 0x0000 },
3893
3894 /* Modify green table for giga & fnet */
3895 { 0x1f, 0x0005 },
3896 { 0x05, 0x8b55 },
3897 { 0x06, 0x0000 },
3898 { 0x05, 0x8b5e },
3899 { 0x06, 0x0000 },
3900 { 0x05, 0x8b67 },
3901 { 0x06, 0x0000 },
3902 { 0x05, 0x8b70 },
3903 { 0x06, 0x0000 },
3904 { 0x1f, 0x0000 },
3905 { 0x1f, 0x0007 },
3906 { 0x1e, 0x0078 },
3907 { 0x17, 0x0000 },
3908 { 0x19, 0x00aa },
3909 { 0x1f, 0x0000 },
3910
3911 /* Modify green table for 10M */
3912 { 0x1f, 0x0005 },
3913 { 0x05, 0x8b79 },
3914 { 0x06, 0xaa00 },
3915 { 0x1f, 0x0000 },
3916
3917 /* Disable hiimpedance detection (RTCT) */
3918 { 0x1f, 0x0003 },
3919 { 0x01, 0x328a },
3920 { 0x1f, 0x0000 }
3921 };
3922
3923
3924 rtl_apply_firmware(tp);
3925
3926 rtl8168f_hw_phy_config(tp);
3927
3928 /* Improve 2-pair detection performance */
3929 rtl_writephy(tp, 0x1f, 0x0005);
3930 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003931 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003932 rtl_writephy(tp, 0x1f, 0x0000);
3933
3934 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3935
3936 /* Modify green table for giga */
3937 rtl_writephy(tp, 0x1f, 0x0005);
3938 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003939 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003940 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003941 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003942 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003943 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003944 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003946 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003947 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003948 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003949 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003950 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003951 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003952 rtl_writephy(tp, 0x1f, 0x0000);
3953
3954 /* uc same-seed solution */
3955 rtl_writephy(tp, 0x1f, 0x0005);
3956 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003957 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003958 rtl_writephy(tp, 0x1f, 0x0000);
3959
3960 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003961 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003962 rtl_writephy(tp, 0x1f, 0x0005);
3963 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003964 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003965 rtl_writephy(tp, 0x1f, 0x0004);
3966 rtl_writephy(tp, 0x1f, 0x0007);
3967 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003968 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003969 rtl_writephy(tp, 0x1f, 0x0000);
3970 rtl_writephy(tp, 0x0d, 0x0007);
3971 rtl_writephy(tp, 0x0e, 0x003c);
3972 rtl_writephy(tp, 0x0d, 0x4007);
3973 rtl_writephy(tp, 0x0e, 0x0000);
3974 rtl_writephy(tp, 0x0d, 0x0000);
3975
3976 /* Green feature */
3977 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003978 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3979 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003980 rtl_writephy(tp, 0x1f, 0x0000);
3981}
3982
Hayes Wangc5583862012-07-02 17:23:22 +08003983static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3984{
Hayes Wangc5583862012-07-02 17:23:22 +08003985 rtl_apply_firmware(tp);
3986
hayeswang41f44d12013-04-01 22:23:36 +00003987 rtl_writephy(tp, 0x1f, 0x0a46);
3988 if (rtl_readphy(tp, 0x10) & 0x0100) {
3989 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003990 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003991 } else {
3992 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003993 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003994 }
Hayes Wangc5583862012-07-02 17:23:22 +08003995
hayeswang41f44d12013-04-01 22:23:36 +00003996 rtl_writephy(tp, 0x1f, 0x0a46);
3997 if (rtl_readphy(tp, 0x13) & 0x0100) {
3998 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003999 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004000 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00004001 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004002 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00004003 }
Hayes Wangc5583862012-07-02 17:23:22 +08004004
hayeswang41f44d12013-04-01 22:23:36 +00004005 /* Enable PHY auto speed down */
4006 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004007 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004008
hayeswangfe7524c2013-04-01 22:23:37 +00004009 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004010 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004011 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004012 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004013 rtl_writephy(tp, 0x1f, 0x0a43);
4014 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004015 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4016 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004017
hayeswang41f44d12013-04-01 22:23:36 +00004018 /* EEE auto-fallback function */
4019 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004020 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004021
hayeswang41f44d12013-04-01 22:23:36 +00004022 /* Enable UC LPF tune function */
4023 rtl_writephy(tp, 0x1f, 0x0a43);
4024 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004025 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004026
4027 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004028 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00004029
hayeswangfe7524c2013-04-01 22:23:37 +00004030 /* Improve SWR Efficiency */
4031 rtl_writephy(tp, 0x1f, 0x0bcd);
4032 rtl_writephy(tp, 0x14, 0x5065);
4033 rtl_writephy(tp, 0x14, 0xd065);
4034 rtl_writephy(tp, 0x1f, 0x0bc8);
4035 rtl_writephy(tp, 0x11, 0x5655);
4036 rtl_writephy(tp, 0x1f, 0x0bcd);
4037 rtl_writephy(tp, 0x14, 0x1065);
4038 rtl_writephy(tp, 0x14, 0x9065);
4039 rtl_writephy(tp, 0x14, 0x1065);
4040
David Chang1bac1072013-11-27 15:48:36 +08004041 /* Check ALDPS bit, disable it if enabled */
4042 rtl_writephy(tp, 0x1f, 0x0a43);
4043 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004044 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08004045
hayeswang41f44d12013-04-01 22:23:36 +00004046 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004047}
4048
hayeswang57538c42013-04-01 22:23:40 +00004049static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
4050{
4051 rtl_apply_firmware(tp);
4052}
4053
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004054static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
4055{
4056 u16 dout_tapbin;
4057 u32 data;
4058
4059 rtl_apply_firmware(tp);
4060
4061 /* CHN EST parameters adjust - giga master */
4062 rtl_writephy(tp, 0x1f, 0x0a43);
4063 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004064 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004065 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004066 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004067 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004068 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004069 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004070 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004071 rtl_writephy(tp, 0x1f, 0x0000);
4072
4073 /* CHN EST parameters adjust - giga slave */
4074 rtl_writephy(tp, 0x1f, 0x0a43);
4075 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004076 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004077 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004078 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004079 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004080 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004081 rtl_writephy(tp, 0x1f, 0x0000);
4082
4083 /* CHN EST parameters adjust - fnet */
4084 rtl_writephy(tp, 0x1f, 0x0a43);
4085 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004086 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004087 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004088 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004089 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004090 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004091 rtl_writephy(tp, 0x1f, 0x0000);
4092
4093 /* enable R-tune & PGA-retune function */
4094 dout_tapbin = 0;
4095 rtl_writephy(tp, 0x1f, 0x0a46);
4096 data = rtl_readphy(tp, 0x13);
4097 data &= 3;
4098 data <<= 2;
4099 dout_tapbin |= data;
4100 data = rtl_readphy(tp, 0x12);
4101 data &= 0xc000;
4102 data >>= 14;
4103 dout_tapbin |= data;
4104 dout_tapbin = ~(dout_tapbin^0x08);
4105 dout_tapbin <<= 12;
4106 dout_tapbin &= 0xf000;
4107 rtl_writephy(tp, 0x1f, 0x0a43);
4108 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004109 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004110 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004111 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004112 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004113 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004114 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004115 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004116
4117 rtl_writephy(tp, 0x1f, 0x0a43);
4118 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004119 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004120 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004121 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004122 rtl_writephy(tp, 0x1f, 0x0000);
4123
4124 /* enable GPHY 10M */
4125 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004126 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004127 rtl_writephy(tp, 0x1f, 0x0000);
4128
4129 /* SAR ADC performance */
4130 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004131 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004132 rtl_writephy(tp, 0x1f, 0x0000);
4133
4134 rtl_writephy(tp, 0x1f, 0x0a43);
4135 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004136 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004137 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004138 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004139 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004140 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004141 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004142 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004143 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004144 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004145 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004146 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004147 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004148 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004149 rtl_writephy(tp, 0x1f, 0x0000);
4150
4151 /* disable phy pfm mode */
4152 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004153 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004154 rtl_writephy(tp, 0x1f, 0x0000);
4155
4156 /* Check ALDPS bit, disable it if enabled */
4157 rtl_writephy(tp, 0x1f, 0x0a43);
4158 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004159 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004160
4161 rtl_writephy(tp, 0x1f, 0x0000);
4162}
4163
4164static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4165{
4166 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4167 u16 rlen;
4168 u32 data;
4169
4170 rtl_apply_firmware(tp);
4171
4172 /* CHIN EST parameter update */
4173 rtl_writephy(tp, 0x1f, 0x0a43);
4174 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004175 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004176 rtl_writephy(tp, 0x1f, 0x0000);
4177
4178 /* enable R-tune & PGA-retune function */
4179 rtl_writephy(tp, 0x1f, 0x0a43);
4180 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004181 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004182 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004183 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004184 rtl_writephy(tp, 0x1f, 0x0000);
4185
4186 /* enable GPHY 10M */
4187 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004188 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004189 rtl_writephy(tp, 0x1f, 0x0000);
4190
4191 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4192 data = r8168_mac_ocp_read(tp, 0xdd02);
4193 ioffset_p3 = ((data & 0x80)>>7);
4194 ioffset_p3 <<= 3;
4195
4196 data = r8168_mac_ocp_read(tp, 0xdd00);
4197 ioffset_p3 |= ((data & (0xe000))>>13);
4198 ioffset_p2 = ((data & (0x1e00))>>9);
4199 ioffset_p1 = ((data & (0x01e0))>>5);
4200 ioffset_p0 = ((data & 0x0010)>>4);
4201 ioffset_p0 <<= 3;
4202 ioffset_p0 |= (data & (0x07));
4203 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4204
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004205 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004206 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004207 rtl_writephy(tp, 0x1f, 0x0bcf);
4208 rtl_writephy(tp, 0x16, data);
4209 rtl_writephy(tp, 0x1f, 0x0000);
4210 }
4211
4212 /* Modify rlen (TX LPF corner frequency) level */
4213 rtl_writephy(tp, 0x1f, 0x0bcd);
4214 data = rtl_readphy(tp, 0x16);
4215 data &= 0x000f;
4216 rlen = 0;
4217 if (data > 3)
4218 rlen = data - 3;
4219 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4220 rtl_writephy(tp, 0x17, data);
4221 rtl_writephy(tp, 0x1f, 0x0bcd);
4222 rtl_writephy(tp, 0x1f, 0x0000);
4223
4224 /* disable phy pfm mode */
4225 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004226 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004227 rtl_writephy(tp, 0x1f, 0x0000);
4228
4229 /* Check ALDPS bit, disable it if enabled */
4230 rtl_writephy(tp, 0x1f, 0x0a43);
4231 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004232 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004233
4234 rtl_writephy(tp, 0x1f, 0x0000);
4235}
4236
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004237static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4238{
4239 /* Enable PHY auto speed down */
4240 rtl_writephy(tp, 0x1f, 0x0a44);
4241 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4242 rtl_writephy(tp, 0x1f, 0x0000);
4243
4244 /* patch 10M & ALDPS */
4245 rtl_writephy(tp, 0x1f, 0x0bcc);
4246 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4247 rtl_writephy(tp, 0x1f, 0x0a44);
4248 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4249 rtl_writephy(tp, 0x1f, 0x0a43);
4250 rtl_writephy(tp, 0x13, 0x8084);
4251 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4252 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4253 rtl_writephy(tp, 0x1f, 0x0000);
4254
4255 /* Enable EEE auto-fallback function */
4256 rtl_writephy(tp, 0x1f, 0x0a4b);
4257 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4258 rtl_writephy(tp, 0x1f, 0x0000);
4259
4260 /* Enable UC LPF tune function */
4261 rtl_writephy(tp, 0x1f, 0x0a43);
4262 rtl_writephy(tp, 0x13, 0x8012);
4263 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4264 rtl_writephy(tp, 0x1f, 0x0000);
4265
4266 /* set rg_sel_sdm_rate */
4267 rtl_writephy(tp, 0x1f, 0x0c42);
4268 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4269 rtl_writephy(tp, 0x1f, 0x0000);
4270
4271 /* Check ALDPS bit, disable it if enabled */
4272 rtl_writephy(tp, 0x1f, 0x0a43);
4273 if (rtl_readphy(tp, 0x10) & 0x0004)
4274 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4275
4276 rtl_writephy(tp, 0x1f, 0x0000);
4277}
4278
4279static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4280{
4281 /* patch 10M & ALDPS */
4282 rtl_writephy(tp, 0x1f, 0x0bcc);
4283 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4284 rtl_writephy(tp, 0x1f, 0x0a44);
4285 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4286 rtl_writephy(tp, 0x1f, 0x0a43);
4287 rtl_writephy(tp, 0x13, 0x8084);
4288 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4289 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4290 rtl_writephy(tp, 0x1f, 0x0000);
4291
4292 /* Enable UC LPF tune function */
4293 rtl_writephy(tp, 0x1f, 0x0a43);
4294 rtl_writephy(tp, 0x13, 0x8012);
4295 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4296 rtl_writephy(tp, 0x1f, 0x0000);
4297
4298 /* Set rg_sel_sdm_rate */
4299 rtl_writephy(tp, 0x1f, 0x0c42);
4300 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4301 rtl_writephy(tp, 0x1f, 0x0000);
4302
4303 /* Channel estimation parameters */
4304 rtl_writephy(tp, 0x1f, 0x0a43);
4305 rtl_writephy(tp, 0x13, 0x80f3);
4306 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4307 rtl_writephy(tp, 0x13, 0x80f0);
4308 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4309 rtl_writephy(tp, 0x13, 0x80ef);
4310 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4311 rtl_writephy(tp, 0x13, 0x80f6);
4312 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4313 rtl_writephy(tp, 0x13, 0x80ec);
4314 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4315 rtl_writephy(tp, 0x13, 0x80ed);
4316 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4317 rtl_writephy(tp, 0x13, 0x80f2);
4318 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4319 rtl_writephy(tp, 0x13, 0x80f4);
4320 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4321 rtl_writephy(tp, 0x1f, 0x0a43);
4322 rtl_writephy(tp, 0x13, 0x8110);
4323 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4324 rtl_writephy(tp, 0x13, 0x810f);
4325 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4326 rtl_writephy(tp, 0x13, 0x8111);
4327 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4328 rtl_writephy(tp, 0x13, 0x8113);
4329 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4330 rtl_writephy(tp, 0x13, 0x8115);
4331 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4332 rtl_writephy(tp, 0x13, 0x810e);
4333 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4334 rtl_writephy(tp, 0x13, 0x810c);
4335 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4336 rtl_writephy(tp, 0x13, 0x810b);
4337 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4338 rtl_writephy(tp, 0x1f, 0x0a43);
4339 rtl_writephy(tp, 0x13, 0x80d1);
4340 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4341 rtl_writephy(tp, 0x13, 0x80cd);
4342 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4343 rtl_writephy(tp, 0x13, 0x80d3);
4344 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4345 rtl_writephy(tp, 0x13, 0x80d5);
4346 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4347 rtl_writephy(tp, 0x13, 0x80d7);
4348 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4349
4350 /* Force PWM-mode */
4351 rtl_writephy(tp, 0x1f, 0x0bcd);
4352 rtl_writephy(tp, 0x14, 0x5065);
4353 rtl_writephy(tp, 0x14, 0xd065);
4354 rtl_writephy(tp, 0x1f, 0x0bc8);
4355 rtl_writephy(tp, 0x12, 0x00ed);
4356 rtl_writephy(tp, 0x1f, 0x0bcd);
4357 rtl_writephy(tp, 0x14, 0x1065);
4358 rtl_writephy(tp, 0x14, 0x9065);
4359 rtl_writephy(tp, 0x14, 0x1065);
4360 rtl_writephy(tp, 0x1f, 0x0000);
4361
4362 /* Check ALDPS bit, disable it if enabled */
4363 rtl_writephy(tp, 0x1f, 0x0a43);
4364 if (rtl_readphy(tp, 0x10) & 0x0004)
4365 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4366
4367 rtl_writephy(tp, 0x1f, 0x0000);
4368}
4369
françois romieu4da19632011-01-03 15:07:55 +00004370static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004371{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004372 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004373 { 0x1f, 0x0003 },
4374 { 0x08, 0x441d },
4375 { 0x01, 0x9100 },
4376 { 0x1f, 0x0000 }
4377 };
4378
françois romieu4da19632011-01-03 15:07:55 +00004379 rtl_writephy(tp, 0x1f, 0x0000);
4380 rtl_patchphy(tp, 0x11, 1 << 12);
4381 rtl_patchphy(tp, 0x19, 1 << 13);
4382 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004383
françois romieu4da19632011-01-03 15:07:55 +00004384 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004385}
4386
Hayes Wang5a5e4442011-02-22 17:26:21 +08004387static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4388{
4389 static const struct phy_reg phy_reg_init[] = {
4390 { 0x1f, 0x0005 },
4391 { 0x1a, 0x0000 },
4392 { 0x1f, 0x0000 },
4393
4394 { 0x1f, 0x0004 },
4395 { 0x1c, 0x0000 },
4396 { 0x1f, 0x0000 },
4397
4398 { 0x1f, 0x0001 },
4399 { 0x15, 0x7701 },
4400 { 0x1f, 0x0000 }
4401 };
4402
4403 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004404 rtl_writephy(tp, 0x1f, 0x0000);
4405 rtl_writephy(tp, 0x18, 0x0310);
4406 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004407
François Romieu953a12c2011-04-24 17:38:48 +02004408 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004409
4410 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4411}
4412
Hayes Wang7e18dca2012-03-30 14:33:02 +08004413static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4414{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004415 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004416 rtl_writephy(tp, 0x1f, 0x0000);
4417 rtl_writephy(tp, 0x18, 0x0310);
4418 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004419
4420 rtl_apply_firmware(tp);
4421
4422 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004423 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004424 rtl_writephy(tp, 0x1f, 0x0004);
4425 rtl_writephy(tp, 0x10, 0x401f);
4426 rtl_writephy(tp, 0x19, 0x7030);
4427 rtl_writephy(tp, 0x1f, 0x0000);
4428}
4429
Hayes Wang5598bfe2012-07-02 17:23:21 +08004430static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4431{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004432 static const struct phy_reg phy_reg_init[] = {
4433 { 0x1f, 0x0004 },
4434 { 0x10, 0xc07f },
4435 { 0x19, 0x7030 },
4436 { 0x1f, 0x0000 }
4437 };
4438
4439 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004440 rtl_writephy(tp, 0x1f, 0x0000);
4441 rtl_writephy(tp, 0x18, 0x0310);
4442 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004443
4444 rtl_apply_firmware(tp);
4445
Francois Romieufdf6fc02012-07-06 22:40:38 +02004446 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004447 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4448
Francois Romieufdf6fc02012-07-06 22:40:38 +02004449 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004450}
4451
Francois Romieu5615d9f2007-08-17 17:50:46 +02004452static void rtl_hw_phy_config(struct net_device *dev)
4453{
4454 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004455
4456 rtl8169_print_mac_version(tp);
4457
4458 switch (tp->mac_version) {
4459 case RTL_GIGA_MAC_VER_01:
4460 break;
4461 case RTL_GIGA_MAC_VER_02:
4462 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004463 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004464 break;
4465 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004466 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004467 break;
françois romieu2e9558562009-08-10 19:44:19 +00004468 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004469 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004470 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004471 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004472 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004473 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004474 case RTL_GIGA_MAC_VER_07:
4475 case RTL_GIGA_MAC_VER_08:
4476 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004477 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004478 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004479 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004480 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004481 break;
4482 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004483 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004484 break;
4485 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004486 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004487 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004488 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004489 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004490 break;
4491 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004492 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004493 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004494 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004495 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004496 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004497 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004498 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004499 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004500 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004501 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004502 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004503 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004504 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004505 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004506 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004507 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004508 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004509 break;
4510 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004511 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004512 break;
4513 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004514 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004515 break;
françois romieue6de30d2011-01-03 15:08:37 +00004516 case RTL_GIGA_MAC_VER_28:
4517 rtl8168d_4_hw_phy_config(tp);
4518 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004519 case RTL_GIGA_MAC_VER_29:
4520 case RTL_GIGA_MAC_VER_30:
4521 rtl8105e_hw_phy_config(tp);
4522 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004523 case RTL_GIGA_MAC_VER_31:
4524 /* None. */
4525 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004526 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004527 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004528 rtl8168e_1_hw_phy_config(tp);
4529 break;
4530 case RTL_GIGA_MAC_VER_34:
4531 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004532 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004533 case RTL_GIGA_MAC_VER_35:
4534 rtl8168f_1_hw_phy_config(tp);
4535 break;
4536 case RTL_GIGA_MAC_VER_36:
4537 rtl8168f_2_hw_phy_config(tp);
4538 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004539
Hayes Wang7e18dca2012-03-30 14:33:02 +08004540 case RTL_GIGA_MAC_VER_37:
4541 rtl8402_hw_phy_config(tp);
4542 break;
4543
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004544 case RTL_GIGA_MAC_VER_38:
4545 rtl8411_hw_phy_config(tp);
4546 break;
4547
Hayes Wang5598bfe2012-07-02 17:23:21 +08004548 case RTL_GIGA_MAC_VER_39:
4549 rtl8106e_hw_phy_config(tp);
4550 break;
4551
Hayes Wangc5583862012-07-02 17:23:22 +08004552 case RTL_GIGA_MAC_VER_40:
4553 rtl8168g_1_hw_phy_config(tp);
4554 break;
hayeswang57538c42013-04-01 22:23:40 +00004555 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004556 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004557 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004558 rtl8168g_2_hw_phy_config(tp);
4559 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004560 case RTL_GIGA_MAC_VER_45:
4561 case RTL_GIGA_MAC_VER_47:
4562 rtl8168h_1_hw_phy_config(tp);
4563 break;
4564 case RTL_GIGA_MAC_VER_46:
4565 case RTL_GIGA_MAC_VER_48:
4566 rtl8168h_2_hw_phy_config(tp);
4567 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004568
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004569 case RTL_GIGA_MAC_VER_49:
4570 rtl8168ep_1_hw_phy_config(tp);
4571 break;
4572 case RTL_GIGA_MAC_VER_50:
4573 case RTL_GIGA_MAC_VER_51:
4574 rtl8168ep_2_hw_phy_config(tp);
4575 break;
4576
Hayes Wangc5583862012-07-02 17:23:22 +08004577 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004578 default:
4579 break;
4580 }
4581}
4582
Francois Romieuda78dbf2012-01-26 14:18:23 +01004583static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 struct timer_list *timer = &tp->timer;
4586 void __iomem *ioaddr = tp->mmio_addr;
4587 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4588
Francois Romieubcf0bf92006-07-26 23:14:13 +02004589 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
françois romieu4da19632011-01-03 15:07:55 +00004591 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004592 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 * A busy loop could burn quite a few cycles on nowadays CPU.
4594 * Let's delay the execution of the timer for a few ticks.
4595 */
4596 timeout = HZ/10;
4597 goto out_mod_timer;
4598 }
4599
4600 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004603 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
françois romieu4da19632011-01-03 15:07:55 +00004605 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
4607out_mod_timer:
4608 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004609}
4610
4611static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4612{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004613 if (!test_and_set_bit(flag, tp->wk.flags))
4614 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004615}
4616
Kees Cook9de36cc2017-10-25 03:53:12 -07004617static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004618{
Kees Cook9de36cc2017-10-25 03:53:12 -07004619 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004620
Francois Romieu98ddf982012-01-31 10:47:34 +01004621 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622}
4623
Francois Romieuffc46952012-07-06 14:19:23 +02004624DECLARE_RTL_COND(rtl_phy_reset_cond)
4625{
4626 return tp->phy_reset_pending(tp);
4627}
4628
Francois Romieubf793292006-11-01 00:53:05 +01004629static void rtl8169_phy_reset(struct net_device *dev,
4630 struct rtl8169_private *tp)
4631{
françois romieu4da19632011-01-03 15:07:55 +00004632 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004633 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004634}
4635
David S. Miller8decf862011-09-22 03:23:13 -04004636static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4637{
4638 void __iomem *ioaddr = tp->mmio_addr;
4639
4640 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4641 (RTL_R8(PHYstatus) & TBI_Enable);
4642}
4643
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004644static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004646 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004647
Francois Romieu5615d9f2007-08-17 17:50:46 +02004648 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004649
Marcus Sundberg773328942008-07-10 21:28:08 +02004650 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4651 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4652 RTL_W8(0x82, 0x01);
4653 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004654
Francois Romieu6dccd162007-02-13 23:38:05 +01004655 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4656
4657 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4658 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004659
Francois Romieubcf0bf92006-07-26 23:14:13 +02004660 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004661 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4662 RTL_W8(0x82, 0x01);
4663 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004664 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004665 }
4666
Francois Romieubf793292006-11-01 00:53:05 +01004667 rtl8169_phy_reset(dev, tp);
4668
Oliver Neukum54405cd2011-01-06 21:55:13 +01004669 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004670 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4671 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4672 (tp->mii.supports_gmii ?
4673 ADVERTISED_1000baseT_Half |
4674 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004675
David S. Miller8decf862011-09-22 03:23:13 -04004676 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004677 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004678}
4679
Francois Romieu773d2022007-01-31 23:47:43 +01004680static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4681{
4682 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004683
Francois Romieuda78dbf2012-01-26 14:18:23 +01004684 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004685
4686 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004687
françois romieu9ecb9aa2012-12-07 11:20:21 +00004688 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004689 RTL_R32(MAC4);
4690
françois romieu9ecb9aa2012-12-07 11:20:21 +00004691 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004692 RTL_R32(MAC0);
4693
françois romieu9ecb9aa2012-12-07 11:20:21 +00004694 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4695 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004696
Francois Romieu773d2022007-01-31 23:47:43 +01004697 RTL_W8(Cfg9346, Cfg9346_Lock);
4698
Francois Romieuda78dbf2012-01-26 14:18:23 +01004699 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004700}
4701
4702static int rtl_set_mac_address(struct net_device *dev, void *p)
4703{
4704 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004705 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004706 struct sockaddr *addr = p;
4707
4708 if (!is_valid_ether_addr(addr->sa_data))
4709 return -EADDRNOTAVAIL;
4710
4711 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4712
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004713 pm_runtime_get_noresume(d);
4714
4715 if (pm_runtime_active(d))
4716 rtl_rar_set(tp, dev->dev_addr);
4717
4718 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004719
4720 return 0;
4721}
4722
Francois Romieu5f787a12006-08-17 13:02:36 +02004723static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4724{
4725 struct rtl8169_private *tp = netdev_priv(dev);
4726 struct mii_ioctl_data *data = if_mii(ifr);
4727
Francois Romieu8b4ab282008-11-19 22:05:25 -08004728 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4729}
Francois Romieu5f787a12006-08-17 13:02:36 +02004730
Francois Romieucecb5fd2011-04-01 10:21:07 +02004731static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4732 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004733{
Francois Romieu5f787a12006-08-17 13:02:36 +02004734 switch (cmd) {
4735 case SIOCGMIIPHY:
4736 data->phy_id = 32; /* Internal PHY */
4737 return 0;
4738
4739 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004740 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004741 return 0;
4742
4743 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004744 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004745 return 0;
4746 }
4747 return -EOPNOTSUPP;
4748}
4749
Francois Romieu8b4ab282008-11-19 22:05:25 -08004750static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4751{
4752 return -EOPNOTSUPP;
4753}
4754
Bill Pembertonbaf63292012-12-03 09:23:28 -05004755static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004756{
4757 struct mdio_ops *ops = &tp->mdio_ops;
4758
4759 switch (tp->mac_version) {
4760 case RTL_GIGA_MAC_VER_27:
4761 ops->write = r8168dp_1_mdio_write;
4762 ops->read = r8168dp_1_mdio_read;
4763 break;
françois romieue6de30d2011-01-03 15:08:37 +00004764 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004765 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004766 ops->write = r8168dp_2_mdio_write;
4767 ops->read = r8168dp_2_mdio_read;
4768 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004769 case RTL_GIGA_MAC_VER_40:
4770 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004771 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004772 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004773 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004774 case RTL_GIGA_MAC_VER_45:
4775 case RTL_GIGA_MAC_VER_46:
4776 case RTL_GIGA_MAC_VER_47:
4777 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004778 case RTL_GIGA_MAC_VER_49:
4779 case RTL_GIGA_MAC_VER_50:
4780 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004781 ops->write = r8168g_mdio_write;
4782 ops->read = r8168g_mdio_read;
4783 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004784 default:
4785 ops->write = r8169_mdio_write;
4786 ops->read = r8169_mdio_read;
4787 break;
4788 }
4789}
4790
hayeswange2409d82013-03-31 17:02:04 +00004791static void rtl_speed_down(struct rtl8169_private *tp)
4792{
4793 u32 adv;
4794 int lpa;
4795
4796 rtl_writephy(tp, 0x1f, 0x0000);
4797 lpa = rtl_readphy(tp, MII_LPA);
4798
4799 if (lpa & (LPA_10HALF | LPA_10FULL))
4800 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4801 else if (lpa & (LPA_100HALF | LPA_100FULL))
4802 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4803 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4804 else
4805 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4806 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4807 (tp->mii.supports_gmii ?
4808 ADVERTISED_1000baseT_Half |
4809 ADVERTISED_1000baseT_Full : 0);
4810
4811 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4812 adv);
4813}
4814
David S. Miller1805b2f2011-10-24 18:18:09 -04004815static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4816{
4817 void __iomem *ioaddr = tp->mmio_addr;
4818
4819 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004820 case RTL_GIGA_MAC_VER_25:
4821 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004822 case RTL_GIGA_MAC_VER_29:
4823 case RTL_GIGA_MAC_VER_30:
4824 case RTL_GIGA_MAC_VER_32:
4825 case RTL_GIGA_MAC_VER_33:
4826 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004827 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004828 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004829 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004830 case RTL_GIGA_MAC_VER_40:
4831 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004832 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004833 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004834 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004835 case RTL_GIGA_MAC_VER_45:
4836 case RTL_GIGA_MAC_VER_46:
4837 case RTL_GIGA_MAC_VER_47:
4838 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004839 case RTL_GIGA_MAC_VER_49:
4840 case RTL_GIGA_MAC_VER_50:
4841 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004842 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4843 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4844 break;
4845 default:
4846 break;
4847 }
4848}
4849
4850static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4851{
4852 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4853 return false;
4854
hayeswange2409d82013-03-31 17:02:04 +00004855 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004856 rtl_wol_suspend_quirk(tp);
4857
4858 return true;
4859}
4860
françois romieu065c27c2011-01-03 15:08:12 +00004861static void r810x_phy_power_down(struct rtl8169_private *tp)
4862{
4863 rtl_writephy(tp, 0x1f, 0x0000);
4864 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4865}
4866
4867static void r810x_phy_power_up(struct rtl8169_private *tp)
4868{
4869 rtl_writephy(tp, 0x1f, 0x0000);
4870 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4871}
4872
4873static void r810x_pll_power_down(struct rtl8169_private *tp)
4874{
Hayes Wang00042992012-03-30 14:33:00 +08004875 void __iomem *ioaddr = tp->mmio_addr;
4876
David S. Miller1805b2f2011-10-24 18:18:09 -04004877 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004878 return;
françois romieu065c27c2011-01-03 15:08:12 +00004879
4880 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004881
4882 switch (tp->mac_version) {
4883 case RTL_GIGA_MAC_VER_07:
4884 case RTL_GIGA_MAC_VER_08:
4885 case RTL_GIGA_MAC_VER_09:
4886 case RTL_GIGA_MAC_VER_10:
4887 case RTL_GIGA_MAC_VER_13:
4888 case RTL_GIGA_MAC_VER_16:
4889 break;
4890 default:
4891 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4892 break;
4893 }
françois romieu065c27c2011-01-03 15:08:12 +00004894}
4895
4896static void r810x_pll_power_up(struct rtl8169_private *tp)
4897{
Hayes Wang00042992012-03-30 14:33:00 +08004898 void __iomem *ioaddr = tp->mmio_addr;
4899
françois romieu065c27c2011-01-03 15:08:12 +00004900 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004901
4902 switch (tp->mac_version) {
4903 case RTL_GIGA_MAC_VER_07:
4904 case RTL_GIGA_MAC_VER_08:
4905 case RTL_GIGA_MAC_VER_09:
4906 case RTL_GIGA_MAC_VER_10:
4907 case RTL_GIGA_MAC_VER_13:
4908 case RTL_GIGA_MAC_VER_16:
4909 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004910 case RTL_GIGA_MAC_VER_47:
4911 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004912 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004913 break;
Hayes Wang00042992012-03-30 14:33:00 +08004914 default:
4915 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4916 break;
4917 }
françois romieu065c27c2011-01-03 15:08:12 +00004918}
4919
4920static void r8168_phy_power_up(struct rtl8169_private *tp)
4921{
4922 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004923 switch (tp->mac_version) {
4924 case RTL_GIGA_MAC_VER_11:
4925 case RTL_GIGA_MAC_VER_12:
4926 case RTL_GIGA_MAC_VER_17:
4927 case RTL_GIGA_MAC_VER_18:
4928 case RTL_GIGA_MAC_VER_19:
4929 case RTL_GIGA_MAC_VER_20:
4930 case RTL_GIGA_MAC_VER_21:
4931 case RTL_GIGA_MAC_VER_22:
4932 case RTL_GIGA_MAC_VER_23:
4933 case RTL_GIGA_MAC_VER_24:
4934 case RTL_GIGA_MAC_VER_25:
4935 case RTL_GIGA_MAC_VER_26:
4936 case RTL_GIGA_MAC_VER_27:
4937 case RTL_GIGA_MAC_VER_28:
4938 case RTL_GIGA_MAC_VER_31:
4939 rtl_writephy(tp, 0x0e, 0x0000);
4940 break;
4941 default:
4942 break;
4943 }
françois romieu065c27c2011-01-03 15:08:12 +00004944 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4945}
4946
4947static void r8168_phy_power_down(struct rtl8169_private *tp)
4948{
4949 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004950 switch (tp->mac_version) {
4951 case RTL_GIGA_MAC_VER_32:
4952 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004953 case RTL_GIGA_MAC_VER_40:
4954 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004955 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4956 break;
4957
4958 case RTL_GIGA_MAC_VER_11:
4959 case RTL_GIGA_MAC_VER_12:
4960 case RTL_GIGA_MAC_VER_17:
4961 case RTL_GIGA_MAC_VER_18:
4962 case RTL_GIGA_MAC_VER_19:
4963 case RTL_GIGA_MAC_VER_20:
4964 case RTL_GIGA_MAC_VER_21:
4965 case RTL_GIGA_MAC_VER_22:
4966 case RTL_GIGA_MAC_VER_23:
4967 case RTL_GIGA_MAC_VER_24:
4968 case RTL_GIGA_MAC_VER_25:
4969 case RTL_GIGA_MAC_VER_26:
4970 case RTL_GIGA_MAC_VER_27:
4971 case RTL_GIGA_MAC_VER_28:
4972 case RTL_GIGA_MAC_VER_31:
4973 rtl_writephy(tp, 0x0e, 0x0200);
4974 default:
4975 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4976 break;
4977 }
françois romieu065c27c2011-01-03 15:08:12 +00004978}
4979
4980static void r8168_pll_power_down(struct rtl8169_private *tp)
4981{
4982 void __iomem *ioaddr = tp->mmio_addr;
4983
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004984 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004985 return;
4986
Francois Romieucecb5fd2011-04-01 10:21:07 +02004987 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4988 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004989 (RTL_R16(CPlusCmd) & ASF)) {
4990 return;
4991 }
4992
hayeswang01dc7fe2011-03-21 01:50:28 +00004993 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4994 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004995 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004996
David S. Miller1805b2f2011-10-24 18:18:09 -04004997 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004998 return;
françois romieu065c27c2011-01-03 15:08:12 +00004999
5000 r8168_phy_power_down(tp);
5001
5002 switch (tp->mac_version) {
5003 case RTL_GIGA_MAC_VER_25:
5004 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005005 case RTL_GIGA_MAC_VER_27:
5006 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005007 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005008 case RTL_GIGA_MAC_VER_32:
5009 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005010 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005011 case RTL_GIGA_MAC_VER_45:
5012 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005013 case RTL_GIGA_MAC_VER_50:
5014 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005015 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5016 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005017 case RTL_GIGA_MAC_VER_40:
5018 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005019 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005020 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005021 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005022 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00005023 break;
françois romieu065c27c2011-01-03 15:08:12 +00005024 }
5025}
5026
5027static void r8168_pll_power_up(struct rtl8169_private *tp)
5028{
5029 void __iomem *ioaddr = tp->mmio_addr;
5030
françois romieu065c27c2011-01-03 15:08:12 +00005031 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:
françois romieu065c27c2011-01-03 15:08:12 +00005039 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5040 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005041 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005042 case RTL_GIGA_MAC_VER_45:
5043 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005044 case RTL_GIGA_MAC_VER_50:
5045 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005046 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005047 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005048 case RTL_GIGA_MAC_VER_40:
5049 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005050 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005051 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005052 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005053 0x00000000, ERIAR_EXGMAC);
5054 break;
françois romieu065c27c2011-01-03 15:08:12 +00005055 }
5056
5057 r8168_phy_power_up(tp);
5058}
5059
Francois Romieud58d46b2011-05-03 16:38:29 +02005060static void rtl_generic_op(struct rtl8169_private *tp,
5061 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00005062{
5063 if (op)
5064 op(tp);
5065}
5066
5067static void rtl_pll_power_down(struct rtl8169_private *tp)
5068{
Francois Romieud58d46b2011-05-03 16:38:29 +02005069 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00005070}
5071
5072static void rtl_pll_power_up(struct rtl8169_private *tp)
5073{
Francois Romieud58d46b2011-05-03 16:38:29 +02005074 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00005075}
5076
Bill Pembertonbaf63292012-12-03 09:23:28 -05005077static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00005078{
5079 struct pll_power_ops *ops = &tp->pll_power_ops;
5080
5081 switch (tp->mac_version) {
5082 case RTL_GIGA_MAC_VER_07:
5083 case RTL_GIGA_MAC_VER_08:
5084 case RTL_GIGA_MAC_VER_09:
5085 case RTL_GIGA_MAC_VER_10:
5086 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08005087 case RTL_GIGA_MAC_VER_29:
5088 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005089 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08005090 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00005091 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005092 case RTL_GIGA_MAC_VER_47:
5093 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005094 ops->down = r810x_pll_power_down;
5095 ops->up = r810x_pll_power_up;
5096 break;
5097
5098 case RTL_GIGA_MAC_VER_11:
5099 case RTL_GIGA_MAC_VER_12:
5100 case RTL_GIGA_MAC_VER_17:
5101 case RTL_GIGA_MAC_VER_18:
5102 case RTL_GIGA_MAC_VER_19:
5103 case RTL_GIGA_MAC_VER_20:
5104 case RTL_GIGA_MAC_VER_21:
5105 case RTL_GIGA_MAC_VER_22:
5106 case RTL_GIGA_MAC_VER_23:
5107 case RTL_GIGA_MAC_VER_24:
5108 case RTL_GIGA_MAC_VER_25:
5109 case RTL_GIGA_MAC_VER_26:
5110 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005111 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005112 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005113 case RTL_GIGA_MAC_VER_32:
5114 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005115 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005116 case RTL_GIGA_MAC_VER_35:
5117 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005118 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005119 case RTL_GIGA_MAC_VER_40:
5120 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005121 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005122 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005123 case RTL_GIGA_MAC_VER_45:
5124 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005125 case RTL_GIGA_MAC_VER_49:
5126 case RTL_GIGA_MAC_VER_50:
5127 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005128 ops->down = r8168_pll_power_down;
5129 ops->up = r8168_pll_power_up;
5130 break;
5131
5132 default:
5133 ops->down = NULL;
5134 ops->up = NULL;
5135 break;
5136 }
5137}
5138
Hayes Wange542a222011-07-06 15:58:04 +08005139static void rtl_init_rxcfg(struct rtl8169_private *tp)
5140{
5141 void __iomem *ioaddr = tp->mmio_addr;
5142
5143 switch (tp->mac_version) {
5144 case RTL_GIGA_MAC_VER_01:
5145 case RTL_GIGA_MAC_VER_02:
5146 case RTL_GIGA_MAC_VER_03:
5147 case RTL_GIGA_MAC_VER_04:
5148 case RTL_GIGA_MAC_VER_05:
5149 case RTL_GIGA_MAC_VER_06:
5150 case RTL_GIGA_MAC_VER_10:
5151 case RTL_GIGA_MAC_VER_11:
5152 case RTL_GIGA_MAC_VER_12:
5153 case RTL_GIGA_MAC_VER_13:
5154 case RTL_GIGA_MAC_VER_14:
5155 case RTL_GIGA_MAC_VER_15:
5156 case RTL_GIGA_MAC_VER_16:
5157 case RTL_GIGA_MAC_VER_17:
5158 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5159 break;
5160 case RTL_GIGA_MAC_VER_18:
5161 case RTL_GIGA_MAC_VER_19:
5162 case RTL_GIGA_MAC_VER_20:
5163 case RTL_GIGA_MAC_VER_21:
5164 case RTL_GIGA_MAC_VER_22:
5165 case RTL_GIGA_MAC_VER_23:
5166 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005167 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005168 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08005169 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5170 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005171 case RTL_GIGA_MAC_VER_40:
5172 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005173 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005174 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005175 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005176 case RTL_GIGA_MAC_VER_45:
5177 case RTL_GIGA_MAC_VER_46:
5178 case RTL_GIGA_MAC_VER_47:
5179 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005180 case RTL_GIGA_MAC_VER_49:
5181 case RTL_GIGA_MAC_VER_50:
5182 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02005183 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005184 break;
Hayes Wange542a222011-07-06 15:58:04 +08005185 default:
5186 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5187 break;
5188 }
5189}
5190
Hayes Wang92fc43b2011-07-06 15:58:03 +08005191static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5192{
Timo Teräs9fba0812013-01-15 21:01:24 +00005193 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005194}
5195
Francois Romieud58d46b2011-05-03 16:38:29 +02005196static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5197{
françois romieu9c5028e2012-03-02 04:43:14 +00005198 void __iomem *ioaddr = tp->mmio_addr;
5199
5200 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005201 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005202 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005203}
5204
5205static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5206{
françois romieu9c5028e2012-03-02 04:43:14 +00005207 void __iomem *ioaddr = tp->mmio_addr;
5208
5209 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005210 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005211 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005212}
5213
5214static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5215{
5216 void __iomem *ioaddr = tp->mmio_addr;
5217
5218 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5219 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005220 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005221}
5222
5223static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5224{
5225 void __iomem *ioaddr = tp->mmio_addr;
5226
5227 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5228 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5229 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5230}
5231
5232static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5233{
5234 void __iomem *ioaddr = tp->mmio_addr;
5235
5236 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5237}
5238
5239static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5240{
5241 void __iomem *ioaddr = tp->mmio_addr;
5242
5243 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5244}
5245
5246static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5247{
5248 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005249
5250 RTL_W8(MaxTxPacketSize, 0x3f);
5251 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5252 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005253 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005254}
5255
5256static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5257{
5258 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005259
5260 RTL_W8(MaxTxPacketSize, 0x0c);
5261 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5262 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005263 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005264}
5265
5266static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5267{
5268 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005269 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005270}
5271
5272static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5273{
5274 rtl_tx_performance_tweak(tp->pci_dev,
5275 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5276}
5277
5278static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5279{
5280 void __iomem *ioaddr = tp->mmio_addr;
5281
5282 r8168b_0_hw_jumbo_enable(tp);
5283
5284 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5285}
5286
5287static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5288{
5289 void __iomem *ioaddr = tp->mmio_addr;
5290
5291 r8168b_0_hw_jumbo_disable(tp);
5292
5293 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5294}
5295
Bill Pembertonbaf63292012-12-03 09:23:28 -05005296static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005297{
5298 struct jumbo_ops *ops = &tp->jumbo_ops;
5299
5300 switch (tp->mac_version) {
5301 case RTL_GIGA_MAC_VER_11:
5302 ops->disable = r8168b_0_hw_jumbo_disable;
5303 ops->enable = r8168b_0_hw_jumbo_enable;
5304 break;
5305 case RTL_GIGA_MAC_VER_12:
5306 case RTL_GIGA_MAC_VER_17:
5307 ops->disable = r8168b_1_hw_jumbo_disable;
5308 ops->enable = r8168b_1_hw_jumbo_enable;
5309 break;
5310 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5311 case RTL_GIGA_MAC_VER_19:
5312 case RTL_GIGA_MAC_VER_20:
5313 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5314 case RTL_GIGA_MAC_VER_22:
5315 case RTL_GIGA_MAC_VER_23:
5316 case RTL_GIGA_MAC_VER_24:
5317 case RTL_GIGA_MAC_VER_25:
5318 case RTL_GIGA_MAC_VER_26:
5319 ops->disable = r8168c_hw_jumbo_disable;
5320 ops->enable = r8168c_hw_jumbo_enable;
5321 break;
5322 case RTL_GIGA_MAC_VER_27:
5323 case RTL_GIGA_MAC_VER_28:
5324 ops->disable = r8168dp_hw_jumbo_disable;
5325 ops->enable = r8168dp_hw_jumbo_enable;
5326 break;
5327 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5328 case RTL_GIGA_MAC_VER_32:
5329 case RTL_GIGA_MAC_VER_33:
5330 case RTL_GIGA_MAC_VER_34:
5331 ops->disable = r8168e_hw_jumbo_disable;
5332 ops->enable = r8168e_hw_jumbo_enable;
5333 break;
5334
5335 /*
5336 * No action needed for jumbo frames with 8169.
5337 * No jumbo for 810x at all.
5338 */
Hayes Wangc5583862012-07-02 17:23:22 +08005339 case RTL_GIGA_MAC_VER_40:
5340 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005341 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005342 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005343 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005344 case RTL_GIGA_MAC_VER_45:
5345 case RTL_GIGA_MAC_VER_46:
5346 case RTL_GIGA_MAC_VER_47:
5347 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005348 case RTL_GIGA_MAC_VER_49:
5349 case RTL_GIGA_MAC_VER_50:
5350 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005351 default:
5352 ops->disable = NULL;
5353 ops->enable = NULL;
5354 break;
5355 }
5356}
5357
Francois Romieuffc46952012-07-06 14:19:23 +02005358DECLARE_RTL_COND(rtl_chipcmd_cond)
5359{
5360 void __iomem *ioaddr = tp->mmio_addr;
5361
5362 return RTL_R8(ChipCmd) & CmdReset;
5363}
5364
Francois Romieu6f43adc2011-04-29 15:05:51 +02005365static void rtl_hw_reset(struct rtl8169_private *tp)
5366{
5367 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005368
Francois Romieu6f43adc2011-04-29 15:05:51 +02005369 RTL_W8(ChipCmd, CmdReset);
5370
Francois Romieuffc46952012-07-06 14:19:23 +02005371 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005372}
5373
Francois Romieub6ffd972011-06-17 17:00:05 +02005374static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5375{
5376 struct rtl_fw *rtl_fw;
5377 const char *name;
5378 int rc = -ENOMEM;
5379
5380 name = rtl_lookup_firmware_name(tp);
5381 if (!name)
5382 goto out_no_firmware;
5383
5384 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5385 if (!rtl_fw)
5386 goto err_warn;
5387
5388 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5389 if (rc < 0)
5390 goto err_free;
5391
Francois Romieufd112f22011-06-18 00:10:29 +02005392 rc = rtl_check_firmware(tp, rtl_fw);
5393 if (rc < 0)
5394 goto err_release_firmware;
5395
Francois Romieub6ffd972011-06-17 17:00:05 +02005396 tp->rtl_fw = rtl_fw;
5397out:
5398 return;
5399
Francois Romieufd112f22011-06-18 00:10:29 +02005400err_release_firmware:
5401 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005402err_free:
5403 kfree(rtl_fw);
5404err_warn:
5405 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5406 name, rc);
5407out_no_firmware:
5408 tp->rtl_fw = NULL;
5409 goto out;
5410}
5411
François Romieu953a12c2011-04-24 17:38:48 +02005412static void rtl_request_firmware(struct rtl8169_private *tp)
5413{
Francois Romieub6ffd972011-06-17 17:00:05 +02005414 if (IS_ERR(tp->rtl_fw))
5415 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005416}
5417
Hayes Wang92fc43b2011-07-06 15:58:03 +08005418static void rtl_rx_close(struct rtl8169_private *tp)
5419{
5420 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005421
Francois Romieu1687b562011-07-19 17:21:29 +02005422 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005423}
5424
Francois Romieuffc46952012-07-06 14:19:23 +02005425DECLARE_RTL_COND(rtl_npq_cond)
5426{
5427 void __iomem *ioaddr = tp->mmio_addr;
5428
5429 return RTL_R8(TxPoll) & NPQ;
5430}
5431
5432DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5433{
5434 void __iomem *ioaddr = tp->mmio_addr;
5435
5436 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5437}
5438
françois romieue6de30d2011-01-03 15:08:37 +00005439static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
françois romieue6de30d2011-01-03 15:08:37 +00005441 void __iomem *ioaddr = tp->mmio_addr;
5442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005444 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Hayes Wang92fc43b2011-07-06 15:58:03 +08005446 rtl_rx_close(tp);
5447
Hayes Wang5d2e1952011-02-22 17:26:22 +08005448 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005449 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5450 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005451 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005452 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005453 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5454 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5455 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5456 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5457 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5458 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5459 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5460 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5461 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5462 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5463 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5464 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005465 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5466 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5467 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5468 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005469 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005470 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005471 } else {
5472 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5473 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005474 }
5475
Hayes Wang92fc43b2011-07-06 15:58:03 +08005476 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477}
5478
Francois Romieu7f796d832007-06-11 23:04:41 +02005479static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005480{
5481 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005482
5483 /* Set DMA burst size and Interframe Gap Time */
5484 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5485 (InterFrameGap << TxInterFrameGapShift));
5486}
5487
Francois Romieu07ce4062007-02-23 23:36:39 +01005488static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489{
5490 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Francois Romieu07ce4062007-02-23 23:36:39 +01005492 tp->hw_start(dev);
5493
Francois Romieuda78dbf2012-01-26 14:18:23 +01005494 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005495}
5496
Francois Romieu7f796d832007-06-11 23:04:41 +02005497static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5498 void __iomem *ioaddr)
5499{
5500 /*
5501 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5502 * register to be written before TxDescAddrLow to work.
5503 * Switching from MMIO to I/O access fixes the issue as well.
5504 */
5505 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005506 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005507 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005508 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005509}
5510
5511static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5512{
5513 u16 cmd;
5514
5515 cmd = RTL_R16(CPlusCmd);
5516 RTL_W16(CPlusCmd, cmd);
5517 return cmd;
5518}
5519
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005520static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005521{
5522 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005523 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005524}
5525
Francois Romieu6dccd162007-02-13 23:38:05 +01005526static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5527{
Francois Romieu37441002011-06-17 22:58:54 +02005528 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005529 u32 mac_version;
5530 u32 clk;
5531 u32 val;
5532 } cfg2_info [] = {
5533 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5534 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5535 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5536 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005537 };
5538 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005539 unsigned int i;
5540 u32 clk;
5541
5542 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005543 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005544 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5545 RTL_W32(0x7c, p->val);
5546 break;
5547 }
5548 }
5549}
5550
Francois Romieue6b763e2012-03-08 09:35:39 +01005551static void rtl_set_rx_mode(struct net_device *dev)
5552{
5553 struct rtl8169_private *tp = netdev_priv(dev);
5554 void __iomem *ioaddr = tp->mmio_addr;
5555 u32 mc_filter[2]; /* Multicast hash filter */
5556 int rx_mode;
5557 u32 tmp = 0;
5558
5559 if (dev->flags & IFF_PROMISC) {
5560 /* Unconditionally log net taps. */
5561 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5562 rx_mode =
5563 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5564 AcceptAllPhys;
5565 mc_filter[1] = mc_filter[0] = 0xffffffff;
5566 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5567 (dev->flags & IFF_ALLMULTI)) {
5568 /* Too many to filter perfectly -- accept all multicasts. */
5569 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5570 mc_filter[1] = mc_filter[0] = 0xffffffff;
5571 } else {
5572 struct netdev_hw_addr *ha;
5573
5574 rx_mode = AcceptBroadcast | AcceptMyPhys;
5575 mc_filter[1] = mc_filter[0] = 0;
5576 netdev_for_each_mc_addr(ha, dev) {
5577 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5578 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5579 rx_mode |= AcceptMulticast;
5580 }
5581 }
5582
5583 if (dev->features & NETIF_F_RXALL)
5584 rx_mode |= (AcceptErr | AcceptRunt);
5585
5586 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5587
5588 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5589 u32 data = mc_filter[0];
5590
5591 mc_filter[0] = swab32(mc_filter[1]);
5592 mc_filter[1] = swab32(data);
5593 }
5594
Nathan Walp04817762012-11-01 12:08:47 +00005595 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5596 mc_filter[1] = mc_filter[0] = 0xffffffff;
5597
Francois Romieue6b763e2012-03-08 09:35:39 +01005598 RTL_W32(MAR0 + 4, mc_filter[1]);
5599 RTL_W32(MAR0 + 0, mc_filter[0]);
5600
5601 RTL_W32(RxConfig, tmp);
5602}
5603
Francois Romieu07ce4062007-02-23 23:36:39 +01005604static void rtl_hw_start_8169(struct net_device *dev)
5605{
5606 struct rtl8169_private *tp = netdev_priv(dev);
5607 void __iomem *ioaddr = tp->mmio_addr;
5608 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005609
Francois Romieu9cb427b2006-11-02 00:10:16 +01005610 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5611 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5612 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5613 }
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005616 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5617 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5618 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5619 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005620 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5621
Hayes Wange542a222011-07-06 15:58:04 +08005622 rtl_init_rxcfg(tp);
5623
françois romieuf0298f82011-01-03 15:07:42 +00005624 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005626 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Francois Romieucecb5fd2011-04-01 10:21:07 +02005628 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5629 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5630 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5631 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005632 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
Francois Romieu7f796d832007-06-11 23:04:41 +02005634 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005635
Francois Romieucecb5fd2011-04-01 10:21:07 +02005636 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5637 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005638 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005640 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 }
5642
Francois Romieubcf0bf92006-07-26 23:14:13 +02005643 RTL_W16(CPlusCmd, tp->cp_cmd);
5644
Francois Romieu6dccd162007-02-13 23:38:05 +01005645 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 /*
5648 * Undocumented corner. Supposedly:
5649 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5650 */
5651 RTL_W16(IntrMitigate, 0x0000);
5652
Francois Romieu7f796d832007-06-11 23:04:41 +02005653 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005654
Francois Romieucecb5fd2011-04-01 10:21:07 +02005655 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5656 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5657 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5658 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005659 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5660 rtl_set_rx_tx_config_registers(tp);
5661 }
5662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005664
5665 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5666 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668 RTL_W32(RxMissed, 0);
5669
Francois Romieu07ce4062007-02-23 23:36:39 +01005670 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
5672 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005673 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005676static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5677{
5678 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005679 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005680}
5681
5682static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5683{
Francois Romieu52989f02012-07-06 13:37:00 +02005684 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005685}
5686
5687static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005688{
5689 u32 csi;
5690
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005691 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5692 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005693}
5694
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005695static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005696{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005697 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005698}
5699
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005700static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005701{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005702 rtl_csi_access_enable(tp, 0x27000000);
5703}
5704
Francois Romieuffc46952012-07-06 14:19:23 +02005705DECLARE_RTL_COND(rtl_csiar_cond)
5706{
5707 void __iomem *ioaddr = tp->mmio_addr;
5708
5709 return RTL_R32(CSIAR) & CSIAR_FLAG;
5710}
5711
Francois Romieu52989f02012-07-06 13:37:00 +02005712static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005713{
Francois Romieu52989f02012-07-06 13:37:00 +02005714 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005715
5716 RTL_W32(CSIDR, value);
5717 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5718 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5719
Francois Romieuffc46952012-07-06 14:19:23 +02005720 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005721}
5722
Francois Romieu52989f02012-07-06 13:37:00 +02005723static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005724{
Francois Romieu52989f02012-07-06 13:37:00 +02005725 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005726
5727 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5728 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5729
Francois Romieuffc46952012-07-06 14:19:23 +02005730 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5731 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005732}
5733
Francois Romieu52989f02012-07-06 13:37:00 +02005734static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005735{
Francois Romieu52989f02012-07-06 13:37:00 +02005736 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005737
5738 RTL_W32(CSIDR, value);
5739 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5740 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5741 CSIAR_FUNC_NIC);
5742
Francois Romieuffc46952012-07-06 14:19:23 +02005743 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005744}
5745
Francois Romieu52989f02012-07-06 13:37:00 +02005746static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005747{
Francois Romieu52989f02012-07-06 13:37:00 +02005748 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005749
5750 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5751 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5752
Francois Romieuffc46952012-07-06 14:19:23 +02005753 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5754 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005755}
5756
hayeswang45dd95c2013-07-08 17:09:01 +08005757static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5758{
5759 void __iomem *ioaddr = tp->mmio_addr;
5760
5761 RTL_W32(CSIDR, value);
5762 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5763 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5764 CSIAR_FUNC_NIC2);
5765
5766 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5767}
5768
5769static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5770{
5771 void __iomem *ioaddr = tp->mmio_addr;
5772
5773 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5774 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5775
5776 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5777 RTL_R32(CSIDR) : ~0;
5778}
5779
Bill Pembertonbaf63292012-12-03 09:23:28 -05005780static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005781{
5782 struct csi_ops *ops = &tp->csi_ops;
5783
5784 switch (tp->mac_version) {
5785 case RTL_GIGA_MAC_VER_01:
5786 case RTL_GIGA_MAC_VER_02:
5787 case RTL_GIGA_MAC_VER_03:
5788 case RTL_GIGA_MAC_VER_04:
5789 case RTL_GIGA_MAC_VER_05:
5790 case RTL_GIGA_MAC_VER_06:
5791 case RTL_GIGA_MAC_VER_10:
5792 case RTL_GIGA_MAC_VER_11:
5793 case RTL_GIGA_MAC_VER_12:
5794 case RTL_GIGA_MAC_VER_13:
5795 case RTL_GIGA_MAC_VER_14:
5796 case RTL_GIGA_MAC_VER_15:
5797 case RTL_GIGA_MAC_VER_16:
5798 case RTL_GIGA_MAC_VER_17:
5799 ops->write = NULL;
5800 ops->read = NULL;
5801 break;
5802
Hayes Wang7e18dca2012-03-30 14:33:02 +08005803 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005804 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005805 ops->write = r8402_csi_write;
5806 ops->read = r8402_csi_read;
5807 break;
5808
hayeswang45dd95c2013-07-08 17:09:01 +08005809 case RTL_GIGA_MAC_VER_44:
5810 ops->write = r8411_csi_write;
5811 ops->read = r8411_csi_read;
5812 break;
5813
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005814 default:
5815 ops->write = r8169_csi_write;
5816 ops->read = r8169_csi_read;
5817 break;
5818 }
Francois Romieudacf8152008-08-02 20:44:13 +02005819}
5820
5821struct ephy_info {
5822 unsigned int offset;
5823 u16 mask;
5824 u16 bits;
5825};
5826
Francois Romieufdf6fc02012-07-06 22:40:38 +02005827static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5828 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005829{
5830 u16 w;
5831
5832 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005833 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5834 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005835 e++;
5836 }
5837}
5838
Francois Romieub726e492008-06-28 12:22:59 +02005839static void rtl_disable_clock_request(struct pci_dev *pdev)
5840{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005841 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5842 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005843}
5844
françois romieue6de30d2011-01-03 15:08:37 +00005845static void rtl_enable_clock_request(struct pci_dev *pdev)
5846{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005847 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5848 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005849}
5850
hayeswangb51ecea2014-07-09 14:52:51 +08005851static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5852{
5853 void __iomem *ioaddr = tp->mmio_addr;
5854 u8 data;
5855
5856 data = RTL_R8(Config3);
5857
5858 if (enable)
5859 data |= Rdy_to_L23;
5860 else
5861 data &= ~Rdy_to_L23;
5862
5863 RTL_W8(Config3, data);
5864}
5865
Francois Romieub726e492008-06-28 12:22:59 +02005866#define R8168_CPCMD_QUIRK_MASK (\
5867 EnableBist | \
5868 Mac_dbgo_oe | \
5869 Force_half_dup | \
5870 Force_rxflow_en | \
5871 Force_txflow_en | \
5872 Cxpl_dbg_sel | \
5873 ASF | \
5874 PktCntrDisable | \
5875 Mac_dbgo_sel)
5876
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005877static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005878{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005879 void __iomem *ioaddr = tp->mmio_addr;
5880 struct pci_dev *pdev = tp->pci_dev;
5881
Francois Romieub726e492008-06-28 12:22:59 +02005882 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5883
5884 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5885
françois romieufaf1e782013-02-27 13:01:57 +00005886 if (tp->dev->mtu <= ETH_DATA_LEN) {
5887 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5888 PCI_EXP_DEVCTL_NOSNOOP_EN);
5889 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005890}
5891
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005892static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005893{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005894 void __iomem *ioaddr = tp->mmio_addr;
5895
5896 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005897
françois romieuf0298f82011-01-03 15:07:42 +00005898 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005899
5900 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005901}
5902
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005903static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005904{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005905 void __iomem *ioaddr = tp->mmio_addr;
5906 struct pci_dev *pdev = tp->pci_dev;
5907
Francois Romieub726e492008-06-28 12:22:59 +02005908 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5909
5910 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5911
françois romieufaf1e782013-02-27 13:01:57 +00005912 if (tp->dev->mtu <= ETH_DATA_LEN)
5913 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005914
5915 rtl_disable_clock_request(pdev);
5916
5917 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005918}
5919
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005920static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005921{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005922 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005923 { 0x01, 0, 0x0001 },
5924 { 0x02, 0x0800, 0x1000 },
5925 { 0x03, 0, 0x0042 },
5926 { 0x06, 0x0080, 0x0000 },
5927 { 0x07, 0, 0x2000 }
5928 };
5929
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005930 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005931
Francois Romieufdf6fc02012-07-06 22:40:38 +02005932 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005933
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005934 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005935}
5936
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005937static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005938{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005939 void __iomem *ioaddr = tp->mmio_addr;
5940 struct pci_dev *pdev = tp->pci_dev;
5941
5942 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005943
5944 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5945
françois romieufaf1e782013-02-27 13:01:57 +00005946 if (tp->dev->mtu <= ETH_DATA_LEN)
5947 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005948
5949 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5950}
5951
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005952static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005953{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005954 void __iomem *ioaddr = tp->mmio_addr;
5955 struct pci_dev *pdev = tp->pci_dev;
5956
5957 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005958
5959 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5960
5961 /* Magic. */
5962 RTL_W8(DBG_REG, 0x20);
5963
françois romieuf0298f82011-01-03 15:07:42 +00005964 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005965
françois romieufaf1e782013-02-27 13:01:57 +00005966 if (tp->dev->mtu <= ETH_DATA_LEN)
5967 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005968
5969 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5970}
5971
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005972static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005973{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005974 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005975 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005976 { 0x02, 0x0800, 0x1000 },
5977 { 0x03, 0, 0x0002 },
5978 { 0x06, 0x0080, 0x0000 }
5979 };
5980
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005981 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005982
5983 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5984
Francois Romieufdf6fc02012-07-06 22:40:38 +02005985 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005986
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005987 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005988}
5989
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005990static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005991{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005992 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005993 { 0x01, 0, 0x0001 },
5994 { 0x03, 0x0400, 0x0220 }
5995 };
5996
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005997 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005998
Francois Romieufdf6fc02012-07-06 22:40:38 +02005999 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02006000
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006001 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006002}
6003
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006004static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02006005{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006006 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02006007}
6008
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006009static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02006010{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006011 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006012
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006013 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006014}
6015
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006016static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02006017{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006018 void __iomem *ioaddr = tp->mmio_addr;
6019 struct pci_dev *pdev = tp->pci_dev;
6020
6021 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02006022
6023 rtl_disable_clock_request(pdev);
6024
françois romieuf0298f82011-01-03 15:07:42 +00006025 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02006026
françois romieufaf1e782013-02-27 13:01:57 +00006027 if (tp->dev->mtu <= ETH_DATA_LEN)
6028 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02006029
6030 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6031}
6032
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006033static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00006034{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006035 void __iomem *ioaddr = tp->mmio_addr;
6036 struct pci_dev *pdev = tp->pci_dev;
6037
6038 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006039
françois romieufaf1e782013-02-27 13:01:57 +00006040 if (tp->dev->mtu <= ETH_DATA_LEN)
6041 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00006042
6043 RTL_W8(MaxTxPacketSize, TxPacketMax);
6044
6045 rtl_disable_clock_request(pdev);
6046}
6047
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006048static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00006049{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006050 void __iomem *ioaddr = tp->mmio_addr;
6051 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00006052 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006053 { 0x0b, 0x0000, 0x0048 },
6054 { 0x19, 0x0020, 0x0050 },
6055 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00006056 };
françois romieue6de30d2011-01-03 15:08:37 +00006057
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006058 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00006059
6060 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6061
6062 RTL_W8(MaxTxPacketSize, TxPacketMax);
6063
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006064 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00006065
6066 rtl_enable_clock_request(pdev);
6067}
6068
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006069static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00006070{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006071 void __iomem *ioaddr = tp->mmio_addr;
6072 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006073 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00006074 { 0x00, 0x0200, 0x0100 },
6075 { 0x00, 0x0000, 0x0004 },
6076 { 0x06, 0x0002, 0x0001 },
6077 { 0x06, 0x0000, 0x0030 },
6078 { 0x07, 0x0000, 0x2000 },
6079 { 0x00, 0x0000, 0x0020 },
6080 { 0x03, 0x5800, 0x2000 },
6081 { 0x03, 0x0000, 0x0001 },
6082 { 0x01, 0x0800, 0x1000 },
6083 { 0x07, 0x0000, 0x4000 },
6084 { 0x1e, 0x0000, 0x2000 },
6085 { 0x19, 0xffff, 0xfe6c },
6086 { 0x0a, 0x0000, 0x0040 }
6087 };
6088
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006089 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006090
Francois Romieufdf6fc02012-07-06 22:40:38 +02006091 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00006092
françois romieufaf1e782013-02-27 13:01:57 +00006093 if (tp->dev->mtu <= ETH_DATA_LEN)
6094 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00006095
6096 RTL_W8(MaxTxPacketSize, TxPacketMax);
6097
6098 rtl_disable_clock_request(pdev);
6099
6100 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02006101 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6102 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00006103
Francois Romieucecb5fd2011-04-01 10:21:07 +02006104 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00006105}
6106
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006107static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08006108{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006109 void __iomem *ioaddr = tp->mmio_addr;
6110 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006111 static const struct ephy_info e_info_8168e_2[] = {
6112 { 0x09, 0x0000, 0x0080 },
6113 { 0x19, 0x0000, 0x0224 }
6114 };
6115
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006116 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006117
Francois Romieufdf6fc02012-07-06 22:40:38 +02006118 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08006119
françois romieufaf1e782013-02-27 13:01:57 +00006120 if (tp->dev->mtu <= ETH_DATA_LEN)
6121 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08006122
Francois Romieufdf6fc02012-07-06 22:40:38 +02006123 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6124 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6125 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6126 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6127 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6128 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006129 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6130 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08006131
Hayes Wang3090bd92011-09-06 16:55:15 +08006132 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08006133
Francois Romieu4521e1a92012-11-01 16:46:28 +00006134 rtl_disable_clock_request(pdev);
6135
Hayes Wang70090422011-07-06 15:58:06 +08006136 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6137 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6138
6139 /* Adjust EEE LED frequency */
6140 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6141
6142 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6143 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006144 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08006145}
6146
Hayes Wang5f886e02012-03-30 14:33:03 +08006147static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08006148{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006149 void __iomem *ioaddr = tp->mmio_addr;
6150 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08006151
Hayes Wang5f886e02012-03-30 14:33:03 +08006152 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006153
6154 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6155
Francois Romieufdf6fc02012-07-06 22:40:38 +02006156 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6157 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6158 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6159 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006160 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6161 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6162 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6163 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006164 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6165 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006166
6167 RTL_W8(MaxTxPacketSize, EarlySize);
6168
Francois Romieu4521e1a92012-11-01 16:46:28 +00006169 rtl_disable_clock_request(pdev);
6170
Hayes Wangc2218922011-09-06 16:55:18 +08006171 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6172 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08006173 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006174 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6175 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006176}
6177
Hayes Wang5f886e02012-03-30 14:33:03 +08006178static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6179{
6180 void __iomem *ioaddr = tp->mmio_addr;
6181 static const struct ephy_info e_info_8168f_1[] = {
6182 { 0x06, 0x00c0, 0x0020 },
6183 { 0x08, 0x0001, 0x0002 },
6184 { 0x09, 0x0000, 0x0080 },
6185 { 0x19, 0x0000, 0x0224 }
6186 };
6187
6188 rtl_hw_start_8168f(tp);
6189
Francois Romieufdf6fc02012-07-06 22:40:38 +02006190 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006191
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006192 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006193
6194 /* Adjust EEE LED frequency */
6195 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6196}
6197
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006198static void rtl_hw_start_8411(struct rtl8169_private *tp)
6199{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006200 static const struct ephy_info e_info_8168f_1[] = {
6201 { 0x06, 0x00c0, 0x0020 },
6202 { 0x0f, 0xffff, 0x5200 },
6203 { 0x1e, 0x0000, 0x4000 },
6204 { 0x19, 0x0000, 0x0224 }
6205 };
6206
6207 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006208 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006209
Francois Romieufdf6fc02012-07-06 22:40:38 +02006210 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006211
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006212 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006213}
6214
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006215static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006216{
6217 void __iomem *ioaddr = tp->mmio_addr;
6218 struct pci_dev *pdev = tp->pci_dev;
6219
hayeswangbeb330a2013-04-01 22:23:39 +00006220 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6221
Hayes Wangc5583862012-07-02 17:23:22 +08006222 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6223 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6224 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6225 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6226
6227 rtl_csi_access_enable_1(tp);
6228
6229 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6230
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006231 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6232 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006233 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006234
Francois Romieu4521e1a92012-11-01 16:46:28 +00006235 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006236 RTL_W8(MaxTxPacketSize, EarlySize);
6237
6238 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6239 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6240
6241 /* Adjust EEE LED frequency */
6242 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6243
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006244 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6245 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006246
6247 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006248}
6249
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006250static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6251{
6252 void __iomem *ioaddr = tp->mmio_addr;
6253 static const struct ephy_info e_info_8168g_1[] = {
6254 { 0x00, 0x0000, 0x0008 },
6255 { 0x0c, 0x37d0, 0x0820 },
6256 { 0x1e, 0x0000, 0x0001 },
6257 { 0x19, 0x8000, 0x0000 }
6258 };
6259
6260 rtl_hw_start_8168g(tp);
6261
6262 /* disable aspm and clock request before access ephy */
6263 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6264 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6265 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6266}
6267
hayeswang57538c42013-04-01 22:23:40 +00006268static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6269{
6270 void __iomem *ioaddr = tp->mmio_addr;
6271 static const struct ephy_info e_info_8168g_2[] = {
6272 { 0x00, 0x0000, 0x0008 },
6273 { 0x0c, 0x3df0, 0x0200 },
6274 { 0x19, 0xffff, 0xfc00 },
6275 { 0x1e, 0xffff, 0x20eb }
6276 };
6277
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006278 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006279
6280 /* disable aspm and clock request before access ephy */
6281 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6282 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6283 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6284}
6285
hayeswang45dd95c2013-07-08 17:09:01 +08006286static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6287{
6288 void __iomem *ioaddr = tp->mmio_addr;
6289 static const struct ephy_info e_info_8411_2[] = {
6290 { 0x00, 0x0000, 0x0008 },
6291 { 0x0c, 0x3df0, 0x0200 },
6292 { 0x0f, 0xffff, 0x5200 },
6293 { 0x19, 0x0020, 0x0000 },
6294 { 0x1e, 0x0000, 0x2000 }
6295 };
6296
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006297 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006298
6299 /* disable aspm and clock request before access ephy */
6300 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6301 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6302 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6303}
6304
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006305static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6306{
6307 void __iomem *ioaddr = tp->mmio_addr;
6308 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006309 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006310 u32 data;
6311 static const struct ephy_info e_info_8168h_1[] = {
6312 { 0x1e, 0x0800, 0x0001 },
6313 { 0x1d, 0x0000, 0x0800 },
6314 { 0x05, 0xffff, 0x2089 },
6315 { 0x06, 0xffff, 0x5881 },
6316 { 0x04, 0xffff, 0x154a },
6317 { 0x01, 0xffff, 0x068b }
6318 };
6319
6320 /* disable aspm and clock request before access ephy */
6321 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6322 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6323 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6324
6325 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6326
6327 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6328 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6329 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6330 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6331
6332 rtl_csi_access_enable_1(tp);
6333
6334 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6335
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006336 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6337 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006338
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006339 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006340
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006341 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006342
6343 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6344
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006345 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6346 RTL_W8(MaxTxPacketSize, EarlySize);
6347
6348 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6349 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6350
6351 /* Adjust EEE LED frequency */
6352 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6353
6354 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006355 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006356
6357 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6358
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006359 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006360
6361 rtl_pcie_state_l2l3_enable(tp, false);
6362
6363 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006364 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006365 rtl_writephy(tp, 0x1f, 0x0000);
6366 if (rg_saw_cnt > 0) {
6367 u16 sw_cnt_1ms_ini;
6368
6369 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6370 sw_cnt_1ms_ini &= 0x0fff;
6371 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006372 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006373 data |= sw_cnt_1ms_ini;
6374 r8168_mac_ocp_write(tp, 0xd412, data);
6375 }
6376
6377 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006378 data &= ~0xf0;
6379 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006380 r8168_mac_ocp_write(tp, 0xe056, data);
6381
6382 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006383 data &= ~0x6000;
6384 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006385 r8168_mac_ocp_write(tp, 0xe052, data);
6386
6387 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006388 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006389 data |= 0x017f;
6390 r8168_mac_ocp_write(tp, 0xe0d6, data);
6391
6392 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006393 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006394 data |= 0x047f;
6395 r8168_mac_ocp_write(tp, 0xd420, data);
6396
6397 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6398 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6399 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6400 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6401}
6402
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006403static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6404{
6405 void __iomem *ioaddr = tp->mmio_addr;
6406 struct pci_dev *pdev = tp->pci_dev;
6407
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006408 rtl8168ep_stop_cmac(tp);
6409
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006410 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6411
6412 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6413 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6414 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6415 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6416
6417 rtl_csi_access_enable_1(tp);
6418
6419 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6420
6421 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6422 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6423
6424 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6425
6426 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6427
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006428 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6429 RTL_W8(MaxTxPacketSize, EarlySize);
6430
6431 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6432 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6433
6434 /* Adjust EEE LED frequency */
6435 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6436
6437 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6438
6439 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6440
6441 rtl_pcie_state_l2l3_enable(tp, false);
6442}
6443
6444static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6445{
6446 void __iomem *ioaddr = tp->mmio_addr;
6447 static const struct ephy_info e_info_8168ep_1[] = {
6448 { 0x00, 0xffff, 0x10ab },
6449 { 0x06, 0xffff, 0xf030 },
6450 { 0x08, 0xffff, 0x2006 },
6451 { 0x0d, 0xffff, 0x1666 },
6452 { 0x0c, 0x3ff0, 0x0000 }
6453 };
6454
6455 /* disable aspm and clock request before access ephy */
6456 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6457 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6458 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6459
6460 rtl_hw_start_8168ep(tp);
6461}
6462
6463static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6464{
6465 void __iomem *ioaddr = tp->mmio_addr;
6466 static const struct ephy_info e_info_8168ep_2[] = {
6467 { 0x00, 0xffff, 0x10a3 },
6468 { 0x19, 0xffff, 0xfc00 },
6469 { 0x1e, 0xffff, 0x20ea }
6470 };
6471
6472 /* disable aspm and clock request before access ephy */
6473 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6474 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6475 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6476
6477 rtl_hw_start_8168ep(tp);
6478
6479 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006480 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006481}
6482
6483static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6484{
6485 void __iomem *ioaddr = tp->mmio_addr;
6486 u32 data;
6487 static const struct ephy_info e_info_8168ep_3[] = {
6488 { 0x00, 0xffff, 0x10a3 },
6489 { 0x19, 0xffff, 0x7c00 },
6490 { 0x1e, 0xffff, 0x20eb },
6491 { 0x0d, 0xffff, 0x1666 }
6492 };
6493
6494 /* disable aspm and clock request before access ephy */
6495 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6496 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6497 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6498
6499 rtl_hw_start_8168ep(tp);
6500
6501 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006502 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006503
6504 data = r8168_mac_ocp_read(tp, 0xd3e2);
6505 data &= 0xf000;
6506 data |= 0x0271;
6507 r8168_mac_ocp_write(tp, 0xd3e2, data);
6508
6509 data = r8168_mac_ocp_read(tp, 0xd3e4);
6510 data &= 0xff00;
6511 r8168_mac_ocp_write(tp, 0xd3e4, data);
6512
6513 data = r8168_mac_ocp_read(tp, 0xe860);
6514 data |= 0x0080;
6515 r8168_mac_ocp_write(tp, 0xe860, data);
6516}
6517
Francois Romieu07ce4062007-02-23 23:36:39 +01006518static void rtl_hw_start_8168(struct net_device *dev)
6519{
Francois Romieu2dd99532007-06-11 23:22:52 +02006520 struct rtl8169_private *tp = netdev_priv(dev);
6521 void __iomem *ioaddr = tp->mmio_addr;
6522
6523 RTL_W8(Cfg9346, Cfg9346_Unlock);
6524
françois romieuf0298f82011-01-03 15:07:42 +00006525 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006526
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006527 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006528
Francois Romieu0e485152007-02-20 00:00:26 +01006529 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006530
6531 RTL_W16(CPlusCmd, tp->cp_cmd);
6532
Francois Romieu0e485152007-02-20 00:00:26 +01006533 RTL_W16(IntrMitigate, 0x5151);
6534
6535 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006536 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006537 tp->event_slow |= RxFIFOOver | PCSTimeout;
6538 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006539 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006540
6541 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6542
hayeswang1a964642013-04-01 22:23:41 +00006543 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006544
6545 RTL_R8(IntrMask);
6546
Francois Romieu219a1e92008-06-28 11:58:39 +02006547 switch (tp->mac_version) {
6548 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006549 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006550 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006551
6552 case RTL_GIGA_MAC_VER_12:
6553 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006554 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006555 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006556
6557 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006558 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006559 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006560
6561 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006562 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006563 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006564
6565 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006566 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006567 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006568
Francois Romieu197ff762008-06-28 13:16:02 +02006569 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006570 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006571 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006572
Francois Romieu6fb07052008-06-29 11:54:28 +02006573 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006574 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006575 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006576
Francois Romieuef3386f2008-06-29 12:24:30 +02006577 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006578 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006579 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006580
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006581 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006582 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006583 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006584
Francois Romieu5b538df2008-07-20 16:22:45 +02006585 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006586 case RTL_GIGA_MAC_VER_26:
6587 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006588 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006589 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006590
françois romieue6de30d2011-01-03 15:08:37 +00006591 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006592 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006593 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006594
hayeswang4804b3b2011-03-21 01:50:29 +00006595 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006596 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006597 break;
6598
hayeswang01dc7fe2011-03-21 01:50:28 +00006599 case RTL_GIGA_MAC_VER_32:
6600 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006601 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006602 break;
6603 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006604 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006605 break;
françois romieue6de30d2011-01-03 15:08:37 +00006606
Hayes Wangc2218922011-09-06 16:55:18 +08006607 case RTL_GIGA_MAC_VER_35:
6608 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006609 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006610 break;
6611
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006612 case RTL_GIGA_MAC_VER_38:
6613 rtl_hw_start_8411(tp);
6614 break;
6615
Hayes Wangc5583862012-07-02 17:23:22 +08006616 case RTL_GIGA_MAC_VER_40:
6617 case RTL_GIGA_MAC_VER_41:
6618 rtl_hw_start_8168g_1(tp);
6619 break;
hayeswang57538c42013-04-01 22:23:40 +00006620 case RTL_GIGA_MAC_VER_42:
6621 rtl_hw_start_8168g_2(tp);
6622 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006623
hayeswang45dd95c2013-07-08 17:09:01 +08006624 case RTL_GIGA_MAC_VER_44:
6625 rtl_hw_start_8411_2(tp);
6626 break;
6627
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006628 case RTL_GIGA_MAC_VER_45:
6629 case RTL_GIGA_MAC_VER_46:
6630 rtl_hw_start_8168h_1(tp);
6631 break;
6632
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006633 case RTL_GIGA_MAC_VER_49:
6634 rtl_hw_start_8168ep_1(tp);
6635 break;
6636
6637 case RTL_GIGA_MAC_VER_50:
6638 rtl_hw_start_8168ep_2(tp);
6639 break;
6640
6641 case RTL_GIGA_MAC_VER_51:
6642 rtl_hw_start_8168ep_3(tp);
6643 break;
6644
Francois Romieu219a1e92008-06-28 11:58:39 +02006645 default:
6646 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6647 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006648 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006649 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006650
hayeswang1a964642013-04-01 22:23:41 +00006651 RTL_W8(Cfg9346, Cfg9346_Lock);
6652
Francois Romieu0e485152007-02-20 00:00:26 +01006653 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6654
hayeswang1a964642013-04-01 22:23:41 +00006655 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006656
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006657 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
Francois Romieu2857ffb2008-08-02 21:08:49 +02006660#define R810X_CPCMD_QUIRK_MASK (\
6661 EnableBist | \
6662 Mac_dbgo_oe | \
6663 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006664 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006665 Force_txflow_en | \
6666 Cxpl_dbg_sel | \
6667 ASF | \
6668 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006669 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006670
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006671static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006672{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006673 void __iomem *ioaddr = tp->mmio_addr;
6674 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006675 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006676 { 0x01, 0, 0x6e65 },
6677 { 0x02, 0, 0x091f },
6678 { 0x03, 0, 0xc2f9 },
6679 { 0x06, 0, 0xafb5 },
6680 { 0x07, 0, 0x0e00 },
6681 { 0x19, 0, 0xec80 },
6682 { 0x01, 0, 0x2e65 },
6683 { 0x01, 0, 0x6e65 }
6684 };
6685 u8 cfg1;
6686
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006687 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006688
6689 RTL_W8(DBG_REG, FIX_NAK_1);
6690
6691 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6692
6693 RTL_W8(Config1,
6694 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6695 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6696
6697 cfg1 = RTL_R8(Config1);
6698 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6699 RTL_W8(Config1, cfg1 & ~LEDS0);
6700
Francois Romieufdf6fc02012-07-06 22:40:38 +02006701 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006702}
6703
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006704static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006705{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006706 void __iomem *ioaddr = tp->mmio_addr;
6707 struct pci_dev *pdev = tp->pci_dev;
6708
6709 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006710
6711 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6712
6713 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6714 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006715}
6716
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006717static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006718{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006719 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006720
Francois Romieufdf6fc02012-07-06 22:40:38 +02006721 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006722}
6723
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006724static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006725{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006726 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006727 static const struct ephy_info e_info_8105e_1[] = {
6728 { 0x07, 0, 0x4000 },
6729 { 0x19, 0, 0x0200 },
6730 { 0x19, 0, 0x0020 },
6731 { 0x1e, 0, 0x2000 },
6732 { 0x03, 0, 0x0001 },
6733 { 0x19, 0, 0x0100 },
6734 { 0x19, 0, 0x0004 },
6735 { 0x0a, 0, 0x0020 }
6736 };
6737
Francois Romieucecb5fd2011-04-01 10:21:07 +02006738 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006739 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6740
Francois Romieucecb5fd2011-04-01 10:21:07 +02006741 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006742 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6743
6744 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006745 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006746
Francois Romieufdf6fc02012-07-06 22:40:38 +02006747 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006748
6749 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006750}
6751
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006752static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006753{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006754 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006755 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006756}
6757
Hayes Wang7e18dca2012-03-30 14:33:02 +08006758static void rtl_hw_start_8402(struct rtl8169_private *tp)
6759{
6760 void __iomem *ioaddr = tp->mmio_addr;
6761 static const struct ephy_info e_info_8402[] = {
6762 { 0x19, 0xffff, 0xff64 },
6763 { 0x1e, 0, 0x4000 }
6764 };
6765
6766 rtl_csi_access_enable_2(tp);
6767
6768 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6769 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6770
6771 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6772 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6773
Francois Romieufdf6fc02012-07-06 22:40:38 +02006774 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006775
6776 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6777
Francois Romieufdf6fc02012-07-06 22:40:38 +02006778 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6779 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006780 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6781 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006782 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6783 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006784 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006785
6786 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006787}
6788
Hayes Wang5598bfe2012-07-02 17:23:21 +08006789static void rtl_hw_start_8106(struct rtl8169_private *tp)
6790{
6791 void __iomem *ioaddr = tp->mmio_addr;
6792
6793 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6794 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6795
Francois Romieu4521e1a92012-11-01 16:46:28 +00006796 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006797 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6798 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006799
6800 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006801}
6802
Francois Romieu07ce4062007-02-23 23:36:39 +01006803static void rtl_hw_start_8101(struct net_device *dev)
6804{
Francois Romieucdf1a602007-06-11 23:29:50 +02006805 struct rtl8169_private *tp = netdev_priv(dev);
6806 void __iomem *ioaddr = tp->mmio_addr;
6807 struct pci_dev *pdev = tp->pci_dev;
6808
Francois Romieuda78dbf2012-01-26 14:18:23 +01006809 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6810 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006811
Francois Romieucecb5fd2011-04-01 10:21:07 +02006812 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006813 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006814 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6815 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006816
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006817 RTL_W8(Cfg9346, Cfg9346_Unlock);
6818
hayeswang1a964642013-04-01 22:23:41 +00006819 RTL_W8(MaxTxPacketSize, TxPacketMax);
6820
6821 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6822
6823 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6824 RTL_W16(CPlusCmd, tp->cp_cmd);
6825
6826 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6827
6828 rtl_set_rx_tx_config_registers(tp);
6829
Francois Romieu2857ffb2008-08-02 21:08:49 +02006830 switch (tp->mac_version) {
6831 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006832 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006833 break;
6834
6835 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006836 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006837 break;
6838
6839 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006840 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006841 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006842
6843 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006844 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006845 break;
6846 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006847 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006848 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006849
6850 case RTL_GIGA_MAC_VER_37:
6851 rtl_hw_start_8402(tp);
6852 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006853
6854 case RTL_GIGA_MAC_VER_39:
6855 rtl_hw_start_8106(tp);
6856 break;
hayeswang58152cd2013-04-01 22:23:42 +00006857 case RTL_GIGA_MAC_VER_43:
6858 rtl_hw_start_8168g_2(tp);
6859 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006860 case RTL_GIGA_MAC_VER_47:
6861 case RTL_GIGA_MAC_VER_48:
6862 rtl_hw_start_8168h_1(tp);
6863 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006864 }
6865
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006866 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006867
Francois Romieucdf1a602007-06-11 23:29:50 +02006868 RTL_W16(IntrMitigate, 0x0000);
6869
Francois Romieucdf1a602007-06-11 23:29:50 +02006870 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006871
Francois Romieucdf1a602007-06-11 23:29:50 +02006872 rtl_set_rx_mode(dev);
6873
hayeswang1a964642013-04-01 22:23:41 +00006874 RTL_R8(IntrMask);
6875
Francois Romieucdf1a602007-06-11 23:29:50 +02006876 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877}
6878
6879static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6880{
Francois Romieud58d46b2011-05-03 16:38:29 +02006881 struct rtl8169_private *tp = netdev_priv(dev);
6882
Francois Romieud58d46b2011-05-03 16:38:29 +02006883 if (new_mtu > ETH_DATA_LEN)
6884 rtl_hw_jumbo_enable(tp);
6885 else
6886 rtl_hw_jumbo_disable(tp);
6887
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006889 netdev_update_features(dev);
6890
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892}
6893
6894static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6895{
Al Viro95e09182007-12-22 18:55:39 +00006896 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6898}
6899
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006900static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6901 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006903 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006904 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006905
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006906 kfree(*data_buff);
6907 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 rtl8169_make_unusable_by_asic(desc);
6909}
6910
6911static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6912{
6913 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6914
Alexander Duycka0750132014-12-11 15:02:17 -08006915 /* Force memory writes to complete before releasing descriptor */
6916 dma_wmb();
6917
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6919}
6920
6921static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6922 u32 rx_buf_sz)
6923{
6924 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 rtl8169_mark_to_asic(desc, rx_buf_sz);
6926}
6927
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006928static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006930 return (void *)ALIGN((long)data, 16);
6931}
6932
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006933static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6934 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006935{
6936 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006938 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006939 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006940 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006942 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6943 if (!data)
6944 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006945
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006946 if (rtl8169_align(data) != data) {
6947 kfree(data);
6948 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6949 if (!data)
6950 return NULL;
6951 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006952
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006953 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006954 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006955 if (unlikely(dma_mapping_error(d, mapping))) {
6956 if (net_ratelimit())
6957 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006958 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
6961 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006962 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006963
6964err_out:
6965 kfree(data);
6966 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967}
6968
6969static void rtl8169_rx_clear(struct rtl8169_private *tp)
6970{
Francois Romieu07d3f512007-02-21 22:40:46 +01006971 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
6973 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006974 if (tp->Rx_databuff[i]) {
6975 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 tp->RxDescArray + i);
6977 }
6978 }
6979}
6980
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006981static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006983 desc->opts1 |= cpu_to_le32(RingEnd);
6984}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006985
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006986static int rtl8169_rx_fill(struct rtl8169_private *tp)
6987{
6988 unsigned int i;
6989
6990 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006991 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006992
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006993 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006995
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006996 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006997 if (!data) {
6998 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006999 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007000 }
7001 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007004 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7005 return 0;
7006
7007err_out:
7008 rtl8169_rx_clear(tp);
7009 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010}
7011
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012static int rtl8169_init_ring(struct net_device *dev)
7013{
7014 struct rtl8169_private *tp = netdev_priv(dev);
7015
7016 rtl8169_init_ring_indexes(tp);
7017
7018 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007019 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007021 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022}
7023
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007024static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 struct TxDesc *desc)
7026{
7027 unsigned int len = tx_skb->len;
7028
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007029 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7030
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 desc->opts1 = 0x00;
7032 desc->opts2 = 0x00;
7033 desc->addr = 0x00;
7034 tx_skb->len = 0;
7035}
7036
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007037static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7038 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039{
7040 unsigned int i;
7041
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007042 for (i = 0; i < n; i++) {
7043 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 struct ring_info *tx_skb = tp->tx_skb + entry;
7045 unsigned int len = tx_skb->len;
7046
7047 if (len) {
7048 struct sk_buff *skb = tx_skb->skb;
7049
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007050 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 tp->TxDescArray + entry);
7052 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007053 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 tx_skb->skb = NULL;
7055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 }
7057 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007058}
7059
7060static void rtl8169_tx_clear(struct rtl8169_private *tp)
7061{
7062 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 tp->cur_tx = tp->dirty_tx = 0;
7064}
7065
Francois Romieu4422bcd2012-01-26 11:23:32 +01007066static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067{
David Howellsc4028952006-11-22 14:57:56 +00007068 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01007069 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
Francois Romieuda78dbf2012-01-26 14:18:23 +01007071 napi_disable(&tp->napi);
7072 netif_stop_queue(dev);
7073 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074
françois romieuc7c2c392011-12-04 20:30:52 +00007075 rtl8169_hw_reset(tp);
7076
Francois Romieu56de4142011-03-15 17:29:31 +01007077 for (i = 0; i < NUM_RX_DESC; i++)
7078 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7079
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00007081 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
Francois Romieuda78dbf2012-01-26 14:18:23 +01007083 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01007084 rtl_hw_start(dev);
7085 netif_wake_queue(dev);
7086 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087}
7088
7089static void rtl8169_tx_timeout(struct net_device *dev)
7090{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007091 struct rtl8169_private *tp = netdev_priv(dev);
7092
7093 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094}
7095
7096static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07007097 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098{
7099 struct skb_shared_info *info = skb_shinfo(skb);
7100 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007101 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007102 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
7104 entry = tp->cur_tx;
7105 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007106 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 dma_addr_t mapping;
7108 u32 status, len;
7109 void *addr;
7110
7111 entry = (entry + 1) % NUM_TX_DESC;
7112
7113 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00007114 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00007115 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007116 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007117 if (unlikely(dma_mapping_error(d, mapping))) {
7118 if (net_ratelimit())
7119 netif_err(tp, drv, tp->dev,
7120 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007121 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
Francois Romieucecb5fd2011-04-01 10:21:07 +02007124 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007125 status = opts[0] | len |
7126 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
7128 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07007129 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 txd->addr = cpu_to_le64(mapping);
7131
7132 tp->tx_skb[entry].len = len;
7133 }
7134
7135 if (cur_frag) {
7136 tp->tx_skb[entry].skb = skb;
7137 txd->opts1 |= cpu_to_le32(LastFrag);
7138 }
7139
7140 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007141
7142err_out:
7143 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7144 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145}
7146
françois romieub423e9a2013-05-18 01:24:46 +00007147static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7148{
7149 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7150}
7151
hayeswange9746042014-07-11 16:25:58 +08007152static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7153 struct net_device *dev);
7154/* r8169_csum_workaround()
7155 * The hw limites the value the transport offset. When the offset is out of the
7156 * range, calculate the checksum by sw.
7157 */
7158static void r8169_csum_workaround(struct rtl8169_private *tp,
7159 struct sk_buff *skb)
7160{
7161 if (skb_shinfo(skb)->gso_size) {
7162 netdev_features_t features = tp->dev->features;
7163 struct sk_buff *segs, *nskb;
7164
7165 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7166 segs = skb_gso_segment(skb, features);
7167 if (IS_ERR(segs) || !segs)
7168 goto drop;
7169
7170 do {
7171 nskb = segs;
7172 segs = segs->next;
7173 nskb->next = NULL;
7174 rtl8169_start_xmit(nskb, tp->dev);
7175 } while (segs);
7176
Alexander Duyckeb781392015-05-01 10:34:44 -07007177 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007178 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7179 if (skb_checksum_help(skb) < 0)
7180 goto drop;
7181
7182 rtl8169_start_xmit(skb, tp->dev);
7183 } else {
7184 struct net_device_stats *stats;
7185
7186drop:
7187 stats = &tp->dev->stats;
7188 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007189 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007190 }
7191}
7192
7193/* msdn_giant_send_check()
7194 * According to the document of microsoft, the TCP Pseudo Header excludes the
7195 * packet length for IPv6 TCP large packets.
7196 */
7197static int msdn_giant_send_check(struct sk_buff *skb)
7198{
7199 const struct ipv6hdr *ipv6h;
7200 struct tcphdr *th;
7201 int ret;
7202
7203 ret = skb_cow_head(skb, 0);
7204 if (ret)
7205 return ret;
7206
7207 ipv6h = ipv6_hdr(skb);
7208 th = tcp_hdr(skb);
7209
7210 th->check = 0;
7211 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7212
7213 return ret;
7214}
7215
7216static inline __be16 get_protocol(struct sk_buff *skb)
7217{
7218 __be16 protocol;
7219
7220 if (skb->protocol == htons(ETH_P_8021Q))
7221 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7222 else
7223 protocol = skb->protocol;
7224
7225 return protocol;
7226}
7227
hayeswang5888d3f2014-07-11 16:25:56 +08007228static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7229 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230{
Michał Mirosław350fb322011-04-08 06:35:56 +00007231 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232
Francois Romieu2b7b4312011-04-18 22:53:24 -07007233 if (mss) {
7234 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007235 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7236 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7237 const struct iphdr *ip = ip_hdr(skb);
7238
7239 if (ip->protocol == IPPROTO_TCP)
7240 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7241 else if (ip->protocol == IPPROTO_UDP)
7242 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7243 else
7244 WARN_ON_ONCE(1);
7245 }
7246
7247 return true;
7248}
7249
7250static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7251 struct sk_buff *skb, u32 *opts)
7252{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007253 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007254 u32 mss = skb_shinfo(skb)->gso_size;
7255
7256 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007257 if (transport_offset > GTTCPHO_MAX) {
7258 netif_warn(tp, tx_err, tp->dev,
7259 "Invalid transport offset 0x%x for TSO\n",
7260 transport_offset);
7261 return false;
7262 }
7263
7264 switch (get_protocol(skb)) {
7265 case htons(ETH_P_IP):
7266 opts[0] |= TD1_GTSENV4;
7267 break;
7268
7269 case htons(ETH_P_IPV6):
7270 if (msdn_giant_send_check(skb))
7271 return false;
7272
7273 opts[0] |= TD1_GTSENV6;
7274 break;
7275
7276 default:
7277 WARN_ON_ONCE(1);
7278 break;
7279 }
7280
hayeswangbdfa4ed2014-07-11 16:25:57 +08007281 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007282 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007283 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007284 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
françois romieub423e9a2013-05-18 01:24:46 +00007286 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007287 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007288
hayeswange9746042014-07-11 16:25:58 +08007289 if (transport_offset > TCPHO_MAX) {
7290 netif_warn(tp, tx_err, tp->dev,
7291 "Invalid transport offset 0x%x\n",
7292 transport_offset);
7293 return false;
7294 }
7295
7296 switch (get_protocol(skb)) {
7297 case htons(ETH_P_IP):
7298 opts[1] |= TD1_IPv4_CS;
7299 ip_protocol = ip_hdr(skb)->protocol;
7300 break;
7301
7302 case htons(ETH_P_IPV6):
7303 opts[1] |= TD1_IPv6_CS;
7304 ip_protocol = ipv6_hdr(skb)->nexthdr;
7305 break;
7306
7307 default:
7308 ip_protocol = IPPROTO_RAW;
7309 break;
7310 }
7311
7312 if (ip_protocol == IPPROTO_TCP)
7313 opts[1] |= TD1_TCP_CS;
7314 else if (ip_protocol == IPPROTO_UDP)
7315 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007316 else
7317 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007318
7319 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007320 } else {
7321 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007322 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 }
hayeswang5888d3f2014-07-11 16:25:56 +08007324
françois romieub423e9a2013-05-18 01:24:46 +00007325 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326}
7327
Stephen Hemminger613573252009-08-31 19:50:58 +00007328static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7329 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330{
7331 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007332 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 struct TxDesc *txd = tp->TxDescArray + entry;
7334 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007335 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 dma_addr_t mapping;
7337 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007338 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007339 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007340
Julien Ducourthial477206a2012-05-09 00:00:06 +02007341 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007342 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007343 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 }
7345
7346 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007347 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348
françois romieub423e9a2013-05-18 01:24:46 +00007349 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7350 opts[0] = DescOwn;
7351
hayeswange9746042014-07-11 16:25:58 +08007352 if (!tp->tso_csum(tp, skb, opts)) {
7353 r8169_csum_workaround(tp, skb);
7354 return NETDEV_TX_OK;
7355 }
françois romieub423e9a2013-05-18 01:24:46 +00007356
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007357 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007358 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007359 if (unlikely(dma_mapping_error(d, mapping))) {
7360 if (net_ratelimit())
7361 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007362 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364
7365 tp->tx_skb[entry].len = len;
7366 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
Francois Romieu2b7b4312011-04-18 22:53:24 -07007368 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007369 if (frags < 0)
7370 goto err_dma_1;
7371 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007372 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007373 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007374 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007375 tp->tx_skb[entry].skb = skb;
7376 }
7377
Francois Romieu2b7b4312011-04-18 22:53:24 -07007378 txd->opts2 = cpu_to_le32(opts[1]);
7379
Richard Cochran5047fb52012-03-10 07:29:42 +00007380 skb_tx_timestamp(skb);
7381
Alexander Duycka0750132014-12-11 15:02:17 -08007382 /* Force memory writes to complete before releasing descriptor */
7383 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Francois Romieucecb5fd2011-04-01 10:21:07 +02007385 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007386 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 txd->opts1 = cpu_to_le32(status);
7388
Alexander Duycka0750132014-12-11 15:02:17 -08007389 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007390 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
Alexander Duycka0750132014-12-11 15:02:17 -08007392 tp->cur_tx += frags + 1;
7393
David S. Miller87cda7c2015-02-22 15:54:29 -05007394 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
David S. Miller87cda7c2015-02-22 15:54:29 -05007396 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007397
David S. Miller87cda7c2015-02-22 15:54:29 -05007398 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007399 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7400 * not miss a ring update when it notices a stopped queue.
7401 */
7402 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007404 /* Sync with rtl_tx:
7405 * - publish queue status and cur_tx ring index (write barrier)
7406 * - refresh dirty_tx ring index (read barrier).
7407 * May the current thread have a pessimistic view of the ring
7408 * status and forget to wake up queue, a racing rtl_tx thread
7409 * can't.
7410 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007411 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007412 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 netif_wake_queue(dev);
7414 }
7415
Stephen Hemminger613573252009-08-31 19:50:58 +00007416 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007418err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007419 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007420err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007421 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007422 dev->stats.tx_dropped++;
7423 return NETDEV_TX_OK;
7424
7425err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007427 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007428 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429}
7430
7431static void rtl8169_pcierr_interrupt(struct net_device *dev)
7432{
7433 struct rtl8169_private *tp = netdev_priv(dev);
7434 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 u16 pci_status, pci_cmd;
7436
7437 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7438 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7439
Joe Perchesbf82c182010-02-09 11:49:50 +00007440 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7441 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442
7443 /*
7444 * The recovery sequence below admits a very elaborated explanation:
7445 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007446 * - I did not see what else could be done;
7447 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 *
7449 * Feel free to adjust to your needs.
7450 */
Francois Romieua27993f2006-12-18 00:04:19 +01007451 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007452 pci_cmd &= ~PCI_COMMAND_PARITY;
7453 else
7454 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7455
7456 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457
7458 pci_write_config_word(pdev, PCI_STATUS,
7459 pci_status & (PCI_STATUS_DETECTED_PARITY |
7460 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7461 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7462
7463 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007464 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007465 void __iomem *ioaddr = tp->mmio_addr;
7466
Joe Perchesbf82c182010-02-09 11:49:50 +00007467 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 tp->cp_cmd &= ~PCIDAC;
7469 RTL_W16(CPlusCmd, tp->cp_cmd);
7470 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 }
7472
françois romieue6de30d2011-01-03 15:08:37 +00007473 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007474
Francois Romieu98ddf982012-01-31 10:47:34 +01007475 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476}
7477
Francois Romieuda78dbf2012-01-26 14:18:23 +01007478static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479{
7480 unsigned int dirty_tx, tx_left;
7481
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 dirty_tx = tp->dirty_tx;
7483 smp_rmb();
7484 tx_left = tp->cur_tx - dirty_tx;
7485
7486 while (tx_left > 0) {
7487 unsigned int entry = dirty_tx % NUM_TX_DESC;
7488 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 u32 status;
7490
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7492 if (status & DescOwn)
7493 break;
7494
Alexander Duycka0750132014-12-11 15:02:17 -08007495 /* This barrier is needed to keep us from reading
7496 * any other fields out of the Tx descriptor until
7497 * we know the status of DescOwn
7498 */
7499 dma_rmb();
7500
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007501 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7502 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007504 u64_stats_update_begin(&tp->tx_stats.syncp);
7505 tp->tx_stats.packets++;
7506 tp->tx_stats.bytes += tx_skb->skb->len;
7507 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007508 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 tx_skb->skb = NULL;
7510 }
7511 dirty_tx++;
7512 tx_left--;
7513 }
7514
7515 if (tp->dirty_tx != dirty_tx) {
7516 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007517 /* Sync with rtl8169_start_xmit:
7518 * - publish dirty_tx ring index (write barrier)
7519 * - refresh cur_tx ring index and queue status (read barrier)
7520 * May the current thread miss the stopped queue condition,
7521 * a racing xmit thread can only have a right view of the
7522 * ring status.
7523 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007524 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007526 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 netif_wake_queue(dev);
7528 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007529 /*
7530 * 8168 hack: TxPoll requests are lost when the Tx packets are
7531 * too close. Let's kick an extra TxPoll request when a burst
7532 * of start_xmit activity is detected (if it is not detected,
7533 * it is slow enough). -- FR
7534 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007535 if (tp->cur_tx != dirty_tx) {
7536 void __iomem *ioaddr = tp->mmio_addr;
7537
Francois Romieud78ae2d2007-08-26 20:08:19 +02007538 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540 }
7541}
7542
Francois Romieu126fa4b2005-05-12 20:09:17 -04007543static inline int rtl8169_fragmented_frame(u32 status)
7544{
7545 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7546}
7547
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007548static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 u32 status = opts1 & RxProtoMask;
7551
7552 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007553 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 skb->ip_summed = CHECKSUM_UNNECESSARY;
7555 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007556 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557}
7558
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007559static struct sk_buff *rtl8169_try_rx_copy(void *data,
7560 struct rtl8169_private *tp,
7561 int pkt_size,
7562 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007564 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007565 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007567 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007568 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007569 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007570 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007571 if (skb)
7572 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007573 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7574
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007575 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576}
7577
Francois Romieuda78dbf2012-01-26 14:18:23 +01007578static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579{
7580 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007581 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584
Timo Teräs9fba0812013-01-15 21:01:24 +00007585 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007587 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 u32 status;
7589
David S. Miller8decf862011-09-22 03:23:13 -04007590 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 if (status & DescOwn)
7592 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007593
7594 /* This barrier is needed to keep us from reading
7595 * any other fields out of the Rx descriptor until
7596 * we know the status of DescOwn
7597 */
7598 dma_rmb();
7599
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007600 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007601 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7602 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007603 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007605 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007607 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007608 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007609 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007610 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007611 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007612 if ((status & (RxRUNT | RxCRC)) &&
7613 !(status & (RxRWT | RxFOVF)) &&
7614 (dev->features & NETIF_F_RXALL))
7615 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007617 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007618 dma_addr_t addr;
7619 int pkt_size;
7620
7621process_pkt:
7622 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007623 if (likely(!(dev->features & NETIF_F_RXFCS)))
7624 pkt_size = (status & 0x00003fff) - 4;
7625 else
7626 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
Francois Romieu126fa4b2005-05-12 20:09:17 -04007628 /*
7629 * The driver does not support incoming fragmented
7630 * frames. They are seen as a symptom of over-mtu
7631 * sized frames.
7632 */
7633 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007634 dev->stats.rx_dropped++;
7635 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007636 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007637 }
7638
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007639 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7640 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007641 if (!skb) {
7642 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007643 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 }
7645
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007646 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 skb_put(skb, pkt_size);
7648 skb->protocol = eth_type_trans(skb, dev);
7649
Francois Romieu7a8fc772011-03-01 17:18:33 +01007650 rtl8169_rx_vlan_tag(desc, skb);
7651
françois romieu39174292015-11-11 23:35:18 +01007652 if (skb->pkt_type == PACKET_MULTICAST)
7653 dev->stats.multicast++;
7654
Francois Romieu56de4142011-03-15 17:29:31 +01007655 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
Junchang Wang8027aa22012-03-04 23:30:32 +01007657 u64_stats_update_begin(&tp->rx_stats.syncp);
7658 tp->rx_stats.packets++;
7659 tp->rx_stats.bytes += pkt_size;
7660 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 }
françois romieuce11ff52013-01-24 13:30:06 +00007662release_descriptor:
7663 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007664 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 }
7666
7667 count = cur_rx - tp->cur_rx;
7668 tp->cur_rx = cur_rx;
7669
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 return count;
7671}
7672
Francois Romieu07d3f512007-02-21 22:40:46 +01007673static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674{
Francois Romieu07d3f512007-02-21 22:40:46 +01007675 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007678 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007680 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007681 if (status && status != 0xffff) {
7682 status &= RTL_EVENT_NAPI | tp->event_slow;
7683 if (status) {
7684 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007685
Francois Romieuda78dbf2012-01-26 14:18:23 +01007686 rtl_irq_disable(tp);
7687 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 return IRQ_RETVAL(handled);
7691}
7692
Francois Romieuda78dbf2012-01-26 14:18:23 +01007693/*
7694 * Workqueue context.
7695 */
7696static void rtl_slow_event_work(struct rtl8169_private *tp)
7697{
7698 struct net_device *dev = tp->dev;
7699 u16 status;
7700
7701 status = rtl_get_events(tp) & tp->event_slow;
7702 rtl_ack_events(tp, status);
7703
7704 if (unlikely(status & RxFIFOOver)) {
7705 switch (tp->mac_version) {
7706 /* Work around for rx fifo overflow */
7707 case RTL_GIGA_MAC_VER_11:
7708 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007709 /* XXX - Hack alert. See rtl_task(). */
7710 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007711 default:
7712 break;
7713 }
7714 }
7715
7716 if (unlikely(status & SYSErr))
7717 rtl8169_pcierr_interrupt(dev);
7718
7719 if (status & LinkChg)
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01007720 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007721
françois romieu7dbb4912012-06-09 10:53:16 +00007722 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007723}
7724
Francois Romieu4422bcd2012-01-26 11:23:32 +01007725static void rtl_task(struct work_struct *work)
7726{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007727 static const struct {
7728 int bitnr;
7729 void (*action)(struct rtl8169_private *);
7730 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007731 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007732 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7733 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7734 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7735 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007736 struct rtl8169_private *tp =
7737 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007738 struct net_device *dev = tp->dev;
7739 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007740
Francois Romieuda78dbf2012-01-26 14:18:23 +01007741 rtl_lock_work(tp);
7742
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007743 if (!netif_running(dev) ||
7744 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007745 goto out_unlock;
7746
7747 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7748 bool pending;
7749
Francois Romieuda78dbf2012-01-26 14:18:23 +01007750 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007751 if (pending)
7752 rtl_work[i].action(tp);
7753 }
7754
7755out_unlock:
7756 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007757}
7758
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007759static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007761 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7762 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007763 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7764 int work_done= 0;
7765 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766
Francois Romieuda78dbf2012-01-26 14:18:23 +01007767 status = rtl_get_events(tp);
7768 rtl_ack_events(tp, status & ~tp->event_slow);
7769
7770 if (status & RTL_EVENT_NAPI_RX)
7771 work_done = rtl_rx(dev, tp, (u32) budget);
7772
7773 if (status & RTL_EVENT_NAPI_TX)
7774 rtl_tx(dev, tp);
7775
7776 if (status & tp->event_slow) {
7777 enable_mask &= ~tp->event_slow;
7778
7779 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007782 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007783 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007784
Francois Romieuda78dbf2012-01-26 14:18:23 +01007785 rtl_irq_enable(tp, enable_mask);
7786 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 }
7788
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007789 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
Francois Romieu523a6092008-09-10 22:28:56 +02007792static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7793{
7794 struct rtl8169_private *tp = netdev_priv(dev);
7795
7796 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7797 return;
7798
7799 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7800 RTL_W32(RxMissed, 0);
7801}
7802
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803static void rtl8169_down(struct net_device *dev)
7804{
7805 struct rtl8169_private *tp = netdev_priv(dev);
7806 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
Francois Romieu4876cc12011-03-11 21:07:11 +01007808 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007810 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007811 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812
Hayes Wang92fc43b2011-07-06 15:58:03 +08007813 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007814 /*
7815 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007816 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7817 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007818 */
Francois Romieu523a6092008-09-10 22:28:56 +02007819 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007822 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 rtl8169_tx_clear(tp);
7825
7826 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007827
7828 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829}
7830
7831static int rtl8169_close(struct net_device *dev)
7832{
7833 struct rtl8169_private *tp = netdev_priv(dev);
7834 struct pci_dev *pdev = tp->pci_dev;
7835
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007836 pm_runtime_get_sync(&pdev->dev);
7837
Francois Romieucecb5fd2011-04-01 10:21:07 +02007838 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007839 rtl8169_update_counters(dev);
7840
Francois Romieuda78dbf2012-01-26 14:18:23 +01007841 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007842 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007843
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007845 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846
Lekensteyn4ea72442013-07-22 09:53:30 +02007847 cancel_work_sync(&tp->wk.work);
7848
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007849 pci_free_irq(pdev, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007851 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7852 tp->RxPhyAddr);
7853 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7854 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 tp->TxDescArray = NULL;
7856 tp->RxDescArray = NULL;
7857
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007858 pm_runtime_put_sync(&pdev->dev);
7859
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 return 0;
7861}
7862
Francois Romieudc1c00c2012-03-08 10:06:18 +01007863#ifdef CONFIG_NET_POLL_CONTROLLER
7864static void rtl8169_netpoll(struct net_device *dev)
7865{
7866 struct rtl8169_private *tp = netdev_priv(dev);
7867
7868 rtl8169_interrupt(tp->pci_dev->irq, dev);
7869}
7870#endif
7871
Francois Romieudf43ac72012-03-08 09:48:40 +01007872static int rtl_open(struct net_device *dev)
7873{
7874 struct rtl8169_private *tp = netdev_priv(dev);
7875 void __iomem *ioaddr = tp->mmio_addr;
7876 struct pci_dev *pdev = tp->pci_dev;
7877 int retval = -ENOMEM;
7878
7879 pm_runtime_get_sync(&pdev->dev);
7880
7881 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007882 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007883 * dma_alloc_coherent provides more.
7884 */
7885 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7886 &tp->TxPhyAddr, GFP_KERNEL);
7887 if (!tp->TxDescArray)
7888 goto err_pm_runtime_put;
7889
7890 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7891 &tp->RxPhyAddr, GFP_KERNEL);
7892 if (!tp->RxDescArray)
7893 goto err_free_tx_0;
7894
7895 retval = rtl8169_init_ring(dev);
7896 if (retval < 0)
7897 goto err_free_rx_1;
7898
7899 INIT_WORK(&tp->wk.work, rtl_task);
7900
7901 smp_mb();
7902
7903 rtl_request_firmware(tp);
7904
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007905 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7906 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007907 if (retval < 0)
7908 goto err_release_fw_2;
7909
7910 rtl_lock_work(tp);
7911
7912 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7913
7914 napi_enable(&tp->napi);
7915
7916 rtl8169_init_phy(dev, tp);
7917
7918 __rtl8169_set_features(dev, dev->features);
7919
7920 rtl_pll_power_up(tp);
7921
7922 rtl_hw_start(dev);
7923
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007924 if (!rtl8169_init_counter_offsets(dev))
7925 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7926
Francois Romieudf43ac72012-03-08 09:48:40 +01007927 netif_start_queue(dev);
7928
7929 rtl_unlock_work(tp);
7930
7931 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007932 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007933
7934 rtl8169_check_link_status(dev, tp, ioaddr);
7935out:
7936 return retval;
7937
7938err_release_fw_2:
7939 rtl_release_firmware(tp);
7940 rtl8169_rx_clear(tp);
7941err_free_rx_1:
7942 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7943 tp->RxPhyAddr);
7944 tp->RxDescArray = NULL;
7945err_free_tx_0:
7946 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7947 tp->TxPhyAddr);
7948 tp->TxDescArray = NULL;
7949err_pm_runtime_put:
7950 pm_runtime_put_noidle(&pdev->dev);
7951 goto out;
7952}
7953
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007954static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007955rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956{
7957 struct rtl8169_private *tp = netdev_priv(dev);
7958 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007959 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007960 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007961 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007963 pm_runtime_get_noresume(&pdev->dev);
7964
7965 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007966 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007967
Junchang Wang8027aa22012-03-04 23:30:32 +01007968 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007969 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007970 stats->rx_packets = tp->rx_stats.packets;
7971 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007972 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007973
Junchang Wang8027aa22012-03-04 23:30:32 +01007974 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007975 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007976 stats->tx_packets = tp->tx_stats.packets;
7977 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007978 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007979
7980 stats->rx_dropped = dev->stats.rx_dropped;
7981 stats->tx_dropped = dev->stats.tx_dropped;
7982 stats->rx_length_errors = dev->stats.rx_length_errors;
7983 stats->rx_errors = dev->stats.rx_errors;
7984 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7985 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7986 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007987 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007988
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007989 /*
7990 * Fetch additonal counter values missing in stats collected by driver
7991 * from tally counters.
7992 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007993 if (pm_runtime_active(&pdev->dev))
7994 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007995
7996 /*
7997 * Subtract values fetched during initalization.
7998 * See rtl8169_init_counter_offsets for a description why we do that.
7999 */
Corinna Vinschen42020322015-09-10 10:47:35 +02008000 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008001 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02008002 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008003 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02008004 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008005 le16_to_cpu(tp->tc_offset.tx_aborted);
8006
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008007 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008008}
8009
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008010static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01008011{
françois romieu065c27c2011-01-03 15:08:12 +00008012 struct rtl8169_private *tp = netdev_priv(dev);
8013
Francois Romieu5d06a992006-02-23 00:47:58 +01008014 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008015 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01008016
8017 netif_device_detach(dev);
8018 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008019
8020 rtl_lock_work(tp);
8021 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008022 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008023 rtl_unlock_work(tp);
8024
8025 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008026}
Francois Romieu5d06a992006-02-23 00:47:58 +01008027
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008028#ifdef CONFIG_PM
8029
8030static int rtl8169_suspend(struct device *device)
8031{
8032 struct pci_dev *pdev = to_pci_dev(device);
8033 struct net_device *dev = pci_get_drvdata(pdev);
8034
8035 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02008036
Francois Romieu5d06a992006-02-23 00:47:58 +01008037 return 0;
8038}
8039
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008040static void __rtl8169_resume(struct net_device *dev)
8041{
françois romieu065c27c2011-01-03 15:08:12 +00008042 struct rtl8169_private *tp = netdev_priv(dev);
8043
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008044 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00008045
8046 rtl_pll_power_up(tp);
8047
Artem Savkovcff4c162012-04-03 10:29:11 +00008048 rtl_lock_work(tp);
8049 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008050 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00008051 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008052
Francois Romieu98ddf982012-01-31 10:47:34 +01008053 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008054}
8055
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008056static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01008057{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008058 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01008059 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008060 struct rtl8169_private *tp = netdev_priv(dev);
8061
8062 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01008063
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008064 if (netif_running(dev))
8065 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01008066
Francois Romieu5d06a992006-02-23 00:47:58 +01008067 return 0;
8068}
8069
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008070static int rtl8169_runtime_suspend(struct device *device)
8071{
8072 struct pci_dev *pdev = to_pci_dev(device);
8073 struct net_device *dev = pci_get_drvdata(pdev);
8074 struct rtl8169_private *tp = netdev_priv(dev);
8075
Heiner Kallweita92a0842018-01-08 21:39:13 +01008076 if (!tp->TxDescArray) {
8077 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008078 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01008079 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008080
Francois Romieuda78dbf2012-01-26 14:18:23 +01008081 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008082 tp->saved_wolopts = __rtl8169_get_wol(tp);
8083 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008084 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008085
8086 rtl8169_net_suspend(dev);
8087
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008088 /* Update counters before going runtime suspend */
8089 rtl8169_rx_missed(dev, tp->mmio_addr);
8090 rtl8169_update_counters(dev);
8091
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008092 return 0;
8093}
8094
8095static int rtl8169_runtime_resume(struct device *device)
8096{
8097 struct pci_dev *pdev = to_pci_dev(device);
8098 struct net_device *dev = pci_get_drvdata(pdev);
8099 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08008100 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008101
8102 if (!tp->TxDescArray)
8103 return 0;
8104
Francois Romieuda78dbf2012-01-26 14:18:23 +01008105 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008106 __rtl8169_set_wol(tp, tp->saved_wolopts);
8107 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01008108 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008109
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008110 rtl8169_init_phy(dev, tp);
8111
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008112 __rtl8169_resume(dev);
8113
8114 return 0;
8115}
8116
8117static int rtl8169_runtime_idle(struct device *device)
8118{
8119 struct pci_dev *pdev = to_pci_dev(device);
8120 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008121
Heiner Kallweita92a0842018-01-08 21:39:13 +01008122 if (!netif_running(dev) || !netif_carrier_ok(dev))
8123 pm_schedule_suspend(device, 10000);
8124
8125 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008126}
8127
Alexey Dobriyan47145212009-12-14 18:00:08 -08008128static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02008129 .suspend = rtl8169_suspend,
8130 .resume = rtl8169_resume,
8131 .freeze = rtl8169_suspend,
8132 .thaw = rtl8169_resume,
8133 .poweroff = rtl8169_suspend,
8134 .restore = rtl8169_resume,
8135 .runtime_suspend = rtl8169_runtime_suspend,
8136 .runtime_resume = rtl8169_runtime_resume,
8137 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008138};
8139
8140#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8141
8142#else /* !CONFIG_PM */
8143
8144#define RTL8169_PM_OPS NULL
8145
8146#endif /* !CONFIG_PM */
8147
David S. Miller1805b2f2011-10-24 18:18:09 -04008148static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8149{
8150 void __iomem *ioaddr = tp->mmio_addr;
8151
8152 /* WoL fails with 8168b when the receiver is disabled. */
8153 switch (tp->mac_version) {
8154 case RTL_GIGA_MAC_VER_11:
8155 case RTL_GIGA_MAC_VER_12:
8156 case RTL_GIGA_MAC_VER_17:
8157 pci_clear_master(tp->pci_dev);
8158
8159 RTL_W8(ChipCmd, CmdRxEnb);
8160 /* PCI commit */
8161 RTL_R8(ChipCmd);
8162 break;
8163 default:
8164 break;
8165 }
8166}
8167
Francois Romieu1765f952008-09-13 17:21:40 +02008168static void rtl_shutdown(struct pci_dev *pdev)
8169{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008170 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00008171 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02008172
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008173 rtl8169_net_suspend(dev);
8174
Francois Romieucecb5fd2011-04-01 10:21:07 +02008175 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08008176 rtl_rar_set(tp, dev->perm_addr);
8177
Hayes Wang92fc43b2011-07-06 15:58:03 +08008178 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00008179
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008180 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04008181 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8182 rtl_wol_suspend_quirk(tp);
8183 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00008184 }
8185
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008186 pci_wake_from_d3(pdev, true);
8187 pci_set_power_state(pdev, PCI_D3hot);
8188 }
8189}
Francois Romieu5d06a992006-02-23 00:47:58 +01008190
Bill Pembertonbaf63292012-12-03 09:23:28 -05008191static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008192{
8193 struct net_device *dev = pci_get_drvdata(pdev);
8194 struct rtl8169_private *tp = netdev_priv(dev);
8195
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008196 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01008197 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01008198
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008199 netif_napi_del(&tp->napi);
8200
Francois Romieue27566e2012-03-08 09:54:01 +01008201 unregister_netdev(dev);
8202
8203 rtl_release_firmware(tp);
8204
8205 if (pci_dev_run_wake(pdev))
8206 pm_runtime_get_noresume(&pdev->dev);
8207
8208 /* restore original MAC address */
8209 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008210}
8211
Francois Romieufa9c3852012-03-08 10:01:50 +01008212static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008213 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008214 .ndo_stop = rtl8169_close,
8215 .ndo_get_stats64 = rtl8169_get_stats64,
8216 .ndo_start_xmit = rtl8169_start_xmit,
8217 .ndo_tx_timeout = rtl8169_tx_timeout,
8218 .ndo_validate_addr = eth_validate_addr,
8219 .ndo_change_mtu = rtl8169_change_mtu,
8220 .ndo_fix_features = rtl8169_fix_features,
8221 .ndo_set_features = rtl8169_set_features,
8222 .ndo_set_mac_address = rtl_set_mac_address,
8223 .ndo_do_ioctl = rtl8169_ioctl,
8224 .ndo_set_rx_mode = rtl_set_rx_mode,
8225#ifdef CONFIG_NET_POLL_CONTROLLER
8226 .ndo_poll_controller = rtl8169_netpoll,
8227#endif
8228
8229};
8230
Francois Romieu31fa8b12012-03-08 10:09:40 +01008231static const struct rtl_cfg_info {
8232 void (*hw_start)(struct net_device *);
8233 unsigned int region;
8234 unsigned int align;
8235 u16 event_slow;
8236 unsigned features;
Francois Romieu50970832017-10-27 13:24:49 +03008237 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008238 u8 default_ver;
8239} rtl_cfg_infos [] = {
8240 [RTL_CFG_0] = {
8241 .hw_start = rtl_hw_start_8169,
8242 .region = 1,
8243 .align = 0,
8244 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8245 .features = RTL_FEATURE_GMII,
Francois Romieu50970832017-10-27 13:24:49 +03008246 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008247 .default_ver = RTL_GIGA_MAC_VER_01,
8248 },
8249 [RTL_CFG_1] = {
8250 .hw_start = rtl_hw_start_8168,
8251 .region = 2,
8252 .align = 8,
8253 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008254 .features = RTL_FEATURE_GMII,
Francois Romieu50970832017-10-27 13:24:49 +03008255 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008256 .default_ver = RTL_GIGA_MAC_VER_11,
8257 },
8258 [RTL_CFG_2] = {
8259 .hw_start = rtl_hw_start_8101,
8260 .region = 2,
8261 .align = 8,
8262 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8263 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03008264 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008265 .default_ver = RTL_GIGA_MAC_VER_13,
8266 }
8267};
8268
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008269static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01008270{
8271 void __iomem *ioaddr = tp->mmio_addr;
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008272 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008273
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008274 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
8275 RTL_W8(Cfg9346, Cfg9346_Unlock);
8276 RTL_W8(Config2, RTL_R8(Config2) & ~MSIEnable);
8277 RTL_W8(Cfg9346, Cfg9346_Lock);
8278 flags = PCI_IRQ_LEGACY;
8279 } else {
8280 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008281 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008282
8283 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01008284}
8285
Hayes Wangc5583862012-07-02 17:23:22 +08008286DECLARE_RTL_COND(rtl_link_list_ready_cond)
8287{
8288 void __iomem *ioaddr = tp->mmio_addr;
8289
8290 return RTL_R8(MCU) & LINK_LIST_RDY;
8291}
8292
8293DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8294{
8295 void __iomem *ioaddr = tp->mmio_addr;
8296
8297 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8298}
8299
Bill Pembertonbaf63292012-12-03 09:23:28 -05008300static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008301{
8302 void __iomem *ioaddr = tp->mmio_addr;
8303 u32 data;
8304
8305 tp->ocp_base = OCP_STD_PHY_BASE;
8306
8307 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8308
8309 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8310 return;
8311
8312 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8313 return;
8314
8315 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8316 msleep(1);
8317 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8318
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008319 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008320 data &= ~(1 << 14);
8321 r8168_mac_ocp_write(tp, 0xe8de, data);
8322
8323 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8324 return;
8325
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008326 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008327 data |= (1 << 15);
8328 r8168_mac_ocp_write(tp, 0xe8de, data);
8329
8330 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8331 return;
8332}
8333
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008334static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8335{
8336 rtl8168ep_stop_cmac(tp);
8337 rtl_hw_init_8168g(tp);
8338}
8339
Bill Pembertonbaf63292012-12-03 09:23:28 -05008340static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008341{
8342 switch (tp->mac_version) {
8343 case RTL_GIGA_MAC_VER_40:
8344 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008345 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008346 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008347 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008348 case RTL_GIGA_MAC_VER_45:
8349 case RTL_GIGA_MAC_VER_46:
8350 case RTL_GIGA_MAC_VER_47:
8351 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008352 rtl_hw_init_8168g(tp);
8353 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008354 case RTL_GIGA_MAC_VER_49:
8355 case RTL_GIGA_MAC_VER_50:
8356 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008357 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008358 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008359 default:
8360 break;
8361 }
8362}
8363
hayeswang929a0312014-09-16 11:40:47 +08008364static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008365{
8366 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8367 const unsigned int region = cfg->region;
8368 struct rtl8169_private *tp;
8369 struct mii_if_info *mii;
8370 struct net_device *dev;
8371 void __iomem *ioaddr;
8372 int chipset, i;
8373 int rc;
8374
8375 if (netif_msg_drv(&debug)) {
8376 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8377 MODULENAME, RTL8169_VERSION);
8378 }
8379
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008380 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8381 if (!dev)
8382 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008383
8384 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008385 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008386 tp = netdev_priv(dev);
8387 tp->dev = dev;
8388 tp->pci_dev = pdev;
8389 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8390
8391 mii = &tp->mii;
8392 mii->dev = dev;
8393 mii->mdio_read = rtl_mdio_read;
8394 mii->mdio_write = rtl_mdio_write;
8395 mii->phy_id_mask = 0x1f;
8396 mii->reg_num_mask = 0x1f;
8397 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8398
8399 /* disable ASPM completely as that cause random device stop working
8400 * problems as well as full system hangs for some PCIe devices users */
8401 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8402 PCIE_LINK_STATE_CLKPM);
8403
8404 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008405 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008406 if (rc < 0) {
8407 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008408 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008409 }
8410
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008411 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008412 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8413
8414 /* make sure PCI base addr 1 is MMIO */
8415 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8416 netif_err(tp, probe, dev,
8417 "region #%d not an MMIO resource, aborting\n",
8418 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008419 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008420 }
8421
8422 /* check for weird/broken PCI region reporting */
8423 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8424 netif_err(tp, probe, dev,
8425 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008426 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008427 }
8428
8429 rc = pci_request_regions(pdev, MODULENAME);
8430 if (rc < 0) {
8431 netif_err(tp, probe, dev, "could not request regions\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008432 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008433 }
8434
Francois Romieu3b6cf252012-03-08 09:59:04 +01008435 /* ioremap MMIO region */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008436 ioaddr = devm_ioremap(&pdev->dev, pci_resource_start(pdev, region),
8437 R8169_REGS_SIZE);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008438 if (!ioaddr) {
8439 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008440 return -EIO;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008441 }
8442 tp->mmio_addr = ioaddr;
8443
8444 if (!pci_is_pcie(pdev))
8445 netif_info(tp, probe, dev, "not PCI Express\n");
8446
8447 /* Identify chip attached to board */
8448 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8449
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008450 tp->cp_cmd = 0;
8451
8452 if ((sizeof(dma_addr_t) > 4) &&
8453 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8454 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008455 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8456 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008457
8458 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8459 if (!pci_is_pcie(pdev))
8460 tp->cp_cmd |= PCIDAC;
8461 dev->features |= NETIF_F_HIGHDMA;
8462 } else {
8463 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8464 if (rc < 0) {
8465 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008466 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008467 }
8468 }
8469
Francois Romieu3b6cf252012-03-08 09:59:04 +01008470 rtl_init_rxcfg(tp);
8471
8472 rtl_irq_disable(tp);
8473
Hayes Wangc5583862012-07-02 17:23:22 +08008474 rtl_hw_initialize(tp);
8475
Francois Romieu3b6cf252012-03-08 09:59:04 +01008476 rtl_hw_reset(tp);
8477
8478 rtl_ack_events(tp, 0xffff);
8479
8480 pci_set_master(pdev);
8481
Francois Romieu3b6cf252012-03-08 09:59:04 +01008482 rtl_init_mdio_ops(tp);
8483 rtl_init_pll_power_ops(tp);
8484 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008485 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008486
8487 rtl8169_print_mac_version(tp);
8488
8489 chipset = tp->mac_version;
8490 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8491
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008492 rc = rtl_alloc_irq(tp);
8493 if (rc < 0) {
8494 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8495 return rc;
8496 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008497
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008498 /* override BIOS settings, use userspace tools to enable WOL */
8499 __rtl8169_set_wol(tp, 0);
8500
Francois Romieu3b6cf252012-03-08 09:59:04 +01008501 if (rtl_tbi_enabled(tp)) {
8502 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008503 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008504 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8505 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8506 tp->link_ok = rtl8169_tbi_link_ok;
8507 tp->do_ioctl = rtl_tbi_ioctl;
8508 } else {
8509 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008510 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008511 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8512 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8513 tp->link_ok = rtl8169_xmii_link_ok;
8514 tp->do_ioctl = rtl_xmii_ioctl;
8515 }
8516
8517 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008518 u64_stats_init(&tp->rx_stats.syncp);
8519 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008520
8521 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008522 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8523 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8524 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8525 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8526 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8527 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8528 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8529 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8530 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8531 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008532 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8533 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008534 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8535 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8536 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8537 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008538 u16 mac_addr[3];
8539
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008540 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8541 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008542
8543 if (is_valid_ether_addr((u8 *)mac_addr))
8544 rtl_rar_set(tp, (u8 *)mac_addr);
8545 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008546 for (i = 0; i < ETH_ALEN; i++)
8547 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008548
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008549 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008550 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008551
8552 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8553
8554 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8555 * properly for all devices */
8556 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008557 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008558
8559 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008560 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8561 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008562 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8563 NETIF_F_HIGHDMA;
8564
hayeswang929a0312014-09-16 11:40:47 +08008565 tp->cp_cmd |= RxChkSum | RxVlan;
8566
8567 /*
8568 * Pretend we are using VLANs; This bypasses a nasty bug where
8569 * Interrupts stop flowing on high load on 8110SCd controllers.
8570 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008571 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008572 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008573 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008574
hayeswang5888d3f2014-07-11 16:25:56 +08008575 if (tp->txd_version == RTL_TD_0)
8576 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008577 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008578 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008579 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8580 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008581 WARN_ON_ONCE(1);
8582
Francois Romieu3b6cf252012-03-08 09:59:04 +01008583 dev->hw_features |= NETIF_F_RXALL;
8584 dev->hw_features |= NETIF_F_RXFCS;
8585
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008586 /* MTU range: 60 - hw-specific max */
8587 dev->min_mtu = ETH_ZLEN;
8588 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8589
Francois Romieu3b6cf252012-03-08 09:59:04 +01008590 tp->hw_start = cfg->hw_start;
8591 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008592 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008593
8594 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8595 ~(RxBOVF | RxFOVF) : ~0;
8596
Kees Cook9de36cc2017-10-25 03:53:12 -07008597 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008598
8599 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8600
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008601 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8602 &tp->counters_phys_addr,
8603 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008604 if (!tp->counters)
8605 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008606
Francois Romieu3b6cf252012-03-08 09:59:04 +01008607 rc = register_netdev(dev);
8608 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008609 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008610
8611 pci_set_drvdata(pdev, dev);
8612
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008613 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8614 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8615 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008616 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8617 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8618 "tx checksumming: %s]\n",
8619 rtl_chip_infos[chipset].jumbo_max,
8620 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8621 }
8622
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008623 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008624 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008625
Francois Romieu3b6cf252012-03-08 09:59:04 +01008626 netif_carrier_off(dev);
8627
Heiner Kallweita92a0842018-01-08 21:39:13 +01008628 if (pci_dev_run_wake(pdev))
8629 pm_runtime_put_sync(&pdev->dev);
8630
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008631 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008632}
8633
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634static struct pci_driver rtl8169_pci_driver = {
8635 .name = MODULENAME,
8636 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008637 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008638 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008639 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008640 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641};
8642
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008643module_pci_driver(rtl8169_pci_driver);