blob: c04df3d2b2a984e054df3da419c62f26eaac697e [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
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef RTL8169_DEBUG
50#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 if (!(expr)) { \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070053 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 }
Joe Perches06fa7352007-10-18 21:15:00 +020055#define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define assert(expr) do {} while (0)
59#define dprintk(fmt, args...) do {} while (0)
60#endif /* RTL8169_DEBUG */
61
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800134 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800135 RTL_GIGA_MAC_VER_35,
136 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200137 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Francois Romieu2b7b4312011-04-18 22:53:24 -0700140enum rtl_tx_desc_version {
141 RTL_TD_0 = 0,
142 RTL_TD_1 = 1,
143};
144
Francois Romieud58d46b2011-05-03 16:38:29 +0200145#define JUMBO_1K ETH_DATA_LEN
146#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
150
151#define _R(NAME,TD,FW,SZ,B) { \
152 .name = NAME, \
153 .txd_version = TD, \
154 .fw_name = FW, \
155 .jumbo_max = SZ, \
156 .jumbo_tx_csum = B \
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800159static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700161 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200162 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200163 u16 jumbo_max;
164 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165} rtl_chip_infos[] = {
166 /* PCI devices. */
167 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200168 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200171 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200172 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200173 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200178 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 /* PCI-E devices. */
180 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200203 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
218 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200225 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
228 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_30] =
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_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
236 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
239 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
242 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800243 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
248 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250#undef _R
251
Francois Romieubcf0bf92006-07-26 23:14:13 +0200252enum cfg_version {
253 RTL_CFG_0 = 0x00,
254 RTL_CFG_1,
255 RTL_CFG_2
256};
257
Francois Romieu07ce4062007-02-23 23:36:39 +0100258static void rtl_hw_start_8169(struct net_device *);
259static void rtl_hw_start_8168(struct net_device *);
260static void rtl_hw_start_8101(struct net_device *);
261
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000262static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000269 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200271 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
272 { PCI_VENDOR_ID_LINKSYS, 0x1032,
273 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100274 { 0x0001, 0x8168,
275 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0,},
277};
278
279MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
280
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000281static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700282static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200283static struct {
284 u32 msg_enable;
285} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Francois Romieu07d3f512007-02-21 22:40:46 +0100287enum rtl_registers {
288 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100289 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100290 MAR0 = 8, /* Multicast filter. */
291 CounterAddrLow = 0x10,
292 CounterAddrHigh = 0x14,
293 TxDescStartAddrLow = 0x20,
294 TxDescStartAddrHigh = 0x24,
295 TxHDescStartAddrLow = 0x28,
296 TxHDescStartAddrHigh = 0x2c,
297 FLASH = 0x30,
298 ERSR = 0x36,
299 ChipCmd = 0x37,
300 TxPoll = 0x38,
301 IntrMask = 0x3c,
302 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700303
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800304 TxConfig = 0x40,
305#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
306#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
307
308 RxConfig = 0x44,
309#define RX128_INT_EN (1 << 15) /* 8111c and later */
310#define RX_MULTI_EN (1 << 14) /* 8111c only */
311#define RXCFG_FIFO_SHIFT 13
312 /* No threshold before first PCI xfer */
313#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
314#define RXCFG_DMA_SHIFT 8
315 /* Unlimited maximum PCI burst. */
316#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700317
Francois Romieu07d3f512007-02-21 22:40:46 +0100318 RxMissed = 0x4c,
319 Cfg9346 = 0x50,
320 Config0 = 0x51,
321 Config1 = 0x52,
322 Config2 = 0x53,
323 Config3 = 0x54,
324 Config4 = 0x55,
325 Config5 = 0x56,
326 MultiIntr = 0x5c,
327 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100328 PHYstatus = 0x6c,
329 RxMaxSize = 0xda,
330 CPlusCmd = 0xe0,
331 IntrMitigate = 0xe2,
332 RxDescAddrLow = 0xe4,
333 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000334 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
335
336#define NoEarlyTx 0x3f /* Max value : no early transmit. */
337
338 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
339
340#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800341#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000342
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 FuncEvent = 0xf0,
344 FuncEventMask = 0xf4,
345 FuncPresetState = 0xf8,
346 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
Francois Romieuf162a5d2008-06-01 22:37:49 +0200349enum rtl8110_registers {
350 TBICSR = 0x64,
351 TBI_ANAR = 0x68,
352 TBI_LPAR = 0x6a,
353};
354
355enum rtl8168_8101_registers {
356 CSIDR = 0x64,
357 CSIAR = 0x68,
358#define CSIAR_FLAG 0x80000000
359#define CSIAR_WRITE_CMD 0x80000000
360#define CSIAR_BYTE_ENABLE 0x0f
361#define CSIAR_BYTE_ENABLE_SHIFT 12
362#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000363 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200364 EPHYAR = 0x80,
365#define EPHYAR_FLAG 0x80000000
366#define EPHYAR_WRITE_CMD 0x80000000
367#define EPHYAR_REG_MASK 0x1f
368#define EPHYAR_REG_SHIFT 16
369#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800370 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200372 DBG_REG = 0xd1,
373#define FIX_NAK_1 (1 << 4)
374#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800375 TWSI = 0xd2,
376 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800377#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800378#define EN_NDP (1 << 3)
379#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000380 EFUSEAR = 0xdc,
381#define EFUSEAR_FLAG 0x80000000
382#define EFUSEAR_WRITE_CMD 0x80000000
383#define EFUSEAR_READ_CMD 0x00000000
384#define EFUSEAR_REG_MASK 0x03ff
385#define EFUSEAR_REG_SHIFT 8
386#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387};
388
françois romieuc0e45c12011-01-03 15:08:04 +0000389enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800390 LED_FREQ = 0x1a,
391 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000392 ERIDR = 0x70,
393 ERIAR = 0x74,
394#define ERIAR_FLAG 0x80000000
395#define ERIAR_WRITE_CMD 0x80000000
396#define ERIAR_READ_CMD 0x00000000
397#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000398#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800399#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
400#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
401#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
402#define ERIAR_MASK_SHIFT 12
403#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
404#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
405#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000406 EPHY_RXER_NUM = 0x7c,
407 OCPDR = 0xb0, /* OCP GPHY access */
408#define OCPDR_WRITE_CMD 0x80000000
409#define OCPDR_READ_CMD 0x00000000
410#define OCPDR_REG_MASK 0x7f
411#define OCPDR_GPHY_REG_SHIFT 16
412#define OCPDR_DATA_MASK 0xffff
413 OCPAR = 0xb4,
414#define OCPAR_FLAG 0x80000000
415#define OCPAR_GPHY_WRITE_CMD 0x8000f060
416#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000417 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
418 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200419#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800420#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000421};
422
Francois Romieu07d3f512007-02-21 22:40:46 +0100423enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100425 SYSErr = 0x8000,
426 PCSTimeout = 0x4000,
427 SWInt = 0x0100,
428 TxDescUnavail = 0x0080,
429 RxFIFOOver = 0x0040,
430 LinkChg = 0x0020,
431 RxOverflow = 0x0010,
432 TxErr = 0x0008,
433 TxOK = 0x0004,
434 RxErr = 0x0002,
435 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400438 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200439 RxFOVF = (1 << 23),
440 RxRWT = (1 << 22),
441 RxRES = (1 << 21),
442 RxRUNT = (1 << 20),
443 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800446 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100447 CmdReset = 0x10,
448 CmdRxEnb = 0x08,
449 CmdTxEnb = 0x04,
450 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Francois Romieu275391a2007-02-23 23:50:28 +0100452 /* TXPoll register p.5 */
453 HPQ = 0x80, /* Poll cmd on the high prio queue */
454 NPQ = 0x40, /* Poll cmd on the low prio queue */
455 FSWInt = 0x01, /* Forced software interrupt */
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100458 Cfg9346_Lock = 0x00,
459 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100462 AcceptErr = 0x20,
463 AcceptRunt = 0x10,
464 AcceptBroadcast = 0x08,
465 AcceptMulticast = 0x04,
466 AcceptMyPhys = 0x02,
467 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200468#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* TxConfigBits */
471 TxInterFrameGapShift = 24,
472 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
473
Francois Romieu5d06a992006-02-23 00:47:58 +0100474 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200475 LEDS1 = (1 << 7),
476 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200477 Speed_down = (1 << 4),
478 MEMMAP = (1 << 3),
479 IOMAP = (1 << 2),
480 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100481 PMEnable = (1 << 0), /* Power Management Enable */
482
Francois Romieu6dccd162007-02-13 23:38:05 +0100483 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000484 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100485 PCI_Clock_66MHz = 0x01,
486 PCI_Clock_33MHz = 0x00,
487
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100488 /* Config3 register p.25 */
489 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
490 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200491 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200492 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493
Francois Romieud58d46b2011-05-03 16:38:29 +0200494 /* Config4 register */
495 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
496
Francois Romieu5d06a992006-02-23 00:47:58 +0100497 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
499 MWF = (1 << 5), /* Accept Multicast wakeup frame */
500 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200501 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100502 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100503 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* TBICSR p.28 */
506 TBIReset = 0x80000000,
507 TBILoopback = 0x40000000,
508 TBINwEnable = 0x20000000,
509 TBINwRestart = 0x10000000,
510 TBILinkOk = 0x02000000,
511 TBINwComplete = 0x01000000,
512
513 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200514 EnableBist = (1 << 15), // 8168 8101
515 Mac_dbgo_oe = (1 << 14), // 8168 8101
516 Normal_mode = (1 << 13), // unused
517 Force_half_dup = (1 << 12), // 8168 8101
518 Force_rxflow_en = (1 << 11), // 8168 8101
519 Force_txflow_en = (1 << 10), // 8168 8101
520 Cxpl_dbg_sel = (1 << 9), // 8168 8101
521 ASF = (1 << 8), // 8168 8101
522 PktCntrDisable = (1 << 7), // 8168 8101
523 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 RxVlan = (1 << 6),
525 RxChkSum = (1 << 5),
526 PCIDAC = (1 << 4),
527 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100528 INTT_0 = 0x0000, // 8168
529 INTT_1 = 0x0001, // 8168
530 INTT_2 = 0x0002, // 8168
531 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100534 TBI_Enable = 0x80,
535 TxFlowCtrl = 0x40,
536 RxFlowCtrl = 0x20,
537 _1000bpsF = 0x10,
538 _100bps = 0x08,
539 _10bps = 0x04,
540 LinkStatus = 0x02,
541 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200545
546 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100547 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Francois Romieu2b7b4312011-04-18 22:53:24 -0700550enum rtl_desc_bit {
551 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
553 RingEnd = (1 << 30), /* End of descriptor ring */
554 FirstFrag = (1 << 29), /* First segment of a packet */
555 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700556};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Francois Romieu2b7b4312011-04-18 22:53:24 -0700558/* Generic case. */
559enum rtl_tx_desc_bit {
560 /* First doubleword. */
561 TD_LSO = (1 << 27), /* Large Send Offload */
562#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564 /* Second doubleword. */
565 TxVlanTag = (1 << 17), /* Add VLAN tag */
566};
567
568/* 8169, 8168b and 810x except 8102e. */
569enum rtl_tx_desc_bit_0 {
570 /* First doubleword. */
571#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
572 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
573 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
574 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
575};
576
577/* 8102e, 8168c and beyond. */
578enum rtl_tx_desc_bit_1 {
579 /* Second doubleword. */
580#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
581 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
582 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
583 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
584};
585
586static const struct rtl_tx_desc_info {
587 struct {
588 u32 udp;
589 u32 tcp;
590 } checksum;
591 u16 mss_shift;
592 u16 opts_offset;
593} tx_desc_info [] = {
594 [RTL_TD_0] = {
595 .checksum = {
596 .udp = TD0_IP_CS | TD0_UDP_CS,
597 .tcp = TD0_IP_CS | TD0_TCP_CS
598 },
599 .mss_shift = TD0_MSS_SHIFT,
600 .opts_offset = 0
601 },
602 [RTL_TD_1] = {
603 .checksum = {
604 .udp = TD1_IP_CS | TD1_UDP_CS,
605 .tcp = TD1_IP_CS | TD1_TCP_CS
606 },
607 .mss_shift = TD1_MSS_SHIFT,
608 .opts_offset = 1
609 }
610};
611
612enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Rx private */
614 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
615 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
616
617#define RxProtoUDP (PID1)
618#define RxProtoTCP (PID0)
619#define RxProtoIP (PID1 | PID0)
620#define RxProtoMask RxProtoIP
621
622 IPFail = (1 << 16), /* IP checksum failed */
623 UDPFail = (1 << 15), /* UDP/IP checksum failed */
624 TCPFail = (1 << 14), /* TCP/IP checksum failed */
625 RxVlanTag = (1 << 16), /* VLAN tag available */
626};
627
628#define RsvdMask 0x3fffc000
629
630struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200631 __le32 opts1;
632 __le32 opts2;
633 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634};
635
636struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200637 __le32 opts1;
638 __le32 opts2;
639 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642struct ring_info {
643 struct sk_buff *skb;
644 u32 len;
645 u8 __pad[sizeof(void *) - sizeof(u32)];
646};
647
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200648enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200649 RTL_FEATURE_WOL = (1 << 0),
650 RTL_FEATURE_MSI = (1 << 1),
651 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200652};
653
Ivan Vecera355423d2009-02-06 21:49:57 -0800654struct rtl8169_counters {
655 __le64 tx_packets;
656 __le64 rx_packets;
657 __le64 tx_errors;
658 __le32 rx_errors;
659 __le16 rx_missed;
660 __le16 align_errors;
661 __le32 tx_one_collision;
662 __le32 tx_multi_collision;
663 __le64 rx_unicast;
664 __le64 rx_broadcast;
665 __le32 rx_multicast;
666 __le16 tx_aborted;
667 __le16 tx_underun;
668};
669
Francois Romieuda78dbf2012-01-26 14:18:23 +0100670enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100671 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100672 RTL_FLAG_TASK_SLOW_PENDING,
673 RTL_FLAG_TASK_RESET_PENDING,
674 RTL_FLAG_TASK_PHY_PENDING,
675 RTL_FLAG_MAX
676};
677
Junchang Wang8027aa22012-03-04 23:30:32 +0100678struct rtl8169_stats {
679 u64 packets;
680 u64 bytes;
681 struct u64_stats_sync syncp;
682};
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684struct rtl8169_private {
685 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200686 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000687 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700688 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200689 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700690 u16 txd_version;
691 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
693 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
694 u32 dirty_rx;
695 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100696 struct rtl8169_stats rx_stats;
697 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
699 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
700 dma_addr_t TxPhyAddr;
701 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000702 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct timer_list timer;
705 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100706
707 u16 event_slow;
françois romieuc0e45c12011-01-03 15:08:04 +0000708
709 struct mdio_ops {
710 void (*write)(void __iomem *, int, int);
711 int (*read)(void __iomem *, int);
712 } mdio_ops;
713
françois romieu065c27c2011-01-03 15:08:12 +0000714 struct pll_power_ops {
715 void (*down)(struct rtl8169_private *);
716 void (*up)(struct rtl8169_private *);
717 } pll_power_ops;
718
Francois Romieud58d46b2011-05-03 16:38:29 +0200719 struct jumbo_ops {
720 void (*enable)(struct rtl8169_private *);
721 void (*disable)(struct rtl8169_private *);
722 } jumbo_ops;
723
Oliver Neukum54405cd2011-01-06 21:55:13 +0100724 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200725 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000726 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100727 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000728 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800730 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100731
732 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100733 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
734 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100735 struct work_struct work;
736 } wk;
737
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200738 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200739
740 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800741 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000742 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400743 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000744
Francois Romieub6ffd972011-06-17 17:00:05 +0200745 struct rtl_fw {
746 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200747
748#define RTL_VER_SIZE 32
749
750 char version[RTL_VER_SIZE];
751
752 struct rtl_fw_phy_action {
753 __le32 *code;
754 size_t size;
755 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200756 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300757#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};
759
Ralf Baechle979b6c12005-06-13 14:30:40 -0700760MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700763MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200764module_param_named(debug, debug.msg_enable, int, 0);
765MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766MODULE_LICENSE("GPL");
767MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000768MODULE_FIRMWARE(FIRMWARE_8168D_1);
769MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000770MODULE_FIRMWARE(FIRMWARE_8168E_1);
771MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400772MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800773MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800774MODULE_FIRMWARE(FIRMWARE_8168F_1);
775MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000778static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
779 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100780static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100782static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100784static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static void rtl8169_tx_timeout(struct net_device *dev);
Junchang Wang8027aa22012-03-04 23:30:32 +0100786static struct rtnl_link_stats64 *rtl8169_get_stats64(struct net_device *dev,
787 struct rtnl_link_stats64
788 *stats);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200789static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Francois Romieu99f252b2007-04-02 22:59:59 +0200790static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700791static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Francois Romieuda78dbf2012-01-26 14:18:23 +0100793static void rtl_lock_work(struct rtl8169_private *tp)
794{
795 mutex_lock(&tp->wk.mutex);
796}
797
798static void rtl_unlock_work(struct rtl8169_private *tp)
799{
800 mutex_unlock(&tp->wk.mutex);
801}
802
Francois Romieud58d46b2011-05-03 16:38:29 +0200803static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
804{
805 int cap = pci_pcie_cap(pdev);
806
807 if (cap) {
808 u16 ctl;
809
810 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
811 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
812 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
813 }
814}
815
françois romieub646d902011-01-03 15:08:21 +0000816static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
817{
818 void __iomem *ioaddr = tp->mmio_addr;
819 int i;
820
821 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
822 for (i = 0; i < 20; i++) {
823 udelay(100);
824 if (RTL_R32(OCPAR) & OCPAR_FLAG)
825 break;
826 }
827 return RTL_R32(OCPDR);
828}
829
830static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
831{
832 void __iomem *ioaddr = tp->mmio_addr;
833 int i;
834
835 RTL_W32(OCPDR, data);
836 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
837 for (i = 0; i < 20; i++) {
838 udelay(100);
839 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
840 break;
841 }
842}
843
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800844static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000845{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800846 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000847 int i;
848
849 RTL_W8(ERIDR, cmd);
850 RTL_W32(ERIAR, 0x800010e8);
851 msleep(2);
852 for (i = 0; i < 5; i++) {
853 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200854 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000855 break;
856 }
857
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800858 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000859}
860
861#define OOB_CMD_RESET 0x00
862#define OOB_CMD_DRIVER_START 0x05
863#define OOB_CMD_DRIVER_STOP 0x06
864
Francois Romieucecb5fd2011-04-01 10:21:07 +0200865static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
866{
867 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
868}
869
françois romieub646d902011-01-03 15:08:21 +0000870static void rtl8168_driver_start(struct rtl8169_private *tp)
871{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200872 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000873 int i;
874
875 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
876
Francois Romieucecb5fd2011-04-01 10:21:07 +0200877 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000878
françois romieub646d902011-01-03 15:08:21 +0000879 for (i = 0; i < 10; i++) {
880 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000881 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000882 break;
883 }
884}
885
886static void rtl8168_driver_stop(struct rtl8169_private *tp)
887{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200888 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000889 int i;
890
891 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
892
Francois Romieucecb5fd2011-04-01 10:21:07 +0200893 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000894
françois romieub646d902011-01-03 15:08:21 +0000895 for (i = 0; i < 10; i++) {
896 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000897 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000898 break;
899 }
900}
901
hayeswang4804b3b2011-03-21 01:50:29 +0000902static int r8168dp_check_dash(struct rtl8169_private *tp)
903{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200904 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000905
Francois Romieucecb5fd2011-04-01 10:21:07 +0200906 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000907}
françois romieub646d902011-01-03 15:08:21 +0000908
françois romieu4da19632011-01-03 15:07:55 +0000909static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 int i;
912
Francois Romieua6baf3a2007-11-08 23:23:21 +0100913 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Francois Romieu23714082006-01-29 00:49:09 +0100915 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100916 /*
917 * Check if the RTL8169 has completed writing to the specified
918 * MII register.
919 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200920 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
Francois Romieu23714082006-01-29 00:49:09 +0100922 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700924 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700925 * According to hardware specs a 20us delay is required after write
926 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700927 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700928 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
françois romieu4da19632011-01-03 15:07:55 +0000931static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int i, value = -1;
934
Francois Romieua6baf3a2007-11-08 23:23:21 +0100935 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Francois Romieu23714082006-01-29 00:49:09 +0100937 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100938 /*
939 * Check if the RTL8169 has completed retrieving data from
940 * the specified MII register.
941 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100943 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
945 }
Francois Romieu23714082006-01-29 00:49:09 +0100946 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700948 /*
949 * According to hardware specs a 20us delay is required after read
950 * complete indication, but before sending next command.
951 */
952 udelay(20);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return value;
955}
956
françois romieuc0e45c12011-01-03 15:08:04 +0000957static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
958{
959 int i;
960
961 RTL_W32(OCPDR, data |
962 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
963 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
964 RTL_W32(EPHY_RXER_NUM, 0);
965
966 for (i = 0; i < 100; i++) {
967 mdelay(1);
968 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
969 break;
970 }
971}
972
973static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
974{
975 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
976 (value & OCPDR_DATA_MASK));
977}
978
979static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
980{
981 int i;
982
983 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
984
985 mdelay(1);
986 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
987 RTL_W32(EPHY_RXER_NUM, 0);
988
989 for (i = 0; i < 100; i++) {
990 mdelay(1);
991 if (RTL_R32(OCPAR) & OCPAR_FLAG)
992 break;
993 }
994
995 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
996}
997
françois romieue6de30d2011-01-03 15:08:37 +0000998#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
999
1000static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1001{
1002 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1003}
1004
1005static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1006{
1007 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1008}
1009
1010static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
1011{
1012 r8168dp_2_mdio_start(ioaddr);
1013
1014 r8169_mdio_write(ioaddr, reg_addr, value);
1015
1016 r8168dp_2_mdio_stop(ioaddr);
1017}
1018
1019static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
1020{
1021 int value;
1022
1023 r8168dp_2_mdio_start(ioaddr);
1024
1025 value = r8169_mdio_read(ioaddr, reg_addr);
1026
1027 r8168dp_2_mdio_stop(ioaddr);
1028
1029 return value;
1030}
1031
françois romieu4da19632011-01-03 15:07:55 +00001032static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001033{
françois romieuc0e45c12011-01-03 15:08:04 +00001034 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001035}
1036
françois romieu4da19632011-01-03 15:07:55 +00001037static int rtl_readphy(struct rtl8169_private *tp, int location)
1038{
françois romieuc0e45c12011-01-03 15:08:04 +00001039 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001040}
1041
1042static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1043{
1044 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1045}
1046
1047static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001048{
1049 int val;
1050
françois romieu4da19632011-01-03 15:07:55 +00001051 val = rtl_readphy(tp, reg_addr);
1052 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001053}
1054
Francois Romieuccdffb92008-07-26 14:26:06 +02001055static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1056 int val)
1057{
1058 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001059
françois romieu4da19632011-01-03 15:07:55 +00001060 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001061}
1062
1063static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1064{
1065 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001066
françois romieu4da19632011-01-03 15:07:55 +00001067 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001068}
1069
Francois Romieudacf8152008-08-02 20:44:13 +02001070static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1071{
1072 unsigned int i;
1073
1074 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1075 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1076
1077 for (i = 0; i < 100; i++) {
1078 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1079 break;
1080 udelay(10);
1081 }
1082}
1083
1084static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1085{
1086 u16 value = 0xffff;
1087 unsigned int i;
1088
1089 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1090
1091 for (i = 0; i < 100; i++) {
1092 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1093 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1094 break;
1095 }
1096 udelay(10);
1097 }
1098
1099 return value;
1100}
1101
1102static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1103{
1104 unsigned int i;
1105
1106 RTL_W32(CSIDR, value);
1107 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1108 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1109
1110 for (i = 0; i < 100; i++) {
1111 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1112 break;
1113 udelay(10);
1114 }
1115}
1116
1117static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1118{
1119 u32 value = ~0x00;
1120 unsigned int i;
1121
1122 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1123 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1124
1125 for (i = 0; i < 100; i++) {
1126 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1127 value = RTL_R32(CSIDR);
1128 break;
1129 }
1130 udelay(10);
1131 }
1132
1133 return value;
1134}
1135
Hayes Wang133ac402011-07-06 15:58:05 +08001136static
1137void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1138{
1139 unsigned int i;
1140
1141 BUG_ON((addr & 3) || (mask == 0));
1142 RTL_W32(ERIDR, val);
1143 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1144
1145 for (i = 0; i < 100; i++) {
1146 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1147 break;
1148 udelay(100);
1149 }
1150}
1151
1152static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1153{
1154 u32 value = ~0x00;
1155 unsigned int i;
1156
1157 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1158
1159 for (i = 0; i < 100; i++) {
1160 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1161 value = RTL_R32(ERIDR);
1162 break;
1163 }
1164 udelay(100);
1165 }
1166
1167 return value;
1168}
1169
1170static void
1171rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1172{
1173 u32 val;
1174
1175 val = rtl_eri_read(ioaddr, addr, type);
1176 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1177}
1178
françois romieuc28aa382011-08-02 03:53:43 +00001179struct exgmac_reg {
1180 u16 addr;
1181 u16 mask;
1182 u32 val;
1183};
1184
1185static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1186 const struct exgmac_reg *r, int len)
1187{
1188 while (len-- > 0) {
1189 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1190 r++;
1191 }
1192}
1193
françois romieudaf9df62009-10-07 12:44:20 +00001194static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1195{
1196 u8 value = 0xff;
1197 unsigned int i;
1198
1199 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1200
1201 for (i = 0; i < 300; i++) {
1202 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1203 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1204 break;
1205 }
1206 udelay(100);
1207 }
1208
1209 return value;
1210}
1211
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001212static u16 rtl_get_events(struct rtl8169_private *tp)
1213{
1214 void __iomem *ioaddr = tp->mmio_addr;
1215
1216 return RTL_R16(IntrStatus);
1217}
1218
1219static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1220{
1221 void __iomem *ioaddr = tp->mmio_addr;
1222
1223 RTL_W16(IntrStatus, bits);
1224 mmiowb();
1225}
1226
1227static void rtl_irq_disable(struct rtl8169_private *tp)
1228{
1229 void __iomem *ioaddr = tp->mmio_addr;
1230
1231 RTL_W16(IntrMask, 0);
1232 mmiowb();
1233}
1234
Francois Romieu3e990ff2012-01-26 12:50:01 +01001235static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1236{
1237 void __iomem *ioaddr = tp->mmio_addr;
1238
1239 RTL_W16(IntrMask, bits);
1240}
1241
Francois Romieuda78dbf2012-01-26 14:18:23 +01001242#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1243#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1244#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1245
1246static void rtl_irq_enable_all(struct rtl8169_private *tp)
1247{
1248 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1249}
1250
françois romieu811fd302011-12-04 20:30:45 +00001251static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
françois romieu811fd302011-12-04 20:30:45 +00001253 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001255 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001256 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
françois romieu811fd302011-12-04 20:30:45 +00001257 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
françois romieu4da19632011-01-03 15:07:55 +00001260static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
françois romieu4da19632011-01-03 15:07:55 +00001262 void __iomem *ioaddr = tp->mmio_addr;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return RTL_R32(TBICSR) & TBIReset;
1265}
1266
françois romieu4da19632011-01-03 15:07:55 +00001267static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
françois romieu4da19632011-01-03 15:07:55 +00001269 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1273{
1274 return RTL_R32(TBICSR) & TBILinkOk;
1275}
1276
1277static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1278{
1279 return RTL_R8(PHYstatus) & LinkStatus;
1280}
1281
françois romieu4da19632011-01-03 15:07:55 +00001282static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
françois romieu4da19632011-01-03 15:07:55 +00001284 void __iomem *ioaddr = tp->mmio_addr;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1287}
1288
françois romieu4da19632011-01-03 15:07:55 +00001289static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 unsigned int val;
1292
françois romieu4da19632011-01-03 15:07:55 +00001293 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1294 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Hayes Wang70090422011-07-06 15:58:06 +08001297static void rtl_link_chg_patch(struct rtl8169_private *tp)
1298{
1299 void __iomem *ioaddr = tp->mmio_addr;
1300 struct net_device *dev = tp->dev;
1301
1302 if (!netif_running(dev))
1303 return;
1304
1305 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1306 if (RTL_R8(PHYstatus) & _1000bpsF) {
1307 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1308 0x00000011, ERIAR_EXGMAC);
1309 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1310 0x00000005, ERIAR_EXGMAC);
1311 } else if (RTL_R8(PHYstatus) & _100bps) {
1312 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1313 0x0000001f, ERIAR_EXGMAC);
1314 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1315 0x00000005, ERIAR_EXGMAC);
1316 } else {
1317 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1318 0x0000001f, ERIAR_EXGMAC);
1319 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1320 0x0000003f, ERIAR_EXGMAC);
1321 }
1322 /* Reset packet filter */
1323 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1324 ERIAR_EXGMAC);
1325 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1326 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001327 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1328 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1329 if (RTL_R8(PHYstatus) & _1000bpsF) {
1330 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1331 0x00000011, ERIAR_EXGMAC);
1332 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1333 0x00000005, ERIAR_EXGMAC);
1334 } else {
1335 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1336 0x0000001f, ERIAR_EXGMAC);
1337 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1338 0x0000003f, ERIAR_EXGMAC);
1339 }
Hayes Wang70090422011-07-06 15:58:06 +08001340 }
1341}
1342
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001343static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001344 struct rtl8169_private *tp,
1345 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001348 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001349 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001350 if (pm)
1351 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001353 if (net_ratelimit())
1354 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001355 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001357 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001358 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001359 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001363static void rtl8169_check_link_status(struct net_device *dev,
1364 struct rtl8169_private *tp,
1365 void __iomem *ioaddr)
1366{
1367 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1368}
1369
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001370#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1371
1372static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1373{
1374 void __iomem *ioaddr = tp->mmio_addr;
1375 u8 options;
1376 u32 wolopts = 0;
1377
1378 options = RTL_R8(Config1);
1379 if (!(options & PMEnable))
1380 return 0;
1381
1382 options = RTL_R8(Config3);
1383 if (options & LinkUp)
1384 wolopts |= WAKE_PHY;
1385 if (options & MagicPacket)
1386 wolopts |= WAKE_MAGIC;
1387
1388 options = RTL_R8(Config5);
1389 if (options & UWF)
1390 wolopts |= WAKE_UCAST;
1391 if (options & BWF)
1392 wolopts |= WAKE_BCAST;
1393 if (options & MWF)
1394 wolopts |= WAKE_MCAST;
1395
1396 return wolopts;
1397}
1398
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1400{
1401 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402
Francois Romieuda78dbf2012-01-26 14:18:23 +01001403 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001405 wol->supported = WAKE_ANY;
1406 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001407
Francois Romieuda78dbf2012-01-26 14:18:23 +01001408 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409}
1410
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001411static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001413 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001414 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001415 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001416 u32 opt;
1417 u16 reg;
1418 u8 mask;
1419 } cfg[] = {
1420 { WAKE_ANY, Config1, PMEnable },
1421 { WAKE_PHY, Config3, LinkUp },
1422 { WAKE_MAGIC, Config3, MagicPacket },
1423 { WAKE_UCAST, Config5, UWF },
1424 { WAKE_BCAST, Config5, BWF },
1425 { WAKE_MCAST, Config5, MWF },
1426 { WAKE_ANY, Config5, LanWake }
1427 };
1428
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001429 RTL_W8(Cfg9346, Cfg9346_Unlock);
1430
1431 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1432 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001433 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001434 options |= cfg[i].mask;
1435 RTL_W8(cfg[i].reg, options);
1436 }
1437
1438 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001439}
1440
1441static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1442{
1443 struct rtl8169_private *tp = netdev_priv(dev);
1444
Francois Romieuda78dbf2012-01-26 14:18:23 +01001445 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001446
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001447 if (wol->wolopts)
1448 tp->features |= RTL_FEATURE_WOL;
1449 else
1450 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001451 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001452
1453 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001454
françois romieuea809072010-11-08 13:23:58 +00001455 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1456
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001457 return 0;
1458}
1459
Francois Romieu31bd2042011-04-26 18:58:59 +02001460static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1461{
Francois Romieu85bffe62011-04-27 08:22:39 +02001462 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static void rtl8169_get_drvinfo(struct net_device *dev,
1466 struct ethtool_drvinfo *info)
1467{
1468 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001469 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Rick Jones68aad782011-11-07 13:29:27 +00001471 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1472 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1473 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001474 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001475 if (!IS_ERR_OR_NULL(rtl_fw))
1476 strlcpy(info->fw_version, rtl_fw->version,
1477 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static int rtl8169_get_regs_len(struct net_device *dev)
1481{
1482 return R8169_REGS_SIZE;
1483}
1484
1485static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001486 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct rtl8169_private *tp = netdev_priv(dev);
1489 void __iomem *ioaddr = tp->mmio_addr;
1490 int ret = 0;
1491 u32 reg;
1492
1493 reg = RTL_R32(TBICSR);
1494 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1495 (duplex == DUPLEX_FULL)) {
1496 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1497 } else if (autoneg == AUTONEG_ENABLE)
1498 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1499 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001500 netif_warn(tp, link, dev,
1501 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ret = -EOPNOTSUPP;
1503 }
1504
1505 return ret;
1506}
1507
1508static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001509 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001512 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001513 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Hayes Wang716b50a2011-02-22 17:26:18 +08001515 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001518 int auto_nego;
1519
françois romieu4da19632011-01-03 15:07:55 +00001520 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001521 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1522 ADVERTISE_100HALF | ADVERTISE_100FULL);
1523
1524 if (adv & ADVERTISED_10baseT_Half)
1525 auto_nego |= ADVERTISE_10HALF;
1526 if (adv & ADVERTISED_10baseT_Full)
1527 auto_nego |= ADVERTISE_10FULL;
1528 if (adv & ADVERTISED_100baseT_Half)
1529 auto_nego |= ADVERTISE_100HALF;
1530 if (adv & ADVERTISED_100baseT_Full)
1531 auto_nego |= ADVERTISE_100FULL;
1532
françois romieu3577aa12009-05-19 10:46:48 +00001533 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1534
françois romieu4da19632011-01-03 15:07:55 +00001535 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001536 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1537
1538 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001539 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001540 if (adv & ADVERTISED_1000baseT_Half)
1541 giga_ctrl |= ADVERTISE_1000HALF;
1542 if (adv & ADVERTISED_1000baseT_Full)
1543 giga_ctrl |= ADVERTISE_1000FULL;
1544 } else if (adv & (ADVERTISED_1000baseT_Half |
1545 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001546 netif_info(tp, link, dev,
1547 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001548 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
françois romieu3577aa12009-05-19 10:46:48 +00001551 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001552
françois romieu4da19632011-01-03 15:07:55 +00001553 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1554 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001555 } else {
1556 giga_ctrl = 0;
1557
1558 if (speed == SPEED_10)
1559 bmcr = 0;
1560 else if (speed == SPEED_100)
1561 bmcr = BMCR_SPEED100;
1562 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001563 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001564
1565 if (duplex == DUPLEX_FULL)
1566 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001567 }
1568
françois romieu4da19632011-01-03 15:07:55 +00001569 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001570
Francois Romieucecb5fd2011-04-01 10:21:07 +02001571 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1572 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001573 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001574 rtl_writephy(tp, 0x17, 0x2138);
1575 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001576 } else {
françois romieu4da19632011-01-03 15:07:55 +00001577 rtl_writephy(tp, 0x17, 0x2108);
1578 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001579 }
1580 }
1581
Oliver Neukum54405cd2011-01-06 21:55:13 +01001582 rc = 0;
1583out:
1584 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001588 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct rtl8169_private *tp = netdev_priv(dev);
1591 int ret;
1592
Oliver Neukum54405cd2011-01-06 21:55:13 +01001593 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001594 if (ret < 0)
1595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Francois Romieu4876cc12011-03-11 21:07:11 +01001597 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1598 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001600 }
1601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return ret;
1603}
1604
1605static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1606{
1607 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int ret;
1609
Francois Romieu4876cc12011-03-11 21:07:11 +01001610 del_timer_sync(&tp->timer);
1611
Francois Romieuda78dbf2012-01-26 14:18:23 +01001612 rtl_lock_work(tp);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001613 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001614 cmd->duplex, cmd->advertising);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001615 rtl_unlock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return ret;
1618}
1619
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001620static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1621 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Francois Romieud58d46b2011-05-03 16:38:29 +02001623 struct rtl8169_private *tp = netdev_priv(dev);
1624
Francois Romieu2b7b4312011-04-18 22:53:24 -07001625 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001626 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Francois Romieud58d46b2011-05-03 16:38:29 +02001628 if (dev->mtu > JUMBO_1K &&
1629 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1630 features &= ~NETIF_F_IP_CSUM;
1631
Michał Mirosław350fb322011-04-08 06:35:56 +00001632 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Francois Romieuda78dbf2012-01-26 14:18:23 +01001635static void __rtl8169_set_features(struct net_device *dev,
1636 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
Ben Greear6bbe0212012-02-10 15:04:33 +00001639 netdev_features_t changed = features ^ dev->features;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001640 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Ben Greear6bbe0212012-02-10 15:04:33 +00001642 if (!(changed & (NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)))
1643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Ben Greear6bbe0212012-02-10 15:04:33 +00001645 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_RX)) {
1646 if (features & NETIF_F_RXCSUM)
1647 tp->cp_cmd |= RxChkSum;
1648 else
1649 tp->cp_cmd &= ~RxChkSum;
Michał Mirosław350fb322011-04-08 06:35:56 +00001650
Ben Greear6bbe0212012-02-10 15:04:33 +00001651 if (dev->features & NETIF_F_HW_VLAN_RX)
1652 tp->cp_cmd |= RxVlan;
1653 else
1654 tp->cp_cmd &= ~RxVlan;
1655
1656 RTL_W16(CPlusCmd, tp->cp_cmd);
1657 RTL_R16(CPlusCmd);
1658 }
1659 if (changed & NETIF_F_RXALL) {
1660 int tmp = (RTL_R32(RxConfig) & ~(AcceptErr | AcceptRunt));
1661 if (features & NETIF_F_RXALL)
1662 tmp |= (AcceptErr | AcceptRunt);
1663 RTL_W32(RxConfig, tmp);
1664 }
Francois Romieuda78dbf2012-01-26 14:18:23 +01001665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Francois Romieuda78dbf2012-01-26 14:18:23 +01001667static int rtl8169_set_features(struct net_device *dev,
1668 netdev_features_t features)
1669{
1670 struct rtl8169_private *tp = netdev_priv(dev);
1671
1672 rtl_lock_work(tp);
1673 __rtl8169_set_features(dev, features);
1674 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 return 0;
1677}
1678
Francois Romieuda78dbf2012-01-26 14:18:23 +01001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1681 struct sk_buff *skb)
1682{
Jesse Grosseab6d182010-10-20 13:56:03 +00001683 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1685}
1686
Francois Romieu7a8fc772011-03-01 17:18:33 +01001687static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Francois Romieu7a8fc772011-03-01 17:18:33 +01001691 if (opts2 & RxVlanTag)
1692 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Francois Romieuccdffb92008-07-26 14:26:06 +02001697static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct rtl8169_private *tp = netdev_priv(dev);
1700 void __iomem *ioaddr = tp->mmio_addr;
1701 u32 status;
1702
1703 cmd->supported =
1704 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1705 cmd->port = PORT_FIBRE;
1706 cmd->transceiver = XCVR_INTERNAL;
1707
1708 status = RTL_R32(TBICSR);
1709 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1710 cmd->autoneg = !!(status & TBINwEnable);
1711
David Decotigny70739492011-04-27 18:32:40 +00001712 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001714
1715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Francois Romieuccdffb92008-07-26 14:26:06 +02001718static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Francois Romieuccdffb92008-07-26 14:26:06 +02001722 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1726{
1727 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001728 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Francois Romieuda78dbf2012-01-26 14:18:23 +01001730 rtl_lock_work(tp);
Francois Romieuccdffb92008-07-26 14:26:06 +02001731 rc = tp->get_settings(dev, cmd);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001732 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Francois Romieuccdffb92008-07-26 14:26:06 +02001734 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
1737static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1738 void *p)
1739{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001740 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Francois Romieu5b0384f2006-08-16 16:00:01 +02001742 if (regs->len > R8169_REGS_SIZE)
1743 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Francois Romieuda78dbf2012-01-26 14:18:23 +01001745 rtl_lock_work(tp);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001746 memcpy_fromio(p, tp->mmio_addr, regs->len);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001747 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001750static u32 rtl8169_get_msglevel(struct net_device *dev)
1751{
1752 struct rtl8169_private *tp = netdev_priv(dev);
1753
1754 return tp->msg_enable;
1755}
1756
1757static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1758{
1759 struct rtl8169_private *tp = netdev_priv(dev);
1760
1761 tp->msg_enable = value;
1762}
1763
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001764static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1765 "tx_packets",
1766 "rx_packets",
1767 "tx_errors",
1768 "rx_errors",
1769 "rx_missed",
1770 "align_errors",
1771 "tx_single_collisions",
1772 "tx_multi_collisions",
1773 "unicast",
1774 "broadcast",
1775 "multicast",
1776 "tx_aborted",
1777 "tx_underrun",
1778};
1779
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001780static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001781{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001782 switch (sset) {
1783 case ETH_SS_STATS:
1784 return ARRAY_SIZE(rtl8169_gstrings);
1785 default:
1786 return -EOPNOTSUPP;
1787 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001788}
1789
Ivan Vecera355423d2009-02-06 21:49:57 -08001790static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001791{
1792 struct rtl8169_private *tp = netdev_priv(dev);
1793 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001794 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795 struct rtl8169_counters *counters;
1796 dma_addr_t paddr;
1797 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001798 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799
Ivan Vecera355423d2009-02-06 21:49:57 -08001800 /*
1801 * Some chips are unable to dump tally counters when the receiver
1802 * is disabled.
1803 */
1804 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1805 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001806
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001807 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808 if (!counters)
1809 return;
1810
1811 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001812 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001813 RTL_W32(CounterAddrLow, cmd);
1814 RTL_W32(CounterAddrLow, cmd | CounterDump);
1815
Ivan Vecera355423d2009-02-06 21:49:57 -08001816 while (wait--) {
1817 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001818 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001819 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001820 }
1821 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001822 }
1823
1824 RTL_W32(CounterAddrLow, 0);
1825 RTL_W32(CounterAddrHigh, 0);
1826
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001827 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001828}
1829
Ivan Vecera355423d2009-02-06 21:49:57 -08001830static void rtl8169_get_ethtool_stats(struct net_device *dev,
1831 struct ethtool_stats *stats, u64 *data)
1832{
1833 struct rtl8169_private *tp = netdev_priv(dev);
1834
1835 ASSERT_RTNL();
1836
1837 rtl8169_update_counters(dev);
1838
1839 data[0] = le64_to_cpu(tp->counters.tx_packets);
1840 data[1] = le64_to_cpu(tp->counters.rx_packets);
1841 data[2] = le64_to_cpu(tp->counters.tx_errors);
1842 data[3] = le32_to_cpu(tp->counters.rx_errors);
1843 data[4] = le16_to_cpu(tp->counters.rx_missed);
1844 data[5] = le16_to_cpu(tp->counters.align_errors);
1845 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1846 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1847 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1848 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1849 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1850 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1851 data[12] = le16_to_cpu(tp->counters.tx_underun);
1852}
1853
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001854static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1855{
1856 switch(stringset) {
1857 case ETH_SS_STATS:
1858 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1859 break;
1860 }
1861}
1862
Jeff Garzik7282d492006-09-13 14:30:00 -04001863static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 .get_drvinfo = rtl8169_get_drvinfo,
1865 .get_regs_len = rtl8169_get_regs_len,
1866 .get_link = ethtool_op_get_link,
1867 .get_settings = rtl8169_get_settings,
1868 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001869 .get_msglevel = rtl8169_get_msglevel,
1870 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001872 .get_wol = rtl8169_get_wol,
1873 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001874 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001875 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001876 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877};
1878
Francois Romieu07d3f512007-02-21 22:40:46 +01001879static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001880 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Francois Romieu5d320a22011-05-08 17:47:36 +02001882 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001883 /*
1884 * The driver currently handles the 8168Bf and the 8168Be identically
1885 * but they can be identified more specifically through the test below
1886 * if needed:
1887 *
1888 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001889 *
1890 * Same thing for the 8101Eb and the 8101Ec:
1891 *
1892 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001893 */
Francois Romieu37441002011-06-17 22:58:54 +02001894 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001896 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int mac_version;
1898 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001899 /* 8168F family. */
1900 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1901 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1902
hayeswang01dc7fe2011-03-21 01:50:28 +00001903 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001904 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001905 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1906 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1907 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1908
Francois Romieu5b538df2008-07-20 16:22:45 +02001909 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001910 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1911 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001912 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001913
françois romieue6de30d2011-01-03 15:08:37 +00001914 /* 8168DP family. */
1915 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1916 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001917 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001918
Francois Romieuef808d52008-06-29 13:10:54 +02001919 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001920 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001921 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001922 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001923 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001924 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1925 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001926 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001927 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001928 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001929
1930 /* 8168B family. */
1931 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1932 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1933 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1934 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1935
1936 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001937 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001938 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1939 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1940 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001941 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1942 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1943 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1944 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1945 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1946 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001947 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001948 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001949 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001950 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1951 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001952 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1953 /* FIXME: where did these entries come from ? -- FR */
1954 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1955 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1956
1957 /* 8110 family. */
1958 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1959 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1960 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1961 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1962 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1963 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1964
Jean Delvaref21b75e2009-05-26 20:54:48 -07001965 /* Catch-all */
1966 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001967 };
1968 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 u32 reg;
1970
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001971 reg = RTL_R32(TxConfig);
1972 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 p++;
1974 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001975
1976 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1977 netif_notice(tp, probe, dev,
1978 "unknown MAC, using family default\n");
1979 tp->mac_version = default_version;
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1984{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001985 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
Francois Romieu867763c2007-08-17 18:21:58 +02001988struct phy_reg {
1989 u16 reg;
1990 u16 val;
1991};
1992
françois romieu4da19632011-01-03 15:07:55 +00001993static void rtl_writephy_batch(struct rtl8169_private *tp,
1994 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001995{
1996 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001997 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001998 regs++;
1999 }
2000}
2001
françois romieubca03d52011-01-03 15:07:31 +00002002#define PHY_READ 0x00000000
2003#define PHY_DATA_OR 0x10000000
2004#define PHY_DATA_AND 0x20000000
2005#define PHY_BJMPN 0x30000000
2006#define PHY_READ_EFUSE 0x40000000
2007#define PHY_READ_MAC_BYTE 0x50000000
2008#define PHY_WRITE_MAC_BYTE 0x60000000
2009#define PHY_CLEAR_READCOUNT 0x70000000
2010#define PHY_WRITE 0x80000000
2011#define PHY_READCOUNT_EQ_SKIP 0x90000000
2012#define PHY_COMP_EQ_SKIPN 0xa0000000
2013#define PHY_COMP_NEQ_SKIPN 0xb0000000
2014#define PHY_WRITE_PREVIOUS 0xc0000000
2015#define PHY_SKIPN 0xd0000000
2016#define PHY_DELAY_MS 0xe0000000
2017#define PHY_WRITE_ERI_WORD 0xf0000000
2018
Hayes Wang960aee62011-06-18 11:37:48 +02002019struct fw_info {
2020 u32 magic;
2021 char version[RTL_VER_SIZE];
2022 __le32 fw_start;
2023 __le32 fw_len;
2024 u8 chksum;
2025} __packed;
2026
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2028
2029static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002030{
Francois Romieub6ffd972011-06-17 17:00:05 +02002031 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002032 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002033 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2034 char *version = rtl_fw->version;
2035 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002036
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037 if (fw->size < FW_OPCODE_SIZE)
2038 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002039
2040 if (!fw_info->magic) {
2041 size_t i, size, start;
2042 u8 checksum = 0;
2043
2044 if (fw->size < sizeof(*fw_info))
2045 goto out;
2046
2047 for (i = 0; i < fw->size; i++)
2048 checksum += fw->data[i];
2049 if (checksum != 0)
2050 goto out;
2051
2052 start = le32_to_cpu(fw_info->fw_start);
2053 if (start > fw->size)
2054 goto out;
2055
2056 size = le32_to_cpu(fw_info->fw_len);
2057 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2058 goto out;
2059
2060 memcpy(version, fw_info->version, RTL_VER_SIZE);
2061
2062 pa->code = (__le32 *)(fw->data + start);
2063 pa->size = size;
2064 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002065 if (fw->size % FW_OPCODE_SIZE)
2066 goto out;
2067
2068 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2069
2070 pa->code = (__le32 *)fw->data;
2071 pa->size = fw->size / FW_OPCODE_SIZE;
2072 }
2073 version[RTL_VER_SIZE - 1] = 0;
2074
2075 rc = true;
2076out:
2077 return rc;
2078}
2079
Francois Romieufd112f22011-06-18 00:10:29 +02002080static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2081 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002082{
Francois Romieufd112f22011-06-18 00:10:29 +02002083 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002084 size_t index;
2085
Francois Romieu1c361ef2011-06-17 17:16:24 +02002086 for (index = 0; index < pa->size; index++) {
2087 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002088 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002089
hayeswang42b82dc2011-01-10 02:07:25 +00002090 switch(action & 0xf0000000) {
2091 case PHY_READ:
2092 case PHY_DATA_OR:
2093 case PHY_DATA_AND:
2094 case PHY_READ_EFUSE:
2095 case PHY_CLEAR_READCOUNT:
2096 case PHY_WRITE:
2097 case PHY_WRITE_PREVIOUS:
2098 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002099 break;
2100
hayeswang42b82dc2011-01-10 02:07:25 +00002101 case PHY_BJMPN:
2102 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002103 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002104 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002105 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002106 }
2107 break;
2108 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002109 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002110 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002111 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002112 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002113 }
2114 break;
2115 case PHY_COMP_EQ_SKIPN:
2116 case PHY_COMP_NEQ_SKIPN:
2117 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002118 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002119 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002120 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002121 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002122 }
2123 break;
2124
2125 case PHY_READ_MAC_BYTE:
2126 case PHY_WRITE_MAC_BYTE:
2127 case PHY_WRITE_ERI_WORD:
2128 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002129 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002130 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002131 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002132 }
2133 }
Francois Romieufd112f22011-06-18 00:10:29 +02002134 rc = true;
2135out:
2136 return rc;
2137}
françois romieubca03d52011-01-03 15:07:31 +00002138
Francois Romieufd112f22011-06-18 00:10:29 +02002139static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2140{
2141 struct net_device *dev = tp->dev;
2142 int rc = -EINVAL;
2143
2144 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2145 netif_err(tp, ifup, dev, "invalid firwmare\n");
2146 goto out;
2147 }
2148
2149 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2150 rc = 0;
2151out:
2152 return rc;
2153}
2154
2155static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2156{
2157 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2158 u32 predata, count;
2159 size_t index;
2160
2161 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002162
Francois Romieu1c361ef2011-06-17 17:16:24 +02002163 for (index = 0; index < pa->size; ) {
2164 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002165 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002166 u32 regno = (action & 0x0fff0000) >> 16;
2167
2168 if (!action)
2169 break;
françois romieubca03d52011-01-03 15:07:31 +00002170
2171 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002172 case PHY_READ:
2173 predata = rtl_readphy(tp, regno);
2174 count++;
2175 index++;
françois romieubca03d52011-01-03 15:07:31 +00002176 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002177 case PHY_DATA_OR:
2178 predata |= data;
2179 index++;
2180 break;
2181 case PHY_DATA_AND:
2182 predata &= data;
2183 index++;
2184 break;
2185 case PHY_BJMPN:
2186 index -= regno;
2187 break;
2188 case PHY_READ_EFUSE:
2189 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2190 index++;
2191 break;
2192 case PHY_CLEAR_READCOUNT:
2193 count = 0;
2194 index++;
2195 break;
2196 case PHY_WRITE:
2197 rtl_writephy(tp, regno, data);
2198 index++;
2199 break;
2200 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002201 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002202 break;
2203 case PHY_COMP_EQ_SKIPN:
2204 if (predata == data)
2205 index += regno;
2206 index++;
2207 break;
2208 case PHY_COMP_NEQ_SKIPN:
2209 if (predata != data)
2210 index += regno;
2211 index++;
2212 break;
2213 case PHY_WRITE_PREVIOUS:
2214 rtl_writephy(tp, regno, predata);
2215 index++;
2216 break;
2217 case PHY_SKIPN:
2218 index += regno + 1;
2219 break;
2220 case PHY_DELAY_MS:
2221 mdelay(data);
2222 index++;
2223 break;
2224
2225 case PHY_READ_MAC_BYTE:
2226 case PHY_WRITE_MAC_BYTE:
2227 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002228 default:
2229 BUG();
2230 }
2231 }
2232}
2233
françois romieuf1e02ed2011-01-13 13:07:53 +00002234static void rtl_release_firmware(struct rtl8169_private *tp)
2235{
Francois Romieub6ffd972011-06-17 17:00:05 +02002236 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2237 release_firmware(tp->rtl_fw->fw);
2238 kfree(tp->rtl_fw);
2239 }
2240 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002241}
2242
François Romieu953a12c2011-04-24 17:38:48 +02002243static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002244{
Francois Romieub6ffd972011-06-17 17:00:05 +02002245 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002246
2247 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002248 if (!IS_ERR_OR_NULL(rtl_fw))
2249 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002250}
2251
2252static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2253{
2254 if (rtl_readphy(tp, reg) != val)
2255 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2256 else
2257 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002258}
2259
françois romieu4da19632011-01-03 15:07:55 +00002260static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002262 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002263 { 0x1f, 0x0001 },
2264 { 0x06, 0x006e },
2265 { 0x08, 0x0708 },
2266 { 0x15, 0x4000 },
2267 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
françois romieu0b9b5712009-08-10 19:44:56 +00002269 { 0x1f, 0x0001 },
2270 { 0x03, 0x00a1 },
2271 { 0x02, 0x0008 },
2272 { 0x01, 0x0120 },
2273 { 0x00, 0x1000 },
2274 { 0x04, 0x0800 },
2275 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
françois romieu0b9b5712009-08-10 19:44:56 +00002277 { 0x03, 0xff41 },
2278 { 0x02, 0xdf60 },
2279 { 0x01, 0x0140 },
2280 { 0x00, 0x0077 },
2281 { 0x04, 0x7800 },
2282 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
françois romieu0b9b5712009-08-10 19:44:56 +00002284 { 0x03, 0x802f },
2285 { 0x02, 0x4f02 },
2286 { 0x01, 0x0409 },
2287 { 0x00, 0xf0f9 },
2288 { 0x04, 0x9800 },
2289 { 0x04, 0x9000 },
2290
2291 { 0x03, 0xdf01 },
2292 { 0x02, 0xdf20 },
2293 { 0x01, 0xff95 },
2294 { 0x00, 0xba00 },
2295 { 0x04, 0xa800 },
2296 { 0x04, 0xa000 },
2297
2298 { 0x03, 0xff41 },
2299 { 0x02, 0xdf20 },
2300 { 0x01, 0x0140 },
2301 { 0x00, 0x00bb },
2302 { 0x04, 0xb800 },
2303 { 0x04, 0xb000 },
2304
2305 { 0x03, 0xdf41 },
2306 { 0x02, 0xdc60 },
2307 { 0x01, 0x6340 },
2308 { 0x00, 0x007d },
2309 { 0x04, 0xd800 },
2310 { 0x04, 0xd000 },
2311
2312 { 0x03, 0xdf01 },
2313 { 0x02, 0xdf20 },
2314 { 0x01, 0x100a },
2315 { 0x00, 0xa0ff },
2316 { 0x04, 0xf800 },
2317 { 0x04, 0xf000 },
2318
2319 { 0x1f, 0x0000 },
2320 { 0x0b, 0x0000 },
2321 { 0x00, 0x9200 }
2322 };
2323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
françois romieu4da19632011-01-03 15:07:55 +00002327static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002330 { 0x1f, 0x0002 },
2331 { 0x01, 0x90d0 },
2332 { 0x1f, 0x0000 }
2333 };
2334
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002336}
2337
françois romieu4da19632011-01-03 15:07:55 +00002338static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002339{
2340 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002341
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002342 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2343 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002344 return;
2345
françois romieu4da19632011-01-03 15:07:55 +00002346 rtl_writephy(tp, 0x1f, 0x0001);
2347 rtl_writephy(tp, 0x10, 0xf01b);
2348 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002349}
2350
françois romieu4da19632011-01-03 15:07:55 +00002351static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002352{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002353 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002354 { 0x1f, 0x0001 },
2355 { 0x04, 0x0000 },
2356 { 0x03, 0x00a1 },
2357 { 0x02, 0x0008 },
2358 { 0x01, 0x0120 },
2359 { 0x00, 0x1000 },
2360 { 0x04, 0x0800 },
2361 { 0x04, 0x9000 },
2362 { 0x03, 0x802f },
2363 { 0x02, 0x4f02 },
2364 { 0x01, 0x0409 },
2365 { 0x00, 0xf099 },
2366 { 0x04, 0x9800 },
2367 { 0x04, 0xa000 },
2368 { 0x03, 0xdf01 },
2369 { 0x02, 0xdf20 },
2370 { 0x01, 0xff95 },
2371 { 0x00, 0xba00 },
2372 { 0x04, 0xa800 },
2373 { 0x04, 0xf000 },
2374 { 0x03, 0xdf01 },
2375 { 0x02, 0xdf20 },
2376 { 0x01, 0x101a },
2377 { 0x00, 0xa0ff },
2378 { 0x04, 0xf800 },
2379 { 0x04, 0x0000 },
2380 { 0x1f, 0x0000 },
2381
2382 { 0x1f, 0x0001 },
2383 { 0x10, 0xf41b },
2384 { 0x14, 0xfb54 },
2385 { 0x18, 0xf5c7 },
2386 { 0x1f, 0x0000 },
2387
2388 { 0x1f, 0x0001 },
2389 { 0x17, 0x0cc0 },
2390 { 0x1f, 0x0000 }
2391 };
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002396}
2397
françois romieu4da19632011-01-03 15:07:55 +00002398static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002399{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002400 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002401 { 0x1f, 0x0001 },
2402 { 0x04, 0x0000 },
2403 { 0x03, 0x00a1 },
2404 { 0x02, 0x0008 },
2405 { 0x01, 0x0120 },
2406 { 0x00, 0x1000 },
2407 { 0x04, 0x0800 },
2408 { 0x04, 0x9000 },
2409 { 0x03, 0x802f },
2410 { 0x02, 0x4f02 },
2411 { 0x01, 0x0409 },
2412 { 0x00, 0xf099 },
2413 { 0x04, 0x9800 },
2414 { 0x04, 0xa000 },
2415 { 0x03, 0xdf01 },
2416 { 0x02, 0xdf20 },
2417 { 0x01, 0xff95 },
2418 { 0x00, 0xba00 },
2419 { 0x04, 0xa800 },
2420 { 0x04, 0xf000 },
2421 { 0x03, 0xdf01 },
2422 { 0x02, 0xdf20 },
2423 { 0x01, 0x101a },
2424 { 0x00, 0xa0ff },
2425 { 0x04, 0xf800 },
2426 { 0x04, 0x0000 },
2427 { 0x1f, 0x0000 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x0b, 0x8480 },
2431 { 0x1f, 0x0000 },
2432
2433 { 0x1f, 0x0001 },
2434 { 0x18, 0x67c7 },
2435 { 0x04, 0x2000 },
2436 { 0x03, 0x002f },
2437 { 0x02, 0x4360 },
2438 { 0x01, 0x0109 },
2439 { 0x00, 0x3022 },
2440 { 0x04, 0x2800 },
2441 { 0x1f, 0x0000 },
2442
2443 { 0x1f, 0x0001 },
2444 { 0x17, 0x0cc0 },
2445 { 0x1f, 0x0000 }
2446 };
2447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002449}
2450
françois romieu4da19632011-01-03 15:07:55 +00002451static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002452{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002453 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002454 { 0x10, 0xf41b },
2455 { 0x1f, 0x0000 }
2456 };
2457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy(tp, 0x1f, 0x0001);
2459 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002460
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002462}
2463
françois romieu4da19632011-01-03 15:07:55 +00002464static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002465{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002467 { 0x1f, 0x0001 },
2468 { 0x10, 0xf41b },
2469 { 0x1f, 0x0000 }
2470 };
2471
françois romieu4da19632011-01-03 15:07:55 +00002472 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002473}
2474
françois romieu4da19632011-01-03 15:07:55 +00002475static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002476{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002477 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002478 { 0x1f, 0x0000 },
2479 { 0x1d, 0x0f00 },
2480 { 0x1f, 0x0002 },
2481 { 0x0c, 0x1ec8 },
2482 { 0x1f, 0x0000 }
2483 };
2484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002486}
2487
françois romieu4da19632011-01-03 15:07:55 +00002488static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002489{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002490 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002491 { 0x1f, 0x0001 },
2492 { 0x1d, 0x3d98 },
2493 { 0x1f, 0x0000 }
2494 };
2495
françois romieu4da19632011-01-03 15:07:55 +00002496 rtl_writephy(tp, 0x1f, 0x0000);
2497 rtl_patchphy(tp, 0x14, 1 << 5);
2498 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002499
françois romieu4da19632011-01-03 15:07:55 +00002500 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002501}
2502
françois romieu4da19632011-01-03 15:07:55 +00002503static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002504{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002505 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002506 { 0x1f, 0x0001 },
2507 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002508 { 0x1f, 0x0002 },
2509 { 0x00, 0x88d4 },
2510 { 0x01, 0x82b1 },
2511 { 0x03, 0x7002 },
2512 { 0x08, 0x9e30 },
2513 { 0x09, 0x01f0 },
2514 { 0x0a, 0x5500 },
2515 { 0x0c, 0x00c8 },
2516 { 0x1f, 0x0003 },
2517 { 0x12, 0xc096 },
2518 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002519 { 0x1f, 0x0000 },
2520 { 0x1f, 0x0000 },
2521 { 0x09, 0x2000 },
2522 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002523 };
2524
françois romieu4da19632011-01-03 15:07:55 +00002525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002526
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl_patchphy(tp, 0x14, 1 << 5);
2528 rtl_patchphy(tp, 0x0d, 1 << 5);
2529 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002530}
2531
françois romieu4da19632011-01-03 15:07:55 +00002532static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002533{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002534 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002535 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002536 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002537 { 0x03, 0x802f },
2538 { 0x02, 0x4f02 },
2539 { 0x01, 0x0409 },
2540 { 0x00, 0xf099 },
2541 { 0x04, 0x9800 },
2542 { 0x04, 0x9000 },
2543 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002544 { 0x1f, 0x0002 },
2545 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002546 { 0x06, 0x0761 },
2547 { 0x1f, 0x0003 },
2548 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002549 { 0x1f, 0x0000 }
2550 };
2551
françois romieu4da19632011-01-03 15:07:55 +00002552 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002553
françois romieu4da19632011-01-03 15:07:55 +00002554 rtl_patchphy(tp, 0x16, 1 << 0);
2555 rtl_patchphy(tp, 0x14, 1 << 5);
2556 rtl_patchphy(tp, 0x0d, 1 << 5);
2557 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002558}
2559
françois romieu4da19632011-01-03 15:07:55 +00002560static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002561{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002562 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002563 { 0x1f, 0x0001 },
2564 { 0x12, 0x2300 },
2565 { 0x1d, 0x3d98 },
2566 { 0x1f, 0x0002 },
2567 { 0x0c, 0x7eb8 },
2568 { 0x06, 0x5461 },
2569 { 0x1f, 0x0003 },
2570 { 0x16, 0x0f0a },
2571 { 0x1f, 0x0000 }
2572 };
2573
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002575
françois romieu4da19632011-01-03 15:07:55 +00002576 rtl_patchphy(tp, 0x16, 1 << 0);
2577 rtl_patchphy(tp, 0x14, 1 << 5);
2578 rtl_patchphy(tp, 0x0d, 1 << 5);
2579 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002580}
2581
françois romieu4da19632011-01-03 15:07:55 +00002582static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002583{
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002585}
2586
françois romieubca03d52011-01-03 15:07:31 +00002587static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002588{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002589 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002590 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002591 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x06, 0x4064 },
2593 { 0x07, 0x2863 },
2594 { 0x08, 0x059c },
2595 { 0x09, 0x26b4 },
2596 { 0x0a, 0x6a19 },
2597 { 0x0b, 0xdcc8 },
2598 { 0x10, 0xf06d },
2599 { 0x14, 0x7f68 },
2600 { 0x18, 0x7fd9 },
2601 { 0x1c, 0xf0ff },
2602 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002603 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002604 { 0x12, 0xf49f },
2605 { 0x13, 0x070b },
2606 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002607 { 0x14, 0x94c0 },
2608
2609 /*
2610 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002611 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002612 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002613 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002614 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002615 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002616 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002617 { 0x06, 0x5561 },
2618
2619 /*
2620 * Can not link to 1Gbps with bad cable
2621 * Decrease SNR threshold form 21.07dB to 19.04dB
2622 */
2623 { 0x1f, 0x0001 },
2624 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002625
2626 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002627 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002628 };
françois romieubca03d52011-01-03 15:07:31 +00002629 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002630
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002632
françois romieubca03d52011-01-03 15:07:31 +00002633 /*
2634 * Rx Error Issue
2635 * Fine Tune Switching regulator parameter
2636 */
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_writephy(tp, 0x1f, 0x0002);
2638 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2639 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002640
françois romieudaf9df62009-10-07 12:44:20 +00002641 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002642 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x1f, 0x0002 },
2644 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002645 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x05, 0x8330 },
2647 { 0x06, 0x669a },
2648 { 0x1f, 0x0002 }
2649 };
2650 int val;
2651
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002653
françois romieu4da19632011-01-03 15:07:55 +00002654 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002655
2656 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002657 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002658 0x0065, 0x0066, 0x0067, 0x0068,
2659 0x0069, 0x006a, 0x006b, 0x006c
2660 };
2661 int i;
2662
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002664
2665 val &= 0xff00;
2666 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002668 }
2669 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002670 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002671 { 0x1f, 0x0002 },
2672 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002673 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002674 { 0x05, 0x8330 },
2675 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002676 };
2677
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002679 }
2680
françois romieubca03d52011-01-03 15:07:31 +00002681 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy(tp, 0x1f, 0x0002);
2683 rtl_patchphy(tp, 0x0d, 0x0300);
2684 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002685
françois romieubca03d52011-01-03 15:07:31 +00002686 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x1f, 0x0002);
2688 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2689 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy(tp, 0x1f, 0x0005);
2692 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002693
2694 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002695
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002697}
2698
françois romieubca03d52011-01-03 15:07:31 +00002699static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002700{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002701 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002702 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002703 { 0x1f, 0x0001 },
2704 { 0x06, 0x4064 },
2705 { 0x07, 0x2863 },
2706 { 0x08, 0x059c },
2707 { 0x09, 0x26b4 },
2708 { 0x0a, 0x6a19 },
2709 { 0x0b, 0xdcc8 },
2710 { 0x10, 0xf06d },
2711 { 0x14, 0x7f68 },
2712 { 0x18, 0x7fd9 },
2713 { 0x1c, 0xf0ff },
2714 { 0x1d, 0x3d9c },
2715 { 0x1f, 0x0003 },
2716 { 0x12, 0xf49f },
2717 { 0x13, 0x070b },
2718 { 0x1a, 0x05ad },
2719 { 0x14, 0x94c0 },
2720
françois romieubca03d52011-01-03 15:07:31 +00002721 /*
2722 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002723 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002724 */
françois romieudaf9df62009-10-07 12:44:20 +00002725 { 0x1f, 0x0002 },
2726 { 0x06, 0x5561 },
2727 { 0x1f, 0x0005 },
2728 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002729 { 0x06, 0x5561 },
2730
2731 /*
2732 * Can not link to 1Gbps with bad cable
2733 * Decrease SNR threshold form 21.07dB to 19.04dB
2734 */
2735 { 0x1f, 0x0001 },
2736 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002737
2738 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002739 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002740 };
françois romieubca03d52011-01-03 15:07:31 +00002741 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002742
françois romieu4da19632011-01-03 15:07:55 +00002743 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002744
2745 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002746 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002747 { 0x1f, 0x0002 },
2748 { 0x05, 0x669a },
2749 { 0x1f, 0x0005 },
2750 { 0x05, 0x8330 },
2751 { 0x06, 0x669a },
2752
2753 { 0x1f, 0x0002 }
2754 };
2755 int val;
2756
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002758
françois romieu4da19632011-01-03 15:07:55 +00002759 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002760 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002761 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002762 0x0065, 0x0066, 0x0067, 0x0068,
2763 0x0069, 0x006a, 0x006b, 0x006c
2764 };
2765 int i;
2766
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002768
2769 val &= 0xff00;
2770 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002771 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002772 }
2773 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002774 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002775 { 0x1f, 0x0002 },
2776 { 0x05, 0x2642 },
2777 { 0x1f, 0x0005 },
2778 { 0x05, 0x8330 },
2779 { 0x06, 0x2642 }
2780 };
2781
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002783 }
2784
françois romieubca03d52011-01-03 15:07:31 +00002785 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x1f, 0x0002);
2787 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2788 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002789
françois romieubca03d52011-01-03 15:07:31 +00002790 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002791 rtl_writephy(tp, 0x1f, 0x0002);
2792 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002793
françois romieu4da19632011-01-03 15:07:55 +00002794 rtl_writephy(tp, 0x1f, 0x0005);
2795 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002796
2797 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002798
françois romieu4da19632011-01-03 15:07:55 +00002799 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002800}
2801
françois romieu4da19632011-01-03 15:07:55 +00002802static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002803{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002804 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002805 { 0x1f, 0x0002 },
2806 { 0x10, 0x0008 },
2807 { 0x0d, 0x006c },
2808
2809 { 0x1f, 0x0000 },
2810 { 0x0d, 0xf880 },
2811
2812 { 0x1f, 0x0001 },
2813 { 0x17, 0x0cc0 },
2814
2815 { 0x1f, 0x0001 },
2816 { 0x0b, 0xa4d8 },
2817 { 0x09, 0x281c },
2818 { 0x07, 0x2883 },
2819 { 0x0a, 0x6b35 },
2820 { 0x1d, 0x3da4 },
2821 { 0x1c, 0xeffd },
2822 { 0x14, 0x7f52 },
2823 { 0x18, 0x7fc6 },
2824 { 0x08, 0x0601 },
2825 { 0x06, 0x4063 },
2826 { 0x10, 0xf074 },
2827 { 0x1f, 0x0003 },
2828 { 0x13, 0x0789 },
2829 { 0x12, 0xf4bd },
2830 { 0x1a, 0x04fd },
2831 { 0x14, 0x84b0 },
2832 { 0x1f, 0x0000 },
2833 { 0x00, 0x9200 },
2834
2835 { 0x1f, 0x0005 },
2836 { 0x01, 0x0340 },
2837 { 0x1f, 0x0001 },
2838 { 0x04, 0x4000 },
2839 { 0x03, 0x1d21 },
2840 { 0x02, 0x0c32 },
2841 { 0x01, 0x0200 },
2842 { 0x00, 0x5554 },
2843 { 0x04, 0x4800 },
2844 { 0x04, 0x4000 },
2845 { 0x04, 0xf000 },
2846 { 0x03, 0xdf01 },
2847 { 0x02, 0xdf20 },
2848 { 0x01, 0x101a },
2849 { 0x00, 0xa0ff },
2850 { 0x04, 0xf800 },
2851 { 0x04, 0xf000 },
2852 { 0x1f, 0x0000 },
2853
2854 { 0x1f, 0x0007 },
2855 { 0x1e, 0x0023 },
2856 { 0x16, 0x0000 },
2857 { 0x1f, 0x0000 }
2858 };
2859
françois romieu4da19632011-01-03 15:07:55 +00002860 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002861}
2862
françois romieue6de30d2011-01-03 15:08:37 +00002863static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2864{
2865 static const struct phy_reg phy_reg_init[] = {
2866 { 0x1f, 0x0001 },
2867 { 0x17, 0x0cc0 },
2868
2869 { 0x1f, 0x0007 },
2870 { 0x1e, 0x002d },
2871 { 0x18, 0x0040 },
2872 { 0x1f, 0x0000 }
2873 };
2874
2875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2876 rtl_patchphy(tp, 0x0d, 1 << 5);
2877}
2878
Hayes Wang70090422011-07-06 15:58:06 +08002879static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002880{
2881 static const struct phy_reg phy_reg_init[] = {
2882 /* Enable Delay cap */
2883 { 0x1f, 0x0005 },
2884 { 0x05, 0x8b80 },
2885 { 0x06, 0xc896 },
2886 { 0x1f, 0x0000 },
2887
2888 /* Channel estimation fine tune */
2889 { 0x1f, 0x0001 },
2890 { 0x0b, 0x6c20 },
2891 { 0x07, 0x2872 },
2892 { 0x1c, 0xefff },
2893 { 0x1f, 0x0003 },
2894 { 0x14, 0x6420 },
2895 { 0x1f, 0x0000 },
2896
2897 /* Update PFM & 10M TX idle timer */
2898 { 0x1f, 0x0007 },
2899 { 0x1e, 0x002f },
2900 { 0x15, 0x1919 },
2901 { 0x1f, 0x0000 },
2902
2903 { 0x1f, 0x0007 },
2904 { 0x1e, 0x00ac },
2905 { 0x18, 0x0006 },
2906 { 0x1f, 0x0000 }
2907 };
2908
Francois Romieu15ecd032011-04-27 13:52:22 -07002909 rtl_apply_firmware(tp);
2910
hayeswang01dc7fe2011-03-21 01:50:28 +00002911 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2912
2913 /* DCO enable for 10M IDLE Power */
2914 rtl_writephy(tp, 0x1f, 0x0007);
2915 rtl_writephy(tp, 0x1e, 0x0023);
2916 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2917 rtl_writephy(tp, 0x1f, 0x0000);
2918
2919 /* For impedance matching */
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002922 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002923
2924 /* PHY auto speed down */
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x002d);
2927 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2930
2931 rtl_writephy(tp, 0x1f, 0x0005);
2932 rtl_writephy(tp, 0x05, 0x8b86);
2933 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2934 rtl_writephy(tp, 0x1f, 0x0000);
2935
2936 rtl_writephy(tp, 0x1f, 0x0005);
2937 rtl_writephy(tp, 0x05, 0x8b85);
2938 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2939 rtl_writephy(tp, 0x1f, 0x0007);
2940 rtl_writephy(tp, 0x1e, 0x0020);
2941 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2942 rtl_writephy(tp, 0x1f, 0x0006);
2943 rtl_writephy(tp, 0x00, 0x5a00);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0007);
2946 rtl_writephy(tp, 0x0e, 0x003c);
2947 rtl_writephy(tp, 0x0d, 0x4007);
2948 rtl_writephy(tp, 0x0e, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0000);
2950}
2951
Hayes Wang70090422011-07-06 15:58:06 +08002952static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2953{
2954 static const struct phy_reg phy_reg_init[] = {
2955 /* Enable Delay cap */
2956 { 0x1f, 0x0004 },
2957 { 0x1f, 0x0007 },
2958 { 0x1e, 0x00ac },
2959 { 0x18, 0x0006 },
2960 { 0x1f, 0x0002 },
2961 { 0x1f, 0x0000 },
2962 { 0x1f, 0x0000 },
2963
2964 /* Channel estimation fine tune */
2965 { 0x1f, 0x0003 },
2966 { 0x09, 0xa20f },
2967 { 0x1f, 0x0000 },
2968 { 0x1f, 0x0000 },
2969
2970 /* Green Setting */
2971 { 0x1f, 0x0005 },
2972 { 0x05, 0x8b5b },
2973 { 0x06, 0x9222 },
2974 { 0x05, 0x8b6d },
2975 { 0x06, 0x8000 },
2976 { 0x05, 0x8b76 },
2977 { 0x06, 0x8000 },
2978 { 0x1f, 0x0000 }
2979 };
2980
2981 rtl_apply_firmware(tp);
2982
2983 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2984
2985 /* For 4-corner performance improve */
2986 rtl_writephy(tp, 0x1f, 0x0005);
2987 rtl_writephy(tp, 0x05, 0x8b80);
2988 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2990
2991 /* PHY auto speed down */
2992 rtl_writephy(tp, 0x1f, 0x0004);
2993 rtl_writephy(tp, 0x1f, 0x0007);
2994 rtl_writephy(tp, 0x1e, 0x002d);
2995 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2996 rtl_writephy(tp, 0x1f, 0x0002);
2997 rtl_writephy(tp, 0x1f, 0x0000);
2998 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2999
3000 /* improve 10M EEE waveform */
3001 rtl_writephy(tp, 0x1f, 0x0005);
3002 rtl_writephy(tp, 0x05, 0x8b86);
3003 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3004 rtl_writephy(tp, 0x1f, 0x0000);
3005
3006 /* Improve 2-pair detection performance */
3007 rtl_writephy(tp, 0x1f, 0x0005);
3008 rtl_writephy(tp, 0x05, 0x8b85);
3009 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3010 rtl_writephy(tp, 0x1f, 0x0000);
3011
3012 /* EEE setting */
3013 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
3014 ERIAR_EXGMAC);
3015 rtl_writephy(tp, 0x1f, 0x0005);
3016 rtl_writephy(tp, 0x05, 0x8b85);
3017 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
3018 rtl_writephy(tp, 0x1f, 0x0004);
3019 rtl_writephy(tp, 0x1f, 0x0007);
3020 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04003021 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08003022 rtl_writephy(tp, 0x1f, 0x0002);
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024 rtl_writephy(tp, 0x0d, 0x0007);
3025 rtl_writephy(tp, 0x0e, 0x003c);
3026 rtl_writephy(tp, 0x0d, 0x4007);
3027 rtl_writephy(tp, 0x0e, 0x0000);
3028 rtl_writephy(tp, 0x0d, 0x0000);
3029
3030 /* Green feature */
3031 rtl_writephy(tp, 0x1f, 0x0003);
3032 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
3033 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
3034 rtl_writephy(tp, 0x1f, 0x0000);
3035}
3036
Hayes Wangc2218922011-09-06 16:55:18 +08003037static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3038{
3039 static const struct phy_reg phy_reg_init[] = {
3040 /* Channel estimation fine tune */
3041 { 0x1f, 0x0003 },
3042 { 0x09, 0xa20f },
3043 { 0x1f, 0x0000 },
3044
3045 /* Modify green table for giga & fnet */
3046 { 0x1f, 0x0005 },
3047 { 0x05, 0x8b55 },
3048 { 0x06, 0x0000 },
3049 { 0x05, 0x8b5e },
3050 { 0x06, 0x0000 },
3051 { 0x05, 0x8b67 },
3052 { 0x06, 0x0000 },
3053 { 0x05, 0x8b70 },
3054 { 0x06, 0x0000 },
3055 { 0x1f, 0x0000 },
3056 { 0x1f, 0x0007 },
3057 { 0x1e, 0x0078 },
3058 { 0x17, 0x0000 },
3059 { 0x19, 0x00fb },
3060 { 0x1f, 0x0000 },
3061
3062 /* Modify green table for 10M */
3063 { 0x1f, 0x0005 },
3064 { 0x05, 0x8b79 },
3065 { 0x06, 0xaa00 },
3066 { 0x1f, 0x0000 },
3067
3068 /* Disable hiimpedance detection (RTCT) */
3069 { 0x1f, 0x0003 },
3070 { 0x01, 0x328a },
3071 { 0x1f, 0x0000 }
3072 };
3073
3074 rtl_apply_firmware(tp);
3075
3076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3077
3078 /* For 4-corner performance improve */
3079 rtl_writephy(tp, 0x1f, 0x0005);
3080 rtl_writephy(tp, 0x05, 0x8b80);
3081 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3082 rtl_writephy(tp, 0x1f, 0x0000);
3083
3084 /* PHY auto speed down */
3085 rtl_writephy(tp, 0x1f, 0x0007);
3086 rtl_writephy(tp, 0x1e, 0x002d);
3087 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3088 rtl_writephy(tp, 0x1f, 0x0000);
3089 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3090
3091 /* Improve 10M EEE waveform */
3092 rtl_writephy(tp, 0x1f, 0x0005);
3093 rtl_writephy(tp, 0x05, 0x8b86);
3094 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3095 rtl_writephy(tp, 0x1f, 0x0000);
3096
3097 /* Improve 2-pair detection performance */
3098 rtl_writephy(tp, 0x1f, 0x0005);
3099 rtl_writephy(tp, 0x05, 0x8b85);
3100 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3101 rtl_writephy(tp, 0x1f, 0x0000);
3102}
3103
3104static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3105{
3106 rtl_apply_firmware(tp);
3107
3108 /* For 4-corner performance improve */
3109 rtl_writephy(tp, 0x1f, 0x0005);
3110 rtl_writephy(tp, 0x05, 0x8b80);
3111 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3112 rtl_writephy(tp, 0x1f, 0x0000);
3113
3114 /* PHY auto speed down */
3115 rtl_writephy(tp, 0x1f, 0x0007);
3116 rtl_writephy(tp, 0x1e, 0x002d);
3117 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3118 rtl_writephy(tp, 0x1f, 0x0000);
3119 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3120
3121 /* Improve 10M EEE waveform */
3122 rtl_writephy(tp, 0x1f, 0x0005);
3123 rtl_writephy(tp, 0x05, 0x8b86);
3124 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3125 rtl_writephy(tp, 0x1f, 0x0000);
3126}
3127
françois romieu4da19632011-01-03 15:07:55 +00003128static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003129{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003130 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003131 { 0x1f, 0x0003 },
3132 { 0x08, 0x441d },
3133 { 0x01, 0x9100 },
3134 { 0x1f, 0x0000 }
3135 };
3136
françois romieu4da19632011-01-03 15:07:55 +00003137 rtl_writephy(tp, 0x1f, 0x0000);
3138 rtl_patchphy(tp, 0x11, 1 << 12);
3139 rtl_patchphy(tp, 0x19, 1 << 13);
3140 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003141
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003143}
3144
Hayes Wang5a5e4442011-02-22 17:26:21 +08003145static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3146{
3147 static const struct phy_reg phy_reg_init[] = {
3148 { 0x1f, 0x0005 },
3149 { 0x1a, 0x0000 },
3150 { 0x1f, 0x0000 },
3151
3152 { 0x1f, 0x0004 },
3153 { 0x1c, 0x0000 },
3154 { 0x1f, 0x0000 },
3155
3156 { 0x1f, 0x0001 },
3157 { 0x15, 0x7701 },
3158 { 0x1f, 0x0000 }
3159 };
3160
3161 /* Disable ALDPS before ram code */
3162 rtl_writephy(tp, 0x1f, 0x0000);
3163 rtl_writephy(tp, 0x18, 0x0310);
3164 msleep(100);
3165
François Romieu953a12c2011-04-24 17:38:48 +02003166 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003167
3168 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3169}
3170
Francois Romieu5615d9f2007-08-17 17:50:46 +02003171static void rtl_hw_phy_config(struct net_device *dev)
3172{
3173 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003174
3175 rtl8169_print_mac_version(tp);
3176
3177 switch (tp->mac_version) {
3178 case RTL_GIGA_MAC_VER_01:
3179 break;
3180 case RTL_GIGA_MAC_VER_02:
3181 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003182 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003183 break;
3184 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003185 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003186 break;
françois romieu2e9558562009-08-10 19:44:19 +00003187 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003189 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003190 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003192 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003193 case RTL_GIGA_MAC_VER_07:
3194 case RTL_GIGA_MAC_VER_08:
3195 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003196 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003197 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003198 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003199 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003200 break;
3201 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003202 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003203 break;
3204 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003205 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003206 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003207 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003208 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003209 break;
3210 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003211 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003212 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003213 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003214 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003215 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003216 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003217 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003218 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003219 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003220 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003221 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003222 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003223 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003224 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003225 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003226 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003227 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003228 break;
3229 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003230 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003231 break;
3232 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003233 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003234 break;
françois romieue6de30d2011-01-03 15:08:37 +00003235 case RTL_GIGA_MAC_VER_28:
3236 rtl8168d_4_hw_phy_config(tp);
3237 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003238 case RTL_GIGA_MAC_VER_29:
3239 case RTL_GIGA_MAC_VER_30:
3240 rtl8105e_hw_phy_config(tp);
3241 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003242 case RTL_GIGA_MAC_VER_31:
3243 /* None. */
3244 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003245 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003246 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003247 rtl8168e_1_hw_phy_config(tp);
3248 break;
3249 case RTL_GIGA_MAC_VER_34:
3250 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003251 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003252 case RTL_GIGA_MAC_VER_35:
3253 rtl8168f_1_hw_phy_config(tp);
3254 break;
3255 case RTL_GIGA_MAC_VER_36:
3256 rtl8168f_2_hw_phy_config(tp);
3257 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003258
Francois Romieu5615d9f2007-08-17 17:50:46 +02003259 default:
3260 break;
3261 }
3262}
3263
Francois Romieuda78dbf2012-01-26 14:18:23 +01003264static void rtl_phy_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct timer_list *timer = &tp->timer;
3267 void __iomem *ioaddr = tp->mmio_addr;
3268 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3269
Francois Romieubcf0bf92006-07-26 23:14:13 +02003270 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
françois romieu4da19632011-01-03 15:07:55 +00003272 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003273 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 * A busy loop could burn quite a few cycles on nowadays CPU.
3275 * Let's delay the execution of the timer for a few ticks.
3276 */
3277 timeout = HZ/10;
3278 goto out_mod_timer;
3279 }
3280
3281 if (tp->link_ok(ioaddr))
Francois Romieuda78dbf2012-01-26 14:18:23 +01003282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Francois Romieuda78dbf2012-01-26 14:18:23 +01003284 netif_warn(tp, link, tp->dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
françois romieu4da19632011-01-03 15:07:55 +00003286 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288out_mod_timer:
3289 mod_timer(timer, jiffies + timeout);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003290}
3291
3292static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3293{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003294 if (!test_and_set_bit(flag, tp->wk.flags))
3295 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003296}
3297
3298static void rtl8169_phy_timer(unsigned long __opaque)
3299{
3300 struct net_device *dev = (struct net_device *)__opaque;
3301 struct rtl8169_private *tp = netdev_priv(dev);
3302
Francois Romieu98ddf982012-01-31 10:47:34 +01003303 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306#ifdef CONFIG_NET_POLL_CONTROLLER
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307static void rtl8169_netpoll(struct net_device *dev)
3308{
3309 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Francois Romieuda78dbf2012-01-26 14:18:23 +01003311 rtl8169_interrupt(tp->pci_dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313#endif
3314
3315static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3316 void __iomem *ioaddr)
3317{
3318 iounmap(ioaddr);
3319 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003320 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 pci_disable_device(pdev);
3322 free_netdev(dev);
3323}
3324
Francois Romieubf793292006-11-01 00:53:05 +01003325static void rtl8169_phy_reset(struct net_device *dev,
3326 struct rtl8169_private *tp)
3327{
Francois Romieu07d3f512007-02-21 22:40:46 +01003328 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003329
françois romieu4da19632011-01-03 15:07:55 +00003330 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003331 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003332 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003333 return;
3334 msleep(1);
3335 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003336 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003337}
3338
David S. Miller8decf862011-09-22 03:23:13 -04003339static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3340{
3341 void __iomem *ioaddr = tp->mmio_addr;
3342
3343 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3344 (RTL_R8(PHYstatus) & TBI_Enable);
3345}
3346
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003347static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003349 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003350
Francois Romieu5615d9f2007-08-17 17:50:46 +02003351 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352
Marcus Sundberg773328942008-07-10 21:28:08 +02003353 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3354 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3355 RTL_W8(0x82, 0x01);
3356 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003357
Francois Romieu6dccd162007-02-13 23:38:05 +01003358 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3359
3360 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3361 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003362
Francois Romieubcf0bf92006-07-26 23:14:13 +02003363 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003364 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3365 RTL_W8(0x82, 0x01);
3366 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003367 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003368 }
3369
Francois Romieubf793292006-11-01 00:53:05 +01003370 rtl8169_phy_reset(dev, tp);
3371
Oliver Neukum54405cd2011-01-06 21:55:13 +01003372 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003373 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3374 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3375 (tp->mii.supports_gmii ?
3376 ADVERTISED_1000baseT_Half |
3377 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003378
David S. Miller8decf862011-09-22 03:23:13 -04003379 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003380 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003381}
3382
Francois Romieu773d2022007-01-31 23:47:43 +01003383static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3384{
3385 void __iomem *ioaddr = tp->mmio_addr;
3386 u32 high;
3387 u32 low;
3388
3389 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3390 high = addr[4] | (addr[5] << 8);
3391
Francois Romieuda78dbf2012-01-26 14:18:23 +01003392 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003393
3394 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003395
Francois Romieu773d2022007-01-31 23:47:43 +01003396 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003397 RTL_R32(MAC4);
3398
Francois Romieu78f1cd02010-03-27 19:35:46 -07003399 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003400 RTL_R32(MAC0);
3401
françois romieuc28aa382011-08-02 03:53:43 +00003402 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3403 const struct exgmac_reg e[] = {
3404 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3405 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3406 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3407 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3408 low >> 16 },
3409 };
3410
3411 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3412 }
3413
Francois Romieu773d2022007-01-31 23:47:43 +01003414 RTL_W8(Cfg9346, Cfg9346_Lock);
3415
Francois Romieuda78dbf2012-01-26 14:18:23 +01003416 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003417}
3418
3419static int rtl_set_mac_address(struct net_device *dev, void *p)
3420{
3421 struct rtl8169_private *tp = netdev_priv(dev);
3422 struct sockaddr *addr = p;
3423
3424 if (!is_valid_ether_addr(addr->sa_data))
3425 return -EADDRNOTAVAIL;
3426
3427 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3428
3429 rtl_rar_set(tp, dev->dev_addr);
3430
3431 return 0;
3432}
3433
Francois Romieu5f787a12006-08-17 13:02:36 +02003434static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3435{
3436 struct rtl8169_private *tp = netdev_priv(dev);
3437 struct mii_ioctl_data *data = if_mii(ifr);
3438
Francois Romieu8b4ab282008-11-19 22:05:25 -08003439 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3440}
Francois Romieu5f787a12006-08-17 13:02:36 +02003441
Francois Romieucecb5fd2011-04-01 10:21:07 +02003442static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3443 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003444{
Francois Romieu5f787a12006-08-17 13:02:36 +02003445 switch (cmd) {
3446 case SIOCGMIIPHY:
3447 data->phy_id = 32; /* Internal PHY */
3448 return 0;
3449
3450 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003451 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003452 return 0;
3453
3454 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003455 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003456 return 0;
3457 }
3458 return -EOPNOTSUPP;
3459}
3460
Francois Romieu8b4ab282008-11-19 22:05:25 -08003461static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3462{
3463 return -EOPNOTSUPP;
3464}
3465
Francois Romieu0e485152007-02-20 00:00:26 +01003466static const struct rtl_cfg_info {
3467 void (*hw_start)(struct net_device *);
3468 unsigned int region;
3469 unsigned int align;
Francois Romieuda78dbf2012-01-26 14:18:23 +01003470 u16 event_slow;
Francois Romieuccdffb92008-07-26 14:26:06 +02003471 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003472 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003473} rtl_cfg_infos [] = {
3474 [RTL_CFG_0] = {
3475 .hw_start = rtl_hw_start_8169,
3476 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003477 .align = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003478 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003479 .features = RTL_FEATURE_GMII,
3480 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003481 },
3482 [RTL_CFG_1] = {
3483 .hw_start = rtl_hw_start_8168,
3484 .region = 2,
3485 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003486 .event_slow = SYSErr | LinkChg | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003487 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3488 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003489 },
3490 [RTL_CFG_2] = {
3491 .hw_start = rtl_hw_start_8101,
3492 .region = 2,
3493 .align = 8,
Francois Romieuda78dbf2012-01-26 14:18:23 +01003494 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
3495 PCSTimeout,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003496 .features = RTL_FEATURE_MSI,
3497 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003498 }
3499};
3500
Francois Romieufbac58f2007-10-04 22:51:38 +02003501/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003502static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003503 const struct rtl_cfg_info *cfg)
3504{
françois romieu2ca6cf02011-12-15 08:37:43 +00003505 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003506 unsigned msi = 0;
3507 u8 cfg2;
3508
3509 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003510 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003511 if (pci_enable_msi(tp->pci_dev)) {
3512 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003513 } else {
3514 cfg2 |= MSIEnable;
3515 msi = RTL_FEATURE_MSI;
3516 }
3517 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003518 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3519 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003520 return msi;
3521}
3522
3523static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3524{
3525 if (tp->features & RTL_FEATURE_MSI) {
3526 pci_disable_msi(pdev);
3527 tp->features &= ~RTL_FEATURE_MSI;
3528 }
3529}
3530
Francois Romieu8b4ab282008-11-19 22:05:25 -08003531static const struct net_device_ops rtl8169_netdev_ops = {
3532 .ndo_open = rtl8169_open,
3533 .ndo_stop = rtl8169_close,
Junchang Wang8027aa22012-03-04 23:30:32 +01003534 .ndo_get_stats64 = rtl8169_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -08003535 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003536 .ndo_tx_timeout = rtl8169_tx_timeout,
3537 .ndo_validate_addr = eth_validate_addr,
3538 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003539 .ndo_fix_features = rtl8169_fix_features,
3540 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003541 .ndo_set_mac_address = rtl_set_mac_address,
3542 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003543 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003544#ifdef CONFIG_NET_POLL_CONTROLLER
3545 .ndo_poll_controller = rtl8169_netpoll,
3546#endif
3547
3548};
3549
françois romieuc0e45c12011-01-03 15:08:04 +00003550static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3551{
3552 struct mdio_ops *ops = &tp->mdio_ops;
3553
3554 switch (tp->mac_version) {
3555 case RTL_GIGA_MAC_VER_27:
3556 ops->write = r8168dp_1_mdio_write;
3557 ops->read = r8168dp_1_mdio_read;
3558 break;
françois romieue6de30d2011-01-03 15:08:37 +00003559 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003560 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003561 ops->write = r8168dp_2_mdio_write;
3562 ops->read = r8168dp_2_mdio_read;
3563 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003564 default:
3565 ops->write = r8169_mdio_write;
3566 ops->read = r8169_mdio_read;
3567 break;
3568 }
3569}
3570
David S. Miller1805b2f2011-10-24 18:18:09 -04003571static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3572{
3573 void __iomem *ioaddr = tp->mmio_addr;
3574
3575 switch (tp->mac_version) {
3576 case RTL_GIGA_MAC_VER_29:
3577 case RTL_GIGA_MAC_VER_30:
3578 case RTL_GIGA_MAC_VER_32:
3579 case RTL_GIGA_MAC_VER_33:
3580 case RTL_GIGA_MAC_VER_34:
3581 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3582 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3583 break;
3584 default:
3585 break;
3586 }
3587}
3588
3589static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3590{
3591 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3592 return false;
3593
3594 rtl_writephy(tp, 0x1f, 0x0000);
3595 rtl_writephy(tp, MII_BMCR, 0x0000);
3596
3597 rtl_wol_suspend_quirk(tp);
3598
3599 return true;
3600}
3601
françois romieu065c27c2011-01-03 15:08:12 +00003602static void r810x_phy_power_down(struct rtl8169_private *tp)
3603{
3604 rtl_writephy(tp, 0x1f, 0x0000);
3605 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3606}
3607
3608static void r810x_phy_power_up(struct rtl8169_private *tp)
3609{
3610 rtl_writephy(tp, 0x1f, 0x0000);
3611 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3612}
3613
3614static void r810x_pll_power_down(struct rtl8169_private *tp)
3615{
David S. Miller1805b2f2011-10-24 18:18:09 -04003616 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003617 return;
françois romieu065c27c2011-01-03 15:08:12 +00003618
3619 r810x_phy_power_down(tp);
3620}
3621
3622static void r810x_pll_power_up(struct rtl8169_private *tp)
3623{
3624 r810x_phy_power_up(tp);
3625}
3626
3627static void r8168_phy_power_up(struct rtl8169_private *tp)
3628{
3629 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003630 switch (tp->mac_version) {
3631 case RTL_GIGA_MAC_VER_11:
3632 case RTL_GIGA_MAC_VER_12:
3633 case RTL_GIGA_MAC_VER_17:
3634 case RTL_GIGA_MAC_VER_18:
3635 case RTL_GIGA_MAC_VER_19:
3636 case RTL_GIGA_MAC_VER_20:
3637 case RTL_GIGA_MAC_VER_21:
3638 case RTL_GIGA_MAC_VER_22:
3639 case RTL_GIGA_MAC_VER_23:
3640 case RTL_GIGA_MAC_VER_24:
3641 case RTL_GIGA_MAC_VER_25:
3642 case RTL_GIGA_MAC_VER_26:
3643 case RTL_GIGA_MAC_VER_27:
3644 case RTL_GIGA_MAC_VER_28:
3645 case RTL_GIGA_MAC_VER_31:
3646 rtl_writephy(tp, 0x0e, 0x0000);
3647 break;
3648 default:
3649 break;
3650 }
françois romieu065c27c2011-01-03 15:08:12 +00003651 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3652}
3653
3654static void r8168_phy_power_down(struct rtl8169_private *tp)
3655{
3656 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003657 switch (tp->mac_version) {
3658 case RTL_GIGA_MAC_VER_32:
3659 case RTL_GIGA_MAC_VER_33:
3660 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3661 break;
3662
3663 case RTL_GIGA_MAC_VER_11:
3664 case RTL_GIGA_MAC_VER_12:
3665 case RTL_GIGA_MAC_VER_17:
3666 case RTL_GIGA_MAC_VER_18:
3667 case RTL_GIGA_MAC_VER_19:
3668 case RTL_GIGA_MAC_VER_20:
3669 case RTL_GIGA_MAC_VER_21:
3670 case RTL_GIGA_MAC_VER_22:
3671 case RTL_GIGA_MAC_VER_23:
3672 case RTL_GIGA_MAC_VER_24:
3673 case RTL_GIGA_MAC_VER_25:
3674 case RTL_GIGA_MAC_VER_26:
3675 case RTL_GIGA_MAC_VER_27:
3676 case RTL_GIGA_MAC_VER_28:
3677 case RTL_GIGA_MAC_VER_31:
3678 rtl_writephy(tp, 0x0e, 0x0200);
3679 default:
3680 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3681 break;
3682 }
françois romieu065c27c2011-01-03 15:08:12 +00003683}
3684
3685static void r8168_pll_power_down(struct rtl8169_private *tp)
3686{
3687 void __iomem *ioaddr = tp->mmio_addr;
3688
Francois Romieucecb5fd2011-04-01 10:21:07 +02003689 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3690 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3691 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003692 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003693 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003694 }
françois romieu065c27c2011-01-03 15:08:12 +00003695
Francois Romieucecb5fd2011-04-01 10:21:07 +02003696 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3697 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003698 (RTL_R16(CPlusCmd) & ASF)) {
3699 return;
3700 }
3701
hayeswang01dc7fe2011-03-21 01:50:28 +00003702 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3703 tp->mac_version == RTL_GIGA_MAC_VER_33)
3704 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3705
David S. Miller1805b2f2011-10-24 18:18:09 -04003706 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003707 return;
françois romieu065c27c2011-01-03 15:08:12 +00003708
3709 r8168_phy_power_down(tp);
3710
3711 switch (tp->mac_version) {
3712 case RTL_GIGA_MAC_VER_25:
3713 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003714 case RTL_GIGA_MAC_VER_27:
3715 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003716 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003717 case RTL_GIGA_MAC_VER_32:
3718 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003719 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3720 break;
3721 }
3722}
3723
3724static void r8168_pll_power_up(struct rtl8169_private *tp)
3725{
3726 void __iomem *ioaddr = tp->mmio_addr;
3727
Francois Romieucecb5fd2011-04-01 10:21:07 +02003728 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3729 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3730 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003731 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003732 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003733 }
françois romieu065c27c2011-01-03 15:08:12 +00003734
3735 switch (tp->mac_version) {
3736 case RTL_GIGA_MAC_VER_25:
3737 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003738 case RTL_GIGA_MAC_VER_27:
3739 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003740 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003741 case RTL_GIGA_MAC_VER_32:
3742 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003743 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3744 break;
3745 }
3746
3747 r8168_phy_power_up(tp);
3748}
3749
Francois Romieud58d46b2011-05-03 16:38:29 +02003750static void rtl_generic_op(struct rtl8169_private *tp,
3751 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003752{
3753 if (op)
3754 op(tp);
3755}
3756
3757static void rtl_pll_power_down(struct rtl8169_private *tp)
3758{
Francois Romieud58d46b2011-05-03 16:38:29 +02003759 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003760}
3761
3762static void rtl_pll_power_up(struct rtl8169_private *tp)
3763{
Francois Romieud58d46b2011-05-03 16:38:29 +02003764 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003765}
3766
3767static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3768{
3769 struct pll_power_ops *ops = &tp->pll_power_ops;
3770
3771 switch (tp->mac_version) {
3772 case RTL_GIGA_MAC_VER_07:
3773 case RTL_GIGA_MAC_VER_08:
3774 case RTL_GIGA_MAC_VER_09:
3775 case RTL_GIGA_MAC_VER_10:
3776 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003777 case RTL_GIGA_MAC_VER_29:
3778 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003779 ops->down = r810x_pll_power_down;
3780 ops->up = r810x_pll_power_up;
3781 break;
3782
3783 case RTL_GIGA_MAC_VER_11:
3784 case RTL_GIGA_MAC_VER_12:
3785 case RTL_GIGA_MAC_VER_17:
3786 case RTL_GIGA_MAC_VER_18:
3787 case RTL_GIGA_MAC_VER_19:
3788 case RTL_GIGA_MAC_VER_20:
3789 case RTL_GIGA_MAC_VER_21:
3790 case RTL_GIGA_MAC_VER_22:
3791 case RTL_GIGA_MAC_VER_23:
3792 case RTL_GIGA_MAC_VER_24:
3793 case RTL_GIGA_MAC_VER_25:
3794 case RTL_GIGA_MAC_VER_26:
3795 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003796 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003797 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003798 case RTL_GIGA_MAC_VER_32:
3799 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003800 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003801 case RTL_GIGA_MAC_VER_35:
3802 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003803 ops->down = r8168_pll_power_down;
3804 ops->up = r8168_pll_power_up;
3805 break;
3806
3807 default:
3808 ops->down = NULL;
3809 ops->up = NULL;
3810 break;
3811 }
3812}
3813
Hayes Wange542a222011-07-06 15:58:04 +08003814static void rtl_init_rxcfg(struct rtl8169_private *tp)
3815{
3816 void __iomem *ioaddr = tp->mmio_addr;
3817
3818 switch (tp->mac_version) {
3819 case RTL_GIGA_MAC_VER_01:
3820 case RTL_GIGA_MAC_VER_02:
3821 case RTL_GIGA_MAC_VER_03:
3822 case RTL_GIGA_MAC_VER_04:
3823 case RTL_GIGA_MAC_VER_05:
3824 case RTL_GIGA_MAC_VER_06:
3825 case RTL_GIGA_MAC_VER_10:
3826 case RTL_GIGA_MAC_VER_11:
3827 case RTL_GIGA_MAC_VER_12:
3828 case RTL_GIGA_MAC_VER_13:
3829 case RTL_GIGA_MAC_VER_14:
3830 case RTL_GIGA_MAC_VER_15:
3831 case RTL_GIGA_MAC_VER_16:
3832 case RTL_GIGA_MAC_VER_17:
3833 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3834 break;
3835 case RTL_GIGA_MAC_VER_18:
3836 case RTL_GIGA_MAC_VER_19:
3837 case RTL_GIGA_MAC_VER_20:
3838 case RTL_GIGA_MAC_VER_21:
3839 case RTL_GIGA_MAC_VER_22:
3840 case RTL_GIGA_MAC_VER_23:
3841 case RTL_GIGA_MAC_VER_24:
3842 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3843 break;
3844 default:
3845 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3846 break;
3847 }
3848}
3849
Hayes Wang92fc43b2011-07-06 15:58:03 +08003850static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3851{
3852 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3853}
3854
Francois Romieud58d46b2011-05-03 16:38:29 +02003855static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3856{
françois romieu9c5028e2012-03-02 04:43:14 +00003857 void __iomem *ioaddr = tp->mmio_addr;
3858
3859 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003860 rtl_generic_op(tp, tp->jumbo_ops.enable);
françois romieu9c5028e2012-03-02 04:43:14 +00003861 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003862}
3863
3864static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3865{
françois romieu9c5028e2012-03-02 04:43:14 +00003866 void __iomem *ioaddr = tp->mmio_addr;
3867
3868 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003869 rtl_generic_op(tp, tp->jumbo_ops.disable);
françois romieu9c5028e2012-03-02 04:43:14 +00003870 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieud58d46b2011-05-03 16:38:29 +02003871}
3872
3873static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3874{
3875 void __iomem *ioaddr = tp->mmio_addr;
3876
3877 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3878 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3879 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3880}
3881
3882static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3883{
3884 void __iomem *ioaddr = tp->mmio_addr;
3885
3886 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3887 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3888 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3889}
3890
3891static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3892{
3893 void __iomem *ioaddr = tp->mmio_addr;
3894
3895 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3896}
3897
3898static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3899{
3900 void __iomem *ioaddr = tp->mmio_addr;
3901
3902 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3903}
3904
3905static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3906{
3907 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003908
3909 RTL_W8(MaxTxPacketSize, 0x3f);
3910 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3911 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003912 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003913}
3914
3915static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3916{
3917 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003918
3919 RTL_W8(MaxTxPacketSize, 0x0c);
3920 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3921 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003922 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003923}
3924
3925static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3926{
3927 rtl_tx_performance_tweak(tp->pci_dev,
3928 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3929}
3930
3931static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3932{
3933 rtl_tx_performance_tweak(tp->pci_dev,
3934 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3935}
3936
3937static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3938{
3939 void __iomem *ioaddr = tp->mmio_addr;
3940
3941 r8168b_0_hw_jumbo_enable(tp);
3942
3943 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3944}
3945
3946static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3947{
3948 void __iomem *ioaddr = tp->mmio_addr;
3949
3950 r8168b_0_hw_jumbo_disable(tp);
3951
3952 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3953}
3954
3955static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3956{
3957 struct jumbo_ops *ops = &tp->jumbo_ops;
3958
3959 switch (tp->mac_version) {
3960 case RTL_GIGA_MAC_VER_11:
3961 ops->disable = r8168b_0_hw_jumbo_disable;
3962 ops->enable = r8168b_0_hw_jumbo_enable;
3963 break;
3964 case RTL_GIGA_MAC_VER_12:
3965 case RTL_GIGA_MAC_VER_17:
3966 ops->disable = r8168b_1_hw_jumbo_disable;
3967 ops->enable = r8168b_1_hw_jumbo_enable;
3968 break;
3969 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3970 case RTL_GIGA_MAC_VER_19:
3971 case RTL_GIGA_MAC_VER_20:
3972 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3973 case RTL_GIGA_MAC_VER_22:
3974 case RTL_GIGA_MAC_VER_23:
3975 case RTL_GIGA_MAC_VER_24:
3976 case RTL_GIGA_MAC_VER_25:
3977 case RTL_GIGA_MAC_VER_26:
3978 ops->disable = r8168c_hw_jumbo_disable;
3979 ops->enable = r8168c_hw_jumbo_enable;
3980 break;
3981 case RTL_GIGA_MAC_VER_27:
3982 case RTL_GIGA_MAC_VER_28:
3983 ops->disable = r8168dp_hw_jumbo_disable;
3984 ops->enable = r8168dp_hw_jumbo_enable;
3985 break;
3986 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3987 case RTL_GIGA_MAC_VER_32:
3988 case RTL_GIGA_MAC_VER_33:
3989 case RTL_GIGA_MAC_VER_34:
3990 ops->disable = r8168e_hw_jumbo_disable;
3991 ops->enable = r8168e_hw_jumbo_enable;
3992 break;
3993
3994 /*
3995 * No action needed for jumbo frames with 8169.
3996 * No jumbo for 810x at all.
3997 */
3998 default:
3999 ops->disable = NULL;
4000 ops->enable = NULL;
4001 break;
4002 }
4003}
4004
Francois Romieu6f43adc2011-04-29 15:05:51 +02004005static void rtl_hw_reset(struct rtl8169_private *tp)
4006{
4007 void __iomem *ioaddr = tp->mmio_addr;
4008 int i;
4009
4010 /* Soft reset the chip. */
4011 RTL_W8(ChipCmd, CmdReset);
4012
4013 /* Check that the chip has finished the reset. */
4014 for (i = 0; i < 100; i++) {
4015 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
4016 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004017 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004018 }
4019}
4020
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004021static int __devinit
4022rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4023{
Francois Romieu0e485152007-02-20 00:00:26 +01004024 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
4025 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02004027 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004028 struct net_device *dev;
4029 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004030 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01004031 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004033 if (netif_msg_drv(&debug)) {
4034 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
4035 MODULENAME, RTL8169_VERSION);
4036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004039 if (!dev) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004040 rc = -ENOMEM;
4041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004045 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00004047 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02004048 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004049 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Francois Romieuccdffb92008-07-26 14:26:06 +02004051 mii = &tp->mii;
4052 mii->dev = dev;
4053 mii->mdio_read = rtl_mdio_read;
4054 mii->mdio_write = rtl_mdio_write;
4055 mii->phy_id_mask = 0x1f;
4056 mii->reg_num_mask = 0x1f;
4057 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
4058
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00004059 /* disable ASPM completely as that cause random device stop working
4060 * problems as well as full system hangs for some PCIe devices users */
4061 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4062 PCIE_LINK_STATE_CLKPM);
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4065 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004066 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004067 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004068 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070
françois romieu87aeec72010-04-26 11:42:06 +00004071 if (pci_set_mwi(pdev) < 0)
4072 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004075 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004076 netif_err(tp, probe, dev,
4077 "region #%d not an MMIO resource, aborting\n",
4078 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004080 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004084 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004085 netif_err(tp, probe, dev,
4086 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004088 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 }
4090
4091 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004092 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004093 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004094 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 }
4096
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004097 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
4099 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004100 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 tp->cp_cmd |= PCIDAC;
4102 dev->features |= NETIF_F_HIGHDMA;
4103 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004104 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004106 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004107 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 }
4109 }
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004112 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004113 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004114 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004116 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004118 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Jon Masone44daad2011-06-27 07:46:31 +00004120 if (!pci_is_pcie(pdev))
4121 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004122
Hayes Wange542a222011-07-06 15:58:04 +08004123 /* Identify chip attached to board */
4124 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4125
4126 rtl_init_rxcfg(tp);
4127
Francois Romieu9085cdfa2012-01-26 12:59:08 +01004128 rtl_irq_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Francois Romieu6f43adc2011-04-29 15:05:51 +02004130 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Francois Romieu9085cdfa2012-01-26 12:59:08 +01004132 rtl_ack_events(tp, 0xffff);
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004133
françois romieuca52efd2009-07-24 12:34:19 +00004134 pci_set_master(pdev);
4135
Francois Romieu7a8fc772011-03-01 17:18:33 +01004136 /*
4137 * Pretend we are using VLANs; This bypasses a nasty bug where
4138 * Interrupts stop flowing on high load on 8110SCd controllers.
4139 */
4140 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4141 tp->cp_cmd |= RxVlan;
4142
françois romieuc0e45c12011-01-03 15:08:04 +00004143 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004144 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004145 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Francois Romieu85bffe62011-04-27 08:22:39 +02004149 chipset = tp->mac_version;
4150 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Francois Romieu5d06a992006-02-23 00:47:58 +01004152 RTL_W8(Cfg9346, Cfg9346_Unlock);
4153 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4154 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004155 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4156 tp->features |= RTL_FEATURE_WOL;
4157 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4158 tp->features |= RTL_FEATURE_WOL;
françois romieu2ca6cf02011-12-15 08:37:43 +00004159 tp->features |= rtl_try_msi(tp, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004160 RTL_W8(Cfg9346, Cfg9346_Lock);
4161
David S. Miller8decf862011-09-22 03:23:13 -04004162 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 tp->set_speed = rtl8169_set_speed_tbi;
4164 tp->get_settings = rtl8169_gset_tbi;
4165 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4166 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4167 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004168 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 } else {
4170 tp->set_speed = rtl8169_set_speed_xmii;
4171 tp->get_settings = rtl8169_gset_xmii;
4172 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4173 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4174 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004175 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
4177
Francois Romieuda78dbf2012-01-26 14:18:23 +01004178 mutex_init(&tp->wk.mutex);
Francois Romieudf58ef512008-10-09 14:35:58 -07004179
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004180 /* Get MAC address */
Joe Perches6a3c910c2011-11-16 09:38:02 +00004181 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004183 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4187 dev->irq = pdev->irq;
4188 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004190 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Michał Mirosław350fb322011-04-08 06:35:56 +00004192 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4193 * properly for all devices */
4194 dev->features |= NETIF_F_RXCSUM |
4195 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4196
4197 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4198 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4199 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4200 NETIF_F_HIGHDMA;
4201
4202 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4203 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4204 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Ben Greear6bbe0212012-02-10 15:04:33 +00004206 dev->hw_features |= NETIF_F_RXALL;
Ben Greear79d0c1d2012-02-10 15:04:34 +00004207 dev->hw_features |= NETIF_F_RXFCS;
Ben Greear6bbe0212012-02-10 15:04:33 +00004208
Francois Romieu0e485152007-02-20 00:00:26 +01004209 tp->hw_start = cfg->hw_start;
Francois Romieuda78dbf2012-01-26 14:18:23 +01004210 tp->event_slow = cfg->event_slow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
David S. Miller8decf862011-09-22 03:23:13 -04004212 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4213 ~(RxBOVF | RxFOVF) : ~0;
4214
Francois Romieu2efa53f2007-03-09 00:00:05 +01004215 init_timer(&tp->timer);
4216 tp->timer.data = (unsigned long) dev;
4217 tp->timer.function = rtl8169_phy_timer;
4218
Francois Romieub6ffd972011-06-17 17:00:05 +02004219 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004222 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004223 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 pci_set_drvdata(pdev, dev);
4226
Joe Perchesbf82c182010-02-09 11:49:50 +00004227 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004228 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004229 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004230 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4231 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4232 "tx checksumming: %s]\n",
4233 rtl_chip_infos[chipset].jumbo_max,
4234 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Francois Romieucecb5fd2011-04-01 10:21:07 +02004237 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4238 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4239 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004240 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004241 }
françois romieub646d902011-01-03 15:08:21 +00004242
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004243 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Alan Sternf3ec4f82010-06-08 15:23:51 -04004245 if (pci_dev_run_wake(pdev))
4246 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004247
Ivan Vecera0d672e92011-02-15 02:08:39 +00004248 netif_carrier_off(dev);
4249
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004250out:
4251 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
françois romieu87aeec72010-04-26 11:42:06 +00004253err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004254 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004255 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004256err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004257 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004258err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004259 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004260 pci_disable_device(pdev);
4261err_out_free_dev_1:
4262 free_netdev(dev);
4263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
Francois Romieu07d3f512007-02-21 22:40:46 +01004266static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268 struct net_device *dev = pci_get_drvdata(pdev);
4269 struct rtl8169_private *tp = netdev_priv(dev);
4270
Francois Romieucecb5fd2011-04-01 10:21:07 +02004271 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4272 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4273 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004274 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004275 }
françois romieub646d902011-01-03 15:08:21 +00004276
Francois Romieu4422bcd2012-01-26 11:23:32 +01004277 cancel_work_sync(&tp->wk.work);
Francois Romieueb2a0212007-02-15 23:37:21 +01004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004280
François Romieu953a12c2011-04-24 17:38:48 +02004281 rtl_release_firmware(tp);
4282
Alan Sternf3ec4f82010-06-08 15:23:51 -04004283 if (pci_dev_run_wake(pdev))
4284 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004285
Ivan Veceracc098dc2009-11-29 23:12:52 -08004286 /* restore original MAC address */
4287 rtl_rar_set(tp, dev->perm_addr);
4288
Francois Romieufbac58f2007-10-04 22:51:38 +02004289 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4291 pci_set_drvdata(pdev, NULL);
4292}
4293
Francois Romieub6ffd972011-06-17 17:00:05 +02004294static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4295{
4296 struct rtl_fw *rtl_fw;
4297 const char *name;
4298 int rc = -ENOMEM;
4299
4300 name = rtl_lookup_firmware_name(tp);
4301 if (!name)
4302 goto out_no_firmware;
4303
4304 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4305 if (!rtl_fw)
4306 goto err_warn;
4307
4308 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4309 if (rc < 0)
4310 goto err_free;
4311
Francois Romieufd112f22011-06-18 00:10:29 +02004312 rc = rtl_check_firmware(tp, rtl_fw);
4313 if (rc < 0)
4314 goto err_release_firmware;
4315
Francois Romieub6ffd972011-06-17 17:00:05 +02004316 tp->rtl_fw = rtl_fw;
4317out:
4318 return;
4319
Francois Romieufd112f22011-06-18 00:10:29 +02004320err_release_firmware:
4321 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004322err_free:
4323 kfree(rtl_fw);
4324err_warn:
4325 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4326 name, rc);
4327out_no_firmware:
4328 tp->rtl_fw = NULL;
4329 goto out;
4330}
4331
François Romieu953a12c2011-04-24 17:38:48 +02004332static void rtl_request_firmware(struct rtl8169_private *tp)
4333{
Francois Romieub6ffd972011-06-17 17:00:05 +02004334 if (IS_ERR(tp->rtl_fw))
4335 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004336}
4337
Francois Romieu4422bcd2012-01-26 11:23:32 +01004338static void rtl_task(struct work_struct *);
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340static int rtl8169_open(struct net_device *dev)
4341{
4342 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004343 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004345 int retval = -ENOMEM;
4346
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004347 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Neil Hormanc0cd8842010-03-29 13:16:02 -07004349 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004351 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004353 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4354 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004356 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004358 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4359 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004361 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363 retval = rtl8169_init_ring(dev);
4364 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004365 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Francois Romieu4422bcd2012-01-26 11:23:32 +01004367 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Francois Romieu99f252b2007-04-02 22:59:59 +02004369 smp_mb();
4370
François Romieu953a12c2011-04-24 17:38:48 +02004371 rtl_request_firmware(tp);
4372
Francois Romieufbac58f2007-10-04 22:51:38 +02004373 retval = request_irq(dev->irq, rtl8169_interrupt,
4374 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004375 dev->name, dev);
4376 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004377 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004378
Francois Romieuda78dbf2012-01-26 14:18:23 +01004379 rtl_lock_work(tp);
4380
Francois Romieu6c4a70c2012-01-31 10:56:44 +01004381 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004382
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004383 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004384
françois romieueee3a962011-01-08 02:17:26 +00004385 rtl8169_init_phy(dev, tp);
4386
Francois Romieuda78dbf2012-01-26 14:18:23 +01004387 __rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004388
françois romieu065c27c2011-01-03 15:08:12 +00004389 rtl_pll_power_up(tp);
4390
Francois Romieu07ce4062007-02-23 23:36:39 +01004391 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Francois Romieuda78dbf2012-01-26 14:18:23 +01004393 netif_start_queue(dev);
4394
4395 rtl_unlock_work(tp);
4396
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004397 tp->saved_wolopts = 0;
4398 pm_runtime_put_noidle(&pdev->dev);
4399
françois romieueee3a962011-01-08 02:17:26 +00004400 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401out:
4402 return retval;
4403
François Romieu953a12c2011-04-24 17:38:48 +02004404err_release_fw_2:
4405 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004406 rtl8169_rx_clear(tp);
4407err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004408 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4409 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004410 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004411err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004412 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4413 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004414 tp->TxDescArray = NULL;
4415err_pm_runtime_put:
4416 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 goto out;
4418}
4419
Hayes Wang92fc43b2011-07-06 15:58:03 +08004420static void rtl_rx_close(struct rtl8169_private *tp)
4421{
4422 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004423
Francois Romieu1687b562011-07-19 17:21:29 +02004424 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004425}
4426
françois romieue6de30d2011-01-03 15:08:37 +00004427static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
françois romieue6de30d2011-01-03 15:08:37 +00004429 void __iomem *ioaddr = tp->mmio_addr;
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004432 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Hayes Wang92fc43b2011-07-06 15:58:03 +08004434 rtl_rx_close(tp);
4435
Hayes Wang5d2e1952011-02-22 17:26:22 +08004436 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004437 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4438 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004439 while (RTL_R8(TxPoll) & NPQ)
4440 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004441 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4442 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4443 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004444 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004445 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4446 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004447 } else {
4448 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4449 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004450 }
4451
Hayes Wang92fc43b2011-07-06 15:58:03 +08004452 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
Francois Romieu7f796d832007-06-11 23:04:41 +02004455static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004456{
4457 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004458
4459 /* Set DMA burst size and Interframe Gap Time */
4460 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4461 (InterFrameGap << TxInterFrameGapShift));
4462}
4463
Francois Romieu07ce4062007-02-23 23:36:39 +01004464static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
4466 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Francois Romieu07ce4062007-02-23 23:36:39 +01004468 tp->hw_start(dev);
4469
Francois Romieuda78dbf2012-01-26 14:18:23 +01004470 rtl_irq_enable_all(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004471}
4472
Francois Romieu7f796d832007-06-11 23:04:41 +02004473static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4474 void __iomem *ioaddr)
4475{
4476 /*
4477 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4478 * register to be written before TxDescAddrLow to work.
4479 * Switching from MMIO to I/O access fixes the issue as well.
4480 */
4481 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004482 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004483 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004484 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004485}
4486
4487static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4488{
4489 u16 cmd;
4490
4491 cmd = RTL_R16(CPlusCmd);
4492 RTL_W16(CPlusCmd, cmd);
4493 return cmd;
4494}
4495
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004496static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004497{
4498 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004499 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004500}
4501
Francois Romieu6dccd162007-02-13 23:38:05 +01004502static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4503{
Francois Romieu37441002011-06-17 22:58:54 +02004504 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004505 u32 mac_version;
4506 u32 clk;
4507 u32 val;
4508 } cfg2_info [] = {
4509 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4510 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4511 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4512 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004513 };
4514 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004515 unsigned int i;
4516 u32 clk;
4517
4518 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004519 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004520 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4521 RTL_W32(0x7c, p->val);
4522 break;
4523 }
4524 }
4525}
4526
Francois Romieu07ce4062007-02-23 23:36:39 +01004527static void rtl_hw_start_8169(struct net_device *dev)
4528{
4529 struct rtl8169_private *tp = netdev_priv(dev);
4530 void __iomem *ioaddr = tp->mmio_addr;
4531 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004532
Francois Romieu9cb427b2006-11-02 00:10:16 +01004533 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4534 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4535 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4536 }
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004539 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4540 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4541 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4542 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004543 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4544
Hayes Wange542a222011-07-06 15:58:04 +08004545 rtl_init_rxcfg(tp);
4546
françois romieuf0298f82011-01-03 15:07:42 +00004547 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004549 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Francois Romieucecb5fd2011-04-01 10:21:07 +02004551 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4552 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4553 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4554 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004555 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Francois Romieu7f796d832007-06-11 23:04:41 +02004557 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004558
Francois Romieucecb5fd2011-04-01 10:21:07 +02004559 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4560 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004561 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004563 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
4565
Francois Romieubcf0bf92006-07-26 23:14:13 +02004566 RTL_W16(CPlusCmd, tp->cp_cmd);
4567
Francois Romieu6dccd162007-02-13 23:38:05 +01004568 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 /*
4571 * Undocumented corner. Supposedly:
4572 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4573 */
4574 RTL_W16(IntrMitigate, 0x0000);
4575
Francois Romieu7f796d832007-06-11 23:04:41 +02004576 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004577
Francois Romieucecb5fd2011-04-01 10:21:07 +02004578 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4579 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4580 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4581 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004582 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4583 rtl_set_rx_tx_config_registers(tp);
4584 }
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004587
4588 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4589 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
4591 RTL_W32(RxMissed, 0);
4592
Francois Romieu07ce4062007-02-23 23:36:39 +01004593 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595 /* no early-rx interrupts */
4596 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
françois romieu650e8d52011-01-03 15:08:29 +00004599static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004600{
4601 u32 csi;
4602
4603 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004604 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4605}
4606
françois romieue6de30d2011-01-03 15:08:37 +00004607static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4608{
4609 rtl_csi_access_enable(ioaddr, 0x17000000);
4610}
4611
françois romieu650e8d52011-01-03 15:08:29 +00004612static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4613{
4614 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004615}
4616
4617struct ephy_info {
4618 unsigned int offset;
4619 u16 mask;
4620 u16 bits;
4621};
4622
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004623static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004624{
4625 u16 w;
4626
4627 while (len-- > 0) {
4628 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4629 rtl_ephy_write(ioaddr, e->offset, w);
4630 e++;
4631 }
4632}
4633
Francois Romieub726e492008-06-28 12:22:59 +02004634static void rtl_disable_clock_request(struct pci_dev *pdev)
4635{
Jon Masone44daad2011-06-27 07:46:31 +00004636 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004637
4638 if (cap) {
4639 u16 ctl;
4640
4641 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4642 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4643 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4644 }
4645}
4646
françois romieue6de30d2011-01-03 15:08:37 +00004647static void rtl_enable_clock_request(struct pci_dev *pdev)
4648{
Jon Masone44daad2011-06-27 07:46:31 +00004649 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004650
4651 if (cap) {
4652 u16 ctl;
4653
4654 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4655 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4656 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4657 }
4658}
4659
Francois Romieub726e492008-06-28 12:22:59 +02004660#define R8168_CPCMD_QUIRK_MASK (\
4661 EnableBist | \
4662 Mac_dbgo_oe | \
4663 Force_half_dup | \
4664 Force_rxflow_en | \
4665 Force_txflow_en | \
4666 Cxpl_dbg_sel | \
4667 ASF | \
4668 PktCntrDisable | \
4669 Mac_dbgo_sel)
4670
Francois Romieu219a1e92008-06-28 11:58:39 +02004671static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4672{
Francois Romieub726e492008-06-28 12:22:59 +02004673 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4674
4675 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4676
Francois Romieu2e68ae42008-06-28 12:00:55 +02004677 rtl_tx_performance_tweak(pdev,
4678 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004679}
4680
4681static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4682{
4683 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004684
françois romieuf0298f82011-01-03 15:07:42 +00004685 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004686
4687 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004688}
4689
4690static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4691{
Francois Romieub726e492008-06-28 12:22:59 +02004692 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4693
4694 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4695
Francois Romieu219a1e92008-06-28 11:58:39 +02004696 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004697
4698 rtl_disable_clock_request(pdev);
4699
4700 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004701}
4702
Francois Romieuef3386f2008-06-29 12:24:30 +02004703static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004704{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004705 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004706 { 0x01, 0, 0x0001 },
4707 { 0x02, 0x0800, 0x1000 },
4708 { 0x03, 0, 0x0042 },
4709 { 0x06, 0x0080, 0x0000 },
4710 { 0x07, 0, 0x2000 }
4711 };
4712
françois romieu650e8d52011-01-03 15:08:29 +00004713 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004714
4715 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4716
Francois Romieu219a1e92008-06-28 11:58:39 +02004717 __rtl_hw_start_8168cp(ioaddr, pdev);
4718}
4719
Francois Romieuef3386f2008-06-29 12:24:30 +02004720static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4721{
françois romieu650e8d52011-01-03 15:08:29 +00004722 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004723
4724 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4725
4726 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4727
4728 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4729}
4730
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004731static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4732{
françois romieu650e8d52011-01-03 15:08:29 +00004733 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004734
4735 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4736
4737 /* Magic. */
4738 RTL_W8(DBG_REG, 0x20);
4739
françois romieuf0298f82011-01-03 15:07:42 +00004740 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004741
4742 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4743
4744 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4745}
4746
Francois Romieu219a1e92008-06-28 11:58:39 +02004747static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4748{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004749 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004750 { 0x02, 0x0800, 0x1000 },
4751 { 0x03, 0, 0x0002 },
4752 { 0x06, 0x0080, 0x0000 }
4753 };
4754
françois romieu650e8d52011-01-03 15:08:29 +00004755 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004756
4757 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4758
4759 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4760
Francois Romieu219a1e92008-06-28 11:58:39 +02004761 __rtl_hw_start_8168cp(ioaddr, pdev);
4762}
4763
4764static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4765{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004766 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004767 { 0x01, 0, 0x0001 },
4768 { 0x03, 0x0400, 0x0220 }
4769 };
4770
françois romieu650e8d52011-01-03 15:08:29 +00004771 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004772
4773 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4774
Francois Romieu219a1e92008-06-28 11:58:39 +02004775 __rtl_hw_start_8168cp(ioaddr, pdev);
4776}
4777
Francois Romieu197ff762008-06-28 13:16:02 +02004778static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4779{
4780 rtl_hw_start_8168c_2(ioaddr, pdev);
4781}
4782
Francois Romieu6fb07052008-06-29 11:54:28 +02004783static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4784{
françois romieu650e8d52011-01-03 15:08:29 +00004785 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004786
4787 __rtl_hw_start_8168cp(ioaddr, pdev);
4788}
4789
Francois Romieu5b538df2008-07-20 16:22:45 +02004790static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4791{
françois romieu650e8d52011-01-03 15:08:29 +00004792 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004793
4794 rtl_disable_clock_request(pdev);
4795
françois romieuf0298f82011-01-03 15:07:42 +00004796 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004797
4798 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4799
4800 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4801}
4802
hayeswang4804b3b2011-03-21 01:50:29 +00004803static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4804{
4805 rtl_csi_access_enable_1(ioaddr);
4806
4807 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4808
4809 RTL_W8(MaxTxPacketSize, TxPacketMax);
4810
4811 rtl_disable_clock_request(pdev);
4812}
4813
françois romieue6de30d2011-01-03 15:08:37 +00004814static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4815{
4816 static const struct ephy_info e_info_8168d_4[] = {
4817 { 0x0b, ~0, 0x48 },
4818 { 0x19, 0x20, 0x50 },
4819 { 0x0c, ~0, 0x20 }
4820 };
4821 int i;
4822
4823 rtl_csi_access_enable_1(ioaddr);
4824
4825 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4826
4827 RTL_W8(MaxTxPacketSize, TxPacketMax);
4828
4829 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4830 const struct ephy_info *e = e_info_8168d_4 + i;
4831 u16 w;
4832
4833 w = rtl_ephy_read(ioaddr, e->offset);
4834 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4835 }
4836
4837 rtl_enable_clock_request(pdev);
4838}
4839
Hayes Wang70090422011-07-06 15:58:06 +08004840static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004841{
Hayes Wang70090422011-07-06 15:58:06 +08004842 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004843 { 0x00, 0x0200, 0x0100 },
4844 { 0x00, 0x0000, 0x0004 },
4845 { 0x06, 0x0002, 0x0001 },
4846 { 0x06, 0x0000, 0x0030 },
4847 { 0x07, 0x0000, 0x2000 },
4848 { 0x00, 0x0000, 0x0020 },
4849 { 0x03, 0x5800, 0x2000 },
4850 { 0x03, 0x0000, 0x0001 },
4851 { 0x01, 0x0800, 0x1000 },
4852 { 0x07, 0x0000, 0x4000 },
4853 { 0x1e, 0x0000, 0x2000 },
4854 { 0x19, 0xffff, 0xfe6c },
4855 { 0x0a, 0x0000, 0x0040 }
4856 };
4857
4858 rtl_csi_access_enable_2(ioaddr);
4859
Hayes Wang70090422011-07-06 15:58:06 +08004860 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004861
4862 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4863
4864 RTL_W8(MaxTxPacketSize, TxPacketMax);
4865
4866 rtl_disable_clock_request(pdev);
4867
4868 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004869 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4870 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004871
Francois Romieucecb5fd2011-04-01 10:21:07 +02004872 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004873}
4874
Hayes Wang70090422011-07-06 15:58:06 +08004875static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4876{
4877 static const struct ephy_info e_info_8168e_2[] = {
4878 { 0x09, 0x0000, 0x0080 },
4879 { 0x19, 0x0000, 0x0224 }
4880 };
4881
4882 rtl_csi_access_enable_1(ioaddr);
4883
4884 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4885
4886 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4887
4888 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4889 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4890 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4891 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4892 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4893 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4894 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4895 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4896 ERIAR_EXGMAC);
4897
Hayes Wang3090bd92011-09-06 16:55:15 +08004898 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004899
4900 rtl_disable_clock_request(pdev);
4901
4902 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4903 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4904
4905 /* Adjust EEE LED frequency */
4906 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4907
4908 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4909 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4910 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4911}
4912
Hayes Wangc2218922011-09-06 16:55:18 +08004913static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4914{
4915 static const struct ephy_info e_info_8168f_1[] = {
4916 { 0x06, 0x00c0, 0x0020 },
4917 { 0x08, 0x0001, 0x0002 },
4918 { 0x09, 0x0000, 0x0080 },
4919 { 0x19, 0x0000, 0x0224 }
4920 };
4921
4922 rtl_csi_access_enable_1(ioaddr);
4923
4924 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4925
4926 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4927
4928 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4929 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4930 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4931 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4932 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4933 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4934 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4935 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4936 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4937 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4938 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4939 ERIAR_EXGMAC);
4940
4941 RTL_W8(MaxTxPacketSize, EarlySize);
4942
4943 rtl_disable_clock_request(pdev);
4944
4945 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4946 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4947
4948 /* Adjust EEE LED frequency */
4949 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4950
4951 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4952 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4953 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4954}
4955
Francois Romieu07ce4062007-02-23 23:36:39 +01004956static void rtl_hw_start_8168(struct net_device *dev)
4957{
Francois Romieu2dd99532007-06-11 23:22:52 +02004958 struct rtl8169_private *tp = netdev_priv(dev);
4959 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004960 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004961
4962 RTL_W8(Cfg9346, Cfg9346_Unlock);
4963
françois romieuf0298f82011-01-03 15:07:42 +00004964 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004965
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004966 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004967
Francois Romieu0e485152007-02-20 00:00:26 +01004968 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004969
4970 RTL_W16(CPlusCmd, tp->cp_cmd);
4971
Francois Romieu0e485152007-02-20 00:00:26 +01004972 RTL_W16(IntrMitigate, 0x5151);
4973
4974 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004975 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01004976 tp->event_slow |= RxFIFOOver | PCSTimeout;
4977 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01004978 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004979
4980 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4981
Francois Romieub8363902008-06-01 12:31:57 +02004982 rtl_set_rx_mode(dev);
4983
4984 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4985 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004986
4987 RTL_R8(IntrMask);
4988
Francois Romieu219a1e92008-06-28 11:58:39 +02004989 switch (tp->mac_version) {
4990 case RTL_GIGA_MAC_VER_11:
4991 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004992 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004993
4994 case RTL_GIGA_MAC_VER_12:
4995 case RTL_GIGA_MAC_VER_17:
4996 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004997 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004998
4999 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02005000 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005001 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005002
5003 case RTL_GIGA_MAC_VER_19:
5004 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005005 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005006
5007 case RTL_GIGA_MAC_VER_20:
5008 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005009 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005010
Francois Romieu197ff762008-06-28 13:16:02 +02005011 case RTL_GIGA_MAC_VER_21:
5012 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005013 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005014
Francois Romieu6fb07052008-06-29 11:54:28 +02005015 case RTL_GIGA_MAC_VER_22:
5016 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005017 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005018
Francois Romieuef3386f2008-06-29 12:24:30 +02005019 case RTL_GIGA_MAC_VER_23:
5020 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005021 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005022
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005023 case RTL_GIGA_MAC_VER_24:
5024 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005025 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005026
Francois Romieu5b538df2008-07-20 16:22:45 +02005027 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005028 case RTL_GIGA_MAC_VER_26:
5029 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02005030 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005031 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005032
françois romieue6de30d2011-01-03 15:08:37 +00005033 case RTL_GIGA_MAC_VER_28:
5034 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00005035 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005036
hayeswang4804b3b2011-03-21 01:50:29 +00005037 case RTL_GIGA_MAC_VER_31:
5038 rtl_hw_start_8168dp(ioaddr, pdev);
5039 break;
5040
hayeswang01dc7fe2011-03-21 01:50:28 +00005041 case RTL_GIGA_MAC_VER_32:
5042 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08005043 rtl_hw_start_8168e_1(ioaddr, pdev);
5044 break;
5045 case RTL_GIGA_MAC_VER_34:
5046 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00005047 break;
françois romieue6de30d2011-01-03 15:08:37 +00005048
Hayes Wangc2218922011-09-06 16:55:18 +08005049 case RTL_GIGA_MAC_VER_35:
5050 case RTL_GIGA_MAC_VER_36:
5051 rtl_hw_start_8168f_1(ioaddr, pdev);
5052 break;
5053
Francois Romieu219a1e92008-06-28 11:58:39 +02005054 default:
5055 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5056 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005057 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005058 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005059
Francois Romieu0e485152007-02-20 00:00:26 +01005060 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5061
Francois Romieub8363902008-06-01 12:31:57 +02005062 RTL_W8(Cfg9346, Cfg9346_Lock);
5063
Francois Romieu2dd99532007-06-11 23:22:52 +02005064 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Francois Romieu2857ffb2008-08-02 21:08:49 +02005067#define R810X_CPCMD_QUIRK_MASK (\
5068 EnableBist | \
5069 Mac_dbgo_oe | \
5070 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005071 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005072 Force_txflow_en | \
5073 Cxpl_dbg_sel | \
5074 ASF | \
5075 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005076 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005077
5078static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5079{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005080 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005081 { 0x01, 0, 0x6e65 },
5082 { 0x02, 0, 0x091f },
5083 { 0x03, 0, 0xc2f9 },
5084 { 0x06, 0, 0xafb5 },
5085 { 0x07, 0, 0x0e00 },
5086 { 0x19, 0, 0xec80 },
5087 { 0x01, 0, 0x2e65 },
5088 { 0x01, 0, 0x6e65 }
5089 };
5090 u8 cfg1;
5091
françois romieu650e8d52011-01-03 15:08:29 +00005092 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005093
5094 RTL_W8(DBG_REG, FIX_NAK_1);
5095
5096 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5097
5098 RTL_W8(Config1,
5099 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5100 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5101
5102 cfg1 = RTL_R8(Config1);
5103 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5104 RTL_W8(Config1, cfg1 & ~LEDS0);
5105
Francois Romieu2857ffb2008-08-02 21:08:49 +02005106 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5107}
5108
5109static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5110{
françois romieu650e8d52011-01-03 15:08:29 +00005111 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005112
5113 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5114
5115 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5116 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005117}
5118
5119static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5120{
5121 rtl_hw_start_8102e_2(ioaddr, pdev);
5122
5123 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5124}
5125
Hayes Wang5a5e4442011-02-22 17:26:21 +08005126static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5127{
5128 static const struct ephy_info e_info_8105e_1[] = {
5129 { 0x07, 0, 0x4000 },
5130 { 0x19, 0, 0x0200 },
5131 { 0x19, 0, 0x0020 },
5132 { 0x1e, 0, 0x2000 },
5133 { 0x03, 0, 0x0001 },
5134 { 0x19, 0, 0x0100 },
5135 { 0x19, 0, 0x0004 },
5136 { 0x0a, 0, 0x0020 }
5137 };
5138
Francois Romieucecb5fd2011-04-01 10:21:07 +02005139 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005140 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5141
Francois Romieucecb5fd2011-04-01 10:21:07 +02005142 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005143 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5144
5145 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005146 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005147
5148 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5149}
5150
5151static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5152{
5153 rtl_hw_start_8105e_1(ioaddr, pdev);
5154 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5155}
5156
Francois Romieu07ce4062007-02-23 23:36:39 +01005157static void rtl_hw_start_8101(struct net_device *dev)
5158{
Francois Romieucdf1a602007-06-11 23:29:50 +02005159 struct rtl8169_private *tp = netdev_priv(dev);
5160 void __iomem *ioaddr = tp->mmio_addr;
5161 struct pci_dev *pdev = tp->pci_dev;
5162
Francois Romieuda78dbf2012-01-26 14:18:23 +01005163 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5164 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005165
Francois Romieucecb5fd2011-04-01 10:21:07 +02005166 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5167 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005168 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005169
5170 if (cap) {
5171 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5172 PCI_EXP_DEVCTL_NOSNOOP_EN);
5173 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005174 }
5175
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005176 RTL_W8(Cfg9346, Cfg9346_Unlock);
5177
Francois Romieu2857ffb2008-08-02 21:08:49 +02005178 switch (tp->mac_version) {
5179 case RTL_GIGA_MAC_VER_07:
5180 rtl_hw_start_8102e_1(ioaddr, pdev);
5181 break;
5182
5183 case RTL_GIGA_MAC_VER_08:
5184 rtl_hw_start_8102e_3(ioaddr, pdev);
5185 break;
5186
5187 case RTL_GIGA_MAC_VER_09:
5188 rtl_hw_start_8102e_2(ioaddr, pdev);
5189 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005190
5191 case RTL_GIGA_MAC_VER_29:
5192 rtl_hw_start_8105e_1(ioaddr, pdev);
5193 break;
5194 case RTL_GIGA_MAC_VER_30:
5195 rtl_hw_start_8105e_2(ioaddr, pdev);
5196 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005197 }
5198
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005199 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005200
françois romieuf0298f82011-01-03 15:07:42 +00005201 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005202
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005203 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005204
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005205 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005206 RTL_W16(CPlusCmd, tp->cp_cmd);
5207
5208 RTL_W16(IntrMitigate, 0x0000);
5209
5210 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5211
5212 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5213 rtl_set_rx_tx_config_registers(tp);
5214
Francois Romieucdf1a602007-06-11 23:29:50 +02005215 RTL_R8(IntrMask);
5216
Francois Romieucdf1a602007-06-11 23:29:50 +02005217 rtl_set_rx_mode(dev);
5218
5219 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
5222static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5223{
Francois Romieud58d46b2011-05-03 16:38:29 +02005224 struct rtl8169_private *tp = netdev_priv(dev);
5225
5226 if (new_mtu < ETH_ZLEN ||
5227 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 return -EINVAL;
5229
Francois Romieud58d46b2011-05-03 16:38:29 +02005230 if (new_mtu > ETH_DATA_LEN)
5231 rtl_hw_jumbo_enable(tp);
5232 else
5233 rtl_hw_jumbo_disable(tp);
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005236 netdev_update_features(dev);
5237
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
5241static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5242{
Al Viro95e09182007-12-22 18:55:39 +00005243 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5245}
5246
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005247static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5248 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005250 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005251 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005252
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005253 kfree(*data_buff);
5254 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 rtl8169_make_unusable_by_asic(desc);
5256}
5257
5258static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5259{
5260 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5261
5262 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5263}
5264
5265static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5266 u32 rx_buf_sz)
5267{
5268 desc->addr = cpu_to_le64(mapping);
5269 wmb();
5270 rtl8169_mark_to_asic(desc, rx_buf_sz);
5271}
5272
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005273static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005275 return (void *)ALIGN((long)data, 16);
5276}
5277
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005278static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5279 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005280{
5281 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005283 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005284 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005285 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005287 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5288 if (!data)
5289 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005290
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005291 if (rtl8169_align(data) != data) {
5292 kfree(data);
5293 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5294 if (!data)
5295 return NULL;
5296 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005297
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005298 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005299 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005300 if (unlikely(dma_mapping_error(d, mapping))) {
5301 if (net_ratelimit())
5302 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005303 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005307 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005308
5309err_out:
5310 kfree(data);
5311 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312}
5313
5314static void rtl8169_rx_clear(struct rtl8169_private *tp)
5315{
Francois Romieu07d3f512007-02-21 22:40:46 +01005316 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317
5318 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005319 if (tp->Rx_databuff[i]) {
5320 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 tp->RxDescArray + i);
5322 }
5323 }
5324}
5325
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005326static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005328 desc->opts1 |= cpu_to_le32(RingEnd);
5329}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005330
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005331static int rtl8169_rx_fill(struct rtl8169_private *tp)
5332{
5333 unsigned int i;
5334
5335 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005336 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005337
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005338 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005340
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005341 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005342 if (!data) {
5343 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005344 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005345 }
5346 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005349 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5350 return 0;
5351
5352err_out:
5353 rtl8169_rx_clear(tp);
5354 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355}
5356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357static int rtl8169_init_ring(struct net_device *dev)
5358{
5359 struct rtl8169_private *tp = netdev_priv(dev);
5360
5361 rtl8169_init_ring_indexes(tp);
5362
5363 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005364 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005366 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367}
5368
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005369static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 struct TxDesc *desc)
5371{
5372 unsigned int len = tx_skb->len;
5373
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005374 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 desc->opts1 = 0x00;
5377 desc->opts2 = 0x00;
5378 desc->addr = 0x00;
5379 tx_skb->len = 0;
5380}
5381
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005382static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5383 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
5385 unsigned int i;
5386
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005387 for (i = 0; i < n; i++) {
5388 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 struct ring_info *tx_skb = tp->tx_skb + entry;
5390 unsigned int len = tx_skb->len;
5391
5392 if (len) {
5393 struct sk_buff *skb = tx_skb->skb;
5394
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005395 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 tp->TxDescArray + entry);
5397 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005398 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 dev_kfree_skb(skb);
5400 tx_skb->skb = NULL;
5401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 }
5403 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005404}
5405
5406static void rtl8169_tx_clear(struct rtl8169_private *tp)
5407{
5408 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 tp->cur_tx = tp->dirty_tx = 0;
Igor Maravic036dafa2012-03-05 00:01:25 +01005410 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411}
5412
Francois Romieu4422bcd2012-01-26 11:23:32 +01005413static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
David Howellsc4028952006-11-22 14:57:56 +00005415 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005416 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
Francois Romieuda78dbf2012-01-26 14:18:23 +01005418 napi_disable(&tp->napi);
5419 netif_stop_queue(dev);
5420 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
françois romieuc7c2c392011-12-04 20:30:52 +00005422 rtl8169_hw_reset(tp);
5423
Francois Romieu56de4142011-03-15 17:29:31 +01005424 for (i = 0; i < NUM_RX_DESC; i++)
5425 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5426
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005428 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
Francois Romieuda78dbf2012-01-26 14:18:23 +01005430 napi_enable(&tp->napi);
Francois Romieu56de4142011-03-15 17:29:31 +01005431 rtl_hw_start(dev);
5432 netif_wake_queue(dev);
5433 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434}
5435
5436static void rtl8169_tx_timeout(struct net_device *dev)
5437{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005438 struct rtl8169_private *tp = netdev_priv(dev);
5439
5440 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
5443static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005444 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445{
5446 struct skb_shared_info *info = skb_shinfo(skb);
5447 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005448 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005449 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 entry = tp->cur_tx;
5452 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005453 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 dma_addr_t mapping;
5455 u32 status, len;
5456 void *addr;
5457
5458 entry = (entry + 1) % NUM_TX_DESC;
5459
5460 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005461 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005462 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005463 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005464 if (unlikely(dma_mapping_error(d, mapping))) {
5465 if (net_ratelimit())
5466 netif_err(tp, drv, tp->dev,
5467 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005468 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Francois Romieucecb5fd2011-04-01 10:21:07 +02005471 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005472 status = opts[0] | len |
5473 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005476 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 txd->addr = cpu_to_le64(mapping);
5478
5479 tp->tx_skb[entry].len = len;
5480 }
5481
5482 if (cur_frag) {
5483 tp->tx_skb[entry].skb = skb;
5484 txd->opts1 |= cpu_to_le32(LastFrag);
5485 }
5486
5487 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005488
5489err_out:
5490 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5491 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Francois Romieu2b7b4312011-04-18 22:53:24 -07005494static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5495 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005497 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005498 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005499 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Francois Romieu2b7b4312011-04-18 22:53:24 -07005501 if (mss) {
5502 opts[0] |= TD_LSO;
5503 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5504 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005505 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
5507 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005508 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005510 opts[offset] |= info->checksum.udp;
5511 else
5512 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514}
5515
Stephen Hemminger613573252009-08-31 19:50:58 +00005516static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5517 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
5519 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005520 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 struct TxDesc *txd = tp->TxDescArray + entry;
5522 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005523 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 dma_addr_t mapping;
5525 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005526 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005527 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005530 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005531 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
5533
5534 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005535 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005537 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005538 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005539 if (unlikely(dma_mapping_error(d, mapping))) {
5540 if (net_ratelimit())
5541 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005542 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
5545 tp->tx_skb[entry].len = len;
5546 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
Francois Romieu2b7b4312011-04-18 22:53:24 -07005548 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5549 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005550
Francois Romieu2b7b4312011-04-18 22:53:24 -07005551 rtl8169_tso_csum(tp, skb, opts);
5552
5553 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005554 if (frags < 0)
5555 goto err_dma_1;
5556 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005557 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005558 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005559 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005560 tp->tx_skb[entry].skb = skb;
5561 }
5562
Francois Romieu2b7b4312011-04-18 22:53:24 -07005563 txd->opts2 = cpu_to_le32(opts[1]);
5564
Igor Maravic036dafa2012-03-05 00:01:25 +01005565 netdev_sent_queue(dev, skb->len);
5566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 wmb();
5568
Francois Romieucecb5fd2011-04-01 10:21:07 +02005569 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005570 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 txd->opts1 = cpu_to_le32(status);
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 tp->cur_tx += frags + 1;
5574
David Dillow4c020a92010-03-03 16:33:10 +00005575 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Francois Romieucecb5fd2011-04-01 10:21:07 +02005577 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Francois Romieuda78dbf2012-01-26 14:18:23 +01005579 mmiowb();
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005582 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5583 * not miss a ring update when it notices a stopped queue.
5584 */
5585 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005587 /* Sync with rtl_tx:
5588 * - publish queue status and cur_tx ring index (write barrier)
5589 * - refresh dirty_tx ring index (read barrier).
5590 * May the current thread have a pessimistic view of the ring
5591 * status and forget to wake up queue, a racing rtl_tx thread
5592 * can't.
5593 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005594 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5596 netif_wake_queue(dev);
5597 }
5598
Stephen Hemminger613573252009-08-31 19:50:58 +00005599 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005601err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005602 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005603err_dma_0:
5604 dev_kfree_skb(skb);
5605 dev->stats.tx_dropped++;
5606 return NETDEV_TX_OK;
5607
5608err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005610 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005611 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612}
5613
5614static void rtl8169_pcierr_interrupt(struct net_device *dev)
5615{
5616 struct rtl8169_private *tp = netdev_priv(dev);
5617 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 u16 pci_status, pci_cmd;
5619
5620 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5621 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5622
Joe Perchesbf82c182010-02-09 11:49:50 +00005623 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5624 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 /*
5627 * The recovery sequence below admits a very elaborated explanation:
5628 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005629 * - I did not see what else could be done;
5630 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 *
5632 * Feel free to adjust to your needs.
5633 */
Francois Romieua27993f2006-12-18 00:04:19 +01005634 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005635 pci_cmd &= ~PCI_COMMAND_PARITY;
5636 else
5637 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5638
5639 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
5641 pci_write_config_word(pdev, PCI_STATUS,
5642 pci_status & (PCI_STATUS_DETECTED_PARITY |
5643 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5644 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5645
5646 /* The infamous DAC f*ckup only happens at boot time */
5647 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005648 void __iomem *ioaddr = tp->mmio_addr;
5649
Joe Perchesbf82c182010-02-09 11:49:50 +00005650 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 tp->cp_cmd &= ~PCIDAC;
5652 RTL_W16(CPlusCmd, tp->cp_cmd);
5653 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 }
5655
françois romieue6de30d2011-01-03 15:08:37 +00005656 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005657
Francois Romieu98ddf982012-01-31 10:47:34 +01005658 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660
Igor Maravic036dafa2012-03-05 00:01:25 +01005661struct rtl_txc {
5662 int packets;
5663 int bytes;
5664};
5665
Francois Romieuda78dbf2012-01-26 14:18:23 +01005666static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667{
Igor Maravic036dafa2012-03-05 00:01:25 +01005668 struct rtl8169_stats *tx_stats = &tp->tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 unsigned int dirty_tx, tx_left;
Igor Maravic036dafa2012-03-05 00:01:25 +01005670 struct rtl_txc txc = { 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 dirty_tx = tp->dirty_tx;
5673 smp_rmb();
5674 tx_left = tp->cur_tx - dirty_tx;
5675
5676 while (tx_left > 0) {
5677 unsigned int entry = dirty_tx % NUM_TX_DESC;
5678 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 u32 status;
5680
5681 rmb();
5682 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5683 if (status & DescOwn)
5684 break;
5685
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005686 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5687 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 if (status & LastFrag) {
Igor Maravic036dafa2012-03-05 00:01:25 +01005689 struct sk_buff *skb = tx_skb->skb;
5690
5691 txc.packets++;
5692 txc.bytes += skb->len;
5693 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 tx_skb->skb = NULL;
5695 }
5696 dirty_tx++;
5697 tx_left--;
5698 }
5699
Igor Maravic036dafa2012-03-05 00:01:25 +01005700 u64_stats_update_begin(&tx_stats->syncp);
5701 tx_stats->packets += txc.packets;
5702 tx_stats->bytes += txc.bytes;
5703 u64_stats_update_end(&tx_stats->syncp);
5704
5705 netdev_completed_queue(dev, txc.packets, txc.bytes);
5706
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 if (tp->dirty_tx != dirty_tx) {
5708 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005709 /* Sync with rtl8169_start_xmit:
5710 * - publish dirty_tx ring index (write barrier)
5711 * - refresh cur_tx ring index and queue status (read barrier)
5712 * May the current thread miss the stopped queue condition,
5713 * a racing xmit thread can only have a right view of the
5714 * ring status.
5715 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005716 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 if (netif_queue_stopped(dev) &&
5718 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5719 netif_wake_queue(dev);
5720 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005721 /*
5722 * 8168 hack: TxPoll requests are lost when the Tx packets are
5723 * too close. Let's kick an extra TxPoll request when a burst
5724 * of start_xmit activity is detected (if it is not detected,
5725 * it is slow enough). -- FR
5726 */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005727 if (tp->cur_tx != dirty_tx) {
5728 void __iomem *ioaddr = tp->mmio_addr;
5729
Francois Romieud78ae2d2007-08-26 20:08:19 +02005730 RTL_W8(TxPoll, NPQ);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 }
5733}
5734
Francois Romieu126fa4b2005-05-12 20:09:17 -04005735static inline int rtl8169_fragmented_frame(u32 status)
5736{
5737 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5738}
5739
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005740static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 u32 status = opts1 & RxProtoMask;
5743
5744 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005745 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 skb->ip_summed = CHECKSUM_UNNECESSARY;
5747 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005748 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005751static struct sk_buff *rtl8169_try_rx_copy(void *data,
5752 struct rtl8169_private *tp,
5753 int pkt_size,
5754 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005756 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005757 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005759 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005760 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005761 prefetch(data);
5762 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5763 if (skb)
5764 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005765 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5766
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005767 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768}
5769
Francois Romieuda78dbf2012-01-26 14:18:23 +01005770static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
5772 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005773 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 cur_rx = tp->cur_rx;
5776 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005777 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005779 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005781 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 u32 status;
5783
5784 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005785 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
5787 if (status & DescOwn)
5788 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005789 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005790 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5791 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005792 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005794 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005796 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005797 if (status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005798 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005799 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005800 }
Ben Greear6bbe0212012-02-10 15:04:33 +00005801 if ((status & (RxRUNT | RxCRC)) &&
5802 !(status & (RxRWT | RxFOVF)) &&
5803 (dev->features & NETIF_F_RXALL))
5804 goto process_pkt;
5805
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005806 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005808 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005809 dma_addr_t addr;
5810 int pkt_size;
5811
5812process_pkt:
5813 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005814 if (likely(!(dev->features & NETIF_F_RXFCS)))
5815 pkt_size = (status & 0x00003fff) - 4;
5816 else
5817 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
Francois Romieu126fa4b2005-05-12 20:09:17 -04005819 /*
5820 * The driver does not support incoming fragmented
5821 * frames. They are seen as a symptom of over-mtu
5822 * sized frames.
5823 */
5824 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005825 dev->stats.rx_dropped++;
5826 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005827 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005828 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005829 }
5830
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005831 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5832 tp, pkt_size, addr);
5833 rtl8169_mark_to_asic(desc, rx_buf_sz);
5834 if (!skb) {
5835 dev->stats.rx_dropped++;
5836 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 }
5838
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005839 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 skb_put(skb, pkt_size);
5841 skb->protocol = eth_type_trans(skb, dev);
5842
Francois Romieu7a8fc772011-03-01 17:18:33 +01005843 rtl8169_rx_vlan_tag(desc, skb);
5844
Francois Romieu56de4142011-03-15 17:29:31 +01005845 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
Junchang Wang8027aa22012-03-04 23:30:32 +01005847 u64_stats_update_begin(&tp->rx_stats.syncp);
5848 tp->rx_stats.packets++;
5849 tp->rx_stats.bytes += pkt_size;
5850 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005852
5853 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005854 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005855 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5856 desc->opts2 = 0;
5857 cur_rx++;
5858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 }
5860
5861 count = cur_rx - tp->cur_rx;
5862 tp->cur_rx = cur_rx;
5863
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005864 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866 return count;
5867}
5868
Francois Romieu07d3f512007-02-21 22:40:46 +01005869static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870{
Francois Romieu07d3f512007-02-21 22:40:46 +01005871 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005874 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005876 status = rtl_get_events(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005877 if (status && status != 0xffff) {
5878 status &= RTL_EVENT_NAPI | tp->event_slow;
5879 if (status) {
5880 handled = 1;
françois romieu811fd302011-12-04 20:30:45 +00005881
Francois Romieuda78dbf2012-01-26 14:18:23 +01005882 rtl_irq_disable(tp);
5883 napi_schedule(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 return IRQ_RETVAL(handled);
5887}
5888
Francois Romieuda78dbf2012-01-26 14:18:23 +01005889/*
5890 * Workqueue context.
5891 */
5892static void rtl_slow_event_work(struct rtl8169_private *tp)
5893{
5894 struct net_device *dev = tp->dev;
5895 u16 status;
5896
5897 status = rtl_get_events(tp) & tp->event_slow;
5898 rtl_ack_events(tp, status);
5899
5900 if (unlikely(status & RxFIFOOver)) {
5901 switch (tp->mac_version) {
5902 /* Work around for rx fifo overflow */
5903 case RTL_GIGA_MAC_VER_11:
5904 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01005905 /* XXX - Hack alert. See rtl_task(). */
5906 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005907 default:
5908 break;
5909 }
5910 }
5911
5912 if (unlikely(status & SYSErr))
5913 rtl8169_pcierr_interrupt(dev);
5914
5915 if (status & LinkChg)
5916 __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
5917
5918 napi_disable(&tp->napi);
5919 rtl_irq_disable(tp);
5920
5921 napi_enable(&tp->napi);
5922 napi_schedule(&tp->napi);
5923}
5924
Francois Romieu4422bcd2012-01-26 11:23:32 +01005925static void rtl_task(struct work_struct *work)
5926{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005927 static const struct {
5928 int bitnr;
5929 void (*action)(struct rtl8169_private *);
5930 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01005931 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01005932 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
5933 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
5934 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
5935 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005936 struct rtl8169_private *tp =
5937 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005938 struct net_device *dev = tp->dev;
5939 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005940
Francois Romieuda78dbf2012-01-26 14:18:23 +01005941 rtl_lock_work(tp);
5942
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005943 if (!netif_running(dev) ||
5944 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005945 goto out_unlock;
5946
5947 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5948 bool pending;
5949
Francois Romieuda78dbf2012-01-26 14:18:23 +01005950 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005951 if (pending)
5952 rtl_work[i].action(tp);
5953 }
5954
5955out_unlock:
5956 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005957}
5958
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005959static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005961 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5962 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005963 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
5964 int work_done= 0;
5965 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
Francois Romieuda78dbf2012-01-26 14:18:23 +01005967 status = rtl_get_events(tp);
5968 rtl_ack_events(tp, status & ~tp->event_slow);
5969
5970 if (status & RTL_EVENT_NAPI_RX)
5971 work_done = rtl_rx(dev, tp, (u32) budget);
5972
5973 if (status & RTL_EVENT_NAPI_TX)
5974 rtl_tx(dev, tp);
5975
5976 if (status & tp->event_slow) {
5977 enable_mask &= ~tp->event_slow;
5978
5979 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
5980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005982 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005983 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005984
Francois Romieuda78dbf2012-01-26 14:18:23 +01005985 rtl_irq_enable(tp, enable_mask);
5986 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
5988
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005989 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Francois Romieu523a6092008-09-10 22:28:56 +02005992static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5993{
5994 struct rtl8169_private *tp = netdev_priv(dev);
5995
5996 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5997 return;
5998
5999 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
6000 RTL_W32(RxMissed, 0);
6001}
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003static void rtl8169_down(struct net_device *dev)
6004{
6005 struct rtl8169_private *tp = netdev_priv(dev);
6006 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Francois Romieu4876cc12011-03-11 21:07:11 +01006008 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006010 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006011 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Hayes Wang92fc43b2011-07-06 15:58:03 +08006013 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006014 /*
6015 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006016 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6017 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006018 */
Francois Romieu523a6092008-09-10 22:28:56 +02006019 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006022 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 rtl8169_tx_clear(tp);
6025
6026 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006027
6028 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
6031static int rtl8169_close(struct net_device *dev)
6032{
6033 struct rtl8169_private *tp = netdev_priv(dev);
6034 struct pci_dev *pdev = tp->pci_dev;
6035
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006036 pm_runtime_get_sync(&pdev->dev);
6037
Francois Romieucecb5fd2011-04-01 10:21:07 +02006038 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08006039 rtl8169_update_counters(dev);
6040
Francois Romieuda78dbf2012-01-26 14:18:23 +01006041 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006042 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006045 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
6047 free_irq(dev->irq, dev);
6048
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006049 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6050 tp->RxPhyAddr);
6051 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6052 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 tp->TxDescArray = NULL;
6054 tp->RxDescArray = NULL;
6055
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006056 pm_runtime_put_sync(&pdev->dev);
6057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 return 0;
6059}
6060
Francois Romieu07ce4062007-02-23 23:36:39 +01006061static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
6063 struct rtl8169_private *tp = netdev_priv(dev);
6064 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01006066 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 u32 tmp = 0;
6068
6069 if (dev->flags & IFF_PROMISC) {
6070 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00006071 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 rx_mode =
6073 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
6074 AcceptAllPhys;
6075 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00006076 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00006077 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 /* Too many to filter perfectly -- accept all multicasts. */
6079 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
6080 mc_filter[1] = mc_filter[0] = 0xffffffff;
6081 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00006082 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01006083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 rx_mode = AcceptBroadcast | AcceptMyPhys;
6085 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00006086 netdev_for_each_mc_addr(ha, dev) {
6087 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6089 rx_mode |= AcceptMulticast;
6090 }
6091 }
6092
Ben Greear6bbe0212012-02-10 15:04:33 +00006093 if (dev->features & NETIF_F_RXALL)
6094 rx_mode |= (AcceptErr | AcceptRunt);
6095
Francois Romieu1687b562011-07-19 17:21:29 +02006096 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Francois Romieuf887cce2008-07-17 22:24:18 +02006098 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01006099 u32 data = mc_filter[0];
6100
6101 mc_filter[0] = swab32(mc_filter[1]);
6102 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02006103 }
6104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006106 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Francois Romieu57a9f232007-06-04 22:10:15 +02006108 RTL_W32(RxConfig, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109}
6110
Junchang Wang8027aa22012-03-04 23:30:32 +01006111static struct rtnl_link_stats64 *
6112rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113{
6114 struct rtl8169_private *tp = netdev_priv(dev);
6115 void __iomem *ioaddr = tp->mmio_addr;
Junchang Wang8027aa22012-03-04 23:30:32 +01006116 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
Francois Romieuda78dbf2012-01-26 14:18:23 +01006118 if (netif_running(dev))
Francois Romieu523a6092008-09-10 22:28:56 +02006119 rtl8169_rx_missed(dev, ioaddr);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006120
Junchang Wang8027aa22012-03-04 23:30:32 +01006121 do {
6122 start = u64_stats_fetch_begin_bh(&tp->rx_stats.syncp);
6123 stats->rx_packets = tp->rx_stats.packets;
6124 stats->rx_bytes = tp->rx_stats.bytes;
6125 } while (u64_stats_fetch_retry_bh(&tp->rx_stats.syncp, start));
6126
6127
6128 do {
6129 start = u64_stats_fetch_begin_bh(&tp->tx_stats.syncp);
6130 stats->tx_packets = tp->tx_stats.packets;
6131 stats->tx_bytes = tp->tx_stats.bytes;
6132 } while (u64_stats_fetch_retry_bh(&tp->tx_stats.syncp, start));
6133
6134 stats->rx_dropped = dev->stats.rx_dropped;
6135 stats->tx_dropped = dev->stats.tx_dropped;
6136 stats->rx_length_errors = dev->stats.rx_length_errors;
6137 stats->rx_errors = dev->stats.rx_errors;
6138 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6139 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6140 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6141
6142 return stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143}
6144
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006145static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006146{
françois romieu065c27c2011-01-03 15:08:12 +00006147 struct rtl8169_private *tp = netdev_priv(dev);
6148
Francois Romieu5d06a992006-02-23 00:47:58 +01006149 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006150 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006151
6152 netif_device_detach(dev);
6153 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006154
6155 rtl_lock_work(tp);
6156 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006157 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006158 rtl_unlock_work(tp);
6159
6160 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006161}
Francois Romieu5d06a992006-02-23 00:47:58 +01006162
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006163#ifdef CONFIG_PM
6164
6165static int rtl8169_suspend(struct device *device)
6166{
6167 struct pci_dev *pdev = to_pci_dev(device);
6168 struct net_device *dev = pci_get_drvdata(pdev);
6169
6170 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006171
Francois Romieu5d06a992006-02-23 00:47:58 +01006172 return 0;
6173}
6174
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006175static void __rtl8169_resume(struct net_device *dev)
6176{
françois romieu065c27c2011-01-03 15:08:12 +00006177 struct rtl8169_private *tp = netdev_priv(dev);
6178
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006179 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006180
6181 rtl_pll_power_up(tp);
6182
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006183 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006184
Francois Romieu98ddf982012-01-31 10:47:34 +01006185 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006186}
6187
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006188static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006189{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006190 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006191 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006192 struct rtl8169_private *tp = netdev_priv(dev);
6193
6194 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006195
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006196 if (netif_running(dev))
6197 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006198
Francois Romieu5d06a992006-02-23 00:47:58 +01006199 return 0;
6200}
6201
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006202static int rtl8169_runtime_suspend(struct device *device)
6203{
6204 struct pci_dev *pdev = to_pci_dev(device);
6205 struct net_device *dev = pci_get_drvdata(pdev);
6206 struct rtl8169_private *tp = netdev_priv(dev);
6207
6208 if (!tp->TxDescArray)
6209 return 0;
6210
Francois Romieuda78dbf2012-01-26 14:18:23 +01006211 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006212 tp->saved_wolopts = __rtl8169_get_wol(tp);
6213 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006214 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006215
6216 rtl8169_net_suspend(dev);
6217
6218 return 0;
6219}
6220
6221static int rtl8169_runtime_resume(struct device *device)
6222{
6223 struct pci_dev *pdev = to_pci_dev(device);
6224 struct net_device *dev = pci_get_drvdata(pdev);
6225 struct rtl8169_private *tp = netdev_priv(dev);
6226
6227 if (!tp->TxDescArray)
6228 return 0;
6229
Francois Romieuda78dbf2012-01-26 14:18:23 +01006230 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006231 __rtl8169_set_wol(tp, tp->saved_wolopts);
6232 tp->saved_wolopts = 0;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006233 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006234
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006235 rtl8169_init_phy(dev, tp);
6236
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006237 __rtl8169_resume(dev);
6238
6239 return 0;
6240}
6241
6242static int rtl8169_runtime_idle(struct device *device)
6243{
6244 struct pci_dev *pdev = to_pci_dev(device);
6245 struct net_device *dev = pci_get_drvdata(pdev);
6246 struct rtl8169_private *tp = netdev_priv(dev);
6247
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006248 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006249}
6250
Alexey Dobriyan47145212009-12-14 18:00:08 -08006251static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006252 .suspend = rtl8169_suspend,
6253 .resume = rtl8169_resume,
6254 .freeze = rtl8169_suspend,
6255 .thaw = rtl8169_resume,
6256 .poweroff = rtl8169_suspend,
6257 .restore = rtl8169_resume,
6258 .runtime_suspend = rtl8169_runtime_suspend,
6259 .runtime_resume = rtl8169_runtime_resume,
6260 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006261};
6262
6263#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6264
6265#else /* !CONFIG_PM */
6266
6267#define RTL8169_PM_OPS NULL
6268
6269#endif /* !CONFIG_PM */
6270
David S. Miller1805b2f2011-10-24 18:18:09 -04006271static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6272{
6273 void __iomem *ioaddr = tp->mmio_addr;
6274
6275 /* WoL fails with 8168b when the receiver is disabled. */
6276 switch (tp->mac_version) {
6277 case RTL_GIGA_MAC_VER_11:
6278 case RTL_GIGA_MAC_VER_12:
6279 case RTL_GIGA_MAC_VER_17:
6280 pci_clear_master(tp->pci_dev);
6281
6282 RTL_W8(ChipCmd, CmdRxEnb);
6283 /* PCI commit */
6284 RTL_R8(ChipCmd);
6285 break;
6286 default:
6287 break;
6288 }
6289}
6290
Francois Romieu1765f952008-09-13 17:21:40 +02006291static void rtl_shutdown(struct pci_dev *pdev)
6292{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006293 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006294 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu2a15cd22012-03-06 01:14:12 +00006295 struct device *d = &pdev->dev;
6296
6297 pm_runtime_get_sync(d);
Francois Romieu1765f952008-09-13 17:21:40 +02006298
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006299 rtl8169_net_suspend(dev);
6300
Francois Romieucecb5fd2011-04-01 10:21:07 +02006301 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006302 rtl_rar_set(tp, dev->perm_addr);
6303
Hayes Wang92fc43b2011-07-06 15:58:03 +08006304 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006305
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006306 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006307 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6308 rtl_wol_suspend_quirk(tp);
6309 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006310 }
6311
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006312 pci_wake_from_d3(pdev, true);
6313 pci_set_power_state(pdev, PCI_D3hot);
6314 }
françois romieu2a15cd22012-03-06 01:14:12 +00006315
6316 pm_runtime_put_noidle(d);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006317}
Francois Romieu5d06a992006-02-23 00:47:58 +01006318
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319static struct pci_driver rtl8169_pci_driver = {
6320 .name = MODULENAME,
6321 .id_table = rtl8169_pci_tbl,
6322 .probe = rtl8169_init_one,
6323 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006324 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006325 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326};
6327
Francois Romieu07d3f512007-02-21 22:40:46 +01006328static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329{
Jeff Garzik29917622006-08-19 17:48:59 -04006330 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331}
6332
Francois Romieu07d3f512007-02-21 22:40:46 +01006333static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334{
6335 pci_unregister_driver(&rtl8169_pci_driver);
6336}
6337
6338module_init(rtl8169_init_module);
6339module_exit(rtl8169_cleanup_module);