blob: 7a7860a17f695017eb03fe3ddd9c50c2787b4273 [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"
françois romieubca03d52011-01-03 15:07:31 +000055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef RTL8169_DEBUG
57#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020058 if (!(expr)) { \
59 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070060 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020061 }
Joe Perches06fa7352007-10-18 21:15:00 +020062#define dprintk(fmt, args...) \
63 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#else
65#define assert(expr) do {} while (0)
66#define dprintk(fmt, args...) do {} while (0)
67#endif /* RTL8169_DEBUG */
68
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020069#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070070 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020071
Julien Ducourthial477206a2012-05-09 00:00:06 +020072#define TX_SLOTS_AVAIL(tp) \
73 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
74
75/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
76#define TX_FRAGS_READY_FOR(tp,nr_frags) \
77 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
80 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050081static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Francois Romieu9c14cea2008-07-05 00:21:15 +020083#define MAX_READ_REQUEST_SHIFT 12
Michal Schmidtaee77e42012-09-09 13:55:26 +000084#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
86
87#define R8169_REGS_SIZE 256
88#define R8169_NAPI_WEIGHT 64
89#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000090#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
92#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
93
94#define RTL8169_TX_TIMEOUT (6*HZ)
95#define RTL8169_PHY_TIMEOUT (10*HZ)
96
97/* write/read MMIO register */
98#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
99#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
100#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
101#define RTL_R8(reg) readb (ioaddr + (reg))
102#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000103#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200106 RTL_GIGA_MAC_VER_01 = 0,
107 RTL_GIGA_MAC_VER_02,
108 RTL_GIGA_MAC_VER_03,
109 RTL_GIGA_MAC_VER_04,
110 RTL_GIGA_MAC_VER_05,
111 RTL_GIGA_MAC_VER_06,
112 RTL_GIGA_MAC_VER_07,
113 RTL_GIGA_MAC_VER_08,
114 RTL_GIGA_MAC_VER_09,
115 RTL_GIGA_MAC_VER_10,
116 RTL_GIGA_MAC_VER_11,
117 RTL_GIGA_MAC_VER_12,
118 RTL_GIGA_MAC_VER_13,
119 RTL_GIGA_MAC_VER_14,
120 RTL_GIGA_MAC_VER_15,
121 RTL_GIGA_MAC_VER_16,
122 RTL_GIGA_MAC_VER_17,
123 RTL_GIGA_MAC_VER_18,
124 RTL_GIGA_MAC_VER_19,
125 RTL_GIGA_MAC_VER_20,
126 RTL_GIGA_MAC_VER_21,
127 RTL_GIGA_MAC_VER_22,
128 RTL_GIGA_MAC_VER_23,
129 RTL_GIGA_MAC_VER_24,
130 RTL_GIGA_MAC_VER_25,
131 RTL_GIGA_MAC_VER_26,
132 RTL_GIGA_MAC_VER_27,
133 RTL_GIGA_MAC_VER_28,
134 RTL_GIGA_MAC_VER_29,
135 RTL_GIGA_MAC_VER_30,
136 RTL_GIGA_MAC_VER_31,
137 RTL_GIGA_MAC_VER_32,
138 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800139 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800140 RTL_GIGA_MAC_VER_35,
141 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800142 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800143 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800144 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800145 RTL_GIGA_MAC_VER_40,
146 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000147 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000148 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800149 RTL_GIGA_MAC_VER_44,
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Francois Romieu2b7b4312011-04-18 22:53:24 -0700153enum rtl_tx_desc_version {
154 RTL_TD_0 = 0,
155 RTL_TD_1 = 1,
156};
157
Francois Romieud58d46b2011-05-03 16:38:29 +0200158#define JUMBO_1K ETH_DATA_LEN
159#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
160#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
161#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
162#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
163
164#define _R(NAME,TD,FW,SZ,B) { \
165 .name = NAME, \
166 .txd_version = TD, \
167 .fw_name = FW, \
168 .jumbo_max = SZ, \
169 .jumbo_tx_csum = B \
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800172static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700174 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 u16 jumbo_max;
177 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200178} rtl_chip_infos[] = {
179 /* PCI devices. */
180 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 /* PCI-E devices. */
193 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_17] =
hayeswangf75761b2014-03-11 15:11:59 +0800214 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200216 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200217 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200221 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200222 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200223 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200224 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
231 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
234 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200236 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200239 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200240 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
241 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200242 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200243 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
244 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200245 [RTL_GIGA_MAC_VER_31] =
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_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200248 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
249 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200250 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200251 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
252 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800253 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200254 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
255 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800256 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200257 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
258 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800259 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200260 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
261 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800262 [RTL_GIGA_MAC_VER_37] =
263 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
264 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800265 [RTL_GIGA_MAC_VER_38] =
266 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
267 JUMBO_9K, false),
Hayes Wang5598bfe2012-07-02 17:23:21 +0800268 [RTL_GIGA_MAC_VER_39] =
269 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
270 JUMBO_1K, true),
Hayes Wangc5583862012-07-02 17:23:22 +0800271 [RTL_GIGA_MAC_VER_40] =
hayeswangbeb330a2013-04-01 22:23:39 +0000272 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
Hayes Wangc5583862012-07-02 17:23:22 +0800273 JUMBO_9K, false),
274 [RTL_GIGA_MAC_VER_41] =
275 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
hayeswang57538c42013-04-01 22:23:40 +0000276 [RTL_GIGA_MAC_VER_42] =
277 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
278 JUMBO_9K, false),
hayeswang58152cd2013-04-01 22:23:42 +0000279 [RTL_GIGA_MAC_VER_43] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
281 JUMBO_1K, true),
hayeswang45dd95c2013-07-08 17:09:01 +0800282 [RTL_GIGA_MAC_VER_44] =
283 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
284 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286#undef _R
287
Francois Romieubcf0bf92006-07-26 23:14:13 +0200288enum cfg_version {
289 RTL_CFG_0 = 0x00,
290 RTL_CFG_1,
291 RTL_CFG_2
292};
293
Benoit Taine9baa3c32014-08-08 15:56:03 +0200294static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200295 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200296 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200297 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100298 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200299 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200300 { PCI_VENDOR_ID_DLINK, 0x4300,
301 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200302 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000303 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200304 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200305 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
306 { PCI_VENDOR_ID_LINKSYS, 0x1032,
307 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100308 { 0x0001, 0x8168,
309 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 {0,},
311};
312
313MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
314
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000315static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700316static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200317static struct {
318 u32 msg_enable;
319} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Francois Romieu07d3f512007-02-21 22:40:46 +0100321enum rtl_registers {
322 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100323 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100324 MAR0 = 8, /* Multicast filter. */
325 CounterAddrLow = 0x10,
326 CounterAddrHigh = 0x14,
327 TxDescStartAddrLow = 0x20,
328 TxDescStartAddrHigh = 0x24,
329 TxHDescStartAddrLow = 0x28,
330 TxHDescStartAddrHigh = 0x2c,
331 FLASH = 0x30,
332 ERSR = 0x36,
333 ChipCmd = 0x37,
334 TxPoll = 0x38,
335 IntrMask = 0x3c,
336 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700337
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800338 TxConfig = 0x40,
339#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
340#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
341
342 RxConfig = 0x44,
343#define RX128_INT_EN (1 << 15) /* 8111c and later */
344#define RX_MULTI_EN (1 << 14) /* 8111c only */
345#define RXCFG_FIFO_SHIFT 13
346 /* No threshold before first PCI xfer */
347#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000348#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800349#define RXCFG_DMA_SHIFT 8
350 /* Unlimited maximum PCI burst. */
351#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700352
Francois Romieu07d3f512007-02-21 22:40:46 +0100353 RxMissed = 0x4c,
354 Cfg9346 = 0x50,
355 Config0 = 0x51,
356 Config1 = 0x52,
357 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200358#define PME_SIGNAL (1 << 5) /* 8168c and later */
359
Francois Romieu07d3f512007-02-21 22:40:46 +0100360 Config3 = 0x54,
361 Config4 = 0x55,
362 Config5 = 0x56,
363 MultiIntr = 0x5c,
364 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100365 PHYstatus = 0x6c,
366 RxMaxSize = 0xda,
367 CPlusCmd = 0xe0,
368 IntrMitigate = 0xe2,
369 RxDescAddrLow = 0xe4,
370 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000371 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
372
373#define NoEarlyTx 0x3f /* Max value : no early transmit. */
374
375 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
376
377#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800378#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000379
Francois Romieu07d3f512007-02-21 22:40:46 +0100380 FuncEvent = 0xf0,
381 FuncEventMask = 0xf4,
382 FuncPresetState = 0xf8,
383 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384};
385
Francois Romieuf162a5d2008-06-01 22:37:49 +0200386enum rtl8110_registers {
387 TBICSR = 0x64,
388 TBI_ANAR = 0x68,
389 TBI_LPAR = 0x6a,
390};
391
392enum rtl8168_8101_registers {
393 CSIDR = 0x64,
394 CSIAR = 0x68,
395#define CSIAR_FLAG 0x80000000
396#define CSIAR_WRITE_CMD 0x80000000
397#define CSIAR_BYTE_ENABLE 0x0f
398#define CSIAR_BYTE_ENABLE_SHIFT 12
399#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800400#define CSIAR_FUNC_CARD 0x00000000
401#define CSIAR_FUNC_SDIO 0x00010000
402#define CSIAR_FUNC_NIC 0x00020000
hayeswang45dd95c2013-07-08 17:09:01 +0800403#define CSIAR_FUNC_NIC2 0x00010000
françois romieu065c27c2011-01-03 15:08:12 +0000404 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200405 EPHYAR = 0x80,
406#define EPHYAR_FLAG 0x80000000
407#define EPHYAR_WRITE_CMD 0x80000000
408#define EPHYAR_REG_MASK 0x1f
409#define EPHYAR_REG_SHIFT 16
410#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800411 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800412#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200413 DBG_REG = 0xd1,
414#define FIX_NAK_1 (1 << 4)
415#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800416 TWSI = 0xd2,
417 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800418#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800419#define TX_EMPTY (1 << 5)
420#define RX_EMPTY (1 << 4)
421#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800422#define EN_NDP (1 << 3)
423#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800424#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000425 EFUSEAR = 0xdc,
426#define EFUSEAR_FLAG 0x80000000
427#define EFUSEAR_WRITE_CMD 0x80000000
428#define EFUSEAR_READ_CMD 0x00000000
429#define EFUSEAR_REG_MASK 0x03ff
430#define EFUSEAR_REG_SHIFT 8
431#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200432};
433
françois romieuc0e45c12011-01-03 15:08:04 +0000434enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800435 LED_FREQ = 0x1a,
436 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000437 ERIDR = 0x70,
438 ERIAR = 0x74,
439#define ERIAR_FLAG 0x80000000
440#define ERIAR_WRITE_CMD 0x80000000
441#define ERIAR_READ_CMD 0x00000000
442#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000443#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800444#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
445#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
446#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
447#define ERIAR_MASK_SHIFT 12
448#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
449#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800450#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800451#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000452 EPHY_RXER_NUM = 0x7c,
453 OCPDR = 0xb0, /* OCP GPHY access */
454#define OCPDR_WRITE_CMD 0x80000000
455#define OCPDR_READ_CMD 0x00000000
456#define OCPDR_REG_MASK 0x7f
457#define OCPDR_GPHY_REG_SHIFT 16
458#define OCPDR_DATA_MASK 0xffff
459 OCPAR = 0xb4,
460#define OCPAR_FLAG 0x80000000
461#define OCPAR_GPHY_WRITE_CMD 0x8000f060
462#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800463 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000464 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
465 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200466#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800467#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800468#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800469#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800470#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000471};
472
Francois Romieu07d3f512007-02-21 22:40:46 +0100473enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100475 SYSErr = 0x8000,
476 PCSTimeout = 0x4000,
477 SWInt = 0x0100,
478 TxDescUnavail = 0x0080,
479 RxFIFOOver = 0x0040,
480 LinkChg = 0x0020,
481 RxOverflow = 0x0010,
482 TxErr = 0x0008,
483 TxOK = 0x0004,
484 RxErr = 0x0002,
485 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400488 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200489 RxFOVF = (1 << 23),
490 RxRWT = (1 << 22),
491 RxRES = (1 << 21),
492 RxRUNT = (1 << 20),
493 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800496 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100497 CmdReset = 0x10,
498 CmdRxEnb = 0x08,
499 CmdTxEnb = 0x04,
500 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Francois Romieu275391a2007-02-23 23:50:28 +0100502 /* TXPoll register p.5 */
503 HPQ = 0x80, /* Poll cmd on the high prio queue */
504 NPQ = 0x40, /* Poll cmd on the low prio queue */
505 FSWInt = 0x01, /* Forced software interrupt */
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100508 Cfg9346_Lock = 0x00,
509 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100512 AcceptErr = 0x20,
513 AcceptRunt = 0x10,
514 AcceptBroadcast = 0x08,
515 AcceptMulticast = 0x04,
516 AcceptMyPhys = 0x02,
517 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200518#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* TxConfigBits */
521 TxInterFrameGapShift = 24,
522 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
523
Francois Romieu5d06a992006-02-23 00:47:58 +0100524 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200525 LEDS1 = (1 << 7),
526 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200527 Speed_down = (1 << 4),
528 MEMMAP = (1 << 3),
529 IOMAP = (1 << 2),
530 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100531 PMEnable = (1 << 0), /* Power Management Enable */
532
Francois Romieu6dccd162007-02-13 23:38:05 +0100533 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000534 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000535 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100536 PCI_Clock_66MHz = 0x01,
537 PCI_Clock_33MHz = 0x00,
538
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100539 /* Config3 register p.25 */
540 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
541 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200542 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800543 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200544 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100545
Francois Romieud58d46b2011-05-03 16:38:29 +0200546 /* Config4 register */
547 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
548
Francois Romieu5d06a992006-02-23 00:47:58 +0100549 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100550 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
551 MWF = (1 << 5), /* Accept Multicast wakeup frame */
552 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200553 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100554 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100555 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000556 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* TBICSR p.28 */
559 TBIReset = 0x80000000,
560 TBILoopback = 0x40000000,
561 TBINwEnable = 0x20000000,
562 TBINwRestart = 0x10000000,
563 TBILinkOk = 0x02000000,
564 TBINwComplete = 0x01000000,
565
566 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200567 EnableBist = (1 << 15), // 8168 8101
568 Mac_dbgo_oe = (1 << 14), // 8168 8101
569 Normal_mode = (1 << 13), // unused
570 Force_half_dup = (1 << 12), // 8168 8101
571 Force_rxflow_en = (1 << 11), // 8168 8101
572 Force_txflow_en = (1 << 10), // 8168 8101
573 Cxpl_dbg_sel = (1 << 9), // 8168 8101
574 ASF = (1 << 8), // 8168 8101
575 PktCntrDisable = (1 << 7), // 8168 8101
576 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 RxVlan = (1 << 6),
578 RxChkSum = (1 << 5),
579 PCIDAC = (1 << 4),
580 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100581 INTT_0 = 0x0000, // 8168
582 INTT_1 = 0x0001, // 8168
583 INTT_2 = 0x0002, // 8168
584 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100587 TBI_Enable = 0x80,
588 TxFlowCtrl = 0x40,
589 RxFlowCtrl = 0x20,
590 _1000bpsF = 0x10,
591 _100bps = 0x08,
592 _10bps = 0x04,
593 LinkStatus = 0x02,
594 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100597 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200598
599 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100600 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Francois Romieu2b7b4312011-04-18 22:53:24 -0700603enum rtl_desc_bit {
604 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
606 RingEnd = (1 << 30), /* End of descriptor ring */
607 FirstFrag = (1 << 29), /* First segment of a packet */
608 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700609};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Francois Romieu2b7b4312011-04-18 22:53:24 -0700611/* Generic case. */
612enum rtl_tx_desc_bit {
613 /* First doubleword. */
614 TD_LSO = (1 << 27), /* Large Send Offload */
615#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Francois Romieu2b7b4312011-04-18 22:53:24 -0700617 /* Second doubleword. */
618 TxVlanTag = (1 << 17), /* Add VLAN tag */
619};
620
621/* 8169, 8168b and 810x except 8102e. */
622enum rtl_tx_desc_bit_0 {
623 /* First doubleword. */
624#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
625 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
626 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
627 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
628};
629
630/* 8102e, 8168c and beyond. */
631enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800632 /* First doubleword. */
633 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800634 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800635#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800636#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800637
Francois Romieu2b7b4312011-04-18 22:53:24 -0700638 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800639#define TCPHO_SHIFT 18
640#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700641#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800642 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
643 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700644 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
645 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
646};
647
Francois Romieu2b7b4312011-04-18 22:53:24 -0700648enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Rx private */
650 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
651 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
652
653#define RxProtoUDP (PID1)
654#define RxProtoTCP (PID0)
655#define RxProtoIP (PID1 | PID0)
656#define RxProtoMask RxProtoIP
657
658 IPFail = (1 << 16), /* IP checksum failed */
659 UDPFail = (1 << 15), /* UDP/IP checksum failed */
660 TCPFail = (1 << 14), /* TCP/IP checksum failed */
661 RxVlanTag = (1 << 16), /* VLAN tag available */
662};
663
664#define RsvdMask 0x3fffc000
665
666struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200667 __le32 opts1;
668 __le32 opts2;
669 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670};
671
672struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200673 __le32 opts1;
674 __le32 opts2;
675 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676};
677
678struct ring_info {
679 struct sk_buff *skb;
680 u32 len;
681 u8 __pad[sizeof(void *) - sizeof(u32)];
682};
683
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200684enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200685 RTL_FEATURE_WOL = (1 << 0),
686 RTL_FEATURE_MSI = (1 << 1),
687 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200688};
689
Ivan Vecera355423d2009-02-06 21:49:57 -0800690struct rtl8169_counters {
691 __le64 tx_packets;
692 __le64 rx_packets;
693 __le64 tx_errors;
694 __le32 rx_errors;
695 __le16 rx_missed;
696 __le16 align_errors;
697 __le32 tx_one_collision;
698 __le32 tx_multi_collision;
699 __le64 rx_unicast;
700 __le64 rx_broadcast;
701 __le32 rx_multicast;
702 __le16 tx_aborted;
703 __le16 tx_underun;
704};
705
Francois Romieuda78dbf2012-01-26 14:18:23 +0100706enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100707 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100708 RTL_FLAG_TASK_SLOW_PENDING,
709 RTL_FLAG_TASK_RESET_PENDING,
710 RTL_FLAG_TASK_PHY_PENDING,
711 RTL_FLAG_MAX
712};
713
Junchang Wang8027aa22012-03-04 23:30:32 +0100714struct rtl8169_stats {
715 u64 packets;
716 u64 bytes;
717 struct u64_stats_sync syncp;
718};
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720struct rtl8169_private {
721 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200722 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000723 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700724 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200725 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700726 u16 txd_version;
727 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
729 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100731 struct rtl8169_stats rx_stats;
732 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
734 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
735 dma_addr_t TxPhyAddr;
736 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000737 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct timer_list timer;
740 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100741
742 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000743
744 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200745 void (*write)(struct rtl8169_private *, int, int);
746 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000747 } mdio_ops;
748
françois romieu065c27c2011-01-03 15:08:12 +0000749 struct pll_power_ops {
750 void (*down)(struct rtl8169_private *);
751 void (*up)(struct rtl8169_private *);
752 } pll_power_ops;
753
Francois Romieud58d46b2011-05-03 16:38:29 +0200754 struct jumbo_ops {
755 void (*enable)(struct rtl8169_private *);
756 void (*disable)(struct rtl8169_private *);
757 } jumbo_ops;
758
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800759 struct csi_ops {
Francois Romieu52989f02012-07-06 13:37:00 +0200760 void (*write)(struct rtl8169_private *, int, int);
761 u32 (*read)(struct rtl8169_private *, int);
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800762 } csi_ops;
763
Oliver Neukum54405cd2011-01-06 21:55:13 +0100764 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200765 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000766 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100767 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000768 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800770 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
hayeswang5888d3f2014-07-11 16:25:56 +0800771 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100772
773 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100774 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
775 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100776 struct work_struct work;
777 } wk;
778
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200779 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200780
781 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800782 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000783 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400784 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000785
Francois Romieub6ffd972011-06-17 17:00:05 +0200786 struct rtl_fw {
787 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200788
789#define RTL_VER_SIZE 32
790
791 char version[RTL_VER_SIZE];
792
793 struct rtl_fw_phy_action {
794 __le32 *code;
795 size_t size;
796 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200797 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300798#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800799
800 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801};
802
Ralf Baechle979b6c12005-06-13 14:30:40 -0700803MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700806MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200807module_param_named(debug, debug.msg_enable, int, 0);
808MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809MODULE_LICENSE("GPL");
810MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000811MODULE_FIRMWARE(FIRMWARE_8168D_1);
812MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000813MODULE_FIRMWARE(FIRMWARE_8168E_1);
814MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400815MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800816MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800817MODULE_FIRMWARE(FIRMWARE_8168F_1);
818MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800819MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800820MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800821MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800822MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000823MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000824MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000825MODULE_FIRMWARE(FIRMWARE_8168G_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Francois Romieuda78dbf2012-01-26 14:18:23 +0100827static void rtl_lock_work(struct rtl8169_private *tp)
828{
829 mutex_lock(&tp->wk.mutex);
830}
831
832static void rtl_unlock_work(struct rtl8169_private *tp)
833{
834 mutex_unlock(&tp->wk.mutex);
835}
836
Francois Romieud58d46b2011-05-03 16:38:29 +0200837static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
838{
Jiang Liu7d7903b2012-07-24 17:20:16 +0800839 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
840 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200841}
842
Francois Romieuffc46952012-07-06 14:19:23 +0200843struct rtl_cond {
844 bool (*check)(struct rtl8169_private *);
845 const char *msg;
846};
847
848static void rtl_udelay(unsigned int d)
849{
850 udelay(d);
851}
852
853static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
854 void (*delay)(unsigned int), unsigned int d, int n,
855 bool high)
856{
857 int i;
858
859 for (i = 0; i < n; i++) {
860 delay(d);
861 if (c->check(tp) == high)
862 return true;
863 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200864 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
865 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200866 return false;
867}
868
869static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
870 const struct rtl_cond *c,
871 unsigned int d, int n)
872{
873 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
874}
875
876static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
877 const struct rtl_cond *c,
878 unsigned int d, int n)
879{
880 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
881}
882
883static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
884 const struct rtl_cond *c,
885 unsigned int d, int n)
886{
887 return rtl_loop_wait(tp, c, msleep, d, n, true);
888}
889
890static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
891 const struct rtl_cond *c,
892 unsigned int d, int n)
893{
894 return rtl_loop_wait(tp, c, msleep, d, n, false);
895}
896
897#define DECLARE_RTL_COND(name) \
898static bool name ## _check(struct rtl8169_private *); \
899 \
900static const struct rtl_cond name = { \
901 .check = name ## _check, \
902 .msg = #name \
903}; \
904 \
905static bool name ## _check(struct rtl8169_private *tp)
906
907DECLARE_RTL_COND(rtl_ocpar_cond)
908{
909 void __iomem *ioaddr = tp->mmio_addr;
910
911 return RTL_R32(OCPAR) & OCPAR_FLAG;
912}
913
françois romieub646d902011-01-03 15:08:21 +0000914static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
915{
916 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000917
918 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200919
920 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
921 RTL_R32(OCPDR) : ~0;
françois romieub646d902011-01-03 15:08:21 +0000922}
923
924static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
925{
926 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000927
928 RTL_W32(OCPDR, data);
929 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Francois Romieuffc46952012-07-06 14:19:23 +0200930
931 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
932}
933
934DECLARE_RTL_COND(rtl_eriar_cond)
935{
936 void __iomem *ioaddr = tp->mmio_addr;
937
938 return RTL_R32(ERIAR) & ERIAR_FLAG;
françois romieub646d902011-01-03 15:08:21 +0000939}
940
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800941static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000942{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800943 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000944
945 RTL_W8(ERIDR, cmd);
946 RTL_W32(ERIAR, 0x800010e8);
947 msleep(2);
Francois Romieuffc46952012-07-06 14:19:23 +0200948
949 if (!rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 5))
950 return;
françois romieub646d902011-01-03 15:08:21 +0000951
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800952 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000953}
954
955#define OOB_CMD_RESET 0x00
956#define OOB_CMD_DRIVER_START 0x05
957#define OOB_CMD_DRIVER_STOP 0x06
958
Francois Romieucecb5fd2011-04-01 10:21:07 +0200959static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
960{
961 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
962}
963
Francois Romieuffc46952012-07-06 14:19:23 +0200964DECLARE_RTL_COND(rtl_ocp_read_cond)
françois romieub646d902011-01-03 15:08:21 +0000965{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200966 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000967
Francois Romieucecb5fd2011-04-01 10:21:07 +0200968 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000969
Francois Romieuffc46952012-07-06 14:19:23 +0200970 return ocp_read(tp, 0x0f, reg) & 0x00000800;
971}
972
973static void rtl8168_driver_start(struct rtl8169_private *tp)
974{
975 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
976
977 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000978}
979
980static void rtl8168_driver_stop(struct rtl8169_private *tp)
981{
françois romieub646d902011-01-03 15:08:21 +0000982 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
983
Francois Romieuffc46952012-07-06 14:19:23 +0200984 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
françois romieub646d902011-01-03 15:08:21 +0000985}
986
hayeswang4804b3b2011-03-21 01:50:29 +0000987static int r8168dp_check_dash(struct rtl8169_private *tp)
988{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200989 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000990
Francois Romieucecb5fd2011-04-01 10:21:07 +0200991 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000992}
françois romieub646d902011-01-03 15:08:21 +0000993
Hayes Wangc5583862012-07-02 17:23:22 +0800994static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
995{
996 if (reg & 0xffff0001) {
997 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
998 return true;
999 }
1000 return false;
1001}
1002
1003DECLARE_RTL_COND(rtl_ocp_gphy_cond)
1004{
1005 void __iomem *ioaddr = tp->mmio_addr;
1006
1007 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
1008}
1009
1010static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1011{
1012 void __iomem *ioaddr = tp->mmio_addr;
1013
1014 if (rtl_ocp_reg_failure(tp, reg))
1015 return;
1016
1017 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
1018
1019 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1020}
1021
1022static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1023{
1024 void __iomem *ioaddr = tp->mmio_addr;
1025
1026 if (rtl_ocp_reg_failure(tp, reg))
1027 return 0;
1028
1029 RTL_W32(GPHY_OCP, reg << 15);
1030
1031 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1032 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1033}
1034
Hayes Wangc5583862012-07-02 17:23:22 +08001035static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1036{
1037 void __iomem *ioaddr = tp->mmio_addr;
1038
1039 if (rtl_ocp_reg_failure(tp, reg))
1040 return;
1041
1042 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +08001043}
1044
1045static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1046{
1047 void __iomem *ioaddr = tp->mmio_addr;
1048
1049 if (rtl_ocp_reg_failure(tp, reg))
1050 return 0;
1051
1052 RTL_W32(OCPDR, reg << 15);
1053
Hayes Wang3a83ad12012-07-11 20:31:56 +08001054 return RTL_R32(OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +08001055}
1056
1057#define OCP_STD_PHY_BASE 0xa400
1058
1059static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1060{
1061 if (reg == 0x1f) {
1062 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1063 return;
1064 }
1065
1066 if (tp->ocp_base != OCP_STD_PHY_BASE)
1067 reg -= 0x10;
1068
1069 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1070}
1071
1072static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1073{
1074 if (tp->ocp_base != OCP_STD_PHY_BASE)
1075 reg -= 0x10;
1076
1077 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1078}
1079
hayeswangeee37862013-04-01 22:23:38 +00001080static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1081{
1082 if (reg == 0x1f) {
1083 tp->ocp_base = value << 4;
1084 return;
1085 }
1086
1087 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1088}
1089
1090static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1091{
1092 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1093}
1094
Francois Romieuffc46952012-07-06 14:19:23 +02001095DECLARE_RTL_COND(rtl_phyar_cond)
1096{
1097 void __iomem *ioaddr = tp->mmio_addr;
1098
1099 return RTL_R32(PHYAR) & 0x80000000;
1100}
1101
Francois Romieu24192212012-07-06 20:19:42 +02001102static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Francois Romieu24192212012-07-06 20:19:42 +02001104 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Francois Romieu24192212012-07-06 20:19:42 +02001106 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Francois Romieuffc46952012-07-06 14:19:23 +02001108 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -07001109 /*
Timo Teräs81a95f02010-06-09 17:31:48 -07001110 * According to hardware specs a 20us delay is required after write
1111 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -07001112 */
Timo Teräs81a95f02010-06-09 17:31:48 -07001113 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Francois Romieu24192212012-07-06 20:19:42 +02001116static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Francois Romieu24192212012-07-06 20:19:42 +02001118 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieuffc46952012-07-06 14:19:23 +02001119 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Francois Romieu24192212012-07-06 20:19:42 +02001121 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Francois Romieuffc46952012-07-06 14:19:23 +02001123 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1124 RTL_R32(PHYAR) & 0xffff : ~0;
1125
Timo Teräs81a95f02010-06-09 17:31:48 -07001126 /*
1127 * According to hardware specs a 20us delay is required after read
1128 * complete indication, but before sending next command.
1129 */
1130 udelay(20);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return value;
1133}
1134
Francois Romieu24192212012-07-06 20:19:42 +02001135static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +00001136{
Francois Romieu24192212012-07-06 20:19:42 +02001137 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001138
Francois Romieu24192212012-07-06 20:19:42 +02001139 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
françois romieuc0e45c12011-01-03 15:08:04 +00001140 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1141 RTL_W32(EPHY_RXER_NUM, 0);
1142
Francois Romieuffc46952012-07-06 14:19:23 +02001143 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +00001144}
1145
Francois Romieu24192212012-07-06 20:19:42 +02001146static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +00001147{
Francois Romieu24192212012-07-06 20:19:42 +02001148 r8168dp_1_mdio_access(tp, reg,
1149 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +00001150}
1151
Francois Romieu24192212012-07-06 20:19:42 +02001152static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +00001153{
Francois Romieu24192212012-07-06 20:19:42 +02001154 void __iomem *ioaddr = tp->mmio_addr;
françois romieuc0e45c12011-01-03 15:08:04 +00001155
Francois Romieu24192212012-07-06 20:19:42 +02001156 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +00001157
1158 mdelay(1);
1159 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1160 RTL_W32(EPHY_RXER_NUM, 0);
1161
Francois Romieuffc46952012-07-06 14:19:23 +02001162 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1163 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +00001164}
1165
françois romieue6de30d2011-01-03 15:08:37 +00001166#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1167
1168static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1169{
1170 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1171}
1172
1173static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1174{
1175 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1176}
1177
Francois Romieu24192212012-07-06 20:19:42 +02001178static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +00001179{
Francois Romieu24192212012-07-06 20:19:42 +02001180 void __iomem *ioaddr = tp->mmio_addr;
1181
françois romieue6de30d2011-01-03 15:08:37 +00001182 r8168dp_2_mdio_start(ioaddr);
1183
Francois Romieu24192212012-07-06 20:19:42 +02001184 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001185
1186 r8168dp_2_mdio_stop(ioaddr);
1187}
1188
Francois Romieu24192212012-07-06 20:19:42 +02001189static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001190{
Francois Romieu24192212012-07-06 20:19:42 +02001191 void __iomem *ioaddr = tp->mmio_addr;
françois romieue6de30d2011-01-03 15:08:37 +00001192 int value;
1193
1194 r8168dp_2_mdio_start(ioaddr);
1195
Francois Romieu24192212012-07-06 20:19:42 +02001196 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001197
1198 r8168dp_2_mdio_stop(ioaddr);
1199
1200 return value;
1201}
1202
françois romieu4da19632011-01-03 15:07:55 +00001203static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001204{
Francois Romieu24192212012-07-06 20:19:42 +02001205 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001206}
1207
françois romieu4da19632011-01-03 15:07:55 +00001208static int rtl_readphy(struct rtl8169_private *tp, int location)
1209{
Francois Romieu24192212012-07-06 20:19:42 +02001210 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001211}
1212
1213static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1214{
1215 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1216}
1217
1218static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001219{
1220 int val;
1221
françois romieu4da19632011-01-03 15:07:55 +00001222 val = rtl_readphy(tp, reg_addr);
1223 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001224}
1225
Francois Romieuccdffb92008-07-26 14:26:06 +02001226static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1227 int val)
1228{
1229 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001230
françois romieu4da19632011-01-03 15:07:55 +00001231 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001232}
1233
1234static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1235{
1236 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001237
françois romieu4da19632011-01-03 15:07:55 +00001238 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001239}
1240
Francois Romieuffc46952012-07-06 14:19:23 +02001241DECLARE_RTL_COND(rtl_ephyar_cond)
1242{
1243 void __iomem *ioaddr = tp->mmio_addr;
1244
1245 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1246}
1247
Francois Romieufdf6fc02012-07-06 22:40:38 +02001248static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001249{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001250 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001251
1252 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1253 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1254
Francois Romieuffc46952012-07-06 14:19:23 +02001255 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1256
1257 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001258}
1259
Francois Romieufdf6fc02012-07-06 22:40:38 +02001260static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001261{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001262 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieudacf8152008-08-02 20:44:13 +02001263
1264 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1265
Francois Romieuffc46952012-07-06 14:19:23 +02001266 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1267 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001268}
1269
Francois Romieufdf6fc02012-07-06 22:40:38 +02001270static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1271 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001272{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001273 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001274
1275 BUG_ON((addr & 3) || (mask == 0));
1276 RTL_W32(ERIDR, val);
1277 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1278
Francois Romieuffc46952012-07-06 14:19:23 +02001279 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001280}
1281
Francois Romieufdf6fc02012-07-06 22:40:38 +02001282static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001283{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001284 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang133ac402011-07-06 15:58:05 +08001285
1286 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1287
Francois Romieuffc46952012-07-06 14:19:23 +02001288 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1289 RTL_R32(ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001290}
1291
Francois Romieufdf6fc02012-07-06 22:40:38 +02001292static void rtl_w1w0_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1293 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001294{
1295 u32 val;
1296
Francois Romieufdf6fc02012-07-06 22:40:38 +02001297 val = rtl_eri_read(tp, addr, type);
1298 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001299}
1300
françois romieuc28aa382011-08-02 03:53:43 +00001301struct exgmac_reg {
1302 u16 addr;
1303 u16 mask;
1304 u32 val;
1305};
1306
Francois Romieufdf6fc02012-07-06 22:40:38 +02001307static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001308 const struct exgmac_reg *r, int len)
1309{
1310 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001311 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001312 r++;
1313 }
1314}
1315
Francois Romieuffc46952012-07-06 14:19:23 +02001316DECLARE_RTL_COND(rtl_efusear_cond)
1317{
1318 void __iomem *ioaddr = tp->mmio_addr;
1319
1320 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1321}
1322
Francois Romieufdf6fc02012-07-06 22:40:38 +02001323static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001324{
Francois Romieufdf6fc02012-07-06 22:40:38 +02001325 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00001326
1327 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1328
Francois Romieuffc46952012-07-06 14:19:23 +02001329 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1330 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001331}
1332
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001333static u16 rtl_get_events(struct rtl8169_private *tp)
1334{
1335 void __iomem *ioaddr = tp->mmio_addr;
1336
1337 return RTL_R16(IntrStatus);
1338}
1339
1340static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1341{
1342 void __iomem *ioaddr = tp->mmio_addr;
1343
1344 RTL_W16(IntrStatus, bits);
1345 mmiowb();
1346}
1347
1348static void rtl_irq_disable(struct rtl8169_private *tp)
1349{
1350 void __iomem *ioaddr = tp->mmio_addr;
1351
1352 RTL_W16(IntrMask, 0);
1353 mmiowb();
1354}
1355
Francois Romieu3e990ff2012-01-26 12:50:01 +01001356static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1357{
1358 void __iomem *ioaddr = tp->mmio_addr;
1359
1360 RTL_W16(IntrMask, bits);
1361}
1362
Francois Romieuda78dbf2012-01-26 14:18:23 +01001363#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1364#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1365#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1366
1367static void rtl_irq_enable_all(struct rtl8169_private *tp)
1368{
1369 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1370}
1371
françois romieu811fd302011-12-04 20:30:45 +00001372static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
françois romieu811fd302011-12-04 20:30:45 +00001374 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001376 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001377 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001378 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
françois romieu4da19632011-01-03 15:07:55 +00001381static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
françois romieu4da19632011-01-03 15:07:55 +00001383 void __iomem *ioaddr = tp->mmio_addr;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return RTL_R32(TBICSR) & TBIReset;
1386}
1387
françois romieu4da19632011-01-03 15:07:55 +00001388static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
françois romieu4da19632011-01-03 15:07:55 +00001390 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1394{
1395 return RTL_R32(TBICSR) & TBILinkOk;
1396}
1397
1398static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1399{
1400 return RTL_R8(PHYstatus) & LinkStatus;
1401}
1402
françois romieu4da19632011-01-03 15:07:55 +00001403static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
françois romieu4da19632011-01-03 15:07:55 +00001405 void __iomem *ioaddr = tp->mmio_addr;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1408}
1409
françois romieu4da19632011-01-03 15:07:55 +00001410static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 unsigned int val;
1413
françois romieu4da19632011-01-03 15:07:55 +00001414 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1415 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Hayes Wang70090422011-07-06 15:58:06 +08001418static void rtl_link_chg_patch(struct rtl8169_private *tp)
1419{
1420 void __iomem *ioaddr = tp->mmio_addr;
1421 struct net_device *dev = tp->dev;
1422
1423 if (!netif_running(dev))
1424 return;
1425
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001426 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1427 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001428 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001429 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1430 ERIAR_EXGMAC);
1431 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1432 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001433 } else if (RTL_R8(PHYstatus) & _100bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001434 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1435 ERIAR_EXGMAC);
1436 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1437 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001438 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001439 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1440 ERIAR_EXGMAC);
1441 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1442 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001443 }
1444 /* Reset packet filter */
Francois Romieufdf6fc02012-07-06 22:40:38 +02001445 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001446 ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02001447 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001448 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001449 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1450 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1451 if (RTL_R8(PHYstatus) & _1000bpsF) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001452 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1453 ERIAR_EXGMAC);
1454 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1455 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001456 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001457 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1458 ERIAR_EXGMAC);
1459 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1460 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001461 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001462 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1463 if (RTL_R8(PHYstatus) & _10bps) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001464 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1465 ERIAR_EXGMAC);
1466 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1467 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001468 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001469 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1470 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001471 }
Hayes Wang70090422011-07-06 15:58:06 +08001472 }
1473}
1474
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001475static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001476 struct rtl8169_private *tp,
1477 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001480 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001481 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001482 if (pm)
1483 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001485 if (net_ratelimit())
1486 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001487 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001489 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001490 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001491 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001495static void rtl8169_check_link_status(struct net_device *dev,
1496 struct rtl8169_private *tp,
1497 void __iomem *ioaddr)
1498{
1499 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1500}
1501
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001502#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1503
1504static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1505{
1506 void __iomem *ioaddr = tp->mmio_addr;
1507 u8 options;
1508 u32 wolopts = 0;
1509
1510 options = RTL_R8(Config1);
1511 if (!(options & PMEnable))
1512 return 0;
1513
1514 options = RTL_R8(Config3);
1515 if (options & LinkUp)
1516 wolopts |= WAKE_PHY;
1517 if (options & MagicPacket)
1518 wolopts |= WAKE_MAGIC;
1519
1520 options = RTL_R8(Config5);
1521 if (options & UWF)
1522 wolopts |= WAKE_UCAST;
1523 if (options & BWF)
1524 wolopts |= WAKE_BCAST;
1525 if (options & MWF)
1526 wolopts |= WAKE_MCAST;
1527
1528 return wolopts;
1529}
1530
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001531static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1532{
1533 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001534
Francois Romieuda78dbf2012-01-26 14:18:23 +01001535 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001536
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001537 wol->supported = WAKE_ANY;
1538 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001539
Francois Romieuda78dbf2012-01-26 14:18:23 +01001540 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001541}
1542
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001543static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001544{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001545 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001546 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001547 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001548 u32 opt;
1549 u16 reg;
1550 u8 mask;
1551 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001552 { WAKE_PHY, Config3, LinkUp },
1553 { WAKE_MAGIC, Config3, MagicPacket },
1554 { WAKE_UCAST, Config5, UWF },
1555 { WAKE_BCAST, Config5, BWF },
1556 { WAKE_MCAST, Config5, MWF },
1557 { WAKE_ANY, Config5, LanWake }
1558 };
Francois Romieu851e6022012-04-17 11:10:11 +02001559 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001560
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001561 RTL_W8(Cfg9346, Cfg9346_Unlock);
1562
1563 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001564 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001565 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001566 options |= cfg[i].mask;
1567 RTL_W8(cfg[i].reg, options);
1568 }
1569
Francois Romieu851e6022012-04-17 11:10:11 +02001570 switch (tp->mac_version) {
1571 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1572 options = RTL_R8(Config1) & ~PMEnable;
1573 if (wolopts)
1574 options |= PMEnable;
1575 RTL_W8(Config1, options);
1576 break;
1577 default:
Francois Romieud387b422012-04-17 11:12:01 +02001578 options = RTL_R8(Config2) & ~PME_SIGNAL;
1579 if (wolopts)
1580 options |= PME_SIGNAL;
1581 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001582 break;
1583 }
1584
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001585 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001586}
1587
1588static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1589{
1590 struct rtl8169_private *tp = netdev_priv(dev);
1591
Francois Romieuda78dbf2012-01-26 14:18:23 +01001592 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001593
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001594 if (wol->wolopts)
1595 tp->features |= RTL_FEATURE_WOL;
1596 else
1597 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001598 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001599
1600 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001601
françois romieuea809072010-11-08 13:23:58 +00001602 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1603
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001604 return 0;
1605}
1606
Francois Romieu31bd2042011-04-26 18:58:59 +02001607static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1608{
Francois Romieu85bffe62011-04-27 08:22:39 +02001609 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static void rtl8169_get_drvinfo(struct net_device *dev,
1613 struct ethtool_drvinfo *info)
1614{
1615 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001616 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Rick Jones68aad782011-11-07 13:29:27 +00001618 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1619 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1620 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001621 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001622 if (!IS_ERR_OR_NULL(rtl_fw))
1623 strlcpy(info->fw_version, rtl_fw->version,
1624 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627static int rtl8169_get_regs_len(struct net_device *dev)
1628{
1629 return R8169_REGS_SIZE;
1630}
1631
1632static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001633 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct rtl8169_private *tp = netdev_priv(dev);
1636 void __iomem *ioaddr = tp->mmio_addr;
1637 int ret = 0;
1638 u32 reg;
1639
1640 reg = RTL_R32(TBICSR);
1641 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1642 (duplex == DUPLEX_FULL)) {
1643 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1644 } else if (autoneg == AUTONEG_ENABLE)
1645 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1646 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001647 netif_warn(tp, link, dev,
1648 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 ret = -EOPNOTSUPP;
1650 }
1651
1652 return ret;
1653}
1654
1655static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001656 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001659 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001660 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Hayes Wang716b50a2011-02-22 17:26:18 +08001662 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001665 int auto_nego;
1666
françois romieu4da19632011-01-03 15:07:55 +00001667 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001668 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1669 ADVERTISE_100HALF | ADVERTISE_100FULL);
1670
1671 if (adv & ADVERTISED_10baseT_Half)
1672 auto_nego |= ADVERTISE_10HALF;
1673 if (adv & ADVERTISED_10baseT_Full)
1674 auto_nego |= ADVERTISE_10FULL;
1675 if (adv & ADVERTISED_100baseT_Half)
1676 auto_nego |= ADVERTISE_100HALF;
1677 if (adv & ADVERTISED_100baseT_Full)
1678 auto_nego |= ADVERTISE_100FULL;
1679
françois romieu3577aa12009-05-19 10:46:48 +00001680 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1681
françois romieu4da19632011-01-03 15:07:55 +00001682 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001683 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1684
1685 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001686 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001687 if (adv & ADVERTISED_1000baseT_Half)
1688 giga_ctrl |= ADVERTISE_1000HALF;
1689 if (adv & ADVERTISED_1000baseT_Full)
1690 giga_ctrl |= ADVERTISE_1000FULL;
1691 } else if (adv & (ADVERTISED_1000baseT_Half |
1692 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001693 netif_info(tp, link, dev,
1694 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001695 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
françois romieu3577aa12009-05-19 10:46:48 +00001698 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001699
françois romieu4da19632011-01-03 15:07:55 +00001700 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1701 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001702 } else {
1703 giga_ctrl = 0;
1704
1705 if (speed == SPEED_10)
1706 bmcr = 0;
1707 else if (speed == SPEED_100)
1708 bmcr = BMCR_SPEED100;
1709 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001710 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001711
1712 if (duplex == DUPLEX_FULL)
1713 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001714 }
1715
françois romieu4da19632011-01-03 15:07:55 +00001716 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001717
Francois Romieucecb5fd2011-04-01 10:21:07 +02001718 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1719 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001720 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001721 rtl_writephy(tp, 0x17, 0x2138);
1722 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001723 } else {
françois romieu4da19632011-01-03 15:07:55 +00001724 rtl_writephy(tp, 0x17, 0x2108);
1725 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001726 }
1727 }
1728
Oliver Neukum54405cd2011-01-06 21:55:13 +01001729 rc = 0;
1730out:
1731 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001735 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct rtl8169_private *tp = netdev_priv(dev);
1738 int ret;
1739
Oliver Neukum54405cd2011-01-06 21:55:13 +01001740 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001741 if (ret < 0)
1742 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Francois Romieu4876cc12011-03-11 21:07:11 +01001744 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1745 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001747 }
1748out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return ret;
1750}
1751
1752static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1753{
1754 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int ret;
1756
Francois Romieu4876cc12011-03-11 21:07:11 +01001757 del_timer_sync(&tp->timer);
1758
Francois Romieuda78dbf2012-01-26 14:18:23 +01001759 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001760 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001761 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001762 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return ret;
1765}
1766
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001767static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1768 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Francois Romieud58d46b2011-05-03 16:38:29 +02001770 struct rtl8169_private *tp = netdev_priv(dev);
1771
Francois Romieu2b7b4312011-04-18 22:53:24 -07001772 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001773 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Francois Romieud58d46b2011-05-03 16:38:29 +02001775 if (dev->mtu > JUMBO_1K &&
1776 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1777 features &= ~NETIF_F_IP_CSUM;
1778
Michał Mirosław350fb322011-04-08 06:35:56 +00001779 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Francois Romieuda78dbf2012-01-26 14:18:23 +01001782static void __rtl8169_set_features(struct net_device *dev,
1783 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001786 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001787 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Patrick McHardyf6469682013-04-19 02:04:27 +00001789 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM |
1790 NETIF_F_HW_VLAN_CTAG_RX)))
Ben Greear6bbe0212012-02-10 15:04:33 +00001791 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Patrick McHardyf6469682013-04-19 02:04:27 +00001793 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX)) {
Ben Greear6bbe0212012-02-10 15:04:33 +00001794 if (features & NETIF_F_RXCSUM)
1795 tp->cp_cmd |= RxChkSum;
1796 else
1797 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001798
hayeswang36d8e822014-09-12 11:35:12 +08001799 if (features & NETIF_F_HW_VLAN_CTAG_RX)
Ben Greear6bbe0212012-02-10 15:04:33 +00001800 tp->cp_cmd |= RxVlan;
1801 else
1802 tp->cp_cmd &= ~RxVlan;
1803
1804 RTL_W16(CPlusCmd, tp->cp_cmd);
1805 RTL_R16(CPlusCmd);
1806 }
1807 if (changed & NETIF_F_RXALL) {
1808 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1809 if (features & NETIF_F_RXALL)
1810 tmp |= (AcceptErr | AcceptRunt);
1811 RTL_W32(RxConfig, tmp);
1812 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001813}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Francois Romieuda78dbf2012-01-26 14:18:23 +01001815static int rtl8169_set_features(struct net_device *dev,
1816 netdev_features_t features)
1817{
1818 struct rtl8169_private *tp = netdev_priv(dev);
1819
1820 rtl_lock_work(tp);
1821 __rtl8169_set_features(dev, features);
1822 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 return 0;
1825}
1826
Francois Romieuda78dbf2012-01-26 14:18:23 +01001827
Kirill Smelkov810f4892012-11-10 21:11:02 +04001828static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Jesse Grosseab6d182010-10-20 13:56:03 +00001830 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1832}
1833
Francois Romieu7a8fc772011-03-01 17:18:33 +01001834static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Francois Romieu7a8fc772011-03-01 17:18:33 +01001838 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001839 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Francois Romieuccdffb92008-07-26 14:26:06 +02001842static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 struct rtl8169_private *tp = netdev_priv(dev);
1845 void __iomem *ioaddr = tp->mmio_addr;
1846 u32 status;
1847
1848 cmd->supported =
1849 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1850 cmd->port = PORT_FIBRE;
1851 cmd->transceiver = XCVR_INTERNAL;
1852
1853 status = RTL_R32(TBICSR);
1854 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1855 cmd->autoneg = !!(status & TBINwEnable);
1856
David Decotigny70739492011-04-27 18:32:40 +00001857 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001859
1860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Francois Romieuccdffb92008-07-26 14:26:06 +02001863static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Francois Romieuccdffb92008-07-26 14:26:06 +02001867 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1871{
1872 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001873 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Francois Romieuda78dbf2012-01-26 14:18:23 +01001875 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001876 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001877 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Francois Romieuccdffb92008-07-26 14:26:06 +02001879 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1883 void *p)
1884{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001885 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001886 u32 __iomem *data = tp->mmio_addr;
1887 u32 *dw = p;
1888 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Francois Romieuda78dbf2012-01-26 14:18:23 +01001890 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001891 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1892 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001893 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001896static u32 rtl8169_get_msglevel(struct net_device *dev)
1897{
1898 struct rtl8169_private *tp = netdev_priv(dev);
1899
1900 return tp->msg_enable;
1901}
1902
1903static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1904{
1905 struct rtl8169_private *tp = netdev_priv(dev);
1906
1907 tp->msg_enable = value;
1908}
1909
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001910static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1911 "tx_packets",
1912 "rx_packets",
1913 "tx_errors",
1914 "rx_errors",
1915 "rx_missed",
1916 "align_errors",
1917 "tx_single_collisions",
1918 "tx_multi_collisions",
1919 "unicast",
1920 "broadcast",
1921 "multicast",
1922 "tx_aborted",
1923 "tx_underrun",
1924};
1925
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001926static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001927{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001928 switch (sset) {
1929 case ETH_SS_STATS:
1930 return ARRAY_SIZE(rtl8169_gstrings);
1931 default:
1932 return -EOPNOTSUPP;
1933 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001934}
1935
Francois Romieuffc46952012-07-06 14:19:23 +02001936DECLARE_RTL_COND(rtl_counters_cond)
1937{
1938 void __iomem *ioaddr = tp->mmio_addr;
1939
1940 return RTL_R32(CounterAddrLow) & CounterDump;
1941}
1942
Ivan Vecera355423d2009-02-06 21:49:57 -08001943static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001944{
1945 struct rtl8169_private *tp = netdev_priv(dev);
1946 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001947 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001948 struct rtl8169_counters *counters;
1949 dma_addr_t paddr;
1950 u32 cmd;
1951
Ivan Vecera355423d2009-02-06 21:49:57 -08001952 /*
1953 * Some chips are unable to dump tally counters when the receiver
1954 * is disabled.
1955 */
1956 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1957 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001958
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001959 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001960 if (!counters)
1961 return;
1962
1963 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001964 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001965 RTL_W32(CounterAddrLow, cmd);
1966 RTL_W32(CounterAddrLow, cmd | CounterDump);
1967
Francois Romieuffc46952012-07-06 14:19:23 +02001968 if (rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000))
1969 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001970
1971 RTL_W32(CounterAddrLow, 0);
1972 RTL_W32(CounterAddrHigh, 0);
1973
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001974 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001975}
1976
Ivan Vecera355423d2009-02-06 21:49:57 -08001977static void rtl8169_get_ethtool_stats(struct net_device *dev,
1978 struct ethtool_stats *stats, u64 *data)
1979{
1980 struct rtl8169_private *tp = netdev_priv(dev);
1981
1982 ASSERT_RTNL();
1983
1984 rtl8169_update_counters(dev);
1985
1986 data[0] = le64_to_cpu(tp->counters.tx_packets);
1987 data[1] = le64_to_cpu(tp->counters.rx_packets);
1988 data[2] = le64_to_cpu(tp->counters.tx_errors);
1989 data[3] = le32_to_cpu(tp->counters.rx_errors);
1990 data[4] = le16_to_cpu(tp->counters.rx_missed);
1991 data[5] = le16_to_cpu(tp->counters.align_errors);
1992 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1993 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1994 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1995 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1996 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1997 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1998 data[12] = le16_to_cpu(tp->counters.tx_underun);
1999}
2000
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002001static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2002{
2003 switch(stringset) {
2004 case ETH_SS_STATS:
2005 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2006 break;
2007 }
2008}
2009
Jeff Garzik7282d492006-09-13 14:30:00 -04002010static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 .get_drvinfo = rtl8169_get_drvinfo,
2012 .get_regs_len = rtl8169_get_regs_len,
2013 .get_link = ethtool_op_get_link,
2014 .get_settings = rtl8169_get_settings,
2015 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002016 .get_msglevel = rtl8169_get_msglevel,
2017 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002019 .get_wol = rtl8169_get_wol,
2020 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002021 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002022 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002023 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002024 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025};
2026
Francois Romieu07d3f512007-02-21 22:40:46 +01002027static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02002028 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Francois Romieu5d320a22011-05-08 17:47:36 +02002030 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002031 /*
2032 * The driver currently handles the 8168Bf and the 8168Be identically
2033 * but they can be identified more specifically through the test below
2034 * if needed:
2035 *
2036 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002037 *
2038 * Same thing for the 8101Eb and the 8101Ec:
2039 *
2040 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002041 */
Francois Romieu37441002011-06-17 22:58:54 +02002042 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002044 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int mac_version;
2046 } mac_info[] = {
Hayes Wangc5583862012-07-02 17:23:22 +08002047 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002048 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002049 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002050 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2051 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2052
Hayes Wangc2218922011-09-06 16:55:18 +08002053 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002054 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002055 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2056 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2057
hayeswang01dc7fe2011-03-21 01:50:28 +00002058 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002059 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002060 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2061 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2062 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2063
Francois Romieu5b538df2008-07-20 16:22:45 +02002064 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002065 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2066 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002067 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002068
françois romieue6de30d2011-01-03 15:08:37 +00002069 /* 8168DP family. */
2070 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2071 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002072 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002073
Francois Romieuef808d52008-06-29 13:10:54 +02002074 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07002075 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02002076 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002077 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002078 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002079 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2080 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002081 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02002082 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02002083 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002084
2085 /* 8168B family. */
2086 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2087 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2088 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2089 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2090
2091 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002092 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2093 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002094 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00002095 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002096 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2097 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2098 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002099 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2100 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2101 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2102 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2103 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2104 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002105 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002106 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002107 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002108 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2109 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002110 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2111 /* FIXME: where did these entries come from ? -- FR */
2112 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2113 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2114
2115 /* 8110 family. */
2116 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2117 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2118 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2119 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2120 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2121 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2122
Jean Delvaref21b75e2009-05-26 20:54:48 -07002123 /* Catch-all */
2124 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002125 };
2126 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 u32 reg;
2128
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002129 reg = RTL_R32(TxConfig);
2130 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 p++;
2132 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002133
2134 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2135 netif_notice(tp, probe, dev,
2136 "unknown MAC, using family default\n");
2137 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002138 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2139 tp->mac_version = tp->mii.supports_gmii ?
2140 RTL_GIGA_MAC_VER_42 :
2141 RTL_GIGA_MAC_VER_43;
Francois Romieu5d320a22011-05-08 17:47:36 +02002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2146{
Francois Romieubcf0bf92006-07-26 23:14:13 +02002147 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Francois Romieu867763c2007-08-17 18:21:58 +02002150struct phy_reg {
2151 u16 reg;
2152 u16 val;
2153};
2154
françois romieu4da19632011-01-03 15:07:55 +00002155static void rtl_writephy_batch(struct rtl8169_private *tp,
2156 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002157{
2158 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002159 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002160 regs++;
2161 }
2162}
2163
françois romieubca03d52011-01-03 15:07:31 +00002164#define PHY_READ 0x00000000
2165#define PHY_DATA_OR 0x10000000
2166#define PHY_DATA_AND 0x20000000
2167#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002168#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002169#define PHY_CLEAR_READCOUNT 0x70000000
2170#define PHY_WRITE 0x80000000
2171#define PHY_READCOUNT_EQ_SKIP 0x90000000
2172#define PHY_COMP_EQ_SKIPN 0xa0000000
2173#define PHY_COMP_NEQ_SKIPN 0xb0000000
2174#define PHY_WRITE_PREVIOUS 0xc0000000
2175#define PHY_SKIPN 0xd0000000
2176#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002177
Hayes Wang960aee62011-06-18 11:37:48 +02002178struct fw_info {
2179 u32 magic;
2180 char version[RTL_VER_SIZE];
2181 __le32 fw_start;
2182 __le32 fw_len;
2183 u8 chksum;
2184} __packed;
2185
Francois Romieu1c361ef2011-06-17 17:16:24 +02002186#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2187
2188static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002189{
Francois Romieub6ffd972011-06-17 17:00:05 +02002190 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002191 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002192 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2193 char *version = rtl_fw->version;
2194 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002195
Francois Romieu1c361ef2011-06-17 17:16:24 +02002196 if (fw->size < FW_OPCODE_SIZE)
2197 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002198
2199 if (!fw_info->magic) {
2200 size_t i, size, start;
2201 u8 checksum = 0;
2202
2203 if (fw->size < sizeof(*fw_info))
2204 goto out;
2205
2206 for (i = 0; i < fw->size; i++)
2207 checksum += fw->data[i];
2208 if (checksum != 0)
2209 goto out;
2210
2211 start = le32_to_cpu(fw_info->fw_start);
2212 if (start > fw->size)
2213 goto out;
2214
2215 size = le32_to_cpu(fw_info->fw_len);
2216 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2217 goto out;
2218
2219 memcpy(version, fw_info->version, RTL_VER_SIZE);
2220
2221 pa->code = (__le32 *)(fw->data + start);
2222 pa->size = size;
2223 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002224 if (fw->size % FW_OPCODE_SIZE)
2225 goto out;
2226
2227 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2228
2229 pa->code = (__le32 *)fw->data;
2230 pa->size = fw->size / FW_OPCODE_SIZE;
2231 }
2232 version[RTL_VER_SIZE - 1] = 0;
2233
2234 rc = true;
2235out:
2236 return rc;
2237}
2238
Francois Romieufd112f22011-06-18 00:10:29 +02002239static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2240 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002241{
Francois Romieufd112f22011-06-18 00:10:29 +02002242 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002243 size_t index;
2244
Francois Romieu1c361ef2011-06-17 17:16:24 +02002245 for (index = 0; index < pa->size; index++) {
2246 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002247 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002248
hayeswang42b82dc2011-01-10 02:07:25 +00002249 switch(action & 0xf0000000) {
2250 case PHY_READ:
2251 case PHY_DATA_OR:
2252 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002253 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002254 case PHY_CLEAR_READCOUNT:
2255 case PHY_WRITE:
2256 case PHY_WRITE_PREVIOUS:
2257 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002258 break;
2259
hayeswang42b82dc2011-01-10 02:07:25 +00002260 case PHY_BJMPN:
2261 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002262 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002263 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002264 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002265 }
2266 break;
2267 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002268 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002269 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002270 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002271 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002272 }
2273 break;
2274 case PHY_COMP_EQ_SKIPN:
2275 case PHY_COMP_NEQ_SKIPN:
2276 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002277 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002278 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002279 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002280 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002281 }
2282 break;
2283
hayeswang42b82dc2011-01-10 02:07:25 +00002284 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002285 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002286 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002287 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002288 }
2289 }
Francois Romieufd112f22011-06-18 00:10:29 +02002290 rc = true;
2291out:
2292 return rc;
2293}
françois romieubca03d52011-01-03 15:07:31 +00002294
Francois Romieufd112f22011-06-18 00:10:29 +02002295static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2296{
2297 struct net_device *dev = tp->dev;
2298 int rc = -EINVAL;
2299
2300 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2301 netif_err(tp, ifup, dev, "invalid firwmare\n");
2302 goto out;
2303 }
2304
2305 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2306 rc = 0;
2307out:
2308 return rc;
2309}
2310
2311static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2312{
2313 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002314 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002315 u32 predata, count;
2316 size_t index;
2317
2318 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002319 org.write = ops->write;
2320 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002321
Francois Romieu1c361ef2011-06-17 17:16:24 +02002322 for (index = 0; index < pa->size; ) {
2323 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002324 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002325 u32 regno = (action & 0x0fff0000) >> 16;
2326
2327 if (!action)
2328 break;
françois romieubca03d52011-01-03 15:07:31 +00002329
2330 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002331 case PHY_READ:
2332 predata = rtl_readphy(tp, regno);
2333 count++;
2334 index++;
françois romieubca03d52011-01-03 15:07:31 +00002335 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002336 case PHY_DATA_OR:
2337 predata |= data;
2338 index++;
2339 break;
2340 case PHY_DATA_AND:
2341 predata &= data;
2342 index++;
2343 break;
2344 case PHY_BJMPN:
2345 index -= regno;
2346 break;
hayeswangeee37862013-04-01 22:23:38 +00002347 case PHY_MDIO_CHG:
2348 if (data == 0) {
2349 ops->write = org.write;
2350 ops->read = org.read;
2351 } else if (data == 1) {
2352 ops->write = mac_mcu_write;
2353 ops->read = mac_mcu_read;
2354 }
2355
hayeswang42b82dc2011-01-10 02:07:25 +00002356 index++;
2357 break;
2358 case PHY_CLEAR_READCOUNT:
2359 count = 0;
2360 index++;
2361 break;
2362 case PHY_WRITE:
2363 rtl_writephy(tp, regno, data);
2364 index++;
2365 break;
2366 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002367 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002368 break;
2369 case PHY_COMP_EQ_SKIPN:
2370 if (predata == data)
2371 index += regno;
2372 index++;
2373 break;
2374 case PHY_COMP_NEQ_SKIPN:
2375 if (predata != data)
2376 index += regno;
2377 index++;
2378 break;
2379 case PHY_WRITE_PREVIOUS:
2380 rtl_writephy(tp, regno, predata);
2381 index++;
2382 break;
2383 case PHY_SKIPN:
2384 index += regno + 1;
2385 break;
2386 case PHY_DELAY_MS:
2387 mdelay(data);
2388 index++;
2389 break;
2390
françois romieubca03d52011-01-03 15:07:31 +00002391 default:
2392 BUG();
2393 }
2394 }
hayeswangeee37862013-04-01 22:23:38 +00002395
2396 ops->write = org.write;
2397 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002398}
2399
françois romieuf1e02ed2011-01-13 13:07:53 +00002400static void rtl_release_firmware(struct rtl8169_private *tp)
2401{
Francois Romieub6ffd972011-06-17 17:00:05 +02002402 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2403 release_firmware(tp->rtl_fw->fw);
2404 kfree(tp->rtl_fw);
2405 }
2406 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002407}
2408
François Romieu953a12c2011-04-24 17:38:48 +02002409static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002410{
Francois Romieub6ffd972011-06-17 17:00:05 +02002411 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002412
2413 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002414 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002415 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002416}
2417
2418static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2419{
2420 if (rtl_readphy(tp, reg) != val)
2421 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2422 else
2423 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002424}
2425
françois romieu4da19632011-01-03 15:07:55 +00002426static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002428 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002429 { 0x1f, 0x0001 },
2430 { 0x06, 0x006e },
2431 { 0x08, 0x0708 },
2432 { 0x15, 0x4000 },
2433 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
françois romieu0b9b5712009-08-10 19:44:56 +00002435 { 0x1f, 0x0001 },
2436 { 0x03, 0x00a1 },
2437 { 0x02, 0x0008 },
2438 { 0x01, 0x0120 },
2439 { 0x00, 0x1000 },
2440 { 0x04, 0x0800 },
2441 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
françois romieu0b9b5712009-08-10 19:44:56 +00002443 { 0x03, 0xff41 },
2444 { 0x02, 0xdf60 },
2445 { 0x01, 0x0140 },
2446 { 0x00, 0x0077 },
2447 { 0x04, 0x7800 },
2448 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
françois romieu0b9b5712009-08-10 19:44:56 +00002450 { 0x03, 0x802f },
2451 { 0x02, 0x4f02 },
2452 { 0x01, 0x0409 },
2453 { 0x00, 0xf0f9 },
2454 { 0x04, 0x9800 },
2455 { 0x04, 0x9000 },
2456
2457 { 0x03, 0xdf01 },
2458 { 0x02, 0xdf20 },
2459 { 0x01, 0xff95 },
2460 { 0x00, 0xba00 },
2461 { 0x04, 0xa800 },
2462 { 0x04, 0xa000 },
2463
2464 { 0x03, 0xff41 },
2465 { 0x02, 0xdf20 },
2466 { 0x01, 0x0140 },
2467 { 0x00, 0x00bb },
2468 { 0x04, 0xb800 },
2469 { 0x04, 0xb000 },
2470
2471 { 0x03, 0xdf41 },
2472 { 0x02, 0xdc60 },
2473 { 0x01, 0x6340 },
2474 { 0x00, 0x007d },
2475 { 0x04, 0xd800 },
2476 { 0x04, 0xd000 },
2477
2478 { 0x03, 0xdf01 },
2479 { 0x02, 0xdf20 },
2480 { 0x01, 0x100a },
2481 { 0x00, 0xa0ff },
2482 { 0x04, 0xf800 },
2483 { 0x04, 0xf000 },
2484
2485 { 0x1f, 0x0000 },
2486 { 0x0b, 0x0000 },
2487 { 0x00, 0x9200 }
2488 };
2489
françois romieu4da19632011-01-03 15:07:55 +00002490 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
françois romieu4da19632011-01-03 15:07:55 +00002493static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002494{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002495 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002496 { 0x1f, 0x0002 },
2497 { 0x01, 0x90d0 },
2498 { 0x1f, 0x0000 }
2499 };
2500
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002502}
2503
françois romieu4da19632011-01-03 15:07:55 +00002504static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002505{
2506 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002507
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002508 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2509 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002510 return;
2511
françois romieu4da19632011-01-03 15:07:55 +00002512 rtl_writephy(tp, 0x1f, 0x0001);
2513 rtl_writephy(tp, 0x10, 0xf01b);
2514 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002515}
2516
françois romieu4da19632011-01-03 15:07:55 +00002517static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002518{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002519 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002520 { 0x1f, 0x0001 },
2521 { 0x04, 0x0000 },
2522 { 0x03, 0x00a1 },
2523 { 0x02, 0x0008 },
2524 { 0x01, 0x0120 },
2525 { 0x00, 0x1000 },
2526 { 0x04, 0x0800 },
2527 { 0x04, 0x9000 },
2528 { 0x03, 0x802f },
2529 { 0x02, 0x4f02 },
2530 { 0x01, 0x0409 },
2531 { 0x00, 0xf099 },
2532 { 0x04, 0x9800 },
2533 { 0x04, 0xa000 },
2534 { 0x03, 0xdf01 },
2535 { 0x02, 0xdf20 },
2536 { 0x01, 0xff95 },
2537 { 0x00, 0xba00 },
2538 { 0x04, 0xa800 },
2539 { 0x04, 0xf000 },
2540 { 0x03, 0xdf01 },
2541 { 0x02, 0xdf20 },
2542 { 0x01, 0x101a },
2543 { 0x00, 0xa0ff },
2544 { 0x04, 0xf800 },
2545 { 0x04, 0x0000 },
2546 { 0x1f, 0x0000 },
2547
2548 { 0x1f, 0x0001 },
2549 { 0x10, 0xf41b },
2550 { 0x14, 0xfb54 },
2551 { 0x18, 0xf5c7 },
2552 { 0x1f, 0x0000 },
2553
2554 { 0x1f, 0x0001 },
2555 { 0x17, 0x0cc0 },
2556 { 0x1f, 0x0000 }
2557 };
2558
françois romieu4da19632011-01-03 15:07:55 +00002559 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002560
françois romieu4da19632011-01-03 15:07:55 +00002561 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002562}
2563
françois romieu4da19632011-01-03 15:07:55 +00002564static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002565{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002566 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002567 { 0x1f, 0x0001 },
2568 { 0x04, 0x0000 },
2569 { 0x03, 0x00a1 },
2570 { 0x02, 0x0008 },
2571 { 0x01, 0x0120 },
2572 { 0x00, 0x1000 },
2573 { 0x04, 0x0800 },
2574 { 0x04, 0x9000 },
2575 { 0x03, 0x802f },
2576 { 0x02, 0x4f02 },
2577 { 0x01, 0x0409 },
2578 { 0x00, 0xf099 },
2579 { 0x04, 0x9800 },
2580 { 0x04, 0xa000 },
2581 { 0x03, 0xdf01 },
2582 { 0x02, 0xdf20 },
2583 { 0x01, 0xff95 },
2584 { 0x00, 0xba00 },
2585 { 0x04, 0xa800 },
2586 { 0x04, 0xf000 },
2587 { 0x03, 0xdf01 },
2588 { 0x02, 0xdf20 },
2589 { 0x01, 0x101a },
2590 { 0x00, 0xa0ff },
2591 { 0x04, 0xf800 },
2592 { 0x04, 0x0000 },
2593 { 0x1f, 0x0000 },
2594
2595 { 0x1f, 0x0001 },
2596 { 0x0b, 0x8480 },
2597 { 0x1f, 0x0000 },
2598
2599 { 0x1f, 0x0001 },
2600 { 0x18, 0x67c7 },
2601 { 0x04, 0x2000 },
2602 { 0x03, 0x002f },
2603 { 0x02, 0x4360 },
2604 { 0x01, 0x0109 },
2605 { 0x00, 0x3022 },
2606 { 0x04, 0x2800 },
2607 { 0x1f, 0x0000 },
2608
2609 { 0x1f, 0x0001 },
2610 { 0x17, 0x0cc0 },
2611 { 0x1f, 0x0000 }
2612 };
2613
françois romieu4da19632011-01-03 15:07:55 +00002614 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002615}
2616
françois romieu4da19632011-01-03 15:07:55 +00002617static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002618{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002619 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002620 { 0x10, 0xf41b },
2621 { 0x1f, 0x0000 }
2622 };
2623
françois romieu4da19632011-01-03 15:07:55 +00002624 rtl_writephy(tp, 0x1f, 0x0001);
2625 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002626
françois romieu4da19632011-01-03 15:07:55 +00002627 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002628}
2629
françois romieu4da19632011-01-03 15:07:55 +00002630static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002631{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002632 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002633 { 0x1f, 0x0001 },
2634 { 0x10, 0xf41b },
2635 { 0x1f, 0x0000 }
2636 };
2637
françois romieu4da19632011-01-03 15:07:55 +00002638 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002639}
2640
françois romieu4da19632011-01-03 15:07:55 +00002641static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002642{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002643 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002644 { 0x1f, 0x0000 },
2645 { 0x1d, 0x0f00 },
2646 { 0x1f, 0x0002 },
2647 { 0x0c, 0x1ec8 },
2648 { 0x1f, 0x0000 }
2649 };
2650
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002652}
2653
françois romieu4da19632011-01-03 15:07:55 +00002654static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002655{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002656 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002657 { 0x1f, 0x0001 },
2658 { 0x1d, 0x3d98 },
2659 { 0x1f, 0x0000 }
2660 };
2661
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl_writephy(tp, 0x1f, 0x0000);
2663 rtl_patchphy(tp, 0x14, 1 << 5);
2664 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002665
françois romieu4da19632011-01-03 15:07:55 +00002666 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002667}
2668
françois romieu4da19632011-01-03 15:07:55 +00002669static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002670{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002671 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002672 { 0x1f, 0x0001 },
2673 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002674 { 0x1f, 0x0002 },
2675 { 0x00, 0x88d4 },
2676 { 0x01, 0x82b1 },
2677 { 0x03, 0x7002 },
2678 { 0x08, 0x9e30 },
2679 { 0x09, 0x01f0 },
2680 { 0x0a, 0x5500 },
2681 { 0x0c, 0x00c8 },
2682 { 0x1f, 0x0003 },
2683 { 0x12, 0xc096 },
2684 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002685 { 0x1f, 0x0000 },
2686 { 0x1f, 0x0000 },
2687 { 0x09, 0x2000 },
2688 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002689 };
2690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002692
françois romieu4da19632011-01-03 15:07:55 +00002693 rtl_patchphy(tp, 0x14, 1 << 5);
2694 rtl_patchphy(tp, 0x0d, 1 << 5);
2695 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002696}
2697
françois romieu4da19632011-01-03 15:07:55 +00002698static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002699{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002700 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002701 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002702 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002703 { 0x03, 0x802f },
2704 { 0x02, 0x4f02 },
2705 { 0x01, 0x0409 },
2706 { 0x00, 0xf099 },
2707 { 0x04, 0x9800 },
2708 { 0x04, 0x9000 },
2709 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002710 { 0x1f, 0x0002 },
2711 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002712 { 0x06, 0x0761 },
2713 { 0x1f, 0x0003 },
2714 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002715 { 0x1f, 0x0000 }
2716 };
2717
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002719
françois romieu4da19632011-01-03 15:07:55 +00002720 rtl_patchphy(tp, 0x16, 1 << 0);
2721 rtl_patchphy(tp, 0x14, 1 << 5);
2722 rtl_patchphy(tp, 0x0d, 1 << 5);
2723 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002724}
2725
françois romieu4da19632011-01-03 15:07:55 +00002726static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002727{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002728 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002729 { 0x1f, 0x0001 },
2730 { 0x12, 0x2300 },
2731 { 0x1d, 0x3d98 },
2732 { 0x1f, 0x0002 },
2733 { 0x0c, 0x7eb8 },
2734 { 0x06, 0x5461 },
2735 { 0x1f, 0x0003 },
2736 { 0x16, 0x0f0a },
2737 { 0x1f, 0x0000 }
2738 };
2739
françois romieu4da19632011-01-03 15:07:55 +00002740 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002741
françois romieu4da19632011-01-03 15:07:55 +00002742 rtl_patchphy(tp, 0x16, 1 << 0);
2743 rtl_patchphy(tp, 0x14, 1 << 5);
2744 rtl_patchphy(tp, 0x0d, 1 << 5);
2745 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002746}
2747
françois romieu4da19632011-01-03 15:07:55 +00002748static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002749{
françois romieu4da19632011-01-03 15:07:55 +00002750 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002751}
2752
françois romieubca03d52011-01-03 15:07:31 +00002753static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002754{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002755 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002756 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002757 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002758 { 0x06, 0x4064 },
2759 { 0x07, 0x2863 },
2760 { 0x08, 0x059c },
2761 { 0x09, 0x26b4 },
2762 { 0x0a, 0x6a19 },
2763 { 0x0b, 0xdcc8 },
2764 { 0x10, 0xf06d },
2765 { 0x14, 0x7f68 },
2766 { 0x18, 0x7fd9 },
2767 { 0x1c, 0xf0ff },
2768 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002769 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002770 { 0x12, 0xf49f },
2771 { 0x13, 0x070b },
2772 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002773 { 0x14, 0x94c0 },
2774
2775 /*
2776 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002777 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002778 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002779 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002780 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002781 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002782 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002783 { 0x06, 0x5561 },
2784
2785 /*
2786 * Can not link to 1Gbps with bad cable
2787 * Decrease SNR threshold form 21.07dB to 19.04dB
2788 */
2789 { 0x1f, 0x0001 },
2790 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002791
2792 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002793 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002794 };
2795
françois romieu4da19632011-01-03 15:07:55 +00002796 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002797
françois romieubca03d52011-01-03 15:07:31 +00002798 /*
2799 * Rx Error Issue
2800 * Fine Tune Switching regulator parameter
2801 */
françois romieu4da19632011-01-03 15:07:55 +00002802 rtl_writephy(tp, 0x1f, 0x0002);
2803 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2804 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002805
Francois Romieufdf6fc02012-07-06 22:40:38 +02002806 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002807 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002808 { 0x1f, 0x0002 },
2809 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002810 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002811 { 0x05, 0x8330 },
2812 { 0x06, 0x669a },
2813 { 0x1f, 0x0002 }
2814 };
2815 int val;
2816
françois romieu4da19632011-01-03 15:07:55 +00002817 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002818
françois romieu4da19632011-01-03 15:07:55 +00002819 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002820
2821 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002822 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002823 0x0065, 0x0066, 0x0067, 0x0068,
2824 0x0069, 0x006a, 0x006b, 0x006c
2825 };
2826 int i;
2827
françois romieu4da19632011-01-03 15:07:55 +00002828 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002829
2830 val &= 0xff00;
2831 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002832 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002833 }
2834 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002835 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002836 { 0x1f, 0x0002 },
2837 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002838 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002839 { 0x05, 0x8330 },
2840 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002841 };
2842
françois romieu4da19632011-01-03 15:07:55 +00002843 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002844 }
2845
françois romieubca03d52011-01-03 15:07:31 +00002846 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002847 rtl_writephy(tp, 0x1f, 0x0002);
2848 rtl_patchphy(tp, 0x0d, 0x0300);
2849 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002850
françois romieubca03d52011-01-03 15:07:31 +00002851 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002852 rtl_writephy(tp, 0x1f, 0x0002);
2853 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2854 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002855
françois romieu4da19632011-01-03 15:07:55 +00002856 rtl_writephy(tp, 0x1f, 0x0005);
2857 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002858
2859 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002860
françois romieu4da19632011-01-03 15:07:55 +00002861 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002862}
2863
françois romieubca03d52011-01-03 15:07:31 +00002864static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002865{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002866 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002867 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002868 { 0x1f, 0x0001 },
2869 { 0x06, 0x4064 },
2870 { 0x07, 0x2863 },
2871 { 0x08, 0x059c },
2872 { 0x09, 0x26b4 },
2873 { 0x0a, 0x6a19 },
2874 { 0x0b, 0xdcc8 },
2875 { 0x10, 0xf06d },
2876 { 0x14, 0x7f68 },
2877 { 0x18, 0x7fd9 },
2878 { 0x1c, 0xf0ff },
2879 { 0x1d, 0x3d9c },
2880 { 0x1f, 0x0003 },
2881 { 0x12, 0xf49f },
2882 { 0x13, 0x070b },
2883 { 0x1a, 0x05ad },
2884 { 0x14, 0x94c0 },
2885
françois romieubca03d52011-01-03 15:07:31 +00002886 /*
2887 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002888 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002889 */
françois romieudaf9df62009-10-07 12:44:20 +00002890 { 0x1f, 0x0002 },
2891 { 0x06, 0x5561 },
2892 { 0x1f, 0x0005 },
2893 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002894 { 0x06, 0x5561 },
2895
2896 /*
2897 * Can not link to 1Gbps with bad cable
2898 * Decrease SNR threshold form 21.07dB to 19.04dB
2899 */
2900 { 0x1f, 0x0001 },
2901 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002902
2903 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002904 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002905 };
2906
françois romieu4da19632011-01-03 15:07:55 +00002907 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002908
Francois Romieufdf6fc02012-07-06 22:40:38 +02002909 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002910 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002911 { 0x1f, 0x0002 },
2912 { 0x05, 0x669a },
2913 { 0x1f, 0x0005 },
2914 { 0x05, 0x8330 },
2915 { 0x06, 0x669a },
2916
2917 { 0x1f, 0x0002 }
2918 };
2919 int val;
2920
françois romieu4da19632011-01-03 15:07:55 +00002921 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002922
françois romieu4da19632011-01-03 15:07:55 +00002923 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002924 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002925 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002926 0x0065, 0x0066, 0x0067, 0x0068,
2927 0x0069, 0x006a, 0x006b, 0x006c
2928 };
2929 int i;
2930
françois romieu4da19632011-01-03 15:07:55 +00002931 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002932
2933 val &= 0xff00;
2934 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002936 }
2937 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002938 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002939 { 0x1f, 0x0002 },
2940 { 0x05, 0x2642 },
2941 { 0x1f, 0x0005 },
2942 { 0x05, 0x8330 },
2943 { 0x06, 0x2642 }
2944 };
2945
françois romieu4da19632011-01-03 15:07:55 +00002946 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002947 }
2948
françois romieubca03d52011-01-03 15:07:31 +00002949 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl_writephy(tp, 0x1f, 0x0002);
2951 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2952 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002953
françois romieubca03d52011-01-03 15:07:31 +00002954 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002955 rtl_writephy(tp, 0x1f, 0x0002);
2956 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002957
françois romieu4da19632011-01-03 15:07:55 +00002958 rtl_writephy(tp, 0x1f, 0x0005);
2959 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002960
2961 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002962
françois romieu4da19632011-01-03 15:07:55 +00002963 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002964}
2965
françois romieu4da19632011-01-03 15:07:55 +00002966static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002967{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002968 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002969 { 0x1f, 0x0002 },
2970 { 0x10, 0x0008 },
2971 { 0x0d, 0x006c },
2972
2973 { 0x1f, 0x0000 },
2974 { 0x0d, 0xf880 },
2975
2976 { 0x1f, 0x0001 },
2977 { 0x17, 0x0cc0 },
2978
2979 { 0x1f, 0x0001 },
2980 { 0x0b, 0xa4d8 },
2981 { 0x09, 0x281c },
2982 { 0x07, 0x2883 },
2983 { 0x0a, 0x6b35 },
2984 { 0x1d, 0x3da4 },
2985 { 0x1c, 0xeffd },
2986 { 0x14, 0x7f52 },
2987 { 0x18, 0x7fc6 },
2988 { 0x08, 0x0601 },
2989 { 0x06, 0x4063 },
2990 { 0x10, 0xf074 },
2991 { 0x1f, 0x0003 },
2992 { 0x13, 0x0789 },
2993 { 0x12, 0xf4bd },
2994 { 0x1a, 0x04fd },
2995 { 0x14, 0x84b0 },
2996 { 0x1f, 0x0000 },
2997 { 0x00, 0x9200 },
2998
2999 { 0x1f, 0x0005 },
3000 { 0x01, 0x0340 },
3001 { 0x1f, 0x0001 },
3002 { 0x04, 0x4000 },
3003 { 0x03, 0x1d21 },
3004 { 0x02, 0x0c32 },
3005 { 0x01, 0x0200 },
3006 { 0x00, 0x5554 },
3007 { 0x04, 0x4800 },
3008 { 0x04, 0x4000 },
3009 { 0x04, 0xf000 },
3010 { 0x03, 0xdf01 },
3011 { 0x02, 0xdf20 },
3012 { 0x01, 0x101a },
3013 { 0x00, 0xa0ff },
3014 { 0x04, 0xf800 },
3015 { 0x04, 0xf000 },
3016 { 0x1f, 0x0000 },
3017
3018 { 0x1f, 0x0007 },
3019 { 0x1e, 0x0023 },
3020 { 0x16, 0x0000 },
3021 { 0x1f, 0x0000 }
3022 };
3023
françois romieu4da19632011-01-03 15:07:55 +00003024 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003025}
3026
françois romieue6de30d2011-01-03 15:08:37 +00003027static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3028{
3029 static const struct phy_reg phy_reg_init[] = {
3030 { 0x1f, 0x0001 },
3031 { 0x17, 0x0cc0 },
3032
3033 { 0x1f, 0x0007 },
3034 { 0x1e, 0x002d },
3035 { 0x18, 0x0040 },
3036 { 0x1f, 0x0000 }
3037 };
3038
3039 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3040 rtl_patchphy(tp, 0x0d, 1 << 5);
3041}
3042
Hayes Wang70090422011-07-06 15:58:06 +08003043static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003044{
3045 static const struct phy_reg phy_reg_init[] = {
3046 /* Enable Delay cap */
3047 { 0x1f, 0x0005 },
3048 { 0x05, 0x8b80 },
3049 { 0x06, 0xc896 },
3050 { 0x1f, 0x0000 },
3051
3052 /* Channel estimation fine tune */
3053 { 0x1f, 0x0001 },
3054 { 0x0b, 0x6c20 },
3055 { 0x07, 0x2872 },
3056 { 0x1c, 0xefff },
3057 { 0x1f, 0x0003 },
3058 { 0x14, 0x6420 },
3059 { 0x1f, 0x0000 },
3060
3061 /* Update PFM & 10M TX idle timer */
3062 { 0x1f, 0x0007 },
3063 { 0x1e, 0x002f },
3064 { 0x15, 0x1919 },
3065 { 0x1f, 0x0000 },
3066
3067 { 0x1f, 0x0007 },
3068 { 0x1e, 0x00ac },
3069 { 0x18, 0x0006 },
3070 { 0x1f, 0x0000 }
3071 };
3072
Francois Romieu15ecd032011-04-27 13:52:22 -07003073 rtl_apply_firmware(tp);
3074
hayeswang01dc7fe2011-03-21 01:50:28 +00003075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3076
3077 /* DCO enable for 10M IDLE Power */
3078 rtl_writephy(tp, 0x1f, 0x0007);
3079 rtl_writephy(tp, 0x1e, 0x0023);
3080 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3081 rtl_writephy(tp, 0x1f, 0x0000);
3082
3083 /* For impedance matching */
3084 rtl_writephy(tp, 0x1f, 0x0002);
3085 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003086 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003087
3088 /* PHY auto speed down */
3089 rtl_writephy(tp, 0x1f, 0x0007);
3090 rtl_writephy(tp, 0x1e, 0x002d);
3091 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
3092 rtl_writephy(tp, 0x1f, 0x0000);
3093 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3094
3095 rtl_writephy(tp, 0x1f, 0x0005);
3096 rtl_writephy(tp, 0x05, 0x8b86);
3097 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3098 rtl_writephy(tp, 0x1f, 0x0000);
3099
3100 rtl_writephy(tp, 0x1f, 0x0005);
3101 rtl_writephy(tp, 0x05, 0x8b85);
3102 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3103 rtl_writephy(tp, 0x1f, 0x0007);
3104 rtl_writephy(tp, 0x1e, 0x0020);
3105 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
3106 rtl_writephy(tp, 0x1f, 0x0006);
3107 rtl_writephy(tp, 0x00, 0x5a00);
3108 rtl_writephy(tp, 0x1f, 0x0000);
3109 rtl_writephy(tp, 0x0d, 0x0007);
3110 rtl_writephy(tp, 0x0e, 0x003c);
3111 rtl_writephy(tp, 0x0d, 0x4007);
3112 rtl_writephy(tp, 0x0e, 0x0000);
3113 rtl_writephy(tp, 0x0d, 0x0000);
3114}
3115
françois romieu9ecb9aa2012-12-07 11:20:21 +00003116static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3117{
3118 const u16 w[] = {
3119 addr[0] | (addr[1] << 8),
3120 addr[2] | (addr[3] << 8),
3121 addr[4] | (addr[5] << 8)
3122 };
3123 const struct exgmac_reg e[] = {
3124 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3125 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3126 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3127 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3128 };
3129
3130 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3131}
3132
Hayes Wang70090422011-07-06 15:58:06 +08003133static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3134{
3135 static const struct phy_reg phy_reg_init[] = {
3136 /* Enable Delay cap */
3137 { 0x1f, 0x0004 },
3138 { 0x1f, 0x0007 },
3139 { 0x1e, 0x00ac },
3140 { 0x18, 0x0006 },
3141 { 0x1f, 0x0002 },
3142 { 0x1f, 0x0000 },
3143 { 0x1f, 0x0000 },
3144
3145 /* Channel estimation fine tune */
3146 { 0x1f, 0x0003 },
3147 { 0x09, 0xa20f },
3148 { 0x1f, 0x0000 },
3149 { 0x1f, 0x0000 },
3150
3151 /* Green Setting */
3152 { 0x1f, 0x0005 },
3153 { 0x05, 0x8b5b },
3154 { 0x06, 0x9222 },
3155 { 0x05, 0x8b6d },
3156 { 0x06, 0x8000 },
3157 { 0x05, 0x8b76 },
3158 { 0x06, 0x8000 },
3159 { 0x1f, 0x0000 }
3160 };
3161
3162 rtl_apply_firmware(tp);
3163
3164 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3165
3166 /* For 4-corner performance improve */
3167 rtl_writephy(tp, 0x1f, 0x0005);
3168 rtl_writephy(tp, 0x05, 0x8b80);
3169 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
3170 rtl_writephy(tp, 0x1f, 0x0000);
3171
3172 /* PHY auto speed down */
3173 rtl_writephy(tp, 0x1f, 0x0004);
3174 rtl_writephy(tp, 0x1f, 0x0007);
3175 rtl_writephy(tp, 0x1e, 0x002d);
3176 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3177 rtl_writephy(tp, 0x1f, 0x0002);
3178 rtl_writephy(tp, 0x1f, 0x0000);
3179 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3180
3181 /* improve 10M EEE waveform */
3182 rtl_writephy(tp, 0x1f, 0x0005);
3183 rtl_writephy(tp, 0x05, 0x8b86);
3184 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3185 rtl_writephy(tp, 0x1f, 0x0000);
3186
3187 /* Improve 2-pair detection performance */
3188 rtl_writephy(tp, 0x1f, 0x0005);
3189 rtl_writephy(tp, 0x05, 0x8b85);
3190 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3191 rtl_writephy(tp, 0x1f, 0x0000);
3192
3193 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003194 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003195 rtl_writephy(tp, 0x1f, 0x0005);
3196 rtl_writephy(tp, 0x05, 0x8b85);
3197 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3198 rtl_writephy(tp, 0x1f, 0x0004);
3199 rtl_writephy(tp, 0x1f, 0x0007);
3200 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003201 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003202 rtl_writephy(tp, 0x1f, 0x0002);
3203 rtl_writephy(tp, 0x1f, 0x0000);
3204 rtl_writephy(tp, 0x0d, 0x0007);
3205 rtl_writephy(tp, 0x0e, 0x003c);
3206 rtl_writephy(tp, 0x0d, 0x4007);
3207 rtl_writephy(tp, 0x0e, 0x0000);
3208 rtl_writephy(tp, 0x0d, 0x0000);
3209
3210 /* Green feature */
3211 rtl_writephy(tp, 0x1f, 0x0003);
3212 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3213 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3214 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003215
françois romieu9ecb9aa2012-12-07 11:20:21 +00003216 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3217 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003218}
3219
Hayes Wang5f886e02012-03-30 14:33:03 +08003220static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3221{
3222 /* For 4-corner performance improve */
3223 rtl_writephy(tp, 0x1f, 0x0005);
3224 rtl_writephy(tp, 0x05, 0x8b80);
3225 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3226 rtl_writephy(tp, 0x1f, 0x0000);
3227
3228 /* PHY auto speed down */
3229 rtl_writephy(tp, 0x1f, 0x0007);
3230 rtl_writephy(tp, 0x1e, 0x002d);
3231 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3232 rtl_writephy(tp, 0x1f, 0x0000);
3233 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3234
3235 /* Improve 10M EEE waveform */
3236 rtl_writephy(tp, 0x1f, 0x0005);
3237 rtl_writephy(tp, 0x05, 0x8b86);
3238 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3239 rtl_writephy(tp, 0x1f, 0x0000);
3240}
3241
Hayes Wangc2218922011-09-06 16:55:18 +08003242static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3243{
3244 static const struct phy_reg phy_reg_init[] = {
3245 /* Channel estimation fine tune */
3246 { 0x1f, 0x0003 },
3247 { 0x09, 0xa20f },
3248 { 0x1f, 0x0000 },
3249
3250 /* Modify green table for giga & fnet */
3251 { 0x1f, 0x0005 },
3252 { 0x05, 0x8b55 },
3253 { 0x06, 0x0000 },
3254 { 0x05, 0x8b5e },
3255 { 0x06, 0x0000 },
3256 { 0x05, 0x8b67 },
3257 { 0x06, 0x0000 },
3258 { 0x05, 0x8b70 },
3259 { 0x06, 0x0000 },
3260 { 0x1f, 0x0000 },
3261 { 0x1f, 0x0007 },
3262 { 0x1e, 0x0078 },
3263 { 0x17, 0x0000 },
3264 { 0x19, 0x00fb },
3265 { 0x1f, 0x0000 },
3266
3267 /* Modify green table for 10M */
3268 { 0x1f, 0x0005 },
3269 { 0x05, 0x8b79 },
3270 { 0x06, 0xaa00 },
3271 { 0x1f, 0x0000 },
3272
3273 /* Disable hiimpedance detection (RTCT) */
3274 { 0x1f, 0x0003 },
3275 { 0x01, 0x328a },
3276 { 0x1f, 0x0000 }
3277 };
3278
3279 rtl_apply_firmware(tp);
3280
3281 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3282
Hayes Wang5f886e02012-03-30 14:33:03 +08003283 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003284
3285 /* Improve 2-pair detection performance */
3286 rtl_writephy(tp, 0x1f, 0x0005);
3287 rtl_writephy(tp, 0x05, 0x8b85);
3288 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3289 rtl_writephy(tp, 0x1f, 0x0000);
3290}
3291
3292static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3293{
3294 rtl_apply_firmware(tp);
3295
Hayes Wang5f886e02012-03-30 14:33:03 +08003296 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003297}
3298
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003299static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3300{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003301 static const struct phy_reg phy_reg_init[] = {
3302 /* Channel estimation fine tune */
3303 { 0x1f, 0x0003 },
3304 { 0x09, 0xa20f },
3305 { 0x1f, 0x0000 },
3306
3307 /* Modify green table for giga & fnet */
3308 { 0x1f, 0x0005 },
3309 { 0x05, 0x8b55 },
3310 { 0x06, 0x0000 },
3311 { 0x05, 0x8b5e },
3312 { 0x06, 0x0000 },
3313 { 0x05, 0x8b67 },
3314 { 0x06, 0x0000 },
3315 { 0x05, 0x8b70 },
3316 { 0x06, 0x0000 },
3317 { 0x1f, 0x0000 },
3318 { 0x1f, 0x0007 },
3319 { 0x1e, 0x0078 },
3320 { 0x17, 0x0000 },
3321 { 0x19, 0x00aa },
3322 { 0x1f, 0x0000 },
3323
3324 /* Modify green table for 10M */
3325 { 0x1f, 0x0005 },
3326 { 0x05, 0x8b79 },
3327 { 0x06, 0xaa00 },
3328 { 0x1f, 0x0000 },
3329
3330 /* Disable hiimpedance detection (RTCT) */
3331 { 0x1f, 0x0003 },
3332 { 0x01, 0x328a },
3333 { 0x1f, 0x0000 }
3334 };
3335
3336
3337 rtl_apply_firmware(tp);
3338
3339 rtl8168f_hw_phy_config(tp);
3340
3341 /* Improve 2-pair detection performance */
3342 rtl_writephy(tp, 0x1f, 0x0005);
3343 rtl_writephy(tp, 0x05, 0x8b85);
3344 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3345 rtl_writephy(tp, 0x1f, 0x0000);
3346
3347 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3348
3349 /* Modify green table for giga */
3350 rtl_writephy(tp, 0x1f, 0x0005);
3351 rtl_writephy(tp, 0x05, 0x8b54);
3352 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3353 rtl_writephy(tp, 0x05, 0x8b5d);
3354 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3355 rtl_writephy(tp, 0x05, 0x8a7c);
3356 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3357 rtl_writephy(tp, 0x05, 0x8a7f);
3358 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3359 rtl_writephy(tp, 0x05, 0x8a82);
3360 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3361 rtl_writephy(tp, 0x05, 0x8a85);
3362 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3363 rtl_writephy(tp, 0x05, 0x8a88);
3364 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3365 rtl_writephy(tp, 0x1f, 0x0000);
3366
3367 /* uc same-seed solution */
3368 rtl_writephy(tp, 0x1f, 0x0005);
3369 rtl_writephy(tp, 0x05, 0x8b85);
3370 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3371 rtl_writephy(tp, 0x1f, 0x0000);
3372
3373 /* eee setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003374 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003375 rtl_writephy(tp, 0x1f, 0x0005);
3376 rtl_writephy(tp, 0x05, 0x8b85);
3377 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3378 rtl_writephy(tp, 0x1f, 0x0004);
3379 rtl_writephy(tp, 0x1f, 0x0007);
3380 rtl_writephy(tp, 0x1e, 0x0020);
3381 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3382 rtl_writephy(tp, 0x1f, 0x0000);
3383 rtl_writephy(tp, 0x0d, 0x0007);
3384 rtl_writephy(tp, 0x0e, 0x003c);
3385 rtl_writephy(tp, 0x0d, 0x4007);
3386 rtl_writephy(tp, 0x0e, 0x0000);
3387 rtl_writephy(tp, 0x0d, 0x0000);
3388
3389 /* Green feature */
3390 rtl_writephy(tp, 0x1f, 0x0003);
3391 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3392 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3393 rtl_writephy(tp, 0x1f, 0x0000);
3394}
3395
Hayes Wangc5583862012-07-02 17:23:22 +08003396static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3397{
Hayes Wangc5583862012-07-02 17:23:22 +08003398 rtl_apply_firmware(tp);
3399
hayeswang41f44d12013-04-01 22:23:36 +00003400 rtl_writephy(tp, 0x1f, 0x0a46);
3401 if (rtl_readphy(tp, 0x10) & 0x0100) {
3402 rtl_writephy(tp, 0x1f, 0x0bcc);
3403 rtl_w1w0_phy(tp, 0x12, 0x0000, 0x8000);
3404 } else {
3405 rtl_writephy(tp, 0x1f, 0x0bcc);
3406 rtl_w1w0_phy(tp, 0x12, 0x8000, 0x0000);
3407 }
Hayes Wangc5583862012-07-02 17:23:22 +08003408
hayeswang41f44d12013-04-01 22:23:36 +00003409 rtl_writephy(tp, 0x1f, 0x0a46);
3410 if (rtl_readphy(tp, 0x13) & 0x0100) {
3411 rtl_writephy(tp, 0x1f, 0x0c41);
3412 rtl_w1w0_phy(tp, 0x15, 0x0002, 0x0000);
3413 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003414 rtl_writephy(tp, 0x1f, 0x0c41);
3415 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003416 }
Hayes Wangc5583862012-07-02 17:23:22 +08003417
hayeswang41f44d12013-04-01 22:23:36 +00003418 /* Enable PHY auto speed down */
3419 rtl_writephy(tp, 0x1f, 0x0a44);
3420 rtl_w1w0_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003421
hayeswangfe7524c2013-04-01 22:23:37 +00003422 rtl_writephy(tp, 0x1f, 0x0bcc);
3423 rtl_w1w0_phy(tp, 0x14, 0x0100, 0x0000);
3424 rtl_writephy(tp, 0x1f, 0x0a44);
3425 rtl_w1w0_phy(tp, 0x11, 0x00c0, 0x0000);
3426 rtl_writephy(tp, 0x1f, 0x0a43);
3427 rtl_writephy(tp, 0x13, 0x8084);
3428 rtl_w1w0_phy(tp, 0x14, 0x0000, 0x6000);
3429 rtl_w1w0_phy(tp, 0x10, 0x1003, 0x0000);
3430
hayeswang41f44d12013-04-01 22:23:36 +00003431 /* EEE auto-fallback function */
3432 rtl_writephy(tp, 0x1f, 0x0a4b);
3433 rtl_w1w0_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003434
hayeswang41f44d12013-04-01 22:23:36 +00003435 /* Enable UC LPF tune function */
3436 rtl_writephy(tp, 0x1f, 0x0a43);
3437 rtl_writephy(tp, 0x13, 0x8012);
3438 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3439
3440 rtl_writephy(tp, 0x1f, 0x0c42);
3441 rtl_w1w0_phy(tp, 0x11, 0x4000, 0x2000);
3442
hayeswangfe7524c2013-04-01 22:23:37 +00003443 /* Improve SWR Efficiency */
3444 rtl_writephy(tp, 0x1f, 0x0bcd);
3445 rtl_writephy(tp, 0x14, 0x5065);
3446 rtl_writephy(tp, 0x14, 0xd065);
3447 rtl_writephy(tp, 0x1f, 0x0bc8);
3448 rtl_writephy(tp, 0x11, 0x5655);
3449 rtl_writephy(tp, 0x1f, 0x0bcd);
3450 rtl_writephy(tp, 0x14, 0x1065);
3451 rtl_writephy(tp, 0x14, 0x9065);
3452 rtl_writephy(tp, 0x14, 0x1065);
3453
David Chang1bac1072013-11-27 15:48:36 +08003454 /* Check ALDPS bit, disable it if enabled */
3455 rtl_writephy(tp, 0x1f, 0x0a43);
3456 if (rtl_readphy(tp, 0x10) & 0x0004)
3457 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0004);
3458
hayeswang41f44d12013-04-01 22:23:36 +00003459 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003460}
3461
hayeswang57538c42013-04-01 22:23:40 +00003462static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3463{
3464 rtl_apply_firmware(tp);
3465}
3466
françois romieu4da19632011-01-03 15:07:55 +00003467static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003468{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003469 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003470 { 0x1f, 0x0003 },
3471 { 0x08, 0x441d },
3472 { 0x01, 0x9100 },
3473 { 0x1f, 0x0000 }
3474 };
3475
françois romieu4da19632011-01-03 15:07:55 +00003476 rtl_writephy(tp, 0x1f, 0x0000);
3477 rtl_patchphy(tp, 0x11, 1 << 12);
3478 rtl_patchphy(tp, 0x19, 1 << 13);
3479 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003480
françois romieu4da19632011-01-03 15:07:55 +00003481 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003482}
3483
Hayes Wang5a5e4442011-02-22 17:26:21 +08003484static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3485{
3486 static const struct phy_reg phy_reg_init[] = {
3487 { 0x1f, 0x0005 },
3488 { 0x1a, 0x0000 },
3489 { 0x1f, 0x0000 },
3490
3491 { 0x1f, 0x0004 },
3492 { 0x1c, 0x0000 },
3493 { 0x1f, 0x0000 },
3494
3495 { 0x1f, 0x0001 },
3496 { 0x15, 0x7701 },
3497 { 0x1f, 0x0000 }
3498 };
3499
3500 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003501 rtl_writephy(tp, 0x1f, 0x0000);
3502 rtl_writephy(tp, 0x18, 0x0310);
3503 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003504
François Romieu953a12c2011-04-24 17:38:48 +02003505 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003506
3507 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3508}
3509
Hayes Wang7e18dca2012-03-30 14:33:02 +08003510static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3511{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003512 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003513 rtl_writephy(tp, 0x1f, 0x0000);
3514 rtl_writephy(tp, 0x18, 0x0310);
3515 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003516
3517 rtl_apply_firmware(tp);
3518
3519 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003520 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003521 rtl_writephy(tp, 0x1f, 0x0004);
3522 rtl_writephy(tp, 0x10, 0x401f);
3523 rtl_writephy(tp, 0x19, 0x7030);
3524 rtl_writephy(tp, 0x1f, 0x0000);
3525}
3526
Hayes Wang5598bfe2012-07-02 17:23:21 +08003527static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3528{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003529 static const struct phy_reg phy_reg_init[] = {
3530 { 0x1f, 0x0004 },
3531 { 0x10, 0xc07f },
3532 { 0x19, 0x7030 },
3533 { 0x1f, 0x0000 }
3534 };
3535
3536 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003537 rtl_writephy(tp, 0x1f, 0x0000);
3538 rtl_writephy(tp, 0x18, 0x0310);
3539 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003540
3541 rtl_apply_firmware(tp);
3542
Francois Romieufdf6fc02012-07-06 22:40:38 +02003543 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003544 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3545
Francois Romieufdf6fc02012-07-06 22:40:38 +02003546 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003547}
3548
Francois Romieu5615d9f2007-08-17 17:50:46 +02003549static void rtl_hw_phy_config(struct net_device *dev)
3550{
3551 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003552
3553 rtl8169_print_mac_version(tp);
3554
3555 switch (tp->mac_version) {
3556 case RTL_GIGA_MAC_VER_01:
3557 break;
3558 case RTL_GIGA_MAC_VER_02:
3559 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003560 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003561 break;
3562 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003563 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003564 break;
françois romieu2e9558562009-08-10 19:44:19 +00003565 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003566 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003567 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003568 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003569 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003570 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003571 case RTL_GIGA_MAC_VER_07:
3572 case RTL_GIGA_MAC_VER_08:
3573 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003574 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003575 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003576 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003577 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003578 break;
3579 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003580 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003581 break;
3582 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003583 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003584 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003585 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003586 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003587 break;
3588 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003589 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003590 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003591 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003592 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003593 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003594 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003595 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003596 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003597 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003598 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003599 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003600 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003601 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003602 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003603 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003604 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003605 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003606 break;
3607 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003608 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003609 break;
3610 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003611 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003612 break;
françois romieue6de30d2011-01-03 15:08:37 +00003613 case RTL_GIGA_MAC_VER_28:
3614 rtl8168d_4_hw_phy_config(tp);
3615 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003616 case RTL_GIGA_MAC_VER_29:
3617 case RTL_GIGA_MAC_VER_30:
3618 rtl8105e_hw_phy_config(tp);
3619 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003620 case RTL_GIGA_MAC_VER_31:
3621 /* None. */
3622 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003623 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003624 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003625 rtl8168e_1_hw_phy_config(tp);
3626 break;
3627 case RTL_GIGA_MAC_VER_34:
3628 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003629 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003630 case RTL_GIGA_MAC_VER_35:
3631 rtl8168f_1_hw_phy_config(tp);
3632 break;
3633 case RTL_GIGA_MAC_VER_36:
3634 rtl8168f_2_hw_phy_config(tp);
3635 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003636
Hayes Wang7e18dca2012-03-30 14:33:02 +08003637 case RTL_GIGA_MAC_VER_37:
3638 rtl8402_hw_phy_config(tp);
3639 break;
3640
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003641 case RTL_GIGA_MAC_VER_38:
3642 rtl8411_hw_phy_config(tp);
3643 break;
3644
Hayes Wang5598bfe2012-07-02 17:23:21 +08003645 case RTL_GIGA_MAC_VER_39:
3646 rtl8106e_hw_phy_config(tp);
3647 break;
3648
Hayes Wangc5583862012-07-02 17:23:22 +08003649 case RTL_GIGA_MAC_VER_40:
3650 rtl8168g_1_hw_phy_config(tp);
3651 break;
hayeswang57538c42013-04-01 22:23:40 +00003652 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003653 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003654 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00003655 rtl8168g_2_hw_phy_config(tp);
3656 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003657
3658 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003659 default:
3660 break;
3661 }
3662}
3663
Francois Romieuda78dbf2012-01-26 14:18:23 +01003664static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 struct timer_list *timer = &tp->timer;
3667 void __iomem *ioaddr = tp->mmio_addr;
3668 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3669
Francois Romieubcf0bf92006-07-26 23:14:13 +02003670 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
françois romieu4da19632011-01-03 15:07:55 +00003672 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003673 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 * A busy loop could burn quite a few cycles on nowadays CPU.
3675 * Let's delay the execution of the timer for a few ticks.
3676 */
3677 timeout = HZ/10;
3678 goto out_mod_timer;
3679 }
3680
3681 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Lekensteyn9bb8eeb2013-08-02 10:36:55 +02003684 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
françois romieu4da19632011-01-03 15:07:55 +00003686 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
3688out_mod_timer:
3689 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003690}
3691
3692static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3693{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003694 if (!test_and_set_bit(flag, tp->wk.flags))
3695 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003696}
3697
3698static void rtl8169_phy_timer(unsigned long __opaque)
3699{
3700 struct net_device *dev = (struct net_device *)__opaque;
3701 struct rtl8169_private *tp = netdev_priv(dev);
3702
Francois Romieu98ddf982012-01-31 10:47:34 +01003703 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3707 void __iomem *ioaddr)
3708{
3709 iounmap(ioaddr);
3710 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003711 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 pci_disable_device(pdev);
3713 free_netdev(dev);
3714}
3715
Francois Romieuffc46952012-07-06 14:19:23 +02003716DECLARE_RTL_COND(rtl_phy_reset_cond)
3717{
3718 return tp->phy_reset_pending(tp);
3719}
3720
Francois Romieubf793292006-11-01 00:53:05 +01003721static void rtl8169_phy_reset(struct net_device *dev,
3722 struct rtl8169_private *tp)
3723{
françois romieu4da19632011-01-03 15:07:55 +00003724 tp->phy_reset_enable(tp);
Francois Romieuffc46952012-07-06 14:19:23 +02003725 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
Francois Romieubf793292006-11-01 00:53:05 +01003726}
3727
David S. Miller8decf862011-09-22 03:23:13 -04003728static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3729{
3730 void __iomem *ioaddr = tp->mmio_addr;
3731
3732 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3733 (RTL_R8(PHYstatus) & TBI_Enable);
3734}
3735
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003736static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003738 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003739
Francois Romieu5615d9f2007-08-17 17:50:46 +02003740 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003741
Marcus Sundberg773328942008-07-10 21:28:08 +02003742 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3743 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3744 RTL_W8(0x82, 0x01);
3745 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003746
Francois Romieu6dccd162007-02-13 23:38:05 +01003747 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3748
3749 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3750 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003751
Francois Romieubcf0bf92006-07-26 23:14:13 +02003752 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003753 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3754 RTL_W8(0x82, 0x01);
3755 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003756 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003757 }
3758
Francois Romieubf793292006-11-01 00:53:05 +01003759 rtl8169_phy_reset(dev, tp);
3760
Oliver Neukum54405cd2011-01-06 21:55:13 +01003761 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003762 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3763 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3764 (tp->mii.supports_gmii ?
3765 ADVERTISED_1000baseT_Half |
3766 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003767
David S. Miller8decf862011-09-22 03:23:13 -04003768 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003769 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003770}
3771
Francois Romieu773d2022007-01-31 23:47:43 +01003772static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3773{
3774 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu773d2022007-01-31 23:47:43 +01003775
Francois Romieuda78dbf2012-01-26 14:18:23 +01003776 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003777
3778 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003779
françois romieu9ecb9aa2012-12-07 11:20:21 +00003780 RTL_W32(MAC4, addr[4] | addr[5] << 8);
françois romieu908ba2b2010-04-26 11:42:58 +00003781 RTL_R32(MAC4);
3782
françois romieu9ecb9aa2012-12-07 11:20:21 +00003783 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
françois romieu908ba2b2010-04-26 11:42:58 +00003784 RTL_R32(MAC0);
3785
françois romieu9ecb9aa2012-12-07 11:20:21 +00003786 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3787 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003788
Francois Romieu773d2022007-01-31 23:47:43 +01003789 RTL_W8(Cfg9346, Cfg9346_Lock);
3790
Francois Romieuda78dbf2012-01-26 14:18:23 +01003791 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003792}
3793
3794static int rtl_set_mac_address(struct net_device *dev, void *p)
3795{
3796 struct rtl8169_private *tp = netdev_priv(dev);
3797 struct sockaddr *addr = p;
3798
3799 if (!is_valid_ether_addr(addr->sa_data))
3800 return -EADDRNOTAVAIL;
3801
3802 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3803
3804 rtl_rar_set(tp, dev->dev_addr);
3805
3806 return 0;
3807}
3808
Francois Romieu5f787a12006-08-17 13:02:36 +02003809static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3810{
3811 struct rtl8169_private *tp = netdev_priv(dev);
3812 struct mii_ioctl_data *data = if_mii(ifr);
3813
Francois Romieu8b4ab282008-11-19 22:05:25 -08003814 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3815}
Francois Romieu5f787a12006-08-17 13:02:36 +02003816
Francois Romieucecb5fd2011-04-01 10:21:07 +02003817static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3818 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003819{
Francois Romieu5f787a12006-08-17 13:02:36 +02003820 switch (cmd) {
3821 case SIOCGMIIPHY:
3822 data->phy_id = 32; /* Internal PHY */
3823 return 0;
3824
3825 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003826 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003827 return 0;
3828
3829 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003830 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003831 return 0;
3832 }
3833 return -EOPNOTSUPP;
3834}
3835
Francois Romieu8b4ab282008-11-19 22:05:25 -08003836static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3837{
3838 return -EOPNOTSUPP;
3839}
3840
Francois Romieufbac58f2007-10-04 22:51:38 +02003841static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3842{
3843 if (tp->features & RTL_FEATURE_MSI) {
3844 pci_disable_msi(pdev);
3845 tp->features &= ~RTL_FEATURE_MSI;
3846 }
3847}
3848
Bill Pembertonbaf63292012-12-03 09:23:28 -05003849static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00003850{
3851 struct mdio_ops *ops = &tp->mdio_ops;
3852
3853 switch (tp->mac_version) {
3854 case RTL_GIGA_MAC_VER_27:
3855 ops->write = r8168dp_1_mdio_write;
3856 ops->read = r8168dp_1_mdio_read;
3857 break;
françois romieue6de30d2011-01-03 15:08:37 +00003858 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003859 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003860 ops->write = r8168dp_2_mdio_write;
3861 ops->read = r8168dp_2_mdio_read;
3862 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003863 case RTL_GIGA_MAC_VER_40:
3864 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00003865 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003866 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003867 case RTL_GIGA_MAC_VER_44:
Hayes Wangc5583862012-07-02 17:23:22 +08003868 ops->write = r8168g_mdio_write;
3869 ops->read = r8168g_mdio_read;
3870 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003871 default:
3872 ops->write = r8169_mdio_write;
3873 ops->read = r8169_mdio_read;
3874 break;
3875 }
3876}
3877
hayeswange2409d82013-03-31 17:02:04 +00003878static void rtl_speed_down(struct rtl8169_private *tp)
3879{
3880 u32 adv;
3881 int lpa;
3882
3883 rtl_writephy(tp, 0x1f, 0x0000);
3884 lpa = rtl_readphy(tp, MII_LPA);
3885
3886 if (lpa & (LPA_10HALF | LPA_10FULL))
3887 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
3888 else if (lpa & (LPA_100HALF | LPA_100FULL))
3889 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3890 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
3891 else
3892 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3893 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3894 (tp->mii.supports_gmii ?
3895 ADVERTISED_1000baseT_Half |
3896 ADVERTISED_1000baseT_Full : 0);
3897
3898 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
3899 adv);
3900}
3901
David S. Miller1805b2f2011-10-24 18:18:09 -04003902static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3903{
3904 void __iomem *ioaddr = tp->mmio_addr;
3905
3906 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003907 case RTL_GIGA_MAC_VER_25:
3908 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003909 case RTL_GIGA_MAC_VER_29:
3910 case RTL_GIGA_MAC_VER_30:
3911 case RTL_GIGA_MAC_VER_32:
3912 case RTL_GIGA_MAC_VER_33:
3913 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003914 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003915 case RTL_GIGA_MAC_VER_38:
Hayes Wang5598bfe2012-07-02 17:23:21 +08003916 case RTL_GIGA_MAC_VER_39:
Hayes Wangc5583862012-07-02 17:23:22 +08003917 case RTL_GIGA_MAC_VER_40:
3918 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00003919 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003920 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003921 case RTL_GIGA_MAC_VER_44:
David S. Miller1805b2f2011-10-24 18:18:09 -04003922 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3923 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3924 break;
3925 default:
3926 break;
3927 }
3928}
3929
3930static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3931{
3932 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3933 return false;
3934
hayeswange2409d82013-03-31 17:02:04 +00003935 rtl_speed_down(tp);
David S. Miller1805b2f2011-10-24 18:18:09 -04003936 rtl_wol_suspend_quirk(tp);
3937
3938 return true;
3939}
3940
françois romieu065c27c2011-01-03 15:08:12 +00003941static void r810x_phy_power_down(struct rtl8169_private *tp)
3942{
3943 rtl_writephy(tp, 0x1f, 0x0000);
3944 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3945}
3946
3947static void r810x_phy_power_up(struct rtl8169_private *tp)
3948{
3949 rtl_writephy(tp, 0x1f, 0x0000);
3950 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3951}
3952
3953static void r810x_pll_power_down(struct rtl8169_private *tp)
3954{
Hayes Wang00042992012-03-30 14:33:00 +08003955 void __iomem *ioaddr = tp->mmio_addr;
3956
David S. Miller1805b2f2011-10-24 18:18:09 -04003957 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003958 return;
françois romieu065c27c2011-01-03 15:08:12 +00003959
3960 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003961
3962 switch (tp->mac_version) {
3963 case RTL_GIGA_MAC_VER_07:
3964 case RTL_GIGA_MAC_VER_08:
3965 case RTL_GIGA_MAC_VER_09:
3966 case RTL_GIGA_MAC_VER_10:
3967 case RTL_GIGA_MAC_VER_13:
3968 case RTL_GIGA_MAC_VER_16:
3969 break;
3970 default:
3971 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3972 break;
3973 }
françois romieu065c27c2011-01-03 15:08:12 +00003974}
3975
3976static void r810x_pll_power_up(struct rtl8169_private *tp)
3977{
Hayes Wang00042992012-03-30 14:33:00 +08003978 void __iomem *ioaddr = tp->mmio_addr;
3979
françois romieu065c27c2011-01-03 15:08:12 +00003980 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003981
3982 switch (tp->mac_version) {
3983 case RTL_GIGA_MAC_VER_07:
3984 case RTL_GIGA_MAC_VER_08:
3985 case RTL_GIGA_MAC_VER_09:
3986 case RTL_GIGA_MAC_VER_10:
3987 case RTL_GIGA_MAC_VER_13:
3988 case RTL_GIGA_MAC_VER_16:
3989 break;
3990 default:
3991 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3992 break;
3993 }
françois romieu065c27c2011-01-03 15:08:12 +00003994}
3995
3996static void r8168_phy_power_up(struct rtl8169_private *tp)
3997{
3998 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003999 switch (tp->mac_version) {
4000 case RTL_GIGA_MAC_VER_11:
4001 case RTL_GIGA_MAC_VER_12:
4002 case RTL_GIGA_MAC_VER_17:
4003 case RTL_GIGA_MAC_VER_18:
4004 case RTL_GIGA_MAC_VER_19:
4005 case RTL_GIGA_MAC_VER_20:
4006 case RTL_GIGA_MAC_VER_21:
4007 case RTL_GIGA_MAC_VER_22:
4008 case RTL_GIGA_MAC_VER_23:
4009 case RTL_GIGA_MAC_VER_24:
4010 case RTL_GIGA_MAC_VER_25:
4011 case RTL_GIGA_MAC_VER_26:
4012 case RTL_GIGA_MAC_VER_27:
4013 case RTL_GIGA_MAC_VER_28:
4014 case RTL_GIGA_MAC_VER_31:
4015 rtl_writephy(tp, 0x0e, 0x0000);
4016 break;
4017 default:
4018 break;
4019 }
françois romieu065c27c2011-01-03 15:08:12 +00004020 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4021}
4022
4023static void r8168_phy_power_down(struct rtl8169_private *tp)
4024{
4025 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00004026 switch (tp->mac_version) {
4027 case RTL_GIGA_MAC_VER_32:
4028 case RTL_GIGA_MAC_VER_33:
hayeswangbeb330a2013-04-01 22:23:39 +00004029 case RTL_GIGA_MAC_VER_40:
4030 case RTL_GIGA_MAC_VER_41:
hayeswang01dc7fe2011-03-21 01:50:28 +00004031 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4032 break;
4033
4034 case RTL_GIGA_MAC_VER_11:
4035 case RTL_GIGA_MAC_VER_12:
4036 case RTL_GIGA_MAC_VER_17:
4037 case RTL_GIGA_MAC_VER_18:
4038 case RTL_GIGA_MAC_VER_19:
4039 case RTL_GIGA_MAC_VER_20:
4040 case RTL_GIGA_MAC_VER_21:
4041 case RTL_GIGA_MAC_VER_22:
4042 case RTL_GIGA_MAC_VER_23:
4043 case RTL_GIGA_MAC_VER_24:
4044 case RTL_GIGA_MAC_VER_25:
4045 case RTL_GIGA_MAC_VER_26:
4046 case RTL_GIGA_MAC_VER_27:
4047 case RTL_GIGA_MAC_VER_28:
4048 case RTL_GIGA_MAC_VER_31:
4049 rtl_writephy(tp, 0x0e, 0x0200);
4050 default:
4051 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4052 break;
4053 }
françois romieu065c27c2011-01-03 15:08:12 +00004054}
4055
4056static void r8168_pll_power_down(struct rtl8169_private *tp)
4057{
4058 void __iomem *ioaddr = tp->mmio_addr;
4059
Francois Romieucecb5fd2011-04-01 10:21:07 +02004060 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4061 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4062 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00004063 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00004064 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08004065 }
françois romieu065c27c2011-01-03 15:08:12 +00004066
Francois Romieucecb5fd2011-04-01 10:21:07 +02004067 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4068 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00004069 (RTL_R16(CPlusCmd) & ASF)) {
4070 return;
4071 }
4072
hayeswang01dc7fe2011-03-21 01:50:28 +00004073 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4074 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004075 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004076
David S. Miller1805b2f2011-10-24 18:18:09 -04004077 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004078 return;
françois romieu065c27c2011-01-03 15:08:12 +00004079
4080 r8168_phy_power_down(tp);
4081
4082 switch (tp->mac_version) {
4083 case RTL_GIGA_MAC_VER_25:
4084 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004085 case RTL_GIGA_MAC_VER_27:
4086 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004087 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004088 case RTL_GIGA_MAC_VER_32:
4089 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004090 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4091 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004092 case RTL_GIGA_MAC_VER_40:
4093 case RTL_GIGA_MAC_VER_41:
4094 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4095 0xfc000000, ERIAR_EXGMAC);
4096 break;
françois romieu065c27c2011-01-03 15:08:12 +00004097 }
4098}
4099
4100static void r8168_pll_power_up(struct rtl8169_private *tp)
4101{
4102 void __iomem *ioaddr = tp->mmio_addr;
4103
françois romieu065c27c2011-01-03 15:08:12 +00004104 switch (tp->mac_version) {
4105 case RTL_GIGA_MAC_VER_25:
4106 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08004107 case RTL_GIGA_MAC_VER_27:
4108 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004109 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004110 case RTL_GIGA_MAC_VER_32:
4111 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00004112 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4113 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004114 case RTL_GIGA_MAC_VER_40:
4115 case RTL_GIGA_MAC_VER_41:
4116 rtl_w1w0_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4117 0x00000000, ERIAR_EXGMAC);
4118 break;
françois romieu065c27c2011-01-03 15:08:12 +00004119 }
4120
4121 r8168_phy_power_up(tp);
4122}
4123
Francois Romieud58d46b2011-05-03 16:38:29 +02004124static void rtl_generic_op(struct rtl8169_private *tp,
4125 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00004126{
4127 if (op)
4128 op(tp);
4129}
4130
4131static void rtl_pll_power_down(struct rtl8169_private *tp)
4132{
Francois Romieud58d46b2011-05-03 16:38:29 +02004133 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00004134}
4135
4136static void rtl_pll_power_up(struct rtl8169_private *tp)
4137{
Francois Romieud58d46b2011-05-03 16:38:29 +02004138 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00004139}
4140
Bill Pembertonbaf63292012-12-03 09:23:28 -05004141static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00004142{
4143 struct pll_power_ops *ops = &tp->pll_power_ops;
4144
4145 switch (tp->mac_version) {
4146 case RTL_GIGA_MAC_VER_07:
4147 case RTL_GIGA_MAC_VER_08:
4148 case RTL_GIGA_MAC_VER_09:
4149 case RTL_GIGA_MAC_VER_10:
4150 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08004151 case RTL_GIGA_MAC_VER_29:
4152 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004153 case RTL_GIGA_MAC_VER_37:
Hayes Wang5598bfe2012-07-02 17:23:21 +08004154 case RTL_GIGA_MAC_VER_39:
hayeswang58152cd2013-04-01 22:23:42 +00004155 case RTL_GIGA_MAC_VER_43:
françois romieu065c27c2011-01-03 15:08:12 +00004156 ops->down = r810x_pll_power_down;
4157 ops->up = r810x_pll_power_up;
4158 break;
4159
4160 case RTL_GIGA_MAC_VER_11:
4161 case RTL_GIGA_MAC_VER_12:
4162 case RTL_GIGA_MAC_VER_17:
4163 case RTL_GIGA_MAC_VER_18:
4164 case RTL_GIGA_MAC_VER_19:
4165 case RTL_GIGA_MAC_VER_20:
4166 case RTL_GIGA_MAC_VER_21:
4167 case RTL_GIGA_MAC_VER_22:
4168 case RTL_GIGA_MAC_VER_23:
4169 case RTL_GIGA_MAC_VER_24:
4170 case RTL_GIGA_MAC_VER_25:
4171 case RTL_GIGA_MAC_VER_26:
4172 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00004173 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004174 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00004175 case RTL_GIGA_MAC_VER_32:
4176 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004177 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08004178 case RTL_GIGA_MAC_VER_35:
4179 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004180 case RTL_GIGA_MAC_VER_38:
Hayes Wangc5583862012-07-02 17:23:22 +08004181 case RTL_GIGA_MAC_VER_40:
4182 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004183 case RTL_GIGA_MAC_VER_42:
hayeswang45dd95c2013-07-08 17:09:01 +08004184 case RTL_GIGA_MAC_VER_44:
françois romieu065c27c2011-01-03 15:08:12 +00004185 ops->down = r8168_pll_power_down;
4186 ops->up = r8168_pll_power_up;
4187 break;
4188
4189 default:
4190 ops->down = NULL;
4191 ops->up = NULL;
4192 break;
4193 }
4194}
4195
Hayes Wange542a222011-07-06 15:58:04 +08004196static void rtl_init_rxcfg(struct rtl8169_private *tp)
4197{
4198 void __iomem *ioaddr = tp->mmio_addr;
4199
4200 switch (tp->mac_version) {
4201 case RTL_GIGA_MAC_VER_01:
4202 case RTL_GIGA_MAC_VER_02:
4203 case RTL_GIGA_MAC_VER_03:
4204 case RTL_GIGA_MAC_VER_04:
4205 case RTL_GIGA_MAC_VER_05:
4206 case RTL_GIGA_MAC_VER_06:
4207 case RTL_GIGA_MAC_VER_10:
4208 case RTL_GIGA_MAC_VER_11:
4209 case RTL_GIGA_MAC_VER_12:
4210 case RTL_GIGA_MAC_VER_13:
4211 case RTL_GIGA_MAC_VER_14:
4212 case RTL_GIGA_MAC_VER_15:
4213 case RTL_GIGA_MAC_VER_16:
4214 case RTL_GIGA_MAC_VER_17:
4215 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4216 break;
4217 case RTL_GIGA_MAC_VER_18:
4218 case RTL_GIGA_MAC_VER_19:
4219 case RTL_GIGA_MAC_VER_20:
4220 case RTL_GIGA_MAC_VER_21:
4221 case RTL_GIGA_MAC_VER_22:
4222 case RTL_GIGA_MAC_VER_23:
4223 case RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004224 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004225 case RTL_GIGA_MAC_VER_35:
Hayes Wange542a222011-07-06 15:58:04 +08004226 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4227 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004228 case RTL_GIGA_MAC_VER_40:
Michel Dänzer7a9810e2014-07-17 12:55:40 +09004229 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4230 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004231 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004232 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004233 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004234 case RTL_GIGA_MAC_VER_44:
hayeswangbeb330a2013-04-01 22:23:39 +00004235 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
4236 break;
Hayes Wange542a222011-07-06 15:58:04 +08004237 default:
4238 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
4239 break;
4240 }
4241}
4242
Hayes Wang92fc43b2011-07-06 15:58:03 +08004243static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4244{
Timo Teräs9fba0812013-01-15 21:01:24 +00004245 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004246}
4247
Francois Romieud58d46b2011-05-03 16:38:29 +02004248static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4249{
françois romieu9c5028e2012-03-02 04:43:14 +00004250 void __iomem *ioaddr = tp->mmio_addr;
4251
4252 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004253 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00004254 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004255}
4256
4257static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4258{
françois romieu9c5028e2012-03-02 04:43:14 +00004259 void __iomem *ioaddr = tp->mmio_addr;
4260
4261 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004262 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00004263 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02004264}
4265
4266static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4267{
4268 void __iomem *ioaddr = tp->mmio_addr;
4269
4270 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4271 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
4272 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
4273}
4274
4275static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4276{
4277 void __iomem *ioaddr = tp->mmio_addr;
4278
4279 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4280 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
4281 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
4282}
4283
4284static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4285{
4286 void __iomem *ioaddr = tp->mmio_addr;
4287
4288 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4289}
4290
4291static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4292{
4293 void __iomem *ioaddr = tp->mmio_addr;
4294
4295 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4296}
4297
4298static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4299{
4300 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004301
4302 RTL_W8(MaxTxPacketSize, 0x3f);
4303 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
4304 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004305 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004306}
4307
4308static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4309{
4310 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02004311
4312 RTL_W8(MaxTxPacketSize, 0x0c);
4313 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
4314 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01004315 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02004316}
4317
4318static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4319{
4320 rtl_tx_performance_tweak(tp->pci_dev,
4321 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4322}
4323
4324static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4325{
4326 rtl_tx_performance_tweak(tp->pci_dev,
4327 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
4328}
4329
4330static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4331{
4332 void __iomem *ioaddr = tp->mmio_addr;
4333
4334 r8168b_0_hw_jumbo_enable(tp);
4335
4336 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
4337}
4338
4339static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4340{
4341 void __iomem *ioaddr = tp->mmio_addr;
4342
4343 r8168b_0_hw_jumbo_disable(tp);
4344
4345 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4346}
4347
Bill Pembertonbaf63292012-12-03 09:23:28 -05004348static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004349{
4350 struct jumbo_ops *ops = &tp->jumbo_ops;
4351
4352 switch (tp->mac_version) {
4353 case RTL_GIGA_MAC_VER_11:
4354 ops->disable = r8168b_0_hw_jumbo_disable;
4355 ops->enable = r8168b_0_hw_jumbo_enable;
4356 break;
4357 case RTL_GIGA_MAC_VER_12:
4358 case RTL_GIGA_MAC_VER_17:
4359 ops->disable = r8168b_1_hw_jumbo_disable;
4360 ops->enable = r8168b_1_hw_jumbo_enable;
4361 break;
4362 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4363 case RTL_GIGA_MAC_VER_19:
4364 case RTL_GIGA_MAC_VER_20:
4365 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4366 case RTL_GIGA_MAC_VER_22:
4367 case RTL_GIGA_MAC_VER_23:
4368 case RTL_GIGA_MAC_VER_24:
4369 case RTL_GIGA_MAC_VER_25:
4370 case RTL_GIGA_MAC_VER_26:
4371 ops->disable = r8168c_hw_jumbo_disable;
4372 ops->enable = r8168c_hw_jumbo_enable;
4373 break;
4374 case RTL_GIGA_MAC_VER_27:
4375 case RTL_GIGA_MAC_VER_28:
4376 ops->disable = r8168dp_hw_jumbo_disable;
4377 ops->enable = r8168dp_hw_jumbo_enable;
4378 break;
4379 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4380 case RTL_GIGA_MAC_VER_32:
4381 case RTL_GIGA_MAC_VER_33:
4382 case RTL_GIGA_MAC_VER_34:
4383 ops->disable = r8168e_hw_jumbo_disable;
4384 ops->enable = r8168e_hw_jumbo_enable;
4385 break;
4386
4387 /*
4388 * No action needed for jumbo frames with 8169.
4389 * No jumbo for 810x at all.
4390 */
Hayes Wangc5583862012-07-02 17:23:22 +08004391 case RTL_GIGA_MAC_VER_40:
4392 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00004393 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004394 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004395 case RTL_GIGA_MAC_VER_44:
Francois Romieud58d46b2011-05-03 16:38:29 +02004396 default:
4397 ops->disable = NULL;
4398 ops->enable = NULL;
4399 break;
4400 }
4401}
4402
Francois Romieuffc46952012-07-06 14:19:23 +02004403DECLARE_RTL_COND(rtl_chipcmd_cond)
4404{
4405 void __iomem *ioaddr = tp->mmio_addr;
4406
4407 return RTL_R8(ChipCmd) & CmdReset;
4408}
4409
Francois Romieu6f43adc2011-04-29 15:05:51 +02004410static void rtl_hw_reset(struct rtl8169_private *tp)
4411{
4412 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu6f43adc2011-04-29 15:05:51 +02004413
Francois Romieu6f43adc2011-04-29 15:05:51 +02004414 RTL_W8(ChipCmd, CmdReset);
4415
Francois Romieuffc46952012-07-06 14:19:23 +02004416 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004417}
4418
Francois Romieub6ffd972011-06-17 17:00:05 +02004419static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4420{
4421 struct rtl_fw *rtl_fw;
4422 const char *name;
4423 int rc = -ENOMEM;
4424
4425 name = rtl_lookup_firmware_name(tp);
4426 if (!name)
4427 goto out_no_firmware;
4428
4429 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4430 if (!rtl_fw)
4431 goto err_warn;
4432
4433 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4434 if (rc < 0)
4435 goto err_free;
4436
Francois Romieufd112f22011-06-18 00:10:29 +02004437 rc = rtl_check_firmware(tp, rtl_fw);
4438 if (rc < 0)
4439 goto err_release_firmware;
4440
Francois Romieub6ffd972011-06-17 17:00:05 +02004441 tp->rtl_fw = rtl_fw;
4442out:
4443 return;
4444
Francois Romieufd112f22011-06-18 00:10:29 +02004445err_release_firmware:
4446 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004447err_free:
4448 kfree(rtl_fw);
4449err_warn:
4450 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4451 name, rc);
4452out_no_firmware:
4453 tp->rtl_fw = NULL;
4454 goto out;
4455}
4456
François Romieu953a12c2011-04-24 17:38:48 +02004457static void rtl_request_firmware(struct rtl8169_private *tp)
4458{
Francois Romieub6ffd972011-06-17 17:00:05 +02004459 if (IS_ERR(tp->rtl_fw))
4460 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004461}
4462
Hayes Wang92fc43b2011-07-06 15:58:03 +08004463static void rtl_rx_close(struct rtl8169_private *tp)
4464{
4465 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004466
Francois Romieu1687b562011-07-19 17:21:29 +02004467 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004468}
4469
Francois Romieuffc46952012-07-06 14:19:23 +02004470DECLARE_RTL_COND(rtl_npq_cond)
4471{
4472 void __iomem *ioaddr = tp->mmio_addr;
4473
4474 return RTL_R8(TxPoll) & NPQ;
4475}
4476
4477DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4478{
4479 void __iomem *ioaddr = tp->mmio_addr;
4480
4481 return RTL_R32(TxConfig) & TXCFG_EMPTY;
4482}
4483
françois romieue6de30d2011-01-03 15:08:37 +00004484static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
françois romieue6de30d2011-01-03 15:08:37 +00004486 void __iomem *ioaddr = tp->mmio_addr;
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004489 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Hayes Wang92fc43b2011-07-06 15:58:03 +08004491 rtl_rx_close(tp);
4492
Hayes Wang5d2e1952011-02-22 17:26:22 +08004493 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004494 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4495 tp->mac_version == RTL_GIGA_MAC_VER_31) {
Francois Romieuffc46952012-07-06 14:19:23 +02004496 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Hayes Wangc2218922011-09-06 16:55:18 +08004497 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4498 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004499 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004500 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
Hayes Wangc5583862012-07-02 17:23:22 +08004501 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
4502 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
hayeswang57538c42013-04-01 22:23:40 +00004503 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
hayeswang58152cd2013-04-01 22:23:42 +00004504 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
hayeswang45dd95c2013-07-08 17:09:01 +08004505 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004506 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004507 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004508 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004509 } else {
4510 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4511 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004512 }
4513
Hayes Wang92fc43b2011-07-06 15:58:03 +08004514 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Francois Romieu7f796d832007-06-11 23:04:41 +02004517static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004518{
4519 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004520
4521 /* Set DMA burst size and Interframe Gap Time */
4522 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4523 (InterFrameGap << TxInterFrameGapShift));
4524}
4525
Francois Romieu07ce4062007-02-23 23:36:39 +01004526static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
4528 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Francois Romieu07ce4062007-02-23 23:36:39 +01004530 tp->hw_start(dev);
4531
Francois Romieuda78dbf2012-01-26 14:18:23 +01004532 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004533}
4534
Francois Romieu7f796d832007-06-11 23:04:41 +02004535static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4536 void __iomem *ioaddr)
4537{
4538 /*
4539 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4540 * register to be written before TxDescAddrLow to work.
4541 * Switching from MMIO to I/O access fixes the issue as well.
4542 */
4543 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004544 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004545 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004546 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004547}
4548
4549static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4550{
4551 u16 cmd;
4552
4553 cmd = RTL_R16(CPlusCmd);
4554 RTL_W16(CPlusCmd, cmd);
4555 return cmd;
4556}
4557
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004558static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004559{
4560 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004561 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004562}
4563
Francois Romieu6dccd162007-02-13 23:38:05 +01004564static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4565{
Francois Romieu37441002011-06-17 22:58:54 +02004566 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004567 u32 mac_version;
4568 u32 clk;
4569 u32 val;
4570 } cfg2_info [] = {
4571 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4572 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4573 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4574 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004575 };
4576 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004577 unsigned int i;
4578 u32 clk;
4579
4580 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004581 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004582 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4583 RTL_W32(0x7c, p->val);
4584 break;
4585 }
4586 }
4587}
4588
Francois Romieue6b763e2012-03-08 09:35:39 +01004589static void rtl_set_rx_mode(struct net_device *dev)
4590{
4591 struct rtl8169_private *tp = netdev_priv(dev);
4592 void __iomem *ioaddr = tp->mmio_addr;
4593 u32 mc_filter[2]; /* Multicast hash filter */
4594 int rx_mode;
4595 u32 tmp = 0;
4596
4597 if (dev->flags & IFF_PROMISC) {
4598 /* Unconditionally log net taps. */
4599 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4600 rx_mode =
4601 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4602 AcceptAllPhys;
4603 mc_filter[1] = mc_filter[0] = 0xffffffff;
4604 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4605 (dev->flags & IFF_ALLMULTI)) {
4606 /* Too many to filter perfectly -- accept all multicasts. */
4607 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4608 mc_filter[1] = mc_filter[0] = 0xffffffff;
4609 } else {
4610 struct netdev_hw_addr *ha;
4611
4612 rx_mode = AcceptBroadcast | AcceptMyPhys;
4613 mc_filter[1] = mc_filter[0] = 0;
4614 netdev_for_each_mc_addr(ha, dev) {
4615 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4616 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4617 rx_mode |= AcceptMulticast;
4618 }
4619 }
4620
4621 if (dev->features & NETIF_F_RXALL)
4622 rx_mode |= (AcceptErr | AcceptRunt);
4623
4624 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4625
4626 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4627 u32 data = mc_filter[0];
4628
4629 mc_filter[0] = swab32(mc_filter[1]);
4630 mc_filter[1] = swab32(data);
4631 }
4632
Nathan Walp04817762012-11-01 12:08:47 +00004633 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4634 mc_filter[1] = mc_filter[0] = 0xffffffff;
4635
Francois Romieue6b763e2012-03-08 09:35:39 +01004636 RTL_W32(MAR0 + 4, mc_filter[1]);
4637 RTL_W32(MAR0 + 0, mc_filter[0]);
4638
4639 RTL_W32(RxConfig, tmp);
4640}
4641
Francois Romieu07ce4062007-02-23 23:36:39 +01004642static void rtl_hw_start_8169(struct net_device *dev)
4643{
4644 struct rtl8169_private *tp = netdev_priv(dev);
4645 void __iomem *ioaddr = tp->mmio_addr;
4646 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004647
Francois Romieu9cb427b2006-11-02 00:10:16 +01004648 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4649 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4650 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4651 }
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004654 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4655 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4656 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4657 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004658 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4659
Hayes Wange542a222011-07-06 15:58:04 +08004660 rtl_init_rxcfg(tp);
4661
françois romieuf0298f82011-01-03 15:07:42 +00004662 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004664 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Francois Romieucecb5fd2011-04-01 10:21:07 +02004666 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4667 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4668 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4669 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004670 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Francois Romieu7f796d832007-06-11 23:04:41 +02004672 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004673
Francois Romieucecb5fd2011-04-01 10:21:07 +02004674 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4675 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004676 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004678 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
4680
Francois Romieubcf0bf92006-07-26 23:14:13 +02004681 RTL_W16(CPlusCmd, tp->cp_cmd);
4682
Francois Romieu6dccd162007-02-13 23:38:05 +01004683 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 /*
4686 * Undocumented corner. Supposedly:
4687 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4688 */
4689 RTL_W16(IntrMitigate, 0x0000);
4690
Francois Romieu7f796d832007-06-11 23:04:41 +02004691 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004692
Francois Romieucecb5fd2011-04-01 10:21:07 +02004693 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4694 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4695 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4696 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004697 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4698 rtl_set_rx_tx_config_registers(tp);
4699 }
4700
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004702
4703 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4704 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 RTL_W32(RxMissed, 0);
4707
Francois Romieu07ce4062007-02-23 23:36:39 +01004708 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 /* no early-rx interrupts */
4711 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004714static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4715{
4716 if (tp->csi_ops.write)
Francois Romieu52989f02012-07-06 13:37:00 +02004717 tp->csi_ops.write(tp, addr, value);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004718}
4719
4720static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4721{
Francois Romieu52989f02012-07-06 13:37:00 +02004722 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004723}
4724
4725static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004726{
4727 u32 csi;
4728
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004729 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4730 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004731}
4732
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004733static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004734{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004735 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004736}
4737
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004738static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004739{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004740 rtl_csi_access_enable(tp, 0x27000000);
4741}
4742
Francois Romieuffc46952012-07-06 14:19:23 +02004743DECLARE_RTL_COND(rtl_csiar_cond)
4744{
4745 void __iomem *ioaddr = tp->mmio_addr;
4746
4747 return RTL_R32(CSIAR) & CSIAR_FLAG;
4748}
4749
Francois Romieu52989f02012-07-06 13:37:00 +02004750static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004751{
Francois Romieu52989f02012-07-06 13:37:00 +02004752 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004753
4754 RTL_W32(CSIDR, value);
4755 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4756 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4757
Francois Romieuffc46952012-07-06 14:19:23 +02004758 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004759}
4760
Francois Romieu52989f02012-07-06 13:37:00 +02004761static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004762{
Francois Romieu52989f02012-07-06 13:37:00 +02004763 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004764
4765 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4766 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4767
Francois Romieuffc46952012-07-06 14:19:23 +02004768 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4769 RTL_R32(CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004770}
4771
Francois Romieu52989f02012-07-06 13:37:00 +02004772static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004773{
Francois Romieu52989f02012-07-06 13:37:00 +02004774 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004775
4776 RTL_W32(CSIDR, value);
4777 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4778 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4779 CSIAR_FUNC_NIC);
4780
Francois Romieuffc46952012-07-06 14:19:23 +02004781 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004782}
4783
Francois Romieu52989f02012-07-06 13:37:00 +02004784static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004785{
Francois Romieu52989f02012-07-06 13:37:00 +02004786 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004787
4788 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4789 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4790
Francois Romieuffc46952012-07-06 14:19:23 +02004791 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4792 RTL_R32(CSIDR) : ~0;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004793}
4794
hayeswang45dd95c2013-07-08 17:09:01 +08004795static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
4796{
4797 void __iomem *ioaddr = tp->mmio_addr;
4798
4799 RTL_W32(CSIDR, value);
4800 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4801 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4802 CSIAR_FUNC_NIC2);
4803
4804 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4805}
4806
4807static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
4808{
4809 void __iomem *ioaddr = tp->mmio_addr;
4810
4811 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
4812 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4813
4814 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4815 RTL_R32(CSIDR) : ~0;
4816}
4817
Bill Pembertonbaf63292012-12-03 09:23:28 -05004818static void rtl_init_csi_ops(struct rtl8169_private *tp)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004819{
4820 struct csi_ops *ops = &tp->csi_ops;
4821
4822 switch (tp->mac_version) {
4823 case RTL_GIGA_MAC_VER_01:
4824 case RTL_GIGA_MAC_VER_02:
4825 case RTL_GIGA_MAC_VER_03:
4826 case RTL_GIGA_MAC_VER_04:
4827 case RTL_GIGA_MAC_VER_05:
4828 case RTL_GIGA_MAC_VER_06:
4829 case RTL_GIGA_MAC_VER_10:
4830 case RTL_GIGA_MAC_VER_11:
4831 case RTL_GIGA_MAC_VER_12:
4832 case RTL_GIGA_MAC_VER_13:
4833 case RTL_GIGA_MAC_VER_14:
4834 case RTL_GIGA_MAC_VER_15:
4835 case RTL_GIGA_MAC_VER_16:
4836 case RTL_GIGA_MAC_VER_17:
4837 ops->write = NULL;
4838 ops->read = NULL;
4839 break;
4840
Hayes Wang7e18dca2012-03-30 14:33:02 +08004841 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004842 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004843 ops->write = r8402_csi_write;
4844 ops->read = r8402_csi_read;
4845 break;
4846
hayeswang45dd95c2013-07-08 17:09:01 +08004847 case RTL_GIGA_MAC_VER_44:
4848 ops->write = r8411_csi_write;
4849 ops->read = r8411_csi_read;
4850 break;
4851
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004852 default:
4853 ops->write = r8169_csi_write;
4854 ops->read = r8169_csi_read;
4855 break;
4856 }
Francois Romieudacf8152008-08-02 20:44:13 +02004857}
4858
4859struct ephy_info {
4860 unsigned int offset;
4861 u16 mask;
4862 u16 bits;
4863};
4864
Francois Romieufdf6fc02012-07-06 22:40:38 +02004865static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4866 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004867{
4868 u16 w;
4869
4870 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004871 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4872 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004873 e++;
4874 }
4875}
4876
Francois Romieub726e492008-06-28 12:22:59 +02004877static void rtl_disable_clock_request(struct pci_dev *pdev)
4878{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004879 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
4880 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004881}
4882
françois romieue6de30d2011-01-03 15:08:37 +00004883static void rtl_enable_clock_request(struct pci_dev *pdev)
4884{
Jiang Liu7d7903b2012-07-24 17:20:16 +08004885 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
4886 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004887}
4888
hayeswangb51ecea2014-07-09 14:52:51 +08004889static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4890{
4891 void __iomem *ioaddr = tp->mmio_addr;
4892 u8 data;
4893
4894 data = RTL_R8(Config3);
4895
4896 if (enable)
4897 data |= Rdy_to_L23;
4898 else
4899 data &= ~Rdy_to_L23;
4900
4901 RTL_W8(Config3, data);
4902}
4903
Francois Romieub726e492008-06-28 12:22:59 +02004904#define R8168_CPCMD_QUIRK_MASK (\
4905 EnableBist | \
4906 Mac_dbgo_oe | \
4907 Force_half_dup | \
4908 Force_rxflow_en | \
4909 Force_txflow_en | \
4910 Cxpl_dbg_sel | \
4911 ASF | \
4912 PktCntrDisable | \
4913 Mac_dbgo_sel)
4914
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004915static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004916{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004917 void __iomem *ioaddr = tp->mmio_addr;
4918 struct pci_dev *pdev = tp->pci_dev;
4919
Francois Romieub726e492008-06-28 12:22:59 +02004920 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4921
4922 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4923
françois romieufaf1e782013-02-27 13:01:57 +00004924 if (tp->dev->mtu <= ETH_DATA_LEN) {
4925 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
4926 PCI_EXP_DEVCTL_NOSNOOP_EN);
4927 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004928}
4929
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004930static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004931{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004932 void __iomem *ioaddr = tp->mmio_addr;
4933
4934 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004935
françois romieuf0298f82011-01-03 15:07:42 +00004936 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004937
4938 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004939}
4940
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004941static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004942{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004943 void __iomem *ioaddr = tp->mmio_addr;
4944 struct pci_dev *pdev = tp->pci_dev;
4945
Francois Romieub726e492008-06-28 12:22:59 +02004946 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4947
4948 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4949
françois romieufaf1e782013-02-27 13:01:57 +00004950 if (tp->dev->mtu <= ETH_DATA_LEN)
4951 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004952
4953 rtl_disable_clock_request(pdev);
4954
4955 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004956}
4957
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004958static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004959{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004960 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004961 { 0x01, 0, 0x0001 },
4962 { 0x02, 0x0800, 0x1000 },
4963 { 0x03, 0, 0x0042 },
4964 { 0x06, 0x0080, 0x0000 },
4965 { 0x07, 0, 0x2000 }
4966 };
4967
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004968 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004969
Francois Romieufdf6fc02012-07-06 22:40:38 +02004970 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004971
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004972 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004973}
4974
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004975static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004976{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004977 void __iomem *ioaddr = tp->mmio_addr;
4978 struct pci_dev *pdev = tp->pci_dev;
4979
4980 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004981
4982 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4983
françois romieufaf1e782013-02-27 13:01:57 +00004984 if (tp->dev->mtu <= ETH_DATA_LEN)
4985 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieuef3386f2008-06-29 12:24:30 +02004986
4987 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4988}
4989
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004990static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004991{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004992 void __iomem *ioaddr = tp->mmio_addr;
4993 struct pci_dev *pdev = tp->pci_dev;
4994
4995 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004996
4997 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4998
4999 /* Magic. */
5000 RTL_W8(DBG_REG, 0x20);
5001
françois romieuf0298f82011-01-03 15:07:42 +00005002 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005003
françois romieufaf1e782013-02-27 13:01:57 +00005004 if (tp->dev->mtu <= ETH_DATA_LEN)
5005 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005006
5007 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5008}
5009
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005010static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005011{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005012 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005013 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005014 { 0x02, 0x0800, 0x1000 },
5015 { 0x03, 0, 0x0002 },
5016 { 0x06, 0x0080, 0x0000 }
5017 };
5018
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005019 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005020
5021 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5022
Francois Romieufdf6fc02012-07-06 22:40:38 +02005023 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02005024
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005025 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005026}
5027
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005028static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02005029{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005030 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02005031 { 0x01, 0, 0x0001 },
5032 { 0x03, 0x0400, 0x0220 }
5033 };
5034
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005035 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02005036
Francois Romieufdf6fc02012-07-06 22:40:38 +02005037 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02005038
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005039 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02005040}
5041
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005042static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02005043{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005044 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02005045}
5046
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005047static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02005048{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005049 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005050
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005051 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02005052}
5053
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005054static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02005055{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005056 void __iomem *ioaddr = tp->mmio_addr;
5057 struct pci_dev *pdev = tp->pci_dev;
5058
5059 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02005060
5061 rtl_disable_clock_request(pdev);
5062
françois romieuf0298f82011-01-03 15:07:42 +00005063 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02005064
françois romieufaf1e782013-02-27 13:01:57 +00005065 if (tp->dev->mtu <= ETH_DATA_LEN)
5066 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu5b538df2008-07-20 16:22:45 +02005067
5068 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5069}
5070
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005071static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00005072{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005073 void __iomem *ioaddr = tp->mmio_addr;
5074 struct pci_dev *pdev = tp->pci_dev;
5075
5076 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005077
françois romieufaf1e782013-02-27 13:01:57 +00005078 if (tp->dev->mtu <= ETH_DATA_LEN)
5079 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang4804b3b2011-03-21 01:50:29 +00005080
5081 RTL_W8(MaxTxPacketSize, TxPacketMax);
5082
5083 rtl_disable_clock_request(pdev);
5084}
5085
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005086static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00005087{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005088 void __iomem *ioaddr = tp->mmio_addr;
5089 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00005090 static const struct ephy_info e_info_8168d_4[] = {
5091 { 0x0b, ~0, 0x48 },
5092 { 0x19, 0x20, 0x50 },
5093 { 0x0c, ~0, 0x20 }
5094 };
5095 int i;
5096
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005097 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00005098
5099 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5100
5101 RTL_W8(MaxTxPacketSize, TxPacketMax);
5102
5103 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
5104 const struct ephy_info *e = e_info_8168d_4 + i;
5105 u16 w;
5106
Francois Romieufdf6fc02012-07-06 22:40:38 +02005107 w = rtl_ephy_read(tp, e->offset);
5108 rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
françois romieue6de30d2011-01-03 15:08:37 +00005109 }
5110
5111 rtl_enable_clock_request(pdev);
5112}
5113
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005114static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00005115{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005116 void __iomem *ioaddr = tp->mmio_addr;
5117 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005118 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00005119 { 0x00, 0x0200, 0x0100 },
5120 { 0x00, 0x0000, 0x0004 },
5121 { 0x06, 0x0002, 0x0001 },
5122 { 0x06, 0x0000, 0x0030 },
5123 { 0x07, 0x0000, 0x2000 },
5124 { 0x00, 0x0000, 0x0020 },
5125 { 0x03, 0x5800, 0x2000 },
5126 { 0x03, 0x0000, 0x0001 },
5127 { 0x01, 0x0800, 0x1000 },
5128 { 0x07, 0x0000, 0x4000 },
5129 { 0x1e, 0x0000, 0x2000 },
5130 { 0x19, 0xffff, 0xfe6c },
5131 { 0x0a, 0x0000, 0x0040 }
5132 };
5133
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005134 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005135
Francois Romieufdf6fc02012-07-06 22:40:38 +02005136 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00005137
françois romieufaf1e782013-02-27 13:01:57 +00005138 if (tp->dev->mtu <= ETH_DATA_LEN)
5139 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
hayeswang01dc7fe2011-03-21 01:50:28 +00005140
5141 RTL_W8(MaxTxPacketSize, TxPacketMax);
5142
5143 rtl_disable_clock_request(pdev);
5144
5145 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02005146 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
5147 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005148
Francois Romieucecb5fd2011-04-01 10:21:07 +02005149 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005150}
5151
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005152static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005153{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005154 void __iomem *ioaddr = tp->mmio_addr;
5155 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08005156 static const struct ephy_info e_info_8168e_2[] = {
5157 { 0x09, 0x0000, 0x0080 },
5158 { 0x19, 0x0000, 0x0224 }
5159 };
5160
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005161 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005162
Francois Romieufdf6fc02012-07-06 22:40:38 +02005163 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005164
françois romieufaf1e782013-02-27 13:01:57 +00005165 if (tp->dev->mtu <= ETH_DATA_LEN)
5166 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Hayes Wang70090422011-07-06 15:58:06 +08005167
Francois Romieufdf6fc02012-07-06 22:40:38 +02005168 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5169 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5170 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5171 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5172 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5173 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5174 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5175 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005176
Hayes Wang3090bd92011-09-06 16:55:15 +08005177 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005178
Francois Romieu4521e1a92012-11-01 16:46:28 +00005179 rtl_disable_clock_request(pdev);
5180
Hayes Wang70090422011-07-06 15:58:06 +08005181 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5182 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5183
5184 /* Adjust EEE LED frequency */
5185 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5186
5187 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5188 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005189 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wang70090422011-07-06 15:58:06 +08005190}
5191
Hayes Wang5f886e02012-03-30 14:33:03 +08005192static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005193{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005194 void __iomem *ioaddr = tp->mmio_addr;
5195 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08005196
Hayes Wang5f886e02012-03-30 14:33:03 +08005197 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005198
5199 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5200
Francois Romieufdf6fc02012-07-06 22:40:38 +02005201 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5202 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5203 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5204 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5205 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5206 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5207 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5208 rtl_w1w0_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5209 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5210 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005211
5212 RTL_W8(MaxTxPacketSize, EarlySize);
5213
Francois Romieu4521e1a92012-11-01 16:46:28 +00005214 rtl_disable_clock_request(pdev);
5215
Hayes Wangc2218922011-09-06 16:55:18 +08005216 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5217 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08005218 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005219 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
5220 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005221}
5222
Hayes Wang5f886e02012-03-30 14:33:03 +08005223static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5224{
5225 void __iomem *ioaddr = tp->mmio_addr;
5226 static const struct ephy_info e_info_8168f_1[] = {
5227 { 0x06, 0x00c0, 0x0020 },
5228 { 0x08, 0x0001, 0x0002 },
5229 { 0x09, 0x0000, 0x0080 },
5230 { 0x19, 0x0000, 0x0224 }
5231 };
5232
5233 rtl_hw_start_8168f(tp);
5234
Francois Romieufdf6fc02012-07-06 22:40:38 +02005235 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005236
Francois Romieufdf6fc02012-07-06 22:40:38 +02005237 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005238
5239 /* Adjust EEE LED frequency */
5240 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5241}
5242
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005243static void rtl_hw_start_8411(struct rtl8169_private *tp)
5244{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005245 static const struct ephy_info e_info_8168f_1[] = {
5246 { 0x06, 0x00c0, 0x0020 },
5247 { 0x0f, 0xffff, 0x5200 },
5248 { 0x1e, 0x0000, 0x4000 },
5249 { 0x19, 0x0000, 0x0224 }
5250 };
5251
5252 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005253 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005254
Francois Romieufdf6fc02012-07-06 22:40:38 +02005255 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005256
Francois Romieufdf6fc02012-07-06 22:40:38 +02005257 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005258}
5259
Hayes Wangc5583862012-07-02 17:23:22 +08005260static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5261{
5262 void __iomem *ioaddr = tp->mmio_addr;
5263 struct pci_dev *pdev = tp->pci_dev;
5264
hayeswangbeb330a2013-04-01 22:23:39 +00005265 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5266
Hayes Wangc5583862012-07-02 17:23:22 +08005267 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5268 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5269 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5270 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5271
5272 rtl_csi_access_enable_1(tp);
5273
5274 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5275
5276 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5277 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005278 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005279
5280 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005281 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08005282 RTL_W8(MaxTxPacketSize, EarlySize);
5283
5284 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5285 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5286
5287 /* Adjust EEE LED frequency */
5288 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
5289
hayeswangbeb330a2013-04-01 22:23:39 +00005290 rtl_w1w0_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5291 rtl_w1w0_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005292
5293 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005294}
5295
hayeswang57538c42013-04-01 22:23:40 +00005296static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5297{
5298 void __iomem *ioaddr = tp->mmio_addr;
5299 static const struct ephy_info e_info_8168g_2[] = {
5300 { 0x00, 0x0000, 0x0008 },
5301 { 0x0c, 0x3df0, 0x0200 },
5302 { 0x19, 0xffff, 0xfc00 },
5303 { 0x1e, 0xffff, 0x20eb }
5304 };
5305
5306 rtl_hw_start_8168g_1(tp);
5307
5308 /* disable aspm and clock request before access ephy */
5309 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5310 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5311 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5312}
5313
hayeswang45dd95c2013-07-08 17:09:01 +08005314static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5315{
5316 void __iomem *ioaddr = tp->mmio_addr;
5317 static const struct ephy_info e_info_8411_2[] = {
5318 { 0x00, 0x0000, 0x0008 },
5319 { 0x0c, 0x3df0, 0x0200 },
5320 { 0x0f, 0xffff, 0x5200 },
5321 { 0x19, 0x0020, 0x0000 },
5322 { 0x1e, 0x0000, 0x2000 }
5323 };
5324
5325 rtl_hw_start_8168g_1(tp);
5326
5327 /* disable aspm and clock request before access ephy */
5328 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
5329 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
5330 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5331}
5332
Francois Romieu07ce4062007-02-23 23:36:39 +01005333static void rtl_hw_start_8168(struct net_device *dev)
5334{
Francois Romieu2dd99532007-06-11 23:22:52 +02005335 struct rtl8169_private *tp = netdev_priv(dev);
5336 void __iomem *ioaddr = tp->mmio_addr;
5337
5338 RTL_W8(Cfg9346, Cfg9346_Unlock);
5339
françois romieuf0298f82011-01-03 15:07:42 +00005340 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005341
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005342 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02005343
Francois Romieu0e485152007-02-20 00:00:26 +01005344 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02005345
5346 RTL_W16(CPlusCmd, tp->cp_cmd);
5347
Francois Romieu0e485152007-02-20 00:00:26 +01005348 RTL_W16(IntrMitigate, 0x5151);
5349
5350 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005351 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005352 tp->event_slow |= RxFIFOOver | PCSTimeout;
5353 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005354 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005355
5356 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5357
hayeswang1a964642013-04-01 22:23:41 +00005358 rtl_set_rx_tx_config_registers(tp);
Francois Romieu2dd99532007-06-11 23:22:52 +02005359
5360 RTL_R8(IntrMask);
5361
Francois Romieu219a1e92008-06-28 11:58:39 +02005362 switch (tp->mac_version) {
5363 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005364 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005365 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005366
5367 case RTL_GIGA_MAC_VER_12:
5368 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005369 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005370 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005371
5372 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005373 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005374 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005375
5376 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005377 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005378 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005379
5380 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005381 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005382 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005383
Francois Romieu197ff762008-06-28 13:16:02 +02005384 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005385 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005386 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005387
Francois Romieu6fb07052008-06-29 11:54:28 +02005388 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005389 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005390 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005391
Francois Romieuef3386f2008-06-29 12:24:30 +02005392 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005393 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005394 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005395
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005396 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005397 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005398 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005399
Francois Romieu5b538df2008-07-20 16:22:45 +02005400 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005401 case RTL_GIGA_MAC_VER_26:
5402 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005403 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005404 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005405
françois romieue6de30d2011-01-03 15:08:37 +00005406 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005407 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005408 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005409
hayeswang4804b3b2011-03-21 01:50:29 +00005410 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005411 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005412 break;
5413
hayeswang01dc7fe2011-03-21 01:50:28 +00005414 case RTL_GIGA_MAC_VER_32:
5415 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005416 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005417 break;
5418 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005419 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005420 break;
françois romieue6de30d2011-01-03 15:08:37 +00005421
Hayes Wangc2218922011-09-06 16:55:18 +08005422 case RTL_GIGA_MAC_VER_35:
5423 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005424 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005425 break;
5426
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005427 case RTL_GIGA_MAC_VER_38:
5428 rtl_hw_start_8411(tp);
5429 break;
5430
Hayes Wangc5583862012-07-02 17:23:22 +08005431 case RTL_GIGA_MAC_VER_40:
5432 case RTL_GIGA_MAC_VER_41:
5433 rtl_hw_start_8168g_1(tp);
5434 break;
hayeswang57538c42013-04-01 22:23:40 +00005435 case RTL_GIGA_MAC_VER_42:
5436 rtl_hw_start_8168g_2(tp);
5437 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005438
hayeswang45dd95c2013-07-08 17:09:01 +08005439 case RTL_GIGA_MAC_VER_44:
5440 rtl_hw_start_8411_2(tp);
5441 break;
5442
Francois Romieu219a1e92008-06-28 11:58:39 +02005443 default:
5444 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5445 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005446 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005447 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005448
hayeswang1a964642013-04-01 22:23:41 +00005449 RTL_W8(Cfg9346, Cfg9346_Lock);
5450
Francois Romieu0e485152007-02-20 00:00:26 +01005451 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5452
hayeswang1a964642013-04-01 22:23:41 +00005453 rtl_set_rx_mode(dev);
Francois Romieub8363902008-06-01 12:31:57 +02005454
Francois Romieu2dd99532007-06-11 23:22:52 +02005455 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Francois Romieu2857ffb2008-08-02 21:08:49 +02005458#define R810X_CPCMD_QUIRK_MASK (\
5459 EnableBist | \
5460 Mac_dbgo_oe | \
5461 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005462 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005463 Force_txflow_en | \
5464 Cxpl_dbg_sel | \
5465 ASF | \
5466 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005467 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005468
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005469static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005470{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005471 void __iomem *ioaddr = tp->mmio_addr;
5472 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005473 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005474 { 0x01, 0, 0x6e65 },
5475 { 0x02, 0, 0x091f },
5476 { 0x03, 0, 0xc2f9 },
5477 { 0x06, 0, 0xafb5 },
5478 { 0x07, 0, 0x0e00 },
5479 { 0x19, 0, 0xec80 },
5480 { 0x01, 0, 0x2e65 },
5481 { 0x01, 0, 0x6e65 }
5482 };
5483 u8 cfg1;
5484
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005485 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005486
5487 RTL_W8(DBG_REG, FIX_NAK_1);
5488
5489 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5490
5491 RTL_W8(Config1,
5492 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5493 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5494
5495 cfg1 = RTL_R8(Config1);
5496 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5497 RTL_W8(Config1, cfg1 & ~LEDS0);
5498
Francois Romieufdf6fc02012-07-06 22:40:38 +02005499 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005500}
5501
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005502static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005503{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005504 void __iomem *ioaddr = tp->mmio_addr;
5505 struct pci_dev *pdev = tp->pci_dev;
5506
5507 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005508
5509 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5510
5511 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5512 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005513}
5514
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005515static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005516{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005517 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005518
Francois Romieufdf6fc02012-07-06 22:40:38 +02005519 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005520}
5521
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005522static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005523{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005524 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005525 static const struct ephy_info e_info_8105e_1[] = {
5526 { 0x07, 0, 0x4000 },
5527 { 0x19, 0, 0x0200 },
5528 { 0x19, 0, 0x0020 },
5529 { 0x1e, 0, 0x2000 },
5530 { 0x03, 0, 0x0001 },
5531 { 0x19, 0, 0x0100 },
5532 { 0x19, 0, 0x0004 },
5533 { 0x0a, 0, 0x0020 }
5534 };
5535
Francois Romieucecb5fd2011-04-01 10:21:07 +02005536 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005537 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5538
Francois Romieucecb5fd2011-04-01 10:21:07 +02005539 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005540 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5541
5542 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005543 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005544
Francois Romieufdf6fc02012-07-06 22:40:38 +02005545 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005546
5547 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005548}
5549
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005550static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005551{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005552 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005553 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005554}
5555
Hayes Wang7e18dca2012-03-30 14:33:02 +08005556static void rtl_hw_start_8402(struct rtl8169_private *tp)
5557{
5558 void __iomem *ioaddr = tp->mmio_addr;
5559 static const struct ephy_info e_info_8402[] = {
5560 { 0x19, 0xffff, 0xff64 },
5561 { 0x1e, 0, 0x4000 }
5562 };
5563
5564 rtl_csi_access_enable_2(tp);
5565
5566 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5567 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5568
5569 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5570 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5571
Francois Romieufdf6fc02012-07-06 22:40:38 +02005572 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005573
5574 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5575
Francois Romieufdf6fc02012-07-06 22:40:38 +02005576 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5577 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5578 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5579 rtl_w1w0_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5580 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5581 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5582 rtl_w1w0_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005583
5584 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005585}
5586
Hayes Wang5598bfe2012-07-02 17:23:21 +08005587static void rtl_hw_start_8106(struct rtl8169_private *tp)
5588{
5589 void __iomem *ioaddr = tp->mmio_addr;
5590
5591 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5592 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5593
Francois Romieu4521e1a92012-11-01 16:46:28 +00005594 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005595 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
5596 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005597
5598 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005599}
5600
Francois Romieu07ce4062007-02-23 23:36:39 +01005601static void rtl_hw_start_8101(struct net_device *dev)
5602{
Francois Romieucdf1a602007-06-11 23:29:50 +02005603 struct rtl8169_private *tp = netdev_priv(dev);
5604 void __iomem *ioaddr = tp->mmio_addr;
5605 struct pci_dev *pdev = tp->pci_dev;
5606
Francois Romieuda78dbf2012-01-26 14:18:23 +01005607 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5608 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005609
Francois Romieucecb5fd2011-04-01 10:21:07 +02005610 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005611 tp->mac_version == RTL_GIGA_MAC_VER_16)
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005612 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
5613 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005614
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005615 RTL_W8(Cfg9346, Cfg9346_Unlock);
5616
hayeswang1a964642013-04-01 22:23:41 +00005617 RTL_W8(MaxTxPacketSize, TxPacketMax);
5618
5619 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
5620
5621 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
5622 RTL_W16(CPlusCmd, tp->cp_cmd);
5623
5624 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5625
5626 rtl_set_rx_tx_config_registers(tp);
5627
Francois Romieu2857ffb2008-08-02 21:08:49 +02005628 switch (tp->mac_version) {
5629 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005630 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005631 break;
5632
5633 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005634 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005635 break;
5636
5637 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005638 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005639 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005640
5641 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005642 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005643 break;
5644 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005645 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005646 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005647
5648 case RTL_GIGA_MAC_VER_37:
5649 rtl_hw_start_8402(tp);
5650 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005651
5652 case RTL_GIGA_MAC_VER_39:
5653 rtl_hw_start_8106(tp);
5654 break;
hayeswang58152cd2013-04-01 22:23:42 +00005655 case RTL_GIGA_MAC_VER_43:
5656 rtl_hw_start_8168g_2(tp);
5657 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005658 }
5659
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005660 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005661
Francois Romieucdf1a602007-06-11 23:29:50 +02005662 RTL_W16(IntrMitigate, 0x0000);
5663
Francois Romieucdf1a602007-06-11 23:29:50 +02005664 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
Francois Romieucdf1a602007-06-11 23:29:50 +02005665
Francois Romieucdf1a602007-06-11 23:29:50 +02005666 rtl_set_rx_mode(dev);
5667
hayeswang1a964642013-04-01 22:23:41 +00005668 RTL_R8(IntrMask);
5669
Francois Romieucdf1a602007-06-11 23:29:50 +02005670 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671}
5672
5673static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5674{
Francois Romieud58d46b2011-05-03 16:38:29 +02005675 struct rtl8169_private *tp = netdev_priv(dev);
5676
5677 if (new_mtu < ETH_ZLEN ||
5678 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 return -EINVAL;
5680
Francois Romieud58d46b2011-05-03 16:38:29 +02005681 if (new_mtu > ETH_DATA_LEN)
5682 rtl_hw_jumbo_enable(tp);
5683 else
5684 rtl_hw_jumbo_disable(tp);
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005687 netdev_update_features(dev);
5688
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
5691
5692static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5693{
Al Viro95e09182007-12-22 18:55:39 +00005694 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5696}
5697
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005698static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5699 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005701 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005702 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005703
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005704 kfree(*data_buff);
5705 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 rtl8169_make_unusable_by_asic(desc);
5707}
5708
5709static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5710{
5711 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5712
5713 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5714}
5715
5716static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5717 u32 rx_buf_sz)
5718{
5719 desc->addr = cpu_to_le64(mapping);
5720 wmb();
5721 rtl8169_mark_to_asic(desc, rx_buf_sz);
5722}
5723
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005724static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005726 return (void *)ALIGN((long)data, 16);
5727}
5728
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005729static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5730 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005731{
5732 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005734 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005735 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005736 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005738 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5739 if (!data)
5740 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005741
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005742 if (rtl8169_align(data) != data) {
5743 kfree(data);
5744 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5745 if (!data)
5746 return NULL;
5747 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005748
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005749 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005750 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005751 if (unlikely(dma_mapping_error(d, mapping))) {
5752 if (net_ratelimit())
5753 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005754 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005758 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005759
5760err_out:
5761 kfree(data);
5762 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763}
5764
5765static void rtl8169_rx_clear(struct rtl8169_private *tp)
5766{
Francois Romieu07d3f512007-02-21 22:40:46 +01005767 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005770 if (tp->Rx_databuff[i]) {
5771 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 tp->RxDescArray + i);
5773 }
5774 }
5775}
5776
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005777static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005779 desc->opts1 |= cpu_to_le32(RingEnd);
5780}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005781
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005782static int rtl8169_rx_fill(struct rtl8169_private *tp)
5783{
5784 unsigned int i;
5785
5786 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005787 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005788
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005789 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005791
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005792 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005793 if (!data) {
5794 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005795 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005796 }
5797 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005800 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5801 return 0;
5802
5803err_out:
5804 rtl8169_rx_clear(tp);
5805 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806}
5807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808static int rtl8169_init_ring(struct net_device *dev)
5809{
5810 struct rtl8169_private *tp = netdev_priv(dev);
5811
5812 rtl8169_init_ring_indexes(tp);
5813
5814 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005815 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005817 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818}
5819
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005820static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 struct TxDesc *desc)
5822{
5823 unsigned int len = tx_skb->len;
5824
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005825 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 desc->opts1 = 0x00;
5828 desc->opts2 = 0x00;
5829 desc->addr = 0x00;
5830 tx_skb->len = 0;
5831}
5832
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005833static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5834 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835{
5836 unsigned int i;
5837
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005838 for (i = 0; i < n; i++) {
5839 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 struct ring_info *tx_skb = tp->tx_skb + entry;
5841 unsigned int len = tx_skb->len;
5842
5843 if (len) {
5844 struct sk_buff *skb = tx_skb->skb;
5845
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005846 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 tp->TxDescArray + entry);
5848 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005849 tp->dev->stats.tx_dropped++;
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005850 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 tx_skb->skb = NULL;
5852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 }
5854 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005855}
5856
5857static void rtl8169_tx_clear(struct rtl8169_private *tp)
5858{
5859 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 tp->cur_tx = tp->dirty_tx = 0;
5861}
5862
Francois Romieu4422bcd2012-01-26 11:23:32 +01005863static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864{
David Howellsc4028952006-11-22 14:57:56 +00005865 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005866 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Francois Romieuda78dbf2012-01-26 14:18:23 +01005868 napi_disable(&tp->napi);
5869 netif_stop_queue(dev);
5870 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
françois romieuc7c2c392011-12-04 20:30:52 +00005872 rtl8169_hw_reset(tp);
5873
Francois Romieu56de4142011-03-15 17:29:31 +01005874 for (i = 0; i < NUM_RX_DESC; i++)
5875 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5876
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005878 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Francois Romieuda78dbf2012-01-26 14:18:23 +01005880 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005881 rtl_hw_start(dev);
5882 netif_wake_queue(dev);
5883 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
5885
5886static void rtl8169_tx_timeout(struct net_device *dev)
5887{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005888 struct rtl8169_private *tp = netdev_priv(dev);
5889
5890 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891}
5892
5893static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005894 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895{
5896 struct skb_shared_info *info = skb_shinfo(skb);
5897 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005898 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005899 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
5901 entry = tp->cur_tx;
5902 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005903 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 dma_addr_t mapping;
5905 u32 status, len;
5906 void *addr;
5907
5908 entry = (entry + 1) % NUM_TX_DESC;
5909
5910 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005911 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005912 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005913 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005914 if (unlikely(dma_mapping_error(d, mapping))) {
5915 if (net_ratelimit())
5916 netif_err(tp, drv, tp->dev,
5917 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005918 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
Francois Romieucecb5fd2011-04-01 10:21:07 +02005921 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005922 status = opts[0] | len |
5923 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
5925 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005926 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 txd->addr = cpu_to_le64(mapping);
5928
5929 tp->tx_skb[entry].len = len;
5930 }
5931
5932 if (cur_frag) {
5933 tp->tx_skb[entry].skb = skb;
5934 txd->opts1 |= cpu_to_le32(LastFrag);
5935 }
5936
5937 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005938
5939err_out:
5940 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5941 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942}
5943
françois romieub423e9a2013-05-18 01:24:46 +00005944static bool rtl_skb_pad(struct sk_buff *skb)
5945{
5946 if (skb_padto(skb, ETH_ZLEN))
5947 return false;
5948 skb_put(skb, ETH_ZLEN - skb->len);
5949 return true;
5950}
5951
5952static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5953{
5954 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5955}
5956
hayeswange9746042014-07-11 16:25:58 +08005957static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5958 struct net_device *dev);
5959/* r8169_csum_workaround()
5960 * The hw limites the value the transport offset. When the offset is out of the
5961 * range, calculate the checksum by sw.
5962 */
5963static void r8169_csum_workaround(struct rtl8169_private *tp,
5964 struct sk_buff *skb)
5965{
5966 if (skb_shinfo(skb)->gso_size) {
5967 netdev_features_t features = tp->dev->features;
5968 struct sk_buff *segs, *nskb;
5969
5970 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5971 segs = skb_gso_segment(skb, features);
5972 if (IS_ERR(segs) || !segs)
5973 goto drop;
5974
5975 do {
5976 nskb = segs;
5977 segs = segs->next;
5978 nskb->next = NULL;
5979 rtl8169_start_xmit(nskb, tp->dev);
5980 } while (segs);
5981
5982 dev_kfree_skb(skb);
5983 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5984 if (skb_checksum_help(skb) < 0)
5985 goto drop;
5986
5987 rtl8169_start_xmit(skb, tp->dev);
5988 } else {
5989 struct net_device_stats *stats;
5990
5991drop:
5992 stats = &tp->dev->stats;
5993 stats->tx_dropped++;
5994 dev_kfree_skb(skb);
5995 }
5996}
5997
5998/* msdn_giant_send_check()
5999 * According to the document of microsoft, the TCP Pseudo Header excludes the
6000 * packet length for IPv6 TCP large packets.
6001 */
6002static int msdn_giant_send_check(struct sk_buff *skb)
6003{
6004 const struct ipv6hdr *ipv6h;
6005 struct tcphdr *th;
6006 int ret;
6007
6008 ret = skb_cow_head(skb, 0);
6009 if (ret)
6010 return ret;
6011
6012 ipv6h = ipv6_hdr(skb);
6013 th = tcp_hdr(skb);
6014
6015 th->check = 0;
6016 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6017
6018 return ret;
6019}
6020
6021static inline __be16 get_protocol(struct sk_buff *skb)
6022{
6023 __be16 protocol;
6024
6025 if (skb->protocol == htons(ETH_P_8021Q))
6026 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
6027 else
6028 protocol = skb->protocol;
6029
6030 return protocol;
6031}
6032
hayeswang5888d3f2014-07-11 16:25:56 +08006033static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6034 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035{
Michał Mirosław350fb322011-04-08 06:35:56 +00006036 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Francois Romieu2b7b4312011-04-18 22:53:24 -07006038 if (mss) {
6039 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006040 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6041 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6042 const struct iphdr *ip = ip_hdr(skb);
6043
6044 if (ip->protocol == IPPROTO_TCP)
6045 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6046 else if (ip->protocol == IPPROTO_UDP)
6047 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6048 else
6049 WARN_ON_ONCE(1);
6050 }
6051
6052 return true;
6053}
6054
6055static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6056 struct sk_buff *skb, u32 *opts)
6057{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006058 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006059 u32 mss = skb_shinfo(skb)->gso_size;
6060
6061 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006062 if (transport_offset > GTTCPHO_MAX) {
6063 netif_warn(tp, tx_err, tp->dev,
6064 "Invalid transport offset 0x%x for TSO\n",
6065 transport_offset);
6066 return false;
6067 }
6068
6069 switch (get_protocol(skb)) {
6070 case htons(ETH_P_IP):
6071 opts[0] |= TD1_GTSENV4;
6072 break;
6073
6074 case htons(ETH_P_IPV6):
6075 if (msdn_giant_send_check(skb))
6076 return false;
6077
6078 opts[0] |= TD1_GTSENV6;
6079 break;
6080
6081 default:
6082 WARN_ON_ONCE(1);
6083 break;
6084 }
6085
hayeswangbdfa4ed2014-07-11 16:25:57 +08006086 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006087 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006088 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006089 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
françois romieub423e9a2013-05-18 01:24:46 +00006091 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6092 return skb_checksum_help(skb) == 0 && rtl_skb_pad(skb);
6093
hayeswange9746042014-07-11 16:25:58 +08006094 if (transport_offset > TCPHO_MAX) {
6095 netif_warn(tp, tx_err, tp->dev,
6096 "Invalid transport offset 0x%x\n",
6097 transport_offset);
6098 return false;
6099 }
6100
6101 switch (get_protocol(skb)) {
6102 case htons(ETH_P_IP):
6103 opts[1] |= TD1_IPv4_CS;
6104 ip_protocol = ip_hdr(skb)->protocol;
6105 break;
6106
6107 case htons(ETH_P_IPV6):
6108 opts[1] |= TD1_IPv6_CS;
6109 ip_protocol = ipv6_hdr(skb)->nexthdr;
6110 break;
6111
6112 default:
6113 ip_protocol = IPPROTO_RAW;
6114 break;
6115 }
6116
6117 if (ip_protocol == IPPROTO_TCP)
6118 opts[1] |= TD1_TCP_CS;
6119 else if (ip_protocol == IPPROTO_UDP)
6120 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006121 else
6122 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006123
6124 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006125 } else {
6126 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6127 return rtl_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 }
hayeswang5888d3f2014-07-11 16:25:56 +08006129
françois romieub423e9a2013-05-18 01:24:46 +00006130 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131}
6132
Stephen Hemminger613573252009-08-31 19:50:58 +00006133static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6134 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135{
6136 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006137 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 struct TxDesc *txd = tp->TxDescArray + entry;
6139 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006140 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 dma_addr_t mapping;
6142 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006143 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006144 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006145
Julien Ducourthial477206a2012-05-09 00:00:06 +02006146 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006147 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006148 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 }
6150
6151 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006152 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
françois romieub423e9a2013-05-18 01:24:46 +00006154 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6155 opts[0] = DescOwn;
6156
hayeswange9746042014-07-11 16:25:58 +08006157 if (!tp->tso_csum(tp, skb, opts)) {
6158 r8169_csum_workaround(tp, skb);
6159 return NETDEV_TX_OK;
6160 }
françois romieub423e9a2013-05-18 01:24:46 +00006161
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006162 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006163 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006164 if (unlikely(dma_mapping_error(d, mapping))) {
6165 if (net_ratelimit())
6166 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006167 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
6170 tp->tx_skb[entry].len = len;
6171 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
Francois Romieu2b7b4312011-04-18 22:53:24 -07006173 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006174 if (frags < 0)
6175 goto err_dma_1;
6176 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006177 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006178 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006179 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006180 tp->tx_skb[entry].skb = skb;
6181 }
6182
Francois Romieu2b7b4312011-04-18 22:53:24 -07006183 txd->opts2 = cpu_to_le32(opts[1]);
6184
Richard Cochran5047fb52012-03-10 07:29:42 +00006185 skb_tx_timestamp(skb);
6186
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 wmb();
6188
Francois Romieucecb5fd2011-04-01 10:21:07 +02006189 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006190 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 txd->opts1 = cpu_to_le32(status);
6192
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 tp->cur_tx += frags + 1;
6194
David Dillow4c020a92010-03-03 16:33:10 +00006195 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196
Francois Romieucecb5fd2011-04-01 10:21:07 +02006197 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
Francois Romieuda78dbf2012-01-26 14:18:23 +01006199 mmiowb();
6200
Julien Ducourthial477206a2012-05-09 00:00:06 +02006201 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006202 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6203 * not miss a ring update when it notices a stopped queue.
6204 */
6205 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006207 /* Sync with rtl_tx:
6208 * - publish queue status and cur_tx ring index (write barrier)
6209 * - refresh dirty_tx ring index (read barrier).
6210 * May the current thread have a pessimistic view of the ring
6211 * status and forget to wake up queue, a racing rtl_tx thread
6212 * can't.
6213 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006214 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006215 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 netif_wake_queue(dev);
6217 }
6218
Stephen Hemminger613573252009-08-31 19:50:58 +00006219 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006221err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006222 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006223err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006224 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006225 dev->stats.tx_dropped++;
6226 return NETDEV_TX_OK;
6227
6228err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006230 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006231 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232}
6233
6234static void rtl8169_pcierr_interrupt(struct net_device *dev)
6235{
6236 struct rtl8169_private *tp = netdev_priv(dev);
6237 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 u16 pci_status, pci_cmd;
6239
6240 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6241 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6242
Joe Perchesbf82c182010-02-09 11:49:50 +00006243 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6244 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 /*
6247 * The recovery sequence below admits a very elaborated explanation:
6248 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006249 * - I did not see what else could be done;
6250 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 *
6252 * Feel free to adjust to your needs.
6253 */
Francois Romieua27993f2006-12-18 00:04:19 +01006254 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006255 pci_cmd &= ~PCI_COMMAND_PARITY;
6256 else
6257 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6258
6259 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 pci_write_config_word(pdev, PCI_STATUS,
6262 pci_status & (PCI_STATUS_DETECTED_PARITY |
6263 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6264 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6265
6266 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006267 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00006268 void __iomem *ioaddr = tp->mmio_addr;
6269
Joe Perchesbf82c182010-02-09 11:49:50 +00006270 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 tp->cp_cmd &= ~PCIDAC;
6272 RTL_W16(CPlusCmd, tp->cp_cmd);
6273 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 }
6275
françois romieue6de30d2011-01-03 15:08:37 +00006276 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006277
Francois Romieu98ddf982012-01-31 10:47:34 +01006278 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279}
6280
Francois Romieuda78dbf2012-01-26 14:18:23 +01006281static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282{
6283 unsigned int dirty_tx, tx_left;
6284
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 dirty_tx = tp->dirty_tx;
6286 smp_rmb();
6287 tx_left = tp->cur_tx - dirty_tx;
6288
6289 while (tx_left > 0) {
6290 unsigned int entry = dirty_tx % NUM_TX_DESC;
6291 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 u32 status;
6293
6294 rmb();
6295 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6296 if (status & DescOwn)
6297 break;
6298
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006299 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
6300 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 if (status & LastFrag) {
Francois Romieu17bcb682012-07-23 22:55:55 +02006302 u64_stats_update_begin(&tp->tx_stats.syncp);
6303 tp->tx_stats.packets++;
6304 tp->tx_stats.bytes += tx_skb->skb->len;
6305 u64_stats_update_end(&tp->tx_stats.syncp);
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006306 dev_kfree_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 tx_skb->skb = NULL;
6308 }
6309 dirty_tx++;
6310 tx_left--;
6311 }
6312
6313 if (tp->dirty_tx != dirty_tx) {
6314 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006315 /* Sync with rtl8169_start_xmit:
6316 * - publish dirty_tx ring index (write barrier)
6317 * - refresh cur_tx ring index and queue status (read barrier)
6318 * May the current thread miss the stopped queue condition,
6319 * a racing xmit thread can only have a right view of the
6320 * ring status.
6321 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006322 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006324 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 netif_wake_queue(dev);
6326 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006327 /*
6328 * 8168 hack: TxPoll requests are lost when the Tx packets are
6329 * too close. Let's kick an extra TxPoll request when a burst
6330 * of start_xmit activity is detected (if it is not detected,
6331 * it is slow enough). -- FR
6332 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006333 if (tp->cur_tx != dirty_tx) {
6334 void __iomem *ioaddr = tp->mmio_addr;
6335
Francois Romieud78ae2d2007-08-26 20:08:19 +02006336 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 }
6339}
6340
Francois Romieu126fa4b2005-05-12 20:09:17 -04006341static inline int rtl8169_fragmented_frame(u32 status)
6342{
6343 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6344}
6345
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006346static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 u32 status = opts1 & RxProtoMask;
6349
6350 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006351 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 skb->ip_summed = CHECKSUM_UNNECESSARY;
6353 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006354 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355}
6356
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006357static struct sk_buff *rtl8169_try_rx_copy(void *data,
6358 struct rtl8169_private *tp,
6359 int pkt_size,
6360 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006362 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006363 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006365 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006366 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006367 prefetch(data);
6368 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
6369 if (skb)
6370 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006371 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6372
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006373 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374}
6375
Francois Romieuda78dbf2012-01-26 14:18:23 +01006376static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377{
6378 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006379 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
Timo Teräs9fba0812013-01-15 21:01:24 +00006383 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006385 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 u32 status;
6387
6388 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04006389 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
6391 if (status & DescOwn)
6392 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006393 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006394 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6395 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006396 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006398 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006400 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006401 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006402 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006403 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02006404 }
Ben Greear6bbe0212012-02-10 15:04:33 +00006405 if ((status & (RxRUNT | RxCRC)) &&
6406 !(status & (RxRWT | RxFOVF)) &&
6407 (dev->features & NETIF_F_RXALL))
6408 goto process_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006410 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006411 dma_addr_t addr;
6412 int pkt_size;
6413
6414process_pkt:
6415 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006416 if (likely(!(dev->features & NETIF_F_RXFCS)))
6417 pkt_size = (status & 0x00003fff) - 4;
6418 else
6419 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Francois Romieu126fa4b2005-05-12 20:09:17 -04006421 /*
6422 * The driver does not support incoming fragmented
6423 * frames. They are seen as a symptom of over-mtu
6424 * sized frames.
6425 */
6426 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006427 dev->stats.rx_dropped++;
6428 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006429 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006430 }
6431
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006432 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6433 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006434 if (!skb) {
6435 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006436 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 }
6438
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006439 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 skb_put(skb, pkt_size);
6441 skb->protocol = eth_type_trans(skb, dev);
6442
Francois Romieu7a8fc772011-03-01 17:18:33 +01006443 rtl8169_rx_vlan_tag(desc, skb);
6444
Francois Romieu56de4142011-03-15 17:29:31 +01006445 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
Junchang Wang8027aa22012-03-04 23:30:32 +01006447 u64_stats_update_begin(&tp->rx_stats.syncp);
6448 tp->rx_stats.packets++;
6449 tp->rx_stats.bytes += pkt_size;
6450 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 }
françois romieuce11ff52013-01-24 13:30:06 +00006452release_descriptor:
6453 desc->opts2 = 0;
6454 wmb();
6455 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 }
6457
6458 count = cur_rx - tp->cur_rx;
6459 tp->cur_rx = cur_rx;
6460
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 return count;
6462}
6463
Francois Romieu07d3f512007-02-21 22:40:46 +01006464static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465{
Francois Romieu07d3f512007-02-21 22:40:46 +01006466 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006469 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
Francois Romieu9085cdfa2012-01-26 12:59:08 +01006471 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006472 if (status && status != 0xffff) {
6473 status &= RTL_EVENT_NAPI | tp->event_slow;
6474 if (status) {
6475 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00006476
Francois Romieuda78dbf2012-01-26 14:18:23 +01006477 rtl_irq_disable(tp);
6478 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 return IRQ_RETVAL(handled);
6482}
6483
Francois Romieuda78dbf2012-01-26 14:18:23 +01006484/*
6485 * Workqueue context.
6486 */
6487static void rtl_slow_event_work(struct rtl8169_private *tp)
6488{
6489 struct net_device *dev = tp->dev;
6490 u16 status;
6491
6492 status = rtl_get_events(tp) & tp->event_slow;
6493 rtl_ack_events(tp, status);
6494
6495 if (unlikely(status & RxFIFOOver)) {
6496 switch (tp->mac_version) {
6497 /* Work around for rx fifo overflow */
6498 case RTL_GIGA_MAC_VER_11:
6499 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006500 /* XXX - Hack alert. See rtl_task(). */
6501 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006502 default:
6503 break;
6504 }
6505 }
6506
6507 if (unlikely(status & SYSErr))
6508 rtl8169_pcierr_interrupt(dev);
6509
6510 if (status & LinkChg)
6511 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
6512
françois romieu7dbb4912012-06-09 10:53:16 +00006513 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006514}
6515
Francois Romieu4422bcd2012-01-26 11:23:32 +01006516static void rtl_task(struct work_struct *work)
6517{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006518 static const struct {
6519 int bitnr;
6520 void (*action)(struct rtl8169_private *);
6521 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006522 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006523 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6524 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6525 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
6526 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006527 struct rtl8169_private *tp =
6528 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006529 struct net_device *dev = tp->dev;
6530 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006531
Francois Romieuda78dbf2012-01-26 14:18:23 +01006532 rtl_lock_work(tp);
6533
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006534 if (!netif_running(dev) ||
6535 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006536 goto out_unlock;
6537
6538 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6539 bool pending;
6540
Francois Romieuda78dbf2012-01-26 14:18:23 +01006541 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006542 if (pending)
6543 rtl_work[i].action(tp);
6544 }
6545
6546out_unlock:
6547 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006548}
6549
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006550static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006552 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6553 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006554 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6555 int work_done= 0;
6556 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
Francois Romieuda78dbf2012-01-26 14:18:23 +01006558 status = rtl_get_events(tp);
6559 rtl_ack_events(tp, status & ~tp->event_slow);
6560
6561 if (status & RTL_EVENT_NAPI_RX)
6562 work_done = rtl_rx(dev, tp, (u32) budget);
6563
6564 if (status & RTL_EVENT_NAPI_TX)
6565 rtl_tx(dev, tp);
6566
6567 if (status & tp->event_slow) {
6568 enable_mask &= ~tp->event_slow;
6569
6570 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006573 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006574 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00006575
Francois Romieuda78dbf2012-01-26 14:18:23 +01006576 rtl_irq_enable(tp, enable_mask);
6577 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 }
6579
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006580 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
Francois Romieu523a6092008-09-10 22:28:56 +02006583static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
6584{
6585 struct rtl8169_private *tp = netdev_priv(dev);
6586
6587 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6588 return;
6589
6590 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6591 RTL_W32(RxMissed, 0);
6592}
6593
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594static void rtl8169_down(struct net_device *dev)
6595{
6596 struct rtl8169_private *tp = netdev_priv(dev);
6597 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Francois Romieu4876cc12011-03-11 21:07:11 +01006599 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006601 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006602 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
Hayes Wang92fc43b2011-07-06 15:58:03 +08006604 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006605 /*
6606 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006607 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6608 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006609 */
Francois Romieu523a6092008-09-10 22:28:56 +02006610 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006613 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 rtl8169_tx_clear(tp);
6616
6617 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006618
6619 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620}
6621
6622static int rtl8169_close(struct net_device *dev)
6623{
6624 struct rtl8169_private *tp = netdev_priv(dev);
6625 struct pci_dev *pdev = tp->pci_dev;
6626
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006627 pm_runtime_get_sync(&pdev->dev);
6628
Francois Romieucecb5fd2011-04-01 10:21:07 +02006629 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006630 rtl8169_update_counters(dev);
6631
Francois Romieuda78dbf2012-01-26 14:18:23 +01006632 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006633 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006634
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006636 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
Lekensteyn4ea72442013-07-22 09:53:30 +02006638 cancel_work_sync(&tp->wk.work);
6639
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006640 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006642 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6643 tp->RxPhyAddr);
6644 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6645 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 tp->TxDescArray = NULL;
6647 tp->RxDescArray = NULL;
6648
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006649 pm_runtime_put_sync(&pdev->dev);
6650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 return 0;
6652}
6653
Francois Romieudc1c00c2012-03-08 10:06:18 +01006654#ifdef CONFIG_NET_POLL_CONTROLLER
6655static void rtl8169_netpoll(struct net_device *dev)
6656{
6657 struct rtl8169_private *tp = netdev_priv(dev);
6658
6659 rtl8169_interrupt(tp->pci_dev->irq, dev);
6660}
6661#endif
6662
Francois Romieudf43ac72012-03-08 09:48:40 +01006663static int rtl_open(struct net_device *dev)
6664{
6665 struct rtl8169_private *tp = netdev_priv(dev);
6666 void __iomem *ioaddr = tp->mmio_addr;
6667 struct pci_dev *pdev = tp->pci_dev;
6668 int retval = -ENOMEM;
6669
6670 pm_runtime_get_sync(&pdev->dev);
6671
6672 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006673 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006674 * dma_alloc_coherent provides more.
6675 */
6676 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6677 &tp->TxPhyAddr, GFP_KERNEL);
6678 if (!tp->TxDescArray)
6679 goto err_pm_runtime_put;
6680
6681 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6682 &tp->RxPhyAddr, GFP_KERNEL);
6683 if (!tp->RxDescArray)
6684 goto err_free_tx_0;
6685
6686 retval = rtl8169_init_ring(dev);
6687 if (retval < 0)
6688 goto err_free_rx_1;
6689
6690 INIT_WORK(&tp->wk.work, rtl_task);
6691
6692 smp_mb();
6693
6694 rtl_request_firmware(tp);
6695
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006696 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006697 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6698 dev->name, dev);
6699 if (retval < 0)
6700 goto err_release_fw_2;
6701
6702 rtl_lock_work(tp);
6703
6704 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6705
6706 napi_enable(&tp->napi);
6707
6708 rtl8169_init_phy(dev, tp);
6709
hayeswang48c20402014-09-12 11:35:11 +08006710 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
6711 tp->cp_cmd |= RxVlan;
6712 else
6713 tp->cp_cmd &= ~RxVlan;
6714
6715 RTL_W16(CPlusCmd, tp->cp_cmd);
Francois Romieudf43ac72012-03-08 09:48:40 +01006716
6717 rtl_pll_power_up(tp);
6718
6719 rtl_hw_start(dev);
6720
6721 netif_start_queue(dev);
6722
6723 rtl_unlock_work(tp);
6724
6725 tp->saved_wolopts = 0;
6726 pm_runtime_put_noidle(&pdev->dev);
6727
6728 rtl8169_check_link_status(dev, tp, ioaddr);
6729out:
6730 return retval;
6731
6732err_release_fw_2:
6733 rtl_release_firmware(tp);
6734 rtl8169_rx_clear(tp);
6735err_free_rx_1:
6736 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6737 tp->RxPhyAddr);
6738 tp->RxDescArray = NULL;
6739err_free_tx_0:
6740 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6741 tp->TxPhyAddr);
6742 tp->TxDescArray = NULL;
6743err_pm_runtime_put:
6744 pm_runtime_put_noidle(&pdev->dev);
6745 goto out;
6746}
6747
Junchang Wang8027aa22012-03-04 23:30:32 +01006748static struct rtnl_link_stats64 *
6749rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750{
6751 struct rtl8169_private *tp = netdev_priv(dev);
6752 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006753 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Francois Romieuda78dbf2012-01-26 14:18:23 +01006755 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006756 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006757
Junchang Wang8027aa22012-03-04 23:30:32 +01006758 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006759 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006760 stats->rx_packets = tp->rx_stats.packets;
6761 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006762 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006763
6764
6765 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006766 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006767 stats->tx_packets = tp->tx_stats.packets;
6768 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006769 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006770
6771 stats->rx_dropped = dev->stats.rx_dropped;
6772 stats->tx_dropped = dev->stats.tx_dropped;
6773 stats->rx_length_errors = dev->stats.rx_length_errors;
6774 stats->rx_errors = dev->stats.rx_errors;
6775 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6776 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6777 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6778
6779 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780}
6781
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006782static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006783{
françois romieu065c27c2011-01-03 15:08:12 +00006784 struct rtl8169_private *tp = netdev_priv(dev);
6785
Francois Romieu5d06a992006-02-23 00:47:58 +01006786 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006787 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006788
6789 netif_device_detach(dev);
6790 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006791
6792 rtl_lock_work(tp);
6793 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006794 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006795 rtl_unlock_work(tp);
6796
6797 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006798}
Francois Romieu5d06a992006-02-23 00:47:58 +01006799
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006800#ifdef CONFIG_PM
6801
6802static int rtl8169_suspend(struct device *device)
6803{
6804 struct pci_dev *pdev = to_pci_dev(device);
6805 struct net_device *dev = pci_get_drvdata(pdev);
6806
6807 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006808
Francois Romieu5d06a992006-02-23 00:47:58 +01006809 return 0;
6810}
6811
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006812static void __rtl8169_resume(struct net_device *dev)
6813{
françois romieu065c27c2011-01-03 15:08:12 +00006814 struct rtl8169_private *tp = netdev_priv(dev);
6815
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006816 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006817
6818 rtl_pll_power_up(tp);
6819
Artem Savkovcff4c162012-04-03 10:29:11 +00006820 rtl_lock_work(tp);
6821 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006822 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006823 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006824
Francois Romieu98ddf982012-01-31 10:47:34 +01006825 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006826}
6827
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006828static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006829{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006830 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006831 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006832 struct rtl8169_private *tp = netdev_priv(dev);
6833
6834 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006835
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006836 if (netif_running(dev))
6837 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006838
Francois Romieu5d06a992006-02-23 00:47:58 +01006839 return 0;
6840}
6841
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006842static int rtl8169_runtime_suspend(struct device *device)
6843{
6844 struct pci_dev *pdev = to_pci_dev(device);
6845 struct net_device *dev = pci_get_drvdata(pdev);
6846 struct rtl8169_private *tp = netdev_priv(dev);
6847
6848 if (!tp->TxDescArray)
6849 return 0;
6850
Francois Romieuda78dbf2012-01-26 14:18:23 +01006851 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006852 tp->saved_wolopts = __rtl8169_get_wol(tp);
6853 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006854 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006855
6856 rtl8169_net_suspend(dev);
6857
6858 return 0;
6859}
6860
6861static int rtl8169_runtime_resume(struct device *device)
6862{
6863 struct pci_dev *pdev = to_pci_dev(device);
6864 struct net_device *dev = pci_get_drvdata(pdev);
6865 struct rtl8169_private *tp = netdev_priv(dev);
6866
6867 if (!tp->TxDescArray)
6868 return 0;
6869
Francois Romieuda78dbf2012-01-26 14:18:23 +01006870 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006871 __rtl8169_set_wol(tp, tp->saved_wolopts);
6872 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006873 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006874
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006875 rtl8169_init_phy(dev, tp);
6876
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006877 __rtl8169_resume(dev);
6878
6879 return 0;
6880}
6881
6882static int rtl8169_runtime_idle(struct device *device)
6883{
6884 struct pci_dev *pdev = to_pci_dev(device);
6885 struct net_device *dev = pci_get_drvdata(pdev);
6886 struct rtl8169_private *tp = netdev_priv(dev);
6887
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006888 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006889}
6890
Alexey Dobriyan47145212009-12-14 18:00:08 -08006891static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006892 .suspend = rtl8169_suspend,
6893 .resume = rtl8169_resume,
6894 .freeze = rtl8169_suspend,
6895 .thaw = rtl8169_resume,
6896 .poweroff = rtl8169_suspend,
6897 .restore = rtl8169_resume,
6898 .runtime_suspend = rtl8169_runtime_suspend,
6899 .runtime_resume = rtl8169_runtime_resume,
6900 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006901};
6902
6903#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6904
6905#else /* !CONFIG_PM */
6906
6907#define RTL8169_PM_OPS NULL
6908
6909#endif /* !CONFIG_PM */
6910
David S. Miller1805b2f2011-10-24 18:18:09 -04006911static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6912{
6913 void __iomem *ioaddr = tp->mmio_addr;
6914
6915 /* WoL fails with 8168b when the receiver is disabled. */
6916 switch (tp->mac_version) {
6917 case RTL_GIGA_MAC_VER_11:
6918 case RTL_GIGA_MAC_VER_12:
6919 case RTL_GIGA_MAC_VER_17:
6920 pci_clear_master(tp->pci_dev);
6921
6922 RTL_W8(ChipCmd, CmdRxEnb);
6923 /* PCI commit */
6924 RTL_R8(ChipCmd);
6925 break;
6926 default:
6927 break;
6928 }
6929}
6930
Francois Romieu1765f952008-09-13 17:21:40 +02006931static void rtl_shutdown(struct pci_dev *pdev)
6932{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006933 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006934 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006935 struct device *d = &pdev->dev;
6936
6937 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006938
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006939 rtl8169_net_suspend(dev);
6940
Francois Romieucecb5fd2011-04-01 10:21:07 +02006941 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006942 rtl_rar_set(tp, dev->perm_addr);
6943
Hayes Wang92fc43b2011-07-06 15:58:03 +08006944 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006945
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006946 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006947 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6948 rtl_wol_suspend_quirk(tp);
6949 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006950 }
6951
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006952 pci_wake_from_d3(pdev, true);
6953 pci_set_power_state(pdev, PCI_D3hot);
6954 }
françois romieu2a15cd22012-03-06 01:14:12 +00006955
6956 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006957}
Francois Romieu5d06a992006-02-23 00:47:58 +01006958
Bill Pembertonbaf63292012-12-03 09:23:28 -05006959static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006960{
6961 struct net_device *dev = pci_get_drvdata(pdev);
6962 struct rtl8169_private *tp = netdev_priv(dev);
6963
6964 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6965 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6966 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6967 rtl8168_driver_stop(tp);
6968 }
6969
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006970 netif_napi_del(&tp->napi);
6971
Francois Romieue27566e2012-03-08 09:54:01 +01006972 unregister_netdev(dev);
6973
6974 rtl_release_firmware(tp);
6975
6976 if (pci_dev_run_wake(pdev))
6977 pm_runtime_get_noresume(&pdev->dev);
6978
6979 /* restore original MAC address */
6980 rtl_rar_set(tp, dev->perm_addr);
6981
6982 rtl_disable_msi(pdev, tp);
6983 rtl8169_release_board(pdev, dev, tp->mmio_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006984}
6985
Francois Romieufa9c3852012-03-08 10:01:50 +01006986static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006987 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006988 .ndo_stop = rtl8169_close,
6989 .ndo_get_stats64 = rtl8169_get_stats64,
6990 .ndo_start_xmit = rtl8169_start_xmit,
6991 .ndo_tx_timeout = rtl8169_tx_timeout,
6992 .ndo_validate_addr = eth_validate_addr,
6993 .ndo_change_mtu = rtl8169_change_mtu,
6994 .ndo_fix_features = rtl8169_fix_features,
6995 .ndo_set_features = rtl8169_set_features,
6996 .ndo_set_mac_address = rtl_set_mac_address,
6997 .ndo_do_ioctl = rtl8169_ioctl,
6998 .ndo_set_rx_mode = rtl_set_rx_mode,
6999#ifdef CONFIG_NET_POLL_CONTROLLER
7000 .ndo_poll_controller = rtl8169_netpoll,
7001#endif
7002
7003};
7004
Francois Romieu31fa8b12012-03-08 10:09:40 +01007005static const struct rtl_cfg_info {
7006 void (*hw_start)(struct net_device *);
7007 unsigned int region;
7008 unsigned int align;
7009 u16 event_slow;
7010 unsigned features;
7011 u8 default_ver;
7012} rtl_cfg_infos [] = {
7013 [RTL_CFG_0] = {
7014 .hw_start = rtl_hw_start_8169,
7015 .region = 1,
7016 .align = 0,
7017 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7018 .features = RTL_FEATURE_GMII,
7019 .default_ver = RTL_GIGA_MAC_VER_01,
7020 },
7021 [RTL_CFG_1] = {
7022 .hw_start = rtl_hw_start_8168,
7023 .region = 2,
7024 .align = 8,
7025 .event_slow = SYSErr | LinkChg | RxOverflow,
7026 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
7027 .default_ver = RTL_GIGA_MAC_VER_11,
7028 },
7029 [RTL_CFG_2] = {
7030 .hw_start = rtl_hw_start_8101,
7031 .region = 2,
7032 .align = 8,
7033 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7034 PCSTimeout,
7035 .features = RTL_FEATURE_MSI,
7036 .default_ver = RTL_GIGA_MAC_VER_13,
7037 }
7038};
7039
7040/* Cfg9346_Unlock assumed. */
7041static unsigned rtl_try_msi(struct rtl8169_private *tp,
7042 const struct rtl_cfg_info *cfg)
7043{
7044 void __iomem *ioaddr = tp->mmio_addr;
7045 unsigned msi = 0;
7046 u8 cfg2;
7047
7048 cfg2 = RTL_R8(Config2) & ~MSIEnable;
7049 if (cfg->features & RTL_FEATURE_MSI) {
7050 if (pci_enable_msi(tp->pci_dev)) {
7051 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
7052 } else {
7053 cfg2 |= MSIEnable;
7054 msi = RTL_FEATURE_MSI;
7055 }
7056 }
7057 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
7058 RTL_W8(Config2, cfg2);
7059 return msi;
7060}
7061
Hayes Wangc5583862012-07-02 17:23:22 +08007062DECLARE_RTL_COND(rtl_link_list_ready_cond)
7063{
7064 void __iomem *ioaddr = tp->mmio_addr;
7065
7066 return RTL_R8(MCU) & LINK_LIST_RDY;
7067}
7068
7069DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7070{
7071 void __iomem *ioaddr = tp->mmio_addr;
7072
7073 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7074}
7075
Bill Pembertonbaf63292012-12-03 09:23:28 -05007076static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007077{
7078 void __iomem *ioaddr = tp->mmio_addr;
7079 u32 data;
7080
7081 tp->ocp_base = OCP_STD_PHY_BASE;
7082
7083 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
7084
7085 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7086 return;
7087
7088 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7089 return;
7090
7091 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7092 msleep(1);
7093 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
7094
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007095 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007096 data &= ~(1 << 14);
7097 r8168_mac_ocp_write(tp, 0xe8de, data);
7098
7099 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7100 return;
7101
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007102 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007103 data |= (1 << 15);
7104 r8168_mac_ocp_write(tp, 0xe8de, data);
7105
7106 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7107 return;
7108}
7109
Bill Pembertonbaf63292012-12-03 09:23:28 -05007110static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007111{
7112 switch (tp->mac_version) {
7113 case RTL_GIGA_MAC_VER_40:
7114 case RTL_GIGA_MAC_VER_41:
hayeswang57538c42013-04-01 22:23:40 +00007115 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00007116 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08007117 case RTL_GIGA_MAC_VER_44:
Hayes Wangc5583862012-07-02 17:23:22 +08007118 rtl_hw_init_8168g(tp);
7119 break;
7120
7121 default:
7122 break;
7123 }
7124}
7125
Bill Pembertonbaf63292012-12-03 09:23:28 -05007126static int
Francois Romieu3b6cf252012-03-08 09:59:04 +01007127rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7128{
7129 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7130 const unsigned int region = cfg->region;
7131 struct rtl8169_private *tp;
7132 struct mii_if_info *mii;
7133 struct net_device *dev;
7134 void __iomem *ioaddr;
7135 int chipset, i;
7136 int rc;
7137
7138 if (netif_msg_drv(&debug)) {
7139 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7140 MODULENAME, RTL8169_VERSION);
7141 }
7142
7143 dev = alloc_etherdev(sizeof (*tp));
7144 if (!dev) {
7145 rc = -ENOMEM;
7146 goto out;
7147 }
7148
7149 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007150 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007151 tp = netdev_priv(dev);
7152 tp->dev = dev;
7153 tp->pci_dev = pdev;
7154 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7155
7156 mii = &tp->mii;
7157 mii->dev = dev;
7158 mii->mdio_read = rtl_mdio_read;
7159 mii->mdio_write = rtl_mdio_write;
7160 mii->phy_id_mask = 0x1f;
7161 mii->reg_num_mask = 0x1f;
7162 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
7163
7164 /* disable ASPM completely as that cause random device stop working
7165 * problems as well as full system hangs for some PCIe devices users */
7166 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
7167 PCIE_LINK_STATE_CLKPM);
7168
7169 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7170 rc = pci_enable_device(pdev);
7171 if (rc < 0) {
7172 netif_err(tp, probe, dev, "enable failure\n");
7173 goto err_out_free_dev_1;
7174 }
7175
7176 if (pci_set_mwi(pdev) < 0)
7177 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
7178
7179 /* make sure PCI base addr 1 is MMIO */
7180 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
7181 netif_err(tp, probe, dev,
7182 "region #%d not an MMIO resource, aborting\n",
7183 region);
7184 rc = -ENODEV;
7185 goto err_out_mwi_2;
7186 }
7187
7188 /* check for weird/broken PCI region reporting */
7189 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7190 netif_err(tp, probe, dev,
7191 "Invalid PCI region size(s), aborting\n");
7192 rc = -ENODEV;
7193 goto err_out_mwi_2;
7194 }
7195
7196 rc = pci_request_regions(pdev, MODULENAME);
7197 if (rc < 0) {
7198 netif_err(tp, probe, dev, "could not request regions\n");
7199 goto err_out_mwi_2;
7200 }
7201
7202 tp->cp_cmd = RxChkSum;
7203
7204 if ((sizeof(dma_addr_t) > 4) &&
7205 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
7206 tp->cp_cmd |= PCIDAC;
7207 dev->features |= NETIF_F_HIGHDMA;
7208 } else {
7209 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7210 if (rc < 0) {
7211 netif_err(tp, probe, dev, "DMA configuration failed\n");
7212 goto err_out_free_res_3;
7213 }
7214 }
7215
7216 /* ioremap MMIO region */
7217 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
7218 if (!ioaddr) {
7219 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
7220 rc = -EIO;
7221 goto err_out_free_res_3;
7222 }
7223 tp->mmio_addr = ioaddr;
7224
7225 if (!pci_is_pcie(pdev))
7226 netif_info(tp, probe, dev, "not PCI Express\n");
7227
7228 /* Identify chip attached to board */
7229 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
7230
7231 rtl_init_rxcfg(tp);
7232
7233 rtl_irq_disable(tp);
7234
Hayes Wangc5583862012-07-02 17:23:22 +08007235 rtl_hw_initialize(tp);
7236
Francois Romieu3b6cf252012-03-08 09:59:04 +01007237 rtl_hw_reset(tp);
7238
7239 rtl_ack_events(tp, 0xffff);
7240
7241 pci_set_master(pdev);
7242
7243 /*
7244 * Pretend we are using VLANs; This bypasses a nasty bug where
7245 * Interrupts stop flowing on high load on 8110SCd controllers.
7246 */
7247 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7248 tp->cp_cmd |= RxVlan;
7249
7250 rtl_init_mdio_ops(tp);
7251 rtl_init_pll_power_ops(tp);
7252 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08007253 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007254
7255 rtl8169_print_mac_version(tp);
7256
7257 chipset = tp->mac_version;
7258 tp->txd_version = rtl_chip_infos[chipset].txd_version;
7259
7260 RTL_W8(Cfg9346, Cfg9346_Unlock);
7261 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
Peter Wu8f9d5132013-08-17 11:00:02 +02007262 RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
Francois Romieu3b6cf252012-03-08 09:59:04 +01007263 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
7264 tp->features |= RTL_FEATURE_WOL;
7265 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
7266 tp->features |= RTL_FEATURE_WOL;
7267 tp->features |= rtl_try_msi(tp, cfg);
7268 RTL_W8(Cfg9346, Cfg9346_Lock);
7269
7270 if (rtl_tbi_enabled(tp)) {
7271 tp->set_speed = rtl8169_set_speed_tbi;
7272 tp->get_settings = rtl8169_gset_tbi;
7273 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
7274 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
7275 tp->link_ok = rtl8169_tbi_link_ok;
7276 tp->do_ioctl = rtl_tbi_ioctl;
7277 } else {
7278 tp->set_speed = rtl8169_set_speed_xmii;
7279 tp->get_settings = rtl8169_gset_xmii;
7280 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
7281 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
7282 tp->link_ok = rtl8169_xmii_link_ok;
7283 tp->do_ioctl = rtl_xmii_ioctl;
7284 }
7285
7286 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007287 u64_stats_init(&tp->rx_stats.syncp);
7288 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007289
7290 /* Get MAC address */
7291 for (i = 0; i < ETH_ALEN; i++)
7292 dev->dev_addr[i] = RTL_R8(MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007293
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007294 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007295 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007296
7297 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
7298
7299 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7300 * properly for all devices */
7301 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007302 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007303
7304 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007305 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7306 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007307 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7308 NETIF_F_HIGHDMA;
7309
7310 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7311 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007312 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007313
hayeswang5888d3f2014-07-11 16:25:56 +08007314 if (tp->txd_version == RTL_TD_0)
7315 tp->tso_csum = rtl8169_tso_csum_v1;
hayeswange9746042014-07-11 16:25:58 +08007316 else if (tp->txd_version == RTL_TD_1) {
hayeswang5888d3f2014-07-11 16:25:56 +08007317 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007318 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7319 } else
hayeswang5888d3f2014-07-11 16:25:56 +08007320 WARN_ON_ONCE(1);
7321
Francois Romieu3b6cf252012-03-08 09:59:04 +01007322 dev->hw_features |= NETIF_F_RXALL;
7323 dev->hw_features |= NETIF_F_RXFCS;
7324
7325 tp->hw_start = cfg->hw_start;
7326 tp->event_slow = cfg->event_slow;
7327
7328 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
7329 ~(RxBOVF | RxFOVF) : ~0;
7330
7331 init_timer(&tp->timer);
7332 tp->timer.data = (unsigned long) dev;
7333 tp->timer.function = rtl8169_phy_timer;
7334
7335 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7336
7337 rc = register_netdev(dev);
7338 if (rc < 0)
7339 goto err_out_msi_4;
7340
7341 pci_set_drvdata(pdev, dev);
7342
Francois Romieu92a7c4e2012-03-10 10:42:12 +01007343 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
7344 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
7345 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007346 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7347 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7348 "tx checksumming: %s]\n",
7349 rtl_chip_infos[chipset].jumbo_max,
7350 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
7351 }
7352
7353 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
7354 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
7355 tp->mac_version == RTL_GIGA_MAC_VER_31) {
7356 rtl8168_driver_start(tp);
7357 }
7358
7359 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
7360
7361 if (pci_dev_run_wake(pdev))
7362 pm_runtime_put_noidle(&pdev->dev);
7363
7364 netif_carrier_off(dev);
7365
7366out:
7367 return rc;
7368
7369err_out_msi_4:
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007370 netif_napi_del(&tp->napi);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007371 rtl_disable_msi(pdev, tp);
7372 iounmap(ioaddr);
7373err_out_free_res_3:
7374 pci_release_regions(pdev);
7375err_out_mwi_2:
7376 pci_clear_mwi(pdev);
7377 pci_disable_device(pdev);
7378err_out_free_dev_1:
7379 free_netdev(dev);
7380 goto out;
7381}
7382
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383static struct pci_driver rtl8169_pci_driver = {
7384 .name = MODULENAME,
7385 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007386 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007387 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007388 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007389 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390};
7391
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007392module_pci_driver(rtl8169_pci_driver);