blob: 86c55a07970caa35185e30860c9af20c21121278 [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,
Francois Romieu85bffe62011-04-27 08:22:39 +0200158 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
Francois Romieu2b7b4312011-04-18 22:53:24 -0700161enum rtl_tx_desc_version {
162 RTL_TD_0 = 0,
163 RTL_TD_1 = 1,
164};
165
Francois Romieud58d46b2011-05-03 16:38:29 +0200166#define JUMBO_1K ETH_DATA_LEN
167#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
168#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
169#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
170#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
171
172#define _R(NAME,TD,FW,SZ,B) { \
173 .name = NAME, \
174 .txd_version = TD, \
175 .fw_name = FW, \
176 .jumbo_max = SZ, \
177 .jumbo_tx_csum = B \
178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800180static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700182 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200183 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200184 u16 jumbo_max;
185 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200186} rtl_chip_infos[] = {
187 /* PCI devices. */
188 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 /* PCI-E devices. */
201 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800222 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200231 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200232 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200233 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200234 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
239 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
242 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200243 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200246 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200247 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
249 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_30] =
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_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200255 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200256 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
257 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200258 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
260 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800261 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200262 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
263 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800264 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200265 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
266 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800267 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
269 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800270 [RTL_GIGA_MAC_VER_37] =
271 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
272 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800273 [RTL_GIGA_MAC_VER_38] =
274 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
275 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800276 [RTL_GIGA_MAC_VER_39] =
277 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
278 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800279 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000280 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800281 JUMBO_9K, false),
282 [RTL_GIGA_MAC_VER_41] =
283 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000284 [RTL_GIGA_MAC_VER_42] =
285 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
286 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000287 [RTL_GIGA_MAC_VER_43] =
288 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
289 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800290 [RTL_GIGA_MAC_VER_44] =
291 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
292 JUMBO_9K, false),
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800293 [RTL_GIGA_MAC_VER_45] =
294 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
295 JUMBO_9K, false),
296 [RTL_GIGA_MAC_VER_46] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_47] =
300 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
301 JUMBO_1K, false),
302 [RTL_GIGA_MAC_VER_48] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
304 JUMBO_1K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306#undef _R
307
Francois Romieubcf0bf92006-07-26 23:14:13 +0200308enum cfg_version {
309 RTL_CFG_0 = 0x00,
310 RTL_CFG_1,
311 RTL_CFG_2
312};
313
Benoit Taine9baa3c32014-08-08 15:56:03 +0200314static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200315 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200316 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200317 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100318 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200319 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200320 { PCI_VENDOR_ID_DLINK, 0x4300,
321 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200322 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000323 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200324 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200325 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
326 { PCI_VENDOR_ID_LINKSYS, 0x1032,
327 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100328 { 0x0001, 0x8168,
329 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 {0,},
331};
332
333MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
334
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000335static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700336static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200337static struct {
338 u32 msg_enable;
339} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Francois Romieu07d3f512007-02-21 22:40:46 +0100341enum rtl_registers {
342 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100343 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100344 MAR0 = 8, /* Multicast filter. */
345 CounterAddrLow = 0x10,
346 CounterAddrHigh = 0x14,
347 TxDescStartAddrLow = 0x20,
348 TxDescStartAddrHigh = 0x24,
349 TxHDescStartAddrLow = 0x28,
350 TxHDescStartAddrHigh = 0x2c,
351 FLASH = 0x30,
352 ERSR = 0x36,
353 ChipCmd = 0x37,
354 TxPoll = 0x38,
355 IntrMask = 0x3c,
356 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700357
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800358 TxConfig = 0x40,
359#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
360#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
361
362 RxConfig = 0x44,
363#define RX128_INT_EN (1 << 15) /* 8111c and later */
364#define RX_MULTI_EN (1 << 14) /* 8111c only */
365#define RXCFG_FIFO_SHIFT 13
366 /* No threshold before first PCI xfer */
367#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000368#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800369#define RXCFG_DMA_SHIFT 8
370 /* Unlimited maximum PCI burst. */
371#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700372
Francois Romieu07d3f512007-02-21 22:40:46 +0100373 RxMissed = 0x4c,
374 Cfg9346 = 0x50,
375 Config0 = 0x51,
376 Config1 = 0x52,
377 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200378#define PME_SIGNAL (1 << 5) /* 8168c and later */
379
Francois Romieu07d3f512007-02-21 22:40:46 +0100380 Config3 = 0x54,
381 Config4 = 0x55,
382 Config5 = 0x56,
383 MultiIntr = 0x5c,
384 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100385 PHYstatus = 0x6c,
386 RxMaxSize = 0xda,
387 CPlusCmd = 0xe0,
388 IntrMitigate = 0xe2,
389 RxDescAddrLow = 0xe4,
390 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000391 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
392
393#define NoEarlyTx 0x3f /* Max value : no early transmit. */
394
395 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
396
397#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800398#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000399
Francois Romieu07d3f512007-02-21 22:40:46 +0100400 FuncEvent = 0xf0,
401 FuncEventMask = 0xf4,
402 FuncPresetState = 0xf8,
403 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
Francois Romieuf162a5d2008-06-01 22:37:49 +0200406enum rtl8110_registers {
407 TBICSR = 0x64,
408 TBI_ANAR = 0x68,
409 TBI_LPAR = 0x6a,
410};
411
412enum rtl8168_8101_registers {
413 CSIDR = 0x64,
414 CSIAR = 0x68,
415#define CSIAR_FLAG 0x80000000
416#define CSIAR_WRITE_CMD 0x80000000
417#define CSIAR_BYTE_ENABLE 0x0f
418#define CSIAR_BYTE_ENABLE_SHIFT 12
419#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800420#define CSIAR_FUNC_CARD 0x00000000
421#define CSIAR_FUNC_SDIO 0x00010000
422#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800423#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000424 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200425 EPHYAR = 0x80,
426#define EPHYAR_FLAG 0x80000000
427#define EPHYAR_WRITE_CMD 0x80000000
428#define EPHYAR_REG_MASK 0x1f
429#define EPHYAR_REG_SHIFT 16
430#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800431 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800432#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800433#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200434 DBG_REG = 0xd1,
435#define FIX_NAK_1 (1 << 4)
436#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800437 TWSI = 0xd2,
438 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800439#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800440#define TX_EMPTY (1 << 5)
441#define RX_EMPTY (1 << 4)
442#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800443#define EN_NDP (1 << 3)
444#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800445#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000446 EFUSEAR = 0xdc,
447#define EFUSEAR_FLAG 0x80000000
448#define EFUSEAR_WRITE_CMD 0x80000000
449#define EFUSEAR_READ_CMD 0x00000000
450#define EFUSEAR_REG_MASK 0x03ff
451#define EFUSEAR_REG_SHIFT 8
452#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800453 MISC_1 = 0xf2,
454#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200455};
456
françois romieuc0e45c12011-01-03 15:08:04 +0000457enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800458 LED_FREQ = 0x1a,
459 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000460 ERIDR = 0x70,
461 ERIAR = 0x74,
462#define ERIAR_FLAG 0x80000000
463#define ERIAR_WRITE_CMD 0x80000000
464#define ERIAR_READ_CMD 0x00000000
465#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000466#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800467#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
468#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
469#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
470#define ERIAR_MASK_SHIFT 12
471#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
472#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800473#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800474#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800475#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000476 EPHY_RXER_NUM = 0x7c,
477 OCPDR = 0xb0, /* OCP GPHY access */
478#define OCPDR_WRITE_CMD 0x80000000
479#define OCPDR_READ_CMD 0x00000000
480#define OCPDR_REG_MASK 0x7f
481#define OCPDR_GPHY_REG_SHIFT 16
482#define OCPDR_DATA_MASK 0xffff
483 OCPAR = 0xb4,
484#define OCPAR_FLAG 0x80000000
485#define OCPAR_GPHY_WRITE_CMD 0x8000f060
486#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800487 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000488 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
489 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200490#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800491#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800492#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800493#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800494#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000495};
496
Francois Romieu07d3f512007-02-21 22:40:46 +0100497enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100499 SYSErr = 0x8000,
500 PCSTimeout = 0x4000,
501 SWInt = 0x0100,
502 TxDescUnavail = 0x0080,
503 RxFIFOOver = 0x0040,
504 LinkChg = 0x0020,
505 RxOverflow = 0x0010,
506 TxErr = 0x0008,
507 TxOK = 0x0004,
508 RxErr = 0x0002,
509 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400512 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200513 RxFOVF = (1 << 23),
514 RxRWT = (1 << 22),
515 RxRES = (1 << 21),
516 RxRUNT = (1 << 20),
517 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800520 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100521 CmdReset = 0x10,
522 CmdRxEnb = 0x08,
523 CmdTxEnb = 0x04,
524 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Francois Romieu275391a2007-02-23 23:50:28 +0100526 /* TXPoll register p.5 */
527 HPQ = 0x80, /* Poll cmd on the high prio queue */
528 NPQ = 0x40, /* Poll cmd on the low prio queue */
529 FSWInt = 0x01, /* Forced software interrupt */
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100532 Cfg9346_Lock = 0x00,
533 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100536 AcceptErr = 0x20,
537 AcceptRunt = 0x10,
538 AcceptBroadcast = 0x08,
539 AcceptMulticast = 0x04,
540 AcceptMyPhys = 0x02,
541 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200542#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* TxConfigBits */
545 TxInterFrameGapShift = 24,
546 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
547
Francois Romieu5d06a992006-02-23 00:47:58 +0100548 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200549 LEDS1 = (1 << 7),
550 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200551 Speed_down = (1 << 4),
552 MEMMAP = (1 << 3),
553 IOMAP = (1 << 2),
554 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100555 PMEnable = (1 << 0), /* Power Management Enable */
556
Francois Romieu6dccd162007-02-13 23:38:05 +0100557 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000558 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000559 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100560 PCI_Clock_66MHz = 0x01,
561 PCI_Clock_33MHz = 0x00,
562
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100563 /* Config3 register p.25 */
564 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
565 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200566 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800567 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200568 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100569
Francois Romieud58d46b2011-05-03 16:38:29 +0200570 /* Config4 register */
571 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
572
Francois Romieu5d06a992006-02-23 00:47:58 +0100573 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100574 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
575 MWF = (1 << 5), /* Accept Multicast wakeup frame */
576 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200577 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100578 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100579 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000580 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* TBICSR p.28 */
583 TBIReset = 0x80000000,
584 TBILoopback = 0x40000000,
585 TBINwEnable = 0x20000000,
586 TBINwRestart = 0x10000000,
587 TBILinkOk = 0x02000000,
588 TBINwComplete = 0x01000000,
589
590 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200591 EnableBist = (1 << 15), // 8168 8101
592 Mac_dbgo_oe = (1 << 14), // 8168 8101
593 Normal_mode = (1 << 13), // unused
594 Force_half_dup = (1 << 12), // 8168 8101
595 Force_rxflow_en = (1 << 11), // 8168 8101
596 Force_txflow_en = (1 << 10), // 8168 8101
597 Cxpl_dbg_sel = (1 << 9), // 8168 8101
598 ASF = (1 << 8), // 8168 8101
599 PktCntrDisable = (1 << 7), // 8168 8101
600 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 RxVlan = (1 << 6),
602 RxChkSum = (1 << 5),
603 PCIDAC = (1 << 4),
604 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100605 INTT_0 = 0x0000, // 8168
606 INTT_1 = 0x0001, // 8168
607 INTT_2 = 0x0002, // 8168
608 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100611 TBI_Enable = 0x80,
612 TxFlowCtrl = 0x40,
613 RxFlowCtrl = 0x20,
614 _1000bpsF = 0x10,
615 _100bps = 0x08,
616 _10bps = 0x04,
617 LinkStatus = 0x02,
618 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100621 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200622
623 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100624 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800625
626 /* magic enable v2 */
627 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628};
629
Francois Romieu2b7b4312011-04-18 22:53:24 -0700630enum rtl_desc_bit {
631 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
633 RingEnd = (1 << 30), /* End of descriptor ring */
634 FirstFrag = (1 << 29), /* First segment of a packet */
635 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700636};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Francois Romieu2b7b4312011-04-18 22:53:24 -0700638/* Generic case. */
639enum rtl_tx_desc_bit {
640 /* First doubleword. */
641 TD_LSO = (1 << 27), /* Large Send Offload */
642#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Francois Romieu2b7b4312011-04-18 22:53:24 -0700644 /* Second doubleword. */
645 TxVlanTag = (1 << 17), /* Add VLAN tag */
646};
647
648/* 8169, 8168b and 810x except 8102e. */
649enum rtl_tx_desc_bit_0 {
650 /* First doubleword. */
651#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
652 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
653 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
654 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
655};
656
657/* 8102e, 8168c and beyond. */
658enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800659 /* First doubleword. */
660 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800661 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800662#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800663#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800664
Francois Romieu2b7b4312011-04-18 22:53:24 -0700665 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800666#define TCPHO_SHIFT 18
667#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700668#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800669 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
670 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700671 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
672 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
673};
674
Francois Romieu2b7b4312011-04-18 22:53:24 -0700675enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* Rx private */
677 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
678 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
679
680#define RxProtoUDP (PID1)
681#define RxProtoTCP (PID0)
682#define RxProtoIP (PID1 | PID0)
683#define RxProtoMask RxProtoIP
684
685 IPFail = (1 << 16), /* IP checksum failed */
686 UDPFail = (1 << 15), /* UDP/IP checksum failed */
687 TCPFail = (1 << 14), /* TCP/IP checksum failed */
688 RxVlanTag = (1 << 16), /* VLAN tag available */
689};
690
691#define RsvdMask 0x3fffc000
692
693struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200694 __le32 opts1;
695 __le32 opts2;
696 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
699struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200700 __le32 opts1;
701 __le32 opts2;
702 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
705struct ring_info {
706 struct sk_buff *skb;
707 u32 len;
708 u8 __pad[sizeof(void *) - sizeof(u32)];
709};
710
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200711enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200712 RTL_FEATURE_WOL = (1 << 0),
713 RTL_FEATURE_MSI = (1 << 1),
714 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200715};
716
Ivan Vecera355423d2009-02-06 21:49:57 -0800717struct rtl8169_counters {
718 __le64 tx_packets;
719 __le64 rx_packets;
720 __le64 tx_errors;
721 __le32 rx_errors;
722 __le16 rx_missed;
723 __le16 align_errors;
724 __le32 tx_one_collision;
725 __le32 tx_multi_collision;
726 __le64 rx_unicast;
727 __le64 rx_broadcast;
728 __le32 rx_multicast;
729 __le16 tx_aborted;
730 __le16 tx_underun;
731};
732
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100734 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100735 RTL_FLAG_TASK_SLOW_PENDING,
736 RTL_FLAG_TASK_RESET_PENDING,
737 RTL_FLAG_TASK_PHY_PENDING,
738 RTL_FLAG_MAX
739};
740
Junchang Wang8027aa22012-03-04 23:30:32 +0100741struct rtl8169_stats {
742 u64 packets;
743 u64 bytes;
744 struct u64_stats_sync syncp;
745};
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747struct rtl8169_private {
748 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200749 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000750 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700751 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200752 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700753 u16 txd_version;
754 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
756 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100758 struct rtl8169_stats rx_stats;
759 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
761 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
762 dma_addr_t TxPhyAddr;
763 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000764 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct timer_list timer;
767 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100768
769 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000770
771 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200772 void (*write)(struct rtl8169_private *, int, int);
773 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000774 } mdio_ops;
775
françois romieu065c27c2011-01-03 15:08:12 +0000776 struct pll_power_ops {
777 void (*down)(struct rtl8169_private *);
778 void (*up)(struct rtl8169_private *);
779 } pll_power_ops;
780
Francois Romieud58d46b2011-05-03 16:38:29 +0200781 struct jumbo_ops {
782 void (*enable)(struct rtl8169_private *);
783 void (*disable)(struct rtl8169_private *);
784 } jumbo_ops;
785
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800786 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200787 void (*write)(struct rtl8169_private *, int, int);
788 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800789 } csi_ops;
790
Oliver Neukum54405cd2011-01-06 21:55:13 +0100791 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200792 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000793 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100794 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000795 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800797 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800798 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100799
800 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100801 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
802 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100803 struct work_struct work;
804 } wk;
805
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200806 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200807
808 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800809 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000810 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400811 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000812
Francois Romieub6ffd972011-06-17 17:00:05 +0200813 struct rtl_fw {
814 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200815
816#define RTL_VER_SIZE 32
817
818 char version[RTL_VER_SIZE];
819
820 struct rtl_fw_phy_action {
821 __le32 *code;
822 size_t size;
823 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200824 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300825#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800826
827 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828};
829
Ralf Baechle979b6c12005-06-13 14:30:40 -0700830MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700833MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200834module_param_named(debug, debug.msg_enable, int, 0);
835MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836MODULE_LICENSE("GPL");
837MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000838MODULE_FIRMWARE(FIRMWARE_8168D_1);
839MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000840MODULE_FIRMWARE(FIRMWARE_8168E_1);
841MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400842MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800843MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800844MODULE_FIRMWARE(FIRMWARE_8168F_1);
845MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800846MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800847MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800848MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800849MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000850MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000851MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000852MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800853MODULE_FIRMWARE(FIRMWARE_8168H_1);
854MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200855MODULE_FIRMWARE(FIRMWARE_8107E_1);
856MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Francois Romieuda78dbf2012-01-26 14:18:23 +0100858static void rtl_lock_work(struct rtl8169_private *tp)
859{
860 mutex_lock(&tp->wk.mutex);
861}
862
863static void rtl_unlock_work(struct rtl8169_private *tp)
864{
865 mutex_unlock(&tp->wk.mutex);
866}
867
Francois Romieud58d46b2011-05-03 16:38:29 +0200868static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
869{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800870 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
871 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200872}
873
Francois Romieuffc46952012-07-06 14:19:23 +0200874struct rtl_cond {
875 bool (*check)(struct rtl8169_private *);
876 const char *msg;
877};
878
879static void rtl_udelay(unsigned int d)
880{
881 udelay(d);
882}
883
884static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
885 void (*delay)(unsigned int), unsigned int d, int n,
886 bool high)
887{
888 int i;
889
890 for (i = 0; i < n; i++) {
891 delay(d);
892 if (c->check(tp) == high)
893 return true;
894 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200895 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
896 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200897 return false;
898}
899
900static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
901 const struct rtl_cond *c,
902 unsigned int d, int n)
903{
904 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
905}
906
907static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
908 const struct rtl_cond *c,
909 unsigned int d, int n)
910{
911 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
912}
913
914static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
915 const struct rtl_cond *c,
916 unsigned int d, int n)
917{
918 return rtl_loop_wait(tp, c, msleep, d, n, true);
919}
920
921static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
922 const struct rtl_cond *c,
923 unsigned int d, int n)
924{
925 return rtl_loop_wait(tp, c, msleep, d, n, false);
926}
927
928#define DECLARE_RTL_COND(name) \
929static bool name ## _check(struct rtl8169_private *); \
930 \
931static const struct rtl_cond name = { \
932 .check = name ## _check, \
933 .msg = #name \
934}; \
935 \
936static bool name ## _check(struct rtl8169_private *tp)
937
938DECLARE_RTL_COND(rtl_ocpar_cond)
939{
940 void __iomem *ioaddr = tp->mmio_addr;
941
942 return RTL_R32(OCPAR) & OCPAR_FLAG;
943}
944
françois romieub646d902011-01-03 15:08:21 +0000945static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
946{
947 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000948
949 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200950
951 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
952 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000953}
954
955static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
956{
957 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000958
959 RTL_W32(OCPDR, data);
960 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200961
962 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
963}
964
965DECLARE_RTL_COND(rtl_eriar_cond)
966{
967 void __iomem *ioaddr = tp->mmio_addr;
968
969 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000970}
971
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800972static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000973{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800974 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000975
976 RTL_W8(ERIDR, cmd);
977 RTL_W32(ERIAR, 0x800010e8);
978 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200979
980 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
981 return;
françois romieub646d902011-01-03 15:08:21 +0000982
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800983 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000984}
985
986#define OOB_CMD_RESET 0x00
987#define OOB_CMD_DRIVER_START 0x05
988#define OOB_CMD_DRIVER_STOP 0x06
989
Francois Romieucecb5fd2011-04-01 10:21:07 +0200990static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
991{
992 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
993}
994
Francois Romieuffc46952012-07-06 14:19:23 +0200995DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000996{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200997 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000998
Francois Romieucecb5fd2011-04-01 10:21:07 +0200999 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00001000
Francois Romieuffc46952012-07-06 14:19:23 +02001001 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1002}
1003
1004static void rtl8168_driver_start(struct rtl8169_private *tp)
1005{
1006 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1007
1008 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +00001009}
1010
1011static void rtl8168_driver_stop(struct rtl8169_private *tp)
1012{
françois romieub646d902011-01-03 15:08:21 +00001013 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1014
Francois Romieuffc46952012-07-06 14:19:23 +02001015 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +00001016}
1017
hayeswang4804b3b2011-03-21 01:50:29 +00001018static int r8168dp_check_dash(struct rtl8169_private *tp)
1019{
Francois Romieucecb5fd2011-04-01 10:21:07 +02001020 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00001021
Francois Romieucecb5fd2011-04-01 10:21:07 +02001022 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +00001023}
françois romieub646d902011-01-03 15:08:21 +00001024
Hayes Wangc5583862012-07-02 17:23:22 +08001025static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
1026{
1027 if (reg & 0xffff0001) {
1028 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
1029 return true;
1030 }
1031 return false;
1032}
1033
1034DECLARE_RTL_COND(rtl_ocp_gphy_cond)
1035{
1036 void __iomem *ioaddr = tp->mmio_addr;
1037
1038 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1039}
1040
1041static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1042{
1043 void __iomem *ioaddr = tp->mmio_addr;
1044
1045 if (rtl_ocp_reg_failure(tp, reg))
1046 return;
1047
1048 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1049
1050 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1051}
1052
1053static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1054{
1055 void __iomem *ioaddr = tp->mmio_addr;
1056
1057 if (rtl_ocp_reg_failure(tp, reg))
1058 return 0;
1059
1060 RTL_W32(GPHY_OCP, reg << 15);
1061
1062 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1063 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1064}
1065
Hayes Wangc5583862012-07-02 17:23:22 +08001066static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1067{
1068 void __iomem *ioaddr = tp->mmio_addr;
1069
1070 if (rtl_ocp_reg_failure(tp, reg))
1071 return;
1072
1073 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001074}
1075
1076static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1077{
1078 void __iomem *ioaddr = tp->mmio_addr;
1079
1080 if (rtl_ocp_reg_failure(tp, reg))
1081 return 0;
1082
1083 RTL_W32(OCPDR, reg << 15);
1084
Hayes Wang3a83ad12012-07-11 20:31:56 +08001085 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001086}
1087
1088#define OCP_STD_PHY_BASE 0xa400
1089
1090static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1091{
1092 if (reg == 0x1f) {
1093 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1094 return;
1095 }
1096
1097 if (tp->ocp_base != OCP_STD_PHY_BASE)
1098 reg -= 0x10;
1099
1100 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1101}
1102
1103static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1104{
1105 if (tp->ocp_base != OCP_STD_PHY_BASE)
1106 reg -= 0x10;
1107
1108 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1109}
1110
hayeswangeee37862013-04-01 22:23:38 +00001111static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1112{
1113 if (reg == 0x1f) {
1114 tp->ocp_base = value << 4;
1115 return;
1116 }
1117
1118 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1119}
1120
1121static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1122{
1123 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1124}
1125
Francois Romieuffc46952012-07-06 14:19:23 +02001126DECLARE_RTL_COND(rtl_phyar_cond)
1127{
1128 void __iomem *ioaddr = tp->mmio_addr;
1129
1130 return RTL_R32(PHYAR) & 0x80000000;
1131}
1132
Francois Romieu24192212012-07-06 20:19:42 +02001133static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Francois Romieu24192212012-07-06 20:19:42 +02001135 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Francois Romieu24192212012-07-06 20:19:42 +02001137 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Francois Romieuffc46952012-07-06 14:19:23 +02001139 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001140 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001141 * According to hardware specs a 20us delay is required after write
1142 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001143 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001144 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Francois Romieu24192212012-07-06 20:19:42 +02001147static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Francois Romieu24192212012-07-06 20:19:42 +02001149 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001150 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Francois Romieu24192212012-07-06 20:19:42 +02001152 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Francois Romieuffc46952012-07-06 14:19:23 +02001154 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1155 RTL_R32(PHYAR) & 0xffff : ~0;
1156
Timo Teräs81a95f02010-06-09 17:31:48 -07001157 /*
1158 * According to hardware specs a 20us delay is required after read
1159 * complete indication, but before sending next command.
1160 */
1161 udelay(20);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return value;
1164}
1165
Francois Romieu24192212012-07-06 20:19:42 +02001166static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001167{
Francois Romieu24192212012-07-06 20:19:42 +02001168 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001169
Francois Romieu24192212012-07-06 20:19:42 +02001170 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001171 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1172 RTL_W32(EPHY_RXER_NUM, 0);
1173
Francois Romieuffc46952012-07-06 14:19:23 +02001174 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001175}
1176
Francois Romieu24192212012-07-06 20:19:42 +02001177static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001178{
Francois Romieu24192212012-07-06 20:19:42 +02001179 r8168dp_1_mdio_access(tp, reg,
1180 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001181}
1182
Francois Romieu24192212012-07-06 20:19:42 +02001183static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001184{
Francois Romieu24192212012-07-06 20:19:42 +02001185 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001186
Francois Romieu24192212012-07-06 20:19:42 +02001187 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001188
1189 mdelay(1);
1190 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1191 RTL_W32(EPHY_RXER_NUM, 0);
1192
Francois Romieuffc46952012-07-06 14:19:23 +02001193 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1194 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001195}
1196
françois romieue6de30d2011-01-03 15:08:37 +00001197#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1198
1199static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1200{
1201 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1202}
1203
1204static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1205{
1206 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1207}
1208
Francois Romieu24192212012-07-06 20:19:42 +02001209static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001210{
Francois Romieu24192212012-07-06 20:19:42 +02001211 void __iomem *ioaddr = tp->mmio_addr;
1212
françois romieue6de30d2011-01-03 15:08:37 +00001213 r8168dp_2_mdio_start(ioaddr);
1214
Francois Romieu24192212012-07-06 20:19:42 +02001215 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001216
1217 r8168dp_2_mdio_stop(ioaddr);
1218}
1219
Francois Romieu24192212012-07-06 20:19:42 +02001220static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001221{
Francois Romieu24192212012-07-06 20:19:42 +02001222 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001223 int value;
1224
1225 r8168dp_2_mdio_start(ioaddr);
1226
Francois Romieu24192212012-07-06 20:19:42 +02001227 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001228
1229 r8168dp_2_mdio_stop(ioaddr);
1230
1231 return value;
1232}
1233
françois romieu4da19632011-01-03 15:07:55 +00001234static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001235{
Francois Romieu24192212012-07-06 20:19:42 +02001236 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001237}
1238
françois romieu4da19632011-01-03 15:07:55 +00001239static int rtl_readphy(struct rtl8169_private *tp, int location)
1240{
Francois Romieu24192212012-07-06 20:19:42 +02001241 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001242}
1243
1244static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1245{
1246 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1247}
1248
1249static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001250{
1251 int val;
1252
françois romieu4da19632011-01-03 15:07:55 +00001253 val = rtl_readphy(tp, reg_addr);
1254 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001255}
1256
Francois Romieuccdffb92008-07-26 14:26:06 +02001257static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1258 int val)
1259{
1260 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001261
françois romieu4da19632011-01-03 15:07:55 +00001262 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001263}
1264
1265static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1266{
1267 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001268
françois romieu4da19632011-01-03 15:07:55 +00001269 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001270}
1271
Francois Romieuffc46952012-07-06 14:19:23 +02001272DECLARE_RTL_COND(rtl_ephyar_cond)
1273{
1274 void __iomem *ioaddr = tp->mmio_addr;
1275
1276 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1277}
1278
Francois Romieufdf6fc02012-07-06 22:40:38 +02001279static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001280{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001281 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001282
1283 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1284 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1285
Francois Romieuffc46952012-07-06 14:19:23 +02001286 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1287
1288 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001289}
1290
Francois Romieufdf6fc02012-07-06 22:40:38 +02001291static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001292{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001293 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001294
1295 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1296
Francois Romieuffc46952012-07-06 14:19:23 +02001297 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1298 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001299}
1300
Francois Romieufdf6fc02012-07-06 22:40:38 +02001301static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1302 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001303{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001304 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001305
1306 BUG_ON((addr & 3) || (mask == 0));
1307 RTL_W32(ERIDR, val);
1308 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1309
Francois Romieuffc46952012-07-06 14:19:23 +02001310 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001311}
1312
Francois Romieufdf6fc02012-07-06 22:40:38 +02001313static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001314{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001315 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001316
1317 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1318
Francois Romieuffc46952012-07-06 14:19:23 +02001319 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1320 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001321}
1322
Francois Romieufdf6fc02012-07-06 22:40:38 +02001323static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1324 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001325{
1326 u32 val;
1327
Francois Romieufdf6fc02012-07-06 22:40:38 +02001328 val = rtl_eri_read(tp, addr, type);
1329 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001330}
1331
françois romieuc28aa382011-08-02 03:53:43 +00001332struct exgmac_reg {
1333 u16 addr;
1334 u16 mask;
1335 u32 val;
1336};
1337
Francois Romieufdf6fc02012-07-06 22:40:38 +02001338static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001339 const struct exgmac_reg *r, int len)
1340{
1341 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001342 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001343 r++;
1344 }
1345}
1346
Francois Romieuffc46952012-07-06 14:19:23 +02001347DECLARE_RTL_COND(rtl_efusear_cond)
1348{
1349 void __iomem *ioaddr = tp->mmio_addr;
1350
1351 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1352}
1353
Francois Romieufdf6fc02012-07-06 22:40:38 +02001354static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001355{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001356 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001357
1358 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1359
Francois Romieuffc46952012-07-06 14:19:23 +02001360 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1361 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001362}
1363
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001364static u16 rtl_get_events(struct rtl8169_private *tp)
1365{
1366 void __iomem *ioaddr = tp->mmio_addr;
1367
1368 return RTL_R16(IntrStatus);
1369}
1370
1371static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1372{
1373 void __iomem *ioaddr = tp->mmio_addr;
1374
1375 RTL_W16(IntrStatus, bits);
1376 mmiowb();
1377}
1378
1379static void rtl_irq_disable(struct rtl8169_private *tp)
1380{
1381 void __iomem *ioaddr = tp->mmio_addr;
1382
1383 RTL_W16(IntrMask, 0);
1384 mmiowb();
1385}
1386
Francois Romieu3e990ff2012-01-26 12:50:01 +01001387static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1388{
1389 void __iomem *ioaddr = tp->mmio_addr;
1390
1391 RTL_W16(IntrMask, bits);
1392}
1393
Francois Romieuda78dbf2012-01-26 14:18:23 +01001394#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1395#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1396#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1397
1398static void rtl_irq_enable_all(struct rtl8169_private *tp)
1399{
1400 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1401}
1402
françois romieu811fd302011-12-04 20:30:45 +00001403static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
françois romieu811fd302011-12-04 20:30:45 +00001405 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001407 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001408 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001409 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
françois romieu4da19632011-01-03 15:07:55 +00001412static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
françois romieu4da19632011-01-03 15:07:55 +00001414 void __iomem *ioaddr = tp->mmio_addr;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return RTL_R32(TBICSR) & TBIReset;
1417}
1418
françois romieu4da19632011-01-03 15:07:55 +00001419static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
françois romieu4da19632011-01-03 15:07:55 +00001421 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1425{
1426 return RTL_R32(TBICSR) & TBILinkOk;
1427}
1428
1429static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1430{
1431 return RTL_R8(PHYstatus) & LinkStatus;
1432}
1433
françois romieu4da19632011-01-03 15:07:55 +00001434static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
françois romieu4da19632011-01-03 15:07:55 +00001436 void __iomem *ioaddr = tp->mmio_addr;
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1439}
1440
françois romieu4da19632011-01-03 15:07:55 +00001441static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 unsigned int val;
1444
françois romieu4da19632011-01-03 15:07:55 +00001445 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1446 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Hayes Wang70090422011-07-06 15:58:06 +08001449static void rtl_link_chg_patch(struct rtl8169_private *tp)
1450{
1451 void __iomem *ioaddr = tp->mmio_addr;
1452 struct net_device *dev = tp->dev;
1453
1454 if (!netif_running(dev))
1455 return;
1456
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001457 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1458 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001459 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001460 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1461 ERIAR_EXGMAC);
1462 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1463 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001464 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001465 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1466 ERIAR_EXGMAC);
1467 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1468 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001469 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001470 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1471 ERIAR_EXGMAC);
1472 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1473 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001474 }
1475 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001476 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001477 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001478 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001479 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001480 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1481 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1482 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001483 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1484 ERIAR_EXGMAC);
1485 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1486 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001487 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001488 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1489 ERIAR_EXGMAC);
1490 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1491 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001492 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001493 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1494 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001495 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1496 ERIAR_EXGMAC);
1497 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1498 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001499 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001500 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1501 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001502 }
Hayes Wang70090422011-07-06 15:58:06 +08001503 }
1504}
1505
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001506static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001507 struct rtl8169_private *tp,
1508 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001511 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001512 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001513 if (pm)
1514 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001516 if (net_ratelimit())
1517 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001518 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001520 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001521 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001522 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001526static void rtl8169_check_link_status(struct net_device *dev,
1527 struct rtl8169_private *tp,
1528 void __iomem *ioaddr)
1529{
1530 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1531}
1532
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001533#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1534
1535static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1536{
1537 void __iomem *ioaddr = tp->mmio_addr;
1538 u8 options;
1539 u32 wolopts = 0;
1540
1541 options = RTL_R8(Config1);
1542 if (!(options & PMEnable))
1543 return 0;
1544
1545 options = RTL_R8(Config3);
1546 if (options & LinkUp)
1547 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001548 switch (tp->mac_version) {
1549 case RTL_GIGA_MAC_VER_45:
1550 case RTL_GIGA_MAC_VER_46:
1551 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1552 wolopts |= WAKE_MAGIC;
1553 break;
1554 default:
1555 if (options & MagicPacket)
1556 wolopts |= WAKE_MAGIC;
1557 break;
1558 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001559
1560 options = RTL_R8(Config5);
1561 if (options & UWF)
1562 wolopts |= WAKE_UCAST;
1563 if (options & BWF)
1564 wolopts |= WAKE_BCAST;
1565 if (options & MWF)
1566 wolopts |= WAKE_MCAST;
1567
1568 return wolopts;
1569}
1570
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001571static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1572{
1573 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001574
Francois Romieuda78dbf2012-01-26 14:18:23 +01001575 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001576
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001577 wol->supported = WAKE_ANY;
1578 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001579
Francois Romieuda78dbf2012-01-26 14:18:23 +01001580 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001581}
1582
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001583static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001584{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001585 void __iomem *ioaddr = tp->mmio_addr;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001586 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001587 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001588 u32 opt;
1589 u16 reg;
1590 u8 mask;
1591 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001592 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001593 { WAKE_UCAST, Config5, UWF },
1594 { WAKE_BCAST, Config5, BWF },
1595 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001596 { WAKE_ANY, Config5, LanWake },
1597 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001598 };
Francois Romieu851e6022012-04-17 11:10:11 +02001599 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001600
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001601 RTL_W8(Cfg9346, Cfg9346_Unlock);
1602
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001603 switch (tp->mac_version) {
1604 case RTL_GIGA_MAC_VER_45:
1605 case RTL_GIGA_MAC_VER_46:
1606 tmp = ARRAY_SIZE(cfg) - 1;
1607 if (wolopts & WAKE_MAGIC)
1608 rtl_w1w0_eri(tp,
1609 0x0dc,
1610 ERIAR_MASK_0100,
1611 MagicPacket_v2,
1612 0x0000,
1613 ERIAR_EXGMAC);
1614 else
1615 rtl_w1w0_eri(tp,
1616 0x0dc,
1617 ERIAR_MASK_0100,
1618 0x0000,
1619 MagicPacket_v2,
1620 ERIAR_EXGMAC);
1621 break;
1622 default:
1623 tmp = ARRAY_SIZE(cfg);
1624 break;
1625 }
1626
1627 for (i = 0; i < tmp; i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001628 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001629 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001630 options |= cfg[i].mask;
1631 RTL_W8(cfg[i].reg, options);
1632 }
1633
Francois Romieu851e6022012-04-17 11:10:11 +02001634 switch (tp->mac_version) {
1635 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1636 options = RTL_R8(Config1) & ~PMEnable;
1637 if (wolopts)
1638 options |= PMEnable;
1639 RTL_W8(Config1, options);
1640 break;
1641 default:
Francois Romieud387b422012-04-17 11:12:01 +02001642 options = RTL_R8(Config2) & ~PME_SIGNAL;
1643 if (wolopts)
1644 options |= PME_SIGNAL;
1645 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001646 break;
1647 }
1648
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001649 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001650}
1651
1652static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1653{
1654 struct rtl8169_private *tp = netdev_priv(dev);
1655
Francois Romieuda78dbf2012-01-26 14:18:23 +01001656 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001657
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001658 if (wol->wolopts)
1659 tp->features |= RTL_FEATURE_WOL;
1660 else
1661 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001662 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001663
1664 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001665
françois romieuea809072010-11-08 13:23:58 +00001666 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1667
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001668 return 0;
1669}
1670
Francois Romieu31bd2042011-04-26 18:58:59 +02001671static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1672{
Francois Romieu85bffe62011-04-27 08:22:39 +02001673 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static void rtl8169_get_drvinfo(struct net_device *dev,
1677 struct ethtool_drvinfo *info)
1678{
1679 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001680 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Rick Jones68aad782011-11-07 13:29:27 +00001682 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1683 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1684 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001685 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001686 if (!IS_ERR_OR_NULL(rtl_fw))
1687 strlcpy(info->fw_version, rtl_fw->version,
1688 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691static int rtl8169_get_regs_len(struct net_device *dev)
1692{
1693 return R8169_REGS_SIZE;
1694}
1695
1696static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001697 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct rtl8169_private *tp = netdev_priv(dev);
1700 void __iomem *ioaddr = tp->mmio_addr;
1701 int ret = 0;
1702 u32 reg;
1703
1704 reg = RTL_R32(TBICSR);
1705 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1706 (duplex == DUPLEX_FULL)) {
1707 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1708 } else if (autoneg == AUTONEG_ENABLE)
1709 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1710 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001711 netif_warn(tp, link, dev,
1712 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ret = -EOPNOTSUPP;
1714 }
1715
1716 return ret;
1717}
1718
1719static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001720 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001723 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001724 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Hayes Wang716b50a2011-02-22 17:26:18 +08001726 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001729 int auto_nego;
1730
françois romieu4da19632011-01-03 15:07:55 +00001731 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001732 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1733 ADVERTISE_100HALF | ADVERTISE_100FULL);
1734
1735 if (adv & ADVERTISED_10baseT_Half)
1736 auto_nego |= ADVERTISE_10HALF;
1737 if (adv & ADVERTISED_10baseT_Full)
1738 auto_nego |= ADVERTISE_10FULL;
1739 if (adv & ADVERTISED_100baseT_Half)
1740 auto_nego |= ADVERTISE_100HALF;
1741 if (adv & ADVERTISED_100baseT_Full)
1742 auto_nego |= ADVERTISE_100FULL;
1743
françois romieu3577aa12009-05-19 10:46:48 +00001744 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1745
françois romieu4da19632011-01-03 15:07:55 +00001746 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001747 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1748
1749 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001750 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001751 if (adv & ADVERTISED_1000baseT_Half)
1752 giga_ctrl |= ADVERTISE_1000HALF;
1753 if (adv & ADVERTISED_1000baseT_Full)
1754 giga_ctrl |= ADVERTISE_1000FULL;
1755 } else if (adv & (ADVERTISED_1000baseT_Half |
1756 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001757 netif_info(tp, link, dev,
1758 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001759 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
françois romieu3577aa12009-05-19 10:46:48 +00001762 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001763
françois romieu4da19632011-01-03 15:07:55 +00001764 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1765 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001766 } else {
1767 giga_ctrl = 0;
1768
1769 if (speed == SPEED_10)
1770 bmcr = 0;
1771 else if (speed == SPEED_100)
1772 bmcr = BMCR_SPEED100;
1773 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001774 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001775
1776 if (duplex == DUPLEX_FULL)
1777 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001778 }
1779
françois romieu4da19632011-01-03 15:07:55 +00001780 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001781
Francois Romieucecb5fd2011-04-01 10:21:07 +02001782 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1783 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001784 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001785 rtl_writephy(tp, 0x17, 0x2138);
1786 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001787 } else {
françois romieu4da19632011-01-03 15:07:55 +00001788 rtl_writephy(tp, 0x17, 0x2108);
1789 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001790 }
1791 }
1792
Oliver Neukum54405cd2011-01-06 21:55:13 +01001793 rc = 0;
1794out:
1795 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001799 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct rtl8169_private *tp = netdev_priv(dev);
1802 int ret;
1803
Oliver Neukum54405cd2011-01-06 21:55:13 +01001804 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001805 if (ret < 0)
1806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Francois Romieu4876cc12011-03-11 21:07:11 +01001808 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1809 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001811 }
1812out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return ret;
1814}
1815
1816static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1817{
1818 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int ret;
1820
Francois Romieu4876cc12011-03-11 21:07:11 +01001821 del_timer_sync(&tp->timer);
1822
Francois Romieuda78dbf2012-01-26 14:18:23 +01001823 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001824 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001825 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001826 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return ret;
1829}
1830
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001831static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1832 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Francois Romieud58d46b2011-05-03 16:38:29 +02001834 struct rtl8169_private *tp = netdev_priv(dev);
1835
Francois Romieu2b7b4312011-04-18 22:53:24 -07001836 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001837 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Francois Romieud58d46b2011-05-03 16:38:29 +02001839 if (dev->mtu > JUMBO_1K &&
1840 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1841 features &= ~NETIF_F_IP_CSUM;
1842
Michał Mirosław350fb322011-04-08 06:35:56 +00001843 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Francois Romieuda78dbf2012-01-26 14:18:23 +01001846static void __rtl8169_set_features(struct net_device *dev,
1847 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001850 void __iomem *ioaddr = tp->mmio_addr;
hayeswang929a0312014-09-16 11:40:47 +08001851 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
hayeswang929a0312014-09-16 11:40:47 +08001853 rx_config = RTL_R32(RxConfig);
1854 if (features & NETIF_F_RXALL)
1855 rx_config |= (AcceptErr | AcceptRunt);
1856 else
1857 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
hayeswang929a0312014-09-16 11:40:47 +08001859 RTL_W32(RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001860
hayeswang929a0312014-09-16 11:40:47 +08001861 if (features & NETIF_F_RXCSUM)
1862 tp->cp_cmd |= RxChkSum;
1863 else
1864 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001865
hayeswang929a0312014-09-16 11:40:47 +08001866 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1867 tp->cp_cmd |= RxVlan;
1868 else
1869 tp->cp_cmd &= ~RxVlan;
1870
1871 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
1872
1873 RTL_W16(CPlusCmd, tp->cp_cmd);
1874 RTL_R16(CPlusCmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001875}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Francois Romieuda78dbf2012-01-26 14:18:23 +01001877static int rtl8169_set_features(struct net_device *dev,
1878 netdev_features_t features)
1879{
1880 struct rtl8169_private *tp = netdev_priv(dev);
1881
hayeswang929a0312014-09-16 11:40:47 +08001882 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
1883
Francois Romieuda78dbf2012-01-26 14:18:23 +01001884 rtl_lock_work(tp);
Dan Carpenter85911d72014-09-19 13:40:25 +03001885 if (features ^ dev->features)
hayeswang929a0312014-09-16 11:40:47 +08001886 __rtl8169_set_features(dev, features);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001887 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 return 0;
1890}
1891
Francois Romieuda78dbf2012-01-26 14:18:23 +01001892
Kirill Smelkov810f4892012-11-10 21:11:02 +04001893static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Jesse Grosseab6d182010-10-20 13:56:03 +00001895 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1897}
1898
Francois Romieu7a8fc772011-03-01 17:18:33 +01001899static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Francois Romieu7a8fc772011-03-01 17:18:33 +01001903 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001904 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Francois Romieuccdffb92008-07-26 14:26:06 +02001907static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct rtl8169_private *tp = netdev_priv(dev);
1910 void __iomem *ioaddr = tp->mmio_addr;
1911 u32 status;
1912
1913 cmd->supported =
1914 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1915 cmd->port = PORT_FIBRE;
1916 cmd->transceiver = XCVR_INTERNAL;
1917
1918 status = RTL_R32(TBICSR);
1919 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1920 cmd->autoneg = !!(status & TBINwEnable);
1921
David Decotigny70739492011-04-27 18:32:40 +00001922 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001924
1925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Francois Romieuccdffb92008-07-26 14:26:06 +02001928static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Francois Romieuccdffb92008-07-26 14:26:06 +02001932 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1936{
1937 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001938 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Francois Romieuda78dbf2012-01-26 14:18:23 +01001940 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001941 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001942 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Francois Romieuccdffb92008-07-26 14:26:06 +02001944 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1948 void *p)
1949{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001950 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001951 u32 __iomem *data = tp->mmio_addr;
1952 u32 *dw = p;
1953 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Francois Romieuda78dbf2012-01-26 14:18:23 +01001955 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001956 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1957 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001958 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001961static u32 rtl8169_get_msglevel(struct net_device *dev)
1962{
1963 struct rtl8169_private *tp = netdev_priv(dev);
1964
1965 return tp->msg_enable;
1966}
1967
1968static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1969{
1970 struct rtl8169_private *tp = netdev_priv(dev);
1971
1972 tp->msg_enable = value;
1973}
1974
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001975static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1976 "tx_packets",
1977 "rx_packets",
1978 "tx_errors",
1979 "rx_errors",
1980 "rx_missed",
1981 "align_errors",
1982 "tx_single_collisions",
1983 "tx_multi_collisions",
1984 "unicast",
1985 "broadcast",
1986 "multicast",
1987 "tx_aborted",
1988 "tx_underrun",
1989};
1990
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001991static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001992{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001993 switch (sset) {
1994 case ETH_SS_STATS:
1995 return ARRAY_SIZE(rtl8169_gstrings);
1996 default:
1997 return -EOPNOTSUPP;
1998 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001999}
2000
Francois Romieuffc46952012-07-06 14:19:23 +02002001DECLARE_RTL_COND(rtl_counters_cond)
2002{
2003 void __iomem *ioaddr = tp->mmio_addr;
2004
2005 return RTL_R32(CounterAddrLow) & CounterDump;
2006}
2007
Ivan Vecera355423d2009-02-06 21:49:57 -08002008static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002009{
2010 struct rtl8169_private *tp = netdev_priv(dev);
2011 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002012 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002013 struct rtl8169_counters *counters;
2014 dma_addr_t paddr;
2015 u32 cmd;
2016
Ivan Vecera355423d2009-02-06 21:49:57 -08002017 /*
2018 * Some chips are unable to dump tally counters when the receiver
2019 * is disabled.
2020 */
2021 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
2022 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002023
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00002024 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002025 if (!counters)
2026 return;
2027
2028 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07002029 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002030 RTL_W32(CounterAddrLow, cmd);
2031 RTL_W32(CounterAddrLow, cmd | CounterDump);
2032
Francois Romieuffc46952012-07-06 14:19:23 +02002033 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
2034 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002035
2036 RTL_W32(CounterAddrLow, 0);
2037 RTL_W32(CounterAddrHigh, 0);
2038
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00002039 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002040}
2041
Ivan Vecera355423d2009-02-06 21:49:57 -08002042static void rtl8169_get_ethtool_stats(struct net_device *dev,
2043 struct ethtool_stats *stats, u64 *data)
2044{
2045 struct rtl8169_private *tp = netdev_priv(dev);
2046
2047 ASSERT_RTNL();
2048
2049 rtl8169_update_counters(dev);
2050
2051 data[0] = le64_to_cpu(tp->counters.tx_packets);
2052 data[1] = le64_to_cpu(tp->counters.rx_packets);
2053 data[2] = le64_to_cpu(tp->counters.tx_errors);
2054 data[3] = le32_to_cpu(tp->counters.rx_errors);
2055 data[4] = le16_to_cpu(tp->counters.rx_missed);
2056 data[5] = le16_to_cpu(tp->counters.align_errors);
2057 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
2058 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
2059 data[8] = le64_to_cpu(tp->counters.rx_unicast);
2060 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
2061 data[10] = le32_to_cpu(tp->counters.rx_multicast);
2062 data[11] = le16_to_cpu(tp->counters.tx_aborted);
2063 data[12] = le16_to_cpu(tp->counters.tx_underun);
2064}
2065
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002066static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2067{
2068 switch(stringset) {
2069 case ETH_SS_STATS:
2070 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2071 break;
2072 }
2073}
2074
Jeff Garzik7282d492006-09-13 14:30:00 -04002075static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 .get_drvinfo = rtl8169_get_drvinfo,
2077 .get_regs_len = rtl8169_get_regs_len,
2078 .get_link = ethtool_op_get_link,
2079 .get_settings = rtl8169_get_settings,
2080 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002081 .get_msglevel = rtl8169_get_msglevel,
2082 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002084 .get_wol = rtl8169_get_wol,
2085 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002086 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002087 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002088 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002089 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090};
2091
Francois Romieu07d3f512007-02-21 22:40:46 +01002092static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002093 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Francois Romieu5d320a22011-05-08 17:47:36 +02002095 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002096 /*
2097 * The driver currently handles the 8168Bf and the 8168Be identically
2098 * but they can be identified more specifically through the test below
2099 * if needed:
2100 *
2101 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002102 *
2103 * Same thing for the 8101Eb and the 8101Ec:
2104 *
2105 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002106 */
Francois Romieu37441002011-06-17 22:58:54 +02002107 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002109 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 int mac_version;
2111 } mac_info[] = {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002112 /* 8168H family. */
2113 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2114 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2115
Hayes Wangc5583862012-07-02 17:23:22 +08002116 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002117 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002118 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002119 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2120 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2121
Hayes Wangc2218922011-09-06 16:55:18 +08002122 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002123 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002124 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2125 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2126
hayeswang01dc7fe2011-03-21 01:50:28 +00002127 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002128 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002129 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2130 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2131 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2132
Francois Romieu5b538df2008-07-20 16:22:45 +02002133 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002134 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2135 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002136 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002137
françois romieue6de30d2011-01-03 15:08:37 +00002138 /* 8168DP family. */
2139 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2140 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002141 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002142
Francois Romieuef808d52008-06-29 13:10:54 +02002143 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002144 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002145 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002146 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002147 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002148 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2149 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002150 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002151 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002152 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002153
2154 /* 8168B family. */
2155 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2156 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2157 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2158 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2159
2160 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002161 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2162 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002163 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002164 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002165 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2166 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2167 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002168 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2169 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2170 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2171 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2172 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2173 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002174 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002175 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002176 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002177 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2178 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002179 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2180 /* FIXME: where did these entries come from ? -- FR */
2181 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2182 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2183
2184 /* 8110 family. */
2185 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2186 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2187 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2188 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2189 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2190 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2191
Jean Delvaref21b75e2009-05-26 20:54:48 -07002192 /* Catch-all */
2193 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002194 };
2195 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 u32 reg;
2197
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002198 reg = RTL_R32(TxConfig);
2199 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 p++;
2201 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002202
2203 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2204 netif_notice(tp, probe, dev,
2205 "unknown MAC, using family default\n");
2206 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002207 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2208 tp->mac_version = tp->mii.supports_gmii ?
2209 RTL_GIGA_MAC_VER_42 :
2210 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002211 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2212 tp->mac_version = tp->mii.supports_gmii ?
2213 RTL_GIGA_MAC_VER_45 :
2214 RTL_GIGA_MAC_VER_47;
2215 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2216 tp->mac_version = tp->mii.supports_gmii ?
2217 RTL_GIGA_MAC_VER_46 :
2218 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2223{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002224 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Francois Romieu867763c2007-08-17 18:21:58 +02002227struct phy_reg {
2228 u16 reg;
2229 u16 val;
2230};
2231
françois romieu4da19632011-01-03 15:07:55 +00002232static void rtl_writephy_batch(struct rtl8169_private *tp,
2233 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002234{
2235 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002236 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002237 regs++;
2238 }
2239}
2240
françois romieubca03d52011-01-03 15:07:31 +00002241#define PHY_READ 0x00000000
2242#define PHY_DATA_OR 0x10000000
2243#define PHY_DATA_AND 0x20000000
2244#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002245#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002246#define PHY_CLEAR_READCOUNT 0x70000000
2247#define PHY_WRITE 0x80000000
2248#define PHY_READCOUNT_EQ_SKIP 0x90000000
2249#define PHY_COMP_EQ_SKIPN 0xa0000000
2250#define PHY_COMP_NEQ_SKIPN 0xb0000000
2251#define PHY_WRITE_PREVIOUS 0xc0000000
2252#define PHY_SKIPN 0xd0000000
2253#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002254
Hayes Wang960aee62011-06-18 11:37:48 +02002255struct fw_info {
2256 u32 magic;
2257 char version[RTL_VER_SIZE];
2258 __le32 fw_start;
2259 __le32 fw_len;
2260 u8 chksum;
2261} __packed;
2262
Francois Romieu1c361ef2011-06-17 17:16:24 +02002263#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2264
2265static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002266{
Francois Romieub6ffd972011-06-17 17:00:05 +02002267 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002268 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002269 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2270 char *version = rtl_fw->version;
2271 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002272
Francois Romieu1c361ef2011-06-17 17:16:24 +02002273 if (fw->size < FW_OPCODE_SIZE)
2274 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002275
2276 if (!fw_info->magic) {
2277 size_t i, size, start;
2278 u8 checksum = 0;
2279
2280 if (fw->size < sizeof(*fw_info))
2281 goto out;
2282
2283 for (i = 0; i < fw->size; i++)
2284 checksum += fw->data[i];
2285 if (checksum != 0)
2286 goto out;
2287
2288 start = le32_to_cpu(fw_info->fw_start);
2289 if (start > fw->size)
2290 goto out;
2291
2292 size = le32_to_cpu(fw_info->fw_len);
2293 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2294 goto out;
2295
2296 memcpy(version, fw_info->version, RTL_VER_SIZE);
2297
2298 pa->code = (__le32 *)(fw->data + start);
2299 pa->size = size;
2300 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002301 if (fw->size % FW_OPCODE_SIZE)
2302 goto out;
2303
2304 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2305
2306 pa->code = (__le32 *)fw->data;
2307 pa->size = fw->size / FW_OPCODE_SIZE;
2308 }
2309 version[RTL_VER_SIZE - 1] = 0;
2310
2311 rc = true;
2312out:
2313 return rc;
2314}
2315
Francois Romieufd112f22011-06-18 00:10:29 +02002316static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2317 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002318{
Francois Romieufd112f22011-06-18 00:10:29 +02002319 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002320 size_t index;
2321
Francois Romieu1c361ef2011-06-17 17:16:24 +02002322 for (index = 0; index < pa->size; index++) {
2323 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002324 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002325
hayeswang42b82dc2011-01-10 02:07:25 +00002326 switch(action & 0xf0000000) {
2327 case PHY_READ:
2328 case PHY_DATA_OR:
2329 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002330 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002331 case PHY_CLEAR_READCOUNT:
2332 case PHY_WRITE:
2333 case PHY_WRITE_PREVIOUS:
2334 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002335 break;
2336
hayeswang42b82dc2011-01-10 02:07:25 +00002337 case PHY_BJMPN:
2338 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002339 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002340 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002341 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002342 }
2343 break;
2344 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002345 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002346 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002347 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002348 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002349 }
2350 break;
2351 case PHY_COMP_EQ_SKIPN:
2352 case PHY_COMP_NEQ_SKIPN:
2353 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002354 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002355 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002356 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002357 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002358 }
2359 break;
2360
hayeswang42b82dc2011-01-10 02:07:25 +00002361 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002362 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002363 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002364 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002365 }
2366 }
Francois Romieufd112f22011-06-18 00:10:29 +02002367 rc = true;
2368out:
2369 return rc;
2370}
françois romieubca03d52011-01-03 15:07:31 +00002371
Francois Romieufd112f22011-06-18 00:10:29 +02002372static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2373{
2374 struct net_device *dev = tp->dev;
2375 int rc = -EINVAL;
2376
2377 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2378 netif_err(tp, ifup, dev, "invalid firwmare\n");
2379 goto out;
2380 }
2381
2382 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2383 rc = 0;
2384out:
2385 return rc;
2386}
2387
2388static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2389{
2390 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002391 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002392 u32 predata, count;
2393 size_t index;
2394
2395 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002396 org.write = ops->write;
2397 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002398
Francois Romieu1c361ef2011-06-17 17:16:24 +02002399 for (index = 0; index < pa->size; ) {
2400 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002401 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002402 u32 regno = (action & 0x0fff0000) >> 16;
2403
2404 if (!action)
2405 break;
françois romieubca03d52011-01-03 15:07:31 +00002406
2407 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002408 case PHY_READ:
2409 predata = rtl_readphy(tp, regno);
2410 count++;
2411 index++;
françois romieubca03d52011-01-03 15:07:31 +00002412 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002413 case PHY_DATA_OR:
2414 predata |= data;
2415 index++;
2416 break;
2417 case PHY_DATA_AND:
2418 predata &= data;
2419 index++;
2420 break;
2421 case PHY_BJMPN:
2422 index -= regno;
2423 break;
hayeswangeee37862013-04-01 22:23:38 +00002424 case PHY_MDIO_CHG:
2425 if (data == 0) {
2426 ops->write = org.write;
2427 ops->read = org.read;
2428 } else if (data == 1) {
2429 ops->write = mac_mcu_write;
2430 ops->read = mac_mcu_read;
2431 }
2432
hayeswang42b82dc2011-01-10 02:07:25 +00002433 index++;
2434 break;
2435 case PHY_CLEAR_READCOUNT:
2436 count = 0;
2437 index++;
2438 break;
2439 case PHY_WRITE:
2440 rtl_writephy(tp, regno, data);
2441 index++;
2442 break;
2443 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002444 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002445 break;
2446 case PHY_COMP_EQ_SKIPN:
2447 if (predata == data)
2448 index += regno;
2449 index++;
2450 break;
2451 case PHY_COMP_NEQ_SKIPN:
2452 if (predata != data)
2453 index += regno;
2454 index++;
2455 break;
2456 case PHY_WRITE_PREVIOUS:
2457 rtl_writephy(tp, regno, predata);
2458 index++;
2459 break;
2460 case PHY_SKIPN:
2461 index += regno + 1;
2462 break;
2463 case PHY_DELAY_MS:
2464 mdelay(data);
2465 index++;
2466 break;
2467
françois romieubca03d52011-01-03 15:07:31 +00002468 default:
2469 BUG();
2470 }
2471 }
hayeswangeee37862013-04-01 22:23:38 +00002472
2473 ops->write = org.write;
2474 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002475}
2476
françois romieuf1e02ed2011-01-13 13:07:53 +00002477static void rtl_release_firmware(struct rtl8169_private *tp)
2478{
Francois Romieub6ffd972011-06-17 17:00:05 +02002479 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2480 release_firmware(tp->rtl_fw->fw);
2481 kfree(tp->rtl_fw);
2482 }
2483 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002484}
2485
François Romieu953a12c2011-04-24 17:38:48 +02002486static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002487{
Francois Romieub6ffd972011-06-17 17:00:05 +02002488 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002489
2490 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002491 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002492 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002493}
2494
2495static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2496{
2497 if (rtl_readphy(tp, reg) != val)
2498 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2499 else
2500 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002501}
2502
françois romieu4da19632011-01-03 15:07:55 +00002503static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002505 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002506 { 0x1f, 0x0001 },
2507 { 0x06, 0x006e },
2508 { 0x08, 0x0708 },
2509 { 0x15, 0x4000 },
2510 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
françois romieu0b9b5712009-08-10 19:44:56 +00002512 { 0x1f, 0x0001 },
2513 { 0x03, 0x00a1 },
2514 { 0x02, 0x0008 },
2515 { 0x01, 0x0120 },
2516 { 0x00, 0x1000 },
2517 { 0x04, 0x0800 },
2518 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
françois romieu0b9b5712009-08-10 19:44:56 +00002520 { 0x03, 0xff41 },
2521 { 0x02, 0xdf60 },
2522 { 0x01, 0x0140 },
2523 { 0x00, 0x0077 },
2524 { 0x04, 0x7800 },
2525 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
françois romieu0b9b5712009-08-10 19:44:56 +00002527 { 0x03, 0x802f },
2528 { 0x02, 0x4f02 },
2529 { 0x01, 0x0409 },
2530 { 0x00, 0xf0f9 },
2531 { 0x04, 0x9800 },
2532 { 0x04, 0x9000 },
2533
2534 { 0x03, 0xdf01 },
2535 { 0x02, 0xdf20 },
2536 { 0x01, 0xff95 },
2537 { 0x00, 0xba00 },
2538 { 0x04, 0xa800 },
2539 { 0x04, 0xa000 },
2540
2541 { 0x03, 0xff41 },
2542 { 0x02, 0xdf20 },
2543 { 0x01, 0x0140 },
2544 { 0x00, 0x00bb },
2545 { 0x04, 0xb800 },
2546 { 0x04, 0xb000 },
2547
2548 { 0x03, 0xdf41 },
2549 { 0x02, 0xdc60 },
2550 { 0x01, 0x6340 },
2551 { 0x00, 0x007d },
2552 { 0x04, 0xd800 },
2553 { 0x04, 0xd000 },
2554
2555 { 0x03, 0xdf01 },
2556 { 0x02, 0xdf20 },
2557 { 0x01, 0x100a },
2558 { 0x00, 0xa0ff },
2559 { 0x04, 0xf800 },
2560 { 0x04, 0xf000 },
2561
2562 { 0x1f, 0x0000 },
2563 { 0x0b, 0x0000 },
2564 { 0x00, 0x9200 }
2565 };
2566
françois romieu4da19632011-01-03 15:07:55 +00002567 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
françois romieu4da19632011-01-03 15:07:55 +00002570static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002571{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002572 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002573 { 0x1f, 0x0002 },
2574 { 0x01, 0x90d0 },
2575 { 0x1f, 0x0000 }
2576 };
2577
françois romieu4da19632011-01-03 15:07:55 +00002578 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002579}
2580
françois romieu4da19632011-01-03 15:07:55 +00002581static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002582{
2583 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002584
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002585 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2586 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002587 return;
2588
françois romieu4da19632011-01-03 15:07:55 +00002589 rtl_writephy(tp, 0x1f, 0x0001);
2590 rtl_writephy(tp, 0x10, 0xf01b);
2591 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002592}
2593
françois romieu4da19632011-01-03 15:07:55 +00002594static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002595{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002596 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002597 { 0x1f, 0x0001 },
2598 { 0x04, 0x0000 },
2599 { 0x03, 0x00a1 },
2600 { 0x02, 0x0008 },
2601 { 0x01, 0x0120 },
2602 { 0x00, 0x1000 },
2603 { 0x04, 0x0800 },
2604 { 0x04, 0x9000 },
2605 { 0x03, 0x802f },
2606 { 0x02, 0x4f02 },
2607 { 0x01, 0x0409 },
2608 { 0x00, 0xf099 },
2609 { 0x04, 0x9800 },
2610 { 0x04, 0xa000 },
2611 { 0x03, 0xdf01 },
2612 { 0x02, 0xdf20 },
2613 { 0x01, 0xff95 },
2614 { 0x00, 0xba00 },
2615 { 0x04, 0xa800 },
2616 { 0x04, 0xf000 },
2617 { 0x03, 0xdf01 },
2618 { 0x02, 0xdf20 },
2619 { 0x01, 0x101a },
2620 { 0x00, 0xa0ff },
2621 { 0x04, 0xf800 },
2622 { 0x04, 0x0000 },
2623 { 0x1f, 0x0000 },
2624
2625 { 0x1f, 0x0001 },
2626 { 0x10, 0xf41b },
2627 { 0x14, 0xfb54 },
2628 { 0x18, 0xf5c7 },
2629 { 0x1f, 0x0000 },
2630
2631 { 0x1f, 0x0001 },
2632 { 0x17, 0x0cc0 },
2633 { 0x1f, 0x0000 }
2634 };
2635
françois romieu4da19632011-01-03 15:07:55 +00002636 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002637
françois romieu4da19632011-01-03 15:07:55 +00002638 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002639}
2640
françois romieu4da19632011-01-03 15:07:55 +00002641static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002642{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002643 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002644 { 0x1f, 0x0001 },
2645 { 0x04, 0x0000 },
2646 { 0x03, 0x00a1 },
2647 { 0x02, 0x0008 },
2648 { 0x01, 0x0120 },
2649 { 0x00, 0x1000 },
2650 { 0x04, 0x0800 },
2651 { 0x04, 0x9000 },
2652 { 0x03, 0x802f },
2653 { 0x02, 0x4f02 },
2654 { 0x01, 0x0409 },
2655 { 0x00, 0xf099 },
2656 { 0x04, 0x9800 },
2657 { 0x04, 0xa000 },
2658 { 0x03, 0xdf01 },
2659 { 0x02, 0xdf20 },
2660 { 0x01, 0xff95 },
2661 { 0x00, 0xba00 },
2662 { 0x04, 0xa800 },
2663 { 0x04, 0xf000 },
2664 { 0x03, 0xdf01 },
2665 { 0x02, 0xdf20 },
2666 { 0x01, 0x101a },
2667 { 0x00, 0xa0ff },
2668 { 0x04, 0xf800 },
2669 { 0x04, 0x0000 },
2670 { 0x1f, 0x0000 },
2671
2672 { 0x1f, 0x0001 },
2673 { 0x0b, 0x8480 },
2674 { 0x1f, 0x0000 },
2675
2676 { 0x1f, 0x0001 },
2677 { 0x18, 0x67c7 },
2678 { 0x04, 0x2000 },
2679 { 0x03, 0x002f },
2680 { 0x02, 0x4360 },
2681 { 0x01, 0x0109 },
2682 { 0x00, 0x3022 },
2683 { 0x04, 0x2800 },
2684 { 0x1f, 0x0000 },
2685
2686 { 0x1f, 0x0001 },
2687 { 0x17, 0x0cc0 },
2688 { 0x1f, 0x0000 }
2689 };
2690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002692}
2693
françois romieu4da19632011-01-03 15:07:55 +00002694static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002695{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002696 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002697 { 0x10, 0xf41b },
2698 { 0x1f, 0x0000 }
2699 };
2700
françois romieu4da19632011-01-03 15:07:55 +00002701 rtl_writephy(tp, 0x1f, 0x0001);
2702 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002703
françois romieu4da19632011-01-03 15:07:55 +00002704 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002705}
2706
françois romieu4da19632011-01-03 15:07:55 +00002707static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002708{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002709 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002710 { 0x1f, 0x0001 },
2711 { 0x10, 0xf41b },
2712 { 0x1f, 0x0000 }
2713 };
2714
françois romieu4da19632011-01-03 15:07:55 +00002715 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002716}
2717
françois romieu4da19632011-01-03 15:07:55 +00002718static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002719{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002720 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002721 { 0x1f, 0x0000 },
2722 { 0x1d, 0x0f00 },
2723 { 0x1f, 0x0002 },
2724 { 0x0c, 0x1ec8 },
2725 { 0x1f, 0x0000 }
2726 };
2727
françois romieu4da19632011-01-03 15:07:55 +00002728 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002729}
2730
françois romieu4da19632011-01-03 15:07:55 +00002731static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002732{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002733 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002734 { 0x1f, 0x0001 },
2735 { 0x1d, 0x3d98 },
2736 { 0x1f, 0x0000 }
2737 };
2738
françois romieu4da19632011-01-03 15:07:55 +00002739 rtl_writephy(tp, 0x1f, 0x0000);
2740 rtl_patchphy(tp, 0x14, 1 << 5);
2741 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002744}
2745
françois romieu4da19632011-01-03 15:07:55 +00002746static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002747{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002748 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002749 { 0x1f, 0x0001 },
2750 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002751 { 0x1f, 0x0002 },
2752 { 0x00, 0x88d4 },
2753 { 0x01, 0x82b1 },
2754 { 0x03, 0x7002 },
2755 { 0x08, 0x9e30 },
2756 { 0x09, 0x01f0 },
2757 { 0x0a, 0x5500 },
2758 { 0x0c, 0x00c8 },
2759 { 0x1f, 0x0003 },
2760 { 0x12, 0xc096 },
2761 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002762 { 0x1f, 0x0000 },
2763 { 0x1f, 0x0000 },
2764 { 0x09, 0x2000 },
2765 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002766 };
2767
françois romieu4da19632011-01-03 15:07:55 +00002768 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002769
françois romieu4da19632011-01-03 15:07:55 +00002770 rtl_patchphy(tp, 0x14, 1 << 5);
2771 rtl_patchphy(tp, 0x0d, 1 << 5);
2772 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002773}
2774
françois romieu4da19632011-01-03 15:07:55 +00002775static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002776{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002777 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002778 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002779 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002780 { 0x03, 0x802f },
2781 { 0x02, 0x4f02 },
2782 { 0x01, 0x0409 },
2783 { 0x00, 0xf099 },
2784 { 0x04, 0x9800 },
2785 { 0x04, 0x9000 },
2786 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002787 { 0x1f, 0x0002 },
2788 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002789 { 0x06, 0x0761 },
2790 { 0x1f, 0x0003 },
2791 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002792 { 0x1f, 0x0000 }
2793 };
2794
françois romieu4da19632011-01-03 15:07:55 +00002795 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002796
françois romieu4da19632011-01-03 15:07:55 +00002797 rtl_patchphy(tp, 0x16, 1 << 0);
2798 rtl_patchphy(tp, 0x14, 1 << 5);
2799 rtl_patchphy(tp, 0x0d, 1 << 5);
2800 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002801}
2802
françois romieu4da19632011-01-03 15:07:55 +00002803static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002804{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002805 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002806 { 0x1f, 0x0001 },
2807 { 0x12, 0x2300 },
2808 { 0x1d, 0x3d98 },
2809 { 0x1f, 0x0002 },
2810 { 0x0c, 0x7eb8 },
2811 { 0x06, 0x5461 },
2812 { 0x1f, 0x0003 },
2813 { 0x16, 0x0f0a },
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));
Francois Romieu197ff762008-06-28 13:16:02 +02002818
françois romieu4da19632011-01-03 15:07:55 +00002819 rtl_patchphy(tp, 0x16, 1 << 0);
2820 rtl_patchphy(tp, 0x14, 1 << 5);
2821 rtl_patchphy(tp, 0x0d, 1 << 5);
2822 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002823}
2824
françois romieu4da19632011-01-03 15:07:55 +00002825static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002826{
françois romieu4da19632011-01-03 15:07:55 +00002827 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002828}
2829
françois romieubca03d52011-01-03 15:07:31 +00002830static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002831{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002832 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002833 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002834 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002835 { 0x06, 0x4064 },
2836 { 0x07, 0x2863 },
2837 { 0x08, 0x059c },
2838 { 0x09, 0x26b4 },
2839 { 0x0a, 0x6a19 },
2840 { 0x0b, 0xdcc8 },
2841 { 0x10, 0xf06d },
2842 { 0x14, 0x7f68 },
2843 { 0x18, 0x7fd9 },
2844 { 0x1c, 0xf0ff },
2845 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002846 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002847 { 0x12, 0xf49f },
2848 { 0x13, 0x070b },
2849 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002850 { 0x14, 0x94c0 },
2851
2852 /*
2853 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002854 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002855 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002856 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002857 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002858 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002859 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002860 { 0x06, 0x5561 },
2861
2862 /*
2863 * Can not link to 1Gbps with bad cable
2864 * Decrease SNR threshold form 21.07dB to 19.04dB
2865 */
2866 { 0x1f, 0x0001 },
2867 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002868
2869 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002870 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002871 };
2872
françois romieu4da19632011-01-03 15:07:55 +00002873 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002874
françois romieubca03d52011-01-03 15:07:31 +00002875 /*
2876 * Rx Error Issue
2877 * Fine Tune Switching regulator parameter
2878 */
françois romieu4da19632011-01-03 15:07:55 +00002879 rtl_writephy(tp, 0x1f, 0x0002);
2880 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2881 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002882
Francois Romieufdf6fc02012-07-06 22:40:38 +02002883 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002884 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002885 { 0x1f, 0x0002 },
2886 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002887 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002888 { 0x05, 0x8330 },
2889 { 0x06, 0x669a },
2890 { 0x1f, 0x0002 }
2891 };
2892 int val;
2893
françois romieu4da19632011-01-03 15:07:55 +00002894 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002895
françois romieu4da19632011-01-03 15:07:55 +00002896 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002897
2898 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002899 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002900 0x0065, 0x0066, 0x0067, 0x0068,
2901 0x0069, 0x006a, 0x006b, 0x006c
2902 };
2903 int i;
2904
françois romieu4da19632011-01-03 15:07:55 +00002905 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002906
2907 val &= 0xff00;
2908 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002909 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002910 }
2911 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002912 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002913 { 0x1f, 0x0002 },
2914 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002915 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002916 { 0x05, 0x8330 },
2917 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002918 };
2919
françois romieu4da19632011-01-03 15:07:55 +00002920 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002921 }
2922
françois romieubca03d52011-01-03 15:07:31 +00002923 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002924 rtl_writephy(tp, 0x1f, 0x0002);
2925 rtl_patchphy(tp, 0x0d, 0x0300);
2926 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002927
françois romieubca03d52011-01-03 15:07:31 +00002928 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002929 rtl_writephy(tp, 0x1f, 0x0002);
2930 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2931 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002932
françois romieu4da19632011-01-03 15:07:55 +00002933 rtl_writephy(tp, 0x1f, 0x0005);
2934 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002935
2936 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002937
françois romieu4da19632011-01-03 15:07:55 +00002938 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002939}
2940
françois romieubca03d52011-01-03 15:07:31 +00002941static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002942{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002943 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002944 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002945 { 0x1f, 0x0001 },
2946 { 0x06, 0x4064 },
2947 { 0x07, 0x2863 },
2948 { 0x08, 0x059c },
2949 { 0x09, 0x26b4 },
2950 { 0x0a, 0x6a19 },
2951 { 0x0b, 0xdcc8 },
2952 { 0x10, 0xf06d },
2953 { 0x14, 0x7f68 },
2954 { 0x18, 0x7fd9 },
2955 { 0x1c, 0xf0ff },
2956 { 0x1d, 0x3d9c },
2957 { 0x1f, 0x0003 },
2958 { 0x12, 0xf49f },
2959 { 0x13, 0x070b },
2960 { 0x1a, 0x05ad },
2961 { 0x14, 0x94c0 },
2962
françois romieubca03d52011-01-03 15:07:31 +00002963 /*
2964 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002965 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002966 */
françois romieudaf9df62009-10-07 12:44:20 +00002967 { 0x1f, 0x0002 },
2968 { 0x06, 0x5561 },
2969 { 0x1f, 0x0005 },
2970 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002971 { 0x06, 0x5561 },
2972
2973 /*
2974 * Can not link to 1Gbps with bad cable
2975 * Decrease SNR threshold form 21.07dB to 19.04dB
2976 */
2977 { 0x1f, 0x0001 },
2978 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002979
2980 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002981 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002982 };
2983
françois romieu4da19632011-01-03 15:07:55 +00002984 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002985
Francois Romieufdf6fc02012-07-06 22:40:38 +02002986 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002987 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002988 { 0x1f, 0x0002 },
2989 { 0x05, 0x669a },
2990 { 0x1f, 0x0005 },
2991 { 0x05, 0x8330 },
2992 { 0x06, 0x669a },
2993
2994 { 0x1f, 0x0002 }
2995 };
2996 int val;
2997
françois romieu4da19632011-01-03 15:07:55 +00002998 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002999
françois romieu4da19632011-01-03 15:07:55 +00003000 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003001 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003002 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003003 0x0065, 0x0066, 0x0067, 0x0068,
3004 0x0069, 0x006a, 0x006b, 0x006c
3005 };
3006 int i;
3007
françois romieu4da19632011-01-03 15:07:55 +00003008 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003009
3010 val &= 0xff00;
3011 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003012 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003013 }
3014 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003015 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003016 { 0x1f, 0x0002 },
3017 { 0x05, 0x2642 },
3018 { 0x1f, 0x0005 },
3019 { 0x05, 0x8330 },
3020 { 0x06, 0x2642 }
3021 };
3022
françois romieu4da19632011-01-03 15:07:55 +00003023 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00003024 }
3025
françois romieubca03d52011-01-03 15:07:31 +00003026 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003027 rtl_writephy(tp, 0x1f, 0x0002);
3028 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
3029 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003030
françois romieubca03d52011-01-03 15:07:31 +00003031 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003032 rtl_writephy(tp, 0x1f, 0x0002);
3033 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003034
françois romieu4da19632011-01-03 15:07:55 +00003035 rtl_writephy(tp, 0x1f, 0x0005);
3036 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003037
3038 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003039
françois romieu4da19632011-01-03 15:07:55 +00003040 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003041}
3042
françois romieu4da19632011-01-03 15:07:55 +00003043static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003044{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003045 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003046 { 0x1f, 0x0002 },
3047 { 0x10, 0x0008 },
3048 { 0x0d, 0x006c },
3049
3050 { 0x1f, 0x0000 },
3051 { 0x0d, 0xf880 },
3052
3053 { 0x1f, 0x0001 },
3054 { 0x17, 0x0cc0 },
3055
3056 { 0x1f, 0x0001 },
3057 { 0x0b, 0xa4d8 },
3058 { 0x09, 0x281c },
3059 { 0x07, 0x2883 },
3060 { 0x0a, 0x6b35 },
3061 { 0x1d, 0x3da4 },
3062 { 0x1c, 0xeffd },
3063 { 0x14, 0x7f52 },
3064 { 0x18, 0x7fc6 },
3065 { 0x08, 0x0601 },
3066 { 0x06, 0x4063 },
3067 { 0x10, 0xf074 },
3068 { 0x1f, 0x0003 },
3069 { 0x13, 0x0789 },
3070 { 0x12, 0xf4bd },
3071 { 0x1a, 0x04fd },
3072 { 0x14, 0x84b0 },
3073 { 0x1f, 0x0000 },
3074 { 0x00, 0x9200 },
3075
3076 { 0x1f, 0x0005 },
3077 { 0x01, 0x0340 },
3078 { 0x1f, 0x0001 },
3079 { 0x04, 0x4000 },
3080 { 0x03, 0x1d21 },
3081 { 0x02, 0x0c32 },
3082 { 0x01, 0x0200 },
3083 { 0x00, 0x5554 },
3084 { 0x04, 0x4800 },
3085 { 0x04, 0x4000 },
3086 { 0x04, 0xf000 },
3087 { 0x03, 0xdf01 },
3088 { 0x02, 0xdf20 },
3089 { 0x01, 0x101a },
3090 { 0x00, 0xa0ff },
3091 { 0x04, 0xf800 },
3092 { 0x04, 0xf000 },
3093 { 0x1f, 0x0000 },
3094
3095 { 0x1f, 0x0007 },
3096 { 0x1e, 0x0023 },
3097 { 0x16, 0x0000 },
3098 { 0x1f, 0x0000 }
3099 };
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 romieue6de30d2011-01-03 15:08:37 +00003104static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3105{
3106 static const struct phy_reg phy_reg_init[] = {
3107 { 0x1f, 0x0001 },
3108 { 0x17, 0x0cc0 },
3109
3110 { 0x1f, 0x0007 },
3111 { 0x1e, 0x002d },
3112 { 0x18, 0x0040 },
3113 { 0x1f, 0x0000 }
3114 };
3115
3116 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3117 rtl_patchphy(tp, 0x0d, 1 << 5);
3118}
3119
Hayes Wang70090422011-07-06 15:58:06 +08003120static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003121{
3122 static const struct phy_reg phy_reg_init[] = {
3123 /* Enable Delay cap */
3124 { 0x1f, 0x0005 },
3125 { 0x05, 0x8b80 },
3126 { 0x06, 0xc896 },
3127 { 0x1f, 0x0000 },
3128
3129 /* Channel estimation fine tune */
3130 { 0x1f, 0x0001 },
3131 { 0x0b, 0x6c20 },
3132 { 0x07, 0x2872 },
3133 { 0x1c, 0xefff },
3134 { 0x1f, 0x0003 },
3135 { 0x14, 0x6420 },
3136 { 0x1f, 0x0000 },
3137
3138 /* Update PFM & 10M TX idle timer */
3139 { 0x1f, 0x0007 },
3140 { 0x1e, 0x002f },
3141 { 0x15, 0x1919 },
3142 { 0x1f, 0x0000 },
3143
3144 { 0x1f, 0x0007 },
3145 { 0x1e, 0x00ac },
3146 { 0x18, 0x0006 },
3147 { 0x1f, 0x0000 }
3148 };
3149
Francois Romieu15ecd032011-04-27 13:52:22 -07003150 rtl_apply_firmware(tp);
3151
hayeswang01dc7fe2011-03-21 01:50:28 +00003152 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3153
3154 /* DCO enable for 10M IDLE Power */
3155 rtl_writephy(tp, 0x1f, 0x0007);
3156 rtl_writephy(tp, 0x1e, 0x0023);
3157 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3158 rtl_writephy(tp, 0x1f, 0x0000);
3159
3160 /* For impedance matching */
3161 rtl_writephy(tp, 0x1f, 0x0002);
3162 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003163 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003164
3165 /* PHY auto speed down */
3166 rtl_writephy(tp, 0x1f, 0x0007);
3167 rtl_writephy(tp, 0x1e, 0x002d);
3168 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3169 rtl_writephy(tp, 0x1f, 0x0000);
3170 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3171
3172 rtl_writephy(tp, 0x1f, 0x0005);
3173 rtl_writephy(tp, 0x05, 0x8b86);
3174 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3175 rtl_writephy(tp, 0x1f, 0x0000);
3176
3177 rtl_writephy(tp, 0x1f, 0x0005);
3178 rtl_writephy(tp, 0x05, 0x8b85);
3179 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3180 rtl_writephy(tp, 0x1f, 0x0007);
3181 rtl_writephy(tp, 0x1e, 0x0020);
3182 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3183 rtl_writephy(tp, 0x1f, 0x0006);
3184 rtl_writephy(tp, 0x00, 0x5a00);
3185 rtl_writephy(tp, 0x1f, 0x0000);
3186 rtl_writephy(tp, 0x0d, 0x0007);
3187 rtl_writephy(tp, 0x0e, 0x003c);
3188 rtl_writephy(tp, 0x0d, 0x4007);
3189 rtl_writephy(tp, 0x0e, 0x0000);
3190 rtl_writephy(tp, 0x0d, 0x0000);
3191}
3192
françois romieu9ecb9aa2012-12-07 11:20:21 +00003193static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3194{
3195 const u16 w[] = {
3196 addr[0] | (addr[1] << 8),
3197 addr[2] | (addr[3] << 8),
3198 addr[4] | (addr[5] << 8)
3199 };
3200 const struct exgmac_reg e[] = {
3201 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3202 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3203 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3204 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3205 };
3206
3207 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3208}
3209
Hayes Wang70090422011-07-06 15:58:06 +08003210static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3211{
3212 static const struct phy_reg phy_reg_init[] = {
3213 /* Enable Delay cap */
3214 { 0x1f, 0x0004 },
3215 { 0x1f, 0x0007 },
3216 { 0x1e, 0x00ac },
3217 { 0x18, 0x0006 },
3218 { 0x1f, 0x0002 },
3219 { 0x1f, 0x0000 },
3220 { 0x1f, 0x0000 },
3221
3222 /* Channel estimation fine tune */
3223 { 0x1f, 0x0003 },
3224 { 0x09, 0xa20f },
3225 { 0x1f, 0x0000 },
3226 { 0x1f, 0x0000 },
3227
3228 /* Green Setting */
3229 { 0x1f, 0x0005 },
3230 { 0x05, 0x8b5b },
3231 { 0x06, 0x9222 },
3232 { 0x05, 0x8b6d },
3233 { 0x06, 0x8000 },
3234 { 0x05, 0x8b76 },
3235 { 0x06, 0x8000 },
3236 { 0x1f, 0x0000 }
3237 };
3238
3239 rtl_apply_firmware(tp);
3240
3241 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3242
3243 /* For 4-corner performance improve */
3244 rtl_writephy(tp, 0x1f, 0x0005);
3245 rtl_writephy(tp, 0x05, 0x8b80);
3246 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3247 rtl_writephy(tp, 0x1f, 0x0000);
3248
3249 /* PHY auto speed down */
3250 rtl_writephy(tp, 0x1f, 0x0004);
3251 rtl_writephy(tp, 0x1f, 0x0007);
3252 rtl_writephy(tp, 0x1e, 0x002d);
3253 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3254 rtl_writephy(tp, 0x1f, 0x0002);
3255 rtl_writephy(tp, 0x1f, 0x0000);
3256 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3257
3258 /* improve 10M EEE waveform */
3259 rtl_writephy(tp, 0x1f, 0x0005);
3260 rtl_writephy(tp, 0x05, 0x8b86);
3261 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3262 rtl_writephy(tp, 0x1f, 0x0000);
3263
3264 /* Improve 2-pair detection performance */
3265 rtl_writephy(tp, 0x1f, 0x0005);
3266 rtl_writephy(tp, 0x05, 0x8b85);
3267 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3268 rtl_writephy(tp, 0x1f, 0x0000);
3269
3270 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003271 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003272 rtl_writephy(tp, 0x1f, 0x0005);
3273 rtl_writephy(tp, 0x05, 0x8b85);
3274 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3275 rtl_writephy(tp, 0x1f, 0x0004);
3276 rtl_writephy(tp, 0x1f, 0x0007);
3277 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003278 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003279 rtl_writephy(tp, 0x1f, 0x0002);
3280 rtl_writephy(tp, 0x1f, 0x0000);
3281 rtl_writephy(tp, 0x0d, 0x0007);
3282 rtl_writephy(tp, 0x0e, 0x003c);
3283 rtl_writephy(tp, 0x0d, 0x4007);
3284 rtl_writephy(tp, 0x0e, 0x0000);
3285 rtl_writephy(tp, 0x0d, 0x0000);
3286
3287 /* Green feature */
3288 rtl_writephy(tp, 0x1f, 0x0003);
3289 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3290 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3291 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003292
françois romieu9ecb9aa2012-12-07 11:20:21 +00003293 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3294 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003295}
3296
Hayes Wang5f886e02012-03-30 14:33:03 +08003297static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3298{
3299 /* For 4-corner performance improve */
3300 rtl_writephy(tp, 0x1f, 0x0005);
3301 rtl_writephy(tp, 0x05, 0x8b80);
3302 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3303 rtl_writephy(tp, 0x1f, 0x0000);
3304
3305 /* PHY auto speed down */
3306 rtl_writephy(tp, 0x1f, 0x0007);
3307 rtl_writephy(tp, 0x1e, 0x002d);
3308 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3309 rtl_writephy(tp, 0x1f, 0x0000);
3310 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3311
3312 /* Improve 10M EEE waveform */
3313 rtl_writephy(tp, 0x1f, 0x0005);
3314 rtl_writephy(tp, 0x05, 0x8b86);
3315 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3316 rtl_writephy(tp, 0x1f, 0x0000);
3317}
3318
Hayes Wangc2218922011-09-06 16:55:18 +08003319static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3320{
3321 static const struct phy_reg phy_reg_init[] = {
3322 /* Channel estimation fine tune */
3323 { 0x1f, 0x0003 },
3324 { 0x09, 0xa20f },
3325 { 0x1f, 0x0000 },
3326
3327 /* Modify green table for giga & fnet */
3328 { 0x1f, 0x0005 },
3329 { 0x05, 0x8b55 },
3330 { 0x06, 0x0000 },
3331 { 0x05, 0x8b5e },
3332 { 0x06, 0x0000 },
3333 { 0x05, 0x8b67 },
3334 { 0x06, 0x0000 },
3335 { 0x05, 0x8b70 },
3336 { 0x06, 0x0000 },
3337 { 0x1f, 0x0000 },
3338 { 0x1f, 0x0007 },
3339 { 0x1e, 0x0078 },
3340 { 0x17, 0x0000 },
3341 { 0x19, 0x00fb },
3342 { 0x1f, 0x0000 },
3343
3344 /* Modify green table for 10M */
3345 { 0x1f, 0x0005 },
3346 { 0x05, 0x8b79 },
3347 { 0x06, 0xaa00 },
3348 { 0x1f, 0x0000 },
3349
3350 /* Disable hiimpedance detection (RTCT) */
3351 { 0x1f, 0x0003 },
3352 { 0x01, 0x328a },
3353 { 0x1f, 0x0000 }
3354 };
3355
3356 rtl_apply_firmware(tp);
3357
3358 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3359
Hayes Wang5f886e02012-03-30 14:33:03 +08003360 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003361
3362 /* Improve 2-pair detection performance */
3363 rtl_writephy(tp, 0x1f, 0x0005);
3364 rtl_writephy(tp, 0x05, 0x8b85);
3365 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3366 rtl_writephy(tp, 0x1f, 0x0000);
3367}
3368
3369static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3370{
3371 rtl_apply_firmware(tp);
3372
Hayes Wang5f886e02012-03-30 14:33:03 +08003373 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003374}
3375
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003376static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3377{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003378 static const struct phy_reg phy_reg_init[] = {
3379 /* Channel estimation fine tune */
3380 { 0x1f, 0x0003 },
3381 { 0x09, 0xa20f },
3382 { 0x1f, 0x0000 },
3383
3384 /* Modify green table for giga & fnet */
3385 { 0x1f, 0x0005 },
3386 { 0x05, 0x8b55 },
3387 { 0x06, 0x0000 },
3388 { 0x05, 0x8b5e },
3389 { 0x06, 0x0000 },
3390 { 0x05, 0x8b67 },
3391 { 0x06, 0x0000 },
3392 { 0x05, 0x8b70 },
3393 { 0x06, 0x0000 },
3394 { 0x1f, 0x0000 },
3395 { 0x1f, 0x0007 },
3396 { 0x1e, 0x0078 },
3397 { 0x17, 0x0000 },
3398 { 0x19, 0x00aa },
3399 { 0x1f, 0x0000 },
3400
3401 /* Modify green table for 10M */
3402 { 0x1f, 0x0005 },
3403 { 0x05, 0x8b79 },
3404 { 0x06, 0xaa00 },
3405 { 0x1f, 0x0000 },
3406
3407 /* Disable hiimpedance detection (RTCT) */
3408 { 0x1f, 0x0003 },
3409 { 0x01, 0x328a },
3410 { 0x1f, 0x0000 }
3411 };
3412
3413
3414 rtl_apply_firmware(tp);
3415
3416 rtl8168f_hw_phy_config(tp);
3417
3418 /* Improve 2-pair detection performance */
3419 rtl_writephy(tp, 0x1f, 0x0005);
3420 rtl_writephy(tp, 0x05, 0x8b85);
3421 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3422 rtl_writephy(tp, 0x1f, 0x0000);
3423
3424 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3425
3426 /* Modify green table for giga */
3427 rtl_writephy(tp, 0x1f, 0x0005);
3428 rtl_writephy(tp, 0x05, 0x8b54);
3429 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3430 rtl_writephy(tp, 0x05, 0x8b5d);
3431 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3432 rtl_writephy(tp, 0x05, 0x8a7c);
3433 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3434 rtl_writephy(tp, 0x05, 0x8a7f);
3435 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3436 rtl_writephy(tp, 0x05, 0x8a82);
3437 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3438 rtl_writephy(tp, 0x05, 0x8a85);
3439 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3440 rtl_writephy(tp, 0x05, 0x8a88);
3441 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3442 rtl_writephy(tp, 0x1f, 0x0000);
3443
3444 /* uc same-seed solution */
3445 rtl_writephy(tp, 0x1f, 0x0005);
3446 rtl_writephy(tp, 0x05, 0x8b85);
3447 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3448 rtl_writephy(tp, 0x1f, 0x0000);
3449
3450 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003451 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003452 rtl_writephy(tp, 0x1f, 0x0005);
3453 rtl_writephy(tp, 0x05, 0x8b85);
3454 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3455 rtl_writephy(tp, 0x1f, 0x0004);
3456 rtl_writephy(tp, 0x1f, 0x0007);
3457 rtl_writephy(tp, 0x1e, 0x0020);
3458 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3459 rtl_writephy(tp, 0x1f, 0x0000);
3460 rtl_writephy(tp, 0x0d, 0x0007);
3461 rtl_writephy(tp, 0x0e, 0x003c);
3462 rtl_writephy(tp, 0x0d, 0x4007);
3463 rtl_writephy(tp, 0x0e, 0x0000);
3464 rtl_writephy(tp, 0x0d, 0x0000);
3465
3466 /* Green feature */
3467 rtl_writephy(tp, 0x1f, 0x0003);
3468 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3469 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3470 rtl_writephy(tp, 0x1f, 0x0000);
3471}
3472
Hayes Wangc5583862012-07-02 17:23:22 +08003473static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3474{
Hayes Wangc5583862012-07-02 17:23:22 +08003475 rtl_apply_firmware(tp);
3476
hayeswang41f44d12013-04-01 22:23:36 +00003477 rtl_writephy(tp, 0x1f, 0x0a46);
3478 if (rtl_readphy(tp, 0x10) & 0x0100) {
3479 rtl_writephy(tp, 0x1f, 0x0bcc);
3480 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3481 } else {
3482 rtl_writephy(tp, 0x1f, 0x0bcc);
3483 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3484 }
Hayes Wangc5583862012-07-02 17:23:22 +08003485
hayeswang41f44d12013-04-01 22:23:36 +00003486 rtl_writephy(tp, 0x1f, 0x0a46);
3487 if (rtl_readphy(tp, 0x13) & 0x0100) {
3488 rtl_writephy(tp, 0x1f, 0x0c41);
3489 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3490 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003491 rtl_writephy(tp, 0x1f, 0x0c41);
3492 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003493 }
Hayes Wangc5583862012-07-02 17:23:22 +08003494
hayeswang41f44d12013-04-01 22:23:36 +00003495 /* Enable PHY auto speed down */
3496 rtl_writephy(tp, 0x1f, 0x0a44);
3497 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003498
hayeswangfe7524c2013-04-01 22:23:37 +00003499 rtl_writephy(tp, 0x1f, 0x0bcc);
3500 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3501 rtl_writephy(tp, 0x1f, 0x0a44);
3502 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3503 rtl_writephy(tp, 0x1f, 0x0a43);
3504 rtl_writephy(tp, 0x13, 0x8084);
3505 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3506 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3507
hayeswang41f44d12013-04-01 22:23:36 +00003508 /* EEE auto-fallback function */
3509 rtl_writephy(tp, 0x1f, 0x0a4b);
3510 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003511
hayeswang41f44d12013-04-01 22:23:36 +00003512 /* Enable UC LPF tune function */
3513 rtl_writephy(tp, 0x1f, 0x0a43);
3514 rtl_writephy(tp, 0x13, 0x8012);
3515 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3516
3517 rtl_writephy(tp, 0x1f, 0x0c42);
3518 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3519
hayeswangfe7524c2013-04-01 22:23:37 +00003520 /* Improve SWR Efficiency */
3521 rtl_writephy(tp, 0x1f, 0x0bcd);
3522 rtl_writephy(tp, 0x14, 0x5065);
3523 rtl_writephy(tp, 0x14, 0xd065);
3524 rtl_writephy(tp, 0x1f, 0x0bc8);
3525 rtl_writephy(tp, 0x11, 0x5655);
3526 rtl_writephy(tp, 0x1f, 0x0bcd);
3527 rtl_writephy(tp, 0x14, 0x1065);
3528 rtl_writephy(tp, 0x14, 0x9065);
3529 rtl_writephy(tp, 0x14, 0x1065);
3530
David Chang1bac1072013-11-27 15:48:36 +08003531 /* Check ALDPS bit, disable it if enabled */
3532 rtl_writephy(tp, 0x1f, 0x0a43);
3533 if (rtl_readphy(tp, 0x10) & 0x0004)
3534 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3535
hayeswang41f44d12013-04-01 22:23:36 +00003536 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003537}
3538
hayeswang57538c42013-04-01 22:23:40 +00003539static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3540{
3541 rtl_apply_firmware(tp);
3542}
3543
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003544static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3545{
3546 u16 dout_tapbin;
3547 u32 data;
3548
3549 rtl_apply_firmware(tp);
3550
3551 /* CHN EST parameters adjust - giga master */
3552 rtl_writephy(tp, 0x1f, 0x0a43);
3553 rtl_writephy(tp, 0x13, 0x809b);
3554 rtl_w1w0_phy(tp, 0x14, 0x8000, 0xf800);
3555 rtl_writephy(tp, 0x13, 0x80a2);
3556 rtl_w1w0_phy(tp, 0x14, 0x8000, 0xff00);
3557 rtl_writephy(tp, 0x13, 0x80a4);
3558 rtl_w1w0_phy(tp, 0x14, 0x8500, 0xff00);
3559 rtl_writephy(tp, 0x13, 0x809c);
3560 rtl_w1w0_phy(tp, 0x14, 0xbd00, 0xff00);
3561 rtl_writephy(tp, 0x1f, 0x0000);
3562
3563 /* CHN EST parameters adjust - giga slave */
3564 rtl_writephy(tp, 0x1f, 0x0a43);
3565 rtl_writephy(tp, 0x13, 0x80ad);
3566 rtl_w1w0_phy(tp, 0x14, 0x7000, 0xf800);
3567 rtl_writephy(tp, 0x13, 0x80b4);
3568 rtl_w1w0_phy(tp, 0x14, 0x5000, 0xff00);
3569 rtl_writephy(tp, 0x13, 0x80ac);
3570 rtl_w1w0_phy(tp, 0x14, 0x4000, 0xff00);
3571 rtl_writephy(tp, 0x1f, 0x0000);
3572
3573 /* CHN EST parameters adjust - fnet */
3574 rtl_writephy(tp, 0x1f, 0x0a43);
3575 rtl_writephy(tp, 0x13, 0x808e);
3576 rtl_w1w0_phy(tp, 0x14, 0x1200, 0xff00);
3577 rtl_writephy(tp, 0x13, 0x8090);
3578 rtl_w1w0_phy(tp, 0x14, 0xe500, 0xff00);
3579 rtl_writephy(tp, 0x13, 0x8092);
3580 rtl_w1w0_phy(tp, 0x14, 0x9f00, 0xff00);
3581 rtl_writephy(tp, 0x1f, 0x0000);
3582
3583 /* enable R-tune & PGA-retune function */
3584 dout_tapbin = 0;
3585 rtl_writephy(tp, 0x1f, 0x0a46);
3586 data = rtl_readphy(tp, 0x13);
3587 data &= 3;
3588 data <<= 2;
3589 dout_tapbin |= data;
3590 data = rtl_readphy(tp, 0x12);
3591 data &= 0xc000;
3592 data >>= 14;
3593 dout_tapbin |= data;
3594 dout_tapbin = ~(dout_tapbin^0x08);
3595 dout_tapbin <<= 12;
3596 dout_tapbin &= 0xf000;
3597 rtl_writephy(tp, 0x1f, 0x0a43);
3598 rtl_writephy(tp, 0x13, 0x827a);
3599 rtl_w1w0_phy(tp, 0x14, dout_tapbin, 0xf000);
3600 rtl_writephy(tp, 0x13, 0x827b);
3601 rtl_w1w0_phy(tp, 0x14, dout_tapbin, 0xf000);
3602 rtl_writephy(tp, 0x13, 0x827c);
3603 rtl_w1w0_phy(tp, 0x14, dout_tapbin, 0xf000);
3604 rtl_writephy(tp, 0x13, 0x827d);
3605 rtl_w1w0_phy(tp, 0x14, dout_tapbin, 0xf000);
3606
3607 rtl_writephy(tp, 0x1f, 0x0a43);
3608 rtl_writephy(tp, 0x13, 0x0811);
3609 rtl_w1w0_phy(tp, 0x14, 0x0800, 0x0000);
3610 rtl_writephy(tp, 0x1f, 0x0a42);
3611 rtl_w1w0_phy(tp, 0x16, 0x0002, 0x0000);
3612 rtl_writephy(tp, 0x1f, 0x0000);
3613
3614 /* enable GPHY 10M */
3615 rtl_writephy(tp, 0x1f, 0x0a44);
3616 rtl_w1w0_phy(tp, 0x11, 0x0800, 0x0000);
3617 rtl_writephy(tp, 0x1f, 0x0000);
3618
3619 /* SAR ADC performance */
3620 rtl_writephy(tp, 0x1f, 0x0bca);
3621 rtl_w1w0_phy(tp, 0x17, 0x4000, 0x3000);
3622 rtl_writephy(tp, 0x1f, 0x0000);
3623
3624 rtl_writephy(tp, 0x1f, 0x0a43);
3625 rtl_writephy(tp, 0x13, 0x803f);
3626 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3627 rtl_writephy(tp, 0x13, 0x8047);
3628 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3629 rtl_writephy(tp, 0x13, 0x804f);
3630 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3631 rtl_writephy(tp, 0x13, 0x8057);
3632 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3633 rtl_writephy(tp, 0x13, 0x805f);
3634 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3635 rtl_writephy(tp, 0x13, 0x8067);
3636 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3637 rtl_writephy(tp, 0x13, 0x806f);
3638 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x3000);
3639 rtl_writephy(tp, 0x1f, 0x0000);
3640
3641 /* disable phy pfm mode */
3642 rtl_writephy(tp, 0x1f, 0x0a44);
3643 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x0080);
3644 rtl_writephy(tp, 0x1f, 0x0000);
3645
3646 /* Check ALDPS bit, disable it if enabled */
3647 rtl_writephy(tp, 0x1f, 0x0a43);
3648 if (rtl_readphy(tp, 0x10) & 0x0004)
3649 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3650
3651 rtl_writephy(tp, 0x1f, 0x0000);
3652}
3653
3654static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3655{
3656 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3657 u16 rlen;
3658 u32 data;
3659
3660 rtl_apply_firmware(tp);
3661
3662 /* CHIN EST parameter update */
3663 rtl_writephy(tp, 0x1f, 0x0a43);
3664 rtl_writephy(tp, 0x13, 0x808a);
3665 rtl_w1w0_phy(tp, 0x14, 0x000a, 0x003f);
3666 rtl_writephy(tp, 0x1f, 0x0000);
3667
3668 /* enable R-tune & PGA-retune function */
3669 rtl_writephy(tp, 0x1f, 0x0a43);
3670 rtl_writephy(tp, 0x13, 0x0811);
3671 rtl_w1w0_phy(tp, 0x14, 0x0800, 0x0000);
3672 rtl_writephy(tp, 0x1f, 0x0a42);
3673 rtl_w1w0_phy(tp, 0x16, 0x0002, 0x0000);
3674 rtl_writephy(tp, 0x1f, 0x0000);
3675
3676 /* enable GPHY 10M */
3677 rtl_writephy(tp, 0x1f, 0x0a44);
3678 rtl_w1w0_phy(tp, 0x11, 0x0800, 0x0000);
3679 rtl_writephy(tp, 0x1f, 0x0000);
3680
3681 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3682 data = r8168_mac_ocp_read(tp, 0xdd02);
3683 ioffset_p3 = ((data & 0x80)>>7);
3684 ioffset_p3 <<= 3;
3685
3686 data = r8168_mac_ocp_read(tp, 0xdd00);
3687 ioffset_p3 |= ((data & (0xe000))>>13);
3688 ioffset_p2 = ((data & (0x1e00))>>9);
3689 ioffset_p1 = ((data & (0x01e0))>>5);
3690 ioffset_p0 = ((data & 0x0010)>>4);
3691 ioffset_p0 <<= 3;
3692 ioffset_p0 |= (data & (0x07));
3693 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3694
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003695 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3696 (ioffset_p1 != 0x0f) || (ioffset_p0 == 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003697 rtl_writephy(tp, 0x1f, 0x0bcf);
3698 rtl_writephy(tp, 0x16, data);
3699 rtl_writephy(tp, 0x1f, 0x0000);
3700 }
3701
3702 /* Modify rlen (TX LPF corner frequency) level */
3703 rtl_writephy(tp, 0x1f, 0x0bcd);
3704 data = rtl_readphy(tp, 0x16);
3705 data &= 0x000f;
3706 rlen = 0;
3707 if (data > 3)
3708 rlen = data - 3;
3709 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3710 rtl_writephy(tp, 0x17, data);
3711 rtl_writephy(tp, 0x1f, 0x0bcd);
3712 rtl_writephy(tp, 0x1f, 0x0000);
3713
3714 /* disable phy pfm mode */
3715 rtl_writephy(tp, 0x1f, 0x0a44);
3716 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x0080);
3717 rtl_writephy(tp, 0x1f, 0x0000);
3718
3719 /* Check ALDPS bit, disable it if enabled */
3720 rtl_writephy(tp, 0x1f, 0x0a43);
3721 if (rtl_readphy(tp, 0x10) & 0x0004)
3722 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3723
3724 rtl_writephy(tp, 0x1f, 0x0000);
3725}
3726
françois romieu4da19632011-01-03 15:07:55 +00003727static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003728{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003729 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003730 { 0x1f, 0x0003 },
3731 { 0x08, 0x441d },
3732 { 0x01, 0x9100 },
3733 { 0x1f, 0x0000 }
3734 };
3735
françois romieu4da19632011-01-03 15:07:55 +00003736 rtl_writephy(tp, 0x1f, 0x0000);
3737 rtl_patchphy(tp, 0x11, 1 << 12);
3738 rtl_patchphy(tp, 0x19, 1 << 13);
3739 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003740
françois romieu4da19632011-01-03 15:07:55 +00003741 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003742}
3743
Hayes Wang5a5e4442011-02-22 17:26:21 +08003744static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3745{
3746 static const struct phy_reg phy_reg_init[] = {
3747 { 0x1f, 0x0005 },
3748 { 0x1a, 0x0000 },
3749 { 0x1f, 0x0000 },
3750
3751 { 0x1f, 0x0004 },
3752 { 0x1c, 0x0000 },
3753 { 0x1f, 0x0000 },
3754
3755 { 0x1f, 0x0001 },
3756 { 0x15, 0x7701 },
3757 { 0x1f, 0x0000 }
3758 };
3759
3760 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003761 rtl_writephy(tp, 0x1f, 0x0000);
3762 rtl_writephy(tp, 0x18, 0x0310);
3763 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003764
François Romieu953a12c2011-04-24 17:38:48 +02003765 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003766
3767 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3768}
3769
Hayes Wang7e18dca2012-03-30 14:33:02 +08003770static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3771{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003772 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003773 rtl_writephy(tp, 0x1f, 0x0000);
3774 rtl_writephy(tp, 0x18, 0x0310);
3775 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003776
3777 rtl_apply_firmware(tp);
3778
3779 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003780 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003781 rtl_writephy(tp, 0x1f, 0x0004);
3782 rtl_writephy(tp, 0x10, 0x401f);
3783 rtl_writephy(tp, 0x19, 0x7030);
3784 rtl_writephy(tp, 0x1f, 0x0000);
3785}
3786
Hayes Wang5598bfe2012-07-02 17:23:21 +08003787static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3788{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003789 static const struct phy_reg phy_reg_init[] = {
3790 { 0x1f, 0x0004 },
3791 { 0x10, 0xc07f },
3792 { 0x19, 0x7030 },
3793 { 0x1f, 0x0000 }
3794 };
3795
3796 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003797 rtl_writephy(tp, 0x1f, 0x0000);
3798 rtl_writephy(tp, 0x18, 0x0310);
3799 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003800
3801 rtl_apply_firmware(tp);
3802
Francois Romieufdf6fc02012-07-06 22:40:38 +02003803 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003804 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3805
Francois Romieufdf6fc02012-07-06 22:40:38 +02003806 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003807}
3808
Francois Romieu5615d9f2007-08-17 17:50:46 +02003809static void rtl_hw_phy_config(struct net_device *dev)
3810{
3811 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003812
3813 rtl8169_print_mac_version(tp);
3814
3815 switch (tp->mac_version) {
3816 case RTL_GIGA_MAC_VER_01:
3817 break;
3818 case RTL_GIGA_MAC_VER_02:
3819 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003820 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003821 break;
3822 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003823 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003824 break;
françois romieu2e9558562009-08-10 19:44:19 +00003825 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003826 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003827 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003828 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003829 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003830 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003831 case RTL_GIGA_MAC_VER_07:
3832 case RTL_GIGA_MAC_VER_08:
3833 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003834 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003835 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003836 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003837 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003838 break;
3839 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003840 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003841 break;
3842 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003843 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003844 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003845 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003846 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003847 break;
3848 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003849 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003850 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003851 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003852 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003853 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003854 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003855 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003856 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003857 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003858 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003859 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003860 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003861 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003862 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003863 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003864 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003865 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003866 break;
3867 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003868 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003869 break;
3870 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003871 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003872 break;
françois romieue6de30d2011-01-03 15:08:37 +00003873 case RTL_GIGA_MAC_VER_28:
3874 rtl8168d_4_hw_phy_config(tp);
3875 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003876 case RTL_GIGA_MAC_VER_29:
3877 case RTL_GIGA_MAC_VER_30:
3878 rtl8105e_hw_phy_config(tp);
3879 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003880 case RTL_GIGA_MAC_VER_31:
3881 /* None. */
3882 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003883 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003884 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003885 rtl8168e_1_hw_phy_config(tp);
3886 break;
3887 case RTL_GIGA_MAC_VER_34:
3888 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003889 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003890 case RTL_GIGA_MAC_VER_35:
3891 rtl8168f_1_hw_phy_config(tp);
3892 break;
3893 case RTL_GIGA_MAC_VER_36:
3894 rtl8168f_2_hw_phy_config(tp);
3895 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003896
Hayes Wang7e18dca2012-03-30 14:33:02 +08003897 case RTL_GIGA_MAC_VER_37:
3898 rtl8402_hw_phy_config(tp);
3899 break;
3900
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003901 case RTL_GIGA_MAC_VER_38:
3902 rtl8411_hw_phy_config(tp);
3903 break;
3904
Hayes Wang5598bfe2012-07-02 17:23:21 +08003905 case RTL_GIGA_MAC_VER_39:
3906 rtl8106e_hw_phy_config(tp);
3907 break;
3908
Hayes Wangc5583862012-07-02 17:23:22 +08003909 case RTL_GIGA_MAC_VER_40:
3910 rtl8168g_1_hw_phy_config(tp);
3911 break;
hayeswang57538c42013-04-01 22:23:40 +00003912 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003913 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003914 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00003915 rtl8168g_2_hw_phy_config(tp);
3916 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003917 case RTL_GIGA_MAC_VER_45:
3918 case RTL_GIGA_MAC_VER_47:
3919 rtl8168h_1_hw_phy_config(tp);
3920 break;
3921 case RTL_GIGA_MAC_VER_46:
3922 case RTL_GIGA_MAC_VER_48:
3923 rtl8168h_2_hw_phy_config(tp);
3924 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003925
3926 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003927 default:
3928 break;
3929 }
3930}
3931
Francois Romieuda78dbf2012-01-26 14:18:23 +01003932static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 struct timer_list *timer = &tp->timer;
3935 void __iomem *ioaddr = tp->mmio_addr;
3936 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3937
Francois Romieubcf0bf92006-07-26 23:14:13 +02003938 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
françois romieu4da19632011-01-03 15:07:55 +00003940 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003941 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 * A busy loop could burn quite a few cycles on nowadays CPU.
3943 * Let's delay the execution of the timer for a few ticks.
3944 */
3945 timeout = HZ/10;
3946 goto out_mod_timer;
3947 }
3948
3949 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003950 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02003952 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
françois romieu4da19632011-01-03 15:07:55 +00003954 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956out_mod_timer:
3957 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003958}
3959
3960static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3961{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003962 if (!test_and_set_bit(flag, tp->wk.flags))
3963 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003964}
3965
3966static void rtl8169_phy_timer(unsigned long __opaque)
3967{
3968 struct net_device *dev = (struct net_device *)__opaque;
3969 struct rtl8169_private *tp = netdev_priv(dev);
3970
Francois Romieu98ddf982012-01-31 10:47:34 +01003971 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3975 void __iomem *ioaddr)
3976{
3977 iounmap(ioaddr);
3978 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003979 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 pci_disable_device(pdev);
3981 free_netdev(dev);
3982}
3983
Francois Romieuffc46952012-07-06 14:19:23 +02003984DECLARE_RTL_COND(rtl_phy_reset_cond)
3985{
3986 return tp->phy_reset_pending(tp);
3987}
3988
Francois Romieubf793292006-11-01 00:53:05 +01003989static void rtl8169_phy_reset(struct net_device *dev,
3990 struct rtl8169_private *tp)
3991{
françois romieu4da19632011-01-03 15:07:55 +00003992 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003993 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003994}
3995
David S. Miller8decf862011-09-22 03:23:13 -04003996static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3997{
3998 void __iomem *ioaddr = tp->mmio_addr;
3999
4000 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4001 (RTL_R8(PHYstatus) & TBI_Enable);
4002}
4003
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004004static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004006 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004007
Francois Romieu5615d9f2007-08-17 17:50:46 +02004008 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004009
Marcus Sundberg773328942008-07-10 21:28:08 +02004010 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4011 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4012 RTL_W8(0x82, 0x01);
4013 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004014
Francois Romieu6dccd162007-02-13 23:38:05 +01004015 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4016
4017 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4018 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004019
Francois Romieubcf0bf92006-07-26 23:14:13 +02004020 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004021 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4022 RTL_W8(0x82, 0x01);
4023 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004024 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004025 }
4026
Francois Romieubf793292006-11-01 00:53:05 +01004027 rtl8169_phy_reset(dev, tp);
4028
Oliver Neukum54405cd2011-01-06 21:55:13 +01004029 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02004030 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4031 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4032 (tp->mii.supports_gmii ?
4033 ADVERTISED_1000baseT_Half |
4034 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004035
David S. Miller8decf862011-09-22 03:23:13 -04004036 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00004037 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004038}
4039
Francois Romieu773d2022007-01-31 23:47:43 +01004040static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4041{
4042 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01004043
Francois Romieuda78dbf2012-01-26 14:18:23 +01004044 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004045
4046 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00004047
françois romieu9ecb9aa2012-12-07 11:20:21 +00004048 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00004049 RTL_R32(MAC4);
4050
françois romieu9ecb9aa2012-12-07 11:20:21 +00004051 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00004052 RTL_R32(MAC0);
4053
françois romieu9ecb9aa2012-12-07 11:20:21 +00004054 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4055 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004056
Francois Romieu773d2022007-01-31 23:47:43 +01004057 RTL_W8(Cfg9346, Cfg9346_Lock);
4058
Francois Romieuda78dbf2012-01-26 14:18:23 +01004059 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004060}
4061
4062static int rtl_set_mac_address(struct net_device *dev, void *p)
4063{
4064 struct rtl8169_private *tp = netdev_priv(dev);
4065 struct sockaddr *addr = p;
4066
4067 if (!is_valid_ether_addr(addr->sa_data))
4068 return -EADDRNOTAVAIL;
4069
4070 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4071
4072 rtl_rar_set(tp, dev->dev_addr);
4073
4074 return 0;
4075}
4076
Francois Romieu5f787a12006-08-17 13:02:36 +02004077static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4078{
4079 struct rtl8169_private *tp = netdev_priv(dev);
4080 struct mii_ioctl_data *data = if_mii(ifr);
4081
Francois Romieu8b4ab282008-11-19 22:05:25 -08004082 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4083}
Francois Romieu5f787a12006-08-17 13:02:36 +02004084
Francois Romieucecb5fd2011-04-01 10:21:07 +02004085static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4086 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004087{
Francois Romieu5f787a12006-08-17 13:02:36 +02004088 switch (cmd) {
4089 case SIOCGMIIPHY:
4090 data->phy_id = 32; /* Internal PHY */
4091 return 0;
4092
4093 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004094 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02004095 return 0;
4096
4097 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00004098 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02004099 return 0;
4100 }
4101 return -EOPNOTSUPP;
4102}
4103
Francois Romieu8b4ab282008-11-19 22:05:25 -08004104static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4105{
4106 return -EOPNOTSUPP;
4107}
4108
Francois Romieufbac58f2007-10-04 22:51:38 +02004109static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
4110{
4111 if (tp->features & RTL_FEATURE_MSI) {
4112 pci_disable_msi(pdev);
4113 tp->features &= ~RTL_FEATURE_MSI;
4114 }
4115}
4116
Bill Pembertonbaf63292012-12-03 09:23:28 -05004117static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004118{
4119 struct mdio_ops *ops = &tp->mdio_ops;
4120
4121 switch (tp->mac_version) {
4122 case RTL_GIGA_MAC_VER_27:
4123 ops->write = r8168dp_1_mdio_write;
4124 ops->read = r8168dp_1_mdio_read;
4125 break;
françois romieue6de30d2011-01-03 15:08:37 +00004126 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004127 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004128 ops->write = r8168dp_2_mdio_write;
4129 ops->read = r8168dp_2_mdio_read;
4130 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004131 case RTL_GIGA_MAC_VER_40:
4132 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004133 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004134 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004135 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004136 case RTL_GIGA_MAC_VER_45:
4137 case RTL_GIGA_MAC_VER_46:
4138 case RTL_GIGA_MAC_VER_47:
4139 case RTL_GIGA_MAC_VER_48:
Hayes Wangc5583862012-07-02 17:23:22 +08004140 ops->write = r8168g_mdio_write;
4141 ops->read = r8168g_mdio_read;
4142 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004143 default:
4144 ops->write = r8169_mdio_write;
4145 ops->read = r8169_mdio_read;
4146 break;
4147 }
4148}
4149
hayeswange2409d82013-03-31 17:02:04 +00004150static void rtl_speed_down(struct rtl8169_private *tp)
4151{
4152 u32 adv;
4153 int lpa;
4154
4155 rtl_writephy(tp, 0x1f, 0x0000);
4156 lpa = rtl_readphy(tp, MII_LPA);
4157
4158 if (lpa & (LPA_10HALF | LPA_10FULL))
4159 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4160 else if (lpa & (LPA_100HALF | LPA_100FULL))
4161 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4162 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4163 else
4164 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4165 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4166 (tp->mii.supports_gmii ?
4167 ADVERTISED_1000baseT_Half |
4168 ADVERTISED_1000baseT_Full : 0);
4169
4170 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4171 adv);
4172}
4173
David S. Miller1805b2f2011-10-24 18:18:09 -04004174static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4175{
4176 void __iomem *ioaddr = tp->mmio_addr;
4177
4178 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004179 case RTL_GIGA_MAC_VER_25:
4180 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004181 case RTL_GIGA_MAC_VER_29:
4182 case RTL_GIGA_MAC_VER_30:
4183 case RTL_GIGA_MAC_VER_32:
4184 case RTL_GIGA_MAC_VER_33:
4185 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004186 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004187 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004188 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08004189 case RTL_GIGA_MAC_VER_40:
4190 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004191 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004192 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004193 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004194 case RTL_GIGA_MAC_VER_45:
4195 case RTL_GIGA_MAC_VER_46:
4196 case RTL_GIGA_MAC_VER_47:
4197 case RTL_GIGA_MAC_VER_48:
David S. Miller1805b2f2011-10-24 18:18:09 -04004198 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4199 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4200 break;
4201 default:
4202 break;
4203 }
4204}
4205
4206static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4207{
4208 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4209 return false;
4210
hayeswange2409d82013-03-31 17:02:04 +00004211 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04004212 rtl_wol_suspend_quirk(tp);
4213
4214 return true;
4215}
4216
françois romieu065c27c2011-01-03 15:08:12 +00004217static void r810x_phy_power_down(struct rtl8169_private *tp)
4218{
4219 rtl_writephy(tp, 0x1f, 0x0000);
4220 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4221}
4222
4223static void r810x_phy_power_up(struct rtl8169_private *tp)
4224{
4225 rtl_writephy(tp, 0x1f, 0x0000);
4226 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4227}
4228
4229static void r810x_pll_power_down(struct rtl8169_private *tp)
4230{
Hayes Wang00042992012-03-30 14:33:00 +08004231 void __iomem *ioaddr = tp->mmio_addr;
4232
David S. Miller1805b2f2011-10-24 18:18:09 -04004233 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004234 return;
françois romieu065c27c2011-01-03 15:08:12 +00004235
4236 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004237
4238 switch (tp->mac_version) {
4239 case RTL_GIGA_MAC_VER_07:
4240 case RTL_GIGA_MAC_VER_08:
4241 case RTL_GIGA_MAC_VER_09:
4242 case RTL_GIGA_MAC_VER_10:
4243 case RTL_GIGA_MAC_VER_13:
4244 case RTL_GIGA_MAC_VER_16:
4245 break;
4246 default:
4247 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4248 break;
4249 }
françois romieu065c27c2011-01-03 15:08:12 +00004250}
4251
4252static void r810x_pll_power_up(struct rtl8169_private *tp)
4253{
Hayes Wang00042992012-03-30 14:33:00 +08004254 void __iomem *ioaddr = tp->mmio_addr;
4255
françois romieu065c27c2011-01-03 15:08:12 +00004256 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08004257
4258 switch (tp->mac_version) {
4259 case RTL_GIGA_MAC_VER_07:
4260 case RTL_GIGA_MAC_VER_08:
4261 case RTL_GIGA_MAC_VER_09:
4262 case RTL_GIGA_MAC_VER_10:
4263 case RTL_GIGA_MAC_VER_13:
4264 case RTL_GIGA_MAC_VER_16:
4265 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004266 case RTL_GIGA_MAC_VER_47:
4267 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004268 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004269 break;
Hayes Wang00042992012-03-30 14:33:00 +08004270 default:
4271 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4272 break;
4273 }
françois romieu065c27c2011-01-03 15:08:12 +00004274}
4275
4276static void r8168_phy_power_up(struct rtl8169_private *tp)
4277{
4278 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004279 switch (tp->mac_version) {
4280 case RTL_GIGA_MAC_VER_11:
4281 case RTL_GIGA_MAC_VER_12:
4282 case RTL_GIGA_MAC_VER_17:
4283 case RTL_GIGA_MAC_VER_18:
4284 case RTL_GIGA_MAC_VER_19:
4285 case RTL_GIGA_MAC_VER_20:
4286 case RTL_GIGA_MAC_VER_21:
4287 case RTL_GIGA_MAC_VER_22:
4288 case RTL_GIGA_MAC_VER_23:
4289 case RTL_GIGA_MAC_VER_24:
4290 case RTL_GIGA_MAC_VER_25:
4291 case RTL_GIGA_MAC_VER_26:
4292 case RTL_GIGA_MAC_VER_27:
4293 case RTL_GIGA_MAC_VER_28:
4294 case RTL_GIGA_MAC_VER_31:
4295 rtl_writephy(tp, 0x0e, 0x0000);
4296 break;
4297 default:
4298 break;
4299 }
françois romieu065c27c2011-01-03 15:08:12 +00004300 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4301}
4302
4303static void r8168_phy_power_down(struct rtl8169_private *tp)
4304{
4305 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004306 switch (tp->mac_version) {
4307 case RTL_GIGA_MAC_VER_32:
4308 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004309 case RTL_GIGA_MAC_VER_40:
4310 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004311 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4312 break;
4313
4314 case RTL_GIGA_MAC_VER_11:
4315 case RTL_GIGA_MAC_VER_12:
4316 case RTL_GIGA_MAC_VER_17:
4317 case RTL_GIGA_MAC_VER_18:
4318 case RTL_GIGA_MAC_VER_19:
4319 case RTL_GIGA_MAC_VER_20:
4320 case RTL_GIGA_MAC_VER_21:
4321 case RTL_GIGA_MAC_VER_22:
4322 case RTL_GIGA_MAC_VER_23:
4323 case RTL_GIGA_MAC_VER_24:
4324 case RTL_GIGA_MAC_VER_25:
4325 case RTL_GIGA_MAC_VER_26:
4326 case RTL_GIGA_MAC_VER_27:
4327 case RTL_GIGA_MAC_VER_28:
4328 case RTL_GIGA_MAC_VER_31:
4329 rtl_writephy(tp, 0x0e, 0x0200);
4330 default:
4331 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4332 break;
4333 }
françois romieu065c27c2011-01-03 15:08:12 +00004334}
4335
4336static void r8168_pll_power_down(struct rtl8169_private *tp)
4337{
4338 void __iomem *ioaddr = tp->mmio_addr;
4339
Francois Romieucecb5fd2011-04-01 10:21:07 +02004340 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4341 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4342 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00004343 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004344 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004345 }
françois romieu065c27c2011-01-03 15:08:12 +00004346
Francois Romieucecb5fd2011-04-01 10:21:07 +02004347 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4348 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004349 (RTL_R16(CPlusCmd) & ASF)) {
4350 return;
4351 }
4352
hayeswang01dc7fe2011-03-21 01:50:28 +00004353 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4354 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004355 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004356
David S. Miller1805b2f2011-10-24 18:18:09 -04004357 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004358 return;
françois romieu065c27c2011-01-03 15:08:12 +00004359
4360 r8168_phy_power_down(tp);
4361
4362 switch (tp->mac_version) {
4363 case RTL_GIGA_MAC_VER_25:
4364 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004365 case RTL_GIGA_MAC_VER_27:
4366 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004367 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004368 case RTL_GIGA_MAC_VER_32:
4369 case RTL_GIGA_MAC_VER_33:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004370 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004371 case RTL_GIGA_MAC_VER_45:
4372 case RTL_GIGA_MAC_VER_46:
françois romieu065c27c2011-01-03 15:08:12 +00004373 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4374 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004375 case RTL_GIGA_MAC_VER_40:
4376 case RTL_GIGA_MAC_VER_41:
4377 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4378 0xfc000000, ERIAR_EXGMAC);
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004379 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004380 break;
françois romieu065c27c2011-01-03 15:08:12 +00004381 }
4382}
4383
4384static void r8168_pll_power_up(struct rtl8169_private *tp)
4385{
4386 void __iomem *ioaddr = tp->mmio_addr;
4387
françois romieu065c27c2011-01-03 15:08:12 +00004388 switch (tp->mac_version) {
4389 case RTL_GIGA_MAC_VER_25:
4390 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004391 case RTL_GIGA_MAC_VER_27:
4392 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004393 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004394 case RTL_GIGA_MAC_VER_32:
4395 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004396 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4397 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004398 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004399 case RTL_GIGA_MAC_VER_45:
4400 case RTL_GIGA_MAC_VER_46:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004401 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004402 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004403 case RTL_GIGA_MAC_VER_40:
4404 case RTL_GIGA_MAC_VER_41:
Chun-Hao Linb8e5e6a2014-10-01 23:17:13 +08004405 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
hayeswangbeb330a2013-04-01 22:23:39 +00004406 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4407 0x00000000, ERIAR_EXGMAC);
4408 break;
françois romieu065c27c2011-01-03 15:08:12 +00004409 }
4410
4411 r8168_phy_power_up(tp);
4412}
4413
Francois Romieud58d46b2011-05-03 16:38:29 +02004414static void rtl_generic_op(struct rtl8169_private *tp,
4415 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004416{
4417 if (op)
4418 op(tp);
4419}
4420
4421static void rtl_pll_power_down(struct rtl8169_private *tp)
4422{
Francois Romieud58d46b2011-05-03 16:38:29 +02004423 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004424}
4425
4426static void rtl_pll_power_up(struct rtl8169_private *tp)
4427{
Francois Romieud58d46b2011-05-03 16:38:29 +02004428 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004429}
4430
Bill Pembertonbaf63292012-12-03 09:23:28 -05004431static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004432{
4433 struct pll_power_ops *ops = &tp->pll_power_ops;
4434
4435 switch (tp->mac_version) {
4436 case RTL_GIGA_MAC_VER_07:
4437 case RTL_GIGA_MAC_VER_08:
4438 case RTL_GIGA_MAC_VER_09:
4439 case RTL_GIGA_MAC_VER_10:
4440 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004441 case RTL_GIGA_MAC_VER_29:
4442 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004443 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004444 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004445 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004446 case RTL_GIGA_MAC_VER_47:
4447 case RTL_GIGA_MAC_VER_48:
françois romieu065c27c2011-01-03 15:08:12 +00004448 ops->down = r810x_pll_power_down;
4449 ops->up = r810x_pll_power_up;
4450 break;
4451
4452 case RTL_GIGA_MAC_VER_11:
4453 case RTL_GIGA_MAC_VER_12:
4454 case RTL_GIGA_MAC_VER_17:
4455 case RTL_GIGA_MAC_VER_18:
4456 case RTL_GIGA_MAC_VER_19:
4457 case RTL_GIGA_MAC_VER_20:
4458 case RTL_GIGA_MAC_VER_21:
4459 case RTL_GIGA_MAC_VER_22:
4460 case RTL_GIGA_MAC_VER_23:
4461 case RTL_GIGA_MAC_VER_24:
4462 case RTL_GIGA_MAC_VER_25:
4463 case RTL_GIGA_MAC_VER_26:
4464 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004465 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004466 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004467 case RTL_GIGA_MAC_VER_32:
4468 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004469 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004470 case RTL_GIGA_MAC_VER_35:
4471 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004472 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004473 case RTL_GIGA_MAC_VER_40:
4474 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004475 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004476 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004477 case RTL_GIGA_MAC_VER_45:
4478 case RTL_GIGA_MAC_VER_46:
françois romieu065c27c2011-01-03 15:08:12 +00004479 ops->down = r8168_pll_power_down;
4480 ops->up = r8168_pll_power_up;
4481 break;
4482
4483 default:
4484 ops->down = NULL;
4485 ops->up = NULL;
4486 break;
4487 }
4488}
4489
Hayes Wange542a222011-07-06 15:58:04 +08004490static void rtl_init_rxcfg(struct rtl8169_private *tp)
4491{
4492 void __iomem *ioaddr = tp->mmio_addr;
4493
4494 switch (tp->mac_version) {
4495 case RTL_GIGA_MAC_VER_01:
4496 case RTL_GIGA_MAC_VER_02:
4497 case RTL_GIGA_MAC_VER_03:
4498 case RTL_GIGA_MAC_VER_04:
4499 case RTL_GIGA_MAC_VER_05:
4500 case RTL_GIGA_MAC_VER_06:
4501 case RTL_GIGA_MAC_VER_10:
4502 case RTL_GIGA_MAC_VER_11:
4503 case RTL_GIGA_MAC_VER_12:
4504 case RTL_GIGA_MAC_VER_13:
4505 case RTL_GIGA_MAC_VER_14:
4506 case RTL_GIGA_MAC_VER_15:
4507 case RTL_GIGA_MAC_VER_16:
4508 case RTL_GIGA_MAC_VER_17:
4509 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4510 break;
4511 case RTL_GIGA_MAC_VER_18:
4512 case RTL_GIGA_MAC_VER_19:
4513 case RTL_GIGA_MAC_VER_20:
4514 case RTL_GIGA_MAC_VER_21:
4515 case RTL_GIGA_MAC_VER_22:
4516 case RTL_GIGA_MAC_VER_23:
4517 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004518 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004519 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004520 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4521 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004522 case RTL_GIGA_MAC_VER_40:
Michel Dänzer7a9810e2014-07-17 12:55:40 +09004523 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4524 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004525 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004526 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004527 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004528 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004529 case RTL_GIGA_MAC_VER_45:
4530 case RTL_GIGA_MAC_VER_46:
4531 case RTL_GIGA_MAC_VER_47:
4532 case RTL_GIGA_MAC_VER_48:
hayeswangbeb330a2013-04-01 22:23:39 +00004533 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4534 break;
Hayes Wange542a222011-07-06 15:58:04 +08004535 default:
4536 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4537 break;
4538 }
4539}
4540
Hayes Wang92fc43b2011-07-06 15:58:03 +08004541static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4542{
Timo Teräs9fba0812013-01-15 21:01:24 +00004543 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004544}
4545
Francois Romieud58d46b2011-05-03 16:38:29 +02004546static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4547{
françois romieu9c5028e2012-03-02 04:43:14 +00004548 void __iomem *ioaddr = tp->mmio_addr;
4549
4550 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004551 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004552 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004553}
4554
4555static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4556{
françois romieu9c5028e2012-03-02 04:43:14 +00004557 void __iomem *ioaddr = tp->mmio_addr;
4558
4559 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004560 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004561 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004562}
4563
4564static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4565{
4566 void __iomem *ioaddr = tp->mmio_addr;
4567
4568 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4569 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4570 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4571}
4572
4573static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4574{
4575 void __iomem *ioaddr = tp->mmio_addr;
4576
4577 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4578 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4579 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4580}
4581
4582static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4583{
4584 void __iomem *ioaddr = tp->mmio_addr;
4585
4586 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4587}
4588
4589static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4590{
4591 void __iomem *ioaddr = tp->mmio_addr;
4592
4593 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4594}
4595
4596static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4597{
4598 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004599
4600 RTL_W8(MaxTxPacketSize, 0x3f);
4601 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4602 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004603 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004604}
4605
4606static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4607{
4608 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004609
4610 RTL_W8(MaxTxPacketSize, 0x0c);
4611 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4612 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004613 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004614}
4615
4616static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4617{
4618 rtl_tx_performance_tweak(tp->pci_dev,
4619 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4620}
4621
4622static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4623{
4624 rtl_tx_performance_tweak(tp->pci_dev,
4625 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4626}
4627
4628static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4629{
4630 void __iomem *ioaddr = tp->mmio_addr;
4631
4632 r8168b_0_hw_jumbo_enable(tp);
4633
4634 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4635}
4636
4637static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4638{
4639 void __iomem *ioaddr = tp->mmio_addr;
4640
4641 r8168b_0_hw_jumbo_disable(tp);
4642
4643 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4644}
4645
Bill Pembertonbaf63292012-12-03 09:23:28 -05004646static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004647{
4648 struct jumbo_ops *ops = &tp->jumbo_ops;
4649
4650 switch (tp->mac_version) {
4651 case RTL_GIGA_MAC_VER_11:
4652 ops->disable = r8168b_0_hw_jumbo_disable;
4653 ops->enable = r8168b_0_hw_jumbo_enable;
4654 break;
4655 case RTL_GIGA_MAC_VER_12:
4656 case RTL_GIGA_MAC_VER_17:
4657 ops->disable = r8168b_1_hw_jumbo_disable;
4658 ops->enable = r8168b_1_hw_jumbo_enable;
4659 break;
4660 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4661 case RTL_GIGA_MAC_VER_19:
4662 case RTL_GIGA_MAC_VER_20:
4663 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4664 case RTL_GIGA_MAC_VER_22:
4665 case RTL_GIGA_MAC_VER_23:
4666 case RTL_GIGA_MAC_VER_24:
4667 case RTL_GIGA_MAC_VER_25:
4668 case RTL_GIGA_MAC_VER_26:
4669 ops->disable = r8168c_hw_jumbo_disable;
4670 ops->enable = r8168c_hw_jumbo_enable;
4671 break;
4672 case RTL_GIGA_MAC_VER_27:
4673 case RTL_GIGA_MAC_VER_28:
4674 ops->disable = r8168dp_hw_jumbo_disable;
4675 ops->enable = r8168dp_hw_jumbo_enable;
4676 break;
4677 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4678 case RTL_GIGA_MAC_VER_32:
4679 case RTL_GIGA_MAC_VER_33:
4680 case RTL_GIGA_MAC_VER_34:
4681 ops->disable = r8168e_hw_jumbo_disable;
4682 ops->enable = r8168e_hw_jumbo_enable;
4683 break;
4684
4685 /*
4686 * No action needed for jumbo frames with 8169.
4687 * No jumbo for 810x at all.
4688 */
Hayes Wangc5583862012-07-02 17:23:22 +08004689 case RTL_GIGA_MAC_VER_40:
4690 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004691 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004692 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004693 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004694 case RTL_GIGA_MAC_VER_45:
4695 case RTL_GIGA_MAC_VER_46:
4696 case RTL_GIGA_MAC_VER_47:
4697 case RTL_GIGA_MAC_VER_48:
Francois Romieud58d46b2011-05-03 16:38:29 +02004698 default:
4699 ops->disable = NULL;
4700 ops->enable = NULL;
4701 break;
4702 }
4703}
4704
Francois Romieuffc46952012-07-06 14:19:23 +02004705DECLARE_RTL_COND(rtl_chipcmd_cond)
4706{
4707 void __iomem *ioaddr = tp->mmio_addr;
4708
4709 return RTL_R8(ChipCmd) & CmdReset;
4710}
4711
Francois Romieu6f43adc2011-04-29 15:05:51 +02004712static void rtl_hw_reset(struct rtl8169_private *tp)
4713{
4714 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004715
Francois Romieu6f43adc2011-04-29 15:05:51 +02004716 RTL_W8(ChipCmd, CmdReset);
4717
Francois Romieuffc46952012-07-06 14:19:23 +02004718 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004719}
4720
Francois Romieub6ffd972011-06-17 17:00:05 +02004721static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4722{
4723 struct rtl_fw *rtl_fw;
4724 const char *name;
4725 int rc = -ENOMEM;
4726
4727 name = rtl_lookup_firmware_name(tp);
4728 if (!name)
4729 goto out_no_firmware;
4730
4731 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4732 if (!rtl_fw)
4733 goto err_warn;
4734
4735 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4736 if (rc < 0)
4737 goto err_free;
4738
Francois Romieufd112f22011-06-18 00:10:29 +02004739 rc = rtl_check_firmware(tp, rtl_fw);
4740 if (rc < 0)
4741 goto err_release_firmware;
4742
Francois Romieub6ffd972011-06-17 17:00:05 +02004743 tp->rtl_fw = rtl_fw;
4744out:
4745 return;
4746
Francois Romieufd112f22011-06-18 00:10:29 +02004747err_release_firmware:
4748 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004749err_free:
4750 kfree(rtl_fw);
4751err_warn:
4752 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4753 name, rc);
4754out_no_firmware:
4755 tp->rtl_fw = NULL;
4756 goto out;
4757}
4758
François Romieu953a12c2011-04-24 17:38:48 +02004759static void rtl_request_firmware(struct rtl8169_private *tp)
4760{
Francois Romieub6ffd972011-06-17 17:00:05 +02004761 if (IS_ERR(tp->rtl_fw))
4762 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004763}
4764
Hayes Wang92fc43b2011-07-06 15:58:03 +08004765static void rtl_rx_close(struct rtl8169_private *tp)
4766{
4767 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004768
Francois Romieu1687b562011-07-19 17:21:29 +02004769 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004770}
4771
Francois Romieuffc46952012-07-06 14:19:23 +02004772DECLARE_RTL_COND(rtl_npq_cond)
4773{
4774 void __iomem *ioaddr = tp->mmio_addr;
4775
4776 return RTL_R8(TxPoll) & NPQ;
4777}
4778
4779DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4780{
4781 void __iomem *ioaddr = tp->mmio_addr;
4782
4783 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4784}
4785
françois romieue6de30d2011-01-03 15:08:37 +00004786static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787{
françois romieue6de30d2011-01-03 15:08:37 +00004788 void __iomem *ioaddr = tp->mmio_addr;
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004791 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Hayes Wang92fc43b2011-07-06 15:58:03 +08004793 rtl_rx_close(tp);
4794
Hayes Wang5d2e1952011-02-22 17:26:22 +08004795 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004796 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4797 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004798 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004799 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004800 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4801 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
4802 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4803 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
4804 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4805 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
4806 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
4807 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
4808 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
4809 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
4810 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
4811 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
4812 tp->mac_version == RTL_GIGA_MAC_VER_48) {
David S. Miller8decf862011-09-22 03:23:13 -04004813 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004814 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004815 } else {
4816 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4817 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004818 }
4819
Hayes Wang92fc43b2011-07-06 15:58:03 +08004820 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Francois Romieu7f796d832007-06-11 23:04:41 +02004823static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004824{
4825 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004826
4827 /* Set DMA burst size and Interframe Gap Time */
4828 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4829 (InterFrameGap << TxInterFrameGapShift));
4830}
4831
Francois Romieu07ce4062007-02-23 23:36:39 +01004832static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
4834 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Francois Romieu07ce4062007-02-23 23:36:39 +01004836 tp->hw_start(dev);
4837
Francois Romieuda78dbf2012-01-26 14:18:23 +01004838 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004839}
4840
Francois Romieu7f796d832007-06-11 23:04:41 +02004841static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4842 void __iomem *ioaddr)
4843{
4844 /*
4845 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4846 * register to be written before TxDescAddrLow to work.
4847 * Switching from MMIO to I/O access fixes the issue as well.
4848 */
4849 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004850 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004851 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004852 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004853}
4854
4855static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4856{
4857 u16 cmd;
4858
4859 cmd = RTL_R16(CPlusCmd);
4860 RTL_W16(CPlusCmd, cmd);
4861 return cmd;
4862}
4863
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004864static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004865{
4866 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004867 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004868}
4869
Francois Romieu6dccd162007-02-13 23:38:05 +01004870static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4871{
Francois Romieu37441002011-06-17 22:58:54 +02004872 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004873 u32 mac_version;
4874 u32 clk;
4875 u32 val;
4876 } cfg2_info [] = {
4877 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4878 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4879 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4880 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004881 };
4882 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004883 unsigned int i;
4884 u32 clk;
4885
4886 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004887 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004888 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4889 RTL_W32(0x7c, p->val);
4890 break;
4891 }
4892 }
4893}
4894
Francois Romieue6b763e2012-03-08 09:35:39 +01004895static void rtl_set_rx_mode(struct net_device *dev)
4896{
4897 struct rtl8169_private *tp = netdev_priv(dev);
4898 void __iomem *ioaddr = tp->mmio_addr;
4899 u32 mc_filter[2]; /* Multicast hash filter */
4900 int rx_mode;
4901 u32 tmp = 0;
4902
4903 if (dev->flags & IFF_PROMISC) {
4904 /* Unconditionally log net taps. */
4905 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4906 rx_mode =
4907 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4908 AcceptAllPhys;
4909 mc_filter[1] = mc_filter[0] = 0xffffffff;
4910 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4911 (dev->flags & IFF_ALLMULTI)) {
4912 /* Too many to filter perfectly -- accept all multicasts. */
4913 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4914 mc_filter[1] = mc_filter[0] = 0xffffffff;
4915 } else {
4916 struct netdev_hw_addr *ha;
4917
4918 rx_mode = AcceptBroadcast | AcceptMyPhys;
4919 mc_filter[1] = mc_filter[0] = 0;
4920 netdev_for_each_mc_addr(ha, dev) {
4921 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4922 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4923 rx_mode |= AcceptMulticast;
4924 }
4925 }
4926
4927 if (dev->features & NETIF_F_RXALL)
4928 rx_mode |= (AcceptErr | AcceptRunt);
4929
4930 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4931
4932 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4933 u32 data = mc_filter[0];
4934
4935 mc_filter[0] = swab32(mc_filter[1]);
4936 mc_filter[1] = swab32(data);
4937 }
4938
Nathan Walp04817762012-11-01 12:08:47 +00004939 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4940 mc_filter[1] = mc_filter[0] = 0xffffffff;
4941
Francois Romieue6b763e2012-03-08 09:35:39 +01004942 RTL_W32(MAR0 + 4, mc_filter[1]);
4943 RTL_W32(MAR0 + 0, mc_filter[0]);
4944
4945 RTL_W32(RxConfig, tmp);
4946}
4947
Francois Romieu07ce4062007-02-23 23:36:39 +01004948static void rtl_hw_start_8169(struct net_device *dev)
4949{
4950 struct rtl8169_private *tp = netdev_priv(dev);
4951 void __iomem *ioaddr = tp->mmio_addr;
4952 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004953
Francois Romieu9cb427b2006-11-02 00:10:16 +01004954 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4955 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4956 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4957 }
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004960 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4961 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4962 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4963 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004964 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4965
Hayes Wange542a222011-07-06 15:58:04 +08004966 rtl_init_rxcfg(tp);
4967
françois romieuf0298f82011-01-03 15:07:42 +00004968 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004970 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
Francois Romieucecb5fd2011-04-01 10:21:07 +02004972 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4973 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4974 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4975 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004976 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Francois Romieu7f796d832007-06-11 23:04:41 +02004978 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004979
Francois Romieucecb5fd2011-04-01 10:21:07 +02004980 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4981 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Chun-Hao Lin05b96872014-10-01 23:17:12 +08004982 dprintk("Set MAC Reg C+CR Offset 0xe0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004984 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
4986
Francois Romieubcf0bf92006-07-26 23:14:13 +02004987 RTL_W16(CPlusCmd, tp->cp_cmd);
4988
Francois Romieu6dccd162007-02-13 23:38:05 +01004989 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 /*
4992 * Undocumented corner. Supposedly:
4993 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4994 */
4995 RTL_W16(IntrMitigate, 0x0000);
4996
Francois Romieu7f796d832007-06-11 23:04:41 +02004997 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004998
Francois Romieucecb5fd2011-04-01 10:21:07 +02004999 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5000 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5001 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5002 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02005003 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5004 rtl_set_rx_tx_config_registers(tp);
5005 }
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02005008
5009 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5010 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
5012 RTL_W32(RxMissed, 0);
5013
Francois Romieu07ce4062007-02-23 23:36:39 +01005014 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 /* no early-rx interrupts */
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005017 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005020static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5021{
5022 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02005023 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005024}
5025
5026static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5027{
Francois Romieu52989f02012-07-06 13:37:00 +02005028 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005029}
5030
5031static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02005032{
5033 u32 csi;
5034
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005035 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5036 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00005037}
5038
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005039static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005040{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005041 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00005042}
5043
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005044static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00005045{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005046 rtl_csi_access_enable(tp, 0x27000000);
5047}
5048
Francois Romieuffc46952012-07-06 14:19:23 +02005049DECLARE_RTL_COND(rtl_csiar_cond)
5050{
5051 void __iomem *ioaddr = tp->mmio_addr;
5052
5053 return RTL_R32(CSIAR) & CSIAR_FLAG;
5054}
5055
Francois Romieu52989f02012-07-06 13:37:00 +02005056static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005057{
Francois Romieu52989f02012-07-06 13:37:00 +02005058 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005059
5060 RTL_W32(CSIDR, value);
5061 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5062 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5063
Francois Romieuffc46952012-07-06 14:19:23 +02005064 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005065}
5066
Francois Romieu52989f02012-07-06 13:37:00 +02005067static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005068{
Francois Romieu52989f02012-07-06 13:37:00 +02005069 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005070
5071 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5072 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5073
Francois Romieuffc46952012-07-06 14:19:23 +02005074 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5075 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005076}
5077
Francois Romieu52989f02012-07-06 13:37:00 +02005078static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005079{
Francois Romieu52989f02012-07-06 13:37:00 +02005080 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005081
5082 RTL_W32(CSIDR, value);
5083 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5084 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5085 CSIAR_FUNC_NIC);
5086
Francois Romieuffc46952012-07-06 14:19:23 +02005087 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005088}
5089
Francois Romieu52989f02012-07-06 13:37:00 +02005090static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08005091{
Francois Romieu52989f02012-07-06 13:37:00 +02005092 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005093
5094 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5095 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5096
Francois Romieuffc46952012-07-06 14:19:23 +02005097 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5098 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005099}
5100
hayeswang45dd95c2013-07-08 17:09:01 +08005101static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5102{
5103 void __iomem *ioaddr = tp->mmio_addr;
5104
5105 RTL_W32(CSIDR, value);
5106 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5107 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5108 CSIAR_FUNC_NIC2);
5109
5110 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5111}
5112
5113static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5114{
5115 void __iomem *ioaddr = tp->mmio_addr;
5116
5117 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5118 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5119
5120 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5121 RTL_R32(CSIDR) : ~0;
5122}
5123
Bill Pembertonbaf63292012-12-03 09:23:28 -05005124static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005125{
5126 struct csi_ops *ops = &tp->csi_ops;
5127
5128 switch (tp->mac_version) {
5129 case RTL_GIGA_MAC_VER_01:
5130 case RTL_GIGA_MAC_VER_02:
5131 case RTL_GIGA_MAC_VER_03:
5132 case RTL_GIGA_MAC_VER_04:
5133 case RTL_GIGA_MAC_VER_05:
5134 case RTL_GIGA_MAC_VER_06:
5135 case RTL_GIGA_MAC_VER_10:
5136 case RTL_GIGA_MAC_VER_11:
5137 case RTL_GIGA_MAC_VER_12:
5138 case RTL_GIGA_MAC_VER_13:
5139 case RTL_GIGA_MAC_VER_14:
5140 case RTL_GIGA_MAC_VER_15:
5141 case RTL_GIGA_MAC_VER_16:
5142 case RTL_GIGA_MAC_VER_17:
5143 ops->write = NULL;
5144 ops->read = NULL;
5145 break;
5146
Hayes Wang7e18dca2012-03-30 14:33:02 +08005147 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005148 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08005149 ops->write = r8402_csi_write;
5150 ops->read = r8402_csi_read;
5151 break;
5152
hayeswang45dd95c2013-07-08 17:09:01 +08005153 case RTL_GIGA_MAC_VER_44:
5154 ops->write = r8411_csi_write;
5155 ops->read = r8411_csi_read;
5156 break;
5157
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005158 default:
5159 ops->write = r8169_csi_write;
5160 ops->read = r8169_csi_read;
5161 break;
5162 }
Francois Romieudacf8152008-08-02 20:44:13 +02005163}
5164
5165struct ephy_info {
5166 unsigned int offset;
5167 u16 mask;
5168 u16 bits;
5169};
5170
Francois Romieufdf6fc02012-07-06 22:40:38 +02005171static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5172 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02005173{
5174 u16 w;
5175
5176 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02005177 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5178 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02005179 e++;
5180 }
5181}
5182
Francois Romieub726e492008-06-28 12:22:59 +02005183static void rtl_disable_clock_request(struct pci_dev *pdev)
5184{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005185 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5186 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02005187}
5188
françois romieue6de30d2011-01-03 15:08:37 +00005189static void rtl_enable_clock_request(struct pci_dev *pdev)
5190{
Jiang Liu7d7903b2012-07-24 17:20:16 +08005191 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5192 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00005193}
5194
hayeswangb51ecea2014-07-09 14:52:51 +08005195static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5196{
5197 void __iomem *ioaddr = tp->mmio_addr;
5198 u8 data;
5199
5200 data = RTL_R8(Config3);
5201
5202 if (enable)
5203 data |= Rdy_to_L23;
5204 else
5205 data &= ~Rdy_to_L23;
5206
5207 RTL_W8(Config3, data);
5208}
5209
Francois Romieub726e492008-06-28 12:22:59 +02005210#define R8168_CPCMD_QUIRK_MASK (\
5211 EnableBist | \
5212 Mac_dbgo_oe | \
5213 Force_half_dup | \
5214 Force_rxflow_en | \
5215 Force_txflow_en | \
5216 Cxpl_dbg_sel | \
5217 ASF | \
5218 PktCntrDisable | \
5219 Mac_dbgo_sel)
5220
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005221static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005222{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005223 void __iomem *ioaddr = tp->mmio_addr;
5224 struct pci_dev *pdev = tp->pci_dev;
5225
Francois Romieub726e492008-06-28 12:22:59 +02005226 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5227
5228 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5229
françois romieufaf1e782013-02-27 13:01:57 +00005230 if (tp->dev->mtu <= ETH_DATA_LEN) {
5231 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5232 PCI_EXP_DEVCTL_NOSNOOP_EN);
5233 }
Francois Romieu219a1e92008-06-28 11:58:39 +02005234}
5235
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005236static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005237{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005238 void __iomem *ioaddr = tp->mmio_addr;
5239
5240 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005241
françois romieuf0298f82011-01-03 15:07:42 +00005242 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02005243
5244 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02005245}
5246
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005247static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005248{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005249 void __iomem *ioaddr = tp->mmio_addr;
5250 struct pci_dev *pdev = tp->pci_dev;
5251
Francois Romieub726e492008-06-28 12:22:59 +02005252 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5253
5254 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5255
françois romieufaf1e782013-02-27 13:01:57 +00005256 if (tp->dev->mtu <= ETH_DATA_LEN)
5257 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02005258
5259 rtl_disable_clock_request(pdev);
5260
5261 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02005262}
5263
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005264static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005265{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005266 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005267 { 0x01, 0, 0x0001 },
5268 { 0x02, 0x0800, 0x1000 },
5269 { 0x03, 0, 0x0042 },
5270 { 0x06, 0x0080, 0x0000 },
5271 { 0x07, 0, 0x2000 }
5272 };
5273
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005274 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005275
Francois Romieufdf6fc02012-07-06 22:40:38 +02005276 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02005277
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005278 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005279}
5280
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005281static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02005282{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005283 void __iomem *ioaddr = tp->mmio_addr;
5284 struct pci_dev *pdev = tp->pci_dev;
5285
5286 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02005287
5288 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5289
françois romieufaf1e782013-02-27 13:01:57 +00005290 if (tp->dev->mtu <= ETH_DATA_LEN)
5291 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02005292
5293 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5294}
5295
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005296static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005297{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005298 void __iomem *ioaddr = tp->mmio_addr;
5299 struct pci_dev *pdev = tp->pci_dev;
5300
5301 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005302
5303 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5304
5305 /* Magic. */
5306 RTL_W8(DBG_REG, 0x20);
5307
françois romieuf0298f82011-01-03 15:07:42 +00005308 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005309
françois romieufaf1e782013-02-27 13:01:57 +00005310 if (tp->dev->mtu <= ETH_DATA_LEN)
5311 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005312
5313 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5314}
5315
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005316static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005317{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005318 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005319 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005320 { 0x02, 0x0800, 0x1000 },
5321 { 0x03, 0, 0x0002 },
5322 { 0x06, 0x0080, 0x0000 }
5323 };
5324
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005325 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005326
5327 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5328
Francois Romieufdf6fc02012-07-06 22:40:38 +02005329 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005330
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005331 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005332}
5333
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005334static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005335{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005336 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005337 { 0x01, 0, 0x0001 },
5338 { 0x03, 0x0400, 0x0220 }
5339 };
5340
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005341 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005342
Francois Romieufdf6fc02012-07-06 22:40:38 +02005343 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005344
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005345 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005346}
5347
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005348static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005349{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005350 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005351}
5352
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005353static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005354{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005355 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005356
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005357 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005358}
5359
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005360static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005361{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005362 void __iomem *ioaddr = tp->mmio_addr;
5363 struct pci_dev *pdev = tp->pci_dev;
5364
5365 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005366
5367 rtl_disable_clock_request(pdev);
5368
françois romieuf0298f82011-01-03 15:07:42 +00005369 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005370
françois romieufaf1e782013-02-27 13:01:57 +00005371 if (tp->dev->mtu <= ETH_DATA_LEN)
5372 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005373
5374 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5375}
5376
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005377static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005378{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005379 void __iomem *ioaddr = tp->mmio_addr;
5380 struct pci_dev *pdev = tp->pci_dev;
5381
5382 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005383
françois romieufaf1e782013-02-27 13:01:57 +00005384 if (tp->dev->mtu <= ETH_DATA_LEN)
5385 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005386
5387 RTL_W8(MaxTxPacketSize, TxPacketMax);
5388
5389 rtl_disable_clock_request(pdev);
5390}
5391
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005392static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005393{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005394 void __iomem *ioaddr = tp->mmio_addr;
5395 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005396 static const struct ephy_info e_info_8168d_4[] = {
5397 { 0x0b, ~0, 0x48 },
5398 { 0x19, 0x20, 0x50 },
5399 { 0x0c, ~0, 0x20 }
5400 };
5401 int i;
5402
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005403 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005404
5405 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5406
5407 RTL_W8(MaxTxPacketSize, TxPacketMax);
5408
5409 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5410 const struct ephy_info *e = e_info_8168d_4 + i;
5411 u16 w;
5412
Francois Romieufdf6fc02012-07-06 22:40:38 +02005413 w = rtl_ephy_read(tp, e->offset);
5414 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00005415 }
5416
5417 rtl_enable_clock_request(pdev);
5418}
5419
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005420static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005421{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005422 void __iomem *ioaddr = tp->mmio_addr;
5423 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005424 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005425 { 0x00, 0x0200, 0x0100 },
5426 { 0x00, 0x0000, 0x0004 },
5427 { 0x06, 0x0002, 0x0001 },
5428 { 0x06, 0x0000, 0x0030 },
5429 { 0x07, 0x0000, 0x2000 },
5430 { 0x00, 0x0000, 0x0020 },
5431 { 0x03, 0x5800, 0x2000 },
5432 { 0x03, 0x0000, 0x0001 },
5433 { 0x01, 0x0800, 0x1000 },
5434 { 0x07, 0x0000, 0x4000 },
5435 { 0x1e, 0x0000, 0x2000 },
5436 { 0x19, 0xffff, 0xfe6c },
5437 { 0x0a, 0x0000, 0x0040 }
5438 };
5439
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005440 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005441
Francois Romieufdf6fc02012-07-06 22:40:38 +02005442 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005443
françois romieufaf1e782013-02-27 13:01:57 +00005444 if (tp->dev->mtu <= ETH_DATA_LEN)
5445 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005446
5447 RTL_W8(MaxTxPacketSize, TxPacketMax);
5448
5449 rtl_disable_clock_request(pdev);
5450
5451 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005452 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5453 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005454
Francois Romieucecb5fd2011-04-01 10:21:07 +02005455 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005456}
5457
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005458static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005459{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005460 void __iomem *ioaddr = tp->mmio_addr;
5461 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005462 static const struct ephy_info e_info_8168e_2[] = {
5463 { 0x09, 0x0000, 0x0080 },
5464 { 0x19, 0x0000, 0x0224 }
5465 };
5466
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005467 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005468
Francois Romieufdf6fc02012-07-06 22:40:38 +02005469 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005470
françois romieufaf1e782013-02-27 13:01:57 +00005471 if (tp->dev->mtu <= ETH_DATA_LEN)
5472 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005473
Francois Romieufdf6fc02012-07-06 22:40:38 +02005474 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5475 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5476 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5477 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5478 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5479 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5480 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5481 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005482
Hayes Wang3090bd92011-09-06 16:55:15 +08005483 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005484
Francois Romieu4521e1a92012-11-01 16:46:28 +00005485 rtl_disable_clock_request(pdev);
5486
Hayes Wang70090422011-07-06 15:58:06 +08005487 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5488 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5489
5490 /* Adjust EEE LED frequency */
5491 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5492
5493 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5494 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005495 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005496}
5497
Hayes Wang5f886e02012-03-30 14:33:03 +08005498static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005499{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005500 void __iomem *ioaddr = tp->mmio_addr;
5501 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005502
Hayes Wang5f886e02012-03-30 14:33:03 +08005503 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005504
5505 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5506
Francois Romieufdf6fc02012-07-06 22:40:38 +02005507 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5508 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5509 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5510 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5511 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5512 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5513 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5514 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5515 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5516 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005517
5518 RTL_W8(MaxTxPacketSize, EarlySize);
5519
Francois Romieu4521e1a92012-11-01 16:46:28 +00005520 rtl_disable_clock_request(pdev);
5521
Hayes Wangc2218922011-09-06 16:55:18 +08005522 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5523 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005524 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005525 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5526 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005527}
5528
Hayes Wang5f886e02012-03-30 14:33:03 +08005529static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5530{
5531 void __iomem *ioaddr = tp->mmio_addr;
5532 static const struct ephy_info e_info_8168f_1[] = {
5533 { 0x06, 0x00c0, 0x0020 },
5534 { 0x08, 0x0001, 0x0002 },
5535 { 0x09, 0x0000, 0x0080 },
5536 { 0x19, 0x0000, 0x0224 }
5537 };
5538
5539 rtl_hw_start_8168f(tp);
5540
Francois Romieufdf6fc02012-07-06 22:40:38 +02005541 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005542
Francois Romieufdf6fc02012-07-06 22:40:38 +02005543 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005544
5545 /* Adjust EEE LED frequency */
5546 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5547}
5548
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005549static void rtl_hw_start_8411(struct rtl8169_private *tp)
5550{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005551 static const struct ephy_info e_info_8168f_1[] = {
5552 { 0x06, 0x00c0, 0x0020 },
5553 { 0x0f, 0xffff, 0x5200 },
5554 { 0x1e, 0x0000, 0x4000 },
5555 { 0x19, 0x0000, 0x0224 }
5556 };
5557
5558 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005559 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005560
Francois Romieufdf6fc02012-07-06 22:40:38 +02005561 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005562
Francois Romieufdf6fc02012-07-06 22:40:38 +02005563 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005564}
5565
Hayes Wangc5583862012-07-02 17:23:22 +08005566static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5567{
5568 void __iomem *ioaddr = tp->mmio_addr;
5569 struct pci_dev *pdev = tp->pci_dev;
5570
hayeswangbeb330a2013-04-01 22:23:39 +00005571 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5572
Hayes Wangc5583862012-07-02 17:23:22 +08005573 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5574 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5575 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5576 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5577
5578 rtl_csi_access_enable_1(tp);
5579
5580 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5581
5582 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5583 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005584 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005585
5586 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005587 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005588 RTL_W8(MaxTxPacketSize, EarlySize);
5589
5590 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5591 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5592
5593 /* Adjust EEE LED frequency */
5594 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5595
hayeswangbeb330a2013-04-01 22:23:39 +00005596 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5597 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005598
5599 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005600}
5601
hayeswang57538c42013-04-01 22:23:40 +00005602static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5603{
5604 void __iomem *ioaddr = tp->mmio_addr;
5605 static const struct ephy_info e_info_8168g_2[] = {
5606 { 0x00, 0x0000, 0x0008 },
5607 { 0x0c, 0x3df0, 0x0200 },
5608 { 0x19, 0xffff, 0xfc00 },
5609 { 0x1e, 0xffff, 0x20eb }
5610 };
5611
5612 rtl_hw_start_8168g_1(tp);
5613
5614 /* disable aspm and clock request before access ephy */
5615 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5616 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5617 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5618}
5619
hayeswang45dd95c2013-07-08 17:09:01 +08005620static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5621{
5622 void __iomem *ioaddr = tp->mmio_addr;
5623 static const struct ephy_info e_info_8411_2[] = {
5624 { 0x00, 0x0000, 0x0008 },
5625 { 0x0c, 0x3df0, 0x0200 },
5626 { 0x0f, 0xffff, 0x5200 },
5627 { 0x19, 0x0020, 0x0000 },
5628 { 0x1e, 0x0000, 0x2000 }
5629 };
5630
5631 rtl_hw_start_8168g_1(tp);
5632
5633 /* disable aspm and clock request before access ephy */
5634 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5635 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5636 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5637}
5638
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005639static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5640{
5641 void __iomem *ioaddr = tp->mmio_addr;
5642 struct pci_dev *pdev = tp->pci_dev;
5643 u16 rg_saw_cnt;
5644 u32 data;
5645 static const struct ephy_info e_info_8168h_1[] = {
5646 { 0x1e, 0x0800, 0x0001 },
5647 { 0x1d, 0x0000, 0x0800 },
5648 { 0x05, 0xffff, 0x2089 },
5649 { 0x06, 0xffff, 0x5881 },
5650 { 0x04, 0xffff, 0x154a },
5651 { 0x01, 0xffff, 0x068b }
5652 };
5653
5654 /* disable aspm and clock request before access ephy */
5655 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5656 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5657 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5658
5659 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5660
5661 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5662 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5663 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5664 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5665
5666 rtl_csi_access_enable_1(tp);
5667
5668 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5669
5670 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5671 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5672
5673 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
5674
5675 rtl_w1w0_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
5676
5677 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5678
5679 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5680 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
5681 RTL_W8(MaxTxPacketSize, EarlySize);
5682
5683 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5684 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5685
5686 /* Adjust EEE LED frequency */
5687 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5688
5689 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
5690 RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
5691
5692 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
5693
5694 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5695
5696 rtl_pcie_state_l2l3_enable(tp, false);
5697
5698 rtl_writephy(tp, 0x1f, 0x0c42);
5699 rg_saw_cnt = rtl_readphy(tp, 0x13);
5700 rtl_writephy(tp, 0x1f, 0x0000);
5701 if (rg_saw_cnt > 0) {
5702 u16 sw_cnt_1ms_ini;
5703
5704 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5705 sw_cnt_1ms_ini &= 0x0fff;
5706 data = r8168_mac_ocp_read(tp, 0xd412);
5707 data &= 0x0fff;
5708 data |= sw_cnt_1ms_ini;
5709 r8168_mac_ocp_write(tp, 0xd412, data);
5710 }
5711
5712 data = r8168_mac_ocp_read(tp, 0xe056);
5713 data &= 0xf0;
5714 data |= 0x07;
5715 r8168_mac_ocp_write(tp, 0xe056, data);
5716
5717 data = r8168_mac_ocp_read(tp, 0xe052);
5718 data &= 0x8008;
5719 data |= 0x6000;
5720 r8168_mac_ocp_write(tp, 0xe052, data);
5721
5722 data = r8168_mac_ocp_read(tp, 0xe0d6);
5723 data &= 0x01ff;
5724 data |= 0x017f;
5725 r8168_mac_ocp_write(tp, 0xe0d6, data);
5726
5727 data = r8168_mac_ocp_read(tp, 0xd420);
5728 data &= 0x0fff;
5729 data |= 0x047f;
5730 r8168_mac_ocp_write(tp, 0xd420, data);
5731
5732 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5733 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5734 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5735 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5736}
5737
Francois Romieu07ce4062007-02-23 23:36:39 +01005738static void rtl_hw_start_8168(struct net_device *dev)
5739{
Francois Romieu2dd99532007-06-11 23:22:52 +02005740 struct rtl8169_private *tp = netdev_priv(dev);
5741 void __iomem *ioaddr = tp->mmio_addr;
5742
5743 RTL_W8(Cfg9346, Cfg9346_Unlock);
5744
françois romieuf0298f82011-01-03 15:07:42 +00005745 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005746
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005747 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02005748
Francois Romieu0e485152007-02-20 00:00:26 +01005749 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02005750
5751 RTL_W16(CPlusCmd, tp->cp_cmd);
5752
Francois Romieu0e485152007-02-20 00:00:26 +01005753 RTL_W16(IntrMitigate, 0x5151);
5754
5755 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005756 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005757 tp->event_slow |= RxFIFOOver | PCSTimeout;
5758 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005759 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005760
5761 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5762
hayeswang1a964642013-04-01 22:23:41 +00005763 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02005764
5765 RTL_R8(IntrMask);
5766
Francois Romieu219a1e92008-06-28 11:58:39 +02005767 switch (tp->mac_version) {
5768 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005769 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005770 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005771
5772 case RTL_GIGA_MAC_VER_12:
5773 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005774 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005775 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005776
5777 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005778 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005779 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005780
5781 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005782 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005783 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005784
5785 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005786 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005787 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005788
Francois Romieu197ff762008-06-28 13:16:02 +02005789 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005790 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005791 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005792
Francois Romieu6fb07052008-06-29 11:54:28 +02005793 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005794 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005795 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005796
Francois Romieuef3386f2008-06-29 12:24:30 +02005797 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005798 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005799 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005800
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005801 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005802 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005803 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005804
Francois Romieu5b538df2008-07-20 16:22:45 +02005805 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005806 case RTL_GIGA_MAC_VER_26:
5807 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005808 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005809 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005810
françois romieue6de30d2011-01-03 15:08:37 +00005811 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005812 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005813 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005814
hayeswang4804b3b2011-03-21 01:50:29 +00005815 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005816 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005817 break;
5818
hayeswang01dc7fe2011-03-21 01:50:28 +00005819 case RTL_GIGA_MAC_VER_32:
5820 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005821 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005822 break;
5823 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005824 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005825 break;
françois romieue6de30d2011-01-03 15:08:37 +00005826
Hayes Wangc2218922011-09-06 16:55:18 +08005827 case RTL_GIGA_MAC_VER_35:
5828 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005829 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005830 break;
5831
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005832 case RTL_GIGA_MAC_VER_38:
5833 rtl_hw_start_8411(tp);
5834 break;
5835
Hayes Wangc5583862012-07-02 17:23:22 +08005836 case RTL_GIGA_MAC_VER_40:
5837 case RTL_GIGA_MAC_VER_41:
5838 rtl_hw_start_8168g_1(tp);
5839 break;
hayeswang57538c42013-04-01 22:23:40 +00005840 case RTL_GIGA_MAC_VER_42:
5841 rtl_hw_start_8168g_2(tp);
5842 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005843
hayeswang45dd95c2013-07-08 17:09:01 +08005844 case RTL_GIGA_MAC_VER_44:
5845 rtl_hw_start_8411_2(tp);
5846 break;
5847
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005848 case RTL_GIGA_MAC_VER_45:
5849 case RTL_GIGA_MAC_VER_46:
5850 rtl_hw_start_8168h_1(tp);
5851 break;
5852
Francois Romieu219a1e92008-06-28 11:58:39 +02005853 default:
5854 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5855 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005856 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005857 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005858
hayeswang1a964642013-04-01 22:23:41 +00005859 RTL_W8(Cfg9346, Cfg9346_Lock);
5860
Francois Romieu0e485152007-02-20 00:00:26 +01005861 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5862
hayeswang1a964642013-04-01 22:23:41 +00005863 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02005864
Chun-Hao Lin05b96872014-10-01 23:17:12 +08005865 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005866}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Francois Romieu2857ffb2008-08-02 21:08:49 +02005868#define R810X_CPCMD_QUIRK_MASK (\
5869 EnableBist | \
5870 Mac_dbgo_oe | \
5871 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005872 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005873 Force_txflow_en | \
5874 Cxpl_dbg_sel | \
5875 ASF | \
5876 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005877 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005878
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005879static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005880{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005881 void __iomem *ioaddr = tp->mmio_addr;
5882 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005883 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005884 { 0x01, 0, 0x6e65 },
5885 { 0x02, 0, 0x091f },
5886 { 0x03, 0, 0xc2f9 },
5887 { 0x06, 0, 0xafb5 },
5888 { 0x07, 0, 0x0e00 },
5889 { 0x19, 0, 0xec80 },
5890 { 0x01, 0, 0x2e65 },
5891 { 0x01, 0, 0x6e65 }
5892 };
5893 u8 cfg1;
5894
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005895 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005896
5897 RTL_W8(DBG_REG, FIX_NAK_1);
5898
5899 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5900
5901 RTL_W8(Config1,
5902 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5903 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5904
5905 cfg1 = RTL_R8(Config1);
5906 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5907 RTL_W8(Config1, cfg1 & ~LEDS0);
5908
Francois Romieufdf6fc02012-07-06 22:40:38 +02005909 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005910}
5911
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005912static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005913{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005914 void __iomem *ioaddr = tp->mmio_addr;
5915 struct pci_dev *pdev = tp->pci_dev;
5916
5917 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005918
5919 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5920
5921 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5922 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005923}
5924
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005925static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005926{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005927 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005928
Francois Romieufdf6fc02012-07-06 22:40:38 +02005929 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005930}
5931
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005932static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005933{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005934 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005935 static const struct ephy_info e_info_8105e_1[] = {
5936 { 0x07, 0, 0x4000 },
5937 { 0x19, 0, 0x0200 },
5938 { 0x19, 0, 0x0020 },
5939 { 0x1e, 0, 0x2000 },
5940 { 0x03, 0, 0x0001 },
5941 { 0x19, 0, 0x0100 },
5942 { 0x19, 0, 0x0004 },
5943 { 0x0a, 0, 0x0020 }
5944 };
5945
Francois Romieucecb5fd2011-04-01 10:21:07 +02005946 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005947 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5948
Francois Romieucecb5fd2011-04-01 10:21:07 +02005949 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005950 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5951
5952 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005953 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005954
Francois Romieufdf6fc02012-07-06 22:40:38 +02005955 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005956
5957 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005958}
5959
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005960static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005961{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005962 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005963 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005964}
5965
Hayes Wang7e18dca2012-03-30 14:33:02 +08005966static void rtl_hw_start_8402(struct rtl8169_private *tp)
5967{
5968 void __iomem *ioaddr = tp->mmio_addr;
5969 static const struct ephy_info e_info_8402[] = {
5970 { 0x19, 0xffff, 0xff64 },
5971 { 0x1e, 0, 0x4000 }
5972 };
5973
5974 rtl_csi_access_enable_2(tp);
5975
5976 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5977 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5978
5979 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5980 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5981
Francois Romieufdf6fc02012-07-06 22:40:38 +02005982 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005983
5984 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5985
Francois Romieufdf6fc02012-07-06 22:40:38 +02005986 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5987 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5988 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5989 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5990 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5991 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5992 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005993
5994 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005995}
5996
Hayes Wang5598bfe2012-07-02 17:23:21 +08005997static void rtl_hw_start_8106(struct rtl8169_private *tp)
5998{
5999 void __iomem *ioaddr = tp->mmio_addr;
6000
6001 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6002 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6003
Francois Romieu4521e1a92012-11-01 16:46:28 +00006004 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006005 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6006 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08006007
6008 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08006009}
6010
Francois Romieu07ce4062007-02-23 23:36:39 +01006011static void rtl_hw_start_8101(struct net_device *dev)
6012{
Francois Romieucdf1a602007-06-11 23:29:50 +02006013 struct rtl8169_private *tp = netdev_priv(dev);
6014 void __iomem *ioaddr = tp->mmio_addr;
6015 struct pci_dev *pdev = tp->pci_dev;
6016
Francois Romieuda78dbf2012-01-26 14:18:23 +01006017 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6018 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00006019
Francois Romieucecb5fd2011-04-01 10:21:07 +02006020 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08006021 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06006022 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6023 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02006024
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006025 RTL_W8(Cfg9346, Cfg9346_Unlock);
6026
hayeswang1a964642013-04-01 22:23:41 +00006027 RTL_W8(MaxTxPacketSize, TxPacketMax);
6028
6029 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6030
6031 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6032 RTL_W16(CPlusCmd, tp->cp_cmd);
6033
6034 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6035
6036 rtl_set_rx_tx_config_registers(tp);
6037
Francois Romieu2857ffb2008-08-02 21:08:49 +02006038 switch (tp->mac_version) {
6039 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006040 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006041 break;
6042
6043 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006044 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006045 break;
6046
6047 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006048 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02006049 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08006050
6051 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006052 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006053 break;
6054 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006055 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08006056 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08006057
6058 case RTL_GIGA_MAC_VER_37:
6059 rtl_hw_start_8402(tp);
6060 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08006061
6062 case RTL_GIGA_MAC_VER_39:
6063 rtl_hw_start_8106(tp);
6064 break;
hayeswang58152cd2013-04-01 22:23:42 +00006065 case RTL_GIGA_MAC_VER_43:
6066 rtl_hw_start_8168g_2(tp);
6067 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006068 case RTL_GIGA_MAC_VER_47:
6069 case RTL_GIGA_MAC_VER_48:
6070 rtl_hw_start_8168h_1(tp);
6071 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02006072 }
6073
Hayes Wangd24e9aa2011-02-22 17:26:19 +08006074 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02006075
Francois Romieucdf1a602007-06-11 23:29:50 +02006076 RTL_W16(IntrMitigate, 0x0000);
6077
Francois Romieucdf1a602007-06-11 23:29:50 +02006078 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02006079
Francois Romieucdf1a602007-06-11 23:29:50 +02006080 rtl_set_rx_mode(dev);
6081
hayeswang1a964642013-04-01 22:23:41 +00006082 RTL_R8(IntrMask);
6083
Francois Romieucdf1a602007-06-11 23:29:50 +02006084 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085}
6086
6087static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6088{
Francois Romieud58d46b2011-05-03 16:38:29 +02006089 struct rtl8169_private *tp = netdev_priv(dev);
6090
6091 if (new_mtu < ETH_ZLEN ||
6092 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 return -EINVAL;
6094
Francois Romieud58d46b2011-05-03 16:38:29 +02006095 if (new_mtu > ETH_DATA_LEN)
6096 rtl_hw_jumbo_enable(tp);
6097 else
6098 rtl_hw_jumbo_disable(tp);
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00006101 netdev_update_features(dev);
6102
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104}
6105
6106static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6107{
Al Viro95e09182007-12-22 18:55:39 +00006108 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6110}
6111
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006112static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6113 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006115 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006116 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006117
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006118 kfree(*data_buff);
6119 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 rtl8169_make_unusable_by_asic(desc);
6121}
6122
6123static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6124{
6125 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6126
6127 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6128}
6129
6130static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6131 u32 rx_buf_sz)
6132{
6133 desc->addr = cpu_to_le64(mapping);
6134 wmb();
6135 rtl8169_mark_to_asic(desc, rx_buf_sz);
6136}
6137
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006138static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006140 return (void *)ALIGN((long)data, 16);
6141}
6142
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006143static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6144 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006145{
6146 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006148 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006149 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006150 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006152 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6153 if (!data)
6154 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01006155
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006156 if (rtl8169_align(data) != data) {
6157 kfree(data);
6158 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6159 if (!data)
6160 return NULL;
6161 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006162
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006163 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00006164 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006165 if (unlikely(dma_mapping_error(d, mapping))) {
6166 if (net_ratelimit())
6167 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006168 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
6171 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006172 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006173
6174err_out:
6175 kfree(data);
6176 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177}
6178
6179static void rtl8169_rx_clear(struct rtl8169_private *tp)
6180{
Francois Romieu07d3f512007-02-21 22:40:46 +01006181 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
6183 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006184 if (tp->Rx_databuff[i]) {
6185 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 tp->RxDescArray + i);
6187 }
6188 }
6189}
6190
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006191static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006193 desc->opts1 |= cpu_to_le32(RingEnd);
6194}
Francois Romieu5b0384f2006-08-16 16:00:01 +02006195
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006196static int rtl8169_rx_fill(struct rtl8169_private *tp)
6197{
6198 unsigned int i;
6199
6200 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006201 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02006202
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006203 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02006205
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006206 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006207 if (!data) {
6208 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006209 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006210 }
6211 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006214 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
6215 return 0;
6216
6217err_out:
6218 rtl8169_rx_clear(tp);
6219 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220}
6221
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222static int rtl8169_init_ring(struct net_device *dev)
6223{
6224 struct rtl8169_private *tp = netdev_priv(dev);
6225
6226 rtl8169_init_ring_indexes(tp);
6227
6228 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006229 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00006231 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232}
6233
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006234static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 struct TxDesc *desc)
6236{
6237 unsigned int len = tx_skb->len;
6238
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006239 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
6240
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 desc->opts1 = 0x00;
6242 desc->opts2 = 0x00;
6243 desc->addr = 0x00;
6244 tx_skb->len = 0;
6245}
6246
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006247static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
6248 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
6250 unsigned int i;
6251
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006252 for (i = 0; i < n; i++) {
6253 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 struct ring_info *tx_skb = tp->tx_skb + entry;
6255 unsigned int len = tx_skb->len;
6256
6257 if (len) {
6258 struct sk_buff *skb = tx_skb->skb;
6259
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006260 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 tp->TxDescArray + entry);
6262 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00006263 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006264 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 tx_skb->skb = NULL;
6266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 }
6268 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006269}
6270
6271static void rtl8169_tx_clear(struct rtl8169_private *tp)
6272{
6273 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 tp->cur_tx = tp->dirty_tx = 0;
6275}
6276
Francois Romieu4422bcd2012-01-26 11:23:32 +01006277static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278{
David Howellsc4028952006-11-22 14:57:56 +00006279 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01006280 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Francois Romieuda78dbf2012-01-26 14:18:23 +01006282 napi_disable(&tp->napi);
6283 netif_stop_queue(dev);
6284 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285
françois romieuc7c2c392011-12-04 20:30:52 +00006286 rtl8169_hw_reset(tp);
6287
Francois Romieu56de4142011-03-15 17:29:31 +01006288 for (i = 0; i < NUM_RX_DESC; i++)
6289 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
6290
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00006292 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Francois Romieuda78dbf2012-01-26 14:18:23 +01006294 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01006295 rtl_hw_start(dev);
6296 netif_wake_queue(dev);
6297 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298}
6299
6300static void rtl8169_tx_timeout(struct net_device *dev)
6301{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006302 struct rtl8169_private *tp = netdev_priv(dev);
6303
6304 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305}
6306
6307static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07006308 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309{
6310 struct skb_shared_info *info = skb_shinfo(skb);
6311 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006312 struct TxDesc *uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006313 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
6315 entry = tp->cur_tx;
6316 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006317 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 dma_addr_t mapping;
6319 u32 status, len;
6320 void *addr;
6321
6322 entry = (entry + 1) % NUM_TX_DESC;
6323
6324 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00006325 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00006326 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006327 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006328 if (unlikely(dma_mapping_error(d, mapping))) {
6329 if (net_ratelimit())
6330 netif_err(tp, drv, tp->dev,
6331 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006332 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
Francois Romieucecb5fd2011-04-01 10:21:07 +02006335 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006336 status = opts[0] | len |
6337 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07006340 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 txd->addr = cpu_to_le64(mapping);
6342
6343 tp->tx_skb[entry].len = len;
6344 }
6345
6346 if (cur_frag) {
6347 tp->tx_skb[entry].skb = skb;
6348 txd->opts1 |= cpu_to_le32(LastFrag);
6349 }
6350
6351 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006352
6353err_out:
6354 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6355 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356}
6357
françois romieub423e9a2013-05-18 01:24:46 +00006358static bool rtl_skb_pad(struct sk_buff *skb)
6359{
6360 if (skb_padto(skb, ETH_ZLEN))
6361 return false;
6362 skb_put(skb, ETH_ZLEN - skb->len);
6363 return true;
6364}
6365
6366static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6367{
6368 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6369}
6370
hayeswange9746042014-07-11 16:25:58 +08006371static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6372 struct net_device *dev);
6373/* r8169_csum_workaround()
6374 * The hw limites the value the transport offset. When the offset is out of the
6375 * range, calculate the checksum by sw.
6376 */
6377static void r8169_csum_workaround(struct rtl8169_private *tp,
6378 struct sk_buff *skb)
6379{
6380 if (skb_shinfo(skb)->gso_size) {
6381 netdev_features_t features = tp->dev->features;
6382 struct sk_buff *segs, *nskb;
6383
6384 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6385 segs = skb_gso_segment(skb, features);
6386 if (IS_ERR(segs) || !segs)
6387 goto drop;
6388
6389 do {
6390 nskb = segs;
6391 segs = segs->next;
6392 nskb->next = NULL;
6393 rtl8169_start_xmit(nskb, tp->dev);
6394 } while (segs);
6395
6396 dev_kfree_skb(skb);
6397 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6398 if (skb_checksum_help(skb) < 0)
6399 goto drop;
6400
6401 rtl8169_start_xmit(skb, tp->dev);
6402 } else {
6403 struct net_device_stats *stats;
6404
6405drop:
6406 stats = &tp->dev->stats;
6407 stats->tx_dropped++;
6408 dev_kfree_skb(skb);
6409 }
6410}
6411
6412/* msdn_giant_send_check()
6413 * According to the document of microsoft, the TCP Pseudo Header excludes the
6414 * packet length for IPv6 TCP large packets.
6415 */
6416static int msdn_giant_send_check(struct sk_buff *skb)
6417{
6418 const struct ipv6hdr *ipv6h;
6419 struct tcphdr *th;
6420 int ret;
6421
6422 ret = skb_cow_head(skb, 0);
6423 if (ret)
6424 return ret;
6425
6426 ipv6h = ipv6_hdr(skb);
6427 th = tcp_hdr(skb);
6428
6429 th->check = 0;
6430 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6431
6432 return ret;
6433}
6434
6435static inline __be16 get_protocol(struct sk_buff *skb)
6436{
6437 __be16 protocol;
6438
6439 if (skb->protocol == htons(ETH_P_8021Q))
6440 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6441 else
6442 protocol = skb->protocol;
6443
6444 return protocol;
6445}
6446
hayeswang5888d3f2014-07-11 16:25:56 +08006447static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6448 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449{
Michał Mirosław350fb322011-04-08 06:35:56 +00006450 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Francois Romieu2b7b4312011-04-18 22:53:24 -07006452 if (mss) {
6453 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006454 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6455 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6456 const struct iphdr *ip = ip_hdr(skb);
6457
6458 if (ip->protocol == IPPROTO_TCP)
6459 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6460 else if (ip->protocol == IPPROTO_UDP)
6461 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6462 else
6463 WARN_ON_ONCE(1);
6464 }
6465
6466 return true;
6467}
6468
6469static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6470 struct sk_buff *skb, u32 *opts)
6471{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006472 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006473 u32 mss = skb_shinfo(skb)->gso_size;
6474
6475 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006476 if (transport_offset > GTTCPHO_MAX) {
6477 netif_warn(tp, tx_err, tp->dev,
6478 "Invalid transport offset 0x%x for TSO\n",
6479 transport_offset);
6480 return false;
6481 }
6482
6483 switch (get_protocol(skb)) {
6484 case htons(ETH_P_IP):
6485 opts[0] |= TD1_GTSENV4;
6486 break;
6487
6488 case htons(ETH_P_IPV6):
6489 if (msdn_giant_send_check(skb))
6490 return false;
6491
6492 opts[0] |= TD1_GTSENV6;
6493 break;
6494
6495 default:
6496 WARN_ON_ONCE(1);
6497 break;
6498 }
6499
hayeswangbdfa4ed2014-07-11 16:25:57 +08006500 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006501 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006502 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006503 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
françois romieub423e9a2013-05-18 01:24:46 +00006505 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6506 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
6507
hayeswange9746042014-07-11 16:25:58 +08006508 if (transport_offset > TCPHO_MAX) {
6509 netif_warn(tp, tx_err, tp->dev,
6510 "Invalid transport offset 0x%x\n",
6511 transport_offset);
6512 return false;
6513 }
6514
6515 switch (get_protocol(skb)) {
6516 case htons(ETH_P_IP):
6517 opts[1] |= TD1_IPv4_CS;
6518 ip_protocol = ip_hdr(skb)->protocol;
6519 break;
6520
6521 case htons(ETH_P_IPV6):
6522 opts[1] |= TD1_IPv6_CS;
6523 ip_protocol = ipv6_hdr(skb)->nexthdr;
6524 break;
6525
6526 default:
6527 ip_protocol = IPPROTO_RAW;
6528 break;
6529 }
6530
6531 if (ip_protocol == IPPROTO_TCP)
6532 opts[1] |= TD1_TCP_CS;
6533 else if (ip_protocol == IPPROTO_UDP)
6534 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006535 else
6536 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006537
6538 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006539 } else {
6540 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6541 return rtl_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 }
hayeswang5888d3f2014-07-11 16:25:56 +08006543
françois romieub423e9a2013-05-18 01:24:46 +00006544 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545}
6546
Stephen Hemminger613573252009-08-31 19:50:58 +00006547static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6548 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549{
6550 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006551 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 struct TxDesc *txd = tp->TxDescArray + entry;
6553 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006554 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 dma_addr_t mapping;
6556 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006557 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006558 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006559
Julien Ducourthial477206a2012-05-09 00:00:06 +02006560 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006561 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006562 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 }
6564
6565 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006566 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
françois romieub423e9a2013-05-18 01:24:46 +00006568 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6569 opts[0] = DescOwn;
6570
hayeswange9746042014-07-11 16:25:58 +08006571 if (!tp->tso_csum(tp, skb, opts)) {
6572 r8169_csum_workaround(tp, skb);
6573 return NETDEV_TX_OK;
6574 }
françois romieub423e9a2013-05-18 01:24:46 +00006575
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006576 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006577 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006578 if (unlikely(dma_mapping_error(d, mapping))) {
6579 if (net_ratelimit())
6580 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006581 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
6584 tp->tx_skb[entry].len = len;
6585 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
Francois Romieu2b7b4312011-04-18 22:53:24 -07006587 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006588 if (frags < 0)
6589 goto err_dma_1;
6590 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006591 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006592 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006593 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006594 tp->tx_skb[entry].skb = skb;
6595 }
6596
Francois Romieu2b7b4312011-04-18 22:53:24 -07006597 txd->opts2 = cpu_to_le32(opts[1]);
6598
Richard Cochran5047fb52012-03-10 07:29:42 +00006599 skb_tx_timestamp(skb);
6600
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 wmb();
6602
Francois Romieucecb5fd2011-04-01 10:21:07 +02006603 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006604 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 txd->opts1 = cpu_to_le32(status);
6606
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 tp->cur_tx += frags + 1;
6608
David Dillow4c020a92010-03-03 16:33:10 +00006609 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610
Francois Romieucecb5fd2011-04-01 10:21:07 +02006611 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
Francois Romieuda78dbf2012-01-26 14:18:23 +01006613 mmiowb();
6614
Julien Ducourthial477206a2012-05-09 00:00:06 +02006615 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006616 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6617 * not miss a ring update when it notices a stopped queue.
6618 */
6619 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006621 /* Sync with rtl_tx:
6622 * - publish queue status and cur_tx ring index (write barrier)
6623 * - refresh dirty_tx ring index (read barrier).
6624 * May the current thread have a pessimistic view of the ring
6625 * status and forget to wake up queue, a racing rtl_tx thread
6626 * can't.
6627 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006628 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006629 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 netif_wake_queue(dev);
6631 }
6632
Stephen Hemminger613573252009-08-31 19:50:58 +00006633 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006635err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006636 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006637err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006638 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006639 dev->stats.tx_dropped++;
6640 return NETDEV_TX_OK;
6641
6642err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006644 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006645 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646}
6647
6648static void rtl8169_pcierr_interrupt(struct net_device *dev)
6649{
6650 struct rtl8169_private *tp = netdev_priv(dev);
6651 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 u16 pci_status, pci_cmd;
6653
6654 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6655 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6656
Joe Perchesbf82c182010-02-09 11:49:50 +00006657 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6658 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
6660 /*
6661 * The recovery sequence below admits a very elaborated explanation:
6662 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006663 * - I did not see what else could be done;
6664 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 *
6666 * Feel free to adjust to your needs.
6667 */
Francois Romieua27993f2006-12-18 00:04:19 +01006668 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006669 pci_cmd &= ~PCI_COMMAND_PARITY;
6670 else
6671 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6672
6673 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
6675 pci_write_config_word(pdev, PCI_STATUS,
6676 pci_status & (PCI_STATUS_DETECTED_PARITY |
6677 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6678 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6679
6680 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006681 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00006682 void __iomem *ioaddr = tp->mmio_addr;
6683
Joe Perchesbf82c182010-02-09 11:49:50 +00006684 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 tp->cp_cmd &= ~PCIDAC;
6686 RTL_W16(CPlusCmd, tp->cp_cmd);
6687 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 }
6689
françois romieue6de30d2011-01-03 15:08:37 +00006690 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006691
Francois Romieu98ddf982012-01-31 10:47:34 +01006692 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693}
6694
Francois Romieuda78dbf2012-01-26 14:18:23 +01006695static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696{
6697 unsigned int dirty_tx, tx_left;
6698
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 dirty_tx = tp->dirty_tx;
6700 smp_rmb();
6701 tx_left = tp->cur_tx - dirty_tx;
6702
6703 while (tx_left > 0) {
6704 unsigned int entry = dirty_tx % NUM_TX_DESC;
6705 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 u32 status;
6707
6708 rmb();
6709 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6710 if (status & DescOwn)
6711 break;
6712
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006713 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6714 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 if (status & LastFrag) {
Francois Romieu17bcb682012-07-23 22:55:55 +02006716 u64_stats_update_begin(&tp->tx_stats.syncp);
6717 tp->tx_stats.packets++;
6718 tp->tx_stats.bytes += tx_skb->skb->len;
6719 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006720 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 tx_skb->skb = NULL;
6722 }
6723 dirty_tx++;
6724 tx_left--;
6725 }
6726
6727 if (tp->dirty_tx != dirty_tx) {
6728 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006729 /* Sync with rtl8169_start_xmit:
6730 * - publish dirty_tx ring index (write barrier)
6731 * - refresh cur_tx ring index and queue status (read barrier)
6732 * May the current thread miss the stopped queue condition,
6733 * a racing xmit thread can only have a right view of the
6734 * ring status.
6735 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006736 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006738 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 netif_wake_queue(dev);
6740 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006741 /*
6742 * 8168 hack: TxPoll requests are lost when the Tx packets are
6743 * too close. Let's kick an extra TxPoll request when a burst
6744 * of start_xmit activity is detected (if it is not detected,
6745 * it is slow enough). -- FR
6746 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006747 if (tp->cur_tx != dirty_tx) {
6748 void __iomem *ioaddr = tp->mmio_addr;
6749
Francois Romieud78ae2d2007-08-26 20:08:19 +02006750 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 }
6753}
6754
Francois Romieu126fa4b2005-05-12 20:09:17 -04006755static inline int rtl8169_fragmented_frame(u32 status)
6756{
6757 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6758}
6759
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006760static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 u32 status = opts1 & RxProtoMask;
6763
6764 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006765 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 skb->ip_summed = CHECKSUM_UNNECESSARY;
6767 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006768 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769}
6770
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006771static struct sk_buff *rtl8169_try_rx_copy(void *data,
6772 struct rtl8169_private *tp,
6773 int pkt_size,
6774 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006776 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006777 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006779 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006780 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006781 prefetch(data);
6782 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6783 if (skb)
6784 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006785 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6786
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006787 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788}
6789
Francois Romieuda78dbf2012-01-26 14:18:23 +01006790static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791{
6792 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006793 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
Timo Teräs9fba0812013-01-15 21:01:24 +00006797 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006799 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 u32 status;
6801
6802 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04006803 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
6805 if (status & DescOwn)
6806 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006807 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006808 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6809 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006810 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006812 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006814 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006815 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006816 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006817 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006818 }
Ben Greear6bbe0212012-02-10 15:04:33 +00006819 if ((status & (RxRUNT | RxCRC)) &&
6820 !(status & (RxRWT | RxFOVF)) &&
6821 (dev->features & NETIF_F_RXALL))
6822 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006824 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006825 dma_addr_t addr;
6826 int pkt_size;
6827
6828process_pkt:
6829 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006830 if (likely(!(dev->features & NETIF_F_RXFCS)))
6831 pkt_size = (status & 0x00003fff) - 4;
6832 else
6833 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
Francois Romieu126fa4b2005-05-12 20:09:17 -04006835 /*
6836 * The driver does not support incoming fragmented
6837 * frames. They are seen as a symptom of over-mtu
6838 * sized frames.
6839 */
6840 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006841 dev->stats.rx_dropped++;
6842 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006843 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006844 }
6845
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006846 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6847 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006848 if (!skb) {
6849 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006850 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 }
6852
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006853 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 skb_put(skb, pkt_size);
6855 skb->protocol = eth_type_trans(skb, dev);
6856
Francois Romieu7a8fc772011-03-01 17:18:33 +01006857 rtl8169_rx_vlan_tag(desc, skb);
6858
Francois Romieu56de4142011-03-15 17:29:31 +01006859 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Junchang Wang8027aa22012-03-04 23:30:32 +01006861 u64_stats_update_begin(&tp->rx_stats.syncp);
6862 tp->rx_stats.packets++;
6863 tp->rx_stats.bytes += pkt_size;
6864 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 }
françois romieuce11ff52013-01-24 13:30:06 +00006866release_descriptor:
6867 desc->opts2 = 0;
6868 wmb();
6869 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 }
6871
6872 count = cur_rx - tp->cur_rx;
6873 tp->cur_rx = cur_rx;
6874
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 return count;
6876}
6877
Francois Romieu07d3f512007-02-21 22:40:46 +01006878static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879{
Francois Romieu07d3f512007-02-21 22:40:46 +01006880 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006883 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006885 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006886 if (status && status != 0xffff) {
6887 status &= RTL_EVENT_NAPI | tp->event_slow;
6888 if (status) {
6889 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006890
Francois Romieuda78dbf2012-01-26 14:18:23 +01006891 rtl_irq_disable(tp);
6892 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 return IRQ_RETVAL(handled);
6896}
6897
Francois Romieuda78dbf2012-01-26 14:18:23 +01006898/*
6899 * Workqueue context.
6900 */
6901static void rtl_slow_event_work(struct rtl8169_private *tp)
6902{
6903 struct net_device *dev = tp->dev;
6904 u16 status;
6905
6906 status = rtl_get_events(tp) & tp->event_slow;
6907 rtl_ack_events(tp, status);
6908
6909 if (unlikely(status & RxFIFOOver)) {
6910 switch (tp->mac_version) {
6911 /* Work around for rx fifo overflow */
6912 case RTL_GIGA_MAC_VER_11:
6913 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006914 /* XXX - Hack alert. See rtl_task(). */
6915 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006916 default:
6917 break;
6918 }
6919 }
6920
6921 if (unlikely(status & SYSErr))
6922 rtl8169_pcierr_interrupt(dev);
6923
6924 if (status & LinkChg)
6925 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6926
françois romieu7dbb4912012-06-09 10:53:16 +00006927 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006928}
6929
Francois Romieu4422bcd2012-01-26 11:23:32 +01006930static void rtl_task(struct work_struct *work)
6931{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006932 static const struct {
6933 int bitnr;
6934 void (*action)(struct rtl8169_private *);
6935 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006936 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006937 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6938 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6939 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6940 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006941 struct rtl8169_private *tp =
6942 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006943 struct net_device *dev = tp->dev;
6944 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006945
Francois Romieuda78dbf2012-01-26 14:18:23 +01006946 rtl_lock_work(tp);
6947
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006948 if (!netif_running(dev) ||
6949 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006950 goto out_unlock;
6951
6952 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6953 bool pending;
6954
Francois Romieuda78dbf2012-01-26 14:18:23 +01006955 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006956 if (pending)
6957 rtl_work[i].action(tp);
6958 }
6959
6960out_unlock:
6961 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006962}
6963
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006964static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006966 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6967 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006968 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6969 int work_done= 0;
6970 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
Francois Romieuda78dbf2012-01-26 14:18:23 +01006972 status = rtl_get_events(tp);
6973 rtl_ack_events(tp, status & ~tp->event_slow);
6974
6975 if (status & RTL_EVENT_NAPI_RX)
6976 work_done = rtl_rx(dev, tp, (u32) budget);
6977
6978 if (status & RTL_EVENT_NAPI_TX)
6979 rtl_tx(dev, tp);
6980
6981 if (status & tp->event_slow) {
6982 enable_mask &= ~tp->event_slow;
6983
6984 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006987 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006988 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006989
Francois Romieuda78dbf2012-01-26 14:18:23 +01006990 rtl_irq_enable(tp, enable_mask);
6991 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 }
6993
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006994 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Francois Romieu523a6092008-09-10 22:28:56 +02006997static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6998{
6999 struct rtl8169_private *tp = netdev_priv(dev);
7000
7001 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7002 return;
7003
7004 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7005 RTL_W32(RxMissed, 0);
7006}
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008static void rtl8169_down(struct net_device *dev)
7009{
7010 struct rtl8169_private *tp = netdev_priv(dev);
7011 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
Francois Romieu4876cc12011-03-11 21:07:11 +01007013 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01007015 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007016 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
Hayes Wang92fc43b2011-07-06 15:58:03 +08007018 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007019 /*
7020 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01007021 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7022 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00007023 */
Francois Romieu523a6092008-09-10 22:28:56 +02007024 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01007027 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 rtl8169_tx_clear(tp);
7030
7031 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00007032
7033 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034}
7035
7036static int rtl8169_close(struct net_device *dev)
7037{
7038 struct rtl8169_private *tp = netdev_priv(dev);
7039 struct pci_dev *pdev = tp->pci_dev;
7040
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007041 pm_runtime_get_sync(&pdev->dev);
7042
Francois Romieucecb5fd2011-04-01 10:21:07 +02007043 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08007044 rtl8169_update_counters(dev);
7045
Francois Romieuda78dbf2012-01-26 14:18:23 +01007046 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007047 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007050 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
Lekensteyn4ea72442013-07-22 09:53:30 +02007052 cancel_work_sync(&tp->wk.work);
7053
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007054 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00007056 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7057 tp->RxPhyAddr);
7058 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7059 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 tp->TxDescArray = NULL;
7061 tp->RxDescArray = NULL;
7062
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007063 pm_runtime_put_sync(&pdev->dev);
7064
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 return 0;
7066}
7067
Francois Romieudc1c00c2012-03-08 10:06:18 +01007068#ifdef CONFIG_NET_POLL_CONTROLLER
7069static void rtl8169_netpoll(struct net_device *dev)
7070{
7071 struct rtl8169_private *tp = netdev_priv(dev);
7072
7073 rtl8169_interrupt(tp->pci_dev->irq, dev);
7074}
7075#endif
7076
Francois Romieudf43ac72012-03-08 09:48:40 +01007077static int rtl_open(struct net_device *dev)
7078{
7079 struct rtl8169_private *tp = netdev_priv(dev);
7080 void __iomem *ioaddr = tp->mmio_addr;
7081 struct pci_dev *pdev = tp->pci_dev;
7082 int retval = -ENOMEM;
7083
7084 pm_runtime_get_sync(&pdev->dev);
7085
7086 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02007087 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01007088 * dma_alloc_coherent provides more.
7089 */
7090 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7091 &tp->TxPhyAddr, GFP_KERNEL);
7092 if (!tp->TxDescArray)
7093 goto err_pm_runtime_put;
7094
7095 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7096 &tp->RxPhyAddr, GFP_KERNEL);
7097 if (!tp->RxDescArray)
7098 goto err_free_tx_0;
7099
7100 retval = rtl8169_init_ring(dev);
7101 if (retval < 0)
7102 goto err_free_rx_1;
7103
7104 INIT_WORK(&tp->wk.work, rtl_task);
7105
7106 smp_mb();
7107
7108 rtl_request_firmware(tp);
7109
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007110 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01007111 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7112 dev->name, dev);
7113 if (retval < 0)
7114 goto err_release_fw_2;
7115
7116 rtl_lock_work(tp);
7117
7118 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7119
7120 napi_enable(&tp->napi);
7121
7122 rtl8169_init_phy(dev, tp);
7123
7124 __rtl8169_set_features(dev, dev->features);
7125
7126 rtl_pll_power_up(tp);
7127
7128 rtl_hw_start(dev);
7129
7130 netif_start_queue(dev);
7131
7132 rtl_unlock_work(tp);
7133
7134 tp->saved_wolopts = 0;
7135 pm_runtime_put_noidle(&pdev->dev);
7136
7137 rtl8169_check_link_status(dev, tp, ioaddr);
7138out:
7139 return retval;
7140
7141err_release_fw_2:
7142 rtl_release_firmware(tp);
7143 rtl8169_rx_clear(tp);
7144err_free_rx_1:
7145 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7146 tp->RxPhyAddr);
7147 tp->RxDescArray = NULL;
7148err_free_tx_0:
7149 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7150 tp->TxPhyAddr);
7151 tp->TxDescArray = NULL;
7152err_pm_runtime_put:
7153 pm_runtime_put_noidle(&pdev->dev);
7154 goto out;
7155}
7156
Junchang Wang8027aa22012-03-04 23:30:32 +01007157static struct rtnl_link_stats64 *
7158rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159{
7160 struct rtl8169_private *tp = netdev_priv(dev);
7161 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01007162 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
Francois Romieuda78dbf2012-01-26 14:18:23 +01007164 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02007165 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02007166
Junchang Wang8027aa22012-03-04 23:30:32 +01007167 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007168 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007169 stats->rx_packets = tp->rx_stats.packets;
7170 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007171 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007172
7173
7174 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07007175 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01007176 stats->tx_packets = tp->tx_stats.packets;
7177 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07007178 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01007179
7180 stats->rx_dropped = dev->stats.rx_dropped;
7181 stats->tx_dropped = dev->stats.tx_dropped;
7182 stats->rx_length_errors = dev->stats.rx_length_errors;
7183 stats->rx_errors = dev->stats.rx_errors;
7184 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7185 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7186 stats->rx_missed_errors = dev->stats.rx_missed_errors;
7187
7188 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189}
7190
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007191static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01007192{
françois romieu065c27c2011-01-03 15:08:12 +00007193 struct rtl8169_private *tp = netdev_priv(dev);
7194
Francois Romieu5d06a992006-02-23 00:47:58 +01007195 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007196 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01007197
7198 netif_device_detach(dev);
7199 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007200
7201 rtl_lock_work(tp);
7202 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007203 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007204 rtl_unlock_work(tp);
7205
7206 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007207}
Francois Romieu5d06a992006-02-23 00:47:58 +01007208
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007209#ifdef CONFIG_PM
7210
7211static int rtl8169_suspend(struct device *device)
7212{
7213 struct pci_dev *pdev = to_pci_dev(device);
7214 struct net_device *dev = pci_get_drvdata(pdev);
7215
7216 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02007217
Francois Romieu5d06a992006-02-23 00:47:58 +01007218 return 0;
7219}
7220
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007221static void __rtl8169_resume(struct net_device *dev)
7222{
françois romieu065c27c2011-01-03 15:08:12 +00007223 struct rtl8169_private *tp = netdev_priv(dev);
7224
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007225 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00007226
7227 rtl_pll_power_up(tp);
7228
Artem Savkovcff4c162012-04-03 10:29:11 +00007229 rtl_lock_work(tp);
7230 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01007231 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00007232 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007233
Francois Romieu98ddf982012-01-31 10:47:34 +01007234 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007235}
7236
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007237static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01007238{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007239 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01007240 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007241 struct rtl8169_private *tp = netdev_priv(dev);
7242
7243 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01007244
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007245 if (netif_running(dev))
7246 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01007247
Francois Romieu5d06a992006-02-23 00:47:58 +01007248 return 0;
7249}
7250
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007251static int rtl8169_runtime_suspend(struct device *device)
7252{
7253 struct pci_dev *pdev = to_pci_dev(device);
7254 struct net_device *dev = pci_get_drvdata(pdev);
7255 struct rtl8169_private *tp = netdev_priv(dev);
7256
7257 if (!tp->TxDescArray)
7258 return 0;
7259
Francois Romieuda78dbf2012-01-26 14:18:23 +01007260 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007261 tp->saved_wolopts = __rtl8169_get_wol(tp);
7262 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01007263 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007264
7265 rtl8169_net_suspend(dev);
7266
7267 return 0;
7268}
7269
7270static int rtl8169_runtime_resume(struct device *device)
7271{
7272 struct pci_dev *pdev = to_pci_dev(device);
7273 struct net_device *dev = pci_get_drvdata(pdev);
7274 struct rtl8169_private *tp = netdev_priv(dev);
7275
7276 if (!tp->TxDescArray)
7277 return 0;
7278
Francois Romieuda78dbf2012-01-26 14:18:23 +01007279 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007280 __rtl8169_set_wol(tp, tp->saved_wolopts);
7281 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01007282 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007283
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00007284 rtl8169_init_phy(dev, tp);
7285
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007286 __rtl8169_resume(dev);
7287
7288 return 0;
7289}
7290
7291static int rtl8169_runtime_idle(struct device *device)
7292{
7293 struct pci_dev *pdev = to_pci_dev(device);
7294 struct net_device *dev = pci_get_drvdata(pdev);
7295 struct rtl8169_private *tp = netdev_priv(dev);
7296
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00007297 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00007298}
7299
Alexey Dobriyan47145212009-12-14 18:00:08 -08007300static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02007301 .suspend = rtl8169_suspend,
7302 .resume = rtl8169_resume,
7303 .freeze = rtl8169_suspend,
7304 .thaw = rtl8169_resume,
7305 .poweroff = rtl8169_suspend,
7306 .restore = rtl8169_resume,
7307 .runtime_suspend = rtl8169_runtime_suspend,
7308 .runtime_resume = rtl8169_runtime_resume,
7309 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007310};
7311
7312#define RTL8169_PM_OPS (&rtl8169_pm_ops)
7313
7314#else /* !CONFIG_PM */
7315
7316#define RTL8169_PM_OPS NULL
7317
7318#endif /* !CONFIG_PM */
7319
David S. Miller1805b2f2011-10-24 18:18:09 -04007320static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7321{
7322 void __iomem *ioaddr = tp->mmio_addr;
7323
7324 /* WoL fails with 8168b when the receiver is disabled. */
7325 switch (tp->mac_version) {
7326 case RTL_GIGA_MAC_VER_11:
7327 case RTL_GIGA_MAC_VER_12:
7328 case RTL_GIGA_MAC_VER_17:
7329 pci_clear_master(tp->pci_dev);
7330
7331 RTL_W8(ChipCmd, CmdRxEnb);
7332 /* PCI commit */
7333 RTL_R8(ChipCmd);
7334 break;
7335 default:
7336 break;
7337 }
7338}
7339
Francois Romieu1765f952008-09-13 17:21:40 +02007340static void rtl_shutdown(struct pci_dev *pdev)
7341{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007342 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007343 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00007344 struct device *d = &pdev->dev;
7345
7346 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02007347
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007348 rtl8169_net_suspend(dev);
7349
Francois Romieucecb5fd2011-04-01 10:21:07 +02007350 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007351 rtl_rar_set(tp, dev->perm_addr);
7352
Hayes Wang92fc43b2011-07-06 15:58:03 +08007353 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007354
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007355 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007356 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
7357 rtl_wol_suspend_quirk(tp);
7358 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007359 }
7360
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007361 pci_wake_from_d3(pdev, true);
7362 pci_set_power_state(pdev, PCI_D3hot);
7363 }
françois romieu2a15cd22012-03-06 01:14:12 +00007364
7365 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007366}
Francois Romieu5d06a992006-02-23 00:47:58 +01007367
Bill Pembertonbaf63292012-12-03 09:23:28 -05007368static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007369{
7370 struct net_device *dev = pci_get_drvdata(pdev);
7371 struct rtl8169_private *tp = netdev_priv(dev);
7372
7373 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7374 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7375 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7376 rtl8168_driver_stop(tp);
7377 }
7378
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007379 netif_napi_del(&tp->napi);
7380
Francois Romieue27566e2012-03-08 09:54:01 +01007381 unregister_netdev(dev);
7382
7383 rtl_release_firmware(tp);
7384
7385 if (pci_dev_run_wake(pdev))
7386 pm_runtime_get_noresume(&pdev->dev);
7387
7388 /* restore original MAC address */
7389 rtl_rar_set(tp, dev->perm_addr);
7390
7391 rtl_disable_msi(pdev, tp);
7392 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007393}
7394
Francois Romieufa9c3852012-03-08 10:01:50 +01007395static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007396 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007397 .ndo_stop = rtl8169_close,
7398 .ndo_get_stats64 = rtl8169_get_stats64,
7399 .ndo_start_xmit = rtl8169_start_xmit,
7400 .ndo_tx_timeout = rtl8169_tx_timeout,
7401 .ndo_validate_addr = eth_validate_addr,
7402 .ndo_change_mtu = rtl8169_change_mtu,
7403 .ndo_fix_features = rtl8169_fix_features,
7404 .ndo_set_features = rtl8169_set_features,
7405 .ndo_set_mac_address = rtl_set_mac_address,
7406 .ndo_do_ioctl = rtl8169_ioctl,
7407 .ndo_set_rx_mode = rtl_set_rx_mode,
7408#ifdef CONFIG_NET_POLL_CONTROLLER
7409 .ndo_poll_controller = rtl8169_netpoll,
7410#endif
7411
7412};
7413
Francois Romieu31fa8b12012-03-08 10:09:40 +01007414static const struct rtl_cfg_info {
7415 void (*hw_start)(struct net_device *);
7416 unsigned int region;
7417 unsigned int align;
7418 u16 event_slow;
7419 unsigned features;
7420 u8 default_ver;
7421} rtl_cfg_infos [] = {
7422 [RTL_CFG_0] = {
7423 .hw_start = rtl_hw_start_8169,
7424 .region = 1,
7425 .align = 0,
7426 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7427 .features = RTL_FEATURE_GMII,
7428 .default_ver = RTL_GIGA_MAC_VER_01,
7429 },
7430 [RTL_CFG_1] = {
7431 .hw_start = rtl_hw_start_8168,
7432 .region = 2,
7433 .align = 8,
7434 .event_slow = SYSErr | LinkChg | RxOverflow,
7435 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
7436 .default_ver = RTL_GIGA_MAC_VER_11,
7437 },
7438 [RTL_CFG_2] = {
7439 .hw_start = rtl_hw_start_8101,
7440 .region = 2,
7441 .align = 8,
7442 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7443 PCSTimeout,
7444 .features = RTL_FEATURE_MSI,
7445 .default_ver = RTL_GIGA_MAC_VER_13,
7446 }
7447};
7448
7449/* Cfg9346_Unlock assumed. */
7450static unsigned rtl_try_msi(struct rtl8169_private *tp,
7451 const struct rtl_cfg_info *cfg)
7452{
7453 void __iomem *ioaddr = tp->mmio_addr;
7454 unsigned msi = 0;
7455 u8 cfg2;
7456
7457 cfg2 = RTL_R8(Config2) & ~MSIEnable;
7458 if (cfg->features & RTL_FEATURE_MSI) {
7459 if (pci_enable_msi(tp->pci_dev)) {
7460 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
7461 } else {
7462 cfg2 |= MSIEnable;
7463 msi = RTL_FEATURE_MSI;
7464 }
7465 }
7466 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
7467 RTL_W8(Config2, cfg2);
7468 return msi;
7469}
7470
Hayes Wangc5583862012-07-02 17:23:22 +08007471DECLARE_RTL_COND(rtl_link_list_ready_cond)
7472{
7473 void __iomem *ioaddr = tp->mmio_addr;
7474
7475 return RTL_R8(MCU) & LINK_LIST_RDY;
7476}
7477
7478DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7479{
7480 void __iomem *ioaddr = tp->mmio_addr;
7481
7482 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7483}
7484
Bill Pembertonbaf63292012-12-03 09:23:28 -05007485static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007486{
7487 void __iomem *ioaddr = tp->mmio_addr;
7488 u32 data;
7489
7490 tp->ocp_base = OCP_STD_PHY_BASE;
7491
7492 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
7493
7494 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7495 return;
7496
7497 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7498 return;
7499
7500 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7501 msleep(1);
7502 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
7503
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007504 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007505 data &= ~(1 << 14);
7506 r8168_mac_ocp_write(tp, 0xe8de, data);
7507
7508 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7509 return;
7510
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007511 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007512 data |= (1 << 15);
7513 r8168_mac_ocp_write(tp, 0xe8de, data);
7514
7515 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7516 return;
7517}
7518
Bill Pembertonbaf63292012-12-03 09:23:28 -05007519static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007520{
7521 switch (tp->mac_version) {
7522 case RTL_GIGA_MAC_VER_40:
7523 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00007524 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00007525 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08007526 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007527 case RTL_GIGA_MAC_VER_45:
7528 case RTL_GIGA_MAC_VER_46:
7529 case RTL_GIGA_MAC_VER_47:
7530 case RTL_GIGA_MAC_VER_48:
Hayes Wangc5583862012-07-02 17:23:22 +08007531 rtl_hw_init_8168g(tp);
7532 break;
7533
7534 default:
7535 break;
7536 }
7537}
7538
hayeswang929a0312014-09-16 11:40:47 +08007539static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007540{
7541 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7542 const unsigned int region = cfg->region;
7543 struct rtl8169_private *tp;
7544 struct mii_if_info *mii;
7545 struct net_device *dev;
7546 void __iomem *ioaddr;
7547 int chipset, i;
7548 int rc;
7549
7550 if (netif_msg_drv(&debug)) {
7551 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7552 MODULENAME, RTL8169_VERSION);
7553 }
7554
7555 dev = alloc_etherdev(sizeof (*tp));
7556 if (!dev) {
7557 rc = -ENOMEM;
7558 goto out;
7559 }
7560
7561 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007562 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007563 tp = netdev_priv(dev);
7564 tp->dev = dev;
7565 tp->pci_dev = pdev;
7566 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7567
7568 mii = &tp->mii;
7569 mii->dev = dev;
7570 mii->mdio_read = rtl_mdio_read;
7571 mii->mdio_write = rtl_mdio_write;
7572 mii->phy_id_mask = 0x1f;
7573 mii->reg_num_mask = 0x1f;
7574 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
7575
7576 /* disable ASPM completely as that cause random device stop working
7577 * problems as well as full system hangs for some PCIe devices users */
7578 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7579 PCIE_LINK_STATE_CLKPM);
7580
7581 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7582 rc = pci_enable_device(pdev);
7583 if (rc < 0) {
7584 netif_err(tp, probe, dev, "enable failure\n");
7585 goto err_out_free_dev_1;
7586 }
7587
7588 if (pci_set_mwi(pdev) < 0)
7589 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
7590
7591 /* make sure PCI base addr 1 is MMIO */
7592 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
7593 netif_err(tp, probe, dev,
7594 "region #%d not an MMIO resource, aborting\n",
7595 region);
7596 rc = -ENODEV;
7597 goto err_out_mwi_2;
7598 }
7599
7600 /* check for weird/broken PCI region reporting */
7601 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7602 netif_err(tp, probe, dev,
7603 "Invalid PCI region size(s), aborting\n");
7604 rc = -ENODEV;
7605 goto err_out_mwi_2;
7606 }
7607
7608 rc = pci_request_regions(pdev, MODULENAME);
7609 if (rc < 0) {
7610 netif_err(tp, probe, dev, "could not request regions\n");
7611 goto err_out_mwi_2;
7612 }
7613
hayeswang929a0312014-09-16 11:40:47 +08007614 tp->cp_cmd = 0;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007615
7616 if ((sizeof(dma_addr_t) > 4) &&
7617 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
7618 tp->cp_cmd |= PCIDAC;
7619 dev->features |= NETIF_F_HIGHDMA;
7620 } else {
7621 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7622 if (rc < 0) {
7623 netif_err(tp, probe, dev, "DMA configuration failed\n");
7624 goto err_out_free_res_3;
7625 }
7626 }
7627
7628 /* ioremap MMIO region */
7629 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
7630 if (!ioaddr) {
7631 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
7632 rc = -EIO;
7633 goto err_out_free_res_3;
7634 }
7635 tp->mmio_addr = ioaddr;
7636
7637 if (!pci_is_pcie(pdev))
7638 netif_info(tp, probe, dev, "not PCI Express\n");
7639
7640 /* Identify chip attached to board */
7641 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
7642
7643 rtl_init_rxcfg(tp);
7644
7645 rtl_irq_disable(tp);
7646
Hayes Wangc5583862012-07-02 17:23:22 +08007647 rtl_hw_initialize(tp);
7648
Francois Romieu3b6cf252012-03-08 09:59:04 +01007649 rtl_hw_reset(tp);
7650
7651 rtl_ack_events(tp, 0xffff);
7652
7653 pci_set_master(pdev);
7654
Francois Romieu3b6cf252012-03-08 09:59:04 +01007655 rtl_init_mdio_ops(tp);
7656 rtl_init_pll_power_ops(tp);
7657 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08007658 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007659
7660 rtl8169_print_mac_version(tp);
7661
7662 chipset = tp->mac_version;
7663 tp->txd_version = rtl_chip_infos[chipset].txd_version;
7664
7665 RTL_W8(Cfg9346, Cfg9346_Unlock);
7666 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02007667 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007668 switch (tp->mac_version) {
7669 case RTL_GIGA_MAC_VER_45:
7670 case RTL_GIGA_MAC_VER_46:
7671 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
7672 tp->features |= RTL_FEATURE_WOL;
7673 if ((RTL_R8(Config3) & LinkUp) != 0)
7674 tp->features |= RTL_FEATURE_WOL;
7675 break;
7676 default:
7677 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
7678 tp->features |= RTL_FEATURE_WOL;
7679 break;
7680 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007681 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
7682 tp->features |= RTL_FEATURE_WOL;
7683 tp->features |= rtl_try_msi(tp, cfg);
7684 RTL_W8(Cfg9346, Cfg9346_Lock);
7685
7686 if (rtl_tbi_enabled(tp)) {
7687 tp->set_speed = rtl8169_set_speed_tbi;
7688 tp->get_settings = rtl8169_gset_tbi;
7689 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7690 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7691 tp->link_ok = rtl8169_tbi_link_ok;
7692 tp->do_ioctl = rtl_tbi_ioctl;
7693 } else {
7694 tp->set_speed = rtl8169_set_speed_xmii;
7695 tp->get_settings = rtl8169_gset_xmii;
7696 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7697 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7698 tp->link_ok = rtl8169_xmii_link_ok;
7699 tp->do_ioctl = rtl_xmii_ioctl;
7700 }
7701
7702 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007703 u64_stats_init(&tp->rx_stats.syncp);
7704 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007705
7706 /* Get MAC address */
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007707 if (tp->mac_version == RTL_GIGA_MAC_VER_45 ||
7708 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
7709 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
7710 tp->mac_version == RTL_GIGA_MAC_VER_48) {
7711 u16 mac_addr[3];
7712
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007713 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7714 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007715
7716 if (is_valid_ether_addr((u8 *)mac_addr))
7717 rtl_rar_set(tp, (u8 *)mac_addr);
7718 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007719 for (i = 0; i < ETH_ALEN; i++)
7720 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007721
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007722 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007723 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007724
7725 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
7726
7727 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7728 * properly for all devices */
7729 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007730 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007731
7732 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007733 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7734 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007735 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7736 NETIF_F_HIGHDMA;
7737
hayeswang929a0312014-09-16 11:40:47 +08007738 tp->cp_cmd |= RxChkSum | RxVlan;
7739
7740 /*
7741 * Pretend we are using VLANs; This bypasses a nasty bug where
7742 * Interrupts stop flowing on high load on 8110SCd controllers.
7743 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007744 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007745 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007746 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007747
hayeswang5888d3f2014-07-11 16:25:56 +08007748 if (tp->txd_version == RTL_TD_0)
7749 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08007750 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08007751 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007752 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7753 } else
hayeswang5888d3f2014-07-11 16:25:56 +08007754 WARN_ON_ONCE(1);
7755
Francois Romieu3b6cf252012-03-08 09:59:04 +01007756 dev->hw_features |= NETIF_F_RXALL;
7757 dev->hw_features |= NETIF_F_RXFCS;
7758
7759 tp->hw_start = cfg->hw_start;
7760 tp->event_slow = cfg->event_slow;
7761
7762 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
7763 ~(RxBOVF | RxFOVF) : ~0;
7764
7765 init_timer(&tp->timer);
7766 tp->timer.data = (unsigned long) dev;
7767 tp->timer.function = rtl8169_phy_timer;
7768
7769 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7770
7771 rc = register_netdev(dev);
7772 if (rc < 0)
7773 goto err_out_msi_4;
7774
7775 pci_set_drvdata(pdev, dev);
7776
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007777 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
7778 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
7779 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007780 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7781 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7782 "tx checksumming: %s]\n",
7783 rtl_chip_infos[chipset].jumbo_max,
7784 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
7785 }
7786
7787 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7788 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7789 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7790 rtl8168_driver_start(tp);
7791 }
7792
7793 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7794
7795 if (pci_dev_run_wake(pdev))
7796 pm_runtime_put_noidle(&pdev->dev);
7797
7798 netif_carrier_off(dev);
7799
7800out:
7801 return rc;
7802
7803err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007804 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007805 rtl_disable_msi(pdev, tp);
7806 iounmap(ioaddr);
7807err_out_free_res_3:
7808 pci_release_regions(pdev);
7809err_out_mwi_2:
7810 pci_clear_mwi(pdev);
7811 pci_disable_device(pdev);
7812err_out_free_dev_1:
7813 free_netdev(dev);
7814 goto out;
7815}
7816
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817static struct pci_driver rtl8169_pci_driver = {
7818 .name = MODULENAME,
7819 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007820 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007821 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007822 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007823 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824};
7825
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007826module_pci_driver(rtl8169_pci_driver);