blob: 0a0638d692f906ffa24f02b8e2f92281a490ff0c [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
Ivan Vecera355423d2009-02-06 21:49:57 -0800738struct rtl8169_counters {
739 __le64 tx_packets;
740 __le64 rx_packets;
741 __le64 tx_errors;
742 __le32 rx_errors;
743 __le16 rx_missed;
744 __le16 align_errors;
745 __le32 tx_one_collision;
746 __le32 tx_multi_collision;
747 __le64 rx_unicast;
748 __le64 rx_broadcast;
749 __le32 rx_multicast;
750 __le16 tx_aborted;
751 __le16 tx_underun;
752};
753
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200754struct rtl8169_tc_offsets {
755 bool inited;
756 __le64 tx_errors;
757 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200758 __le16 tx_aborted;
759};
760
Francois Romieuda78dbf2012-01-26 14:18:23 +0100761enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100762 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100763 RTL_FLAG_TASK_SLOW_PENDING,
764 RTL_FLAG_TASK_RESET_PENDING,
765 RTL_FLAG_TASK_PHY_PENDING,
766 RTL_FLAG_MAX
767};
768
Junchang Wang8027aa22012-03-04 23:30:32 +0100769struct rtl8169_stats {
770 u64 packets;
771 u64 bytes;
772 struct u64_stats_sync syncp;
773};
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775struct rtl8169_private {
776 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200777 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000778 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700779 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200780 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700781 u16 txd_version;
782 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
784 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100786 struct rtl8169_stats rx_stats;
787 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
789 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
790 dma_addr_t TxPhyAddr;
791 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000792 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct timer_list timer;
795 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100796
797 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300798 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000799
800 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200801 void (*write)(struct rtl8169_private *, int, int);
802 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000803 } mdio_ops;
804
françois romieu065c27c2011-01-03 15:08:12 +0000805 struct pll_power_ops {
806 void (*down)(struct rtl8169_private *);
807 void (*up)(struct rtl8169_private *);
808 } pll_power_ops;
809
Francois Romieud58d46b2011-05-03 16:38:29 +0200810 struct jumbo_ops {
811 void (*enable)(struct rtl8169_private *);
812 void (*disable)(struct rtl8169_private *);
813 } jumbo_ops;
814
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800815 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200816 void (*write)(struct rtl8169_private *, int, int);
817 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800818 } csi_ops;
819
Oliver Neukum54405cd2011-01-06 21:55:13 +0100820 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100821 int (*get_link_ksettings)(struct net_device *,
822 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000823 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100824 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000825 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800827 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800828 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100829
830 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100831 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
832 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100833 struct work_struct work;
834 } wk;
835
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200836 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200837
838 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200839 dma_addr_t counters_phys_addr;
840 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200841 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000842 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400843 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000844
Francois Romieub6ffd972011-06-17 17:00:05 +0200845 struct rtl_fw {
846 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200847
848#define RTL_VER_SIZE 32
849
850 char version[RTL_VER_SIZE];
851
852 struct rtl_fw_phy_action {
853 __le32 *code;
854 size_t size;
855 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200856 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300857#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800858
859 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Ralf Baechle979b6c12005-06-13 14:30:40 -0700862MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700865MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200866module_param_named(debug, debug.msg_enable, int, 0);
867MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868MODULE_LICENSE("GPL");
869MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000870MODULE_FIRMWARE(FIRMWARE_8168D_1);
871MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000872MODULE_FIRMWARE(FIRMWARE_8168E_1);
873MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400874MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800875MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800876MODULE_FIRMWARE(FIRMWARE_8168F_1);
877MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800878MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800879MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800880MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800881MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000882MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000883MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000884MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800885MODULE_FIRMWARE(FIRMWARE_8168H_1);
886MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200887MODULE_FIRMWARE(FIRMWARE_8107E_1);
888MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Francois Romieuda78dbf2012-01-26 14:18:23 +0100890static void rtl_lock_work(struct rtl8169_private *tp)
891{
892 mutex_lock(&tp->wk.mutex);
893}
894
895static void rtl_unlock_work(struct rtl8169_private *tp)
896{
897 mutex_unlock(&tp->wk.mutex);
898}
899
Francois Romieud58d46b2011-05-03 16:38:29 +0200900static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
901{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800902 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
903 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200904}
905
Francois Romieuffc46952012-07-06 14:19:23 +0200906struct rtl_cond {
907 bool (*check)(struct rtl8169_private *);
908 const char *msg;
909};
910
911static void rtl_udelay(unsigned int d)
912{
913 udelay(d);
914}
915
916static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
917 void (*delay)(unsigned int), unsigned int d, int n,
918 bool high)
919{
920 int i;
921
922 for (i = 0; i < n; i++) {
923 delay(d);
924 if (c->check(tp) == high)
925 return true;
926 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200927 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
928 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200929 return false;
930}
931
932static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
933 const struct rtl_cond *c,
934 unsigned int d, int n)
935{
936 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
937}
938
939static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
940 const struct rtl_cond *c,
941 unsigned int d, int n)
942{
943 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
944}
945
946static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
947 const struct rtl_cond *c,
948 unsigned int d, int n)
949{
950 return rtl_loop_wait(tp, c, msleep, d, n, true);
951}
952
953static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
954 const struct rtl_cond *c,
955 unsigned int d, int n)
956{
957 return rtl_loop_wait(tp, c, msleep, d, n, false);
958}
959
960#define DECLARE_RTL_COND(name) \
961static bool name ## _check(struct rtl8169_private *); \
962 \
963static const struct rtl_cond name = { \
964 .check = name ## _check, \
965 .msg = #name \
966}; \
967 \
968static bool name ## _check(struct rtl8169_private *tp)
969
Hayes Wangc5583862012-07-02 17:23:22 +0800970static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
971{
972 if (reg & 0xffff0001) {
973 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
974 return true;
975 }
976 return false;
977}
978
979DECLARE_RTL_COND(rtl_ocp_gphy_cond)
980{
981 void __iomem *ioaddr = tp->mmio_addr;
982
983 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
984}
985
986static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
987{
988 void __iomem *ioaddr = tp->mmio_addr;
989
990 if (rtl_ocp_reg_failure(tp, reg))
991 return;
992
993 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
994
995 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
996}
997
998static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
999{
1000 void __iomem *ioaddr = tp->mmio_addr;
1001
1002 if (rtl_ocp_reg_failure(tp, reg))
1003 return 0;
1004
1005 RTL_W32(GPHY_OCP, reg << 15);
1006
1007 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1008 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1009}
1010
Hayes Wangc5583862012-07-02 17:23:22 +08001011static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1012{
1013 void __iomem *ioaddr = tp->mmio_addr;
1014
1015 if (rtl_ocp_reg_failure(tp, reg))
1016 return;
1017
1018 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001019}
1020
1021static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1022{
1023 void __iomem *ioaddr = tp->mmio_addr;
1024
1025 if (rtl_ocp_reg_failure(tp, reg))
1026 return 0;
1027
1028 RTL_W32(OCPDR, reg << 15);
1029
Hayes Wang3a83ad12012-07-11 20:31:56 +08001030 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001031}
1032
1033#define OCP_STD_PHY_BASE 0xa400
1034
1035static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1036{
1037 if (reg == 0x1f) {
1038 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1039 return;
1040 }
1041
1042 if (tp->ocp_base != OCP_STD_PHY_BASE)
1043 reg -= 0x10;
1044
1045 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1046}
1047
1048static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1049{
1050 if (tp->ocp_base != OCP_STD_PHY_BASE)
1051 reg -= 0x10;
1052
1053 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1054}
1055
hayeswangeee37862013-04-01 22:23:38 +00001056static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1057{
1058 if (reg == 0x1f) {
1059 tp->ocp_base = value << 4;
1060 return;
1061 }
1062
1063 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1064}
1065
1066static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1067{
1068 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1069}
1070
Francois Romieuffc46952012-07-06 14:19:23 +02001071DECLARE_RTL_COND(rtl_phyar_cond)
1072{
1073 void __iomem *ioaddr = tp->mmio_addr;
1074
1075 return RTL_R32(PHYAR) & 0x80000000;
1076}
1077
Francois Romieu24192212012-07-06 20:19:42 +02001078static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Francois Romieu24192212012-07-06 20:19:42 +02001080 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Francois Romieu24192212012-07-06 20:19:42 +02001082 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Francois Romieuffc46952012-07-06 14:19:23 +02001084 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001085 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001086 * According to hardware specs a 20us delay is required after write
1087 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001088 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001089 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Francois Romieu24192212012-07-06 20:19:42 +02001092static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Francois Romieu24192212012-07-06 20:19:42 +02001094 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001095 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Francois Romieu24192212012-07-06 20:19:42 +02001097 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Francois Romieuffc46952012-07-06 14:19:23 +02001099 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1100 RTL_R32(PHYAR) & 0xffff : ~0;
1101
Timo Teräs81a95f02010-06-09 17:31:48 -07001102 /*
1103 * According to hardware specs a 20us delay is required after read
1104 * complete indication, but before sending next command.
1105 */
1106 udelay(20);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return value;
1109}
1110
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001111DECLARE_RTL_COND(rtl_ocpar_cond)
1112{
1113 void __iomem *ioaddr = tp->mmio_addr;
1114
1115 return RTL_R32(OCPAR) & OCPAR_FLAG;
1116}
1117
Francois Romieu24192212012-07-06 20:19:42 +02001118static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001119{
Francois Romieu24192212012-07-06 20:19:42 +02001120 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001121
Francois Romieu24192212012-07-06 20:19:42 +02001122 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001123 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1124 RTL_W32(EPHY_RXER_NUM, 0);
1125
Francois Romieuffc46952012-07-06 14:19:23 +02001126 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001127}
1128
Francois Romieu24192212012-07-06 20:19:42 +02001129static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001130{
Francois Romieu24192212012-07-06 20:19:42 +02001131 r8168dp_1_mdio_access(tp, reg,
1132 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001133}
1134
Francois Romieu24192212012-07-06 20:19:42 +02001135static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001136{
Francois Romieu24192212012-07-06 20:19:42 +02001137 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001138
Francois Romieu24192212012-07-06 20:19:42 +02001139 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001140
1141 mdelay(1);
1142 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1143 RTL_W32(EPHY_RXER_NUM, 0);
1144
Francois Romieuffc46952012-07-06 14:19:23 +02001145 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1146 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001147}
1148
françois romieue6de30d2011-01-03 15:08:37 +00001149#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1150
1151static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1152{
1153 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1154}
1155
1156static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1157{
1158 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1159}
1160
Francois Romieu24192212012-07-06 20:19:42 +02001161static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001162{
Francois Romieu24192212012-07-06 20:19:42 +02001163 void __iomem *ioaddr = tp->mmio_addr;
1164
françois romieue6de30d2011-01-03 15:08:37 +00001165 r8168dp_2_mdio_start(ioaddr);
1166
Francois Romieu24192212012-07-06 20:19:42 +02001167 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001168
1169 r8168dp_2_mdio_stop(ioaddr);
1170}
1171
Francois Romieu24192212012-07-06 20:19:42 +02001172static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001173{
Francois Romieu24192212012-07-06 20:19:42 +02001174 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001175 int value;
1176
1177 r8168dp_2_mdio_start(ioaddr);
1178
Francois Romieu24192212012-07-06 20:19:42 +02001179 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001180
1181 r8168dp_2_mdio_stop(ioaddr);
1182
1183 return value;
1184}
1185
françois romieu4da19632011-01-03 15:07:55 +00001186static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001187{
Francois Romieu24192212012-07-06 20:19:42 +02001188 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001189}
1190
françois romieu4da19632011-01-03 15:07:55 +00001191static int rtl_readphy(struct rtl8169_private *tp, int location)
1192{
Francois Romieu24192212012-07-06 20:19:42 +02001193 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001194}
1195
1196static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1197{
1198 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1199}
1200
Chun-Hao Lin76564422014-10-01 23:17:17 +08001201static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001202{
1203 int val;
1204
françois romieu4da19632011-01-03 15:07:55 +00001205 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001206 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001207}
1208
Francois Romieuccdffb92008-07-26 14:26:06 +02001209static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1210 int val)
1211{
1212 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001213
françois romieu4da19632011-01-03 15:07:55 +00001214 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001215}
1216
1217static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1218{
1219 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001220
françois romieu4da19632011-01-03 15:07:55 +00001221 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001222}
1223
Francois Romieuffc46952012-07-06 14:19:23 +02001224DECLARE_RTL_COND(rtl_ephyar_cond)
1225{
1226 void __iomem *ioaddr = tp->mmio_addr;
1227
1228 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1229}
1230
Francois Romieufdf6fc02012-07-06 22:40:38 +02001231static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001232{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001233 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001234
1235 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1236 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1237
Francois Romieuffc46952012-07-06 14:19:23 +02001238 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1239
1240 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001241}
1242
Francois Romieufdf6fc02012-07-06 22:40:38 +02001243static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001244{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001245 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001246
1247 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1248
Francois Romieuffc46952012-07-06 14:19:23 +02001249 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1250 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001251}
1252
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001253DECLARE_RTL_COND(rtl_eriar_cond)
1254{
1255 void __iomem *ioaddr = tp->mmio_addr;
1256
1257 return RTL_R32(ERIAR) & ERIAR_FLAG;
1258}
1259
Francois Romieufdf6fc02012-07-06 22:40:38 +02001260static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1261 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001262{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001263 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001264
1265 BUG_ON((addr & 3) || (mask == 0));
1266 RTL_W32(ERIDR, val);
1267 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1268
Francois Romieuffc46952012-07-06 14:19:23 +02001269 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001270}
1271
Francois Romieufdf6fc02012-07-06 22:40:38 +02001272static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001273{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001274 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001275
1276 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1277
Francois Romieuffc46952012-07-06 14:19:23 +02001278 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1279 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001280}
1281
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001282static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001283 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001284{
1285 u32 val;
1286
Francois Romieufdf6fc02012-07-06 22:40:38 +02001287 val = rtl_eri_read(tp, addr, type);
1288 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001289}
1290
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001291static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1292{
1293 void __iomem *ioaddr = tp->mmio_addr;
1294
1295 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1296 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1297 RTL_R32(OCPDR) : ~0;
1298}
1299
1300static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1301{
1302 return rtl_eri_read(tp, reg, ERIAR_OOB);
1303}
1304
1305static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1306{
1307 switch (tp->mac_version) {
1308 case RTL_GIGA_MAC_VER_27:
1309 case RTL_GIGA_MAC_VER_28:
1310 case RTL_GIGA_MAC_VER_31:
1311 return r8168dp_ocp_read(tp, mask, reg);
1312 case RTL_GIGA_MAC_VER_49:
1313 case RTL_GIGA_MAC_VER_50:
1314 case RTL_GIGA_MAC_VER_51:
1315 return r8168ep_ocp_read(tp, mask, reg);
1316 default:
1317 BUG();
1318 return ~0;
1319 }
1320}
1321
1322static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1323 u32 data)
1324{
1325 void __iomem *ioaddr = tp->mmio_addr;
1326
1327 RTL_W32(OCPDR, data);
1328 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1329 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1330}
1331
1332static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1333 u32 data)
1334{
1335 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1336 data, ERIAR_OOB);
1337}
1338
1339static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1340{
1341 switch (tp->mac_version) {
1342 case RTL_GIGA_MAC_VER_27:
1343 case RTL_GIGA_MAC_VER_28:
1344 case RTL_GIGA_MAC_VER_31:
1345 r8168dp_ocp_write(tp, mask, reg, data);
1346 break;
1347 case RTL_GIGA_MAC_VER_49:
1348 case RTL_GIGA_MAC_VER_50:
1349 case RTL_GIGA_MAC_VER_51:
1350 r8168ep_ocp_write(tp, mask, reg, data);
1351 break;
1352 default:
1353 BUG();
1354 break;
1355 }
1356}
1357
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001358static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1359{
1360 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1361
1362 ocp_write(tp, 0x1, 0x30, 0x00000001);
1363}
1364
1365#define OOB_CMD_RESET 0x00
1366#define OOB_CMD_DRIVER_START 0x05
1367#define OOB_CMD_DRIVER_STOP 0x06
1368
1369static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1370{
1371 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1372}
1373
1374DECLARE_RTL_COND(rtl_ocp_read_cond)
1375{
1376 u16 reg;
1377
1378 reg = rtl8168_get_ocp_reg(tp);
1379
1380 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1381}
1382
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001383DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1384{
1385 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1386}
1387
1388DECLARE_RTL_COND(rtl_ocp_tx_cond)
1389{
1390 void __iomem *ioaddr = tp->mmio_addr;
1391
Chunhao Lin086ca232018-01-31 01:32:36 +08001392 return RTL_R8(IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001393}
1394
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001395static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1396{
1397 void __iomem *ioaddr = tp->mmio_addr;
1398
1399 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001400 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001401 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1402 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1403}
1404
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001405static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001406{
1407 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001408 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1409}
1410
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001411static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1412{
1413 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1414 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1415 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1416}
1417
1418static void rtl8168_driver_start(struct rtl8169_private *tp)
1419{
1420 switch (tp->mac_version) {
1421 case RTL_GIGA_MAC_VER_27:
1422 case RTL_GIGA_MAC_VER_28:
1423 case RTL_GIGA_MAC_VER_31:
1424 rtl8168dp_driver_start(tp);
1425 break;
1426 case RTL_GIGA_MAC_VER_49:
1427 case RTL_GIGA_MAC_VER_50:
1428 case RTL_GIGA_MAC_VER_51:
1429 rtl8168ep_driver_start(tp);
1430 break;
1431 default:
1432 BUG();
1433 break;
1434 }
1435}
1436
1437static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1438{
1439 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1440 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1441}
1442
1443static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1444{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001445 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001446 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1447 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1448 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1449}
1450
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001451static void rtl8168_driver_stop(struct rtl8169_private *tp)
1452{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001453 switch (tp->mac_version) {
1454 case RTL_GIGA_MAC_VER_27:
1455 case RTL_GIGA_MAC_VER_28:
1456 case RTL_GIGA_MAC_VER_31:
1457 rtl8168dp_driver_stop(tp);
1458 break;
1459 case RTL_GIGA_MAC_VER_49:
1460 case RTL_GIGA_MAC_VER_50:
1461 case RTL_GIGA_MAC_VER_51:
1462 rtl8168ep_driver_stop(tp);
1463 break;
1464 default:
1465 BUG();
1466 break;
1467 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001468}
1469
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001470static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001471{
1472 u16 reg = rtl8168_get_ocp_reg(tp);
1473
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001474 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001475}
1476
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001477static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001478{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001479 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001480}
1481
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001482static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001483{
1484 switch (tp->mac_version) {
1485 case RTL_GIGA_MAC_VER_27:
1486 case RTL_GIGA_MAC_VER_28:
1487 case RTL_GIGA_MAC_VER_31:
1488 return r8168dp_check_dash(tp);
1489 case RTL_GIGA_MAC_VER_49:
1490 case RTL_GIGA_MAC_VER_50:
1491 case RTL_GIGA_MAC_VER_51:
1492 return r8168ep_check_dash(tp);
1493 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001494 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001495 }
1496}
1497
françois romieuc28aa382011-08-02 03:53:43 +00001498struct exgmac_reg {
1499 u16 addr;
1500 u16 mask;
1501 u32 val;
1502};
1503
Francois Romieufdf6fc02012-07-06 22:40:38 +02001504static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001505 const struct exgmac_reg *r, int len)
1506{
1507 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001508 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001509 r++;
1510 }
1511}
1512
Francois Romieuffc46952012-07-06 14:19:23 +02001513DECLARE_RTL_COND(rtl_efusear_cond)
1514{
1515 void __iomem *ioaddr = tp->mmio_addr;
1516
1517 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1518}
1519
Francois Romieufdf6fc02012-07-06 22:40:38 +02001520static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001521{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001522 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001523
1524 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1525
Francois Romieuffc46952012-07-06 14:19:23 +02001526 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1527 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001528}
1529
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001530static u16 rtl_get_events(struct rtl8169_private *tp)
1531{
1532 void __iomem *ioaddr = tp->mmio_addr;
1533
1534 return RTL_R16(IntrStatus);
1535}
1536
1537static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1538{
1539 void __iomem *ioaddr = tp->mmio_addr;
1540
1541 RTL_W16(IntrStatus, bits);
1542 mmiowb();
1543}
1544
1545static void rtl_irq_disable(struct rtl8169_private *tp)
1546{
1547 void __iomem *ioaddr = tp->mmio_addr;
1548
1549 RTL_W16(IntrMask, 0);
1550 mmiowb();
1551}
1552
Francois Romieu3e990ff2012-01-26 12:50:01 +01001553static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1554{
1555 void __iomem *ioaddr = tp->mmio_addr;
1556
1557 RTL_W16(IntrMask, bits);
1558}
1559
Francois Romieuda78dbf2012-01-26 14:18:23 +01001560#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1561#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1562#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1563
1564static void rtl_irq_enable_all(struct rtl8169_private *tp)
1565{
1566 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1567}
1568
françois romieu811fd302011-12-04 20:30:45 +00001569static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
françois romieu811fd302011-12-04 20:30:45 +00001571 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001573 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001574 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001575 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
françois romieu4da19632011-01-03 15:07:55 +00001578static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
françois romieu4da19632011-01-03 15:07:55 +00001580 void __iomem *ioaddr = tp->mmio_addr;
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return RTL_R32(TBICSR) & TBIReset;
1583}
1584
françois romieu4da19632011-01-03 15:07:55 +00001585static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
françois romieu4da19632011-01-03 15:07:55 +00001587 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1591{
1592 return RTL_R32(TBICSR) & TBILinkOk;
1593}
1594
1595static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1596{
1597 return RTL_R8(PHYstatus) & LinkStatus;
1598}
1599
françois romieu4da19632011-01-03 15:07:55 +00001600static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
françois romieu4da19632011-01-03 15:07:55 +00001602 void __iomem *ioaddr = tp->mmio_addr;
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1605}
1606
françois romieu4da19632011-01-03 15:07:55 +00001607static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 unsigned int val;
1610
françois romieu4da19632011-01-03 15:07:55 +00001611 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1612 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Hayes Wang70090422011-07-06 15:58:06 +08001615static void rtl_link_chg_patch(struct rtl8169_private *tp)
1616{
1617 void __iomem *ioaddr = tp->mmio_addr;
1618 struct net_device *dev = tp->dev;
1619
1620 if (!netif_running(dev))
1621 return;
1622
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001623 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1624 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001625 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001626 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1627 ERIAR_EXGMAC);
1628 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1629 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001630 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001631 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1632 ERIAR_EXGMAC);
1633 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1634 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001635 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001636 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1637 ERIAR_EXGMAC);
1638 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1639 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001640 }
1641 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001642 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001643 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001644 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001645 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001646 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1647 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1648 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001649 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1650 ERIAR_EXGMAC);
1651 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1652 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001653 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001654 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1655 ERIAR_EXGMAC);
1656 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1657 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001658 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001659 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1660 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001661 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1662 ERIAR_EXGMAC);
1663 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1664 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001665 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001666 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1667 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001668 }
Hayes Wang70090422011-07-06 15:58:06 +08001669 }
1670}
1671
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001672static void rtl8169_check_link_status(struct net_device *dev,
1673 struct rtl8169_private *tp,
1674 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001677 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001678 /* This is to cancel a scheduled suspend if there's one. */
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01001679 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001681 if (net_ratelimit())
1682 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001683 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001685 netif_info(tp, ifdown, dev, "link down\n");
Heiner Kallweita92a0842018-01-08 21:39:13 +01001686 pm_runtime_idle(&tp->pci_dev->dev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001690#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1691
1692static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1693{
1694 void __iomem *ioaddr = tp->mmio_addr;
1695 u8 options;
1696 u32 wolopts = 0;
1697
1698 options = RTL_R8(Config1);
1699 if (!(options & PMEnable))
1700 return 0;
1701
1702 options = RTL_R8(Config3);
1703 if (options & LinkUp)
1704 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001705 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001706 case RTL_GIGA_MAC_VER_34:
1707 case RTL_GIGA_MAC_VER_35:
1708 case RTL_GIGA_MAC_VER_36:
1709 case RTL_GIGA_MAC_VER_37:
1710 case RTL_GIGA_MAC_VER_38:
1711 case RTL_GIGA_MAC_VER_40:
1712 case RTL_GIGA_MAC_VER_41:
1713 case RTL_GIGA_MAC_VER_42:
1714 case RTL_GIGA_MAC_VER_43:
1715 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001716 case RTL_GIGA_MAC_VER_45:
1717 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001718 case RTL_GIGA_MAC_VER_47:
1719 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001720 case RTL_GIGA_MAC_VER_49:
1721 case RTL_GIGA_MAC_VER_50:
1722 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001723 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1724 wolopts |= WAKE_MAGIC;
1725 break;
1726 default:
1727 if (options & MagicPacket)
1728 wolopts |= WAKE_MAGIC;
1729 break;
1730 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001731
1732 options = RTL_R8(Config5);
1733 if (options & UWF)
1734 wolopts |= WAKE_UCAST;
1735 if (options & BWF)
1736 wolopts |= WAKE_BCAST;
1737 if (options & MWF)
1738 wolopts |= WAKE_MCAST;
1739
1740 return wolopts;
1741}
1742
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001743static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1744{
1745 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001746 struct device *d = &tp->pci_dev->dev;
1747
1748 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001749
Francois Romieuda78dbf2012-01-26 14:18:23 +01001750 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001751
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001752 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001753 if (pm_runtime_active(d))
1754 wol->wolopts = __rtl8169_get_wol(tp);
1755 else
1756 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001757
Francois Romieuda78dbf2012-01-26 14:18:23 +01001758 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001759
1760 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001761}
1762
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001763static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001764{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001765 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001766 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001767 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001768 u32 opt;
1769 u16 reg;
1770 u8 mask;
1771 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001772 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001773 { WAKE_UCAST, Config5, UWF },
1774 { WAKE_BCAST, Config5, BWF },
1775 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001776 { WAKE_ANY, Config5, LanWake },
1777 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001778 };
Francois Romieu851e6022012-04-17 11:10:11 +02001779 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001780
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001781 RTL_W8(Cfg9346, Cfg9346_Unlock);
1782
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001783 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001784 case RTL_GIGA_MAC_VER_34:
1785 case RTL_GIGA_MAC_VER_35:
1786 case RTL_GIGA_MAC_VER_36:
1787 case RTL_GIGA_MAC_VER_37:
1788 case RTL_GIGA_MAC_VER_38:
1789 case RTL_GIGA_MAC_VER_40:
1790 case RTL_GIGA_MAC_VER_41:
1791 case RTL_GIGA_MAC_VER_42:
1792 case RTL_GIGA_MAC_VER_43:
1793 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001794 case RTL_GIGA_MAC_VER_45:
1795 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001796 case RTL_GIGA_MAC_VER_47:
1797 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001798 case RTL_GIGA_MAC_VER_49:
1799 case RTL_GIGA_MAC_VER_50:
1800 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001801 tmp = ARRAY_SIZE(cfg) - 1;
1802 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001803 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001804 0x0dc,
1805 ERIAR_MASK_0100,
1806 MagicPacket_v2,
1807 0x0000,
1808 ERIAR_EXGMAC);
1809 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001810 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001811 0x0dc,
1812 ERIAR_MASK_0100,
1813 0x0000,
1814 MagicPacket_v2,
1815 ERIAR_EXGMAC);
1816 break;
1817 default:
1818 tmp = ARRAY_SIZE(cfg);
1819 break;
1820 }
1821
1822 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001823 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001824 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001825 options |= cfg[i].mask;
1826 RTL_W8(cfg[i].reg, options);
1827 }
1828
Francois Romieu851e6022012-04-17 11:10:11 +02001829 switch (tp->mac_version) {
1830 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1831 options = RTL_R8(Config1) & ~PMEnable;
1832 if (wolopts)
1833 options |= PMEnable;
1834 RTL_W8(Config1, options);
1835 break;
1836 default:
Francois Romieud387b422012-04-17 11:12:01 +02001837 options = RTL_R8(Config2) & ~PME_SIGNAL;
1838 if (wolopts)
1839 options |= PME_SIGNAL;
1840 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001841 break;
1842 }
1843
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001844 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001845}
1846
1847static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1848{
1849 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001850 struct device *d = &tp->pci_dev->dev;
1851
1852 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001853
Francois Romieuda78dbf2012-01-26 14:18:23 +01001854 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001855
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001856 if (pm_runtime_active(d))
1857 __rtl8169_set_wol(tp, wol->wolopts);
1858 else
1859 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001860
1861 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001862
françois romieuea809072010-11-08 13:23:58 +00001863 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1864
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001865 pm_runtime_put_noidle(d);
1866
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001867 return 0;
1868}
1869
Francois Romieu31bd2042011-04-26 18:58:59 +02001870static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1871{
Francois Romieu85bffe62011-04-27 08:22:39 +02001872 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875static void rtl8169_get_drvinfo(struct net_device *dev,
1876 struct ethtool_drvinfo *info)
1877{
1878 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001879 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Rick Jones68aad782011-11-07 13:29:27 +00001881 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1882 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1883 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001884 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001885 if (!IS_ERR_OR_NULL(rtl_fw))
1886 strlcpy(info->fw_version, rtl_fw->version,
1887 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
1890static int rtl8169_get_regs_len(struct net_device *dev)
1891{
1892 return R8169_REGS_SIZE;
1893}
1894
1895static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001896 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct rtl8169_private *tp = netdev_priv(dev);
1899 void __iomem *ioaddr = tp->mmio_addr;
1900 int ret = 0;
1901 u32 reg;
1902
1903 reg = RTL_R32(TBICSR);
1904 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1905 (duplex == DUPLEX_FULL)) {
1906 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1907 } else if (autoneg == AUTONEG_ENABLE)
1908 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1909 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001910 netif_warn(tp, link, dev,
1911 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 ret = -EOPNOTSUPP;
1913 }
1914
1915 return ret;
1916}
1917
1918static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001919 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001922 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001923 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Hayes Wang716b50a2011-02-22 17:26:18 +08001925 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001928 int auto_nego;
1929
françois romieu4da19632011-01-03 15:07:55 +00001930 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001931 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1932 ADVERTISE_100HALF | ADVERTISE_100FULL);
1933
1934 if (adv & ADVERTISED_10baseT_Half)
1935 auto_nego |= ADVERTISE_10HALF;
1936 if (adv & ADVERTISED_10baseT_Full)
1937 auto_nego |= ADVERTISE_10FULL;
1938 if (adv & ADVERTISED_100baseT_Half)
1939 auto_nego |= ADVERTISE_100HALF;
1940 if (adv & ADVERTISED_100baseT_Full)
1941 auto_nego |= ADVERTISE_100FULL;
1942
françois romieu3577aa12009-05-19 10:46:48 +00001943 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1944
françois romieu4da19632011-01-03 15:07:55 +00001945 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001946 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1947
1948 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001949 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001950 if (adv & ADVERTISED_1000baseT_Half)
1951 giga_ctrl |= ADVERTISE_1000HALF;
1952 if (adv & ADVERTISED_1000baseT_Full)
1953 giga_ctrl |= ADVERTISE_1000FULL;
1954 } else if (adv & (ADVERTISED_1000baseT_Half |
1955 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001956 netif_info(tp, link, dev,
1957 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001958 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
françois romieu3577aa12009-05-19 10:46:48 +00001961 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001962
françois romieu4da19632011-01-03 15:07:55 +00001963 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1964 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001965 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001966 if (speed == SPEED_10)
1967 bmcr = 0;
1968 else if (speed == SPEED_100)
1969 bmcr = BMCR_SPEED100;
1970 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001971 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001972
1973 if (duplex == DUPLEX_FULL)
1974 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001975 }
1976
françois romieu4da19632011-01-03 15:07:55 +00001977 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001978
Francois Romieucecb5fd2011-04-01 10:21:07 +02001979 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1980 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001981 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001982 rtl_writephy(tp, 0x17, 0x2138);
1983 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001984 } else {
françois romieu4da19632011-01-03 15:07:55 +00001985 rtl_writephy(tp, 0x17, 0x2108);
1986 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001987 }
1988 }
1989
Oliver Neukum54405cd2011-01-06 21:55:13 +01001990 rc = 0;
1991out:
1992 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001996 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct rtl8169_private *tp = netdev_priv(dev);
1999 int ret;
2000
Oliver Neukum54405cd2011-01-06 21:55:13 +01002001 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002002 if (ret < 0)
2003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Francois Romieu4876cc12011-03-11 21:07:11 +01002005 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002006 (advertising & ADVERTISED_1000baseT_Full) &&
2007 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002009 }
2010out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return ret;
2012}
2013
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002014static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2015 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Francois Romieud58d46b2011-05-03 16:38:29 +02002017 struct rtl8169_private *tp = netdev_priv(dev);
2018
Francois Romieu2b7b4312011-04-18 22:53:24 -07002019 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002020 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Francois Romieud58d46b2011-05-03 16:38:29 +02002022 if (dev->mtu > JUMBO_1K &&
2023 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2024 features &= ~NETIF_F_IP_CSUM;
2025
Michał Mirosław350fb322011-04-08 06:35:56 +00002026 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Francois Romieuda78dbf2012-01-26 14:18:23 +01002029static void __rtl8169_set_features(struct net_device *dev,
2030 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002033 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002034 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
hayeswang929a0312014-09-16 11:40:47 +08002036 rx_config = RTL_R32(RxConfig);
2037 if (features & NETIF_F_RXALL)
2038 rx_config |= (AcceptErr | AcceptRunt);
2039 else
2040 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
hayeswang929a0312014-09-16 11:40:47 +08002042 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002043
hayeswang929a0312014-09-16 11:40:47 +08002044 if (features & NETIF_F_RXCSUM)
2045 tp->cp_cmd |= RxChkSum;
2046 else
2047 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002048
hayeswang929a0312014-09-16 11:40:47 +08002049 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2050 tp->cp_cmd |= RxVlan;
2051 else
2052 tp->cp_cmd &= ~RxVlan;
2053
2054 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2055
2056 RTL_W16(CPlusCmd, tp->cp_cmd);
2057 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Francois Romieuda78dbf2012-01-26 14:18:23 +01002060static int rtl8169_set_features(struct net_device *dev,
2061 netdev_features_t features)
2062{
2063 struct rtl8169_private *tp = netdev_priv(dev);
2064
hayeswang929a0312014-09-16 11:40:47 +08002065 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2066
Francois Romieuda78dbf2012-01-26 14:18:23 +01002067 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002068 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002069 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002070 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 return 0;
2073}
2074
Francois Romieuda78dbf2012-01-26 14:18:23 +01002075
Kirill Smelkov810f4892012-11-10 21:11:02 +04002076static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002078 return (skb_vlan_tag_present(skb)) ?
2079 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Francois Romieu7a8fc772011-03-01 17:18:33 +01002082static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Francois Romieu7a8fc772011-03-01 17:18:33 +01002086 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002087 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002090static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2091 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct rtl8169_private *tp = netdev_priv(dev);
2094 void __iomem *ioaddr = tp->mmio_addr;
2095 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002096 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002098 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002100 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 status = RTL_R32(TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002103 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2104 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002106 cmd->base.speed = SPEED_1000;
2107 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2108
2109 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2110 supported);
2111 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2112 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002113
2114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002117static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2118 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002122 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2123
2124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002127static int rtl8169_get_link_ksettings(struct net_device *dev,
2128 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002131 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Francois Romieuda78dbf2012-01-26 14:18:23 +01002133 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002134 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002135 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Francois Romieuccdffb92008-07-26 14:26:06 +02002137 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002140static int rtl8169_set_link_ksettings(struct net_device *dev,
2141 const struct ethtool_link_ksettings *cmd)
2142{
2143 struct rtl8169_private *tp = netdev_priv(dev);
2144 int rc;
2145 u32 advertising;
2146
2147 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2148 cmd->link_modes.advertising))
2149 return -EINVAL;
2150
2151 del_timer_sync(&tp->timer);
2152
2153 rtl_lock_work(tp);
2154 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2155 cmd->base.duplex, advertising);
2156 rtl_unlock_work(tp);
2157
2158 return rc;
2159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2162 void *p)
2163{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002164 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002165 u32 __iomem *data = tp->mmio_addr;
2166 u32 *dw = p;
2167 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Francois Romieuda78dbf2012-01-26 14:18:23 +01002169 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002170 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2171 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002172 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002175static u32 rtl8169_get_msglevel(struct net_device *dev)
2176{
2177 struct rtl8169_private *tp = netdev_priv(dev);
2178
2179 return tp->msg_enable;
2180}
2181
2182static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2183{
2184 struct rtl8169_private *tp = netdev_priv(dev);
2185
2186 tp->msg_enable = value;
2187}
2188
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002189static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2190 "tx_packets",
2191 "rx_packets",
2192 "tx_errors",
2193 "rx_errors",
2194 "rx_missed",
2195 "align_errors",
2196 "tx_single_collisions",
2197 "tx_multi_collisions",
2198 "unicast",
2199 "broadcast",
2200 "multicast",
2201 "tx_aborted",
2202 "tx_underrun",
2203};
2204
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002205static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002206{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002207 switch (sset) {
2208 case ETH_SS_STATS:
2209 return ARRAY_SIZE(rtl8169_gstrings);
2210 default:
2211 return -EOPNOTSUPP;
2212 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002213}
2214
Corinna Vinschen42020322015-09-10 10:47:35 +02002215DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002216{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002217 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002218
Corinna Vinschen42020322015-09-10 10:47:35 +02002219 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002220}
2221
Corinna Vinschen42020322015-09-10 10:47:35 +02002222static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002223{
2224 struct rtl8169_private *tp = netdev_priv(dev);
2225 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002226 dma_addr_t paddr = tp->counters_phys_addr;
2227 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02002228
2229 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Francois Romieua78e9362018-01-26 01:53:26 +01002230 RTL_R32(CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02002231 cmd = (u64)paddr & DMA_BIT_MASK(32);
2232 RTL_W32(CounterAddrLow, cmd);
2233 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2234
Francois Romieua78e9362018-01-26 01:53:26 +01002235 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002236}
2237
2238static bool rtl8169_reset_counters(struct net_device *dev)
2239{
2240 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002241
2242 /*
2243 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2244 * tally counters.
2245 */
2246 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2247 return true;
2248
Corinna Vinschen42020322015-09-10 10:47:35 +02002249 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002250}
2251
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002252static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002253{
2254 struct rtl8169_private *tp = netdev_priv(dev);
2255 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002256
Ivan Vecera355423d2009-02-06 21:49:57 -08002257 /*
2258 * Some chips are unable to dump tally counters when the receiver
2259 * is disabled.
2260 */
2261 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002262 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002263
Corinna Vinschen42020322015-09-10 10:47:35 +02002264 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002265}
2266
2267static bool rtl8169_init_counter_offsets(struct net_device *dev)
2268{
2269 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002270 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002271 bool ret = false;
2272
2273 /*
2274 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2275 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2276 * reset by a power cycle, while the counter values collected by the
2277 * driver are reset at every driver unload/load cycle.
2278 *
2279 * To make sure the HW values returned by @get_stats64 match the SW
2280 * values, we collect the initial values at first open(*) and use them
2281 * as offsets to normalize the values returned by @get_stats64.
2282 *
2283 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2284 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2285 * set at open time by rtl_hw_start.
2286 */
2287
2288 if (tp->tc_offset.inited)
2289 return true;
2290
2291 /* If both, reset and update fail, propagate to caller. */
2292 if (rtl8169_reset_counters(dev))
2293 ret = true;
2294
2295 if (rtl8169_update_counters(dev))
2296 ret = true;
2297
Corinna Vinschen42020322015-09-10 10:47:35 +02002298 tp->tc_offset.tx_errors = counters->tx_errors;
2299 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2300 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002301 tp->tc_offset.inited = true;
2302
2303 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002304}
2305
Ivan Vecera355423d2009-02-06 21:49:57 -08002306static void rtl8169_get_ethtool_stats(struct net_device *dev,
2307 struct ethtool_stats *stats, u64 *data)
2308{
2309 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002310 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002311 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002312
2313 ASSERT_RTNL();
2314
Chun-Hao Line0636232016-07-29 16:37:55 +08002315 pm_runtime_get_noresume(d);
2316
2317 if (pm_runtime_active(d))
2318 rtl8169_update_counters(dev);
2319
2320 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002321
Corinna Vinschen42020322015-09-10 10:47:35 +02002322 data[0] = le64_to_cpu(counters->tx_packets);
2323 data[1] = le64_to_cpu(counters->rx_packets);
2324 data[2] = le64_to_cpu(counters->tx_errors);
2325 data[3] = le32_to_cpu(counters->rx_errors);
2326 data[4] = le16_to_cpu(counters->rx_missed);
2327 data[5] = le16_to_cpu(counters->align_errors);
2328 data[6] = le32_to_cpu(counters->tx_one_collision);
2329 data[7] = le32_to_cpu(counters->tx_multi_collision);
2330 data[8] = le64_to_cpu(counters->rx_unicast);
2331 data[9] = le64_to_cpu(counters->rx_broadcast);
2332 data[10] = le32_to_cpu(counters->rx_multicast);
2333 data[11] = le16_to_cpu(counters->tx_aborted);
2334 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002335}
2336
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002337static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2338{
2339 switch(stringset) {
2340 case ETH_SS_STATS:
2341 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2342 break;
2343 }
2344}
2345
Florian Fainellif0903ea2016-12-03 12:01:19 -08002346static int rtl8169_nway_reset(struct net_device *dev)
2347{
2348 struct rtl8169_private *tp = netdev_priv(dev);
2349
2350 return mii_nway_restart(&tp->mii);
2351}
2352
Francois Romieu50970832017-10-27 13:24:49 +03002353/*
2354 * Interrupt coalescing
2355 *
2356 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2357 * > 8169, 8168 and 810x line of chipsets
2358 *
2359 * 8169, 8168, and 8136(810x) serial chipsets support it.
2360 *
2361 * > 2 - the Tx timer unit at gigabit speed
2362 *
2363 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2364 * (0xe0) bit 1 and bit 0.
2365 *
2366 * For 8169
2367 * bit[1:0] \ speed 1000M 100M 10M
2368 * 0 0 320ns 2.56us 40.96us
2369 * 0 1 2.56us 20.48us 327.7us
2370 * 1 0 5.12us 40.96us 655.4us
2371 * 1 1 10.24us 81.92us 1.31ms
2372 *
2373 * For the other
2374 * bit[1:0] \ speed 1000M 100M 10M
2375 * 0 0 5us 2.56us 40.96us
2376 * 0 1 40us 20.48us 327.7us
2377 * 1 0 80us 40.96us 655.4us
2378 * 1 1 160us 81.92us 1.31ms
2379 */
2380
2381/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2382struct rtl_coalesce_scale {
2383 /* Rx / Tx */
2384 u32 nsecs[2];
2385};
2386
2387/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2388struct rtl_coalesce_info {
2389 u32 speed;
2390 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2391};
2392
2393/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2394#define rxtx_x1822(r, t) { \
2395 {{(r), (t)}}, \
2396 {{(r)*8, (t)*8}}, \
2397 {{(r)*8*2, (t)*8*2}}, \
2398 {{(r)*8*2*2, (t)*8*2*2}}, \
2399}
2400static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2401 /* speed delays: rx00 tx00 */
2402 { SPEED_10, rxtx_x1822(40960, 40960) },
2403 { SPEED_100, rxtx_x1822( 2560, 2560) },
2404 { SPEED_1000, rxtx_x1822( 320, 320) },
2405 { 0 },
2406};
2407
2408static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2409 /* speed delays: rx00 tx00 */
2410 { SPEED_10, rxtx_x1822(40960, 40960) },
2411 { SPEED_100, rxtx_x1822( 2560, 2560) },
2412 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2413 { 0 },
2414};
2415#undef rxtx_x1822
2416
2417/* get rx/tx scale vector corresponding to current speed */
2418static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2419{
2420 struct rtl8169_private *tp = netdev_priv(dev);
2421 struct ethtool_link_ksettings ecmd;
2422 const struct rtl_coalesce_info *ci;
2423 int rc;
2424
2425 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2426 if (rc < 0)
2427 return ERR_PTR(rc);
2428
2429 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2430 if (ecmd.base.speed == ci->speed) {
2431 return ci;
2432 }
2433 }
2434
2435 return ERR_PTR(-ELNRNG);
2436}
2437
2438static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2439{
2440 struct rtl8169_private *tp = netdev_priv(dev);
2441 void __iomem *ioaddr = tp->mmio_addr;
2442 const struct rtl_coalesce_info *ci;
2443 const struct rtl_coalesce_scale *scale;
2444 struct {
2445 u32 *max_frames;
2446 u32 *usecs;
2447 } coal_settings [] = {
2448 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2449 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2450 }, *p = coal_settings;
2451 int i;
2452 u16 w;
2453
2454 memset(ec, 0, sizeof(*ec));
2455
2456 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2457 ci = rtl_coalesce_info(dev);
2458 if (IS_ERR(ci))
2459 return PTR_ERR(ci);
2460
2461 scale = &ci->scalev[RTL_R16(CPlusCmd) & 3];
2462
2463 /* read IntrMitigate and adjust according to scale */
2464 for (w = RTL_R16(IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2465 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2466 w >>= RTL_COALESCE_SHIFT;
2467 *p->usecs = w & RTL_COALESCE_MASK;
2468 }
2469
2470 for (i = 0; i < 2; i++) {
2471 p = coal_settings + i;
2472 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2473
2474 /*
2475 * ethtool_coalesce says it is illegal to set both usecs and
2476 * max_frames to 0.
2477 */
2478 if (!*p->usecs && !*p->max_frames)
2479 *p->max_frames = 1;
2480 }
2481
2482 return 0;
2483}
2484
2485/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2486static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2487 struct net_device *dev, u32 nsec, u16 *cp01)
2488{
2489 const struct rtl_coalesce_info *ci;
2490 u16 i;
2491
2492 ci = rtl_coalesce_info(dev);
2493 if (IS_ERR(ci))
2494 return ERR_CAST(ci);
2495
2496 for (i = 0; i < 4; i++) {
2497 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2498 ci->scalev[i].nsecs[1]);
2499 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2500 *cp01 = i;
2501 return &ci->scalev[i];
2502 }
2503 }
2504
2505 return ERR_PTR(-EINVAL);
2506}
2507
2508static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2509{
2510 struct rtl8169_private *tp = netdev_priv(dev);
2511 void __iomem *ioaddr = tp->mmio_addr;
2512 const struct rtl_coalesce_scale *scale;
2513 struct {
2514 u32 frames;
2515 u32 usecs;
2516 } coal_settings [] = {
2517 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2518 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2519 }, *p = coal_settings;
2520 u16 w = 0, cp01;
2521 int i;
2522
2523 scale = rtl_coalesce_choose_scale(dev,
2524 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2525 if (IS_ERR(scale))
2526 return PTR_ERR(scale);
2527
2528 for (i = 0; i < 2; i++, p++) {
2529 u32 units;
2530
2531 /*
2532 * accept max_frames=1 we returned in rtl_get_coalesce.
2533 * accept it not only when usecs=0 because of e.g. the following scenario:
2534 *
2535 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2536 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2537 * - then user does `ethtool -C eth0 rx-usecs 100`
2538 *
2539 * since ethtool sends to kernel whole ethtool_coalesce
2540 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2541 * we'll reject it below in `frames % 4 != 0`.
2542 */
2543 if (p->frames == 1) {
2544 p->frames = 0;
2545 }
2546
2547 units = p->usecs * 1000 / scale->nsecs[i];
2548 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2549 return -EINVAL;
2550
2551 w <<= RTL_COALESCE_SHIFT;
2552 w |= units;
2553 w <<= RTL_COALESCE_SHIFT;
2554 w |= p->frames >> 2;
2555 }
2556
2557 rtl_lock_work(tp);
2558
2559 RTL_W16(IntrMitigate, swab16(w));
2560
2561 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2562 RTL_W16(CPlusCmd, tp->cp_cmd);
2563 RTL_R16(CPlusCmd);
2564
2565 rtl_unlock_work(tp);
2566
2567 return 0;
2568}
2569
Jeff Garzik7282d492006-09-13 14:30:00 -04002570static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .get_drvinfo = rtl8169_get_drvinfo,
2572 .get_regs_len = rtl8169_get_regs_len,
2573 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002574 .get_coalesce = rtl_get_coalesce,
2575 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002576 .get_msglevel = rtl8169_get_msglevel,
2577 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002579 .get_wol = rtl8169_get_wol,
2580 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002581 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002582 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002583 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002584 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002585 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002586 .get_link_ksettings = rtl8169_get_link_ksettings,
Tobias Jakobi9e77d7a2017-11-21 16:15:57 +01002587 .set_link_ksettings = rtl8169_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588};
2589
Francois Romieu07d3f512007-02-21 22:40:46 +01002590static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002591 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Francois Romieu5d320a22011-05-08 17:47:36 +02002593 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002594 /*
2595 * The driver currently handles the 8168Bf and the 8168Be identically
2596 * but they can be identified more specifically through the test below
2597 * if needed:
2598 *
2599 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002600 *
2601 * Same thing for the 8101Eb and the 8101Ec:
2602 *
2603 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002604 */
Francois Romieu37441002011-06-17 22:58:54 +02002605 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002607 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 int mac_version;
2609 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002610 /* 8168EP family. */
2611 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2612 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2613 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2614
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002615 /* 8168H family. */
2616 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2617 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2618
Hayes Wangc5583862012-07-02 17:23:22 +08002619 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002620 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002621 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002622 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2623 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2624
Hayes Wangc2218922011-09-06 16:55:18 +08002625 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002626 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002627 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2628 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2629
hayeswang01dc7fe2011-03-21 01:50:28 +00002630 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002631 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002632 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2633 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2634 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2635
Francois Romieu5b538df2008-07-20 16:22:45 +02002636 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002637 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2638 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002639 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002640
françois romieue6de30d2011-01-03 15:08:37 +00002641 /* 8168DP family. */
2642 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2643 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002644 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002645
Francois Romieuef808d52008-06-29 13:10:54 +02002646 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002647 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002648 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002649 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002650 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002651 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2652 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002653 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002654 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002655 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002656
2657 /* 8168B family. */
2658 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2659 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2660 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2661 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2662
2663 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002664 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2665 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002666 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002667 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002668 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2669 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2670 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002671 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2672 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2673 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2674 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2675 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2676 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002677 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002678 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002679 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002680 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2681 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002682 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2683 /* FIXME: where did these entries come from ? -- FR */
2684 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2685 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2686
2687 /* 8110 family. */
2688 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2689 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2690 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2691 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2692 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2693 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2694
Jean Delvaref21b75e2009-05-26 20:54:48 -07002695 /* Catch-all */
2696 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002697 };
2698 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 u32 reg;
2700
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002701 reg = RTL_R32(TxConfig);
2702 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 p++;
2704 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002705
2706 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2707 netif_notice(tp, probe, dev,
2708 "unknown MAC, using family default\n");
2709 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002710 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2711 tp->mac_version = tp->mii.supports_gmii ?
2712 RTL_GIGA_MAC_VER_42 :
2713 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002714 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2715 tp->mac_version = tp->mii.supports_gmii ?
2716 RTL_GIGA_MAC_VER_45 :
2717 RTL_GIGA_MAC_VER_47;
2718 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2719 tp->mac_version = tp->mii.supports_gmii ?
2720 RTL_GIGA_MAC_VER_46 :
2721 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
2725static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2726{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002727 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
Francois Romieu867763c2007-08-17 18:21:58 +02002730struct phy_reg {
2731 u16 reg;
2732 u16 val;
2733};
2734
françois romieu4da19632011-01-03 15:07:55 +00002735static void rtl_writephy_batch(struct rtl8169_private *tp,
2736 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002737{
2738 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002739 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002740 regs++;
2741 }
2742}
2743
françois romieubca03d52011-01-03 15:07:31 +00002744#define PHY_READ 0x00000000
2745#define PHY_DATA_OR 0x10000000
2746#define PHY_DATA_AND 0x20000000
2747#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002748#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002749#define PHY_CLEAR_READCOUNT 0x70000000
2750#define PHY_WRITE 0x80000000
2751#define PHY_READCOUNT_EQ_SKIP 0x90000000
2752#define PHY_COMP_EQ_SKIPN 0xa0000000
2753#define PHY_COMP_NEQ_SKIPN 0xb0000000
2754#define PHY_WRITE_PREVIOUS 0xc0000000
2755#define PHY_SKIPN 0xd0000000
2756#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002757
Hayes Wang960aee62011-06-18 11:37:48 +02002758struct fw_info {
2759 u32 magic;
2760 char version[RTL_VER_SIZE];
2761 __le32 fw_start;
2762 __le32 fw_len;
2763 u8 chksum;
2764} __packed;
2765
Francois Romieu1c361ef2011-06-17 17:16:24 +02002766#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2767
2768static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002769{
Francois Romieub6ffd972011-06-17 17:00:05 +02002770 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002771 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002772 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2773 char *version = rtl_fw->version;
2774 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002775
Francois Romieu1c361ef2011-06-17 17:16:24 +02002776 if (fw->size < FW_OPCODE_SIZE)
2777 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002778
2779 if (!fw_info->magic) {
2780 size_t i, size, start;
2781 u8 checksum = 0;
2782
2783 if (fw->size < sizeof(*fw_info))
2784 goto out;
2785
2786 for (i = 0; i < fw->size; i++)
2787 checksum += fw->data[i];
2788 if (checksum != 0)
2789 goto out;
2790
2791 start = le32_to_cpu(fw_info->fw_start);
2792 if (start > fw->size)
2793 goto out;
2794
2795 size = le32_to_cpu(fw_info->fw_len);
2796 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2797 goto out;
2798
2799 memcpy(version, fw_info->version, RTL_VER_SIZE);
2800
2801 pa->code = (__le32 *)(fw->data + start);
2802 pa->size = size;
2803 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002804 if (fw->size % FW_OPCODE_SIZE)
2805 goto out;
2806
2807 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2808
2809 pa->code = (__le32 *)fw->data;
2810 pa->size = fw->size / FW_OPCODE_SIZE;
2811 }
2812 version[RTL_VER_SIZE - 1] = 0;
2813
2814 rc = true;
2815out:
2816 return rc;
2817}
2818
Francois Romieufd112f22011-06-18 00:10:29 +02002819static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2820 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002821{
Francois Romieufd112f22011-06-18 00:10:29 +02002822 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002823 size_t index;
2824
Francois Romieu1c361ef2011-06-17 17:16:24 +02002825 for (index = 0; index < pa->size; index++) {
2826 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002827 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002828
hayeswang42b82dc2011-01-10 02:07:25 +00002829 switch(action & 0xf0000000) {
2830 case PHY_READ:
2831 case PHY_DATA_OR:
2832 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002833 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002834 case PHY_CLEAR_READCOUNT:
2835 case PHY_WRITE:
2836 case PHY_WRITE_PREVIOUS:
2837 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002838 break;
2839
hayeswang42b82dc2011-01-10 02:07:25 +00002840 case PHY_BJMPN:
2841 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002842 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002843 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002844 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002845 }
2846 break;
2847 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002848 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002849 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002850 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002851 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002852 }
2853 break;
2854 case PHY_COMP_EQ_SKIPN:
2855 case PHY_COMP_NEQ_SKIPN:
2856 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002857 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002858 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002859 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002860 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002861 }
2862 break;
2863
hayeswang42b82dc2011-01-10 02:07:25 +00002864 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002865 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002866 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002867 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002868 }
2869 }
Francois Romieufd112f22011-06-18 00:10:29 +02002870 rc = true;
2871out:
2872 return rc;
2873}
françois romieubca03d52011-01-03 15:07:31 +00002874
Francois Romieufd112f22011-06-18 00:10:29 +02002875static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2876{
2877 struct net_device *dev = tp->dev;
2878 int rc = -EINVAL;
2879
2880 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002881 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002882 goto out;
2883 }
2884
2885 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2886 rc = 0;
2887out:
2888 return rc;
2889}
2890
2891static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2892{
2893 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002894 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002895 u32 predata, count;
2896 size_t index;
2897
2898 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002899 org.write = ops->write;
2900 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002901
Francois Romieu1c361ef2011-06-17 17:16:24 +02002902 for (index = 0; index < pa->size; ) {
2903 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002904 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002905 u32 regno = (action & 0x0fff0000) >> 16;
2906
2907 if (!action)
2908 break;
françois romieubca03d52011-01-03 15:07:31 +00002909
2910 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002911 case PHY_READ:
2912 predata = rtl_readphy(tp, regno);
2913 count++;
2914 index++;
françois romieubca03d52011-01-03 15:07:31 +00002915 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002916 case PHY_DATA_OR:
2917 predata |= data;
2918 index++;
2919 break;
2920 case PHY_DATA_AND:
2921 predata &= data;
2922 index++;
2923 break;
2924 case PHY_BJMPN:
2925 index -= regno;
2926 break;
hayeswangeee37862013-04-01 22:23:38 +00002927 case PHY_MDIO_CHG:
2928 if (data == 0) {
2929 ops->write = org.write;
2930 ops->read = org.read;
2931 } else if (data == 1) {
2932 ops->write = mac_mcu_write;
2933 ops->read = mac_mcu_read;
2934 }
2935
hayeswang42b82dc2011-01-10 02:07:25 +00002936 index++;
2937 break;
2938 case PHY_CLEAR_READCOUNT:
2939 count = 0;
2940 index++;
2941 break;
2942 case PHY_WRITE:
2943 rtl_writephy(tp, regno, data);
2944 index++;
2945 break;
2946 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002947 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002948 break;
2949 case PHY_COMP_EQ_SKIPN:
2950 if (predata == data)
2951 index += regno;
2952 index++;
2953 break;
2954 case PHY_COMP_NEQ_SKIPN:
2955 if (predata != data)
2956 index += regno;
2957 index++;
2958 break;
2959 case PHY_WRITE_PREVIOUS:
2960 rtl_writephy(tp, regno, predata);
2961 index++;
2962 break;
2963 case PHY_SKIPN:
2964 index += regno + 1;
2965 break;
2966 case PHY_DELAY_MS:
2967 mdelay(data);
2968 index++;
2969 break;
2970
françois romieubca03d52011-01-03 15:07:31 +00002971 default:
2972 BUG();
2973 }
2974 }
hayeswangeee37862013-04-01 22:23:38 +00002975
2976 ops->write = org.write;
2977 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002978}
2979
françois romieuf1e02ed2011-01-13 13:07:53 +00002980static void rtl_release_firmware(struct rtl8169_private *tp)
2981{
Francois Romieub6ffd972011-06-17 17:00:05 +02002982 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2983 release_firmware(tp->rtl_fw->fw);
2984 kfree(tp->rtl_fw);
2985 }
2986 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002987}
2988
François Romieu953a12c2011-04-24 17:38:48 +02002989static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002990{
Francois Romieub6ffd972011-06-17 17:00:05 +02002991 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002992
2993 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002994 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002995 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002996}
2997
2998static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2999{
3000 if (rtl_readphy(tp, reg) != val)
3001 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
3002 else
3003 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00003004}
3005
françois romieu4da19632011-01-03 15:07:55 +00003006static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003008 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00003009 { 0x1f, 0x0001 },
3010 { 0x06, 0x006e },
3011 { 0x08, 0x0708 },
3012 { 0x15, 0x4000 },
3013 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
françois romieu0b9b5712009-08-10 19:44:56 +00003015 { 0x1f, 0x0001 },
3016 { 0x03, 0x00a1 },
3017 { 0x02, 0x0008 },
3018 { 0x01, 0x0120 },
3019 { 0x00, 0x1000 },
3020 { 0x04, 0x0800 },
3021 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
françois romieu0b9b5712009-08-10 19:44:56 +00003023 { 0x03, 0xff41 },
3024 { 0x02, 0xdf60 },
3025 { 0x01, 0x0140 },
3026 { 0x00, 0x0077 },
3027 { 0x04, 0x7800 },
3028 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
françois romieu0b9b5712009-08-10 19:44:56 +00003030 { 0x03, 0x802f },
3031 { 0x02, 0x4f02 },
3032 { 0x01, 0x0409 },
3033 { 0x00, 0xf0f9 },
3034 { 0x04, 0x9800 },
3035 { 0x04, 0x9000 },
3036
3037 { 0x03, 0xdf01 },
3038 { 0x02, 0xdf20 },
3039 { 0x01, 0xff95 },
3040 { 0x00, 0xba00 },
3041 { 0x04, 0xa800 },
3042 { 0x04, 0xa000 },
3043
3044 { 0x03, 0xff41 },
3045 { 0x02, 0xdf20 },
3046 { 0x01, 0x0140 },
3047 { 0x00, 0x00bb },
3048 { 0x04, 0xb800 },
3049 { 0x04, 0xb000 },
3050
3051 { 0x03, 0xdf41 },
3052 { 0x02, 0xdc60 },
3053 { 0x01, 0x6340 },
3054 { 0x00, 0x007d },
3055 { 0x04, 0xd800 },
3056 { 0x04, 0xd000 },
3057
3058 { 0x03, 0xdf01 },
3059 { 0x02, 0xdf20 },
3060 { 0x01, 0x100a },
3061 { 0x00, 0xa0ff },
3062 { 0x04, 0xf800 },
3063 { 0x04, 0xf000 },
3064
3065 { 0x1f, 0x0000 },
3066 { 0x0b, 0x0000 },
3067 { 0x00, 0x9200 }
3068 };
3069
françois romieu4da19632011-01-03 15:07:55 +00003070 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
françois romieu4da19632011-01-03 15:07:55 +00003073static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003074{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003075 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003076 { 0x1f, 0x0002 },
3077 { 0x01, 0x90d0 },
3078 { 0x1f, 0x0000 }
3079 };
3080
françois romieu4da19632011-01-03 15:07:55 +00003081 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003082}
3083
françois romieu4da19632011-01-03 15:07:55 +00003084static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003085{
3086 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003087
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003088 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3089 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003090 return;
3091
françois romieu4da19632011-01-03 15:07:55 +00003092 rtl_writephy(tp, 0x1f, 0x0001);
3093 rtl_writephy(tp, 0x10, 0xf01b);
3094 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003095}
3096
françois romieu4da19632011-01-03 15:07:55 +00003097static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003098{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003099 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003100 { 0x1f, 0x0001 },
3101 { 0x04, 0x0000 },
3102 { 0x03, 0x00a1 },
3103 { 0x02, 0x0008 },
3104 { 0x01, 0x0120 },
3105 { 0x00, 0x1000 },
3106 { 0x04, 0x0800 },
3107 { 0x04, 0x9000 },
3108 { 0x03, 0x802f },
3109 { 0x02, 0x4f02 },
3110 { 0x01, 0x0409 },
3111 { 0x00, 0xf099 },
3112 { 0x04, 0x9800 },
3113 { 0x04, 0xa000 },
3114 { 0x03, 0xdf01 },
3115 { 0x02, 0xdf20 },
3116 { 0x01, 0xff95 },
3117 { 0x00, 0xba00 },
3118 { 0x04, 0xa800 },
3119 { 0x04, 0xf000 },
3120 { 0x03, 0xdf01 },
3121 { 0x02, 0xdf20 },
3122 { 0x01, 0x101a },
3123 { 0x00, 0xa0ff },
3124 { 0x04, 0xf800 },
3125 { 0x04, 0x0000 },
3126 { 0x1f, 0x0000 },
3127
3128 { 0x1f, 0x0001 },
3129 { 0x10, 0xf41b },
3130 { 0x14, 0xfb54 },
3131 { 0x18, 0xf5c7 },
3132 { 0x1f, 0x0000 },
3133
3134 { 0x1f, 0x0001 },
3135 { 0x17, 0x0cc0 },
3136 { 0x1f, 0x0000 }
3137 };
3138
françois romieu4da19632011-01-03 15:07:55 +00003139 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003140
françois romieu4da19632011-01-03 15:07:55 +00003141 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003142}
3143
françois romieu4da19632011-01-03 15:07:55 +00003144static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003145{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003146 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003147 { 0x1f, 0x0001 },
3148 { 0x04, 0x0000 },
3149 { 0x03, 0x00a1 },
3150 { 0x02, 0x0008 },
3151 { 0x01, 0x0120 },
3152 { 0x00, 0x1000 },
3153 { 0x04, 0x0800 },
3154 { 0x04, 0x9000 },
3155 { 0x03, 0x802f },
3156 { 0x02, 0x4f02 },
3157 { 0x01, 0x0409 },
3158 { 0x00, 0xf099 },
3159 { 0x04, 0x9800 },
3160 { 0x04, 0xa000 },
3161 { 0x03, 0xdf01 },
3162 { 0x02, 0xdf20 },
3163 { 0x01, 0xff95 },
3164 { 0x00, 0xba00 },
3165 { 0x04, 0xa800 },
3166 { 0x04, 0xf000 },
3167 { 0x03, 0xdf01 },
3168 { 0x02, 0xdf20 },
3169 { 0x01, 0x101a },
3170 { 0x00, 0xa0ff },
3171 { 0x04, 0xf800 },
3172 { 0x04, 0x0000 },
3173 { 0x1f, 0x0000 },
3174
3175 { 0x1f, 0x0001 },
3176 { 0x0b, 0x8480 },
3177 { 0x1f, 0x0000 },
3178
3179 { 0x1f, 0x0001 },
3180 { 0x18, 0x67c7 },
3181 { 0x04, 0x2000 },
3182 { 0x03, 0x002f },
3183 { 0x02, 0x4360 },
3184 { 0x01, 0x0109 },
3185 { 0x00, 0x3022 },
3186 { 0x04, 0x2800 },
3187 { 0x1f, 0x0000 },
3188
3189 { 0x1f, 0x0001 },
3190 { 0x17, 0x0cc0 },
3191 { 0x1f, 0x0000 }
3192 };
3193
françois romieu4da19632011-01-03 15:07:55 +00003194 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003195}
3196
françois romieu4da19632011-01-03 15:07:55 +00003197static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003198{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003199 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003200 { 0x10, 0xf41b },
3201 { 0x1f, 0x0000 }
3202 };
3203
françois romieu4da19632011-01-03 15:07:55 +00003204 rtl_writephy(tp, 0x1f, 0x0001);
3205 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003206
françois romieu4da19632011-01-03 15:07:55 +00003207 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003208}
3209
françois romieu4da19632011-01-03 15:07:55 +00003210static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003211{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003212 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003213 { 0x1f, 0x0001 },
3214 { 0x10, 0xf41b },
3215 { 0x1f, 0x0000 }
3216 };
3217
françois romieu4da19632011-01-03 15:07:55 +00003218 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003219}
3220
françois romieu4da19632011-01-03 15:07:55 +00003221static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003222{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003223 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003224 { 0x1f, 0x0000 },
3225 { 0x1d, 0x0f00 },
3226 { 0x1f, 0x0002 },
3227 { 0x0c, 0x1ec8 },
3228 { 0x1f, 0x0000 }
3229 };
3230
françois romieu4da19632011-01-03 15:07:55 +00003231 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003232}
3233
françois romieu4da19632011-01-03 15:07:55 +00003234static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003235{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003236 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003237 { 0x1f, 0x0001 },
3238 { 0x1d, 0x3d98 },
3239 { 0x1f, 0x0000 }
3240 };
3241
françois romieu4da19632011-01-03 15:07:55 +00003242 rtl_writephy(tp, 0x1f, 0x0000);
3243 rtl_patchphy(tp, 0x14, 1 << 5);
3244 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003245
françois romieu4da19632011-01-03 15:07:55 +00003246 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003247}
3248
françois romieu4da19632011-01-03 15:07:55 +00003249static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003250{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003251 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003252 { 0x1f, 0x0001 },
3253 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003254 { 0x1f, 0x0002 },
3255 { 0x00, 0x88d4 },
3256 { 0x01, 0x82b1 },
3257 { 0x03, 0x7002 },
3258 { 0x08, 0x9e30 },
3259 { 0x09, 0x01f0 },
3260 { 0x0a, 0x5500 },
3261 { 0x0c, 0x00c8 },
3262 { 0x1f, 0x0003 },
3263 { 0x12, 0xc096 },
3264 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003265 { 0x1f, 0x0000 },
3266 { 0x1f, 0x0000 },
3267 { 0x09, 0x2000 },
3268 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003269 };
3270
françois romieu4da19632011-01-03 15:07:55 +00003271 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003272
françois romieu4da19632011-01-03 15:07:55 +00003273 rtl_patchphy(tp, 0x14, 1 << 5);
3274 rtl_patchphy(tp, 0x0d, 1 << 5);
3275 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003276}
3277
françois romieu4da19632011-01-03 15:07:55 +00003278static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003279{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003280 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003281 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003282 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003283 { 0x03, 0x802f },
3284 { 0x02, 0x4f02 },
3285 { 0x01, 0x0409 },
3286 { 0x00, 0xf099 },
3287 { 0x04, 0x9800 },
3288 { 0x04, 0x9000 },
3289 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003290 { 0x1f, 0x0002 },
3291 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003292 { 0x06, 0x0761 },
3293 { 0x1f, 0x0003 },
3294 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003295 { 0x1f, 0x0000 }
3296 };
3297
françois romieu4da19632011-01-03 15:07:55 +00003298 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003299
françois romieu4da19632011-01-03 15:07:55 +00003300 rtl_patchphy(tp, 0x16, 1 << 0);
3301 rtl_patchphy(tp, 0x14, 1 << 5);
3302 rtl_patchphy(tp, 0x0d, 1 << 5);
3303 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003304}
3305
françois romieu4da19632011-01-03 15:07:55 +00003306static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003307{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003308 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003309 { 0x1f, 0x0001 },
3310 { 0x12, 0x2300 },
3311 { 0x1d, 0x3d98 },
3312 { 0x1f, 0x0002 },
3313 { 0x0c, 0x7eb8 },
3314 { 0x06, 0x5461 },
3315 { 0x1f, 0x0003 },
3316 { 0x16, 0x0f0a },
3317 { 0x1f, 0x0000 }
3318 };
3319
françois romieu4da19632011-01-03 15:07:55 +00003320 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003321
françois romieu4da19632011-01-03 15:07:55 +00003322 rtl_patchphy(tp, 0x16, 1 << 0);
3323 rtl_patchphy(tp, 0x14, 1 << 5);
3324 rtl_patchphy(tp, 0x0d, 1 << 5);
3325 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003326}
3327
françois romieu4da19632011-01-03 15:07:55 +00003328static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003329{
françois romieu4da19632011-01-03 15:07:55 +00003330 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003331}
3332
françois romieubca03d52011-01-03 15:07:31 +00003333static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003334{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003335 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003336 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003337 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003338 { 0x06, 0x4064 },
3339 { 0x07, 0x2863 },
3340 { 0x08, 0x059c },
3341 { 0x09, 0x26b4 },
3342 { 0x0a, 0x6a19 },
3343 { 0x0b, 0xdcc8 },
3344 { 0x10, 0xf06d },
3345 { 0x14, 0x7f68 },
3346 { 0x18, 0x7fd9 },
3347 { 0x1c, 0xf0ff },
3348 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003349 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003350 { 0x12, 0xf49f },
3351 { 0x13, 0x070b },
3352 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003353 { 0x14, 0x94c0 },
3354
3355 /*
3356 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003357 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003358 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003359 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003360 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003361 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003362 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003363 { 0x06, 0x5561 },
3364
3365 /*
3366 * Can not link to 1Gbps with bad cable
3367 * Decrease SNR threshold form 21.07dB to 19.04dB
3368 */
3369 { 0x1f, 0x0001 },
3370 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003371
3372 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003373 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003374 };
3375
françois romieu4da19632011-01-03 15:07:55 +00003376 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003377
françois romieubca03d52011-01-03 15:07:31 +00003378 /*
3379 * Rx Error Issue
3380 * Fine Tune Switching regulator parameter
3381 */
françois romieu4da19632011-01-03 15:07:55 +00003382 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003383 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3384 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003385
Francois Romieufdf6fc02012-07-06 22:40:38 +02003386 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003387 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003388 { 0x1f, 0x0002 },
3389 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003390 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003391 { 0x05, 0x8330 },
3392 { 0x06, 0x669a },
3393 { 0x1f, 0x0002 }
3394 };
3395 int val;
3396
françois romieu4da19632011-01-03 15:07:55 +00003397 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003398
françois romieu4da19632011-01-03 15:07:55 +00003399 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003400
3401 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003402 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003403 0x0065, 0x0066, 0x0067, 0x0068,
3404 0x0069, 0x006a, 0x006b, 0x006c
3405 };
3406 int i;
3407
françois romieu4da19632011-01-03 15:07:55 +00003408 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003409
3410 val &= 0xff00;
3411 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003412 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003413 }
3414 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003415 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003416 { 0x1f, 0x0002 },
3417 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003418 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003419 { 0x05, 0x8330 },
3420 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003421 };
3422
françois romieu4da19632011-01-03 15:07:55 +00003423 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003424 }
3425
françois romieubca03d52011-01-03 15:07:31 +00003426 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003427 rtl_writephy(tp, 0x1f, 0x0002);
3428 rtl_patchphy(tp, 0x0d, 0x0300);
3429 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003430
françois romieubca03d52011-01-03 15:07:31 +00003431 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003432 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003433 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3434 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003435
françois romieu4da19632011-01-03 15:07:55 +00003436 rtl_writephy(tp, 0x1f, 0x0005);
3437 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003438
3439 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003440
françois romieu4da19632011-01-03 15:07:55 +00003441 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003442}
3443
françois romieubca03d52011-01-03 15:07:31 +00003444static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003445{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003446 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003447 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003448 { 0x1f, 0x0001 },
3449 { 0x06, 0x4064 },
3450 { 0x07, 0x2863 },
3451 { 0x08, 0x059c },
3452 { 0x09, 0x26b4 },
3453 { 0x0a, 0x6a19 },
3454 { 0x0b, 0xdcc8 },
3455 { 0x10, 0xf06d },
3456 { 0x14, 0x7f68 },
3457 { 0x18, 0x7fd9 },
3458 { 0x1c, 0xf0ff },
3459 { 0x1d, 0x3d9c },
3460 { 0x1f, 0x0003 },
3461 { 0x12, 0xf49f },
3462 { 0x13, 0x070b },
3463 { 0x1a, 0x05ad },
3464 { 0x14, 0x94c0 },
3465
françois romieubca03d52011-01-03 15:07:31 +00003466 /*
3467 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003468 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003469 */
françois romieudaf9df62009-10-07 12:44:20 +00003470 { 0x1f, 0x0002 },
3471 { 0x06, 0x5561 },
3472 { 0x1f, 0x0005 },
3473 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003474 { 0x06, 0x5561 },
3475
3476 /*
3477 * Can not link to 1Gbps with bad cable
3478 * Decrease SNR threshold form 21.07dB to 19.04dB
3479 */
3480 { 0x1f, 0x0001 },
3481 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003482
3483 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003484 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003485 };
3486
françois romieu4da19632011-01-03 15:07:55 +00003487 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003488
Francois Romieufdf6fc02012-07-06 22:40:38 +02003489 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003490 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003491 { 0x1f, 0x0002 },
3492 { 0x05, 0x669a },
3493 { 0x1f, 0x0005 },
3494 { 0x05, 0x8330 },
3495 { 0x06, 0x669a },
3496
3497 { 0x1f, 0x0002 }
3498 };
3499 int val;
3500
françois romieu4da19632011-01-03 15:07:55 +00003501 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003502
françois romieu4da19632011-01-03 15:07:55 +00003503 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003504 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003505 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003506 0x0065, 0x0066, 0x0067, 0x0068,
3507 0x0069, 0x006a, 0x006b, 0x006c
3508 };
3509 int i;
3510
françois romieu4da19632011-01-03 15:07:55 +00003511 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003512
3513 val &= 0xff00;
3514 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003515 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003516 }
3517 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003518 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003519 { 0x1f, 0x0002 },
3520 { 0x05, 0x2642 },
3521 { 0x1f, 0x0005 },
3522 { 0x05, 0x8330 },
3523 { 0x06, 0x2642 }
3524 };
3525
françois romieu4da19632011-01-03 15:07:55 +00003526 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003527 }
3528
françois romieubca03d52011-01-03 15:07:31 +00003529 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003530 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003531 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3532 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003533
françois romieubca03d52011-01-03 15:07:31 +00003534 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003535 rtl_writephy(tp, 0x1f, 0x0002);
3536 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003537
françois romieu4da19632011-01-03 15:07:55 +00003538 rtl_writephy(tp, 0x1f, 0x0005);
3539 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003540
3541 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003542
françois romieu4da19632011-01-03 15:07:55 +00003543 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003544}
3545
françois romieu4da19632011-01-03 15:07:55 +00003546static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003547{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003548 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003549 { 0x1f, 0x0002 },
3550 { 0x10, 0x0008 },
3551 { 0x0d, 0x006c },
3552
3553 { 0x1f, 0x0000 },
3554 { 0x0d, 0xf880 },
3555
3556 { 0x1f, 0x0001 },
3557 { 0x17, 0x0cc0 },
3558
3559 { 0x1f, 0x0001 },
3560 { 0x0b, 0xa4d8 },
3561 { 0x09, 0x281c },
3562 { 0x07, 0x2883 },
3563 { 0x0a, 0x6b35 },
3564 { 0x1d, 0x3da4 },
3565 { 0x1c, 0xeffd },
3566 { 0x14, 0x7f52 },
3567 { 0x18, 0x7fc6 },
3568 { 0x08, 0x0601 },
3569 { 0x06, 0x4063 },
3570 { 0x10, 0xf074 },
3571 { 0x1f, 0x0003 },
3572 { 0x13, 0x0789 },
3573 { 0x12, 0xf4bd },
3574 { 0x1a, 0x04fd },
3575 { 0x14, 0x84b0 },
3576 { 0x1f, 0x0000 },
3577 { 0x00, 0x9200 },
3578
3579 { 0x1f, 0x0005 },
3580 { 0x01, 0x0340 },
3581 { 0x1f, 0x0001 },
3582 { 0x04, 0x4000 },
3583 { 0x03, 0x1d21 },
3584 { 0x02, 0x0c32 },
3585 { 0x01, 0x0200 },
3586 { 0x00, 0x5554 },
3587 { 0x04, 0x4800 },
3588 { 0x04, 0x4000 },
3589 { 0x04, 0xf000 },
3590 { 0x03, 0xdf01 },
3591 { 0x02, 0xdf20 },
3592 { 0x01, 0x101a },
3593 { 0x00, 0xa0ff },
3594 { 0x04, 0xf800 },
3595 { 0x04, 0xf000 },
3596 { 0x1f, 0x0000 },
3597
3598 { 0x1f, 0x0007 },
3599 { 0x1e, 0x0023 },
3600 { 0x16, 0x0000 },
3601 { 0x1f, 0x0000 }
3602 };
3603
françois romieu4da19632011-01-03 15:07:55 +00003604 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003605}
3606
françois romieue6de30d2011-01-03 15:08:37 +00003607static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3608{
3609 static const struct phy_reg phy_reg_init[] = {
3610 { 0x1f, 0x0001 },
3611 { 0x17, 0x0cc0 },
3612
3613 { 0x1f, 0x0007 },
3614 { 0x1e, 0x002d },
3615 { 0x18, 0x0040 },
3616 { 0x1f, 0x0000 }
3617 };
3618
3619 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3620 rtl_patchphy(tp, 0x0d, 1 << 5);
3621}
3622
Hayes Wang70090422011-07-06 15:58:06 +08003623static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003624{
3625 static const struct phy_reg phy_reg_init[] = {
3626 /* Enable Delay cap */
3627 { 0x1f, 0x0005 },
3628 { 0x05, 0x8b80 },
3629 { 0x06, 0xc896 },
3630 { 0x1f, 0x0000 },
3631
3632 /* Channel estimation fine tune */
3633 { 0x1f, 0x0001 },
3634 { 0x0b, 0x6c20 },
3635 { 0x07, 0x2872 },
3636 { 0x1c, 0xefff },
3637 { 0x1f, 0x0003 },
3638 { 0x14, 0x6420 },
3639 { 0x1f, 0x0000 },
3640
3641 /* Update PFM & 10M TX idle timer */
3642 { 0x1f, 0x0007 },
3643 { 0x1e, 0x002f },
3644 { 0x15, 0x1919 },
3645 { 0x1f, 0x0000 },
3646
3647 { 0x1f, 0x0007 },
3648 { 0x1e, 0x00ac },
3649 { 0x18, 0x0006 },
3650 { 0x1f, 0x0000 }
3651 };
3652
Francois Romieu15ecd032011-04-27 13:52:22 -07003653 rtl_apply_firmware(tp);
3654
hayeswang01dc7fe2011-03-21 01:50:28 +00003655 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3656
3657 /* DCO enable for 10M IDLE Power */
3658 rtl_writephy(tp, 0x1f, 0x0007);
3659 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003660 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003661 rtl_writephy(tp, 0x1f, 0x0000);
3662
3663 /* For impedance matching */
3664 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003665 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003666 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003667
3668 /* PHY auto speed down */
3669 rtl_writephy(tp, 0x1f, 0x0007);
3670 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003671 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003672 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003673 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003674
3675 rtl_writephy(tp, 0x1f, 0x0005);
3676 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003678 rtl_writephy(tp, 0x1f, 0x0000);
3679
3680 rtl_writephy(tp, 0x1f, 0x0005);
3681 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003682 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003683 rtl_writephy(tp, 0x1f, 0x0007);
3684 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003685 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003686 rtl_writephy(tp, 0x1f, 0x0006);
3687 rtl_writephy(tp, 0x00, 0x5a00);
3688 rtl_writephy(tp, 0x1f, 0x0000);
3689 rtl_writephy(tp, 0x0d, 0x0007);
3690 rtl_writephy(tp, 0x0e, 0x003c);
3691 rtl_writephy(tp, 0x0d, 0x4007);
3692 rtl_writephy(tp, 0x0e, 0x0000);
3693 rtl_writephy(tp, 0x0d, 0x0000);
3694}
3695
françois romieu9ecb9aa2012-12-07 11:20:21 +00003696static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3697{
3698 const u16 w[] = {
3699 addr[0] | (addr[1] << 8),
3700 addr[2] | (addr[3] << 8),
3701 addr[4] | (addr[5] << 8)
3702 };
3703 const struct exgmac_reg e[] = {
3704 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3705 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3706 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3707 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3708 };
3709
3710 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3711}
3712
Hayes Wang70090422011-07-06 15:58:06 +08003713static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3714{
3715 static const struct phy_reg phy_reg_init[] = {
3716 /* Enable Delay cap */
3717 { 0x1f, 0x0004 },
3718 { 0x1f, 0x0007 },
3719 { 0x1e, 0x00ac },
3720 { 0x18, 0x0006 },
3721 { 0x1f, 0x0002 },
3722 { 0x1f, 0x0000 },
3723 { 0x1f, 0x0000 },
3724
3725 /* Channel estimation fine tune */
3726 { 0x1f, 0x0003 },
3727 { 0x09, 0xa20f },
3728 { 0x1f, 0x0000 },
3729 { 0x1f, 0x0000 },
3730
3731 /* Green Setting */
3732 { 0x1f, 0x0005 },
3733 { 0x05, 0x8b5b },
3734 { 0x06, 0x9222 },
3735 { 0x05, 0x8b6d },
3736 { 0x06, 0x8000 },
3737 { 0x05, 0x8b76 },
3738 { 0x06, 0x8000 },
3739 { 0x1f, 0x0000 }
3740 };
3741
3742 rtl_apply_firmware(tp);
3743
3744 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3745
3746 /* For 4-corner performance improve */
3747 rtl_writephy(tp, 0x1f, 0x0005);
3748 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003749 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003750 rtl_writephy(tp, 0x1f, 0x0000);
3751
3752 /* PHY auto speed down */
3753 rtl_writephy(tp, 0x1f, 0x0004);
3754 rtl_writephy(tp, 0x1f, 0x0007);
3755 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003756 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003757 rtl_writephy(tp, 0x1f, 0x0002);
3758 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003759 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003760
3761 /* improve 10M EEE waveform */
3762 rtl_writephy(tp, 0x1f, 0x0005);
3763 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003764 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003765 rtl_writephy(tp, 0x1f, 0x0000);
3766
3767 /* Improve 2-pair detection performance */
3768 rtl_writephy(tp, 0x1f, 0x0005);
3769 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003770 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003771 rtl_writephy(tp, 0x1f, 0x0000);
3772
3773 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003774 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003775 rtl_writephy(tp, 0x1f, 0x0005);
3776 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003777 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003778 rtl_writephy(tp, 0x1f, 0x0004);
3779 rtl_writephy(tp, 0x1f, 0x0007);
3780 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003781 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003782 rtl_writephy(tp, 0x1f, 0x0002);
3783 rtl_writephy(tp, 0x1f, 0x0000);
3784 rtl_writephy(tp, 0x0d, 0x0007);
3785 rtl_writephy(tp, 0x0e, 0x003c);
3786 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003787 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003788 rtl_writephy(tp, 0x0d, 0x0000);
3789
3790 /* Green feature */
3791 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003792 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3793 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003794 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003795 rtl_writephy(tp, 0x1f, 0x0005);
3796 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3797 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003798
françois romieu9ecb9aa2012-12-07 11:20:21 +00003799 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3800 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003801}
3802
Hayes Wang5f886e02012-03-30 14:33:03 +08003803static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3804{
3805 /* For 4-corner performance improve */
3806 rtl_writephy(tp, 0x1f, 0x0005);
3807 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003808 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003809 rtl_writephy(tp, 0x1f, 0x0000);
3810
3811 /* PHY auto speed down */
3812 rtl_writephy(tp, 0x1f, 0x0007);
3813 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003814 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003815 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003816 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003817
3818 /* Improve 10M EEE waveform */
3819 rtl_writephy(tp, 0x1f, 0x0005);
3820 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003821 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003822 rtl_writephy(tp, 0x1f, 0x0000);
3823}
3824
Hayes Wangc2218922011-09-06 16:55:18 +08003825static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3826{
3827 static const struct phy_reg phy_reg_init[] = {
3828 /* Channel estimation fine tune */
3829 { 0x1f, 0x0003 },
3830 { 0x09, 0xa20f },
3831 { 0x1f, 0x0000 },
3832
3833 /* Modify green table for giga & fnet */
3834 { 0x1f, 0x0005 },
3835 { 0x05, 0x8b55 },
3836 { 0x06, 0x0000 },
3837 { 0x05, 0x8b5e },
3838 { 0x06, 0x0000 },
3839 { 0x05, 0x8b67 },
3840 { 0x06, 0x0000 },
3841 { 0x05, 0x8b70 },
3842 { 0x06, 0x0000 },
3843 { 0x1f, 0x0000 },
3844 { 0x1f, 0x0007 },
3845 { 0x1e, 0x0078 },
3846 { 0x17, 0x0000 },
3847 { 0x19, 0x00fb },
3848 { 0x1f, 0x0000 },
3849
3850 /* Modify green table for 10M */
3851 { 0x1f, 0x0005 },
3852 { 0x05, 0x8b79 },
3853 { 0x06, 0xaa00 },
3854 { 0x1f, 0x0000 },
3855
3856 /* Disable hiimpedance detection (RTCT) */
3857 { 0x1f, 0x0003 },
3858 { 0x01, 0x328a },
3859 { 0x1f, 0x0000 }
3860 };
3861
3862 rtl_apply_firmware(tp);
3863
3864 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3865
Hayes Wang5f886e02012-03-30 14:33:03 +08003866 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003867
3868 /* Improve 2-pair detection performance */
3869 rtl_writephy(tp, 0x1f, 0x0005);
3870 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003871 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003872 rtl_writephy(tp, 0x1f, 0x0000);
3873}
3874
3875static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3876{
3877 rtl_apply_firmware(tp);
3878
Hayes Wang5f886e02012-03-30 14:33:03 +08003879 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003880}
3881
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003882static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3883{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003884 static const struct phy_reg phy_reg_init[] = {
3885 /* Channel estimation fine tune */
3886 { 0x1f, 0x0003 },
3887 { 0x09, 0xa20f },
3888 { 0x1f, 0x0000 },
3889
3890 /* Modify green table for giga & fnet */
3891 { 0x1f, 0x0005 },
3892 { 0x05, 0x8b55 },
3893 { 0x06, 0x0000 },
3894 { 0x05, 0x8b5e },
3895 { 0x06, 0x0000 },
3896 { 0x05, 0x8b67 },
3897 { 0x06, 0x0000 },
3898 { 0x05, 0x8b70 },
3899 { 0x06, 0x0000 },
3900 { 0x1f, 0x0000 },
3901 { 0x1f, 0x0007 },
3902 { 0x1e, 0x0078 },
3903 { 0x17, 0x0000 },
3904 { 0x19, 0x00aa },
3905 { 0x1f, 0x0000 },
3906
3907 /* Modify green table for 10M */
3908 { 0x1f, 0x0005 },
3909 { 0x05, 0x8b79 },
3910 { 0x06, 0xaa00 },
3911 { 0x1f, 0x0000 },
3912
3913 /* Disable hiimpedance detection (RTCT) */
3914 { 0x1f, 0x0003 },
3915 { 0x01, 0x328a },
3916 { 0x1f, 0x0000 }
3917 };
3918
3919
3920 rtl_apply_firmware(tp);
3921
3922 rtl8168f_hw_phy_config(tp);
3923
3924 /* Improve 2-pair detection performance */
3925 rtl_writephy(tp, 0x1f, 0x0005);
3926 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003927 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003928 rtl_writephy(tp, 0x1f, 0x0000);
3929
3930 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3931
3932 /* Modify green table for giga */
3933 rtl_writephy(tp, 0x1f, 0x0005);
3934 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003935 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003936 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003937 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003938 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003939 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003940 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003941 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003942 rtl_writephy(tp, 0x05, 0x8a82);
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, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003945 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003946 rtl_writephy(tp, 0x05, 0x8a88);
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, 0x1f, 0x0000);
3949
3950 /* uc same-seed solution */
3951 rtl_writephy(tp, 0x1f, 0x0005);
3952 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003953 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003954 rtl_writephy(tp, 0x1f, 0x0000);
3955
3956 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003957 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003958 rtl_writephy(tp, 0x1f, 0x0005);
3959 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003960 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003961 rtl_writephy(tp, 0x1f, 0x0004);
3962 rtl_writephy(tp, 0x1f, 0x0007);
3963 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003964 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003965 rtl_writephy(tp, 0x1f, 0x0000);
3966 rtl_writephy(tp, 0x0d, 0x0007);
3967 rtl_writephy(tp, 0x0e, 0x003c);
3968 rtl_writephy(tp, 0x0d, 0x4007);
3969 rtl_writephy(tp, 0x0e, 0x0000);
3970 rtl_writephy(tp, 0x0d, 0x0000);
3971
3972 /* Green feature */
3973 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003974 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3975 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003976 rtl_writephy(tp, 0x1f, 0x0000);
3977}
3978
Hayes Wangc5583862012-07-02 17:23:22 +08003979static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3980{
Hayes Wangc5583862012-07-02 17:23:22 +08003981 rtl_apply_firmware(tp);
3982
hayeswang41f44d12013-04-01 22:23:36 +00003983 rtl_writephy(tp, 0x1f, 0x0a46);
3984 if (rtl_readphy(tp, 0x10) & 0x0100) {
3985 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003986 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003987 } else {
3988 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003989 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003990 }
Hayes Wangc5583862012-07-02 17:23:22 +08003991
hayeswang41f44d12013-04-01 22:23:36 +00003992 rtl_writephy(tp, 0x1f, 0x0a46);
3993 if (rtl_readphy(tp, 0x13) & 0x0100) {
3994 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003995 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003996 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003997 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003998 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003999 }
Hayes Wangc5583862012-07-02 17:23:22 +08004000
hayeswang41f44d12013-04-01 22:23:36 +00004001 /* Enable PHY auto speed down */
4002 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004003 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004004
hayeswangfe7524c2013-04-01 22:23:37 +00004005 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004006 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004007 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004008 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004009 rtl_writephy(tp, 0x1f, 0x0a43);
4010 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004011 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4012 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004013
hayeswang41f44d12013-04-01 22:23:36 +00004014 /* EEE auto-fallback function */
4015 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004016 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004017
hayeswang41f44d12013-04-01 22:23:36 +00004018 /* Enable UC LPF tune function */
4019 rtl_writephy(tp, 0x1f, 0x0a43);
4020 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004021 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004022
4023 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004024 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00004025
hayeswangfe7524c2013-04-01 22:23:37 +00004026 /* Improve SWR Efficiency */
4027 rtl_writephy(tp, 0x1f, 0x0bcd);
4028 rtl_writephy(tp, 0x14, 0x5065);
4029 rtl_writephy(tp, 0x14, 0xd065);
4030 rtl_writephy(tp, 0x1f, 0x0bc8);
4031 rtl_writephy(tp, 0x11, 0x5655);
4032 rtl_writephy(tp, 0x1f, 0x0bcd);
4033 rtl_writephy(tp, 0x14, 0x1065);
4034 rtl_writephy(tp, 0x14, 0x9065);
4035 rtl_writephy(tp, 0x14, 0x1065);
4036
David Chang1bac1072013-11-27 15:48:36 +08004037 /* Check ALDPS bit, disable it if enabled */
4038 rtl_writephy(tp, 0x1f, 0x0a43);
4039 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004040 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08004041
hayeswang41f44d12013-04-01 22:23:36 +00004042 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004043}
4044
hayeswang57538c42013-04-01 22:23:40 +00004045static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
4046{
4047 rtl_apply_firmware(tp);
4048}
4049
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004050static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
4051{
4052 u16 dout_tapbin;
4053 u32 data;
4054
4055 rtl_apply_firmware(tp);
4056
4057 /* CHN EST parameters adjust - giga master */
4058 rtl_writephy(tp, 0x1f, 0x0a43);
4059 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004060 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004061 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004062 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004063 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004064 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004065 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004066 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004067 rtl_writephy(tp, 0x1f, 0x0000);
4068
4069 /* CHN EST parameters adjust - giga slave */
4070 rtl_writephy(tp, 0x1f, 0x0a43);
4071 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004072 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004073 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004074 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004075 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004076 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004077 rtl_writephy(tp, 0x1f, 0x0000);
4078
4079 /* CHN EST parameters adjust - fnet */
4080 rtl_writephy(tp, 0x1f, 0x0a43);
4081 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004082 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004083 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004084 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004085 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004086 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004087 rtl_writephy(tp, 0x1f, 0x0000);
4088
4089 /* enable R-tune & PGA-retune function */
4090 dout_tapbin = 0;
4091 rtl_writephy(tp, 0x1f, 0x0a46);
4092 data = rtl_readphy(tp, 0x13);
4093 data &= 3;
4094 data <<= 2;
4095 dout_tapbin |= data;
4096 data = rtl_readphy(tp, 0x12);
4097 data &= 0xc000;
4098 data >>= 14;
4099 dout_tapbin |= data;
4100 dout_tapbin = ~(dout_tapbin^0x08);
4101 dout_tapbin <<= 12;
4102 dout_tapbin &= 0xf000;
4103 rtl_writephy(tp, 0x1f, 0x0a43);
4104 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004105 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004106 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004107 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004108 rtl_writephy(tp, 0x13, 0x827c);
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, 0x827d);
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
4113 rtl_writephy(tp, 0x1f, 0x0a43);
4114 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004115 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004116 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004117 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004118 rtl_writephy(tp, 0x1f, 0x0000);
4119
4120 /* enable GPHY 10M */
4121 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004122 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004123 rtl_writephy(tp, 0x1f, 0x0000);
4124
4125 /* SAR ADC performance */
4126 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004127 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004128 rtl_writephy(tp, 0x1f, 0x0000);
4129
4130 rtl_writephy(tp, 0x1f, 0x0a43);
4131 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004132 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004133 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004134 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004135 rtl_writephy(tp, 0x13, 0x804f);
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, 0x8057);
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, 0x805f);
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, 0x8067);
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, 0x806f);
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, 0x1f, 0x0000);
4146
4147 /* disable phy pfm mode */
4148 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004149 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004150 rtl_writephy(tp, 0x1f, 0x0000);
4151
4152 /* Check ALDPS bit, disable it if enabled */
4153 rtl_writephy(tp, 0x1f, 0x0a43);
4154 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004155 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004156
4157 rtl_writephy(tp, 0x1f, 0x0000);
4158}
4159
4160static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4161{
4162 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4163 u16 rlen;
4164 u32 data;
4165
4166 rtl_apply_firmware(tp);
4167
4168 /* CHIN EST parameter update */
4169 rtl_writephy(tp, 0x1f, 0x0a43);
4170 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004171 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004172 rtl_writephy(tp, 0x1f, 0x0000);
4173
4174 /* enable R-tune & PGA-retune function */
4175 rtl_writephy(tp, 0x1f, 0x0a43);
4176 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004177 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004178 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004179 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004180 rtl_writephy(tp, 0x1f, 0x0000);
4181
4182 /* enable GPHY 10M */
4183 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004184 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004185 rtl_writephy(tp, 0x1f, 0x0000);
4186
4187 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4188 data = r8168_mac_ocp_read(tp, 0xdd02);
4189 ioffset_p3 = ((data & 0x80)>>7);
4190 ioffset_p3 <<= 3;
4191
4192 data = r8168_mac_ocp_read(tp, 0xdd00);
4193 ioffset_p3 |= ((data & (0xe000))>>13);
4194 ioffset_p2 = ((data & (0x1e00))>>9);
4195 ioffset_p1 = ((data & (0x01e0))>>5);
4196 ioffset_p0 = ((data & 0x0010)>>4);
4197 ioffset_p0 <<= 3;
4198 ioffset_p0 |= (data & (0x07));
4199 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4200
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004201 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004202 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004203 rtl_writephy(tp, 0x1f, 0x0bcf);
4204 rtl_writephy(tp, 0x16, data);
4205 rtl_writephy(tp, 0x1f, 0x0000);
4206 }
4207
4208 /* Modify rlen (TX LPF corner frequency) level */
4209 rtl_writephy(tp, 0x1f, 0x0bcd);
4210 data = rtl_readphy(tp, 0x16);
4211 data &= 0x000f;
4212 rlen = 0;
4213 if (data > 3)
4214 rlen = data - 3;
4215 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4216 rtl_writephy(tp, 0x17, data);
4217 rtl_writephy(tp, 0x1f, 0x0bcd);
4218 rtl_writephy(tp, 0x1f, 0x0000);
4219
4220 /* disable phy pfm mode */
4221 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004222 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004223 rtl_writephy(tp, 0x1f, 0x0000);
4224
4225 /* Check ALDPS bit, disable it if enabled */
4226 rtl_writephy(tp, 0x1f, 0x0a43);
4227 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004228 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004229
4230 rtl_writephy(tp, 0x1f, 0x0000);
4231}
4232
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004233static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4234{
4235 /* Enable PHY auto speed down */
4236 rtl_writephy(tp, 0x1f, 0x0a44);
4237 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4238 rtl_writephy(tp, 0x1f, 0x0000);
4239
4240 /* patch 10M & ALDPS */
4241 rtl_writephy(tp, 0x1f, 0x0bcc);
4242 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4243 rtl_writephy(tp, 0x1f, 0x0a44);
4244 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4245 rtl_writephy(tp, 0x1f, 0x0a43);
4246 rtl_writephy(tp, 0x13, 0x8084);
4247 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4248 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4249 rtl_writephy(tp, 0x1f, 0x0000);
4250
4251 /* Enable EEE auto-fallback function */
4252 rtl_writephy(tp, 0x1f, 0x0a4b);
4253 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4254 rtl_writephy(tp, 0x1f, 0x0000);
4255
4256 /* Enable UC LPF tune function */
4257 rtl_writephy(tp, 0x1f, 0x0a43);
4258 rtl_writephy(tp, 0x13, 0x8012);
4259 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4260 rtl_writephy(tp, 0x1f, 0x0000);
4261
4262 /* set rg_sel_sdm_rate */
4263 rtl_writephy(tp, 0x1f, 0x0c42);
4264 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4265 rtl_writephy(tp, 0x1f, 0x0000);
4266
4267 /* Check ALDPS bit, disable it if enabled */
4268 rtl_writephy(tp, 0x1f, 0x0a43);
4269 if (rtl_readphy(tp, 0x10) & 0x0004)
4270 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4271
4272 rtl_writephy(tp, 0x1f, 0x0000);
4273}
4274
4275static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4276{
4277 /* patch 10M & ALDPS */
4278 rtl_writephy(tp, 0x1f, 0x0bcc);
4279 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4280 rtl_writephy(tp, 0x1f, 0x0a44);
4281 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4282 rtl_writephy(tp, 0x1f, 0x0a43);
4283 rtl_writephy(tp, 0x13, 0x8084);
4284 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4285 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4286 rtl_writephy(tp, 0x1f, 0x0000);
4287
4288 /* Enable UC LPF tune function */
4289 rtl_writephy(tp, 0x1f, 0x0a43);
4290 rtl_writephy(tp, 0x13, 0x8012);
4291 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4292 rtl_writephy(tp, 0x1f, 0x0000);
4293
4294 /* Set rg_sel_sdm_rate */
4295 rtl_writephy(tp, 0x1f, 0x0c42);
4296 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4297 rtl_writephy(tp, 0x1f, 0x0000);
4298
4299 /* Channel estimation parameters */
4300 rtl_writephy(tp, 0x1f, 0x0a43);
4301 rtl_writephy(tp, 0x13, 0x80f3);
4302 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4303 rtl_writephy(tp, 0x13, 0x80f0);
4304 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4305 rtl_writephy(tp, 0x13, 0x80ef);
4306 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4307 rtl_writephy(tp, 0x13, 0x80f6);
4308 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4309 rtl_writephy(tp, 0x13, 0x80ec);
4310 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4311 rtl_writephy(tp, 0x13, 0x80ed);
4312 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4313 rtl_writephy(tp, 0x13, 0x80f2);
4314 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4315 rtl_writephy(tp, 0x13, 0x80f4);
4316 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4317 rtl_writephy(tp, 0x1f, 0x0a43);
4318 rtl_writephy(tp, 0x13, 0x8110);
4319 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4320 rtl_writephy(tp, 0x13, 0x810f);
4321 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4322 rtl_writephy(tp, 0x13, 0x8111);
4323 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4324 rtl_writephy(tp, 0x13, 0x8113);
4325 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4326 rtl_writephy(tp, 0x13, 0x8115);
4327 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4328 rtl_writephy(tp, 0x13, 0x810e);
4329 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4330 rtl_writephy(tp, 0x13, 0x810c);
4331 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4332 rtl_writephy(tp, 0x13, 0x810b);
4333 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4334 rtl_writephy(tp, 0x1f, 0x0a43);
4335 rtl_writephy(tp, 0x13, 0x80d1);
4336 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4337 rtl_writephy(tp, 0x13, 0x80cd);
4338 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4339 rtl_writephy(tp, 0x13, 0x80d3);
4340 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4341 rtl_writephy(tp, 0x13, 0x80d5);
4342 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4343 rtl_writephy(tp, 0x13, 0x80d7);
4344 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4345
4346 /* Force PWM-mode */
4347 rtl_writephy(tp, 0x1f, 0x0bcd);
4348 rtl_writephy(tp, 0x14, 0x5065);
4349 rtl_writephy(tp, 0x14, 0xd065);
4350 rtl_writephy(tp, 0x1f, 0x0bc8);
4351 rtl_writephy(tp, 0x12, 0x00ed);
4352 rtl_writephy(tp, 0x1f, 0x0bcd);
4353 rtl_writephy(tp, 0x14, 0x1065);
4354 rtl_writephy(tp, 0x14, 0x9065);
4355 rtl_writephy(tp, 0x14, 0x1065);
4356 rtl_writephy(tp, 0x1f, 0x0000);
4357
4358 /* Check ALDPS bit, disable it if enabled */
4359 rtl_writephy(tp, 0x1f, 0x0a43);
4360 if (rtl_readphy(tp, 0x10) & 0x0004)
4361 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4362
4363 rtl_writephy(tp, 0x1f, 0x0000);
4364}
4365
françois romieu4da19632011-01-03 15:07:55 +00004366static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004367{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004368 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004369 { 0x1f, 0x0003 },
4370 { 0x08, 0x441d },
4371 { 0x01, 0x9100 },
4372 { 0x1f, 0x0000 }
4373 };
4374
françois romieu4da19632011-01-03 15:07:55 +00004375 rtl_writephy(tp, 0x1f, 0x0000);
4376 rtl_patchphy(tp, 0x11, 1 << 12);
4377 rtl_patchphy(tp, 0x19, 1 << 13);
4378 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004379
françois romieu4da19632011-01-03 15:07:55 +00004380 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004381}
4382
Hayes Wang5a5e4442011-02-22 17:26:21 +08004383static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4384{
4385 static const struct phy_reg phy_reg_init[] = {
4386 { 0x1f, 0x0005 },
4387 { 0x1a, 0x0000 },
4388 { 0x1f, 0x0000 },
4389
4390 { 0x1f, 0x0004 },
4391 { 0x1c, 0x0000 },
4392 { 0x1f, 0x0000 },
4393
4394 { 0x1f, 0x0001 },
4395 { 0x15, 0x7701 },
4396 { 0x1f, 0x0000 }
4397 };
4398
4399 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004400 rtl_writephy(tp, 0x1f, 0x0000);
4401 rtl_writephy(tp, 0x18, 0x0310);
4402 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004403
François Romieu953a12c2011-04-24 17:38:48 +02004404 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004405
4406 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4407}
4408
Hayes Wang7e18dca2012-03-30 14:33:02 +08004409static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4410{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004411 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004412 rtl_writephy(tp, 0x1f, 0x0000);
4413 rtl_writephy(tp, 0x18, 0x0310);
4414 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004415
4416 rtl_apply_firmware(tp);
4417
4418 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004419 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004420 rtl_writephy(tp, 0x1f, 0x0004);
4421 rtl_writephy(tp, 0x10, 0x401f);
4422 rtl_writephy(tp, 0x19, 0x7030);
4423 rtl_writephy(tp, 0x1f, 0x0000);
4424}
4425
Hayes Wang5598bfe2012-07-02 17:23:21 +08004426static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4427{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004428 static const struct phy_reg phy_reg_init[] = {
4429 { 0x1f, 0x0004 },
4430 { 0x10, 0xc07f },
4431 { 0x19, 0x7030 },
4432 { 0x1f, 0x0000 }
4433 };
4434
4435 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004436 rtl_writephy(tp, 0x1f, 0x0000);
4437 rtl_writephy(tp, 0x18, 0x0310);
4438 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004439
4440 rtl_apply_firmware(tp);
4441
Francois Romieufdf6fc02012-07-06 22:40:38 +02004442 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004443 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4444
Francois Romieufdf6fc02012-07-06 22:40:38 +02004445 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004446}
4447
Francois Romieu5615d9f2007-08-17 17:50:46 +02004448static void rtl_hw_phy_config(struct net_device *dev)
4449{
4450 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004451
4452 rtl8169_print_mac_version(tp);
4453
4454 switch (tp->mac_version) {
4455 case RTL_GIGA_MAC_VER_01:
4456 break;
4457 case RTL_GIGA_MAC_VER_02:
4458 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004459 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004460 break;
4461 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004462 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004463 break;
françois romieu2e9558562009-08-10 19:44:19 +00004464 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004465 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004466 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004467 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004468 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004469 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004470 case RTL_GIGA_MAC_VER_07:
4471 case RTL_GIGA_MAC_VER_08:
4472 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004473 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004474 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004475 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004476 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004477 break;
4478 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004479 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004480 break;
4481 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004482 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004483 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004484 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004485 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004486 break;
4487 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004488 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004489 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004490 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004491 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004492 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004493 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004494 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004495 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004496 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004497 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004498 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004499 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004500 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004501 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004502 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004503 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004504 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004505 break;
4506 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004507 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004508 break;
4509 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004510 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004511 break;
françois romieue6de30d2011-01-03 15:08:37 +00004512 case RTL_GIGA_MAC_VER_28:
4513 rtl8168d_4_hw_phy_config(tp);
4514 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004515 case RTL_GIGA_MAC_VER_29:
4516 case RTL_GIGA_MAC_VER_30:
4517 rtl8105e_hw_phy_config(tp);
4518 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004519 case RTL_GIGA_MAC_VER_31:
4520 /* None. */
4521 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004522 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004523 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004524 rtl8168e_1_hw_phy_config(tp);
4525 break;
4526 case RTL_GIGA_MAC_VER_34:
4527 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004528 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004529 case RTL_GIGA_MAC_VER_35:
4530 rtl8168f_1_hw_phy_config(tp);
4531 break;
4532 case RTL_GIGA_MAC_VER_36:
4533 rtl8168f_2_hw_phy_config(tp);
4534 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004535
Hayes Wang7e18dca2012-03-30 14:33:02 +08004536 case RTL_GIGA_MAC_VER_37:
4537 rtl8402_hw_phy_config(tp);
4538 break;
4539
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004540 case RTL_GIGA_MAC_VER_38:
4541 rtl8411_hw_phy_config(tp);
4542 break;
4543
Hayes Wang5598bfe2012-07-02 17:23:21 +08004544 case RTL_GIGA_MAC_VER_39:
4545 rtl8106e_hw_phy_config(tp);
4546 break;
4547
Hayes Wangc5583862012-07-02 17:23:22 +08004548 case RTL_GIGA_MAC_VER_40:
4549 rtl8168g_1_hw_phy_config(tp);
4550 break;
hayeswang57538c42013-04-01 22:23:40 +00004551 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004552 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004553 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004554 rtl8168g_2_hw_phy_config(tp);
4555 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004556 case RTL_GIGA_MAC_VER_45:
4557 case RTL_GIGA_MAC_VER_47:
4558 rtl8168h_1_hw_phy_config(tp);
4559 break;
4560 case RTL_GIGA_MAC_VER_46:
4561 case RTL_GIGA_MAC_VER_48:
4562 rtl8168h_2_hw_phy_config(tp);
4563 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004564
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004565 case RTL_GIGA_MAC_VER_49:
4566 rtl8168ep_1_hw_phy_config(tp);
4567 break;
4568 case RTL_GIGA_MAC_VER_50:
4569 case RTL_GIGA_MAC_VER_51:
4570 rtl8168ep_2_hw_phy_config(tp);
4571 break;
4572
Hayes Wangc5583862012-07-02 17:23:22 +08004573 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004574 default:
4575 break;
4576 }
4577}
4578
Francois Romieuda78dbf2012-01-26 14:18:23 +01004579static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 struct timer_list *timer = &tp->timer;
4582 void __iomem *ioaddr = tp->mmio_addr;
4583 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4584
Francois Romieubcf0bf92006-07-26 23:14:13 +02004585 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
françois romieu4da19632011-01-03 15:07:55 +00004587 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004588 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 * A busy loop could burn quite a few cycles on nowadays CPU.
4590 * Let's delay the execution of the timer for a few ticks.
4591 */
4592 timeout = HZ/10;
4593 goto out_mod_timer;
4594 }
4595
4596 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004597 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004599 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
françois romieu4da19632011-01-03 15:07:55 +00004601 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
4603out_mod_timer:
4604 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004605}
4606
4607static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4608{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004609 if (!test_and_set_bit(flag, tp->wk.flags))
4610 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004611}
4612
Kees Cook9de36cc2017-10-25 03:53:12 -07004613static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004614{
Kees Cook9de36cc2017-10-25 03:53:12 -07004615 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004616
Francois Romieu98ddf982012-01-31 10:47:34 +01004617 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
4619
Francois Romieuffc46952012-07-06 14:19:23 +02004620DECLARE_RTL_COND(rtl_phy_reset_cond)
4621{
4622 return tp->phy_reset_pending(tp);
4623}
4624
Francois Romieubf793292006-11-01 00:53:05 +01004625static void rtl8169_phy_reset(struct net_device *dev,
4626 struct rtl8169_private *tp)
4627{
françois romieu4da19632011-01-03 15:07:55 +00004628 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004629 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004630}
4631
David S. Miller8decf862011-09-22 03:23:13 -04004632static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4633{
4634 void __iomem *ioaddr = tp->mmio_addr;
4635
4636 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4637 (RTL_R8(PHYstatus) & TBI_Enable);
4638}
4639
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004640static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004642 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004643
Francois Romieu5615d9f2007-08-17 17:50:46 +02004644 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004645
Marcus Sundberg773328942008-07-10 21:28:08 +02004646 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4647 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4648 RTL_W8(0x82, 0x01);
4649 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004650
Francois Romieu6dccd162007-02-13 23:38:05 +01004651 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4652
4653 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4654 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004655
Francois Romieubcf0bf92006-07-26 23:14:13 +02004656 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004657 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4658 RTL_W8(0x82, 0x01);
4659 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004660 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004661 }
4662
Francois Romieubf793292006-11-01 00:53:05 +01004663 rtl8169_phy_reset(dev, tp);
4664
Oliver Neukum54405cd2011-01-06 21:55:13 +01004665 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004666 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4667 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4668 (tp->mii.supports_gmii ?
4669 ADVERTISED_1000baseT_Half |
4670 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004671
David S. Miller8decf862011-09-22 03:23:13 -04004672 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004673 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004674}
4675
Francois Romieu773d2022007-01-31 23:47:43 +01004676static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4677{
4678 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004679
Francois Romieuda78dbf2012-01-26 14:18:23 +01004680 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004681
4682 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004683
françois romieu9ecb9aa2012-12-07 11:20:21 +00004684 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004685 RTL_R32(MAC4);
4686
françois romieu9ecb9aa2012-12-07 11:20:21 +00004687 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004688 RTL_R32(MAC0);
4689
françois romieu9ecb9aa2012-12-07 11:20:21 +00004690 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4691 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004692
Francois Romieu773d2022007-01-31 23:47:43 +01004693 RTL_W8(Cfg9346, Cfg9346_Lock);
4694
Francois Romieuda78dbf2012-01-26 14:18:23 +01004695 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004696}
4697
4698static int rtl_set_mac_address(struct net_device *dev, void *p)
4699{
4700 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004701 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004702 struct sockaddr *addr = p;
4703
4704 if (!is_valid_ether_addr(addr->sa_data))
4705 return -EADDRNOTAVAIL;
4706
4707 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4708
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004709 pm_runtime_get_noresume(d);
4710
4711 if (pm_runtime_active(d))
4712 rtl_rar_set(tp, dev->dev_addr);
4713
4714 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004715
4716 return 0;
4717}
4718
Francois Romieu5f787a12006-08-17 13:02:36 +02004719static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4720{
4721 struct rtl8169_private *tp = netdev_priv(dev);
4722 struct mii_ioctl_data *data = if_mii(ifr);
4723
Francois Romieu8b4ab282008-11-19 22:05:25 -08004724 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4725}
Francois Romieu5f787a12006-08-17 13:02:36 +02004726
Francois Romieucecb5fd2011-04-01 10:21:07 +02004727static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4728 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004729{
Francois Romieu5f787a12006-08-17 13:02:36 +02004730 switch (cmd) {
4731 case SIOCGMIIPHY:
4732 data->phy_id = 32; /* Internal PHY */
4733 return 0;
4734
4735 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004736 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004737 return 0;
4738
4739 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004740 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004741 return 0;
4742 }
4743 return -EOPNOTSUPP;
4744}
4745
Francois Romieu8b4ab282008-11-19 22:05:25 -08004746static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4747{
4748 return -EOPNOTSUPP;
4749}
4750
Bill Pembertonbaf63292012-12-03 09:23:28 -05004751static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004752{
4753 struct mdio_ops *ops = &tp->mdio_ops;
4754
4755 switch (tp->mac_version) {
4756 case RTL_GIGA_MAC_VER_27:
4757 ops->write = r8168dp_1_mdio_write;
4758 ops->read = r8168dp_1_mdio_read;
4759 break;
françois romieue6de30d2011-01-03 15:08:37 +00004760 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004761 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004762 ops->write = r8168dp_2_mdio_write;
4763 ops->read = r8168dp_2_mdio_read;
4764 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004765 case RTL_GIGA_MAC_VER_40:
4766 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004767 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004768 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004769 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004770 case RTL_GIGA_MAC_VER_45:
4771 case RTL_GIGA_MAC_VER_46:
4772 case RTL_GIGA_MAC_VER_47:
4773 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004774 case RTL_GIGA_MAC_VER_49:
4775 case RTL_GIGA_MAC_VER_50:
4776 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004777 ops->write = r8168g_mdio_write;
4778 ops->read = r8168g_mdio_read;
4779 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004780 default:
4781 ops->write = r8169_mdio_write;
4782 ops->read = r8169_mdio_read;
4783 break;
4784 }
4785}
4786
hayeswange2409d82013-03-31 17:02:04 +00004787static void rtl_speed_down(struct rtl8169_private *tp)
4788{
4789 u32 adv;
4790 int lpa;
4791
4792 rtl_writephy(tp, 0x1f, 0x0000);
4793 lpa = rtl_readphy(tp, MII_LPA);
4794
4795 if (lpa & (LPA_10HALF | LPA_10FULL))
4796 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4797 else if (lpa & (LPA_100HALF | LPA_100FULL))
4798 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4799 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4800 else
4801 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4802 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4803 (tp->mii.supports_gmii ?
4804 ADVERTISED_1000baseT_Half |
4805 ADVERTISED_1000baseT_Full : 0);
4806
4807 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4808 adv);
4809}
4810
David S. Miller1805b2f2011-10-24 18:18:09 -04004811static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4812{
4813 void __iomem *ioaddr = tp->mmio_addr;
4814
4815 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004816 case RTL_GIGA_MAC_VER_25:
4817 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004818 case RTL_GIGA_MAC_VER_29:
4819 case RTL_GIGA_MAC_VER_30:
4820 case RTL_GIGA_MAC_VER_32:
4821 case RTL_GIGA_MAC_VER_33:
4822 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004823 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004824 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004825 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004826 case RTL_GIGA_MAC_VER_40:
4827 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004828 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004829 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004830 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004831 case RTL_GIGA_MAC_VER_45:
4832 case RTL_GIGA_MAC_VER_46:
4833 case RTL_GIGA_MAC_VER_47:
4834 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004835 case RTL_GIGA_MAC_VER_49:
4836 case RTL_GIGA_MAC_VER_50:
4837 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004838 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4839 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4840 break;
4841 default:
4842 break;
4843 }
4844}
4845
4846static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4847{
4848 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4849 return false;
4850
hayeswange2409d82013-03-31 17:02:04 +00004851 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004852 rtl_wol_suspend_quirk(tp);
4853
4854 return true;
4855}
4856
françois romieu065c27c2011-01-03 15:08:12 +00004857static void r810x_phy_power_down(struct rtl8169_private *tp)
4858{
4859 rtl_writephy(tp, 0x1f, 0x0000);
4860 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4861}
4862
4863static void r810x_phy_power_up(struct rtl8169_private *tp)
4864{
4865 rtl_writephy(tp, 0x1f, 0x0000);
4866 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4867}
4868
4869static void r810x_pll_power_down(struct rtl8169_private *tp)
4870{
Hayes Wang00042992012-03-30 14:33:00 +08004871 void __iomem *ioaddr = tp->mmio_addr;
4872
David S. Miller1805b2f2011-10-24 18:18:09 -04004873 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004874 return;
françois romieu065c27c2011-01-03 15:08:12 +00004875
4876 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004877
4878 switch (tp->mac_version) {
4879 case RTL_GIGA_MAC_VER_07:
4880 case RTL_GIGA_MAC_VER_08:
4881 case RTL_GIGA_MAC_VER_09:
4882 case RTL_GIGA_MAC_VER_10:
4883 case RTL_GIGA_MAC_VER_13:
4884 case RTL_GIGA_MAC_VER_16:
4885 break;
4886 default:
4887 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4888 break;
4889 }
françois romieu065c27c2011-01-03 15:08:12 +00004890}
4891
4892static void r810x_pll_power_up(struct rtl8169_private *tp)
4893{
Hayes Wang00042992012-03-30 14:33:00 +08004894 void __iomem *ioaddr = tp->mmio_addr;
4895
françois romieu065c27c2011-01-03 15:08:12 +00004896 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004897
4898 switch (tp->mac_version) {
4899 case RTL_GIGA_MAC_VER_07:
4900 case RTL_GIGA_MAC_VER_08:
4901 case RTL_GIGA_MAC_VER_09:
4902 case RTL_GIGA_MAC_VER_10:
4903 case RTL_GIGA_MAC_VER_13:
4904 case RTL_GIGA_MAC_VER_16:
4905 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004906 case RTL_GIGA_MAC_VER_47:
4907 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004908 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004909 break;
Hayes Wang00042992012-03-30 14:33:00 +08004910 default:
4911 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4912 break;
4913 }
françois romieu065c27c2011-01-03 15:08:12 +00004914}
4915
4916static void r8168_phy_power_up(struct rtl8169_private *tp)
4917{
4918 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004919 switch (tp->mac_version) {
4920 case RTL_GIGA_MAC_VER_11:
4921 case RTL_GIGA_MAC_VER_12:
4922 case RTL_GIGA_MAC_VER_17:
4923 case RTL_GIGA_MAC_VER_18:
4924 case RTL_GIGA_MAC_VER_19:
4925 case RTL_GIGA_MAC_VER_20:
4926 case RTL_GIGA_MAC_VER_21:
4927 case RTL_GIGA_MAC_VER_22:
4928 case RTL_GIGA_MAC_VER_23:
4929 case RTL_GIGA_MAC_VER_24:
4930 case RTL_GIGA_MAC_VER_25:
4931 case RTL_GIGA_MAC_VER_26:
4932 case RTL_GIGA_MAC_VER_27:
4933 case RTL_GIGA_MAC_VER_28:
4934 case RTL_GIGA_MAC_VER_31:
4935 rtl_writephy(tp, 0x0e, 0x0000);
4936 break;
4937 default:
4938 break;
4939 }
françois romieu065c27c2011-01-03 15:08:12 +00004940 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4941}
4942
4943static void r8168_phy_power_down(struct rtl8169_private *tp)
4944{
4945 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004946 switch (tp->mac_version) {
4947 case RTL_GIGA_MAC_VER_32:
4948 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004949 case RTL_GIGA_MAC_VER_40:
4950 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004951 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4952 break;
4953
4954 case RTL_GIGA_MAC_VER_11:
4955 case RTL_GIGA_MAC_VER_12:
4956 case RTL_GIGA_MAC_VER_17:
4957 case RTL_GIGA_MAC_VER_18:
4958 case RTL_GIGA_MAC_VER_19:
4959 case RTL_GIGA_MAC_VER_20:
4960 case RTL_GIGA_MAC_VER_21:
4961 case RTL_GIGA_MAC_VER_22:
4962 case RTL_GIGA_MAC_VER_23:
4963 case RTL_GIGA_MAC_VER_24:
4964 case RTL_GIGA_MAC_VER_25:
4965 case RTL_GIGA_MAC_VER_26:
4966 case RTL_GIGA_MAC_VER_27:
4967 case RTL_GIGA_MAC_VER_28:
4968 case RTL_GIGA_MAC_VER_31:
4969 rtl_writephy(tp, 0x0e, 0x0200);
4970 default:
4971 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4972 break;
4973 }
françois romieu065c27c2011-01-03 15:08:12 +00004974}
4975
4976static void r8168_pll_power_down(struct rtl8169_private *tp)
4977{
4978 void __iomem *ioaddr = tp->mmio_addr;
4979
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004980 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004981 return;
4982
Francois Romieucecb5fd2011-04-01 10:21:07 +02004983 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4984 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004985 (RTL_R16(CPlusCmd) & ASF)) {
4986 return;
4987 }
4988
hayeswang01dc7fe2011-03-21 01:50:28 +00004989 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4990 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004991 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004992
David S. Miller1805b2f2011-10-24 18:18:09 -04004993 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004994 return;
françois romieu065c27c2011-01-03 15:08:12 +00004995
4996 r8168_phy_power_down(tp);
4997
4998 switch (tp->mac_version) {
4999 case RTL_GIGA_MAC_VER_25:
5000 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005001 case RTL_GIGA_MAC_VER_27:
5002 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005003 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005004 case RTL_GIGA_MAC_VER_32:
5005 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005006 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005007 case RTL_GIGA_MAC_VER_45:
5008 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005009 case RTL_GIGA_MAC_VER_50:
5010 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005011 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5012 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005013 case RTL_GIGA_MAC_VER_40:
5014 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005015 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005016 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005017 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005018 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00005019 break;
françois romieu065c27c2011-01-03 15:08:12 +00005020 }
5021}
5022
5023static void r8168_pll_power_up(struct rtl8169_private *tp)
5024{
5025 void __iomem *ioaddr = tp->mmio_addr;
5026
françois romieu065c27c2011-01-03 15:08:12 +00005027 switch (tp->mac_version) {
5028 case RTL_GIGA_MAC_VER_25:
5029 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005030 case RTL_GIGA_MAC_VER_27:
5031 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005032 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005033 case RTL_GIGA_MAC_VER_32:
5034 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00005035 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5036 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005037 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005038 case RTL_GIGA_MAC_VER_45:
5039 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005040 case RTL_GIGA_MAC_VER_50:
5041 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005042 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005043 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005044 case RTL_GIGA_MAC_VER_40:
5045 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005046 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005047 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005048 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005049 0x00000000, ERIAR_EXGMAC);
5050 break;
françois romieu065c27c2011-01-03 15:08:12 +00005051 }
5052
5053 r8168_phy_power_up(tp);
5054}
5055
Francois Romieud58d46b2011-05-03 16:38:29 +02005056static void rtl_generic_op(struct rtl8169_private *tp,
5057 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00005058{
5059 if (op)
5060 op(tp);
5061}
5062
5063static void rtl_pll_power_down(struct rtl8169_private *tp)
5064{
Francois Romieud58d46b2011-05-03 16:38:29 +02005065 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00005066}
5067
5068static void rtl_pll_power_up(struct rtl8169_private *tp)
5069{
Francois Romieud58d46b2011-05-03 16:38:29 +02005070 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00005071}
5072
Bill Pembertonbaf63292012-12-03 09:23:28 -05005073static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00005074{
5075 struct pll_power_ops *ops = &tp->pll_power_ops;
5076
5077 switch (tp->mac_version) {
5078 case RTL_GIGA_MAC_VER_07:
5079 case RTL_GIGA_MAC_VER_08:
5080 case RTL_GIGA_MAC_VER_09:
5081 case RTL_GIGA_MAC_VER_10:
5082 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08005083 case RTL_GIGA_MAC_VER_29:
5084 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005085 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08005086 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00005087 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005088 case RTL_GIGA_MAC_VER_47:
5089 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005090 ops->down = r810x_pll_power_down;
5091 ops->up = r810x_pll_power_up;
5092 break;
5093
5094 case RTL_GIGA_MAC_VER_11:
5095 case RTL_GIGA_MAC_VER_12:
5096 case RTL_GIGA_MAC_VER_17:
5097 case RTL_GIGA_MAC_VER_18:
5098 case RTL_GIGA_MAC_VER_19:
5099 case RTL_GIGA_MAC_VER_20:
5100 case RTL_GIGA_MAC_VER_21:
5101 case RTL_GIGA_MAC_VER_22:
5102 case RTL_GIGA_MAC_VER_23:
5103 case RTL_GIGA_MAC_VER_24:
5104 case RTL_GIGA_MAC_VER_25:
5105 case RTL_GIGA_MAC_VER_26:
5106 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005107 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005108 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005109 case RTL_GIGA_MAC_VER_32:
5110 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005111 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005112 case RTL_GIGA_MAC_VER_35:
5113 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005114 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005115 case RTL_GIGA_MAC_VER_40:
5116 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005117 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005118 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005119 case RTL_GIGA_MAC_VER_45:
5120 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005121 case RTL_GIGA_MAC_VER_49:
5122 case RTL_GIGA_MAC_VER_50:
5123 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005124 ops->down = r8168_pll_power_down;
5125 ops->up = r8168_pll_power_up;
5126 break;
5127
5128 default:
5129 ops->down = NULL;
5130 ops->up = NULL;
5131 break;
5132 }
5133}
5134
Hayes Wange542a222011-07-06 15:58:04 +08005135static void rtl_init_rxcfg(struct rtl8169_private *tp)
5136{
5137 void __iomem *ioaddr = tp->mmio_addr;
5138
5139 switch (tp->mac_version) {
5140 case RTL_GIGA_MAC_VER_01:
5141 case RTL_GIGA_MAC_VER_02:
5142 case RTL_GIGA_MAC_VER_03:
5143 case RTL_GIGA_MAC_VER_04:
5144 case RTL_GIGA_MAC_VER_05:
5145 case RTL_GIGA_MAC_VER_06:
5146 case RTL_GIGA_MAC_VER_10:
5147 case RTL_GIGA_MAC_VER_11:
5148 case RTL_GIGA_MAC_VER_12:
5149 case RTL_GIGA_MAC_VER_13:
5150 case RTL_GIGA_MAC_VER_14:
5151 case RTL_GIGA_MAC_VER_15:
5152 case RTL_GIGA_MAC_VER_16:
5153 case RTL_GIGA_MAC_VER_17:
5154 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5155 break;
5156 case RTL_GIGA_MAC_VER_18:
5157 case RTL_GIGA_MAC_VER_19:
5158 case RTL_GIGA_MAC_VER_20:
5159 case RTL_GIGA_MAC_VER_21:
5160 case RTL_GIGA_MAC_VER_22:
5161 case RTL_GIGA_MAC_VER_23:
5162 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005163 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005164 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08005165 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5166 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005167 case RTL_GIGA_MAC_VER_40:
5168 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005169 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005170 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005171 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005172 case RTL_GIGA_MAC_VER_45:
5173 case RTL_GIGA_MAC_VER_46:
5174 case RTL_GIGA_MAC_VER_47:
5175 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005176 case RTL_GIGA_MAC_VER_49:
5177 case RTL_GIGA_MAC_VER_50:
5178 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02005179 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005180 break;
Hayes Wange542a222011-07-06 15:58:04 +08005181 default:
5182 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5183 break;
5184 }
5185}
5186
Hayes Wang92fc43b2011-07-06 15:58:03 +08005187static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5188{
Timo Teräs9fba0812013-01-15 21:01:24 +00005189 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005190}
5191
Francois Romieud58d46b2011-05-03 16:38:29 +02005192static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5193{
françois romieu9c5028e2012-03-02 04:43:14 +00005194 void __iomem *ioaddr = tp->mmio_addr;
5195
5196 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005197 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005198 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005199}
5200
5201static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5202{
françois romieu9c5028e2012-03-02 04:43:14 +00005203 void __iomem *ioaddr = tp->mmio_addr;
5204
5205 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005206 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005207 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005208}
5209
5210static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5211{
5212 void __iomem *ioaddr = tp->mmio_addr;
5213
5214 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5215 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005216 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005217}
5218
5219static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5220{
5221 void __iomem *ioaddr = tp->mmio_addr;
5222
5223 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5224 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5225 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5226}
5227
5228static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5229{
5230 void __iomem *ioaddr = tp->mmio_addr;
5231
5232 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5233}
5234
5235static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5236{
5237 void __iomem *ioaddr = tp->mmio_addr;
5238
5239 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5240}
5241
5242static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5243{
5244 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005245
5246 RTL_W8(MaxTxPacketSize, 0x3f);
5247 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5248 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005249 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005250}
5251
5252static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5253{
5254 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005255
5256 RTL_W8(MaxTxPacketSize, 0x0c);
5257 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5258 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005259 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005260}
5261
5262static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5263{
5264 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005265 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005266}
5267
5268static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5269{
5270 rtl_tx_performance_tweak(tp->pci_dev,
5271 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5272}
5273
5274static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5275{
5276 void __iomem *ioaddr = tp->mmio_addr;
5277
5278 r8168b_0_hw_jumbo_enable(tp);
5279
5280 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5281}
5282
5283static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5284{
5285 void __iomem *ioaddr = tp->mmio_addr;
5286
5287 r8168b_0_hw_jumbo_disable(tp);
5288
5289 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5290}
5291
Bill Pembertonbaf63292012-12-03 09:23:28 -05005292static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005293{
5294 struct jumbo_ops *ops = &tp->jumbo_ops;
5295
5296 switch (tp->mac_version) {
5297 case RTL_GIGA_MAC_VER_11:
5298 ops->disable = r8168b_0_hw_jumbo_disable;
5299 ops->enable = r8168b_0_hw_jumbo_enable;
5300 break;
5301 case RTL_GIGA_MAC_VER_12:
5302 case RTL_GIGA_MAC_VER_17:
5303 ops->disable = r8168b_1_hw_jumbo_disable;
5304 ops->enable = r8168b_1_hw_jumbo_enable;
5305 break;
5306 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5307 case RTL_GIGA_MAC_VER_19:
5308 case RTL_GIGA_MAC_VER_20:
5309 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5310 case RTL_GIGA_MAC_VER_22:
5311 case RTL_GIGA_MAC_VER_23:
5312 case RTL_GIGA_MAC_VER_24:
5313 case RTL_GIGA_MAC_VER_25:
5314 case RTL_GIGA_MAC_VER_26:
5315 ops->disable = r8168c_hw_jumbo_disable;
5316 ops->enable = r8168c_hw_jumbo_enable;
5317 break;
5318 case RTL_GIGA_MAC_VER_27:
5319 case RTL_GIGA_MAC_VER_28:
5320 ops->disable = r8168dp_hw_jumbo_disable;
5321 ops->enable = r8168dp_hw_jumbo_enable;
5322 break;
5323 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5324 case RTL_GIGA_MAC_VER_32:
5325 case RTL_GIGA_MAC_VER_33:
5326 case RTL_GIGA_MAC_VER_34:
5327 ops->disable = r8168e_hw_jumbo_disable;
5328 ops->enable = r8168e_hw_jumbo_enable;
5329 break;
5330
5331 /*
5332 * No action needed for jumbo frames with 8169.
5333 * No jumbo for 810x at all.
5334 */
Hayes Wangc5583862012-07-02 17:23:22 +08005335 case RTL_GIGA_MAC_VER_40:
5336 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005337 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005338 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005339 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005340 case RTL_GIGA_MAC_VER_45:
5341 case RTL_GIGA_MAC_VER_46:
5342 case RTL_GIGA_MAC_VER_47:
5343 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005344 case RTL_GIGA_MAC_VER_49:
5345 case RTL_GIGA_MAC_VER_50:
5346 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005347 default:
5348 ops->disable = NULL;
5349 ops->enable = NULL;
5350 break;
5351 }
5352}
5353
Francois Romieuffc46952012-07-06 14:19:23 +02005354DECLARE_RTL_COND(rtl_chipcmd_cond)
5355{
5356 void __iomem *ioaddr = tp->mmio_addr;
5357
5358 return RTL_R8(ChipCmd) & CmdReset;
5359}
5360
Francois Romieu6f43adc2011-04-29 15:05:51 +02005361static void rtl_hw_reset(struct rtl8169_private *tp)
5362{
5363 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005364
Francois Romieu6f43adc2011-04-29 15:05:51 +02005365 RTL_W8(ChipCmd, CmdReset);
5366
Francois Romieuffc46952012-07-06 14:19:23 +02005367 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005368}
5369
Francois Romieub6ffd972011-06-17 17:00:05 +02005370static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5371{
5372 struct rtl_fw *rtl_fw;
5373 const char *name;
5374 int rc = -ENOMEM;
5375
5376 name = rtl_lookup_firmware_name(tp);
5377 if (!name)
5378 goto out_no_firmware;
5379
5380 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5381 if (!rtl_fw)
5382 goto err_warn;
5383
5384 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5385 if (rc < 0)
5386 goto err_free;
5387
Francois Romieufd112f22011-06-18 00:10:29 +02005388 rc = rtl_check_firmware(tp, rtl_fw);
5389 if (rc < 0)
5390 goto err_release_firmware;
5391
Francois Romieub6ffd972011-06-17 17:00:05 +02005392 tp->rtl_fw = rtl_fw;
5393out:
5394 return;
5395
Francois Romieufd112f22011-06-18 00:10:29 +02005396err_release_firmware:
5397 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005398err_free:
5399 kfree(rtl_fw);
5400err_warn:
5401 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5402 name, rc);
5403out_no_firmware:
5404 tp->rtl_fw = NULL;
5405 goto out;
5406}
5407
François Romieu953a12c2011-04-24 17:38:48 +02005408static void rtl_request_firmware(struct rtl8169_private *tp)
5409{
Francois Romieub6ffd972011-06-17 17:00:05 +02005410 if (IS_ERR(tp->rtl_fw))
5411 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005412}
5413
Hayes Wang92fc43b2011-07-06 15:58:03 +08005414static void rtl_rx_close(struct rtl8169_private *tp)
5415{
5416 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005417
Francois Romieu1687b562011-07-19 17:21:29 +02005418 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005419}
5420
Francois Romieuffc46952012-07-06 14:19:23 +02005421DECLARE_RTL_COND(rtl_npq_cond)
5422{
5423 void __iomem *ioaddr = tp->mmio_addr;
5424
5425 return RTL_R8(TxPoll) & NPQ;
5426}
5427
5428DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5429{
5430 void __iomem *ioaddr = tp->mmio_addr;
5431
5432 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5433}
5434
françois romieue6de30d2011-01-03 15:08:37 +00005435static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
françois romieue6de30d2011-01-03 15:08:37 +00005437 void __iomem *ioaddr = tp->mmio_addr;
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005440 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Hayes Wang92fc43b2011-07-06 15:58:03 +08005442 rtl_rx_close(tp);
5443
Hayes Wang5d2e1952011-02-22 17:26:22 +08005444 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005445 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5446 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005447 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005448 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005449 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5450 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5451 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5452 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5453 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5454 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5455 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5456 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5457 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5458 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5459 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5460 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005461 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5462 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5463 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5464 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005465 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005466 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005467 } else {
5468 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5469 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005470 }
5471
Hayes Wang92fc43b2011-07-06 15:58:03 +08005472 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473}
5474
Francois Romieu7f796d832007-06-11 23:04:41 +02005475static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005476{
5477 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005478
5479 /* Set DMA burst size and Interframe Gap Time */
5480 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5481 (InterFrameGap << TxInterFrameGapShift));
5482}
5483
Francois Romieu07ce4062007-02-23 23:36:39 +01005484static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485{
5486 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Francois Romieu07ce4062007-02-23 23:36:39 +01005488 tp->hw_start(dev);
5489
Francois Romieuda78dbf2012-01-26 14:18:23 +01005490 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005491}
5492
Francois Romieu7f796d832007-06-11 23:04:41 +02005493static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5494 void __iomem *ioaddr)
5495{
5496 /*
5497 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5498 * register to be written before TxDescAddrLow to work.
5499 * Switching from MMIO to I/O access fixes the issue as well.
5500 */
5501 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005502 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005503 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005504 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005505}
5506
5507static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5508{
5509 u16 cmd;
5510
5511 cmd = RTL_R16(CPlusCmd);
5512 RTL_W16(CPlusCmd, cmd);
5513 return cmd;
5514}
5515
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005516static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005517{
5518 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005519 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005520}
5521
Francois Romieu6dccd162007-02-13 23:38:05 +01005522static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5523{
Francois Romieu37441002011-06-17 22:58:54 +02005524 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005525 u32 mac_version;
5526 u32 clk;
5527 u32 val;
5528 } cfg2_info [] = {
5529 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5530 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5531 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5532 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005533 };
5534 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005535 unsigned int i;
5536 u32 clk;
5537
5538 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005539 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005540 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5541 RTL_W32(0x7c, p->val);
5542 break;
5543 }
5544 }
5545}
5546
Francois Romieue6b763e2012-03-08 09:35:39 +01005547static void rtl_set_rx_mode(struct net_device *dev)
5548{
5549 struct rtl8169_private *tp = netdev_priv(dev);
5550 void __iomem *ioaddr = tp->mmio_addr;
5551 u32 mc_filter[2]; /* Multicast hash filter */
5552 int rx_mode;
5553 u32 tmp = 0;
5554
5555 if (dev->flags & IFF_PROMISC) {
5556 /* Unconditionally log net taps. */
5557 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5558 rx_mode =
5559 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5560 AcceptAllPhys;
5561 mc_filter[1] = mc_filter[0] = 0xffffffff;
5562 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5563 (dev->flags & IFF_ALLMULTI)) {
5564 /* Too many to filter perfectly -- accept all multicasts. */
5565 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5566 mc_filter[1] = mc_filter[0] = 0xffffffff;
5567 } else {
5568 struct netdev_hw_addr *ha;
5569
5570 rx_mode = AcceptBroadcast | AcceptMyPhys;
5571 mc_filter[1] = mc_filter[0] = 0;
5572 netdev_for_each_mc_addr(ha, dev) {
5573 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5574 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5575 rx_mode |= AcceptMulticast;
5576 }
5577 }
5578
5579 if (dev->features & NETIF_F_RXALL)
5580 rx_mode |= (AcceptErr | AcceptRunt);
5581
5582 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5583
5584 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5585 u32 data = mc_filter[0];
5586
5587 mc_filter[0] = swab32(mc_filter[1]);
5588 mc_filter[1] = swab32(data);
5589 }
5590
Nathan Walp04817762012-11-01 12:08:47 +00005591 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5592 mc_filter[1] = mc_filter[0] = 0xffffffff;
5593
Francois Romieue6b763e2012-03-08 09:35:39 +01005594 RTL_W32(MAR0 + 4, mc_filter[1]);
5595 RTL_W32(MAR0 + 0, mc_filter[0]);
5596
5597 RTL_W32(RxConfig, tmp);
5598}
5599
Francois Romieu07ce4062007-02-23 23:36:39 +01005600static void rtl_hw_start_8169(struct net_device *dev)
5601{
5602 struct rtl8169_private *tp = netdev_priv(dev);
5603 void __iomem *ioaddr = tp->mmio_addr;
5604 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005605
Francois Romieu9cb427b2006-11-02 00:10:16 +01005606 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5607 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5608 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5609 }
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005612 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5613 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5614 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5615 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005616 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5617
Hayes Wange542a222011-07-06 15:58:04 +08005618 rtl_init_rxcfg(tp);
5619
françois romieuf0298f82011-01-03 15:07:42 +00005620 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005622 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Francois Romieucecb5fd2011-04-01 10:21:07 +02005624 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5625 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5626 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5627 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005628 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Francois Romieu7f796d832007-06-11 23:04:41 +02005630 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005631
Francois Romieucecb5fd2011-04-01 10:21:07 +02005632 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5633 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005634 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005636 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638
Francois Romieubcf0bf92006-07-26 23:14:13 +02005639 RTL_W16(CPlusCmd, tp->cp_cmd);
5640
Francois Romieu6dccd162007-02-13 23:38:05 +01005641 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 /*
5644 * Undocumented corner. Supposedly:
5645 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5646 */
5647 RTL_W16(IntrMitigate, 0x0000);
5648
Francois Romieu7f796d832007-06-11 23:04:41 +02005649 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005650
Francois Romieucecb5fd2011-04-01 10:21:07 +02005651 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5652 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5653 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5654 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005655 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5656 rtl_set_rx_tx_config_registers(tp);
5657 }
5658
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005660
5661 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5662 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
5664 RTL_W32(RxMissed, 0);
5665
Francois Romieu07ce4062007-02-23 23:36:39 +01005666 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005669 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005672static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5673{
5674 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005675 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005676}
5677
5678static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5679{
Francois Romieu52989f02012-07-06 13:37:00 +02005680 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005681}
5682
5683static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005684{
5685 u32 csi;
5686
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005687 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5688 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005689}
5690
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005691static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005692{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005693 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005694}
5695
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005696static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005697{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005698 rtl_csi_access_enable(tp, 0x27000000);
5699}
5700
Francois Romieuffc46952012-07-06 14:19:23 +02005701DECLARE_RTL_COND(rtl_csiar_cond)
5702{
5703 void __iomem *ioaddr = tp->mmio_addr;
5704
5705 return RTL_R32(CSIAR) & CSIAR_FLAG;
5706}
5707
Francois Romieu52989f02012-07-06 13:37:00 +02005708static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005709{
Francois Romieu52989f02012-07-06 13:37:00 +02005710 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005711
5712 RTL_W32(CSIDR, value);
5713 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5714 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5715
Francois Romieuffc46952012-07-06 14:19:23 +02005716 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005717}
5718
Francois Romieu52989f02012-07-06 13:37:00 +02005719static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005720{
Francois Romieu52989f02012-07-06 13:37:00 +02005721 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005722
5723 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5724 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5725
Francois Romieuffc46952012-07-06 14:19:23 +02005726 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5727 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005728}
5729
Francois Romieu52989f02012-07-06 13:37:00 +02005730static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005731{
Francois Romieu52989f02012-07-06 13:37:00 +02005732 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005733
5734 RTL_W32(CSIDR, value);
5735 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5736 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5737 CSIAR_FUNC_NIC);
5738
Francois Romieuffc46952012-07-06 14:19:23 +02005739 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005740}
5741
Francois Romieu52989f02012-07-06 13:37:00 +02005742static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005743{
Francois Romieu52989f02012-07-06 13:37:00 +02005744 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005745
5746 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5747 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5748
Francois Romieuffc46952012-07-06 14:19:23 +02005749 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5750 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005751}
5752
hayeswang45dd95c2013-07-08 17:09:01 +08005753static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5754{
5755 void __iomem *ioaddr = tp->mmio_addr;
5756
5757 RTL_W32(CSIDR, value);
5758 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5759 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5760 CSIAR_FUNC_NIC2);
5761
5762 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5763}
5764
5765static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5766{
5767 void __iomem *ioaddr = tp->mmio_addr;
5768
5769 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5770 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5771
5772 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5773 RTL_R32(CSIDR) : ~0;
5774}
5775
Bill Pembertonbaf63292012-12-03 09:23:28 -05005776static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005777{
5778 struct csi_ops *ops = &tp->csi_ops;
5779
5780 switch (tp->mac_version) {
5781 case RTL_GIGA_MAC_VER_01:
5782 case RTL_GIGA_MAC_VER_02:
5783 case RTL_GIGA_MAC_VER_03:
5784 case RTL_GIGA_MAC_VER_04:
5785 case RTL_GIGA_MAC_VER_05:
5786 case RTL_GIGA_MAC_VER_06:
5787 case RTL_GIGA_MAC_VER_10:
5788 case RTL_GIGA_MAC_VER_11:
5789 case RTL_GIGA_MAC_VER_12:
5790 case RTL_GIGA_MAC_VER_13:
5791 case RTL_GIGA_MAC_VER_14:
5792 case RTL_GIGA_MAC_VER_15:
5793 case RTL_GIGA_MAC_VER_16:
5794 case RTL_GIGA_MAC_VER_17:
5795 ops->write = NULL;
5796 ops->read = NULL;
5797 break;
5798
Hayes Wang7e18dca2012-03-30 14:33:02 +08005799 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005800 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005801 ops->write = r8402_csi_write;
5802 ops->read = r8402_csi_read;
5803 break;
5804
hayeswang45dd95c2013-07-08 17:09:01 +08005805 case RTL_GIGA_MAC_VER_44:
5806 ops->write = r8411_csi_write;
5807 ops->read = r8411_csi_read;
5808 break;
5809
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005810 default:
5811 ops->write = r8169_csi_write;
5812 ops->read = r8169_csi_read;
5813 break;
5814 }
Francois Romieudacf8152008-08-02 20:44:13 +02005815}
5816
5817struct ephy_info {
5818 unsigned int offset;
5819 u16 mask;
5820 u16 bits;
5821};
5822
Francois Romieufdf6fc02012-07-06 22:40:38 +02005823static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5824 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005825{
5826 u16 w;
5827
5828 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005829 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5830 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005831 e++;
5832 }
5833}
5834
Francois Romieub726e492008-06-28 12:22:59 +02005835static void rtl_disable_clock_request(struct pci_dev *pdev)
5836{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005837 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5838 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005839}
5840
françois romieue6de30d2011-01-03 15:08:37 +00005841static void rtl_enable_clock_request(struct pci_dev *pdev)
5842{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005843 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5844 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005845}
5846
hayeswangb51ecea2014-07-09 14:52:51 +08005847static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5848{
5849 void __iomem *ioaddr = tp->mmio_addr;
5850 u8 data;
5851
5852 data = RTL_R8(Config3);
5853
5854 if (enable)
5855 data |= Rdy_to_L23;
5856 else
5857 data &= ~Rdy_to_L23;
5858
5859 RTL_W8(Config3, data);
5860}
5861
Francois Romieub726e492008-06-28 12:22:59 +02005862#define R8168_CPCMD_QUIRK_MASK (\
5863 EnableBist | \
5864 Mac_dbgo_oe | \
5865 Force_half_dup | \
5866 Force_rxflow_en | \
5867 Force_txflow_en | \
5868 Cxpl_dbg_sel | \
5869 ASF | \
5870 PktCntrDisable | \
5871 Mac_dbgo_sel)
5872
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005873static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005874{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005875 void __iomem *ioaddr = tp->mmio_addr;
5876 struct pci_dev *pdev = tp->pci_dev;
5877
Francois Romieub726e492008-06-28 12:22:59 +02005878 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5879
5880 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5881
françois romieufaf1e782013-02-27 13:01:57 +00005882 if (tp->dev->mtu <= ETH_DATA_LEN) {
5883 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5884 PCI_EXP_DEVCTL_NOSNOOP_EN);
5885 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005886}
5887
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005888static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005889{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005890 void __iomem *ioaddr = tp->mmio_addr;
5891
5892 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005893
françois romieuf0298f82011-01-03 15:07:42 +00005894 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005895
5896 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005897}
5898
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005899static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005900{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005901 void __iomem *ioaddr = tp->mmio_addr;
5902 struct pci_dev *pdev = tp->pci_dev;
5903
Francois Romieub726e492008-06-28 12:22:59 +02005904 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5905
5906 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5907
françois romieufaf1e782013-02-27 13:01:57 +00005908 if (tp->dev->mtu <= ETH_DATA_LEN)
5909 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005910
5911 rtl_disable_clock_request(pdev);
5912
5913 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005914}
5915
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005916static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005917{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005918 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005919 { 0x01, 0, 0x0001 },
5920 { 0x02, 0x0800, 0x1000 },
5921 { 0x03, 0, 0x0042 },
5922 { 0x06, 0x0080, 0x0000 },
5923 { 0x07, 0, 0x2000 }
5924 };
5925
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005926 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005927
Francois Romieufdf6fc02012-07-06 22:40:38 +02005928 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005929
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005930 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005931}
5932
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005933static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005934{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005935 void __iomem *ioaddr = tp->mmio_addr;
5936 struct pci_dev *pdev = tp->pci_dev;
5937
5938 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005939
5940 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5941
françois romieufaf1e782013-02-27 13:01:57 +00005942 if (tp->dev->mtu <= ETH_DATA_LEN)
5943 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005944
5945 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5946}
5947
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005948static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005949{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005950 void __iomem *ioaddr = tp->mmio_addr;
5951 struct pci_dev *pdev = tp->pci_dev;
5952
5953 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005954
5955 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5956
5957 /* Magic. */
5958 RTL_W8(DBG_REG, 0x20);
5959
françois romieuf0298f82011-01-03 15:07:42 +00005960 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005961
françois romieufaf1e782013-02-27 13:01:57 +00005962 if (tp->dev->mtu <= ETH_DATA_LEN)
5963 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005964
5965 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5966}
5967
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005968static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005969{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005970 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005971 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005972 { 0x02, 0x0800, 0x1000 },
5973 { 0x03, 0, 0x0002 },
5974 { 0x06, 0x0080, 0x0000 }
5975 };
5976
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005977 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005978
5979 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5980
Francois Romieufdf6fc02012-07-06 22:40:38 +02005981 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005982
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005983 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005984}
5985
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005986static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005987{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005988 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005989 { 0x01, 0, 0x0001 },
5990 { 0x03, 0x0400, 0x0220 }
5991 };
5992
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005993 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005994
Francois Romieufdf6fc02012-07-06 22:40:38 +02005995 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005996
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005997 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005998}
5999
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006000static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02006001{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006002 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02006003}
6004
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006005static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02006006{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006007 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006008
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006009 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006010}
6011
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006012static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02006013{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006014 void __iomem *ioaddr = tp->mmio_addr;
6015 struct pci_dev *pdev = tp->pci_dev;
6016
6017 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02006018
6019 rtl_disable_clock_request(pdev);
6020
françois romieuf0298f82011-01-03 15:07:42 +00006021 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02006022
françois romieufaf1e782013-02-27 13:01:57 +00006023 if (tp->dev->mtu <= ETH_DATA_LEN)
6024 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02006025
6026 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6027}
6028
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006029static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00006030{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006031 void __iomem *ioaddr = tp->mmio_addr;
6032 struct pci_dev *pdev = tp->pci_dev;
6033
6034 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006035
françois romieufaf1e782013-02-27 13:01:57 +00006036 if (tp->dev->mtu <= ETH_DATA_LEN)
6037 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00006038
6039 RTL_W8(MaxTxPacketSize, TxPacketMax);
6040
6041 rtl_disable_clock_request(pdev);
6042}
6043
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006044static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00006045{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006046 void __iomem *ioaddr = tp->mmio_addr;
6047 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00006048 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006049 { 0x0b, 0x0000, 0x0048 },
6050 { 0x19, 0x0020, 0x0050 },
6051 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00006052 };
françois romieue6de30d2011-01-03 15:08:37 +00006053
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006054 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00006055
6056 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6057
6058 RTL_W8(MaxTxPacketSize, TxPacketMax);
6059
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006060 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00006061
6062 rtl_enable_clock_request(pdev);
6063}
6064
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006065static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00006066{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006067 void __iomem *ioaddr = tp->mmio_addr;
6068 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006069 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00006070 { 0x00, 0x0200, 0x0100 },
6071 { 0x00, 0x0000, 0x0004 },
6072 { 0x06, 0x0002, 0x0001 },
6073 { 0x06, 0x0000, 0x0030 },
6074 { 0x07, 0x0000, 0x2000 },
6075 { 0x00, 0x0000, 0x0020 },
6076 { 0x03, 0x5800, 0x2000 },
6077 { 0x03, 0x0000, 0x0001 },
6078 { 0x01, 0x0800, 0x1000 },
6079 { 0x07, 0x0000, 0x4000 },
6080 { 0x1e, 0x0000, 0x2000 },
6081 { 0x19, 0xffff, 0xfe6c },
6082 { 0x0a, 0x0000, 0x0040 }
6083 };
6084
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006085 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006086
Francois Romieufdf6fc02012-07-06 22:40:38 +02006087 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00006088
françois romieufaf1e782013-02-27 13:01:57 +00006089 if (tp->dev->mtu <= ETH_DATA_LEN)
6090 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00006091
6092 RTL_W8(MaxTxPacketSize, TxPacketMax);
6093
6094 rtl_disable_clock_request(pdev);
6095
6096 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02006097 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6098 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00006099
Francois Romieucecb5fd2011-04-01 10:21:07 +02006100 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00006101}
6102
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006103static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08006104{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006105 void __iomem *ioaddr = tp->mmio_addr;
6106 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006107 static const struct ephy_info e_info_8168e_2[] = {
6108 { 0x09, 0x0000, 0x0080 },
6109 { 0x19, 0x0000, 0x0224 }
6110 };
6111
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006112 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006113
Francois Romieufdf6fc02012-07-06 22:40:38 +02006114 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08006115
françois romieufaf1e782013-02-27 13:01:57 +00006116 if (tp->dev->mtu <= ETH_DATA_LEN)
6117 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08006118
Francois Romieufdf6fc02012-07-06 22:40:38 +02006119 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6120 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6121 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6122 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6123 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6124 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006125 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6126 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08006127
Hayes Wang3090bd92011-09-06 16:55:15 +08006128 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08006129
Francois Romieu4521e1a92012-11-01 16:46:28 +00006130 rtl_disable_clock_request(pdev);
6131
Hayes Wang70090422011-07-06 15:58:06 +08006132 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6133 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6134
6135 /* Adjust EEE LED frequency */
6136 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6137
6138 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6139 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006140 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08006141}
6142
Hayes Wang5f886e02012-03-30 14:33:03 +08006143static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08006144{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006145 void __iomem *ioaddr = tp->mmio_addr;
6146 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08006147
Hayes Wang5f886e02012-03-30 14:33:03 +08006148 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006149
6150 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6151
Francois Romieufdf6fc02012-07-06 22:40:38 +02006152 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6153 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6154 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6155 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006156 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6157 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6158 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6159 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006160 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6161 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006162
6163 RTL_W8(MaxTxPacketSize, EarlySize);
6164
Francois Romieu4521e1a92012-11-01 16:46:28 +00006165 rtl_disable_clock_request(pdev);
6166
Hayes Wangc2218922011-09-06 16:55:18 +08006167 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6168 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08006169 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006170 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6171 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006172}
6173
Hayes Wang5f886e02012-03-30 14:33:03 +08006174static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6175{
6176 void __iomem *ioaddr = tp->mmio_addr;
6177 static const struct ephy_info e_info_8168f_1[] = {
6178 { 0x06, 0x00c0, 0x0020 },
6179 { 0x08, 0x0001, 0x0002 },
6180 { 0x09, 0x0000, 0x0080 },
6181 { 0x19, 0x0000, 0x0224 }
6182 };
6183
6184 rtl_hw_start_8168f(tp);
6185
Francois Romieufdf6fc02012-07-06 22:40:38 +02006186 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006187
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006188 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006189
6190 /* Adjust EEE LED frequency */
6191 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6192}
6193
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006194static void rtl_hw_start_8411(struct rtl8169_private *tp)
6195{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006196 static const struct ephy_info e_info_8168f_1[] = {
6197 { 0x06, 0x00c0, 0x0020 },
6198 { 0x0f, 0xffff, 0x5200 },
6199 { 0x1e, 0x0000, 0x4000 },
6200 { 0x19, 0x0000, 0x0224 }
6201 };
6202
6203 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006204 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006205
Francois Romieufdf6fc02012-07-06 22:40:38 +02006206 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006207
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006208 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006209}
6210
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006211static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006212{
6213 void __iomem *ioaddr = tp->mmio_addr;
6214 struct pci_dev *pdev = tp->pci_dev;
6215
hayeswangbeb330a2013-04-01 22:23:39 +00006216 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6217
Hayes Wangc5583862012-07-02 17:23:22 +08006218 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6219 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6220 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6221 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6222
6223 rtl_csi_access_enable_1(tp);
6224
6225 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6226
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006227 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6228 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006229 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006230
Francois Romieu4521e1a92012-11-01 16:46:28 +00006231 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006232 RTL_W8(MaxTxPacketSize, EarlySize);
6233
6234 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6235 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6236
6237 /* Adjust EEE LED frequency */
6238 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6239
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006240 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6241 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006242
6243 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006244}
6245
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006246static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6247{
6248 void __iomem *ioaddr = tp->mmio_addr;
6249 static const struct ephy_info e_info_8168g_1[] = {
6250 { 0x00, 0x0000, 0x0008 },
6251 { 0x0c, 0x37d0, 0x0820 },
6252 { 0x1e, 0x0000, 0x0001 },
6253 { 0x19, 0x8000, 0x0000 }
6254 };
6255
6256 rtl_hw_start_8168g(tp);
6257
6258 /* disable aspm and clock request before access ephy */
6259 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6260 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6261 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6262}
6263
hayeswang57538c42013-04-01 22:23:40 +00006264static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6265{
6266 void __iomem *ioaddr = tp->mmio_addr;
6267 static const struct ephy_info e_info_8168g_2[] = {
6268 { 0x00, 0x0000, 0x0008 },
6269 { 0x0c, 0x3df0, 0x0200 },
6270 { 0x19, 0xffff, 0xfc00 },
6271 { 0x1e, 0xffff, 0x20eb }
6272 };
6273
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006274 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006275
6276 /* disable aspm and clock request before access ephy */
6277 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6278 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6279 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6280}
6281
hayeswang45dd95c2013-07-08 17:09:01 +08006282static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6283{
6284 void __iomem *ioaddr = tp->mmio_addr;
6285 static const struct ephy_info e_info_8411_2[] = {
6286 { 0x00, 0x0000, 0x0008 },
6287 { 0x0c, 0x3df0, 0x0200 },
6288 { 0x0f, 0xffff, 0x5200 },
6289 { 0x19, 0x0020, 0x0000 },
6290 { 0x1e, 0x0000, 0x2000 }
6291 };
6292
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006293 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006294
6295 /* disable aspm and clock request before access ephy */
6296 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6297 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6298 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6299}
6300
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006301static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6302{
6303 void __iomem *ioaddr = tp->mmio_addr;
6304 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006305 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006306 u32 data;
6307 static const struct ephy_info e_info_8168h_1[] = {
6308 { 0x1e, 0x0800, 0x0001 },
6309 { 0x1d, 0x0000, 0x0800 },
6310 { 0x05, 0xffff, 0x2089 },
6311 { 0x06, 0xffff, 0x5881 },
6312 { 0x04, 0xffff, 0x154a },
6313 { 0x01, 0xffff, 0x068b }
6314 };
6315
6316 /* disable aspm and clock request before access ephy */
6317 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6318 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6319 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6320
6321 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6322
6323 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6324 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6325 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6326 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6327
6328 rtl_csi_access_enable_1(tp);
6329
6330 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6331
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006332 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6333 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006334
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006335 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006336
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006337 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006338
6339 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6340
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006341 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6342 RTL_W8(MaxTxPacketSize, EarlySize);
6343
6344 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6345 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6346
6347 /* Adjust EEE LED frequency */
6348 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6349
6350 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006351 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006352
6353 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6354
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006355 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006356
6357 rtl_pcie_state_l2l3_enable(tp, false);
6358
6359 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006360 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006361 rtl_writephy(tp, 0x1f, 0x0000);
6362 if (rg_saw_cnt > 0) {
6363 u16 sw_cnt_1ms_ini;
6364
6365 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6366 sw_cnt_1ms_ini &= 0x0fff;
6367 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006368 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006369 data |= sw_cnt_1ms_ini;
6370 r8168_mac_ocp_write(tp, 0xd412, data);
6371 }
6372
6373 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006374 data &= ~0xf0;
6375 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006376 r8168_mac_ocp_write(tp, 0xe056, data);
6377
6378 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006379 data &= ~0x6000;
6380 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006381 r8168_mac_ocp_write(tp, 0xe052, data);
6382
6383 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006384 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006385 data |= 0x017f;
6386 r8168_mac_ocp_write(tp, 0xe0d6, data);
6387
6388 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006389 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006390 data |= 0x047f;
6391 r8168_mac_ocp_write(tp, 0xd420, data);
6392
6393 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6394 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6395 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6396 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6397}
6398
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006399static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6400{
6401 void __iomem *ioaddr = tp->mmio_addr;
6402 struct pci_dev *pdev = tp->pci_dev;
6403
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006404 rtl8168ep_stop_cmac(tp);
6405
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006406 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6407
6408 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6409 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6410 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6411 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6412
6413 rtl_csi_access_enable_1(tp);
6414
6415 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6416
6417 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6418 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6419
6420 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6421
6422 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6423
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006424 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6425 RTL_W8(MaxTxPacketSize, EarlySize);
6426
6427 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6428 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6429
6430 /* Adjust EEE LED frequency */
6431 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6432
6433 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6434
6435 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6436
6437 rtl_pcie_state_l2l3_enable(tp, false);
6438}
6439
6440static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6441{
6442 void __iomem *ioaddr = tp->mmio_addr;
6443 static const struct ephy_info e_info_8168ep_1[] = {
6444 { 0x00, 0xffff, 0x10ab },
6445 { 0x06, 0xffff, 0xf030 },
6446 { 0x08, 0xffff, 0x2006 },
6447 { 0x0d, 0xffff, 0x1666 },
6448 { 0x0c, 0x3ff0, 0x0000 }
6449 };
6450
6451 /* disable aspm and clock request before access ephy */
6452 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6453 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6454 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6455
6456 rtl_hw_start_8168ep(tp);
6457}
6458
6459static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6460{
6461 void __iomem *ioaddr = tp->mmio_addr;
6462 static const struct ephy_info e_info_8168ep_2[] = {
6463 { 0x00, 0xffff, 0x10a3 },
6464 { 0x19, 0xffff, 0xfc00 },
6465 { 0x1e, 0xffff, 0x20ea }
6466 };
6467
6468 /* disable aspm and clock request before access ephy */
6469 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6470 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6471 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6472
6473 rtl_hw_start_8168ep(tp);
6474
6475 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006476 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006477}
6478
6479static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6480{
6481 void __iomem *ioaddr = tp->mmio_addr;
6482 u32 data;
6483 static const struct ephy_info e_info_8168ep_3[] = {
6484 { 0x00, 0xffff, 0x10a3 },
6485 { 0x19, 0xffff, 0x7c00 },
6486 { 0x1e, 0xffff, 0x20eb },
6487 { 0x0d, 0xffff, 0x1666 }
6488 };
6489
6490 /* disable aspm and clock request before access ephy */
6491 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6492 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6493 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6494
6495 rtl_hw_start_8168ep(tp);
6496
6497 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006498 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006499
6500 data = r8168_mac_ocp_read(tp, 0xd3e2);
6501 data &= 0xf000;
6502 data |= 0x0271;
6503 r8168_mac_ocp_write(tp, 0xd3e2, data);
6504
6505 data = r8168_mac_ocp_read(tp, 0xd3e4);
6506 data &= 0xff00;
6507 r8168_mac_ocp_write(tp, 0xd3e4, data);
6508
6509 data = r8168_mac_ocp_read(tp, 0xe860);
6510 data |= 0x0080;
6511 r8168_mac_ocp_write(tp, 0xe860, data);
6512}
6513
Francois Romieu07ce4062007-02-23 23:36:39 +01006514static void rtl_hw_start_8168(struct net_device *dev)
6515{
Francois Romieu2dd99532007-06-11 23:22:52 +02006516 struct rtl8169_private *tp = netdev_priv(dev);
6517 void __iomem *ioaddr = tp->mmio_addr;
6518
6519 RTL_W8(Cfg9346, Cfg9346_Unlock);
6520
françois romieuf0298f82011-01-03 15:07:42 +00006521 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006522
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006523 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006524
Francois Romieu0e485152007-02-20 00:00:26 +01006525 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006526
6527 RTL_W16(CPlusCmd, tp->cp_cmd);
6528
Francois Romieu0e485152007-02-20 00:00:26 +01006529 RTL_W16(IntrMitigate, 0x5151);
6530
6531 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006532 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006533 tp->event_slow |= RxFIFOOver | PCSTimeout;
6534 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006535 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006536
6537 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6538
hayeswang1a964642013-04-01 22:23:41 +00006539 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006540
6541 RTL_R8(IntrMask);
6542
Francois Romieu219a1e92008-06-28 11:58:39 +02006543 switch (tp->mac_version) {
6544 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006545 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006546 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006547
6548 case RTL_GIGA_MAC_VER_12:
6549 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006550 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006551 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006552
6553 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006554 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006555 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006556
6557 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006558 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006559 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006560
6561 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006562 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006563 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006564
Francois Romieu197ff762008-06-28 13:16:02 +02006565 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006566 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006567 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006568
Francois Romieu6fb07052008-06-29 11:54:28 +02006569 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006570 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006571 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006572
Francois Romieuef3386f2008-06-29 12:24:30 +02006573 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006574 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006575 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006576
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006577 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006578 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006579 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006580
Francois Romieu5b538df2008-07-20 16:22:45 +02006581 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006582 case RTL_GIGA_MAC_VER_26:
6583 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006584 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006585 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006586
françois romieue6de30d2011-01-03 15:08:37 +00006587 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006588 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006589 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006590
hayeswang4804b3b2011-03-21 01:50:29 +00006591 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006592 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006593 break;
6594
hayeswang01dc7fe2011-03-21 01:50:28 +00006595 case RTL_GIGA_MAC_VER_32:
6596 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006597 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006598 break;
6599 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006600 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006601 break;
françois romieue6de30d2011-01-03 15:08:37 +00006602
Hayes Wangc2218922011-09-06 16:55:18 +08006603 case RTL_GIGA_MAC_VER_35:
6604 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006605 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006606 break;
6607
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006608 case RTL_GIGA_MAC_VER_38:
6609 rtl_hw_start_8411(tp);
6610 break;
6611
Hayes Wangc5583862012-07-02 17:23:22 +08006612 case RTL_GIGA_MAC_VER_40:
6613 case RTL_GIGA_MAC_VER_41:
6614 rtl_hw_start_8168g_1(tp);
6615 break;
hayeswang57538c42013-04-01 22:23:40 +00006616 case RTL_GIGA_MAC_VER_42:
6617 rtl_hw_start_8168g_2(tp);
6618 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006619
hayeswang45dd95c2013-07-08 17:09:01 +08006620 case RTL_GIGA_MAC_VER_44:
6621 rtl_hw_start_8411_2(tp);
6622 break;
6623
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006624 case RTL_GIGA_MAC_VER_45:
6625 case RTL_GIGA_MAC_VER_46:
6626 rtl_hw_start_8168h_1(tp);
6627 break;
6628
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006629 case RTL_GIGA_MAC_VER_49:
6630 rtl_hw_start_8168ep_1(tp);
6631 break;
6632
6633 case RTL_GIGA_MAC_VER_50:
6634 rtl_hw_start_8168ep_2(tp);
6635 break;
6636
6637 case RTL_GIGA_MAC_VER_51:
6638 rtl_hw_start_8168ep_3(tp);
6639 break;
6640
Francois Romieu219a1e92008-06-28 11:58:39 +02006641 default:
6642 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6643 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006644 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006645 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006646
hayeswang1a964642013-04-01 22:23:41 +00006647 RTL_W8(Cfg9346, Cfg9346_Lock);
6648
Francois Romieu0e485152007-02-20 00:00:26 +01006649 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6650
hayeswang1a964642013-04-01 22:23:41 +00006651 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006652
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006653 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
Francois Romieu2857ffb2008-08-02 21:08:49 +02006656#define R810X_CPCMD_QUIRK_MASK (\
6657 EnableBist | \
6658 Mac_dbgo_oe | \
6659 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006660 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006661 Force_txflow_en | \
6662 Cxpl_dbg_sel | \
6663 ASF | \
6664 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006665 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006666
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006667static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006668{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006669 void __iomem *ioaddr = tp->mmio_addr;
6670 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006671 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006672 { 0x01, 0, 0x6e65 },
6673 { 0x02, 0, 0x091f },
6674 { 0x03, 0, 0xc2f9 },
6675 { 0x06, 0, 0xafb5 },
6676 { 0x07, 0, 0x0e00 },
6677 { 0x19, 0, 0xec80 },
6678 { 0x01, 0, 0x2e65 },
6679 { 0x01, 0, 0x6e65 }
6680 };
6681 u8 cfg1;
6682
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006683 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006684
6685 RTL_W8(DBG_REG, FIX_NAK_1);
6686
6687 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6688
6689 RTL_W8(Config1,
6690 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6691 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6692
6693 cfg1 = RTL_R8(Config1);
6694 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6695 RTL_W8(Config1, cfg1 & ~LEDS0);
6696
Francois Romieufdf6fc02012-07-06 22:40:38 +02006697 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006698}
6699
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006700static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006701{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006702 void __iomem *ioaddr = tp->mmio_addr;
6703 struct pci_dev *pdev = tp->pci_dev;
6704
6705 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006706
6707 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6708
6709 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6710 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006711}
6712
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006713static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006714{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006715 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006716
Francois Romieufdf6fc02012-07-06 22:40:38 +02006717 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006718}
6719
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006720static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006721{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006722 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006723 static const struct ephy_info e_info_8105e_1[] = {
6724 { 0x07, 0, 0x4000 },
6725 { 0x19, 0, 0x0200 },
6726 { 0x19, 0, 0x0020 },
6727 { 0x1e, 0, 0x2000 },
6728 { 0x03, 0, 0x0001 },
6729 { 0x19, 0, 0x0100 },
6730 { 0x19, 0, 0x0004 },
6731 { 0x0a, 0, 0x0020 }
6732 };
6733
Francois Romieucecb5fd2011-04-01 10:21:07 +02006734 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006735 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6736
Francois Romieucecb5fd2011-04-01 10:21:07 +02006737 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006738 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6739
6740 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006741 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006742
Francois Romieufdf6fc02012-07-06 22:40:38 +02006743 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006744
6745 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006746}
6747
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006748static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006749{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006750 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006751 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006752}
6753
Hayes Wang7e18dca2012-03-30 14:33:02 +08006754static void rtl_hw_start_8402(struct rtl8169_private *tp)
6755{
6756 void __iomem *ioaddr = tp->mmio_addr;
6757 static const struct ephy_info e_info_8402[] = {
6758 { 0x19, 0xffff, 0xff64 },
6759 { 0x1e, 0, 0x4000 }
6760 };
6761
6762 rtl_csi_access_enable_2(tp);
6763
6764 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6765 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6766
6767 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6768 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6769
Francois Romieufdf6fc02012-07-06 22:40:38 +02006770 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006771
6772 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6773
Francois Romieufdf6fc02012-07-06 22:40:38 +02006774 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6775 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006776 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6777 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006778 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6779 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006780 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006781
6782 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006783}
6784
Hayes Wang5598bfe2012-07-02 17:23:21 +08006785static void rtl_hw_start_8106(struct rtl8169_private *tp)
6786{
6787 void __iomem *ioaddr = tp->mmio_addr;
6788
6789 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6790 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6791
Francois Romieu4521e1a92012-11-01 16:46:28 +00006792 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006793 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6794 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006795
6796 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006797}
6798
Francois Romieu07ce4062007-02-23 23:36:39 +01006799static void rtl_hw_start_8101(struct net_device *dev)
6800{
Francois Romieucdf1a602007-06-11 23:29:50 +02006801 struct rtl8169_private *tp = netdev_priv(dev);
6802 void __iomem *ioaddr = tp->mmio_addr;
6803 struct pci_dev *pdev = tp->pci_dev;
6804
Francois Romieuda78dbf2012-01-26 14:18:23 +01006805 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6806 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006807
Francois Romieucecb5fd2011-04-01 10:21:07 +02006808 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006809 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006810 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6811 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006812
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006813 RTL_W8(Cfg9346, Cfg9346_Unlock);
6814
hayeswang1a964642013-04-01 22:23:41 +00006815 RTL_W8(MaxTxPacketSize, TxPacketMax);
6816
6817 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6818
6819 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6820 RTL_W16(CPlusCmd, tp->cp_cmd);
6821
6822 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6823
6824 rtl_set_rx_tx_config_registers(tp);
6825
Francois Romieu2857ffb2008-08-02 21:08:49 +02006826 switch (tp->mac_version) {
6827 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006828 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006829 break;
6830
6831 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006832 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006833 break;
6834
6835 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006836 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006837 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006838
6839 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006840 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006841 break;
6842 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006843 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006844 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006845
6846 case RTL_GIGA_MAC_VER_37:
6847 rtl_hw_start_8402(tp);
6848 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006849
6850 case RTL_GIGA_MAC_VER_39:
6851 rtl_hw_start_8106(tp);
6852 break;
hayeswang58152cd2013-04-01 22:23:42 +00006853 case RTL_GIGA_MAC_VER_43:
6854 rtl_hw_start_8168g_2(tp);
6855 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006856 case RTL_GIGA_MAC_VER_47:
6857 case RTL_GIGA_MAC_VER_48:
6858 rtl_hw_start_8168h_1(tp);
6859 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006860 }
6861
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006862 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006863
Francois Romieucdf1a602007-06-11 23:29:50 +02006864 RTL_W16(IntrMitigate, 0x0000);
6865
Francois Romieucdf1a602007-06-11 23:29:50 +02006866 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006867
Francois Romieucdf1a602007-06-11 23:29:50 +02006868 rtl_set_rx_mode(dev);
6869
hayeswang1a964642013-04-01 22:23:41 +00006870 RTL_R8(IntrMask);
6871
Francois Romieucdf1a602007-06-11 23:29:50 +02006872 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873}
6874
6875static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6876{
Francois Romieud58d46b2011-05-03 16:38:29 +02006877 struct rtl8169_private *tp = netdev_priv(dev);
6878
Francois Romieud58d46b2011-05-03 16:38:29 +02006879 if (new_mtu > ETH_DATA_LEN)
6880 rtl_hw_jumbo_enable(tp);
6881 else
6882 rtl_hw_jumbo_disable(tp);
6883
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006885 netdev_update_features(dev);
6886
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888}
6889
6890static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6891{
Al Viro95e09182007-12-22 18:55:39 +00006892 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6894}
6895
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006896static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6897 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006899 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006900 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006901
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006902 kfree(*data_buff);
6903 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 rtl8169_make_unusable_by_asic(desc);
6905}
6906
6907static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6908{
6909 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6910
Alexander Duycka0750132014-12-11 15:02:17 -08006911 /* Force memory writes to complete before releasing descriptor */
6912 dma_wmb();
6913
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6915}
6916
6917static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6918 u32 rx_buf_sz)
6919{
6920 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 rtl8169_mark_to_asic(desc, rx_buf_sz);
6922}
6923
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006924static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006926 return (void *)ALIGN((long)data, 16);
6927}
6928
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006929static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6930 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006931{
6932 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006934 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006935 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006936 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006938 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6939 if (!data)
6940 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006941
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006942 if (rtl8169_align(data) != data) {
6943 kfree(data);
6944 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6945 if (!data)
6946 return NULL;
6947 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006948
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006949 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006950 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006951 if (unlikely(dma_mapping_error(d, mapping))) {
6952 if (net_ratelimit())
6953 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006954 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
6957 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006958 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006959
6960err_out:
6961 kfree(data);
6962 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963}
6964
6965static void rtl8169_rx_clear(struct rtl8169_private *tp)
6966{
Francois Romieu07d3f512007-02-21 22:40:46 +01006967 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
6969 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006970 if (tp->Rx_databuff[i]) {
6971 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 tp->RxDescArray + i);
6973 }
6974 }
6975}
6976
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006977static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006979 desc->opts1 |= cpu_to_le32(RingEnd);
6980}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006981
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006982static int rtl8169_rx_fill(struct rtl8169_private *tp)
6983{
6984 unsigned int i;
6985
6986 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006987 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006988
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006989 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006991
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006992 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006993 if (!data) {
6994 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006995 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006996 }
6997 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007000 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7001 return 0;
7002
7003err_out:
7004 rtl8169_rx_clear(tp);
7005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006}
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008static int rtl8169_init_ring(struct net_device *dev)
7009{
7010 struct rtl8169_private *tp = netdev_priv(dev);
7011
7012 rtl8169_init_ring_indexes(tp);
7013
7014 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007015 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007017 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018}
7019
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007020static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 struct TxDesc *desc)
7022{
7023 unsigned int len = tx_skb->len;
7024
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007025 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7026
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 desc->opts1 = 0x00;
7028 desc->opts2 = 0x00;
7029 desc->addr = 0x00;
7030 tx_skb->len = 0;
7031}
7032
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007033static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7034 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035{
7036 unsigned int i;
7037
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007038 for (i = 0; i < n; i++) {
7039 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 struct ring_info *tx_skb = tp->tx_skb + entry;
7041 unsigned int len = tx_skb->len;
7042
7043 if (len) {
7044 struct sk_buff *skb = tx_skb->skb;
7045
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007046 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 tp->TxDescArray + entry);
7048 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007049 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 tx_skb->skb = NULL;
7051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 }
7053 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007054}
7055
7056static void rtl8169_tx_clear(struct rtl8169_private *tp)
7057{
7058 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 tp->cur_tx = tp->dirty_tx = 0;
7060}
7061
Francois Romieu4422bcd2012-01-26 11:23:32 +01007062static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063{
David Howellsc4028952006-11-22 14:57:56 +00007064 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01007065 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
Francois Romieuda78dbf2012-01-26 14:18:23 +01007067 napi_disable(&tp->napi);
7068 netif_stop_queue(dev);
7069 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
françois romieuc7c2c392011-12-04 20:30:52 +00007071 rtl8169_hw_reset(tp);
7072
Francois Romieu56de4142011-03-15 17:29:31 +01007073 for (i = 0; i < NUM_RX_DESC; i++)
7074 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7075
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00007077 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
Francois Romieuda78dbf2012-01-26 14:18:23 +01007079 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01007080 rtl_hw_start(dev);
7081 netif_wake_queue(dev);
7082 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083}
7084
7085static void rtl8169_tx_timeout(struct net_device *dev)
7086{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007087 struct rtl8169_private *tp = netdev_priv(dev);
7088
7089 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090}
7091
7092static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07007093 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094{
7095 struct skb_shared_info *info = skb_shinfo(skb);
7096 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007097 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007098 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
7100 entry = tp->cur_tx;
7101 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007102 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 dma_addr_t mapping;
7104 u32 status, len;
7105 void *addr;
7106
7107 entry = (entry + 1) % NUM_TX_DESC;
7108
7109 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00007110 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00007111 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007112 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007113 if (unlikely(dma_mapping_error(d, mapping))) {
7114 if (net_ratelimit())
7115 netif_err(tp, drv, tp->dev,
7116 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007117 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
Francois Romieucecb5fd2011-04-01 10:21:07 +02007120 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007121 status = opts[0] | len |
7122 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
7124 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07007125 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 txd->addr = cpu_to_le64(mapping);
7127
7128 tp->tx_skb[entry].len = len;
7129 }
7130
7131 if (cur_frag) {
7132 tp->tx_skb[entry].skb = skb;
7133 txd->opts1 |= cpu_to_le32(LastFrag);
7134 }
7135
7136 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007137
7138err_out:
7139 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7140 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141}
7142
françois romieub423e9a2013-05-18 01:24:46 +00007143static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7144{
7145 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7146}
7147
hayeswange9746042014-07-11 16:25:58 +08007148static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7149 struct net_device *dev);
7150/* r8169_csum_workaround()
7151 * The hw limites the value the transport offset. When the offset is out of the
7152 * range, calculate the checksum by sw.
7153 */
7154static void r8169_csum_workaround(struct rtl8169_private *tp,
7155 struct sk_buff *skb)
7156{
7157 if (skb_shinfo(skb)->gso_size) {
7158 netdev_features_t features = tp->dev->features;
7159 struct sk_buff *segs, *nskb;
7160
7161 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7162 segs = skb_gso_segment(skb, features);
7163 if (IS_ERR(segs) || !segs)
7164 goto drop;
7165
7166 do {
7167 nskb = segs;
7168 segs = segs->next;
7169 nskb->next = NULL;
7170 rtl8169_start_xmit(nskb, tp->dev);
7171 } while (segs);
7172
Alexander Duyckeb781392015-05-01 10:34:44 -07007173 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007174 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7175 if (skb_checksum_help(skb) < 0)
7176 goto drop;
7177
7178 rtl8169_start_xmit(skb, tp->dev);
7179 } else {
7180 struct net_device_stats *stats;
7181
7182drop:
7183 stats = &tp->dev->stats;
7184 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007185 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007186 }
7187}
7188
7189/* msdn_giant_send_check()
7190 * According to the document of microsoft, the TCP Pseudo Header excludes the
7191 * packet length for IPv6 TCP large packets.
7192 */
7193static int msdn_giant_send_check(struct sk_buff *skb)
7194{
7195 const struct ipv6hdr *ipv6h;
7196 struct tcphdr *th;
7197 int ret;
7198
7199 ret = skb_cow_head(skb, 0);
7200 if (ret)
7201 return ret;
7202
7203 ipv6h = ipv6_hdr(skb);
7204 th = tcp_hdr(skb);
7205
7206 th->check = 0;
7207 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7208
7209 return ret;
7210}
7211
7212static inline __be16 get_protocol(struct sk_buff *skb)
7213{
7214 __be16 protocol;
7215
7216 if (skb->protocol == htons(ETH_P_8021Q))
7217 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7218 else
7219 protocol = skb->protocol;
7220
7221 return protocol;
7222}
7223
hayeswang5888d3f2014-07-11 16:25:56 +08007224static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7225 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226{
Michał Mirosław350fb322011-04-08 06:35:56 +00007227 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
Francois Romieu2b7b4312011-04-18 22:53:24 -07007229 if (mss) {
7230 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007231 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7232 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7233 const struct iphdr *ip = ip_hdr(skb);
7234
7235 if (ip->protocol == IPPROTO_TCP)
7236 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7237 else if (ip->protocol == IPPROTO_UDP)
7238 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7239 else
7240 WARN_ON_ONCE(1);
7241 }
7242
7243 return true;
7244}
7245
7246static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7247 struct sk_buff *skb, u32 *opts)
7248{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007249 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007250 u32 mss = skb_shinfo(skb)->gso_size;
7251
7252 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007253 if (transport_offset > GTTCPHO_MAX) {
7254 netif_warn(tp, tx_err, tp->dev,
7255 "Invalid transport offset 0x%x for TSO\n",
7256 transport_offset);
7257 return false;
7258 }
7259
7260 switch (get_protocol(skb)) {
7261 case htons(ETH_P_IP):
7262 opts[0] |= TD1_GTSENV4;
7263 break;
7264
7265 case htons(ETH_P_IPV6):
7266 if (msdn_giant_send_check(skb))
7267 return false;
7268
7269 opts[0] |= TD1_GTSENV6;
7270 break;
7271
7272 default:
7273 WARN_ON_ONCE(1);
7274 break;
7275 }
7276
hayeswangbdfa4ed2014-07-11 16:25:57 +08007277 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007278 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007279 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007280 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
françois romieub423e9a2013-05-18 01:24:46 +00007282 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007283 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007284
hayeswange9746042014-07-11 16:25:58 +08007285 if (transport_offset > TCPHO_MAX) {
7286 netif_warn(tp, tx_err, tp->dev,
7287 "Invalid transport offset 0x%x\n",
7288 transport_offset);
7289 return false;
7290 }
7291
7292 switch (get_protocol(skb)) {
7293 case htons(ETH_P_IP):
7294 opts[1] |= TD1_IPv4_CS;
7295 ip_protocol = ip_hdr(skb)->protocol;
7296 break;
7297
7298 case htons(ETH_P_IPV6):
7299 opts[1] |= TD1_IPv6_CS;
7300 ip_protocol = ipv6_hdr(skb)->nexthdr;
7301 break;
7302
7303 default:
7304 ip_protocol = IPPROTO_RAW;
7305 break;
7306 }
7307
7308 if (ip_protocol == IPPROTO_TCP)
7309 opts[1] |= TD1_TCP_CS;
7310 else if (ip_protocol == IPPROTO_UDP)
7311 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007312 else
7313 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007314
7315 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007316 } else {
7317 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007318 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 }
hayeswang5888d3f2014-07-11 16:25:56 +08007320
françois romieub423e9a2013-05-18 01:24:46 +00007321 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322}
7323
Stephen Hemminger613573252009-08-31 19:50:58 +00007324static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7325 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326{
7327 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007328 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329 struct TxDesc *txd = tp->TxDescArray + entry;
7330 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007331 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 dma_addr_t mapping;
7333 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007334 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007335 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007336
Julien Ducourthial477206a2012-05-09 00:00:06 +02007337 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007338 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007339 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 }
7341
7342 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007343 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344
françois romieub423e9a2013-05-18 01:24:46 +00007345 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7346 opts[0] = DescOwn;
7347
hayeswange9746042014-07-11 16:25:58 +08007348 if (!tp->tso_csum(tp, skb, opts)) {
7349 r8169_csum_workaround(tp, skb);
7350 return NETDEV_TX_OK;
7351 }
françois romieub423e9a2013-05-18 01:24:46 +00007352
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007353 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007354 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007355 if (unlikely(dma_mapping_error(d, mapping))) {
7356 if (net_ratelimit())
7357 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007358 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
7361 tp->tx_skb[entry].len = len;
7362 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
Francois Romieu2b7b4312011-04-18 22:53:24 -07007364 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007365 if (frags < 0)
7366 goto err_dma_1;
7367 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007368 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007369 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007370 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007371 tp->tx_skb[entry].skb = skb;
7372 }
7373
Francois Romieu2b7b4312011-04-18 22:53:24 -07007374 txd->opts2 = cpu_to_le32(opts[1]);
7375
Richard Cochran5047fb52012-03-10 07:29:42 +00007376 skb_tx_timestamp(skb);
7377
Alexander Duycka0750132014-12-11 15:02:17 -08007378 /* Force memory writes to complete before releasing descriptor */
7379 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380
Francois Romieucecb5fd2011-04-01 10:21:07 +02007381 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007382 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 txd->opts1 = cpu_to_le32(status);
7384
Alexander Duycka0750132014-12-11 15:02:17 -08007385 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007386 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Alexander Duycka0750132014-12-11 15:02:17 -08007388 tp->cur_tx += frags + 1;
7389
David S. Miller87cda7c2015-02-22 15:54:29 -05007390 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
David S. Miller87cda7c2015-02-22 15:54:29 -05007392 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007393
David S. Miller87cda7c2015-02-22 15:54:29 -05007394 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007395 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7396 * not miss a ring update when it notices a stopped queue.
7397 */
7398 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007400 /* Sync with rtl_tx:
7401 * - publish queue status and cur_tx ring index (write barrier)
7402 * - refresh dirty_tx ring index (read barrier).
7403 * May the current thread have a pessimistic view of the ring
7404 * status and forget to wake up queue, a racing rtl_tx thread
7405 * can't.
7406 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007407 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007408 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 netif_wake_queue(dev);
7410 }
7411
Stephen Hemminger613573252009-08-31 19:50:58 +00007412 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007414err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007415 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007416err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007417 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007418 dev->stats.tx_dropped++;
7419 return NETDEV_TX_OK;
7420
7421err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007423 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007424 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425}
7426
7427static void rtl8169_pcierr_interrupt(struct net_device *dev)
7428{
7429 struct rtl8169_private *tp = netdev_priv(dev);
7430 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 u16 pci_status, pci_cmd;
7432
7433 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7434 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7435
Joe Perchesbf82c182010-02-09 11:49:50 +00007436 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7437 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438
7439 /*
7440 * The recovery sequence below admits a very elaborated explanation:
7441 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007442 * - I did not see what else could be done;
7443 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 *
7445 * Feel free to adjust to your needs.
7446 */
Francois Romieua27993f2006-12-18 00:04:19 +01007447 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007448 pci_cmd &= ~PCI_COMMAND_PARITY;
7449 else
7450 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7451
7452 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
7454 pci_write_config_word(pdev, PCI_STATUS,
7455 pci_status & (PCI_STATUS_DETECTED_PARITY |
7456 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7457 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7458
7459 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007460 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007461 void __iomem *ioaddr = tp->mmio_addr;
7462
Joe Perchesbf82c182010-02-09 11:49:50 +00007463 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 tp->cp_cmd &= ~PCIDAC;
7465 RTL_W16(CPlusCmd, tp->cp_cmd);
7466 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 }
7468
françois romieue6de30d2011-01-03 15:08:37 +00007469 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007470
Francois Romieu98ddf982012-01-31 10:47:34 +01007471 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472}
7473
Francois Romieuda78dbf2012-01-26 14:18:23 +01007474static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475{
7476 unsigned int dirty_tx, tx_left;
7477
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 dirty_tx = tp->dirty_tx;
7479 smp_rmb();
7480 tx_left = tp->cur_tx - dirty_tx;
7481
7482 while (tx_left > 0) {
7483 unsigned int entry = dirty_tx % NUM_TX_DESC;
7484 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 u32 status;
7486
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7488 if (status & DescOwn)
7489 break;
7490
Alexander Duycka0750132014-12-11 15:02:17 -08007491 /* This barrier is needed to keep us from reading
7492 * any other fields out of the Tx descriptor until
7493 * we know the status of DescOwn
7494 */
7495 dma_rmb();
7496
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007497 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7498 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007500 u64_stats_update_begin(&tp->tx_stats.syncp);
7501 tp->tx_stats.packets++;
7502 tp->tx_stats.bytes += tx_skb->skb->len;
7503 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007504 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 tx_skb->skb = NULL;
7506 }
7507 dirty_tx++;
7508 tx_left--;
7509 }
7510
7511 if (tp->dirty_tx != dirty_tx) {
7512 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007513 /* Sync with rtl8169_start_xmit:
7514 * - publish dirty_tx ring index (write barrier)
7515 * - refresh cur_tx ring index and queue status (read barrier)
7516 * May the current thread miss the stopped queue condition,
7517 * a racing xmit thread can only have a right view of the
7518 * ring status.
7519 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007520 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007522 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 netif_wake_queue(dev);
7524 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007525 /*
7526 * 8168 hack: TxPoll requests are lost when the Tx packets are
7527 * too close. Let's kick an extra TxPoll request when a burst
7528 * of start_xmit activity is detected (if it is not detected,
7529 * it is slow enough). -- FR
7530 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007531 if (tp->cur_tx != dirty_tx) {
7532 void __iomem *ioaddr = tp->mmio_addr;
7533
Francois Romieud78ae2d2007-08-26 20:08:19 +02007534 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 }
7537}
7538
Francois Romieu126fa4b2005-05-12 20:09:17 -04007539static inline int rtl8169_fragmented_frame(u32 status)
7540{
7541 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7542}
7543
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007544static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 u32 status = opts1 & RxProtoMask;
7547
7548 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007549 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 skb->ip_summed = CHECKSUM_UNNECESSARY;
7551 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007552 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553}
7554
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007555static struct sk_buff *rtl8169_try_rx_copy(void *data,
7556 struct rtl8169_private *tp,
7557 int pkt_size,
7558 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007560 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007561 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007563 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007564 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007565 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007566 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007567 if (skb)
7568 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007569 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7570
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007571 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572}
7573
Francois Romieuda78dbf2012-01-26 14:18:23 +01007574static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575{
7576 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007577 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
Timo Teräs9fba0812013-01-15 21:01:24 +00007581 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007583 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 u32 status;
7585
David S. Miller8decf862011-09-22 03:23:13 -04007586 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 if (status & DescOwn)
7588 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007589
7590 /* This barrier is needed to keep us from reading
7591 * any other fields out of the Rx descriptor until
7592 * we know the status of DescOwn
7593 */
7594 dma_rmb();
7595
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007596 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007597 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7598 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007599 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007601 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007603 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007604 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007605 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007606 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007607 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007608 if ((status & (RxRUNT | RxCRC)) &&
7609 !(status & (RxRWT | RxFOVF)) &&
7610 (dev->features & NETIF_F_RXALL))
7611 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007613 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007614 dma_addr_t addr;
7615 int pkt_size;
7616
7617process_pkt:
7618 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007619 if (likely(!(dev->features & NETIF_F_RXFCS)))
7620 pkt_size = (status & 0x00003fff) - 4;
7621 else
7622 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
Francois Romieu126fa4b2005-05-12 20:09:17 -04007624 /*
7625 * The driver does not support incoming fragmented
7626 * frames. They are seen as a symptom of over-mtu
7627 * sized frames.
7628 */
7629 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007630 dev->stats.rx_dropped++;
7631 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007632 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007633 }
7634
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007635 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7636 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007637 if (!skb) {
7638 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007639 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 }
7641
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007642 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 skb_put(skb, pkt_size);
7644 skb->protocol = eth_type_trans(skb, dev);
7645
Francois Romieu7a8fc772011-03-01 17:18:33 +01007646 rtl8169_rx_vlan_tag(desc, skb);
7647
françois romieu39174292015-11-11 23:35:18 +01007648 if (skb->pkt_type == PACKET_MULTICAST)
7649 dev->stats.multicast++;
7650
Francois Romieu56de4142011-03-15 17:29:31 +01007651 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652
Junchang Wang8027aa22012-03-04 23:30:32 +01007653 u64_stats_update_begin(&tp->rx_stats.syncp);
7654 tp->rx_stats.packets++;
7655 tp->rx_stats.bytes += pkt_size;
7656 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 }
françois romieuce11ff52013-01-24 13:30:06 +00007658release_descriptor:
7659 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007660 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 }
7662
7663 count = cur_rx - tp->cur_rx;
7664 tp->cur_rx = cur_rx;
7665
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 return count;
7667}
7668
Francois Romieu07d3f512007-02-21 22:40:46 +01007669static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670{
Francois Romieu07d3f512007-02-21 22:40:46 +01007671 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007674 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007676 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007677 if (status && status != 0xffff) {
7678 status &= RTL_EVENT_NAPI | tp->event_slow;
7679 if (status) {
7680 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007681
Francois Romieuda78dbf2012-01-26 14:18:23 +01007682 rtl_irq_disable(tp);
7683 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 return IRQ_RETVAL(handled);
7687}
7688
Francois Romieuda78dbf2012-01-26 14:18:23 +01007689/*
7690 * Workqueue context.
7691 */
7692static void rtl_slow_event_work(struct rtl8169_private *tp)
7693{
7694 struct net_device *dev = tp->dev;
7695 u16 status;
7696
7697 status = rtl_get_events(tp) & tp->event_slow;
7698 rtl_ack_events(tp, status);
7699
7700 if (unlikely(status & RxFIFOOver)) {
7701 switch (tp->mac_version) {
7702 /* Work around for rx fifo overflow */
7703 case RTL_GIGA_MAC_VER_11:
7704 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007705 /* XXX - Hack alert. See rtl_task(). */
7706 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007707 default:
7708 break;
7709 }
7710 }
7711
7712 if (unlikely(status & SYSErr))
7713 rtl8169_pcierr_interrupt(dev);
7714
7715 if (status & LinkChg)
Heiner Kallweitef4d5fc2018-01-08 21:39:07 +01007716 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007717
françois romieu7dbb4912012-06-09 10:53:16 +00007718 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007719}
7720
Francois Romieu4422bcd2012-01-26 11:23:32 +01007721static void rtl_task(struct work_struct *work)
7722{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007723 static const struct {
7724 int bitnr;
7725 void (*action)(struct rtl8169_private *);
7726 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007727 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007728 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7729 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7730 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7731 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007732 struct rtl8169_private *tp =
7733 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007734 struct net_device *dev = tp->dev;
7735 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007736
Francois Romieuda78dbf2012-01-26 14:18:23 +01007737 rtl_lock_work(tp);
7738
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007739 if (!netif_running(dev) ||
7740 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007741 goto out_unlock;
7742
7743 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7744 bool pending;
7745
Francois Romieuda78dbf2012-01-26 14:18:23 +01007746 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007747 if (pending)
7748 rtl_work[i].action(tp);
7749 }
7750
7751out_unlock:
7752 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007753}
7754
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007755static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007757 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7758 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007759 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7760 int work_done= 0;
7761 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762
Francois Romieuda78dbf2012-01-26 14:18:23 +01007763 status = rtl_get_events(tp);
7764 rtl_ack_events(tp, status & ~tp->event_slow);
7765
7766 if (status & RTL_EVENT_NAPI_RX)
7767 work_done = rtl_rx(dev, tp, (u32) budget);
7768
7769 if (status & RTL_EVENT_NAPI_TX)
7770 rtl_tx(dev, tp);
7771
7772 if (status & tp->event_slow) {
7773 enable_mask &= ~tp->event_slow;
7774
7775 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007778 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007779 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007780
Francois Romieuda78dbf2012-01-26 14:18:23 +01007781 rtl_irq_enable(tp, enable_mask);
7782 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 }
7784
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007785 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787
Francois Romieu523a6092008-09-10 22:28:56 +02007788static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7789{
7790 struct rtl8169_private *tp = netdev_priv(dev);
7791
7792 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7793 return;
7794
7795 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7796 RTL_W32(RxMissed, 0);
7797}
7798
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799static void rtl8169_down(struct net_device *dev)
7800{
7801 struct rtl8169_private *tp = netdev_priv(dev);
7802 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Francois Romieu4876cc12011-03-11 21:07:11 +01007804 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007806 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007807 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
Hayes Wang92fc43b2011-07-06 15:58:03 +08007809 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007810 /*
7811 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007812 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7813 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007814 */
Francois Romieu523a6092008-09-10 22:28:56 +02007815 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007818 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 rtl8169_tx_clear(tp);
7821
7822 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007823
7824 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825}
7826
7827static int rtl8169_close(struct net_device *dev)
7828{
7829 struct rtl8169_private *tp = netdev_priv(dev);
7830 struct pci_dev *pdev = tp->pci_dev;
7831
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007832 pm_runtime_get_sync(&pdev->dev);
7833
Francois Romieucecb5fd2011-04-01 10:21:07 +02007834 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007835 rtl8169_update_counters(dev);
7836
Francois Romieuda78dbf2012-01-26 14:18:23 +01007837 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007838 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007839
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007841 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842
Lekensteyn4ea72442013-07-22 09:53:30 +02007843 cancel_work_sync(&tp->wk.work);
7844
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007845 pci_free_irq(pdev, 0, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007847 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7848 tp->RxPhyAddr);
7849 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7850 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 tp->TxDescArray = NULL;
7852 tp->RxDescArray = NULL;
7853
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007854 pm_runtime_put_sync(&pdev->dev);
7855
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 return 0;
7857}
7858
Francois Romieudc1c00c2012-03-08 10:06:18 +01007859#ifdef CONFIG_NET_POLL_CONTROLLER
7860static void rtl8169_netpoll(struct net_device *dev)
7861{
7862 struct rtl8169_private *tp = netdev_priv(dev);
7863
7864 rtl8169_interrupt(tp->pci_dev->irq, dev);
7865}
7866#endif
7867
Francois Romieudf43ac72012-03-08 09:48:40 +01007868static int rtl_open(struct net_device *dev)
7869{
7870 struct rtl8169_private *tp = netdev_priv(dev);
7871 void __iomem *ioaddr = tp->mmio_addr;
7872 struct pci_dev *pdev = tp->pci_dev;
7873 int retval = -ENOMEM;
7874
7875 pm_runtime_get_sync(&pdev->dev);
7876
7877 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007878 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007879 * dma_alloc_coherent provides more.
7880 */
7881 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7882 &tp->TxPhyAddr, GFP_KERNEL);
7883 if (!tp->TxDescArray)
7884 goto err_pm_runtime_put;
7885
7886 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7887 &tp->RxPhyAddr, GFP_KERNEL);
7888 if (!tp->RxDescArray)
7889 goto err_free_tx_0;
7890
7891 retval = rtl8169_init_ring(dev);
7892 if (retval < 0)
7893 goto err_free_rx_1;
7894
7895 INIT_WORK(&tp->wk.work, rtl_task);
7896
7897 smp_mb();
7898
7899 rtl_request_firmware(tp);
7900
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007901 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, dev,
7902 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01007903 if (retval < 0)
7904 goto err_release_fw_2;
7905
7906 rtl_lock_work(tp);
7907
7908 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7909
7910 napi_enable(&tp->napi);
7911
7912 rtl8169_init_phy(dev, tp);
7913
7914 __rtl8169_set_features(dev, dev->features);
7915
7916 rtl_pll_power_up(tp);
7917
7918 rtl_hw_start(dev);
7919
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007920 if (!rtl8169_init_counter_offsets(dev))
7921 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7922
Francois Romieudf43ac72012-03-08 09:48:40 +01007923 netif_start_queue(dev);
7924
7925 rtl_unlock_work(tp);
7926
7927 tp->saved_wolopts = 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01007928 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01007929
7930 rtl8169_check_link_status(dev, tp, ioaddr);
7931out:
7932 return retval;
7933
7934err_release_fw_2:
7935 rtl_release_firmware(tp);
7936 rtl8169_rx_clear(tp);
7937err_free_rx_1:
7938 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7939 tp->RxPhyAddr);
7940 tp->RxDescArray = NULL;
7941err_free_tx_0:
7942 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7943 tp->TxPhyAddr);
7944 tp->TxDescArray = NULL;
7945err_pm_runtime_put:
7946 pm_runtime_put_noidle(&pdev->dev);
7947 goto out;
7948}
7949
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007950static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007951rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952{
7953 struct rtl8169_private *tp = netdev_priv(dev);
7954 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007955 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02007956 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01007957 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007959 pm_runtime_get_noresume(&pdev->dev);
7960
7961 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007962 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007963
Junchang Wang8027aa22012-03-04 23:30:32 +01007964 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007965 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007966 stats->rx_packets = tp->rx_stats.packets;
7967 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007968 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007969
Junchang Wang8027aa22012-03-04 23:30:32 +01007970 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007971 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007972 stats->tx_packets = tp->tx_stats.packets;
7973 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007974 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007975
7976 stats->rx_dropped = dev->stats.rx_dropped;
7977 stats->tx_dropped = dev->stats.tx_dropped;
7978 stats->rx_length_errors = dev->stats.rx_length_errors;
7979 stats->rx_errors = dev->stats.rx_errors;
7980 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7981 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7982 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02007983 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01007984
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007985 /*
7986 * Fetch additonal counter values missing in stats collected by driver
7987 * from tally counters.
7988 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08007989 if (pm_runtime_active(&pdev->dev))
7990 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007991
7992 /*
7993 * Subtract values fetched during initalization.
7994 * See rtl8169_init_counter_offsets for a description why we do that.
7995 */
Corinna Vinschen42020322015-09-10 10:47:35 +02007996 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007997 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02007998 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007999 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02008000 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008001 le16_to_cpu(tp->tc_offset.tx_aborted);
8002
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008003 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004}
8005
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008006static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01008007{
françois romieu065c27c2011-01-03 15:08:12 +00008008 struct rtl8169_private *tp = netdev_priv(dev);
8009
Francois Romieu5d06a992006-02-23 00:47:58 +01008010 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008011 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01008012
8013 netif_device_detach(dev);
8014 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008015
8016 rtl_lock_work(tp);
8017 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008018 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008019 rtl_unlock_work(tp);
8020
8021 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008022}
Francois Romieu5d06a992006-02-23 00:47:58 +01008023
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008024#ifdef CONFIG_PM
8025
8026static int rtl8169_suspend(struct device *device)
8027{
8028 struct pci_dev *pdev = to_pci_dev(device);
8029 struct net_device *dev = pci_get_drvdata(pdev);
8030
8031 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02008032
Francois Romieu5d06a992006-02-23 00:47:58 +01008033 return 0;
8034}
8035
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008036static void __rtl8169_resume(struct net_device *dev)
8037{
françois romieu065c27c2011-01-03 15:08:12 +00008038 struct rtl8169_private *tp = netdev_priv(dev);
8039
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008040 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00008041
8042 rtl_pll_power_up(tp);
8043
Artem Savkovcff4c162012-04-03 10:29:11 +00008044 rtl_lock_work(tp);
8045 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008046 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00008047 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008048
Francois Romieu98ddf982012-01-31 10:47:34 +01008049 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008050}
8051
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008052static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01008053{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008054 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01008055 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008056 struct rtl8169_private *tp = netdev_priv(dev);
8057
8058 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01008059
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008060 if (netif_running(dev))
8061 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01008062
Francois Romieu5d06a992006-02-23 00:47:58 +01008063 return 0;
8064}
8065
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008066static int rtl8169_runtime_suspend(struct device *device)
8067{
8068 struct pci_dev *pdev = to_pci_dev(device);
8069 struct net_device *dev = pci_get_drvdata(pdev);
8070 struct rtl8169_private *tp = netdev_priv(dev);
8071
Heiner Kallweita92a0842018-01-08 21:39:13 +01008072 if (!tp->TxDescArray) {
8073 rtl_pll_power_down(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008074 return 0;
Heiner Kallweita92a0842018-01-08 21:39:13 +01008075 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008076
Francois Romieuda78dbf2012-01-26 14:18:23 +01008077 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008078 tp->saved_wolopts = __rtl8169_get_wol(tp);
8079 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008080 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008081
8082 rtl8169_net_suspend(dev);
8083
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008084 /* Update counters before going runtime suspend */
8085 rtl8169_rx_missed(dev, tp->mmio_addr);
8086 rtl8169_update_counters(dev);
8087
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008088 return 0;
8089}
8090
8091static int rtl8169_runtime_resume(struct device *device)
8092{
8093 struct pci_dev *pdev = to_pci_dev(device);
8094 struct net_device *dev = pci_get_drvdata(pdev);
8095 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08008096 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008097
8098 if (!tp->TxDescArray)
8099 return 0;
8100
Francois Romieuda78dbf2012-01-26 14:18:23 +01008101 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008102 __rtl8169_set_wol(tp, tp->saved_wolopts);
8103 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01008104 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008105
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008106 rtl8169_init_phy(dev, tp);
8107
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008108 __rtl8169_resume(dev);
8109
8110 return 0;
8111}
8112
8113static int rtl8169_runtime_idle(struct device *device)
8114{
8115 struct pci_dev *pdev = to_pci_dev(device);
8116 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008117
Heiner Kallweita92a0842018-01-08 21:39:13 +01008118 if (!netif_running(dev) || !netif_carrier_ok(dev))
8119 pm_schedule_suspend(device, 10000);
8120
8121 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008122}
8123
Alexey Dobriyan47145212009-12-14 18:00:08 -08008124static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02008125 .suspend = rtl8169_suspend,
8126 .resume = rtl8169_resume,
8127 .freeze = rtl8169_suspend,
8128 .thaw = rtl8169_resume,
8129 .poweroff = rtl8169_suspend,
8130 .restore = rtl8169_resume,
8131 .runtime_suspend = rtl8169_runtime_suspend,
8132 .runtime_resume = rtl8169_runtime_resume,
8133 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008134};
8135
8136#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8137
8138#else /* !CONFIG_PM */
8139
8140#define RTL8169_PM_OPS NULL
8141
8142#endif /* !CONFIG_PM */
8143
David S. Miller1805b2f2011-10-24 18:18:09 -04008144static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8145{
8146 void __iomem *ioaddr = tp->mmio_addr;
8147
8148 /* WoL fails with 8168b when the receiver is disabled. */
8149 switch (tp->mac_version) {
8150 case RTL_GIGA_MAC_VER_11:
8151 case RTL_GIGA_MAC_VER_12:
8152 case RTL_GIGA_MAC_VER_17:
8153 pci_clear_master(tp->pci_dev);
8154
8155 RTL_W8(ChipCmd, CmdRxEnb);
8156 /* PCI commit */
8157 RTL_R8(ChipCmd);
8158 break;
8159 default:
8160 break;
8161 }
8162}
8163
Francois Romieu1765f952008-09-13 17:21:40 +02008164static void rtl_shutdown(struct pci_dev *pdev)
8165{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008166 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00008167 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02008168
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008169 rtl8169_net_suspend(dev);
8170
Francois Romieucecb5fd2011-04-01 10:21:07 +02008171 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08008172 rtl_rar_set(tp, dev->perm_addr);
8173
Hayes Wang92fc43b2011-07-06 15:58:03 +08008174 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00008175
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008176 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04008177 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8178 rtl_wol_suspend_quirk(tp);
8179 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00008180 }
8181
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008182 pci_wake_from_d3(pdev, true);
8183 pci_set_power_state(pdev, PCI_D3hot);
8184 }
8185}
Francois Romieu5d06a992006-02-23 00:47:58 +01008186
Bill Pembertonbaf63292012-12-03 09:23:28 -05008187static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008188{
8189 struct net_device *dev = pci_get_drvdata(pdev);
8190 struct rtl8169_private *tp = netdev_priv(dev);
8191
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008192 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01008193 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01008194
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008195 netif_napi_del(&tp->napi);
8196
Francois Romieue27566e2012-03-08 09:54:01 +01008197 unregister_netdev(dev);
8198
8199 rtl_release_firmware(tp);
8200
8201 if (pci_dev_run_wake(pdev))
8202 pm_runtime_get_noresume(&pdev->dev);
8203
8204 /* restore original MAC address */
8205 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008206}
8207
Francois Romieufa9c3852012-03-08 10:01:50 +01008208static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008209 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008210 .ndo_stop = rtl8169_close,
8211 .ndo_get_stats64 = rtl8169_get_stats64,
8212 .ndo_start_xmit = rtl8169_start_xmit,
8213 .ndo_tx_timeout = rtl8169_tx_timeout,
8214 .ndo_validate_addr = eth_validate_addr,
8215 .ndo_change_mtu = rtl8169_change_mtu,
8216 .ndo_fix_features = rtl8169_fix_features,
8217 .ndo_set_features = rtl8169_set_features,
8218 .ndo_set_mac_address = rtl_set_mac_address,
8219 .ndo_do_ioctl = rtl8169_ioctl,
8220 .ndo_set_rx_mode = rtl_set_rx_mode,
8221#ifdef CONFIG_NET_POLL_CONTROLLER
8222 .ndo_poll_controller = rtl8169_netpoll,
8223#endif
8224
8225};
8226
Francois Romieu31fa8b12012-03-08 10:09:40 +01008227static const struct rtl_cfg_info {
8228 void (*hw_start)(struct net_device *);
8229 unsigned int region;
8230 unsigned int align;
8231 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008232 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03008233 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008234 u8 default_ver;
8235} rtl_cfg_infos [] = {
8236 [RTL_CFG_0] = {
8237 .hw_start = rtl_hw_start_8169,
8238 .region = 1,
8239 .align = 0,
8240 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008241 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008242 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008243 .default_ver = RTL_GIGA_MAC_VER_01,
8244 },
8245 [RTL_CFG_1] = {
8246 .hw_start = rtl_hw_start_8168,
8247 .region = 2,
8248 .align = 8,
8249 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01008250 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03008251 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008252 .default_ver = RTL_GIGA_MAC_VER_11,
8253 },
8254 [RTL_CFG_2] = {
8255 .hw_start = rtl_hw_start_8101,
8256 .region = 2,
8257 .align = 8,
8258 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8259 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03008260 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008261 .default_ver = RTL_GIGA_MAC_VER_13,
8262 }
8263};
8264
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008265static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01008266{
8267 void __iomem *ioaddr = tp->mmio_addr;
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008268 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008269
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008270 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
8271 RTL_W8(Cfg9346, Cfg9346_Unlock);
8272 RTL_W8(Config2, RTL_R8(Config2) & ~MSIEnable);
8273 RTL_W8(Cfg9346, Cfg9346_Lock);
8274 flags = PCI_IRQ_LEGACY;
8275 } else {
8276 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008277 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008278
8279 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01008280}
8281
Hayes Wangc5583862012-07-02 17:23:22 +08008282DECLARE_RTL_COND(rtl_link_list_ready_cond)
8283{
8284 void __iomem *ioaddr = tp->mmio_addr;
8285
8286 return RTL_R8(MCU) & LINK_LIST_RDY;
8287}
8288
8289DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8290{
8291 void __iomem *ioaddr = tp->mmio_addr;
8292
8293 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8294}
8295
Bill Pembertonbaf63292012-12-03 09:23:28 -05008296static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008297{
8298 void __iomem *ioaddr = tp->mmio_addr;
8299 u32 data;
8300
8301 tp->ocp_base = OCP_STD_PHY_BASE;
8302
8303 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8304
8305 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8306 return;
8307
8308 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8309 return;
8310
8311 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8312 msleep(1);
8313 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8314
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008315 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008316 data &= ~(1 << 14);
8317 r8168_mac_ocp_write(tp, 0xe8de, data);
8318
8319 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8320 return;
8321
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008322 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008323 data |= (1 << 15);
8324 r8168_mac_ocp_write(tp, 0xe8de, data);
8325
8326 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8327 return;
8328}
8329
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008330static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8331{
8332 rtl8168ep_stop_cmac(tp);
8333 rtl_hw_init_8168g(tp);
8334}
8335
Bill Pembertonbaf63292012-12-03 09:23:28 -05008336static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008337{
8338 switch (tp->mac_version) {
8339 case RTL_GIGA_MAC_VER_40:
8340 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008341 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008342 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008343 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008344 case RTL_GIGA_MAC_VER_45:
8345 case RTL_GIGA_MAC_VER_46:
8346 case RTL_GIGA_MAC_VER_47:
8347 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008348 rtl_hw_init_8168g(tp);
8349 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008350 case RTL_GIGA_MAC_VER_49:
8351 case RTL_GIGA_MAC_VER_50:
8352 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008353 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008354 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008355 default:
8356 break;
8357 }
8358}
8359
hayeswang929a0312014-09-16 11:40:47 +08008360static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008361{
8362 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8363 const unsigned int region = cfg->region;
8364 struct rtl8169_private *tp;
8365 struct mii_if_info *mii;
8366 struct net_device *dev;
8367 void __iomem *ioaddr;
8368 int chipset, i;
8369 int rc;
8370
8371 if (netif_msg_drv(&debug)) {
8372 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8373 MODULENAME, RTL8169_VERSION);
8374 }
8375
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008376 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8377 if (!dev)
8378 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008379
8380 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008381 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008382 tp = netdev_priv(dev);
8383 tp->dev = dev;
8384 tp->pci_dev = pdev;
8385 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8386
8387 mii = &tp->mii;
8388 mii->dev = dev;
8389 mii->mdio_read = rtl_mdio_read;
8390 mii->mdio_write = rtl_mdio_write;
8391 mii->phy_id_mask = 0x1f;
8392 mii->reg_num_mask = 0x1f;
Heiner Kallweit14967f92018-02-28 07:55:20 +01008393 mii->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008394
8395 /* disable ASPM completely as that cause random device stop working
8396 * problems as well as full system hangs for some PCIe devices users */
8397 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8398 PCIE_LINK_STATE_CLKPM);
8399
8400 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008401 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008402 if (rc < 0) {
8403 netif_err(tp, probe, dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008404 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008405 }
8406
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008407 if (pcim_set_mwi(pdev) < 0)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008408 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8409
8410 /* make sure PCI base addr 1 is MMIO */
8411 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8412 netif_err(tp, probe, dev,
8413 "region #%d not an MMIO resource, aborting\n",
8414 region);
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008415 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008416 }
8417
8418 /* check for weird/broken PCI region reporting */
8419 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8420 netif_err(tp, probe, dev,
8421 "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008422 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008423 }
8424
8425 rc = pci_request_regions(pdev, MODULENAME);
8426 if (rc < 0) {
8427 netif_err(tp, probe, dev, "could not request regions\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008428 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008429 }
8430
Francois Romieu3b6cf252012-03-08 09:59:04 +01008431 /* ioremap MMIO region */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008432 ioaddr = devm_ioremap(&pdev->dev, pci_resource_start(pdev, region),
8433 R8169_REGS_SIZE);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008434 if (!ioaddr) {
8435 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008436 return -EIO;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008437 }
8438 tp->mmio_addr = ioaddr;
8439
8440 if (!pci_is_pcie(pdev))
8441 netif_info(tp, probe, dev, "not PCI Express\n");
8442
8443 /* Identify chip attached to board */
8444 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8445
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008446 tp->cp_cmd = 0;
8447
8448 if ((sizeof(dma_addr_t) > 4) &&
8449 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8450 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008451 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8452 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008453
8454 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8455 if (!pci_is_pcie(pdev))
8456 tp->cp_cmd |= PCIDAC;
8457 dev->features |= NETIF_F_HIGHDMA;
8458 } else {
8459 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8460 if (rc < 0) {
8461 netif_err(tp, probe, dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008462 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008463 }
8464 }
8465
Francois Romieu3b6cf252012-03-08 09:59:04 +01008466 rtl_init_rxcfg(tp);
8467
8468 rtl_irq_disable(tp);
8469
Hayes Wangc5583862012-07-02 17:23:22 +08008470 rtl_hw_initialize(tp);
8471
Francois Romieu3b6cf252012-03-08 09:59:04 +01008472 rtl_hw_reset(tp);
8473
8474 rtl_ack_events(tp, 0xffff);
8475
8476 pci_set_master(pdev);
8477
Francois Romieu3b6cf252012-03-08 09:59:04 +01008478 rtl_init_mdio_ops(tp);
8479 rtl_init_pll_power_ops(tp);
8480 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008481 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008482
8483 rtl8169_print_mac_version(tp);
8484
8485 chipset = tp->mac_version;
8486 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8487
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01008488 rc = rtl_alloc_irq(tp);
8489 if (rc < 0) {
8490 netif_err(tp, probe, dev, "Can't allocate interrupt\n");
8491 return rc;
8492 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008493
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01008494 /* override BIOS settings, use userspace tools to enable WOL */
8495 __rtl8169_set_wol(tp, 0);
8496
Francois Romieu3b6cf252012-03-08 09:59:04 +01008497 if (rtl_tbi_enabled(tp)) {
8498 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008499 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008500 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8501 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8502 tp->link_ok = rtl8169_tbi_link_ok;
8503 tp->do_ioctl = rtl_tbi_ioctl;
8504 } else {
8505 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008506 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008507 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8508 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8509 tp->link_ok = rtl8169_xmii_link_ok;
8510 tp->do_ioctl = rtl_xmii_ioctl;
8511 }
8512
8513 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008514 u64_stats_init(&tp->rx_stats.syncp);
8515 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008516
8517 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008518 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8519 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8520 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8521 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8522 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8523 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8524 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8525 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8526 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8527 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008528 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8529 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008530 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8531 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8532 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8533 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008534 u16 mac_addr[3];
8535
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008536 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8537 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008538
8539 if (is_valid_ether_addr((u8 *)mac_addr))
8540 rtl_rar_set(tp, (u8 *)mac_addr);
8541 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008542 for (i = 0; i < ETH_ALEN; i++)
8543 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008544
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008545 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008546 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008547
8548 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8549
8550 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8551 * properly for all devices */
8552 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008553 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008554
8555 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008556 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8557 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008558 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8559 NETIF_F_HIGHDMA;
8560
hayeswang929a0312014-09-16 11:40:47 +08008561 tp->cp_cmd |= RxChkSum | RxVlan;
8562
8563 /*
8564 * Pretend we are using VLANs; This bypasses a nasty bug where
8565 * Interrupts stop flowing on high load on 8110SCd controllers.
8566 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008567 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008568 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008569 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008570
hayeswang5888d3f2014-07-11 16:25:56 +08008571 if (tp->txd_version == RTL_TD_0)
8572 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008573 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008574 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008575 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8576 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008577 WARN_ON_ONCE(1);
8578
Francois Romieu3b6cf252012-03-08 09:59:04 +01008579 dev->hw_features |= NETIF_F_RXALL;
8580 dev->hw_features |= NETIF_F_RXFCS;
8581
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008582 /* MTU range: 60 - hw-specific max */
8583 dev->min_mtu = ETH_ZLEN;
8584 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8585
Francois Romieu3b6cf252012-03-08 09:59:04 +01008586 tp->hw_start = cfg->hw_start;
8587 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008588 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008589
8590 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8591 ~(RxBOVF | RxFOVF) : ~0;
8592
Kees Cook9de36cc2017-10-25 03:53:12 -07008593 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008594
8595 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8596
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008597 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8598 &tp->counters_phys_addr,
8599 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008600 if (!tp->counters)
8601 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02008602
Francois Romieu3b6cf252012-03-08 09:59:04 +01008603 rc = register_netdev(dev);
8604 if (rc < 0)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01008605 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008606
8607 pci_set_drvdata(pdev, dev);
8608
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008609 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8610 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8611 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008612 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8613 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8614 "tx checksumming: %s]\n",
8615 rtl_chip_infos[chipset].jumbo_max,
8616 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8617 }
8618
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01008619 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01008620 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008621
Francois Romieu3b6cf252012-03-08 09:59:04 +01008622 netif_carrier_off(dev);
8623
Heiner Kallweita92a0842018-01-08 21:39:13 +01008624 if (pci_dev_run_wake(pdev))
8625 pm_runtime_put_sync(&pdev->dev);
8626
Heiner Kallweit4c45d242017-12-12 07:41:02 +01008627 return 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008628}
8629
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630static struct pci_driver rtl8169_pci_driver = {
8631 .name = MODULENAME,
8632 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008633 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008634 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008635 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008636 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637};
8638
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008639module_pci_driver(rtl8169_pci_driver);