blob: 2cb3622c4acc93ad669f1f09b92e816e4d86a095 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080048#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080049#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080050#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080051#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000052#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000053#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000054#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080055#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef RTL8169_DEBUG
61#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020062 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070064 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020065 }
Joe Perches06fa7352007-10-18 21:15:00 +020066#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020073#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070074 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020075
Julien Ducourthial477206a2012-05-09 00:00:06 +020076#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050085static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Francois Romieu9c14cea2008-07-05 00:21:15 +020087#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000088#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000094#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000107#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800143 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800146 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800147 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800148 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000151 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000152 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800153 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
Francois Romieud58d46b2011-05-03 16:38:29 +0200169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800183static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700185 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 u16 jumbo_max;
188 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200218 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200220 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200228 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200230 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200236 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200238 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200246 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200248 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200253 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200256 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200261 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800264 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800270 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800282 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318#undef _R
319
Francois Romieubcf0bf92006-07-26 23:14:13 +0200320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
Benoit Taine9baa3c32014-08-08 15:56:03 +0200326static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100341 { 0x0001, 0x8168,
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 {0,},
344};
345
346MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000348static int rx_buf_sz = 16383;
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200349static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200350static struct {
351 u32 msg_enable;
352} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francois Romieu07d3f512007-02-21 22:40:46 +0100354enum rtl_registers {
355 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100356 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
364 FLASH = 0x30,
365 ERSR = 0x36,
366 ChipCmd = 0x37,
367 TxPoll = 0x38,
368 IntrMask = 0x3c,
369 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700370
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371 TxConfig = 0x40,
372#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
374
375 RxConfig = 0x44,
376#define RX128_INT_EN (1 << 15) /* 8111c and later */
377#define RX_MULTI_EN (1 << 14) /* 8111c only */
378#define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000381#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700385
Francois Romieu07d3f512007-02-21 22:40:46 +0100386 RxMissed = 0x4c,
387 Cfg9346 = 0x50,
388 Config0 = 0x51,
389 Config1 = 0x52,
390 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200391#define PME_SIGNAL (1 << 5) /* 8168c and later */
392
Francois Romieu07d3f512007-02-21 22:40:46 +0100393 Config3 = 0x54,
394 Config4 = 0x55,
395 Config5 = 0x56,
396 MultiIntr = 0x5c,
397 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 PHYstatus = 0x6c,
399 RxMaxSize = 0xda,
400 CPlusCmd = 0xe0,
401 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300402
403#define RTL_COALESCE_MASK 0x0f
404#define RTL_COALESCE_SHIFT 4
405#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
406#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
407
Francois Romieu07d3f512007-02-21 22:40:46 +0100408 RxDescAddrLow = 0xe4,
409 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000410 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
411
412#define NoEarlyTx 0x3f /* Max value : no early transmit. */
413
414 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
415
416#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800417#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000418
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncEvent = 0xf0,
420 FuncEventMask = 0xf4,
421 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800422 IBCR0 = 0xf8,
423 IBCR2 = 0xf9,
424 IBIMR0 = 0xfa,
425 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100426 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429enum rtl8110_registers {
430 TBICSR = 0x64,
431 TBI_ANAR = 0x68,
432 TBI_LPAR = 0x6a,
433};
434
435enum rtl8168_8101_registers {
436 CSIDR = 0x64,
437 CSIAR = 0x68,
438#define CSIAR_FLAG 0x80000000
439#define CSIAR_WRITE_CMD 0x80000000
440#define CSIAR_BYTE_ENABLE 0x0f
441#define CSIAR_BYTE_ENABLE_SHIFT 12
442#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800443#define CSIAR_FUNC_CARD 0x00000000
444#define CSIAR_FUNC_SDIO 0x00010000
445#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800446#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000447 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 EPHYAR = 0x80,
449#define EPHYAR_FLAG 0x80000000
450#define EPHYAR_WRITE_CMD 0x80000000
451#define EPHYAR_REG_MASK 0x1f
452#define EPHYAR_REG_SHIFT 16
453#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800454 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800456#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200457 DBG_REG = 0xd1,
458#define FIX_NAK_1 (1 << 4)
459#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800460 TWSI = 0xd2,
461 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800462#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800463#define TX_EMPTY (1 << 5)
464#define RX_EMPTY (1 << 4)
465#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800466#define EN_NDP (1 << 3)
467#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800468#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000469 EFUSEAR = 0xdc,
470#define EFUSEAR_FLAG 0x80000000
471#define EFUSEAR_WRITE_CMD 0x80000000
472#define EFUSEAR_READ_CMD 0x00000000
473#define EFUSEAR_REG_MASK 0x03ff
474#define EFUSEAR_REG_SHIFT 8
475#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800476 MISC_1 = 0xf2,
477#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478};
479
françois romieuc0e45c12011-01-03 15:08:04 +0000480enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800481 LED_FREQ = 0x1a,
482 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000483 ERIDR = 0x70,
484 ERIAR = 0x74,
485#define ERIAR_FLAG 0x80000000
486#define ERIAR_WRITE_CMD 0x80000000
487#define ERIAR_READ_CMD 0x00000000
488#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000489#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800490#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
491#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
492#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800493#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800494#define ERIAR_MASK_SHIFT 12
495#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
496#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800497#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800498#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800499#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000500 EPHY_RXER_NUM = 0x7c,
501 OCPDR = 0xb0, /* OCP GPHY access */
502#define OCPDR_WRITE_CMD 0x80000000
503#define OCPDR_READ_CMD 0x00000000
504#define OCPDR_REG_MASK 0x7f
505#define OCPDR_GPHY_REG_SHIFT 16
506#define OCPDR_DATA_MASK 0xffff
507 OCPAR = 0xb4,
508#define OCPAR_FLAG 0x80000000
509#define OCPAR_GPHY_WRITE_CMD 0x8000f060
510#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800511 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000512 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
513 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200514#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800515#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800516#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800517#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800518#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000519};
520
Francois Romieu07d3f512007-02-21 22:40:46 +0100521enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100523 SYSErr = 0x8000,
524 PCSTimeout = 0x4000,
525 SWInt = 0x0100,
526 TxDescUnavail = 0x0080,
527 RxFIFOOver = 0x0040,
528 LinkChg = 0x0020,
529 RxOverflow = 0x0010,
530 TxErr = 0x0008,
531 TxOK = 0x0004,
532 RxErr = 0x0002,
533 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400536 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200537 RxFOVF = (1 << 23),
538 RxRWT = (1 << 22),
539 RxRES = (1 << 21),
540 RxRUNT = (1 << 20),
541 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800544 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100545 CmdReset = 0x10,
546 CmdRxEnb = 0x08,
547 CmdTxEnb = 0x04,
548 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Francois Romieu275391a2007-02-23 23:50:28 +0100550 /* TXPoll register p.5 */
551 HPQ = 0x80, /* Poll cmd on the high prio queue */
552 NPQ = 0x40, /* Poll cmd on the low prio queue */
553 FSWInt = 0x01, /* Forced software interrupt */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100556 Cfg9346_Lock = 0x00,
557 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100560 AcceptErr = 0x20,
561 AcceptRunt = 0x10,
562 AcceptBroadcast = 0x08,
563 AcceptMulticast = 0x04,
564 AcceptMyPhys = 0x02,
565 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200566#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* TxConfigBits */
569 TxInterFrameGapShift = 24,
570 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
571
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200573 LEDS1 = (1 << 7),
574 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200575 Speed_down = (1 << 4),
576 MEMMAP = (1 << 3),
577 IOMAP = (1 << 2),
578 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100579 PMEnable = (1 << 0), /* Power Management Enable */
580
Francois Romieu6dccd162007-02-13 23:38:05 +0100581 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000582 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000583 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100584 PCI_Clock_66MHz = 0x01,
585 PCI_Clock_33MHz = 0x00,
586
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100587 /* Config3 register p.25 */
588 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
589 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200590 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800591 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200592 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100593
Francois Romieud58d46b2011-05-03 16:38:29 +0200594 /* Config4 register */
595 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
596
Francois Romieu5d06a992006-02-23 00:47:58 +0100597 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100598 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
599 MWF = (1 << 5), /* Accept Multicast wakeup frame */
600 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200601 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100602 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100603 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000604 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* TBICSR p.28 */
607 TBIReset = 0x80000000,
608 TBILoopback = 0x40000000,
609 TBINwEnable = 0x20000000,
610 TBINwRestart = 0x10000000,
611 TBILinkOk = 0x02000000,
612 TBINwComplete = 0x01000000,
613
614 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200615 EnableBist = (1 << 15), // 8168 8101
616 Mac_dbgo_oe = (1 << 14), // 8168 8101
617 Normal_mode = (1 << 13), // unused
618 Force_half_dup = (1 << 12), // 8168 8101
619 Force_rxflow_en = (1 << 11), // 8168 8101
620 Force_txflow_en = (1 << 10), // 8168 8101
621 Cxpl_dbg_sel = (1 << 9), // 8168 8101
622 ASF = (1 << 8), // 8168 8101
623 PktCntrDisable = (1 << 7), // 8168 8101
624 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 RxVlan = (1 << 6),
626 RxChkSum = (1 << 5),
627 PCIDAC = (1 << 4),
628 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100629 INTT_0 = 0x0000, // 8168
630 INTT_1 = 0x0001, // 8168
631 INTT_2 = 0x0002, // 8168
632 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100635 TBI_Enable = 0x80,
636 TxFlowCtrl = 0x40,
637 RxFlowCtrl = 0x20,
638 _1000bpsF = 0x10,
639 _100bps = 0x08,
640 _10bps = 0x04,
641 LinkStatus = 0x02,
642 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100645 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200646
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200647 /* ResetCounterCommand */
648 CounterReset = 0x1,
649
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200650 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100651 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800652
653 /* magic enable v2 */
654 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655};
656
Francois Romieu2b7b4312011-04-18 22:53:24 -0700657enum rtl_desc_bit {
658 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
660 RingEnd = (1 << 30), /* End of descriptor ring */
661 FirstFrag = (1 << 29), /* First segment of a packet */
662 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700663};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Francois Romieu2b7b4312011-04-18 22:53:24 -0700665/* Generic case. */
666enum rtl_tx_desc_bit {
667 /* First doubleword. */
668 TD_LSO = (1 << 27), /* Large Send Offload */
669#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Francois Romieu2b7b4312011-04-18 22:53:24 -0700671 /* Second doubleword. */
672 TxVlanTag = (1 << 17), /* Add VLAN tag */
673};
674
675/* 8169, 8168b and 810x except 8102e. */
676enum rtl_tx_desc_bit_0 {
677 /* First doubleword. */
678#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
679 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
680 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
681 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
682};
683
684/* 8102e, 8168c and beyond. */
685enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800686 /* First doubleword. */
687 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800688 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800689#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800690#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800691
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800693#define TCPHO_SHIFT 18
694#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700695#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800696 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
697 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700698 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
699 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
700};
701
Francois Romieu2b7b4312011-04-18 22:53:24 -0700702enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Rx private */
704 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500705 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707#define RxProtoUDP (PID1)
708#define RxProtoTCP (PID0)
709#define RxProtoIP (PID1 | PID0)
710#define RxProtoMask RxProtoIP
711
712 IPFail = (1 << 16), /* IP checksum failed */
713 UDPFail = (1 << 15), /* UDP/IP checksum failed */
714 TCPFail = (1 << 14), /* TCP/IP checksum failed */
715 RxVlanTag = (1 << 16), /* VLAN tag available */
716};
717
718#define RsvdMask 0x3fffc000
719
720struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200721 __le32 opts1;
722 __le32 opts2;
723 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724};
725
726struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200727 __le32 opts1;
728 __le32 opts2;
729 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
732struct ring_info {
733 struct sk_buff *skb;
734 u32 len;
735 u8 __pad[sizeof(void *) - sizeof(u32)];
736};
737
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200738enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200739 RTL_FEATURE_WOL = (1 << 0),
740 RTL_FEATURE_MSI = (1 << 1),
741 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200742};
743
Ivan Vecera355423d2009-02-06 21:49:57 -0800744struct rtl8169_counters {
745 __le64 tx_packets;
746 __le64 rx_packets;
747 __le64 tx_errors;
748 __le32 rx_errors;
749 __le16 rx_missed;
750 __le16 align_errors;
751 __le32 tx_one_collision;
752 __le32 tx_multi_collision;
753 __le64 rx_unicast;
754 __le64 rx_broadcast;
755 __le32 rx_multicast;
756 __le16 tx_aborted;
757 __le16 tx_underun;
758};
759
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200760struct rtl8169_tc_offsets {
761 bool inited;
762 __le64 tx_errors;
763 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200764 __le16 tx_aborted;
765};
766
Francois Romieuda78dbf2012-01-26 14:18:23 +0100767enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100768 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100769 RTL_FLAG_TASK_SLOW_PENDING,
770 RTL_FLAG_TASK_RESET_PENDING,
771 RTL_FLAG_TASK_PHY_PENDING,
772 RTL_FLAG_MAX
773};
774
Junchang Wang8027aa22012-03-04 23:30:32 +0100775struct rtl8169_stats {
776 u64 packets;
777 u64 bytes;
778 struct u64_stats_sync syncp;
779};
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781struct rtl8169_private {
782 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200783 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000784 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700785 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200786 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700787 u16 txd_version;
788 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
790 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100792 struct rtl8169_stats rx_stats;
793 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
795 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
796 dma_addr_t TxPhyAddr;
797 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000798 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct timer_list timer;
801 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100802
803 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300804 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000805
806 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200807 void (*write)(struct rtl8169_private *, int, int);
808 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000809 } mdio_ops;
810
françois romieu065c27c2011-01-03 15:08:12 +0000811 struct pll_power_ops {
812 void (*down)(struct rtl8169_private *);
813 void (*up)(struct rtl8169_private *);
814 } pll_power_ops;
815
Francois Romieud58d46b2011-05-03 16:38:29 +0200816 struct jumbo_ops {
817 void (*enable)(struct rtl8169_private *);
818 void (*disable)(struct rtl8169_private *);
819 } jumbo_ops;
820
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800821 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200822 void (*write)(struct rtl8169_private *, int, int);
823 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800824 } csi_ops;
825
Oliver Neukum54405cd2011-01-06 21:55:13 +0100826 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +0100827 int (*get_link_ksettings)(struct net_device *,
828 struct ethtool_link_ksettings *);
françois romieu4da19632011-01-03 15:07:55 +0000829 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100830 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000831 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800833 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800834 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100835
836 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100837 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
838 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100839 struct work_struct work;
840 } wk;
841
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200842 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200843
844 struct mii_if_info mii;
Corinna Vinschen42020322015-09-10 10:47:35 +0200845 dma_addr_t counters_phys_addr;
846 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200847 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000848 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400849 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000850
Francois Romieub6ffd972011-06-17 17:00:05 +0200851 struct rtl_fw {
852 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200853
854#define RTL_VER_SIZE 32
855
856 char version[RTL_VER_SIZE];
857
858 struct rtl_fw_phy_action {
859 __le32 *code;
860 size_t size;
861 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200862 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300863#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800864
865 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
867
Ralf Baechle979b6c12005-06-13 14:30:40 -0700868MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700871MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200872module_param_named(debug, debug.msg_enable, int, 0);
873MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874MODULE_LICENSE("GPL");
875MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000876MODULE_FIRMWARE(FIRMWARE_8168D_1);
877MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000878MODULE_FIRMWARE(FIRMWARE_8168E_1);
879MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400880MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800881MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800882MODULE_FIRMWARE(FIRMWARE_8168F_1);
883MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800884MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800885MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800886MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800887MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000888MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000889MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000890MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800891MODULE_FIRMWARE(FIRMWARE_8168H_1);
892MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200893MODULE_FIRMWARE(FIRMWARE_8107E_1);
894MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Francois Romieuda78dbf2012-01-26 14:18:23 +0100896static void rtl_lock_work(struct rtl8169_private *tp)
897{
898 mutex_lock(&tp->wk.mutex);
899}
900
901static void rtl_unlock_work(struct rtl8169_private *tp)
902{
903 mutex_unlock(&tp->wk.mutex);
904}
905
Francois Romieud58d46b2011-05-03 16:38:29 +0200906static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
907{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800908 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
909 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200910}
911
Francois Romieuffc46952012-07-06 14:19:23 +0200912struct rtl_cond {
913 bool (*check)(struct rtl8169_private *);
914 const char *msg;
915};
916
917static void rtl_udelay(unsigned int d)
918{
919 udelay(d);
920}
921
922static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
923 void (*delay)(unsigned int), unsigned int d, int n,
924 bool high)
925{
926 int i;
927
928 for (i = 0; i < n; i++) {
929 delay(d);
930 if (c->check(tp) == high)
931 return true;
932 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200933 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
934 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200935 return false;
936}
937
938static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
939 const struct rtl_cond *c,
940 unsigned int d, int n)
941{
942 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
943}
944
945static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
946 const struct rtl_cond *c,
947 unsigned int d, int n)
948{
949 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
950}
951
952static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
953 const struct rtl_cond *c,
954 unsigned int d, int n)
955{
956 return rtl_loop_wait(tp, c, msleep, d, n, true);
957}
958
959static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
960 const struct rtl_cond *c,
961 unsigned int d, int n)
962{
963 return rtl_loop_wait(tp, c, msleep, d, n, false);
964}
965
966#define DECLARE_RTL_COND(name) \
967static bool name ## _check(struct rtl8169_private *); \
968 \
969static const struct rtl_cond name = { \
970 .check = name ## _check, \
971 .msg = #name \
972}; \
973 \
974static bool name ## _check(struct rtl8169_private *tp)
975
Hayes Wangc5583862012-07-02 17:23:22 +0800976static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
977{
978 if (reg & 0xffff0001) {
979 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
980 return true;
981 }
982 return false;
983}
984
985DECLARE_RTL_COND(rtl_ocp_gphy_cond)
986{
987 void __iomem *ioaddr = tp->mmio_addr;
988
989 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
990}
991
992static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
993{
994 void __iomem *ioaddr = tp->mmio_addr;
995
996 if (rtl_ocp_reg_failure(tp, reg))
997 return;
998
999 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1000
1001 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1002}
1003
1004static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1005{
1006 void __iomem *ioaddr = tp->mmio_addr;
1007
1008 if (rtl_ocp_reg_failure(tp, reg))
1009 return 0;
1010
1011 RTL_W32(GPHY_OCP, reg << 15);
1012
1013 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1014 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1015}
1016
Hayes Wangc5583862012-07-02 17:23:22 +08001017static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1018{
1019 void __iomem *ioaddr = tp->mmio_addr;
1020
1021 if (rtl_ocp_reg_failure(tp, reg))
1022 return;
1023
1024 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001025}
1026
1027static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1028{
1029 void __iomem *ioaddr = tp->mmio_addr;
1030
1031 if (rtl_ocp_reg_failure(tp, reg))
1032 return 0;
1033
1034 RTL_W32(OCPDR, reg << 15);
1035
Hayes Wang3a83ad12012-07-11 20:31:56 +08001036 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001037}
1038
1039#define OCP_STD_PHY_BASE 0xa400
1040
1041static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1042{
1043 if (reg == 0x1f) {
1044 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1045 return;
1046 }
1047
1048 if (tp->ocp_base != OCP_STD_PHY_BASE)
1049 reg -= 0x10;
1050
1051 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1052}
1053
1054static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1055{
1056 if (tp->ocp_base != OCP_STD_PHY_BASE)
1057 reg -= 0x10;
1058
1059 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1060}
1061
hayeswangeee37862013-04-01 22:23:38 +00001062static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1063{
1064 if (reg == 0x1f) {
1065 tp->ocp_base = value << 4;
1066 return;
1067 }
1068
1069 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1070}
1071
1072static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1073{
1074 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1075}
1076
Francois Romieuffc46952012-07-06 14:19:23 +02001077DECLARE_RTL_COND(rtl_phyar_cond)
1078{
1079 void __iomem *ioaddr = tp->mmio_addr;
1080
1081 return RTL_R32(PHYAR) & 0x80000000;
1082}
1083
Francois Romieu24192212012-07-06 20:19:42 +02001084static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Francois Romieu24192212012-07-06 20:19:42 +02001086 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Francois Romieu24192212012-07-06 20:19:42 +02001088 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Francois Romieuffc46952012-07-06 14:19:23 +02001090 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001091 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001092 * According to hardware specs a 20us delay is required after write
1093 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001094 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001095 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Francois Romieu24192212012-07-06 20:19:42 +02001098static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Francois Romieu24192212012-07-06 20:19:42 +02001100 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001101 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Francois Romieu24192212012-07-06 20:19:42 +02001103 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Francois Romieuffc46952012-07-06 14:19:23 +02001105 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1106 RTL_R32(PHYAR) & 0xffff : ~0;
1107
Timo Teräs81a95f02010-06-09 17:31:48 -07001108 /*
1109 * According to hardware specs a 20us delay is required after read
1110 * complete indication, but before sending next command.
1111 */
1112 udelay(20);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return value;
1115}
1116
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001117DECLARE_RTL_COND(rtl_ocpar_cond)
1118{
1119 void __iomem *ioaddr = tp->mmio_addr;
1120
1121 return RTL_R32(OCPAR) & OCPAR_FLAG;
1122}
1123
Francois Romieu24192212012-07-06 20:19:42 +02001124static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001125{
Francois Romieu24192212012-07-06 20:19:42 +02001126 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001127
Francois Romieu24192212012-07-06 20:19:42 +02001128 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001129 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1130 RTL_W32(EPHY_RXER_NUM, 0);
1131
Francois Romieuffc46952012-07-06 14:19:23 +02001132 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001133}
1134
Francois Romieu24192212012-07-06 20:19:42 +02001135static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001136{
Francois Romieu24192212012-07-06 20:19:42 +02001137 r8168dp_1_mdio_access(tp, reg,
1138 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001139}
1140
Francois Romieu24192212012-07-06 20:19:42 +02001141static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001142{
Francois Romieu24192212012-07-06 20:19:42 +02001143 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001144
Francois Romieu24192212012-07-06 20:19:42 +02001145 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001146
1147 mdelay(1);
1148 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1149 RTL_W32(EPHY_RXER_NUM, 0);
1150
Francois Romieuffc46952012-07-06 14:19:23 +02001151 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1152 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001153}
1154
françois romieue6de30d2011-01-03 15:08:37 +00001155#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1156
1157static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1158{
1159 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1160}
1161
1162static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1163{
1164 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1165}
1166
Francois Romieu24192212012-07-06 20:19:42 +02001167static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001168{
Francois Romieu24192212012-07-06 20:19:42 +02001169 void __iomem *ioaddr = tp->mmio_addr;
1170
françois romieue6de30d2011-01-03 15:08:37 +00001171 r8168dp_2_mdio_start(ioaddr);
1172
Francois Romieu24192212012-07-06 20:19:42 +02001173 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001174
1175 r8168dp_2_mdio_stop(ioaddr);
1176}
1177
Francois Romieu24192212012-07-06 20:19:42 +02001178static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001179{
Francois Romieu24192212012-07-06 20:19:42 +02001180 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001181 int value;
1182
1183 r8168dp_2_mdio_start(ioaddr);
1184
Francois Romieu24192212012-07-06 20:19:42 +02001185 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001186
1187 r8168dp_2_mdio_stop(ioaddr);
1188
1189 return value;
1190}
1191
françois romieu4da19632011-01-03 15:07:55 +00001192static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001193{
Francois Romieu24192212012-07-06 20:19:42 +02001194 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001195}
1196
françois romieu4da19632011-01-03 15:07:55 +00001197static int rtl_readphy(struct rtl8169_private *tp, int location)
1198{
Francois Romieu24192212012-07-06 20:19:42 +02001199 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001200}
1201
1202static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1203{
1204 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1205}
1206
Chun-Hao Lin76564422014-10-01 23:17:17 +08001207static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001208{
1209 int val;
1210
françois romieu4da19632011-01-03 15:07:55 +00001211 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001212 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001213}
1214
Francois Romieuccdffb92008-07-26 14:26:06 +02001215static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1216 int val)
1217{
1218 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001219
françois romieu4da19632011-01-03 15:07:55 +00001220 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001221}
1222
1223static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1224{
1225 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001226
françois romieu4da19632011-01-03 15:07:55 +00001227 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001228}
1229
Francois Romieuffc46952012-07-06 14:19:23 +02001230DECLARE_RTL_COND(rtl_ephyar_cond)
1231{
1232 void __iomem *ioaddr = tp->mmio_addr;
1233
1234 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1235}
1236
Francois Romieufdf6fc02012-07-06 22:40:38 +02001237static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001238{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001239 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001240
1241 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1242 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1243
Francois Romieuffc46952012-07-06 14:19:23 +02001244 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1245
1246 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001247}
1248
Francois Romieufdf6fc02012-07-06 22:40:38 +02001249static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001250{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001251 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001252
1253 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1254
Francois Romieuffc46952012-07-06 14:19:23 +02001255 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1256 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001257}
1258
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001259DECLARE_RTL_COND(rtl_eriar_cond)
1260{
1261 void __iomem *ioaddr = tp->mmio_addr;
1262
1263 return RTL_R32(ERIAR) & ERIAR_FLAG;
1264}
1265
Francois Romieufdf6fc02012-07-06 22:40:38 +02001266static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1267 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001268{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001269 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001270
1271 BUG_ON((addr & 3) || (mask == 0));
1272 RTL_W32(ERIDR, val);
1273 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1274
Francois Romieuffc46952012-07-06 14:19:23 +02001275 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001276}
1277
Francois Romieufdf6fc02012-07-06 22:40:38 +02001278static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001279{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001280 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001281
1282 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1283
Francois Romieuffc46952012-07-06 14:19:23 +02001284 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1285 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001286}
1287
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001288static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001289 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001290{
1291 u32 val;
1292
Francois Romieufdf6fc02012-07-06 22:40:38 +02001293 val = rtl_eri_read(tp, addr, type);
1294 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001295}
1296
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001297static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1298{
1299 void __iomem *ioaddr = tp->mmio_addr;
1300
1301 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1302 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1303 RTL_R32(OCPDR) : ~0;
1304}
1305
1306static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1307{
1308 return rtl_eri_read(tp, reg, ERIAR_OOB);
1309}
1310
1311static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1312{
1313 switch (tp->mac_version) {
1314 case RTL_GIGA_MAC_VER_27:
1315 case RTL_GIGA_MAC_VER_28:
1316 case RTL_GIGA_MAC_VER_31:
1317 return r8168dp_ocp_read(tp, mask, reg);
1318 case RTL_GIGA_MAC_VER_49:
1319 case RTL_GIGA_MAC_VER_50:
1320 case RTL_GIGA_MAC_VER_51:
1321 return r8168ep_ocp_read(tp, mask, reg);
1322 default:
1323 BUG();
1324 return ~0;
1325 }
1326}
1327
1328static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1329 u32 data)
1330{
1331 void __iomem *ioaddr = tp->mmio_addr;
1332
1333 RTL_W32(OCPDR, data);
1334 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1335 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1336}
1337
1338static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1339 u32 data)
1340{
1341 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1342 data, ERIAR_OOB);
1343}
1344
1345static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1346{
1347 switch (tp->mac_version) {
1348 case RTL_GIGA_MAC_VER_27:
1349 case RTL_GIGA_MAC_VER_28:
1350 case RTL_GIGA_MAC_VER_31:
1351 r8168dp_ocp_write(tp, mask, reg, data);
1352 break;
1353 case RTL_GIGA_MAC_VER_49:
1354 case RTL_GIGA_MAC_VER_50:
1355 case RTL_GIGA_MAC_VER_51:
1356 r8168ep_ocp_write(tp, mask, reg, data);
1357 break;
1358 default:
1359 BUG();
1360 break;
1361 }
1362}
1363
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001364static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1365{
1366 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1367
1368 ocp_write(tp, 0x1, 0x30, 0x00000001);
1369}
1370
1371#define OOB_CMD_RESET 0x00
1372#define OOB_CMD_DRIVER_START 0x05
1373#define OOB_CMD_DRIVER_STOP 0x06
1374
1375static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1376{
1377 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1378}
1379
1380DECLARE_RTL_COND(rtl_ocp_read_cond)
1381{
1382 u16 reg;
1383
1384 reg = rtl8168_get_ocp_reg(tp);
1385
1386 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1387}
1388
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001389DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1390{
1391 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1392}
1393
1394DECLARE_RTL_COND(rtl_ocp_tx_cond)
1395{
1396 void __iomem *ioaddr = tp->mmio_addr;
1397
1398 return RTL_R8(IBISR0) & 0x02;
1399}
1400
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001401static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1402{
1403 void __iomem *ioaddr = tp->mmio_addr;
1404
1405 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1406 rtl_msleep_loop_wait_low(tp, &rtl_ocp_tx_cond, 50, 2000);
1407 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1408 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1409}
1410
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001411static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001412{
1413 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001414 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1415}
1416
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001417static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1418{
1419 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1420 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1421 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1422}
1423
1424static void rtl8168_driver_start(struct rtl8169_private *tp)
1425{
1426 switch (tp->mac_version) {
1427 case RTL_GIGA_MAC_VER_27:
1428 case RTL_GIGA_MAC_VER_28:
1429 case RTL_GIGA_MAC_VER_31:
1430 rtl8168dp_driver_start(tp);
1431 break;
1432 case RTL_GIGA_MAC_VER_49:
1433 case RTL_GIGA_MAC_VER_50:
1434 case RTL_GIGA_MAC_VER_51:
1435 rtl8168ep_driver_start(tp);
1436 break;
1437 default:
1438 BUG();
1439 break;
1440 }
1441}
1442
1443static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1444{
1445 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1446 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1447}
1448
1449static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1450{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001451 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001452 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1453 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1454 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1455}
1456
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001457static void rtl8168_driver_stop(struct rtl8169_private *tp)
1458{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001459 switch (tp->mac_version) {
1460 case RTL_GIGA_MAC_VER_27:
1461 case RTL_GIGA_MAC_VER_28:
1462 case RTL_GIGA_MAC_VER_31:
1463 rtl8168dp_driver_stop(tp);
1464 break;
1465 case RTL_GIGA_MAC_VER_49:
1466 case RTL_GIGA_MAC_VER_50:
1467 case RTL_GIGA_MAC_VER_51:
1468 rtl8168ep_driver_stop(tp);
1469 break;
1470 default:
1471 BUG();
1472 break;
1473 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001474}
1475
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001476static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001477{
1478 u16 reg = rtl8168_get_ocp_reg(tp);
1479
1480 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1481}
1482
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001483static int r8168ep_check_dash(struct rtl8169_private *tp)
1484{
1485 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1486}
1487
1488static int r8168_check_dash(struct rtl8169_private *tp)
1489{
1490 switch (tp->mac_version) {
1491 case RTL_GIGA_MAC_VER_27:
1492 case RTL_GIGA_MAC_VER_28:
1493 case RTL_GIGA_MAC_VER_31:
1494 return r8168dp_check_dash(tp);
1495 case RTL_GIGA_MAC_VER_49:
1496 case RTL_GIGA_MAC_VER_50:
1497 case RTL_GIGA_MAC_VER_51:
1498 return r8168ep_check_dash(tp);
1499 default:
1500 return 0;
1501 }
1502}
1503
françois romieuc28aa382011-08-02 03:53:43 +00001504struct exgmac_reg {
1505 u16 addr;
1506 u16 mask;
1507 u32 val;
1508};
1509
Francois Romieufdf6fc02012-07-06 22:40:38 +02001510static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001511 const struct exgmac_reg *r, int len)
1512{
1513 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001514 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001515 r++;
1516 }
1517}
1518
Francois Romieuffc46952012-07-06 14:19:23 +02001519DECLARE_RTL_COND(rtl_efusear_cond)
1520{
1521 void __iomem *ioaddr = tp->mmio_addr;
1522
1523 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1524}
1525
Francois Romieufdf6fc02012-07-06 22:40:38 +02001526static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001527{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001528 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001529
1530 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1531
Francois Romieuffc46952012-07-06 14:19:23 +02001532 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1533 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001534}
1535
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001536static u16 rtl_get_events(struct rtl8169_private *tp)
1537{
1538 void __iomem *ioaddr = tp->mmio_addr;
1539
1540 return RTL_R16(IntrStatus);
1541}
1542
1543static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1544{
1545 void __iomem *ioaddr = tp->mmio_addr;
1546
1547 RTL_W16(IntrStatus, bits);
1548 mmiowb();
1549}
1550
1551static void rtl_irq_disable(struct rtl8169_private *tp)
1552{
1553 void __iomem *ioaddr = tp->mmio_addr;
1554
1555 RTL_W16(IntrMask, 0);
1556 mmiowb();
1557}
1558
Francois Romieu3e990ff2012-01-26 12:50:01 +01001559static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1560{
1561 void __iomem *ioaddr = tp->mmio_addr;
1562
1563 RTL_W16(IntrMask, bits);
1564}
1565
Francois Romieuda78dbf2012-01-26 14:18:23 +01001566#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1567#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1568#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1569
1570static void rtl_irq_enable_all(struct rtl8169_private *tp)
1571{
1572 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1573}
1574
françois romieu811fd302011-12-04 20:30:45 +00001575static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
françois romieu811fd302011-12-04 20:30:45 +00001577 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001579 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001580 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001581 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
françois romieu4da19632011-01-03 15:07:55 +00001584static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
françois romieu4da19632011-01-03 15:07:55 +00001586 void __iomem *ioaddr = tp->mmio_addr;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return RTL_R32(TBICSR) & TBIReset;
1589}
1590
françois romieu4da19632011-01-03 15:07:55 +00001591static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
françois romieu4da19632011-01-03 15:07:55 +00001593 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1597{
1598 return RTL_R32(TBICSR) & TBILinkOk;
1599}
1600
1601static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1602{
1603 return RTL_R8(PHYstatus) & LinkStatus;
1604}
1605
françois romieu4da19632011-01-03 15:07:55 +00001606static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
françois romieu4da19632011-01-03 15:07:55 +00001608 void __iomem *ioaddr = tp->mmio_addr;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1611}
1612
françois romieu4da19632011-01-03 15:07:55 +00001613static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 unsigned int val;
1616
françois romieu4da19632011-01-03 15:07:55 +00001617 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1618 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Hayes Wang70090422011-07-06 15:58:06 +08001621static void rtl_link_chg_patch(struct rtl8169_private *tp)
1622{
1623 void __iomem *ioaddr = tp->mmio_addr;
1624 struct net_device *dev = tp->dev;
1625
1626 if (!netif_running(dev))
1627 return;
1628
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001629 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1630 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001631 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001632 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1633 ERIAR_EXGMAC);
1634 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1635 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001636 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001637 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1638 ERIAR_EXGMAC);
1639 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1640 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001641 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001642 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1643 ERIAR_EXGMAC);
1644 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1645 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001646 }
1647 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001648 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001649 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001650 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001651 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001652 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1653 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1654 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001655 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1656 ERIAR_EXGMAC);
1657 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1658 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001659 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001660 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1661 ERIAR_EXGMAC);
1662 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1663 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001664 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001665 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1666 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001667 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1668 ERIAR_EXGMAC);
1669 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1670 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001671 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001672 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1673 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001674 }
Hayes Wang70090422011-07-06 15:58:06 +08001675 }
1676}
1677
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001678static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001679 struct rtl8169_private *tp,
1680 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001683 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001684 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001685 if (pm)
1686 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001688 if (net_ratelimit())
1689 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001692 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001693 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001694 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001698static void rtl8169_check_link_status(struct net_device *dev,
1699 struct rtl8169_private *tp,
1700 void __iomem *ioaddr)
1701{
1702 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1703}
1704
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001705#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1706
1707static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1708{
1709 void __iomem *ioaddr = tp->mmio_addr;
1710 u8 options;
1711 u32 wolopts = 0;
1712
1713 options = RTL_R8(Config1);
1714 if (!(options & PMEnable))
1715 return 0;
1716
1717 options = RTL_R8(Config3);
1718 if (options & LinkUp)
1719 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001720 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001721 case RTL_GIGA_MAC_VER_34:
1722 case RTL_GIGA_MAC_VER_35:
1723 case RTL_GIGA_MAC_VER_36:
1724 case RTL_GIGA_MAC_VER_37:
1725 case RTL_GIGA_MAC_VER_38:
1726 case RTL_GIGA_MAC_VER_40:
1727 case RTL_GIGA_MAC_VER_41:
1728 case RTL_GIGA_MAC_VER_42:
1729 case RTL_GIGA_MAC_VER_43:
1730 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001731 case RTL_GIGA_MAC_VER_45:
1732 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001733 case RTL_GIGA_MAC_VER_47:
1734 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001735 case RTL_GIGA_MAC_VER_49:
1736 case RTL_GIGA_MAC_VER_50:
1737 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001738 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1739 wolopts |= WAKE_MAGIC;
1740 break;
1741 default:
1742 if (options & MagicPacket)
1743 wolopts |= WAKE_MAGIC;
1744 break;
1745 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001746
1747 options = RTL_R8(Config5);
1748 if (options & UWF)
1749 wolopts |= WAKE_UCAST;
1750 if (options & BWF)
1751 wolopts |= WAKE_BCAST;
1752 if (options & MWF)
1753 wolopts |= WAKE_MCAST;
1754
1755 return wolopts;
1756}
1757
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001758static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1759{
1760 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001761 struct device *d = &tp->pci_dev->dev;
1762
1763 pm_runtime_get_noresume(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001764
Francois Romieuda78dbf2012-01-26 14:18:23 +01001765 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001766
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001767 wol->supported = WAKE_ANY;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001768 if (pm_runtime_active(d))
1769 wol->wolopts = __rtl8169_get_wol(tp);
1770 else
1771 wol->wolopts = tp->saved_wolopts;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001772
Francois Romieuda78dbf2012-01-26 14:18:23 +01001773 rtl_unlock_work(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001774
1775 pm_runtime_put_noidle(d);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001776}
1777
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001778static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001779{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001780 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001781 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001782 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001783 u32 opt;
1784 u16 reg;
1785 u8 mask;
1786 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001787 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001788 { WAKE_UCAST, Config5, UWF },
1789 { WAKE_BCAST, Config5, BWF },
1790 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001791 { WAKE_ANY, Config5, LanWake },
1792 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001793 };
Francois Romieu851e6022012-04-17 11:10:11 +02001794 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001795
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001796 RTL_W8(Cfg9346, Cfg9346_Unlock);
1797
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001798 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001799 case RTL_GIGA_MAC_VER_34:
1800 case RTL_GIGA_MAC_VER_35:
1801 case RTL_GIGA_MAC_VER_36:
1802 case RTL_GIGA_MAC_VER_37:
1803 case RTL_GIGA_MAC_VER_38:
1804 case RTL_GIGA_MAC_VER_40:
1805 case RTL_GIGA_MAC_VER_41:
1806 case RTL_GIGA_MAC_VER_42:
1807 case RTL_GIGA_MAC_VER_43:
1808 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001809 case RTL_GIGA_MAC_VER_45:
1810 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001811 case RTL_GIGA_MAC_VER_47:
1812 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001813 case RTL_GIGA_MAC_VER_49:
1814 case RTL_GIGA_MAC_VER_50:
1815 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001816 tmp = ARRAY_SIZE(cfg) - 1;
1817 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001818 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001819 0x0dc,
1820 ERIAR_MASK_0100,
1821 MagicPacket_v2,
1822 0x0000,
1823 ERIAR_EXGMAC);
1824 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001825 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001826 0x0dc,
1827 ERIAR_MASK_0100,
1828 0x0000,
1829 MagicPacket_v2,
1830 ERIAR_EXGMAC);
1831 break;
1832 default:
1833 tmp = ARRAY_SIZE(cfg);
1834 break;
1835 }
1836
1837 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001838 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001839 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001840 options |= cfg[i].mask;
1841 RTL_W8(cfg[i].reg, options);
1842 }
1843
Francois Romieu851e6022012-04-17 11:10:11 +02001844 switch (tp->mac_version) {
1845 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1846 options = RTL_R8(Config1) & ~PMEnable;
1847 if (wolopts)
1848 options |= PMEnable;
1849 RTL_W8(Config1, options);
1850 break;
1851 default:
Francois Romieud387b422012-04-17 11:12:01 +02001852 options = RTL_R8(Config2) & ~PME_SIGNAL;
1853 if (wolopts)
1854 options |= PME_SIGNAL;
1855 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001856 break;
1857 }
1858
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001859 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001860}
1861
1862static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1863{
1864 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001865 struct device *d = &tp->pci_dev->dev;
1866
1867 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001868
Francois Romieuda78dbf2012-01-26 14:18:23 +01001869 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001870
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001871 if (wol->wolopts)
1872 tp->features |= RTL_FEATURE_WOL;
1873 else
1874 tp->features &= ~RTL_FEATURE_WOL;
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001875 if (pm_runtime_active(d))
1876 __rtl8169_set_wol(tp, wol->wolopts);
1877 else
1878 tp->saved_wolopts = wol->wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001879
1880 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001881
françois romieuea809072010-11-08 13:23:58 +00001882 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1883
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001884 pm_runtime_put_noidle(d);
1885
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001886 return 0;
1887}
1888
Francois Romieu31bd2042011-04-26 18:58:59 +02001889static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1890{
Francois Romieu85bffe62011-04-27 08:22:39 +02001891 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001892}
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894static void rtl8169_get_drvinfo(struct net_device *dev,
1895 struct ethtool_drvinfo *info)
1896{
1897 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001898 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Rick Jones68aad782011-11-07 13:29:27 +00001900 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1901 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1902 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001903 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001904 if (!IS_ERR_OR_NULL(rtl_fw))
1905 strlcpy(info->fw_version, rtl_fw->version,
1906 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909static int rtl8169_get_regs_len(struct net_device *dev)
1910{
1911 return R8169_REGS_SIZE;
1912}
1913
1914static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001915 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 struct rtl8169_private *tp = netdev_priv(dev);
1918 void __iomem *ioaddr = tp->mmio_addr;
1919 int ret = 0;
1920 u32 reg;
1921
1922 reg = RTL_R32(TBICSR);
1923 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1924 (duplex == DUPLEX_FULL)) {
1925 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1926 } else if (autoneg == AUTONEG_ENABLE)
1927 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1928 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001929 netif_warn(tp, link, dev,
1930 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ret = -EOPNOTSUPP;
1932 }
1933
1934 return ret;
1935}
1936
1937static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001938 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001941 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001942 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Hayes Wang716b50a2011-02-22 17:26:18 +08001944 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001947 int auto_nego;
1948
françois romieu4da19632011-01-03 15:07:55 +00001949 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001950 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1951 ADVERTISE_100HALF | ADVERTISE_100FULL);
1952
1953 if (adv & ADVERTISED_10baseT_Half)
1954 auto_nego |= ADVERTISE_10HALF;
1955 if (adv & ADVERTISED_10baseT_Full)
1956 auto_nego |= ADVERTISE_10FULL;
1957 if (adv & ADVERTISED_100baseT_Half)
1958 auto_nego |= ADVERTISE_100HALF;
1959 if (adv & ADVERTISED_100baseT_Full)
1960 auto_nego |= ADVERTISE_100FULL;
1961
françois romieu3577aa12009-05-19 10:46:48 +00001962 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1963
françois romieu4da19632011-01-03 15:07:55 +00001964 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001965 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1966
1967 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001968 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001969 if (adv & ADVERTISED_1000baseT_Half)
1970 giga_ctrl |= ADVERTISE_1000HALF;
1971 if (adv & ADVERTISED_1000baseT_Full)
1972 giga_ctrl |= ADVERTISE_1000FULL;
1973 } else if (adv & (ADVERTISED_1000baseT_Half |
1974 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001975 netif_info(tp, link, dev,
1976 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001977 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
françois romieu3577aa12009-05-19 10:46:48 +00001980 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001981
françois romieu4da19632011-01-03 15:07:55 +00001982 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1983 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001984 } else {
françois romieu3577aa12009-05-19 10:46:48 +00001985 if (speed == SPEED_10)
1986 bmcr = 0;
1987 else if (speed == SPEED_100)
1988 bmcr = BMCR_SPEED100;
1989 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001990 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001991
1992 if (duplex == DUPLEX_FULL)
1993 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001994 }
1995
françois romieu4da19632011-01-03 15:07:55 +00001996 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001997
Francois Romieucecb5fd2011-04-01 10:21:07 +02001998 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1999 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00002000 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00002001 rtl_writephy(tp, 0x17, 0x2138);
2002 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00002003 } else {
françois romieu4da19632011-01-03 15:07:55 +00002004 rtl_writephy(tp, 0x17, 0x2108);
2005 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00002006 }
2007 }
2008
Oliver Neukum54405cd2011-01-06 21:55:13 +01002009 rc = 0;
2010out:
2011 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01002015 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct rtl8169_private *tp = netdev_priv(dev);
2018 int ret;
2019
Oliver Neukum54405cd2011-01-06 21:55:13 +01002020 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01002021 if (ret < 0)
2022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Francois Romieu4876cc12011-03-11 21:07:11 +01002024 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
Chun-Hao Linc4556972016-03-11 14:21:14 +08002025 (advertising & ADVERTISED_1000baseT_Full) &&
2026 !pci_is_pcie(tp->pci_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01002028 }
2029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return ret;
2031}
2032
2033static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2034{
2035 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int ret;
2037
Francois Romieu4876cc12011-03-11 21:07:11 +01002038 del_timer_sync(&tp->timer);
2039
Francois Romieuda78dbf2012-01-26 14:18:23 +01002040 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002041 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00002042 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002043 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return ret;
2046}
2047
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002048static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2049 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Francois Romieud58d46b2011-05-03 16:38:29 +02002051 struct rtl8169_private *tp = netdev_priv(dev);
2052
Francois Romieu2b7b4312011-04-18 22:53:24 -07002053 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002054 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Francois Romieud58d46b2011-05-03 16:38:29 +02002056 if (dev->mtu > JUMBO_1K &&
2057 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2058 features &= ~NETIF_F_IP_CSUM;
2059
Michał Mirosław350fb322011-04-08 06:35:56 +00002060 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Francois Romieuda78dbf2012-01-26 14:18:23 +01002063static void __rtl8169_set_features(struct net_device *dev,
2064 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002067 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002068 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
hayeswang929a0312014-09-16 11:40:47 +08002070 rx_config = RTL_R32(RxConfig);
2071 if (features & NETIF_F_RXALL)
2072 rx_config |= (AcceptErr | AcceptRunt);
2073 else
2074 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
hayeswang929a0312014-09-16 11:40:47 +08002076 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002077
hayeswang929a0312014-09-16 11:40:47 +08002078 if (features & NETIF_F_RXCSUM)
2079 tp->cp_cmd |= RxChkSum;
2080 else
2081 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002082
hayeswang929a0312014-09-16 11:40:47 +08002083 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2084 tp->cp_cmd |= RxVlan;
2085 else
2086 tp->cp_cmd &= ~RxVlan;
2087
2088 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2089
2090 RTL_W16(CPlusCmd, tp->cp_cmd);
2091 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Francois Romieuda78dbf2012-01-26 14:18:23 +01002094static int rtl8169_set_features(struct net_device *dev,
2095 netdev_features_t features)
2096{
2097 struct rtl8169_private *tp = netdev_priv(dev);
2098
hayeswang929a0312014-09-16 11:40:47 +08002099 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2100
Francois Romieuda78dbf2012-01-26 14:18:23 +01002101 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002102 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002103 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002104 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 return 0;
2107}
2108
Francois Romieuda78dbf2012-01-26 14:18:23 +01002109
Kirill Smelkov810f4892012-11-10 21:11:02 +04002110static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002112 return (skb_vlan_tag_present(skb)) ?
2113 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Francois Romieu7a8fc772011-03-01 17:18:33 +01002116static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Francois Romieu7a8fc772011-03-01 17:18:33 +01002120 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002121 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002124static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2125 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 struct rtl8169_private *tp = netdev_priv(dev);
2128 void __iomem *ioaddr = tp->mmio_addr;
2129 u32 status;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002130 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002132 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002134 cmd->base.port = PORT_FIBRE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 status = RTL_R32(TBICSR);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002137 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2138 cmd->base.autoneg = !!(status & TBINwEnable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002140 cmd->base.speed = SPEED_1000;
2141 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2142
2143 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2144 supported);
2145 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2146 advertising);
Francois Romieuccdffb92008-07-26 14:26:06 +02002147
2148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002151static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2152 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03002156 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2157
2158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002161static int rtl8169_get_link_ksettings(struct net_device *dev,
2162 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002165 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Francois Romieuda78dbf2012-01-26 14:18:23 +01002167 rtl_lock_work(tp);
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002168 rc = tp->get_link_ksettings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002169 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Francois Romieuccdffb92008-07-26 14:26:06 +02002171 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
2174static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2175 void *p)
2176{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002177 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002178 u32 __iomem *data = tp->mmio_addr;
2179 u32 *dw = p;
2180 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Francois Romieuda78dbf2012-01-26 14:18:23 +01002182 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002183 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2184 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002185 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002188static u32 rtl8169_get_msglevel(struct net_device *dev)
2189{
2190 struct rtl8169_private *tp = netdev_priv(dev);
2191
2192 return tp->msg_enable;
2193}
2194
2195static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2196{
2197 struct rtl8169_private *tp = netdev_priv(dev);
2198
2199 tp->msg_enable = value;
2200}
2201
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002202static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2203 "tx_packets",
2204 "rx_packets",
2205 "tx_errors",
2206 "rx_errors",
2207 "rx_missed",
2208 "align_errors",
2209 "tx_single_collisions",
2210 "tx_multi_collisions",
2211 "unicast",
2212 "broadcast",
2213 "multicast",
2214 "tx_aborted",
2215 "tx_underrun",
2216};
2217
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002218static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002219{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002220 switch (sset) {
2221 case ETH_SS_STATS:
2222 return ARRAY_SIZE(rtl8169_gstrings);
2223 default:
2224 return -EOPNOTSUPP;
2225 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002226}
2227
Corinna Vinschen42020322015-09-10 10:47:35 +02002228DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002229{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002230 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002231
Corinna Vinschen42020322015-09-10 10:47:35 +02002232 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002233}
2234
Corinna Vinschen42020322015-09-10 10:47:35 +02002235static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002236{
2237 struct rtl8169_private *tp = netdev_priv(dev);
2238 void __iomem *ioaddr = tp->mmio_addr;
Corinna Vinschen42020322015-09-10 10:47:35 +02002239 dma_addr_t paddr = tp->counters_phys_addr;
2240 u32 cmd;
2241 bool ret;
2242
2243 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
2244 cmd = (u64)paddr & DMA_BIT_MASK(32);
2245 RTL_W32(CounterAddrLow, cmd);
2246 RTL_W32(CounterAddrLow, cmd | counter_cmd);
2247
2248 ret = rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002249
2250 RTL_W32(CounterAddrLow, 0);
2251 RTL_W32(CounterAddrHigh, 0);
2252
Corinna Vinschen42020322015-09-10 10:47:35 +02002253 return ret;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002254}
2255
2256static bool rtl8169_reset_counters(struct net_device *dev)
2257{
2258 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002259
2260 /*
2261 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2262 * tally counters.
2263 */
2264 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2265 return true;
2266
Corinna Vinschen42020322015-09-10 10:47:35 +02002267 return rtl8169_do_counters(dev, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02002268}
2269
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002270static bool rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002271{
2272 struct rtl8169_private *tp = netdev_priv(dev);
2273 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002274
Ivan Vecera355423d2009-02-06 21:49:57 -08002275 /*
2276 * Some chips are unable to dump tally counters when the receiver
2277 * is disabled.
2278 */
2279 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002280 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002281
Corinna Vinschen42020322015-09-10 10:47:35 +02002282 return rtl8169_do_counters(dev, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002283}
2284
2285static bool rtl8169_init_counter_offsets(struct net_device *dev)
2286{
2287 struct rtl8169_private *tp = netdev_priv(dev);
Corinna Vinschen42020322015-09-10 10:47:35 +02002288 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002289 bool ret = false;
2290
2291 /*
2292 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2293 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2294 * reset by a power cycle, while the counter values collected by the
2295 * driver are reset at every driver unload/load cycle.
2296 *
2297 * To make sure the HW values returned by @get_stats64 match the SW
2298 * values, we collect the initial values at first open(*) and use them
2299 * as offsets to normalize the values returned by @get_stats64.
2300 *
2301 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2302 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2303 * set at open time by rtl_hw_start.
2304 */
2305
2306 if (tp->tc_offset.inited)
2307 return true;
2308
2309 /* If both, reset and update fail, propagate to caller. */
2310 if (rtl8169_reset_counters(dev))
2311 ret = true;
2312
2313 if (rtl8169_update_counters(dev))
2314 ret = true;
2315
Corinna Vinschen42020322015-09-10 10:47:35 +02002316 tp->tc_offset.tx_errors = counters->tx_errors;
2317 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2318 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02002319 tp->tc_offset.inited = true;
2320
2321 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002322}
2323
Ivan Vecera355423d2009-02-06 21:49:57 -08002324static void rtl8169_get_ethtool_stats(struct net_device *dev,
2325 struct ethtool_stats *stats, u64 *data)
2326{
2327 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Line0636232016-07-29 16:37:55 +08002328 struct device *d = &tp->pci_dev->dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02002329 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08002330
2331 ASSERT_RTNL();
2332
Chun-Hao Line0636232016-07-29 16:37:55 +08002333 pm_runtime_get_noresume(d);
2334
2335 if (pm_runtime_active(d))
2336 rtl8169_update_counters(dev);
2337
2338 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08002339
Corinna Vinschen42020322015-09-10 10:47:35 +02002340 data[0] = le64_to_cpu(counters->tx_packets);
2341 data[1] = le64_to_cpu(counters->rx_packets);
2342 data[2] = le64_to_cpu(counters->tx_errors);
2343 data[3] = le32_to_cpu(counters->rx_errors);
2344 data[4] = le16_to_cpu(counters->rx_missed);
2345 data[5] = le16_to_cpu(counters->align_errors);
2346 data[6] = le32_to_cpu(counters->tx_one_collision);
2347 data[7] = le32_to_cpu(counters->tx_multi_collision);
2348 data[8] = le64_to_cpu(counters->rx_unicast);
2349 data[9] = le64_to_cpu(counters->rx_broadcast);
2350 data[10] = le32_to_cpu(counters->rx_multicast);
2351 data[11] = le16_to_cpu(counters->tx_aborted);
2352 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08002353}
2354
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002355static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2356{
2357 switch(stringset) {
2358 case ETH_SS_STATS:
2359 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2360 break;
2361 }
2362}
2363
Florian Fainellif0903ea2016-12-03 12:01:19 -08002364static int rtl8169_nway_reset(struct net_device *dev)
2365{
2366 struct rtl8169_private *tp = netdev_priv(dev);
2367
2368 return mii_nway_restart(&tp->mii);
2369}
2370
Francois Romieu50970832017-10-27 13:24:49 +03002371/*
2372 * Interrupt coalescing
2373 *
2374 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2375 * > 8169, 8168 and 810x line of chipsets
2376 *
2377 * 8169, 8168, and 8136(810x) serial chipsets support it.
2378 *
2379 * > 2 - the Tx timer unit at gigabit speed
2380 *
2381 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2382 * (0xe0) bit 1 and bit 0.
2383 *
2384 * For 8169
2385 * bit[1:0] \ speed 1000M 100M 10M
2386 * 0 0 320ns 2.56us 40.96us
2387 * 0 1 2.56us 20.48us 327.7us
2388 * 1 0 5.12us 40.96us 655.4us
2389 * 1 1 10.24us 81.92us 1.31ms
2390 *
2391 * For the other
2392 * bit[1:0] \ speed 1000M 100M 10M
2393 * 0 0 5us 2.56us 40.96us
2394 * 0 1 40us 20.48us 327.7us
2395 * 1 0 80us 40.96us 655.4us
2396 * 1 1 160us 81.92us 1.31ms
2397 */
2398
2399/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2400struct rtl_coalesce_scale {
2401 /* Rx / Tx */
2402 u32 nsecs[2];
2403};
2404
2405/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2406struct rtl_coalesce_info {
2407 u32 speed;
2408 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2409};
2410
2411/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2412#define rxtx_x1822(r, t) { \
2413 {{(r), (t)}}, \
2414 {{(r)*8, (t)*8}}, \
2415 {{(r)*8*2, (t)*8*2}}, \
2416 {{(r)*8*2*2, (t)*8*2*2}}, \
2417}
2418static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2419 /* speed delays: rx00 tx00 */
2420 { SPEED_10, rxtx_x1822(40960, 40960) },
2421 { SPEED_100, rxtx_x1822( 2560, 2560) },
2422 { SPEED_1000, rxtx_x1822( 320, 320) },
2423 { 0 },
2424};
2425
2426static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2427 /* speed delays: rx00 tx00 */
2428 { SPEED_10, rxtx_x1822(40960, 40960) },
2429 { SPEED_100, rxtx_x1822( 2560, 2560) },
2430 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2431 { 0 },
2432};
2433#undef rxtx_x1822
2434
2435/* get rx/tx scale vector corresponding to current speed */
2436static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2437{
2438 struct rtl8169_private *tp = netdev_priv(dev);
2439 struct ethtool_link_ksettings ecmd;
2440 const struct rtl_coalesce_info *ci;
2441 int rc;
2442
2443 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2444 if (rc < 0)
2445 return ERR_PTR(rc);
2446
2447 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2448 if (ecmd.base.speed == ci->speed) {
2449 return ci;
2450 }
2451 }
2452
2453 return ERR_PTR(-ELNRNG);
2454}
2455
2456static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2457{
2458 struct rtl8169_private *tp = netdev_priv(dev);
2459 void __iomem *ioaddr = tp->mmio_addr;
2460 const struct rtl_coalesce_info *ci;
2461 const struct rtl_coalesce_scale *scale;
2462 struct {
2463 u32 *max_frames;
2464 u32 *usecs;
2465 } coal_settings [] = {
2466 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2467 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2468 }, *p = coal_settings;
2469 int i;
2470 u16 w;
2471
2472 memset(ec, 0, sizeof(*ec));
2473
2474 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2475 ci = rtl_coalesce_info(dev);
2476 if (IS_ERR(ci))
2477 return PTR_ERR(ci);
2478
2479 scale = &ci->scalev[RTL_R16(CPlusCmd) & 3];
2480
2481 /* read IntrMitigate and adjust according to scale */
2482 for (w = RTL_R16(IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2483 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2484 w >>= RTL_COALESCE_SHIFT;
2485 *p->usecs = w & RTL_COALESCE_MASK;
2486 }
2487
2488 for (i = 0; i < 2; i++) {
2489 p = coal_settings + i;
2490 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2491
2492 /*
2493 * ethtool_coalesce says it is illegal to set both usecs and
2494 * max_frames to 0.
2495 */
2496 if (!*p->usecs && !*p->max_frames)
2497 *p->max_frames = 1;
2498 }
2499
2500 return 0;
2501}
2502
2503/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2504static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2505 struct net_device *dev, u32 nsec, u16 *cp01)
2506{
2507 const struct rtl_coalesce_info *ci;
2508 u16 i;
2509
2510 ci = rtl_coalesce_info(dev);
2511 if (IS_ERR(ci))
2512 return ERR_CAST(ci);
2513
2514 for (i = 0; i < 4; i++) {
2515 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2516 ci->scalev[i].nsecs[1]);
2517 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2518 *cp01 = i;
2519 return &ci->scalev[i];
2520 }
2521 }
2522
2523 return ERR_PTR(-EINVAL);
2524}
2525
2526static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2527{
2528 struct rtl8169_private *tp = netdev_priv(dev);
2529 void __iomem *ioaddr = tp->mmio_addr;
2530 const struct rtl_coalesce_scale *scale;
2531 struct {
2532 u32 frames;
2533 u32 usecs;
2534 } coal_settings [] = {
2535 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2536 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2537 }, *p = coal_settings;
2538 u16 w = 0, cp01;
2539 int i;
2540
2541 scale = rtl_coalesce_choose_scale(dev,
2542 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2543 if (IS_ERR(scale))
2544 return PTR_ERR(scale);
2545
2546 for (i = 0; i < 2; i++, p++) {
2547 u32 units;
2548
2549 /*
2550 * accept max_frames=1 we returned in rtl_get_coalesce.
2551 * accept it not only when usecs=0 because of e.g. the following scenario:
2552 *
2553 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2554 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2555 * - then user does `ethtool -C eth0 rx-usecs 100`
2556 *
2557 * since ethtool sends to kernel whole ethtool_coalesce
2558 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2559 * we'll reject it below in `frames % 4 != 0`.
2560 */
2561 if (p->frames == 1) {
2562 p->frames = 0;
2563 }
2564
2565 units = p->usecs * 1000 / scale->nsecs[i];
2566 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2567 return -EINVAL;
2568
2569 w <<= RTL_COALESCE_SHIFT;
2570 w |= units;
2571 w <<= RTL_COALESCE_SHIFT;
2572 w |= p->frames >> 2;
2573 }
2574
2575 rtl_lock_work(tp);
2576
2577 RTL_W16(IntrMitigate, swab16(w));
2578
2579 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2580 RTL_W16(CPlusCmd, tp->cp_cmd);
2581 RTL_R16(CPlusCmd);
2582
2583 rtl_unlock_work(tp);
2584
2585 return 0;
2586}
2587
Jeff Garzik7282d492006-09-13 14:30:00 -04002588static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 .get_drvinfo = rtl8169_get_drvinfo,
2590 .get_regs_len = rtl8169_get_regs_len,
2591 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002592 .get_coalesce = rtl_get_coalesce,
2593 .set_coalesce = rtl_set_coalesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002595 .get_msglevel = rtl8169_get_msglevel,
2596 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002598 .get_wol = rtl8169_get_wol,
2599 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002600 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002601 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002602 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002603 .get_ts_info = ethtool_op_get_ts_info,
Florian Fainellif0903ea2016-12-03 12:01:19 -08002604 .nway_reset = rtl8169_nway_reset,
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01002605 .get_link_ksettings = rtl8169_get_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606};
2607
Francois Romieu07d3f512007-02-21 22:40:46 +01002608static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002609 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Francois Romieu5d320a22011-05-08 17:47:36 +02002611 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002612 /*
2613 * The driver currently handles the 8168Bf and the 8168Be identically
2614 * but they can be identified more specifically through the test below
2615 * if needed:
2616 *
2617 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002618 *
2619 * Same thing for the 8101Eb and the 8101Ec:
2620 *
2621 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002622 */
Francois Romieu37441002011-06-17 22:58:54 +02002623 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002625 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 int mac_version;
2627 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002628 /* 8168EP family. */
2629 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2630 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2631 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2632
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002633 /* 8168H family. */
2634 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2635 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2636
Hayes Wangc5583862012-07-02 17:23:22 +08002637 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002638 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002639 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002640 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2641 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2642
Hayes Wangc2218922011-09-06 16:55:18 +08002643 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002644 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002645 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2646 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2647
hayeswang01dc7fe2011-03-21 01:50:28 +00002648 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002649 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002650 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2651 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2652 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2653
Francois Romieu5b538df2008-07-20 16:22:45 +02002654 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002655 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2656 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002657 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002658
françois romieue6de30d2011-01-03 15:08:37 +00002659 /* 8168DP family. */
2660 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2661 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002662 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002663
Francois Romieuef808d52008-06-29 13:10:54 +02002664 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002665 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002666 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002667 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002668 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002669 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2670 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002671 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002672 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002673 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002674
2675 /* 8168B family. */
2676 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2677 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2678 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2679 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2680
2681 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002682 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2683 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002684 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002685 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002686 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2687 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2688 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002689 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2690 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2691 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2692 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2693 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2694 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002695 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002696 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002697 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002698 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2699 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002700 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2701 /* FIXME: where did these entries come from ? -- FR */
2702 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2703 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2704
2705 /* 8110 family. */
2706 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2707 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2708 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2709 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2710 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2711 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2712
Jean Delvaref21b75e2009-05-26 20:54:48 -07002713 /* Catch-all */
2714 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002715 };
2716 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 u32 reg;
2718
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002719 reg = RTL_R32(TxConfig);
2720 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 p++;
2722 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002723
2724 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2725 netif_notice(tp, probe, dev,
2726 "unknown MAC, using family default\n");
2727 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002728 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2729 tp->mac_version = tp->mii.supports_gmii ?
2730 RTL_GIGA_MAC_VER_42 :
2731 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002732 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2733 tp->mac_version = tp->mii.supports_gmii ?
2734 RTL_GIGA_MAC_VER_45 :
2735 RTL_GIGA_MAC_VER_47;
2736 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2737 tp->mac_version = tp->mii.supports_gmii ?
2738 RTL_GIGA_MAC_VER_46 :
2739 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
2743static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2744{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002745 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Francois Romieu867763c2007-08-17 18:21:58 +02002748struct phy_reg {
2749 u16 reg;
2750 u16 val;
2751};
2752
françois romieu4da19632011-01-03 15:07:55 +00002753static void rtl_writephy_batch(struct rtl8169_private *tp,
2754 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002755{
2756 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002758 regs++;
2759 }
2760}
2761
françois romieubca03d52011-01-03 15:07:31 +00002762#define PHY_READ 0x00000000
2763#define PHY_DATA_OR 0x10000000
2764#define PHY_DATA_AND 0x20000000
2765#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002766#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002767#define PHY_CLEAR_READCOUNT 0x70000000
2768#define PHY_WRITE 0x80000000
2769#define PHY_READCOUNT_EQ_SKIP 0x90000000
2770#define PHY_COMP_EQ_SKIPN 0xa0000000
2771#define PHY_COMP_NEQ_SKIPN 0xb0000000
2772#define PHY_WRITE_PREVIOUS 0xc0000000
2773#define PHY_SKIPN 0xd0000000
2774#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002775
Hayes Wang960aee62011-06-18 11:37:48 +02002776struct fw_info {
2777 u32 magic;
2778 char version[RTL_VER_SIZE];
2779 __le32 fw_start;
2780 __le32 fw_len;
2781 u8 chksum;
2782} __packed;
2783
Francois Romieu1c361ef2011-06-17 17:16:24 +02002784#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2785
2786static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002787{
Francois Romieub6ffd972011-06-17 17:00:05 +02002788 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002789 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002790 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2791 char *version = rtl_fw->version;
2792 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002793
Francois Romieu1c361ef2011-06-17 17:16:24 +02002794 if (fw->size < FW_OPCODE_SIZE)
2795 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002796
2797 if (!fw_info->magic) {
2798 size_t i, size, start;
2799 u8 checksum = 0;
2800
2801 if (fw->size < sizeof(*fw_info))
2802 goto out;
2803
2804 for (i = 0; i < fw->size; i++)
2805 checksum += fw->data[i];
2806 if (checksum != 0)
2807 goto out;
2808
2809 start = le32_to_cpu(fw_info->fw_start);
2810 if (start > fw->size)
2811 goto out;
2812
2813 size = le32_to_cpu(fw_info->fw_len);
2814 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2815 goto out;
2816
2817 memcpy(version, fw_info->version, RTL_VER_SIZE);
2818
2819 pa->code = (__le32 *)(fw->data + start);
2820 pa->size = size;
2821 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002822 if (fw->size % FW_OPCODE_SIZE)
2823 goto out;
2824
2825 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2826
2827 pa->code = (__le32 *)fw->data;
2828 pa->size = fw->size / FW_OPCODE_SIZE;
2829 }
2830 version[RTL_VER_SIZE - 1] = 0;
2831
2832 rc = true;
2833out:
2834 return rc;
2835}
2836
Francois Romieufd112f22011-06-18 00:10:29 +02002837static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2838 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002839{
Francois Romieufd112f22011-06-18 00:10:29 +02002840 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002841 size_t index;
2842
Francois Romieu1c361ef2011-06-17 17:16:24 +02002843 for (index = 0; index < pa->size; index++) {
2844 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002845 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002846
hayeswang42b82dc2011-01-10 02:07:25 +00002847 switch(action & 0xf0000000) {
2848 case PHY_READ:
2849 case PHY_DATA_OR:
2850 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002851 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002852 case PHY_CLEAR_READCOUNT:
2853 case PHY_WRITE:
2854 case PHY_WRITE_PREVIOUS:
2855 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002856 break;
2857
hayeswang42b82dc2011-01-10 02:07:25 +00002858 case PHY_BJMPN:
2859 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002860 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002861 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002862 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002863 }
2864 break;
2865 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002866 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002867 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002868 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002869 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002870 }
2871 break;
2872 case PHY_COMP_EQ_SKIPN:
2873 case PHY_COMP_NEQ_SKIPN:
2874 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002875 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002876 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002877 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002878 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002879 }
2880 break;
2881
hayeswang42b82dc2011-01-10 02:07:25 +00002882 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002883 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002884 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002885 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002886 }
2887 }
Francois Romieufd112f22011-06-18 00:10:29 +02002888 rc = true;
2889out:
2890 return rc;
2891}
françois romieubca03d52011-01-03 15:07:31 +00002892
Francois Romieufd112f22011-06-18 00:10:29 +02002893static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2894{
2895 struct net_device *dev = tp->dev;
2896 int rc = -EINVAL;
2897
2898 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002899 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002900 goto out;
2901 }
2902
2903 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2904 rc = 0;
2905out:
2906 return rc;
2907}
2908
2909static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2910{
2911 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002912 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002913 u32 predata, count;
2914 size_t index;
2915
2916 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002917 org.write = ops->write;
2918 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002919
Francois Romieu1c361ef2011-06-17 17:16:24 +02002920 for (index = 0; index < pa->size; ) {
2921 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002922 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002923 u32 regno = (action & 0x0fff0000) >> 16;
2924
2925 if (!action)
2926 break;
françois romieubca03d52011-01-03 15:07:31 +00002927
2928 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002929 case PHY_READ:
2930 predata = rtl_readphy(tp, regno);
2931 count++;
2932 index++;
françois romieubca03d52011-01-03 15:07:31 +00002933 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002934 case PHY_DATA_OR:
2935 predata |= data;
2936 index++;
2937 break;
2938 case PHY_DATA_AND:
2939 predata &= data;
2940 index++;
2941 break;
2942 case PHY_BJMPN:
2943 index -= regno;
2944 break;
hayeswangeee37862013-04-01 22:23:38 +00002945 case PHY_MDIO_CHG:
2946 if (data == 0) {
2947 ops->write = org.write;
2948 ops->read = org.read;
2949 } else if (data == 1) {
2950 ops->write = mac_mcu_write;
2951 ops->read = mac_mcu_read;
2952 }
2953
hayeswang42b82dc2011-01-10 02:07:25 +00002954 index++;
2955 break;
2956 case PHY_CLEAR_READCOUNT:
2957 count = 0;
2958 index++;
2959 break;
2960 case PHY_WRITE:
2961 rtl_writephy(tp, regno, data);
2962 index++;
2963 break;
2964 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002965 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002966 break;
2967 case PHY_COMP_EQ_SKIPN:
2968 if (predata == data)
2969 index += regno;
2970 index++;
2971 break;
2972 case PHY_COMP_NEQ_SKIPN:
2973 if (predata != data)
2974 index += regno;
2975 index++;
2976 break;
2977 case PHY_WRITE_PREVIOUS:
2978 rtl_writephy(tp, regno, predata);
2979 index++;
2980 break;
2981 case PHY_SKIPN:
2982 index += regno + 1;
2983 break;
2984 case PHY_DELAY_MS:
2985 mdelay(data);
2986 index++;
2987 break;
2988
françois romieubca03d52011-01-03 15:07:31 +00002989 default:
2990 BUG();
2991 }
2992 }
hayeswangeee37862013-04-01 22:23:38 +00002993
2994 ops->write = org.write;
2995 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002996}
2997
françois romieuf1e02ed2011-01-13 13:07:53 +00002998static void rtl_release_firmware(struct rtl8169_private *tp)
2999{
Francois Romieub6ffd972011-06-17 17:00:05 +02003000 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
3001 release_firmware(tp->rtl_fw->fw);
3002 kfree(tp->rtl_fw);
3003 }
3004 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00003005}
3006
François Romieu953a12c2011-04-24 17:38:48 +02003007static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00003008{
Francois Romieub6ffd972011-06-17 17:00:05 +02003009 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00003010
3011 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01003012 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02003013 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02003014}
3015
3016static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
3017{
3018 if (rtl_readphy(tp, reg) != val)
3019 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
3020 else
3021 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00003022}
3023
françois romieu4da19632011-01-03 15:07:55 +00003024static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003026 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00003027 { 0x1f, 0x0001 },
3028 { 0x06, 0x006e },
3029 { 0x08, 0x0708 },
3030 { 0x15, 0x4000 },
3031 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
françois romieu0b9b5712009-08-10 19:44:56 +00003033 { 0x1f, 0x0001 },
3034 { 0x03, 0x00a1 },
3035 { 0x02, 0x0008 },
3036 { 0x01, 0x0120 },
3037 { 0x00, 0x1000 },
3038 { 0x04, 0x0800 },
3039 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
françois romieu0b9b5712009-08-10 19:44:56 +00003041 { 0x03, 0xff41 },
3042 { 0x02, 0xdf60 },
3043 { 0x01, 0x0140 },
3044 { 0x00, 0x0077 },
3045 { 0x04, 0x7800 },
3046 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
françois romieu0b9b5712009-08-10 19:44:56 +00003048 { 0x03, 0x802f },
3049 { 0x02, 0x4f02 },
3050 { 0x01, 0x0409 },
3051 { 0x00, 0xf0f9 },
3052 { 0x04, 0x9800 },
3053 { 0x04, 0x9000 },
3054
3055 { 0x03, 0xdf01 },
3056 { 0x02, 0xdf20 },
3057 { 0x01, 0xff95 },
3058 { 0x00, 0xba00 },
3059 { 0x04, 0xa800 },
3060 { 0x04, 0xa000 },
3061
3062 { 0x03, 0xff41 },
3063 { 0x02, 0xdf20 },
3064 { 0x01, 0x0140 },
3065 { 0x00, 0x00bb },
3066 { 0x04, 0xb800 },
3067 { 0x04, 0xb000 },
3068
3069 { 0x03, 0xdf41 },
3070 { 0x02, 0xdc60 },
3071 { 0x01, 0x6340 },
3072 { 0x00, 0x007d },
3073 { 0x04, 0xd800 },
3074 { 0x04, 0xd000 },
3075
3076 { 0x03, 0xdf01 },
3077 { 0x02, 0xdf20 },
3078 { 0x01, 0x100a },
3079 { 0x00, 0xa0ff },
3080 { 0x04, 0xf800 },
3081 { 0x04, 0xf000 },
3082
3083 { 0x1f, 0x0000 },
3084 { 0x0b, 0x0000 },
3085 { 0x00, 0x9200 }
3086 };
3087
françois romieu4da19632011-01-03 15:07:55 +00003088 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
françois romieu4da19632011-01-03 15:07:55 +00003091static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02003092{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003093 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02003094 { 0x1f, 0x0002 },
3095 { 0x01, 0x90d0 },
3096 { 0x1f, 0x0000 }
3097 };
3098
françois romieu4da19632011-01-03 15:07:55 +00003099 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02003100}
3101
françois romieu4da19632011-01-03 15:07:55 +00003102static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003103{
3104 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00003105
Sergei Shtylyovccbae552011-07-22 05:37:24 +00003106 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3107 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00003108 return;
3109
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl_writephy(tp, 0x1f, 0x0001);
3111 rtl_writephy(tp, 0x10, 0xf01b);
3112 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00003113}
3114
françois romieu4da19632011-01-03 15:07:55 +00003115static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00003116{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003117 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00003118 { 0x1f, 0x0001 },
3119 { 0x04, 0x0000 },
3120 { 0x03, 0x00a1 },
3121 { 0x02, 0x0008 },
3122 { 0x01, 0x0120 },
3123 { 0x00, 0x1000 },
3124 { 0x04, 0x0800 },
3125 { 0x04, 0x9000 },
3126 { 0x03, 0x802f },
3127 { 0x02, 0x4f02 },
3128 { 0x01, 0x0409 },
3129 { 0x00, 0xf099 },
3130 { 0x04, 0x9800 },
3131 { 0x04, 0xa000 },
3132 { 0x03, 0xdf01 },
3133 { 0x02, 0xdf20 },
3134 { 0x01, 0xff95 },
3135 { 0x00, 0xba00 },
3136 { 0x04, 0xa800 },
3137 { 0x04, 0xf000 },
3138 { 0x03, 0xdf01 },
3139 { 0x02, 0xdf20 },
3140 { 0x01, 0x101a },
3141 { 0x00, 0xa0ff },
3142 { 0x04, 0xf800 },
3143 { 0x04, 0x0000 },
3144 { 0x1f, 0x0000 },
3145
3146 { 0x1f, 0x0001 },
3147 { 0x10, 0xf41b },
3148 { 0x14, 0xfb54 },
3149 { 0x18, 0xf5c7 },
3150 { 0x1f, 0x0000 },
3151
3152 { 0x1f, 0x0001 },
3153 { 0x17, 0x0cc0 },
3154 { 0x1f, 0x0000 }
3155 };
3156
françois romieu4da19632011-01-03 15:07:55 +00003157 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00003158
françois romieu4da19632011-01-03 15:07:55 +00003159 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003160}
3161
françois romieu4da19632011-01-03 15:07:55 +00003162static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00003163{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003164 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00003165 { 0x1f, 0x0001 },
3166 { 0x04, 0x0000 },
3167 { 0x03, 0x00a1 },
3168 { 0x02, 0x0008 },
3169 { 0x01, 0x0120 },
3170 { 0x00, 0x1000 },
3171 { 0x04, 0x0800 },
3172 { 0x04, 0x9000 },
3173 { 0x03, 0x802f },
3174 { 0x02, 0x4f02 },
3175 { 0x01, 0x0409 },
3176 { 0x00, 0xf099 },
3177 { 0x04, 0x9800 },
3178 { 0x04, 0xa000 },
3179 { 0x03, 0xdf01 },
3180 { 0x02, 0xdf20 },
3181 { 0x01, 0xff95 },
3182 { 0x00, 0xba00 },
3183 { 0x04, 0xa800 },
3184 { 0x04, 0xf000 },
3185 { 0x03, 0xdf01 },
3186 { 0x02, 0xdf20 },
3187 { 0x01, 0x101a },
3188 { 0x00, 0xa0ff },
3189 { 0x04, 0xf800 },
3190 { 0x04, 0x0000 },
3191 { 0x1f, 0x0000 },
3192
3193 { 0x1f, 0x0001 },
3194 { 0x0b, 0x8480 },
3195 { 0x1f, 0x0000 },
3196
3197 { 0x1f, 0x0001 },
3198 { 0x18, 0x67c7 },
3199 { 0x04, 0x2000 },
3200 { 0x03, 0x002f },
3201 { 0x02, 0x4360 },
3202 { 0x01, 0x0109 },
3203 { 0x00, 0x3022 },
3204 { 0x04, 0x2800 },
3205 { 0x1f, 0x0000 },
3206
3207 { 0x1f, 0x0001 },
3208 { 0x17, 0x0cc0 },
3209 { 0x1f, 0x0000 }
3210 };
3211
françois romieu4da19632011-01-03 15:07:55 +00003212 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00003213}
3214
françois romieu4da19632011-01-03 15:07:55 +00003215static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003216{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003217 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003218 { 0x10, 0xf41b },
3219 { 0x1f, 0x0000 }
3220 };
3221
françois romieu4da19632011-01-03 15:07:55 +00003222 rtl_writephy(tp, 0x1f, 0x0001);
3223 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02003224
françois romieu4da19632011-01-03 15:07:55 +00003225 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003226}
3227
françois romieu4da19632011-01-03 15:07:55 +00003228static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02003229{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003230 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02003231 { 0x1f, 0x0001 },
3232 { 0x10, 0xf41b },
3233 { 0x1f, 0x0000 }
3234 };
3235
françois romieu4da19632011-01-03 15:07:55 +00003236 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02003237}
3238
françois romieu4da19632011-01-03 15:07:55 +00003239static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003240{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003241 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02003242 { 0x1f, 0x0000 },
3243 { 0x1d, 0x0f00 },
3244 { 0x1f, 0x0002 },
3245 { 0x0c, 0x1ec8 },
3246 { 0x1f, 0x0000 }
3247 };
3248
françois romieu4da19632011-01-03 15:07:55 +00003249 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02003250}
3251
françois romieu4da19632011-01-03 15:07:55 +00003252static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02003253{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003254 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02003255 { 0x1f, 0x0001 },
3256 { 0x1d, 0x3d98 },
3257 { 0x1f, 0x0000 }
3258 };
3259
françois romieu4da19632011-01-03 15:07:55 +00003260 rtl_writephy(tp, 0x1f, 0x0000);
3261 rtl_patchphy(tp, 0x14, 1 << 5);
3262 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02003263
françois romieu4da19632011-01-03 15:07:55 +00003264 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02003265}
3266
françois romieu4da19632011-01-03 15:07:55 +00003267static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02003268{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003269 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02003270 { 0x1f, 0x0001 },
3271 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02003272 { 0x1f, 0x0002 },
3273 { 0x00, 0x88d4 },
3274 { 0x01, 0x82b1 },
3275 { 0x03, 0x7002 },
3276 { 0x08, 0x9e30 },
3277 { 0x09, 0x01f0 },
3278 { 0x0a, 0x5500 },
3279 { 0x0c, 0x00c8 },
3280 { 0x1f, 0x0003 },
3281 { 0x12, 0xc096 },
3282 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02003283 { 0x1f, 0x0000 },
3284 { 0x1f, 0x0000 },
3285 { 0x09, 0x2000 },
3286 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02003287 };
3288
françois romieu4da19632011-01-03 15:07:55 +00003289 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003290
françois romieu4da19632011-01-03 15:07:55 +00003291 rtl_patchphy(tp, 0x14, 1 << 5);
3292 rtl_patchphy(tp, 0x0d, 1 << 5);
3293 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02003294}
3295
françois romieu4da19632011-01-03 15:07:55 +00003296static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02003297{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003298 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02003299 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003300 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003301 { 0x03, 0x802f },
3302 { 0x02, 0x4f02 },
3303 { 0x01, 0x0409 },
3304 { 0x00, 0xf099 },
3305 { 0x04, 0x9800 },
3306 { 0x04, 0x9000 },
3307 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003308 { 0x1f, 0x0002 },
3309 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02003310 { 0x06, 0x0761 },
3311 { 0x1f, 0x0003 },
3312 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02003313 { 0x1f, 0x0000 }
3314 };
3315
françois romieu4da19632011-01-03 15:07:55 +00003316 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02003317
françois romieu4da19632011-01-03 15:07:55 +00003318 rtl_patchphy(tp, 0x16, 1 << 0);
3319 rtl_patchphy(tp, 0x14, 1 << 5);
3320 rtl_patchphy(tp, 0x0d, 1 << 5);
3321 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003322}
3323
françois romieu4da19632011-01-03 15:07:55 +00003324static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02003325{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003326 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02003327 { 0x1f, 0x0001 },
3328 { 0x12, 0x2300 },
3329 { 0x1d, 0x3d98 },
3330 { 0x1f, 0x0002 },
3331 { 0x0c, 0x7eb8 },
3332 { 0x06, 0x5461 },
3333 { 0x1f, 0x0003 },
3334 { 0x16, 0x0f0a },
3335 { 0x1f, 0x0000 }
3336 };
3337
françois romieu4da19632011-01-03 15:07:55 +00003338 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02003339
françois romieu4da19632011-01-03 15:07:55 +00003340 rtl_patchphy(tp, 0x16, 1 << 0);
3341 rtl_patchphy(tp, 0x14, 1 << 5);
3342 rtl_patchphy(tp, 0x0d, 1 << 5);
3343 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003344}
3345
françois romieu4da19632011-01-03 15:07:55 +00003346static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003347{
françois romieu4da19632011-01-03 15:07:55 +00003348 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003349}
3350
françois romieubca03d52011-01-03 15:07:31 +00003351static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003352{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003353 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003354 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003355 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003356 { 0x06, 0x4064 },
3357 { 0x07, 0x2863 },
3358 { 0x08, 0x059c },
3359 { 0x09, 0x26b4 },
3360 { 0x0a, 0x6a19 },
3361 { 0x0b, 0xdcc8 },
3362 { 0x10, 0xf06d },
3363 { 0x14, 0x7f68 },
3364 { 0x18, 0x7fd9 },
3365 { 0x1c, 0xf0ff },
3366 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003367 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003368 { 0x12, 0xf49f },
3369 { 0x13, 0x070b },
3370 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003371 { 0x14, 0x94c0 },
3372
3373 /*
3374 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003375 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003376 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003377 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003378 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003379 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003380 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003381 { 0x06, 0x5561 },
3382
3383 /*
3384 * Can not link to 1Gbps with bad cable
3385 * Decrease SNR threshold form 21.07dB to 19.04dB
3386 */
3387 { 0x1f, 0x0001 },
3388 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003389
3390 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003391 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003392 };
3393
françois romieu4da19632011-01-03 15:07:55 +00003394 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003395
françois romieubca03d52011-01-03 15:07:31 +00003396 /*
3397 * Rx Error Issue
3398 * Fine Tune Switching regulator parameter
3399 */
françois romieu4da19632011-01-03 15:07:55 +00003400 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003401 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3402 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003403
Francois Romieufdf6fc02012-07-06 22:40:38 +02003404 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003405 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003406 { 0x1f, 0x0002 },
3407 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003408 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003409 { 0x05, 0x8330 },
3410 { 0x06, 0x669a },
3411 { 0x1f, 0x0002 }
3412 };
3413 int val;
3414
françois romieu4da19632011-01-03 15:07:55 +00003415 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003416
françois romieu4da19632011-01-03 15:07:55 +00003417 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003418
3419 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003420 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003421 0x0065, 0x0066, 0x0067, 0x0068,
3422 0x0069, 0x006a, 0x006b, 0x006c
3423 };
3424 int i;
3425
françois romieu4da19632011-01-03 15:07:55 +00003426 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003427
3428 val &= 0xff00;
3429 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003430 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003431 }
3432 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003433 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003434 { 0x1f, 0x0002 },
3435 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003436 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003437 { 0x05, 0x8330 },
3438 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003439 };
3440
françois romieu4da19632011-01-03 15:07:55 +00003441 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003442 }
3443
françois romieubca03d52011-01-03 15:07:31 +00003444 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003445 rtl_writephy(tp, 0x1f, 0x0002);
3446 rtl_patchphy(tp, 0x0d, 0x0300);
3447 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003448
françois romieubca03d52011-01-03 15:07:31 +00003449 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003450 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003451 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3452 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003453
françois romieu4da19632011-01-03 15:07:55 +00003454 rtl_writephy(tp, 0x1f, 0x0005);
3455 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003456
3457 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003458
françois romieu4da19632011-01-03 15:07:55 +00003459 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003460}
3461
françois romieubca03d52011-01-03 15:07:31 +00003462static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003463{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003464 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003465 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003466 { 0x1f, 0x0001 },
3467 { 0x06, 0x4064 },
3468 { 0x07, 0x2863 },
3469 { 0x08, 0x059c },
3470 { 0x09, 0x26b4 },
3471 { 0x0a, 0x6a19 },
3472 { 0x0b, 0xdcc8 },
3473 { 0x10, 0xf06d },
3474 { 0x14, 0x7f68 },
3475 { 0x18, 0x7fd9 },
3476 { 0x1c, 0xf0ff },
3477 { 0x1d, 0x3d9c },
3478 { 0x1f, 0x0003 },
3479 { 0x12, 0xf49f },
3480 { 0x13, 0x070b },
3481 { 0x1a, 0x05ad },
3482 { 0x14, 0x94c0 },
3483
françois romieubca03d52011-01-03 15:07:31 +00003484 /*
3485 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003486 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003487 */
françois romieudaf9df62009-10-07 12:44:20 +00003488 { 0x1f, 0x0002 },
3489 { 0x06, 0x5561 },
3490 { 0x1f, 0x0005 },
3491 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003492 { 0x06, 0x5561 },
3493
3494 /*
3495 * Can not link to 1Gbps with bad cable
3496 * Decrease SNR threshold form 21.07dB to 19.04dB
3497 */
3498 { 0x1f, 0x0001 },
3499 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003500
3501 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003502 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003503 };
3504
françois romieu4da19632011-01-03 15:07:55 +00003505 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003506
Francois Romieufdf6fc02012-07-06 22:40:38 +02003507 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003508 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003509 { 0x1f, 0x0002 },
3510 { 0x05, 0x669a },
3511 { 0x1f, 0x0005 },
3512 { 0x05, 0x8330 },
3513 { 0x06, 0x669a },
3514
3515 { 0x1f, 0x0002 }
3516 };
3517 int val;
3518
françois romieu4da19632011-01-03 15:07:55 +00003519 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003520
françois romieu4da19632011-01-03 15:07:55 +00003521 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003522 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003523 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003524 0x0065, 0x0066, 0x0067, 0x0068,
3525 0x0069, 0x006a, 0x006b, 0x006c
3526 };
3527 int i;
3528
françois romieu4da19632011-01-03 15:07:55 +00003529 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003530
3531 val &= 0xff00;
3532 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003533 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003534 }
3535 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003536 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003537 { 0x1f, 0x0002 },
3538 { 0x05, 0x2642 },
3539 { 0x1f, 0x0005 },
3540 { 0x05, 0x8330 },
3541 { 0x06, 0x2642 }
3542 };
3543
françois romieu4da19632011-01-03 15:07:55 +00003544 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003545 }
3546
françois romieubca03d52011-01-03 15:07:31 +00003547 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003548 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003549 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3550 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003551
françois romieubca03d52011-01-03 15:07:31 +00003552 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003553 rtl_writephy(tp, 0x1f, 0x0002);
3554 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003555
françois romieu4da19632011-01-03 15:07:55 +00003556 rtl_writephy(tp, 0x1f, 0x0005);
3557 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003558
3559 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003560
françois romieu4da19632011-01-03 15:07:55 +00003561 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003562}
3563
françois romieu4da19632011-01-03 15:07:55 +00003564static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003565{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003566 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003567 { 0x1f, 0x0002 },
3568 { 0x10, 0x0008 },
3569 { 0x0d, 0x006c },
3570
3571 { 0x1f, 0x0000 },
3572 { 0x0d, 0xf880 },
3573
3574 { 0x1f, 0x0001 },
3575 { 0x17, 0x0cc0 },
3576
3577 { 0x1f, 0x0001 },
3578 { 0x0b, 0xa4d8 },
3579 { 0x09, 0x281c },
3580 { 0x07, 0x2883 },
3581 { 0x0a, 0x6b35 },
3582 { 0x1d, 0x3da4 },
3583 { 0x1c, 0xeffd },
3584 { 0x14, 0x7f52 },
3585 { 0x18, 0x7fc6 },
3586 { 0x08, 0x0601 },
3587 { 0x06, 0x4063 },
3588 { 0x10, 0xf074 },
3589 { 0x1f, 0x0003 },
3590 { 0x13, 0x0789 },
3591 { 0x12, 0xf4bd },
3592 { 0x1a, 0x04fd },
3593 { 0x14, 0x84b0 },
3594 { 0x1f, 0x0000 },
3595 { 0x00, 0x9200 },
3596
3597 { 0x1f, 0x0005 },
3598 { 0x01, 0x0340 },
3599 { 0x1f, 0x0001 },
3600 { 0x04, 0x4000 },
3601 { 0x03, 0x1d21 },
3602 { 0x02, 0x0c32 },
3603 { 0x01, 0x0200 },
3604 { 0x00, 0x5554 },
3605 { 0x04, 0x4800 },
3606 { 0x04, 0x4000 },
3607 { 0x04, 0xf000 },
3608 { 0x03, 0xdf01 },
3609 { 0x02, 0xdf20 },
3610 { 0x01, 0x101a },
3611 { 0x00, 0xa0ff },
3612 { 0x04, 0xf800 },
3613 { 0x04, 0xf000 },
3614 { 0x1f, 0x0000 },
3615
3616 { 0x1f, 0x0007 },
3617 { 0x1e, 0x0023 },
3618 { 0x16, 0x0000 },
3619 { 0x1f, 0x0000 }
3620 };
3621
françois romieu4da19632011-01-03 15:07:55 +00003622 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003623}
3624
françois romieue6de30d2011-01-03 15:08:37 +00003625static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3626{
3627 static const struct phy_reg phy_reg_init[] = {
3628 { 0x1f, 0x0001 },
3629 { 0x17, 0x0cc0 },
3630
3631 { 0x1f, 0x0007 },
3632 { 0x1e, 0x002d },
3633 { 0x18, 0x0040 },
3634 { 0x1f, 0x0000 }
3635 };
3636
3637 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3638 rtl_patchphy(tp, 0x0d, 1 << 5);
3639}
3640
Hayes Wang70090422011-07-06 15:58:06 +08003641static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003642{
3643 static const struct phy_reg phy_reg_init[] = {
3644 /* Enable Delay cap */
3645 { 0x1f, 0x0005 },
3646 { 0x05, 0x8b80 },
3647 { 0x06, 0xc896 },
3648 { 0x1f, 0x0000 },
3649
3650 /* Channel estimation fine tune */
3651 { 0x1f, 0x0001 },
3652 { 0x0b, 0x6c20 },
3653 { 0x07, 0x2872 },
3654 { 0x1c, 0xefff },
3655 { 0x1f, 0x0003 },
3656 { 0x14, 0x6420 },
3657 { 0x1f, 0x0000 },
3658
3659 /* Update PFM & 10M TX idle timer */
3660 { 0x1f, 0x0007 },
3661 { 0x1e, 0x002f },
3662 { 0x15, 0x1919 },
3663 { 0x1f, 0x0000 },
3664
3665 { 0x1f, 0x0007 },
3666 { 0x1e, 0x00ac },
3667 { 0x18, 0x0006 },
3668 { 0x1f, 0x0000 }
3669 };
3670
Francois Romieu15ecd032011-04-27 13:52:22 -07003671 rtl_apply_firmware(tp);
3672
hayeswang01dc7fe2011-03-21 01:50:28 +00003673 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3674
3675 /* DCO enable for 10M IDLE Power */
3676 rtl_writephy(tp, 0x1f, 0x0007);
3677 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003678 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003679 rtl_writephy(tp, 0x1f, 0x0000);
3680
3681 /* For impedance matching */
3682 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003684 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003685
3686 /* PHY auto speed down */
3687 rtl_writephy(tp, 0x1f, 0x0007);
3688 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003689 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003690 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003692
3693 rtl_writephy(tp, 0x1f, 0x0005);
3694 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003695 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003696 rtl_writephy(tp, 0x1f, 0x0000);
3697
3698 rtl_writephy(tp, 0x1f, 0x0005);
3699 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003700 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003701 rtl_writephy(tp, 0x1f, 0x0007);
3702 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003703 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003704 rtl_writephy(tp, 0x1f, 0x0006);
3705 rtl_writephy(tp, 0x00, 0x5a00);
3706 rtl_writephy(tp, 0x1f, 0x0000);
3707 rtl_writephy(tp, 0x0d, 0x0007);
3708 rtl_writephy(tp, 0x0e, 0x003c);
3709 rtl_writephy(tp, 0x0d, 0x4007);
3710 rtl_writephy(tp, 0x0e, 0x0000);
3711 rtl_writephy(tp, 0x0d, 0x0000);
3712}
3713
françois romieu9ecb9aa2012-12-07 11:20:21 +00003714static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3715{
3716 const u16 w[] = {
3717 addr[0] | (addr[1] << 8),
3718 addr[2] | (addr[3] << 8),
3719 addr[4] | (addr[5] << 8)
3720 };
3721 const struct exgmac_reg e[] = {
3722 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3723 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3724 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3725 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3726 };
3727
3728 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3729}
3730
Hayes Wang70090422011-07-06 15:58:06 +08003731static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3732{
3733 static const struct phy_reg phy_reg_init[] = {
3734 /* Enable Delay cap */
3735 { 0x1f, 0x0004 },
3736 { 0x1f, 0x0007 },
3737 { 0x1e, 0x00ac },
3738 { 0x18, 0x0006 },
3739 { 0x1f, 0x0002 },
3740 { 0x1f, 0x0000 },
3741 { 0x1f, 0x0000 },
3742
3743 /* Channel estimation fine tune */
3744 { 0x1f, 0x0003 },
3745 { 0x09, 0xa20f },
3746 { 0x1f, 0x0000 },
3747 { 0x1f, 0x0000 },
3748
3749 /* Green Setting */
3750 { 0x1f, 0x0005 },
3751 { 0x05, 0x8b5b },
3752 { 0x06, 0x9222 },
3753 { 0x05, 0x8b6d },
3754 { 0x06, 0x8000 },
3755 { 0x05, 0x8b76 },
3756 { 0x06, 0x8000 },
3757 { 0x1f, 0x0000 }
3758 };
3759
3760 rtl_apply_firmware(tp);
3761
3762 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3763
3764 /* For 4-corner performance improve */
3765 rtl_writephy(tp, 0x1f, 0x0005);
3766 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003767 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003768 rtl_writephy(tp, 0x1f, 0x0000);
3769
3770 /* PHY auto speed down */
3771 rtl_writephy(tp, 0x1f, 0x0004);
3772 rtl_writephy(tp, 0x1f, 0x0007);
3773 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003774 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003775 rtl_writephy(tp, 0x1f, 0x0002);
3776 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003777 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003778
3779 /* improve 10M EEE waveform */
3780 rtl_writephy(tp, 0x1f, 0x0005);
3781 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003782 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003783 rtl_writephy(tp, 0x1f, 0x0000);
3784
3785 /* Improve 2-pair detection performance */
3786 rtl_writephy(tp, 0x1f, 0x0005);
3787 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003788 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003789 rtl_writephy(tp, 0x1f, 0x0000);
3790
3791 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003792 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003793 rtl_writephy(tp, 0x1f, 0x0005);
3794 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003795 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003796 rtl_writephy(tp, 0x1f, 0x0004);
3797 rtl_writephy(tp, 0x1f, 0x0007);
3798 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003799 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003800 rtl_writephy(tp, 0x1f, 0x0002);
3801 rtl_writephy(tp, 0x1f, 0x0000);
3802 rtl_writephy(tp, 0x0d, 0x0007);
3803 rtl_writephy(tp, 0x0e, 0x003c);
3804 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003805 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003806 rtl_writephy(tp, 0x0d, 0x0000);
3807
3808 /* Green feature */
3809 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003810 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3811 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003812 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003813 rtl_writephy(tp, 0x1f, 0x0005);
3814 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3815 rtl_writephy(tp, 0x1f, 0x0000);
3816 /* soft-reset phy */
3817 rtl_writephy(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE | BMCR_ANRESTART);
hayeswange0c07552012-10-23 20:24:03 +00003818
françois romieu9ecb9aa2012-12-07 11:20:21 +00003819 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3820 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003821}
3822
Hayes Wang5f886e02012-03-30 14:33:03 +08003823static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3824{
3825 /* For 4-corner performance improve */
3826 rtl_writephy(tp, 0x1f, 0x0005);
3827 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003828 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003829 rtl_writephy(tp, 0x1f, 0x0000);
3830
3831 /* PHY auto speed down */
3832 rtl_writephy(tp, 0x1f, 0x0007);
3833 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003834 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003835 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003836 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003837
3838 /* Improve 10M EEE waveform */
3839 rtl_writephy(tp, 0x1f, 0x0005);
3840 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003841 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003842 rtl_writephy(tp, 0x1f, 0x0000);
3843}
3844
Hayes Wangc2218922011-09-06 16:55:18 +08003845static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3846{
3847 static const struct phy_reg phy_reg_init[] = {
3848 /* Channel estimation fine tune */
3849 { 0x1f, 0x0003 },
3850 { 0x09, 0xa20f },
3851 { 0x1f, 0x0000 },
3852
3853 /* Modify green table for giga & fnet */
3854 { 0x1f, 0x0005 },
3855 { 0x05, 0x8b55 },
3856 { 0x06, 0x0000 },
3857 { 0x05, 0x8b5e },
3858 { 0x06, 0x0000 },
3859 { 0x05, 0x8b67 },
3860 { 0x06, 0x0000 },
3861 { 0x05, 0x8b70 },
3862 { 0x06, 0x0000 },
3863 { 0x1f, 0x0000 },
3864 { 0x1f, 0x0007 },
3865 { 0x1e, 0x0078 },
3866 { 0x17, 0x0000 },
3867 { 0x19, 0x00fb },
3868 { 0x1f, 0x0000 },
3869
3870 /* Modify green table for 10M */
3871 { 0x1f, 0x0005 },
3872 { 0x05, 0x8b79 },
3873 { 0x06, 0xaa00 },
3874 { 0x1f, 0x0000 },
3875
3876 /* Disable hiimpedance detection (RTCT) */
3877 { 0x1f, 0x0003 },
3878 { 0x01, 0x328a },
3879 { 0x1f, 0x0000 }
3880 };
3881
3882 rtl_apply_firmware(tp);
3883
3884 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3885
Hayes Wang5f886e02012-03-30 14:33:03 +08003886 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003887
3888 /* Improve 2-pair detection performance */
3889 rtl_writephy(tp, 0x1f, 0x0005);
3890 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003891 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003892 rtl_writephy(tp, 0x1f, 0x0000);
3893}
3894
3895static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3896{
3897 rtl_apply_firmware(tp);
3898
Hayes Wang5f886e02012-03-30 14:33:03 +08003899 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003900}
3901
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003902static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3903{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003904 static const struct phy_reg phy_reg_init[] = {
3905 /* Channel estimation fine tune */
3906 { 0x1f, 0x0003 },
3907 { 0x09, 0xa20f },
3908 { 0x1f, 0x0000 },
3909
3910 /* Modify green table for giga & fnet */
3911 { 0x1f, 0x0005 },
3912 { 0x05, 0x8b55 },
3913 { 0x06, 0x0000 },
3914 { 0x05, 0x8b5e },
3915 { 0x06, 0x0000 },
3916 { 0x05, 0x8b67 },
3917 { 0x06, 0x0000 },
3918 { 0x05, 0x8b70 },
3919 { 0x06, 0x0000 },
3920 { 0x1f, 0x0000 },
3921 { 0x1f, 0x0007 },
3922 { 0x1e, 0x0078 },
3923 { 0x17, 0x0000 },
3924 { 0x19, 0x00aa },
3925 { 0x1f, 0x0000 },
3926
3927 /* Modify green table for 10M */
3928 { 0x1f, 0x0005 },
3929 { 0x05, 0x8b79 },
3930 { 0x06, 0xaa00 },
3931 { 0x1f, 0x0000 },
3932
3933 /* Disable hiimpedance detection (RTCT) */
3934 { 0x1f, 0x0003 },
3935 { 0x01, 0x328a },
3936 { 0x1f, 0x0000 }
3937 };
3938
3939
3940 rtl_apply_firmware(tp);
3941
3942 rtl8168f_hw_phy_config(tp);
3943
3944 /* Improve 2-pair detection performance */
3945 rtl_writephy(tp, 0x1f, 0x0005);
3946 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003947 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003948 rtl_writephy(tp, 0x1f, 0x0000);
3949
3950 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3951
3952 /* Modify green table for giga */
3953 rtl_writephy(tp, 0x1f, 0x0005);
3954 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003955 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003956 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003957 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003958 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003959 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003960 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003961 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003962 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003963 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003964 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003965 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003966 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003967 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003968 rtl_writephy(tp, 0x1f, 0x0000);
3969
3970 /* uc same-seed solution */
3971 rtl_writephy(tp, 0x1f, 0x0005);
3972 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003973 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003974 rtl_writephy(tp, 0x1f, 0x0000);
3975
3976 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003977 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003978 rtl_writephy(tp, 0x1f, 0x0005);
3979 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003980 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003981 rtl_writephy(tp, 0x1f, 0x0004);
3982 rtl_writephy(tp, 0x1f, 0x0007);
3983 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003984 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003985 rtl_writephy(tp, 0x1f, 0x0000);
3986 rtl_writephy(tp, 0x0d, 0x0007);
3987 rtl_writephy(tp, 0x0e, 0x003c);
3988 rtl_writephy(tp, 0x0d, 0x4007);
3989 rtl_writephy(tp, 0x0e, 0x0000);
3990 rtl_writephy(tp, 0x0d, 0x0000);
3991
3992 /* Green feature */
3993 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003994 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3995 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003996 rtl_writephy(tp, 0x1f, 0x0000);
3997}
3998
Hayes Wangc5583862012-07-02 17:23:22 +08003999static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
4000{
Hayes Wangc5583862012-07-02 17:23:22 +08004001 rtl_apply_firmware(tp);
4002
hayeswang41f44d12013-04-01 22:23:36 +00004003 rtl_writephy(tp, 0x1f, 0x0a46);
4004 if (rtl_readphy(tp, 0x10) & 0x0100) {
4005 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004006 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00004007 } else {
4008 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004009 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004010 }
Hayes Wangc5583862012-07-02 17:23:22 +08004011
hayeswang41f44d12013-04-01 22:23:36 +00004012 rtl_writephy(tp, 0x1f, 0x0a46);
4013 if (rtl_readphy(tp, 0x13) & 0x0100) {
4014 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004015 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004016 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00004017 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004018 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00004019 }
Hayes Wangc5583862012-07-02 17:23:22 +08004020
hayeswang41f44d12013-04-01 22:23:36 +00004021 /* Enable PHY auto speed down */
4022 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004023 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004024
hayeswangfe7524c2013-04-01 22:23:37 +00004025 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004026 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004027 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004028 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004029 rtl_writephy(tp, 0x1f, 0x0a43);
4030 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004031 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4032 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00004033
hayeswang41f44d12013-04-01 22:23:36 +00004034 /* EEE auto-fallback function */
4035 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004036 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004037
hayeswang41f44d12013-04-01 22:23:36 +00004038 /* Enable UC LPF tune function */
4039 rtl_writephy(tp, 0x1f, 0x0a43);
4040 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004041 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00004042
4043 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004044 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00004045
hayeswangfe7524c2013-04-01 22:23:37 +00004046 /* Improve SWR Efficiency */
4047 rtl_writephy(tp, 0x1f, 0x0bcd);
4048 rtl_writephy(tp, 0x14, 0x5065);
4049 rtl_writephy(tp, 0x14, 0xd065);
4050 rtl_writephy(tp, 0x1f, 0x0bc8);
4051 rtl_writephy(tp, 0x11, 0x5655);
4052 rtl_writephy(tp, 0x1f, 0x0bcd);
4053 rtl_writephy(tp, 0x14, 0x1065);
4054 rtl_writephy(tp, 0x14, 0x9065);
4055 rtl_writephy(tp, 0x14, 0x1065);
4056
David Chang1bac1072013-11-27 15:48:36 +08004057 /* Check ALDPS bit, disable it if enabled */
4058 rtl_writephy(tp, 0x1f, 0x0a43);
4059 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004060 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08004061
hayeswang41f44d12013-04-01 22:23:36 +00004062 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004063}
4064
hayeswang57538c42013-04-01 22:23:40 +00004065static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
4066{
4067 rtl_apply_firmware(tp);
4068}
4069
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004070static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
4071{
4072 u16 dout_tapbin;
4073 u32 data;
4074
4075 rtl_apply_firmware(tp);
4076
4077 /* CHN EST parameters adjust - giga master */
4078 rtl_writephy(tp, 0x1f, 0x0a43);
4079 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004080 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004081 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004082 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004083 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004084 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004085 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004086 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004087 rtl_writephy(tp, 0x1f, 0x0000);
4088
4089 /* CHN EST parameters adjust - giga slave */
4090 rtl_writephy(tp, 0x1f, 0x0a43);
4091 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004092 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004093 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004094 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004095 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004096 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004097 rtl_writephy(tp, 0x1f, 0x0000);
4098
4099 /* CHN EST parameters adjust - fnet */
4100 rtl_writephy(tp, 0x1f, 0x0a43);
4101 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004102 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004103 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004104 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004105 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004106 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004107 rtl_writephy(tp, 0x1f, 0x0000);
4108
4109 /* enable R-tune & PGA-retune function */
4110 dout_tapbin = 0;
4111 rtl_writephy(tp, 0x1f, 0x0a46);
4112 data = rtl_readphy(tp, 0x13);
4113 data &= 3;
4114 data <<= 2;
4115 dout_tapbin |= data;
4116 data = rtl_readphy(tp, 0x12);
4117 data &= 0xc000;
4118 data >>= 14;
4119 dout_tapbin |= data;
4120 dout_tapbin = ~(dout_tapbin^0x08);
4121 dout_tapbin <<= 12;
4122 dout_tapbin &= 0xf000;
4123 rtl_writephy(tp, 0x1f, 0x0a43);
4124 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004125 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004126 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004127 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004128 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004129 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004130 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004131 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004132
4133 rtl_writephy(tp, 0x1f, 0x0a43);
4134 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004135 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004136 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004137 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004138 rtl_writephy(tp, 0x1f, 0x0000);
4139
4140 /* enable GPHY 10M */
4141 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004142 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004143 rtl_writephy(tp, 0x1f, 0x0000);
4144
4145 /* SAR ADC performance */
4146 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004147 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004148 rtl_writephy(tp, 0x1f, 0x0000);
4149
4150 rtl_writephy(tp, 0x1f, 0x0a43);
4151 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004152 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004153 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004154 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004155 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004156 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004157 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004158 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004159 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004160 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004161 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004162 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004163 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004164 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004165 rtl_writephy(tp, 0x1f, 0x0000);
4166
4167 /* disable phy pfm mode */
4168 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004169 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004170 rtl_writephy(tp, 0x1f, 0x0000);
4171
4172 /* Check ALDPS bit, disable it if enabled */
4173 rtl_writephy(tp, 0x1f, 0x0a43);
4174 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004175 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004176
4177 rtl_writephy(tp, 0x1f, 0x0000);
4178}
4179
4180static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4181{
4182 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4183 u16 rlen;
4184 u32 data;
4185
4186 rtl_apply_firmware(tp);
4187
4188 /* CHIN EST parameter update */
4189 rtl_writephy(tp, 0x1f, 0x0a43);
4190 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004191 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004192 rtl_writephy(tp, 0x1f, 0x0000);
4193
4194 /* enable R-tune & PGA-retune function */
4195 rtl_writephy(tp, 0x1f, 0x0a43);
4196 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004197 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004198 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004199 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004200 rtl_writephy(tp, 0x1f, 0x0000);
4201
4202 /* enable GPHY 10M */
4203 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08004204 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004205 rtl_writephy(tp, 0x1f, 0x0000);
4206
4207 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4208 data = r8168_mac_ocp_read(tp, 0xdd02);
4209 ioffset_p3 = ((data & 0x80)>>7);
4210 ioffset_p3 <<= 3;
4211
4212 data = r8168_mac_ocp_read(tp, 0xdd00);
4213 ioffset_p3 |= ((data & (0xe000))>>13);
4214 ioffset_p2 = ((data & (0x1e00))>>9);
4215 ioffset_p1 = ((data & (0x01e0))>>5);
4216 ioffset_p0 = ((data & 0x0010)>>4);
4217 ioffset_p0 <<= 3;
4218 ioffset_p0 |= (data & (0x07));
4219 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4220
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004221 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08004222 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004223 rtl_writephy(tp, 0x1f, 0x0bcf);
4224 rtl_writephy(tp, 0x16, data);
4225 rtl_writephy(tp, 0x1f, 0x0000);
4226 }
4227
4228 /* Modify rlen (TX LPF corner frequency) level */
4229 rtl_writephy(tp, 0x1f, 0x0bcd);
4230 data = rtl_readphy(tp, 0x16);
4231 data &= 0x000f;
4232 rlen = 0;
4233 if (data > 3)
4234 rlen = data - 3;
4235 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4236 rtl_writephy(tp, 0x17, data);
4237 rtl_writephy(tp, 0x1f, 0x0bcd);
4238 rtl_writephy(tp, 0x1f, 0x0000);
4239
4240 /* disable phy pfm mode */
4241 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08004242 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004243 rtl_writephy(tp, 0x1f, 0x0000);
4244
4245 /* Check ALDPS bit, disable it if enabled */
4246 rtl_writephy(tp, 0x1f, 0x0a43);
4247 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08004248 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004249
4250 rtl_writephy(tp, 0x1f, 0x0000);
4251}
4252
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004253static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4254{
4255 /* Enable PHY auto speed down */
4256 rtl_writephy(tp, 0x1f, 0x0a44);
4257 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4258 rtl_writephy(tp, 0x1f, 0x0000);
4259
4260 /* patch 10M & ALDPS */
4261 rtl_writephy(tp, 0x1f, 0x0bcc);
4262 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4263 rtl_writephy(tp, 0x1f, 0x0a44);
4264 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4265 rtl_writephy(tp, 0x1f, 0x0a43);
4266 rtl_writephy(tp, 0x13, 0x8084);
4267 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4268 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4269 rtl_writephy(tp, 0x1f, 0x0000);
4270
4271 /* Enable EEE auto-fallback function */
4272 rtl_writephy(tp, 0x1f, 0x0a4b);
4273 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4274 rtl_writephy(tp, 0x1f, 0x0000);
4275
4276 /* Enable UC LPF tune function */
4277 rtl_writephy(tp, 0x1f, 0x0a43);
4278 rtl_writephy(tp, 0x13, 0x8012);
4279 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4280 rtl_writephy(tp, 0x1f, 0x0000);
4281
4282 /* set rg_sel_sdm_rate */
4283 rtl_writephy(tp, 0x1f, 0x0c42);
4284 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4285 rtl_writephy(tp, 0x1f, 0x0000);
4286
4287 /* Check ALDPS bit, disable it if enabled */
4288 rtl_writephy(tp, 0x1f, 0x0a43);
4289 if (rtl_readphy(tp, 0x10) & 0x0004)
4290 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4291
4292 rtl_writephy(tp, 0x1f, 0x0000);
4293}
4294
4295static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4296{
4297 /* patch 10M & ALDPS */
4298 rtl_writephy(tp, 0x1f, 0x0bcc);
4299 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4300 rtl_writephy(tp, 0x1f, 0x0a44);
4301 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4302 rtl_writephy(tp, 0x1f, 0x0a43);
4303 rtl_writephy(tp, 0x13, 0x8084);
4304 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4305 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4306 rtl_writephy(tp, 0x1f, 0x0000);
4307
4308 /* Enable UC LPF tune function */
4309 rtl_writephy(tp, 0x1f, 0x0a43);
4310 rtl_writephy(tp, 0x13, 0x8012);
4311 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4312 rtl_writephy(tp, 0x1f, 0x0000);
4313
4314 /* Set rg_sel_sdm_rate */
4315 rtl_writephy(tp, 0x1f, 0x0c42);
4316 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4317 rtl_writephy(tp, 0x1f, 0x0000);
4318
4319 /* Channel estimation parameters */
4320 rtl_writephy(tp, 0x1f, 0x0a43);
4321 rtl_writephy(tp, 0x13, 0x80f3);
4322 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4323 rtl_writephy(tp, 0x13, 0x80f0);
4324 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4325 rtl_writephy(tp, 0x13, 0x80ef);
4326 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4327 rtl_writephy(tp, 0x13, 0x80f6);
4328 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4329 rtl_writephy(tp, 0x13, 0x80ec);
4330 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4331 rtl_writephy(tp, 0x13, 0x80ed);
4332 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4333 rtl_writephy(tp, 0x13, 0x80f2);
4334 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4335 rtl_writephy(tp, 0x13, 0x80f4);
4336 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4337 rtl_writephy(tp, 0x1f, 0x0a43);
4338 rtl_writephy(tp, 0x13, 0x8110);
4339 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4340 rtl_writephy(tp, 0x13, 0x810f);
4341 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4342 rtl_writephy(tp, 0x13, 0x8111);
4343 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4344 rtl_writephy(tp, 0x13, 0x8113);
4345 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4346 rtl_writephy(tp, 0x13, 0x8115);
4347 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4348 rtl_writephy(tp, 0x13, 0x810e);
4349 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4350 rtl_writephy(tp, 0x13, 0x810c);
4351 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4352 rtl_writephy(tp, 0x13, 0x810b);
4353 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4354 rtl_writephy(tp, 0x1f, 0x0a43);
4355 rtl_writephy(tp, 0x13, 0x80d1);
4356 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4357 rtl_writephy(tp, 0x13, 0x80cd);
4358 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4359 rtl_writephy(tp, 0x13, 0x80d3);
4360 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4361 rtl_writephy(tp, 0x13, 0x80d5);
4362 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4363 rtl_writephy(tp, 0x13, 0x80d7);
4364 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4365
4366 /* Force PWM-mode */
4367 rtl_writephy(tp, 0x1f, 0x0bcd);
4368 rtl_writephy(tp, 0x14, 0x5065);
4369 rtl_writephy(tp, 0x14, 0xd065);
4370 rtl_writephy(tp, 0x1f, 0x0bc8);
4371 rtl_writephy(tp, 0x12, 0x00ed);
4372 rtl_writephy(tp, 0x1f, 0x0bcd);
4373 rtl_writephy(tp, 0x14, 0x1065);
4374 rtl_writephy(tp, 0x14, 0x9065);
4375 rtl_writephy(tp, 0x14, 0x1065);
4376 rtl_writephy(tp, 0x1f, 0x0000);
4377
4378 /* Check ALDPS bit, disable it if enabled */
4379 rtl_writephy(tp, 0x1f, 0x0a43);
4380 if (rtl_readphy(tp, 0x10) & 0x0004)
4381 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4382
4383 rtl_writephy(tp, 0x1f, 0x0000);
4384}
4385
françois romieu4da19632011-01-03 15:07:55 +00004386static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004387{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004388 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004389 { 0x1f, 0x0003 },
4390 { 0x08, 0x441d },
4391 { 0x01, 0x9100 },
4392 { 0x1f, 0x0000 }
4393 };
4394
françois romieu4da19632011-01-03 15:07:55 +00004395 rtl_writephy(tp, 0x1f, 0x0000);
4396 rtl_patchphy(tp, 0x11, 1 << 12);
4397 rtl_patchphy(tp, 0x19, 1 << 13);
4398 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004399
françois romieu4da19632011-01-03 15:07:55 +00004400 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004401}
4402
Hayes Wang5a5e4442011-02-22 17:26:21 +08004403static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4404{
4405 static const struct phy_reg phy_reg_init[] = {
4406 { 0x1f, 0x0005 },
4407 { 0x1a, 0x0000 },
4408 { 0x1f, 0x0000 },
4409
4410 { 0x1f, 0x0004 },
4411 { 0x1c, 0x0000 },
4412 { 0x1f, 0x0000 },
4413
4414 { 0x1f, 0x0001 },
4415 { 0x15, 0x7701 },
4416 { 0x1f, 0x0000 }
4417 };
4418
4419 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004420 rtl_writephy(tp, 0x1f, 0x0000);
4421 rtl_writephy(tp, 0x18, 0x0310);
4422 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004423
François Romieu953a12c2011-04-24 17:38:48 +02004424 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004425
4426 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4427}
4428
Hayes Wang7e18dca2012-03-30 14:33:02 +08004429static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4430{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004431 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004432 rtl_writephy(tp, 0x1f, 0x0000);
4433 rtl_writephy(tp, 0x18, 0x0310);
4434 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004435
4436 rtl_apply_firmware(tp);
4437
4438 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004439 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004440 rtl_writephy(tp, 0x1f, 0x0004);
4441 rtl_writephy(tp, 0x10, 0x401f);
4442 rtl_writephy(tp, 0x19, 0x7030);
4443 rtl_writephy(tp, 0x1f, 0x0000);
4444}
4445
Hayes Wang5598bfe2012-07-02 17:23:21 +08004446static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4447{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004448 static const struct phy_reg phy_reg_init[] = {
4449 { 0x1f, 0x0004 },
4450 { 0x10, 0xc07f },
4451 { 0x19, 0x7030 },
4452 { 0x1f, 0x0000 }
4453 };
4454
4455 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004456 rtl_writephy(tp, 0x1f, 0x0000);
4457 rtl_writephy(tp, 0x18, 0x0310);
4458 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004459
4460 rtl_apply_firmware(tp);
4461
Francois Romieufdf6fc02012-07-06 22:40:38 +02004462 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004463 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4464
Francois Romieufdf6fc02012-07-06 22:40:38 +02004465 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004466}
4467
Francois Romieu5615d9f2007-08-17 17:50:46 +02004468static void rtl_hw_phy_config(struct net_device *dev)
4469{
4470 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004471
4472 rtl8169_print_mac_version(tp);
4473
4474 switch (tp->mac_version) {
4475 case RTL_GIGA_MAC_VER_01:
4476 break;
4477 case RTL_GIGA_MAC_VER_02:
4478 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004479 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004480 break;
4481 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004482 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004483 break;
françois romieu2e9558562009-08-10 19:44:19 +00004484 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004485 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004486 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004487 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004488 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004489 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004490 case RTL_GIGA_MAC_VER_07:
4491 case RTL_GIGA_MAC_VER_08:
4492 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004493 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004494 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004495 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004496 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004497 break;
4498 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004499 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004500 break;
4501 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004502 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004503 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004504 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004505 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004506 break;
4507 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004508 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004509 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004510 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004511 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004512 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004513 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004514 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004515 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004516 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004517 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004518 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004519 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004520 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004521 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004522 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004523 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004524 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004525 break;
4526 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004527 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004528 break;
4529 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004530 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004531 break;
françois romieue6de30d2011-01-03 15:08:37 +00004532 case RTL_GIGA_MAC_VER_28:
4533 rtl8168d_4_hw_phy_config(tp);
4534 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004535 case RTL_GIGA_MAC_VER_29:
4536 case RTL_GIGA_MAC_VER_30:
4537 rtl8105e_hw_phy_config(tp);
4538 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004539 case RTL_GIGA_MAC_VER_31:
4540 /* None. */
4541 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004542 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004543 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004544 rtl8168e_1_hw_phy_config(tp);
4545 break;
4546 case RTL_GIGA_MAC_VER_34:
4547 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004548 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004549 case RTL_GIGA_MAC_VER_35:
4550 rtl8168f_1_hw_phy_config(tp);
4551 break;
4552 case RTL_GIGA_MAC_VER_36:
4553 rtl8168f_2_hw_phy_config(tp);
4554 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004555
Hayes Wang7e18dca2012-03-30 14:33:02 +08004556 case RTL_GIGA_MAC_VER_37:
4557 rtl8402_hw_phy_config(tp);
4558 break;
4559
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004560 case RTL_GIGA_MAC_VER_38:
4561 rtl8411_hw_phy_config(tp);
4562 break;
4563
Hayes Wang5598bfe2012-07-02 17:23:21 +08004564 case RTL_GIGA_MAC_VER_39:
4565 rtl8106e_hw_phy_config(tp);
4566 break;
4567
Hayes Wangc5583862012-07-02 17:23:22 +08004568 case RTL_GIGA_MAC_VER_40:
4569 rtl8168g_1_hw_phy_config(tp);
4570 break;
hayeswang57538c42013-04-01 22:23:40 +00004571 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004572 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004573 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004574 rtl8168g_2_hw_phy_config(tp);
4575 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004576 case RTL_GIGA_MAC_VER_45:
4577 case RTL_GIGA_MAC_VER_47:
4578 rtl8168h_1_hw_phy_config(tp);
4579 break;
4580 case RTL_GIGA_MAC_VER_46:
4581 case RTL_GIGA_MAC_VER_48:
4582 rtl8168h_2_hw_phy_config(tp);
4583 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004584
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004585 case RTL_GIGA_MAC_VER_49:
4586 rtl8168ep_1_hw_phy_config(tp);
4587 break;
4588 case RTL_GIGA_MAC_VER_50:
4589 case RTL_GIGA_MAC_VER_51:
4590 rtl8168ep_2_hw_phy_config(tp);
4591 break;
4592
Hayes Wangc5583862012-07-02 17:23:22 +08004593 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004594 default:
4595 break;
4596 }
4597}
4598
Francois Romieuda78dbf2012-01-26 14:18:23 +01004599static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 struct timer_list *timer = &tp->timer;
4602 void __iomem *ioaddr = tp->mmio_addr;
4603 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4604
Francois Romieubcf0bf92006-07-26 23:14:13 +02004605 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
françois romieu4da19632011-01-03 15:07:55 +00004607 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004608 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 * A busy loop could burn quite a few cycles on nowadays CPU.
4610 * Let's delay the execution of the timer for a few ticks.
4611 */
4612 timeout = HZ/10;
4613 goto out_mod_timer;
4614 }
4615
4616 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004617 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004619 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
françois romieu4da19632011-01-03 15:07:55 +00004621 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
4623out_mod_timer:
4624 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004625}
4626
4627static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4628{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004629 if (!test_and_set_bit(flag, tp->wk.flags))
4630 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004631}
4632
Kees Cook9de36cc2017-10-25 03:53:12 -07004633static void rtl8169_phy_timer(struct timer_list *t)
Francois Romieuda78dbf2012-01-26 14:18:23 +01004634{
Kees Cook9de36cc2017-10-25 03:53:12 -07004635 struct rtl8169_private *tp = from_timer(tp, t, timer);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004636
Francois Romieu98ddf982012-01-31 10:47:34 +01004637 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638}
4639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4641 void __iomem *ioaddr)
4642{
4643 iounmap(ioaddr);
4644 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004645 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 pci_disable_device(pdev);
4647 free_netdev(dev);
4648}
4649
Francois Romieuffc46952012-07-06 14:19:23 +02004650DECLARE_RTL_COND(rtl_phy_reset_cond)
4651{
4652 return tp->phy_reset_pending(tp);
4653}
4654
Francois Romieubf793292006-11-01 00:53:05 +01004655static void rtl8169_phy_reset(struct net_device *dev,
4656 struct rtl8169_private *tp)
4657{
françois romieu4da19632011-01-03 15:07:55 +00004658 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004659 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004660}
4661
David S. Miller8decf862011-09-22 03:23:13 -04004662static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4663{
4664 void __iomem *ioaddr = tp->mmio_addr;
4665
4666 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4667 (RTL_R8(PHYstatus) & TBI_Enable);
4668}
4669
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004670static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004672 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004673
Francois Romieu5615d9f2007-08-17 17:50:46 +02004674 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004675
Marcus Sundberg773328942008-07-10 21:28:08 +02004676 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4677 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4678 RTL_W8(0x82, 0x01);
4679 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004680
Francois Romieu6dccd162007-02-13 23:38:05 +01004681 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4682
4683 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4684 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004685
Francois Romieubcf0bf92006-07-26 23:14:13 +02004686 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004687 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4688 RTL_W8(0x82, 0x01);
4689 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004690 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004691 }
4692
Francois Romieubf793292006-11-01 00:53:05 +01004693 rtl8169_phy_reset(dev, tp);
4694
Oliver Neukum54405cd2011-01-06 21:55:13 +01004695 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004696 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4697 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4698 (tp->mii.supports_gmii ?
4699 ADVERTISED_1000baseT_Half |
4700 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004701
David S. Miller8decf862011-09-22 03:23:13 -04004702 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004703 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004704}
4705
Francois Romieu773d2022007-01-31 23:47:43 +01004706static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4707{
4708 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004709
Francois Romieuda78dbf2012-01-26 14:18:23 +01004710 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004711
4712 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004713
françois romieu9ecb9aa2012-12-07 11:20:21 +00004714 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004715 RTL_R32(MAC4);
4716
françois romieu9ecb9aa2012-12-07 11:20:21 +00004717 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004718 RTL_R32(MAC0);
4719
françois romieu9ecb9aa2012-12-07 11:20:21 +00004720 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4721 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004722
Francois Romieu773d2022007-01-31 23:47:43 +01004723 RTL_W8(Cfg9346, Cfg9346_Lock);
4724
Francois Romieuda78dbf2012-01-26 14:18:23 +01004725 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004726}
4727
4728static int rtl_set_mac_address(struct net_device *dev, void *p)
4729{
4730 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004731 struct device *d = &tp->pci_dev->dev;
Francois Romieu773d2022007-01-31 23:47:43 +01004732 struct sockaddr *addr = p;
4733
4734 if (!is_valid_ether_addr(addr->sa_data))
4735 return -EADDRNOTAVAIL;
4736
4737 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4738
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004739 pm_runtime_get_noresume(d);
4740
4741 if (pm_runtime_active(d))
4742 rtl_rar_set(tp, dev->dev_addr);
4743
4744 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004745
4746 return 0;
4747}
4748
Francois Romieu5f787a12006-08-17 13:02:36 +02004749static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4750{
4751 struct rtl8169_private *tp = netdev_priv(dev);
4752 struct mii_ioctl_data *data = if_mii(ifr);
4753
Francois Romieu8b4ab282008-11-19 22:05:25 -08004754 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4755}
Francois Romieu5f787a12006-08-17 13:02:36 +02004756
Francois Romieucecb5fd2011-04-01 10:21:07 +02004757static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4758 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004759{
Francois Romieu5f787a12006-08-17 13:02:36 +02004760 switch (cmd) {
4761 case SIOCGMIIPHY:
4762 data->phy_id = 32; /* Internal PHY */
4763 return 0;
4764
4765 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004766 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004767 return 0;
4768
4769 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004770 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004771 return 0;
4772 }
4773 return -EOPNOTSUPP;
4774}
4775
Francois Romieu8b4ab282008-11-19 22:05:25 -08004776static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4777{
4778 return -EOPNOTSUPP;
4779}
4780
Francois Romieufbac58f2007-10-04 22:51:38 +02004781static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4782{
4783 if (tp->features & RTL_FEATURE_MSI) {
4784 pci_disable_msi(pdev);
4785 tp->features &= ~RTL_FEATURE_MSI;
4786 }
4787}
4788
Bill Pembertonbaf63292012-12-03 09:23:28 -05004789static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004790{
4791 struct mdio_ops *ops = &tp->mdio_ops;
4792
4793 switch (tp->mac_version) {
4794 case RTL_GIGA_MAC_VER_27:
4795 ops->write = r8168dp_1_mdio_write;
4796 ops->read = r8168dp_1_mdio_read;
4797 break;
françois romieue6de30d2011-01-03 15:08:37 +00004798 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004799 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004800 ops->write = r8168dp_2_mdio_write;
4801 ops->read = r8168dp_2_mdio_read;
4802 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004803 case RTL_GIGA_MAC_VER_40:
4804 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004805 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004806 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004807 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004808 case RTL_GIGA_MAC_VER_45:
4809 case RTL_GIGA_MAC_VER_46:
4810 case RTL_GIGA_MAC_VER_47:
4811 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004812 case RTL_GIGA_MAC_VER_49:
4813 case RTL_GIGA_MAC_VER_50:
4814 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004815 ops->write = r8168g_mdio_write;
4816 ops->read = r8168g_mdio_read;
4817 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004818 default:
4819 ops->write = r8169_mdio_write;
4820 ops->read = r8169_mdio_read;
4821 break;
4822 }
4823}
4824
hayeswange2409d82013-03-31 17:02:04 +00004825static void rtl_speed_down(struct rtl8169_private *tp)
4826{
4827 u32 adv;
4828 int lpa;
4829
4830 rtl_writephy(tp, 0x1f, 0x0000);
4831 lpa = rtl_readphy(tp, MII_LPA);
4832
4833 if (lpa & (LPA_10HALF | LPA_10FULL))
4834 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4835 else if (lpa & (LPA_100HALF | LPA_100FULL))
4836 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4837 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4838 else
4839 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4840 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4841 (tp->mii.supports_gmii ?
4842 ADVERTISED_1000baseT_Half |
4843 ADVERTISED_1000baseT_Full : 0);
4844
4845 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4846 adv);
4847}
4848
David S. Miller1805b2f2011-10-24 18:18:09 -04004849static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4850{
4851 void __iomem *ioaddr = tp->mmio_addr;
4852
4853 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004854 case RTL_GIGA_MAC_VER_25:
4855 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004856 case RTL_GIGA_MAC_VER_29:
4857 case RTL_GIGA_MAC_VER_30:
4858 case RTL_GIGA_MAC_VER_32:
4859 case RTL_GIGA_MAC_VER_33:
4860 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004861 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004862 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004863 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004864 case RTL_GIGA_MAC_VER_40:
4865 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004866 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004867 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004868 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004869 case RTL_GIGA_MAC_VER_45:
4870 case RTL_GIGA_MAC_VER_46:
4871 case RTL_GIGA_MAC_VER_47:
4872 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004873 case RTL_GIGA_MAC_VER_49:
4874 case RTL_GIGA_MAC_VER_50:
4875 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004876 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4877 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4878 break;
4879 default:
4880 break;
4881 }
4882}
4883
4884static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4885{
4886 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4887 return false;
4888
hayeswange2409d82013-03-31 17:02:04 +00004889 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004890 rtl_wol_suspend_quirk(tp);
4891
4892 return true;
4893}
4894
françois romieu065c27c2011-01-03 15:08:12 +00004895static void r810x_phy_power_down(struct rtl8169_private *tp)
4896{
4897 rtl_writephy(tp, 0x1f, 0x0000);
4898 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4899}
4900
4901static void r810x_phy_power_up(struct rtl8169_private *tp)
4902{
4903 rtl_writephy(tp, 0x1f, 0x0000);
4904 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4905}
4906
4907static void r810x_pll_power_down(struct rtl8169_private *tp)
4908{
Hayes Wang00042992012-03-30 14:33:00 +08004909 void __iomem *ioaddr = tp->mmio_addr;
4910
David S. Miller1805b2f2011-10-24 18:18:09 -04004911 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004912 return;
françois romieu065c27c2011-01-03 15:08:12 +00004913
4914 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004915
4916 switch (tp->mac_version) {
4917 case RTL_GIGA_MAC_VER_07:
4918 case RTL_GIGA_MAC_VER_08:
4919 case RTL_GIGA_MAC_VER_09:
4920 case RTL_GIGA_MAC_VER_10:
4921 case RTL_GIGA_MAC_VER_13:
4922 case RTL_GIGA_MAC_VER_16:
4923 break;
4924 default:
4925 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4926 break;
4927 }
françois romieu065c27c2011-01-03 15:08:12 +00004928}
4929
4930static void r810x_pll_power_up(struct rtl8169_private *tp)
4931{
Hayes Wang00042992012-03-30 14:33:00 +08004932 void __iomem *ioaddr = tp->mmio_addr;
4933
françois romieu065c27c2011-01-03 15:08:12 +00004934 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004935
4936 switch (tp->mac_version) {
4937 case RTL_GIGA_MAC_VER_07:
4938 case RTL_GIGA_MAC_VER_08:
4939 case RTL_GIGA_MAC_VER_09:
4940 case RTL_GIGA_MAC_VER_10:
4941 case RTL_GIGA_MAC_VER_13:
4942 case RTL_GIGA_MAC_VER_16:
4943 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004944 case RTL_GIGA_MAC_VER_47:
4945 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004946 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004947 break;
Hayes Wang00042992012-03-30 14:33:00 +08004948 default:
4949 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4950 break;
4951 }
françois romieu065c27c2011-01-03 15:08:12 +00004952}
4953
4954static void r8168_phy_power_up(struct rtl8169_private *tp)
4955{
4956 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004957 switch (tp->mac_version) {
4958 case RTL_GIGA_MAC_VER_11:
4959 case RTL_GIGA_MAC_VER_12:
4960 case RTL_GIGA_MAC_VER_17:
4961 case RTL_GIGA_MAC_VER_18:
4962 case RTL_GIGA_MAC_VER_19:
4963 case RTL_GIGA_MAC_VER_20:
4964 case RTL_GIGA_MAC_VER_21:
4965 case RTL_GIGA_MAC_VER_22:
4966 case RTL_GIGA_MAC_VER_23:
4967 case RTL_GIGA_MAC_VER_24:
4968 case RTL_GIGA_MAC_VER_25:
4969 case RTL_GIGA_MAC_VER_26:
4970 case RTL_GIGA_MAC_VER_27:
4971 case RTL_GIGA_MAC_VER_28:
4972 case RTL_GIGA_MAC_VER_31:
4973 rtl_writephy(tp, 0x0e, 0x0000);
4974 break;
4975 default:
4976 break;
4977 }
françois romieu065c27c2011-01-03 15:08:12 +00004978 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4979}
4980
4981static void r8168_phy_power_down(struct rtl8169_private *tp)
4982{
4983 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004984 switch (tp->mac_version) {
4985 case RTL_GIGA_MAC_VER_32:
4986 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004987 case RTL_GIGA_MAC_VER_40:
4988 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004989 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4990 break;
4991
4992 case RTL_GIGA_MAC_VER_11:
4993 case RTL_GIGA_MAC_VER_12:
4994 case RTL_GIGA_MAC_VER_17:
4995 case RTL_GIGA_MAC_VER_18:
4996 case RTL_GIGA_MAC_VER_19:
4997 case RTL_GIGA_MAC_VER_20:
4998 case RTL_GIGA_MAC_VER_21:
4999 case RTL_GIGA_MAC_VER_22:
5000 case RTL_GIGA_MAC_VER_23:
5001 case RTL_GIGA_MAC_VER_24:
5002 case RTL_GIGA_MAC_VER_25:
5003 case RTL_GIGA_MAC_VER_26:
5004 case RTL_GIGA_MAC_VER_27:
5005 case RTL_GIGA_MAC_VER_28:
5006 case RTL_GIGA_MAC_VER_31:
5007 rtl_writephy(tp, 0x0e, 0x0200);
5008 default:
5009 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
5010 break;
5011 }
françois romieu065c27c2011-01-03 15:08:12 +00005012}
5013
5014static void r8168_pll_power_down(struct rtl8169_private *tp)
5015{
5016 void __iomem *ioaddr = tp->mmio_addr;
5017
Francois Romieucecb5fd2011-04-01 10:21:07 +02005018 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
5019 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005020 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
5021 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5022 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5023 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08005024 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00005025 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08005026 }
françois romieu065c27c2011-01-03 15:08:12 +00005027
Francois Romieucecb5fd2011-04-01 10:21:07 +02005028 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
5029 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00005030 (RTL_R16(CPlusCmd) & ASF)) {
5031 return;
5032 }
5033
hayeswang01dc7fe2011-03-21 01:50:28 +00005034 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
5035 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02005036 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00005037
David S. Miller1805b2f2011-10-24 18:18:09 -04005038 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00005039 return;
françois romieu065c27c2011-01-03 15:08:12 +00005040
5041 r8168_phy_power_down(tp);
5042
5043 switch (tp->mac_version) {
5044 case RTL_GIGA_MAC_VER_25:
5045 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005046 case RTL_GIGA_MAC_VER_27:
5047 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005048 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005049 case RTL_GIGA_MAC_VER_32:
5050 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005051 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005052 case RTL_GIGA_MAC_VER_45:
5053 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005054 case RTL_GIGA_MAC_VER_50:
5055 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005056 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5057 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005058 case RTL_GIGA_MAC_VER_40:
5059 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005060 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005061 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005062 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005063 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00005064 break;
françois romieu065c27c2011-01-03 15:08:12 +00005065 }
5066}
5067
5068static void r8168_pll_power_up(struct rtl8169_private *tp)
5069{
5070 void __iomem *ioaddr = tp->mmio_addr;
5071
françois romieu065c27c2011-01-03 15:08:12 +00005072 switch (tp->mac_version) {
5073 case RTL_GIGA_MAC_VER_25:
5074 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08005075 case RTL_GIGA_MAC_VER_27:
5076 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005077 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005078 case RTL_GIGA_MAC_VER_32:
5079 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00005080 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5081 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08005082 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005083 case RTL_GIGA_MAC_VER_45:
5084 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005085 case RTL_GIGA_MAC_VER_50:
5086 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005087 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005088 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005089 case RTL_GIGA_MAC_VER_40:
5090 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005091 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08005092 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005093 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00005094 0x00000000, ERIAR_EXGMAC);
5095 break;
françois romieu065c27c2011-01-03 15:08:12 +00005096 }
5097
5098 r8168_phy_power_up(tp);
5099}
5100
Francois Romieud58d46b2011-05-03 16:38:29 +02005101static void rtl_generic_op(struct rtl8169_private *tp,
5102 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00005103{
5104 if (op)
5105 op(tp);
5106}
5107
5108static void rtl_pll_power_down(struct rtl8169_private *tp)
5109{
Francois Romieud58d46b2011-05-03 16:38:29 +02005110 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00005111}
5112
5113static void rtl_pll_power_up(struct rtl8169_private *tp)
5114{
Francois Romieud58d46b2011-05-03 16:38:29 +02005115 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00005116}
5117
Bill Pembertonbaf63292012-12-03 09:23:28 -05005118static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00005119{
5120 struct pll_power_ops *ops = &tp->pll_power_ops;
5121
5122 switch (tp->mac_version) {
5123 case RTL_GIGA_MAC_VER_07:
5124 case RTL_GIGA_MAC_VER_08:
5125 case RTL_GIGA_MAC_VER_09:
5126 case RTL_GIGA_MAC_VER_10:
5127 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08005128 case RTL_GIGA_MAC_VER_29:
5129 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005130 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08005131 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00005132 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005133 case RTL_GIGA_MAC_VER_47:
5134 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00005135 ops->down = r810x_pll_power_down;
5136 ops->up = r810x_pll_power_up;
5137 break;
5138
5139 case RTL_GIGA_MAC_VER_11:
5140 case RTL_GIGA_MAC_VER_12:
5141 case RTL_GIGA_MAC_VER_17:
5142 case RTL_GIGA_MAC_VER_18:
5143 case RTL_GIGA_MAC_VER_19:
5144 case RTL_GIGA_MAC_VER_20:
5145 case RTL_GIGA_MAC_VER_21:
5146 case RTL_GIGA_MAC_VER_22:
5147 case RTL_GIGA_MAC_VER_23:
5148 case RTL_GIGA_MAC_VER_24:
5149 case RTL_GIGA_MAC_VER_25:
5150 case RTL_GIGA_MAC_VER_26:
5151 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00005152 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005153 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00005154 case RTL_GIGA_MAC_VER_32:
5155 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005156 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08005157 case RTL_GIGA_MAC_VER_35:
5158 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005159 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08005160 case RTL_GIGA_MAC_VER_40:
5161 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005162 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08005163 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005164 case RTL_GIGA_MAC_VER_45:
5165 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005166 case RTL_GIGA_MAC_VER_49:
5167 case RTL_GIGA_MAC_VER_50:
5168 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00005169 ops->down = r8168_pll_power_down;
5170 ops->up = r8168_pll_power_up;
5171 break;
5172
5173 default:
5174 ops->down = NULL;
5175 ops->up = NULL;
5176 break;
5177 }
5178}
5179
Hayes Wange542a222011-07-06 15:58:04 +08005180static void rtl_init_rxcfg(struct rtl8169_private *tp)
5181{
5182 void __iomem *ioaddr = tp->mmio_addr;
5183
5184 switch (tp->mac_version) {
5185 case RTL_GIGA_MAC_VER_01:
5186 case RTL_GIGA_MAC_VER_02:
5187 case RTL_GIGA_MAC_VER_03:
5188 case RTL_GIGA_MAC_VER_04:
5189 case RTL_GIGA_MAC_VER_05:
5190 case RTL_GIGA_MAC_VER_06:
5191 case RTL_GIGA_MAC_VER_10:
5192 case RTL_GIGA_MAC_VER_11:
5193 case RTL_GIGA_MAC_VER_12:
5194 case RTL_GIGA_MAC_VER_13:
5195 case RTL_GIGA_MAC_VER_14:
5196 case RTL_GIGA_MAC_VER_15:
5197 case RTL_GIGA_MAC_VER_16:
5198 case RTL_GIGA_MAC_VER_17:
5199 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5200 break;
5201 case RTL_GIGA_MAC_VER_18:
5202 case RTL_GIGA_MAC_VER_19:
5203 case RTL_GIGA_MAC_VER_20:
5204 case RTL_GIGA_MAC_VER_21:
5205 case RTL_GIGA_MAC_VER_22:
5206 case RTL_GIGA_MAC_VER_23:
5207 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00005208 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02005209 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08005210 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5211 break;
hayeswangbeb330a2013-04-01 22:23:39 +00005212 case RTL_GIGA_MAC_VER_40:
5213 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005214 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005215 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005216 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005217 case RTL_GIGA_MAC_VER_45:
5218 case RTL_GIGA_MAC_VER_46:
5219 case RTL_GIGA_MAC_VER_47:
5220 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005221 case RTL_GIGA_MAC_VER_49:
5222 case RTL_GIGA_MAC_VER_50:
5223 case RTL_GIGA_MAC_VER_51:
Ivan Vecera7ebc4822015-08-04 22:11:43 +02005224 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00005225 break;
Hayes Wange542a222011-07-06 15:58:04 +08005226 default:
5227 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5228 break;
5229 }
5230}
5231
Hayes Wang92fc43b2011-07-06 15:58:03 +08005232static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5233{
Timo Teräs9fba0812013-01-15 21:01:24 +00005234 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005235}
5236
Francois Romieud58d46b2011-05-03 16:38:29 +02005237static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5238{
françois romieu9c5028e2012-03-02 04:43:14 +00005239 void __iomem *ioaddr = tp->mmio_addr;
5240
5241 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005242 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00005243 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005244}
5245
5246static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5247{
françois romieu9c5028e2012-03-02 04:43:14 +00005248 void __iomem *ioaddr = tp->mmio_addr;
5249
5250 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005251 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00005252 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02005253}
5254
5255static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5256{
5257 void __iomem *ioaddr = tp->mmio_addr;
5258
5259 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5260 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005261 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005262}
5263
5264static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5265{
5266 void __iomem *ioaddr = tp->mmio_addr;
5267
5268 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5269 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5270 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5271}
5272
5273static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5274{
5275 void __iomem *ioaddr = tp->mmio_addr;
5276
5277 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5278}
5279
5280static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5281{
5282 void __iomem *ioaddr = tp->mmio_addr;
5283
5284 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5285}
5286
5287static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5288{
5289 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005290
5291 RTL_W8(MaxTxPacketSize, 0x3f);
5292 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5293 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005294 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02005295}
5296
5297static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5298{
5299 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02005300
5301 RTL_W8(MaxTxPacketSize, 0x0c);
5302 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5303 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01005304 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02005305}
5306
5307static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5308{
5309 rtl_tx_performance_tweak(tp->pci_dev,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01005310 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02005311}
5312
5313static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5314{
5315 rtl_tx_performance_tweak(tp->pci_dev,
5316 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5317}
5318
5319static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5320{
5321 void __iomem *ioaddr = tp->mmio_addr;
5322
5323 r8168b_0_hw_jumbo_enable(tp);
5324
5325 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5326}
5327
5328static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5329{
5330 void __iomem *ioaddr = tp->mmio_addr;
5331
5332 r8168b_0_hw_jumbo_disable(tp);
5333
5334 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5335}
5336
Bill Pembertonbaf63292012-12-03 09:23:28 -05005337static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02005338{
5339 struct jumbo_ops *ops = &tp->jumbo_ops;
5340
5341 switch (tp->mac_version) {
5342 case RTL_GIGA_MAC_VER_11:
5343 ops->disable = r8168b_0_hw_jumbo_disable;
5344 ops->enable = r8168b_0_hw_jumbo_enable;
5345 break;
5346 case RTL_GIGA_MAC_VER_12:
5347 case RTL_GIGA_MAC_VER_17:
5348 ops->disable = r8168b_1_hw_jumbo_disable;
5349 ops->enable = r8168b_1_hw_jumbo_enable;
5350 break;
5351 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5352 case RTL_GIGA_MAC_VER_19:
5353 case RTL_GIGA_MAC_VER_20:
5354 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5355 case RTL_GIGA_MAC_VER_22:
5356 case RTL_GIGA_MAC_VER_23:
5357 case RTL_GIGA_MAC_VER_24:
5358 case RTL_GIGA_MAC_VER_25:
5359 case RTL_GIGA_MAC_VER_26:
5360 ops->disable = r8168c_hw_jumbo_disable;
5361 ops->enable = r8168c_hw_jumbo_enable;
5362 break;
5363 case RTL_GIGA_MAC_VER_27:
5364 case RTL_GIGA_MAC_VER_28:
5365 ops->disable = r8168dp_hw_jumbo_disable;
5366 ops->enable = r8168dp_hw_jumbo_enable;
5367 break;
5368 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5369 case RTL_GIGA_MAC_VER_32:
5370 case RTL_GIGA_MAC_VER_33:
5371 case RTL_GIGA_MAC_VER_34:
5372 ops->disable = r8168e_hw_jumbo_disable;
5373 ops->enable = r8168e_hw_jumbo_enable;
5374 break;
5375
5376 /*
5377 * No action needed for jumbo frames with 8169.
5378 * No jumbo for 810x at all.
5379 */
Hayes Wangc5583862012-07-02 17:23:22 +08005380 case RTL_GIGA_MAC_VER_40:
5381 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005382 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005383 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005384 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005385 case RTL_GIGA_MAC_VER_45:
5386 case RTL_GIGA_MAC_VER_46:
5387 case RTL_GIGA_MAC_VER_47:
5388 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005389 case RTL_GIGA_MAC_VER_49:
5390 case RTL_GIGA_MAC_VER_50:
5391 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005392 default:
5393 ops->disable = NULL;
5394 ops->enable = NULL;
5395 break;
5396 }
5397}
5398
Francois Romieuffc46952012-07-06 14:19:23 +02005399DECLARE_RTL_COND(rtl_chipcmd_cond)
5400{
5401 void __iomem *ioaddr = tp->mmio_addr;
5402
5403 return RTL_R8(ChipCmd) & CmdReset;
5404}
5405
Francois Romieu6f43adc2011-04-29 15:05:51 +02005406static void rtl_hw_reset(struct rtl8169_private *tp)
5407{
5408 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005409
Francois Romieu6f43adc2011-04-29 15:05:51 +02005410 RTL_W8(ChipCmd, CmdReset);
5411
Francois Romieuffc46952012-07-06 14:19:23 +02005412 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005413}
5414
Francois Romieub6ffd972011-06-17 17:00:05 +02005415static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5416{
5417 struct rtl_fw *rtl_fw;
5418 const char *name;
5419 int rc = -ENOMEM;
5420
5421 name = rtl_lookup_firmware_name(tp);
5422 if (!name)
5423 goto out_no_firmware;
5424
5425 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5426 if (!rtl_fw)
5427 goto err_warn;
5428
5429 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5430 if (rc < 0)
5431 goto err_free;
5432
Francois Romieufd112f22011-06-18 00:10:29 +02005433 rc = rtl_check_firmware(tp, rtl_fw);
5434 if (rc < 0)
5435 goto err_release_firmware;
5436
Francois Romieub6ffd972011-06-17 17:00:05 +02005437 tp->rtl_fw = rtl_fw;
5438out:
5439 return;
5440
Francois Romieufd112f22011-06-18 00:10:29 +02005441err_release_firmware:
5442 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005443err_free:
5444 kfree(rtl_fw);
5445err_warn:
5446 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5447 name, rc);
5448out_no_firmware:
5449 tp->rtl_fw = NULL;
5450 goto out;
5451}
5452
François Romieu953a12c2011-04-24 17:38:48 +02005453static void rtl_request_firmware(struct rtl8169_private *tp)
5454{
Francois Romieub6ffd972011-06-17 17:00:05 +02005455 if (IS_ERR(tp->rtl_fw))
5456 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005457}
5458
Hayes Wang92fc43b2011-07-06 15:58:03 +08005459static void rtl_rx_close(struct rtl8169_private *tp)
5460{
5461 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005462
Francois Romieu1687b562011-07-19 17:21:29 +02005463 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005464}
5465
Francois Romieuffc46952012-07-06 14:19:23 +02005466DECLARE_RTL_COND(rtl_npq_cond)
5467{
5468 void __iomem *ioaddr = tp->mmio_addr;
5469
5470 return RTL_R8(TxPoll) & NPQ;
5471}
5472
5473DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5474{
5475 void __iomem *ioaddr = tp->mmio_addr;
5476
5477 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5478}
5479
françois romieue6de30d2011-01-03 15:08:37 +00005480static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481{
françois romieue6de30d2011-01-03 15:08:37 +00005482 void __iomem *ioaddr = tp->mmio_addr;
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005485 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Hayes Wang92fc43b2011-07-06 15:58:03 +08005487 rtl_rx_close(tp);
5488
Hayes Wang5d2e1952011-02-22 17:26:22 +08005489 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005490 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5491 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005492 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005493 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005494 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5495 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5496 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5497 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5498 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5499 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5500 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5501 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5502 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5503 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5504 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5505 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005506 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5507 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5508 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5509 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005510 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005511 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005512 } else {
5513 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5514 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005515 }
5516
Hayes Wang92fc43b2011-07-06 15:58:03 +08005517 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
5519
Francois Romieu7f796d832007-06-11 23:04:41 +02005520static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005521{
5522 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005523
5524 /* Set DMA burst size and Interframe Gap Time */
5525 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5526 (InterFrameGap << TxInterFrameGapShift));
5527}
5528
Francois Romieu07ce4062007-02-23 23:36:39 +01005529static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
5531 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Francois Romieu07ce4062007-02-23 23:36:39 +01005533 tp->hw_start(dev);
5534
Francois Romieuda78dbf2012-01-26 14:18:23 +01005535 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005536}
5537
Francois Romieu7f796d832007-06-11 23:04:41 +02005538static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5539 void __iomem *ioaddr)
5540{
5541 /*
5542 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5543 * register to be written before TxDescAddrLow to work.
5544 * Switching from MMIO to I/O access fixes the issue as well.
5545 */
5546 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005547 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005548 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005549 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005550}
5551
5552static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5553{
5554 u16 cmd;
5555
5556 cmd = RTL_R16(CPlusCmd);
5557 RTL_W16(CPlusCmd, cmd);
5558 return cmd;
5559}
5560
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005561static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005562{
5563 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005564 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005565}
5566
Francois Romieu6dccd162007-02-13 23:38:05 +01005567static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5568{
Francois Romieu37441002011-06-17 22:58:54 +02005569 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005570 u32 mac_version;
5571 u32 clk;
5572 u32 val;
5573 } cfg2_info [] = {
5574 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5575 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5576 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5577 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005578 };
5579 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005580 unsigned int i;
5581 u32 clk;
5582
5583 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005584 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005585 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5586 RTL_W32(0x7c, p->val);
5587 break;
5588 }
5589 }
5590}
5591
Francois Romieue6b763e2012-03-08 09:35:39 +01005592static void rtl_set_rx_mode(struct net_device *dev)
5593{
5594 struct rtl8169_private *tp = netdev_priv(dev);
5595 void __iomem *ioaddr = tp->mmio_addr;
5596 u32 mc_filter[2]; /* Multicast hash filter */
5597 int rx_mode;
5598 u32 tmp = 0;
5599
5600 if (dev->flags & IFF_PROMISC) {
5601 /* Unconditionally log net taps. */
5602 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5603 rx_mode =
5604 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5605 AcceptAllPhys;
5606 mc_filter[1] = mc_filter[0] = 0xffffffff;
5607 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5608 (dev->flags & IFF_ALLMULTI)) {
5609 /* Too many to filter perfectly -- accept all multicasts. */
5610 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5611 mc_filter[1] = mc_filter[0] = 0xffffffff;
5612 } else {
5613 struct netdev_hw_addr *ha;
5614
5615 rx_mode = AcceptBroadcast | AcceptMyPhys;
5616 mc_filter[1] = mc_filter[0] = 0;
5617 netdev_for_each_mc_addr(ha, dev) {
5618 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5619 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5620 rx_mode |= AcceptMulticast;
5621 }
5622 }
5623
5624 if (dev->features & NETIF_F_RXALL)
5625 rx_mode |= (AcceptErr | AcceptRunt);
5626
5627 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5628
5629 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5630 u32 data = mc_filter[0];
5631
5632 mc_filter[0] = swab32(mc_filter[1]);
5633 mc_filter[1] = swab32(data);
5634 }
5635
Nathan Walp04817762012-11-01 12:08:47 +00005636 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5637 mc_filter[1] = mc_filter[0] = 0xffffffff;
5638
Francois Romieue6b763e2012-03-08 09:35:39 +01005639 RTL_W32(MAR0 + 4, mc_filter[1]);
5640 RTL_W32(MAR0 + 0, mc_filter[0]);
5641
5642 RTL_W32(RxConfig, tmp);
5643}
5644
Francois Romieu07ce4062007-02-23 23:36:39 +01005645static void rtl_hw_start_8169(struct net_device *dev)
5646{
5647 struct rtl8169_private *tp = netdev_priv(dev);
5648 void __iomem *ioaddr = tp->mmio_addr;
5649 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005650
Francois Romieu9cb427b2006-11-02 00:10:16 +01005651 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5652 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5653 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5654 }
5655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005657 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5658 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5659 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5660 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005661 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5662
Hayes Wange542a222011-07-06 15:58:04 +08005663 rtl_init_rxcfg(tp);
5664
françois romieuf0298f82011-01-03 15:07:42 +00005665 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005667 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
Francois Romieucecb5fd2011-04-01 10:21:07 +02005669 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5670 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5671 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5672 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005673 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Francois Romieu7f796d832007-06-11 23:04:41 +02005675 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005676
Francois Romieucecb5fd2011-04-01 10:21:07 +02005677 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5678 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005679 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005681 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 }
5683
Francois Romieubcf0bf92006-07-26 23:14:13 +02005684 RTL_W16(CPlusCmd, tp->cp_cmd);
5685
Francois Romieu6dccd162007-02-13 23:38:05 +01005686 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 /*
5689 * Undocumented corner. Supposedly:
5690 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5691 */
5692 RTL_W16(IntrMitigate, 0x0000);
5693
Francois Romieu7f796d832007-06-11 23:04:41 +02005694 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005695
Francois Romieucecb5fd2011-04-01 10:21:07 +02005696 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5697 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5698 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5699 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005700 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5701 rtl_set_rx_tx_config_registers(tp);
5702 }
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005705
5706 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5707 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
5709 RTL_W32(RxMissed, 0);
5710
Francois Romieu07ce4062007-02-23 23:36:39 +01005711 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
5713 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005714 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005717static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5718{
5719 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005720 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005721}
5722
5723static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5724{
Francois Romieu52989f02012-07-06 13:37:00 +02005725 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005726}
5727
5728static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005729{
5730 u32 csi;
5731
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005732 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5733 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005734}
5735
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005736static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005737{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005738 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005739}
5740
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005741static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005742{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005743 rtl_csi_access_enable(tp, 0x27000000);
5744}
5745
Francois Romieuffc46952012-07-06 14:19:23 +02005746DECLARE_RTL_COND(rtl_csiar_cond)
5747{
5748 void __iomem *ioaddr = tp->mmio_addr;
5749
5750 return RTL_R32(CSIAR) & CSIAR_FLAG;
5751}
5752
Francois Romieu52989f02012-07-06 13:37:00 +02005753static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005754{
Francois Romieu52989f02012-07-06 13:37:00 +02005755 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005756
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
Francois Romieuffc46952012-07-06 14:19:23 +02005761 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005762}
5763
Francois Romieu52989f02012-07-06 13:37:00 +02005764static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005765{
Francois Romieu52989f02012-07-06 13:37:00 +02005766 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005767
5768 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5769 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5770
Francois Romieuffc46952012-07-06 14:19:23 +02005771 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5772 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005773}
5774
Francois Romieu52989f02012-07-06 13:37:00 +02005775static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005776{
Francois Romieu52989f02012-07-06 13:37:00 +02005777 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005778
5779 RTL_W32(CSIDR, value);
5780 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5781 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5782 CSIAR_FUNC_NIC);
5783
Francois Romieuffc46952012-07-06 14:19:23 +02005784 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005785}
5786
Francois Romieu52989f02012-07-06 13:37:00 +02005787static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005788{
Francois Romieu52989f02012-07-06 13:37:00 +02005789 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005790
5791 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5792 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5793
Francois Romieuffc46952012-07-06 14:19:23 +02005794 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5795 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005796}
5797
hayeswang45dd95c2013-07-08 17:09:01 +08005798static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5799{
5800 void __iomem *ioaddr = tp->mmio_addr;
5801
5802 RTL_W32(CSIDR, value);
5803 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5804 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5805 CSIAR_FUNC_NIC2);
5806
5807 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5808}
5809
5810static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5811{
5812 void __iomem *ioaddr = tp->mmio_addr;
5813
5814 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5815 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5816
5817 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5818 RTL_R32(CSIDR) : ~0;
5819}
5820
Bill Pembertonbaf63292012-12-03 09:23:28 -05005821static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005822{
5823 struct csi_ops *ops = &tp->csi_ops;
5824
5825 switch (tp->mac_version) {
5826 case RTL_GIGA_MAC_VER_01:
5827 case RTL_GIGA_MAC_VER_02:
5828 case RTL_GIGA_MAC_VER_03:
5829 case RTL_GIGA_MAC_VER_04:
5830 case RTL_GIGA_MAC_VER_05:
5831 case RTL_GIGA_MAC_VER_06:
5832 case RTL_GIGA_MAC_VER_10:
5833 case RTL_GIGA_MAC_VER_11:
5834 case RTL_GIGA_MAC_VER_12:
5835 case RTL_GIGA_MAC_VER_13:
5836 case RTL_GIGA_MAC_VER_14:
5837 case RTL_GIGA_MAC_VER_15:
5838 case RTL_GIGA_MAC_VER_16:
5839 case RTL_GIGA_MAC_VER_17:
5840 ops->write = NULL;
5841 ops->read = NULL;
5842 break;
5843
Hayes Wang7e18dca2012-03-30 14:33:02 +08005844 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005845 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005846 ops->write = r8402_csi_write;
5847 ops->read = r8402_csi_read;
5848 break;
5849
hayeswang45dd95c2013-07-08 17:09:01 +08005850 case RTL_GIGA_MAC_VER_44:
5851 ops->write = r8411_csi_write;
5852 ops->read = r8411_csi_read;
5853 break;
5854
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005855 default:
5856 ops->write = r8169_csi_write;
5857 ops->read = r8169_csi_read;
5858 break;
5859 }
Francois Romieudacf8152008-08-02 20:44:13 +02005860}
5861
5862struct ephy_info {
5863 unsigned int offset;
5864 u16 mask;
5865 u16 bits;
5866};
5867
Francois Romieufdf6fc02012-07-06 22:40:38 +02005868static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5869 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005870{
5871 u16 w;
5872
5873 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005874 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5875 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005876 e++;
5877 }
5878}
5879
Francois Romieub726e492008-06-28 12:22:59 +02005880static void rtl_disable_clock_request(struct pci_dev *pdev)
5881{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005882 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5883 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005884}
5885
françois romieue6de30d2011-01-03 15:08:37 +00005886static void rtl_enable_clock_request(struct pci_dev *pdev)
5887{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005888 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5889 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005890}
5891
hayeswangb51ecea2014-07-09 14:52:51 +08005892static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5893{
5894 void __iomem *ioaddr = tp->mmio_addr;
5895 u8 data;
5896
5897 data = RTL_R8(Config3);
5898
5899 if (enable)
5900 data |= Rdy_to_L23;
5901 else
5902 data &= ~Rdy_to_L23;
5903
5904 RTL_W8(Config3, data);
5905}
5906
Francois Romieub726e492008-06-28 12:22:59 +02005907#define R8168_CPCMD_QUIRK_MASK (\
5908 EnableBist | \
5909 Mac_dbgo_oe | \
5910 Force_half_dup | \
5911 Force_rxflow_en | \
5912 Force_txflow_en | \
5913 Cxpl_dbg_sel | \
5914 ASF | \
5915 PktCntrDisable | \
5916 Mac_dbgo_sel)
5917
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005918static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005919{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005920 void __iomem *ioaddr = tp->mmio_addr;
5921 struct pci_dev *pdev = tp->pci_dev;
5922
Francois Romieub726e492008-06-28 12:22:59 +02005923 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5924
5925 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5926
françois romieufaf1e782013-02-27 13:01:57 +00005927 if (tp->dev->mtu <= ETH_DATA_LEN) {
5928 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5929 PCI_EXP_DEVCTL_NOSNOOP_EN);
5930 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005931}
5932
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005933static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005934{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005935 void __iomem *ioaddr = tp->mmio_addr;
5936
5937 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005938
françois romieuf0298f82011-01-03 15:07:42 +00005939 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005940
5941 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005942}
5943
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005944static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005945{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005946 void __iomem *ioaddr = tp->mmio_addr;
5947 struct pci_dev *pdev = tp->pci_dev;
5948
Francois Romieub726e492008-06-28 12:22:59 +02005949 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5950
5951 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5952
françois romieufaf1e782013-02-27 13:01:57 +00005953 if (tp->dev->mtu <= ETH_DATA_LEN)
5954 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005955
5956 rtl_disable_clock_request(pdev);
5957
5958 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005959}
5960
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005961static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005962{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005963 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005964 { 0x01, 0, 0x0001 },
5965 { 0x02, 0x0800, 0x1000 },
5966 { 0x03, 0, 0x0042 },
5967 { 0x06, 0x0080, 0x0000 },
5968 { 0x07, 0, 0x2000 }
5969 };
5970
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005971 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005972
Francois Romieufdf6fc02012-07-06 22:40:38 +02005973 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005974
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005975 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005976}
5977
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005978static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005979{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005980 void __iomem *ioaddr = tp->mmio_addr;
5981 struct pci_dev *pdev = tp->pci_dev;
5982
5983 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005984
5985 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5986
françois romieufaf1e782013-02-27 13:01:57 +00005987 if (tp->dev->mtu <= ETH_DATA_LEN)
5988 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005989
5990 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5991}
5992
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005993static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005994{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005995 void __iomem *ioaddr = tp->mmio_addr;
5996 struct pci_dev *pdev = tp->pci_dev;
5997
5998 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005999
6000 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6001
6002 /* Magic. */
6003 RTL_W8(DBG_REG, 0x20);
6004
françois romieuf0298f82011-01-03 15:07:42 +00006005 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006006
françois romieufaf1e782013-02-27 13:01:57 +00006007 if (tp->dev->mtu <= ETH_DATA_LEN)
6008 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006009
6010 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6011}
6012
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006013static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02006014{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006015 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006016 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02006017 { 0x02, 0x0800, 0x1000 },
6018 { 0x03, 0, 0x0002 },
6019 { 0x06, 0x0080, 0x0000 }
6020 };
6021
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006022 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02006023
6024 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
6025
Francois Romieufdf6fc02012-07-06 22:40:38 +02006026 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02006027
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006028 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006029}
6030
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006031static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02006032{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006033 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02006034 { 0x01, 0, 0x0001 },
6035 { 0x03, 0x0400, 0x0220 }
6036 };
6037
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006038 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02006039
Francois Romieufdf6fc02012-07-06 22:40:38 +02006040 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02006041
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006042 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02006043}
6044
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006045static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02006046{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006047 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02006048}
6049
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006050static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02006051{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006052 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006053
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006054 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02006055}
6056
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006057static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02006058{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006059 void __iomem *ioaddr = tp->mmio_addr;
6060 struct pci_dev *pdev = tp->pci_dev;
6061
6062 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02006063
6064 rtl_disable_clock_request(pdev);
6065
françois romieuf0298f82011-01-03 15:07:42 +00006066 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02006067
françois romieufaf1e782013-02-27 13:01:57 +00006068 if (tp->dev->mtu <= ETH_DATA_LEN)
6069 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02006070
6071 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6072}
6073
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006074static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00006075{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006076 void __iomem *ioaddr = tp->mmio_addr;
6077 struct pci_dev *pdev = tp->pci_dev;
6078
6079 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006080
françois romieufaf1e782013-02-27 13:01:57 +00006081 if (tp->dev->mtu <= ETH_DATA_LEN)
6082 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00006083
6084 RTL_W8(MaxTxPacketSize, TxPacketMax);
6085
6086 rtl_disable_clock_request(pdev);
6087}
6088
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006089static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00006090{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006091 void __iomem *ioaddr = tp->mmio_addr;
6092 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00006093 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006094 { 0x0b, 0x0000, 0x0048 },
6095 { 0x19, 0x0020, 0x0050 },
6096 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00006097 };
françois romieue6de30d2011-01-03 15:08:37 +00006098
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006099 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00006100
6101 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6102
6103 RTL_W8(MaxTxPacketSize, TxPacketMax);
6104
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08006105 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00006106
6107 rtl_enable_clock_request(pdev);
6108}
6109
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006110static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00006111{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006112 void __iomem *ioaddr = tp->mmio_addr;
6113 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006114 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00006115 { 0x00, 0x0200, 0x0100 },
6116 { 0x00, 0x0000, 0x0004 },
6117 { 0x06, 0x0002, 0x0001 },
6118 { 0x06, 0x0000, 0x0030 },
6119 { 0x07, 0x0000, 0x2000 },
6120 { 0x00, 0x0000, 0x0020 },
6121 { 0x03, 0x5800, 0x2000 },
6122 { 0x03, 0x0000, 0x0001 },
6123 { 0x01, 0x0800, 0x1000 },
6124 { 0x07, 0x0000, 0x4000 },
6125 { 0x1e, 0x0000, 0x2000 },
6126 { 0x19, 0xffff, 0xfe6c },
6127 { 0x0a, 0x0000, 0x0040 }
6128 };
6129
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006130 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006131
Francois Romieufdf6fc02012-07-06 22:40:38 +02006132 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00006133
françois romieufaf1e782013-02-27 13:01:57 +00006134 if (tp->dev->mtu <= ETH_DATA_LEN)
6135 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00006136
6137 RTL_W8(MaxTxPacketSize, TxPacketMax);
6138
6139 rtl_disable_clock_request(pdev);
6140
6141 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02006142 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6143 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00006144
Francois Romieucecb5fd2011-04-01 10:21:07 +02006145 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00006146}
6147
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006148static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08006149{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006150 void __iomem *ioaddr = tp->mmio_addr;
6151 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08006152 static const struct ephy_info e_info_8168e_2[] = {
6153 { 0x09, 0x0000, 0x0080 },
6154 { 0x19, 0x0000, 0x0224 }
6155 };
6156
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006157 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006158
Francois Romieufdf6fc02012-07-06 22:40:38 +02006159 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08006160
françois romieufaf1e782013-02-27 13:01:57 +00006161 if (tp->dev->mtu <= ETH_DATA_LEN)
6162 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08006163
Francois Romieufdf6fc02012-07-06 22:40:38 +02006164 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6165 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6166 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6167 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6168 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6169 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006170 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6171 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08006172
Hayes Wang3090bd92011-09-06 16:55:15 +08006173 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08006174
Francois Romieu4521e1a92012-11-01 16:46:28 +00006175 rtl_disable_clock_request(pdev);
6176
Hayes Wang70090422011-07-06 15:58:06 +08006177 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6178 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6179
6180 /* Adjust EEE LED frequency */
6181 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6182
6183 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6184 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006185 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08006186}
6187
Hayes Wang5f886e02012-03-30 14:33:03 +08006188static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08006189{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006190 void __iomem *ioaddr = tp->mmio_addr;
6191 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08006192
Hayes Wang5f886e02012-03-30 14:33:03 +08006193 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006194
6195 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6196
Francois Romieufdf6fc02012-07-06 22:40:38 +02006197 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6198 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6199 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6200 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006201 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6202 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6203 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6204 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006205 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6206 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08006207
6208 RTL_W8(MaxTxPacketSize, EarlySize);
6209
Francois Romieu4521e1a92012-11-01 16:46:28 +00006210 rtl_disable_clock_request(pdev);
6211
Hayes Wangc2218922011-09-06 16:55:18 +08006212 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6213 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08006214 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00006215 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6216 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08006217}
6218
Hayes Wang5f886e02012-03-30 14:33:03 +08006219static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6220{
6221 void __iomem *ioaddr = tp->mmio_addr;
6222 static const struct ephy_info e_info_8168f_1[] = {
6223 { 0x06, 0x00c0, 0x0020 },
6224 { 0x08, 0x0001, 0x0002 },
6225 { 0x09, 0x0000, 0x0080 },
6226 { 0x19, 0x0000, 0x0224 }
6227 };
6228
6229 rtl_hw_start_8168f(tp);
6230
Francois Romieufdf6fc02012-07-06 22:40:38 +02006231 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08006232
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006233 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08006234
6235 /* Adjust EEE LED frequency */
6236 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6237}
6238
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006239static void rtl_hw_start_8411(struct rtl8169_private *tp)
6240{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006241 static const struct ephy_info e_info_8168f_1[] = {
6242 { 0x06, 0x00c0, 0x0020 },
6243 { 0x0f, 0xffff, 0x5200 },
6244 { 0x1e, 0x0000, 0x4000 },
6245 { 0x19, 0x0000, 0x0224 }
6246 };
6247
6248 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08006249 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006250
Francois Romieufdf6fc02012-07-06 22:40:38 +02006251 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006252
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006253 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006254}
6255
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006256static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006257{
6258 void __iomem *ioaddr = tp->mmio_addr;
6259 struct pci_dev *pdev = tp->pci_dev;
6260
hayeswangbeb330a2013-04-01 22:23:39 +00006261 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6262
Hayes Wangc5583862012-07-02 17:23:22 +08006263 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6264 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6265 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6266 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6267
6268 rtl_csi_access_enable_1(tp);
6269
6270 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6271
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006272 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6273 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00006274 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08006275
Francois Romieu4521e1a92012-11-01 16:46:28 +00006276 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006277 RTL_W8(MaxTxPacketSize, EarlySize);
6278
6279 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6280 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6281
6282 /* Adjust EEE LED frequency */
6283 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6284
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006285 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6286 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006287
6288 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08006289}
6290
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006291static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6292{
6293 void __iomem *ioaddr = tp->mmio_addr;
6294 static const struct ephy_info e_info_8168g_1[] = {
6295 { 0x00, 0x0000, 0x0008 },
6296 { 0x0c, 0x37d0, 0x0820 },
6297 { 0x1e, 0x0000, 0x0001 },
6298 { 0x19, 0x8000, 0x0000 }
6299 };
6300
6301 rtl_hw_start_8168g(tp);
6302
6303 /* disable aspm and clock request before access ephy */
6304 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6305 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6306 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6307}
6308
hayeswang57538c42013-04-01 22:23:40 +00006309static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6310{
6311 void __iomem *ioaddr = tp->mmio_addr;
6312 static const struct ephy_info e_info_8168g_2[] = {
6313 { 0x00, 0x0000, 0x0008 },
6314 { 0x0c, 0x3df0, 0x0200 },
6315 { 0x19, 0xffff, 0xfc00 },
6316 { 0x1e, 0xffff, 0x20eb }
6317 };
6318
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006319 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00006320
6321 /* disable aspm and clock request before access ephy */
6322 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6323 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6324 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6325}
6326
hayeswang45dd95c2013-07-08 17:09:01 +08006327static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6328{
6329 void __iomem *ioaddr = tp->mmio_addr;
6330 static const struct ephy_info e_info_8411_2[] = {
6331 { 0x00, 0x0000, 0x0008 },
6332 { 0x0c, 0x3df0, 0x0200 },
6333 { 0x0f, 0xffff, 0x5200 },
6334 { 0x19, 0x0020, 0x0000 },
6335 { 0x1e, 0x0000, 0x2000 }
6336 };
6337
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08006338 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08006339
6340 /* disable aspm and clock request before access ephy */
6341 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6342 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6343 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6344}
6345
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006346static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6347{
6348 void __iomem *ioaddr = tp->mmio_addr;
6349 struct pci_dev *pdev = tp->pci_dev;
Andrzej Hajda72521ea2015-09-24 16:00:24 +02006350 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006351 u32 data;
6352 static const struct ephy_info e_info_8168h_1[] = {
6353 { 0x1e, 0x0800, 0x0001 },
6354 { 0x1d, 0x0000, 0x0800 },
6355 { 0x05, 0xffff, 0x2089 },
6356 { 0x06, 0xffff, 0x5881 },
6357 { 0x04, 0xffff, 0x154a },
6358 { 0x01, 0xffff, 0x068b }
6359 };
6360
6361 /* disable aspm and clock request before access ephy */
6362 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6363 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6364 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6365
6366 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6367
6368 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6369 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6370 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6371 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6372
6373 rtl_csi_access_enable_1(tp);
6374
6375 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6376
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006377 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6378 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006379
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006380 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006381
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006382 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006383
6384 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6385
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006386 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6387 RTL_W8(MaxTxPacketSize, EarlySize);
6388
6389 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6390 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6391
6392 /* Adjust EEE LED frequency */
6393 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6394
6395 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006396 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006397
6398 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6399
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006400 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006401
6402 rtl_pcie_state_l2l3_enable(tp, false);
6403
6404 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08006405 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006406 rtl_writephy(tp, 0x1f, 0x0000);
6407 if (rg_saw_cnt > 0) {
6408 u16 sw_cnt_1ms_ini;
6409
6410 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6411 sw_cnt_1ms_ini &= 0x0fff;
6412 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006413 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006414 data |= sw_cnt_1ms_ini;
6415 r8168_mac_ocp_write(tp, 0xd412, data);
6416 }
6417
6418 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006419 data &= ~0xf0;
6420 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006421 r8168_mac_ocp_write(tp, 0xe056, data);
6422
6423 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006424 data &= ~0x6000;
6425 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006426 r8168_mac_ocp_write(tp, 0xe052, data);
6427
6428 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006429 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006430 data |= 0x017f;
6431 r8168_mac_ocp_write(tp, 0xe0d6, data);
6432
6433 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08006434 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006435 data |= 0x047f;
6436 r8168_mac_ocp_write(tp, 0xd420, data);
6437
6438 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6439 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6440 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6441 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6442}
6443
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006444static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6445{
6446 void __iomem *ioaddr = tp->mmio_addr;
6447 struct pci_dev *pdev = tp->pci_dev;
6448
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006449 rtl8168ep_stop_cmac(tp);
6450
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006451 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6452
6453 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6454 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6455 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6456 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6457
6458 rtl_csi_access_enable_1(tp);
6459
6460 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6461
6462 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6463 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6464
6465 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6466
6467 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6468
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006469 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6470 RTL_W8(MaxTxPacketSize, EarlySize);
6471
6472 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6473 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6474
6475 /* Adjust EEE LED frequency */
6476 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6477
6478 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6479
6480 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6481
6482 rtl_pcie_state_l2l3_enable(tp, false);
6483}
6484
6485static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6486{
6487 void __iomem *ioaddr = tp->mmio_addr;
6488 static const struct ephy_info e_info_8168ep_1[] = {
6489 { 0x00, 0xffff, 0x10ab },
6490 { 0x06, 0xffff, 0xf030 },
6491 { 0x08, 0xffff, 0x2006 },
6492 { 0x0d, 0xffff, 0x1666 },
6493 { 0x0c, 0x3ff0, 0x0000 }
6494 };
6495
6496 /* disable aspm and clock request before access ephy */
6497 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6498 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6499 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6500
6501 rtl_hw_start_8168ep(tp);
6502}
6503
6504static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6505{
6506 void __iomem *ioaddr = tp->mmio_addr;
6507 static const struct ephy_info e_info_8168ep_2[] = {
6508 { 0x00, 0xffff, 0x10a3 },
6509 { 0x19, 0xffff, 0xfc00 },
6510 { 0x1e, 0xffff, 0x20ea }
6511 };
6512
6513 /* disable aspm and clock request before access ephy */
6514 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6515 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6516 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6517
6518 rtl_hw_start_8168ep(tp);
6519
6520 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006521 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006522}
6523
6524static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6525{
6526 void __iomem *ioaddr = tp->mmio_addr;
6527 u32 data;
6528 static const struct ephy_info e_info_8168ep_3[] = {
6529 { 0x00, 0xffff, 0x10a3 },
6530 { 0x19, 0xffff, 0x7c00 },
6531 { 0x1e, 0xffff, 0x20eb },
6532 { 0x0d, 0xffff, 0x1666 }
6533 };
6534
6535 /* disable aspm and clock request before access ephy */
6536 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6537 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6538 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6539
6540 rtl_hw_start_8168ep(tp);
6541
6542 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
Chun-Hao Lin69f3dc32015-12-29 22:13:37 +08006543 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006544
6545 data = r8168_mac_ocp_read(tp, 0xd3e2);
6546 data &= 0xf000;
6547 data |= 0x0271;
6548 r8168_mac_ocp_write(tp, 0xd3e2, data);
6549
6550 data = r8168_mac_ocp_read(tp, 0xd3e4);
6551 data &= 0xff00;
6552 r8168_mac_ocp_write(tp, 0xd3e4, data);
6553
6554 data = r8168_mac_ocp_read(tp, 0xe860);
6555 data |= 0x0080;
6556 r8168_mac_ocp_write(tp, 0xe860, data);
6557}
6558
Francois Romieu07ce4062007-02-23 23:36:39 +01006559static void rtl_hw_start_8168(struct net_device *dev)
6560{
Francois Romieu2dd99532007-06-11 23:22:52 +02006561 struct rtl8169_private *tp = netdev_priv(dev);
6562 void __iomem *ioaddr = tp->mmio_addr;
6563
6564 RTL_W8(Cfg9346, Cfg9346_Unlock);
6565
françois romieuf0298f82011-01-03 15:07:42 +00006566 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006567
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006568 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006569
Francois Romieu0e485152007-02-20 00:00:26 +01006570 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006571
6572 RTL_W16(CPlusCmd, tp->cp_cmd);
6573
Francois Romieu0e485152007-02-20 00:00:26 +01006574 RTL_W16(IntrMitigate, 0x5151);
6575
6576 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006577 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006578 tp->event_slow |= RxFIFOOver | PCSTimeout;
6579 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006580 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006581
6582 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6583
hayeswang1a964642013-04-01 22:23:41 +00006584 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006585
6586 RTL_R8(IntrMask);
6587
Francois Romieu219a1e92008-06-28 11:58:39 +02006588 switch (tp->mac_version) {
6589 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006590 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006591 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006592
6593 case RTL_GIGA_MAC_VER_12:
6594 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006595 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006596 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006597
6598 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006599 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006600 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006601
6602 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006603 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006604 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006605
6606 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006607 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006608 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006609
Francois Romieu197ff762008-06-28 13:16:02 +02006610 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006611 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006612 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006613
Francois Romieu6fb07052008-06-29 11:54:28 +02006614 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006615 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006616 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006617
Francois Romieuef3386f2008-06-29 12:24:30 +02006618 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006619 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006620 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006621
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006622 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006623 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006624 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006625
Francois Romieu5b538df2008-07-20 16:22:45 +02006626 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006627 case RTL_GIGA_MAC_VER_26:
6628 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006629 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006630 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006631
françois romieue6de30d2011-01-03 15:08:37 +00006632 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006633 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006634 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006635
hayeswang4804b3b2011-03-21 01:50:29 +00006636 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006637 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006638 break;
6639
hayeswang01dc7fe2011-03-21 01:50:28 +00006640 case RTL_GIGA_MAC_VER_32:
6641 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006642 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006643 break;
6644 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006645 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006646 break;
françois romieue6de30d2011-01-03 15:08:37 +00006647
Hayes Wangc2218922011-09-06 16:55:18 +08006648 case RTL_GIGA_MAC_VER_35:
6649 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006650 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006651 break;
6652
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006653 case RTL_GIGA_MAC_VER_38:
6654 rtl_hw_start_8411(tp);
6655 break;
6656
Hayes Wangc5583862012-07-02 17:23:22 +08006657 case RTL_GIGA_MAC_VER_40:
6658 case RTL_GIGA_MAC_VER_41:
6659 rtl_hw_start_8168g_1(tp);
6660 break;
hayeswang57538c42013-04-01 22:23:40 +00006661 case RTL_GIGA_MAC_VER_42:
6662 rtl_hw_start_8168g_2(tp);
6663 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006664
hayeswang45dd95c2013-07-08 17:09:01 +08006665 case RTL_GIGA_MAC_VER_44:
6666 rtl_hw_start_8411_2(tp);
6667 break;
6668
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006669 case RTL_GIGA_MAC_VER_45:
6670 case RTL_GIGA_MAC_VER_46:
6671 rtl_hw_start_8168h_1(tp);
6672 break;
6673
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006674 case RTL_GIGA_MAC_VER_49:
6675 rtl_hw_start_8168ep_1(tp);
6676 break;
6677
6678 case RTL_GIGA_MAC_VER_50:
6679 rtl_hw_start_8168ep_2(tp);
6680 break;
6681
6682 case RTL_GIGA_MAC_VER_51:
6683 rtl_hw_start_8168ep_3(tp);
6684 break;
6685
Francois Romieu219a1e92008-06-28 11:58:39 +02006686 default:
6687 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6688 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006689 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006690 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006691
hayeswang1a964642013-04-01 22:23:41 +00006692 RTL_W8(Cfg9346, Cfg9346_Lock);
6693
Francois Romieu0e485152007-02-20 00:00:26 +01006694 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6695
hayeswang1a964642013-04-01 22:23:41 +00006696 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006697
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006698 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
Francois Romieu2857ffb2008-08-02 21:08:49 +02006701#define R810X_CPCMD_QUIRK_MASK (\
6702 EnableBist | \
6703 Mac_dbgo_oe | \
6704 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006705 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006706 Force_txflow_en | \
6707 Cxpl_dbg_sel | \
6708 ASF | \
6709 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006710 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006711
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006712static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006713{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006714 void __iomem *ioaddr = tp->mmio_addr;
6715 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006716 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006717 { 0x01, 0, 0x6e65 },
6718 { 0x02, 0, 0x091f },
6719 { 0x03, 0, 0xc2f9 },
6720 { 0x06, 0, 0xafb5 },
6721 { 0x07, 0, 0x0e00 },
6722 { 0x19, 0, 0xec80 },
6723 { 0x01, 0, 0x2e65 },
6724 { 0x01, 0, 0x6e65 }
6725 };
6726 u8 cfg1;
6727
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006728 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006729
6730 RTL_W8(DBG_REG, FIX_NAK_1);
6731
6732 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6733
6734 RTL_W8(Config1,
6735 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6736 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6737
6738 cfg1 = RTL_R8(Config1);
6739 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6740 RTL_W8(Config1, cfg1 & ~LEDS0);
6741
Francois Romieufdf6fc02012-07-06 22:40:38 +02006742 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006743}
6744
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006745static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006746{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006747 void __iomem *ioaddr = tp->mmio_addr;
6748 struct pci_dev *pdev = tp->pci_dev;
6749
6750 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006751
6752 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6753
6754 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6755 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006756}
6757
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006758static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006759{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006760 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006761
Francois Romieufdf6fc02012-07-06 22:40:38 +02006762 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006763}
6764
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006765static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006766{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006767 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006768 static const struct ephy_info e_info_8105e_1[] = {
6769 { 0x07, 0, 0x4000 },
6770 { 0x19, 0, 0x0200 },
6771 { 0x19, 0, 0x0020 },
6772 { 0x1e, 0, 0x2000 },
6773 { 0x03, 0, 0x0001 },
6774 { 0x19, 0, 0x0100 },
6775 { 0x19, 0, 0x0004 },
6776 { 0x0a, 0, 0x0020 }
6777 };
6778
Francois Romieucecb5fd2011-04-01 10:21:07 +02006779 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006780 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6781
Francois Romieucecb5fd2011-04-01 10:21:07 +02006782 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006783 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6784
6785 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006786 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006787
Francois Romieufdf6fc02012-07-06 22:40:38 +02006788 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006789
6790 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006791}
6792
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006793static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006794{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006795 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006796 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006797}
6798
Hayes Wang7e18dca2012-03-30 14:33:02 +08006799static void rtl_hw_start_8402(struct rtl8169_private *tp)
6800{
6801 void __iomem *ioaddr = tp->mmio_addr;
6802 static const struct ephy_info e_info_8402[] = {
6803 { 0x19, 0xffff, 0xff64 },
6804 { 0x1e, 0, 0x4000 }
6805 };
6806
6807 rtl_csi_access_enable_2(tp);
6808
6809 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6810 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6811
6812 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6813 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6814
Francois Romieufdf6fc02012-07-06 22:40:38 +02006815 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006816
6817 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6818
Francois Romieufdf6fc02012-07-06 22:40:38 +02006819 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6820 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006821 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6822 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006823 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6824 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006825 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006826
6827 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006828}
6829
Hayes Wang5598bfe2012-07-02 17:23:21 +08006830static void rtl_hw_start_8106(struct rtl8169_private *tp)
6831{
6832 void __iomem *ioaddr = tp->mmio_addr;
6833
6834 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6835 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6836
Francois Romieu4521e1a92012-11-01 16:46:28 +00006837 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006838 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6839 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006840
6841 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006842}
6843
Francois Romieu07ce4062007-02-23 23:36:39 +01006844static void rtl_hw_start_8101(struct net_device *dev)
6845{
Francois Romieucdf1a602007-06-11 23:29:50 +02006846 struct rtl8169_private *tp = netdev_priv(dev);
6847 void __iomem *ioaddr = tp->mmio_addr;
6848 struct pci_dev *pdev = tp->pci_dev;
6849
Francois Romieuda78dbf2012-01-26 14:18:23 +01006850 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6851 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006852
Francois Romieucecb5fd2011-04-01 10:21:07 +02006853 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006854 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006855 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6856 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006857
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006858 RTL_W8(Cfg9346, Cfg9346_Unlock);
6859
hayeswang1a964642013-04-01 22:23:41 +00006860 RTL_W8(MaxTxPacketSize, TxPacketMax);
6861
6862 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6863
6864 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6865 RTL_W16(CPlusCmd, tp->cp_cmd);
6866
6867 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6868
6869 rtl_set_rx_tx_config_registers(tp);
6870
Francois Romieu2857ffb2008-08-02 21:08:49 +02006871 switch (tp->mac_version) {
6872 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006873 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006874 break;
6875
6876 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006877 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006878 break;
6879
6880 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006881 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006882 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006883
6884 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006885 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006886 break;
6887 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006888 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006889 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006890
6891 case RTL_GIGA_MAC_VER_37:
6892 rtl_hw_start_8402(tp);
6893 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006894
6895 case RTL_GIGA_MAC_VER_39:
6896 rtl_hw_start_8106(tp);
6897 break;
hayeswang58152cd2013-04-01 22:23:42 +00006898 case RTL_GIGA_MAC_VER_43:
6899 rtl_hw_start_8168g_2(tp);
6900 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006901 case RTL_GIGA_MAC_VER_47:
6902 case RTL_GIGA_MAC_VER_48:
6903 rtl_hw_start_8168h_1(tp);
6904 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006905 }
6906
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006907 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006908
Francois Romieucdf1a602007-06-11 23:29:50 +02006909 RTL_W16(IntrMitigate, 0x0000);
6910
Francois Romieucdf1a602007-06-11 23:29:50 +02006911 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006912
Francois Romieucdf1a602007-06-11 23:29:50 +02006913 rtl_set_rx_mode(dev);
6914
hayeswang1a964642013-04-01 22:23:41 +00006915 RTL_R8(IntrMask);
6916
Francois Romieucdf1a602007-06-11 23:29:50 +02006917 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918}
6919
6920static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6921{
Francois Romieud58d46b2011-05-03 16:38:29 +02006922 struct rtl8169_private *tp = netdev_priv(dev);
6923
Francois Romieud58d46b2011-05-03 16:38:29 +02006924 if (new_mtu > ETH_DATA_LEN)
6925 rtl_hw_jumbo_enable(tp);
6926 else
6927 rtl_hw_jumbo_disable(tp);
6928
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006930 netdev_update_features(dev);
6931
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933}
6934
6935static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6936{
Al Viro95e09182007-12-22 18:55:39 +00006937 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6939}
6940
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006941static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6942 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006944 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006945 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006946
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006947 kfree(*data_buff);
6948 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 rtl8169_make_unusable_by_asic(desc);
6950}
6951
6952static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6953{
6954 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6955
Alexander Duycka0750132014-12-11 15:02:17 -08006956 /* Force memory writes to complete before releasing descriptor */
6957 dma_wmb();
6958
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6960}
6961
6962static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6963 u32 rx_buf_sz)
6964{
6965 desc->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 rtl8169_mark_to_asic(desc, rx_buf_sz);
6967}
6968
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006969static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006971 return (void *)ALIGN((long)data, 16);
6972}
6973
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006974static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6975 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006976{
6977 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006979 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006980 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006981 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006983 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6984 if (!data)
6985 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006986
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006987 if (rtl8169_align(data) != data) {
6988 kfree(data);
6989 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6990 if (!data)
6991 return NULL;
6992 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006993
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006994 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006995 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006996 if (unlikely(dma_mapping_error(d, mapping))) {
6997 if (net_ratelimit())
6998 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006999 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
7002 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007003 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007004
7005err_out:
7006 kfree(data);
7007 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008}
7009
7010static void rtl8169_rx_clear(struct rtl8169_private *tp)
7011{
Francois Romieu07d3f512007-02-21 22:40:46 +01007012 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007015 if (tp->Rx_databuff[i]) {
7016 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 tp->RxDescArray + i);
7018 }
7019 }
7020}
7021
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007022static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007024 desc->opts1 |= cpu_to_le32(RingEnd);
7025}
Francois Romieu5b0384f2006-08-16 16:00:01 +02007026
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007027static int rtl8169_rx_fill(struct rtl8169_private *tp)
7028{
7029 unsigned int i;
7030
7031 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007032 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02007033
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007034 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02007036
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007037 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007038 if (!data) {
7039 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007040 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007041 }
7042 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007045 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7046 return 0;
7047
7048err_out:
7049 rtl8169_rx_clear(tp);
7050 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051}
7052
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053static int rtl8169_init_ring(struct net_device *dev)
7054{
7055 struct rtl8169_private *tp = netdev_priv(dev);
7056
7057 rtl8169_init_ring_indexes(tp);
7058
7059 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007060 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00007062 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063}
7064
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007065static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 struct TxDesc *desc)
7067{
7068 unsigned int len = tx_skb->len;
7069
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007070 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7071
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 desc->opts1 = 0x00;
7073 desc->opts2 = 0x00;
7074 desc->addr = 0x00;
7075 tx_skb->len = 0;
7076}
7077
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007078static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7079 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080{
7081 unsigned int i;
7082
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007083 for (i = 0; i < n; i++) {
7084 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 struct ring_info *tx_skb = tp->tx_skb + entry;
7086 unsigned int len = tx_skb->len;
7087
7088 if (len) {
7089 struct sk_buff *skb = tx_skb->skb;
7090
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007091 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 tp->TxDescArray + entry);
7093 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007094 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 tx_skb->skb = NULL;
7096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 }
7098 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007099}
7100
7101static void rtl8169_tx_clear(struct rtl8169_private *tp)
7102{
7103 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 tp->cur_tx = tp->dirty_tx = 0;
7105}
7106
Francois Romieu4422bcd2012-01-26 11:23:32 +01007107static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108{
David Howellsc4028952006-11-22 14:57:56 +00007109 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01007110 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Francois Romieuda78dbf2012-01-26 14:18:23 +01007112 napi_disable(&tp->napi);
7113 netif_stop_queue(dev);
7114 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
françois romieuc7c2c392011-12-04 20:30:52 +00007116 rtl8169_hw_reset(tp);
7117
Francois Romieu56de4142011-03-15 17:29:31 +01007118 for (i = 0; i < NUM_RX_DESC; i++)
7119 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7120
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00007122 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
Francois Romieuda78dbf2012-01-26 14:18:23 +01007124 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01007125 rtl_hw_start(dev);
7126 netif_wake_queue(dev);
7127 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128}
7129
7130static void rtl8169_tx_timeout(struct net_device *dev)
7131{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007132 struct rtl8169_private *tp = netdev_priv(dev);
7133
7134 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135}
7136
7137static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07007138 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139{
7140 struct skb_shared_info *info = skb_shinfo(skb);
7141 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007142 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007143 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144
7145 entry = tp->cur_tx;
7146 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007147 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 dma_addr_t mapping;
7149 u32 status, len;
7150 void *addr;
7151
7152 entry = (entry + 1) % NUM_TX_DESC;
7153
7154 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00007155 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00007156 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007157 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007158 if (unlikely(dma_mapping_error(d, mapping))) {
7159 if (net_ratelimit())
7160 netif_err(tp, drv, tp->dev,
7161 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007162 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
Francois Romieucecb5fd2011-04-01 10:21:07 +02007165 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007166 status = opts[0] | len |
7167 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168
7169 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07007170 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 txd->addr = cpu_to_le64(mapping);
7172
7173 tp->tx_skb[entry].len = len;
7174 }
7175
7176 if (cur_frag) {
7177 tp->tx_skb[entry].skb = skb;
7178 txd->opts1 |= cpu_to_le32(LastFrag);
7179 }
7180
7181 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007182
7183err_out:
7184 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7185 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186}
7187
françois romieub423e9a2013-05-18 01:24:46 +00007188static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7189{
7190 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7191}
7192
hayeswange9746042014-07-11 16:25:58 +08007193static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7194 struct net_device *dev);
7195/* r8169_csum_workaround()
7196 * The hw limites the value the transport offset. When the offset is out of the
7197 * range, calculate the checksum by sw.
7198 */
7199static void r8169_csum_workaround(struct rtl8169_private *tp,
7200 struct sk_buff *skb)
7201{
7202 if (skb_shinfo(skb)->gso_size) {
7203 netdev_features_t features = tp->dev->features;
7204 struct sk_buff *segs, *nskb;
7205
7206 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7207 segs = skb_gso_segment(skb, features);
7208 if (IS_ERR(segs) || !segs)
7209 goto drop;
7210
7211 do {
7212 nskb = segs;
7213 segs = segs->next;
7214 nskb->next = NULL;
7215 rtl8169_start_xmit(nskb, tp->dev);
7216 } while (segs);
7217
Alexander Duyckeb781392015-05-01 10:34:44 -07007218 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007219 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7220 if (skb_checksum_help(skb) < 0)
7221 goto drop;
7222
7223 rtl8169_start_xmit(skb, tp->dev);
7224 } else {
7225 struct net_device_stats *stats;
7226
7227drop:
7228 stats = &tp->dev->stats;
7229 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07007230 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08007231 }
7232}
7233
7234/* msdn_giant_send_check()
7235 * According to the document of microsoft, the TCP Pseudo Header excludes the
7236 * packet length for IPv6 TCP large packets.
7237 */
7238static int msdn_giant_send_check(struct sk_buff *skb)
7239{
7240 const struct ipv6hdr *ipv6h;
7241 struct tcphdr *th;
7242 int ret;
7243
7244 ret = skb_cow_head(skb, 0);
7245 if (ret)
7246 return ret;
7247
7248 ipv6h = ipv6_hdr(skb);
7249 th = tcp_hdr(skb);
7250
7251 th->check = 0;
7252 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7253
7254 return ret;
7255}
7256
7257static inline __be16 get_protocol(struct sk_buff *skb)
7258{
7259 __be16 protocol;
7260
7261 if (skb->protocol == htons(ETH_P_8021Q))
7262 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7263 else
7264 protocol = skb->protocol;
7265
7266 return protocol;
7267}
7268
hayeswang5888d3f2014-07-11 16:25:56 +08007269static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7270 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271{
Michał Mirosław350fb322011-04-08 06:35:56 +00007272 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
Francois Romieu2b7b4312011-04-18 22:53:24 -07007274 if (mss) {
7275 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08007276 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7277 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7278 const struct iphdr *ip = ip_hdr(skb);
7279
7280 if (ip->protocol == IPPROTO_TCP)
7281 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7282 else if (ip->protocol == IPPROTO_UDP)
7283 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7284 else
7285 WARN_ON_ONCE(1);
7286 }
7287
7288 return true;
7289}
7290
7291static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7292 struct sk_buff *skb, u32 *opts)
7293{
hayeswangbdfa4ed2014-07-11 16:25:57 +08007294 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08007295 u32 mss = skb_shinfo(skb)->gso_size;
7296
7297 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08007298 if (transport_offset > GTTCPHO_MAX) {
7299 netif_warn(tp, tx_err, tp->dev,
7300 "Invalid transport offset 0x%x for TSO\n",
7301 transport_offset);
7302 return false;
7303 }
7304
7305 switch (get_protocol(skb)) {
7306 case htons(ETH_P_IP):
7307 opts[0] |= TD1_GTSENV4;
7308 break;
7309
7310 case htons(ETH_P_IPV6):
7311 if (msdn_giant_send_check(skb))
7312 return false;
7313
7314 opts[0] |= TD1_GTSENV6;
7315 break;
7316
7317 default:
7318 WARN_ON_ONCE(1);
7319 break;
7320 }
7321
hayeswangbdfa4ed2014-07-11 16:25:57 +08007322 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08007323 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007324 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08007325 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326
françois romieub423e9a2013-05-18 01:24:46 +00007327 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007328 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00007329
hayeswange9746042014-07-11 16:25:58 +08007330 if (transport_offset > TCPHO_MAX) {
7331 netif_warn(tp, tx_err, tp->dev,
7332 "Invalid transport offset 0x%x\n",
7333 transport_offset);
7334 return false;
7335 }
7336
7337 switch (get_protocol(skb)) {
7338 case htons(ETH_P_IP):
7339 opts[1] |= TD1_IPv4_CS;
7340 ip_protocol = ip_hdr(skb)->protocol;
7341 break;
7342
7343 case htons(ETH_P_IPV6):
7344 opts[1] |= TD1_IPv6_CS;
7345 ip_protocol = ipv6_hdr(skb)->nexthdr;
7346 break;
7347
7348 default:
7349 ip_protocol = IPPROTO_RAW;
7350 break;
7351 }
7352
7353 if (ip_protocol == IPPROTO_TCP)
7354 opts[1] |= TD1_TCP_CS;
7355 else if (ip_protocol == IPPROTO_UDP)
7356 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007357 else
7358 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007359
7360 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007361 } else {
7362 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08007363 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 }
hayeswang5888d3f2014-07-11 16:25:56 +08007365
françois romieub423e9a2013-05-18 01:24:46 +00007366 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367}
7368
Stephen Hemminger613573252009-08-31 19:50:58 +00007369static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7370 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371{
7372 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007373 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 struct TxDesc *txd = tp->TxDescArray + entry;
7375 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007376 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 dma_addr_t mapping;
7378 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007379 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007380 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007381
Julien Ducourthial477206a2012-05-09 00:00:06 +02007382 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007383 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007384 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 }
7386
7387 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007388 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
françois romieub423e9a2013-05-18 01:24:46 +00007390 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7391 opts[0] = DescOwn;
7392
hayeswange9746042014-07-11 16:25:58 +08007393 if (!tp->tso_csum(tp, skb, opts)) {
7394 r8169_csum_workaround(tp, skb);
7395 return NETDEV_TX_OK;
7396 }
françois romieub423e9a2013-05-18 01:24:46 +00007397
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007398 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007399 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007400 if (unlikely(dma_mapping_error(d, mapping))) {
7401 if (net_ratelimit())
7402 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007403 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405
7406 tp->tx_skb[entry].len = len;
7407 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408
Francois Romieu2b7b4312011-04-18 22:53:24 -07007409 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007410 if (frags < 0)
7411 goto err_dma_1;
7412 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007413 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007414 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007415 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007416 tp->tx_skb[entry].skb = skb;
7417 }
7418
Francois Romieu2b7b4312011-04-18 22:53:24 -07007419 txd->opts2 = cpu_to_le32(opts[1]);
7420
Richard Cochran5047fb52012-03-10 07:29:42 +00007421 skb_tx_timestamp(skb);
7422
Alexander Duycka0750132014-12-11 15:02:17 -08007423 /* Force memory writes to complete before releasing descriptor */
7424 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
Francois Romieucecb5fd2011-04-01 10:21:07 +02007426 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007427 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 txd->opts1 = cpu_to_le32(status);
7429
Alexander Duycka0750132014-12-11 15:02:17 -08007430 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00007431 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432
Alexander Duycka0750132014-12-11 15:02:17 -08007433 tp->cur_tx += frags + 1;
7434
David S. Miller87cda7c2015-02-22 15:54:29 -05007435 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
David S. Miller87cda7c2015-02-22 15:54:29 -05007437 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01007438
David S. Miller87cda7c2015-02-22 15:54:29 -05007439 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007440 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7441 * not miss a ring update when it notices a stopped queue.
7442 */
7443 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007445 /* Sync with rtl_tx:
7446 * - publish queue status and cur_tx ring index (write barrier)
7447 * - refresh dirty_tx ring index (read barrier).
7448 * May the current thread have a pessimistic view of the ring
7449 * status and forget to wake up queue, a racing rtl_tx thread
7450 * can't.
7451 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007452 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007453 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 netif_wake_queue(dev);
7455 }
7456
Stephen Hemminger613573252009-08-31 19:50:58 +00007457 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007459err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007460 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007461err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007462 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007463 dev->stats.tx_dropped++;
7464 return NETDEV_TX_OK;
7465
7466err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007468 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007469 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470}
7471
7472static void rtl8169_pcierr_interrupt(struct net_device *dev)
7473{
7474 struct rtl8169_private *tp = netdev_priv(dev);
7475 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 u16 pci_status, pci_cmd;
7477
7478 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7479 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7480
Joe Perchesbf82c182010-02-09 11:49:50 +00007481 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7482 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483
7484 /*
7485 * The recovery sequence below admits a very elaborated explanation:
7486 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007487 * - I did not see what else could be done;
7488 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 *
7490 * Feel free to adjust to your needs.
7491 */
Francois Romieua27993f2006-12-18 00:04:19 +01007492 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007493 pci_cmd &= ~PCI_COMMAND_PARITY;
7494 else
7495 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7496
7497 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498
7499 pci_write_config_word(pdev, PCI_STATUS,
7500 pci_status & (PCI_STATUS_DETECTED_PARITY |
7501 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7502 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7503
7504 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007505 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007506 void __iomem *ioaddr = tp->mmio_addr;
7507
Joe Perchesbf82c182010-02-09 11:49:50 +00007508 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 tp->cp_cmd &= ~PCIDAC;
7510 RTL_W16(CPlusCmd, tp->cp_cmd);
7511 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 }
7513
françois romieue6de30d2011-01-03 15:08:37 +00007514 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007515
Francois Romieu98ddf982012-01-31 10:47:34 +01007516 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517}
7518
Francois Romieuda78dbf2012-01-26 14:18:23 +01007519static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520{
7521 unsigned int dirty_tx, tx_left;
7522
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 dirty_tx = tp->dirty_tx;
7524 smp_rmb();
7525 tx_left = tp->cur_tx - dirty_tx;
7526
7527 while (tx_left > 0) {
7528 unsigned int entry = dirty_tx % NUM_TX_DESC;
7529 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 u32 status;
7531
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7533 if (status & DescOwn)
7534 break;
7535
Alexander Duycka0750132014-12-11 15:02:17 -08007536 /* This barrier is needed to keep us from reading
7537 * any other fields out of the Tx descriptor until
7538 * we know the status of DescOwn
7539 */
7540 dma_rmb();
7541
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007542 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7543 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05007545 u64_stats_update_begin(&tp->tx_stats.syncp);
7546 tp->tx_stats.packets++;
7547 tp->tx_stats.bytes += tx_skb->skb->len;
7548 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07007549 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 tx_skb->skb = NULL;
7551 }
7552 dirty_tx++;
7553 tx_left--;
7554 }
7555
7556 if (tp->dirty_tx != dirty_tx) {
7557 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007558 /* Sync with rtl8169_start_xmit:
7559 * - publish dirty_tx ring index (write barrier)
7560 * - refresh cur_tx ring index and queue status (read barrier)
7561 * May the current thread miss the stopped queue condition,
7562 * a racing xmit thread can only have a right view of the
7563 * ring status.
7564 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007565 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007567 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 netif_wake_queue(dev);
7569 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007570 /*
7571 * 8168 hack: TxPoll requests are lost when the Tx packets are
7572 * too close. Let's kick an extra TxPoll request when a burst
7573 * of start_xmit activity is detected (if it is not detected,
7574 * it is slow enough). -- FR
7575 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007576 if (tp->cur_tx != dirty_tx) {
7577 void __iomem *ioaddr = tp->mmio_addr;
7578
Francois Romieud78ae2d2007-08-26 20:08:19 +02007579 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 }
7582}
7583
Francois Romieu126fa4b2005-05-12 20:09:17 -04007584static inline int rtl8169_fragmented_frame(u32 status)
7585{
7586 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7587}
7588
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007589static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 u32 status = opts1 & RxProtoMask;
7592
7593 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007594 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 skb->ip_summed = CHECKSUM_UNNECESSARY;
7596 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007597 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598}
7599
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007600static struct sk_buff *rtl8169_try_rx_copy(void *data,
7601 struct rtl8169_private *tp,
7602 int pkt_size,
7603 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007605 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007606 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007608 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007609 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007610 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08007611 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007612 if (skb)
7613 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007614 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7615
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007616 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617}
7618
Francois Romieuda78dbf2012-01-26 14:18:23 +01007619static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620{
7621 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007622 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
Timo Teräs9fba0812013-01-15 21:01:24 +00007626 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007628 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 u32 status;
7630
David S. Miller8decf862011-09-22 03:23:13 -04007631 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 if (status & DescOwn)
7633 break;
Alexander Duycka0750132014-12-11 15:02:17 -08007634
7635 /* This barrier is needed to keep us from reading
7636 * any other fields out of the Rx descriptor until
7637 * we know the status of DescOwn
7638 */
7639 dma_rmb();
7640
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007641 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007642 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7643 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007644 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007646 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007648 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007649 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007650 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007651 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007652 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007653 if ((status & (RxRUNT | RxCRC)) &&
7654 !(status & (RxRWT | RxFOVF)) &&
7655 (dev->features & NETIF_F_RXALL))
7656 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007658 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007659 dma_addr_t addr;
7660 int pkt_size;
7661
7662process_pkt:
7663 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007664 if (likely(!(dev->features & NETIF_F_RXFCS)))
7665 pkt_size = (status & 0x00003fff) - 4;
7666 else
7667 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668
Francois Romieu126fa4b2005-05-12 20:09:17 -04007669 /*
7670 * The driver does not support incoming fragmented
7671 * frames. They are seen as a symptom of over-mtu
7672 * sized frames.
7673 */
7674 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007675 dev->stats.rx_dropped++;
7676 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007677 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007678 }
7679
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007680 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7681 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007682 if (!skb) {
7683 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007684 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 }
7686
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007687 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 skb_put(skb, pkt_size);
7689 skb->protocol = eth_type_trans(skb, dev);
7690
Francois Romieu7a8fc772011-03-01 17:18:33 +01007691 rtl8169_rx_vlan_tag(desc, skb);
7692
françois romieu39174292015-11-11 23:35:18 +01007693 if (skb->pkt_type == PACKET_MULTICAST)
7694 dev->stats.multicast++;
7695
Francois Romieu56de4142011-03-15 17:29:31 +01007696 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
Junchang Wang8027aa22012-03-04 23:30:32 +01007698 u64_stats_update_begin(&tp->rx_stats.syncp);
7699 tp->rx_stats.packets++;
7700 tp->rx_stats.bytes += pkt_size;
7701 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702 }
françois romieuce11ff52013-01-24 13:30:06 +00007703release_descriptor:
7704 desc->opts2 = 0;
françois romieuce11ff52013-01-24 13:30:06 +00007705 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706 }
7707
7708 count = cur_rx - tp->cur_rx;
7709 tp->cur_rx = cur_rx;
7710
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 return count;
7712}
7713
Francois Romieu07d3f512007-02-21 22:40:46 +01007714static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715{
Francois Romieu07d3f512007-02-21 22:40:46 +01007716 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007719 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007721 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007722 if (status && status != 0xffff) {
7723 status &= RTL_EVENT_NAPI | tp->event_slow;
7724 if (status) {
7725 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007726
Francois Romieuda78dbf2012-01-26 14:18:23 +01007727 rtl_irq_disable(tp);
7728 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731 return IRQ_RETVAL(handled);
7732}
7733
Francois Romieuda78dbf2012-01-26 14:18:23 +01007734/*
7735 * Workqueue context.
7736 */
7737static void rtl_slow_event_work(struct rtl8169_private *tp)
7738{
7739 struct net_device *dev = tp->dev;
7740 u16 status;
7741
7742 status = rtl_get_events(tp) & tp->event_slow;
7743 rtl_ack_events(tp, status);
7744
7745 if (unlikely(status & RxFIFOOver)) {
7746 switch (tp->mac_version) {
7747 /* Work around for rx fifo overflow */
7748 case RTL_GIGA_MAC_VER_11:
7749 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007750 /* XXX - Hack alert. See rtl_task(). */
7751 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007752 default:
7753 break;
7754 }
7755 }
7756
7757 if (unlikely(status & SYSErr))
7758 rtl8169_pcierr_interrupt(dev);
7759
7760 if (status & LinkChg)
7761 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7762
françois romieu7dbb4912012-06-09 10:53:16 +00007763 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007764}
7765
Francois Romieu4422bcd2012-01-26 11:23:32 +01007766static void rtl_task(struct work_struct *work)
7767{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007768 static const struct {
7769 int bitnr;
7770 void (*action)(struct rtl8169_private *);
7771 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007772 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007773 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7774 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7775 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7776 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007777 struct rtl8169_private *tp =
7778 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007779 struct net_device *dev = tp->dev;
7780 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007781
Francois Romieuda78dbf2012-01-26 14:18:23 +01007782 rtl_lock_work(tp);
7783
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007784 if (!netif_running(dev) ||
7785 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007786 goto out_unlock;
7787
7788 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7789 bool pending;
7790
Francois Romieuda78dbf2012-01-26 14:18:23 +01007791 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007792 if (pending)
7793 rtl_work[i].action(tp);
7794 }
7795
7796out_unlock:
7797 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007798}
7799
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007800static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007802 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7803 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007804 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7805 int work_done= 0;
7806 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
Francois Romieuda78dbf2012-01-26 14:18:23 +01007808 status = rtl_get_events(tp);
7809 rtl_ack_events(tp, status & ~tp->event_slow);
7810
7811 if (status & RTL_EVENT_NAPI_RX)
7812 work_done = rtl_rx(dev, tp, (u32) budget);
7813
7814 if (status & RTL_EVENT_NAPI_TX)
7815 rtl_tx(dev, tp);
7816
7817 if (status & tp->event_slow) {
7818 enable_mask &= ~tp->event_slow;
7819
7820 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007823 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08007824 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00007825
Francois Romieuda78dbf2012-01-26 14:18:23 +01007826 rtl_irq_enable(tp, enable_mask);
7827 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828 }
7829
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007830 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
Francois Romieu523a6092008-09-10 22:28:56 +02007833static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7834{
7835 struct rtl8169_private *tp = netdev_priv(dev);
7836
7837 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7838 return;
7839
7840 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7841 RTL_W32(RxMissed, 0);
7842}
7843
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844static void rtl8169_down(struct net_device *dev)
7845{
7846 struct rtl8169_private *tp = netdev_priv(dev);
7847 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848
Francois Romieu4876cc12011-03-11 21:07:11 +01007849 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007851 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007852 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853
Hayes Wang92fc43b2011-07-06 15:58:03 +08007854 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007855 /*
7856 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007857 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7858 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007859 */
Francois Romieu523a6092008-09-10 22:28:56 +02007860 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007863 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 rtl8169_tx_clear(tp);
7866
7867 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007868
7869 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870}
7871
7872static int rtl8169_close(struct net_device *dev)
7873{
7874 struct rtl8169_private *tp = netdev_priv(dev);
7875 struct pci_dev *pdev = tp->pci_dev;
7876
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007877 pm_runtime_get_sync(&pdev->dev);
7878
Francois Romieucecb5fd2011-04-01 10:21:07 +02007879 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007880 rtl8169_update_counters(dev);
7881
Francois Romieuda78dbf2012-01-26 14:18:23 +01007882 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007883 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007884
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007886 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887
Lekensteyn4ea72442013-07-22 09:53:30 +02007888 cancel_work_sync(&tp->wk.work);
7889
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007890 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007892 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7893 tp->RxPhyAddr);
7894 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7895 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 tp->TxDescArray = NULL;
7897 tp->RxDescArray = NULL;
7898
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007899 pm_runtime_put_sync(&pdev->dev);
7900
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 return 0;
7902}
7903
Francois Romieudc1c00c2012-03-08 10:06:18 +01007904#ifdef CONFIG_NET_POLL_CONTROLLER
7905static void rtl8169_netpoll(struct net_device *dev)
7906{
7907 struct rtl8169_private *tp = netdev_priv(dev);
7908
7909 rtl8169_interrupt(tp->pci_dev->irq, dev);
7910}
7911#endif
7912
Francois Romieudf43ac72012-03-08 09:48:40 +01007913static int rtl_open(struct net_device *dev)
7914{
7915 struct rtl8169_private *tp = netdev_priv(dev);
7916 void __iomem *ioaddr = tp->mmio_addr;
7917 struct pci_dev *pdev = tp->pci_dev;
7918 int retval = -ENOMEM;
7919
7920 pm_runtime_get_sync(&pdev->dev);
7921
7922 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007923 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007924 * dma_alloc_coherent provides more.
7925 */
7926 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7927 &tp->TxPhyAddr, GFP_KERNEL);
7928 if (!tp->TxDescArray)
7929 goto err_pm_runtime_put;
7930
7931 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7932 &tp->RxPhyAddr, GFP_KERNEL);
7933 if (!tp->RxDescArray)
7934 goto err_free_tx_0;
7935
7936 retval = rtl8169_init_ring(dev);
7937 if (retval < 0)
7938 goto err_free_rx_1;
7939
7940 INIT_WORK(&tp->wk.work, rtl_task);
7941
7942 smp_mb();
7943
7944 rtl_request_firmware(tp);
7945
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007946 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007947 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7948 dev->name, dev);
7949 if (retval < 0)
7950 goto err_release_fw_2;
7951
7952 rtl_lock_work(tp);
7953
7954 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7955
7956 napi_enable(&tp->napi);
7957
7958 rtl8169_init_phy(dev, tp);
7959
7960 __rtl8169_set_features(dev, dev->features);
7961
7962 rtl_pll_power_up(tp);
7963
7964 rtl_hw_start(dev);
7965
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02007966 if (!rtl8169_init_counter_offsets(dev))
7967 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7968
Francois Romieudf43ac72012-03-08 09:48:40 +01007969 netif_start_queue(dev);
7970
7971 rtl_unlock_work(tp);
7972
7973 tp->saved_wolopts = 0;
7974 pm_runtime_put_noidle(&pdev->dev);
7975
7976 rtl8169_check_link_status(dev, tp, ioaddr);
7977out:
7978 return retval;
7979
7980err_release_fw_2:
7981 rtl_release_firmware(tp);
7982 rtl8169_rx_clear(tp);
7983err_free_rx_1:
7984 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7985 tp->RxPhyAddr);
7986 tp->RxDescArray = NULL;
7987err_free_tx_0:
7988 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7989 tp->TxPhyAddr);
7990 tp->TxDescArray = NULL;
7991err_pm_runtime_put:
7992 pm_runtime_put_noidle(&pdev->dev);
7993 goto out;
7994}
7995
stephen hemmingerbc1f4472017-01-06 19:12:52 -08007996static void
Junchang Wang8027aa22012-03-04 23:30:32 +01007997rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998{
7999 struct rtl8169_private *tp = netdev_priv(dev);
8000 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008001 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02008002 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01008003 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008004
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008005 pm_runtime_get_noresume(&pdev->dev);
8006
8007 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Francois Romieu523a6092008-09-10 22:28:56 +02008008 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02008009
Junchang Wang8027aa22012-03-04 23:30:32 +01008010 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07008011 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01008012 stats->rx_packets = tp->rx_stats.packets;
8013 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07008014 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01008015
Junchang Wang8027aa22012-03-04 23:30:32 +01008016 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07008017 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01008018 stats->tx_packets = tp->tx_stats.packets;
8019 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07008020 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01008021
8022 stats->rx_dropped = dev->stats.rx_dropped;
8023 stats->tx_dropped = dev->stats.tx_dropped;
8024 stats->rx_length_errors = dev->stats.rx_length_errors;
8025 stats->rx_errors = dev->stats.rx_errors;
8026 stats->rx_crc_errors = dev->stats.rx_crc_errors;
8027 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
8028 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02008029 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01008030
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008031 /*
8032 * Fetch additonal counter values missing in stats collected by driver
8033 * from tally counters.
8034 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008035 if (pm_runtime_active(&pdev->dev))
8036 rtl8169_update_counters(dev);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008037
8038 /*
8039 * Subtract values fetched during initalization.
8040 * See rtl8169_init_counter_offsets for a description why we do that.
8041 */
Corinna Vinschen42020322015-09-10 10:47:35 +02008042 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008043 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02008044 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008045 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02008046 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02008047 le16_to_cpu(tp->tc_offset.tx_aborted);
8048
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008049 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050}
8051
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008052static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01008053{
françois romieu065c27c2011-01-03 15:08:12 +00008054 struct rtl8169_private *tp = netdev_priv(dev);
8055
Francois Romieu5d06a992006-02-23 00:47:58 +01008056 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008057 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01008058
8059 netif_device_detach(dev);
8060 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008061
8062 rtl_lock_work(tp);
8063 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008064 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008065 rtl_unlock_work(tp);
8066
8067 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008068}
Francois Romieu5d06a992006-02-23 00:47:58 +01008069
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008070#ifdef CONFIG_PM
8071
8072static int rtl8169_suspend(struct device *device)
8073{
8074 struct pci_dev *pdev = to_pci_dev(device);
8075 struct net_device *dev = pci_get_drvdata(pdev);
8076
8077 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02008078
Francois Romieu5d06a992006-02-23 00:47:58 +01008079 return 0;
8080}
8081
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008082static void __rtl8169_resume(struct net_device *dev)
8083{
françois romieu065c27c2011-01-03 15:08:12 +00008084 struct rtl8169_private *tp = netdev_priv(dev);
8085
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008086 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00008087
8088 rtl_pll_power_up(tp);
8089
Artem Savkovcff4c162012-04-03 10:29:11 +00008090 rtl_lock_work(tp);
8091 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01008092 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00008093 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008094
Francois Romieu98ddf982012-01-31 10:47:34 +01008095 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008096}
8097
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008098static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01008099{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008100 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01008101 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008102 struct rtl8169_private *tp = netdev_priv(dev);
8103
8104 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01008105
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008106 if (netif_running(dev))
8107 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01008108
Francois Romieu5d06a992006-02-23 00:47:58 +01008109 return 0;
8110}
8111
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008112static int rtl8169_runtime_suspend(struct device *device)
8113{
8114 struct pci_dev *pdev = to_pci_dev(device);
8115 struct net_device *dev = pci_get_drvdata(pdev);
8116 struct rtl8169_private *tp = netdev_priv(dev);
8117
8118 if (!tp->TxDescArray)
8119 return 0;
8120
Francois Romieuda78dbf2012-01-26 14:18:23 +01008121 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008122 tp->saved_wolopts = __rtl8169_get_wol(tp);
8123 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01008124 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008125
8126 rtl8169_net_suspend(dev);
8127
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08008128 /* Update counters before going runtime suspend */
8129 rtl8169_rx_missed(dev, tp->mmio_addr);
8130 rtl8169_update_counters(dev);
8131
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008132 return 0;
8133}
8134
8135static int rtl8169_runtime_resume(struct device *device)
8136{
8137 struct pci_dev *pdev = to_pci_dev(device);
8138 struct net_device *dev = pci_get_drvdata(pdev);
8139 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08008140 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008141
8142 if (!tp->TxDescArray)
8143 return 0;
8144
Francois Romieuda78dbf2012-01-26 14:18:23 +01008145 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008146 __rtl8169_set_wol(tp, tp->saved_wolopts);
8147 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01008148 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008149
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00008150 rtl8169_init_phy(dev, tp);
8151
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008152 __rtl8169_resume(dev);
8153
8154 return 0;
8155}
8156
8157static int rtl8169_runtime_idle(struct device *device)
8158{
8159 struct pci_dev *pdev = to_pci_dev(device);
8160 struct net_device *dev = pci_get_drvdata(pdev);
8161 struct rtl8169_private *tp = netdev_priv(dev);
8162
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00008163 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00008164}
8165
Alexey Dobriyan47145212009-12-14 18:00:08 -08008166static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02008167 .suspend = rtl8169_suspend,
8168 .resume = rtl8169_resume,
8169 .freeze = rtl8169_suspend,
8170 .thaw = rtl8169_resume,
8171 .poweroff = rtl8169_suspend,
8172 .restore = rtl8169_resume,
8173 .runtime_suspend = rtl8169_runtime_suspend,
8174 .runtime_resume = rtl8169_runtime_resume,
8175 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008176};
8177
8178#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8179
8180#else /* !CONFIG_PM */
8181
8182#define RTL8169_PM_OPS NULL
8183
8184#endif /* !CONFIG_PM */
8185
David S. Miller1805b2f2011-10-24 18:18:09 -04008186static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8187{
8188 void __iomem *ioaddr = tp->mmio_addr;
8189
8190 /* WoL fails with 8168b when the receiver is disabled. */
8191 switch (tp->mac_version) {
8192 case RTL_GIGA_MAC_VER_11:
8193 case RTL_GIGA_MAC_VER_12:
8194 case RTL_GIGA_MAC_VER_17:
8195 pci_clear_master(tp->pci_dev);
8196
8197 RTL_W8(ChipCmd, CmdRxEnb);
8198 /* PCI commit */
8199 RTL_R8(ChipCmd);
8200 break;
8201 default:
8202 break;
8203 }
8204}
8205
Francois Romieu1765f952008-09-13 17:21:40 +02008206static void rtl_shutdown(struct pci_dev *pdev)
8207{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008208 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00008209 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00008210 struct device *d = &pdev->dev;
8211
8212 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02008213
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008214 rtl8169_net_suspend(dev);
8215
Francois Romieucecb5fd2011-04-01 10:21:07 +02008216 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08008217 rtl_rar_set(tp, dev->perm_addr);
8218
Hayes Wang92fc43b2011-07-06 15:58:03 +08008219 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00008220
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008221 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04008222 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8223 rtl_wol_suspend_quirk(tp);
8224 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00008225 }
8226
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008227 pci_wake_from_d3(pdev, true);
8228 pci_set_power_state(pdev, PCI_D3hot);
8229 }
françois romieu2a15cd22012-03-06 01:14:12 +00008230
8231 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008232}
Francois Romieu5d06a992006-02-23 00:47:58 +01008233
Bill Pembertonbaf63292012-12-03 09:23:28 -05008234static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01008235{
8236 struct net_device *dev = pci_get_drvdata(pdev);
8237 struct rtl8169_private *tp = netdev_priv(dev);
8238
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008239 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8240 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008241 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8242 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8243 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8244 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008245 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01008246 rtl8168_driver_stop(tp);
8247 }
8248
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008249 netif_napi_del(&tp->napi);
8250
Francois Romieue27566e2012-03-08 09:54:01 +01008251 unregister_netdev(dev);
8252
Corinna Vinschen42020322015-09-10 10:47:35 +02008253 dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
8254 tp->counters, tp->counters_phys_addr);
8255
Francois Romieue27566e2012-03-08 09:54:01 +01008256 rtl_release_firmware(tp);
8257
8258 if (pci_dev_run_wake(pdev))
8259 pm_runtime_get_noresume(&pdev->dev);
8260
8261 /* restore original MAC address */
8262 rtl_rar_set(tp, dev->perm_addr);
8263
8264 rtl_disable_msi(pdev, tp);
8265 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01008266}
8267
Francois Romieufa9c3852012-03-08 10:01:50 +01008268static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01008269 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01008270 .ndo_stop = rtl8169_close,
8271 .ndo_get_stats64 = rtl8169_get_stats64,
8272 .ndo_start_xmit = rtl8169_start_xmit,
8273 .ndo_tx_timeout = rtl8169_tx_timeout,
8274 .ndo_validate_addr = eth_validate_addr,
8275 .ndo_change_mtu = rtl8169_change_mtu,
8276 .ndo_fix_features = rtl8169_fix_features,
8277 .ndo_set_features = rtl8169_set_features,
8278 .ndo_set_mac_address = rtl_set_mac_address,
8279 .ndo_do_ioctl = rtl8169_ioctl,
8280 .ndo_set_rx_mode = rtl_set_rx_mode,
8281#ifdef CONFIG_NET_POLL_CONTROLLER
8282 .ndo_poll_controller = rtl8169_netpoll,
8283#endif
8284
8285};
8286
Francois Romieu31fa8b12012-03-08 10:09:40 +01008287static const struct rtl_cfg_info {
8288 void (*hw_start)(struct net_device *);
8289 unsigned int region;
8290 unsigned int align;
8291 u16 event_slow;
8292 unsigned features;
Francois Romieu50970832017-10-27 13:24:49 +03008293 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01008294 u8 default_ver;
8295} rtl_cfg_infos [] = {
8296 [RTL_CFG_0] = {
8297 .hw_start = rtl_hw_start_8169,
8298 .region = 1,
8299 .align = 0,
8300 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8301 .features = RTL_FEATURE_GMII,
Francois Romieu50970832017-10-27 13:24:49 +03008302 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008303 .default_ver = RTL_GIGA_MAC_VER_01,
8304 },
8305 [RTL_CFG_1] = {
8306 .hw_start = rtl_hw_start_8168,
8307 .region = 2,
8308 .align = 8,
8309 .event_slow = SYSErr | LinkChg | RxOverflow,
8310 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008311 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008312 .default_ver = RTL_GIGA_MAC_VER_11,
8313 },
8314 [RTL_CFG_2] = {
8315 .hw_start = rtl_hw_start_8101,
8316 .region = 2,
8317 .align = 8,
8318 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8319 PCSTimeout,
8320 .features = RTL_FEATURE_MSI,
Francois Romieu50970832017-10-27 13:24:49 +03008321 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01008322 .default_ver = RTL_GIGA_MAC_VER_13,
8323 }
8324};
8325
8326/* Cfg9346_Unlock assumed. */
8327static unsigned rtl_try_msi(struct rtl8169_private *tp,
8328 const struct rtl_cfg_info *cfg)
8329{
8330 void __iomem *ioaddr = tp->mmio_addr;
8331 unsigned msi = 0;
8332 u8 cfg2;
8333
8334 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8335 if (cfg->features & RTL_FEATURE_MSI) {
8336 if (pci_enable_msi(tp->pci_dev)) {
8337 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8338 } else {
8339 cfg2 |= MSIEnable;
8340 msi = RTL_FEATURE_MSI;
8341 }
8342 }
8343 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8344 RTL_W8(Config2, cfg2);
8345 return msi;
8346}
8347
Hayes Wangc5583862012-07-02 17:23:22 +08008348DECLARE_RTL_COND(rtl_link_list_ready_cond)
8349{
8350 void __iomem *ioaddr = tp->mmio_addr;
8351
8352 return RTL_R8(MCU) & LINK_LIST_RDY;
8353}
8354
8355DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8356{
8357 void __iomem *ioaddr = tp->mmio_addr;
8358
8359 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8360}
8361
Bill Pembertonbaf63292012-12-03 09:23:28 -05008362static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008363{
8364 void __iomem *ioaddr = tp->mmio_addr;
8365 u32 data;
8366
8367 tp->ocp_base = OCP_STD_PHY_BASE;
8368
8369 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8370
8371 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8372 return;
8373
8374 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8375 return;
8376
8377 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8378 msleep(1);
8379 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8380
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008381 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008382 data &= ~(1 << 14);
8383 r8168_mac_ocp_write(tp, 0xe8de, data);
8384
8385 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8386 return;
8387
Hayes Wang5f8bcce2012-07-10 08:47:05 +02008388 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08008389 data |= (1 << 15);
8390 r8168_mac_ocp_write(tp, 0xe8de, data);
8391
8392 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8393 return;
8394}
8395
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008396static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8397{
8398 rtl8168ep_stop_cmac(tp);
8399 rtl_hw_init_8168g(tp);
8400}
8401
Bill Pembertonbaf63292012-12-03 09:23:28 -05008402static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008403{
8404 switch (tp->mac_version) {
8405 case RTL_GIGA_MAC_VER_40:
8406 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008407 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008408 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008409 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008410 case RTL_GIGA_MAC_VER_45:
8411 case RTL_GIGA_MAC_VER_46:
8412 case RTL_GIGA_MAC_VER_47:
8413 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008414 rtl_hw_init_8168g(tp);
8415 break;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008416 case RTL_GIGA_MAC_VER_49:
8417 case RTL_GIGA_MAC_VER_50:
8418 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08008419 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08008420 break;
Hayes Wangc5583862012-07-02 17:23:22 +08008421 default:
8422 break;
8423 }
8424}
8425
hayeswang929a0312014-09-16 11:40:47 +08008426static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008427{
8428 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8429 const unsigned int region = cfg->region;
8430 struct rtl8169_private *tp;
8431 struct mii_if_info *mii;
8432 struct net_device *dev;
8433 void __iomem *ioaddr;
8434 int chipset, i;
8435 int rc;
8436
8437 if (netif_msg_drv(&debug)) {
8438 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8439 MODULENAME, RTL8169_VERSION);
8440 }
8441
8442 dev = alloc_etherdev(sizeof (*tp));
8443 if (!dev) {
8444 rc = -ENOMEM;
8445 goto out;
8446 }
8447
8448 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008449 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008450 tp = netdev_priv(dev);
8451 tp->dev = dev;
8452 tp->pci_dev = pdev;
8453 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8454
8455 mii = &tp->mii;
8456 mii->dev = dev;
8457 mii->mdio_read = rtl_mdio_read;
8458 mii->mdio_write = rtl_mdio_write;
8459 mii->phy_id_mask = 0x1f;
8460 mii->reg_num_mask = 0x1f;
8461 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8462
8463 /* disable ASPM completely as that cause random device stop working
8464 * problems as well as full system hangs for some PCIe devices users */
8465 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8466 PCIE_LINK_STATE_CLKPM);
8467
8468 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8469 rc = pci_enable_device(pdev);
8470 if (rc < 0) {
8471 netif_err(tp, probe, dev, "enable failure\n");
8472 goto err_out_free_dev_1;
8473 }
8474
8475 if (pci_set_mwi(pdev) < 0)
8476 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8477
8478 /* make sure PCI base addr 1 is MMIO */
8479 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8480 netif_err(tp, probe, dev,
8481 "region #%d not an MMIO resource, aborting\n",
8482 region);
8483 rc = -ENODEV;
8484 goto err_out_mwi_2;
8485 }
8486
8487 /* check for weird/broken PCI region reporting */
8488 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8489 netif_err(tp, probe, dev,
8490 "Invalid PCI region size(s), aborting\n");
8491 rc = -ENODEV;
8492 goto err_out_mwi_2;
8493 }
8494
8495 rc = pci_request_regions(pdev, MODULENAME);
8496 if (rc < 0) {
8497 netif_err(tp, probe, dev, "could not request regions\n");
8498 goto err_out_mwi_2;
8499 }
8500
Francois Romieu3b6cf252012-03-08 09:59:04 +01008501 /* ioremap MMIO region */
8502 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8503 if (!ioaddr) {
8504 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8505 rc = -EIO;
8506 goto err_out_free_res_3;
8507 }
8508 tp->mmio_addr = ioaddr;
8509
8510 if (!pci_is_pcie(pdev))
8511 netif_info(tp, probe, dev, "not PCI Express\n");
8512
8513 /* Identify chip attached to board */
8514 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8515
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008516 tp->cp_cmd = 0;
8517
8518 if ((sizeof(dma_addr_t) > 4) &&
8519 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8520 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01008521 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8522 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008523
8524 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8525 if (!pci_is_pcie(pdev))
8526 tp->cp_cmd |= PCIDAC;
8527 dev->features |= NETIF_F_HIGHDMA;
8528 } else {
8529 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8530 if (rc < 0) {
8531 netif_err(tp, probe, dev, "DMA configuration failed\n");
8532 goto err_out_unmap_4;
8533 }
8534 }
8535
Francois Romieu3b6cf252012-03-08 09:59:04 +01008536 rtl_init_rxcfg(tp);
8537
8538 rtl_irq_disable(tp);
8539
Hayes Wangc5583862012-07-02 17:23:22 +08008540 rtl_hw_initialize(tp);
8541
Francois Romieu3b6cf252012-03-08 09:59:04 +01008542 rtl_hw_reset(tp);
8543
8544 rtl_ack_events(tp, 0xffff);
8545
8546 pci_set_master(pdev);
8547
Francois Romieu3b6cf252012-03-08 09:59:04 +01008548 rtl_init_mdio_ops(tp);
8549 rtl_init_pll_power_ops(tp);
8550 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008551 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008552
8553 rtl8169_print_mac_version(tp);
8554
8555 chipset = tp->mac_version;
8556 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8557
8558 RTL_W8(Cfg9346, Cfg9346_Unlock);
8559 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02008560 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008561 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008562 case RTL_GIGA_MAC_VER_34:
8563 case RTL_GIGA_MAC_VER_35:
8564 case RTL_GIGA_MAC_VER_36:
8565 case RTL_GIGA_MAC_VER_37:
8566 case RTL_GIGA_MAC_VER_38:
8567 case RTL_GIGA_MAC_VER_40:
8568 case RTL_GIGA_MAC_VER_41:
8569 case RTL_GIGA_MAC_VER_42:
8570 case RTL_GIGA_MAC_VER_43:
8571 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008572 case RTL_GIGA_MAC_VER_45:
8573 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008574 case RTL_GIGA_MAC_VER_47:
8575 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008576 case RTL_GIGA_MAC_VER_49:
8577 case RTL_GIGA_MAC_VER_50:
8578 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008579 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8580 tp->features |= RTL_FEATURE_WOL;
8581 if ((RTL_R8(Config3) & LinkUp) != 0)
8582 tp->features |= RTL_FEATURE_WOL;
8583 break;
8584 default:
8585 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8586 tp->features |= RTL_FEATURE_WOL;
8587 break;
8588 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008589 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8590 tp->features |= RTL_FEATURE_WOL;
8591 tp->features |= rtl_try_msi(tp, cfg);
8592 RTL_W8(Cfg9346, Cfg9346_Lock);
8593
8594 if (rtl_tbi_enabled(tp)) {
8595 tp->set_speed = rtl8169_set_speed_tbi;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008596 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008597 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8598 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8599 tp->link_ok = rtl8169_tbi_link_ok;
8600 tp->do_ioctl = rtl_tbi_ioctl;
8601 } else {
8602 tp->set_speed = rtl8169_set_speed_xmii;
Philippe Reynes6fa1ba62017-02-23 22:34:43 +01008603 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008604 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8605 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8606 tp->link_ok = rtl8169_xmii_link_ok;
8607 tp->do_ioctl = rtl_xmii_ioctl;
8608 }
8609
8610 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008611 u64_stats_init(&tp->rx_stats.syncp);
8612 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008613
8614 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008615 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8616 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8617 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8618 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8619 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8620 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8621 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8622 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8623 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8624 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008625 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8626 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008627 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8628 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8629 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8630 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008631 u16 mac_addr[3];
8632
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008633 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8634 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008635
8636 if (is_valid_ether_addr((u8 *)mac_addr))
8637 rtl_rar_set(tp, (u8 *)mac_addr);
8638 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008639 for (i = 0; i < ETH_ALEN; i++)
8640 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008641
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008642 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008643 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008644
8645 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8646
8647 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8648 * properly for all devices */
8649 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008650 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008651
8652 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008653 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8654 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008655 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8656 NETIF_F_HIGHDMA;
8657
hayeswang929a0312014-09-16 11:40:47 +08008658 tp->cp_cmd |= RxChkSum | RxVlan;
8659
8660 /*
8661 * Pretend we are using VLANs; This bypasses a nasty bug where
8662 * Interrupts stop flowing on high load on 8110SCd controllers.
8663 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008664 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008665 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008666 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008667
hayeswang5888d3f2014-07-11 16:25:56 +08008668 if (tp->txd_version == RTL_TD_0)
8669 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008670 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008671 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008672 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8673 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008674 WARN_ON_ONCE(1);
8675
Francois Romieu3b6cf252012-03-08 09:59:04 +01008676 dev->hw_features |= NETIF_F_RXALL;
8677 dev->hw_features |= NETIF_F_RXFCS;
8678
Jarod Wilsonc7315a92016-10-17 15:54:09 -04008679 /* MTU range: 60 - hw-specific max */
8680 dev->min_mtu = ETH_ZLEN;
8681 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8682
Francois Romieu3b6cf252012-03-08 09:59:04 +01008683 tp->hw_start = cfg->hw_start;
8684 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03008685 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008686
8687 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8688 ~(RxBOVF | RxFOVF) : ~0;
8689
Kees Cook9de36cc2017-10-25 03:53:12 -07008690 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008691
8692 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8693
Corinna Vinschen42020322015-09-10 10:47:35 +02008694 tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8695 &tp->counters_phys_addr, GFP_KERNEL);
8696 if (!tp->counters) {
8697 rc = -ENOMEM;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008698 goto err_out_msi_5;
Corinna Vinschen42020322015-09-10 10:47:35 +02008699 }
8700
Francois Romieu3b6cf252012-03-08 09:59:04 +01008701 rc = register_netdev(dev);
8702 if (rc < 0)
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008703 goto err_out_cnt_6;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008704
8705 pci_set_drvdata(pdev, dev);
8706
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008707 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8708 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8709 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008710 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8711 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8712 "tx checksumming: %s]\n",
8713 rtl_chip_infos[chipset].jumbo_max,
8714 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8715 }
8716
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008717 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8718 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008719 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8720 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8721 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8722 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008723 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008724 rtl8168_driver_start(tp);
8725 }
8726
Francois Romieu3b6cf252012-03-08 09:59:04 +01008727 if (pci_dev_run_wake(pdev))
8728 pm_runtime_put_noidle(&pdev->dev);
8729
8730 netif_carrier_off(dev);
8731
8732out:
8733 return rc;
8734
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008735err_out_cnt_6:
Corinna Vinschen42020322015-09-10 10:47:35 +02008736 dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
8737 tp->counters_phys_addr);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008738err_out_msi_5:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008739 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008740 rtl_disable_msi(pdev, tp);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02008741err_out_unmap_4:
Francois Romieu3b6cf252012-03-08 09:59:04 +01008742 iounmap(ioaddr);
8743err_out_free_res_3:
8744 pci_release_regions(pdev);
8745err_out_mwi_2:
8746 pci_clear_mwi(pdev);
8747 pci_disable_device(pdev);
8748err_out_free_dev_1:
8749 free_netdev(dev);
8750 goto out;
8751}
8752
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753static struct pci_driver rtl8169_pci_driver = {
8754 .name = MODULENAME,
8755 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008756 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008757 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008758 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008759 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008760};
8761
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008762module_pci_driver(rtl8169_pci_driver);