blob: 00628d84342f1e1196e73bd9fe0ae14465c068c1 [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>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef RTL8169_DEBUG
51#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020052 if (!(expr)) { \
53 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070054 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020055 }
Joe Perches06fa7352007-10-18 21:15:00 +020056#define dprintk(fmt, args...) \
57 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
59#define assert(expr) do {} while (0)
60#define dprintk(fmt, args...) do {} while (0)
61#endif /* RTL8169_DEBUG */
62
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020063#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070064 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define TX_BUFFS_AVAIL(tp) \
67 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
70 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050071static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Francois Romieu9c14cea2008-07-05 00:21:15 +020073#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
76#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
77
78#define R8169_REGS_SIZE 256
79#define R8169_NAPI_WEIGHT 64
80#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
81#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
82#define RX_BUF_SIZE 1536 /* Rx Buffer size */
83#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
84#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
85
86#define RTL8169_TX_TIMEOUT (6*HZ)
87#define RTL8169_PHY_TIMEOUT (10*HZ)
88
françois romieuea8dbdd2009-03-15 01:10:50 +000089#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
90#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020091#define RTL_EEPROM_SIG_ADDR 0x0000
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* write/read MMIO register */
94#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97#define RTL_R8(reg) readb (ioaddr + (reg))
98#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000099#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200102 RTL_GIGA_MAC_VER_01 = 0,
103 RTL_GIGA_MAC_VER_02,
104 RTL_GIGA_MAC_VER_03,
105 RTL_GIGA_MAC_VER_04,
106 RTL_GIGA_MAC_VER_05,
107 RTL_GIGA_MAC_VER_06,
108 RTL_GIGA_MAC_VER_07,
109 RTL_GIGA_MAC_VER_08,
110 RTL_GIGA_MAC_VER_09,
111 RTL_GIGA_MAC_VER_10,
112 RTL_GIGA_MAC_VER_11,
113 RTL_GIGA_MAC_VER_12,
114 RTL_GIGA_MAC_VER_13,
115 RTL_GIGA_MAC_VER_14,
116 RTL_GIGA_MAC_VER_15,
117 RTL_GIGA_MAC_VER_16,
118 RTL_GIGA_MAC_VER_17,
119 RTL_GIGA_MAC_VER_18,
120 RTL_GIGA_MAC_VER_19,
121 RTL_GIGA_MAC_VER_20,
122 RTL_GIGA_MAC_VER_21,
123 RTL_GIGA_MAC_VER_22,
124 RTL_GIGA_MAC_VER_23,
125 RTL_GIGA_MAC_VER_24,
126 RTL_GIGA_MAC_VER_25,
127 RTL_GIGA_MAC_VER_26,
128 RTL_GIGA_MAC_VER_27,
129 RTL_GIGA_MAC_VER_28,
130 RTL_GIGA_MAC_VER_29,
131 RTL_GIGA_MAC_VER_30,
132 RTL_GIGA_MAC_VER_31,
133 RTL_GIGA_MAC_VER_32,
134 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800135 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800136 RTL_GIGA_MAC_VER_35,
137 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800138 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800139 RTL_GIGA_MAC_VER_38,
Francois Romieu85bffe62011-04-27 08:22:39 +0200140 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Francois Romieu2b7b4312011-04-18 22:53:24 -0700143enum rtl_tx_desc_version {
144 RTL_TD_0 = 0,
145 RTL_TD_1 = 1,
146};
147
Francois Romieud58d46b2011-05-03 16:38:29 +0200148#define JUMBO_1K ETH_DATA_LEN
149#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
150#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
151#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
152#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
153
154#define _R(NAME,TD,FW,SZ,B) { \
155 .name = NAME, \
156 .txd_version = TD, \
157 .fw_name = FW, \
158 .jumbo_max = SZ, \
159 .jumbo_tx_csum = B \
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800162static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700164 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200166 u16 jumbo_max;
167 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200168} rtl_chip_infos[] = {
169 /* PCI devices. */
170 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200171 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200172 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200173 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200174 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200175 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200176 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200177 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200178 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200179 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200180 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 /* PCI-E devices. */
183 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200184 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200185 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200186 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200187 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200188 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200189 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200190 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200191 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200192 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200193 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200194 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200195 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200196 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200197 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200198 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200199 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200200 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200201 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200202 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200203 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200204 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200205 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200206 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200207 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200208 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200209 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200210 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200211 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200212 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200213 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200214 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200215 [RTL_GIGA_MAC_VER_23] =
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_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200218 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
224 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200225 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200226 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200227 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200228 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
231 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
234 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200235 [RTL_GIGA_MAC_VER_31] =
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_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
239 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200240 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
242 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800243 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
248 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800249 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200250 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
251 JUMBO_9K, false),
Hayes Wang7e18dca2012-03-30 14:33:02 +0800252 [RTL_GIGA_MAC_VER_37] =
253 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
254 JUMBO_1K, true),
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800255 [RTL_GIGA_MAC_VER_38] =
256 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
257 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259#undef _R
260
Francois Romieubcf0bf92006-07-26 23:14:13 +0200261enum cfg_version {
262 RTL_CFG_0 = 0x00,
263 RTL_CFG_1,
264 RTL_CFG_2
265};
266
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000267static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200268 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200269 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100271 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200272 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
273 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000274 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200275 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200276 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
277 { PCI_VENDOR_ID_LINKSYS, 0x1032,
278 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100279 { 0x0001, 0x8168,
280 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 {0,},
282};
283
284MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
285
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000286static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700287static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200288static struct {
289 u32 msg_enable;
290} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Francois Romieu07d3f512007-02-21 22:40:46 +0100292enum rtl_registers {
293 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100294 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100295 MAR0 = 8, /* Multicast filter. */
296 CounterAddrLow = 0x10,
297 CounterAddrHigh = 0x14,
298 TxDescStartAddrLow = 0x20,
299 TxDescStartAddrHigh = 0x24,
300 TxHDescStartAddrLow = 0x28,
301 TxHDescStartAddrHigh = 0x2c,
302 FLASH = 0x30,
303 ERSR = 0x36,
304 ChipCmd = 0x37,
305 TxPoll = 0x38,
306 IntrMask = 0x3c,
307 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700308
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800309 TxConfig = 0x40,
310#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
311#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
312
313 RxConfig = 0x44,
314#define RX128_INT_EN (1 << 15) /* 8111c and later */
315#define RX_MULTI_EN (1 << 14) /* 8111c only */
316#define RXCFG_FIFO_SHIFT 13
317 /* No threshold before first PCI xfer */
318#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
319#define RXCFG_DMA_SHIFT 8
320 /* Unlimited maximum PCI burst. */
321#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700322
Francois Romieu07d3f512007-02-21 22:40:46 +0100323 RxMissed = 0x4c,
324 Cfg9346 = 0x50,
325 Config0 = 0x51,
326 Config1 = 0x52,
327 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200328#define PME_SIGNAL (1 << 5) /* 8168c and later */
329
Francois Romieu07d3f512007-02-21 22:40:46 +0100330 Config3 = 0x54,
331 Config4 = 0x55,
332 Config5 = 0x56,
333 MultiIntr = 0x5c,
334 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100335 PHYstatus = 0x6c,
336 RxMaxSize = 0xda,
337 CPlusCmd = 0xe0,
338 IntrMitigate = 0xe2,
339 RxDescAddrLow = 0xe4,
340 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000341 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
342
343#define NoEarlyTx 0x3f /* Max value : no early transmit. */
344
345 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
346
347#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800348#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000349
Francois Romieu07d3f512007-02-21 22:40:46 +0100350 FuncEvent = 0xf0,
351 FuncEventMask = 0xf4,
352 FuncPresetState = 0xf8,
353 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
Francois Romieuf162a5d2008-06-01 22:37:49 +0200356enum rtl8110_registers {
357 TBICSR = 0x64,
358 TBI_ANAR = 0x68,
359 TBI_LPAR = 0x6a,
360};
361
362enum rtl8168_8101_registers {
363 CSIDR = 0x64,
364 CSIAR = 0x68,
365#define CSIAR_FLAG 0x80000000
366#define CSIAR_WRITE_CMD 0x80000000
367#define CSIAR_BYTE_ENABLE 0x0f
368#define CSIAR_BYTE_ENABLE_SHIFT 12
369#define CSIAR_ADDR_MASK 0x0fff
Hayes Wang7e18dca2012-03-30 14:33:02 +0800370#define CSIAR_FUNC_CARD 0x00000000
371#define CSIAR_FUNC_SDIO 0x00010000
372#define CSIAR_FUNC_NIC 0x00020000
françois romieu065c27c2011-01-03 15:08:12 +0000373 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200374 EPHYAR = 0x80,
375#define EPHYAR_FLAG 0x80000000
376#define EPHYAR_WRITE_CMD 0x80000000
377#define EPHYAR_REG_MASK 0x1f
378#define EPHYAR_REG_SHIFT 16
379#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800380 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800381#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200382 DBG_REG = 0xd1,
383#define FIX_NAK_1 (1 << 4)
384#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800385 TWSI = 0xd2,
386 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800387#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800388#define EN_NDP (1 << 3)
389#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000390 EFUSEAR = 0xdc,
391#define EFUSEAR_FLAG 0x80000000
392#define EFUSEAR_WRITE_CMD 0x80000000
393#define EFUSEAR_READ_CMD 0x00000000
394#define EFUSEAR_REG_MASK 0x03ff
395#define EFUSEAR_REG_SHIFT 8
396#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200397};
398
françois romieuc0e45c12011-01-03 15:08:04 +0000399enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800400 LED_FREQ = 0x1a,
401 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000402 ERIDR = 0x70,
403 ERIAR = 0x74,
404#define ERIAR_FLAG 0x80000000
405#define ERIAR_WRITE_CMD 0x80000000
406#define ERIAR_READ_CMD 0x00000000
407#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000408#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800409#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
410#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
411#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
412#define ERIAR_MASK_SHIFT 12
413#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
414#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
415#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000416 EPHY_RXER_NUM = 0x7c,
417 OCPDR = 0xb0, /* OCP GPHY access */
418#define OCPDR_WRITE_CMD 0x80000000
419#define OCPDR_READ_CMD 0x00000000
420#define OCPDR_REG_MASK 0x7f
421#define OCPDR_GPHY_REG_SHIFT 16
422#define OCPDR_DATA_MASK 0xffff
423 OCPAR = 0xb4,
424#define OCPAR_FLAG 0x80000000
425#define OCPAR_GPHY_WRITE_CMD 0x8000f060
426#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000427 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
428 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200429#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800430#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000431};
432
Francois Romieu07d3f512007-02-21 22:40:46 +0100433enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100435 SYSErr = 0x8000,
436 PCSTimeout = 0x4000,
437 SWInt = 0x0100,
438 TxDescUnavail = 0x0080,
439 RxFIFOOver = 0x0040,
440 LinkChg = 0x0020,
441 RxOverflow = 0x0010,
442 TxErr = 0x0008,
443 TxOK = 0x0004,
444 RxErr = 0x0002,
445 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400448 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200449 RxFOVF = (1 << 23),
450 RxRWT = (1 << 22),
451 RxRES = (1 << 21),
452 RxRUNT = (1 << 20),
453 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800456 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100457 CmdReset = 0x10,
458 CmdRxEnb = 0x08,
459 CmdTxEnb = 0x04,
460 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Francois Romieu275391a2007-02-23 23:50:28 +0100462 /* TXPoll register p.5 */
463 HPQ = 0x80, /* Poll cmd on the high prio queue */
464 NPQ = 0x40, /* Poll cmd on the low prio queue */
465 FSWInt = 0x01, /* Forced software interrupt */
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100468 Cfg9346_Lock = 0x00,
469 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100472 AcceptErr = 0x20,
473 AcceptRunt = 0x10,
474 AcceptBroadcast = 0x08,
475 AcceptMulticast = 0x04,
476 AcceptMyPhys = 0x02,
477 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200478#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* TxConfigBits */
481 TxInterFrameGapShift = 24,
482 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
483
Francois Romieu5d06a992006-02-23 00:47:58 +0100484 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200485 LEDS1 = (1 << 7),
486 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200487 Speed_down = (1 << 4),
488 MEMMAP = (1 << 3),
489 IOMAP = (1 << 2),
490 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100491 PMEnable = (1 << 0), /* Power Management Enable */
492
Francois Romieu6dccd162007-02-13 23:38:05 +0100493 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000494 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100495 PCI_Clock_66MHz = 0x01,
496 PCI_Clock_33MHz = 0x00,
497
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 /* Config3 register p.25 */
499 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
500 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200501 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200502 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100503
Francois Romieud58d46b2011-05-03 16:38:29 +0200504 /* Config4 register */
505 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
506
Francois Romieu5d06a992006-02-23 00:47:58 +0100507 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100508 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
509 MWF = (1 << 5), /* Accept Multicast wakeup frame */
510 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200511 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100512 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100513 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* TBICSR p.28 */
516 TBIReset = 0x80000000,
517 TBILoopback = 0x40000000,
518 TBINwEnable = 0x20000000,
519 TBINwRestart = 0x10000000,
520 TBILinkOk = 0x02000000,
521 TBINwComplete = 0x01000000,
522
523 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200524 EnableBist = (1 << 15), // 8168 8101
525 Mac_dbgo_oe = (1 << 14), // 8168 8101
526 Normal_mode = (1 << 13), // unused
527 Force_half_dup = (1 << 12), // 8168 8101
528 Force_rxflow_en = (1 << 11), // 8168 8101
529 Force_txflow_en = (1 << 10), // 8168 8101
530 Cxpl_dbg_sel = (1 << 9), // 8168 8101
531 ASF = (1 << 8), // 8168 8101
532 PktCntrDisable = (1 << 7), // 8168 8101
533 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 RxVlan = (1 << 6),
535 RxChkSum = (1 << 5),
536 PCIDAC = (1 << 4),
537 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100538 INTT_0 = 0x0000, // 8168
539 INTT_1 = 0x0001, // 8168
540 INTT_2 = 0x0002, // 8168
541 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 TBI_Enable = 0x80,
545 TxFlowCtrl = 0x40,
546 RxFlowCtrl = 0x20,
547 _1000bpsF = 0x10,
548 _100bps = 0x08,
549 _10bps = 0x04,
550 LinkStatus = 0x02,
551 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100554 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200555
556 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100557 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558};
559
Francois Romieu2b7b4312011-04-18 22:53:24 -0700560enum rtl_desc_bit {
561 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
563 RingEnd = (1 << 30), /* End of descriptor ring */
564 FirstFrag = (1 << 29), /* First segment of a packet */
565 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700566};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Francois Romieu2b7b4312011-04-18 22:53:24 -0700568/* Generic case. */
569enum rtl_tx_desc_bit {
570 /* First doubleword. */
571 TD_LSO = (1 << 27), /* Large Send Offload */
572#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Francois Romieu2b7b4312011-04-18 22:53:24 -0700574 /* Second doubleword. */
575 TxVlanTag = (1 << 17), /* Add VLAN tag */
576};
577
578/* 8169, 8168b and 810x except 8102e. */
579enum rtl_tx_desc_bit_0 {
580 /* First doubleword. */
581#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
582 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
583 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
584 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
585};
586
587/* 8102e, 8168c and beyond. */
588enum rtl_tx_desc_bit_1 {
589 /* Second doubleword. */
590#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
591 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
592 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
593 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
594};
595
596static const struct rtl_tx_desc_info {
597 struct {
598 u32 udp;
599 u32 tcp;
600 } checksum;
601 u16 mss_shift;
602 u16 opts_offset;
603} tx_desc_info [] = {
604 [RTL_TD_0] = {
605 .checksum = {
606 .udp = TD0_IP_CS | TD0_UDP_CS,
607 .tcp = TD0_IP_CS | TD0_TCP_CS
608 },
609 .mss_shift = TD0_MSS_SHIFT,
610 .opts_offset = 0
611 },
612 [RTL_TD_1] = {
613 .checksum = {
614 .udp = TD1_IP_CS | TD1_UDP_CS,
615 .tcp = TD1_IP_CS | TD1_TCP_CS
616 },
617 .mss_shift = TD1_MSS_SHIFT,
618 .opts_offset = 1
619 }
620};
621
622enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* Rx private */
624 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
625 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
626
627#define RxProtoUDP (PID1)
628#define RxProtoTCP (PID0)
629#define RxProtoIP (PID1 | PID0)
630#define RxProtoMask RxProtoIP
631
632 IPFail = (1 << 16), /* IP checksum failed */
633 UDPFail = (1 << 15), /* UDP/IP checksum failed */
634 TCPFail = (1 << 14), /* TCP/IP checksum failed */
635 RxVlanTag = (1 << 16), /* VLAN tag available */
636};
637
638#define RsvdMask 0x3fffc000
639
640struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200641 __le32 opts1;
642 __le32 opts2;
643 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644};
645
646struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200647 __le32 opts1;
648 __le32 opts2;
649 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650};
651
652struct ring_info {
653 struct sk_buff *skb;
654 u32 len;
655 u8 __pad[sizeof(void *) - sizeof(u32)];
656};
657
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200658enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200659 RTL_FEATURE_WOL = (1 << 0),
660 RTL_FEATURE_MSI = (1 << 1),
661 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200662};
663
Ivan Vecera355423d2009-02-06 21:49:57 -0800664struct rtl8169_counters {
665 __le64 tx_packets;
666 __le64 rx_packets;
667 __le64 tx_errors;
668 __le32 rx_errors;
669 __le16 rx_missed;
670 __le16 align_errors;
671 __le32 tx_one_collision;
672 __le32 tx_multi_collision;
673 __le64 rx_unicast;
674 __le64 rx_broadcast;
675 __le32 rx_multicast;
676 __le16 tx_aborted;
677 __le16 tx_underun;
678};
679
Francois Romieuda78dbf2012-01-26 14:18:23 +0100680enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100681 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100682 RTL_FLAG_TASK_SLOW_PENDING,
683 RTL_FLAG_TASK_RESET_PENDING,
684 RTL_FLAG_TASK_PHY_PENDING,
685 RTL_FLAG_MAX
686};
687
Junchang Wang8027aa22012-03-04 23:30:32 +0100688struct rtl8169_stats {
689 u64 packets;
690 u64 bytes;
691 struct u64_stats_sync syncp;
692};
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694struct rtl8169_private {
695 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200696 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000697 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700698 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200699 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700700 u16 txd_version;
701 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
703 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
704 u32 dirty_rx;
705 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100706 struct rtl8169_stats rx_stats;
707 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
709 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
710 dma_addr_t TxPhyAddr;
711 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000712 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct timer_list timer;
715 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100716
717 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000718
719 struct mdio_ops {
720 void (*write)(void __iomem *, int, int);
721 int (*read)(void __iomem *, int);
722 } mdio_ops;
723
françois romieu065c27c2011-01-03 15:08:12 +0000724 struct pll_power_ops {
725 void (*down)(struct rtl8169_private *);
726 void (*up)(struct rtl8169_private *);
727 } pll_power_ops;
728
Francois Romieud58d46b2011-05-03 16:38:29 +0200729 struct jumbo_ops {
730 void (*enable)(struct rtl8169_private *);
731 void (*disable)(struct rtl8169_private *);
732 } jumbo_ops;
733
Hayes Wangbeb1fe12012-03-30 14:33:01 +0800734 struct csi_ops {
735 void (*write)(void __iomem *, int, int);
736 u32 (*read)(void __iomem *, int);
737 } csi_ops;
738
Oliver Neukum54405cd2011-01-06 21:55:13 +0100739 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200740 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000741 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100742 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000743 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800745 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100746
747 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100748 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
749 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100750 struct work_struct work;
751 } wk;
752
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200753 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200754
755 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800756 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000757 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400758 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000759
Francois Romieub6ffd972011-06-17 17:00:05 +0200760 struct rtl_fw {
761 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200762
763#define RTL_VER_SIZE 32
764
765 char version[RTL_VER_SIZE];
766
767 struct rtl_fw_phy_action {
768 __le32 *code;
769 size_t size;
770 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200771 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300772#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773};
774
Ralf Baechle979b6c12005-06-13 14:30:40 -0700775MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700778MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200779module_param_named(debug, debug.msg_enable, int, 0);
780MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781MODULE_LICENSE("GPL");
782MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000783MODULE_FIRMWARE(FIRMWARE_8168D_1);
784MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000785MODULE_FIRMWARE(FIRMWARE_8168E_1);
786MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400787MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800788MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800789MODULE_FIRMWARE(FIRMWARE_8168F_1);
790MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800791MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800792MODULE_FIRMWARE(FIRMWARE_8411_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Francois Romieuda78dbf2012-01-26 14:18:23 +0100794static void rtl_lock_work(struct rtl8169_private *tp)
795{
796 mutex_lock(&tp->wk.mutex);
797}
798
799static void rtl_unlock_work(struct rtl8169_private *tp)
800{
801 mutex_unlock(&tp->wk.mutex);
802}
803
Francois Romieud58d46b2011-05-03 16:38:29 +0200804static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
805{
806 int cap = pci_pcie_cap(pdev);
807
808 if (cap) {
809 u16 ctl;
810
811 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
812 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
813 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
814 }
815}
816
françois romieub646d902011-01-03 15:08:21 +0000817static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
818{
819 void __iomem *ioaddr = tp->mmio_addr;
820 int i;
821
822 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
823 for (i = 0; i < 20; i++) {
824 udelay(100);
825 if (RTL_R32(OCPAR) & OCPAR_FLAG)
826 break;
827 }
828 return RTL_R32(OCPDR);
829}
830
831static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
832{
833 void __iomem *ioaddr = tp->mmio_addr;
834 int i;
835
836 RTL_W32(OCPDR, data);
837 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
838 for (i = 0; i < 20; i++) {
839 udelay(100);
840 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
841 break;
842 }
843}
844
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800845static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000846{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800847 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000848 int i;
849
850 RTL_W8(ERIDR, cmd);
851 RTL_W32(ERIAR, 0x800010e8);
852 msleep(2);
853 for (i = 0; i < 5; i++) {
854 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200855 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000856 break;
857 }
858
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800859 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000860}
861
862#define OOB_CMD_RESET 0x00
863#define OOB_CMD_DRIVER_START 0x05
864#define OOB_CMD_DRIVER_STOP 0x06
865
Francois Romieucecb5fd2011-04-01 10:21:07 +0200866static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
867{
868 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
869}
870
françois romieub646d902011-01-03 15:08:21 +0000871static void rtl8168_driver_start(struct rtl8169_private *tp)
872{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200873 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000874 int i;
875
876 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
877
Francois Romieucecb5fd2011-04-01 10:21:07 +0200878 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000879
françois romieub646d902011-01-03 15:08:21 +0000880 for (i = 0; i < 10; i++) {
881 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000882 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000883 break;
884 }
885}
886
887static void rtl8168_driver_stop(struct rtl8169_private *tp)
888{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200889 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000890 int i;
891
892 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
893
Francois Romieucecb5fd2011-04-01 10:21:07 +0200894 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000895
françois romieub646d902011-01-03 15:08:21 +0000896 for (i = 0; i < 10; i++) {
897 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000898 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000899 break;
900 }
901}
902
hayeswang4804b3b2011-03-21 01:50:29 +0000903static int r8168dp_check_dash(struct rtl8169_private *tp)
904{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200905 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000906
Francois Romieucecb5fd2011-04-01 10:21:07 +0200907 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000908}
françois romieub646d902011-01-03 15:08:21 +0000909
françois romieu4da19632011-01-03 15:07:55 +0000910static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int i;
913
Francois Romieua6baf3a2007-11-08 23:23:21 +0100914 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Francois Romieu23714082006-01-29 00:49:09 +0100916 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100917 /*
918 * Check if the RTL8169 has completed writing to the specified
919 * MII register.
920 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200921 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 break;
Francois Romieu23714082006-01-29 00:49:09 +0100923 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700925 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700926 * According to hardware specs a 20us delay is required after write
927 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700928 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700929 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
françois romieu4da19632011-01-03 15:07:55 +0000932static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 int i, value = -1;
935
Francois Romieua6baf3a2007-11-08 23:23:21 +0100936 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Francois Romieu23714082006-01-29 00:49:09 +0100938 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100939 /*
940 * Check if the RTL8169 has completed retrieving data from
941 * the specified MII register.
942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100944 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
946 }
Francois Romieu23714082006-01-29 00:49:09 +0100947 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700949 /*
950 * According to hardware specs a 20us delay is required after read
951 * complete indication, but before sending next command.
952 */
953 udelay(20);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return value;
956}
957
françois romieuc0e45c12011-01-03 15:08:04 +0000958static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
959{
960 int i;
961
962 RTL_W32(OCPDR, data |
963 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
964 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
965 RTL_W32(EPHY_RXER_NUM, 0);
966
967 for (i = 0; i < 100; i++) {
968 mdelay(1);
969 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
970 break;
971 }
972}
973
974static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
975{
976 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
977 (value & OCPDR_DATA_MASK));
978}
979
980static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
981{
982 int i;
983
984 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
985
986 mdelay(1);
987 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
988 RTL_W32(EPHY_RXER_NUM, 0);
989
990 for (i = 0; i < 100; i++) {
991 mdelay(1);
992 if (RTL_R32(OCPAR) & OCPAR_FLAG)
993 break;
994 }
995
996 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
997}
998
françois romieue6de30d2011-01-03 15:08:37 +0000999#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1000
1001static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1002{
1003 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1004}
1005
1006static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1007{
1008 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1009}
1010
1011static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1012{
1013 r8168dp_2_mdio_start(ioaddr);
1014
1015 r8169_mdio_write(ioaddr, reg_addr, value);
1016
1017 r8168dp_2_mdio_stop(ioaddr);
1018}
1019
1020static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1021{
1022 int value;
1023
1024 r8168dp_2_mdio_start(ioaddr);
1025
1026 value = r8169_mdio_read(ioaddr, reg_addr);
1027
1028 r8168dp_2_mdio_stop(ioaddr);
1029
1030 return value;
1031}
1032
françois romieu4da19632011-01-03 15:07:55 +00001033static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001034{
françois romieuc0e45c12011-01-03 15:08:04 +00001035 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001036}
1037
françois romieu4da19632011-01-03 15:07:55 +00001038static int rtl_readphy(struct rtl8169_private *tp, int location)
1039{
françois romieuc0e45c12011-01-03 15:08:04 +00001040 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001041}
1042
1043static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1044{
1045 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1046}
1047
1048static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001049{
1050 int val;
1051
françois romieu4da19632011-01-03 15:07:55 +00001052 val = rtl_readphy(tp, reg_addr);
1053 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001054}
1055
Francois Romieuccdffb92008-07-26 14:26:06 +02001056static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1057 int val)
1058{
1059 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001060
françois romieu4da19632011-01-03 15:07:55 +00001061 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001062}
1063
1064static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1065{
1066 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001067
françois romieu4da19632011-01-03 15:07:55 +00001068 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001069}
1070
Francois Romieudacf8152008-08-02 20:44:13 +02001071static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1072{
1073 unsigned int i;
1074
1075 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1076 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1077
1078 for (i = 0; i < 100; i++) {
1079 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1080 break;
1081 udelay(10);
1082 }
1083}
1084
1085static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1086{
1087 u16 value = 0xffff;
1088 unsigned int i;
1089
1090 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1091
1092 for (i = 0; i < 100; i++) {
1093 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1094 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1095 break;
1096 }
1097 udelay(10);
1098 }
1099
1100 return value;
1101}
1102
Hayes Wang133ac402011-07-06 15:58:05 +08001103static
1104void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1105{
1106 unsigned int i;
1107
1108 BUG_ON((addr & 3) || (mask == 0));
1109 RTL_W32(ERIDR, val);
1110 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1111
1112 for (i = 0; i < 100; i++) {
1113 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1114 break;
1115 udelay(100);
1116 }
1117}
1118
1119static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1120{
1121 u32 value = ~0x00;
1122 unsigned int i;
1123
1124 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1125
1126 for (i = 0; i < 100; i++) {
1127 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1128 value = RTL_R32(ERIDR);
1129 break;
1130 }
1131 udelay(100);
1132 }
1133
1134 return value;
1135}
1136
1137static void
1138rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1139{
1140 u32 val;
1141
1142 val = rtl_eri_read(ioaddr, addr, type);
1143 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1144}
1145
françois romieuc28aa382011-08-02 03:53:43 +00001146struct exgmac_reg {
1147 u16 addr;
1148 u16 mask;
1149 u32 val;
1150};
1151
1152static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1153 const struct exgmac_reg *r, int len)
1154{
1155 while (len-- > 0) {
1156 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1157 r++;
1158 }
1159}
1160
françois romieudaf9df62009-10-07 12:44:20 +00001161static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1162{
1163 u8 value = 0xff;
1164 unsigned int i;
1165
1166 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1167
1168 for (i = 0; i < 300; i++) {
1169 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1170 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1171 break;
1172 }
1173 udelay(100);
1174 }
1175
1176 return value;
1177}
1178
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001179static u16 rtl_get_events(struct rtl8169_private *tp)
1180{
1181 void __iomem *ioaddr = tp->mmio_addr;
1182
1183 return RTL_R16(IntrStatus);
1184}
1185
1186static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1187{
1188 void __iomem *ioaddr = tp->mmio_addr;
1189
1190 RTL_W16(IntrStatus, bits);
1191 mmiowb();
1192}
1193
1194static void rtl_irq_disable(struct rtl8169_private *tp)
1195{
1196 void __iomem *ioaddr = tp->mmio_addr;
1197
1198 RTL_W16(IntrMask, 0);
1199 mmiowb();
1200}
1201
Francois Romieu3e990ff2012-01-26 12:50:01 +01001202static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1203{
1204 void __iomem *ioaddr = tp->mmio_addr;
1205
1206 RTL_W16(IntrMask, bits);
1207}
1208
Francois Romieuda78dbf2012-01-26 14:18:23 +01001209#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1210#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1211#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1212
1213static void rtl_irq_enable_all(struct rtl8169_private *tp)
1214{
1215 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1216}
1217
françois romieu811fd302011-12-04 20:30:45 +00001218static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
françois romieu811fd302011-12-04 20:30:45 +00001220 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001222 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001223 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001224 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
françois romieu4da19632011-01-03 15:07:55 +00001227static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
françois romieu4da19632011-01-03 15:07:55 +00001229 void __iomem *ioaddr = tp->mmio_addr;
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return RTL_R32(TBICSR) & TBIReset;
1232}
1233
françois romieu4da19632011-01-03 15:07:55 +00001234static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
françois romieu4da19632011-01-03 15:07:55 +00001236 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1240{
1241 return RTL_R32(TBICSR) & TBILinkOk;
1242}
1243
1244static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1245{
1246 return RTL_R8(PHYstatus) & LinkStatus;
1247}
1248
françois romieu4da19632011-01-03 15:07:55 +00001249static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
françois romieu4da19632011-01-03 15:07:55 +00001251 void __iomem *ioaddr = tp->mmio_addr;
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1254}
1255
françois romieu4da19632011-01-03 15:07:55 +00001256static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 unsigned int val;
1259
françois romieu4da19632011-01-03 15:07:55 +00001260 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1261 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Hayes Wang70090422011-07-06 15:58:06 +08001264static void rtl_link_chg_patch(struct rtl8169_private *tp)
1265{
1266 void __iomem *ioaddr = tp->mmio_addr;
1267 struct net_device *dev = tp->dev;
1268
1269 if (!netif_running(dev))
1270 return;
1271
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001272 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1273 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Hayes Wang70090422011-07-06 15:58:06 +08001274 if (RTL_R8(PHYstatus) & _1000bpsF) {
1275 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1276 0x00000011, ERIAR_EXGMAC);
1277 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1278 0x00000005, ERIAR_EXGMAC);
1279 } else if (RTL_R8(PHYstatus) & _100bps) {
1280 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1281 0x0000001f, ERIAR_EXGMAC);
1282 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1283 0x00000005, ERIAR_EXGMAC);
1284 } else {
1285 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1286 0x0000001f, ERIAR_EXGMAC);
1287 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1288 0x0000003f, ERIAR_EXGMAC);
1289 }
1290 /* Reset packet filter */
1291 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1292 ERIAR_EXGMAC);
1293 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1294 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001295 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1296 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1297 if (RTL_R8(PHYstatus) & _1000bpsF) {
1298 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1299 0x00000011, ERIAR_EXGMAC);
1300 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1301 0x00000005, ERIAR_EXGMAC);
1302 } else {
1303 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1304 0x0000001f, ERIAR_EXGMAC);
1305 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1306 0x0000003f, ERIAR_EXGMAC);
1307 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001308 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1309 if (RTL_R8(PHYstatus) & _10bps) {
1310 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1311 0x4d02, ERIAR_EXGMAC);
1312 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_0011,
1313 0x0060, ERIAR_EXGMAC);
1314 } else {
1315 rtl_eri_write(ioaddr, 0x1d0, ERIAR_MASK_0011,
1316 0x0000, ERIAR_EXGMAC);
1317 }
Hayes Wang70090422011-07-06 15:58:06 +08001318 }
1319}
1320
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001321static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001322 struct rtl8169_private *tp,
1323 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001326 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001327 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001328 if (pm)
1329 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001331 if (net_ratelimit())
1332 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001333 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001335 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001336 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001337 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001341static void rtl8169_check_link_status(struct net_device *dev,
1342 struct rtl8169_private *tp,
1343 void __iomem *ioaddr)
1344{
1345 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1346}
1347
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001348#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1349
1350static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1351{
1352 void __iomem *ioaddr = tp->mmio_addr;
1353 u8 options;
1354 u32 wolopts = 0;
1355
1356 options = RTL_R8(Config1);
1357 if (!(options & PMEnable))
1358 return 0;
1359
1360 options = RTL_R8(Config3);
1361 if (options & LinkUp)
1362 wolopts |= WAKE_PHY;
1363 if (options & MagicPacket)
1364 wolopts |= WAKE_MAGIC;
1365
1366 options = RTL_R8(Config5);
1367 if (options & UWF)
1368 wolopts |= WAKE_UCAST;
1369 if (options & BWF)
1370 wolopts |= WAKE_BCAST;
1371 if (options & MWF)
1372 wolopts |= WAKE_MCAST;
1373
1374 return wolopts;
1375}
1376
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001377static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1378{
1379 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001380
Francois Romieuda78dbf2012-01-26 14:18:23 +01001381 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001383 wol->supported = WAKE_ANY;
1384 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001385
Francois Romieuda78dbf2012-01-26 14:18:23 +01001386 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001387}
1388
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001389static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001390{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001391 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001392 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001393 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394 u32 opt;
1395 u16 reg;
1396 u8 mask;
1397 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398 { WAKE_PHY, Config3, LinkUp },
1399 { WAKE_MAGIC, Config3, MagicPacket },
1400 { WAKE_UCAST, Config5, UWF },
1401 { WAKE_BCAST, Config5, BWF },
1402 { WAKE_MCAST, Config5, MWF },
1403 { WAKE_ANY, Config5, LanWake }
1404 };
Francois Romieu851e6022012-04-17 11:10:11 +02001405 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001406
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001407 RTL_W8(Cfg9346, Cfg9346_Unlock);
1408
1409 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
Francois Romieu851e6022012-04-17 11:10:11 +02001410 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001411 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412 options |= cfg[i].mask;
1413 RTL_W8(cfg[i].reg, options);
1414 }
1415
Francois Romieu851e6022012-04-17 11:10:11 +02001416 switch (tp->mac_version) {
1417 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1418 options = RTL_R8(Config1) & ~PMEnable;
1419 if (wolopts)
1420 options |= PMEnable;
1421 RTL_W8(Config1, options);
1422 break;
1423 default:
Francois Romieud387b422012-04-17 11:12:01 +02001424 options = RTL_R8(Config2) & ~PME_SIGNAL;
1425 if (wolopts)
1426 options |= PME_SIGNAL;
1427 RTL_W8(Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001428 break;
1429 }
1430
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001431 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001432}
1433
1434static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1435{
1436 struct rtl8169_private *tp = netdev_priv(dev);
1437
Francois Romieuda78dbf2012-01-26 14:18:23 +01001438 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001439
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001440 if (wol->wolopts)
1441 tp->features |= RTL_FEATURE_WOL;
1442 else
1443 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001444 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001445
1446 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001447
françois romieuea809072010-11-08 13:23:58 +00001448 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1449
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001450 return 0;
1451}
1452
Francois Romieu31bd2042011-04-26 18:58:59 +02001453static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1454{
Francois Romieu85bffe62011-04-27 08:22:39 +02001455 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458static void rtl8169_get_drvinfo(struct net_device *dev,
1459 struct ethtool_drvinfo *info)
1460{
1461 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001462 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Rick Jones68aad782011-11-07 13:29:27 +00001464 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1465 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1466 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001467 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001468 if (!IS_ERR_OR_NULL(rtl_fw))
1469 strlcpy(info->fw_version, rtl_fw->version,
1470 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
1473static int rtl8169_get_regs_len(struct net_device *dev)
1474{
1475 return R8169_REGS_SIZE;
1476}
1477
1478static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001479 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct rtl8169_private *tp = netdev_priv(dev);
1482 void __iomem *ioaddr = tp->mmio_addr;
1483 int ret = 0;
1484 u32 reg;
1485
1486 reg = RTL_R32(TBICSR);
1487 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1488 (duplex == DUPLEX_FULL)) {
1489 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1490 } else if (autoneg == AUTONEG_ENABLE)
1491 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1492 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001493 netif_warn(tp, link, dev,
1494 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ret = -EOPNOTSUPP;
1496 }
1497
1498 return ret;
1499}
1500
1501static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001502 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001505 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001506 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Hayes Wang716b50a2011-02-22 17:26:18 +08001508 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001511 int auto_nego;
1512
françois romieu4da19632011-01-03 15:07:55 +00001513 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001514 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1515 ADVERTISE_100HALF | ADVERTISE_100FULL);
1516
1517 if (adv & ADVERTISED_10baseT_Half)
1518 auto_nego |= ADVERTISE_10HALF;
1519 if (adv & ADVERTISED_10baseT_Full)
1520 auto_nego |= ADVERTISE_10FULL;
1521 if (adv & ADVERTISED_100baseT_Half)
1522 auto_nego |= ADVERTISE_100HALF;
1523 if (adv & ADVERTISED_100baseT_Full)
1524 auto_nego |= ADVERTISE_100FULL;
1525
françois romieu3577aa12009-05-19 10:46:48 +00001526 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1527
françois romieu4da19632011-01-03 15:07:55 +00001528 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001529 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1530
1531 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001532 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001533 if (adv & ADVERTISED_1000baseT_Half)
1534 giga_ctrl |= ADVERTISE_1000HALF;
1535 if (adv & ADVERTISED_1000baseT_Full)
1536 giga_ctrl |= ADVERTISE_1000FULL;
1537 } else if (adv & (ADVERTISED_1000baseT_Half |
1538 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001539 netif_info(tp, link, dev,
1540 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001541 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
françois romieu3577aa12009-05-19 10:46:48 +00001544 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001545
françois romieu4da19632011-01-03 15:07:55 +00001546 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1547 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001548 } else {
1549 giga_ctrl = 0;
1550
1551 if (speed == SPEED_10)
1552 bmcr = 0;
1553 else if (speed == SPEED_100)
1554 bmcr = BMCR_SPEED100;
1555 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001556 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001557
1558 if (duplex == DUPLEX_FULL)
1559 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001560 }
1561
françois romieu4da19632011-01-03 15:07:55 +00001562 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001563
Francois Romieucecb5fd2011-04-01 10:21:07 +02001564 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1565 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001566 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001567 rtl_writephy(tp, 0x17, 0x2138);
1568 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001569 } else {
françois romieu4da19632011-01-03 15:07:55 +00001570 rtl_writephy(tp, 0x17, 0x2108);
1571 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001572 }
1573 }
1574
Oliver Neukum54405cd2011-01-06 21:55:13 +01001575 rc = 0;
1576out:
1577 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001581 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct rtl8169_private *tp = netdev_priv(dev);
1584 int ret;
1585
Oliver Neukum54405cd2011-01-06 21:55:13 +01001586 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001587 if (ret < 0)
1588 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Francois Romieu4876cc12011-03-11 21:07:11 +01001590 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1591 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001593 }
1594out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return ret;
1596}
1597
1598static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1599{
1600 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int ret;
1602
Francois Romieu4876cc12011-03-11 21:07:11 +01001603 del_timer_sync(&tp->timer);
1604
Francois Romieuda78dbf2012-01-26 14:18:23 +01001605 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001606 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001607 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001608 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return ret;
1611}
1612
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001613static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1614 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Francois Romieud58d46b2011-05-03 16:38:29 +02001616 struct rtl8169_private *tp = netdev_priv(dev);
1617
Francois Romieu2b7b4312011-04-18 22:53:24 -07001618 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001619 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Francois Romieud58d46b2011-05-03 16:38:29 +02001621 if (dev->mtu > JUMBO_1K &&
1622 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1623 features &= ~NETIF_F_IP_CSUM;
1624
Michał Mirosław350fb322011-04-08 06:35:56 +00001625 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Francois Romieuda78dbf2012-01-26 14:18:23 +01001628static void __rtl8169_set_features(struct net_device *dev,
1629 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001632 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001633 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Ben Greear6bbe0212012-02-10 15:04:33 +00001635 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1636 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Ben Greear6bbe0212012-02-10 15:04:33 +00001638 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1639 if (features & NETIF_F_RXCSUM)
1640 tp->cp_cmd |= RxChkSum;
1641 else
1642 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001643
Ben Greear6bbe0212012-02-10 15:04:33 +00001644 if (dev->features & NETIF_F_HW_VLAN_RX)
1645 tp->cp_cmd |= RxVlan;
1646 else
1647 tp->cp_cmd &= ~RxVlan;
1648
1649 RTL_W16(CPlusCmd, tp->cp_cmd);
1650 RTL_R16(CPlusCmd);
1651 }
1652 if (changed & NETIF_F_RXALL) {
1653 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1654 if (features & NETIF_F_RXALL)
1655 tmp |= (AcceptErr | AcceptRunt);
1656 RTL_W32(RxConfig, tmp);
1657 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Francois Romieuda78dbf2012-01-26 14:18:23 +01001660static int rtl8169_set_features(struct net_device *dev,
1661 netdev_features_t features)
1662{
1663 struct rtl8169_private *tp = netdev_priv(dev);
1664
1665 rtl_lock_work(tp);
1666 __rtl8169_set_features(dev, features);
1667 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 return 0;
1670}
1671
Francois Romieuda78dbf2012-01-26 14:18:23 +01001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1674 struct sk_buff *skb)
1675{
Jesse Grosseab6d182010-10-20 13:56:03 +00001676 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1678}
1679
Francois Romieu7a8fc772011-03-01 17:18:33 +01001680static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Francois Romieu7a8fc772011-03-01 17:18:33 +01001684 if (opts2 & RxVlanTag)
1685 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Francois Romieuccdffb92008-07-26 14:26:06 +02001690static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 struct rtl8169_private *tp = netdev_priv(dev);
1693 void __iomem *ioaddr = tp->mmio_addr;
1694 u32 status;
1695
1696 cmd->supported =
1697 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1698 cmd->port = PORT_FIBRE;
1699 cmd->transceiver = XCVR_INTERNAL;
1700
1701 status = RTL_R32(TBICSR);
1702 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1703 cmd->autoneg = !!(status & TBINwEnable);
1704
David Decotigny70739492011-04-27 18:32:40 +00001705 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001707
1708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Francois Romieuccdffb92008-07-26 14:26:06 +02001711static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Francois Romieuccdffb92008-07-26 14:26:06 +02001715 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1719{
1720 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001721 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Francois Romieuda78dbf2012-01-26 14:18:23 +01001723 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001724 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001725 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Francois Romieuccdffb92008-07-26 14:26:06 +02001727 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1731 void *p)
1732{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001733 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Francois Romieu5b0384f2006-08-16 16:00:01 +02001735 if (regs->len > R8169_REGS_SIZE)
1736 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Francois Romieuda78dbf2012-01-26 14:18:23 +01001738 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001739 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001740 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001743static u32 rtl8169_get_msglevel(struct net_device *dev)
1744{
1745 struct rtl8169_private *tp = netdev_priv(dev);
1746
1747 return tp->msg_enable;
1748}
1749
1750static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1751{
1752 struct rtl8169_private *tp = netdev_priv(dev);
1753
1754 tp->msg_enable = value;
1755}
1756
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001757static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1758 "tx_packets",
1759 "rx_packets",
1760 "tx_errors",
1761 "rx_errors",
1762 "rx_missed",
1763 "align_errors",
1764 "tx_single_collisions",
1765 "tx_multi_collisions",
1766 "unicast",
1767 "broadcast",
1768 "multicast",
1769 "tx_aborted",
1770 "tx_underrun",
1771};
1772
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001773static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001774{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001775 switch (sset) {
1776 case ETH_SS_STATS:
1777 return ARRAY_SIZE(rtl8169_gstrings);
1778 default:
1779 return -EOPNOTSUPP;
1780 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781}
1782
Ivan Vecera355423d2009-02-06 21:49:57 -08001783static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001784{
1785 struct rtl8169_private *tp = netdev_priv(dev);
1786 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001787 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001788 struct rtl8169_counters *counters;
1789 dma_addr_t paddr;
1790 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001791 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792
Ivan Vecera355423d2009-02-06 21:49:57 -08001793 /*
1794 * Some chips are unable to dump tally counters when the receiver
1795 * is disabled.
1796 */
1797 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1798 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001800 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001801 if (!counters)
1802 return;
1803
1804 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001805 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001806 RTL_W32(CounterAddrLow, cmd);
1807 RTL_W32(CounterAddrLow, cmd | CounterDump);
1808
Ivan Vecera355423d2009-02-06 21:49:57 -08001809 while (wait--) {
1810 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001811 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001812 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001813 }
1814 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001815 }
1816
1817 RTL_W32(CounterAddrLow, 0);
1818 RTL_W32(CounterAddrHigh, 0);
1819
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001820 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001821}
1822
Ivan Vecera355423d2009-02-06 21:49:57 -08001823static void rtl8169_get_ethtool_stats(struct net_device *dev,
1824 struct ethtool_stats *stats, u64 *data)
1825{
1826 struct rtl8169_private *tp = netdev_priv(dev);
1827
1828 ASSERT_RTNL();
1829
1830 rtl8169_update_counters(dev);
1831
1832 data[0] = le64_to_cpu(tp->counters.tx_packets);
1833 data[1] = le64_to_cpu(tp->counters.rx_packets);
1834 data[2] = le64_to_cpu(tp->counters.tx_errors);
1835 data[3] = le32_to_cpu(tp->counters.rx_errors);
1836 data[4] = le16_to_cpu(tp->counters.rx_missed);
1837 data[5] = le16_to_cpu(tp->counters.align_errors);
1838 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1839 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1840 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1841 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1842 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1843 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1844 data[12] = le16_to_cpu(tp->counters.tx_underun);
1845}
1846
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001847static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1848{
1849 switch(stringset) {
1850 case ETH_SS_STATS:
1851 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1852 break;
1853 }
1854}
1855
Jeff Garzik7282d492006-09-13 14:30:00 -04001856static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 .get_drvinfo = rtl8169_get_drvinfo,
1858 .get_regs_len = rtl8169_get_regs_len,
1859 .get_link = ethtool_op_get_link,
1860 .get_settings = rtl8169_get_settings,
1861 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001862 .get_msglevel = rtl8169_get_msglevel,
1863 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001865 .get_wol = rtl8169_get_wol,
1866 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001867 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001868 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001869 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00001870 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871};
1872
Francois Romieu07d3f512007-02-21 22:40:46 +01001873static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001874 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Francois Romieu5d320a22011-05-08 17:47:36 +02001876 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001877 /*
1878 * The driver currently handles the 8168Bf and the 8168Be identically
1879 * but they can be identified more specifically through the test below
1880 * if needed:
1881 *
1882 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001883 *
1884 * Same thing for the 8101Eb and the 8101Ec:
1885 *
1886 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001887 */
Francois Romieu37441002011-06-17 22:58:54 +02001888 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001890 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 int mac_version;
1892 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001893 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001894 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08001895 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1896 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1897
hayeswang01dc7fe2011-03-21 01:50:28 +00001898 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001899 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001900 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1901 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1902 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1903
Francois Romieu5b538df2008-07-20 16:22:45 +02001904 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001905 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1906 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001907 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001908
françois romieue6de30d2011-01-03 15:08:37 +00001909 /* 8168DP family. */
1910 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1911 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001912 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001913
Francois Romieuef808d52008-06-29 13:10:54 +02001914 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001915 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001916 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001917 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001918 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001919 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1920 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001921 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001922 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001923 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001924
1925 /* 8168B family. */
1926 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1927 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1928 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1929 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1930
1931 /* 8101 family. */
Hayes Wang7e18dca2012-03-30 14:33:02 +08001932 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
hayeswang36a0e6c2011-03-21 01:50:30 +00001933 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001934 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1935 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1936 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001937 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1938 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1939 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1940 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1941 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1942 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001943 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001944 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001945 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001946 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1947 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001948 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1949 /* FIXME: where did these entries come from ? -- FR */
1950 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1951 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1952
1953 /* 8110 family. */
1954 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1955 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1956 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1957 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1958 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1959 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1960
Jean Delvaref21b75e2009-05-26 20:54:48 -07001961 /* Catch-all */
1962 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001963 };
1964 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 u32 reg;
1966
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001967 reg = RTL_R32(TxConfig);
1968 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 p++;
1970 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001971
1972 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1973 netif_notice(tp, probe, dev,
1974 "unknown MAC, using family default\n");
1975 tp->mac_version = default_version;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1980{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001981 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Francois Romieu867763c2007-08-17 18:21:58 +02001984struct phy_reg {
1985 u16 reg;
1986 u16 val;
1987};
1988
françois romieu4da19632011-01-03 15:07:55 +00001989static void rtl_writephy_batch(struct rtl8169_private *tp,
1990 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001991{
1992 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001993 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001994 regs++;
1995 }
1996}
1997
françois romieubca03d52011-01-03 15:07:31 +00001998#define PHY_READ 0x00000000
1999#define PHY_DATA_OR 0x10000000
2000#define PHY_DATA_AND 0x20000000
2001#define PHY_BJMPN 0x30000000
2002#define PHY_READ_EFUSE 0x40000000
2003#define PHY_READ_MAC_BYTE 0x50000000
2004#define PHY_WRITE_MAC_BYTE 0x60000000
2005#define PHY_CLEAR_READCOUNT 0x70000000
2006#define PHY_WRITE 0x80000000
2007#define PHY_READCOUNT_EQ_SKIP 0x90000000
2008#define PHY_COMP_EQ_SKIPN 0xa0000000
2009#define PHY_COMP_NEQ_SKIPN 0xb0000000
2010#define PHY_WRITE_PREVIOUS 0xc0000000
2011#define PHY_SKIPN 0xd0000000
2012#define PHY_DELAY_MS 0xe0000000
2013#define PHY_WRITE_ERI_WORD 0xf0000000
2014
Hayes Wang960aee62011-06-18 11:37:48 +02002015struct fw_info {
2016 u32 magic;
2017 char version[RTL_VER_SIZE];
2018 __le32 fw_start;
2019 __le32 fw_len;
2020 u8 chksum;
2021} __packed;
2022
Francois Romieu1c361ef2011-06-17 17:16:24 +02002023#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2024
2025static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002026{
Francois Romieub6ffd972011-06-17 17:00:05 +02002027 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002028 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002029 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2030 char *version = rtl_fw->version;
2031 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002032
Francois Romieu1c361ef2011-06-17 17:16:24 +02002033 if (fw->size < FW_OPCODE_SIZE)
2034 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002035
2036 if (!fw_info->magic) {
2037 size_t i, size, start;
2038 u8 checksum = 0;
2039
2040 if (fw->size < sizeof(*fw_info))
2041 goto out;
2042
2043 for (i = 0; i < fw->size; i++)
2044 checksum += fw->data[i];
2045 if (checksum != 0)
2046 goto out;
2047
2048 start = le32_to_cpu(fw_info->fw_start);
2049 if (start > fw->size)
2050 goto out;
2051
2052 size = le32_to_cpu(fw_info->fw_len);
2053 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2054 goto out;
2055
2056 memcpy(version, fw_info->version, RTL_VER_SIZE);
2057
2058 pa->code = (__le32 *)(fw->data + start);
2059 pa->size = size;
2060 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002061 if (fw->size % FW_OPCODE_SIZE)
2062 goto out;
2063
2064 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2065
2066 pa->code = (__le32 *)fw->data;
2067 pa->size = fw->size / FW_OPCODE_SIZE;
2068 }
2069 version[RTL_VER_SIZE - 1] = 0;
2070
2071 rc = true;
2072out:
2073 return rc;
2074}
2075
Francois Romieufd112f22011-06-18 00:10:29 +02002076static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2077 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002078{
Francois Romieufd112f22011-06-18 00:10:29 +02002079 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002080 size_t index;
2081
Francois Romieu1c361ef2011-06-17 17:16:24 +02002082 for (index = 0; index < pa->size; index++) {
2083 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002084 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002085
hayeswang42b82dc2011-01-10 02:07:25 +00002086 switch(action & 0xf0000000) {
2087 case PHY_READ:
2088 case PHY_DATA_OR:
2089 case PHY_DATA_AND:
2090 case PHY_READ_EFUSE:
2091 case PHY_CLEAR_READCOUNT:
2092 case PHY_WRITE:
2093 case PHY_WRITE_PREVIOUS:
2094 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002095 break;
2096
hayeswang42b82dc2011-01-10 02:07:25 +00002097 case PHY_BJMPN:
2098 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002099 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002100 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002101 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002102 }
2103 break;
2104 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002105 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002106 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002107 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002108 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002109 }
2110 break;
2111 case PHY_COMP_EQ_SKIPN:
2112 case PHY_COMP_NEQ_SKIPN:
2113 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002114 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002115 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002116 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002117 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002118 }
2119 break;
2120
2121 case PHY_READ_MAC_BYTE:
2122 case PHY_WRITE_MAC_BYTE:
2123 case PHY_WRITE_ERI_WORD:
2124 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002125 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002126 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002127 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002128 }
2129 }
Francois Romieufd112f22011-06-18 00:10:29 +02002130 rc = true;
2131out:
2132 return rc;
2133}
françois romieubca03d52011-01-03 15:07:31 +00002134
Francois Romieufd112f22011-06-18 00:10:29 +02002135static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2136{
2137 struct net_device *dev = tp->dev;
2138 int rc = -EINVAL;
2139
2140 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2141 netif_err(tp, ifup, dev, "invalid firwmare\n");
2142 goto out;
2143 }
2144
2145 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2146 rc = 0;
2147out:
2148 return rc;
2149}
2150
2151static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2152{
2153 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2154 u32 predata, count;
2155 size_t index;
2156
2157 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002158
Francois Romieu1c361ef2011-06-17 17:16:24 +02002159 for (index = 0; index < pa->size; ) {
2160 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002161 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002162 u32 regno = (action & 0x0fff0000) >> 16;
2163
2164 if (!action)
2165 break;
françois romieubca03d52011-01-03 15:07:31 +00002166
2167 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002168 case PHY_READ:
2169 predata = rtl_readphy(tp, regno);
2170 count++;
2171 index++;
françois romieubca03d52011-01-03 15:07:31 +00002172 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002173 case PHY_DATA_OR:
2174 predata |= data;
2175 index++;
2176 break;
2177 case PHY_DATA_AND:
2178 predata &= data;
2179 index++;
2180 break;
2181 case PHY_BJMPN:
2182 index -= regno;
2183 break;
2184 case PHY_READ_EFUSE:
2185 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2186 index++;
2187 break;
2188 case PHY_CLEAR_READCOUNT:
2189 count = 0;
2190 index++;
2191 break;
2192 case PHY_WRITE:
2193 rtl_writephy(tp, regno, data);
2194 index++;
2195 break;
2196 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002197 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002198 break;
2199 case PHY_COMP_EQ_SKIPN:
2200 if (predata == data)
2201 index += regno;
2202 index++;
2203 break;
2204 case PHY_COMP_NEQ_SKIPN:
2205 if (predata != data)
2206 index += regno;
2207 index++;
2208 break;
2209 case PHY_WRITE_PREVIOUS:
2210 rtl_writephy(tp, regno, predata);
2211 index++;
2212 break;
2213 case PHY_SKIPN:
2214 index += regno + 1;
2215 break;
2216 case PHY_DELAY_MS:
2217 mdelay(data);
2218 index++;
2219 break;
2220
2221 case PHY_READ_MAC_BYTE:
2222 case PHY_WRITE_MAC_BYTE:
2223 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002224 default:
2225 BUG();
2226 }
2227 }
2228}
2229
françois romieuf1e02ed2011-01-13 13:07:53 +00002230static void rtl_release_firmware(struct rtl8169_private *tp)
2231{
Francois Romieub6ffd972011-06-17 17:00:05 +02002232 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2233 release_firmware(tp->rtl_fw->fw);
2234 kfree(tp->rtl_fw);
2235 }
2236 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002237}
2238
François Romieu953a12c2011-04-24 17:38:48 +02002239static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002240{
Francois Romieub6ffd972011-06-17 17:00:05 +02002241 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002242
2243 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002244 if (!IS_ERR_OR_NULL(rtl_fw))
2245 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002246}
2247
2248static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2249{
2250 if (rtl_readphy(tp, reg) != val)
2251 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2252 else
2253 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002254}
2255
françois romieu4da19632011-01-03 15:07:55 +00002256static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002258 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002259 { 0x1f, 0x0001 },
2260 { 0x06, 0x006e },
2261 { 0x08, 0x0708 },
2262 { 0x15, 0x4000 },
2263 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
françois romieu0b9b5712009-08-10 19:44:56 +00002265 { 0x1f, 0x0001 },
2266 { 0x03, 0x00a1 },
2267 { 0x02, 0x0008 },
2268 { 0x01, 0x0120 },
2269 { 0x00, 0x1000 },
2270 { 0x04, 0x0800 },
2271 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
françois romieu0b9b5712009-08-10 19:44:56 +00002273 { 0x03, 0xff41 },
2274 { 0x02, 0xdf60 },
2275 { 0x01, 0x0140 },
2276 { 0x00, 0x0077 },
2277 { 0x04, 0x7800 },
2278 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
françois romieu0b9b5712009-08-10 19:44:56 +00002280 { 0x03, 0x802f },
2281 { 0x02, 0x4f02 },
2282 { 0x01, 0x0409 },
2283 { 0x00, 0xf0f9 },
2284 { 0x04, 0x9800 },
2285 { 0x04, 0x9000 },
2286
2287 { 0x03, 0xdf01 },
2288 { 0x02, 0xdf20 },
2289 { 0x01, 0xff95 },
2290 { 0x00, 0xba00 },
2291 { 0x04, 0xa800 },
2292 { 0x04, 0xa000 },
2293
2294 { 0x03, 0xff41 },
2295 { 0x02, 0xdf20 },
2296 { 0x01, 0x0140 },
2297 { 0x00, 0x00bb },
2298 { 0x04, 0xb800 },
2299 { 0x04, 0xb000 },
2300
2301 { 0x03, 0xdf41 },
2302 { 0x02, 0xdc60 },
2303 { 0x01, 0x6340 },
2304 { 0x00, 0x007d },
2305 { 0x04, 0xd800 },
2306 { 0x04, 0xd000 },
2307
2308 { 0x03, 0xdf01 },
2309 { 0x02, 0xdf20 },
2310 { 0x01, 0x100a },
2311 { 0x00, 0xa0ff },
2312 { 0x04, 0xf800 },
2313 { 0x04, 0xf000 },
2314
2315 { 0x1f, 0x0000 },
2316 { 0x0b, 0x0000 },
2317 { 0x00, 0x9200 }
2318 };
2319
françois romieu4da19632011-01-03 15:07:55 +00002320 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
françois romieu4da19632011-01-03 15:07:55 +00002323static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002324{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002325 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002326 { 0x1f, 0x0002 },
2327 { 0x01, 0x90d0 },
2328 { 0x1f, 0x0000 }
2329 };
2330
françois romieu4da19632011-01-03 15:07:55 +00002331 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002332}
2333
françois romieu4da19632011-01-03 15:07:55 +00002334static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002335{
2336 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002337
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002338 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2339 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002340 return;
2341
françois romieu4da19632011-01-03 15:07:55 +00002342 rtl_writephy(tp, 0x1f, 0x0001);
2343 rtl_writephy(tp, 0x10, 0xf01b);
2344 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002345}
2346
françois romieu4da19632011-01-03 15:07:55 +00002347static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002348{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002349 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002350 { 0x1f, 0x0001 },
2351 { 0x04, 0x0000 },
2352 { 0x03, 0x00a1 },
2353 { 0x02, 0x0008 },
2354 { 0x01, 0x0120 },
2355 { 0x00, 0x1000 },
2356 { 0x04, 0x0800 },
2357 { 0x04, 0x9000 },
2358 { 0x03, 0x802f },
2359 { 0x02, 0x4f02 },
2360 { 0x01, 0x0409 },
2361 { 0x00, 0xf099 },
2362 { 0x04, 0x9800 },
2363 { 0x04, 0xa000 },
2364 { 0x03, 0xdf01 },
2365 { 0x02, 0xdf20 },
2366 { 0x01, 0xff95 },
2367 { 0x00, 0xba00 },
2368 { 0x04, 0xa800 },
2369 { 0x04, 0xf000 },
2370 { 0x03, 0xdf01 },
2371 { 0x02, 0xdf20 },
2372 { 0x01, 0x101a },
2373 { 0x00, 0xa0ff },
2374 { 0x04, 0xf800 },
2375 { 0x04, 0x0000 },
2376 { 0x1f, 0x0000 },
2377
2378 { 0x1f, 0x0001 },
2379 { 0x10, 0xf41b },
2380 { 0x14, 0xfb54 },
2381 { 0x18, 0xf5c7 },
2382 { 0x1f, 0x0000 },
2383
2384 { 0x1f, 0x0001 },
2385 { 0x17, 0x0cc0 },
2386 { 0x1f, 0x0000 }
2387 };
2388
françois romieu4da19632011-01-03 15:07:55 +00002389 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002390
françois romieu4da19632011-01-03 15:07:55 +00002391 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002392}
2393
françois romieu4da19632011-01-03 15:07:55 +00002394static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002395{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002396 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002397 { 0x1f, 0x0001 },
2398 { 0x04, 0x0000 },
2399 { 0x03, 0x00a1 },
2400 { 0x02, 0x0008 },
2401 { 0x01, 0x0120 },
2402 { 0x00, 0x1000 },
2403 { 0x04, 0x0800 },
2404 { 0x04, 0x9000 },
2405 { 0x03, 0x802f },
2406 { 0x02, 0x4f02 },
2407 { 0x01, 0x0409 },
2408 { 0x00, 0xf099 },
2409 { 0x04, 0x9800 },
2410 { 0x04, 0xa000 },
2411 { 0x03, 0xdf01 },
2412 { 0x02, 0xdf20 },
2413 { 0x01, 0xff95 },
2414 { 0x00, 0xba00 },
2415 { 0x04, 0xa800 },
2416 { 0x04, 0xf000 },
2417 { 0x03, 0xdf01 },
2418 { 0x02, 0xdf20 },
2419 { 0x01, 0x101a },
2420 { 0x00, 0xa0ff },
2421 { 0x04, 0xf800 },
2422 { 0x04, 0x0000 },
2423 { 0x1f, 0x0000 },
2424
2425 { 0x1f, 0x0001 },
2426 { 0x0b, 0x8480 },
2427 { 0x1f, 0x0000 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x18, 0x67c7 },
2431 { 0x04, 0x2000 },
2432 { 0x03, 0x002f },
2433 { 0x02, 0x4360 },
2434 { 0x01, 0x0109 },
2435 { 0x00, 0x3022 },
2436 { 0x04, 0x2800 },
2437 { 0x1f, 0x0000 },
2438
2439 { 0x1f, 0x0001 },
2440 { 0x17, 0x0cc0 },
2441 { 0x1f, 0x0000 }
2442 };
2443
françois romieu4da19632011-01-03 15:07:55 +00002444 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002445}
2446
françois romieu4da19632011-01-03 15:07:55 +00002447static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002448{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002449 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002450 { 0x10, 0xf41b },
2451 { 0x1f, 0x0000 }
2452 };
2453
françois romieu4da19632011-01-03 15:07:55 +00002454 rtl_writephy(tp, 0x1f, 0x0001);
2455 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002456
françois romieu4da19632011-01-03 15:07:55 +00002457 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002458}
2459
françois romieu4da19632011-01-03 15:07:55 +00002460static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002461{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002462 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002463 { 0x1f, 0x0001 },
2464 { 0x10, 0xf41b },
2465 { 0x1f, 0x0000 }
2466 };
2467
françois romieu4da19632011-01-03 15:07:55 +00002468 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002469}
2470
françois romieu4da19632011-01-03 15:07:55 +00002471static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002472{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002473 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002474 { 0x1f, 0x0000 },
2475 { 0x1d, 0x0f00 },
2476 { 0x1f, 0x0002 },
2477 { 0x0c, 0x1ec8 },
2478 { 0x1f, 0x0000 }
2479 };
2480
françois romieu4da19632011-01-03 15:07:55 +00002481 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002482}
2483
françois romieu4da19632011-01-03 15:07:55 +00002484static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002485{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002486 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002487 { 0x1f, 0x0001 },
2488 { 0x1d, 0x3d98 },
2489 { 0x1f, 0x0000 }
2490 };
2491
françois romieu4da19632011-01-03 15:07:55 +00002492 rtl_writephy(tp, 0x1f, 0x0000);
2493 rtl_patchphy(tp, 0x14, 1 << 5);
2494 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002495
françois romieu4da19632011-01-03 15:07:55 +00002496 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002497}
2498
françois romieu4da19632011-01-03 15:07:55 +00002499static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002500{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002501 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002502 { 0x1f, 0x0001 },
2503 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002504 { 0x1f, 0x0002 },
2505 { 0x00, 0x88d4 },
2506 { 0x01, 0x82b1 },
2507 { 0x03, 0x7002 },
2508 { 0x08, 0x9e30 },
2509 { 0x09, 0x01f0 },
2510 { 0x0a, 0x5500 },
2511 { 0x0c, 0x00c8 },
2512 { 0x1f, 0x0003 },
2513 { 0x12, 0xc096 },
2514 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002515 { 0x1f, 0x0000 },
2516 { 0x1f, 0x0000 },
2517 { 0x09, 0x2000 },
2518 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002519 };
2520
françois romieu4da19632011-01-03 15:07:55 +00002521 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002522
françois romieu4da19632011-01-03 15:07:55 +00002523 rtl_patchphy(tp, 0x14, 1 << 5);
2524 rtl_patchphy(tp, 0x0d, 1 << 5);
2525 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002526}
2527
françois romieu4da19632011-01-03 15:07:55 +00002528static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002529{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002530 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002531 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002532 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002533 { 0x03, 0x802f },
2534 { 0x02, 0x4f02 },
2535 { 0x01, 0x0409 },
2536 { 0x00, 0xf099 },
2537 { 0x04, 0x9800 },
2538 { 0x04, 0x9000 },
2539 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002540 { 0x1f, 0x0002 },
2541 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002542 { 0x06, 0x0761 },
2543 { 0x1f, 0x0003 },
2544 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002545 { 0x1f, 0x0000 }
2546 };
2547
françois romieu4da19632011-01-03 15:07:55 +00002548 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002549
françois romieu4da19632011-01-03 15:07:55 +00002550 rtl_patchphy(tp, 0x16, 1 << 0);
2551 rtl_patchphy(tp, 0x14, 1 << 5);
2552 rtl_patchphy(tp, 0x0d, 1 << 5);
2553 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002554}
2555
françois romieu4da19632011-01-03 15:07:55 +00002556static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002557{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002558 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002559 { 0x1f, 0x0001 },
2560 { 0x12, 0x2300 },
2561 { 0x1d, 0x3d98 },
2562 { 0x1f, 0x0002 },
2563 { 0x0c, 0x7eb8 },
2564 { 0x06, 0x5461 },
2565 { 0x1f, 0x0003 },
2566 { 0x16, 0x0f0a },
2567 { 0x1f, 0x0000 }
2568 };
2569
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002571
françois romieu4da19632011-01-03 15:07:55 +00002572 rtl_patchphy(tp, 0x16, 1 << 0);
2573 rtl_patchphy(tp, 0x14, 1 << 5);
2574 rtl_patchphy(tp, 0x0d, 1 << 5);
2575 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002576}
2577
françois romieu4da19632011-01-03 15:07:55 +00002578static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002579{
françois romieu4da19632011-01-03 15:07:55 +00002580 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002581}
2582
françois romieubca03d52011-01-03 15:07:31 +00002583static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002584{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002585 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002586 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002587 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002588 { 0x06, 0x4064 },
2589 { 0x07, 0x2863 },
2590 { 0x08, 0x059c },
2591 { 0x09, 0x26b4 },
2592 { 0x0a, 0x6a19 },
2593 { 0x0b, 0xdcc8 },
2594 { 0x10, 0xf06d },
2595 { 0x14, 0x7f68 },
2596 { 0x18, 0x7fd9 },
2597 { 0x1c, 0xf0ff },
2598 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002599 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002600 { 0x12, 0xf49f },
2601 { 0x13, 0x070b },
2602 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002603 { 0x14, 0x94c0 },
2604
2605 /*
2606 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002607 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002608 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002609 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002610 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002611 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002612 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002613 { 0x06, 0x5561 },
2614
2615 /*
2616 * Can not link to 1Gbps with bad cable
2617 * Decrease SNR threshold form 21.07dB to 19.04dB
2618 */
2619 { 0x1f, 0x0001 },
2620 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002621
2622 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002623 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002624 };
françois romieubca03d52011-01-03 15:07:31 +00002625 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002626
françois romieu4da19632011-01-03 15:07:55 +00002627 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002628
françois romieubca03d52011-01-03 15:07:31 +00002629 /*
2630 * Rx Error Issue
2631 * Fine Tune Switching regulator parameter
2632 */
françois romieu4da19632011-01-03 15:07:55 +00002633 rtl_writephy(tp, 0x1f, 0x0002);
2634 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2635 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002636
françois romieudaf9df62009-10-07 12:44:20 +00002637 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002638 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002639 { 0x1f, 0x0002 },
2640 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002641 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002642 { 0x05, 0x8330 },
2643 { 0x06, 0x669a },
2644 { 0x1f, 0x0002 }
2645 };
2646 int val;
2647
françois romieu4da19632011-01-03 15:07:55 +00002648 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002649
françois romieu4da19632011-01-03 15:07:55 +00002650 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002651
2652 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002653 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002654 0x0065, 0x0066, 0x0067, 0x0068,
2655 0x0069, 0x006a, 0x006b, 0x006c
2656 };
2657 int i;
2658
françois romieu4da19632011-01-03 15:07:55 +00002659 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002660
2661 val &= 0xff00;
2662 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002664 }
2665 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002666 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002667 { 0x1f, 0x0002 },
2668 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002669 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002670 { 0x05, 0x8330 },
2671 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002672 };
2673
françois romieu4da19632011-01-03 15:07:55 +00002674 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002675 }
2676
françois romieubca03d52011-01-03 15:07:31 +00002677 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy(tp, 0x1f, 0x0002);
2679 rtl_patchphy(tp, 0x0d, 0x0300);
2680 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002681
françois romieubca03d52011-01-03 15:07:31 +00002682 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl_writephy(tp, 0x1f, 0x0002);
2684 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2685 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002686
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x1f, 0x0005);
2688 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002689
2690 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002691
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002693}
2694
françois romieubca03d52011-01-03 15:07:31 +00002695static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002696{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002697 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002698 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002699 { 0x1f, 0x0001 },
2700 { 0x06, 0x4064 },
2701 { 0x07, 0x2863 },
2702 { 0x08, 0x059c },
2703 { 0x09, 0x26b4 },
2704 { 0x0a, 0x6a19 },
2705 { 0x0b, 0xdcc8 },
2706 { 0x10, 0xf06d },
2707 { 0x14, 0x7f68 },
2708 { 0x18, 0x7fd9 },
2709 { 0x1c, 0xf0ff },
2710 { 0x1d, 0x3d9c },
2711 { 0x1f, 0x0003 },
2712 { 0x12, 0xf49f },
2713 { 0x13, 0x070b },
2714 { 0x1a, 0x05ad },
2715 { 0x14, 0x94c0 },
2716
françois romieubca03d52011-01-03 15:07:31 +00002717 /*
2718 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002719 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002720 */
françois romieudaf9df62009-10-07 12:44:20 +00002721 { 0x1f, 0x0002 },
2722 { 0x06, 0x5561 },
2723 { 0x1f, 0x0005 },
2724 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002725 { 0x06, 0x5561 },
2726
2727 /*
2728 * Can not link to 1Gbps with bad cable
2729 * Decrease SNR threshold form 21.07dB to 19.04dB
2730 */
2731 { 0x1f, 0x0001 },
2732 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002733
2734 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002735 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002736 };
françois romieubca03d52011-01-03 15:07:31 +00002737 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002738
françois romieu4da19632011-01-03 15:07:55 +00002739 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002740
2741 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002742 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002743 { 0x1f, 0x0002 },
2744 { 0x05, 0x669a },
2745 { 0x1f, 0x0005 },
2746 { 0x05, 0x8330 },
2747 { 0x06, 0x669a },
2748
2749 { 0x1f, 0x0002 }
2750 };
2751 int val;
2752
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002754
françois romieu4da19632011-01-03 15:07:55 +00002755 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002756 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002757 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002758 0x0065, 0x0066, 0x0067, 0x0068,
2759 0x0069, 0x006a, 0x006b, 0x006c
2760 };
2761 int i;
2762
françois romieu4da19632011-01-03 15:07:55 +00002763 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002764
2765 val &= 0xff00;
2766 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002768 }
2769 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002770 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002771 { 0x1f, 0x0002 },
2772 { 0x05, 0x2642 },
2773 { 0x1f, 0x0005 },
2774 { 0x05, 0x8330 },
2775 { 0x06, 0x2642 }
2776 };
2777
françois romieu4da19632011-01-03 15:07:55 +00002778 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002779 }
2780
françois romieubca03d52011-01-03 15:07:31 +00002781 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy(tp, 0x1f, 0x0002);
2783 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2784 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002785
françois romieubca03d52011-01-03 15:07:31 +00002786 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002787 rtl_writephy(tp, 0x1f, 0x0002);
2788 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002789
françois romieu4da19632011-01-03 15:07:55 +00002790 rtl_writephy(tp, 0x1f, 0x0005);
2791 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002792
2793 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002794
françois romieu4da19632011-01-03 15:07:55 +00002795 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002796}
2797
françois romieu4da19632011-01-03 15:07:55 +00002798static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002799{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002800 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002801 { 0x1f, 0x0002 },
2802 { 0x10, 0x0008 },
2803 { 0x0d, 0x006c },
2804
2805 { 0x1f, 0x0000 },
2806 { 0x0d, 0xf880 },
2807
2808 { 0x1f, 0x0001 },
2809 { 0x17, 0x0cc0 },
2810
2811 { 0x1f, 0x0001 },
2812 { 0x0b, 0xa4d8 },
2813 { 0x09, 0x281c },
2814 { 0x07, 0x2883 },
2815 { 0x0a, 0x6b35 },
2816 { 0x1d, 0x3da4 },
2817 { 0x1c, 0xeffd },
2818 { 0x14, 0x7f52 },
2819 { 0x18, 0x7fc6 },
2820 { 0x08, 0x0601 },
2821 { 0x06, 0x4063 },
2822 { 0x10, 0xf074 },
2823 { 0x1f, 0x0003 },
2824 { 0x13, 0x0789 },
2825 { 0x12, 0xf4bd },
2826 { 0x1a, 0x04fd },
2827 { 0x14, 0x84b0 },
2828 { 0x1f, 0x0000 },
2829 { 0x00, 0x9200 },
2830
2831 { 0x1f, 0x0005 },
2832 { 0x01, 0x0340 },
2833 { 0x1f, 0x0001 },
2834 { 0x04, 0x4000 },
2835 { 0x03, 0x1d21 },
2836 { 0x02, 0x0c32 },
2837 { 0x01, 0x0200 },
2838 { 0x00, 0x5554 },
2839 { 0x04, 0x4800 },
2840 { 0x04, 0x4000 },
2841 { 0x04, 0xf000 },
2842 { 0x03, 0xdf01 },
2843 { 0x02, 0xdf20 },
2844 { 0x01, 0x101a },
2845 { 0x00, 0xa0ff },
2846 { 0x04, 0xf800 },
2847 { 0x04, 0xf000 },
2848 { 0x1f, 0x0000 },
2849
2850 { 0x1f, 0x0007 },
2851 { 0x1e, 0x0023 },
2852 { 0x16, 0x0000 },
2853 { 0x1f, 0x0000 }
2854 };
2855
françois romieu4da19632011-01-03 15:07:55 +00002856 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002857}
2858
françois romieue6de30d2011-01-03 15:08:37 +00002859static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2860{
2861 static const struct phy_reg phy_reg_init[] = {
2862 { 0x1f, 0x0001 },
2863 { 0x17, 0x0cc0 },
2864
2865 { 0x1f, 0x0007 },
2866 { 0x1e, 0x002d },
2867 { 0x18, 0x0040 },
2868 { 0x1f, 0x0000 }
2869 };
2870
2871 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2872 rtl_patchphy(tp, 0x0d, 1 << 5);
2873}
2874
Hayes Wang70090422011-07-06 15:58:06 +08002875static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002876{
2877 static const struct phy_reg phy_reg_init[] = {
2878 /* Enable Delay cap */
2879 { 0x1f, 0x0005 },
2880 { 0x05, 0x8b80 },
2881 { 0x06, 0xc896 },
2882 { 0x1f, 0x0000 },
2883
2884 /* Channel estimation fine tune */
2885 { 0x1f, 0x0001 },
2886 { 0x0b, 0x6c20 },
2887 { 0x07, 0x2872 },
2888 { 0x1c, 0xefff },
2889 { 0x1f, 0x0003 },
2890 { 0x14, 0x6420 },
2891 { 0x1f, 0x0000 },
2892
2893 /* Update PFM & 10M TX idle timer */
2894 { 0x1f, 0x0007 },
2895 { 0x1e, 0x002f },
2896 { 0x15, 0x1919 },
2897 { 0x1f, 0x0000 },
2898
2899 { 0x1f, 0x0007 },
2900 { 0x1e, 0x00ac },
2901 { 0x18, 0x0006 },
2902 { 0x1f, 0x0000 }
2903 };
2904
Francois Romieu15ecd032011-04-27 13:52:22 -07002905 rtl_apply_firmware(tp);
2906
hayeswang01dc7fe2011-03-21 01:50:28 +00002907 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2908
2909 /* DCO enable for 10M IDLE Power */
2910 rtl_writephy(tp, 0x1f, 0x0007);
2911 rtl_writephy(tp, 0x1e, 0x0023);
2912 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2913 rtl_writephy(tp, 0x1f, 0x0000);
2914
2915 /* For impedance matching */
2916 rtl_writephy(tp, 0x1f, 0x0002);
2917 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002918 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002919
2920 /* PHY auto speed down */
2921 rtl_writephy(tp, 0x1f, 0x0007);
2922 rtl_writephy(tp, 0x1e, 0x002d);
2923 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2924 rtl_writephy(tp, 0x1f, 0x0000);
2925 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2926
2927 rtl_writephy(tp, 0x1f, 0x0005);
2928 rtl_writephy(tp, 0x05, 0x8b86);
2929 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2930 rtl_writephy(tp, 0x1f, 0x0000);
2931
2932 rtl_writephy(tp, 0x1f, 0x0005);
2933 rtl_writephy(tp, 0x05, 0x8b85);
2934 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2935 rtl_writephy(tp, 0x1f, 0x0007);
2936 rtl_writephy(tp, 0x1e, 0x0020);
2937 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2938 rtl_writephy(tp, 0x1f, 0x0006);
2939 rtl_writephy(tp, 0x00, 0x5a00);
2940 rtl_writephy(tp, 0x1f, 0x0000);
2941 rtl_writephy(tp, 0x0d, 0x0007);
2942 rtl_writephy(tp, 0x0e, 0x003c);
2943 rtl_writephy(tp, 0x0d, 0x4007);
2944 rtl_writephy(tp, 0x0e, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0000);
2946}
2947
Hayes Wang70090422011-07-06 15:58:06 +08002948static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2949{
2950 static const struct phy_reg phy_reg_init[] = {
2951 /* Enable Delay cap */
2952 { 0x1f, 0x0004 },
2953 { 0x1f, 0x0007 },
2954 { 0x1e, 0x00ac },
2955 { 0x18, 0x0006 },
2956 { 0x1f, 0x0002 },
2957 { 0x1f, 0x0000 },
2958 { 0x1f, 0x0000 },
2959
2960 /* Channel estimation fine tune */
2961 { 0x1f, 0x0003 },
2962 { 0x09, 0xa20f },
2963 { 0x1f, 0x0000 },
2964 { 0x1f, 0x0000 },
2965
2966 /* Green Setting */
2967 { 0x1f, 0x0005 },
2968 { 0x05, 0x8b5b },
2969 { 0x06, 0x9222 },
2970 { 0x05, 0x8b6d },
2971 { 0x06, 0x8000 },
2972 { 0x05, 0x8b76 },
2973 { 0x06, 0x8000 },
2974 { 0x1f, 0x0000 }
2975 };
2976
2977 rtl_apply_firmware(tp);
2978
2979 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2980
2981 /* For 4-corner performance improve */
2982 rtl_writephy(tp, 0x1f, 0x0005);
2983 rtl_writephy(tp, 0x05, 0x8b80);
2984 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2985 rtl_writephy(tp, 0x1f, 0x0000);
2986
2987 /* PHY auto speed down */
2988 rtl_writephy(tp, 0x1f, 0x0004);
2989 rtl_writephy(tp, 0x1f, 0x0007);
2990 rtl_writephy(tp, 0x1e, 0x002d);
2991 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2992 rtl_writephy(tp, 0x1f, 0x0002);
2993 rtl_writephy(tp, 0x1f, 0x0000);
2994 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2995
2996 /* improve 10M EEE waveform */
2997 rtl_writephy(tp, 0x1f, 0x0005);
2998 rtl_writephy(tp, 0x05, 0x8b86);
2999 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3000 rtl_writephy(tp, 0x1f, 0x0000);
3001
3002 /* Improve 2-pair detection performance */
3003 rtl_writephy(tp, 0x1f, 0x0005);
3004 rtl_writephy(tp, 0x05, 0x8b85);
3005 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3006 rtl_writephy(tp, 0x1f, 0x0000);
3007
3008 /* EEE setting */
3009 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3010 ERIAR_EXGMAC);
3011 rtl_writephy(tp, 0x1f, 0x0005);
3012 rtl_writephy(tp, 0x05, 0x8b85);
3013 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3014 rtl_writephy(tp, 0x1f, 0x0004);
3015 rtl_writephy(tp, 0x1f, 0x0007);
3016 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003017 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003018 rtl_writephy(tp, 0x1f, 0x0002);
3019 rtl_writephy(tp, 0x1f, 0x0000);
3020 rtl_writephy(tp, 0x0d, 0x0007);
3021 rtl_writephy(tp, 0x0e, 0x003c);
3022 rtl_writephy(tp, 0x0d, 0x4007);
3023 rtl_writephy(tp, 0x0e, 0x0000);
3024 rtl_writephy(tp, 0x0d, 0x0000);
3025
3026 /* Green feature */
3027 rtl_writephy(tp, 0x1f, 0x0003);
3028 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3029 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3030 rtl_writephy(tp, 0x1f, 0x0000);
3031}
3032
Hayes Wang5f886e02012-03-30 14:33:03 +08003033static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3034{
3035 /* For 4-corner performance improve */
3036 rtl_writephy(tp, 0x1f, 0x0005);
3037 rtl_writephy(tp, 0x05, 0x8b80);
3038 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3039 rtl_writephy(tp, 0x1f, 0x0000);
3040
3041 /* PHY auto speed down */
3042 rtl_writephy(tp, 0x1f, 0x0007);
3043 rtl_writephy(tp, 0x1e, 0x002d);
3044 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3045 rtl_writephy(tp, 0x1f, 0x0000);
3046 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3047
3048 /* Improve 10M EEE waveform */
3049 rtl_writephy(tp, 0x1f, 0x0005);
3050 rtl_writephy(tp, 0x05, 0x8b86);
3051 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3052 rtl_writephy(tp, 0x1f, 0x0000);
3053}
3054
Hayes Wangc2218922011-09-06 16:55:18 +08003055static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3056{
3057 static const struct phy_reg phy_reg_init[] = {
3058 /* Channel estimation fine tune */
3059 { 0x1f, 0x0003 },
3060 { 0x09, 0xa20f },
3061 { 0x1f, 0x0000 },
3062
3063 /* Modify green table for giga & fnet */
3064 { 0x1f, 0x0005 },
3065 { 0x05, 0x8b55 },
3066 { 0x06, 0x0000 },
3067 { 0x05, 0x8b5e },
3068 { 0x06, 0x0000 },
3069 { 0x05, 0x8b67 },
3070 { 0x06, 0x0000 },
3071 { 0x05, 0x8b70 },
3072 { 0x06, 0x0000 },
3073 { 0x1f, 0x0000 },
3074 { 0x1f, 0x0007 },
3075 { 0x1e, 0x0078 },
3076 { 0x17, 0x0000 },
3077 { 0x19, 0x00fb },
3078 { 0x1f, 0x0000 },
3079
3080 /* Modify green table for 10M */
3081 { 0x1f, 0x0005 },
3082 { 0x05, 0x8b79 },
3083 { 0x06, 0xaa00 },
3084 { 0x1f, 0x0000 },
3085
3086 /* Disable hiimpedance detection (RTCT) */
3087 { 0x1f, 0x0003 },
3088 { 0x01, 0x328a },
3089 { 0x1f, 0x0000 }
3090 };
3091
3092 rtl_apply_firmware(tp);
3093
3094 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3095
Hayes Wang5f886e02012-03-30 14:33:03 +08003096 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003097
3098 /* Improve 2-pair detection performance */
3099 rtl_writephy(tp, 0x1f, 0x0005);
3100 rtl_writephy(tp, 0x05, 0x8b85);
3101 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3102 rtl_writephy(tp, 0x1f, 0x0000);
3103}
3104
3105static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3106{
3107 rtl_apply_firmware(tp);
3108
Hayes Wang5f886e02012-03-30 14:33:03 +08003109 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003110}
3111
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003112static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3113{
3114 void __iomem *ioaddr = tp->mmio_addr;
3115 static const struct phy_reg phy_reg_init[] = {
3116 /* Channel estimation fine tune */
3117 { 0x1f, 0x0003 },
3118 { 0x09, 0xa20f },
3119 { 0x1f, 0x0000 },
3120
3121 /* Modify green table for giga & fnet */
3122 { 0x1f, 0x0005 },
3123 { 0x05, 0x8b55 },
3124 { 0x06, 0x0000 },
3125 { 0x05, 0x8b5e },
3126 { 0x06, 0x0000 },
3127 { 0x05, 0x8b67 },
3128 { 0x06, 0x0000 },
3129 { 0x05, 0x8b70 },
3130 { 0x06, 0x0000 },
3131 { 0x1f, 0x0000 },
3132 { 0x1f, 0x0007 },
3133 { 0x1e, 0x0078 },
3134 { 0x17, 0x0000 },
3135 { 0x19, 0x00aa },
3136 { 0x1f, 0x0000 },
3137
3138 /* Modify green table for 10M */
3139 { 0x1f, 0x0005 },
3140 { 0x05, 0x8b79 },
3141 { 0x06, 0xaa00 },
3142 { 0x1f, 0x0000 },
3143
3144 /* Disable hiimpedance detection (RTCT) */
3145 { 0x1f, 0x0003 },
3146 { 0x01, 0x328a },
3147 { 0x1f, 0x0000 }
3148 };
3149
3150
3151 rtl_apply_firmware(tp);
3152
3153 rtl8168f_hw_phy_config(tp);
3154
3155 /* Improve 2-pair detection performance */
3156 rtl_writephy(tp, 0x1f, 0x0005);
3157 rtl_writephy(tp, 0x05, 0x8b85);
3158 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3159 rtl_writephy(tp, 0x1f, 0x0000);
3160
3161 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3162
3163 /* Modify green table for giga */
3164 rtl_writephy(tp, 0x1f, 0x0005);
3165 rtl_writephy(tp, 0x05, 0x8b54);
3166 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3167 rtl_writephy(tp, 0x05, 0x8b5d);
3168 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0800);
3169 rtl_writephy(tp, 0x05, 0x8a7c);
3170 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3171 rtl_writephy(tp, 0x05, 0x8a7f);
3172 rtl_w1w0_phy(tp, 0x06, 0x0100, 0x0000);
3173 rtl_writephy(tp, 0x05, 0x8a82);
3174 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3175 rtl_writephy(tp, 0x05, 0x8a85);
3176 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3177 rtl_writephy(tp, 0x05, 0x8a88);
3178 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
3179 rtl_writephy(tp, 0x1f, 0x0000);
3180
3181 /* uc same-seed solution */
3182 rtl_writephy(tp, 0x1f, 0x0005);
3183 rtl_writephy(tp, 0x05, 0x8b85);
3184 rtl_w1w0_phy(tp, 0x06, 0x8000, 0x0000);
3185 rtl_writephy(tp, 0x1f, 0x0000);
3186
3187 /* eee setting */
3188 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3189 rtl_writephy(tp, 0x1f, 0x0005);
3190 rtl_writephy(tp, 0x05, 0x8b85);
3191 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3192 rtl_writephy(tp, 0x1f, 0x0004);
3193 rtl_writephy(tp, 0x1f, 0x0007);
3194 rtl_writephy(tp, 0x1e, 0x0020);
3195 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
3196 rtl_writephy(tp, 0x1f, 0x0000);
3197 rtl_writephy(tp, 0x0d, 0x0007);
3198 rtl_writephy(tp, 0x0e, 0x003c);
3199 rtl_writephy(tp, 0x0d, 0x4007);
3200 rtl_writephy(tp, 0x0e, 0x0000);
3201 rtl_writephy(tp, 0x0d, 0x0000);
3202
3203 /* Green feature */
3204 rtl_writephy(tp, 0x1f, 0x0003);
3205 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3206 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3207 rtl_writephy(tp, 0x1f, 0x0000);
3208}
3209
françois romieu4da19632011-01-03 15:07:55 +00003210static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003211{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003212 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003213 { 0x1f, 0x0003 },
3214 { 0x08, 0x441d },
3215 { 0x01, 0x9100 },
3216 { 0x1f, 0x0000 }
3217 };
3218
françois romieu4da19632011-01-03 15:07:55 +00003219 rtl_writephy(tp, 0x1f, 0x0000);
3220 rtl_patchphy(tp, 0x11, 1 << 12);
3221 rtl_patchphy(tp, 0x19, 1 << 13);
3222 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003223
françois romieu4da19632011-01-03 15:07:55 +00003224 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003225}
3226
Hayes Wang5a5e4442011-02-22 17:26:21 +08003227static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3228{
3229 static const struct phy_reg phy_reg_init[] = {
3230 { 0x1f, 0x0005 },
3231 { 0x1a, 0x0000 },
3232 { 0x1f, 0x0000 },
3233
3234 { 0x1f, 0x0004 },
3235 { 0x1c, 0x0000 },
3236 { 0x1f, 0x0000 },
3237
3238 { 0x1f, 0x0001 },
3239 { 0x15, 0x7701 },
3240 { 0x1f, 0x0000 }
3241 };
3242
3243 /* Disable ALDPS before ram code */
3244 rtl_writephy(tp, 0x1f, 0x0000);
3245 rtl_writephy(tp, 0x18, 0x0310);
3246 msleep(100);
3247
François Romieu953a12c2011-04-24 17:38:48 +02003248 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003249
3250 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3251}
3252
Hayes Wang7e18dca2012-03-30 14:33:02 +08003253static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3254{
3255 void __iomem *ioaddr = tp->mmio_addr;
3256
3257 /* Disable ALDPS before setting firmware */
3258 rtl_writephy(tp, 0x1f, 0x0000);
3259 rtl_writephy(tp, 0x18, 0x0310);
3260 msleep(20);
3261
3262 rtl_apply_firmware(tp);
3263
3264 /* EEE setting */
3265 rtl_eri_write(ioaddr, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3266 rtl_writephy(tp, 0x1f, 0x0004);
3267 rtl_writephy(tp, 0x10, 0x401f);
3268 rtl_writephy(tp, 0x19, 0x7030);
3269 rtl_writephy(tp, 0x1f, 0x0000);
3270}
3271
Francois Romieu5615d9f2007-08-17 17:50:46 +02003272static void rtl_hw_phy_config(struct net_device *dev)
3273{
3274 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003275
3276 rtl8169_print_mac_version(tp);
3277
3278 switch (tp->mac_version) {
3279 case RTL_GIGA_MAC_VER_01:
3280 break;
3281 case RTL_GIGA_MAC_VER_02:
3282 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003283 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003284 break;
3285 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003286 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003287 break;
françois romieu2e9558562009-08-10 19:44:19 +00003288 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003289 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003290 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003291 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003292 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003293 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003294 case RTL_GIGA_MAC_VER_07:
3295 case RTL_GIGA_MAC_VER_08:
3296 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003297 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003298 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003299 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003300 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003301 break;
3302 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003303 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003304 break;
3305 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003306 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003307 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003308 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003309 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003310 break;
3311 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003312 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003313 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003314 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003315 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003316 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003317 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003318 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003319 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003320 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003321 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003322 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003323 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003324 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003325 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003326 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003327 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003328 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003329 break;
3330 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003331 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003332 break;
3333 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003334 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003335 break;
françois romieue6de30d2011-01-03 15:08:37 +00003336 case RTL_GIGA_MAC_VER_28:
3337 rtl8168d_4_hw_phy_config(tp);
3338 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003339 case RTL_GIGA_MAC_VER_29:
3340 case RTL_GIGA_MAC_VER_30:
3341 rtl8105e_hw_phy_config(tp);
3342 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003343 case RTL_GIGA_MAC_VER_31:
3344 /* None. */
3345 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003346 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003347 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003348 rtl8168e_1_hw_phy_config(tp);
3349 break;
3350 case RTL_GIGA_MAC_VER_34:
3351 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003352 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003353 case RTL_GIGA_MAC_VER_35:
3354 rtl8168f_1_hw_phy_config(tp);
3355 break;
3356 case RTL_GIGA_MAC_VER_36:
3357 rtl8168f_2_hw_phy_config(tp);
3358 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003359
Hayes Wang7e18dca2012-03-30 14:33:02 +08003360 case RTL_GIGA_MAC_VER_37:
3361 rtl8402_hw_phy_config(tp);
3362 break;
3363
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003364 case RTL_GIGA_MAC_VER_38:
3365 rtl8411_hw_phy_config(tp);
3366 break;
3367
Francois Romieu5615d9f2007-08-17 17:50:46 +02003368 default:
3369 break;
3370 }
3371}
3372
Francois Romieuda78dbf2012-01-26 14:18:23 +01003373static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 struct timer_list *timer = &tp->timer;
3376 void __iomem *ioaddr = tp->mmio_addr;
3377 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3378
Francois Romieubcf0bf92006-07-26 23:14:13 +02003379 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
françois romieu4da19632011-01-03 15:07:55 +00003381 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003382 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 * A busy loop could burn quite a few cycles on nowadays CPU.
3384 * Let's delay the execution of the timer for a few ticks.
3385 */
3386 timeout = HZ/10;
3387 goto out_mod_timer;
3388 }
3389
3390 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003391 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Francois Romieuda78dbf2012-01-26 14:18:23 +01003393 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
françois romieu4da19632011-01-03 15:07:55 +00003395 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397out_mod_timer:
3398 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003399}
3400
3401static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3402{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003403 if (!test_and_set_bit(flag, tp->wk.flags))
3404 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003405}
3406
3407static void rtl8169_phy_timer(unsigned long __opaque)
3408{
3409 struct net_device *dev = (struct net_device *)__opaque;
3410 struct rtl8169_private *tp = netdev_priv(dev);
3411
Francois Romieu98ddf982012-01-31 10:47:34 +01003412 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3416 void __iomem *ioaddr)
3417{
3418 iounmap(ioaddr);
3419 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003420 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 pci_disable_device(pdev);
3422 free_netdev(dev);
3423}
3424
Francois Romieubf793292006-11-01 00:53:05 +01003425static void rtl8169_phy_reset(struct net_device *dev,
3426 struct rtl8169_private *tp)
3427{
Francois Romieu07d3f512007-02-21 22:40:46 +01003428 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003429
françois romieu4da19632011-01-03 15:07:55 +00003430 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003431 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003432 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003433 return;
3434 msleep(1);
3435 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003436 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003437}
3438
David S. Miller8decf862011-09-22 03:23:13 -04003439static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3440{
3441 void __iomem *ioaddr = tp->mmio_addr;
3442
3443 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3444 (RTL_R8(PHYstatus) & TBI_Enable);
3445}
3446
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003447static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003449 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003450
Francois Romieu5615d9f2007-08-17 17:50:46 +02003451 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003452
Marcus Sundberg773328942008-07-10 21:28:08 +02003453 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3454 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3455 RTL_W8(0x82, 0x01);
3456 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003457
Francois Romieu6dccd162007-02-13 23:38:05 +01003458 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3459
3460 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3461 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003462
Francois Romieubcf0bf92006-07-26 23:14:13 +02003463 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003464 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3465 RTL_W8(0x82, 0x01);
3466 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003467 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003468 }
3469
Francois Romieubf793292006-11-01 00:53:05 +01003470 rtl8169_phy_reset(dev, tp);
3471
Oliver Neukum54405cd2011-01-06 21:55:13 +01003472 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003473 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3474 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3475 (tp->mii.supports_gmii ?
3476 ADVERTISED_1000baseT_Half |
3477 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003478
David S. Miller8decf862011-09-22 03:23:13 -04003479 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003480 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003481}
3482
Francois Romieu773d2022007-01-31 23:47:43 +01003483static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3484{
3485 void __iomem *ioaddr = tp->mmio_addr;
3486 u32 high;
3487 u32 low;
3488
3489 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3490 high = addr[4] | (addr[5] << 8);
3491
Francois Romieuda78dbf2012-01-26 14:18:23 +01003492 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003493
3494 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003495
Francois Romieu773d2022007-01-31 23:47:43 +01003496 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003497 RTL_R32(MAC4);
3498
Francois Romieu78f1cd02010-03-27 19:35:46 -07003499 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003500 RTL_R32(MAC0);
3501
françois romieuc28aa382011-08-02 03:53:43 +00003502 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3503 const struct exgmac_reg e[] = {
3504 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3505 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3506 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3507 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3508 low >> 16 },
3509 };
3510
3511 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3512 }
3513
Francois Romieu773d2022007-01-31 23:47:43 +01003514 RTL_W8(Cfg9346, Cfg9346_Lock);
3515
Francois Romieuda78dbf2012-01-26 14:18:23 +01003516 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003517}
3518
3519static int rtl_set_mac_address(struct net_device *dev, void *p)
3520{
3521 struct rtl8169_private *tp = netdev_priv(dev);
3522 struct sockaddr *addr = p;
3523
3524 if (!is_valid_ether_addr(addr->sa_data))
3525 return -EADDRNOTAVAIL;
3526
3527 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3528
3529 rtl_rar_set(tp, dev->dev_addr);
3530
3531 return 0;
3532}
3533
Francois Romieu5f787a12006-08-17 13:02:36 +02003534static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3535{
3536 struct rtl8169_private *tp = netdev_priv(dev);
3537 struct mii_ioctl_data *data = if_mii(ifr);
3538
Francois Romieu8b4ab282008-11-19 22:05:25 -08003539 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3540}
Francois Romieu5f787a12006-08-17 13:02:36 +02003541
Francois Romieucecb5fd2011-04-01 10:21:07 +02003542static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3543 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003544{
Francois Romieu5f787a12006-08-17 13:02:36 +02003545 switch (cmd) {
3546 case SIOCGMIIPHY:
3547 data->phy_id = 32; /* Internal PHY */
3548 return 0;
3549
3550 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003551 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003552 return 0;
3553
3554 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003555 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003556 return 0;
3557 }
3558 return -EOPNOTSUPP;
3559}
3560
Francois Romieu8b4ab282008-11-19 22:05:25 -08003561static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3562{
3563 return -EOPNOTSUPP;
3564}
3565
Francois Romieufbac58f2007-10-04 22:51:38 +02003566static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3567{
3568 if (tp->features & RTL_FEATURE_MSI) {
3569 pci_disable_msi(pdev);
3570 tp->features &= ~RTL_FEATURE_MSI;
3571 }
3572}
3573
françois romieuc0e45c12011-01-03 15:08:04 +00003574static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3575{
3576 struct mdio_ops *ops = &tp->mdio_ops;
3577
3578 switch (tp->mac_version) {
3579 case RTL_GIGA_MAC_VER_27:
3580 ops->write = r8168dp_1_mdio_write;
3581 ops->read = r8168dp_1_mdio_read;
3582 break;
françois romieue6de30d2011-01-03 15:08:37 +00003583 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003584 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003585 ops->write = r8168dp_2_mdio_write;
3586 ops->read = r8168dp_2_mdio_read;
3587 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003588 default:
3589 ops->write = r8169_mdio_write;
3590 ops->read = r8169_mdio_read;
3591 break;
3592 }
3593}
3594
David S. Miller1805b2f2011-10-24 18:18:09 -04003595static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3596{
3597 void __iomem *ioaddr = tp->mmio_addr;
3598
3599 switch (tp->mac_version) {
3600 case RTL_GIGA_MAC_VER_29:
3601 case RTL_GIGA_MAC_VER_30:
3602 case RTL_GIGA_MAC_VER_32:
3603 case RTL_GIGA_MAC_VER_33:
3604 case RTL_GIGA_MAC_VER_34:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003605 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003606 case RTL_GIGA_MAC_VER_38:
David S. Miller1805b2f2011-10-24 18:18:09 -04003607 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3608 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3609 break;
3610 default:
3611 break;
3612 }
3613}
3614
3615static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3616{
3617 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3618 return false;
3619
3620 rtl_writephy(tp, 0x1f, 0x0000);
3621 rtl_writephy(tp, MII_BMCR, 0x0000);
3622
3623 rtl_wol_suspend_quirk(tp);
3624
3625 return true;
3626}
3627
françois romieu065c27c2011-01-03 15:08:12 +00003628static void r810x_phy_power_down(struct rtl8169_private *tp)
3629{
3630 rtl_writephy(tp, 0x1f, 0x0000);
3631 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3632}
3633
3634static void r810x_phy_power_up(struct rtl8169_private *tp)
3635{
3636 rtl_writephy(tp, 0x1f, 0x0000);
3637 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3638}
3639
3640static void r810x_pll_power_down(struct rtl8169_private *tp)
3641{
Hayes Wang00042992012-03-30 14:33:00 +08003642 void __iomem *ioaddr = tp->mmio_addr;
3643
David S. Miller1805b2f2011-10-24 18:18:09 -04003644 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003645 return;
françois romieu065c27c2011-01-03 15:08:12 +00003646
3647 r810x_phy_power_down(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003648
3649 switch (tp->mac_version) {
3650 case RTL_GIGA_MAC_VER_07:
3651 case RTL_GIGA_MAC_VER_08:
3652 case RTL_GIGA_MAC_VER_09:
3653 case RTL_GIGA_MAC_VER_10:
3654 case RTL_GIGA_MAC_VER_13:
3655 case RTL_GIGA_MAC_VER_16:
3656 break;
3657 default:
3658 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3659 break;
3660 }
françois romieu065c27c2011-01-03 15:08:12 +00003661}
3662
3663static void r810x_pll_power_up(struct rtl8169_private *tp)
3664{
Hayes Wang00042992012-03-30 14:33:00 +08003665 void __iomem *ioaddr = tp->mmio_addr;
3666
françois romieu065c27c2011-01-03 15:08:12 +00003667 r810x_phy_power_up(tp);
Hayes Wang00042992012-03-30 14:33:00 +08003668
3669 switch (tp->mac_version) {
3670 case RTL_GIGA_MAC_VER_07:
3671 case RTL_GIGA_MAC_VER_08:
3672 case RTL_GIGA_MAC_VER_09:
3673 case RTL_GIGA_MAC_VER_10:
3674 case RTL_GIGA_MAC_VER_13:
3675 case RTL_GIGA_MAC_VER_16:
3676 break;
3677 default:
3678 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3679 break;
3680 }
françois romieu065c27c2011-01-03 15:08:12 +00003681}
3682
3683static void r8168_phy_power_up(struct rtl8169_private *tp)
3684{
3685 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003686 switch (tp->mac_version) {
3687 case RTL_GIGA_MAC_VER_11:
3688 case RTL_GIGA_MAC_VER_12:
3689 case RTL_GIGA_MAC_VER_17:
3690 case RTL_GIGA_MAC_VER_18:
3691 case RTL_GIGA_MAC_VER_19:
3692 case RTL_GIGA_MAC_VER_20:
3693 case RTL_GIGA_MAC_VER_21:
3694 case RTL_GIGA_MAC_VER_22:
3695 case RTL_GIGA_MAC_VER_23:
3696 case RTL_GIGA_MAC_VER_24:
3697 case RTL_GIGA_MAC_VER_25:
3698 case RTL_GIGA_MAC_VER_26:
3699 case RTL_GIGA_MAC_VER_27:
3700 case RTL_GIGA_MAC_VER_28:
3701 case RTL_GIGA_MAC_VER_31:
3702 rtl_writephy(tp, 0x0e, 0x0000);
3703 break;
3704 default:
3705 break;
3706 }
françois romieu065c27c2011-01-03 15:08:12 +00003707 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3708}
3709
3710static void r8168_phy_power_down(struct rtl8169_private *tp)
3711{
3712 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003713 switch (tp->mac_version) {
3714 case RTL_GIGA_MAC_VER_32:
3715 case RTL_GIGA_MAC_VER_33:
3716 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3717 break;
3718
3719 case RTL_GIGA_MAC_VER_11:
3720 case RTL_GIGA_MAC_VER_12:
3721 case RTL_GIGA_MAC_VER_17:
3722 case RTL_GIGA_MAC_VER_18:
3723 case RTL_GIGA_MAC_VER_19:
3724 case RTL_GIGA_MAC_VER_20:
3725 case RTL_GIGA_MAC_VER_21:
3726 case RTL_GIGA_MAC_VER_22:
3727 case RTL_GIGA_MAC_VER_23:
3728 case RTL_GIGA_MAC_VER_24:
3729 case RTL_GIGA_MAC_VER_25:
3730 case RTL_GIGA_MAC_VER_26:
3731 case RTL_GIGA_MAC_VER_27:
3732 case RTL_GIGA_MAC_VER_28:
3733 case RTL_GIGA_MAC_VER_31:
3734 rtl_writephy(tp, 0x0e, 0x0200);
3735 default:
3736 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3737 break;
3738 }
françois romieu065c27c2011-01-03 15:08:12 +00003739}
3740
3741static void r8168_pll_power_down(struct rtl8169_private *tp)
3742{
3743 void __iomem *ioaddr = tp->mmio_addr;
3744
Francois Romieucecb5fd2011-04-01 10:21:07 +02003745 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3746 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3747 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003748 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003749 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003750 }
françois romieu065c27c2011-01-03 15:08:12 +00003751
Francois Romieucecb5fd2011-04-01 10:21:07 +02003752 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3753 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003754 (RTL_R16(CPlusCmd) & ASF)) {
3755 return;
3756 }
3757
hayeswang01dc7fe2011-03-21 01:50:28 +00003758 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3759 tp->mac_version == RTL_GIGA_MAC_VER_33)
3760 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3761
David S. Miller1805b2f2011-10-24 18:18:09 -04003762 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003763 return;
françois romieu065c27c2011-01-03 15:08:12 +00003764
3765 r8168_phy_power_down(tp);
3766
3767 switch (tp->mac_version) {
3768 case RTL_GIGA_MAC_VER_25:
3769 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003770 case RTL_GIGA_MAC_VER_27:
3771 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003772 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003773 case RTL_GIGA_MAC_VER_32:
3774 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003775 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3776 break;
3777 }
3778}
3779
3780static void r8168_pll_power_up(struct rtl8169_private *tp)
3781{
3782 void __iomem *ioaddr = tp->mmio_addr;
3783
françois romieu065c27c2011-01-03 15:08:12 +00003784 switch (tp->mac_version) {
3785 case RTL_GIGA_MAC_VER_25:
3786 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003787 case RTL_GIGA_MAC_VER_27:
3788 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003789 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003790 case RTL_GIGA_MAC_VER_32:
3791 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003792 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3793 break;
3794 }
3795
3796 r8168_phy_power_up(tp);
3797}
3798
Francois Romieud58d46b2011-05-03 16:38:29 +02003799static void rtl_generic_op(struct rtl8169_private *tp,
3800 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003801{
3802 if (op)
3803 op(tp);
3804}
3805
3806static void rtl_pll_power_down(struct rtl8169_private *tp)
3807{
Francois Romieud58d46b2011-05-03 16:38:29 +02003808 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003809}
3810
3811static void rtl_pll_power_up(struct rtl8169_private *tp)
3812{
Francois Romieud58d46b2011-05-03 16:38:29 +02003813 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003814}
3815
3816static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3817{
3818 struct pll_power_ops *ops = &tp->pll_power_ops;
3819
3820 switch (tp->mac_version) {
3821 case RTL_GIGA_MAC_VER_07:
3822 case RTL_GIGA_MAC_VER_08:
3823 case RTL_GIGA_MAC_VER_09:
3824 case RTL_GIGA_MAC_VER_10:
3825 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003826 case RTL_GIGA_MAC_VER_29:
3827 case RTL_GIGA_MAC_VER_30:
Hayes Wang7e18dca2012-03-30 14:33:02 +08003828 case RTL_GIGA_MAC_VER_37:
françois romieu065c27c2011-01-03 15:08:12 +00003829 ops->down = r810x_pll_power_down;
3830 ops->up = r810x_pll_power_up;
3831 break;
3832
3833 case RTL_GIGA_MAC_VER_11:
3834 case RTL_GIGA_MAC_VER_12:
3835 case RTL_GIGA_MAC_VER_17:
3836 case RTL_GIGA_MAC_VER_18:
3837 case RTL_GIGA_MAC_VER_19:
3838 case RTL_GIGA_MAC_VER_20:
3839 case RTL_GIGA_MAC_VER_21:
3840 case RTL_GIGA_MAC_VER_22:
3841 case RTL_GIGA_MAC_VER_23:
3842 case RTL_GIGA_MAC_VER_24:
3843 case RTL_GIGA_MAC_VER_25:
3844 case RTL_GIGA_MAC_VER_26:
3845 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003846 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003847 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003848 case RTL_GIGA_MAC_VER_32:
3849 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003850 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003851 case RTL_GIGA_MAC_VER_35:
3852 case RTL_GIGA_MAC_VER_36:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003853 case RTL_GIGA_MAC_VER_38:
françois romieu065c27c2011-01-03 15:08:12 +00003854 ops->down = r8168_pll_power_down;
3855 ops->up = r8168_pll_power_up;
3856 break;
3857
3858 default:
3859 ops->down = NULL;
3860 ops->up = NULL;
3861 break;
3862 }
3863}
3864
Hayes Wange542a222011-07-06 15:58:04 +08003865static void rtl_init_rxcfg(struct rtl8169_private *tp)
3866{
3867 void __iomem *ioaddr = tp->mmio_addr;
3868
3869 switch (tp->mac_version) {
3870 case RTL_GIGA_MAC_VER_01:
3871 case RTL_GIGA_MAC_VER_02:
3872 case RTL_GIGA_MAC_VER_03:
3873 case RTL_GIGA_MAC_VER_04:
3874 case RTL_GIGA_MAC_VER_05:
3875 case RTL_GIGA_MAC_VER_06:
3876 case RTL_GIGA_MAC_VER_10:
3877 case RTL_GIGA_MAC_VER_11:
3878 case RTL_GIGA_MAC_VER_12:
3879 case RTL_GIGA_MAC_VER_13:
3880 case RTL_GIGA_MAC_VER_14:
3881 case RTL_GIGA_MAC_VER_15:
3882 case RTL_GIGA_MAC_VER_16:
3883 case RTL_GIGA_MAC_VER_17:
3884 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3885 break;
3886 case RTL_GIGA_MAC_VER_18:
3887 case RTL_GIGA_MAC_VER_19:
3888 case RTL_GIGA_MAC_VER_20:
3889 case RTL_GIGA_MAC_VER_21:
3890 case RTL_GIGA_MAC_VER_22:
3891 case RTL_GIGA_MAC_VER_23:
3892 case RTL_GIGA_MAC_VER_24:
3893 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3894 break;
3895 default:
3896 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3897 break;
3898 }
3899}
3900
Hayes Wang92fc43b2011-07-06 15:58:03 +08003901static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3902{
3903 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3904}
3905
Francois Romieud58d46b2011-05-03 16:38:29 +02003906static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3907{
françois romieu9c5028e2012-03-02 04:43:14 +00003908 void __iomem *ioaddr = tp->mmio_addr;
3909
3910 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003911 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003912 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003913}
3914
3915static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3916{
françois romieu9c5028e2012-03-02 04:43:14 +00003917 void __iomem *ioaddr = tp->mmio_addr;
3918
3919 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003920 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003921 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003922}
3923
3924static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3925{
3926 void __iomem *ioaddr = tp->mmio_addr;
3927
3928 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3929 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3930 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3931}
3932
3933static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3934{
3935 void __iomem *ioaddr = tp->mmio_addr;
3936
3937 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3938 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3939 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3940}
3941
3942static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3943{
3944 void __iomem *ioaddr = tp->mmio_addr;
3945
3946 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3947}
3948
3949static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3950{
3951 void __iomem *ioaddr = tp->mmio_addr;
3952
3953 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3954}
3955
3956static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3957{
3958 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003959
3960 RTL_W8(MaxTxPacketSize, 0x3f);
3961 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3962 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003963 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003964}
3965
3966static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3967{
3968 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003969
3970 RTL_W8(MaxTxPacketSize, 0x0c);
3971 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3972 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003973 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003974}
3975
3976static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3977{
3978 rtl_tx_performance_tweak(tp->pci_dev,
3979 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3980}
3981
3982static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3983{
3984 rtl_tx_performance_tweak(tp->pci_dev,
3985 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3986}
3987
3988static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3989{
3990 void __iomem *ioaddr = tp->mmio_addr;
3991
3992 r8168b_0_hw_jumbo_enable(tp);
3993
3994 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3995}
3996
3997static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3998{
3999 void __iomem *ioaddr = tp->mmio_addr;
4000
4001 r8168b_0_hw_jumbo_disable(tp);
4002
4003 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4004}
4005
4006static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
4007{
4008 struct jumbo_ops *ops = &tp->jumbo_ops;
4009
4010 switch (tp->mac_version) {
4011 case RTL_GIGA_MAC_VER_11:
4012 ops->disable = r8168b_0_hw_jumbo_disable;
4013 ops->enable = r8168b_0_hw_jumbo_enable;
4014 break;
4015 case RTL_GIGA_MAC_VER_12:
4016 case RTL_GIGA_MAC_VER_17:
4017 ops->disable = r8168b_1_hw_jumbo_disable;
4018 ops->enable = r8168b_1_hw_jumbo_enable;
4019 break;
4020 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4021 case RTL_GIGA_MAC_VER_19:
4022 case RTL_GIGA_MAC_VER_20:
4023 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4024 case RTL_GIGA_MAC_VER_22:
4025 case RTL_GIGA_MAC_VER_23:
4026 case RTL_GIGA_MAC_VER_24:
4027 case RTL_GIGA_MAC_VER_25:
4028 case RTL_GIGA_MAC_VER_26:
4029 ops->disable = r8168c_hw_jumbo_disable;
4030 ops->enable = r8168c_hw_jumbo_enable;
4031 break;
4032 case RTL_GIGA_MAC_VER_27:
4033 case RTL_GIGA_MAC_VER_28:
4034 ops->disable = r8168dp_hw_jumbo_disable;
4035 ops->enable = r8168dp_hw_jumbo_enable;
4036 break;
4037 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4038 case RTL_GIGA_MAC_VER_32:
4039 case RTL_GIGA_MAC_VER_33:
4040 case RTL_GIGA_MAC_VER_34:
4041 ops->disable = r8168e_hw_jumbo_disable;
4042 ops->enable = r8168e_hw_jumbo_enable;
4043 break;
4044
4045 /*
4046 * No action needed for jumbo frames with 8169.
4047 * No jumbo for 810x at all.
4048 */
4049 default:
4050 ops->disable = NULL;
4051 ops->enable = NULL;
4052 break;
4053 }
4054}
4055
Francois Romieu6f43adc2011-04-29 15:05:51 +02004056static void rtl_hw_reset(struct rtl8169_private *tp)
4057{
4058 void __iomem *ioaddr = tp->mmio_addr;
4059 int i;
4060
4061 /* Soft reset the chip. */
4062 RTL_W8(ChipCmd, CmdReset);
4063
4064 /* Check that the chip has finished the reset. */
4065 for (i = 0; i < 100; i++) {
4066 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4067 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004068 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004069 }
4070}
4071
Francois Romieub6ffd972011-06-17 17:00:05 +02004072static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4073{
4074 struct rtl_fw *rtl_fw;
4075 const char *name;
4076 int rc = -ENOMEM;
4077
4078 name = rtl_lookup_firmware_name(tp);
4079 if (!name)
4080 goto out_no_firmware;
4081
4082 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4083 if (!rtl_fw)
4084 goto err_warn;
4085
4086 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4087 if (rc < 0)
4088 goto err_free;
4089
Francois Romieufd112f22011-06-18 00:10:29 +02004090 rc = rtl_check_firmware(tp, rtl_fw);
4091 if (rc < 0)
4092 goto err_release_firmware;
4093
Francois Romieub6ffd972011-06-17 17:00:05 +02004094 tp->rtl_fw = rtl_fw;
4095out:
4096 return;
4097
Francois Romieufd112f22011-06-18 00:10:29 +02004098err_release_firmware:
4099 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004100err_free:
4101 kfree(rtl_fw);
4102err_warn:
4103 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4104 name, rc);
4105out_no_firmware:
4106 tp->rtl_fw = NULL;
4107 goto out;
4108}
4109
François Romieu953a12c2011-04-24 17:38:48 +02004110static void rtl_request_firmware(struct rtl8169_private *tp)
4111{
Francois Romieub6ffd972011-06-17 17:00:05 +02004112 if (IS_ERR(tp->rtl_fw))
4113 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004114}
4115
Hayes Wang92fc43b2011-07-06 15:58:03 +08004116static void rtl_rx_close(struct rtl8169_private *tp)
4117{
4118 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004119
Francois Romieu1687b562011-07-19 17:21:29 +02004120 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004121}
4122
françois romieue6de30d2011-01-03 15:08:37 +00004123static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
françois romieue6de30d2011-01-03 15:08:37 +00004125 void __iomem *ioaddr = tp->mmio_addr;
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004128 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Hayes Wang92fc43b2011-07-06 15:58:03 +08004130 rtl_rx_close(tp);
4131
Hayes Wang5d2e1952011-02-22 17:26:22 +08004132 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004133 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4134 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004135 while (RTL_R8(TxPoll) & NPQ)
4136 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004137 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4138 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
Hayes Wang7e18dca2012-03-30 14:33:02 +08004139 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004140 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
4141 tp->mac_version == RTL_GIGA_MAC_VER_38) {
David S. Miller8decf862011-09-22 03:23:13 -04004142 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004143 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4144 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004145 } else {
4146 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4147 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004148 }
4149
Hayes Wang92fc43b2011-07-06 15:58:03 +08004150 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151}
4152
Francois Romieu7f796d832007-06-11 23:04:41 +02004153static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004154{
4155 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004156
4157 /* Set DMA burst size and Interframe Gap Time */
4158 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4159 (InterFrameGap << TxInterFrameGapShift));
4160}
4161
Francois Romieu07ce4062007-02-23 23:36:39 +01004162static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
4164 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Francois Romieu07ce4062007-02-23 23:36:39 +01004166 tp->hw_start(dev);
4167
Francois Romieuda78dbf2012-01-26 14:18:23 +01004168 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004169}
4170
Francois Romieu7f796d832007-06-11 23:04:41 +02004171static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4172 void __iomem *ioaddr)
4173{
4174 /*
4175 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4176 * register to be written before TxDescAddrLow to work.
4177 * Switching from MMIO to I/O access fixes the issue as well.
4178 */
4179 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004180 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004181 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004182 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004183}
4184
4185static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4186{
4187 u16 cmd;
4188
4189 cmd = RTL_R16(CPlusCmd);
4190 RTL_W16(CPlusCmd, cmd);
4191 return cmd;
4192}
4193
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004194static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004195{
4196 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004197 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004198}
4199
Francois Romieu6dccd162007-02-13 23:38:05 +01004200static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4201{
Francois Romieu37441002011-06-17 22:58:54 +02004202 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004203 u32 mac_version;
4204 u32 clk;
4205 u32 val;
4206 } cfg2_info [] = {
4207 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4208 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4209 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4210 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004211 };
4212 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004213 unsigned int i;
4214 u32 clk;
4215
4216 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004217 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004218 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4219 RTL_W32(0x7c, p->val);
4220 break;
4221 }
4222 }
4223}
4224
Francois Romieue6b763e2012-03-08 09:35:39 +01004225static void rtl_set_rx_mode(struct net_device *dev)
4226{
4227 struct rtl8169_private *tp = netdev_priv(dev);
4228 void __iomem *ioaddr = tp->mmio_addr;
4229 u32 mc_filter[2]; /* Multicast hash filter */
4230 int rx_mode;
4231 u32 tmp = 0;
4232
4233 if (dev->flags & IFF_PROMISC) {
4234 /* Unconditionally log net taps. */
4235 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4236 rx_mode =
4237 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4238 AcceptAllPhys;
4239 mc_filter[1] = mc_filter[0] = 0xffffffff;
4240 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4241 (dev->flags & IFF_ALLMULTI)) {
4242 /* Too many to filter perfectly -- accept all multicasts. */
4243 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4244 mc_filter[1] = mc_filter[0] = 0xffffffff;
4245 } else {
4246 struct netdev_hw_addr *ha;
4247
4248 rx_mode = AcceptBroadcast | AcceptMyPhys;
4249 mc_filter[1] = mc_filter[0] = 0;
4250 netdev_for_each_mc_addr(ha, dev) {
4251 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4252 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4253 rx_mode |= AcceptMulticast;
4254 }
4255 }
4256
4257 if (dev->features & NETIF_F_RXALL)
4258 rx_mode |= (AcceptErr | AcceptRunt);
4259
4260 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4261
4262 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4263 u32 data = mc_filter[0];
4264
4265 mc_filter[0] = swab32(mc_filter[1]);
4266 mc_filter[1] = swab32(data);
4267 }
4268
4269 RTL_W32(MAR0 + 4, mc_filter[1]);
4270 RTL_W32(MAR0 + 0, mc_filter[0]);
4271
4272 RTL_W32(RxConfig, tmp);
4273}
4274
Francois Romieu07ce4062007-02-23 23:36:39 +01004275static void rtl_hw_start_8169(struct net_device *dev)
4276{
4277 struct rtl8169_private *tp = netdev_priv(dev);
4278 void __iomem *ioaddr = tp->mmio_addr;
4279 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004280
Francois Romieu9cb427b2006-11-02 00:10:16 +01004281 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4282 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4283 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4284 }
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004287 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4288 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4289 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4290 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004291 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4292
Hayes Wange542a222011-07-06 15:58:04 +08004293 rtl_init_rxcfg(tp);
4294
françois romieuf0298f82011-01-03 15:07:42 +00004295 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004297 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Francois Romieucecb5fd2011-04-01 10:21:07 +02004299 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4300 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4301 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4302 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004303 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Francois Romieu7f796d832007-06-11 23:04:41 +02004305 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004306
Francois Romieucecb5fd2011-04-01 10:21:07 +02004307 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4308 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004309 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004311 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
4313
Francois Romieubcf0bf92006-07-26 23:14:13 +02004314 RTL_W16(CPlusCmd, tp->cp_cmd);
4315
Francois Romieu6dccd162007-02-13 23:38:05 +01004316 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 /*
4319 * Undocumented corner. Supposedly:
4320 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4321 */
4322 RTL_W16(IntrMitigate, 0x0000);
4323
Francois Romieu7f796d832007-06-11 23:04:41 +02004324 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004325
Francois Romieucecb5fd2011-04-01 10:21:07 +02004326 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4327 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4328 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4329 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004330 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4331 rtl_set_rx_tx_config_registers(tp);
4332 }
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004335
4336 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4337 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
4339 RTL_W32(RxMissed, 0);
4340
Francois Romieu07ce4062007-02-23 23:36:39 +01004341 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 /* no early-rx interrupts */
4344 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004347static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4348{
4349 if (tp->csi_ops.write)
4350 tp->csi_ops.write(tp->mmio_addr, addr, value);
4351}
4352
4353static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4354{
4355 if (tp->csi_ops.read)
4356 return tp->csi_ops.read(tp->mmio_addr, addr);
4357 else
4358 return ~0;
4359}
4360
4361static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004362{
4363 u32 csi;
4364
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004365 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4366 rtl_csi_write(tp, 0x070c, csi | bits);
françois romieu650e8d52011-01-03 15:08:29 +00004367}
4368
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004369static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004370{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004371 rtl_csi_access_enable(tp, 0x17000000);
françois romieue6de30d2011-01-03 15:08:37 +00004372}
4373
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004374static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
françois romieu650e8d52011-01-03 15:08:29 +00004375{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004376 rtl_csi_access_enable(tp, 0x27000000);
4377}
4378
4379static void r8169_csi_write(void __iomem *ioaddr, int addr, int value)
4380{
4381 unsigned int i;
4382
4383 RTL_W32(CSIDR, value);
4384 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4385 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4386
4387 for (i = 0; i < 100; i++) {
4388 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4389 break;
4390 udelay(10);
4391 }
4392}
4393
4394static u32 r8169_csi_read(void __iomem *ioaddr, int addr)
4395{
4396 u32 value = ~0x00;
4397 unsigned int i;
4398
4399 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
4400 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4401
4402 for (i = 0; i < 100; i++) {
4403 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4404 value = RTL_R32(CSIDR);
4405 break;
4406 }
4407 udelay(10);
4408 }
4409
4410 return value;
4411}
4412
Hayes Wang7e18dca2012-03-30 14:33:02 +08004413static void r8402_csi_write(void __iomem *ioaddr, int addr, int value)
4414{
4415 unsigned int i;
4416
4417 RTL_W32(CSIDR, value);
4418 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4419 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
4420 CSIAR_FUNC_NIC);
4421
4422 for (i = 0; i < 100; i++) {
4423 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
4424 break;
4425 udelay(10);
4426 }
4427}
4428
4429static u32 r8402_csi_read(void __iomem *ioaddr, int addr)
4430{
4431 u32 value = ~0x00;
4432 unsigned int i;
4433
4434 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
4435 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
4436
4437 for (i = 0; i < 100; i++) {
4438 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
4439 value = RTL_R32(CSIDR);
4440 break;
4441 }
4442 udelay(10);
4443 }
4444
4445 return value;
4446}
4447
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004448static void __devinit rtl_init_csi_ops(struct rtl8169_private *tp)
4449{
4450 struct csi_ops *ops = &tp->csi_ops;
4451
4452 switch (tp->mac_version) {
4453 case RTL_GIGA_MAC_VER_01:
4454 case RTL_GIGA_MAC_VER_02:
4455 case RTL_GIGA_MAC_VER_03:
4456 case RTL_GIGA_MAC_VER_04:
4457 case RTL_GIGA_MAC_VER_05:
4458 case RTL_GIGA_MAC_VER_06:
4459 case RTL_GIGA_MAC_VER_10:
4460 case RTL_GIGA_MAC_VER_11:
4461 case RTL_GIGA_MAC_VER_12:
4462 case RTL_GIGA_MAC_VER_13:
4463 case RTL_GIGA_MAC_VER_14:
4464 case RTL_GIGA_MAC_VER_15:
4465 case RTL_GIGA_MAC_VER_16:
4466 case RTL_GIGA_MAC_VER_17:
4467 ops->write = NULL;
4468 ops->read = NULL;
4469 break;
4470
Hayes Wang7e18dca2012-03-30 14:33:02 +08004471 case RTL_GIGA_MAC_VER_37:
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004472 case RTL_GIGA_MAC_VER_38:
Hayes Wang7e18dca2012-03-30 14:33:02 +08004473 ops->write = r8402_csi_write;
4474 ops->read = r8402_csi_read;
4475 break;
4476
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004477 default:
4478 ops->write = r8169_csi_write;
4479 ops->read = r8169_csi_read;
4480 break;
4481 }
Francois Romieudacf8152008-08-02 20:44:13 +02004482}
4483
4484struct ephy_info {
4485 unsigned int offset;
4486 u16 mask;
4487 u16 bits;
4488};
4489
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004490static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004491{
4492 u16 w;
4493
4494 while (len-- > 0) {
4495 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4496 rtl_ephy_write(ioaddr, e->offset, w);
4497 e++;
4498 }
4499}
4500
Francois Romieub726e492008-06-28 12:22:59 +02004501static void rtl_disable_clock_request(struct pci_dev *pdev)
4502{
Jon Masone44daad2011-06-27 07:46:31 +00004503 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004504
4505 if (cap) {
4506 u16 ctl;
4507
4508 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4509 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4510 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4511 }
4512}
4513
françois romieue6de30d2011-01-03 15:08:37 +00004514static void rtl_enable_clock_request(struct pci_dev *pdev)
4515{
Jon Masone44daad2011-06-27 07:46:31 +00004516 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004517
4518 if (cap) {
4519 u16 ctl;
4520
4521 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4522 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4523 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4524 }
4525}
4526
Francois Romieub726e492008-06-28 12:22:59 +02004527#define R8168_CPCMD_QUIRK_MASK (\
4528 EnableBist | \
4529 Mac_dbgo_oe | \
4530 Force_half_dup | \
4531 Force_rxflow_en | \
4532 Force_txflow_en | \
4533 Cxpl_dbg_sel | \
4534 ASF | \
4535 PktCntrDisable | \
4536 Mac_dbgo_sel)
4537
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004538static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004539{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004540 void __iomem *ioaddr = tp->mmio_addr;
4541 struct pci_dev *pdev = tp->pci_dev;
4542
Francois Romieub726e492008-06-28 12:22:59 +02004543 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4544
4545 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4546
Francois Romieu2e68ae42008-06-28 12:00:55 +02004547 rtl_tx_performance_tweak(pdev,
4548 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004549}
4550
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004551static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004552{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004553 void __iomem *ioaddr = tp->mmio_addr;
4554
4555 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004556
françois romieuf0298f82011-01-03 15:07:42 +00004557 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004558
4559 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004560}
4561
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004562static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004563{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004564 void __iomem *ioaddr = tp->mmio_addr;
4565 struct pci_dev *pdev = tp->pci_dev;
4566
Francois Romieub726e492008-06-28 12:22:59 +02004567 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4568
4569 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4570
Francois Romieu219a1e92008-06-28 11:58:39 +02004571 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004572
4573 rtl_disable_clock_request(pdev);
4574
4575 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004576}
4577
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004578static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004579{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004580 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004581 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004582 { 0x01, 0, 0x0001 },
4583 { 0x02, 0x0800, 0x1000 },
4584 { 0x03, 0, 0x0042 },
4585 { 0x06, 0x0080, 0x0000 },
4586 { 0x07, 0, 0x2000 }
4587 };
4588
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004589 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004590
4591 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4592
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004593 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004594}
4595
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004596static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004597{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004598 void __iomem *ioaddr = tp->mmio_addr;
4599 struct pci_dev *pdev = tp->pci_dev;
4600
4601 rtl_csi_access_enable_2(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004602
4603 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4604
4605 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4606
4607 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4608}
4609
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004610static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004611{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004612 void __iomem *ioaddr = tp->mmio_addr;
4613 struct pci_dev *pdev = tp->pci_dev;
4614
4615 rtl_csi_access_enable_2(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004616
4617 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4618
4619 /* Magic. */
4620 RTL_W8(DBG_REG, 0x20);
4621
françois romieuf0298f82011-01-03 15:07:42 +00004622 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004623
4624 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4625
4626 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4627}
4628
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004629static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004630{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004631 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004632 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004633 { 0x02, 0x0800, 0x1000 },
4634 { 0x03, 0, 0x0002 },
4635 { 0x06, 0x0080, 0x0000 }
4636 };
4637
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004638 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004639
4640 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4641
4642 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4643
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004644 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004645}
4646
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004647static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004648{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004649 void __iomem *ioaddr = tp->mmio_addr;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004650 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004651 { 0x01, 0, 0x0001 },
4652 { 0x03, 0x0400, 0x0220 }
4653 };
4654
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004655 rtl_csi_access_enable_2(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004656
4657 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4658
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004659 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004660}
4661
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004662static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004663{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004664 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004665}
4666
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004667static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004668{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004669 rtl_csi_access_enable_2(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004670
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004671 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004672}
4673
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004674static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004675{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004676 void __iomem *ioaddr = tp->mmio_addr;
4677 struct pci_dev *pdev = tp->pci_dev;
4678
4679 rtl_csi_access_enable_2(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004680
4681 rtl_disable_clock_request(pdev);
4682
françois romieuf0298f82011-01-03 15:07:42 +00004683 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004684
4685 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4686
4687 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4688}
4689
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004690static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004691{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004692 void __iomem *ioaddr = tp->mmio_addr;
4693 struct pci_dev *pdev = tp->pci_dev;
4694
4695 rtl_csi_access_enable_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004696
4697 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4698
4699 RTL_W8(MaxTxPacketSize, TxPacketMax);
4700
4701 rtl_disable_clock_request(pdev);
4702}
4703
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004704static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004705{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004706 void __iomem *ioaddr = tp->mmio_addr;
4707 struct pci_dev *pdev = tp->pci_dev;
françois romieue6de30d2011-01-03 15:08:37 +00004708 static const struct ephy_info e_info_8168d_4[] = {
4709 { 0x0b, ~0, 0x48 },
4710 { 0x19, 0x20, 0x50 },
4711 { 0x0c, ~0, 0x20 }
4712 };
4713 int i;
4714
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004715 rtl_csi_access_enable_1(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004716
4717 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4718
4719 RTL_W8(MaxTxPacketSize, TxPacketMax);
4720
4721 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4722 const struct ephy_info *e = e_info_8168d_4 + i;
4723 u16 w;
4724
4725 w = rtl_ephy_read(ioaddr, e->offset);
4726 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4727 }
4728
4729 rtl_enable_clock_request(pdev);
4730}
4731
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004732static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004733{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004734 void __iomem *ioaddr = tp->mmio_addr;
4735 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004736 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004737 { 0x00, 0x0200, 0x0100 },
4738 { 0x00, 0x0000, 0x0004 },
4739 { 0x06, 0x0002, 0x0001 },
4740 { 0x06, 0x0000, 0x0030 },
4741 { 0x07, 0x0000, 0x2000 },
4742 { 0x00, 0x0000, 0x0020 },
4743 { 0x03, 0x5800, 0x2000 },
4744 { 0x03, 0x0000, 0x0001 },
4745 { 0x01, 0x0800, 0x1000 },
4746 { 0x07, 0x0000, 0x4000 },
4747 { 0x1e, 0x0000, 0x2000 },
4748 { 0x19, 0xffff, 0xfe6c },
4749 { 0x0a, 0x0000, 0x0040 }
4750 };
4751
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004752 rtl_csi_access_enable_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004753
Hayes Wang70090422011-07-06 15:58:06 +08004754 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004755
4756 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4757
4758 RTL_W8(MaxTxPacketSize, TxPacketMax);
4759
4760 rtl_disable_clock_request(pdev);
4761
4762 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004763 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4764 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004765
Francois Romieucecb5fd2011-04-01 10:21:07 +02004766 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004767}
4768
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004769static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004770{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004771 void __iomem *ioaddr = tp->mmio_addr;
4772 struct pci_dev *pdev = tp->pci_dev;
Hayes Wang70090422011-07-06 15:58:06 +08004773 static const struct ephy_info e_info_8168e_2[] = {
4774 { 0x09, 0x0000, 0x0080 },
4775 { 0x19, 0x0000, 0x0224 }
4776 };
4777
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004778 rtl_csi_access_enable_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004779
4780 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4781
4782 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4783
4784 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4785 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4786 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4787 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4788 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4789 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4790 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4791 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4792 ERIAR_EXGMAC);
4793
Hayes Wang3090bd92011-09-06 16:55:15 +08004794 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004795
4796 rtl_disable_clock_request(pdev);
4797
4798 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4799 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4800
4801 /* Adjust EEE LED frequency */
4802 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4803
4804 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4805 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4806 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4807}
4808
Hayes Wang5f886e02012-03-30 14:33:03 +08004809static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004810{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004811 void __iomem *ioaddr = tp->mmio_addr;
4812 struct pci_dev *pdev = tp->pci_dev;
Hayes Wangc2218922011-09-06 16:55:18 +08004813
Hayes Wang5f886e02012-03-30 14:33:03 +08004814 rtl_csi_access_enable_2(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004815
4816 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4817
4818 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4819 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4820 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4821 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4822 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4823 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4824 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4825 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4826 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4827 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004828
4829 RTL_W8(MaxTxPacketSize, EarlySize);
4830
4831 rtl_disable_clock_request(pdev);
4832
4833 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4834 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
Hayes Wangc2218922011-09-06 16:55:18 +08004835 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4836 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4837 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4838}
4839
Hayes Wang5f886e02012-03-30 14:33:03 +08004840static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4841{
4842 void __iomem *ioaddr = tp->mmio_addr;
4843 static const struct ephy_info e_info_8168f_1[] = {
4844 { 0x06, 0x00c0, 0x0020 },
4845 { 0x08, 0x0001, 0x0002 },
4846 { 0x09, 0x0000, 0x0080 },
4847 { 0x19, 0x0000, 0x0224 }
4848 };
4849
4850 rtl_hw_start_8168f(tp);
4851
4852 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4853
4854 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4855 ERIAR_EXGMAC);
4856
4857 /* Adjust EEE LED frequency */
4858 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4859}
4860
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004861static void rtl_hw_start_8411(struct rtl8169_private *tp)
4862{
4863 void __iomem *ioaddr = tp->mmio_addr;
4864 static const struct ephy_info e_info_8168f_1[] = {
4865 { 0x06, 0x00c0, 0x0020 },
4866 { 0x0f, 0xffff, 0x5200 },
4867 { 0x1e, 0x0000, 0x4000 },
4868 { 0x19, 0x0000, 0x0224 }
4869 };
4870
4871 rtl_hw_start_8168f(tp);
4872
4873 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4874
4875 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000,
4876 ERIAR_EXGMAC);
4877}
4878
Francois Romieu07ce4062007-02-23 23:36:39 +01004879static void rtl_hw_start_8168(struct net_device *dev)
4880{
Francois Romieu2dd99532007-06-11 23:22:52 +02004881 struct rtl8169_private *tp = netdev_priv(dev);
4882 void __iomem *ioaddr = tp->mmio_addr;
4883
4884 RTL_W8(Cfg9346, Cfg9346_Unlock);
4885
françois romieuf0298f82011-01-03 15:07:42 +00004886 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004887
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004888 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004889
Francois Romieu0e485152007-02-20 00:00:26 +01004890 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004891
4892 RTL_W16(CPlusCmd, tp->cp_cmd);
4893
Francois Romieu0e485152007-02-20 00:00:26 +01004894 RTL_W16(IntrMitigate, 0x5151);
4895
4896 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004897 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004898 tp->event_slow |= RxFIFOOver | PCSTimeout;
4899 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004900 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004901
4902 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4903
Francois Romieub8363902008-06-01 12:31:57 +02004904 rtl_set_rx_mode(dev);
4905
4906 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4907 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004908
4909 RTL_R8(IntrMask);
4910
Francois Romieu219a1e92008-06-28 11:58:39 +02004911 switch (tp->mac_version) {
4912 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004913 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004914 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004915
4916 case RTL_GIGA_MAC_VER_12:
4917 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004918 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004919 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004920
4921 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004922 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004923 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004924
4925 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004926 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004927 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004928
4929 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004930 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004931 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004932
Francois Romieu197ff762008-06-28 13:16:02 +02004933 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004934 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004935 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004936
Francois Romieu6fb07052008-06-29 11:54:28 +02004937 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004938 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004939 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004940
Francois Romieuef3386f2008-06-29 12:24:30 +02004941 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004942 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004943 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004944
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004945 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004946 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004947 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004948
Francois Romieu5b538df2008-07-20 16:22:45 +02004949 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004950 case RTL_GIGA_MAC_VER_26:
4951 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004952 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004953 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004954
françois romieue6de30d2011-01-03 15:08:37 +00004955 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004956 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004957 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004958
hayeswang4804b3b2011-03-21 01:50:29 +00004959 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004960 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004961 break;
4962
hayeswang01dc7fe2011-03-21 01:50:28 +00004963 case RTL_GIGA_MAC_VER_32:
4964 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004965 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004966 break;
4967 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004968 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004969 break;
françois romieue6de30d2011-01-03 15:08:37 +00004970
Hayes Wangc2218922011-09-06 16:55:18 +08004971 case RTL_GIGA_MAC_VER_35:
4972 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004973 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004974 break;
4975
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004976 case RTL_GIGA_MAC_VER_38:
4977 rtl_hw_start_8411(tp);
4978 break;
4979
Francois Romieu219a1e92008-06-28 11:58:39 +02004980 default:
4981 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4982 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004983 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004984 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004985
Francois Romieu0e485152007-02-20 00:00:26 +01004986 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4987
Francois Romieub8363902008-06-01 12:31:57 +02004988 RTL_W8(Cfg9346, Cfg9346_Lock);
4989
Francois Romieu2dd99532007-06-11 23:22:52 +02004990 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Francois Romieu2857ffb2008-08-02 21:08:49 +02004993#define R810X_CPCMD_QUIRK_MASK (\
4994 EnableBist | \
4995 Mac_dbgo_oe | \
4996 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004997 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004998 Force_txflow_en | \
4999 Cxpl_dbg_sel | \
5000 ASF | \
5001 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005002 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005003
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005004static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005005{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005006 void __iomem *ioaddr = tp->mmio_addr;
5007 struct pci_dev *pdev = tp->pci_dev;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005008 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005009 { 0x01, 0, 0x6e65 },
5010 { 0x02, 0, 0x091f },
5011 { 0x03, 0, 0xc2f9 },
5012 { 0x06, 0, 0xafb5 },
5013 { 0x07, 0, 0x0e00 },
5014 { 0x19, 0, 0xec80 },
5015 { 0x01, 0, 0x2e65 },
5016 { 0x01, 0, 0x6e65 }
5017 };
5018 u8 cfg1;
5019
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005020 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005021
5022 RTL_W8(DBG_REG, FIX_NAK_1);
5023
5024 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5025
5026 RTL_W8(Config1,
5027 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5028 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5029
5030 cfg1 = RTL_R8(Config1);
5031 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5032 RTL_W8(Config1, cfg1 & ~LEDS0);
5033
Francois Romieu2857ffb2008-08-02 21:08:49 +02005034 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5035}
5036
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005037static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005038{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005039 void __iomem *ioaddr = tp->mmio_addr;
5040 struct pci_dev *pdev = tp->pci_dev;
5041
5042 rtl_csi_access_enable_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005043
5044 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5045
5046 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5047 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005048}
5049
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005050static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005051{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005052 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005053
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005054 rtl_ephy_write(tp->mmio_addr, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005055}
5056
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005057static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005058{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005059 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005060 static const struct ephy_info e_info_8105e_1[] = {
5061 { 0x07, 0, 0x4000 },
5062 { 0x19, 0, 0x0200 },
5063 { 0x19, 0, 0x0020 },
5064 { 0x1e, 0, 0x2000 },
5065 { 0x03, 0, 0x0001 },
5066 { 0x19, 0, 0x0100 },
5067 { 0x19, 0, 0x0004 },
5068 { 0x0a, 0, 0x0020 }
5069 };
5070
Francois Romieucecb5fd2011-04-01 10:21:07 +02005071 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005072 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5073
Francois Romieucecb5fd2011-04-01 10:21:07 +02005074 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005075 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5076
5077 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005078 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005079
5080 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5081}
5082
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005083static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005084{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005085 void __iomem *ioaddr = tp->mmio_addr;
5086
5087 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005088 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5089}
5090
Hayes Wang7e18dca2012-03-30 14:33:02 +08005091static void rtl_hw_start_8402(struct rtl8169_private *tp)
5092{
5093 void __iomem *ioaddr = tp->mmio_addr;
5094 static const struct ephy_info e_info_8402[] = {
5095 { 0x19, 0xffff, 0xff64 },
5096 { 0x1e, 0, 0x4000 }
5097 };
5098
5099 rtl_csi_access_enable_2(tp);
5100
5101 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5102 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5103
5104 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
5105 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
5106
5107 rtl_ephy_init(ioaddr, e_info_8402, ARRAY_SIZE(e_info_8402));
5108
5109 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5110
5111 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5112 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5113 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5114 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5115 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5116 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5117 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00,
5118 ERIAR_EXGMAC);
5119}
5120
Francois Romieu07ce4062007-02-23 23:36:39 +01005121static void rtl_hw_start_8101(struct net_device *dev)
5122{
Francois Romieucdf1a602007-06-11 23:29:50 +02005123 struct rtl8169_private *tp = netdev_priv(dev);
5124 void __iomem *ioaddr = tp->mmio_addr;
5125 struct pci_dev *pdev = tp->pci_dev;
5126
Francois Romieuda78dbf2012-01-26 14:18:23 +01005127 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5128 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005129
Francois Romieucecb5fd2011-04-01 10:21:07 +02005130 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5131 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005132 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005133
5134 if (cap) {
5135 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5136 PCI_EXP_DEVCTL_NOSNOOP_EN);
5137 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005138 }
5139
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005140 RTL_W8(Cfg9346, Cfg9346_Unlock);
5141
Francois Romieu2857ffb2008-08-02 21:08:49 +02005142 switch (tp->mac_version) {
5143 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005144 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005145 break;
5146
5147 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005148 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005149 break;
5150
5151 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005152 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005153 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005154
5155 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005156 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005157 break;
5158 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005159 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005160 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005161
5162 case RTL_GIGA_MAC_VER_37:
5163 rtl_hw_start_8402(tp);
5164 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005165 }
5166
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005167 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005168
françois romieuf0298f82011-01-03 15:07:42 +00005169 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005170
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005171 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005172
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005173 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005174 RTL_W16(CPlusCmd, tp->cp_cmd);
5175
5176 RTL_W16(IntrMitigate, 0x0000);
5177
5178 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5179
5180 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5181 rtl_set_rx_tx_config_registers(tp);
5182
Francois Romieucdf1a602007-06-11 23:29:50 +02005183 RTL_R8(IntrMask);
5184
Francois Romieucdf1a602007-06-11 23:29:50 +02005185 rtl_set_rx_mode(dev);
5186
5187 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
5190static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5191{
Francois Romieud58d46b2011-05-03 16:38:29 +02005192 struct rtl8169_private *tp = netdev_priv(dev);
5193
5194 if (new_mtu < ETH_ZLEN ||
5195 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return -EINVAL;
5197
Francois Romieud58d46b2011-05-03 16:38:29 +02005198 if (new_mtu > ETH_DATA_LEN)
5199 rtl_hw_jumbo_enable(tp);
5200 else
5201 rtl_hw_jumbo_disable(tp);
5202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005204 netdev_update_features(dev);
5205
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207}
5208
5209static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5210{
Al Viro95e09182007-12-22 18:55:39 +00005211 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5213}
5214
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005215static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5216 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005218 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005219 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005220
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005221 kfree(*data_buff);
5222 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 rtl8169_make_unusable_by_asic(desc);
5224}
5225
5226static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5227{
5228 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5229
5230 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5231}
5232
5233static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5234 u32 rx_buf_sz)
5235{
5236 desc->addr = cpu_to_le64(mapping);
5237 wmb();
5238 rtl8169_mark_to_asic(desc, rx_buf_sz);
5239}
5240
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005241static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005243 return (void *)ALIGN((long)data, 16);
5244}
5245
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005246static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5247 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005248{
5249 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005251 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005252 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005253 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005255 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5256 if (!data)
5257 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005258
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005259 if (rtl8169_align(data) != data) {
5260 kfree(data);
5261 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5262 if (!data)
5263 return NULL;
5264 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005265
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005266 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005267 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005268 if (unlikely(dma_mapping_error(d, mapping))) {
5269 if (net_ratelimit())
5270 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005271 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
5274 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005275 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005276
5277err_out:
5278 kfree(data);
5279 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}
5281
5282static void rtl8169_rx_clear(struct rtl8169_private *tp)
5283{
Francois Romieu07d3f512007-02-21 22:40:46 +01005284 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
5286 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005287 if (tp->Rx_databuff[i]) {
5288 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 tp->RxDescArray + i);
5290 }
5291 }
5292}
5293
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005294static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005296 desc->opts1 |= cpu_to_le32(RingEnd);
5297}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005298
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005299static int rtl8169_rx_fill(struct rtl8169_private *tp)
5300{
5301 unsigned int i;
5302
5303 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005304 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005305
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005306 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005308
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005309 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005310 if (!data) {
5311 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005312 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005313 }
5314 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005317 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5318 return 0;
5319
5320err_out:
5321 rtl8169_rx_clear(tp);
5322 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323}
5324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325static int rtl8169_init_ring(struct net_device *dev)
5326{
5327 struct rtl8169_private *tp = netdev_priv(dev);
5328
5329 rtl8169_init_ring_indexes(tp);
5330
5331 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005332 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005334 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335}
5336
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005337static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 struct TxDesc *desc)
5339{
5340 unsigned int len = tx_skb->len;
5341
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005342 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 desc->opts1 = 0x00;
5345 desc->opts2 = 0x00;
5346 desc->addr = 0x00;
5347 tx_skb->len = 0;
5348}
5349
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005350static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5351 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
5353 unsigned int i;
5354
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005355 for (i = 0; i < n; i++) {
5356 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct ring_info *tx_skb = tp->tx_skb + entry;
5358 unsigned int len = tx_skb->len;
5359
5360 if (len) {
5361 struct sk_buff *skb = tx_skb->skb;
5362
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005363 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 tp->TxDescArray + entry);
5365 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005366 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 dev_kfree_skb(skb);
5368 tx_skb->skb = NULL;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005372}
5373
5374static void rtl8169_tx_clear(struct rtl8169_private *tp)
5375{
5376 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005378 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
Francois Romieu4422bcd2012-01-26 11:23:32 +01005381static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
David Howellsc4028952006-11-22 14:57:56 +00005383 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005384 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Francois Romieuda78dbf2012-01-26 14:18:23 +01005386 napi_disable(&tp->napi);
5387 netif_stop_queue(dev);
5388 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
françois romieuc7c2c392011-12-04 20:30:52 +00005390 rtl8169_hw_reset(tp);
5391
Francois Romieu56de4142011-03-15 17:29:31 +01005392 for (i = 0; i < NUM_RX_DESC; i++)
5393 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005396 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Francois Romieuda78dbf2012-01-26 14:18:23 +01005398 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005399 rtl_hw_start(dev);
5400 netif_wake_queue(dev);
5401 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
5404static void rtl8169_tx_timeout(struct net_device *dev)
5405{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005406 struct rtl8169_private *tp = netdev_priv(dev);
5407
5408 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
5411static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005412 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
5414 struct skb_shared_info *info = skb_shinfo(skb);
5415 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005416 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005417 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419 entry = tp->cur_tx;
5420 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005421 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 dma_addr_t mapping;
5423 u32 status, len;
5424 void *addr;
5425
5426 entry = (entry + 1) % NUM_TX_DESC;
5427
5428 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005429 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005430 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005431 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005432 if (unlikely(dma_mapping_error(d, mapping))) {
5433 if (net_ratelimit())
5434 netif_err(tp, drv, tp->dev,
5435 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005436 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Francois Romieucecb5fd2011-04-01 10:21:07 +02005439 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005440 status = opts[0] | len |
5441 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005444 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 txd->addr = cpu_to_le64(mapping);
5446
5447 tp->tx_skb[entry].len = len;
5448 }
5449
5450 if (cur_frag) {
5451 tp->tx_skb[entry].skb = skb;
5452 txd->opts1 |= cpu_to_le32(LastFrag);
5453 }
5454
5455 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005456
5457err_out:
5458 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5459 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460}
5461
Francois Romieu2b7b4312011-04-18 22:53:24 -07005462static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5463 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005465 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005466 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005467 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Francois Romieu2b7b4312011-04-18 22:53:24 -07005469 if (mss) {
5470 opts[0] |= TD_LSO;
5471 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5472 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005473 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005476 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005478 opts[offset] |= info->checksum.udp;
5479 else
5480 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482}
5483
Stephen Hemminger613573252009-08-31 19:50:58 +00005484static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5485 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
5487 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005488 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 struct TxDesc *txd = tp->TxDescArray + entry;
5490 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005491 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 dma_addr_t mapping;
5493 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005494 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005495 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005498 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005499 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 }
5501
5502 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005503 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005505 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005506 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005507 if (unlikely(dma_mapping_error(d, mapping))) {
5508 if (net_ratelimit())
5509 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005510 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
5513 tp->tx_skb[entry].len = len;
5514 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Francois Romieu2b7b4312011-04-18 22:53:24 -07005516 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5517 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005518
Francois Romieu2b7b4312011-04-18 22:53:24 -07005519 rtl8169_tso_csum(tp, skb, opts);
5520
5521 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005522 if (frags < 0)
5523 goto err_dma_1;
5524 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005525 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005526 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005527 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005528 tp->tx_skb[entry].skb = skb;
5529 }
5530
Francois Romieu2b7b4312011-04-18 22:53:24 -07005531 txd->opts2 = cpu_to_le32(opts[1]);
5532
Igor Maravic036dafa2012-03-05 00:01:25 +01005533 netdev_sent_queue(dev, skb->len);
5534
Richard Cochran5047fb52012-03-10 07:29:42 +00005535 skb_tx_timestamp(skb);
5536
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 wmb();
5538
Francois Romieucecb5fd2011-04-01 10:21:07 +02005539 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005540 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 txd->opts1 = cpu_to_le32(status);
5542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 tp->cur_tx += frags + 1;
5544
David Dillow4c020a92010-03-03 16:33:10 +00005545 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Francois Romieucecb5fd2011-04-01 10:21:07 +02005547 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Francois Romieuda78dbf2012-01-26 14:18:23 +01005549 mmiowb();
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005552 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5553 * not miss a ring update when it notices a stopped queue.
5554 */
5555 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005557 /* Sync with rtl_tx:
5558 * - publish queue status and cur_tx ring index (write barrier)
5559 * - refresh dirty_tx ring index (read barrier).
5560 * May the current thread have a pessimistic view of the ring
5561 * status and forget to wake up queue, a racing rtl_tx thread
5562 * can't.
5563 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005564 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5566 netif_wake_queue(dev);
5567 }
5568
Stephen Hemminger613573252009-08-31 19:50:58 +00005569 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005571err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005572 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005573err_dma_0:
5574 dev_kfree_skb(skb);
5575 dev->stats.tx_dropped++;
5576 return NETDEV_TX_OK;
5577
5578err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005580 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005581 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582}
5583
5584static void rtl8169_pcierr_interrupt(struct net_device *dev)
5585{
5586 struct rtl8169_private *tp = netdev_priv(dev);
5587 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 u16 pci_status, pci_cmd;
5589
5590 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5591 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5592
Joe Perchesbf82c182010-02-09 11:49:50 +00005593 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5594 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
5596 /*
5597 * The recovery sequence below admits a very elaborated explanation:
5598 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005599 * - I did not see what else could be done;
5600 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 *
5602 * Feel free to adjust to your needs.
5603 */
Francois Romieua27993f2006-12-18 00:04:19 +01005604 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005605 pci_cmd &= ~PCI_COMMAND_PARITY;
5606 else
5607 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5608
5609 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 pci_write_config_word(pdev, PCI_STATUS,
5612 pci_status & (PCI_STATUS_DETECTED_PARITY |
5613 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5614 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5615
5616 /* The infamous DAC f*ckup only happens at boot time */
5617 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005618 void __iomem *ioaddr = tp->mmio_addr;
5619
Joe Perchesbf82c182010-02-09 11:49:50 +00005620 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 tp->cp_cmd &= ~PCIDAC;
5622 RTL_W16(CPlusCmd, tp->cp_cmd);
5623 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 }
5625
françois romieue6de30d2011-01-03 15:08:37 +00005626 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005627
Francois Romieu98ddf982012-01-31 10:47:34 +01005628 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629}
5630
Igor Maravic036dafa2012-03-05 00:01:25 +01005631struct rtl_txc {
5632 int packets;
5633 int bytes;
5634};
5635
Francois Romieuda78dbf2012-01-26 14:18:23 +01005636static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
Igor Maravic036dafa2012-03-05 00:01:25 +01005638 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005640 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 dirty_tx = tp->dirty_tx;
5643 smp_rmb();
5644 tx_left = tp->cur_tx - dirty_tx;
5645
5646 while (tx_left > 0) {
5647 unsigned int entry = dirty_tx % NUM_TX_DESC;
5648 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 u32 status;
5650
5651 rmb();
5652 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5653 if (status & DescOwn)
5654 break;
5655
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005656 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5657 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005659 struct sk_buff *skb = tx_skb->skb;
5660
5661 txc.packets++;
5662 txc.bytes += skb->len;
5663 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 tx_skb->skb = NULL;
5665 }
5666 dirty_tx++;
5667 tx_left--;
5668 }
5669
Igor Maravic036dafa2012-03-05 00:01:25 +01005670 u64_stats_update_begin(&tx_stats->syncp);
5671 tx_stats->packets += txc.packets;
5672 tx_stats->bytes += txc.bytes;
5673 u64_stats_update_end(&tx_stats->syncp);
5674
5675 netdev_completed_queue(dev, txc.packets, txc.bytes);
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 if (tp->dirty_tx != dirty_tx) {
5678 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005679 /* Sync with rtl8169_start_xmit:
5680 * - publish dirty_tx ring index (write barrier)
5681 * - refresh cur_tx ring index and queue status (read barrier)
5682 * May the current thread miss the stopped queue condition,
5683 * a racing xmit thread can only have a right view of the
5684 * ring status.
5685 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005686 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 if (netif_queue_stopped(dev) &&
5688 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5689 netif_wake_queue(dev);
5690 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005691 /*
5692 * 8168 hack: TxPoll requests are lost when the Tx packets are
5693 * too close. Let's kick an extra TxPoll request when a burst
5694 * of start_xmit activity is detected (if it is not detected,
5695 * it is slow enough). -- FR
5696 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005697 if (tp->cur_tx != dirty_tx) {
5698 void __iomem *ioaddr = tp->mmio_addr;
5699
Francois Romieud78ae2d2007-08-26 20:08:19 +02005700 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 }
5703}
5704
Francois Romieu126fa4b2005-05-12 20:09:17 -04005705static inline int rtl8169_fragmented_frame(u32 status)
5706{
5707 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5708}
5709
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005710static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 u32 status = opts1 & RxProtoMask;
5713
5714 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005715 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 skb->ip_summed = CHECKSUM_UNNECESSARY;
5717 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005718 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005721static struct sk_buff *rtl8169_try_rx_copy(void *data,
5722 struct rtl8169_private *tp,
5723 int pkt_size,
5724 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005726 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005727 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005729 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005730 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005731 prefetch(data);
5732 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5733 if (skb)
5734 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005735 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5736
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005737 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738}
5739
Francois Romieuda78dbf2012-01-26 14:18:23 +01005740static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741{
5742 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005743 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 cur_rx = tp->cur_rx;
5746 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005747 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005749 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005751 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 u32 status;
5753
5754 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005755 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 if (status & DescOwn)
5758 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005759 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005760 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5761 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005762 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005764 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005766 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005767 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005768 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005769 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005770 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005771 if ((status & (RxRUNT | RxCRC)) &&
5772 !(status & (RxRWT | RxFOVF)) &&
5773 (dev->features & NETIF_F_RXALL))
5774 goto process_pkt;
5775
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005776 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005778 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005779 dma_addr_t addr;
5780 int pkt_size;
5781
5782process_pkt:
5783 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005784 if (likely(!(dev->features & NETIF_F_RXFCS)))
5785 pkt_size = (status & 0x00003fff) - 4;
5786 else
5787 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Francois Romieu126fa4b2005-05-12 20:09:17 -04005789 /*
5790 * The driver does not support incoming fragmented
5791 * frames. They are seen as a symptom of over-mtu
5792 * sized frames.
5793 */
5794 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005795 dev->stats.rx_dropped++;
5796 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005797 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005798 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005799 }
5800
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005801 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5802 tp, pkt_size, addr);
5803 rtl8169_mark_to_asic(desc, rx_buf_sz);
5804 if (!skb) {
5805 dev->stats.rx_dropped++;
5806 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 }
5808
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005809 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 skb_put(skb, pkt_size);
5811 skb->protocol = eth_type_trans(skb, dev);
5812
Francois Romieu7a8fc772011-03-01 17:18:33 +01005813 rtl8169_rx_vlan_tag(desc, skb);
5814
Francois Romieu56de4142011-03-15 17:29:31 +01005815 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Junchang Wang8027aa22012-03-04 23:30:32 +01005817 u64_stats_update_begin(&tp->rx_stats.syncp);
5818 tp->rx_stats.packets++;
5819 tp->rx_stats.bytes += pkt_size;
5820 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005822
5823 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005824 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005825 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5826 desc->opts2 = 0;
5827 cur_rx++;
5828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 }
5830
5831 count = cur_rx - tp->cur_rx;
5832 tp->cur_rx = cur_rx;
5833
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005834 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
5836 return count;
5837}
5838
Francois Romieu07d3f512007-02-21 22:40:46 +01005839static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840{
Francois Romieu07d3f512007-02-21 22:40:46 +01005841 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005844 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005846 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005847 if (status && status != 0xffff) {
5848 status &= RTL_EVENT_NAPI | tp->event_slow;
5849 if (status) {
5850 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005851
Francois Romieuda78dbf2012-01-26 14:18:23 +01005852 rtl_irq_disable(tp);
5853 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return IRQ_RETVAL(handled);
5857}
5858
Francois Romieuda78dbf2012-01-26 14:18:23 +01005859/*
5860 * Workqueue context.
5861 */
5862static void rtl_slow_event_work(struct rtl8169_private *tp)
5863{
5864 struct net_device *dev = tp->dev;
5865 u16 status;
5866
5867 status = rtl_get_events(tp) & tp->event_slow;
5868 rtl_ack_events(tp, status);
5869
5870 if (unlikely(status & RxFIFOOver)) {
5871 switch (tp->mac_version) {
5872 /* Work around for rx fifo overflow */
5873 case RTL_GIGA_MAC_VER_11:
5874 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005875 /* XXX - Hack alert. See rtl_task(). */
5876 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005877 default:
5878 break;
5879 }
5880 }
5881
5882 if (unlikely(status & SYSErr))
5883 rtl8169_pcierr_interrupt(dev);
5884
5885 if (status & LinkChg)
5886 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5887
5888 napi_disable(&tp->napi);
5889 rtl_irq_disable(tp);
5890
5891 napi_enable(&tp->napi);
5892 napi_schedule(&tp->napi);
5893}
5894
Francois Romieu4422bcd2012-01-26 11:23:32 +01005895static void rtl_task(struct work_struct *work)
5896{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005897 static const struct {
5898 int bitnr;
5899 void (*action)(struct rtl8169_private *);
5900 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005901 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005902 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5903 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5904 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5905 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005906 struct rtl8169_private *tp =
5907 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005908 struct net_device *dev = tp->dev;
5909 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005910
Francois Romieuda78dbf2012-01-26 14:18:23 +01005911 rtl_lock_work(tp);
5912
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005913 if (!netif_running(dev) ||
5914 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005915 goto out_unlock;
5916
5917 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5918 bool pending;
5919
Francois Romieuda78dbf2012-01-26 14:18:23 +01005920 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005921 if (pending)
5922 rtl_work[i].action(tp);
5923 }
5924
5925out_unlock:
5926 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005927}
5928
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005929static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005931 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5932 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005933 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5934 int work_done= 0;
5935 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Francois Romieuda78dbf2012-01-26 14:18:23 +01005937 status = rtl_get_events(tp);
5938 rtl_ack_events(tp, status & ~tp->event_slow);
5939
5940 if (status & RTL_EVENT_NAPI_RX)
5941 work_done = rtl_rx(dev, tp, (u32) budget);
5942
5943 if (status & RTL_EVENT_NAPI_TX)
5944 rtl_tx(dev, tp);
5945
5946 if (status & tp->event_slow) {
5947 enable_mask &= ~tp->event_slow;
5948
5949 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005952 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005953 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005954
Francois Romieuda78dbf2012-01-26 14:18:23 +01005955 rtl_irq_enable(tp, enable_mask);
5956 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 }
5958
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005959 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
Francois Romieu523a6092008-09-10 22:28:56 +02005962static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5963{
5964 struct rtl8169_private *tp = netdev_priv(dev);
5965
5966 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5967 return;
5968
5969 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5970 RTL_W32(RxMissed, 0);
5971}
5972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973static void rtl8169_down(struct net_device *dev)
5974{
5975 struct rtl8169_private *tp = netdev_priv(dev);
5976 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Francois Romieu4876cc12011-03-11 21:07:11 +01005978 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005980 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005981 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Hayes Wang92fc43b2011-07-06 15:58:03 +08005983 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005984 /*
5985 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005986 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5987 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005988 */
Francois Romieu523a6092008-09-10 22:28:56 +02005989 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005992 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 rtl8169_tx_clear(tp);
5995
5996 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005997
5998 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
6001static int rtl8169_close(struct net_device *dev)
6002{
6003 struct rtl8169_private *tp = netdev_priv(dev);
6004 struct pci_dev *pdev = tp->pci_dev;
6005
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006006 pm_runtime_get_sync(&pdev->dev);
6007
Francois Romieucecb5fd2011-04-01 10:21:07 +02006008 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006009 rtl8169_update_counters(dev);
6010
Francois Romieuda78dbf2012-01-26 14:18:23 +01006011 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006012 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006013
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006015 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006017 free_irq(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006019 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6020 tp->RxPhyAddr);
6021 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6022 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 tp->TxDescArray = NULL;
6024 tp->RxDescArray = NULL;
6025
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006026 pm_runtime_put_sync(&pdev->dev);
6027
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 return 0;
6029}
6030
Francois Romieudc1c00c2012-03-08 10:06:18 +01006031#ifdef CONFIG_NET_POLL_CONTROLLER
6032static void rtl8169_netpoll(struct net_device *dev)
6033{
6034 struct rtl8169_private *tp = netdev_priv(dev);
6035
6036 rtl8169_interrupt(tp->pci_dev->irq, dev);
6037}
6038#endif
6039
Francois Romieudf43ac72012-03-08 09:48:40 +01006040static int rtl_open(struct net_device *dev)
6041{
6042 struct rtl8169_private *tp = netdev_priv(dev);
6043 void __iomem *ioaddr = tp->mmio_addr;
6044 struct pci_dev *pdev = tp->pci_dev;
6045 int retval = -ENOMEM;
6046
6047 pm_runtime_get_sync(&pdev->dev);
6048
6049 /*
6050 * Rx and Tx desscriptors needs 256 bytes alignment.
6051 * dma_alloc_coherent provides more.
6052 */
6053 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6054 &tp->TxPhyAddr, GFP_KERNEL);
6055 if (!tp->TxDescArray)
6056 goto err_pm_runtime_put;
6057
6058 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6059 &tp->RxPhyAddr, GFP_KERNEL);
6060 if (!tp->RxDescArray)
6061 goto err_free_tx_0;
6062
6063 retval = rtl8169_init_ring(dev);
6064 if (retval < 0)
6065 goto err_free_rx_1;
6066
6067 INIT_WORK(&tp->wk.work, rtl_task);
6068
6069 smp_mb();
6070
6071 rtl_request_firmware(tp);
6072
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006073 retval = request_irq(pdev->irq, rtl8169_interrupt,
Francois Romieudf43ac72012-03-08 09:48:40 +01006074 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
6075 dev->name, dev);
6076 if (retval < 0)
6077 goto err_release_fw_2;
6078
6079 rtl_lock_work(tp);
6080
6081 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6082
6083 napi_enable(&tp->napi);
6084
6085 rtl8169_init_phy(dev, tp);
6086
6087 __rtl8169_set_features(dev, dev->features);
6088
6089 rtl_pll_power_up(tp);
6090
6091 rtl_hw_start(dev);
6092
6093 netif_start_queue(dev);
6094
6095 rtl_unlock_work(tp);
6096
6097 tp->saved_wolopts = 0;
6098 pm_runtime_put_noidle(&pdev->dev);
6099
6100 rtl8169_check_link_status(dev, tp, ioaddr);
6101out:
6102 return retval;
6103
6104err_release_fw_2:
6105 rtl_release_firmware(tp);
6106 rtl8169_rx_clear(tp);
6107err_free_rx_1:
6108 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6109 tp->RxPhyAddr);
6110 tp->RxDescArray = NULL;
6111err_free_tx_0:
6112 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6113 tp->TxPhyAddr);
6114 tp->TxDescArray = NULL;
6115err_pm_runtime_put:
6116 pm_runtime_put_noidle(&pdev->dev);
6117 goto out;
6118}
6119
Junchang Wang8027aa22012-03-04 23:30:32 +01006120static struct rtnl_link_stats64 *
6121rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122{
6123 struct rtl8169_private *tp = netdev_priv(dev);
6124 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006125 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Francois Romieuda78dbf2012-01-26 14:18:23 +01006127 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006128 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006129
Junchang Wang8027aa22012-03-04 23:30:32 +01006130 do {
6131 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6132 stats->rx_packets = tp->rx_stats.packets;
6133 stats->rx_bytes = tp->rx_stats.bytes;
6134 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6135
6136
6137 do {
6138 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6139 stats->tx_packets = tp->tx_stats.packets;
6140 stats->tx_bytes = tp->tx_stats.bytes;
6141 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6142
6143 stats->rx_dropped = dev->stats.rx_dropped;
6144 stats->tx_dropped = dev->stats.tx_dropped;
6145 stats->rx_length_errors = dev->stats.rx_length_errors;
6146 stats->rx_errors = dev->stats.rx_errors;
6147 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6148 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6149 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6150
6151 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152}
6153
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006154static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006155{
françois romieu065c27c2011-01-03 15:08:12 +00006156 struct rtl8169_private *tp = netdev_priv(dev);
6157
Francois Romieu5d06a992006-02-23 00:47:58 +01006158 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006159 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006160
6161 netif_device_detach(dev);
6162 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006163
6164 rtl_lock_work(tp);
6165 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006166 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006167 rtl_unlock_work(tp);
6168
6169 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006170}
Francois Romieu5d06a992006-02-23 00:47:58 +01006171
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006172#ifdef CONFIG_PM
6173
6174static int rtl8169_suspend(struct device *device)
6175{
6176 struct pci_dev *pdev = to_pci_dev(device);
6177 struct net_device *dev = pci_get_drvdata(pdev);
6178
6179 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006180
Francois Romieu5d06a992006-02-23 00:47:58 +01006181 return 0;
6182}
6183
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006184static void __rtl8169_resume(struct net_device *dev)
6185{
françois romieu065c27c2011-01-03 15:08:12 +00006186 struct rtl8169_private *tp = netdev_priv(dev);
6187
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006188 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006189
6190 rtl_pll_power_up(tp);
6191
Artem Savkovcff4c162012-04-03 10:29:11 +00006192 rtl_lock_work(tp);
6193 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006194 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006195 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006196
Francois Romieu98ddf982012-01-31 10:47:34 +01006197 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006198}
6199
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006200static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006201{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006202 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006203 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006204 struct rtl8169_private *tp = netdev_priv(dev);
6205
6206 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006207
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006208 if (netif_running(dev))
6209 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006210
Francois Romieu5d06a992006-02-23 00:47:58 +01006211 return 0;
6212}
6213
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006214static int rtl8169_runtime_suspend(struct device *device)
6215{
6216 struct pci_dev *pdev = to_pci_dev(device);
6217 struct net_device *dev = pci_get_drvdata(pdev);
6218 struct rtl8169_private *tp = netdev_priv(dev);
6219
6220 if (!tp->TxDescArray)
6221 return 0;
6222
Francois Romieuda78dbf2012-01-26 14:18:23 +01006223 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006224 tp->saved_wolopts = __rtl8169_get_wol(tp);
6225 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006226 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006227
6228 rtl8169_net_suspend(dev);
6229
6230 return 0;
6231}
6232
6233static int rtl8169_runtime_resume(struct device *device)
6234{
6235 struct pci_dev *pdev = to_pci_dev(device);
6236 struct net_device *dev = pci_get_drvdata(pdev);
6237 struct rtl8169_private *tp = netdev_priv(dev);
6238
6239 if (!tp->TxDescArray)
6240 return 0;
6241
Francois Romieuda78dbf2012-01-26 14:18:23 +01006242 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006243 __rtl8169_set_wol(tp, tp->saved_wolopts);
6244 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006245 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006246
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006247 rtl8169_init_phy(dev, tp);
6248
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006249 __rtl8169_resume(dev);
6250
6251 return 0;
6252}
6253
6254static int rtl8169_runtime_idle(struct device *device)
6255{
6256 struct pci_dev *pdev = to_pci_dev(device);
6257 struct net_device *dev = pci_get_drvdata(pdev);
6258 struct rtl8169_private *tp = netdev_priv(dev);
6259
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006260 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006261}
6262
Alexey Dobriyan47145212009-12-14 18:00:08 -08006263static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006264 .suspend = rtl8169_suspend,
6265 .resume = rtl8169_resume,
6266 .freeze = rtl8169_suspend,
6267 .thaw = rtl8169_resume,
6268 .poweroff = rtl8169_suspend,
6269 .restore = rtl8169_resume,
6270 .runtime_suspend = rtl8169_runtime_suspend,
6271 .runtime_resume = rtl8169_runtime_resume,
6272 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006273};
6274
6275#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6276
6277#else /* !CONFIG_PM */
6278
6279#define RTL8169_PM_OPS NULL
6280
6281#endif /* !CONFIG_PM */
6282
David S. Miller1805b2f2011-10-24 18:18:09 -04006283static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6284{
6285 void __iomem *ioaddr = tp->mmio_addr;
6286
6287 /* WoL fails with 8168b when the receiver is disabled. */
6288 switch (tp->mac_version) {
6289 case RTL_GIGA_MAC_VER_11:
6290 case RTL_GIGA_MAC_VER_12:
6291 case RTL_GIGA_MAC_VER_17:
6292 pci_clear_master(tp->pci_dev);
6293
6294 RTL_W8(ChipCmd, CmdRxEnb);
6295 /* PCI commit */
6296 RTL_R8(ChipCmd);
6297 break;
6298 default:
6299 break;
6300 }
6301}
6302
Francois Romieu1765f952008-09-13 17:21:40 +02006303static void rtl_shutdown(struct pci_dev *pdev)
6304{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006305 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006306 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006307 struct device *d = &pdev->dev;
6308
6309 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006310
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006311 rtl8169_net_suspend(dev);
6312
Francois Romieucecb5fd2011-04-01 10:21:07 +02006313 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006314 rtl_rar_set(tp, dev->perm_addr);
6315
Hayes Wang92fc43b2011-07-06 15:58:03 +08006316 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006317
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006318 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006319 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6320 rtl_wol_suspend_quirk(tp);
6321 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006322 }
6323
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006324 pci_wake_from_d3(pdev, true);
6325 pci_set_power_state(pdev, PCI_D3hot);
6326 }
françois romieu2a15cd22012-03-06 01:14:12 +00006327
6328 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006329}
Francois Romieu5d06a992006-02-23 00:47:58 +01006330
Francois Romieue27566e2012-03-08 09:54:01 +01006331static void __devexit rtl_remove_one(struct pci_dev *pdev)
6332{
6333 struct net_device *dev = pci_get_drvdata(pdev);
6334 struct rtl8169_private *tp = netdev_priv(dev);
6335
6336 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6337 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6338 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6339 rtl8168_driver_stop(tp);
6340 }
6341
6342 cancel_work_sync(&tp->wk.work);
6343
6344 unregister_netdev(dev);
6345
6346 rtl_release_firmware(tp);
6347
6348 if (pci_dev_run_wake(pdev))
6349 pm_runtime_get_noresume(&pdev->dev);
6350
6351 /* restore original MAC address */
6352 rtl_rar_set(tp, dev->perm_addr);
6353
6354 rtl_disable_msi(pdev, tp);
6355 rtl8169_release_board(pdev, dev, tp->mmio_addr);
6356 pci_set_drvdata(pdev, NULL);
6357}
6358
Francois Romieufa9c3852012-03-08 10:01:50 +01006359static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006360 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006361 .ndo_stop = rtl8169_close,
6362 .ndo_get_stats64 = rtl8169_get_stats64,
6363 .ndo_start_xmit = rtl8169_start_xmit,
6364 .ndo_tx_timeout = rtl8169_tx_timeout,
6365 .ndo_validate_addr = eth_validate_addr,
6366 .ndo_change_mtu = rtl8169_change_mtu,
6367 .ndo_fix_features = rtl8169_fix_features,
6368 .ndo_set_features = rtl8169_set_features,
6369 .ndo_set_mac_address = rtl_set_mac_address,
6370 .ndo_do_ioctl = rtl8169_ioctl,
6371 .ndo_set_rx_mode = rtl_set_rx_mode,
6372#ifdef CONFIG_NET_POLL_CONTROLLER
6373 .ndo_poll_controller = rtl8169_netpoll,
6374#endif
6375
6376};
6377
Francois Romieu31fa8b12012-03-08 10:09:40 +01006378static const struct rtl_cfg_info {
6379 void (*hw_start)(struct net_device *);
6380 unsigned int region;
6381 unsigned int align;
6382 u16 event_slow;
6383 unsigned features;
6384 u8 default_ver;
6385} rtl_cfg_infos [] = {
6386 [RTL_CFG_0] = {
6387 .hw_start = rtl_hw_start_8169,
6388 .region = 1,
6389 .align = 0,
6390 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6391 .features = RTL_FEATURE_GMII,
6392 .default_ver = RTL_GIGA_MAC_VER_01,
6393 },
6394 [RTL_CFG_1] = {
6395 .hw_start = rtl_hw_start_8168,
6396 .region = 2,
6397 .align = 8,
6398 .event_slow = SYSErr | LinkChg | RxOverflow,
6399 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
6400 .default_ver = RTL_GIGA_MAC_VER_11,
6401 },
6402 [RTL_CFG_2] = {
6403 .hw_start = rtl_hw_start_8101,
6404 .region = 2,
6405 .align = 8,
6406 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
6407 PCSTimeout,
6408 .features = RTL_FEATURE_MSI,
6409 .default_ver = RTL_GIGA_MAC_VER_13,
6410 }
6411};
6412
6413/* Cfg9346_Unlock assumed. */
6414static unsigned rtl_try_msi(struct rtl8169_private *tp,
6415 const struct rtl_cfg_info *cfg)
6416{
6417 void __iomem *ioaddr = tp->mmio_addr;
6418 unsigned msi = 0;
6419 u8 cfg2;
6420
6421 cfg2 = RTL_R8(Config2) & ~MSIEnable;
6422 if (cfg->features & RTL_FEATURE_MSI) {
6423 if (pci_enable_msi(tp->pci_dev)) {
6424 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
6425 } else {
6426 cfg2 |= MSIEnable;
6427 msi = RTL_FEATURE_MSI;
6428 }
6429 }
6430 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6431 RTL_W8(Config2, cfg2);
6432 return msi;
6433}
6434
Francois Romieu3b6cf252012-03-08 09:59:04 +01006435static int __devinit
6436rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6437{
6438 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
6439 const unsigned int region = cfg->region;
6440 struct rtl8169_private *tp;
6441 struct mii_if_info *mii;
6442 struct net_device *dev;
6443 void __iomem *ioaddr;
6444 int chipset, i;
6445 int rc;
6446
6447 if (netif_msg_drv(&debug)) {
6448 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
6449 MODULENAME, RTL8169_VERSION);
6450 }
6451
6452 dev = alloc_etherdev(sizeof (*tp));
6453 if (!dev) {
6454 rc = -ENOMEM;
6455 goto out;
6456 }
6457
6458 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006459 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006460 tp = netdev_priv(dev);
6461 tp->dev = dev;
6462 tp->pci_dev = pdev;
6463 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
6464
6465 mii = &tp->mii;
6466 mii->dev = dev;
6467 mii->mdio_read = rtl_mdio_read;
6468 mii->mdio_write = rtl_mdio_write;
6469 mii->phy_id_mask = 0x1f;
6470 mii->reg_num_mask = 0x1f;
6471 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
6472
6473 /* disable ASPM completely as that cause random device stop working
6474 * problems as well as full system hangs for some PCIe devices users */
6475 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6476 PCIE_LINK_STATE_CLKPM);
6477
6478 /* enable device (incl. PCI PM wakeup and hotplug setup) */
6479 rc = pci_enable_device(pdev);
6480 if (rc < 0) {
6481 netif_err(tp, probe, dev, "enable failure\n");
6482 goto err_out_free_dev_1;
6483 }
6484
6485 if (pci_set_mwi(pdev) < 0)
6486 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
6487
6488 /* make sure PCI base addr 1 is MMIO */
6489 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
6490 netif_err(tp, probe, dev,
6491 "region #%d not an MMIO resource, aborting\n",
6492 region);
6493 rc = -ENODEV;
6494 goto err_out_mwi_2;
6495 }
6496
6497 /* check for weird/broken PCI region reporting */
6498 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
6499 netif_err(tp, probe, dev,
6500 "Invalid PCI region size(s), aborting\n");
6501 rc = -ENODEV;
6502 goto err_out_mwi_2;
6503 }
6504
6505 rc = pci_request_regions(pdev, MODULENAME);
6506 if (rc < 0) {
6507 netif_err(tp, probe, dev, "could not request regions\n");
6508 goto err_out_mwi_2;
6509 }
6510
6511 tp->cp_cmd = RxChkSum;
6512
6513 if ((sizeof(dma_addr_t) > 4) &&
6514 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
6515 tp->cp_cmd |= PCIDAC;
6516 dev->features |= NETIF_F_HIGHDMA;
6517 } else {
6518 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6519 if (rc < 0) {
6520 netif_err(tp, probe, dev, "DMA configuration failed\n");
6521 goto err_out_free_res_3;
6522 }
6523 }
6524
6525 /* ioremap MMIO region */
6526 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
6527 if (!ioaddr) {
6528 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
6529 rc = -EIO;
6530 goto err_out_free_res_3;
6531 }
6532 tp->mmio_addr = ioaddr;
6533
6534 if (!pci_is_pcie(pdev))
6535 netif_info(tp, probe, dev, "not PCI Express\n");
6536
6537 /* Identify chip attached to board */
6538 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
6539
6540 rtl_init_rxcfg(tp);
6541
6542 rtl_irq_disable(tp);
6543
6544 rtl_hw_reset(tp);
6545
6546 rtl_ack_events(tp, 0xffff);
6547
6548 pci_set_master(pdev);
6549
6550 /*
6551 * Pretend we are using VLANs; This bypasses a nasty bug where
6552 * Interrupts stop flowing on high load on 8110SCd controllers.
6553 */
6554 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6555 tp->cp_cmd |= RxVlan;
6556
6557 rtl_init_mdio_ops(tp);
6558 rtl_init_pll_power_ops(tp);
6559 rtl_init_jumbo_ops(tp);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08006560 rtl_init_csi_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006561
6562 rtl8169_print_mac_version(tp);
6563
6564 chipset = tp->mac_version;
6565 tp->txd_version = rtl_chip_infos[chipset].txd_version;
6566
6567 RTL_W8(Cfg9346, Cfg9346_Unlock);
6568 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
6569 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
6570 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
6571 tp->features |= RTL_FEATURE_WOL;
6572 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
6573 tp->features |= RTL_FEATURE_WOL;
6574 tp->features |= rtl_try_msi(tp, cfg);
6575 RTL_W8(Cfg9346, Cfg9346_Lock);
6576
6577 if (rtl_tbi_enabled(tp)) {
6578 tp->set_speed = rtl8169_set_speed_tbi;
6579 tp->get_settings = rtl8169_gset_tbi;
6580 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
6581 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
6582 tp->link_ok = rtl8169_tbi_link_ok;
6583 tp->do_ioctl = rtl_tbi_ioctl;
6584 } else {
6585 tp->set_speed = rtl8169_set_speed_xmii;
6586 tp->get_settings = rtl8169_gset_xmii;
6587 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
6588 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
6589 tp->link_ok = rtl8169_xmii_link_ok;
6590 tp->do_ioctl = rtl_xmii_ioctl;
6591 }
6592
6593 mutex_init(&tp->wk.mutex);
6594
6595 /* Get MAC address */
6596 for (i = 0; i < ETH_ALEN; i++)
6597 dev->dev_addr[i] = RTL_R8(MAC0 + i);
6598 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
6599
6600 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
6601 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006602
6603 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
6604
6605 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6606 * properly for all devices */
6607 dev->features |= NETIF_F_RXCSUM |
6608 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6609
6610 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6611 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
6612 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6613 NETIF_F_HIGHDMA;
6614
6615 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
6616 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
6617 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
6618
6619 dev->hw_features |= NETIF_F_RXALL;
6620 dev->hw_features |= NETIF_F_RXFCS;
6621
6622 tp->hw_start = cfg->hw_start;
6623 tp->event_slow = cfg->event_slow;
6624
6625 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
6626 ~(RxBOVF | RxFOVF) : ~0;
6627
6628 init_timer(&tp->timer);
6629 tp->timer.data = (unsigned long) dev;
6630 tp->timer.function = rtl8169_phy_timer;
6631
6632 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
6633
6634 rc = register_netdev(dev);
6635 if (rc < 0)
6636 goto err_out_msi_4;
6637
6638 pci_set_drvdata(pdev, dev);
6639
Francois Romieu92a7c4e2012-03-10 10:42:12 +01006640 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
6641 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
6642 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006643 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
6644 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
6645 "tx checksumming: %s]\n",
6646 rtl_chip_infos[chipset].jumbo_max,
6647 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
6648 }
6649
6650 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
6651 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
6652 tp->mac_version == RTL_GIGA_MAC_VER_31) {
6653 rtl8168_driver_start(tp);
6654 }
6655
6656 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
6657
6658 if (pci_dev_run_wake(pdev))
6659 pm_runtime_put_noidle(&pdev->dev);
6660
6661 netif_carrier_off(dev);
6662
6663out:
6664 return rc;
6665
6666err_out_msi_4:
6667 rtl_disable_msi(pdev, tp);
6668 iounmap(ioaddr);
6669err_out_free_res_3:
6670 pci_release_regions(pdev);
6671err_out_mwi_2:
6672 pci_clear_mwi(pdev);
6673 pci_disable_device(pdev);
6674err_out_free_dev_1:
6675 free_netdev(dev);
6676 goto out;
6677}
6678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679static struct pci_driver rtl8169_pci_driver = {
6680 .name = MODULENAME,
6681 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006682 .probe = rtl_init_one,
Francois Romieue27566e2012-03-08 09:54:01 +01006683 .remove = __devexit_p(rtl_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006684 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006685 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686};
6687
Francois Romieu07d3f512007-02-21 22:40:46 +01006688static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689{
Jeff Garzik29917622006-08-19 17:48:59 -04006690 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691}
6692
Francois Romieu07d3f512007-02-21 22:40:46 +01006693static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694{
6695 pci_unregister_driver(&rtl8169_pci_driver);
6696}
6697
6698module_init(rtl8169_init_module);
6699module_exit(rtl8169_cleanup_module);