blob: 7f4515f5a82c7839d5b16a40bcbfeb36649d3296 [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 },
Francois Romieud81bf552006-09-20 21:31:20 +0200329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200332 { PCI_VENDOR_ID_DLINK, 0x4300,
333 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200334 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200336 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200337 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
338 { PCI_VENDOR_ID_LINKSYS, 0x1032,
339 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100340 { 0x0001, 0x8168,
341 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 {0,},
343};
344
345MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
346
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000347static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700348static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200349static struct {
350 u32 msg_enable;
351} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Francois Romieu07d3f512007-02-21 22:40:46 +0100353enum rtl_registers {
354 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100355 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 MAR0 = 8, /* Multicast filter. */
357 CounterAddrLow = 0x10,
358 CounterAddrHigh = 0x14,
359 TxDescStartAddrLow = 0x20,
360 TxDescStartAddrHigh = 0x24,
361 TxHDescStartAddrLow = 0x28,
362 TxHDescStartAddrHigh = 0x2c,
363 FLASH = 0x30,
364 ERSR = 0x36,
365 ChipCmd = 0x37,
366 TxPoll = 0x38,
367 IntrMask = 0x3c,
368 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700369
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800370 TxConfig = 0x40,
371#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
372#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
373
374 RxConfig = 0x44,
375#define RX128_INT_EN (1 << 15) /* 8111c and later */
376#define RX_MULTI_EN (1 << 14) /* 8111c only */
377#define RXCFG_FIFO_SHIFT 13
378 /* No threshold before first PCI xfer */
379#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000380#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800381#define RXCFG_DMA_SHIFT 8
382 /* Unlimited maximum PCI burst. */
383#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700384
Francois Romieu07d3f512007-02-21 22:40:46 +0100385 RxMissed = 0x4c,
386 Cfg9346 = 0x50,
387 Config0 = 0x51,
388 Config1 = 0x52,
389 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200390#define PME_SIGNAL (1 << 5) /* 8168c and later */
391
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 Config3 = 0x54,
393 Config4 = 0x55,
394 Config5 = 0x56,
395 MultiIntr = 0x5c,
396 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100397 PHYstatus = 0x6c,
398 RxMaxSize = 0xda,
399 CPlusCmd = 0xe0,
400 IntrMitigate = 0xe2,
401 RxDescAddrLow = 0xe4,
402 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000403 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
404
405#define NoEarlyTx 0x3f /* Max value : no early transmit. */
406
407 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
408
409#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800410#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000411
Francois Romieu07d3f512007-02-21 22:40:46 +0100412 FuncEvent = 0xf0,
413 FuncEventMask = 0xf4,
414 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800415 IBCR0 = 0xf8,
416 IBCR2 = 0xf9,
417 IBIMR0 = 0xfa,
418 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Francois Romieuf162a5d2008-06-01 22:37:49 +0200422enum rtl8110_registers {
423 TBICSR = 0x64,
424 TBI_ANAR = 0x68,
425 TBI_LPAR = 0x6a,
426};
427
428enum rtl8168_8101_registers {
429 CSIDR = 0x64,
430 CSIAR = 0x68,
431#define CSIAR_FLAG 0x80000000
432#define CSIAR_WRITE_CMD 0x80000000
433#define CSIAR_BYTE_ENABLE 0x0f
434#define CSIAR_BYTE_ENABLE_SHIFT 12
435#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800436#define CSIAR_FUNC_CARD 0x00000000
437#define CSIAR_FUNC_SDIO 0x00010000
438#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800439#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000440 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200441 EPHYAR = 0x80,
442#define EPHYAR_FLAG 0x80000000
443#define EPHYAR_WRITE_CMD 0x80000000
444#define EPHYAR_REG_MASK 0x1f
445#define EPHYAR_REG_SHIFT 16
446#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800447 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800448#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800449#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200450 DBG_REG = 0xd1,
451#define FIX_NAK_1 (1 << 4)
452#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800453 TWSI = 0xd2,
454 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800455#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800456#define TX_EMPTY (1 << 5)
457#define RX_EMPTY (1 << 4)
458#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800459#define EN_NDP (1 << 3)
460#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800461#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000462 EFUSEAR = 0xdc,
463#define EFUSEAR_FLAG 0x80000000
464#define EFUSEAR_WRITE_CMD 0x80000000
465#define EFUSEAR_READ_CMD 0x00000000
466#define EFUSEAR_REG_MASK 0x03ff
467#define EFUSEAR_REG_SHIFT 8
468#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800469 MISC_1 = 0xf2,
470#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471};
472
françois romieuc0e45c12011-01-03 15:08:04 +0000473enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800474 LED_FREQ = 0x1a,
475 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000476 ERIDR = 0x70,
477 ERIAR = 0x74,
478#define ERIAR_FLAG 0x80000000
479#define ERIAR_WRITE_CMD 0x80000000
480#define ERIAR_READ_CMD 0x00000000
481#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000482#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800483#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
484#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
485#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800486#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800487#define ERIAR_MASK_SHIFT 12
488#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
489#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800490#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800491#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800492#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000493 EPHY_RXER_NUM = 0x7c,
494 OCPDR = 0xb0, /* OCP GPHY access */
495#define OCPDR_WRITE_CMD 0x80000000
496#define OCPDR_READ_CMD 0x00000000
497#define OCPDR_REG_MASK 0x7f
498#define OCPDR_GPHY_REG_SHIFT 16
499#define OCPDR_DATA_MASK 0xffff
500 OCPAR = 0xb4,
501#define OCPAR_FLAG 0x80000000
502#define OCPAR_GPHY_WRITE_CMD 0x8000f060
503#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800504 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000505 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
506 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200507#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800508#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800509#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800510#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800511#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000512};
513
Francois Romieu07d3f512007-02-21 22:40:46 +0100514enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100516 SYSErr = 0x8000,
517 PCSTimeout = 0x4000,
518 SWInt = 0x0100,
519 TxDescUnavail = 0x0080,
520 RxFIFOOver = 0x0040,
521 LinkChg = 0x0020,
522 RxOverflow = 0x0010,
523 TxErr = 0x0008,
524 TxOK = 0x0004,
525 RxErr = 0x0002,
526 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400529 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200530 RxFOVF = (1 << 23),
531 RxRWT = (1 << 22),
532 RxRES = (1 << 21),
533 RxRUNT = (1 << 20),
534 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800537 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100538 CmdReset = 0x10,
539 CmdRxEnb = 0x08,
540 CmdTxEnb = 0x04,
541 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Francois Romieu275391a2007-02-23 23:50:28 +0100543 /* TXPoll register p.5 */
544 HPQ = 0x80, /* Poll cmd on the high prio queue */
545 NPQ = 0x40, /* Poll cmd on the low prio queue */
546 FSWInt = 0x01, /* Forced software interrupt */
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100549 Cfg9346_Lock = 0x00,
550 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100553 AcceptErr = 0x20,
554 AcceptRunt = 0x10,
555 AcceptBroadcast = 0x08,
556 AcceptMulticast = 0x04,
557 AcceptMyPhys = 0x02,
558 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200559#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* TxConfigBits */
562 TxInterFrameGapShift = 24,
563 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
564
Francois Romieu5d06a992006-02-23 00:47:58 +0100565 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200566 LEDS1 = (1 << 7),
567 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200568 Speed_down = (1 << 4),
569 MEMMAP = (1 << 3),
570 IOMAP = (1 << 2),
571 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100572 PMEnable = (1 << 0), /* Power Management Enable */
573
Francois Romieu6dccd162007-02-13 23:38:05 +0100574 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000575 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000576 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100577 PCI_Clock_66MHz = 0x01,
578 PCI_Clock_33MHz = 0x00,
579
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100580 /* Config3 register p.25 */
581 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
582 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200583 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800584 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200585 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100586
Francois Romieud58d46b2011-05-03 16:38:29 +0200587 /* Config4 register */
588 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
589
Francois Romieu5d06a992006-02-23 00:47:58 +0100590 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100591 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
592 MWF = (1 << 5), /* Accept Multicast wakeup frame */
593 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200594 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100595 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100596 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000597 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* TBICSR p.28 */
600 TBIReset = 0x80000000,
601 TBILoopback = 0x40000000,
602 TBINwEnable = 0x20000000,
603 TBINwRestart = 0x10000000,
604 TBILinkOk = 0x02000000,
605 TBINwComplete = 0x01000000,
606
607 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200608 EnableBist = (1 << 15), // 8168 8101
609 Mac_dbgo_oe = (1 << 14), // 8168 8101
610 Normal_mode = (1 << 13), // unused
611 Force_half_dup = (1 << 12), // 8168 8101
612 Force_rxflow_en = (1 << 11), // 8168 8101
613 Force_txflow_en = (1 << 10), // 8168 8101
614 Cxpl_dbg_sel = (1 << 9), // 8168 8101
615 ASF = (1 << 8), // 8168 8101
616 PktCntrDisable = (1 << 7), // 8168 8101
617 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 RxVlan = (1 << 6),
619 RxChkSum = (1 << 5),
620 PCIDAC = (1 << 4),
621 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100622 INTT_0 = 0x0000, // 8168
623 INTT_1 = 0x0001, // 8168
624 INTT_2 = 0x0002, // 8168
625 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100628 TBI_Enable = 0x80,
629 TxFlowCtrl = 0x40,
630 RxFlowCtrl = 0x20,
631 _1000bpsF = 0x10,
632 _100bps = 0x08,
633 _10bps = 0x04,
634 LinkStatus = 0x02,
635 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100638 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200639
640 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100641 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800642
643 /* magic enable v2 */
644 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};
646
Francois Romieu2b7b4312011-04-18 22:53:24 -0700647enum rtl_desc_bit {
648 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
650 RingEnd = (1 << 30), /* End of descriptor ring */
651 FirstFrag = (1 << 29), /* First segment of a packet */
652 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700653};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Francois Romieu2b7b4312011-04-18 22:53:24 -0700655/* Generic case. */
656enum rtl_tx_desc_bit {
657 /* First doubleword. */
658 TD_LSO = (1 << 27), /* Large Send Offload */
659#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Francois Romieu2b7b4312011-04-18 22:53:24 -0700661 /* Second doubleword. */
662 TxVlanTag = (1 << 17), /* Add VLAN tag */
663};
664
665/* 8169, 8168b and 810x except 8102e. */
666enum rtl_tx_desc_bit_0 {
667 /* First doubleword. */
668#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
669 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
670 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
671 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
672};
673
674/* 8102e, 8168c and beyond. */
675enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800676 /* First doubleword. */
677 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800678 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800679#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800680#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800681
Francois Romieu2b7b4312011-04-18 22:53:24 -0700682 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800683#define TCPHO_SHIFT 18
684#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700685#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800686 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
687 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700688 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
689 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
690};
691
Francois Romieu2b7b4312011-04-18 22:53:24 -0700692enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* Rx private */
694 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
695 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
696
697#define RxProtoUDP (PID1)
698#define RxProtoTCP (PID0)
699#define RxProtoIP (PID1 | PID0)
700#define RxProtoMask RxProtoIP
701
702 IPFail = (1 << 16), /* IP checksum failed */
703 UDPFail = (1 << 15), /* UDP/IP checksum failed */
704 TCPFail = (1 << 14), /* TCP/IP checksum failed */
705 RxVlanTag = (1 << 16), /* VLAN tag available */
706};
707
708#define RsvdMask 0x3fffc000
709
710struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200711 __le32 opts1;
712 __le32 opts2;
713 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
716struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200717 __le32 opts1;
718 __le32 opts2;
719 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
722struct ring_info {
723 struct sk_buff *skb;
724 u32 len;
725 u8 __pad[sizeof(void *) - sizeof(u32)];
726};
727
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200728enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200729 RTL_FEATURE_WOL = (1 << 0),
730 RTL_FEATURE_MSI = (1 << 1),
731 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200732};
733
Ivan Vecera355423d2009-02-06 21:49:57 -0800734struct rtl8169_counters {
735 __le64 tx_packets;
736 __le64 rx_packets;
737 __le64 tx_errors;
738 __le32 rx_errors;
739 __le16 rx_missed;
740 __le16 align_errors;
741 __le32 tx_one_collision;
742 __le32 tx_multi_collision;
743 __le64 rx_unicast;
744 __le64 rx_broadcast;
745 __le32 rx_multicast;
746 __le16 tx_aborted;
747 __le16 tx_underun;
748};
749
Francois Romieuda78dbf2012-01-26 14:18:23 +0100750enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100751 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100752 RTL_FLAG_TASK_SLOW_PENDING,
753 RTL_FLAG_TASK_RESET_PENDING,
754 RTL_FLAG_TASK_PHY_PENDING,
755 RTL_FLAG_MAX
756};
757
Junchang Wang8027aa22012-03-04 23:30:32 +0100758struct rtl8169_stats {
759 u64 packets;
760 u64 bytes;
761 struct u64_stats_sync syncp;
762};
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764struct rtl8169_private {
765 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200766 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000767 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700768 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200769 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700770 u16 txd_version;
771 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
773 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100775 struct rtl8169_stats rx_stats;
776 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
778 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
779 dma_addr_t TxPhyAddr;
780 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000781 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct timer_list timer;
784 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100785
786 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000787
788 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200789 void (*write)(struct rtl8169_private *, int, int);
790 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000791 } mdio_ops;
792
françois romieu065c27c2011-01-03 15:08:12 +0000793 struct pll_power_ops {
794 void (*down)(struct rtl8169_private *);
795 void (*up)(struct rtl8169_private *);
796 } pll_power_ops;
797
Francois Romieud58d46b2011-05-03 16:38:29 +0200798 struct jumbo_ops {
799 void (*enable)(struct rtl8169_private *);
800 void (*disable)(struct rtl8169_private *);
801 } jumbo_ops;
802
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800803 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200804 void (*write)(struct rtl8169_private *, int, int);
805 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800806 } csi_ops;
807
Oliver Neukum54405cd2011-01-06 21:55:13 +0100808 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200809 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000810 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100811 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000812 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800814 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800815 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100816
817 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100818 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
819 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100820 struct work_struct work;
821 } wk;
822
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200823 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200824
825 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800826 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000827 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400828 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000829
Francois Romieub6ffd972011-06-17 17:00:05 +0200830 struct rtl_fw {
831 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200832
833#define RTL_VER_SIZE 32
834
835 char version[RTL_VER_SIZE];
836
837 struct rtl_fw_phy_action {
838 __le32 *code;
839 size_t size;
840 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200841 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300842#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800843
844 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845};
846
Ralf Baechle979b6c12005-06-13 14:30:40 -0700847MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700850MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200851module_param_named(debug, debug.msg_enable, int, 0);
852MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853MODULE_LICENSE("GPL");
854MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000855MODULE_FIRMWARE(FIRMWARE_8168D_1);
856MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000857MODULE_FIRMWARE(FIRMWARE_8168E_1);
858MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400859MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800860MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800861MODULE_FIRMWARE(FIRMWARE_8168F_1);
862MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800863MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800864MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800865MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800866MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000867MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000868MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000869MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800870MODULE_FIRMWARE(FIRMWARE_8168H_1);
871MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200872MODULE_FIRMWARE(FIRMWARE_8107E_1);
873MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Francois Romieuda78dbf2012-01-26 14:18:23 +0100875static void rtl_lock_work(struct rtl8169_private *tp)
876{
877 mutex_lock(&tp->wk.mutex);
878}
879
880static void rtl_unlock_work(struct rtl8169_private *tp)
881{
882 mutex_unlock(&tp->wk.mutex);
883}
884
Francois Romieud58d46b2011-05-03 16:38:29 +0200885static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
886{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800887 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
888 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200889}
890
Francois Romieuffc46952012-07-06 14:19:23 +0200891struct rtl_cond {
892 bool (*check)(struct rtl8169_private *);
893 const char *msg;
894};
895
896static void rtl_udelay(unsigned int d)
897{
898 udelay(d);
899}
900
901static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
902 void (*delay)(unsigned int), unsigned int d, int n,
903 bool high)
904{
905 int i;
906
907 for (i = 0; i < n; i++) {
908 delay(d);
909 if (c->check(tp) == high)
910 return true;
911 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200912 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
913 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200914 return false;
915}
916
917static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
918 const struct rtl_cond *c,
919 unsigned int d, int n)
920{
921 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
922}
923
924static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
925 const struct rtl_cond *c,
926 unsigned int d, int n)
927{
928 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
929}
930
931static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
932 const struct rtl_cond *c,
933 unsigned int d, int n)
934{
935 return rtl_loop_wait(tp, c, msleep, d, n, true);
936}
937
938static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
939 const struct rtl_cond *c,
940 unsigned int d, int n)
941{
942 return rtl_loop_wait(tp, c, msleep, d, n, false);
943}
944
945#define DECLARE_RTL_COND(name) \
946static bool name ## _check(struct rtl8169_private *); \
947 \
948static const struct rtl_cond name = { \
949 .check = name ## _check, \
950 .msg = #name \
951}; \
952 \
953static bool name ## _check(struct rtl8169_private *tp)
954
Hayes Wangc5583862012-07-02 17:23:22 +0800955static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
956{
957 if (reg & 0xffff0001) {
958 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
959 return true;
960 }
961 return false;
962}
963
964DECLARE_RTL_COND(rtl_ocp_gphy_cond)
965{
966 void __iomem *ioaddr = tp->mmio_addr;
967
968 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
969}
970
971static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
972{
973 void __iomem *ioaddr = tp->mmio_addr;
974
975 if (rtl_ocp_reg_failure(tp, reg))
976 return;
977
978 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
979
980 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
981}
982
983static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
984{
985 void __iomem *ioaddr = tp->mmio_addr;
986
987 if (rtl_ocp_reg_failure(tp, reg))
988 return 0;
989
990 RTL_W32(GPHY_OCP, reg << 15);
991
992 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
993 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
994}
995
Hayes Wangc5583862012-07-02 17:23:22 +0800996static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
997{
998 void __iomem *ioaddr = tp->mmio_addr;
999
1000 if (rtl_ocp_reg_failure(tp, reg))
1001 return;
1002
1003 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001004}
1005
1006static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1007{
1008 void __iomem *ioaddr = tp->mmio_addr;
1009
1010 if (rtl_ocp_reg_failure(tp, reg))
1011 return 0;
1012
1013 RTL_W32(OCPDR, reg << 15);
1014
Hayes Wang3a83ad12012-07-11 20:31:56 +08001015 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001016}
1017
1018#define OCP_STD_PHY_BASE 0xa400
1019
1020static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1021{
1022 if (reg == 0x1f) {
1023 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1024 return;
1025 }
1026
1027 if (tp->ocp_base != OCP_STD_PHY_BASE)
1028 reg -= 0x10;
1029
1030 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1031}
1032
1033static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1034{
1035 if (tp->ocp_base != OCP_STD_PHY_BASE)
1036 reg -= 0x10;
1037
1038 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1039}
1040
hayeswangeee37862013-04-01 22:23:38 +00001041static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1042{
1043 if (reg == 0x1f) {
1044 tp->ocp_base = value << 4;
1045 return;
1046 }
1047
1048 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1049}
1050
1051static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1052{
1053 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1054}
1055
Francois Romieuffc46952012-07-06 14:19:23 +02001056DECLARE_RTL_COND(rtl_phyar_cond)
1057{
1058 void __iomem *ioaddr = tp->mmio_addr;
1059
1060 return RTL_R32(PHYAR) & 0x80000000;
1061}
1062
Francois Romieu24192212012-07-06 20:19:42 +02001063static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Francois Romieu24192212012-07-06 20:19:42 +02001065 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Francois Romieu24192212012-07-06 20:19:42 +02001067 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Francois Romieuffc46952012-07-06 14:19:23 +02001069 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001070 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001071 * According to hardware specs a 20us delay is required after write
1072 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001073 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001074 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Francois Romieu24192212012-07-06 20:19:42 +02001077static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Francois Romieu24192212012-07-06 20:19:42 +02001079 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001080 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Francois Romieu24192212012-07-06 20:19:42 +02001082 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Francois Romieuffc46952012-07-06 14:19:23 +02001084 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1085 RTL_R32(PHYAR) & 0xffff : ~0;
1086
Timo Teräs81a95f02010-06-09 17:31:48 -07001087 /*
1088 * According to hardware specs a 20us delay is required after read
1089 * complete indication, but before sending next command.
1090 */
1091 udelay(20);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return value;
1094}
1095
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001096DECLARE_RTL_COND(rtl_ocpar_cond)
1097{
1098 void __iomem *ioaddr = tp->mmio_addr;
1099
1100 return RTL_R32(OCPAR) & OCPAR_FLAG;
1101}
1102
Francois Romieu24192212012-07-06 20:19:42 +02001103static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001104{
Francois Romieu24192212012-07-06 20:19:42 +02001105 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001106
Francois Romieu24192212012-07-06 20:19:42 +02001107 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001108 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1109 RTL_W32(EPHY_RXER_NUM, 0);
1110
Francois Romieuffc46952012-07-06 14:19:23 +02001111 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001112}
1113
Francois Romieu24192212012-07-06 20:19:42 +02001114static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001115{
Francois Romieu24192212012-07-06 20:19:42 +02001116 r8168dp_1_mdio_access(tp, reg,
1117 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001118}
1119
Francois Romieu24192212012-07-06 20:19:42 +02001120static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001121{
Francois Romieu24192212012-07-06 20:19:42 +02001122 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001123
Francois Romieu24192212012-07-06 20:19:42 +02001124 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001125
1126 mdelay(1);
1127 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1128 RTL_W32(EPHY_RXER_NUM, 0);
1129
Francois Romieuffc46952012-07-06 14:19:23 +02001130 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1131 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001132}
1133
françois romieue6de30d2011-01-03 15:08:37 +00001134#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1135
1136static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1137{
1138 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1139}
1140
1141static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1142{
1143 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1144}
1145
Francois Romieu24192212012-07-06 20:19:42 +02001146static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001147{
Francois Romieu24192212012-07-06 20:19:42 +02001148 void __iomem *ioaddr = tp->mmio_addr;
1149
françois romieue6de30d2011-01-03 15:08:37 +00001150 r8168dp_2_mdio_start(ioaddr);
1151
Francois Romieu24192212012-07-06 20:19:42 +02001152 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001153
1154 r8168dp_2_mdio_stop(ioaddr);
1155}
1156
Francois Romieu24192212012-07-06 20:19:42 +02001157static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001158{
Francois Romieu24192212012-07-06 20:19:42 +02001159 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001160 int value;
1161
1162 r8168dp_2_mdio_start(ioaddr);
1163
Francois Romieu24192212012-07-06 20:19:42 +02001164 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001165
1166 r8168dp_2_mdio_stop(ioaddr);
1167
1168 return value;
1169}
1170
françois romieu4da19632011-01-03 15:07:55 +00001171static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001172{
Francois Romieu24192212012-07-06 20:19:42 +02001173 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001174}
1175
françois romieu4da19632011-01-03 15:07:55 +00001176static int rtl_readphy(struct rtl8169_private *tp, int location)
1177{
Francois Romieu24192212012-07-06 20:19:42 +02001178 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001179}
1180
1181static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1182{
1183 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1184}
1185
Chun-Hao Lin76564422014-10-01 23:17:17 +08001186static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001187{
1188 int val;
1189
françois romieu4da19632011-01-03 15:07:55 +00001190 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001191 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001192}
1193
Francois Romieuccdffb92008-07-26 14:26:06 +02001194static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1195 int val)
1196{
1197 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001198
françois romieu4da19632011-01-03 15:07:55 +00001199 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001200}
1201
1202static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1203{
1204 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001205
françois romieu4da19632011-01-03 15:07:55 +00001206 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001207}
1208
Francois Romieuffc46952012-07-06 14:19:23 +02001209DECLARE_RTL_COND(rtl_ephyar_cond)
1210{
1211 void __iomem *ioaddr = tp->mmio_addr;
1212
1213 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1214}
1215
Francois Romieufdf6fc02012-07-06 22:40:38 +02001216static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001217{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001218 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001219
1220 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1221 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1222
Francois Romieuffc46952012-07-06 14:19:23 +02001223 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1224
1225 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001226}
1227
Francois Romieufdf6fc02012-07-06 22:40:38 +02001228static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001229{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001230 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001231
1232 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1233
Francois Romieuffc46952012-07-06 14:19:23 +02001234 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1235 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001236}
1237
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001238DECLARE_RTL_COND(rtl_eriar_cond)
1239{
1240 void __iomem *ioaddr = tp->mmio_addr;
1241
1242 return RTL_R32(ERIAR) & ERIAR_FLAG;
1243}
1244
Francois Romieufdf6fc02012-07-06 22:40:38 +02001245static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1246 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001247{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001249
1250 BUG_ON((addr & 3) || (mask == 0));
1251 RTL_W32(ERIDR, val);
1252 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1253
Francois Romieuffc46952012-07-06 14:19:23 +02001254 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001255}
1256
Francois Romieufdf6fc02012-07-06 22:40:38 +02001257static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001258{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001259 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001260
1261 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1262
Francois Romieuffc46952012-07-06 14:19:23 +02001263 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1264 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001265}
1266
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001267static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001268 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001269{
1270 u32 val;
1271
Francois Romieufdf6fc02012-07-06 22:40:38 +02001272 val = rtl_eri_read(tp, addr, type);
1273 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001274}
1275
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001276static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1277{
1278 void __iomem *ioaddr = tp->mmio_addr;
1279
1280 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1281 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1282 RTL_R32(OCPDR) : ~0;
1283}
1284
1285static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1286{
1287 return rtl_eri_read(tp, reg, ERIAR_OOB);
1288}
1289
1290static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1291{
1292 switch (tp->mac_version) {
1293 case RTL_GIGA_MAC_VER_27:
1294 case RTL_GIGA_MAC_VER_28:
1295 case RTL_GIGA_MAC_VER_31:
1296 return r8168dp_ocp_read(tp, mask, reg);
1297 case RTL_GIGA_MAC_VER_49:
1298 case RTL_GIGA_MAC_VER_50:
1299 case RTL_GIGA_MAC_VER_51:
1300 return r8168ep_ocp_read(tp, mask, reg);
1301 default:
1302 BUG();
1303 return ~0;
1304 }
1305}
1306
1307static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1308 u32 data)
1309{
1310 void __iomem *ioaddr = tp->mmio_addr;
1311
1312 RTL_W32(OCPDR, data);
1313 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1314 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1315}
1316
1317static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1318 u32 data)
1319{
1320 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1321 data, ERIAR_OOB);
1322}
1323
1324static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1325{
1326 switch (tp->mac_version) {
1327 case RTL_GIGA_MAC_VER_27:
1328 case RTL_GIGA_MAC_VER_28:
1329 case RTL_GIGA_MAC_VER_31:
1330 r8168dp_ocp_write(tp, mask, reg, data);
1331 break;
1332 case RTL_GIGA_MAC_VER_49:
1333 case RTL_GIGA_MAC_VER_50:
1334 case RTL_GIGA_MAC_VER_51:
1335 r8168ep_ocp_write(tp, mask, reg, data);
1336 break;
1337 default:
1338 BUG();
1339 break;
1340 }
1341}
1342
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001343static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1344{
1345 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1346
1347 ocp_write(tp, 0x1, 0x30, 0x00000001);
1348}
1349
1350#define OOB_CMD_RESET 0x00
1351#define OOB_CMD_DRIVER_START 0x05
1352#define OOB_CMD_DRIVER_STOP 0x06
1353
1354static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1355{
1356 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1357}
1358
1359DECLARE_RTL_COND(rtl_ocp_read_cond)
1360{
1361 u16 reg;
1362
1363 reg = rtl8168_get_ocp_reg(tp);
1364
1365 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1366}
1367
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001368DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1369{
1370 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1371}
1372
1373DECLARE_RTL_COND(rtl_ocp_tx_cond)
1374{
1375 void __iomem *ioaddr = tp->mmio_addr;
1376
1377 return RTL_R8(IBISR0) & 0x02;
1378}
1379
1380static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001381{
1382 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001383 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1384}
1385
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001386static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1387{
1388 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1389 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1390 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1391}
1392
1393static void rtl8168_driver_start(struct rtl8169_private *tp)
1394{
1395 switch (tp->mac_version) {
1396 case RTL_GIGA_MAC_VER_27:
1397 case RTL_GIGA_MAC_VER_28:
1398 case RTL_GIGA_MAC_VER_31:
1399 rtl8168dp_driver_start(tp);
1400 break;
1401 case RTL_GIGA_MAC_VER_49:
1402 case RTL_GIGA_MAC_VER_50:
1403 case RTL_GIGA_MAC_VER_51:
1404 rtl8168ep_driver_start(tp);
1405 break;
1406 default:
1407 BUG();
1408 break;
1409 }
1410}
1411
1412static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1413{
1414 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1415 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1416}
1417
1418static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1419{
1420 void __iomem *ioaddr = tp->mmio_addr;
1421
1422 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
1423 rtl_msleep_loop_wait_low(tp, &rtl_ocp_tx_cond, 50, 2000);
1424 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1425 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1426 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1427 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1428 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1429}
1430
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001431static void rtl8168_driver_stop(struct rtl8169_private *tp)
1432{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001433 switch (tp->mac_version) {
1434 case RTL_GIGA_MAC_VER_27:
1435 case RTL_GIGA_MAC_VER_28:
1436 case RTL_GIGA_MAC_VER_31:
1437 rtl8168dp_driver_stop(tp);
1438 break;
1439 case RTL_GIGA_MAC_VER_49:
1440 case RTL_GIGA_MAC_VER_50:
1441 case RTL_GIGA_MAC_VER_51:
1442 rtl8168ep_driver_stop(tp);
1443 break;
1444 default:
1445 BUG();
1446 break;
1447 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001448}
1449
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001450static int r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001451{
1452 u16 reg = rtl8168_get_ocp_reg(tp);
1453
1454 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1455}
1456
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001457static int r8168ep_check_dash(struct rtl8169_private *tp)
1458{
1459 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1460}
1461
1462static int r8168_check_dash(struct rtl8169_private *tp)
1463{
1464 switch (tp->mac_version) {
1465 case RTL_GIGA_MAC_VER_27:
1466 case RTL_GIGA_MAC_VER_28:
1467 case RTL_GIGA_MAC_VER_31:
1468 return r8168dp_check_dash(tp);
1469 case RTL_GIGA_MAC_VER_49:
1470 case RTL_GIGA_MAC_VER_50:
1471 case RTL_GIGA_MAC_VER_51:
1472 return r8168ep_check_dash(tp);
1473 default:
1474 return 0;
1475 }
1476}
1477
françois romieuc28aa382011-08-02 03:53:43 +00001478struct exgmac_reg {
1479 u16 addr;
1480 u16 mask;
1481 u32 val;
1482};
1483
Francois Romieufdf6fc02012-07-06 22:40:38 +02001484static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001485 const struct exgmac_reg *r, int len)
1486{
1487 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001488 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001489 r++;
1490 }
1491}
1492
Francois Romieuffc46952012-07-06 14:19:23 +02001493DECLARE_RTL_COND(rtl_efusear_cond)
1494{
1495 void __iomem *ioaddr = tp->mmio_addr;
1496
1497 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1498}
1499
Francois Romieufdf6fc02012-07-06 22:40:38 +02001500static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001501{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001502 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001503
1504 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1505
Francois Romieuffc46952012-07-06 14:19:23 +02001506 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1507 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001508}
1509
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001510static u16 rtl_get_events(struct rtl8169_private *tp)
1511{
1512 void __iomem *ioaddr = tp->mmio_addr;
1513
1514 return RTL_R16(IntrStatus);
1515}
1516
1517static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1518{
1519 void __iomem *ioaddr = tp->mmio_addr;
1520
1521 RTL_W16(IntrStatus, bits);
1522 mmiowb();
1523}
1524
1525static void rtl_irq_disable(struct rtl8169_private *tp)
1526{
1527 void __iomem *ioaddr = tp->mmio_addr;
1528
1529 RTL_W16(IntrMask, 0);
1530 mmiowb();
1531}
1532
Francois Romieu3e990ff2012-01-26 12:50:01 +01001533static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1534{
1535 void __iomem *ioaddr = tp->mmio_addr;
1536
1537 RTL_W16(IntrMask, bits);
1538}
1539
Francois Romieuda78dbf2012-01-26 14:18:23 +01001540#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1541#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1542#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1543
1544static void rtl_irq_enable_all(struct rtl8169_private *tp)
1545{
1546 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1547}
1548
françois romieu811fd302011-12-04 20:30:45 +00001549static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
françois romieu811fd302011-12-04 20:30:45 +00001551 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001553 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001554 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001555 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
françois romieu4da19632011-01-03 15:07:55 +00001558static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
françois romieu4da19632011-01-03 15:07:55 +00001560 void __iomem *ioaddr = tp->mmio_addr;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return RTL_R32(TBICSR) & TBIReset;
1563}
1564
françois romieu4da19632011-01-03 15:07:55 +00001565static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
françois romieu4da19632011-01-03 15:07:55 +00001567 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1571{
1572 return RTL_R32(TBICSR) & TBILinkOk;
1573}
1574
1575static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1576{
1577 return RTL_R8(PHYstatus) & LinkStatus;
1578}
1579
françois romieu4da19632011-01-03 15:07:55 +00001580static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
françois romieu4da19632011-01-03 15:07:55 +00001582 void __iomem *ioaddr = tp->mmio_addr;
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1585}
1586
françois romieu4da19632011-01-03 15:07:55 +00001587static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 unsigned int val;
1590
françois romieu4da19632011-01-03 15:07:55 +00001591 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1592 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Hayes Wang70090422011-07-06 15:58:06 +08001595static void rtl_link_chg_patch(struct rtl8169_private *tp)
1596{
1597 void __iomem *ioaddr = tp->mmio_addr;
1598 struct net_device *dev = tp->dev;
1599
1600 if (!netif_running(dev))
1601 return;
1602
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001603 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1604 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001605 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001606 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1607 ERIAR_EXGMAC);
1608 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1609 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001610 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001611 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1612 ERIAR_EXGMAC);
1613 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1614 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001615 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001616 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1617 ERIAR_EXGMAC);
1618 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1619 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001620 }
1621 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001622 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001623 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001624 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001625 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001626 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1627 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1628 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001629 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1630 ERIAR_EXGMAC);
1631 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1632 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001633 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001634 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1635 ERIAR_EXGMAC);
1636 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1637 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001638 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001639 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1640 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001641 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1642 ERIAR_EXGMAC);
1643 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1644 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001645 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001646 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1647 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001648 }
Hayes Wang70090422011-07-06 15:58:06 +08001649 }
1650}
1651
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001652static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001653 struct rtl8169_private *tp,
1654 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001657 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001658 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001659 if (pm)
1660 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001662 if (net_ratelimit())
1663 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001664 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001666 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001667 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001668 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001672static void rtl8169_check_link_status(struct net_device *dev,
1673 struct rtl8169_private *tp,
1674 void __iomem *ioaddr)
1675{
1676 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1677}
1678
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001679#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1680
1681static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1682{
1683 void __iomem *ioaddr = tp->mmio_addr;
1684 u8 options;
1685 u32 wolopts = 0;
1686
1687 options = RTL_R8(Config1);
1688 if (!(options & PMEnable))
1689 return 0;
1690
1691 options = RTL_R8(Config3);
1692 if (options & LinkUp)
1693 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001694 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001695 case RTL_GIGA_MAC_VER_34:
1696 case RTL_GIGA_MAC_VER_35:
1697 case RTL_GIGA_MAC_VER_36:
1698 case RTL_GIGA_MAC_VER_37:
1699 case RTL_GIGA_MAC_VER_38:
1700 case RTL_GIGA_MAC_VER_40:
1701 case RTL_GIGA_MAC_VER_41:
1702 case RTL_GIGA_MAC_VER_42:
1703 case RTL_GIGA_MAC_VER_43:
1704 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001705 case RTL_GIGA_MAC_VER_45:
1706 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001707 case RTL_GIGA_MAC_VER_47:
1708 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001709 case RTL_GIGA_MAC_VER_49:
1710 case RTL_GIGA_MAC_VER_50:
1711 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001712 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1713 wolopts |= WAKE_MAGIC;
1714 break;
1715 default:
1716 if (options & MagicPacket)
1717 wolopts |= WAKE_MAGIC;
1718 break;
1719 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001720
1721 options = RTL_R8(Config5);
1722 if (options & UWF)
1723 wolopts |= WAKE_UCAST;
1724 if (options & BWF)
1725 wolopts |= WAKE_BCAST;
1726 if (options & MWF)
1727 wolopts |= WAKE_MCAST;
1728
1729 return wolopts;
1730}
1731
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001732static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1733{
1734 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001735
Francois Romieuda78dbf2012-01-26 14:18:23 +01001736 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001737
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001738 wol->supported = WAKE_ANY;
1739 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001740
Francois Romieuda78dbf2012-01-26 14:18:23 +01001741 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001742}
1743
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001744static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001745{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001746 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001747 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001748 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001749 u32 opt;
1750 u16 reg;
1751 u8 mask;
1752 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001753 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001754 { WAKE_UCAST, Config5, UWF },
1755 { WAKE_BCAST, Config5, BWF },
1756 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001757 { WAKE_ANY, Config5, LanWake },
1758 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001759 };
Francois Romieu851e6022012-04-17 11:10:11 +02001760 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001761
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001762 RTL_W8(Cfg9346, Cfg9346_Unlock);
1763
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001764 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001765 case RTL_GIGA_MAC_VER_34:
1766 case RTL_GIGA_MAC_VER_35:
1767 case RTL_GIGA_MAC_VER_36:
1768 case RTL_GIGA_MAC_VER_37:
1769 case RTL_GIGA_MAC_VER_38:
1770 case RTL_GIGA_MAC_VER_40:
1771 case RTL_GIGA_MAC_VER_41:
1772 case RTL_GIGA_MAC_VER_42:
1773 case RTL_GIGA_MAC_VER_43:
1774 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001775 case RTL_GIGA_MAC_VER_45:
1776 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08001777 case RTL_GIGA_MAC_VER_47:
1778 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001779 case RTL_GIGA_MAC_VER_49:
1780 case RTL_GIGA_MAC_VER_50:
1781 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001782 tmp = ARRAY_SIZE(cfg) - 1;
1783 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001784 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001785 0x0dc,
1786 ERIAR_MASK_0100,
1787 MagicPacket_v2,
1788 0x0000,
1789 ERIAR_EXGMAC);
1790 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001791 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001792 0x0dc,
1793 ERIAR_MASK_0100,
1794 0x0000,
1795 MagicPacket_v2,
1796 ERIAR_EXGMAC);
1797 break;
1798 default:
1799 tmp = ARRAY_SIZE(cfg);
1800 break;
1801 }
1802
1803 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001804 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001805 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001806 options |= cfg[i].mask;
1807 RTL_W8(cfg[i].reg, options);
1808 }
1809
Francois Romieu851e6022012-04-17 11:10:11 +02001810 switch (tp->mac_version) {
1811 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1812 options = RTL_R8(Config1) & ~PMEnable;
1813 if (wolopts)
1814 options |= PMEnable;
1815 RTL_W8(Config1, options);
1816 break;
1817 default:
Francois Romieud387b422012-04-17 11:12:01 +02001818 options = RTL_R8(Config2) & ~PME_SIGNAL;
1819 if (wolopts)
1820 options |= PME_SIGNAL;
1821 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001822 break;
1823 }
1824
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001825 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001826}
1827
1828static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1829{
1830 struct rtl8169_private *tp = netdev_priv(dev);
1831
Francois Romieuda78dbf2012-01-26 14:18:23 +01001832 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001833
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001834 if (wol->wolopts)
1835 tp->features |= RTL_FEATURE_WOL;
1836 else
1837 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001838 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001839
1840 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001841
françois romieuea809072010-11-08 13:23:58 +00001842 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1843
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001844 return 0;
1845}
1846
Francois Romieu31bd2042011-04-26 18:58:59 +02001847static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1848{
Francois Romieu85bffe62011-04-27 08:22:39 +02001849 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001850}
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852static void rtl8169_get_drvinfo(struct net_device *dev,
1853 struct ethtool_drvinfo *info)
1854{
1855 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001856 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Rick Jones68aad782011-11-07 13:29:27 +00001858 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1859 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1860 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001861 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001862 if (!IS_ERR_OR_NULL(rtl_fw))
1863 strlcpy(info->fw_version, rtl_fw->version,
1864 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867static int rtl8169_get_regs_len(struct net_device *dev)
1868{
1869 return R8169_REGS_SIZE;
1870}
1871
1872static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001873 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 struct rtl8169_private *tp = netdev_priv(dev);
1876 void __iomem *ioaddr = tp->mmio_addr;
1877 int ret = 0;
1878 u32 reg;
1879
1880 reg = RTL_R32(TBICSR);
1881 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1882 (duplex == DUPLEX_FULL)) {
1883 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1884 } else if (autoneg == AUTONEG_ENABLE)
1885 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1886 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001887 netif_warn(tp, link, dev,
1888 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 ret = -EOPNOTSUPP;
1890 }
1891
1892 return ret;
1893}
1894
1895static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001896 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001899 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001900 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Hayes Wang716b50a2011-02-22 17:26:18 +08001902 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001905 int auto_nego;
1906
françois romieu4da19632011-01-03 15:07:55 +00001907 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001908 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1909 ADVERTISE_100HALF | ADVERTISE_100FULL);
1910
1911 if (adv & ADVERTISED_10baseT_Half)
1912 auto_nego |= ADVERTISE_10HALF;
1913 if (adv & ADVERTISED_10baseT_Full)
1914 auto_nego |= ADVERTISE_10FULL;
1915 if (adv & ADVERTISED_100baseT_Half)
1916 auto_nego |= ADVERTISE_100HALF;
1917 if (adv & ADVERTISED_100baseT_Full)
1918 auto_nego |= ADVERTISE_100FULL;
1919
françois romieu3577aa12009-05-19 10:46:48 +00001920 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1921
françois romieu4da19632011-01-03 15:07:55 +00001922 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001923 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1924
1925 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001926 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001927 if (adv & ADVERTISED_1000baseT_Half)
1928 giga_ctrl |= ADVERTISE_1000HALF;
1929 if (adv & ADVERTISED_1000baseT_Full)
1930 giga_ctrl |= ADVERTISE_1000FULL;
1931 } else if (adv & (ADVERTISED_1000baseT_Half |
1932 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001933 netif_info(tp, link, dev,
1934 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001935 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
françois romieu3577aa12009-05-19 10:46:48 +00001938 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001939
françois romieu4da19632011-01-03 15:07:55 +00001940 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1941 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001942 } else {
1943 giga_ctrl = 0;
1944
1945 if (speed == SPEED_10)
1946 bmcr = 0;
1947 else if (speed == SPEED_100)
1948 bmcr = BMCR_SPEED100;
1949 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001950 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001951
1952 if (duplex == DUPLEX_FULL)
1953 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001954 }
1955
françois romieu4da19632011-01-03 15:07:55 +00001956 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001957
Francois Romieucecb5fd2011-04-01 10:21:07 +02001958 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1959 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001960 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001961 rtl_writephy(tp, 0x17, 0x2138);
1962 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001963 } else {
françois romieu4da19632011-01-03 15:07:55 +00001964 rtl_writephy(tp, 0x17, 0x2108);
1965 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001966 }
1967 }
1968
Oliver Neukum54405cd2011-01-06 21:55:13 +01001969 rc = 0;
1970out:
1971 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001975 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct rtl8169_private *tp = netdev_priv(dev);
1978 int ret;
1979
Oliver Neukum54405cd2011-01-06 21:55:13 +01001980 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001981 if (ret < 0)
1982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Francois Romieu4876cc12011-03-11 21:07:11 +01001984 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1985 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001987 }
1988out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return ret;
1990}
1991
1992static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1993{
1994 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int ret;
1996
Francois Romieu4876cc12011-03-11 21:07:11 +01001997 del_timer_sync(&tp->timer);
1998
Francois Romieuda78dbf2012-01-26 14:18:23 +01001999 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002000 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00002001 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002002 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return ret;
2005}
2006
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002007static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2008 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Francois Romieud58d46b2011-05-03 16:38:29 +02002010 struct rtl8169_private *tp = netdev_priv(dev);
2011
Francois Romieu2b7b4312011-04-18 22:53:24 -07002012 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00002013 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Francois Romieud58d46b2011-05-03 16:38:29 +02002015 if (dev->mtu > JUMBO_1K &&
2016 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2017 features &= ~NETIF_F_IP_CSUM;
2018
Michał Mirosław350fb322011-04-08 06:35:56 +00002019 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Francois Romieuda78dbf2012-01-26 14:18:23 +01002022static void __rtl8169_set_features(struct net_device *dev,
2023 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002026 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08002027 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
hayeswang929a0312014-09-16 11:40:47 +08002029 rx_config = RTL_R32(RxConfig);
2030 if (features & NETIF_F_RXALL)
2031 rx_config |= (AcceptErr | AcceptRunt);
2032 else
2033 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
hayeswang929a0312014-09-16 11:40:47 +08002035 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00002036
hayeswang929a0312014-09-16 11:40:47 +08002037 if (features & NETIF_F_RXCSUM)
2038 tp->cp_cmd |= RxChkSum;
2039 else
2040 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00002041
hayeswang929a0312014-09-16 11:40:47 +08002042 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2043 tp->cp_cmd |= RxVlan;
2044 else
2045 tp->cp_cmd &= ~RxVlan;
2046
2047 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2048
2049 RTL_W16(CPlusCmd, tp->cp_cmd);
2050 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Francois Romieuda78dbf2012-01-26 14:18:23 +01002053static int rtl8169_set_features(struct net_device *dev,
2054 netdev_features_t features)
2055{
2056 struct rtl8169_private *tp = netdev_priv(dev);
2057
hayeswang929a0312014-09-16 11:40:47 +08002058 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2059
Francois Romieuda78dbf2012-01-26 14:18:23 +01002060 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03002061 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08002062 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002063 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 return 0;
2066}
2067
Francois Romieuda78dbf2012-01-26 14:18:23 +01002068
Kirill Smelkov810f4892012-11-10 21:11:02 +04002069static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Jesse Grosseab6d182010-10-20 13:56:03 +00002071 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
2073}
2074
Francois Romieu7a8fc772011-03-01 17:18:33 +01002075static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Francois Romieu7a8fc772011-03-01 17:18:33 +01002079 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00002080 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Francois Romieuccdffb92008-07-26 14:26:06 +02002083static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct rtl8169_private *tp = netdev_priv(dev);
2086 void __iomem *ioaddr = tp->mmio_addr;
2087 u32 status;
2088
2089 cmd->supported =
2090 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
2091 cmd->port = PORT_FIBRE;
2092 cmd->transceiver = XCVR_INTERNAL;
2093
2094 status = RTL_R32(TBICSR);
2095 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2096 cmd->autoneg = !!(status & TBINwEnable);
2097
David Decotigny70739492011-04-27 18:32:40 +00002098 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02002100
2101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Francois Romieuccdffb92008-07-26 14:26:06 +02002104static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Francois Romieuccdffb92008-07-26 14:26:06 +02002108 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2112{
2113 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02002114 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Francois Romieuda78dbf2012-01-26 14:18:23 +01002116 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02002117 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002118 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Francois Romieuccdffb92008-07-26 14:26:06 +02002120 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
2123static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2124 void *p)
2125{
Francois Romieu5b0384f2006-08-16 16:00:01 +02002126 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02002127 u32 __iomem *data = tp->mmio_addr;
2128 u32 *dw = p;
2129 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Francois Romieuda78dbf2012-01-26 14:18:23 +01002131 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02002132 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2133 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01002134 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002137static u32 rtl8169_get_msglevel(struct net_device *dev)
2138{
2139 struct rtl8169_private *tp = netdev_priv(dev);
2140
2141 return tp->msg_enable;
2142}
2143
2144static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2145{
2146 struct rtl8169_private *tp = netdev_priv(dev);
2147
2148 tp->msg_enable = value;
2149}
2150
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002151static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2152 "tx_packets",
2153 "rx_packets",
2154 "tx_errors",
2155 "rx_errors",
2156 "rx_missed",
2157 "align_errors",
2158 "tx_single_collisions",
2159 "tx_multi_collisions",
2160 "unicast",
2161 "broadcast",
2162 "multicast",
2163 "tx_aborted",
2164 "tx_underrun",
2165};
2166
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002167static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002168{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002169 switch (sset) {
2170 case ETH_SS_STATS:
2171 return ARRAY_SIZE(rtl8169_gstrings);
2172 default:
2173 return -EOPNOTSUPP;
2174 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002175}
2176
Francois Romieuffc46952012-07-06 14:19:23 +02002177DECLARE_RTL_COND(rtl_counters_cond)
2178{
2179 void __iomem *ioaddr = tp->mmio_addr;
2180
2181 return RTL_R32(CounterAddrLow) & CounterDump;
2182}
2183
Ivan Vecera355423d2009-02-06 21:49:57 -08002184static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002185{
2186 struct rtl8169_private *tp = netdev_priv(dev);
2187 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002188 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002189 struct rtl8169_counters *counters;
2190 dma_addr_t paddr;
2191 u32 cmd;
2192
Ivan Vecera355423d2009-02-06 21:49:57 -08002193 /*
2194 * Some chips are unable to dump tally counters when the receiver
2195 * is disabled.
2196 */
2197 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
2198 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002199
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00002200 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002201 if (!counters)
2202 return;
2203
2204 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07002205 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002206 RTL_W32(CounterAddrLow, cmd);
2207 RTL_W32(CounterAddrLow, cmd | CounterDump);
2208
Francois Romieuffc46952012-07-06 14:19:23 +02002209 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
2210 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002211
2212 RTL_W32(CounterAddrLow, 0);
2213 RTL_W32(CounterAddrHigh, 0);
2214
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00002215 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002216}
2217
Ivan Vecera355423d2009-02-06 21:49:57 -08002218static void rtl8169_get_ethtool_stats(struct net_device *dev,
2219 struct ethtool_stats *stats, u64 *data)
2220{
2221 struct rtl8169_private *tp = netdev_priv(dev);
2222
2223 ASSERT_RTNL();
2224
2225 rtl8169_update_counters(dev);
2226
2227 data[0] = le64_to_cpu(tp->counters.tx_packets);
2228 data[1] = le64_to_cpu(tp->counters.rx_packets);
2229 data[2] = le64_to_cpu(tp->counters.tx_errors);
2230 data[3] = le32_to_cpu(tp->counters.rx_errors);
2231 data[4] = le16_to_cpu(tp->counters.rx_missed);
2232 data[5] = le16_to_cpu(tp->counters.align_errors);
2233 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
2234 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
2235 data[8] = le64_to_cpu(tp->counters.rx_unicast);
2236 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
2237 data[10] = le32_to_cpu(tp->counters.rx_multicast);
2238 data[11] = le16_to_cpu(tp->counters.tx_aborted);
2239 data[12] = le16_to_cpu(tp->counters.tx_underun);
2240}
2241
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002242static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2243{
2244 switch(stringset) {
2245 case ETH_SS_STATS:
2246 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2247 break;
2248 }
2249}
2250
Jeff Garzik7282d492006-09-13 14:30:00 -04002251static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 .get_drvinfo = rtl8169_get_drvinfo,
2253 .get_regs_len = rtl8169_get_regs_len,
2254 .get_link = ethtool_op_get_link,
2255 .get_settings = rtl8169_get_settings,
2256 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002257 .get_msglevel = rtl8169_get_msglevel,
2258 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002260 .get_wol = rtl8169_get_wol,
2261 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002262 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002263 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002264 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002265 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266};
2267
Francois Romieu07d3f512007-02-21 22:40:46 +01002268static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002269 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Francois Romieu5d320a22011-05-08 17:47:36 +02002271 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002272 /*
2273 * The driver currently handles the 8168Bf and the 8168Be identically
2274 * but they can be identified more specifically through the test below
2275 * if needed:
2276 *
2277 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002278 *
2279 * Same thing for the 8101Eb and the 8101Ec:
2280 *
2281 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002282 */
Francois Romieu37441002011-06-17 22:58:54 +02002283 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002285 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 int mac_version;
2287 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002288 /* 8168EP family. */
2289 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2290 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2291 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2292
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002293 /* 8168H family. */
2294 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2295 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2296
Hayes Wangc5583862012-07-02 17:23:22 +08002297 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002298 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002299 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002300 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2301 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2302
Hayes Wangc2218922011-09-06 16:55:18 +08002303 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002304 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002305 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2306 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2307
hayeswang01dc7fe2011-03-21 01:50:28 +00002308 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002309 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002310 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2311 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2312 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2313
Francois Romieu5b538df2008-07-20 16:22:45 +02002314 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002315 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2316 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002317 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002318
françois romieue6de30d2011-01-03 15:08:37 +00002319 /* 8168DP family. */
2320 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2321 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002322 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002323
Francois Romieuef808d52008-06-29 13:10:54 +02002324 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002325 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002326 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002327 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002328 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002329 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2330 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002331 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002332 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002333 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002334
2335 /* 8168B family. */
2336 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2337 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2338 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2339 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2340
2341 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002342 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2343 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002344 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002345 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002346 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2347 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2348 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002349 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2350 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2351 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2352 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2353 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2354 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002355 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002356 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002357 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002358 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2359 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002360 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2361 /* FIXME: where did these entries come from ? -- FR */
2362 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2363 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2364
2365 /* 8110 family. */
2366 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2367 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2368 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2369 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2370 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2371 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2372
Jean Delvaref21b75e2009-05-26 20:54:48 -07002373 /* Catch-all */
2374 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002375 };
2376 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 u32 reg;
2378
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002379 reg = RTL_R32(TxConfig);
2380 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 p++;
2382 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002383
2384 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2385 netif_notice(tp, probe, dev,
2386 "unknown MAC, using family default\n");
2387 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002388 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2389 tp->mac_version = tp->mii.supports_gmii ?
2390 RTL_GIGA_MAC_VER_42 :
2391 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002392 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2393 tp->mac_version = tp->mii.supports_gmii ?
2394 RTL_GIGA_MAC_VER_45 :
2395 RTL_GIGA_MAC_VER_47;
2396 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2397 tp->mac_version = tp->mii.supports_gmii ?
2398 RTL_GIGA_MAC_VER_46 :
2399 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2404{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002405 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
Francois Romieu867763c2007-08-17 18:21:58 +02002408struct phy_reg {
2409 u16 reg;
2410 u16 val;
2411};
2412
françois romieu4da19632011-01-03 15:07:55 +00002413static void rtl_writephy_batch(struct rtl8169_private *tp,
2414 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002415{
2416 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002417 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002418 regs++;
2419 }
2420}
2421
françois romieubca03d52011-01-03 15:07:31 +00002422#define PHY_READ 0x00000000
2423#define PHY_DATA_OR 0x10000000
2424#define PHY_DATA_AND 0x20000000
2425#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002426#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002427#define PHY_CLEAR_READCOUNT 0x70000000
2428#define PHY_WRITE 0x80000000
2429#define PHY_READCOUNT_EQ_SKIP 0x90000000
2430#define PHY_COMP_EQ_SKIPN 0xa0000000
2431#define PHY_COMP_NEQ_SKIPN 0xb0000000
2432#define PHY_WRITE_PREVIOUS 0xc0000000
2433#define PHY_SKIPN 0xd0000000
2434#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002435
Hayes Wang960aee62011-06-18 11:37:48 +02002436struct fw_info {
2437 u32 magic;
2438 char version[RTL_VER_SIZE];
2439 __le32 fw_start;
2440 __le32 fw_len;
2441 u8 chksum;
2442} __packed;
2443
Francois Romieu1c361ef2011-06-17 17:16:24 +02002444#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2445
2446static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002447{
Francois Romieub6ffd972011-06-17 17:00:05 +02002448 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002449 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002450 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2451 char *version = rtl_fw->version;
2452 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002453
Francois Romieu1c361ef2011-06-17 17:16:24 +02002454 if (fw->size < FW_OPCODE_SIZE)
2455 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002456
2457 if (!fw_info->magic) {
2458 size_t i, size, start;
2459 u8 checksum = 0;
2460
2461 if (fw->size < sizeof(*fw_info))
2462 goto out;
2463
2464 for (i = 0; i < fw->size; i++)
2465 checksum += fw->data[i];
2466 if (checksum != 0)
2467 goto out;
2468
2469 start = le32_to_cpu(fw_info->fw_start);
2470 if (start > fw->size)
2471 goto out;
2472
2473 size = le32_to_cpu(fw_info->fw_len);
2474 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2475 goto out;
2476
2477 memcpy(version, fw_info->version, RTL_VER_SIZE);
2478
2479 pa->code = (__le32 *)(fw->data + start);
2480 pa->size = size;
2481 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002482 if (fw->size % FW_OPCODE_SIZE)
2483 goto out;
2484
2485 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2486
2487 pa->code = (__le32 *)fw->data;
2488 pa->size = fw->size / FW_OPCODE_SIZE;
2489 }
2490 version[RTL_VER_SIZE - 1] = 0;
2491
2492 rc = true;
2493out:
2494 return rc;
2495}
2496
Francois Romieufd112f22011-06-18 00:10:29 +02002497static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2498 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002499{
Francois Romieufd112f22011-06-18 00:10:29 +02002500 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002501 size_t index;
2502
Francois Romieu1c361ef2011-06-17 17:16:24 +02002503 for (index = 0; index < pa->size; index++) {
2504 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002505 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002506
hayeswang42b82dc2011-01-10 02:07:25 +00002507 switch(action & 0xf0000000) {
2508 case PHY_READ:
2509 case PHY_DATA_OR:
2510 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002511 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002512 case PHY_CLEAR_READCOUNT:
2513 case PHY_WRITE:
2514 case PHY_WRITE_PREVIOUS:
2515 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002516 break;
2517
hayeswang42b82dc2011-01-10 02:07:25 +00002518 case PHY_BJMPN:
2519 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002520 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002521 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002522 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002523 }
2524 break;
2525 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002526 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002527 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002528 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002529 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002530 }
2531 break;
2532 case PHY_COMP_EQ_SKIPN:
2533 case PHY_COMP_NEQ_SKIPN:
2534 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002535 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002536 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002537 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002538 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002539 }
2540 break;
2541
hayeswang42b82dc2011-01-10 02:07:25 +00002542 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002543 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002544 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002545 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002546 }
2547 }
Francois Romieufd112f22011-06-18 00:10:29 +02002548 rc = true;
2549out:
2550 return rc;
2551}
françois romieubca03d52011-01-03 15:07:31 +00002552
Francois Romieufd112f22011-06-18 00:10:29 +02002553static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2554{
2555 struct net_device *dev = tp->dev;
2556 int rc = -EINVAL;
2557
2558 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2559 netif_err(tp, ifup, dev, "invalid firwmare\n");
2560 goto out;
2561 }
2562
2563 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2564 rc = 0;
2565out:
2566 return rc;
2567}
2568
2569static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2570{
2571 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002572 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002573 u32 predata, count;
2574 size_t index;
2575
2576 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002577 org.write = ops->write;
2578 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002579
Francois Romieu1c361ef2011-06-17 17:16:24 +02002580 for (index = 0; index < pa->size; ) {
2581 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002582 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002583 u32 regno = (action & 0x0fff0000) >> 16;
2584
2585 if (!action)
2586 break;
françois romieubca03d52011-01-03 15:07:31 +00002587
2588 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002589 case PHY_READ:
2590 predata = rtl_readphy(tp, regno);
2591 count++;
2592 index++;
françois romieubca03d52011-01-03 15:07:31 +00002593 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002594 case PHY_DATA_OR:
2595 predata |= data;
2596 index++;
2597 break;
2598 case PHY_DATA_AND:
2599 predata &= data;
2600 index++;
2601 break;
2602 case PHY_BJMPN:
2603 index -= regno;
2604 break;
hayeswangeee37862013-04-01 22:23:38 +00002605 case PHY_MDIO_CHG:
2606 if (data == 0) {
2607 ops->write = org.write;
2608 ops->read = org.read;
2609 } else if (data == 1) {
2610 ops->write = mac_mcu_write;
2611 ops->read = mac_mcu_read;
2612 }
2613
hayeswang42b82dc2011-01-10 02:07:25 +00002614 index++;
2615 break;
2616 case PHY_CLEAR_READCOUNT:
2617 count = 0;
2618 index++;
2619 break;
2620 case PHY_WRITE:
2621 rtl_writephy(tp, regno, data);
2622 index++;
2623 break;
2624 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002625 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002626 break;
2627 case PHY_COMP_EQ_SKIPN:
2628 if (predata == data)
2629 index += regno;
2630 index++;
2631 break;
2632 case PHY_COMP_NEQ_SKIPN:
2633 if (predata != data)
2634 index += regno;
2635 index++;
2636 break;
2637 case PHY_WRITE_PREVIOUS:
2638 rtl_writephy(tp, regno, predata);
2639 index++;
2640 break;
2641 case PHY_SKIPN:
2642 index += regno + 1;
2643 break;
2644 case PHY_DELAY_MS:
2645 mdelay(data);
2646 index++;
2647 break;
2648
françois romieubca03d52011-01-03 15:07:31 +00002649 default:
2650 BUG();
2651 }
2652 }
hayeswangeee37862013-04-01 22:23:38 +00002653
2654 ops->write = org.write;
2655 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002656}
2657
françois romieuf1e02ed2011-01-13 13:07:53 +00002658static void rtl_release_firmware(struct rtl8169_private *tp)
2659{
Francois Romieub6ffd972011-06-17 17:00:05 +02002660 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2661 release_firmware(tp->rtl_fw->fw);
2662 kfree(tp->rtl_fw);
2663 }
2664 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002665}
2666
François Romieu953a12c2011-04-24 17:38:48 +02002667static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002668{
Francois Romieub6ffd972011-06-17 17:00:05 +02002669 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002670
2671 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002672 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002673 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002674}
2675
2676static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2677{
2678 if (rtl_readphy(tp, reg) != val)
2679 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2680 else
2681 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002682}
2683
françois romieu4da19632011-01-03 15:07:55 +00002684static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002686 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002687 { 0x1f, 0x0001 },
2688 { 0x06, 0x006e },
2689 { 0x08, 0x0708 },
2690 { 0x15, 0x4000 },
2691 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
françois romieu0b9b5712009-08-10 19:44:56 +00002693 { 0x1f, 0x0001 },
2694 { 0x03, 0x00a1 },
2695 { 0x02, 0x0008 },
2696 { 0x01, 0x0120 },
2697 { 0x00, 0x1000 },
2698 { 0x04, 0x0800 },
2699 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
françois romieu0b9b5712009-08-10 19:44:56 +00002701 { 0x03, 0xff41 },
2702 { 0x02, 0xdf60 },
2703 { 0x01, 0x0140 },
2704 { 0x00, 0x0077 },
2705 { 0x04, 0x7800 },
2706 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
françois romieu0b9b5712009-08-10 19:44:56 +00002708 { 0x03, 0x802f },
2709 { 0x02, 0x4f02 },
2710 { 0x01, 0x0409 },
2711 { 0x00, 0xf0f9 },
2712 { 0x04, 0x9800 },
2713 { 0x04, 0x9000 },
2714
2715 { 0x03, 0xdf01 },
2716 { 0x02, 0xdf20 },
2717 { 0x01, 0xff95 },
2718 { 0x00, 0xba00 },
2719 { 0x04, 0xa800 },
2720 { 0x04, 0xa000 },
2721
2722 { 0x03, 0xff41 },
2723 { 0x02, 0xdf20 },
2724 { 0x01, 0x0140 },
2725 { 0x00, 0x00bb },
2726 { 0x04, 0xb800 },
2727 { 0x04, 0xb000 },
2728
2729 { 0x03, 0xdf41 },
2730 { 0x02, 0xdc60 },
2731 { 0x01, 0x6340 },
2732 { 0x00, 0x007d },
2733 { 0x04, 0xd800 },
2734 { 0x04, 0xd000 },
2735
2736 { 0x03, 0xdf01 },
2737 { 0x02, 0xdf20 },
2738 { 0x01, 0x100a },
2739 { 0x00, 0xa0ff },
2740 { 0x04, 0xf800 },
2741 { 0x04, 0xf000 },
2742
2743 { 0x1f, 0x0000 },
2744 { 0x0b, 0x0000 },
2745 { 0x00, 0x9200 }
2746 };
2747
françois romieu4da19632011-01-03 15:07:55 +00002748 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
françois romieu4da19632011-01-03 15:07:55 +00002751static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002752{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002753 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002754 { 0x1f, 0x0002 },
2755 { 0x01, 0x90d0 },
2756 { 0x1f, 0x0000 }
2757 };
2758
françois romieu4da19632011-01-03 15:07:55 +00002759 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002760}
2761
françois romieu4da19632011-01-03 15:07:55 +00002762static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002763{
2764 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002765
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002766 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2767 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002768 return;
2769
françois romieu4da19632011-01-03 15:07:55 +00002770 rtl_writephy(tp, 0x1f, 0x0001);
2771 rtl_writephy(tp, 0x10, 0xf01b);
2772 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002773}
2774
françois romieu4da19632011-01-03 15:07:55 +00002775static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002776{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002777 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002778 { 0x1f, 0x0001 },
2779 { 0x04, 0x0000 },
2780 { 0x03, 0x00a1 },
2781 { 0x02, 0x0008 },
2782 { 0x01, 0x0120 },
2783 { 0x00, 0x1000 },
2784 { 0x04, 0x0800 },
2785 { 0x04, 0x9000 },
2786 { 0x03, 0x802f },
2787 { 0x02, 0x4f02 },
2788 { 0x01, 0x0409 },
2789 { 0x00, 0xf099 },
2790 { 0x04, 0x9800 },
2791 { 0x04, 0xa000 },
2792 { 0x03, 0xdf01 },
2793 { 0x02, 0xdf20 },
2794 { 0x01, 0xff95 },
2795 { 0x00, 0xba00 },
2796 { 0x04, 0xa800 },
2797 { 0x04, 0xf000 },
2798 { 0x03, 0xdf01 },
2799 { 0x02, 0xdf20 },
2800 { 0x01, 0x101a },
2801 { 0x00, 0xa0ff },
2802 { 0x04, 0xf800 },
2803 { 0x04, 0x0000 },
2804 { 0x1f, 0x0000 },
2805
2806 { 0x1f, 0x0001 },
2807 { 0x10, 0xf41b },
2808 { 0x14, 0xfb54 },
2809 { 0x18, 0xf5c7 },
2810 { 0x1f, 0x0000 },
2811
2812 { 0x1f, 0x0001 },
2813 { 0x17, 0x0cc0 },
2814 { 0x1f, 0x0000 }
2815 };
2816
françois romieu4da19632011-01-03 15:07:55 +00002817 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002818
françois romieu4da19632011-01-03 15:07:55 +00002819 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002820}
2821
françois romieu4da19632011-01-03 15:07:55 +00002822static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002823{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002824 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002825 { 0x1f, 0x0001 },
2826 { 0x04, 0x0000 },
2827 { 0x03, 0x00a1 },
2828 { 0x02, 0x0008 },
2829 { 0x01, 0x0120 },
2830 { 0x00, 0x1000 },
2831 { 0x04, 0x0800 },
2832 { 0x04, 0x9000 },
2833 { 0x03, 0x802f },
2834 { 0x02, 0x4f02 },
2835 { 0x01, 0x0409 },
2836 { 0x00, 0xf099 },
2837 { 0x04, 0x9800 },
2838 { 0x04, 0xa000 },
2839 { 0x03, 0xdf01 },
2840 { 0x02, 0xdf20 },
2841 { 0x01, 0xff95 },
2842 { 0x00, 0xba00 },
2843 { 0x04, 0xa800 },
2844 { 0x04, 0xf000 },
2845 { 0x03, 0xdf01 },
2846 { 0x02, 0xdf20 },
2847 { 0x01, 0x101a },
2848 { 0x00, 0xa0ff },
2849 { 0x04, 0xf800 },
2850 { 0x04, 0x0000 },
2851 { 0x1f, 0x0000 },
2852
2853 { 0x1f, 0x0001 },
2854 { 0x0b, 0x8480 },
2855 { 0x1f, 0x0000 },
2856
2857 { 0x1f, 0x0001 },
2858 { 0x18, 0x67c7 },
2859 { 0x04, 0x2000 },
2860 { 0x03, 0x002f },
2861 { 0x02, 0x4360 },
2862 { 0x01, 0x0109 },
2863 { 0x00, 0x3022 },
2864 { 0x04, 0x2800 },
2865 { 0x1f, 0x0000 },
2866
2867 { 0x1f, 0x0001 },
2868 { 0x17, 0x0cc0 },
2869 { 0x1f, 0x0000 }
2870 };
2871
françois romieu4da19632011-01-03 15:07:55 +00002872 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002873}
2874
françois romieu4da19632011-01-03 15:07:55 +00002875static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002876{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002877 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002878 { 0x10, 0xf41b },
2879 { 0x1f, 0x0000 }
2880 };
2881
françois romieu4da19632011-01-03 15:07:55 +00002882 rtl_writephy(tp, 0x1f, 0x0001);
2883 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002884
françois romieu4da19632011-01-03 15:07:55 +00002885 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002886}
2887
françois romieu4da19632011-01-03 15:07:55 +00002888static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002889{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002890 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002891 { 0x1f, 0x0001 },
2892 { 0x10, 0xf41b },
2893 { 0x1f, 0x0000 }
2894 };
2895
françois romieu4da19632011-01-03 15:07:55 +00002896 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002897}
2898
françois romieu4da19632011-01-03 15:07:55 +00002899static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002900{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002901 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002902 { 0x1f, 0x0000 },
2903 { 0x1d, 0x0f00 },
2904 { 0x1f, 0x0002 },
2905 { 0x0c, 0x1ec8 },
2906 { 0x1f, 0x0000 }
2907 };
2908
françois romieu4da19632011-01-03 15:07:55 +00002909 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002910}
2911
françois romieu4da19632011-01-03 15:07:55 +00002912static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002913{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002914 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002915 { 0x1f, 0x0001 },
2916 { 0x1d, 0x3d98 },
2917 { 0x1f, 0x0000 }
2918 };
2919
françois romieu4da19632011-01-03 15:07:55 +00002920 rtl_writephy(tp, 0x1f, 0x0000);
2921 rtl_patchphy(tp, 0x14, 1 << 5);
2922 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002923
françois romieu4da19632011-01-03 15:07:55 +00002924 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002925}
2926
françois romieu4da19632011-01-03 15:07:55 +00002927static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002928{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002929 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002930 { 0x1f, 0x0001 },
2931 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002932 { 0x1f, 0x0002 },
2933 { 0x00, 0x88d4 },
2934 { 0x01, 0x82b1 },
2935 { 0x03, 0x7002 },
2936 { 0x08, 0x9e30 },
2937 { 0x09, 0x01f0 },
2938 { 0x0a, 0x5500 },
2939 { 0x0c, 0x00c8 },
2940 { 0x1f, 0x0003 },
2941 { 0x12, 0xc096 },
2942 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002943 { 0x1f, 0x0000 },
2944 { 0x1f, 0x0000 },
2945 { 0x09, 0x2000 },
2946 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002947 };
2948
françois romieu4da19632011-01-03 15:07:55 +00002949 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002950
françois romieu4da19632011-01-03 15:07:55 +00002951 rtl_patchphy(tp, 0x14, 1 << 5);
2952 rtl_patchphy(tp, 0x0d, 1 << 5);
2953 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002954}
2955
françois romieu4da19632011-01-03 15:07:55 +00002956static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002957{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002958 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002959 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002960 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002961 { 0x03, 0x802f },
2962 { 0x02, 0x4f02 },
2963 { 0x01, 0x0409 },
2964 { 0x00, 0xf099 },
2965 { 0x04, 0x9800 },
2966 { 0x04, 0x9000 },
2967 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002968 { 0x1f, 0x0002 },
2969 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002970 { 0x06, 0x0761 },
2971 { 0x1f, 0x0003 },
2972 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002973 { 0x1f, 0x0000 }
2974 };
2975
françois romieu4da19632011-01-03 15:07:55 +00002976 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002977
françois romieu4da19632011-01-03 15:07:55 +00002978 rtl_patchphy(tp, 0x16, 1 << 0);
2979 rtl_patchphy(tp, 0x14, 1 << 5);
2980 rtl_patchphy(tp, 0x0d, 1 << 5);
2981 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002982}
2983
françois romieu4da19632011-01-03 15:07:55 +00002984static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002985{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002986 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002987 { 0x1f, 0x0001 },
2988 { 0x12, 0x2300 },
2989 { 0x1d, 0x3d98 },
2990 { 0x1f, 0x0002 },
2991 { 0x0c, 0x7eb8 },
2992 { 0x06, 0x5461 },
2993 { 0x1f, 0x0003 },
2994 { 0x16, 0x0f0a },
2995 { 0x1f, 0x0000 }
2996 };
2997
françois romieu4da19632011-01-03 15:07:55 +00002998 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002999
françois romieu4da19632011-01-03 15:07:55 +00003000 rtl_patchphy(tp, 0x16, 1 << 0);
3001 rtl_patchphy(tp, 0x14, 1 << 5);
3002 rtl_patchphy(tp, 0x0d, 1 << 5);
3003 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02003004}
3005
françois romieu4da19632011-01-03 15:07:55 +00003006static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02003007{
françois romieu4da19632011-01-03 15:07:55 +00003008 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003009}
3010
françois romieubca03d52011-01-03 15:07:31 +00003011static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02003012{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003013 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003014 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02003015 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00003016 { 0x06, 0x4064 },
3017 { 0x07, 0x2863 },
3018 { 0x08, 0x059c },
3019 { 0x09, 0x26b4 },
3020 { 0x0a, 0x6a19 },
3021 { 0x0b, 0xdcc8 },
3022 { 0x10, 0xf06d },
3023 { 0x14, 0x7f68 },
3024 { 0x18, 0x7fd9 },
3025 { 0x1c, 0xf0ff },
3026 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02003027 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00003028 { 0x12, 0xf49f },
3029 { 0x13, 0x070b },
3030 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00003031 { 0x14, 0x94c0 },
3032
3033 /*
3034 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003035 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003036 */
Francois Romieu5b538df2008-07-20 16:22:45 +02003037 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00003038 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003039 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003040 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003041 { 0x06, 0x5561 },
3042
3043 /*
3044 * Can not link to 1Gbps with bad cable
3045 * Decrease SNR threshold form 21.07dB to 19.04dB
3046 */
3047 { 0x1f, 0x0001 },
3048 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003049
3050 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003051 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003052 };
3053
françois romieu4da19632011-01-03 15:07:55 +00003054 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02003055
françois romieubca03d52011-01-03 15:07:31 +00003056 /*
3057 * Rx Error Issue
3058 * Fine Tune Switching regulator parameter
3059 */
françois romieu4da19632011-01-03 15:07:55 +00003060 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003061 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3062 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00003063
Francois Romieufdf6fc02012-07-06 22:40:38 +02003064 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003065 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003066 { 0x1f, 0x0002 },
3067 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02003068 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003069 { 0x05, 0x8330 },
3070 { 0x06, 0x669a },
3071 { 0x1f, 0x0002 }
3072 };
3073 int val;
3074
françois romieu4da19632011-01-03 15:07:55 +00003075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003076
françois romieu4da19632011-01-03 15:07:55 +00003077 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003078
3079 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003080 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003081 0x0065, 0x0066, 0x0067, 0x0068,
3082 0x0069, 0x006a, 0x006b, 0x006c
3083 };
3084 int i;
3085
françois romieu4da19632011-01-03 15:07:55 +00003086 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003087
3088 val &= 0xff00;
3089 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003090 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003091 }
3092 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003093 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003094 { 0x1f, 0x0002 },
3095 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02003096 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00003097 { 0x05, 0x8330 },
3098 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003099 };
3100
françois romieu4da19632011-01-03 15:07:55 +00003101 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003102 }
3103
françois romieubca03d52011-01-03 15:07:31 +00003104 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003105 rtl_writephy(tp, 0x1f, 0x0002);
3106 rtl_patchphy(tp, 0x0d, 0x0300);
3107 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003108
françois romieubca03d52011-01-03 15:07:31 +00003109 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003111 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3112 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003113
françois romieu4da19632011-01-03 15:07:55 +00003114 rtl_writephy(tp, 0x1f, 0x0005);
3115 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003116
3117 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003118
françois romieu4da19632011-01-03 15:07:55 +00003119 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003120}
3121
françois romieubca03d52011-01-03 15:07:31 +00003122static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003123{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003124 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003125 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003126 { 0x1f, 0x0001 },
3127 { 0x06, 0x4064 },
3128 { 0x07, 0x2863 },
3129 { 0x08, 0x059c },
3130 { 0x09, 0x26b4 },
3131 { 0x0a, 0x6a19 },
3132 { 0x0b, 0xdcc8 },
3133 { 0x10, 0xf06d },
3134 { 0x14, 0x7f68 },
3135 { 0x18, 0x7fd9 },
3136 { 0x1c, 0xf0ff },
3137 { 0x1d, 0x3d9c },
3138 { 0x1f, 0x0003 },
3139 { 0x12, 0xf49f },
3140 { 0x13, 0x070b },
3141 { 0x1a, 0x05ad },
3142 { 0x14, 0x94c0 },
3143
françois romieubca03d52011-01-03 15:07:31 +00003144 /*
3145 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003146 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003147 */
françois romieudaf9df62009-10-07 12:44:20 +00003148 { 0x1f, 0x0002 },
3149 { 0x06, 0x5561 },
3150 { 0x1f, 0x0005 },
3151 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003152 { 0x06, 0x5561 },
3153
3154 /*
3155 * Can not link to 1Gbps with bad cable
3156 * Decrease SNR threshold form 21.07dB to 19.04dB
3157 */
3158 { 0x1f, 0x0001 },
3159 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003160
3161 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003162 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003163 };
3164
françois romieu4da19632011-01-03 15:07:55 +00003165 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00003166
Francois Romieufdf6fc02012-07-06 22:40:38 +02003167 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003168 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003169 { 0x1f, 0x0002 },
3170 { 0x05, 0x669a },
3171 { 0x1f, 0x0005 },
3172 { 0x05, 0x8330 },
3173 { 0x06, 0x669a },
3174
3175 { 0x1f, 0x0002 }
3176 };
3177 int val;
3178
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003180
françois romieu4da19632011-01-03 15:07:55 +00003181 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003182 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003183 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003184 0x0065, 0x0066, 0x0067, 0x0068,
3185 0x0069, 0x006a, 0x006b, 0x006c
3186 };
3187 int i;
3188
françois romieu4da19632011-01-03 15:07:55 +00003189 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003190
3191 val &= 0xff00;
3192 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003193 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003194 }
3195 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003196 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003197 { 0x1f, 0x0002 },
3198 { 0x05, 0x2642 },
3199 { 0x1f, 0x0005 },
3200 { 0x05, 0x8330 },
3201 { 0x06, 0x2642 }
3202 };
3203
françois romieu4da19632011-01-03 15:07:55 +00003204 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003205 }
3206
françois romieubca03d52011-01-03 15:07:31 +00003207 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003209 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3210 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003211
françois romieubca03d52011-01-03 15:07:31 +00003212 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003213 rtl_writephy(tp, 0x1f, 0x0002);
3214 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003215
françois romieu4da19632011-01-03 15:07:55 +00003216 rtl_writephy(tp, 0x1f, 0x0005);
3217 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003218
3219 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003220
françois romieu4da19632011-01-03 15:07:55 +00003221 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003222}
3223
françois romieu4da19632011-01-03 15:07:55 +00003224static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003225{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003226 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003227 { 0x1f, 0x0002 },
3228 { 0x10, 0x0008 },
3229 { 0x0d, 0x006c },
3230
3231 { 0x1f, 0x0000 },
3232 { 0x0d, 0xf880 },
3233
3234 { 0x1f, 0x0001 },
3235 { 0x17, 0x0cc0 },
3236
3237 { 0x1f, 0x0001 },
3238 { 0x0b, 0xa4d8 },
3239 { 0x09, 0x281c },
3240 { 0x07, 0x2883 },
3241 { 0x0a, 0x6b35 },
3242 { 0x1d, 0x3da4 },
3243 { 0x1c, 0xeffd },
3244 { 0x14, 0x7f52 },
3245 { 0x18, 0x7fc6 },
3246 { 0x08, 0x0601 },
3247 { 0x06, 0x4063 },
3248 { 0x10, 0xf074 },
3249 { 0x1f, 0x0003 },
3250 { 0x13, 0x0789 },
3251 { 0x12, 0xf4bd },
3252 { 0x1a, 0x04fd },
3253 { 0x14, 0x84b0 },
3254 { 0x1f, 0x0000 },
3255 { 0x00, 0x9200 },
3256
3257 { 0x1f, 0x0005 },
3258 { 0x01, 0x0340 },
3259 { 0x1f, 0x0001 },
3260 { 0x04, 0x4000 },
3261 { 0x03, 0x1d21 },
3262 { 0x02, 0x0c32 },
3263 { 0x01, 0x0200 },
3264 { 0x00, 0x5554 },
3265 { 0x04, 0x4800 },
3266 { 0x04, 0x4000 },
3267 { 0x04, 0xf000 },
3268 { 0x03, 0xdf01 },
3269 { 0x02, 0xdf20 },
3270 { 0x01, 0x101a },
3271 { 0x00, 0xa0ff },
3272 { 0x04, 0xf800 },
3273 { 0x04, 0xf000 },
3274 { 0x1f, 0x0000 },
3275
3276 { 0x1f, 0x0007 },
3277 { 0x1e, 0x0023 },
3278 { 0x16, 0x0000 },
3279 { 0x1f, 0x0000 }
3280 };
3281
françois romieu4da19632011-01-03 15:07:55 +00003282 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003283}
3284
françois romieue6de30d2011-01-03 15:08:37 +00003285static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3286{
3287 static const struct phy_reg phy_reg_init[] = {
3288 { 0x1f, 0x0001 },
3289 { 0x17, 0x0cc0 },
3290
3291 { 0x1f, 0x0007 },
3292 { 0x1e, 0x002d },
3293 { 0x18, 0x0040 },
3294 { 0x1f, 0x0000 }
3295 };
3296
3297 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3298 rtl_patchphy(tp, 0x0d, 1 << 5);
3299}
3300
Hayes Wang70090422011-07-06 15:58:06 +08003301static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003302{
3303 static const struct phy_reg phy_reg_init[] = {
3304 /* Enable Delay cap */
3305 { 0x1f, 0x0005 },
3306 { 0x05, 0x8b80 },
3307 { 0x06, 0xc896 },
3308 { 0x1f, 0x0000 },
3309
3310 /* Channel estimation fine tune */
3311 { 0x1f, 0x0001 },
3312 { 0x0b, 0x6c20 },
3313 { 0x07, 0x2872 },
3314 { 0x1c, 0xefff },
3315 { 0x1f, 0x0003 },
3316 { 0x14, 0x6420 },
3317 { 0x1f, 0x0000 },
3318
3319 /* Update PFM & 10M TX idle timer */
3320 { 0x1f, 0x0007 },
3321 { 0x1e, 0x002f },
3322 { 0x15, 0x1919 },
3323 { 0x1f, 0x0000 },
3324
3325 { 0x1f, 0x0007 },
3326 { 0x1e, 0x00ac },
3327 { 0x18, 0x0006 },
3328 { 0x1f, 0x0000 }
3329 };
3330
Francois Romieu15ecd032011-04-27 13:52:22 -07003331 rtl_apply_firmware(tp);
3332
hayeswang01dc7fe2011-03-21 01:50:28 +00003333 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3334
3335 /* DCO enable for 10M IDLE Power */
3336 rtl_writephy(tp, 0x1f, 0x0007);
3337 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003338 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003339 rtl_writephy(tp, 0x1f, 0x0000);
3340
3341 /* For impedance matching */
3342 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003343 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003344 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003345
3346 /* PHY auto speed down */
3347 rtl_writephy(tp, 0x1f, 0x0007);
3348 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003349 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003350 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003351 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003352
3353 rtl_writephy(tp, 0x1f, 0x0005);
3354 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003355 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003356 rtl_writephy(tp, 0x1f, 0x0000);
3357
3358 rtl_writephy(tp, 0x1f, 0x0005);
3359 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003360 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003361 rtl_writephy(tp, 0x1f, 0x0007);
3362 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003363 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003364 rtl_writephy(tp, 0x1f, 0x0006);
3365 rtl_writephy(tp, 0x00, 0x5a00);
3366 rtl_writephy(tp, 0x1f, 0x0000);
3367 rtl_writephy(tp, 0x0d, 0x0007);
3368 rtl_writephy(tp, 0x0e, 0x003c);
3369 rtl_writephy(tp, 0x0d, 0x4007);
3370 rtl_writephy(tp, 0x0e, 0x0000);
3371 rtl_writephy(tp, 0x0d, 0x0000);
3372}
3373
françois romieu9ecb9aa2012-12-07 11:20:21 +00003374static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3375{
3376 const u16 w[] = {
3377 addr[0] | (addr[1] << 8),
3378 addr[2] | (addr[3] << 8),
3379 addr[4] | (addr[5] << 8)
3380 };
3381 const struct exgmac_reg e[] = {
3382 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3383 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3384 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3385 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3386 };
3387
3388 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3389}
3390
Hayes Wang70090422011-07-06 15:58:06 +08003391static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3392{
3393 static const struct phy_reg phy_reg_init[] = {
3394 /* Enable Delay cap */
3395 { 0x1f, 0x0004 },
3396 { 0x1f, 0x0007 },
3397 { 0x1e, 0x00ac },
3398 { 0x18, 0x0006 },
3399 { 0x1f, 0x0002 },
3400 { 0x1f, 0x0000 },
3401 { 0x1f, 0x0000 },
3402
3403 /* Channel estimation fine tune */
3404 { 0x1f, 0x0003 },
3405 { 0x09, 0xa20f },
3406 { 0x1f, 0x0000 },
3407 { 0x1f, 0x0000 },
3408
3409 /* Green Setting */
3410 { 0x1f, 0x0005 },
3411 { 0x05, 0x8b5b },
3412 { 0x06, 0x9222 },
3413 { 0x05, 0x8b6d },
3414 { 0x06, 0x8000 },
3415 { 0x05, 0x8b76 },
3416 { 0x06, 0x8000 },
3417 { 0x1f, 0x0000 }
3418 };
3419
3420 rtl_apply_firmware(tp);
3421
3422 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3423
3424 /* For 4-corner performance improve */
3425 rtl_writephy(tp, 0x1f, 0x0005);
3426 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003427 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003428 rtl_writephy(tp, 0x1f, 0x0000);
3429
3430 /* PHY auto speed down */
3431 rtl_writephy(tp, 0x1f, 0x0004);
3432 rtl_writephy(tp, 0x1f, 0x0007);
3433 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003434 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003435 rtl_writephy(tp, 0x1f, 0x0002);
3436 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003437 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003438
3439 /* improve 10M EEE waveform */
3440 rtl_writephy(tp, 0x1f, 0x0005);
3441 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003442 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003443 rtl_writephy(tp, 0x1f, 0x0000);
3444
3445 /* Improve 2-pair detection performance */
3446 rtl_writephy(tp, 0x1f, 0x0005);
3447 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003448 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003449 rtl_writephy(tp, 0x1f, 0x0000);
3450
3451 /* EEE setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003452 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003453 rtl_writephy(tp, 0x1f, 0x0005);
3454 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003455 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wang70090422011-07-06 15:58:06 +08003456 rtl_writephy(tp, 0x1f, 0x0004);
3457 rtl_writephy(tp, 0x1f, 0x0007);
3458 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003459 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003460 rtl_writephy(tp, 0x1f, 0x0002);
3461 rtl_writephy(tp, 0x1f, 0x0000);
3462 rtl_writephy(tp, 0x0d, 0x0007);
3463 rtl_writephy(tp, 0x0e, 0x003c);
3464 rtl_writephy(tp, 0x0d, 0x4007);
3465 rtl_writephy(tp, 0x0e, 0x0000);
3466 rtl_writephy(tp, 0x0d, 0x0000);
3467
3468 /* Green feature */
3469 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003470 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3471 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wang70090422011-07-06 15:58:06 +08003472 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003473
françois romieu9ecb9aa2012-12-07 11:20:21 +00003474 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3475 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003476}
3477
Hayes Wang5f886e02012-03-30 14:33:03 +08003478static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3479{
3480 /* For 4-corner performance improve */
3481 rtl_writephy(tp, 0x1f, 0x0005);
3482 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003483 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003484 rtl_writephy(tp, 0x1f, 0x0000);
3485
3486 /* PHY auto speed down */
3487 rtl_writephy(tp, 0x1f, 0x0007);
3488 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003489 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003490 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003491 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003492
3493 /* Improve 10M EEE waveform */
3494 rtl_writephy(tp, 0x1f, 0x0005);
3495 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003496 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003497 rtl_writephy(tp, 0x1f, 0x0000);
3498}
3499
Hayes Wangc2218922011-09-06 16:55:18 +08003500static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3501{
3502 static const struct phy_reg phy_reg_init[] = {
3503 /* Channel estimation fine tune */
3504 { 0x1f, 0x0003 },
3505 { 0x09, 0xa20f },
3506 { 0x1f, 0x0000 },
3507
3508 /* Modify green table for giga & fnet */
3509 { 0x1f, 0x0005 },
3510 { 0x05, 0x8b55 },
3511 { 0x06, 0x0000 },
3512 { 0x05, 0x8b5e },
3513 { 0x06, 0x0000 },
3514 { 0x05, 0x8b67 },
3515 { 0x06, 0x0000 },
3516 { 0x05, 0x8b70 },
3517 { 0x06, 0x0000 },
3518 { 0x1f, 0x0000 },
3519 { 0x1f, 0x0007 },
3520 { 0x1e, 0x0078 },
3521 { 0x17, 0x0000 },
3522 { 0x19, 0x00fb },
3523 { 0x1f, 0x0000 },
3524
3525 /* Modify green table for 10M */
3526 { 0x1f, 0x0005 },
3527 { 0x05, 0x8b79 },
3528 { 0x06, 0xaa00 },
3529 { 0x1f, 0x0000 },
3530
3531 /* Disable hiimpedance detection (RTCT) */
3532 { 0x1f, 0x0003 },
3533 { 0x01, 0x328a },
3534 { 0x1f, 0x0000 }
3535 };
3536
3537 rtl_apply_firmware(tp);
3538
3539 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3540
Hayes Wang5f886e02012-03-30 14:33:03 +08003541 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003542
3543 /* Improve 2-pair detection performance */
3544 rtl_writephy(tp, 0x1f, 0x0005);
3545 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003546 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003547 rtl_writephy(tp, 0x1f, 0x0000);
3548}
3549
3550static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3551{
3552 rtl_apply_firmware(tp);
3553
Hayes Wang5f886e02012-03-30 14:33:03 +08003554 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003555}
3556
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003557static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3558{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003559 static const struct phy_reg phy_reg_init[] = {
3560 /* Channel estimation fine tune */
3561 { 0x1f, 0x0003 },
3562 { 0x09, 0xa20f },
3563 { 0x1f, 0x0000 },
3564
3565 /* Modify green table for giga & fnet */
3566 { 0x1f, 0x0005 },
3567 { 0x05, 0x8b55 },
3568 { 0x06, 0x0000 },
3569 { 0x05, 0x8b5e },
3570 { 0x06, 0x0000 },
3571 { 0x05, 0x8b67 },
3572 { 0x06, 0x0000 },
3573 { 0x05, 0x8b70 },
3574 { 0x06, 0x0000 },
3575 { 0x1f, 0x0000 },
3576 { 0x1f, 0x0007 },
3577 { 0x1e, 0x0078 },
3578 { 0x17, 0x0000 },
3579 { 0x19, 0x00aa },
3580 { 0x1f, 0x0000 },
3581
3582 /* Modify green table for 10M */
3583 { 0x1f, 0x0005 },
3584 { 0x05, 0x8b79 },
3585 { 0x06, 0xaa00 },
3586 { 0x1f, 0x0000 },
3587
3588 /* Disable hiimpedance detection (RTCT) */
3589 { 0x1f, 0x0003 },
3590 { 0x01, 0x328a },
3591 { 0x1f, 0x0000 }
3592 };
3593
3594
3595 rtl_apply_firmware(tp);
3596
3597 rtl8168f_hw_phy_config(tp);
3598
3599 /* Improve 2-pair detection performance */
3600 rtl_writephy(tp, 0x1f, 0x0005);
3601 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003602 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003603 rtl_writephy(tp, 0x1f, 0x0000);
3604
3605 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3606
3607 /* Modify green table for giga */
3608 rtl_writephy(tp, 0x1f, 0x0005);
3609 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003610 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003611 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003612 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003613 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003614 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003615 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003616 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003617 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003618 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003619 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003620 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003621 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003622 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003623 rtl_writephy(tp, 0x1f, 0x0000);
3624
3625 /* uc same-seed solution */
3626 rtl_writephy(tp, 0x1f, 0x0005);
3627 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003628 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003629 rtl_writephy(tp, 0x1f, 0x0000);
3630
3631 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003632 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003633 rtl_writephy(tp, 0x1f, 0x0005);
3634 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003635 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003636 rtl_writephy(tp, 0x1f, 0x0004);
3637 rtl_writephy(tp, 0x1f, 0x0007);
3638 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003639 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003640 rtl_writephy(tp, 0x1f, 0x0000);
3641 rtl_writephy(tp, 0x0d, 0x0007);
3642 rtl_writephy(tp, 0x0e, 0x003c);
3643 rtl_writephy(tp, 0x0d, 0x4007);
3644 rtl_writephy(tp, 0x0e, 0x0000);
3645 rtl_writephy(tp, 0x0d, 0x0000);
3646
3647 /* Green feature */
3648 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003649 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3650 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003651 rtl_writephy(tp, 0x1f, 0x0000);
3652}
3653
Hayes Wangc5583862012-07-02 17:23:22 +08003654static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3655{
Hayes Wangc5583862012-07-02 17:23:22 +08003656 rtl_apply_firmware(tp);
3657
hayeswang41f44d12013-04-01 22:23:36 +00003658 rtl_writephy(tp, 0x1f, 0x0a46);
3659 if (rtl_readphy(tp, 0x10) & 0x0100) {
3660 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003661 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003662 } else {
3663 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003664 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003665 }
Hayes Wangc5583862012-07-02 17:23:22 +08003666
hayeswang41f44d12013-04-01 22:23:36 +00003667 rtl_writephy(tp, 0x1f, 0x0a46);
3668 if (rtl_readphy(tp, 0x13) & 0x0100) {
3669 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003670 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003671 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003672 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003673 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003674 }
Hayes Wangc5583862012-07-02 17:23:22 +08003675
hayeswang41f44d12013-04-01 22:23:36 +00003676 /* Enable PHY auto speed down */
3677 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003678 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003679
hayeswangfe7524c2013-04-01 22:23:37 +00003680 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003681 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003682 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003684 rtl_writephy(tp, 0x1f, 0x0a43);
3685 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003686 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3687 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003688
hayeswang41f44d12013-04-01 22:23:36 +00003689 /* EEE auto-fallback function */
3690 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003691 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003692
hayeswang41f44d12013-04-01 22:23:36 +00003693 /* Enable UC LPF tune function */
3694 rtl_writephy(tp, 0x1f, 0x0a43);
3695 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003696 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003697
3698 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003699 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003700
hayeswangfe7524c2013-04-01 22:23:37 +00003701 /* Improve SWR Efficiency */
3702 rtl_writephy(tp, 0x1f, 0x0bcd);
3703 rtl_writephy(tp, 0x14, 0x5065);
3704 rtl_writephy(tp, 0x14, 0xd065);
3705 rtl_writephy(tp, 0x1f, 0x0bc8);
3706 rtl_writephy(tp, 0x11, 0x5655);
3707 rtl_writephy(tp, 0x1f, 0x0bcd);
3708 rtl_writephy(tp, 0x14, 0x1065);
3709 rtl_writephy(tp, 0x14, 0x9065);
3710 rtl_writephy(tp, 0x14, 0x1065);
3711
David Chang1bac1072013-11-27 15:48:36 +08003712 /* Check ALDPS bit, disable it if enabled */
3713 rtl_writephy(tp, 0x1f, 0x0a43);
3714 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003715 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003716
hayeswang41f44d12013-04-01 22:23:36 +00003717 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003718}
3719
hayeswang57538c42013-04-01 22:23:40 +00003720static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3721{
3722 rtl_apply_firmware(tp);
3723}
3724
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003725static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3726{
3727 u16 dout_tapbin;
3728 u32 data;
3729
3730 rtl_apply_firmware(tp);
3731
3732 /* CHN EST parameters adjust - giga master */
3733 rtl_writephy(tp, 0x1f, 0x0a43);
3734 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003735 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003736 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003737 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003738 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003739 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003740 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003741 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003742 rtl_writephy(tp, 0x1f, 0x0000);
3743
3744 /* CHN EST parameters adjust - giga slave */
3745 rtl_writephy(tp, 0x1f, 0x0a43);
3746 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003747 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003748 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003749 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003750 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003751 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003752 rtl_writephy(tp, 0x1f, 0x0000);
3753
3754 /* CHN EST parameters adjust - fnet */
3755 rtl_writephy(tp, 0x1f, 0x0a43);
3756 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003757 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003758 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003759 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003760 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003761 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003762 rtl_writephy(tp, 0x1f, 0x0000);
3763
3764 /* enable R-tune & PGA-retune function */
3765 dout_tapbin = 0;
3766 rtl_writephy(tp, 0x1f, 0x0a46);
3767 data = rtl_readphy(tp, 0x13);
3768 data &= 3;
3769 data <<= 2;
3770 dout_tapbin |= data;
3771 data = rtl_readphy(tp, 0x12);
3772 data &= 0xc000;
3773 data >>= 14;
3774 dout_tapbin |= data;
3775 dout_tapbin = ~(dout_tapbin^0x08);
3776 dout_tapbin <<= 12;
3777 dout_tapbin &= 0xf000;
3778 rtl_writephy(tp, 0x1f, 0x0a43);
3779 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003780 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003781 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003782 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003783 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003784 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003785 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003786 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003787
3788 rtl_writephy(tp, 0x1f, 0x0a43);
3789 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003790 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003791 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003792 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003793 rtl_writephy(tp, 0x1f, 0x0000);
3794
3795 /* enable GPHY 10M */
3796 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003797 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003798 rtl_writephy(tp, 0x1f, 0x0000);
3799
3800 /* SAR ADC performance */
3801 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003802 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003803 rtl_writephy(tp, 0x1f, 0x0000);
3804
3805 rtl_writephy(tp, 0x1f, 0x0a43);
3806 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003807 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003808 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003809 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003810 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003811 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003812 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003813 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003814 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003815 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003816 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003817 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003818 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003819 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003820 rtl_writephy(tp, 0x1f, 0x0000);
3821
3822 /* disable phy pfm mode */
3823 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003824 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003825 rtl_writephy(tp, 0x1f, 0x0000);
3826
3827 /* Check ALDPS bit, disable it if enabled */
3828 rtl_writephy(tp, 0x1f, 0x0a43);
3829 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003830 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003831
3832 rtl_writephy(tp, 0x1f, 0x0000);
3833}
3834
3835static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3836{
3837 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3838 u16 rlen;
3839 u32 data;
3840
3841 rtl_apply_firmware(tp);
3842
3843 /* CHIN EST parameter update */
3844 rtl_writephy(tp, 0x1f, 0x0a43);
3845 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003846 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003847 rtl_writephy(tp, 0x1f, 0x0000);
3848
3849 /* enable R-tune & PGA-retune function */
3850 rtl_writephy(tp, 0x1f, 0x0a43);
3851 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003852 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003853 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003854 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003855 rtl_writephy(tp, 0x1f, 0x0000);
3856
3857 /* enable GPHY 10M */
3858 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003859 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003860 rtl_writephy(tp, 0x1f, 0x0000);
3861
3862 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3863 data = r8168_mac_ocp_read(tp, 0xdd02);
3864 ioffset_p3 = ((data & 0x80)>>7);
3865 ioffset_p3 <<= 3;
3866
3867 data = r8168_mac_ocp_read(tp, 0xdd00);
3868 ioffset_p3 |= ((data & (0xe000))>>13);
3869 ioffset_p2 = ((data & (0x1e00))>>9);
3870 ioffset_p1 = ((data & (0x01e0))>>5);
3871 ioffset_p0 = ((data & 0x0010)>>4);
3872 ioffset_p0 <<= 3;
3873 ioffset_p0 |= (data & (0x07));
3874 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3875
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003876 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3877 (ioffset_p1 != 0x0f) || (ioffset_p0 == 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003878 rtl_writephy(tp, 0x1f, 0x0bcf);
3879 rtl_writephy(tp, 0x16, data);
3880 rtl_writephy(tp, 0x1f, 0x0000);
3881 }
3882
3883 /* Modify rlen (TX LPF corner frequency) level */
3884 rtl_writephy(tp, 0x1f, 0x0bcd);
3885 data = rtl_readphy(tp, 0x16);
3886 data &= 0x000f;
3887 rlen = 0;
3888 if (data > 3)
3889 rlen = data - 3;
3890 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3891 rtl_writephy(tp, 0x17, data);
3892 rtl_writephy(tp, 0x1f, 0x0bcd);
3893 rtl_writephy(tp, 0x1f, 0x0000);
3894
3895 /* disable phy pfm mode */
3896 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003897 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003898 rtl_writephy(tp, 0x1f, 0x0000);
3899
3900 /* Check ALDPS bit, disable it if enabled */
3901 rtl_writephy(tp, 0x1f, 0x0a43);
3902 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003903 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003904
3905 rtl_writephy(tp, 0x1f, 0x0000);
3906}
3907
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003908static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3909{
3910 /* Enable PHY auto speed down */
3911 rtl_writephy(tp, 0x1f, 0x0a44);
3912 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3913 rtl_writephy(tp, 0x1f, 0x0000);
3914
3915 /* patch 10M & ALDPS */
3916 rtl_writephy(tp, 0x1f, 0x0bcc);
3917 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3918 rtl_writephy(tp, 0x1f, 0x0a44);
3919 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3920 rtl_writephy(tp, 0x1f, 0x0a43);
3921 rtl_writephy(tp, 0x13, 0x8084);
3922 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3923 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3924 rtl_writephy(tp, 0x1f, 0x0000);
3925
3926 /* Enable EEE auto-fallback function */
3927 rtl_writephy(tp, 0x1f, 0x0a4b);
3928 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3929 rtl_writephy(tp, 0x1f, 0x0000);
3930
3931 /* Enable UC LPF tune function */
3932 rtl_writephy(tp, 0x1f, 0x0a43);
3933 rtl_writephy(tp, 0x13, 0x8012);
3934 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3935 rtl_writephy(tp, 0x1f, 0x0000);
3936
3937 /* set rg_sel_sdm_rate */
3938 rtl_writephy(tp, 0x1f, 0x0c42);
3939 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3940 rtl_writephy(tp, 0x1f, 0x0000);
3941
3942 /* Check ALDPS bit, disable it if enabled */
3943 rtl_writephy(tp, 0x1f, 0x0a43);
3944 if (rtl_readphy(tp, 0x10) & 0x0004)
3945 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3946
3947 rtl_writephy(tp, 0x1f, 0x0000);
3948}
3949
3950static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3951{
3952 /* patch 10M & ALDPS */
3953 rtl_writephy(tp, 0x1f, 0x0bcc);
3954 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3955 rtl_writephy(tp, 0x1f, 0x0a44);
3956 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3957 rtl_writephy(tp, 0x1f, 0x0a43);
3958 rtl_writephy(tp, 0x13, 0x8084);
3959 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3960 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3961 rtl_writephy(tp, 0x1f, 0x0000);
3962
3963 /* Enable UC LPF tune function */
3964 rtl_writephy(tp, 0x1f, 0x0a43);
3965 rtl_writephy(tp, 0x13, 0x8012);
3966 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3967 rtl_writephy(tp, 0x1f, 0x0000);
3968
3969 /* Set rg_sel_sdm_rate */
3970 rtl_writephy(tp, 0x1f, 0x0c42);
3971 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3972 rtl_writephy(tp, 0x1f, 0x0000);
3973
3974 /* Channel estimation parameters */
3975 rtl_writephy(tp, 0x1f, 0x0a43);
3976 rtl_writephy(tp, 0x13, 0x80f3);
3977 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3978 rtl_writephy(tp, 0x13, 0x80f0);
3979 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3980 rtl_writephy(tp, 0x13, 0x80ef);
3981 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3982 rtl_writephy(tp, 0x13, 0x80f6);
3983 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3984 rtl_writephy(tp, 0x13, 0x80ec);
3985 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3986 rtl_writephy(tp, 0x13, 0x80ed);
3987 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3988 rtl_writephy(tp, 0x13, 0x80f2);
3989 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3990 rtl_writephy(tp, 0x13, 0x80f4);
3991 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3992 rtl_writephy(tp, 0x1f, 0x0a43);
3993 rtl_writephy(tp, 0x13, 0x8110);
3994 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3995 rtl_writephy(tp, 0x13, 0x810f);
3996 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3997 rtl_writephy(tp, 0x13, 0x8111);
3998 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3999 rtl_writephy(tp, 0x13, 0x8113);
4000 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4001 rtl_writephy(tp, 0x13, 0x8115);
4002 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4003 rtl_writephy(tp, 0x13, 0x810e);
4004 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4005 rtl_writephy(tp, 0x13, 0x810c);
4006 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4007 rtl_writephy(tp, 0x13, 0x810b);
4008 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4009 rtl_writephy(tp, 0x1f, 0x0a43);
4010 rtl_writephy(tp, 0x13, 0x80d1);
4011 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4012 rtl_writephy(tp, 0x13, 0x80cd);
4013 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4014 rtl_writephy(tp, 0x13, 0x80d3);
4015 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4016 rtl_writephy(tp, 0x13, 0x80d5);
4017 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4018 rtl_writephy(tp, 0x13, 0x80d7);
4019 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4020
4021 /* Force PWM-mode */
4022 rtl_writephy(tp, 0x1f, 0x0bcd);
4023 rtl_writephy(tp, 0x14, 0x5065);
4024 rtl_writephy(tp, 0x14, 0xd065);
4025 rtl_writephy(tp, 0x1f, 0x0bc8);
4026 rtl_writephy(tp, 0x12, 0x00ed);
4027 rtl_writephy(tp, 0x1f, 0x0bcd);
4028 rtl_writephy(tp, 0x14, 0x1065);
4029 rtl_writephy(tp, 0x14, 0x9065);
4030 rtl_writephy(tp, 0x14, 0x1065);
4031 rtl_writephy(tp, 0x1f, 0x0000);
4032
4033 /* Check ALDPS bit, disable it if enabled */
4034 rtl_writephy(tp, 0x1f, 0x0a43);
4035 if (rtl_readphy(tp, 0x10) & 0x0004)
4036 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4037
4038 rtl_writephy(tp, 0x1f, 0x0000);
4039}
4040
françois romieu4da19632011-01-03 15:07:55 +00004041static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004042{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004043 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004044 { 0x1f, 0x0003 },
4045 { 0x08, 0x441d },
4046 { 0x01, 0x9100 },
4047 { 0x1f, 0x0000 }
4048 };
4049
françois romieu4da19632011-01-03 15:07:55 +00004050 rtl_writephy(tp, 0x1f, 0x0000);
4051 rtl_patchphy(tp, 0x11, 1 << 12);
4052 rtl_patchphy(tp, 0x19, 1 << 13);
4053 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004054
françois romieu4da19632011-01-03 15:07:55 +00004055 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02004056}
4057
Hayes Wang5a5e4442011-02-22 17:26:21 +08004058static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4059{
4060 static const struct phy_reg phy_reg_init[] = {
4061 { 0x1f, 0x0005 },
4062 { 0x1a, 0x0000 },
4063 { 0x1f, 0x0000 },
4064
4065 { 0x1f, 0x0004 },
4066 { 0x1c, 0x0000 },
4067 { 0x1f, 0x0000 },
4068
4069 { 0x1f, 0x0001 },
4070 { 0x15, 0x7701 },
4071 { 0x1f, 0x0000 }
4072 };
4073
4074 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004075 rtl_writephy(tp, 0x1f, 0x0000);
4076 rtl_writephy(tp, 0x18, 0x0310);
4077 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004078
François Romieu953a12c2011-04-24 17:38:48 +02004079 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004080
4081 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4082}
4083
Hayes Wang7e18dca2012-03-30 14:33:02 +08004084static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4085{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004086 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01004087 rtl_writephy(tp, 0x1f, 0x0000);
4088 rtl_writephy(tp, 0x18, 0x0310);
4089 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004090
4091 rtl_apply_firmware(tp);
4092
4093 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02004094 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004095 rtl_writephy(tp, 0x1f, 0x0004);
4096 rtl_writephy(tp, 0x10, 0x401f);
4097 rtl_writephy(tp, 0x19, 0x7030);
4098 rtl_writephy(tp, 0x1f, 0x0000);
4099}
4100
Hayes Wang5598bfe2012-07-02 17:23:21 +08004101static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4102{
Hayes Wang5598bfe2012-07-02 17:23:21 +08004103 static const struct phy_reg phy_reg_init[] = {
4104 { 0x1f, 0x0004 },
4105 { 0x10, 0xc07f },
4106 { 0x19, 0x7030 },
4107 { 0x1f, 0x0000 }
4108 };
4109
4110 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01004111 rtl_writephy(tp, 0x1f, 0x0000);
4112 rtl_writephy(tp, 0x18, 0x0310);
4113 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004114
4115 rtl_apply_firmware(tp);
4116
Francois Romieufdf6fc02012-07-06 22:40:38 +02004117 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004118 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4119
Francois Romieufdf6fc02012-07-06 22:40:38 +02004120 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08004121}
4122
Francois Romieu5615d9f2007-08-17 17:50:46 +02004123static void rtl_hw_phy_config(struct net_device *dev)
4124{
4125 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004126
4127 rtl8169_print_mac_version(tp);
4128
4129 switch (tp->mac_version) {
4130 case RTL_GIGA_MAC_VER_01:
4131 break;
4132 case RTL_GIGA_MAC_VER_02:
4133 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00004134 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004135 break;
4136 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00004137 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004138 break;
françois romieu2e9558562009-08-10 19:44:19 +00004139 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00004140 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00004141 break;
françois romieu8c7006a2009-08-10 19:43:29 +00004142 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00004143 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00004144 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02004145 case RTL_GIGA_MAC_VER_07:
4146 case RTL_GIGA_MAC_VER_08:
4147 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00004148 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004149 break;
Francois Romieu236b8082008-05-30 16:11:48 +02004150 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00004151 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004152 break;
4153 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00004154 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004155 break;
4156 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00004157 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02004158 break;
Francois Romieu867763c2007-08-17 18:21:58 +02004159 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00004160 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004161 break;
4162 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00004163 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02004164 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02004165 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00004166 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02004167 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004168 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00004169 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004170 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004171 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00004172 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004173 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004174 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004175 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00004176 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004177 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004178 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00004179 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004180 break;
4181 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00004182 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00004183 break;
4184 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00004185 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004186 break;
françois romieue6de30d2011-01-03 15:08:37 +00004187 case RTL_GIGA_MAC_VER_28:
4188 rtl8168d_4_hw_phy_config(tp);
4189 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004190 case RTL_GIGA_MAC_VER_29:
4191 case RTL_GIGA_MAC_VER_30:
4192 rtl8105e_hw_phy_config(tp);
4193 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004194 case RTL_GIGA_MAC_VER_31:
4195 /* None. */
4196 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00004197 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00004198 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004199 rtl8168e_1_hw_phy_config(tp);
4200 break;
4201 case RTL_GIGA_MAC_VER_34:
4202 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004203 break;
Hayes Wangc2218922011-09-06 16:55:18 +08004204 case RTL_GIGA_MAC_VER_35:
4205 rtl8168f_1_hw_phy_config(tp);
4206 break;
4207 case RTL_GIGA_MAC_VER_36:
4208 rtl8168f_2_hw_phy_config(tp);
4209 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004210
Hayes Wang7e18dca2012-03-30 14:33:02 +08004211 case RTL_GIGA_MAC_VER_37:
4212 rtl8402_hw_phy_config(tp);
4213 break;
4214
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004215 case RTL_GIGA_MAC_VER_38:
4216 rtl8411_hw_phy_config(tp);
4217 break;
4218
Hayes Wang5598bfe2012-07-02 17:23:21 +08004219 case RTL_GIGA_MAC_VER_39:
4220 rtl8106e_hw_phy_config(tp);
4221 break;
4222
Hayes Wangc5583862012-07-02 17:23:22 +08004223 case RTL_GIGA_MAC_VER_40:
4224 rtl8168g_1_hw_phy_config(tp);
4225 break;
hayeswang57538c42013-04-01 22:23:40 +00004226 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004227 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004228 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004229 rtl8168g_2_hw_phy_config(tp);
4230 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004231 case RTL_GIGA_MAC_VER_45:
4232 case RTL_GIGA_MAC_VER_47:
4233 rtl8168h_1_hw_phy_config(tp);
4234 break;
4235 case RTL_GIGA_MAC_VER_46:
4236 case RTL_GIGA_MAC_VER_48:
4237 rtl8168h_2_hw_phy_config(tp);
4238 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004239
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004240 case RTL_GIGA_MAC_VER_49:
4241 rtl8168ep_1_hw_phy_config(tp);
4242 break;
4243 case RTL_GIGA_MAC_VER_50:
4244 case RTL_GIGA_MAC_VER_51:
4245 rtl8168ep_2_hw_phy_config(tp);
4246 break;
4247
Hayes Wangc5583862012-07-02 17:23:22 +08004248 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004249 default:
4250 break;
4251 }
4252}
4253
Francois Romieuda78dbf2012-01-26 14:18:23 +01004254static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 struct timer_list *timer = &tp->timer;
4257 void __iomem *ioaddr = tp->mmio_addr;
4258 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4259
Francois Romieubcf0bf92006-07-26 23:14:13 +02004260 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
françois romieu4da19632011-01-03 15:07:55 +00004262 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02004263 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 * A busy loop could burn quite a few cycles on nowadays CPU.
4265 * Let's delay the execution of the timer for a few ticks.
4266 */
4267 timeout = HZ/10;
4268 goto out_mod_timer;
4269 }
4270
4271 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01004272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02004274 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
françois romieu4da19632011-01-03 15:07:55 +00004276 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278out_mod_timer:
4279 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004280}
4281
4282static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4283{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004284 if (!test_and_set_bit(flag, tp->wk.flags))
4285 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004286}
4287
4288static void rtl8169_phy_timer(unsigned long __opaque)
4289{
4290 struct net_device *dev = (struct net_device *)__opaque;
4291 struct rtl8169_private *tp = netdev_priv(dev);
4292
Francois Romieu98ddf982012-01-31 10:47:34 +01004293 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
4297 void __iomem *ioaddr)
4298{
4299 iounmap(ioaddr);
4300 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004301 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 pci_disable_device(pdev);
4303 free_netdev(dev);
4304}
4305
Francois Romieuffc46952012-07-06 14:19:23 +02004306DECLARE_RTL_COND(rtl_phy_reset_cond)
4307{
4308 return tp->phy_reset_pending(tp);
4309}
4310
Francois Romieubf793292006-11-01 00:53:05 +01004311static void rtl8169_phy_reset(struct net_device *dev,
4312 struct rtl8169_private *tp)
4313{
françois romieu4da19632011-01-03 15:07:55 +00004314 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02004315 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01004316}
4317
David S. Miller8decf862011-09-22 03:23:13 -04004318static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4319{
4320 void __iomem *ioaddr = tp->mmio_addr;
4321
4322 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4323 (RTL_R8(PHYstatus) & TBI_Enable);
4324}
4325
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004326static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004328 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004329
Francois Romieu5615d9f2007-08-17 17:50:46 +02004330 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004331
Marcus Sundberg773328942008-07-10 21:28:08 +02004332 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4333 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4334 RTL_W8(0x82, 0x01);
4335 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004336
Francois Romieu6dccd162007-02-13 23:38:05 +01004337 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4338
4339 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4340 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004341
Francois Romieubcf0bf92006-07-26 23:14:13 +02004342 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004343 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4344 RTL_W8(0x82, 0x01);
4345 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004346 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004347 }
4348
Francois Romieubf793292006-11-01 00:53:05 +01004349 rtl8169_phy_reset(dev, tp);
4350
Oliver Neukum54405cd2011-01-06 21:55:13 +01004351 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004352 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4353 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4354 (tp->mii.supports_gmii ?
4355 ADVERTISED_1000baseT_Half |
4356 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004357
David S. Miller8decf862011-09-22 03:23:13 -04004358 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004359 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004360}
4361
Francois Romieu773d2022007-01-31 23:47:43 +01004362static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4363{
4364 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004365
Francois Romieuda78dbf2012-01-26 14:18:23 +01004366 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004367
4368 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004369
françois romieu9ecb9aa2012-12-07 11:20:21 +00004370 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004371 RTL_R32(MAC4);
4372
françois romieu9ecb9aa2012-12-07 11:20:21 +00004373 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004374 RTL_R32(MAC0);
4375
françois romieu9ecb9aa2012-12-07 11:20:21 +00004376 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4377 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004378
Francois Romieu773d2022007-01-31 23:47:43 +01004379 RTL_W8(Cfg9346, Cfg9346_Lock);
4380
Francois Romieuda78dbf2012-01-26 14:18:23 +01004381 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004382}
4383
4384static int rtl_set_mac_address(struct net_device *dev, void *p)
4385{
4386 struct rtl8169_private *tp = netdev_priv(dev);
4387 struct sockaddr *addr = p;
4388
4389 if (!is_valid_ether_addr(addr->sa_data))
4390 return -EADDRNOTAVAIL;
4391
4392 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4393
4394 rtl_rar_set(tp, dev->dev_addr);
4395
4396 return 0;
4397}
4398
Francois Romieu5f787a12006-08-17 13:02:36 +02004399static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4400{
4401 struct rtl8169_private *tp = netdev_priv(dev);
4402 struct mii_ioctl_data *data = if_mii(ifr);
4403
Francois Romieu8b4ab282008-11-19 22:05:25 -08004404 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4405}
Francois Romieu5f787a12006-08-17 13:02:36 +02004406
Francois Romieucecb5fd2011-04-01 10:21:07 +02004407static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4408 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004409{
Francois Romieu5f787a12006-08-17 13:02:36 +02004410 switch (cmd) {
4411 case SIOCGMIIPHY:
4412 data->phy_id = 32; /* Internal PHY */
4413 return 0;
4414
4415 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004416 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004417 return 0;
4418
4419 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004420 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004421 return 0;
4422 }
4423 return -EOPNOTSUPP;
4424}
4425
Francois Romieu8b4ab282008-11-19 22:05:25 -08004426static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4427{
4428 return -EOPNOTSUPP;
4429}
4430
Francois Romieufbac58f2007-10-04 22:51:38 +02004431static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4432{
4433 if (tp->features & RTL_FEATURE_MSI) {
4434 pci_disable_msi(pdev);
4435 tp->features &= ~RTL_FEATURE_MSI;
4436 }
4437}
4438
Bill Pembertonbaf63292012-12-03 09:23:28 -05004439static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004440{
4441 struct mdio_ops *ops = &tp->mdio_ops;
4442
4443 switch (tp->mac_version) {
4444 case RTL_GIGA_MAC_VER_27:
4445 ops->write = r8168dp_1_mdio_write;
4446 ops->read = r8168dp_1_mdio_read;
4447 break;
françois romieue6de30d2011-01-03 15:08:37 +00004448 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004449 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004450 ops->write = r8168dp_2_mdio_write;
4451 ops->read = r8168dp_2_mdio_read;
4452 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004453 case RTL_GIGA_MAC_VER_40:
4454 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004455 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004456 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004457 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004458 case RTL_GIGA_MAC_VER_45:
4459 case RTL_GIGA_MAC_VER_46:
4460 case RTL_GIGA_MAC_VER_47:
4461 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004462 case RTL_GIGA_MAC_VER_49:
4463 case RTL_GIGA_MAC_VER_50:
4464 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004465 ops->write = r8168g_mdio_write;
4466 ops->read = r8168g_mdio_read;
4467 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004468 default:
4469 ops->write = r8169_mdio_write;
4470 ops->read = r8169_mdio_read;
4471 break;
4472 }
4473}
4474
hayeswange2409d82013-03-31 17:02:04 +00004475static void rtl_speed_down(struct rtl8169_private *tp)
4476{
4477 u32 adv;
4478 int lpa;
4479
4480 rtl_writephy(tp, 0x1f, 0x0000);
4481 lpa = rtl_readphy(tp, MII_LPA);
4482
4483 if (lpa & (LPA_10HALF | LPA_10FULL))
4484 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4485 else if (lpa & (LPA_100HALF | LPA_100FULL))
4486 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4487 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4488 else
4489 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4490 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4491 (tp->mii.supports_gmii ?
4492 ADVERTISED_1000baseT_Half |
4493 ADVERTISED_1000baseT_Full : 0);
4494
4495 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4496 adv);
4497}
4498
David S. Miller1805b2f2011-10-24 18:18:09 -04004499static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4500{
4501 void __iomem *ioaddr = tp->mmio_addr;
4502
4503 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004504 case RTL_GIGA_MAC_VER_25:
4505 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004506 case RTL_GIGA_MAC_VER_29:
4507 case RTL_GIGA_MAC_VER_30:
4508 case RTL_GIGA_MAC_VER_32:
4509 case RTL_GIGA_MAC_VER_33:
4510 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004511 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004512 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004513 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004514 case RTL_GIGA_MAC_VER_40:
4515 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004516 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004517 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004518 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004519 case RTL_GIGA_MAC_VER_45:
4520 case RTL_GIGA_MAC_VER_46:
4521 case RTL_GIGA_MAC_VER_47:
4522 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004523 case RTL_GIGA_MAC_VER_49:
4524 case RTL_GIGA_MAC_VER_50:
4525 case RTL_GIGA_MAC_VER_51:
David S. Miller1805b2f2011-10-24 18:18:09 -04004526 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4527 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4528 break;
4529 default:
4530 break;
4531 }
4532}
4533
4534static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4535{
4536 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4537 return false;
4538
hayeswange2409d82013-03-31 17:02:04 +00004539 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004540 rtl_wol_suspend_quirk(tp);
4541
4542 return true;
4543}
4544
françois romieu065c27c2011-01-03 15:08:12 +00004545static void r810x_phy_power_down(struct rtl8169_private *tp)
4546{
4547 rtl_writephy(tp, 0x1f, 0x0000);
4548 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4549}
4550
4551static void r810x_phy_power_up(struct rtl8169_private *tp)
4552{
4553 rtl_writephy(tp, 0x1f, 0x0000);
4554 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4555}
4556
4557static void r810x_pll_power_down(struct rtl8169_private *tp)
4558{
Hayes Wang00042992012-03-30 14:33:00 +08004559 void __iomem *ioaddr = tp->mmio_addr;
4560
David S. Miller1805b2f2011-10-24 18:18:09 -04004561 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004562 return;
françois romieu065c27c2011-01-03 15:08:12 +00004563
4564 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004565
4566 switch (tp->mac_version) {
4567 case RTL_GIGA_MAC_VER_07:
4568 case RTL_GIGA_MAC_VER_08:
4569 case RTL_GIGA_MAC_VER_09:
4570 case RTL_GIGA_MAC_VER_10:
4571 case RTL_GIGA_MAC_VER_13:
4572 case RTL_GIGA_MAC_VER_16:
4573 break;
4574 default:
4575 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4576 break;
4577 }
françois romieu065c27c2011-01-03 15:08:12 +00004578}
4579
4580static void r810x_pll_power_up(struct rtl8169_private *tp)
4581{
Hayes Wang00042992012-03-30 14:33:00 +08004582 void __iomem *ioaddr = tp->mmio_addr;
4583
françois romieu065c27c2011-01-03 15:08:12 +00004584 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004585
4586 switch (tp->mac_version) {
4587 case RTL_GIGA_MAC_VER_07:
4588 case RTL_GIGA_MAC_VER_08:
4589 case RTL_GIGA_MAC_VER_09:
4590 case RTL_GIGA_MAC_VER_10:
4591 case RTL_GIGA_MAC_VER_13:
4592 case RTL_GIGA_MAC_VER_16:
4593 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004594 case RTL_GIGA_MAC_VER_47:
4595 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004596 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004597 break;
Hayes Wang00042992012-03-30 14:33:00 +08004598 default:
4599 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4600 break;
4601 }
françois romieu065c27c2011-01-03 15:08:12 +00004602}
4603
4604static void r8168_phy_power_up(struct rtl8169_private *tp)
4605{
4606 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004607 switch (tp->mac_version) {
4608 case RTL_GIGA_MAC_VER_11:
4609 case RTL_GIGA_MAC_VER_12:
4610 case RTL_GIGA_MAC_VER_17:
4611 case RTL_GIGA_MAC_VER_18:
4612 case RTL_GIGA_MAC_VER_19:
4613 case RTL_GIGA_MAC_VER_20:
4614 case RTL_GIGA_MAC_VER_21:
4615 case RTL_GIGA_MAC_VER_22:
4616 case RTL_GIGA_MAC_VER_23:
4617 case RTL_GIGA_MAC_VER_24:
4618 case RTL_GIGA_MAC_VER_25:
4619 case RTL_GIGA_MAC_VER_26:
4620 case RTL_GIGA_MAC_VER_27:
4621 case RTL_GIGA_MAC_VER_28:
4622 case RTL_GIGA_MAC_VER_31:
4623 rtl_writephy(tp, 0x0e, 0x0000);
4624 break;
4625 default:
4626 break;
4627 }
françois romieu065c27c2011-01-03 15:08:12 +00004628 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4629}
4630
4631static void r8168_phy_power_down(struct rtl8169_private *tp)
4632{
4633 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004634 switch (tp->mac_version) {
4635 case RTL_GIGA_MAC_VER_32:
4636 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004637 case RTL_GIGA_MAC_VER_40:
4638 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004639 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4640 break;
4641
4642 case RTL_GIGA_MAC_VER_11:
4643 case RTL_GIGA_MAC_VER_12:
4644 case RTL_GIGA_MAC_VER_17:
4645 case RTL_GIGA_MAC_VER_18:
4646 case RTL_GIGA_MAC_VER_19:
4647 case RTL_GIGA_MAC_VER_20:
4648 case RTL_GIGA_MAC_VER_21:
4649 case RTL_GIGA_MAC_VER_22:
4650 case RTL_GIGA_MAC_VER_23:
4651 case RTL_GIGA_MAC_VER_24:
4652 case RTL_GIGA_MAC_VER_25:
4653 case RTL_GIGA_MAC_VER_26:
4654 case RTL_GIGA_MAC_VER_27:
4655 case RTL_GIGA_MAC_VER_28:
4656 case RTL_GIGA_MAC_VER_31:
4657 rtl_writephy(tp, 0x0e, 0x0200);
4658 default:
4659 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4660 break;
4661 }
françois romieu065c27c2011-01-03 15:08:12 +00004662}
4663
4664static void r8168_pll_power_down(struct rtl8169_private *tp)
4665{
4666 void __iomem *ioaddr = tp->mmio_addr;
4667
Francois Romieucecb5fd2011-04-01 10:21:07 +02004668 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4669 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004670 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4671 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4672 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4673 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Lin2f8c0402014-10-01 23:17:19 +08004674 r8168_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004675 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004676 }
françois romieu065c27c2011-01-03 15:08:12 +00004677
Francois Romieucecb5fd2011-04-01 10:21:07 +02004678 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4679 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004680 (RTL_R16(CPlusCmd) & ASF)) {
4681 return;
4682 }
4683
hayeswang01dc7fe2011-03-21 01:50:28 +00004684 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4685 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004686 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004687
David S. Miller1805b2f2011-10-24 18:18:09 -04004688 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004689 return;
françois romieu065c27c2011-01-03 15:08:12 +00004690
4691 r8168_phy_power_down(tp);
4692
4693 switch (tp->mac_version) {
4694 case RTL_GIGA_MAC_VER_25:
4695 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004696 case RTL_GIGA_MAC_VER_27:
4697 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004698 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004699 case RTL_GIGA_MAC_VER_32:
4700 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004701 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004702 case RTL_GIGA_MAC_VER_45:
4703 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004704 case RTL_GIGA_MAC_VER_50:
4705 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004706 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4707 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004708 case RTL_GIGA_MAC_VER_40:
4709 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004710 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004711 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004712 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004713 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004714 break;
françois romieu065c27c2011-01-03 15:08:12 +00004715 }
4716}
4717
4718static void r8168_pll_power_up(struct rtl8169_private *tp)
4719{
4720 void __iomem *ioaddr = tp->mmio_addr;
4721
françois romieu065c27c2011-01-03 15:08:12 +00004722 switch (tp->mac_version) {
4723 case RTL_GIGA_MAC_VER_25:
4724 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004725 case RTL_GIGA_MAC_VER_27:
4726 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004727 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004728 case RTL_GIGA_MAC_VER_32:
4729 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004730 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4731 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004732 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004733 case RTL_GIGA_MAC_VER_45:
4734 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004735 case RTL_GIGA_MAC_VER_50:
4736 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004737 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004738 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004739 case RTL_GIGA_MAC_VER_40:
4740 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004741 case RTL_GIGA_MAC_VER_49:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004742 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004743 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004744 0x00000000, ERIAR_EXGMAC);
4745 break;
françois romieu065c27c2011-01-03 15:08:12 +00004746 }
4747
4748 r8168_phy_power_up(tp);
4749}
4750
Francois Romieud58d46b2011-05-03 16:38:29 +02004751static void rtl_generic_op(struct rtl8169_private *tp,
4752 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004753{
4754 if (op)
4755 op(tp);
4756}
4757
4758static void rtl_pll_power_down(struct rtl8169_private *tp)
4759{
Francois Romieud58d46b2011-05-03 16:38:29 +02004760 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004761}
4762
4763static void rtl_pll_power_up(struct rtl8169_private *tp)
4764{
Francois Romieud58d46b2011-05-03 16:38:29 +02004765 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004766}
4767
Bill Pembertonbaf63292012-12-03 09:23:28 -05004768static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004769{
4770 struct pll_power_ops *ops = &tp->pll_power_ops;
4771
4772 switch (tp->mac_version) {
4773 case RTL_GIGA_MAC_VER_07:
4774 case RTL_GIGA_MAC_VER_08:
4775 case RTL_GIGA_MAC_VER_09:
4776 case RTL_GIGA_MAC_VER_10:
4777 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004778 case RTL_GIGA_MAC_VER_29:
4779 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004780 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004781 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004782 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004783 case RTL_GIGA_MAC_VER_47:
4784 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004785 ops->down = r810x_pll_power_down;
4786 ops->up = r810x_pll_power_up;
4787 break;
4788
4789 case RTL_GIGA_MAC_VER_11:
4790 case RTL_GIGA_MAC_VER_12:
4791 case RTL_GIGA_MAC_VER_17:
4792 case RTL_GIGA_MAC_VER_18:
4793 case RTL_GIGA_MAC_VER_19:
4794 case RTL_GIGA_MAC_VER_20:
4795 case RTL_GIGA_MAC_VER_21:
4796 case RTL_GIGA_MAC_VER_22:
4797 case RTL_GIGA_MAC_VER_23:
4798 case RTL_GIGA_MAC_VER_24:
4799 case RTL_GIGA_MAC_VER_25:
4800 case RTL_GIGA_MAC_VER_26:
4801 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004802 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004803 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004804 case RTL_GIGA_MAC_VER_32:
4805 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004806 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004807 case RTL_GIGA_MAC_VER_35:
4808 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004809 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004810 case RTL_GIGA_MAC_VER_40:
4811 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004812 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004813 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004814 case RTL_GIGA_MAC_VER_45:
4815 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004816 case RTL_GIGA_MAC_VER_49:
4817 case RTL_GIGA_MAC_VER_50:
4818 case RTL_GIGA_MAC_VER_51:
françois romieu065c27c2011-01-03 15:08:12 +00004819 ops->down = r8168_pll_power_down;
4820 ops->up = r8168_pll_power_up;
4821 break;
4822
4823 default:
4824 ops->down = NULL;
4825 ops->up = NULL;
4826 break;
4827 }
4828}
4829
Hayes Wange542a222011-07-06 15:58:04 +08004830static void rtl_init_rxcfg(struct rtl8169_private *tp)
4831{
4832 void __iomem *ioaddr = tp->mmio_addr;
4833
4834 switch (tp->mac_version) {
4835 case RTL_GIGA_MAC_VER_01:
4836 case RTL_GIGA_MAC_VER_02:
4837 case RTL_GIGA_MAC_VER_03:
4838 case RTL_GIGA_MAC_VER_04:
4839 case RTL_GIGA_MAC_VER_05:
4840 case RTL_GIGA_MAC_VER_06:
4841 case RTL_GIGA_MAC_VER_10:
4842 case RTL_GIGA_MAC_VER_11:
4843 case RTL_GIGA_MAC_VER_12:
4844 case RTL_GIGA_MAC_VER_13:
4845 case RTL_GIGA_MAC_VER_14:
4846 case RTL_GIGA_MAC_VER_15:
4847 case RTL_GIGA_MAC_VER_16:
4848 case RTL_GIGA_MAC_VER_17:
4849 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4850 break;
4851 case RTL_GIGA_MAC_VER_18:
4852 case RTL_GIGA_MAC_VER_19:
4853 case RTL_GIGA_MAC_VER_20:
4854 case RTL_GIGA_MAC_VER_21:
4855 case RTL_GIGA_MAC_VER_22:
4856 case RTL_GIGA_MAC_VER_23:
4857 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004858 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004859 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004860 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4861 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004862 case RTL_GIGA_MAC_VER_40:
Michel Dänzer7a9810e2014-07-17 12:55:40 +09004863 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4864 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004865 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:
hayeswangbeb330a2013-04-01 22:23:39 +00004876 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4877 break;
Hayes Wange542a222011-07-06 15:58:04 +08004878 default:
4879 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4880 break;
4881 }
4882}
4883
Hayes Wang92fc43b2011-07-06 15:58:03 +08004884static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4885{
Timo Teräs9fba0812013-01-15 21:01:24 +00004886 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004887}
4888
Francois Romieud58d46b2011-05-03 16:38:29 +02004889static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4890{
françois romieu9c5028e2012-03-02 04:43:14 +00004891 void __iomem *ioaddr = tp->mmio_addr;
4892
4893 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004894 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004895 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004896}
4897
4898static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4899{
françois romieu9c5028e2012-03-02 04:43:14 +00004900 void __iomem *ioaddr = tp->mmio_addr;
4901
4902 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004903 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004904 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004905}
4906
4907static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4908{
4909 void __iomem *ioaddr = tp->mmio_addr;
4910
4911 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4912 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4913 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4914}
4915
4916static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4917{
4918 void __iomem *ioaddr = tp->mmio_addr;
4919
4920 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4921 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4922 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4923}
4924
4925static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4926{
4927 void __iomem *ioaddr = tp->mmio_addr;
4928
4929 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4930}
4931
4932static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4933{
4934 void __iomem *ioaddr = tp->mmio_addr;
4935
4936 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4937}
4938
4939static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4940{
4941 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004942
4943 RTL_W8(MaxTxPacketSize, 0x3f);
4944 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4945 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004946 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004947}
4948
4949static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4950{
4951 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004952
4953 RTL_W8(MaxTxPacketSize, 0x0c);
4954 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4955 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004956 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004957}
4958
4959static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4960{
4961 rtl_tx_performance_tweak(tp->pci_dev,
4962 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4963}
4964
4965static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4966{
4967 rtl_tx_performance_tweak(tp->pci_dev,
4968 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4969}
4970
4971static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4972{
4973 void __iomem *ioaddr = tp->mmio_addr;
4974
4975 r8168b_0_hw_jumbo_enable(tp);
4976
4977 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4978}
4979
4980static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4981{
4982 void __iomem *ioaddr = tp->mmio_addr;
4983
4984 r8168b_0_hw_jumbo_disable(tp);
4985
4986 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4987}
4988
Bill Pembertonbaf63292012-12-03 09:23:28 -05004989static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004990{
4991 struct jumbo_ops *ops = &tp->jumbo_ops;
4992
4993 switch (tp->mac_version) {
4994 case RTL_GIGA_MAC_VER_11:
4995 ops->disable = r8168b_0_hw_jumbo_disable;
4996 ops->enable = r8168b_0_hw_jumbo_enable;
4997 break;
4998 case RTL_GIGA_MAC_VER_12:
4999 case RTL_GIGA_MAC_VER_17:
5000 ops->disable = r8168b_1_hw_jumbo_disable;
5001 ops->enable = r8168b_1_hw_jumbo_enable;
5002 break;
5003 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5004 case RTL_GIGA_MAC_VER_19:
5005 case RTL_GIGA_MAC_VER_20:
5006 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5007 case RTL_GIGA_MAC_VER_22:
5008 case RTL_GIGA_MAC_VER_23:
5009 case RTL_GIGA_MAC_VER_24:
5010 case RTL_GIGA_MAC_VER_25:
5011 case RTL_GIGA_MAC_VER_26:
5012 ops->disable = r8168c_hw_jumbo_disable;
5013 ops->enable = r8168c_hw_jumbo_enable;
5014 break;
5015 case RTL_GIGA_MAC_VER_27:
5016 case RTL_GIGA_MAC_VER_28:
5017 ops->disable = r8168dp_hw_jumbo_disable;
5018 ops->enable = r8168dp_hw_jumbo_enable;
5019 break;
5020 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5021 case RTL_GIGA_MAC_VER_32:
5022 case RTL_GIGA_MAC_VER_33:
5023 case RTL_GIGA_MAC_VER_34:
5024 ops->disable = r8168e_hw_jumbo_disable;
5025 ops->enable = r8168e_hw_jumbo_enable;
5026 break;
5027
5028 /*
5029 * No action needed for jumbo frames with 8169.
5030 * No jumbo for 810x at all.
5031 */
Hayes Wangc5583862012-07-02 17:23:22 +08005032 case RTL_GIGA_MAC_VER_40:
5033 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00005034 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00005035 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08005036 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005037 case RTL_GIGA_MAC_VER_45:
5038 case RTL_GIGA_MAC_VER_46:
5039 case RTL_GIGA_MAC_VER_47:
5040 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005041 case RTL_GIGA_MAC_VER_49:
5042 case RTL_GIGA_MAC_VER_50:
5043 case RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02005044 default:
5045 ops->disable = NULL;
5046 ops->enable = NULL;
5047 break;
5048 }
5049}
5050
Francois Romieuffc46952012-07-06 14:19:23 +02005051DECLARE_RTL_COND(rtl_chipcmd_cond)
5052{
5053 void __iomem *ioaddr = tp->mmio_addr;
5054
5055 return RTL_R8(ChipCmd) & CmdReset;
5056}
5057
Francois Romieu6f43adc2011-04-29 15:05:51 +02005058static void rtl_hw_reset(struct rtl8169_private *tp)
5059{
5060 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02005061
Francois Romieu6f43adc2011-04-29 15:05:51 +02005062 RTL_W8(ChipCmd, CmdReset);
5063
Francois Romieuffc46952012-07-06 14:19:23 +02005064 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Florian Westphal1e918872014-10-01 13:38:03 +02005065
5066 netdev_reset_queue(tp->dev);
Francois Romieu6f43adc2011-04-29 15:05:51 +02005067}
5068
Francois Romieub6ffd972011-06-17 17:00:05 +02005069static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
5070{
5071 struct rtl_fw *rtl_fw;
5072 const char *name;
5073 int rc = -ENOMEM;
5074
5075 name = rtl_lookup_firmware_name(tp);
5076 if (!name)
5077 goto out_no_firmware;
5078
5079 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5080 if (!rtl_fw)
5081 goto err_warn;
5082
5083 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5084 if (rc < 0)
5085 goto err_free;
5086
Francois Romieufd112f22011-06-18 00:10:29 +02005087 rc = rtl_check_firmware(tp, rtl_fw);
5088 if (rc < 0)
5089 goto err_release_firmware;
5090
Francois Romieub6ffd972011-06-17 17:00:05 +02005091 tp->rtl_fw = rtl_fw;
5092out:
5093 return;
5094
Francois Romieufd112f22011-06-18 00:10:29 +02005095err_release_firmware:
5096 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02005097err_free:
5098 kfree(rtl_fw);
5099err_warn:
5100 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5101 name, rc);
5102out_no_firmware:
5103 tp->rtl_fw = NULL;
5104 goto out;
5105}
5106
François Romieu953a12c2011-04-24 17:38:48 +02005107static void rtl_request_firmware(struct rtl8169_private *tp)
5108{
Francois Romieub6ffd972011-06-17 17:00:05 +02005109 if (IS_ERR(tp->rtl_fw))
5110 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02005111}
5112
Hayes Wang92fc43b2011-07-06 15:58:03 +08005113static void rtl_rx_close(struct rtl8169_private *tp)
5114{
5115 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08005116
Francois Romieu1687b562011-07-19 17:21:29 +02005117 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005118}
5119
Francois Romieuffc46952012-07-06 14:19:23 +02005120DECLARE_RTL_COND(rtl_npq_cond)
5121{
5122 void __iomem *ioaddr = tp->mmio_addr;
5123
5124 return RTL_R8(TxPoll) & NPQ;
5125}
5126
5127DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5128{
5129 void __iomem *ioaddr = tp->mmio_addr;
5130
5131 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5132}
5133
françois romieue6de30d2011-01-03 15:08:37 +00005134static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135{
françois romieue6de30d2011-01-03 15:08:37 +00005136 void __iomem *ioaddr = tp->mmio_addr;
5137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00005139 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Hayes Wang92fc43b2011-07-06 15:58:03 +08005141 rtl_rx_close(tp);
5142
Hayes Wang5d2e1952011-02-22 17:26:22 +08005143 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00005144 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5145 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02005146 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08005147 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005148 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5149 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5150 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5151 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5152 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5153 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5154 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5155 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5156 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5157 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5158 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5159 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005160 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5161 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5162 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5163 tp->mac_version == RTL_GIGA_MAC_VER_51) {
David S. Miller8decf862011-09-22 03:23:13 -04005164 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02005165 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08005166 } else {
5167 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5168 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00005169 }
5170
Hayes Wang92fc43b2011-07-06 15:58:03 +08005171 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172}
5173
Francois Romieu7f796d832007-06-11 23:04:41 +02005174static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005175{
5176 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01005177
5178 /* Set DMA burst size and Interframe Gap Time */
5179 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5180 (InterFrameGap << TxInterFrameGapShift));
5181}
5182
Francois Romieu07ce4062007-02-23 23:36:39 +01005183static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184{
5185 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Francois Romieu07ce4062007-02-23 23:36:39 +01005187 tp->hw_start(dev);
5188
Francois Romieuda78dbf2012-01-26 14:18:23 +01005189 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01005190}
5191
Francois Romieu7f796d832007-06-11 23:04:41 +02005192static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5193 void __iomem *ioaddr)
5194{
5195 /*
5196 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5197 * register to be written before TxDescAddrLow to work.
5198 * Switching from MMIO to I/O access fixes the issue as well.
5199 */
5200 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005201 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005202 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07005203 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02005204}
5205
5206static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5207{
5208 u16 cmd;
5209
5210 cmd = RTL_R16(CPlusCmd);
5211 RTL_W16(CPlusCmd, cmd);
5212 return cmd;
5213}
5214
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07005215static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02005216{
5217 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00005218 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02005219}
5220
Francois Romieu6dccd162007-02-13 23:38:05 +01005221static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5222{
Francois Romieu37441002011-06-17 22:58:54 +02005223 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01005224 u32 mac_version;
5225 u32 clk;
5226 u32 val;
5227 } cfg2_info [] = {
5228 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5229 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5230 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5231 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02005232 };
5233 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01005234 unsigned int i;
5235 u32 clk;
5236
5237 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01005238 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01005239 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5240 RTL_W32(0x7c, p->val);
5241 break;
5242 }
5243 }
5244}
5245
Francois Romieue6b763e2012-03-08 09:35:39 +01005246static void rtl_set_rx_mode(struct net_device *dev)
5247{
5248 struct rtl8169_private *tp = netdev_priv(dev);
5249 void __iomem *ioaddr = tp->mmio_addr;
5250 u32 mc_filter[2]; /* Multicast hash filter */
5251 int rx_mode;
5252 u32 tmp = 0;
5253
5254 if (dev->flags & IFF_PROMISC) {
5255 /* Unconditionally log net taps. */
5256 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5257 rx_mode =
5258 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5259 AcceptAllPhys;
5260 mc_filter[1] = mc_filter[0] = 0xffffffff;
5261 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5262 (dev->flags & IFF_ALLMULTI)) {
5263 /* Too many to filter perfectly -- accept all multicasts. */
5264 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5265 mc_filter[1] = mc_filter[0] = 0xffffffff;
5266 } else {
5267 struct netdev_hw_addr *ha;
5268
5269 rx_mode = AcceptBroadcast | AcceptMyPhys;
5270 mc_filter[1] = mc_filter[0] = 0;
5271 netdev_for_each_mc_addr(ha, dev) {
5272 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5273 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5274 rx_mode |= AcceptMulticast;
5275 }
5276 }
5277
5278 if (dev->features & NETIF_F_RXALL)
5279 rx_mode |= (AcceptErr | AcceptRunt);
5280
5281 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5282
5283 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5284 u32 data = mc_filter[0];
5285
5286 mc_filter[0] = swab32(mc_filter[1]);
5287 mc_filter[1] = swab32(data);
5288 }
5289
Nathan Walp04817762012-11-01 12:08:47 +00005290 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5291 mc_filter[1] = mc_filter[0] = 0xffffffff;
5292
Francois Romieue6b763e2012-03-08 09:35:39 +01005293 RTL_W32(MAR0 + 4, mc_filter[1]);
5294 RTL_W32(MAR0 + 0, mc_filter[0]);
5295
5296 RTL_W32(RxConfig, tmp);
5297}
5298
Francois Romieu07ce4062007-02-23 23:36:39 +01005299static void rtl_hw_start_8169(struct net_device *dev)
5300{
5301 struct rtl8169_private *tp = netdev_priv(dev);
5302 void __iomem *ioaddr = tp->mmio_addr;
5303 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01005304
Francois Romieu9cb427b2006-11-02 00:10:16 +01005305 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5306 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5307 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5308 }
5309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02005311 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5312 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5313 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5314 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01005315 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5316
Hayes Wange542a222011-07-06 15:58:04 +08005317 rtl_init_rxcfg(tp);
5318
françois romieuf0298f82011-01-03 15:07:42 +00005319 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005321 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
Francois Romieucecb5fd2011-04-01 10:21:07 +02005323 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5324 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5325 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5326 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02005327 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
Francois Romieu7f796d832007-06-11 23:04:41 +02005329 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005330
Francois Romieucecb5fd2011-04-01 10:21:07 +02005331 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5332 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005333 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02005335 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 }
5337
Francois Romieubcf0bf92006-07-26 23:14:13 +02005338 RTL_W16(CPlusCmd, tp->cp_cmd);
5339
Francois Romieu6dccd162007-02-13 23:38:05 +01005340 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5341
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 /*
5343 * Undocumented corner. Supposedly:
5344 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5345 */
5346 RTL_W16(IntrMitigate, 0x0000);
5347
Francois Romieu7f796d832007-06-11 23:04:41 +02005348 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01005349
Francois Romieucecb5fd2011-04-01 10:21:07 +02005350 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5351 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5352 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5353 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005354 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5355 rtl_set_rx_tx_config_registers(tp);
5356 }
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005359
5360 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5361 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
5363 RTL_W32(RxMissed, 0);
5364
Francois Romieu07ce4062007-02-23 23:36:39 +01005365 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
5367 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005368 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005369}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005371static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5372{
5373 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005374 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005375}
5376
5377static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5378{
Francois Romieu52989f02012-07-06 13:37:00 +02005379 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005380}
5381
5382static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005383{
5384 u32 csi;
5385
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005386 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5387 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005388}
5389
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005390static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005391{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005392 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005393}
5394
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005395static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005396{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005397 rtl_csi_access_enable(tp, 0x27000000);
5398}
5399
Francois Romieuffc46952012-07-06 14:19:23 +02005400DECLARE_RTL_COND(rtl_csiar_cond)
5401{
5402 void __iomem *ioaddr = tp->mmio_addr;
5403
5404 return RTL_R32(CSIAR) & CSIAR_FLAG;
5405}
5406
Francois Romieu52989f02012-07-06 13:37:00 +02005407static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005408{
Francois Romieu52989f02012-07-06 13:37:00 +02005409 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005410
5411 RTL_W32(CSIDR, value);
5412 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5413 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5414
Francois Romieuffc46952012-07-06 14:19:23 +02005415 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005416}
5417
Francois Romieu52989f02012-07-06 13:37:00 +02005418static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005419{
Francois Romieu52989f02012-07-06 13:37:00 +02005420 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005421
5422 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5423 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5424
Francois Romieuffc46952012-07-06 14:19:23 +02005425 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5426 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005427}
5428
Francois Romieu52989f02012-07-06 13:37:00 +02005429static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005430{
Francois Romieu52989f02012-07-06 13:37:00 +02005431 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005432
5433 RTL_W32(CSIDR, value);
5434 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5435 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5436 CSIAR_FUNC_NIC);
5437
Francois Romieuffc46952012-07-06 14:19:23 +02005438 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005439}
5440
Francois Romieu52989f02012-07-06 13:37:00 +02005441static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005442{
Francois Romieu52989f02012-07-06 13:37:00 +02005443 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005444
5445 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5446 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5447
Francois Romieuffc46952012-07-06 14:19:23 +02005448 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5449 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005450}
5451
hayeswang45dd95c2013-07-08 17:09:01 +08005452static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5453{
5454 void __iomem *ioaddr = tp->mmio_addr;
5455
5456 RTL_W32(CSIDR, value);
5457 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5458 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5459 CSIAR_FUNC_NIC2);
5460
5461 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5462}
5463
5464static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5465{
5466 void __iomem *ioaddr = tp->mmio_addr;
5467
5468 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5469 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5470
5471 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5472 RTL_R32(CSIDR) : ~0;
5473}
5474
Bill Pembertonbaf63292012-12-03 09:23:28 -05005475static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005476{
5477 struct csi_ops *ops = &tp->csi_ops;
5478
5479 switch (tp->mac_version) {
5480 case RTL_GIGA_MAC_VER_01:
5481 case RTL_GIGA_MAC_VER_02:
5482 case RTL_GIGA_MAC_VER_03:
5483 case RTL_GIGA_MAC_VER_04:
5484 case RTL_GIGA_MAC_VER_05:
5485 case RTL_GIGA_MAC_VER_06:
5486 case RTL_GIGA_MAC_VER_10:
5487 case RTL_GIGA_MAC_VER_11:
5488 case RTL_GIGA_MAC_VER_12:
5489 case RTL_GIGA_MAC_VER_13:
5490 case RTL_GIGA_MAC_VER_14:
5491 case RTL_GIGA_MAC_VER_15:
5492 case RTL_GIGA_MAC_VER_16:
5493 case RTL_GIGA_MAC_VER_17:
5494 ops->write = NULL;
5495 ops->read = NULL;
5496 break;
5497
Hayes Wang7e18dca2012-03-30 14:33:02 +08005498 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005499 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005500 ops->write = r8402_csi_write;
5501 ops->read = r8402_csi_read;
5502 break;
5503
hayeswang45dd95c2013-07-08 17:09:01 +08005504 case RTL_GIGA_MAC_VER_44:
5505 ops->write = r8411_csi_write;
5506 ops->read = r8411_csi_read;
5507 break;
5508
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005509 default:
5510 ops->write = r8169_csi_write;
5511 ops->read = r8169_csi_read;
5512 break;
5513 }
Francois Romieudacf8152008-08-02 20:44:13 +02005514}
5515
5516struct ephy_info {
5517 unsigned int offset;
5518 u16 mask;
5519 u16 bits;
5520};
5521
Francois Romieufdf6fc02012-07-06 22:40:38 +02005522static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5523 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005524{
5525 u16 w;
5526
5527 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005528 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5529 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005530 e++;
5531 }
5532}
5533
Francois Romieub726e492008-06-28 12:22:59 +02005534static void rtl_disable_clock_request(struct pci_dev *pdev)
5535{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005536 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5537 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005538}
5539
françois romieue6de30d2011-01-03 15:08:37 +00005540static void rtl_enable_clock_request(struct pci_dev *pdev)
5541{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005542 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5543 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005544}
5545
hayeswangb51ecea2014-07-09 14:52:51 +08005546static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5547{
5548 void __iomem *ioaddr = tp->mmio_addr;
5549 u8 data;
5550
5551 data = RTL_R8(Config3);
5552
5553 if (enable)
5554 data |= Rdy_to_L23;
5555 else
5556 data &= ~Rdy_to_L23;
5557
5558 RTL_W8(Config3, data);
5559}
5560
Francois Romieub726e492008-06-28 12:22:59 +02005561#define R8168_CPCMD_QUIRK_MASK (\
5562 EnableBist | \
5563 Mac_dbgo_oe | \
5564 Force_half_dup | \
5565 Force_rxflow_en | \
5566 Force_txflow_en | \
5567 Cxpl_dbg_sel | \
5568 ASF | \
5569 PktCntrDisable | \
5570 Mac_dbgo_sel)
5571
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005572static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005573{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005574 void __iomem *ioaddr = tp->mmio_addr;
5575 struct pci_dev *pdev = tp->pci_dev;
5576
Francois Romieub726e492008-06-28 12:22:59 +02005577 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5578
5579 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5580
françois romieufaf1e782013-02-27 13:01:57 +00005581 if (tp->dev->mtu <= ETH_DATA_LEN) {
5582 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5583 PCI_EXP_DEVCTL_NOSNOOP_EN);
5584 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005585}
5586
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005587static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005588{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005589 void __iomem *ioaddr = tp->mmio_addr;
5590
5591 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005592
françois romieuf0298f82011-01-03 15:07:42 +00005593 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005594
5595 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005596}
5597
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005598static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005599{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005600 void __iomem *ioaddr = tp->mmio_addr;
5601 struct pci_dev *pdev = tp->pci_dev;
5602
Francois Romieub726e492008-06-28 12:22:59 +02005603 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5604
5605 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5606
françois romieufaf1e782013-02-27 13:01:57 +00005607 if (tp->dev->mtu <= ETH_DATA_LEN)
5608 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005609
5610 rtl_disable_clock_request(pdev);
5611
5612 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005613}
5614
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005615static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005616{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005617 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005618 { 0x01, 0, 0x0001 },
5619 { 0x02, 0x0800, 0x1000 },
5620 { 0x03, 0, 0x0042 },
5621 { 0x06, 0x0080, 0x0000 },
5622 { 0x07, 0, 0x2000 }
5623 };
5624
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005625 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005626
Francois Romieufdf6fc02012-07-06 22:40:38 +02005627 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005628
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005629 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005630}
5631
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005632static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005633{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005634 void __iomem *ioaddr = tp->mmio_addr;
5635 struct pci_dev *pdev = tp->pci_dev;
5636
5637 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005638
5639 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5640
françois romieufaf1e782013-02-27 13:01:57 +00005641 if (tp->dev->mtu <= ETH_DATA_LEN)
5642 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005643
5644 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5645}
5646
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005647static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005648{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005649 void __iomem *ioaddr = tp->mmio_addr;
5650 struct pci_dev *pdev = tp->pci_dev;
5651
5652 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005653
5654 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5655
5656 /* Magic. */
5657 RTL_W8(DBG_REG, 0x20);
5658
françois romieuf0298f82011-01-03 15:07:42 +00005659 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005660
françois romieufaf1e782013-02-27 13:01:57 +00005661 if (tp->dev->mtu <= ETH_DATA_LEN)
5662 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005663
5664 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5665}
5666
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005667static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005668{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005669 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005670 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005671 { 0x02, 0x0800, 0x1000 },
5672 { 0x03, 0, 0x0002 },
5673 { 0x06, 0x0080, 0x0000 }
5674 };
5675
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005676 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005677
5678 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5679
Francois Romieufdf6fc02012-07-06 22:40:38 +02005680 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005681
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005682 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005683}
5684
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005685static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005686{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005687 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005688 { 0x01, 0, 0x0001 },
5689 { 0x03, 0x0400, 0x0220 }
5690 };
5691
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005692 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005693
Francois Romieufdf6fc02012-07-06 22:40:38 +02005694 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005695
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005696 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005697}
5698
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005699static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005700{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005701 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005702}
5703
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005704static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005705{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005706 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005707
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005708 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005709}
5710
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005711static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005712{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005713 void __iomem *ioaddr = tp->mmio_addr;
5714 struct pci_dev *pdev = tp->pci_dev;
5715
5716 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005717
5718 rtl_disable_clock_request(pdev);
5719
françois romieuf0298f82011-01-03 15:07:42 +00005720 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005721
françois romieufaf1e782013-02-27 13:01:57 +00005722 if (tp->dev->mtu <= ETH_DATA_LEN)
5723 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005724
5725 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5726}
5727
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005728static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005729{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005730 void __iomem *ioaddr = tp->mmio_addr;
5731 struct pci_dev *pdev = tp->pci_dev;
5732
5733 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005734
françois romieufaf1e782013-02-27 13:01:57 +00005735 if (tp->dev->mtu <= ETH_DATA_LEN)
5736 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005737
5738 RTL_W8(MaxTxPacketSize, TxPacketMax);
5739
5740 rtl_disable_clock_request(pdev);
5741}
5742
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005743static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005744{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005745 void __iomem *ioaddr = tp->mmio_addr;
5746 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005747 static const struct ephy_info e_info_8168d_4[] = {
5748 { 0x0b, ~0, 0x48 },
5749 { 0x19, 0x20, 0x50 },
5750 { 0x0c, ~0, 0x20 }
5751 };
5752 int i;
5753
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005754 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005755
5756 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5757
5758 RTL_W8(MaxTxPacketSize, TxPacketMax);
5759
5760 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5761 const struct ephy_info *e = e_info_8168d_4 + i;
5762 u16 w;
5763
Francois Romieufdf6fc02012-07-06 22:40:38 +02005764 w = rtl_ephy_read(tp, e->offset);
5765 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00005766 }
5767
5768 rtl_enable_clock_request(pdev);
5769}
5770
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005771static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005772{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005773 void __iomem *ioaddr = tp->mmio_addr;
5774 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005775 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005776 { 0x00, 0x0200, 0x0100 },
5777 { 0x00, 0x0000, 0x0004 },
5778 { 0x06, 0x0002, 0x0001 },
5779 { 0x06, 0x0000, 0x0030 },
5780 { 0x07, 0x0000, 0x2000 },
5781 { 0x00, 0x0000, 0x0020 },
5782 { 0x03, 0x5800, 0x2000 },
5783 { 0x03, 0x0000, 0x0001 },
5784 { 0x01, 0x0800, 0x1000 },
5785 { 0x07, 0x0000, 0x4000 },
5786 { 0x1e, 0x0000, 0x2000 },
5787 { 0x19, 0xffff, 0xfe6c },
5788 { 0x0a, 0x0000, 0x0040 }
5789 };
5790
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005791 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005792
Francois Romieufdf6fc02012-07-06 22:40:38 +02005793 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005794
françois romieufaf1e782013-02-27 13:01:57 +00005795 if (tp->dev->mtu <= ETH_DATA_LEN)
5796 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005797
5798 RTL_W8(MaxTxPacketSize, TxPacketMax);
5799
5800 rtl_disable_clock_request(pdev);
5801
5802 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005803 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5804 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005805
Francois Romieucecb5fd2011-04-01 10:21:07 +02005806 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005807}
5808
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005809static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005810{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005811 void __iomem *ioaddr = tp->mmio_addr;
5812 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005813 static const struct ephy_info e_info_8168e_2[] = {
5814 { 0x09, 0x0000, 0x0080 },
5815 { 0x19, 0x0000, 0x0224 }
5816 };
5817
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005818 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005819
Francois Romieufdf6fc02012-07-06 22:40:38 +02005820 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005821
françois romieufaf1e782013-02-27 13:01:57 +00005822 if (tp->dev->mtu <= ETH_DATA_LEN)
5823 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005824
Francois Romieufdf6fc02012-07-06 22:40:38 +02005825 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5826 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5827 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5828 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5829 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5830 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005831 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5832 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005833
Hayes Wang3090bd92011-09-06 16:55:15 +08005834 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005835
Francois Romieu4521e1a92012-11-01 16:46:28 +00005836 rtl_disable_clock_request(pdev);
5837
Hayes Wang70090422011-07-06 15:58:06 +08005838 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5839 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5840
5841 /* Adjust EEE LED frequency */
5842 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5843
5844 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5845 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005846 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005847}
5848
Hayes Wang5f886e02012-03-30 14:33:03 +08005849static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005850{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005851 void __iomem *ioaddr = tp->mmio_addr;
5852 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005853
Hayes Wang5f886e02012-03-30 14:33:03 +08005854 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005855
5856 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5857
Francois Romieufdf6fc02012-07-06 22:40:38 +02005858 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5859 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5860 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5861 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005862 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5863 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5864 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5865 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005866 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5867 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005868
5869 RTL_W8(MaxTxPacketSize, EarlySize);
5870
Francois Romieu4521e1a92012-11-01 16:46:28 +00005871 rtl_disable_clock_request(pdev);
5872
Hayes Wangc2218922011-09-06 16:55:18 +08005873 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5874 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005875 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005876 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5877 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005878}
5879
Hayes Wang5f886e02012-03-30 14:33:03 +08005880static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5881{
5882 void __iomem *ioaddr = tp->mmio_addr;
5883 static const struct ephy_info e_info_8168f_1[] = {
5884 { 0x06, 0x00c0, 0x0020 },
5885 { 0x08, 0x0001, 0x0002 },
5886 { 0x09, 0x0000, 0x0080 },
5887 { 0x19, 0x0000, 0x0224 }
5888 };
5889
5890 rtl_hw_start_8168f(tp);
5891
Francois Romieufdf6fc02012-07-06 22:40:38 +02005892 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005893
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005894 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005895
5896 /* Adjust EEE LED frequency */
5897 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5898}
5899
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005900static void rtl_hw_start_8411(struct rtl8169_private *tp)
5901{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005902 static const struct ephy_info e_info_8168f_1[] = {
5903 { 0x06, 0x00c0, 0x0020 },
5904 { 0x0f, 0xffff, 0x5200 },
5905 { 0x1e, 0x0000, 0x4000 },
5906 { 0x19, 0x0000, 0x0224 }
5907 };
5908
5909 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005910 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005911
Francois Romieufdf6fc02012-07-06 22:40:38 +02005912 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005913
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005914 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005915}
5916
Hayes Wangc5583862012-07-02 17:23:22 +08005917static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5918{
5919 void __iomem *ioaddr = tp->mmio_addr;
5920 struct pci_dev *pdev = tp->pci_dev;
5921
hayeswangbeb330a2013-04-01 22:23:39 +00005922 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5923
Hayes Wangc5583862012-07-02 17:23:22 +08005924 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5925 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5926 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5927 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5928
5929 rtl_csi_access_enable_1(tp);
5930
5931 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5932
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005933 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5934 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005935 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005936
Francois Romieu4521e1a92012-11-01 16:46:28 +00005937 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005938 RTL_W8(MaxTxPacketSize, EarlySize);
5939
5940 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5941 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5942
5943 /* Adjust EEE LED frequency */
5944 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5945
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005946 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5947 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005948
5949 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005950}
5951
hayeswang57538c42013-04-01 22:23:40 +00005952static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5953{
5954 void __iomem *ioaddr = tp->mmio_addr;
5955 static const struct ephy_info e_info_8168g_2[] = {
5956 { 0x00, 0x0000, 0x0008 },
5957 { 0x0c, 0x3df0, 0x0200 },
5958 { 0x19, 0xffff, 0xfc00 },
5959 { 0x1e, 0xffff, 0x20eb }
5960 };
5961
5962 rtl_hw_start_8168g_1(tp);
5963
5964 /* disable aspm and clock request before access ephy */
5965 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5966 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5967 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5968}
5969
hayeswang45dd95c2013-07-08 17:09:01 +08005970static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5971{
5972 void __iomem *ioaddr = tp->mmio_addr;
5973 static const struct ephy_info e_info_8411_2[] = {
5974 { 0x00, 0x0000, 0x0008 },
5975 { 0x0c, 0x3df0, 0x0200 },
5976 { 0x0f, 0xffff, 0x5200 },
5977 { 0x19, 0x0020, 0x0000 },
5978 { 0x1e, 0x0000, 0x2000 }
5979 };
5980
5981 rtl_hw_start_8168g_1(tp);
5982
5983 /* disable aspm and clock request before access ephy */
5984 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5985 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5986 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5987}
5988
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005989static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5990{
5991 void __iomem *ioaddr = tp->mmio_addr;
5992 struct pci_dev *pdev = tp->pci_dev;
5993 u16 rg_saw_cnt;
5994 u32 data;
5995 static const struct ephy_info e_info_8168h_1[] = {
5996 { 0x1e, 0x0800, 0x0001 },
5997 { 0x1d, 0x0000, 0x0800 },
5998 { 0x05, 0xffff, 0x2089 },
5999 { 0x06, 0xffff, 0x5881 },
6000 { 0x04, 0xffff, 0x154a },
6001 { 0x01, 0xffff, 0x068b }
6002 };
6003
6004 /* disable aspm and clock request before access ephy */
6005 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6006 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6007 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6008
6009 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6010
6011 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6012 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6013 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6014 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6015
6016 rtl_csi_access_enable_1(tp);
6017
6018 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6019
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006020 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6021 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006022
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006023 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006024
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006025 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006026
6027 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6028
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006029 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6030 RTL_W8(MaxTxPacketSize, EarlySize);
6031
6032 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6033 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6034
6035 /* Adjust EEE LED frequency */
6036 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6037
6038 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6039 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6040
6041 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6042
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006043 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006044
6045 rtl_pcie_state_l2l3_enable(tp, false);
6046
6047 rtl_writephy(tp, 0x1f, 0x0c42);
6048 rg_saw_cnt = rtl_readphy(tp, 0x13);
6049 rtl_writephy(tp, 0x1f, 0x0000);
6050 if (rg_saw_cnt > 0) {
6051 u16 sw_cnt_1ms_ini;
6052
6053 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6054 sw_cnt_1ms_ini &= 0x0fff;
6055 data = r8168_mac_ocp_read(tp, 0xd412);
6056 data &= 0x0fff;
6057 data |= sw_cnt_1ms_ini;
6058 r8168_mac_ocp_write(tp, 0xd412, data);
6059 }
6060
6061 data = r8168_mac_ocp_read(tp, 0xe056);
6062 data &= 0xf0;
6063 data |= 0x07;
6064 r8168_mac_ocp_write(tp, 0xe056, data);
6065
6066 data = r8168_mac_ocp_read(tp, 0xe052);
6067 data &= 0x8008;
6068 data |= 0x6000;
6069 r8168_mac_ocp_write(tp, 0xe052, data);
6070
6071 data = r8168_mac_ocp_read(tp, 0xe0d6);
6072 data &= 0x01ff;
6073 data |= 0x017f;
6074 r8168_mac_ocp_write(tp, 0xe0d6, data);
6075
6076 data = r8168_mac_ocp_read(tp, 0xd420);
6077 data &= 0x0fff;
6078 data |= 0x047f;
6079 r8168_mac_ocp_write(tp, 0xd420, data);
6080
6081 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6082 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6083 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6084 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6085}
6086
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006087static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6088{
6089 void __iomem *ioaddr = tp->mmio_addr;
6090 struct pci_dev *pdev = tp->pci_dev;
6091
6092 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6093
6094 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6095 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6096 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6097 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6098
6099 rtl_csi_access_enable_1(tp);
6100
6101 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6102
6103 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6104 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6105
6106 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6107
6108 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6109
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006110 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6111 RTL_W8(MaxTxPacketSize, EarlySize);
6112
6113 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6114 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6115
6116 /* Adjust EEE LED frequency */
6117 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6118
6119 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6120
6121 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6122
6123 rtl_pcie_state_l2l3_enable(tp, false);
6124}
6125
6126static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6127{
6128 void __iomem *ioaddr = tp->mmio_addr;
6129 static const struct ephy_info e_info_8168ep_1[] = {
6130 { 0x00, 0xffff, 0x10ab },
6131 { 0x06, 0xffff, 0xf030 },
6132 { 0x08, 0xffff, 0x2006 },
6133 { 0x0d, 0xffff, 0x1666 },
6134 { 0x0c, 0x3ff0, 0x0000 }
6135 };
6136
6137 /* disable aspm and clock request before access ephy */
6138 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6139 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6140 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6141
6142 rtl_hw_start_8168ep(tp);
6143}
6144
6145static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6146{
6147 void __iomem *ioaddr = tp->mmio_addr;
6148 static const struct ephy_info e_info_8168ep_2[] = {
6149 { 0x00, 0xffff, 0x10a3 },
6150 { 0x19, 0xffff, 0xfc00 },
6151 { 0x1e, 0xffff, 0x20ea }
6152 };
6153
6154 /* disable aspm and clock request before access ephy */
6155 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6156 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6157 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6158
6159 rtl_hw_start_8168ep(tp);
6160
6161 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6162 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6163}
6164
6165static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6166{
6167 void __iomem *ioaddr = tp->mmio_addr;
6168 u32 data;
6169 static const struct ephy_info e_info_8168ep_3[] = {
6170 { 0x00, 0xffff, 0x10a3 },
6171 { 0x19, 0xffff, 0x7c00 },
6172 { 0x1e, 0xffff, 0x20eb },
6173 { 0x0d, 0xffff, 0x1666 }
6174 };
6175
6176 /* disable aspm and clock request before access ephy */
6177 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6178 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6179 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6180
6181 rtl_hw_start_8168ep(tp);
6182
6183 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
6184 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6185
6186 data = r8168_mac_ocp_read(tp, 0xd3e2);
6187 data &= 0xf000;
6188 data |= 0x0271;
6189 r8168_mac_ocp_write(tp, 0xd3e2, data);
6190
6191 data = r8168_mac_ocp_read(tp, 0xd3e4);
6192 data &= 0xff00;
6193 r8168_mac_ocp_write(tp, 0xd3e4, data);
6194
6195 data = r8168_mac_ocp_read(tp, 0xe860);
6196 data |= 0x0080;
6197 r8168_mac_ocp_write(tp, 0xe860, data);
6198}
6199
Francois Romieu07ce4062007-02-23 23:36:39 +01006200static void rtl_hw_start_8168(struct net_device *dev)
6201{
Francois Romieu2dd99532007-06-11 23:22:52 +02006202 struct rtl8169_private *tp = netdev_priv(dev);
6203 void __iomem *ioaddr = tp->mmio_addr;
6204
6205 RTL_W8(Cfg9346, Cfg9346_Unlock);
6206
françois romieuf0298f82011-01-03 15:07:42 +00006207 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02006208
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006209 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02006210
Francois Romieu0e485152007-02-20 00:00:26 +01006211 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02006212
6213 RTL_W16(CPlusCmd, tp->cp_cmd);
6214
Francois Romieu0e485152007-02-20 00:00:26 +01006215 RTL_W16(IntrMitigate, 0x5151);
6216
6217 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00006218 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006219 tp->event_slow |= RxFIFOOver | PCSTimeout;
6220 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01006221 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006222
6223 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6224
hayeswang1a964642013-04-01 22:23:41 +00006225 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02006226
6227 RTL_R8(IntrMask);
6228
Francois Romieu219a1e92008-06-28 11:58:39 +02006229 switch (tp->mac_version) {
6230 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006231 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006232 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006233
6234 case RTL_GIGA_MAC_VER_12:
6235 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006236 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006237 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006238
6239 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006240 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006241 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006242
6243 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006244 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006245 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006246
6247 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006248 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006249 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006250
Francois Romieu197ff762008-06-28 13:16:02 +02006251 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006252 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006253 break;
Francois Romieu197ff762008-06-28 13:16:02 +02006254
Francois Romieu6fb07052008-06-29 11:54:28 +02006255 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006256 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006257 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02006258
Francois Romieuef3386f2008-06-29 12:24:30 +02006259 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006260 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006261 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02006262
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006263 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006264 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006265 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02006266
Francois Romieu5b538df2008-07-20 16:22:45 +02006267 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00006268 case RTL_GIGA_MAC_VER_26:
6269 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006270 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006271 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02006272
françois romieue6de30d2011-01-03 15:08:37 +00006273 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006274 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006275 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02006276
hayeswang4804b3b2011-03-21 01:50:29 +00006277 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006278 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00006279 break;
6280
hayeswang01dc7fe2011-03-21 01:50:28 +00006281 case RTL_GIGA_MAC_VER_32:
6282 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006283 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08006284 break;
6285 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006286 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00006287 break;
françois romieue6de30d2011-01-03 15:08:37 +00006288
Hayes Wangc2218922011-09-06 16:55:18 +08006289 case RTL_GIGA_MAC_VER_35:
6290 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006291 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08006292 break;
6293
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08006294 case RTL_GIGA_MAC_VER_38:
6295 rtl_hw_start_8411(tp);
6296 break;
6297
Hayes Wangc5583862012-07-02 17:23:22 +08006298 case RTL_GIGA_MAC_VER_40:
6299 case RTL_GIGA_MAC_VER_41:
6300 rtl_hw_start_8168g_1(tp);
6301 break;
hayeswang57538c42013-04-01 22:23:40 +00006302 case RTL_GIGA_MAC_VER_42:
6303 rtl_hw_start_8168g_2(tp);
6304 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006305
hayeswang45dd95c2013-07-08 17:09:01 +08006306 case RTL_GIGA_MAC_VER_44:
6307 rtl_hw_start_8411_2(tp);
6308 break;
6309
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006310 case RTL_GIGA_MAC_VER_45:
6311 case RTL_GIGA_MAC_VER_46:
6312 rtl_hw_start_8168h_1(tp);
6313 break;
6314
Chun-Hao Lin935e2212014-10-07 15:10:41 +08006315 case RTL_GIGA_MAC_VER_49:
6316 rtl_hw_start_8168ep_1(tp);
6317 break;
6318
6319 case RTL_GIGA_MAC_VER_50:
6320 rtl_hw_start_8168ep_2(tp);
6321 break;
6322
6323 case RTL_GIGA_MAC_VER_51:
6324 rtl_hw_start_8168ep_3(tp);
6325 break;
6326
Francois Romieu219a1e92008-06-28 11:58:39 +02006327 default:
6328 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6329 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00006330 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02006331 }
Francois Romieu2dd99532007-06-11 23:22:52 +02006332
hayeswang1a964642013-04-01 22:23:41 +00006333 RTL_W8(Cfg9346, Cfg9346_Lock);
6334
Francois Romieu0e485152007-02-20 00:00:26 +01006335 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6336
hayeswang1a964642013-04-01 22:23:41 +00006337 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02006338
Chun-Hao Lin05b96872014-10-01 23:17:12 +08006339 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01006340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Francois Romieu2857ffb2008-08-02 21:08:49 +02006342#define R810X_CPCMD_QUIRK_MASK (\
6343 EnableBist | \
6344 Mac_dbgo_oe | \
6345 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00006346 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02006347 Force_txflow_en | \
6348 Cxpl_dbg_sel | \
6349 ASF | \
6350 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006351 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006352
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006353static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006354{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006355 void __iomem *ioaddr = tp->mmio_addr;
6356 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08006357 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02006358 { 0x01, 0, 0x6e65 },
6359 { 0x02, 0, 0x091f },
6360 { 0x03, 0, 0xc2f9 },
6361 { 0x06, 0, 0xafb5 },
6362 { 0x07, 0, 0x0e00 },
6363 { 0x19, 0, 0xec80 },
6364 { 0x01, 0, 0x2e65 },
6365 { 0x01, 0, 0x6e65 }
6366 };
6367 u8 cfg1;
6368
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006369 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006370
6371 RTL_W8(DBG_REG, FIX_NAK_1);
6372
6373 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6374
6375 RTL_W8(Config1,
6376 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6377 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6378
6379 cfg1 = RTL_R8(Config1);
6380 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6381 RTL_W8(Config1, cfg1 & ~LEDS0);
6382
Francois Romieufdf6fc02012-07-06 22:40:38 +02006383 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02006384}
6385
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006386static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006387{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006388 void __iomem *ioaddr = tp->mmio_addr;
6389 struct pci_dev *pdev = tp->pci_dev;
6390
6391 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006392
6393 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6394
6395 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6396 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006397}
6398
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006399static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02006400{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006401 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006402
Francois Romieufdf6fc02012-07-06 22:40:38 +02006403 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006404}
6405
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006406static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006407{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006408 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006409 static const struct ephy_info e_info_8105e_1[] = {
6410 { 0x07, 0, 0x4000 },
6411 { 0x19, 0, 0x0200 },
6412 { 0x19, 0, 0x0020 },
6413 { 0x1e, 0, 0x2000 },
6414 { 0x03, 0, 0x0001 },
6415 { 0x19, 0, 0x0100 },
6416 { 0x19, 0, 0x0004 },
6417 { 0x0a, 0, 0x0020 }
6418 };
6419
Francois Romieucecb5fd2011-04-01 10:21:07 +02006420 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006421 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6422
Francois Romieucecb5fd2011-04-01 10:21:07 +02006423 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08006424 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6425
6426 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08006427 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006428
Francois Romieufdf6fc02012-07-06 22:40:38 +02006429 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08006430
6431 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006432}
6433
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006434static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08006435{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006436 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006437 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006438}
6439
Hayes Wang7e18dca2012-03-30 14:33:02 +08006440static void rtl_hw_start_8402(struct rtl8169_private *tp)
6441{
6442 void __iomem *ioaddr = tp->mmio_addr;
6443 static const struct ephy_info e_info_8402[] = {
6444 { 0x19, 0xffff, 0xff64 },
6445 { 0x1e, 0, 0x4000 }
6446 };
6447
6448 rtl_csi_access_enable_2(tp);
6449
6450 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6451 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6452
6453 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6454 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6455
Francois Romieufdf6fc02012-07-06 22:40:38 +02006456 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08006457
6458 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6459
Francois Romieufdf6fc02012-07-06 22:40:38 +02006460 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6461 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006462 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);
Francois Romieufdf6fc02012-07-06 22:40:38 +02006464 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6465 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08006466 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08006467
6468 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08006469}
6470
Hayes Wang5598bfe2012-07-02 17:23:21 +08006471static void rtl_hw_start_8106(struct rtl8169_private *tp)
6472{
6473 void __iomem *ioaddr = tp->mmio_addr;
6474
6475 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6476 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6477
Francois Romieu4521e1a92012-11-01 16:46:28 +00006478 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006479 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6480 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006481
6482 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006483}
6484
Francois Romieu07ce4062007-02-23 23:36:39 +01006485static void rtl_hw_start_8101(struct net_device *dev)
6486{
Francois Romieucdf1a602007-06-11 23:29:50 +02006487 struct rtl8169_private *tp = netdev_priv(dev);
6488 void __iomem *ioaddr = tp->mmio_addr;
6489 struct pci_dev *pdev = tp->pci_dev;
6490
Francois Romieuda78dbf2012-01-26 14:18:23 +01006491 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6492 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006493
Francois Romieucecb5fd2011-04-01 10:21:07 +02006494 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006495 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006496 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6497 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006498
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006499 RTL_W8(Cfg9346, Cfg9346_Unlock);
6500
hayeswang1a964642013-04-01 22:23:41 +00006501 RTL_W8(MaxTxPacketSize, TxPacketMax);
6502
6503 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6504
6505 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6506 RTL_W16(CPlusCmd, tp->cp_cmd);
6507
6508 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6509
6510 rtl_set_rx_tx_config_registers(tp);
6511
Francois Romieu2857ffb2008-08-02 21:08:49 +02006512 switch (tp->mac_version) {
6513 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006514 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006515 break;
6516
6517 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006518 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006519 break;
6520
6521 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006522 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006523 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006524
6525 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006526 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006527 break;
6528 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006529 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006530 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006531
6532 case RTL_GIGA_MAC_VER_37:
6533 rtl_hw_start_8402(tp);
6534 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006535
6536 case RTL_GIGA_MAC_VER_39:
6537 rtl_hw_start_8106(tp);
6538 break;
hayeswang58152cd2013-04-01 22:23:42 +00006539 case RTL_GIGA_MAC_VER_43:
6540 rtl_hw_start_8168g_2(tp);
6541 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006542 case RTL_GIGA_MAC_VER_47:
6543 case RTL_GIGA_MAC_VER_48:
6544 rtl_hw_start_8168h_1(tp);
6545 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006546 }
6547
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006548 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006549
Francois Romieucdf1a602007-06-11 23:29:50 +02006550 RTL_W16(IntrMitigate, 0x0000);
6551
Francois Romieucdf1a602007-06-11 23:29:50 +02006552 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006553
Francois Romieucdf1a602007-06-11 23:29:50 +02006554 rtl_set_rx_mode(dev);
6555
hayeswang1a964642013-04-01 22:23:41 +00006556 RTL_R8(IntrMask);
6557
Francois Romieucdf1a602007-06-11 23:29:50 +02006558 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559}
6560
6561static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6562{
Francois Romieud58d46b2011-05-03 16:38:29 +02006563 struct rtl8169_private *tp = netdev_priv(dev);
6564
6565 if (new_mtu < ETH_ZLEN ||
6566 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 return -EINVAL;
6568
Francois Romieud58d46b2011-05-03 16:38:29 +02006569 if (new_mtu > ETH_DATA_LEN)
6570 rtl_hw_jumbo_enable(tp);
6571 else
6572 rtl_hw_jumbo_disable(tp);
6573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006575 netdev_update_features(dev);
6576
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578}
6579
6580static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6581{
Al Viro95e09182007-12-22 18:55:39 +00006582 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6584}
6585
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006586static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6587 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006589 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006590 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006591
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006592 kfree(*data_buff);
6593 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 rtl8169_make_unusable_by_asic(desc);
6595}
6596
6597static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6598{
6599 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6600
6601 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6602}
6603
6604static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6605 u32 rx_buf_sz)
6606{
6607 desc->addr = cpu_to_le64(mapping);
6608 wmb();
6609 rtl8169_mark_to_asic(desc, rx_buf_sz);
6610}
6611
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006612static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006614 return (void *)ALIGN((long)data, 16);
6615}
6616
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006617static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6618 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006619{
6620 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006622 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006623 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006624 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006626 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6627 if (!data)
6628 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006629
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006630 if (rtl8169_align(data) != data) {
6631 kfree(data);
6632 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6633 if (!data)
6634 return NULL;
6635 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006636
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006637 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006638 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006639 if (unlikely(dma_mapping_error(d, mapping))) {
6640 if (net_ratelimit())
6641 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006642 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
6645 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006646 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006647
6648err_out:
6649 kfree(data);
6650 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651}
6652
6653static void rtl8169_rx_clear(struct rtl8169_private *tp)
6654{
Francois Romieu07d3f512007-02-21 22:40:46 +01006655 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006658 if (tp->Rx_databuff[i]) {
6659 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 tp->RxDescArray + i);
6661 }
6662 }
6663}
6664
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006665static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006667 desc->opts1 |= cpu_to_le32(RingEnd);
6668}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006669
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006670static int rtl8169_rx_fill(struct rtl8169_private *tp)
6671{
6672 unsigned int i;
6673
6674 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006675 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006676
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006677 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006679
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006680 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006681 if (!data) {
6682 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006683 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006684 }
6685 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006688 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6689 return 0;
6690
6691err_out:
6692 rtl8169_rx_clear(tp);
6693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694}
6695
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696static int rtl8169_init_ring(struct net_device *dev)
6697{
6698 struct rtl8169_private *tp = netdev_priv(dev);
6699
6700 rtl8169_init_ring_indexes(tp);
6701
6702 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006703 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006705 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706}
6707
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006708static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 struct TxDesc *desc)
6710{
6711 unsigned int len = tx_skb->len;
6712
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006713 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6714
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 desc->opts1 = 0x00;
6716 desc->opts2 = 0x00;
6717 desc->addr = 0x00;
6718 tx_skb->len = 0;
6719}
6720
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006721static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6722 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723{
6724 unsigned int i;
6725
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006726 for (i = 0; i < n; i++) {
6727 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 struct ring_info *tx_skb = tp->tx_skb + entry;
6729 unsigned int len = tx_skb->len;
6730
6731 if (len) {
6732 struct sk_buff *skb = tx_skb->skb;
6733
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006734 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 tp->TxDescArray + entry);
6736 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00006737 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006738 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 tx_skb->skb = NULL;
6740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 }
6742 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006743}
6744
6745static void rtl8169_tx_clear(struct rtl8169_private *tp)
6746{
6747 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 tp->cur_tx = tp->dirty_tx = 0;
6749}
6750
Francois Romieu4422bcd2012-01-26 11:23:32 +01006751static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752{
David Howellsc4028952006-11-22 14:57:56 +00006753 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006754 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
Francois Romieuda78dbf2012-01-26 14:18:23 +01006756 napi_disable(&tp->napi);
6757 netif_stop_queue(dev);
6758 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
françois romieuc7c2c392011-12-04 20:30:52 +00006760 rtl8169_hw_reset(tp);
6761
Francois Romieu56de4142011-03-15 17:29:31 +01006762 for (i = 0; i < NUM_RX_DESC; i++)
6763 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6764
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006766 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Francois Romieuda78dbf2012-01-26 14:18:23 +01006768 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006769 rtl_hw_start(dev);
6770 netif_wake_queue(dev);
6771 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772}
6773
6774static void rtl8169_tx_timeout(struct net_device *dev)
6775{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006776 struct rtl8169_private *tp = netdev_priv(dev);
6777
6778 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779}
6780
6781static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006782 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783{
6784 struct skb_shared_info *info = skb_shinfo(skb);
6785 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006786 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006787 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788
6789 entry = tp->cur_tx;
6790 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006791 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 dma_addr_t mapping;
6793 u32 status, len;
6794 void *addr;
6795
6796 entry = (entry + 1) % NUM_TX_DESC;
6797
6798 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006799 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006800 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006801 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006802 if (unlikely(dma_mapping_error(d, mapping))) {
6803 if (net_ratelimit())
6804 netif_err(tp, drv, tp->dev,
6805 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006806 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808
Francois Romieucecb5fd2011-04-01 10:21:07 +02006809 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006810 status = opts[0] | len |
6811 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
6813 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006814 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 txd->addr = cpu_to_le64(mapping);
6816
6817 tp->tx_skb[entry].len = len;
6818 }
6819
6820 if (cur_frag) {
6821 tp->tx_skb[entry].skb = skb;
6822 txd->opts1 |= cpu_to_le32(LastFrag);
6823 }
6824
6825 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006826
6827err_out:
6828 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6829 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830}
6831
françois romieub423e9a2013-05-18 01:24:46 +00006832static bool rtl_skb_pad(struct sk_buff *skb)
6833{
6834 if (skb_padto(skb, ETH_ZLEN))
6835 return false;
6836 skb_put(skb, ETH_ZLEN - skb->len);
6837 return true;
6838}
6839
6840static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6841{
6842 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6843}
6844
hayeswange9746042014-07-11 16:25:58 +08006845static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6846 struct net_device *dev);
6847/* r8169_csum_workaround()
6848 * The hw limites the value the transport offset. When the offset is out of the
6849 * range, calculate the checksum by sw.
6850 */
6851static void r8169_csum_workaround(struct rtl8169_private *tp,
6852 struct sk_buff *skb)
6853{
6854 if (skb_shinfo(skb)->gso_size) {
6855 netdev_features_t features = tp->dev->features;
6856 struct sk_buff *segs, *nskb;
6857
6858 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6859 segs = skb_gso_segment(skb, features);
6860 if (IS_ERR(segs) || !segs)
6861 goto drop;
6862
6863 do {
6864 nskb = segs;
6865 segs = segs->next;
6866 nskb->next = NULL;
6867 rtl8169_start_xmit(nskb, tp->dev);
6868 } while (segs);
6869
6870 dev_kfree_skb(skb);
6871 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6872 if (skb_checksum_help(skb) < 0)
6873 goto drop;
6874
6875 rtl8169_start_xmit(skb, tp->dev);
6876 } else {
6877 struct net_device_stats *stats;
6878
6879drop:
6880 stats = &tp->dev->stats;
6881 stats->tx_dropped++;
6882 dev_kfree_skb(skb);
6883 }
6884}
6885
6886/* msdn_giant_send_check()
6887 * According to the document of microsoft, the TCP Pseudo Header excludes the
6888 * packet length for IPv6 TCP large packets.
6889 */
6890static int msdn_giant_send_check(struct sk_buff *skb)
6891{
6892 const struct ipv6hdr *ipv6h;
6893 struct tcphdr *th;
6894 int ret;
6895
6896 ret = skb_cow_head(skb, 0);
6897 if (ret)
6898 return ret;
6899
6900 ipv6h = ipv6_hdr(skb);
6901 th = tcp_hdr(skb);
6902
6903 th->check = 0;
6904 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6905
6906 return ret;
6907}
6908
6909static inline __be16 get_protocol(struct sk_buff *skb)
6910{
6911 __be16 protocol;
6912
6913 if (skb->protocol == htons(ETH_P_8021Q))
6914 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6915 else
6916 protocol = skb->protocol;
6917
6918 return protocol;
6919}
6920
hayeswang5888d3f2014-07-11 16:25:56 +08006921static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6922 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923{
Michał Mirosław350fb322011-04-08 06:35:56 +00006924 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
Francois Romieu2b7b4312011-04-18 22:53:24 -07006926 if (mss) {
6927 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006928 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6929 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6930 const struct iphdr *ip = ip_hdr(skb);
6931
6932 if (ip->protocol == IPPROTO_TCP)
6933 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6934 else if (ip->protocol == IPPROTO_UDP)
6935 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6936 else
6937 WARN_ON_ONCE(1);
6938 }
6939
6940 return true;
6941}
6942
6943static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6944 struct sk_buff *skb, u32 *opts)
6945{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006946 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006947 u32 mss = skb_shinfo(skb)->gso_size;
6948
6949 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006950 if (transport_offset > GTTCPHO_MAX) {
6951 netif_warn(tp, tx_err, tp->dev,
6952 "Invalid transport offset 0x%x for TSO\n",
6953 transport_offset);
6954 return false;
6955 }
6956
6957 switch (get_protocol(skb)) {
6958 case htons(ETH_P_IP):
6959 opts[0] |= TD1_GTSENV4;
6960 break;
6961
6962 case htons(ETH_P_IPV6):
6963 if (msdn_giant_send_check(skb))
6964 return false;
6965
6966 opts[0] |= TD1_GTSENV6;
6967 break;
6968
6969 default:
6970 WARN_ON_ONCE(1);
6971 break;
6972 }
6973
hayeswangbdfa4ed2014-07-11 16:25:57 +08006974 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006975 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006976 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006977 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
françois romieub423e9a2013-05-18 01:24:46 +00006979 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6980 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
6981
hayeswange9746042014-07-11 16:25:58 +08006982 if (transport_offset > TCPHO_MAX) {
6983 netif_warn(tp, tx_err, tp->dev,
6984 "Invalid transport offset 0x%x\n",
6985 transport_offset);
6986 return false;
6987 }
6988
6989 switch (get_protocol(skb)) {
6990 case htons(ETH_P_IP):
6991 opts[1] |= TD1_IPv4_CS;
6992 ip_protocol = ip_hdr(skb)->protocol;
6993 break;
6994
6995 case htons(ETH_P_IPV6):
6996 opts[1] |= TD1_IPv6_CS;
6997 ip_protocol = ipv6_hdr(skb)->nexthdr;
6998 break;
6999
7000 default:
7001 ip_protocol = IPPROTO_RAW;
7002 break;
7003 }
7004
7005 if (ip_protocol == IPPROTO_TCP)
7006 opts[1] |= TD1_TCP_CS;
7007 else if (ip_protocol == IPPROTO_UDP)
7008 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007009 else
7010 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08007011
7012 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00007013 } else {
7014 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
7015 return rtl_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 }
hayeswang5888d3f2014-07-11 16:25:56 +08007017
françois romieub423e9a2013-05-18 01:24:46 +00007018 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019}
7020
Stephen Hemminger613573252009-08-31 19:50:58 +00007021static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7022 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023{
7024 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007025 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 struct TxDesc *txd = tp->TxDescArray + entry;
7027 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007028 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 dma_addr_t mapping;
7030 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07007031 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007032 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02007033
Julien Ducourthial477206a2012-05-09 00:00:06 +02007034 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007035 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007036 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 }
7038
7039 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007040 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
françois romieub423e9a2013-05-18 01:24:46 +00007042 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7043 opts[0] = DescOwn;
7044
hayeswange9746042014-07-11 16:25:58 +08007045 if (!tp->tso_csum(tp, skb, opts)) {
7046 r8169_csum_workaround(tp, skb);
7047 return NETDEV_TX_OK;
7048 }
françois romieub423e9a2013-05-18 01:24:46 +00007049
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007050 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007051 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007052 if (unlikely(dma_mapping_error(d, mapping))) {
7053 if (net_ratelimit())
7054 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007055 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00007056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
7058 tp->tx_skb[entry].len = len;
7059 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
Francois Romieu2b7b4312011-04-18 22:53:24 -07007061 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007062 if (frags < 0)
7063 goto err_dma_1;
7064 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07007065 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007066 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07007067 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007068 tp->tx_skb[entry].skb = skb;
7069 }
7070
Francois Romieu2b7b4312011-04-18 22:53:24 -07007071 txd->opts2 = cpu_to_le32(opts[1]);
7072
Florian Westphal1e918872014-10-01 13:38:03 +02007073 netdev_sent_queue(dev, skb->len);
7074
Richard Cochran5047fb52012-03-10 07:29:42 +00007075 skb_tx_timestamp(skb);
7076
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 wmb();
7078
Francois Romieucecb5fd2011-04-01 10:21:07 +02007079 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07007080 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 txd->opts1 = cpu_to_le32(status);
7082
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 tp->cur_tx += frags + 1;
7084
David Dillow4c020a92010-03-03 16:33:10 +00007085 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086
Francois Romieucecb5fd2011-04-01 10:21:07 +02007087 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Francois Romieuda78dbf2012-01-26 14:18:23 +01007089 mmiowb();
7090
Julien Ducourthial477206a2012-05-09 00:00:06 +02007091 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01007092 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7093 * not miss a ring update when it notices a stopped queue.
7094 */
7095 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01007097 /* Sync with rtl_tx:
7098 * - publish queue status and cur_tx ring index (write barrier)
7099 * - refresh dirty_tx ring index (read barrier).
7100 * May the current thread have a pessimistic view of the ring
7101 * status and forget to wake up queue, a racing rtl_tx thread
7102 * can't.
7103 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007104 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02007105 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 netif_wake_queue(dev);
7107 }
7108
Stephen Hemminger613573252009-08-31 19:50:58 +00007109 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007111err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007112 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007113err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007114 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00007115 dev->stats.tx_dropped++;
7116 return NETDEV_TX_OK;
7117
7118err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007120 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00007121 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122}
7123
7124static void rtl8169_pcierr_interrupt(struct net_device *dev)
7125{
7126 struct rtl8169_private *tp = netdev_priv(dev);
7127 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 u16 pci_status, pci_cmd;
7129
7130 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7131 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7132
Joe Perchesbf82c182010-02-09 11:49:50 +00007133 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7134 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
7136 /*
7137 * The recovery sequence below admits a very elaborated explanation:
7138 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01007139 * - I did not see what else could be done;
7140 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 *
7142 * Feel free to adjust to your needs.
7143 */
Francois Romieua27993f2006-12-18 00:04:19 +01007144 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01007145 pci_cmd &= ~PCI_COMMAND_PARITY;
7146 else
7147 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7148
7149 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
7151 pci_write_config_word(pdev, PCI_STATUS,
7152 pci_status & (PCI_STATUS_DETECTED_PARITY |
7153 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7154 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7155
7156 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00007157 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00007158 void __iomem *ioaddr = tp->mmio_addr;
7159
Joe Perchesbf82c182010-02-09 11:49:50 +00007160 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 tp->cp_cmd &= ~PCIDAC;
7162 RTL_W16(CPlusCmd, tp->cp_cmd);
7163 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 }
7165
françois romieue6de30d2011-01-03 15:08:37 +00007166 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01007167
Francois Romieu98ddf982012-01-31 10:47:34 +01007168 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169}
7170
Francois Romieuda78dbf2012-01-26 14:18:23 +01007171static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172{
7173 unsigned int dirty_tx, tx_left;
Florian Westphal1e918872014-10-01 13:38:03 +02007174 unsigned int bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 dirty_tx = tp->dirty_tx;
7177 smp_rmb();
7178 tx_left = tp->cur_tx - dirty_tx;
7179
7180 while (tx_left > 0) {
7181 unsigned int entry = dirty_tx % NUM_TX_DESC;
7182 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 u32 status;
7184
7185 rmb();
7186 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7187 if (status & DescOwn)
7188 break;
7189
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007190 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7191 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 if (status & LastFrag) {
Florian Westphal1e918872014-10-01 13:38:03 +02007193 pkts_compl++;
7194 bytes_compl += tx_skb->skb->len;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07007195 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 tx_skb->skb = NULL;
7197 }
7198 dirty_tx++;
7199 tx_left--;
7200 }
7201
7202 if (tp->dirty_tx != dirty_tx) {
Florian Westphal1e918872014-10-01 13:38:03 +02007203 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
7204
7205 u64_stats_update_begin(&tp->tx_stats.syncp);
7206 tp->tx_stats.packets += pkts_compl;
7207 tp->tx_stats.bytes += bytes_compl;
7208 u64_stats_update_end(&tp->tx_stats.syncp);
7209
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01007211 /* Sync with rtl8169_start_xmit:
7212 * - publish dirty_tx ring index (write barrier)
7213 * - refresh cur_tx ring index and queue status (read barrier)
7214 * May the current thread miss the stopped queue condition,
7215 * a racing xmit thread can only have a right view of the
7216 * ring status.
7217 */
Francois Romieu1e874e02012-01-27 15:05:38 +01007218 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02007220 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 netif_wake_queue(dev);
7222 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02007223 /*
7224 * 8168 hack: TxPoll requests are lost when the Tx packets are
7225 * too close. Let's kick an extra TxPoll request when a burst
7226 * of start_xmit activity is detected (if it is not detected,
7227 * it is slow enough). -- FR
7228 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007229 if (tp->cur_tx != dirty_tx) {
7230 void __iomem *ioaddr = tp->mmio_addr;
7231
Francois Romieud78ae2d2007-08-26 20:08:19 +02007232 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 }
7235}
7236
Francois Romieu126fa4b2005-05-12 20:09:17 -04007237static inline int rtl8169_fragmented_frame(u32 status)
7238{
7239 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7240}
7241
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007242static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 u32 status = opts1 & RxProtoMask;
7245
7246 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00007247 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 skb->ip_summed = CHECKSUM_UNNECESSARY;
7249 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007250 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251}
7252
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007253static struct sk_buff *rtl8169_try_rx_copy(void *data,
7254 struct rtl8169_private *tp,
7255 int pkt_size,
7256 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02007258 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007259 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007261 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007262 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007263 prefetch(data);
7264 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
7265 if (skb)
7266 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00007267 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7268
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007269 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270}
7271
Francois Romieuda78dbf2012-01-26 14:18:23 +01007272static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273{
7274 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007275 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Timo Teräs9fba0812013-01-15 21:01:24 +00007279 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007281 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 u32 status;
7283
7284 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04007285 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 if (status & DescOwn)
7288 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02007289 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00007290 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7291 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007292 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02007294 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02007296 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007297 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01007298 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02007299 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02007300 }
Ben Greear6bbe0212012-02-10 15:04:33 +00007301 if ((status & (RxRUNT | RxCRC)) &&
7302 !(status & (RxRWT | RxFOVF)) &&
7303 (dev->features & NETIF_F_RXALL))
7304 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007306 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00007307 dma_addr_t addr;
7308 int pkt_size;
7309
7310process_pkt:
7311 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00007312 if (likely(!(dev->features & NETIF_F_RXFCS)))
7313 pkt_size = (status & 0x00003fff) - 4;
7314 else
7315 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
Francois Romieu126fa4b2005-05-12 20:09:17 -04007317 /*
7318 * The driver does not support incoming fragmented
7319 * frames. They are seen as a symptom of over-mtu
7320 * sized frames.
7321 */
7322 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02007323 dev->stats.rx_dropped++;
7324 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00007325 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04007326 }
7327
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007328 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7329 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00007330 if (!skb) {
7331 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00007332 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 }
7334
Eric Dumazetadea1ac72010-09-05 20:04:05 -07007335 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 skb_put(skb, pkt_size);
7337 skb->protocol = eth_type_trans(skb, dev);
7338
Francois Romieu7a8fc772011-03-01 17:18:33 +01007339 rtl8169_rx_vlan_tag(desc, skb);
7340
Francois Romieu56de4142011-03-15 17:29:31 +01007341 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342
Junchang Wang8027aa22012-03-04 23:30:32 +01007343 u64_stats_update_begin(&tp->rx_stats.syncp);
7344 tp->rx_stats.packets++;
7345 tp->rx_stats.bytes += pkt_size;
7346 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 }
françois romieuce11ff52013-01-24 13:30:06 +00007348release_descriptor:
7349 desc->opts2 = 0;
7350 wmb();
7351 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 }
7353
7354 count = cur_rx - tp->cur_rx;
7355 tp->cur_rx = cur_rx;
7356
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 return count;
7358}
7359
Francois Romieu07d3f512007-02-21 22:40:46 +01007360static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361{
Francois Romieu07d3f512007-02-21 22:40:46 +01007362 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007365 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366
Francois Romieu9085cdfa2012-01-26 12:59:08 +01007367 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007368 if (status && status != 0xffff) {
7369 status &= RTL_EVENT_NAPI | tp->event_slow;
7370 if (status) {
7371 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00007372
Francois Romieuda78dbf2012-01-26 14:18:23 +01007373 rtl_irq_disable(tp);
7374 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 return IRQ_RETVAL(handled);
7378}
7379
Francois Romieuda78dbf2012-01-26 14:18:23 +01007380/*
7381 * Workqueue context.
7382 */
7383static void rtl_slow_event_work(struct rtl8169_private *tp)
7384{
7385 struct net_device *dev = tp->dev;
7386 u16 status;
7387
7388 status = rtl_get_events(tp) & tp->event_slow;
7389 rtl_ack_events(tp, status);
7390
7391 if (unlikely(status & RxFIFOOver)) {
7392 switch (tp->mac_version) {
7393 /* Work around for rx fifo overflow */
7394 case RTL_GIGA_MAC_VER_11:
7395 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01007396 /* XXX - Hack alert. See rtl_task(). */
7397 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007398 default:
7399 break;
7400 }
7401 }
7402
7403 if (unlikely(status & SYSErr))
7404 rtl8169_pcierr_interrupt(dev);
7405
7406 if (status & LinkChg)
7407 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
7408
françois romieu7dbb4912012-06-09 10:53:16 +00007409 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007410}
7411
Francois Romieu4422bcd2012-01-26 11:23:32 +01007412static void rtl_task(struct work_struct *work)
7413{
Francois Romieuda78dbf2012-01-26 14:18:23 +01007414 static const struct {
7415 int bitnr;
7416 void (*action)(struct rtl8169_private *);
7417 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01007418 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007419 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7420 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7421 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7422 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01007423 struct rtl8169_private *tp =
7424 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007425 struct net_device *dev = tp->dev;
7426 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01007427
Francois Romieuda78dbf2012-01-26 14:18:23 +01007428 rtl_lock_work(tp);
7429
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007430 if (!netif_running(dev) ||
7431 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01007432 goto out_unlock;
7433
7434 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7435 bool pending;
7436
Francois Romieuda78dbf2012-01-26 14:18:23 +01007437 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007438 if (pending)
7439 rtl_work[i].action(tp);
7440 }
7441
7442out_unlock:
7443 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01007444}
7445
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007446static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007448 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7449 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007450 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7451 int work_done= 0;
7452 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
Francois Romieuda78dbf2012-01-26 14:18:23 +01007454 status = rtl_get_events(tp);
7455 rtl_ack_events(tp, status & ~tp->event_slow);
7456
7457 if (status & RTL_EVENT_NAPI_RX)
7458 work_done = rtl_rx(dev, tp, (u32) budget);
7459
7460 if (status & RTL_EVENT_NAPI_TX)
7461 rtl_tx(dev, tp);
7462
7463 if (status & tp->event_slow) {
7464 enable_mask &= ~tp->event_slow;
7465
7466 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007469 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007470 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00007471
Francois Romieuda78dbf2012-01-26 14:18:23 +01007472 rtl_irq_enable(tp, enable_mask);
7473 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 }
7475
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007476 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
Francois Romieu523a6092008-09-10 22:28:56 +02007479static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7480{
7481 struct rtl8169_private *tp = netdev_priv(dev);
7482
7483 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7484 return;
7485
7486 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7487 RTL_W32(RxMissed, 0);
7488}
7489
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490static void rtl8169_down(struct net_device *dev)
7491{
7492 struct rtl8169_private *tp = netdev_priv(dev);
7493 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
Francois Romieu4876cc12011-03-11 21:07:11 +01007495 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007497 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007498 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
Hayes Wang92fc43b2011-07-06 15:58:03 +08007500 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007501 /*
7502 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007503 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7504 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007505 */
Francois Romieu523a6092008-09-10 22:28:56 +02007506 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007509 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 rtl8169_tx_clear(tp);
7512
7513 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007514
7515 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516}
7517
7518static int rtl8169_close(struct net_device *dev)
7519{
7520 struct rtl8169_private *tp = netdev_priv(dev);
7521 struct pci_dev *pdev = tp->pci_dev;
7522
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007523 pm_runtime_get_sync(&pdev->dev);
7524
Francois Romieucecb5fd2011-04-01 10:21:07 +02007525 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007526 rtl8169_update_counters(dev);
7527
Francois Romieuda78dbf2012-01-26 14:18:23 +01007528 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007529 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007530
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007532 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533
Lekensteyn4ea72442013-07-22 09:53:30 +02007534 cancel_work_sync(&tp->wk.work);
7535
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007536 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007538 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7539 tp->RxPhyAddr);
7540 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7541 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 tp->TxDescArray = NULL;
7543 tp->RxDescArray = NULL;
7544
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007545 pm_runtime_put_sync(&pdev->dev);
7546
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 return 0;
7548}
7549
Francois Romieudc1c00c2012-03-08 10:06:18 +01007550#ifdef CONFIG_NET_POLL_CONTROLLER
7551static void rtl8169_netpoll(struct net_device *dev)
7552{
7553 struct rtl8169_private *tp = netdev_priv(dev);
7554
7555 rtl8169_interrupt(tp->pci_dev->irq, dev);
7556}
7557#endif
7558
Francois Romieudf43ac72012-03-08 09:48:40 +01007559static int rtl_open(struct net_device *dev)
7560{
7561 struct rtl8169_private *tp = netdev_priv(dev);
7562 void __iomem *ioaddr = tp->mmio_addr;
7563 struct pci_dev *pdev = tp->pci_dev;
7564 int retval = -ENOMEM;
7565
7566 pm_runtime_get_sync(&pdev->dev);
7567
7568 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007569 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007570 * dma_alloc_coherent provides more.
7571 */
7572 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7573 &tp->TxPhyAddr, GFP_KERNEL);
7574 if (!tp->TxDescArray)
7575 goto err_pm_runtime_put;
7576
7577 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7578 &tp->RxPhyAddr, GFP_KERNEL);
7579 if (!tp->RxDescArray)
7580 goto err_free_tx_0;
7581
7582 retval = rtl8169_init_ring(dev);
7583 if (retval < 0)
7584 goto err_free_rx_1;
7585
7586 INIT_WORK(&tp->wk.work, rtl_task);
7587
7588 smp_mb();
7589
7590 rtl_request_firmware(tp);
7591
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007592 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007593 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7594 dev->name, dev);
7595 if (retval < 0)
7596 goto err_release_fw_2;
7597
7598 rtl_lock_work(tp);
7599
7600 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7601
7602 napi_enable(&tp->napi);
7603
7604 rtl8169_init_phy(dev, tp);
7605
7606 __rtl8169_set_features(dev, dev->features);
7607
7608 rtl_pll_power_up(tp);
7609
7610 rtl_hw_start(dev);
7611
7612 netif_start_queue(dev);
7613
7614 rtl_unlock_work(tp);
7615
7616 tp->saved_wolopts = 0;
7617 pm_runtime_put_noidle(&pdev->dev);
7618
7619 rtl8169_check_link_status(dev, tp, ioaddr);
7620out:
7621 return retval;
7622
7623err_release_fw_2:
7624 rtl_release_firmware(tp);
7625 rtl8169_rx_clear(tp);
7626err_free_rx_1:
7627 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7628 tp->RxPhyAddr);
7629 tp->RxDescArray = NULL;
7630err_free_tx_0:
7631 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7632 tp->TxPhyAddr);
7633 tp->TxDescArray = NULL;
7634err_pm_runtime_put:
7635 pm_runtime_put_noidle(&pdev->dev);
7636 goto out;
7637}
7638
Junchang Wang8027aa22012-03-04 23:30:32 +01007639static struct rtnl_link_stats64 *
7640rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641{
7642 struct rtl8169_private *tp = netdev_priv(dev);
7643 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01007644 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645
Francois Romieuda78dbf2012-01-26 14:18:23 +01007646 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007647 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007648
Junchang Wang8027aa22012-03-04 23:30:32 +01007649 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007650 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007651 stats->rx_packets = tp->rx_stats.packets;
7652 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007653 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007654
7655
7656 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007657 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007658 stats->tx_packets = tp->tx_stats.packets;
7659 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007660 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007661
7662 stats->rx_dropped = dev->stats.rx_dropped;
7663 stats->tx_dropped = dev->stats.tx_dropped;
7664 stats->rx_length_errors = dev->stats.rx_length_errors;
7665 stats->rx_errors = dev->stats.rx_errors;
7666 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7667 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7668 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7669
7670 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671}
7672
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007673static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007674{
françois romieu065c27c2011-01-03 15:08:12 +00007675 struct rtl8169_private *tp = netdev_priv(dev);
7676
Francois Romieu5d06a992006-02-23 00:47:58 +01007677 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007678 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007679
7680 netif_device_detach(dev);
7681 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007682
7683 rtl_lock_work(tp);
7684 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007685 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007686 rtl_unlock_work(tp);
7687
7688 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007689}
Francois Romieu5d06a992006-02-23 00:47:58 +01007690
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007691#ifdef CONFIG_PM
7692
7693static int rtl8169_suspend(struct device *device)
7694{
7695 struct pci_dev *pdev = to_pci_dev(device);
7696 struct net_device *dev = pci_get_drvdata(pdev);
7697
7698 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007699
Francois Romieu5d06a992006-02-23 00:47:58 +01007700 return 0;
7701}
7702
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007703static void __rtl8169_resume(struct net_device *dev)
7704{
françois romieu065c27c2011-01-03 15:08:12 +00007705 struct rtl8169_private *tp = netdev_priv(dev);
7706
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007707 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007708
7709 rtl_pll_power_up(tp);
7710
Artem Savkovcff4c162012-04-03 10:29:11 +00007711 rtl_lock_work(tp);
7712 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007713 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007714 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007715
Francois Romieu98ddf982012-01-31 10:47:34 +01007716 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007717}
7718
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007719static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007720{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007721 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007722 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007723 struct rtl8169_private *tp = netdev_priv(dev);
7724
7725 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007726
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007727 if (netif_running(dev))
7728 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007729
Francois Romieu5d06a992006-02-23 00:47:58 +01007730 return 0;
7731}
7732
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007733static int rtl8169_runtime_suspend(struct device *device)
7734{
7735 struct pci_dev *pdev = to_pci_dev(device);
7736 struct net_device *dev = pci_get_drvdata(pdev);
7737 struct rtl8169_private *tp = netdev_priv(dev);
7738
7739 if (!tp->TxDescArray)
7740 return 0;
7741
Francois Romieuda78dbf2012-01-26 14:18:23 +01007742 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007743 tp->saved_wolopts = __rtl8169_get_wol(tp);
7744 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007745 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007746
7747 rtl8169_net_suspend(dev);
7748
7749 return 0;
7750}
7751
7752static int rtl8169_runtime_resume(struct device *device)
7753{
7754 struct pci_dev *pdev = to_pci_dev(device);
7755 struct net_device *dev = pci_get_drvdata(pdev);
7756 struct rtl8169_private *tp = netdev_priv(dev);
7757
7758 if (!tp->TxDescArray)
7759 return 0;
7760
Francois Romieuda78dbf2012-01-26 14:18:23 +01007761 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007762 __rtl8169_set_wol(tp, tp->saved_wolopts);
7763 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007764 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007765
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007766 rtl8169_init_phy(dev, tp);
7767
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007768 __rtl8169_resume(dev);
7769
7770 return 0;
7771}
7772
7773static int rtl8169_runtime_idle(struct device *device)
7774{
7775 struct pci_dev *pdev = to_pci_dev(device);
7776 struct net_device *dev = pci_get_drvdata(pdev);
7777 struct rtl8169_private *tp = netdev_priv(dev);
7778
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00007779 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007780}
7781
Alexey Dobriyan47145212009-12-14 18:00:08 -08007782static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007783 .suspend = rtl8169_suspend,
7784 .resume = rtl8169_resume,
7785 .freeze = rtl8169_suspend,
7786 .thaw = rtl8169_resume,
7787 .poweroff = rtl8169_suspend,
7788 .restore = rtl8169_resume,
7789 .runtime_suspend = rtl8169_runtime_suspend,
7790 .runtime_resume = rtl8169_runtime_resume,
7791 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007792};
7793
7794#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7795
7796#else /* !CONFIG_PM */
7797
7798#define RTL8169_PM_OPS NULL
7799
7800#endif /* !CONFIG_PM */
7801
David S. Miller1805b2f2011-10-24 18:18:09 -04007802static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7803{
7804 void __iomem *ioaddr = tp->mmio_addr;
7805
7806 /* WoL fails with 8168b when the receiver is disabled. */
7807 switch (tp->mac_version) {
7808 case RTL_GIGA_MAC_VER_11:
7809 case RTL_GIGA_MAC_VER_12:
7810 case RTL_GIGA_MAC_VER_17:
7811 pci_clear_master(tp->pci_dev);
7812
7813 RTL_W8(ChipCmd, CmdRxEnb);
7814 /* PCI commit */
7815 RTL_R8(ChipCmd);
7816 break;
7817 default:
7818 break;
7819 }
7820}
7821
Francois Romieu1765f952008-09-13 17:21:40 +02007822static void rtl_shutdown(struct pci_dev *pdev)
7823{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007824 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007825 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00007826 struct device *d = &pdev->dev;
7827
7828 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02007829
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007830 rtl8169_net_suspend(dev);
7831
Francois Romieucecb5fd2011-04-01 10:21:07 +02007832 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007833 rtl_rar_set(tp, dev->perm_addr);
7834
Hayes Wang92fc43b2011-07-06 15:58:03 +08007835 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007836
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007837 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007838 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7839 rtl_wol_suspend_quirk(tp);
7840 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007841 }
7842
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007843 pci_wake_from_d3(pdev, true);
7844 pci_set_power_state(pdev, PCI_D3hot);
7845 }
françois romieu2a15cd22012-03-06 01:14:12 +00007846
7847 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007848}
Francois Romieu5d06a992006-02-23 00:47:58 +01007849
Bill Pembertonbaf63292012-12-03 09:23:28 -05007850static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007851{
7852 struct net_device *dev = pci_get_drvdata(pdev);
7853 struct rtl8169_private *tp = netdev_priv(dev);
7854
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08007855 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7856 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08007857 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
7858 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
7859 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
7860 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08007861 r8168_check_dash(tp)) {
Francois Romieue27566e2012-03-08 09:54:01 +01007862 rtl8168_driver_stop(tp);
7863 }
7864
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007865 netif_napi_del(&tp->napi);
7866
Francois Romieue27566e2012-03-08 09:54:01 +01007867 unregister_netdev(dev);
7868
7869 rtl_release_firmware(tp);
7870
7871 if (pci_dev_run_wake(pdev))
7872 pm_runtime_get_noresume(&pdev->dev);
7873
7874 /* restore original MAC address */
7875 rtl_rar_set(tp, dev->perm_addr);
7876
7877 rtl_disable_msi(pdev, tp);
7878 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007879}
7880
Francois Romieufa9c3852012-03-08 10:01:50 +01007881static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007882 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007883 .ndo_stop = rtl8169_close,
7884 .ndo_get_stats64 = rtl8169_get_stats64,
7885 .ndo_start_xmit = rtl8169_start_xmit,
7886 .ndo_tx_timeout = rtl8169_tx_timeout,
7887 .ndo_validate_addr = eth_validate_addr,
7888 .ndo_change_mtu = rtl8169_change_mtu,
7889 .ndo_fix_features = rtl8169_fix_features,
7890 .ndo_set_features = rtl8169_set_features,
7891 .ndo_set_mac_address = rtl_set_mac_address,
7892 .ndo_do_ioctl = rtl8169_ioctl,
7893 .ndo_set_rx_mode = rtl_set_rx_mode,
7894#ifdef CONFIG_NET_POLL_CONTROLLER
7895 .ndo_poll_controller = rtl8169_netpoll,
7896#endif
7897
7898};
7899
Francois Romieu31fa8b12012-03-08 10:09:40 +01007900static const struct rtl_cfg_info {
7901 void (*hw_start)(struct net_device *);
7902 unsigned int region;
7903 unsigned int align;
7904 u16 event_slow;
7905 unsigned features;
7906 u8 default_ver;
7907} rtl_cfg_infos [] = {
7908 [RTL_CFG_0] = {
7909 .hw_start = rtl_hw_start_8169,
7910 .region = 1,
7911 .align = 0,
7912 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7913 .features = RTL_FEATURE_GMII,
7914 .default_ver = RTL_GIGA_MAC_VER_01,
7915 },
7916 [RTL_CFG_1] = {
7917 .hw_start = rtl_hw_start_8168,
7918 .region = 2,
7919 .align = 8,
7920 .event_slow = SYSErr | LinkChg | RxOverflow,
7921 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
7922 .default_ver = RTL_GIGA_MAC_VER_11,
7923 },
7924 [RTL_CFG_2] = {
7925 .hw_start = rtl_hw_start_8101,
7926 .region = 2,
7927 .align = 8,
7928 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7929 PCSTimeout,
7930 .features = RTL_FEATURE_MSI,
7931 .default_ver = RTL_GIGA_MAC_VER_13,
7932 }
7933};
7934
7935/* Cfg9346_Unlock assumed. */
7936static unsigned rtl_try_msi(struct rtl8169_private *tp,
7937 const struct rtl_cfg_info *cfg)
7938{
7939 void __iomem *ioaddr = tp->mmio_addr;
7940 unsigned msi = 0;
7941 u8 cfg2;
7942
7943 cfg2 = RTL_R8(Config2) & ~MSIEnable;
7944 if (cfg->features & RTL_FEATURE_MSI) {
7945 if (pci_enable_msi(tp->pci_dev)) {
7946 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
7947 } else {
7948 cfg2 |= MSIEnable;
7949 msi = RTL_FEATURE_MSI;
7950 }
7951 }
7952 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
7953 RTL_W8(Config2, cfg2);
7954 return msi;
7955}
7956
Hayes Wangc5583862012-07-02 17:23:22 +08007957DECLARE_RTL_COND(rtl_link_list_ready_cond)
7958{
7959 void __iomem *ioaddr = tp->mmio_addr;
7960
7961 return RTL_R8(MCU) & LINK_LIST_RDY;
7962}
7963
7964DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7965{
7966 void __iomem *ioaddr = tp->mmio_addr;
7967
7968 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7969}
7970
Bill Pembertonbaf63292012-12-03 09:23:28 -05007971static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007972{
7973 void __iomem *ioaddr = tp->mmio_addr;
7974 u32 data;
7975
7976 tp->ocp_base = OCP_STD_PHY_BASE;
7977
7978 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
7979
7980 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7981 return;
7982
7983 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7984 return;
7985
7986 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7987 msleep(1);
7988 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
7989
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007990 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007991 data &= ~(1 << 14);
7992 r8168_mac_ocp_write(tp, 0xe8de, data);
7993
7994 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7995 return;
7996
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007997 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007998 data |= (1 << 15);
7999 r8168_mac_ocp_write(tp, 0xe8de, data);
8000
8001 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8002 return;
8003}
8004
Bill Pembertonbaf63292012-12-03 09:23:28 -05008005static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08008006{
8007 switch (tp->mac_version) {
8008 case RTL_GIGA_MAC_VER_40:
8009 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00008010 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00008011 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08008012 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008013 case RTL_GIGA_MAC_VER_45:
8014 case RTL_GIGA_MAC_VER_46:
8015 case RTL_GIGA_MAC_VER_47:
8016 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008017 case RTL_GIGA_MAC_VER_49:
8018 case RTL_GIGA_MAC_VER_50:
8019 case RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08008020 rtl_hw_init_8168g(tp);
8021 break;
8022
8023 default:
8024 break;
8025 }
8026}
8027
hayeswang929a0312014-09-16 11:40:47 +08008028static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01008029{
8030 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8031 const unsigned int region = cfg->region;
8032 struct rtl8169_private *tp;
8033 struct mii_if_info *mii;
8034 struct net_device *dev;
8035 void __iomem *ioaddr;
8036 int chipset, i;
8037 int rc;
8038
8039 if (netif_msg_drv(&debug)) {
8040 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8041 MODULENAME, RTL8169_VERSION);
8042 }
8043
8044 dev = alloc_etherdev(sizeof (*tp));
8045 if (!dev) {
8046 rc = -ENOMEM;
8047 goto out;
8048 }
8049
8050 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01008051 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008052 tp = netdev_priv(dev);
8053 tp->dev = dev;
8054 tp->pci_dev = pdev;
8055 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8056
8057 mii = &tp->mii;
8058 mii->dev = dev;
8059 mii->mdio_read = rtl_mdio_read;
8060 mii->mdio_write = rtl_mdio_write;
8061 mii->phy_id_mask = 0x1f;
8062 mii->reg_num_mask = 0x1f;
8063 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8064
8065 /* disable ASPM completely as that cause random device stop working
8066 * problems as well as full system hangs for some PCIe devices users */
8067 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8068 PCIE_LINK_STATE_CLKPM);
8069
8070 /* enable device (incl. PCI PM wakeup and hotplug setup) */
8071 rc = pci_enable_device(pdev);
8072 if (rc < 0) {
8073 netif_err(tp, probe, dev, "enable failure\n");
8074 goto err_out_free_dev_1;
8075 }
8076
8077 if (pci_set_mwi(pdev) < 0)
8078 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8079
8080 /* make sure PCI base addr 1 is MMIO */
8081 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8082 netif_err(tp, probe, dev,
8083 "region #%d not an MMIO resource, aborting\n",
8084 region);
8085 rc = -ENODEV;
8086 goto err_out_mwi_2;
8087 }
8088
8089 /* check for weird/broken PCI region reporting */
8090 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8091 netif_err(tp, probe, dev,
8092 "Invalid PCI region size(s), aborting\n");
8093 rc = -ENODEV;
8094 goto err_out_mwi_2;
8095 }
8096
8097 rc = pci_request_regions(pdev, MODULENAME);
8098 if (rc < 0) {
8099 netif_err(tp, probe, dev, "could not request regions\n");
8100 goto err_out_mwi_2;
8101 }
8102
hayeswang929a0312014-09-16 11:40:47 +08008103 tp->cp_cmd = 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008104
8105 if ((sizeof(dma_addr_t) > 4) &&
8106 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
8107 tp->cp_cmd |= PCIDAC;
8108 dev->features |= NETIF_F_HIGHDMA;
8109 } else {
8110 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8111 if (rc < 0) {
8112 netif_err(tp, probe, dev, "DMA configuration failed\n");
8113 goto err_out_free_res_3;
8114 }
8115 }
8116
8117 /* ioremap MMIO region */
8118 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
8119 if (!ioaddr) {
8120 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
8121 rc = -EIO;
8122 goto err_out_free_res_3;
8123 }
8124 tp->mmio_addr = ioaddr;
8125
8126 if (!pci_is_pcie(pdev))
8127 netif_info(tp, probe, dev, "not PCI Express\n");
8128
8129 /* Identify chip attached to board */
8130 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8131
8132 rtl_init_rxcfg(tp);
8133
8134 rtl_irq_disable(tp);
8135
Hayes Wangc5583862012-07-02 17:23:22 +08008136 rtl_hw_initialize(tp);
8137
Francois Romieu3b6cf252012-03-08 09:59:04 +01008138 rtl_hw_reset(tp);
8139
8140 rtl_ack_events(tp, 0xffff);
8141
8142 pci_set_master(pdev);
8143
Francois Romieu3b6cf252012-03-08 09:59:04 +01008144 rtl_init_mdio_ops(tp);
8145 rtl_init_pll_power_ops(tp);
8146 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08008147 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008148
8149 rtl8169_print_mac_version(tp);
8150
8151 chipset = tp->mac_version;
8152 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8153
8154 RTL_W8(Cfg9346, Cfg9346_Unlock);
8155 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02008156 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008157 switch (tp->mac_version) {
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008158 case RTL_GIGA_MAC_VER_34:
8159 case RTL_GIGA_MAC_VER_35:
8160 case RTL_GIGA_MAC_VER_36:
8161 case RTL_GIGA_MAC_VER_37:
8162 case RTL_GIGA_MAC_VER_38:
8163 case RTL_GIGA_MAC_VER_40:
8164 case RTL_GIGA_MAC_VER_41:
8165 case RTL_GIGA_MAC_VER_42:
8166 case RTL_GIGA_MAC_VER_43:
8167 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008168 case RTL_GIGA_MAC_VER_45:
8169 case RTL_GIGA_MAC_VER_46:
Chun-Hao Linac85bcd2014-10-01 23:17:16 +08008170 case RTL_GIGA_MAC_VER_47:
8171 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008172 case RTL_GIGA_MAC_VER_49:
8173 case RTL_GIGA_MAC_VER_50:
8174 case RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008175 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
8176 tp->features |= RTL_FEATURE_WOL;
8177 if ((RTL_R8(Config3) & LinkUp) != 0)
8178 tp->features |= RTL_FEATURE_WOL;
8179 break;
8180 default:
8181 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
8182 tp->features |= RTL_FEATURE_WOL;
8183 break;
8184 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008185 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
8186 tp->features |= RTL_FEATURE_WOL;
8187 tp->features |= rtl_try_msi(tp, cfg);
8188 RTL_W8(Cfg9346, Cfg9346_Lock);
8189
8190 if (rtl_tbi_enabled(tp)) {
8191 tp->set_speed = rtl8169_set_speed_tbi;
8192 tp->get_settings = rtl8169_gset_tbi;
8193 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8194 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8195 tp->link_ok = rtl8169_tbi_link_ok;
8196 tp->do_ioctl = rtl_tbi_ioctl;
8197 } else {
8198 tp->set_speed = rtl8169_set_speed_xmii;
8199 tp->get_settings = rtl8169_gset_xmii;
8200 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8201 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8202 tp->link_ok = rtl8169_xmii_link_ok;
8203 tp->do_ioctl = rtl_xmii_ioctl;
8204 }
8205
8206 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05008207 u64_stats_init(&tp->rx_stats.syncp);
8208 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008209
8210 /* Get MAC address */
Chun-Hao Lin89cceb22014-10-01 23:17:15 +08008211 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8212 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8213 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8214 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8215 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8216 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8217 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8218 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8219 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8220 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008221 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8222 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008223 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8224 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8225 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8226 tp->mac_version == RTL_GIGA_MAC_VER_51) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008227 u16 mac_addr[3];
8228
Chun-Hao Lin05b96872014-10-01 23:17:12 +08008229 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8230 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08008231
8232 if (is_valid_ether_addr((u8 *)mac_addr))
8233 rtl_rar_set(tp, (u8 *)mac_addr);
8234 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01008235 for (i = 0; i < ETH_ALEN; i++)
8236 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008237
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00008238 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008239 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008240
8241 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8242
8243 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8244 * properly for all devices */
8245 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00008246 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008247
8248 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00008249 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8250 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008251 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8252 NETIF_F_HIGHDMA;
8253
hayeswang929a0312014-09-16 11:40:47 +08008254 tp->cp_cmd |= RxChkSum | RxVlan;
8255
8256 /*
8257 * Pretend we are using VLANs; This bypasses a nasty bug where
8258 * Interrupts stop flowing on high load on 8110SCd controllers.
8259 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01008260 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08008261 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00008262 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01008263
hayeswang5888d3f2014-07-11 16:25:56 +08008264 if (tp->txd_version == RTL_TD_0)
8265 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08008266 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08008267 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08008268 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8269 } else
hayeswang5888d3f2014-07-11 16:25:56 +08008270 WARN_ON_ONCE(1);
8271
Francois Romieu3b6cf252012-03-08 09:59:04 +01008272 dev->hw_features |= NETIF_F_RXALL;
8273 dev->hw_features |= NETIF_F_RXFCS;
8274
8275 tp->hw_start = cfg->hw_start;
8276 tp->event_slow = cfg->event_slow;
8277
8278 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8279 ~(RxBOVF | RxFOVF) : ~0;
8280
8281 init_timer(&tp->timer);
8282 tp->timer.data = (unsigned long) dev;
8283 tp->timer.function = rtl8169_phy_timer;
8284
8285 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8286
8287 rc = register_netdev(dev);
8288 if (rc < 0)
8289 goto err_out_msi_4;
8290
8291 pci_set_drvdata(pdev, dev);
8292
Francois Romieu92a7c4e2012-03-10 10:42:12 +01008293 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8294 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8295 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008296 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8297 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8298 "tx checksumming: %s]\n",
8299 rtl_chip_infos[chipset].jumbo_max,
8300 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8301 }
8302
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008303 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8304 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
Chun-Hao Lin935e2212014-10-07 15:10:41 +08008305 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8306 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8307 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8308 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
Chun-Hao Linee7a1be2014-10-01 23:17:21 +08008309 r8168_check_dash(tp)) {
Francois Romieu3b6cf252012-03-08 09:59:04 +01008310 rtl8168_driver_start(tp);
8311 }
8312
8313 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
8314
8315 if (pci_dev_run_wake(pdev))
8316 pm_runtime_put_noidle(&pdev->dev);
8317
8318 netif_carrier_off(dev);
8319
8320out:
8321 return rc;
8322
8323err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00008324 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01008325 rtl_disable_msi(pdev, tp);
8326 iounmap(ioaddr);
8327err_out_free_res_3:
8328 pci_release_regions(pdev);
8329err_out_mwi_2:
8330 pci_clear_mwi(pdev);
8331 pci_disable_device(pdev);
8332err_out_free_dev_1:
8333 free_netdev(dev);
8334 goto out;
8335}
8336
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337static struct pci_driver rtl8169_pci_driver = {
8338 .name = MODULENAME,
8339 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01008340 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05008341 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02008342 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00008343 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344};
8345
Devendra Naga3eeb7da2012-10-26 09:27:42 +00008346module_pci_driver(rtl8169_pci_driver);