blob: 8dd13f5a9203f7cdae46127b7eff8c4ea39e05c1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670struct rtl8169_private {
671 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200672 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000673 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700674 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200675 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200676 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700677 u16 txd_version;
678 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
680 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
681 u32 dirty_rx;
682 u32 dirty_tx;
683 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
684 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
685 dma_addr_t TxPhyAddr;
686 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000687 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct timer_list timer;
690 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100691 u16 intr_event;
692 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000694
695 struct mdio_ops {
696 void (*write)(void __iomem *, int, int);
697 int (*read)(void __iomem *, int);
698 } mdio_ops;
699
françois romieu065c27c2011-01-03 15:08:12 +0000700 struct pll_power_ops {
701 void (*down)(struct rtl8169_private *);
702 void (*up)(struct rtl8169_private *);
703 } pll_power_ops;
704
Francois Romieud58d46b2011-05-03 16:38:29 +0200705 struct jumbo_ops {
706 void (*enable)(struct rtl8169_private *);
707 void (*disable)(struct rtl8169_private *);
708 } jumbo_ops;
709
Oliver Neukum54405cd2011-01-06 21:55:13 +0100710 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200711 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000712 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100713 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000714 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800716 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100717
718 struct {
719 struct work_struct work;
720 } wk;
721
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200722 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200723
724 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800725 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000726 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400727 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000728
Francois Romieub6ffd972011-06-17 17:00:05 +0200729 struct rtl_fw {
730 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200731
732#define RTL_VER_SIZE 32
733
734 char version[RTL_VER_SIZE];
735
736 struct rtl_fw_phy_action {
737 __le32 *code;
738 size_t size;
739 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200740 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300741#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742};
743
Ralf Baechle979b6c12005-06-13 14:30:40 -0700744MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700747MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200748module_param_named(debug, debug.msg_enable, int, 0);
749MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750MODULE_LICENSE("GPL");
751MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000752MODULE_FIRMWARE(FIRMWARE_8168D_1);
753MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000754MODULE_FIRMWARE(FIRMWARE_8168E_1);
755MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400756MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800757MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800758MODULE_FIRMWARE(FIRMWARE_8168F_1);
759MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000762static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
763 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100764static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100766static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100768static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200770static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700772 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200773static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200775static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700776static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Francois Romieud58d46b2011-05-03 16:38:29 +0200778static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
779{
780 int cap = pci_pcie_cap(pdev);
781
782 if (cap) {
783 u16 ctl;
784
785 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
786 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
787 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
788 }
789}
790
françois romieub646d902011-01-03 15:08:21 +0000791static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
792{
793 void __iomem *ioaddr = tp->mmio_addr;
794 int i;
795
796 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
797 for (i = 0; i < 20; i++) {
798 udelay(100);
799 if (RTL_R32(OCPAR) & OCPAR_FLAG)
800 break;
801 }
802 return RTL_R32(OCPDR);
803}
804
805static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
806{
807 void __iomem *ioaddr = tp->mmio_addr;
808 int i;
809
810 RTL_W32(OCPDR, data);
811 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
812 for (i = 0; i < 20; i++) {
813 udelay(100);
814 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
815 break;
816 }
817}
818
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800819static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000820{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800821 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000822 int i;
823
824 RTL_W8(ERIDR, cmd);
825 RTL_W32(ERIAR, 0x800010e8);
826 msleep(2);
827 for (i = 0; i < 5; i++) {
828 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200829 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000830 break;
831 }
832
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800833 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000834}
835
836#define OOB_CMD_RESET 0x00
837#define OOB_CMD_DRIVER_START 0x05
838#define OOB_CMD_DRIVER_STOP 0x06
839
Francois Romieucecb5fd2011-04-01 10:21:07 +0200840static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
841{
842 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
843}
844
françois romieub646d902011-01-03 15:08:21 +0000845static void rtl8168_driver_start(struct rtl8169_private *tp)
846{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200847 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000848 int i;
849
850 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
851
Francois Romieucecb5fd2011-04-01 10:21:07 +0200852 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000853
françois romieub646d902011-01-03 15:08:21 +0000854 for (i = 0; i < 10; i++) {
855 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000856 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000857 break;
858 }
859}
860
861static void rtl8168_driver_stop(struct rtl8169_private *tp)
862{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200863 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000864 int i;
865
866 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
867
Francois Romieucecb5fd2011-04-01 10:21:07 +0200868 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000869
françois romieub646d902011-01-03 15:08:21 +0000870 for (i = 0; i < 10; i++) {
871 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000872 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000873 break;
874 }
875}
876
hayeswang4804b3b2011-03-21 01:50:29 +0000877static int r8168dp_check_dash(struct rtl8169_private *tp)
878{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200879 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000880
Francois Romieucecb5fd2011-04-01 10:21:07 +0200881 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000882}
françois romieub646d902011-01-03 15:08:21 +0000883
françois romieu4da19632011-01-03 15:07:55 +0000884static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int i;
887
Francois Romieua6baf3a2007-11-08 23:23:21 +0100888 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Francois Romieu23714082006-01-29 00:49:09 +0100890 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100891 /*
892 * Check if the RTL8169 has completed writing to the specified
893 * MII register.
894 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200895 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
Francois Romieu23714082006-01-29 00:49:09 +0100897 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700899 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700900 * According to hardware specs a 20us delay is required after write
901 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700902 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700903 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
françois romieu4da19632011-01-03 15:07:55 +0000906static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 int i, value = -1;
909
Francois Romieua6baf3a2007-11-08 23:23:21 +0100910 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Francois Romieu23714082006-01-29 00:49:09 +0100912 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100913 /*
914 * Check if the RTL8169 has completed retrieving data from
915 * the specified MII register.
916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100918 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 }
Francois Romieu23714082006-01-29 00:49:09 +0100921 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700923 /*
924 * According to hardware specs a 20us delay is required after read
925 * complete indication, but before sending next command.
926 */
927 udelay(20);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return value;
930}
931
françois romieuc0e45c12011-01-03 15:08:04 +0000932static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
933{
934 int i;
935
936 RTL_W32(OCPDR, data |
937 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
938 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
939 RTL_W32(EPHY_RXER_NUM, 0);
940
941 for (i = 0; i < 100; i++) {
942 mdelay(1);
943 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
944 break;
945 }
946}
947
948static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
949{
950 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
951 (value & OCPDR_DATA_MASK));
952}
953
954static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
955{
956 int i;
957
958 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
959
960 mdelay(1);
961 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
962 RTL_W32(EPHY_RXER_NUM, 0);
963
964 for (i = 0; i < 100; i++) {
965 mdelay(1);
966 if (RTL_R32(OCPAR) & OCPAR_FLAG)
967 break;
968 }
969
970 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
971}
972
françois romieue6de30d2011-01-03 15:08:37 +0000973#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
974
975static void r8168dp_2_mdio_start(void __iomem *ioaddr)
976{
977 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
978}
979
980static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
981{
982 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
983}
984
985static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
986{
987 r8168dp_2_mdio_start(ioaddr);
988
989 r8169_mdio_write(ioaddr, reg_addr, value);
990
991 r8168dp_2_mdio_stop(ioaddr);
992}
993
994static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
995{
996 int value;
997
998 r8168dp_2_mdio_start(ioaddr);
999
1000 value = r8169_mdio_read(ioaddr, reg_addr);
1001
1002 r8168dp_2_mdio_stop(ioaddr);
1003
1004 return value;
1005}
1006
françois romieu4da19632011-01-03 15:07:55 +00001007static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001008{
françois romieuc0e45c12011-01-03 15:08:04 +00001009 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001010}
1011
françois romieu4da19632011-01-03 15:07:55 +00001012static int rtl_readphy(struct rtl8169_private *tp, int location)
1013{
françois romieuc0e45c12011-01-03 15:08:04 +00001014 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001015}
1016
1017static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1018{
1019 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1020}
1021
1022static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001023{
1024 int val;
1025
françois romieu4da19632011-01-03 15:07:55 +00001026 val = rtl_readphy(tp, reg_addr);
1027 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001028}
1029
Francois Romieuccdffb92008-07-26 14:26:06 +02001030static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1031 int val)
1032{
1033 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001034
françois romieu4da19632011-01-03 15:07:55 +00001035 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001036}
1037
1038static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1039{
1040 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001041
françois romieu4da19632011-01-03 15:07:55 +00001042 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001043}
1044
Francois Romieudacf8152008-08-02 20:44:13 +02001045static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1046{
1047 unsigned int i;
1048
1049 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1050 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1051
1052 for (i = 0; i < 100; i++) {
1053 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1054 break;
1055 udelay(10);
1056 }
1057}
1058
1059static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1060{
1061 u16 value = 0xffff;
1062 unsigned int i;
1063
1064 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1065
1066 for (i = 0; i < 100; i++) {
1067 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1068 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1069 break;
1070 }
1071 udelay(10);
1072 }
1073
1074 return value;
1075}
1076
1077static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1078{
1079 unsigned int i;
1080
1081 RTL_W32(CSIDR, value);
1082 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1083 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1084
1085 for (i = 0; i < 100; i++) {
1086 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1087 break;
1088 udelay(10);
1089 }
1090}
1091
1092static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1093{
1094 u32 value = ~0x00;
1095 unsigned int i;
1096
1097 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1098 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1099
1100 for (i = 0; i < 100; i++) {
1101 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1102 value = RTL_R32(CSIDR);
1103 break;
1104 }
1105 udelay(10);
1106 }
1107
1108 return value;
1109}
1110
Hayes Wang133ac402011-07-06 15:58:05 +08001111static
1112void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1113{
1114 unsigned int i;
1115
1116 BUG_ON((addr & 3) || (mask == 0));
1117 RTL_W32(ERIDR, val);
1118 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1119
1120 for (i = 0; i < 100; i++) {
1121 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1122 break;
1123 udelay(100);
1124 }
1125}
1126
1127static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1128{
1129 u32 value = ~0x00;
1130 unsigned int i;
1131
1132 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1133
1134 for (i = 0; i < 100; i++) {
1135 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1136 value = RTL_R32(ERIDR);
1137 break;
1138 }
1139 udelay(100);
1140 }
1141
1142 return value;
1143}
1144
1145static void
1146rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1147{
1148 u32 val;
1149
1150 val = rtl_eri_read(ioaddr, addr, type);
1151 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1152}
1153
françois romieuc28aa382011-08-02 03:53:43 +00001154struct exgmac_reg {
1155 u16 addr;
1156 u16 mask;
1157 u32 val;
1158};
1159
1160static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1161 const struct exgmac_reg *r, int len)
1162{
1163 while (len-- > 0) {
1164 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1165 r++;
1166 }
1167}
1168
françois romieudaf9df62009-10-07 12:44:20 +00001169static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1170{
1171 u8 value = 0xff;
1172 unsigned int i;
1173
1174 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1175
1176 for (i = 0; i < 300; i++) {
1177 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1178 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1179 break;
1180 }
1181 udelay(100);
1182 }
1183
1184 return value;
1185}
1186
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001187static u16 rtl_get_events(struct rtl8169_private *tp)
1188{
1189 void __iomem *ioaddr = tp->mmio_addr;
1190
1191 return RTL_R16(IntrStatus);
1192}
1193
1194static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1195{
1196 void __iomem *ioaddr = tp->mmio_addr;
1197
1198 RTL_W16(IntrStatus, bits);
1199 mmiowb();
1200}
1201
1202static void rtl_irq_disable(struct rtl8169_private *tp)
1203{
1204 void __iomem *ioaddr = tp->mmio_addr;
1205
1206 RTL_W16(IntrMask, 0);
1207 mmiowb();
1208}
1209
Francois Romieu3e990ff2012-01-26 12:50:01 +01001210static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1211{
1212 void __iomem *ioaddr = tp->mmio_addr;
1213
1214 RTL_W16(IntrMask, bits);
1215}
1216
françois romieu811fd302011-12-04 20:30:45 +00001217static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
françois romieu811fd302011-12-04 20:30:45 +00001219 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001221 rtl_irq_disable(tp);
1222 rtl_ack_events(tp, tp->intr_event);
françois romieu811fd302011-12-04 20:30:45 +00001223 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
françois romieu4da19632011-01-03 15:07:55 +00001226static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
françois romieu4da19632011-01-03 15:07:55 +00001228 void __iomem *ioaddr = tp->mmio_addr;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return RTL_R32(TBICSR) & TBIReset;
1231}
1232
françois romieu4da19632011-01-03 15:07:55 +00001233static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
françois romieu4da19632011-01-03 15:07:55 +00001235 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1239{
1240 return RTL_R32(TBICSR) & TBILinkOk;
1241}
1242
1243static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1244{
1245 return RTL_R8(PHYstatus) & LinkStatus;
1246}
1247
françois romieu4da19632011-01-03 15:07:55 +00001248static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
françois romieu4da19632011-01-03 15:07:55 +00001250 void __iomem *ioaddr = tp->mmio_addr;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1253}
1254
françois romieu4da19632011-01-03 15:07:55 +00001255static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 unsigned int val;
1258
françois romieu4da19632011-01-03 15:07:55 +00001259 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1260 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Hayes Wang70090422011-07-06 15:58:06 +08001263static void rtl_link_chg_patch(struct rtl8169_private *tp)
1264{
1265 void __iomem *ioaddr = tp->mmio_addr;
1266 struct net_device *dev = tp->dev;
1267
1268 if (!netif_running(dev))
1269 return;
1270
1271 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1272 if (RTL_R8(PHYstatus) & _1000bpsF) {
1273 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1274 0x00000011, ERIAR_EXGMAC);
1275 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1276 0x00000005, ERIAR_EXGMAC);
1277 } else if (RTL_R8(PHYstatus) & _100bps) {
1278 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1279 0x0000001f, ERIAR_EXGMAC);
1280 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1281 0x00000005, ERIAR_EXGMAC);
1282 } else {
1283 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1284 0x0000001f, ERIAR_EXGMAC);
1285 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1286 0x0000003f, ERIAR_EXGMAC);
1287 }
1288 /* Reset packet filter */
1289 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1290 ERIAR_EXGMAC);
1291 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1292 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001293 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1294 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1295 if (RTL_R8(PHYstatus) & _1000bpsF) {
1296 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1297 0x00000011, ERIAR_EXGMAC);
1298 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1299 0x00000005, ERIAR_EXGMAC);
1300 } else {
1301 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1302 0x0000001f, ERIAR_EXGMAC);
1303 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1304 0x0000003f, ERIAR_EXGMAC);
1305 }
Hayes Wang70090422011-07-06 15:58:06 +08001306 }
1307}
1308
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001309static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001310 struct rtl8169_private *tp,
1311 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 unsigned long flags;
1314
1315 spin_lock_irqsave(&tp->lock, flags);
1316 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001317 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001318 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001319 if (pm)
1320 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001322 if (net_ratelimit())
1323 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001324 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001326 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001327 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001328 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 spin_unlock_irqrestore(&tp->lock, flags);
1331}
1332
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001333static void rtl8169_check_link_status(struct net_device *dev,
1334 struct rtl8169_private *tp,
1335 void __iomem *ioaddr)
1336{
1337 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1338}
1339
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001340#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1341
1342static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1343{
1344 void __iomem *ioaddr = tp->mmio_addr;
1345 u8 options;
1346 u32 wolopts = 0;
1347
1348 options = RTL_R8(Config1);
1349 if (!(options & PMEnable))
1350 return 0;
1351
1352 options = RTL_R8(Config3);
1353 if (options & LinkUp)
1354 wolopts |= WAKE_PHY;
1355 if (options & MagicPacket)
1356 wolopts |= WAKE_MAGIC;
1357
1358 options = RTL_R8(Config5);
1359 if (options & UWF)
1360 wolopts |= WAKE_UCAST;
1361 if (options & BWF)
1362 wolopts |= WAKE_BCAST;
1363 if (options & MWF)
1364 wolopts |= WAKE_MCAST;
1365
1366 return wolopts;
1367}
1368
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001369static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1370{
1371 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001372
1373 spin_lock_irq(&tp->lock);
1374
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001375 wol->supported = WAKE_ANY;
1376 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001377
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001378 spin_unlock_irq(&tp->lock);
1379}
1380
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001381static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001383 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001384 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001385 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001386 u32 opt;
1387 u16 reg;
1388 u8 mask;
1389 } cfg[] = {
1390 { WAKE_ANY, Config1, PMEnable },
1391 { WAKE_PHY, Config3, LinkUp },
1392 { WAKE_MAGIC, Config3, MagicPacket },
1393 { WAKE_UCAST, Config5, UWF },
1394 { WAKE_BCAST, Config5, BWF },
1395 { WAKE_MCAST, Config5, MWF },
1396 { WAKE_ANY, Config5, LanWake }
1397 };
1398
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399 RTL_W8(Cfg9346, Cfg9346_Unlock);
1400
1401 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1402 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001403 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404 options |= cfg[i].mask;
1405 RTL_W8(cfg[i].reg, options);
1406 }
1407
1408 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001409}
1410
1411static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1412{
1413 struct rtl8169_private *tp = netdev_priv(dev);
1414
1415 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001416
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001417 if (wol->wolopts)
1418 tp->features |= RTL_FEATURE_WOL;
1419 else
1420 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001421 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001422 spin_unlock_irq(&tp->lock);
1423
françois romieuea809072010-11-08 13:23:58 +00001424 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1425
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001426 return 0;
1427}
1428
Francois Romieu31bd2042011-04-26 18:58:59 +02001429static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1430{
Francois Romieu85bffe62011-04-27 08:22:39 +02001431 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001432}
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434static void rtl8169_get_drvinfo(struct net_device *dev,
1435 struct ethtool_drvinfo *info)
1436{
1437 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001438 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Rick Jones68aad782011-11-07 13:29:27 +00001440 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1441 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1442 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001443 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001444 if (!IS_ERR_OR_NULL(rtl_fw))
1445 strlcpy(info->fw_version, rtl_fw->version,
1446 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449static int rtl8169_get_regs_len(struct net_device *dev)
1450{
1451 return R8169_REGS_SIZE;
1452}
1453
1454static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001455 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct rtl8169_private *tp = netdev_priv(dev);
1458 void __iomem *ioaddr = tp->mmio_addr;
1459 int ret = 0;
1460 u32 reg;
1461
1462 reg = RTL_R32(TBICSR);
1463 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1464 (duplex == DUPLEX_FULL)) {
1465 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1466 } else if (autoneg == AUTONEG_ENABLE)
1467 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1468 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001469 netif_warn(tp, link, dev,
1470 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ret = -EOPNOTSUPP;
1472 }
1473
1474 return ret;
1475}
1476
1477static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001478 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001481 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001482 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Hayes Wang716b50a2011-02-22 17:26:18 +08001484 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001487 int auto_nego;
1488
françois romieu4da19632011-01-03 15:07:55 +00001489 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001490 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1491 ADVERTISE_100HALF | ADVERTISE_100FULL);
1492
1493 if (adv & ADVERTISED_10baseT_Half)
1494 auto_nego |= ADVERTISE_10HALF;
1495 if (adv & ADVERTISED_10baseT_Full)
1496 auto_nego |= ADVERTISE_10FULL;
1497 if (adv & ADVERTISED_100baseT_Half)
1498 auto_nego |= ADVERTISE_100HALF;
1499 if (adv & ADVERTISED_100baseT_Full)
1500 auto_nego |= ADVERTISE_100FULL;
1501
françois romieu3577aa12009-05-19 10:46:48 +00001502 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1503
françois romieu4da19632011-01-03 15:07:55 +00001504 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001505 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1506
1507 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001508 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001509 if (adv & ADVERTISED_1000baseT_Half)
1510 giga_ctrl |= ADVERTISE_1000HALF;
1511 if (adv & ADVERTISED_1000baseT_Full)
1512 giga_ctrl |= ADVERTISE_1000FULL;
1513 } else if (adv & (ADVERTISED_1000baseT_Half |
1514 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001515 netif_info(tp, link, dev,
1516 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001517 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
françois romieu3577aa12009-05-19 10:46:48 +00001520 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001521
françois romieu4da19632011-01-03 15:07:55 +00001522 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1523 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001524 } else {
1525 giga_ctrl = 0;
1526
1527 if (speed == SPEED_10)
1528 bmcr = 0;
1529 else if (speed == SPEED_100)
1530 bmcr = BMCR_SPEED100;
1531 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001532 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001533
1534 if (duplex == DUPLEX_FULL)
1535 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001536 }
1537
françois romieu4da19632011-01-03 15:07:55 +00001538 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001539
Francois Romieucecb5fd2011-04-01 10:21:07 +02001540 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1541 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001542 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001543 rtl_writephy(tp, 0x17, 0x2138);
1544 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001545 } else {
françois romieu4da19632011-01-03 15:07:55 +00001546 rtl_writephy(tp, 0x17, 0x2108);
1547 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001548 }
1549 }
1550
Oliver Neukum54405cd2011-01-06 21:55:13 +01001551 rc = 0;
1552out:
1553 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001557 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct rtl8169_private *tp = netdev_priv(dev);
1560 int ret;
1561
Oliver Neukum54405cd2011-01-06 21:55:13 +01001562 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001563 if (ret < 0)
1564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Francois Romieu4876cc12011-03-11 21:07:11 +01001566 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1567 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001569 }
1570out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return ret;
1572}
1573
1574static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1575{
1576 struct rtl8169_private *tp = netdev_priv(dev);
1577 unsigned long flags;
1578 int ret;
1579
Francois Romieu4876cc12011-03-11 21:07:11 +01001580 del_timer_sync(&tp->timer);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001583 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001584 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return ret;
1588}
1589
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001590static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1591 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Francois Romieud58d46b2011-05-03 16:38:29 +02001593 struct rtl8169_private *tp = netdev_priv(dev);
1594
Francois Romieu2b7b4312011-04-18 22:53:24 -07001595 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001596 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Francois Romieud58d46b2011-05-03 16:38:29 +02001598 if (dev->mtu > JUMBO_1K &&
1599 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1600 features &= ~NETIF_F_IP_CSUM;
1601
Michał Mirosław350fb322011-04-08 06:35:56 +00001602 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001605static int rtl8169_set_features(struct net_device *dev,
1606 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct rtl8169_private *tp = netdev_priv(dev);
1609 void __iomem *ioaddr = tp->mmio_addr;
1610 unsigned long flags;
1611
1612 spin_lock_irqsave(&tp->lock, flags);
1613
Michał Mirosław350fb322011-04-08 06:35:56 +00001614 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 tp->cp_cmd |= RxChkSum;
1616 else
1617 tp->cp_cmd &= ~RxChkSum;
1618
Michał Mirosław350fb322011-04-08 06:35:56 +00001619 if (dev->features & NETIF_F_HW_VLAN_RX)
1620 tp->cp_cmd |= RxVlan;
1621 else
1622 tp->cp_cmd &= ~RxVlan;
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 RTL_W16(CPlusCmd, tp->cp_cmd);
1625 RTL_R16(CPlusCmd);
1626
1627 spin_unlock_irqrestore(&tp->lock, flags);
1628
1629 return 0;
1630}
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1633 struct sk_buff *skb)
1634{
Jesse Grosseab6d182010-10-20 13:56:03 +00001635 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1637}
1638
Francois Romieu7a8fc772011-03-01 17:18:33 +01001639static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Francois Romieu7a8fc772011-03-01 17:18:33 +01001643 if (opts2 & RxVlanTag)
1644 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Francois Romieuccdffb92008-07-26 14:26:06 +02001649static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct rtl8169_private *tp = netdev_priv(dev);
1652 void __iomem *ioaddr = tp->mmio_addr;
1653 u32 status;
1654
1655 cmd->supported =
1656 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1657 cmd->port = PORT_FIBRE;
1658 cmd->transceiver = XCVR_INTERNAL;
1659
1660 status = RTL_R32(TBICSR);
1661 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1662 cmd->autoneg = !!(status & TBINwEnable);
1663
David Decotigny70739492011-04-27 18:32:40 +00001664 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001666
1667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Francois Romieuccdffb92008-07-26 14:26:06 +02001670static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Francois Romieuccdffb92008-07-26 14:26:06 +02001674 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1678{
1679 struct rtl8169_private *tp = netdev_priv(dev);
1680 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001681 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 spin_lock_irqsave(&tp->lock, flags);
1684
Francois Romieuccdffb92008-07-26 14:26:06 +02001685 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001688 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1692 void *p)
1693{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001694 struct rtl8169_private *tp = netdev_priv(dev);
1695 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Francois Romieu5b0384f2006-08-16 16:00:01 +02001697 if (regs->len > R8169_REGS_SIZE)
1698 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Francois Romieu5b0384f2006-08-16 16:00:01 +02001700 spin_lock_irqsave(&tp->lock, flags);
1701 memcpy_fromio(p, tp->mmio_addr, regs->len);
1702 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001705static u32 rtl8169_get_msglevel(struct net_device *dev)
1706{
1707 struct rtl8169_private *tp = netdev_priv(dev);
1708
1709 return tp->msg_enable;
1710}
1711
1712static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1713{
1714 struct rtl8169_private *tp = netdev_priv(dev);
1715
1716 tp->msg_enable = value;
1717}
1718
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001719static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1720 "tx_packets",
1721 "rx_packets",
1722 "tx_errors",
1723 "rx_errors",
1724 "rx_missed",
1725 "align_errors",
1726 "tx_single_collisions",
1727 "tx_multi_collisions",
1728 "unicast",
1729 "broadcast",
1730 "multicast",
1731 "tx_aborted",
1732 "tx_underrun",
1733};
1734
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001735static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001736{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001737 switch (sset) {
1738 case ETH_SS_STATS:
1739 return ARRAY_SIZE(rtl8169_gstrings);
1740 default:
1741 return -EOPNOTSUPP;
1742 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001743}
1744
Ivan Vecera355423d2009-02-06 21:49:57 -08001745static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001746{
1747 struct rtl8169_private *tp = netdev_priv(dev);
1748 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001749 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001750 struct rtl8169_counters *counters;
1751 dma_addr_t paddr;
1752 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001753 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001754
Ivan Vecera355423d2009-02-06 21:49:57 -08001755 /*
1756 * Some chips are unable to dump tally counters when the receiver
1757 * is disabled.
1758 */
1759 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1760 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001761
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001762 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001763 if (!counters)
1764 return;
1765
1766 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001767 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001768 RTL_W32(CounterAddrLow, cmd);
1769 RTL_W32(CounterAddrLow, cmd | CounterDump);
1770
Ivan Vecera355423d2009-02-06 21:49:57 -08001771 while (wait--) {
1772 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001773 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001774 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001775 }
1776 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001777 }
1778
1779 RTL_W32(CounterAddrLow, 0);
1780 RTL_W32(CounterAddrHigh, 0);
1781
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001782 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001783}
1784
Ivan Vecera355423d2009-02-06 21:49:57 -08001785static void rtl8169_get_ethtool_stats(struct net_device *dev,
1786 struct ethtool_stats *stats, u64 *data)
1787{
1788 struct rtl8169_private *tp = netdev_priv(dev);
1789
1790 ASSERT_RTNL();
1791
1792 rtl8169_update_counters(dev);
1793
1794 data[0] = le64_to_cpu(tp->counters.tx_packets);
1795 data[1] = le64_to_cpu(tp->counters.rx_packets);
1796 data[2] = le64_to_cpu(tp->counters.tx_errors);
1797 data[3] = le32_to_cpu(tp->counters.rx_errors);
1798 data[4] = le16_to_cpu(tp->counters.rx_missed);
1799 data[5] = le16_to_cpu(tp->counters.align_errors);
1800 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1801 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1802 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1803 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1804 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1805 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1806 data[12] = le16_to_cpu(tp->counters.tx_underun);
1807}
1808
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001809static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1810{
1811 switch(stringset) {
1812 case ETH_SS_STATS:
1813 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1814 break;
1815 }
1816}
1817
Jeff Garzik7282d492006-09-13 14:30:00 -04001818static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 .get_drvinfo = rtl8169_get_drvinfo,
1820 .get_regs_len = rtl8169_get_regs_len,
1821 .get_link = ethtool_op_get_link,
1822 .get_settings = rtl8169_get_settings,
1823 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001824 .get_msglevel = rtl8169_get_msglevel,
1825 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001827 .get_wol = rtl8169_get_wol,
1828 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001829 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001830 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001831 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832};
1833
Francois Romieu07d3f512007-02-21 22:40:46 +01001834static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001835 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Francois Romieu5d320a22011-05-08 17:47:36 +02001837 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001838 /*
1839 * The driver currently handles the 8168Bf and the 8168Be identically
1840 * but they can be identified more specifically through the test below
1841 * if needed:
1842 *
1843 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001844 *
1845 * Same thing for the 8101Eb and the 8101Ec:
1846 *
1847 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001848 */
Francois Romieu37441002011-06-17 22:58:54 +02001849 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001851 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 int mac_version;
1853 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001854 /* 8168F family. */
1855 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1856 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1857
hayeswang01dc7fe2011-03-21 01:50:28 +00001858 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001859 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001860 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1861 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1862 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1863
Francois Romieu5b538df2008-07-20 16:22:45 +02001864 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001865 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1866 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001867 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001868
françois romieue6de30d2011-01-03 15:08:37 +00001869 /* 8168DP family. */
1870 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1871 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001872 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001873
Francois Romieuef808d52008-06-29 13:10:54 +02001874 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001875 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001876 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001877 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001878 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001879 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1880 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001881 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001882 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001883 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001884
1885 /* 8168B family. */
1886 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1887 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1888 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1889 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1890
1891 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001892 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001893 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1894 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1895 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001896 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1897 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1898 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1899 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1900 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1901 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001902 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001903 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001904 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001905 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1906 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001907 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1908 /* FIXME: where did these entries come from ? -- FR */
1909 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1910 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1911
1912 /* 8110 family. */
1913 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1914 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1915 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1916 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1917 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1918 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1919
Jean Delvaref21b75e2009-05-26 20:54:48 -07001920 /* Catch-all */
1921 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001922 };
1923 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 u32 reg;
1925
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001926 reg = RTL_R32(TxConfig);
1927 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 p++;
1929 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001930
1931 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1932 netif_notice(tp, probe, dev,
1933 "unknown MAC, using family default\n");
1934 tp->mac_version = default_version;
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1939{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001940 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Francois Romieu867763c2007-08-17 18:21:58 +02001943struct phy_reg {
1944 u16 reg;
1945 u16 val;
1946};
1947
françois romieu4da19632011-01-03 15:07:55 +00001948static void rtl_writephy_batch(struct rtl8169_private *tp,
1949 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001950{
1951 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001952 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001953 regs++;
1954 }
1955}
1956
françois romieubca03d52011-01-03 15:07:31 +00001957#define PHY_READ 0x00000000
1958#define PHY_DATA_OR 0x10000000
1959#define PHY_DATA_AND 0x20000000
1960#define PHY_BJMPN 0x30000000
1961#define PHY_READ_EFUSE 0x40000000
1962#define PHY_READ_MAC_BYTE 0x50000000
1963#define PHY_WRITE_MAC_BYTE 0x60000000
1964#define PHY_CLEAR_READCOUNT 0x70000000
1965#define PHY_WRITE 0x80000000
1966#define PHY_READCOUNT_EQ_SKIP 0x90000000
1967#define PHY_COMP_EQ_SKIPN 0xa0000000
1968#define PHY_COMP_NEQ_SKIPN 0xb0000000
1969#define PHY_WRITE_PREVIOUS 0xc0000000
1970#define PHY_SKIPN 0xd0000000
1971#define PHY_DELAY_MS 0xe0000000
1972#define PHY_WRITE_ERI_WORD 0xf0000000
1973
Hayes Wang960aee62011-06-18 11:37:48 +02001974struct fw_info {
1975 u32 magic;
1976 char version[RTL_VER_SIZE];
1977 __le32 fw_start;
1978 __le32 fw_len;
1979 u8 chksum;
1980} __packed;
1981
Francois Romieu1c361ef2011-06-17 17:16:24 +02001982#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1983
1984static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001985{
Francois Romieub6ffd972011-06-17 17:00:05 +02001986 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001987 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001988 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1989 char *version = rtl_fw->version;
1990 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001991
Francois Romieu1c361ef2011-06-17 17:16:24 +02001992 if (fw->size < FW_OPCODE_SIZE)
1993 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001994
1995 if (!fw_info->magic) {
1996 size_t i, size, start;
1997 u8 checksum = 0;
1998
1999 if (fw->size < sizeof(*fw_info))
2000 goto out;
2001
2002 for (i = 0; i < fw->size; i++)
2003 checksum += fw->data[i];
2004 if (checksum != 0)
2005 goto out;
2006
2007 start = le32_to_cpu(fw_info->fw_start);
2008 if (start > fw->size)
2009 goto out;
2010
2011 size = le32_to_cpu(fw_info->fw_len);
2012 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2013 goto out;
2014
2015 memcpy(version, fw_info->version, RTL_VER_SIZE);
2016
2017 pa->code = (__le32 *)(fw->data + start);
2018 pa->size = size;
2019 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002020 if (fw->size % FW_OPCODE_SIZE)
2021 goto out;
2022
2023 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2024
2025 pa->code = (__le32 *)fw->data;
2026 pa->size = fw->size / FW_OPCODE_SIZE;
2027 }
2028 version[RTL_VER_SIZE - 1] = 0;
2029
2030 rc = true;
2031out:
2032 return rc;
2033}
2034
Francois Romieufd112f22011-06-18 00:10:29 +02002035static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2036 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002037{
Francois Romieufd112f22011-06-18 00:10:29 +02002038 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002039 size_t index;
2040
Francois Romieu1c361ef2011-06-17 17:16:24 +02002041 for (index = 0; index < pa->size; index++) {
2042 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002043 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002044
hayeswang42b82dc2011-01-10 02:07:25 +00002045 switch(action & 0xf0000000) {
2046 case PHY_READ:
2047 case PHY_DATA_OR:
2048 case PHY_DATA_AND:
2049 case PHY_READ_EFUSE:
2050 case PHY_CLEAR_READCOUNT:
2051 case PHY_WRITE:
2052 case PHY_WRITE_PREVIOUS:
2053 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002054 break;
2055
hayeswang42b82dc2011-01-10 02:07:25 +00002056 case PHY_BJMPN:
2057 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002058 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002059 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002060 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002061 }
2062 break;
2063 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002064 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002065 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002066 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002067 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002068 }
2069 break;
2070 case PHY_COMP_EQ_SKIPN:
2071 case PHY_COMP_NEQ_SKIPN:
2072 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002073 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002074 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002075 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002076 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002077 }
2078 break;
2079
2080 case PHY_READ_MAC_BYTE:
2081 case PHY_WRITE_MAC_BYTE:
2082 case PHY_WRITE_ERI_WORD:
2083 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002084 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002085 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002086 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002087 }
2088 }
Francois Romieufd112f22011-06-18 00:10:29 +02002089 rc = true;
2090out:
2091 return rc;
2092}
françois romieubca03d52011-01-03 15:07:31 +00002093
Francois Romieufd112f22011-06-18 00:10:29 +02002094static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2095{
2096 struct net_device *dev = tp->dev;
2097 int rc = -EINVAL;
2098
2099 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2100 netif_err(tp, ifup, dev, "invalid firwmare\n");
2101 goto out;
2102 }
2103
2104 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2105 rc = 0;
2106out:
2107 return rc;
2108}
2109
2110static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2111{
2112 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2113 u32 predata, count;
2114 size_t index;
2115
2116 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002117
Francois Romieu1c361ef2011-06-17 17:16:24 +02002118 for (index = 0; index < pa->size; ) {
2119 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002120 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002121 u32 regno = (action & 0x0fff0000) >> 16;
2122
2123 if (!action)
2124 break;
françois romieubca03d52011-01-03 15:07:31 +00002125
2126 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002127 case PHY_READ:
2128 predata = rtl_readphy(tp, regno);
2129 count++;
2130 index++;
françois romieubca03d52011-01-03 15:07:31 +00002131 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002132 case PHY_DATA_OR:
2133 predata |= data;
2134 index++;
2135 break;
2136 case PHY_DATA_AND:
2137 predata &= data;
2138 index++;
2139 break;
2140 case PHY_BJMPN:
2141 index -= regno;
2142 break;
2143 case PHY_READ_EFUSE:
2144 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2145 index++;
2146 break;
2147 case PHY_CLEAR_READCOUNT:
2148 count = 0;
2149 index++;
2150 break;
2151 case PHY_WRITE:
2152 rtl_writephy(tp, regno, data);
2153 index++;
2154 break;
2155 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002156 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002157 break;
2158 case PHY_COMP_EQ_SKIPN:
2159 if (predata == data)
2160 index += regno;
2161 index++;
2162 break;
2163 case PHY_COMP_NEQ_SKIPN:
2164 if (predata != data)
2165 index += regno;
2166 index++;
2167 break;
2168 case PHY_WRITE_PREVIOUS:
2169 rtl_writephy(tp, regno, predata);
2170 index++;
2171 break;
2172 case PHY_SKIPN:
2173 index += regno + 1;
2174 break;
2175 case PHY_DELAY_MS:
2176 mdelay(data);
2177 index++;
2178 break;
2179
2180 case PHY_READ_MAC_BYTE:
2181 case PHY_WRITE_MAC_BYTE:
2182 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002183 default:
2184 BUG();
2185 }
2186 }
2187}
2188
françois romieuf1e02ed2011-01-13 13:07:53 +00002189static void rtl_release_firmware(struct rtl8169_private *tp)
2190{
Francois Romieub6ffd972011-06-17 17:00:05 +02002191 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2192 release_firmware(tp->rtl_fw->fw);
2193 kfree(tp->rtl_fw);
2194 }
2195 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002196}
2197
François Romieu953a12c2011-04-24 17:38:48 +02002198static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002199{
Francois Romieub6ffd972011-06-17 17:00:05 +02002200 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002201
2202 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002203 if (!IS_ERR_OR_NULL(rtl_fw))
2204 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002205}
2206
2207static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2208{
2209 if (rtl_readphy(tp, reg) != val)
2210 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2211 else
2212 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002213}
2214
françois romieu4da19632011-01-03 15:07:55 +00002215static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002217 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002218 { 0x1f, 0x0001 },
2219 { 0x06, 0x006e },
2220 { 0x08, 0x0708 },
2221 { 0x15, 0x4000 },
2222 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
françois romieu0b9b5712009-08-10 19:44:56 +00002224 { 0x1f, 0x0001 },
2225 { 0x03, 0x00a1 },
2226 { 0x02, 0x0008 },
2227 { 0x01, 0x0120 },
2228 { 0x00, 0x1000 },
2229 { 0x04, 0x0800 },
2230 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
françois romieu0b9b5712009-08-10 19:44:56 +00002232 { 0x03, 0xff41 },
2233 { 0x02, 0xdf60 },
2234 { 0x01, 0x0140 },
2235 { 0x00, 0x0077 },
2236 { 0x04, 0x7800 },
2237 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
françois romieu0b9b5712009-08-10 19:44:56 +00002239 { 0x03, 0x802f },
2240 { 0x02, 0x4f02 },
2241 { 0x01, 0x0409 },
2242 { 0x00, 0xf0f9 },
2243 { 0x04, 0x9800 },
2244 { 0x04, 0x9000 },
2245
2246 { 0x03, 0xdf01 },
2247 { 0x02, 0xdf20 },
2248 { 0x01, 0xff95 },
2249 { 0x00, 0xba00 },
2250 { 0x04, 0xa800 },
2251 { 0x04, 0xa000 },
2252
2253 { 0x03, 0xff41 },
2254 { 0x02, 0xdf20 },
2255 { 0x01, 0x0140 },
2256 { 0x00, 0x00bb },
2257 { 0x04, 0xb800 },
2258 { 0x04, 0xb000 },
2259
2260 { 0x03, 0xdf41 },
2261 { 0x02, 0xdc60 },
2262 { 0x01, 0x6340 },
2263 { 0x00, 0x007d },
2264 { 0x04, 0xd800 },
2265 { 0x04, 0xd000 },
2266
2267 { 0x03, 0xdf01 },
2268 { 0x02, 0xdf20 },
2269 { 0x01, 0x100a },
2270 { 0x00, 0xa0ff },
2271 { 0x04, 0xf800 },
2272 { 0x04, 0xf000 },
2273
2274 { 0x1f, 0x0000 },
2275 { 0x0b, 0x0000 },
2276 { 0x00, 0x9200 }
2277 };
2278
françois romieu4da19632011-01-03 15:07:55 +00002279 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
françois romieu4da19632011-01-03 15:07:55 +00002282static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002283{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002284 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002285 { 0x1f, 0x0002 },
2286 { 0x01, 0x90d0 },
2287 { 0x1f, 0x0000 }
2288 };
2289
françois romieu4da19632011-01-03 15:07:55 +00002290 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002291}
2292
françois romieu4da19632011-01-03 15:07:55 +00002293static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002294{
2295 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002296
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002297 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2298 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002299 return;
2300
françois romieu4da19632011-01-03 15:07:55 +00002301 rtl_writephy(tp, 0x1f, 0x0001);
2302 rtl_writephy(tp, 0x10, 0xf01b);
2303 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002304}
2305
françois romieu4da19632011-01-03 15:07:55 +00002306static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002307{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002308 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002309 { 0x1f, 0x0001 },
2310 { 0x04, 0x0000 },
2311 { 0x03, 0x00a1 },
2312 { 0x02, 0x0008 },
2313 { 0x01, 0x0120 },
2314 { 0x00, 0x1000 },
2315 { 0x04, 0x0800 },
2316 { 0x04, 0x9000 },
2317 { 0x03, 0x802f },
2318 { 0x02, 0x4f02 },
2319 { 0x01, 0x0409 },
2320 { 0x00, 0xf099 },
2321 { 0x04, 0x9800 },
2322 { 0x04, 0xa000 },
2323 { 0x03, 0xdf01 },
2324 { 0x02, 0xdf20 },
2325 { 0x01, 0xff95 },
2326 { 0x00, 0xba00 },
2327 { 0x04, 0xa800 },
2328 { 0x04, 0xf000 },
2329 { 0x03, 0xdf01 },
2330 { 0x02, 0xdf20 },
2331 { 0x01, 0x101a },
2332 { 0x00, 0xa0ff },
2333 { 0x04, 0xf800 },
2334 { 0x04, 0x0000 },
2335 { 0x1f, 0x0000 },
2336
2337 { 0x1f, 0x0001 },
2338 { 0x10, 0xf41b },
2339 { 0x14, 0xfb54 },
2340 { 0x18, 0xf5c7 },
2341 { 0x1f, 0x0000 },
2342
2343 { 0x1f, 0x0001 },
2344 { 0x17, 0x0cc0 },
2345 { 0x1f, 0x0000 }
2346 };
2347
françois romieu4da19632011-01-03 15:07:55 +00002348 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002349
françois romieu4da19632011-01-03 15:07:55 +00002350 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002351}
2352
françois romieu4da19632011-01-03 15:07:55 +00002353static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002354{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002355 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002356 { 0x1f, 0x0001 },
2357 { 0x04, 0x0000 },
2358 { 0x03, 0x00a1 },
2359 { 0x02, 0x0008 },
2360 { 0x01, 0x0120 },
2361 { 0x00, 0x1000 },
2362 { 0x04, 0x0800 },
2363 { 0x04, 0x9000 },
2364 { 0x03, 0x802f },
2365 { 0x02, 0x4f02 },
2366 { 0x01, 0x0409 },
2367 { 0x00, 0xf099 },
2368 { 0x04, 0x9800 },
2369 { 0x04, 0xa000 },
2370 { 0x03, 0xdf01 },
2371 { 0x02, 0xdf20 },
2372 { 0x01, 0xff95 },
2373 { 0x00, 0xba00 },
2374 { 0x04, 0xa800 },
2375 { 0x04, 0xf000 },
2376 { 0x03, 0xdf01 },
2377 { 0x02, 0xdf20 },
2378 { 0x01, 0x101a },
2379 { 0x00, 0xa0ff },
2380 { 0x04, 0xf800 },
2381 { 0x04, 0x0000 },
2382 { 0x1f, 0x0000 },
2383
2384 { 0x1f, 0x0001 },
2385 { 0x0b, 0x8480 },
2386 { 0x1f, 0x0000 },
2387
2388 { 0x1f, 0x0001 },
2389 { 0x18, 0x67c7 },
2390 { 0x04, 0x2000 },
2391 { 0x03, 0x002f },
2392 { 0x02, 0x4360 },
2393 { 0x01, 0x0109 },
2394 { 0x00, 0x3022 },
2395 { 0x04, 0x2800 },
2396 { 0x1f, 0x0000 },
2397
2398 { 0x1f, 0x0001 },
2399 { 0x17, 0x0cc0 },
2400 { 0x1f, 0x0000 }
2401 };
2402
françois romieu4da19632011-01-03 15:07:55 +00002403 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002404}
2405
françois romieu4da19632011-01-03 15:07:55 +00002406static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002407{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002408 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002409 { 0x10, 0xf41b },
2410 { 0x1f, 0x0000 }
2411 };
2412
françois romieu4da19632011-01-03 15:07:55 +00002413 rtl_writephy(tp, 0x1f, 0x0001);
2414 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002415
françois romieu4da19632011-01-03 15:07:55 +00002416 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002417}
2418
françois romieu4da19632011-01-03 15:07:55 +00002419static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002420{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002421 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002422 { 0x1f, 0x0001 },
2423 { 0x10, 0xf41b },
2424 { 0x1f, 0x0000 }
2425 };
2426
françois romieu4da19632011-01-03 15:07:55 +00002427 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002428}
2429
françois romieu4da19632011-01-03 15:07:55 +00002430static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002431{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002432 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002433 { 0x1f, 0x0000 },
2434 { 0x1d, 0x0f00 },
2435 { 0x1f, 0x0002 },
2436 { 0x0c, 0x1ec8 },
2437 { 0x1f, 0x0000 }
2438 };
2439
françois romieu4da19632011-01-03 15:07:55 +00002440 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002441}
2442
françois romieu4da19632011-01-03 15:07:55 +00002443static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002444{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002445 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002446 { 0x1f, 0x0001 },
2447 { 0x1d, 0x3d98 },
2448 { 0x1f, 0x0000 }
2449 };
2450
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl_writephy(tp, 0x1f, 0x0000);
2452 rtl_patchphy(tp, 0x14, 1 << 5);
2453 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002454
françois romieu4da19632011-01-03 15:07:55 +00002455 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002456}
2457
françois romieu4da19632011-01-03 15:07:55 +00002458static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002459{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002460 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002461 { 0x1f, 0x0001 },
2462 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002463 { 0x1f, 0x0002 },
2464 { 0x00, 0x88d4 },
2465 { 0x01, 0x82b1 },
2466 { 0x03, 0x7002 },
2467 { 0x08, 0x9e30 },
2468 { 0x09, 0x01f0 },
2469 { 0x0a, 0x5500 },
2470 { 0x0c, 0x00c8 },
2471 { 0x1f, 0x0003 },
2472 { 0x12, 0xc096 },
2473 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002474 { 0x1f, 0x0000 },
2475 { 0x1f, 0x0000 },
2476 { 0x09, 0x2000 },
2477 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002478 };
2479
françois romieu4da19632011-01-03 15:07:55 +00002480 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002481
françois romieu4da19632011-01-03 15:07:55 +00002482 rtl_patchphy(tp, 0x14, 1 << 5);
2483 rtl_patchphy(tp, 0x0d, 1 << 5);
2484 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002485}
2486
françois romieu4da19632011-01-03 15:07:55 +00002487static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002488{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002489 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002490 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002491 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002492 { 0x03, 0x802f },
2493 { 0x02, 0x4f02 },
2494 { 0x01, 0x0409 },
2495 { 0x00, 0xf099 },
2496 { 0x04, 0x9800 },
2497 { 0x04, 0x9000 },
2498 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002499 { 0x1f, 0x0002 },
2500 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002501 { 0x06, 0x0761 },
2502 { 0x1f, 0x0003 },
2503 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002504 { 0x1f, 0x0000 }
2505 };
2506
françois romieu4da19632011-01-03 15:07:55 +00002507 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002508
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_patchphy(tp, 0x16, 1 << 0);
2510 rtl_patchphy(tp, 0x14, 1 << 5);
2511 rtl_patchphy(tp, 0x0d, 1 << 5);
2512 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002513}
2514
françois romieu4da19632011-01-03 15:07:55 +00002515static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002516{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002517 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002518 { 0x1f, 0x0001 },
2519 { 0x12, 0x2300 },
2520 { 0x1d, 0x3d98 },
2521 { 0x1f, 0x0002 },
2522 { 0x0c, 0x7eb8 },
2523 { 0x06, 0x5461 },
2524 { 0x1f, 0x0003 },
2525 { 0x16, 0x0f0a },
2526 { 0x1f, 0x0000 }
2527 };
2528
françois romieu4da19632011-01-03 15:07:55 +00002529 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002530
françois romieu4da19632011-01-03 15:07:55 +00002531 rtl_patchphy(tp, 0x16, 1 << 0);
2532 rtl_patchphy(tp, 0x14, 1 << 5);
2533 rtl_patchphy(tp, 0x0d, 1 << 5);
2534 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002535}
2536
françois romieu4da19632011-01-03 15:07:55 +00002537static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002538{
françois romieu4da19632011-01-03 15:07:55 +00002539 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002540}
2541
françois romieubca03d52011-01-03 15:07:31 +00002542static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002543{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002544 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002545 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002546 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002547 { 0x06, 0x4064 },
2548 { 0x07, 0x2863 },
2549 { 0x08, 0x059c },
2550 { 0x09, 0x26b4 },
2551 { 0x0a, 0x6a19 },
2552 { 0x0b, 0xdcc8 },
2553 { 0x10, 0xf06d },
2554 { 0x14, 0x7f68 },
2555 { 0x18, 0x7fd9 },
2556 { 0x1c, 0xf0ff },
2557 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002558 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002559 { 0x12, 0xf49f },
2560 { 0x13, 0x070b },
2561 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002562 { 0x14, 0x94c0 },
2563
2564 /*
2565 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002566 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002567 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002568 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002569 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002570 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002571 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002572 { 0x06, 0x5561 },
2573
2574 /*
2575 * Can not link to 1Gbps with bad cable
2576 * Decrease SNR threshold form 21.07dB to 19.04dB
2577 */
2578 { 0x1f, 0x0001 },
2579 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002580
2581 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002582 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002583 };
françois romieubca03d52011-01-03 15:07:31 +00002584 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002585
françois romieu4da19632011-01-03 15:07:55 +00002586 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002587
françois romieubca03d52011-01-03 15:07:31 +00002588 /*
2589 * Rx Error Issue
2590 * Fine Tune Switching regulator parameter
2591 */
françois romieu4da19632011-01-03 15:07:55 +00002592 rtl_writephy(tp, 0x1f, 0x0002);
2593 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2594 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002595
françois romieudaf9df62009-10-07 12:44:20 +00002596 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002597 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002598 { 0x1f, 0x0002 },
2599 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002600 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002601 { 0x05, 0x8330 },
2602 { 0x06, 0x669a },
2603 { 0x1f, 0x0002 }
2604 };
2605 int val;
2606
françois romieu4da19632011-01-03 15:07:55 +00002607 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002608
françois romieu4da19632011-01-03 15:07:55 +00002609 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002610
2611 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002612 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002613 0x0065, 0x0066, 0x0067, 0x0068,
2614 0x0069, 0x006a, 0x006b, 0x006c
2615 };
2616 int i;
2617
françois romieu4da19632011-01-03 15:07:55 +00002618 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002619
2620 val &= 0xff00;
2621 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002622 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002623 }
2624 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002625 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002626 { 0x1f, 0x0002 },
2627 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002628 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002629 { 0x05, 0x8330 },
2630 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002631 };
2632
françois romieu4da19632011-01-03 15:07:55 +00002633 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002634 }
2635
françois romieubca03d52011-01-03 15:07:31 +00002636 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_writephy(tp, 0x1f, 0x0002);
2638 rtl_patchphy(tp, 0x0d, 0x0300);
2639 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002640
françois romieubca03d52011-01-03 15:07:31 +00002641 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002642 rtl_writephy(tp, 0x1f, 0x0002);
2643 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2644 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002645
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl_writephy(tp, 0x1f, 0x0005);
2647 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002648
2649 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002650
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002652}
2653
françois romieubca03d52011-01-03 15:07:31 +00002654static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002655{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002656 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002657 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002658 { 0x1f, 0x0001 },
2659 { 0x06, 0x4064 },
2660 { 0x07, 0x2863 },
2661 { 0x08, 0x059c },
2662 { 0x09, 0x26b4 },
2663 { 0x0a, 0x6a19 },
2664 { 0x0b, 0xdcc8 },
2665 { 0x10, 0xf06d },
2666 { 0x14, 0x7f68 },
2667 { 0x18, 0x7fd9 },
2668 { 0x1c, 0xf0ff },
2669 { 0x1d, 0x3d9c },
2670 { 0x1f, 0x0003 },
2671 { 0x12, 0xf49f },
2672 { 0x13, 0x070b },
2673 { 0x1a, 0x05ad },
2674 { 0x14, 0x94c0 },
2675
françois romieubca03d52011-01-03 15:07:31 +00002676 /*
2677 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002678 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002679 */
françois romieudaf9df62009-10-07 12:44:20 +00002680 { 0x1f, 0x0002 },
2681 { 0x06, 0x5561 },
2682 { 0x1f, 0x0005 },
2683 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002684 { 0x06, 0x5561 },
2685
2686 /*
2687 * Can not link to 1Gbps with bad cable
2688 * Decrease SNR threshold form 21.07dB to 19.04dB
2689 */
2690 { 0x1f, 0x0001 },
2691 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002692
2693 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002694 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002695 };
françois romieubca03d52011-01-03 15:07:31 +00002696 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002697
françois romieu4da19632011-01-03 15:07:55 +00002698 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002699
2700 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002701 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002702 { 0x1f, 0x0002 },
2703 { 0x05, 0x669a },
2704 { 0x1f, 0x0005 },
2705 { 0x05, 0x8330 },
2706 { 0x06, 0x669a },
2707
2708 { 0x1f, 0x0002 }
2709 };
2710 int val;
2711
françois romieu4da19632011-01-03 15:07:55 +00002712 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002713
françois romieu4da19632011-01-03 15:07:55 +00002714 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002715 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002716 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002717 0x0065, 0x0066, 0x0067, 0x0068,
2718 0x0069, 0x006a, 0x006b, 0x006c
2719 };
2720 int i;
2721
françois romieu4da19632011-01-03 15:07:55 +00002722 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002723
2724 val &= 0xff00;
2725 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002726 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002727 }
2728 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002729 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002730 { 0x1f, 0x0002 },
2731 { 0x05, 0x2642 },
2732 { 0x1f, 0x0005 },
2733 { 0x05, 0x8330 },
2734 { 0x06, 0x2642 }
2735 };
2736
françois romieu4da19632011-01-03 15:07:55 +00002737 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002738 }
2739
françois romieubca03d52011-01-03 15:07:31 +00002740 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002741 rtl_writephy(tp, 0x1f, 0x0002);
2742 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2743 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002744
françois romieubca03d52011-01-03 15:07:31 +00002745 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002746 rtl_writephy(tp, 0x1f, 0x0002);
2747 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002748
françois romieu4da19632011-01-03 15:07:55 +00002749 rtl_writephy(tp, 0x1f, 0x0005);
2750 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002751
2752 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002753
françois romieu4da19632011-01-03 15:07:55 +00002754 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002755}
2756
françois romieu4da19632011-01-03 15:07:55 +00002757static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002758{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002759 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002760 { 0x1f, 0x0002 },
2761 { 0x10, 0x0008 },
2762 { 0x0d, 0x006c },
2763
2764 { 0x1f, 0x0000 },
2765 { 0x0d, 0xf880 },
2766
2767 { 0x1f, 0x0001 },
2768 { 0x17, 0x0cc0 },
2769
2770 { 0x1f, 0x0001 },
2771 { 0x0b, 0xa4d8 },
2772 { 0x09, 0x281c },
2773 { 0x07, 0x2883 },
2774 { 0x0a, 0x6b35 },
2775 { 0x1d, 0x3da4 },
2776 { 0x1c, 0xeffd },
2777 { 0x14, 0x7f52 },
2778 { 0x18, 0x7fc6 },
2779 { 0x08, 0x0601 },
2780 { 0x06, 0x4063 },
2781 { 0x10, 0xf074 },
2782 { 0x1f, 0x0003 },
2783 { 0x13, 0x0789 },
2784 { 0x12, 0xf4bd },
2785 { 0x1a, 0x04fd },
2786 { 0x14, 0x84b0 },
2787 { 0x1f, 0x0000 },
2788 { 0x00, 0x9200 },
2789
2790 { 0x1f, 0x0005 },
2791 { 0x01, 0x0340 },
2792 { 0x1f, 0x0001 },
2793 { 0x04, 0x4000 },
2794 { 0x03, 0x1d21 },
2795 { 0x02, 0x0c32 },
2796 { 0x01, 0x0200 },
2797 { 0x00, 0x5554 },
2798 { 0x04, 0x4800 },
2799 { 0x04, 0x4000 },
2800 { 0x04, 0xf000 },
2801 { 0x03, 0xdf01 },
2802 { 0x02, 0xdf20 },
2803 { 0x01, 0x101a },
2804 { 0x00, 0xa0ff },
2805 { 0x04, 0xf800 },
2806 { 0x04, 0xf000 },
2807 { 0x1f, 0x0000 },
2808
2809 { 0x1f, 0x0007 },
2810 { 0x1e, 0x0023 },
2811 { 0x16, 0x0000 },
2812 { 0x1f, 0x0000 }
2813 };
2814
françois romieu4da19632011-01-03 15:07:55 +00002815 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002816}
2817
françois romieue6de30d2011-01-03 15:08:37 +00002818static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2819{
2820 static const struct phy_reg phy_reg_init[] = {
2821 { 0x1f, 0x0001 },
2822 { 0x17, 0x0cc0 },
2823
2824 { 0x1f, 0x0007 },
2825 { 0x1e, 0x002d },
2826 { 0x18, 0x0040 },
2827 { 0x1f, 0x0000 }
2828 };
2829
2830 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2831 rtl_patchphy(tp, 0x0d, 1 << 5);
2832}
2833
Hayes Wang70090422011-07-06 15:58:06 +08002834static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002835{
2836 static const struct phy_reg phy_reg_init[] = {
2837 /* Enable Delay cap */
2838 { 0x1f, 0x0005 },
2839 { 0x05, 0x8b80 },
2840 { 0x06, 0xc896 },
2841 { 0x1f, 0x0000 },
2842
2843 /* Channel estimation fine tune */
2844 { 0x1f, 0x0001 },
2845 { 0x0b, 0x6c20 },
2846 { 0x07, 0x2872 },
2847 { 0x1c, 0xefff },
2848 { 0x1f, 0x0003 },
2849 { 0x14, 0x6420 },
2850 { 0x1f, 0x0000 },
2851
2852 /* Update PFM & 10M TX idle timer */
2853 { 0x1f, 0x0007 },
2854 { 0x1e, 0x002f },
2855 { 0x15, 0x1919 },
2856 { 0x1f, 0x0000 },
2857
2858 { 0x1f, 0x0007 },
2859 { 0x1e, 0x00ac },
2860 { 0x18, 0x0006 },
2861 { 0x1f, 0x0000 }
2862 };
2863
Francois Romieu15ecd032011-04-27 13:52:22 -07002864 rtl_apply_firmware(tp);
2865
hayeswang01dc7fe2011-03-21 01:50:28 +00002866 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2867
2868 /* DCO enable for 10M IDLE Power */
2869 rtl_writephy(tp, 0x1f, 0x0007);
2870 rtl_writephy(tp, 0x1e, 0x0023);
2871 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2872 rtl_writephy(tp, 0x1f, 0x0000);
2873
2874 /* For impedance matching */
2875 rtl_writephy(tp, 0x1f, 0x0002);
2876 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002877 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002878
2879 /* PHY auto speed down */
2880 rtl_writephy(tp, 0x1f, 0x0007);
2881 rtl_writephy(tp, 0x1e, 0x002d);
2882 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2883 rtl_writephy(tp, 0x1f, 0x0000);
2884 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2885
2886 rtl_writephy(tp, 0x1f, 0x0005);
2887 rtl_writephy(tp, 0x05, 0x8b86);
2888 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2889 rtl_writephy(tp, 0x1f, 0x0000);
2890
2891 rtl_writephy(tp, 0x1f, 0x0005);
2892 rtl_writephy(tp, 0x05, 0x8b85);
2893 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2894 rtl_writephy(tp, 0x1f, 0x0007);
2895 rtl_writephy(tp, 0x1e, 0x0020);
2896 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2897 rtl_writephy(tp, 0x1f, 0x0006);
2898 rtl_writephy(tp, 0x00, 0x5a00);
2899 rtl_writephy(tp, 0x1f, 0x0000);
2900 rtl_writephy(tp, 0x0d, 0x0007);
2901 rtl_writephy(tp, 0x0e, 0x003c);
2902 rtl_writephy(tp, 0x0d, 0x4007);
2903 rtl_writephy(tp, 0x0e, 0x0000);
2904 rtl_writephy(tp, 0x0d, 0x0000);
2905}
2906
Hayes Wang70090422011-07-06 15:58:06 +08002907static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2908{
2909 static const struct phy_reg phy_reg_init[] = {
2910 /* Enable Delay cap */
2911 { 0x1f, 0x0004 },
2912 { 0x1f, 0x0007 },
2913 { 0x1e, 0x00ac },
2914 { 0x18, 0x0006 },
2915 { 0x1f, 0x0002 },
2916 { 0x1f, 0x0000 },
2917 { 0x1f, 0x0000 },
2918
2919 /* Channel estimation fine tune */
2920 { 0x1f, 0x0003 },
2921 { 0x09, 0xa20f },
2922 { 0x1f, 0x0000 },
2923 { 0x1f, 0x0000 },
2924
2925 /* Green Setting */
2926 { 0x1f, 0x0005 },
2927 { 0x05, 0x8b5b },
2928 { 0x06, 0x9222 },
2929 { 0x05, 0x8b6d },
2930 { 0x06, 0x8000 },
2931 { 0x05, 0x8b76 },
2932 { 0x06, 0x8000 },
2933 { 0x1f, 0x0000 }
2934 };
2935
2936 rtl_apply_firmware(tp);
2937
2938 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2939
2940 /* For 4-corner performance improve */
2941 rtl_writephy(tp, 0x1f, 0x0005);
2942 rtl_writephy(tp, 0x05, 0x8b80);
2943 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945
2946 /* PHY auto speed down */
2947 rtl_writephy(tp, 0x1f, 0x0004);
2948 rtl_writephy(tp, 0x1f, 0x0007);
2949 rtl_writephy(tp, 0x1e, 0x002d);
2950 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2951 rtl_writephy(tp, 0x1f, 0x0002);
2952 rtl_writephy(tp, 0x1f, 0x0000);
2953 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2954
2955 /* improve 10M EEE waveform */
2956 rtl_writephy(tp, 0x1f, 0x0005);
2957 rtl_writephy(tp, 0x05, 0x8b86);
2958 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2959 rtl_writephy(tp, 0x1f, 0x0000);
2960
2961 /* Improve 2-pair detection performance */
2962 rtl_writephy(tp, 0x1f, 0x0005);
2963 rtl_writephy(tp, 0x05, 0x8b85);
2964 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2965 rtl_writephy(tp, 0x1f, 0x0000);
2966
2967 /* EEE setting */
2968 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2969 ERIAR_EXGMAC);
2970 rtl_writephy(tp, 0x1f, 0x0005);
2971 rtl_writephy(tp, 0x05, 0x8b85);
2972 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2973 rtl_writephy(tp, 0x1f, 0x0004);
2974 rtl_writephy(tp, 0x1f, 0x0007);
2975 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04002976 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08002977 rtl_writephy(tp, 0x1f, 0x0002);
2978 rtl_writephy(tp, 0x1f, 0x0000);
2979 rtl_writephy(tp, 0x0d, 0x0007);
2980 rtl_writephy(tp, 0x0e, 0x003c);
2981 rtl_writephy(tp, 0x0d, 0x4007);
2982 rtl_writephy(tp, 0x0e, 0x0000);
2983 rtl_writephy(tp, 0x0d, 0x0000);
2984
2985 /* Green feature */
2986 rtl_writephy(tp, 0x1f, 0x0003);
2987 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2988 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2989 rtl_writephy(tp, 0x1f, 0x0000);
2990}
2991
Hayes Wangc2218922011-09-06 16:55:18 +08002992static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2993{
2994 static const struct phy_reg phy_reg_init[] = {
2995 /* Channel estimation fine tune */
2996 { 0x1f, 0x0003 },
2997 { 0x09, 0xa20f },
2998 { 0x1f, 0x0000 },
2999
3000 /* Modify green table for giga & fnet */
3001 { 0x1f, 0x0005 },
3002 { 0x05, 0x8b55 },
3003 { 0x06, 0x0000 },
3004 { 0x05, 0x8b5e },
3005 { 0x06, 0x0000 },
3006 { 0x05, 0x8b67 },
3007 { 0x06, 0x0000 },
3008 { 0x05, 0x8b70 },
3009 { 0x06, 0x0000 },
3010 { 0x1f, 0x0000 },
3011 { 0x1f, 0x0007 },
3012 { 0x1e, 0x0078 },
3013 { 0x17, 0x0000 },
3014 { 0x19, 0x00fb },
3015 { 0x1f, 0x0000 },
3016
3017 /* Modify green table for 10M */
3018 { 0x1f, 0x0005 },
3019 { 0x05, 0x8b79 },
3020 { 0x06, 0xaa00 },
3021 { 0x1f, 0x0000 },
3022
3023 /* Disable hiimpedance detection (RTCT) */
3024 { 0x1f, 0x0003 },
3025 { 0x01, 0x328a },
3026 { 0x1f, 0x0000 }
3027 };
3028
3029 rtl_apply_firmware(tp);
3030
3031 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3032
3033 /* For 4-corner performance improve */
3034 rtl_writephy(tp, 0x1f, 0x0005);
3035 rtl_writephy(tp, 0x05, 0x8b80);
3036 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3037 rtl_writephy(tp, 0x1f, 0x0000);
3038
3039 /* PHY auto speed down */
3040 rtl_writephy(tp, 0x1f, 0x0007);
3041 rtl_writephy(tp, 0x1e, 0x002d);
3042 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3043 rtl_writephy(tp, 0x1f, 0x0000);
3044 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3045
3046 /* Improve 10M EEE waveform */
3047 rtl_writephy(tp, 0x1f, 0x0005);
3048 rtl_writephy(tp, 0x05, 0x8b86);
3049 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3050 rtl_writephy(tp, 0x1f, 0x0000);
3051
3052 /* Improve 2-pair detection performance */
3053 rtl_writephy(tp, 0x1f, 0x0005);
3054 rtl_writephy(tp, 0x05, 0x8b85);
3055 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3056 rtl_writephy(tp, 0x1f, 0x0000);
3057}
3058
3059static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3060{
3061 rtl_apply_firmware(tp);
3062
3063 /* For 4-corner performance improve */
3064 rtl_writephy(tp, 0x1f, 0x0005);
3065 rtl_writephy(tp, 0x05, 0x8b80);
3066 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3067 rtl_writephy(tp, 0x1f, 0x0000);
3068
3069 /* PHY auto speed down */
3070 rtl_writephy(tp, 0x1f, 0x0007);
3071 rtl_writephy(tp, 0x1e, 0x002d);
3072 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3073 rtl_writephy(tp, 0x1f, 0x0000);
3074 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3075
3076 /* Improve 10M EEE waveform */
3077 rtl_writephy(tp, 0x1f, 0x0005);
3078 rtl_writephy(tp, 0x05, 0x8b86);
3079 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3080 rtl_writephy(tp, 0x1f, 0x0000);
3081}
3082
françois romieu4da19632011-01-03 15:07:55 +00003083static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003084{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003085 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003086 { 0x1f, 0x0003 },
3087 { 0x08, 0x441d },
3088 { 0x01, 0x9100 },
3089 { 0x1f, 0x0000 }
3090 };
3091
françois romieu4da19632011-01-03 15:07:55 +00003092 rtl_writephy(tp, 0x1f, 0x0000);
3093 rtl_patchphy(tp, 0x11, 1 << 12);
3094 rtl_patchphy(tp, 0x19, 1 << 13);
3095 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003096
françois romieu4da19632011-01-03 15:07:55 +00003097 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003098}
3099
Hayes Wang5a5e4442011-02-22 17:26:21 +08003100static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3101{
3102 static const struct phy_reg phy_reg_init[] = {
3103 { 0x1f, 0x0005 },
3104 { 0x1a, 0x0000 },
3105 { 0x1f, 0x0000 },
3106
3107 { 0x1f, 0x0004 },
3108 { 0x1c, 0x0000 },
3109 { 0x1f, 0x0000 },
3110
3111 { 0x1f, 0x0001 },
3112 { 0x15, 0x7701 },
3113 { 0x1f, 0x0000 }
3114 };
3115
3116 /* Disable ALDPS before ram code */
3117 rtl_writephy(tp, 0x1f, 0x0000);
3118 rtl_writephy(tp, 0x18, 0x0310);
3119 msleep(100);
3120
François Romieu953a12c2011-04-24 17:38:48 +02003121 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003122
3123 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3124}
3125
Francois Romieu5615d9f2007-08-17 17:50:46 +02003126static void rtl_hw_phy_config(struct net_device *dev)
3127{
3128 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003129
3130 rtl8169_print_mac_version(tp);
3131
3132 switch (tp->mac_version) {
3133 case RTL_GIGA_MAC_VER_01:
3134 break;
3135 case RTL_GIGA_MAC_VER_02:
3136 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003137 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003138 break;
3139 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003140 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003141 break;
françois romieu2e9558562009-08-10 19:44:19 +00003142 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003143 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003144 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003145 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003146 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003147 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003148 case RTL_GIGA_MAC_VER_07:
3149 case RTL_GIGA_MAC_VER_08:
3150 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003151 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003152 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003153 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003154 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003155 break;
3156 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003157 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003158 break;
3159 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003160 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003161 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003162 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003163 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003164 break;
3165 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003166 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003167 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003168 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003169 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003170 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003171 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003172 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003173 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003174 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003175 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003176 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003177 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003178 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003179 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003180 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003181 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003182 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003183 break;
3184 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003185 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003186 break;
3187 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003188 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003189 break;
françois romieue6de30d2011-01-03 15:08:37 +00003190 case RTL_GIGA_MAC_VER_28:
3191 rtl8168d_4_hw_phy_config(tp);
3192 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003193 case RTL_GIGA_MAC_VER_29:
3194 case RTL_GIGA_MAC_VER_30:
3195 rtl8105e_hw_phy_config(tp);
3196 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003197 case RTL_GIGA_MAC_VER_31:
3198 /* None. */
3199 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003200 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003201 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003202 rtl8168e_1_hw_phy_config(tp);
3203 break;
3204 case RTL_GIGA_MAC_VER_34:
3205 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003206 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003207 case RTL_GIGA_MAC_VER_35:
3208 rtl8168f_1_hw_phy_config(tp);
3209 break;
3210 case RTL_GIGA_MAC_VER_36:
3211 rtl8168f_2_hw_phy_config(tp);
3212 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003213
Francois Romieu5615d9f2007-08-17 17:50:46 +02003214 default:
3215 break;
3216 }
3217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219static void rtl8169_phy_timer(unsigned long __opaque)
3220{
3221 struct net_device *dev = (struct net_device *)__opaque;
3222 struct rtl8169_private *tp = netdev_priv(dev);
3223 struct timer_list *timer = &tp->timer;
3224 void __iomem *ioaddr = tp->mmio_addr;
3225 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3226
Francois Romieubcf0bf92006-07-26 23:14:13 +02003227 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 spin_lock_irq(&tp->lock);
3230
françois romieu4da19632011-01-03 15:07:55 +00003231 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003232 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 * A busy loop could burn quite a few cycles on nowadays CPU.
3234 * Let's delay the execution of the timer for a few ticks.
3235 */
3236 timeout = HZ/10;
3237 goto out_mod_timer;
3238 }
3239
3240 if (tp->link_ok(ioaddr))
3241 goto out_unlock;
3242
Joe Perchesbf82c182010-02-09 11:49:50 +00003243 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
françois romieu4da19632011-01-03 15:07:55 +00003245 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247out_mod_timer:
3248 mod_timer(timer, jiffies + timeout);
3249out_unlock:
3250 spin_unlock_irq(&tp->lock);
3251}
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253#ifdef CONFIG_NET_POLL_CONTROLLER
3254/*
3255 * Polling 'interrupt' - used by things like netconsole to send skbs
3256 * without having to re-enable interrupts. It's not called while
3257 * the interrupt routine is executing.
3258 */
3259static void rtl8169_netpoll(struct net_device *dev)
3260{
3261 struct rtl8169_private *tp = netdev_priv(dev);
3262 struct pci_dev *pdev = tp->pci_dev;
3263
3264 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003265 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 enable_irq(pdev->irq);
3267}
3268#endif
3269
3270static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3271 void __iomem *ioaddr)
3272{
3273 iounmap(ioaddr);
3274 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003275 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 pci_disable_device(pdev);
3277 free_netdev(dev);
3278}
3279
Francois Romieubf793292006-11-01 00:53:05 +01003280static void rtl8169_phy_reset(struct net_device *dev,
3281 struct rtl8169_private *tp)
3282{
Francois Romieu07d3f512007-02-21 22:40:46 +01003283 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003284
françois romieu4da19632011-01-03 15:07:55 +00003285 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003286 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003287 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003288 return;
3289 msleep(1);
3290 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003291 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003292}
3293
David S. Miller8decf862011-09-22 03:23:13 -04003294static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3295{
3296 void __iomem *ioaddr = tp->mmio_addr;
3297
3298 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3299 (RTL_R8(PHYstatus) & TBI_Enable);
3300}
3301
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003302static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003304 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003305
Francois Romieu5615d9f2007-08-17 17:50:46 +02003306 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003307
Marcus Sundberg773328942008-07-10 21:28:08 +02003308 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3309 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3310 RTL_W8(0x82, 0x01);
3311 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003312
Francois Romieu6dccd162007-02-13 23:38:05 +01003313 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3314
3315 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3316 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003317
Francois Romieubcf0bf92006-07-26 23:14:13 +02003318 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003319 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3320 RTL_W8(0x82, 0x01);
3321 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003322 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003323 }
3324
Francois Romieubf793292006-11-01 00:53:05 +01003325 rtl8169_phy_reset(dev, tp);
3326
Oliver Neukum54405cd2011-01-06 21:55:13 +01003327 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003328 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3329 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3330 (tp->mii.supports_gmii ?
3331 ADVERTISED_1000baseT_Half |
3332 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003333
David S. Miller8decf862011-09-22 03:23:13 -04003334 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003335 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003336}
3337
Francois Romieu773d2022007-01-31 23:47:43 +01003338static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3339{
3340 void __iomem *ioaddr = tp->mmio_addr;
3341 u32 high;
3342 u32 low;
3343
3344 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3345 high = addr[4] | (addr[5] << 8);
3346
3347 spin_lock_irq(&tp->lock);
3348
3349 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003350
Francois Romieu773d2022007-01-31 23:47:43 +01003351 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003352 RTL_R32(MAC4);
3353
Francois Romieu78f1cd02010-03-27 19:35:46 -07003354 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003355 RTL_R32(MAC0);
3356
françois romieuc28aa382011-08-02 03:53:43 +00003357 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3358 const struct exgmac_reg e[] = {
3359 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3360 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3361 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3362 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3363 low >> 16 },
3364 };
3365
3366 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3367 }
3368
Francois Romieu773d2022007-01-31 23:47:43 +01003369 RTL_W8(Cfg9346, Cfg9346_Lock);
3370
3371 spin_unlock_irq(&tp->lock);
3372}
3373
3374static int rtl_set_mac_address(struct net_device *dev, void *p)
3375{
3376 struct rtl8169_private *tp = netdev_priv(dev);
3377 struct sockaddr *addr = p;
3378
3379 if (!is_valid_ether_addr(addr->sa_data))
3380 return -EADDRNOTAVAIL;
3381
3382 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3383
3384 rtl_rar_set(tp, dev->dev_addr);
3385
3386 return 0;
3387}
3388
Francois Romieu5f787a12006-08-17 13:02:36 +02003389static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3390{
3391 struct rtl8169_private *tp = netdev_priv(dev);
3392 struct mii_ioctl_data *data = if_mii(ifr);
3393
Francois Romieu8b4ab282008-11-19 22:05:25 -08003394 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3395}
Francois Romieu5f787a12006-08-17 13:02:36 +02003396
Francois Romieucecb5fd2011-04-01 10:21:07 +02003397static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3398 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003399{
Francois Romieu5f787a12006-08-17 13:02:36 +02003400 switch (cmd) {
3401 case SIOCGMIIPHY:
3402 data->phy_id = 32; /* Internal PHY */
3403 return 0;
3404
3405 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003406 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003407 return 0;
3408
3409 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003410 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003411 return 0;
3412 }
3413 return -EOPNOTSUPP;
3414}
3415
Francois Romieu8b4ab282008-11-19 22:05:25 -08003416static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3417{
3418 return -EOPNOTSUPP;
3419}
3420
Francois Romieu0e485152007-02-20 00:00:26 +01003421static const struct rtl_cfg_info {
3422 void (*hw_start)(struct net_device *);
3423 unsigned int region;
3424 unsigned int align;
3425 u16 intr_event;
3426 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003427 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003428 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003429} rtl_cfg_infos [] = {
3430 [RTL_CFG_0] = {
3431 .hw_start = rtl_hw_start_8169,
3432 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003433 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003434 .intr_event = SYSErr | LinkChg | RxOverflow |
3435 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003436 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003437 .features = RTL_FEATURE_GMII,
3438 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003439 },
3440 [RTL_CFG_1] = {
3441 .hw_start = rtl_hw_start_8168,
3442 .region = 2,
3443 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003444 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003445 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003446 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003447 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3448 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003449 },
3450 [RTL_CFG_2] = {
3451 .hw_start = rtl_hw_start_8101,
3452 .region = 2,
3453 .align = 8,
3454 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3455 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003456 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003457 .features = RTL_FEATURE_MSI,
3458 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003459 }
3460};
3461
Francois Romieufbac58f2007-10-04 22:51:38 +02003462/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003463static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003464 const struct rtl_cfg_info *cfg)
3465{
françois romieu2ca6cf02011-12-15 08:37:43 +00003466 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003467 unsigned msi = 0;
3468 u8 cfg2;
3469
3470 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003471 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003472 if (pci_enable_msi(tp->pci_dev)) {
3473 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003474 } else {
3475 cfg2 |= MSIEnable;
3476 msi = RTL_FEATURE_MSI;
3477 }
3478 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003479 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3480 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003481 return msi;
3482}
3483
3484static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3485{
3486 if (tp->features & RTL_FEATURE_MSI) {
3487 pci_disable_msi(pdev);
3488 tp->features &= ~RTL_FEATURE_MSI;
3489 }
3490}
3491
Francois Romieu8b4ab282008-11-19 22:05:25 -08003492static const struct net_device_ops rtl8169_netdev_ops = {
3493 .ndo_open = rtl8169_open,
3494 .ndo_stop = rtl8169_close,
3495 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003496 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003497 .ndo_tx_timeout = rtl8169_tx_timeout,
3498 .ndo_validate_addr = eth_validate_addr,
3499 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003500 .ndo_fix_features = rtl8169_fix_features,
3501 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003502 .ndo_set_mac_address = rtl_set_mac_address,
3503 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003504 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003505#ifdef CONFIG_NET_POLL_CONTROLLER
3506 .ndo_poll_controller = rtl8169_netpoll,
3507#endif
3508
3509};
3510
françois romieuc0e45c12011-01-03 15:08:04 +00003511static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3512{
3513 struct mdio_ops *ops = &tp->mdio_ops;
3514
3515 switch (tp->mac_version) {
3516 case RTL_GIGA_MAC_VER_27:
3517 ops->write = r8168dp_1_mdio_write;
3518 ops->read = r8168dp_1_mdio_read;
3519 break;
françois romieue6de30d2011-01-03 15:08:37 +00003520 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003521 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003522 ops->write = r8168dp_2_mdio_write;
3523 ops->read = r8168dp_2_mdio_read;
3524 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003525 default:
3526 ops->write = r8169_mdio_write;
3527 ops->read = r8169_mdio_read;
3528 break;
3529 }
3530}
3531
David S. Miller1805b2f2011-10-24 18:18:09 -04003532static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3533{
3534 void __iomem *ioaddr = tp->mmio_addr;
3535
3536 switch (tp->mac_version) {
3537 case RTL_GIGA_MAC_VER_29:
3538 case RTL_GIGA_MAC_VER_30:
3539 case RTL_GIGA_MAC_VER_32:
3540 case RTL_GIGA_MAC_VER_33:
3541 case RTL_GIGA_MAC_VER_34:
3542 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3543 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3544 break;
3545 default:
3546 break;
3547 }
3548}
3549
3550static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3551{
3552 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3553 return false;
3554
3555 rtl_writephy(tp, 0x1f, 0x0000);
3556 rtl_writephy(tp, MII_BMCR, 0x0000);
3557
3558 rtl_wol_suspend_quirk(tp);
3559
3560 return true;
3561}
3562
françois romieu065c27c2011-01-03 15:08:12 +00003563static void r810x_phy_power_down(struct rtl8169_private *tp)
3564{
3565 rtl_writephy(tp, 0x1f, 0x0000);
3566 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3567}
3568
3569static void r810x_phy_power_up(struct rtl8169_private *tp)
3570{
3571 rtl_writephy(tp, 0x1f, 0x0000);
3572 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3573}
3574
3575static void r810x_pll_power_down(struct rtl8169_private *tp)
3576{
David S. Miller1805b2f2011-10-24 18:18:09 -04003577 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003578 return;
françois romieu065c27c2011-01-03 15:08:12 +00003579
3580 r810x_phy_power_down(tp);
3581}
3582
3583static void r810x_pll_power_up(struct rtl8169_private *tp)
3584{
3585 r810x_phy_power_up(tp);
3586}
3587
3588static void r8168_phy_power_up(struct rtl8169_private *tp)
3589{
3590 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003591 switch (tp->mac_version) {
3592 case RTL_GIGA_MAC_VER_11:
3593 case RTL_GIGA_MAC_VER_12:
3594 case RTL_GIGA_MAC_VER_17:
3595 case RTL_GIGA_MAC_VER_18:
3596 case RTL_GIGA_MAC_VER_19:
3597 case RTL_GIGA_MAC_VER_20:
3598 case RTL_GIGA_MAC_VER_21:
3599 case RTL_GIGA_MAC_VER_22:
3600 case RTL_GIGA_MAC_VER_23:
3601 case RTL_GIGA_MAC_VER_24:
3602 case RTL_GIGA_MAC_VER_25:
3603 case RTL_GIGA_MAC_VER_26:
3604 case RTL_GIGA_MAC_VER_27:
3605 case RTL_GIGA_MAC_VER_28:
3606 case RTL_GIGA_MAC_VER_31:
3607 rtl_writephy(tp, 0x0e, 0x0000);
3608 break;
3609 default:
3610 break;
3611 }
françois romieu065c27c2011-01-03 15:08:12 +00003612 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3613}
3614
3615static void r8168_phy_power_down(struct rtl8169_private *tp)
3616{
3617 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003618 switch (tp->mac_version) {
3619 case RTL_GIGA_MAC_VER_32:
3620 case RTL_GIGA_MAC_VER_33:
3621 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3622 break;
3623
3624 case RTL_GIGA_MAC_VER_11:
3625 case RTL_GIGA_MAC_VER_12:
3626 case RTL_GIGA_MAC_VER_17:
3627 case RTL_GIGA_MAC_VER_18:
3628 case RTL_GIGA_MAC_VER_19:
3629 case RTL_GIGA_MAC_VER_20:
3630 case RTL_GIGA_MAC_VER_21:
3631 case RTL_GIGA_MAC_VER_22:
3632 case RTL_GIGA_MAC_VER_23:
3633 case RTL_GIGA_MAC_VER_24:
3634 case RTL_GIGA_MAC_VER_25:
3635 case RTL_GIGA_MAC_VER_26:
3636 case RTL_GIGA_MAC_VER_27:
3637 case RTL_GIGA_MAC_VER_28:
3638 case RTL_GIGA_MAC_VER_31:
3639 rtl_writephy(tp, 0x0e, 0x0200);
3640 default:
3641 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3642 break;
3643 }
françois romieu065c27c2011-01-03 15:08:12 +00003644}
3645
3646static void r8168_pll_power_down(struct rtl8169_private *tp)
3647{
3648 void __iomem *ioaddr = tp->mmio_addr;
3649
Francois Romieucecb5fd2011-04-01 10:21:07 +02003650 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3651 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3652 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003653 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003654 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003655 }
françois romieu065c27c2011-01-03 15:08:12 +00003656
Francois Romieucecb5fd2011-04-01 10:21:07 +02003657 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3658 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003659 (RTL_R16(CPlusCmd) & ASF)) {
3660 return;
3661 }
3662
hayeswang01dc7fe2011-03-21 01:50:28 +00003663 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3664 tp->mac_version == RTL_GIGA_MAC_VER_33)
3665 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3666
David S. Miller1805b2f2011-10-24 18:18:09 -04003667 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003668 return;
françois romieu065c27c2011-01-03 15:08:12 +00003669
3670 r8168_phy_power_down(tp);
3671
3672 switch (tp->mac_version) {
3673 case RTL_GIGA_MAC_VER_25:
3674 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003675 case RTL_GIGA_MAC_VER_27:
3676 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003677 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003678 case RTL_GIGA_MAC_VER_32:
3679 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003680 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3681 break;
3682 }
3683}
3684
3685static void r8168_pll_power_up(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
3696 switch (tp->mac_version) {
3697 case RTL_GIGA_MAC_VER_25:
3698 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003699 case RTL_GIGA_MAC_VER_27:
3700 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003701 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003702 case RTL_GIGA_MAC_VER_32:
3703 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003704 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3705 break;
3706 }
3707
3708 r8168_phy_power_up(tp);
3709}
3710
Francois Romieud58d46b2011-05-03 16:38:29 +02003711static void rtl_generic_op(struct rtl8169_private *tp,
3712 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003713{
3714 if (op)
3715 op(tp);
3716}
3717
3718static void rtl_pll_power_down(struct rtl8169_private *tp)
3719{
Francois Romieud58d46b2011-05-03 16:38:29 +02003720 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003721}
3722
3723static void rtl_pll_power_up(struct rtl8169_private *tp)
3724{
Francois Romieud58d46b2011-05-03 16:38:29 +02003725 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003726}
3727
3728static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3729{
3730 struct pll_power_ops *ops = &tp->pll_power_ops;
3731
3732 switch (tp->mac_version) {
3733 case RTL_GIGA_MAC_VER_07:
3734 case RTL_GIGA_MAC_VER_08:
3735 case RTL_GIGA_MAC_VER_09:
3736 case RTL_GIGA_MAC_VER_10:
3737 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003738 case RTL_GIGA_MAC_VER_29:
3739 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003740 ops->down = r810x_pll_power_down;
3741 ops->up = r810x_pll_power_up;
3742 break;
3743
3744 case RTL_GIGA_MAC_VER_11:
3745 case RTL_GIGA_MAC_VER_12:
3746 case RTL_GIGA_MAC_VER_17:
3747 case RTL_GIGA_MAC_VER_18:
3748 case RTL_GIGA_MAC_VER_19:
3749 case RTL_GIGA_MAC_VER_20:
3750 case RTL_GIGA_MAC_VER_21:
3751 case RTL_GIGA_MAC_VER_22:
3752 case RTL_GIGA_MAC_VER_23:
3753 case RTL_GIGA_MAC_VER_24:
3754 case RTL_GIGA_MAC_VER_25:
3755 case RTL_GIGA_MAC_VER_26:
3756 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003757 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003758 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003759 case RTL_GIGA_MAC_VER_32:
3760 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003761 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003762 case RTL_GIGA_MAC_VER_35:
3763 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003764 ops->down = r8168_pll_power_down;
3765 ops->up = r8168_pll_power_up;
3766 break;
3767
3768 default:
3769 ops->down = NULL;
3770 ops->up = NULL;
3771 break;
3772 }
3773}
3774
Hayes Wange542a222011-07-06 15:58:04 +08003775static void rtl_init_rxcfg(struct rtl8169_private *tp)
3776{
3777 void __iomem *ioaddr = tp->mmio_addr;
3778
3779 switch (tp->mac_version) {
3780 case RTL_GIGA_MAC_VER_01:
3781 case RTL_GIGA_MAC_VER_02:
3782 case RTL_GIGA_MAC_VER_03:
3783 case RTL_GIGA_MAC_VER_04:
3784 case RTL_GIGA_MAC_VER_05:
3785 case RTL_GIGA_MAC_VER_06:
3786 case RTL_GIGA_MAC_VER_10:
3787 case RTL_GIGA_MAC_VER_11:
3788 case RTL_GIGA_MAC_VER_12:
3789 case RTL_GIGA_MAC_VER_13:
3790 case RTL_GIGA_MAC_VER_14:
3791 case RTL_GIGA_MAC_VER_15:
3792 case RTL_GIGA_MAC_VER_16:
3793 case RTL_GIGA_MAC_VER_17:
3794 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3795 break;
3796 case RTL_GIGA_MAC_VER_18:
3797 case RTL_GIGA_MAC_VER_19:
3798 case RTL_GIGA_MAC_VER_20:
3799 case RTL_GIGA_MAC_VER_21:
3800 case RTL_GIGA_MAC_VER_22:
3801 case RTL_GIGA_MAC_VER_23:
3802 case RTL_GIGA_MAC_VER_24:
3803 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3804 break;
3805 default:
3806 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3807 break;
3808 }
3809}
3810
Hayes Wang92fc43b2011-07-06 15:58:03 +08003811static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3812{
3813 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3814}
3815
Francois Romieud58d46b2011-05-03 16:38:29 +02003816static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3817{
3818 rtl_generic_op(tp, tp->jumbo_ops.enable);
3819}
3820
3821static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3822{
3823 rtl_generic_op(tp, tp->jumbo_ops.disable);
3824}
3825
3826static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3827{
3828 void __iomem *ioaddr = tp->mmio_addr;
3829
3830 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3831 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3832 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3833}
3834
3835static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3836{
3837 void __iomem *ioaddr = tp->mmio_addr;
3838
3839 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3840 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3841 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3842}
3843
3844static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3845{
3846 void __iomem *ioaddr = tp->mmio_addr;
3847
3848 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3849}
3850
3851static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3852{
3853 void __iomem *ioaddr = tp->mmio_addr;
3854
3855 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3856}
3857
3858static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3859{
3860 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003861
3862 RTL_W8(MaxTxPacketSize, 0x3f);
3863 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3864 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003865 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003866}
3867
3868static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3869{
3870 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003871
3872 RTL_W8(MaxTxPacketSize, 0x0c);
3873 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3874 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003875 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003876}
3877
3878static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3879{
3880 rtl_tx_performance_tweak(tp->pci_dev,
3881 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3882}
3883
3884static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3885{
3886 rtl_tx_performance_tweak(tp->pci_dev,
3887 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3888}
3889
3890static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3891{
3892 void __iomem *ioaddr = tp->mmio_addr;
3893
3894 r8168b_0_hw_jumbo_enable(tp);
3895
3896 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3897}
3898
3899static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3900{
3901 void __iomem *ioaddr = tp->mmio_addr;
3902
3903 r8168b_0_hw_jumbo_disable(tp);
3904
3905 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3906}
3907
3908static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3909{
3910 struct jumbo_ops *ops = &tp->jumbo_ops;
3911
3912 switch (tp->mac_version) {
3913 case RTL_GIGA_MAC_VER_11:
3914 ops->disable = r8168b_0_hw_jumbo_disable;
3915 ops->enable = r8168b_0_hw_jumbo_enable;
3916 break;
3917 case RTL_GIGA_MAC_VER_12:
3918 case RTL_GIGA_MAC_VER_17:
3919 ops->disable = r8168b_1_hw_jumbo_disable;
3920 ops->enable = r8168b_1_hw_jumbo_enable;
3921 break;
3922 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3923 case RTL_GIGA_MAC_VER_19:
3924 case RTL_GIGA_MAC_VER_20:
3925 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3926 case RTL_GIGA_MAC_VER_22:
3927 case RTL_GIGA_MAC_VER_23:
3928 case RTL_GIGA_MAC_VER_24:
3929 case RTL_GIGA_MAC_VER_25:
3930 case RTL_GIGA_MAC_VER_26:
3931 ops->disable = r8168c_hw_jumbo_disable;
3932 ops->enable = r8168c_hw_jumbo_enable;
3933 break;
3934 case RTL_GIGA_MAC_VER_27:
3935 case RTL_GIGA_MAC_VER_28:
3936 ops->disable = r8168dp_hw_jumbo_disable;
3937 ops->enable = r8168dp_hw_jumbo_enable;
3938 break;
3939 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3940 case RTL_GIGA_MAC_VER_32:
3941 case RTL_GIGA_MAC_VER_33:
3942 case RTL_GIGA_MAC_VER_34:
3943 ops->disable = r8168e_hw_jumbo_disable;
3944 ops->enable = r8168e_hw_jumbo_enable;
3945 break;
3946
3947 /*
3948 * No action needed for jumbo frames with 8169.
3949 * No jumbo for 810x at all.
3950 */
3951 default:
3952 ops->disable = NULL;
3953 ops->enable = NULL;
3954 break;
3955 }
3956}
3957
Francois Romieu6f43adc2011-04-29 15:05:51 +02003958static void rtl_hw_reset(struct rtl8169_private *tp)
3959{
3960 void __iomem *ioaddr = tp->mmio_addr;
3961 int i;
3962
3963 /* Soft reset the chip. */
3964 RTL_W8(ChipCmd, CmdReset);
3965
3966 /* Check that the chip has finished the reset. */
3967 for (i = 0; i < 100; i++) {
3968 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3969 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003970 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003971 }
3972}
3973
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003974static int __devinit
3975rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3976{
Francois Romieu0e485152007-02-20 00:00:26 +01003977 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3978 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003980 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003981 struct net_device *dev;
3982 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003983 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003984 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003986 if (netif_msg_drv(&debug)) {
3987 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3988 MODULENAME, RTL8169_VERSION);
3989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003992 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003993 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003994 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003995 rc = -ENOMEM;
3996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004000 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00004002 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02004003 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004004 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Francois Romieuccdffb92008-07-26 14:26:06 +02004006 mii = &tp->mii;
4007 mii->dev = dev;
4008 mii->mdio_read = rtl_mdio_read;
4009 mii->mdio_write = rtl_mdio_write;
4010 mii->phy_id_mask = 0x1f;
4011 mii->reg_num_mask = 0x1f;
4012 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
4013
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00004014 /* disable ASPM completely as that cause random device stop working
4015 * problems as well as full system hangs for some PCIe devices users */
4016 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
4017 PCIE_LINK_STATE_CLKPM);
4018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4020 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004021 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004022 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004023 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
4025
françois romieu87aeec72010-04-26 11:42:06 +00004026 if (pci_set_mwi(pdev) < 0)
4027 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004030 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004031 netif_err(tp, probe, dev,
4032 "region #%d not an MMIO resource, aborting\n",
4033 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004035 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004039 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004040 netif_err(tp, probe, dev,
4041 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004043 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
4045
4046 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004047 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004048 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004049 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
4051
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004052 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
4054 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004055 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 tp->cp_cmd |= PCIDAC;
4057 dev->features |= NETIF_F_HIGHDMA;
4058 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004059 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004061 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004062 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
4064 }
4065
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004067 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004068 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004069 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004071 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004073 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Jon Masone44daad2011-06-27 07:46:31 +00004075 if (!pci_is_pcie(pdev))
4076 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004077
Hayes Wange542a222011-07-06 15:58:04 +08004078 /* Identify chip attached to board */
4079 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4080
4081 rtl_init_rxcfg(tp);
4082
Francois Romieu9085cdfa2012-01-26 12:59:08 +01004083 rtl_irq_disable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Francois Romieu6f43adc2011-04-29 15:05:51 +02004085 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Francois Romieu9085cdfa2012-01-26 12:59:08 +01004087 rtl_ack_events(tp, 0xffff);
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004088
françois romieuca52efd2009-07-24 12:34:19 +00004089 pci_set_master(pdev);
4090
Francois Romieu7a8fc772011-03-01 17:18:33 +01004091 /*
4092 * Pretend we are using VLANs; This bypasses a nasty bug where
4093 * Interrupts stop flowing on high load on 8110SCd controllers.
4094 */
4095 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4096 tp->cp_cmd |= RxVlan;
4097
françois romieuc0e45c12011-01-03 15:08:04 +00004098 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004099 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004100 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Francois Romieu85bffe62011-04-27 08:22:39 +02004104 chipset = tp->mac_version;
4105 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Francois Romieu5d06a992006-02-23 00:47:58 +01004107 RTL_W8(Cfg9346, Cfg9346_Unlock);
4108 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4109 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004110 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4111 tp->features |= RTL_FEATURE_WOL;
4112 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4113 tp->features |= RTL_FEATURE_WOL;
françois romieu2ca6cf02011-12-15 08:37:43 +00004114 tp->features |= rtl_try_msi(tp, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004115 RTL_W8(Cfg9346, Cfg9346_Lock);
4116
David S. Miller8decf862011-09-22 03:23:13 -04004117 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 tp->set_speed = rtl8169_set_speed_tbi;
4119 tp->get_settings = rtl8169_gset_tbi;
4120 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4121 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4122 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004123 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 } else {
4125 tp->set_speed = rtl8169_set_speed_xmii;
4126 tp->get_settings = rtl8169_gset_xmii;
4127 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4128 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4129 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004130 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
4132
Francois Romieudf58ef512008-10-09 14:35:58 -07004133 spin_lock_init(&tp->lock);
4134
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004135 /* Get MAC address */
Joe Perches6a3c9102011-11-16 09:38:02 +00004136 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004138 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4142 dev->irq = pdev->irq;
4143 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004145 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Michał Mirosław350fb322011-04-08 06:35:56 +00004147 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4148 * properly for all devices */
4149 dev->features |= NETIF_F_RXCSUM |
4150 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4151
4152 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4153 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4154 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4155 NETIF_F_HIGHDMA;
4156
4157 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4158 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4159 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01004162 tp->hw_start = cfg->hw_start;
4163 tp->intr_event = cfg->intr_event;
4164 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
David S. Miller8decf862011-09-22 03:23:13 -04004166 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4167 ~(RxBOVF | RxFOVF) : ~0;
4168
Francois Romieu2efa53f2007-03-09 00:00:05 +01004169 init_timer(&tp->timer);
4170 tp->timer.data = (unsigned long) dev;
4171 tp->timer.function = rtl8169_phy_timer;
4172
Francois Romieub6ffd972011-06-17 17:00:05 +02004173 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004176 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004177 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
4179 pci_set_drvdata(pdev, dev);
4180
Joe Perchesbf82c182010-02-09 11:49:50 +00004181 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004182 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004183 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004184 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4185 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4186 "tx checksumming: %s]\n",
4187 rtl_chip_infos[chipset].jumbo_max,
4188 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Francois Romieucecb5fd2011-04-01 10:21:07 +02004191 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4192 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4193 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004194 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004195 }
françois romieub646d902011-01-03 15:08:21 +00004196
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004197 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Alan Sternf3ec4f82010-06-08 15:23:51 -04004199 if (pci_dev_run_wake(pdev))
4200 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004201
Ivan Vecera0d672e92011-02-15 02:08:39 +00004202 netif_carrier_off(dev);
4203
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004204out:
4205 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
françois romieu87aeec72010-04-26 11:42:06 +00004207err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004208 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004209 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004210err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004211 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004212err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004213 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004214 pci_disable_device(pdev);
4215err_out_free_dev_1:
4216 free_netdev(dev);
4217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219
Francois Romieu07d3f512007-02-21 22:40:46 +01004220static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 struct net_device *dev = pci_get_drvdata(pdev);
4223 struct rtl8169_private *tp = netdev_priv(dev);
4224
Francois Romieucecb5fd2011-04-01 10:21:07 +02004225 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4226 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4227 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004228 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004229 }
françois romieub646d902011-01-03 15:08:21 +00004230
Francois Romieu4422bcd2012-01-26 11:23:32 +01004231 cancel_work_sync(&tp->wk.work);
Francois Romieueb2a0212007-02-15 23:37:21 +01004232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004234
François Romieu953a12c2011-04-24 17:38:48 +02004235 rtl_release_firmware(tp);
4236
Alan Sternf3ec4f82010-06-08 15:23:51 -04004237 if (pci_dev_run_wake(pdev))
4238 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004239
Ivan Veceracc098dc2009-11-29 23:12:52 -08004240 /* restore original MAC address */
4241 rtl_rar_set(tp, dev->perm_addr);
4242
Francois Romieufbac58f2007-10-04 22:51:38 +02004243 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4245 pci_set_drvdata(pdev, NULL);
4246}
4247
Francois Romieub6ffd972011-06-17 17:00:05 +02004248static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4249{
4250 struct rtl_fw *rtl_fw;
4251 const char *name;
4252 int rc = -ENOMEM;
4253
4254 name = rtl_lookup_firmware_name(tp);
4255 if (!name)
4256 goto out_no_firmware;
4257
4258 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4259 if (!rtl_fw)
4260 goto err_warn;
4261
4262 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4263 if (rc < 0)
4264 goto err_free;
4265
Francois Romieufd112f22011-06-18 00:10:29 +02004266 rc = rtl_check_firmware(tp, rtl_fw);
4267 if (rc < 0)
4268 goto err_release_firmware;
4269
Francois Romieub6ffd972011-06-17 17:00:05 +02004270 tp->rtl_fw = rtl_fw;
4271out:
4272 return;
4273
Francois Romieufd112f22011-06-18 00:10:29 +02004274err_release_firmware:
4275 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004276err_free:
4277 kfree(rtl_fw);
4278err_warn:
4279 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4280 name, rc);
4281out_no_firmware:
4282 tp->rtl_fw = NULL;
4283 goto out;
4284}
4285
François Romieu953a12c2011-04-24 17:38:48 +02004286static void rtl_request_firmware(struct rtl8169_private *tp)
4287{
Francois Romieub6ffd972011-06-17 17:00:05 +02004288 if (IS_ERR(tp->rtl_fw))
4289 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004290}
4291
Francois Romieu4422bcd2012-01-26 11:23:32 +01004292static void rtl_task(struct work_struct *);
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294static int rtl8169_open(struct net_device *dev)
4295{
4296 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004297 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004299 int retval = -ENOMEM;
4300
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004301 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Neil Hormanc0cd8842010-03-29 13:16:02 -07004303 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004305 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004307 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4308 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004310 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004312 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4313 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004315 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 retval = rtl8169_init_ring(dev);
4318 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004319 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Francois Romieu4422bcd2012-01-26 11:23:32 +01004321 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Francois Romieu99f252b2007-04-02 22:59:59 +02004323 smp_mb();
4324
François Romieu953a12c2011-04-24 17:38:48 +02004325 rtl_request_firmware(tp);
4326
Francois Romieufbac58f2007-10-04 22:51:38 +02004327 retval = request_irq(dev->irq, rtl8169_interrupt,
4328 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004329 dev->name, dev);
4330 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004331 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004332
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004333 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004334
françois romieueee3a962011-01-08 02:17:26 +00004335 rtl8169_init_phy(dev, tp);
4336
Michał Mirosław350fb322011-04-08 06:35:56 +00004337 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004338
françois romieu065c27c2011-01-03 15:08:12 +00004339 rtl_pll_power_up(tp);
4340
Francois Romieu07ce4062007-02-23 23:36:39 +01004341 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004343 tp->saved_wolopts = 0;
4344 pm_runtime_put_noidle(&pdev->dev);
4345
françois romieueee3a962011-01-08 02:17:26 +00004346 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347out:
4348 return retval;
4349
François Romieu953a12c2011-04-24 17:38:48 +02004350err_release_fw_2:
4351 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004352 rtl8169_rx_clear(tp);
4353err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004354 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4355 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004356 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004357err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004358 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4359 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004360 tp->TxDescArray = NULL;
4361err_pm_runtime_put:
4362 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 goto out;
4364}
4365
Hayes Wang92fc43b2011-07-06 15:58:03 +08004366static void rtl_rx_close(struct rtl8169_private *tp)
4367{
4368 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004369
Francois Romieu1687b562011-07-19 17:21:29 +02004370 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004371}
4372
françois romieue6de30d2011-01-03 15:08:37 +00004373static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
françois romieue6de30d2011-01-03 15:08:37 +00004375 void __iomem *ioaddr = tp->mmio_addr;
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004378 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Hayes Wang92fc43b2011-07-06 15:58:03 +08004380 rtl_rx_close(tp);
4381
Hayes Wang5d2e1952011-02-22 17:26:22 +08004382 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004383 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4384 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004385 while (RTL_R8(TxPoll) & NPQ)
4386 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004387 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4388 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4389 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004390 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004391 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4392 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004393 } else {
4394 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4395 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004396 }
4397
Hayes Wang92fc43b2011-07-06 15:58:03 +08004398 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Francois Romieu7f796d832007-06-11 23:04:41 +02004401static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004402{
4403 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004404
4405 /* Set DMA burst size and Interframe Gap Time */
4406 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4407 (InterFrameGap << TxInterFrameGapShift));
4408}
4409
Francois Romieu07ce4062007-02-23 23:36:39 +01004410static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
4412 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Francois Romieu07ce4062007-02-23 23:36:39 +01004414 tp->hw_start(dev);
4415
Francois Romieu3e990ff2012-01-26 12:50:01 +01004416 rtl_irq_enable(tp, tp->intr_event);
4417
Francois Romieu07ce4062007-02-23 23:36:39 +01004418 netif_start_queue(dev);
4419}
4420
Francois Romieu7f796d832007-06-11 23:04:41 +02004421static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4422 void __iomem *ioaddr)
4423{
4424 /*
4425 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4426 * register to be written before TxDescAddrLow to work.
4427 * Switching from MMIO to I/O access fixes the issue as well.
4428 */
4429 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004430 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004431 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004432 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004433}
4434
4435static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4436{
4437 u16 cmd;
4438
4439 cmd = RTL_R16(CPlusCmd);
4440 RTL_W16(CPlusCmd, cmd);
4441 return cmd;
4442}
4443
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004444static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004445{
4446 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004447 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004448}
4449
Francois Romieu6dccd162007-02-13 23:38:05 +01004450static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4451{
Francois Romieu37441002011-06-17 22:58:54 +02004452 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004453 u32 mac_version;
4454 u32 clk;
4455 u32 val;
4456 } cfg2_info [] = {
4457 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4458 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4459 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4460 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004461 };
4462 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004463 unsigned int i;
4464 u32 clk;
4465
4466 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004467 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004468 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4469 RTL_W32(0x7c, p->val);
4470 break;
4471 }
4472 }
4473}
4474
Francois Romieu07ce4062007-02-23 23:36:39 +01004475static void rtl_hw_start_8169(struct net_device *dev)
4476{
4477 struct rtl8169_private *tp = netdev_priv(dev);
4478 void __iomem *ioaddr = tp->mmio_addr;
4479 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004480
Francois Romieu9cb427b2006-11-02 00:10:16 +01004481 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4482 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4483 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4484 }
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004487 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4488 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4489 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4490 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004491 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4492
Hayes Wange542a222011-07-06 15:58:04 +08004493 rtl_init_rxcfg(tp);
4494
françois romieuf0298f82011-01-03 15:07:42 +00004495 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004497 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Francois Romieucecb5fd2011-04-01 10:21:07 +02004499 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4500 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4501 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4502 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004503 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Francois Romieu7f796d832007-06-11 23:04:41 +02004505 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004506
Francois Romieucecb5fd2011-04-01 10:21:07 +02004507 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4508 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004509 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004511 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
4513
Francois Romieubcf0bf92006-07-26 23:14:13 +02004514 RTL_W16(CPlusCmd, tp->cp_cmd);
4515
Francois Romieu6dccd162007-02-13 23:38:05 +01004516 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 /*
4519 * Undocumented corner. Supposedly:
4520 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4521 */
4522 RTL_W16(IntrMitigate, 0x0000);
4523
Francois Romieu7f796d832007-06-11 23:04:41 +02004524 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004525
Francois Romieucecb5fd2011-04-01 10:21:07 +02004526 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4527 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4528 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4529 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004530 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4531 rtl_set_rx_tx_config_registers(tp);
4532 }
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004535
4536 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4537 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539 RTL_W32(RxMissed, 0);
4540
Francois Romieu07ce4062007-02-23 23:36:39 +01004541 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
4543 /* no early-rx interrupts */
4544 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
françois romieu650e8d52011-01-03 15:08:29 +00004547static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004548{
4549 u32 csi;
4550
4551 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004552 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4553}
4554
françois romieue6de30d2011-01-03 15:08:37 +00004555static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4556{
4557 rtl_csi_access_enable(ioaddr, 0x17000000);
4558}
4559
françois romieu650e8d52011-01-03 15:08:29 +00004560static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4561{
4562 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004563}
4564
4565struct ephy_info {
4566 unsigned int offset;
4567 u16 mask;
4568 u16 bits;
4569};
4570
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004571static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004572{
4573 u16 w;
4574
4575 while (len-- > 0) {
4576 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4577 rtl_ephy_write(ioaddr, e->offset, w);
4578 e++;
4579 }
4580}
4581
Francois Romieub726e492008-06-28 12:22:59 +02004582static void rtl_disable_clock_request(struct pci_dev *pdev)
4583{
Jon Masone44daad2011-06-27 07:46:31 +00004584 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004585
4586 if (cap) {
4587 u16 ctl;
4588
4589 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4590 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4591 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4592 }
4593}
4594
françois romieue6de30d2011-01-03 15:08:37 +00004595static void rtl_enable_clock_request(struct pci_dev *pdev)
4596{
Jon Masone44daad2011-06-27 07:46:31 +00004597 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004598
4599 if (cap) {
4600 u16 ctl;
4601
4602 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4603 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4604 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4605 }
4606}
4607
Francois Romieub726e492008-06-28 12:22:59 +02004608#define R8168_CPCMD_QUIRK_MASK (\
4609 EnableBist | \
4610 Mac_dbgo_oe | \
4611 Force_half_dup | \
4612 Force_rxflow_en | \
4613 Force_txflow_en | \
4614 Cxpl_dbg_sel | \
4615 ASF | \
4616 PktCntrDisable | \
4617 Mac_dbgo_sel)
4618
Francois Romieu219a1e92008-06-28 11:58:39 +02004619static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4620{
Francois Romieub726e492008-06-28 12:22:59 +02004621 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4622
4623 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4624
Francois Romieu2e68ae42008-06-28 12:00:55 +02004625 rtl_tx_performance_tweak(pdev,
4626 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004627}
4628
4629static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4630{
4631 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004632
françois romieuf0298f82011-01-03 15:07:42 +00004633 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004634
4635 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004636}
4637
4638static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4639{
Francois Romieub726e492008-06-28 12:22:59 +02004640 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4641
4642 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4643
Francois Romieu219a1e92008-06-28 11:58:39 +02004644 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004645
4646 rtl_disable_clock_request(pdev);
4647
4648 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004649}
4650
Francois Romieuef3386f2008-06-29 12:24:30 +02004651static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004652{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004653 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004654 { 0x01, 0, 0x0001 },
4655 { 0x02, 0x0800, 0x1000 },
4656 { 0x03, 0, 0x0042 },
4657 { 0x06, 0x0080, 0x0000 },
4658 { 0x07, 0, 0x2000 }
4659 };
4660
françois romieu650e8d52011-01-03 15:08:29 +00004661 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004662
4663 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4664
Francois Romieu219a1e92008-06-28 11:58:39 +02004665 __rtl_hw_start_8168cp(ioaddr, pdev);
4666}
4667
Francois Romieuef3386f2008-06-29 12:24:30 +02004668static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4669{
françois romieu650e8d52011-01-03 15:08:29 +00004670 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004671
4672 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4673
4674 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4675
4676 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4677}
4678
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004679static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4680{
françois romieu650e8d52011-01-03 15:08:29 +00004681 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004682
4683 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4684
4685 /* Magic. */
4686 RTL_W8(DBG_REG, 0x20);
4687
françois romieuf0298f82011-01-03 15:07:42 +00004688 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004689
4690 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4691
4692 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4693}
4694
Francois Romieu219a1e92008-06-28 11:58:39 +02004695static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4696{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004697 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004698 { 0x02, 0x0800, 0x1000 },
4699 { 0x03, 0, 0x0002 },
4700 { 0x06, 0x0080, 0x0000 }
4701 };
4702
françois romieu650e8d52011-01-03 15:08:29 +00004703 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004704
4705 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4706
4707 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4708
Francois Romieu219a1e92008-06-28 11:58:39 +02004709 __rtl_hw_start_8168cp(ioaddr, pdev);
4710}
4711
4712static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4713{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004714 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004715 { 0x01, 0, 0x0001 },
4716 { 0x03, 0x0400, 0x0220 }
4717 };
4718
françois romieu650e8d52011-01-03 15:08:29 +00004719 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004720
4721 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4722
Francois Romieu219a1e92008-06-28 11:58:39 +02004723 __rtl_hw_start_8168cp(ioaddr, pdev);
4724}
4725
Francois Romieu197ff762008-06-28 13:16:02 +02004726static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4727{
4728 rtl_hw_start_8168c_2(ioaddr, pdev);
4729}
4730
Francois Romieu6fb07052008-06-29 11:54:28 +02004731static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4732{
françois romieu650e8d52011-01-03 15:08:29 +00004733 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004734
4735 __rtl_hw_start_8168cp(ioaddr, pdev);
4736}
4737
Francois Romieu5b538df2008-07-20 16:22:45 +02004738static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4739{
françois romieu650e8d52011-01-03 15:08:29 +00004740 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004741
4742 rtl_disable_clock_request(pdev);
4743
françois romieuf0298f82011-01-03 15:07:42 +00004744 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004745
4746 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4747
4748 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4749}
4750
hayeswang4804b3b2011-03-21 01:50:29 +00004751static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4752{
4753 rtl_csi_access_enable_1(ioaddr);
4754
4755 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4756
4757 RTL_W8(MaxTxPacketSize, TxPacketMax);
4758
4759 rtl_disable_clock_request(pdev);
4760}
4761
françois romieue6de30d2011-01-03 15:08:37 +00004762static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4763{
4764 static const struct ephy_info e_info_8168d_4[] = {
4765 { 0x0b, ~0, 0x48 },
4766 { 0x19, 0x20, 0x50 },
4767 { 0x0c, ~0, 0x20 }
4768 };
4769 int i;
4770
4771 rtl_csi_access_enable_1(ioaddr);
4772
4773 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4774
4775 RTL_W8(MaxTxPacketSize, TxPacketMax);
4776
4777 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4778 const struct ephy_info *e = e_info_8168d_4 + i;
4779 u16 w;
4780
4781 w = rtl_ephy_read(ioaddr, e->offset);
4782 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4783 }
4784
4785 rtl_enable_clock_request(pdev);
4786}
4787
Hayes Wang70090422011-07-06 15:58:06 +08004788static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004789{
Hayes Wang70090422011-07-06 15:58:06 +08004790 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004791 { 0x00, 0x0200, 0x0100 },
4792 { 0x00, 0x0000, 0x0004 },
4793 { 0x06, 0x0002, 0x0001 },
4794 { 0x06, 0x0000, 0x0030 },
4795 { 0x07, 0x0000, 0x2000 },
4796 { 0x00, 0x0000, 0x0020 },
4797 { 0x03, 0x5800, 0x2000 },
4798 { 0x03, 0x0000, 0x0001 },
4799 { 0x01, 0x0800, 0x1000 },
4800 { 0x07, 0x0000, 0x4000 },
4801 { 0x1e, 0x0000, 0x2000 },
4802 { 0x19, 0xffff, 0xfe6c },
4803 { 0x0a, 0x0000, 0x0040 }
4804 };
4805
4806 rtl_csi_access_enable_2(ioaddr);
4807
Hayes Wang70090422011-07-06 15:58:06 +08004808 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004809
4810 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4811
4812 RTL_W8(MaxTxPacketSize, TxPacketMax);
4813
4814 rtl_disable_clock_request(pdev);
4815
4816 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004817 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4818 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004819
Francois Romieucecb5fd2011-04-01 10:21:07 +02004820 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004821}
4822
Hayes Wang70090422011-07-06 15:58:06 +08004823static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4824{
4825 static const struct ephy_info e_info_8168e_2[] = {
4826 { 0x09, 0x0000, 0x0080 },
4827 { 0x19, 0x0000, 0x0224 }
4828 };
4829
4830 rtl_csi_access_enable_1(ioaddr);
4831
4832 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4833
4834 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4835
4836 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4837 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4838 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4839 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4840 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4841 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4842 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4843 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4844 ERIAR_EXGMAC);
4845
Hayes Wang3090bd92011-09-06 16:55:15 +08004846 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004847
4848 rtl_disable_clock_request(pdev);
4849
4850 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4851 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4852
4853 /* Adjust EEE LED frequency */
4854 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4855
4856 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4857 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4858 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4859}
4860
Hayes Wangc2218922011-09-06 16:55:18 +08004861static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4862{
4863 static const struct ephy_info e_info_8168f_1[] = {
4864 { 0x06, 0x00c0, 0x0020 },
4865 { 0x08, 0x0001, 0x0002 },
4866 { 0x09, 0x0000, 0x0080 },
4867 { 0x19, 0x0000, 0x0224 }
4868 };
4869
4870 rtl_csi_access_enable_1(ioaddr);
4871
4872 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4873
4874 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4875
4876 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4877 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4878 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4879 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4880 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4881 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4882 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4883 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4884 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4885 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4886 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4887 ERIAR_EXGMAC);
4888
4889 RTL_W8(MaxTxPacketSize, EarlySize);
4890
4891 rtl_disable_clock_request(pdev);
4892
4893 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4894 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4895
4896 /* Adjust EEE LED frequency */
4897 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4898
4899 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4900 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4901 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4902}
4903
Francois Romieu07ce4062007-02-23 23:36:39 +01004904static void rtl_hw_start_8168(struct net_device *dev)
4905{
Francois Romieu2dd99532007-06-11 23:22:52 +02004906 struct rtl8169_private *tp = netdev_priv(dev);
4907 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004908 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004909
4910 RTL_W8(Cfg9346, Cfg9346_Unlock);
4911
françois romieuf0298f82011-01-03 15:07:42 +00004912 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004913
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004914 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004915
Francois Romieu0e485152007-02-20 00:00:26 +01004916 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004917
4918 RTL_W16(CPlusCmd, tp->cp_cmd);
4919
Francois Romieu0e485152007-02-20 00:00:26 +01004920 RTL_W16(IntrMitigate, 0x5151);
4921
4922 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004923 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieu0e485152007-02-20 00:00:26 +01004924 tp->intr_event |= RxFIFOOver | PCSTimeout;
4925 tp->intr_event &= ~RxOverflow;
4926 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004927
4928 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4929
Francois Romieub8363902008-06-01 12:31:57 +02004930 rtl_set_rx_mode(dev);
4931
4932 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4933 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004934
4935 RTL_R8(IntrMask);
4936
Francois Romieu219a1e92008-06-28 11:58:39 +02004937 switch (tp->mac_version) {
4938 case RTL_GIGA_MAC_VER_11:
4939 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004940 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004941
4942 case RTL_GIGA_MAC_VER_12:
4943 case RTL_GIGA_MAC_VER_17:
4944 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004945 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004946
4947 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004948 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004949 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004950
4951 case RTL_GIGA_MAC_VER_19:
4952 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004953 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004954
4955 case RTL_GIGA_MAC_VER_20:
4956 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004957 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004958
Francois Romieu197ff762008-06-28 13:16:02 +02004959 case RTL_GIGA_MAC_VER_21:
4960 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004961 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004962
Francois Romieu6fb07052008-06-29 11:54:28 +02004963 case RTL_GIGA_MAC_VER_22:
4964 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004965 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004966
Francois Romieuef3386f2008-06-29 12:24:30 +02004967 case RTL_GIGA_MAC_VER_23:
4968 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004969 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004970
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004971 case RTL_GIGA_MAC_VER_24:
4972 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004973 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004974
Francois Romieu5b538df2008-07-20 16:22:45 +02004975 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004976 case RTL_GIGA_MAC_VER_26:
4977 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004978 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004979 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004980
françois romieue6de30d2011-01-03 15:08:37 +00004981 case RTL_GIGA_MAC_VER_28:
4982 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004983 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004984
hayeswang4804b3b2011-03-21 01:50:29 +00004985 case RTL_GIGA_MAC_VER_31:
4986 rtl_hw_start_8168dp(ioaddr, pdev);
4987 break;
4988
hayeswang01dc7fe2011-03-21 01:50:28 +00004989 case RTL_GIGA_MAC_VER_32:
4990 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004991 rtl_hw_start_8168e_1(ioaddr, pdev);
4992 break;
4993 case RTL_GIGA_MAC_VER_34:
4994 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004995 break;
françois romieue6de30d2011-01-03 15:08:37 +00004996
Hayes Wangc2218922011-09-06 16:55:18 +08004997 case RTL_GIGA_MAC_VER_35:
4998 case RTL_GIGA_MAC_VER_36:
4999 rtl_hw_start_8168f_1(ioaddr, pdev);
5000 break;
5001
Francois Romieu219a1e92008-06-28 11:58:39 +02005002 default:
5003 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
5004 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005005 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005006 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005007
Francois Romieu0e485152007-02-20 00:00:26 +01005008 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5009
Francois Romieub8363902008-06-01 12:31:57 +02005010 RTL_W8(Cfg9346, Cfg9346_Lock);
5011
Francois Romieu2dd99532007-06-11 23:22:52 +02005012 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01005013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Francois Romieu2857ffb2008-08-02 21:08:49 +02005015#define R810X_CPCMD_QUIRK_MASK (\
5016 EnableBist | \
5017 Mac_dbgo_oe | \
5018 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00005019 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02005020 Force_txflow_en | \
5021 Cxpl_dbg_sel | \
5022 ASF | \
5023 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005024 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005025
5026static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5027{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005028 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005029 { 0x01, 0, 0x6e65 },
5030 { 0x02, 0, 0x091f },
5031 { 0x03, 0, 0xc2f9 },
5032 { 0x06, 0, 0xafb5 },
5033 { 0x07, 0, 0x0e00 },
5034 { 0x19, 0, 0xec80 },
5035 { 0x01, 0, 0x2e65 },
5036 { 0x01, 0, 0x6e65 }
5037 };
5038 u8 cfg1;
5039
françois romieu650e8d52011-01-03 15:08:29 +00005040 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005041
5042 RTL_W8(DBG_REG, FIX_NAK_1);
5043
5044 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5045
5046 RTL_W8(Config1,
5047 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5048 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5049
5050 cfg1 = RTL_R8(Config1);
5051 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5052 RTL_W8(Config1, cfg1 & ~LEDS0);
5053
Francois Romieu2857ffb2008-08-02 21:08:49 +02005054 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5055}
5056
5057static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5058{
françois romieu650e8d52011-01-03 15:08:29 +00005059 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005060
5061 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5062
5063 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5064 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005065}
5066
5067static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5068{
5069 rtl_hw_start_8102e_2(ioaddr, pdev);
5070
5071 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5072}
5073
Hayes Wang5a5e4442011-02-22 17:26:21 +08005074static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5075{
5076 static const struct ephy_info e_info_8105e_1[] = {
5077 { 0x07, 0, 0x4000 },
5078 { 0x19, 0, 0x0200 },
5079 { 0x19, 0, 0x0020 },
5080 { 0x1e, 0, 0x2000 },
5081 { 0x03, 0, 0x0001 },
5082 { 0x19, 0, 0x0100 },
5083 { 0x19, 0, 0x0004 },
5084 { 0x0a, 0, 0x0020 }
5085 };
5086
Francois Romieucecb5fd2011-04-01 10:21:07 +02005087 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005088 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5089
Francois Romieucecb5fd2011-04-01 10:21:07 +02005090 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005091 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5092
5093 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005094 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005095
5096 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5097}
5098
5099static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5100{
5101 rtl_hw_start_8105e_1(ioaddr, pdev);
5102 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5103}
5104
Francois Romieu07ce4062007-02-23 23:36:39 +01005105static void rtl_hw_start_8101(struct net_device *dev)
5106{
Francois Romieucdf1a602007-06-11 23:29:50 +02005107 struct rtl8169_private *tp = netdev_priv(dev);
5108 void __iomem *ioaddr = tp->mmio_addr;
5109 struct pci_dev *pdev = tp->pci_dev;
5110
françois romieu811fd302011-12-04 20:30:45 +00005111 if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
5112 tp->intr_event &= ~RxFIFOOver;
5113 tp->napi_event &= ~RxFIFOOver;
5114 }
5115
Francois Romieucecb5fd2011-04-01 10:21:07 +02005116 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5117 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005118 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005119
5120 if (cap) {
5121 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5122 PCI_EXP_DEVCTL_NOSNOOP_EN);
5123 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005124 }
5125
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005126 RTL_W8(Cfg9346, Cfg9346_Unlock);
5127
Francois Romieu2857ffb2008-08-02 21:08:49 +02005128 switch (tp->mac_version) {
5129 case RTL_GIGA_MAC_VER_07:
5130 rtl_hw_start_8102e_1(ioaddr, pdev);
5131 break;
5132
5133 case RTL_GIGA_MAC_VER_08:
5134 rtl_hw_start_8102e_3(ioaddr, pdev);
5135 break;
5136
5137 case RTL_GIGA_MAC_VER_09:
5138 rtl_hw_start_8102e_2(ioaddr, pdev);
5139 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005140
5141 case RTL_GIGA_MAC_VER_29:
5142 rtl_hw_start_8105e_1(ioaddr, pdev);
5143 break;
5144 case RTL_GIGA_MAC_VER_30:
5145 rtl_hw_start_8105e_2(ioaddr, pdev);
5146 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005147 }
5148
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005149 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005150
françois romieuf0298f82011-01-03 15:07:42 +00005151 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005152
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005153 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005154
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005155 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005156 RTL_W16(CPlusCmd, tp->cp_cmd);
5157
5158 RTL_W16(IntrMitigate, 0x0000);
5159
5160 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5161
5162 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5163 rtl_set_rx_tx_config_registers(tp);
5164
Francois Romieucdf1a602007-06-11 23:29:50 +02005165 RTL_R8(IntrMask);
5166
Francois Romieucdf1a602007-06-11 23:29:50 +02005167 rtl_set_rx_mode(dev);
5168
5169 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170}
5171
5172static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5173{
Francois Romieud58d46b2011-05-03 16:38:29 +02005174 struct rtl8169_private *tp = netdev_priv(dev);
5175
5176 if (new_mtu < ETH_ZLEN ||
5177 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return -EINVAL;
5179
Francois Romieud58d46b2011-05-03 16:38:29 +02005180 if (new_mtu > ETH_DATA_LEN)
5181 rtl_hw_jumbo_enable(tp);
5182 else
5183 rtl_hw_jumbo_disable(tp);
5184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005186 netdev_update_features(dev);
5187
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189}
5190
5191static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5192{
Al Viro95e09182007-12-22 18:55:39 +00005193 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5195}
5196
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005197static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5198 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005200 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005201 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005202
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005203 kfree(*data_buff);
5204 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 rtl8169_make_unusable_by_asic(desc);
5206}
5207
5208static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5209{
5210 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5211
5212 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5213}
5214
5215static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5216 u32 rx_buf_sz)
5217{
5218 desc->addr = cpu_to_le64(mapping);
5219 wmb();
5220 rtl8169_mark_to_asic(desc, rx_buf_sz);
5221}
5222
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005223static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005225 return (void *)ALIGN((long)data, 16);
5226}
5227
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005228static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5229 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005230{
5231 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005233 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005234 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005235 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005237 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5238 if (!data)
5239 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005240
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005241 if (rtl8169_align(data) != data) {
5242 kfree(data);
5243 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5244 if (!data)
5245 return NULL;
5246 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005247
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005248 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005249 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005250 if (unlikely(dma_mapping_error(d, mapping))) {
5251 if (net_ratelimit())
5252 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005253 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005257 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005258
5259err_out:
5260 kfree(data);
5261 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262}
5263
5264static void rtl8169_rx_clear(struct rtl8169_private *tp)
5265{
Francois Romieu07d3f512007-02-21 22:40:46 +01005266 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005269 if (tp->Rx_databuff[i]) {
5270 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 tp->RxDescArray + i);
5272 }
5273 }
5274}
5275
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005276static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005278 desc->opts1 |= cpu_to_le32(RingEnd);
5279}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005280
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005281static int rtl8169_rx_fill(struct rtl8169_private *tp)
5282{
5283 unsigned int i;
5284
5285 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005286 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005287
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005288 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005290
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005291 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005292 if (!data) {
5293 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005294 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005295 }
5296 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005299 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5300 return 0;
5301
5302err_out:
5303 rtl8169_rx_clear(tp);
5304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305}
5306
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307static int rtl8169_init_ring(struct net_device *dev)
5308{
5309 struct rtl8169_private *tp = netdev_priv(dev);
5310
5311 rtl8169_init_ring_indexes(tp);
5312
5313 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005314 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005316 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317}
5318
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005319static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 struct TxDesc *desc)
5321{
5322 unsigned int len = tx_skb->len;
5323
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005324 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 desc->opts1 = 0x00;
5327 desc->opts2 = 0x00;
5328 desc->addr = 0x00;
5329 tx_skb->len = 0;
5330}
5331
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005332static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5333 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
5335 unsigned int i;
5336
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005337 for (i = 0; i < n; i++) {
5338 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 struct ring_info *tx_skb = tp->tx_skb + entry;
5340 unsigned int len = tx_skb->len;
5341
5342 if (len) {
5343 struct sk_buff *skb = tx_skb->skb;
5344
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005345 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 tp->TxDescArray + entry);
5347 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005348 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 dev_kfree_skb(skb);
5350 tx_skb->skb = NULL;
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
5353 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005354}
5355
5356static void rtl8169_tx_clear(struct rtl8169_private *tp)
5357{
5358 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 tp->cur_tx = tp->dirty_tx = 0;
5360}
5361
Francois Romieu4422bcd2012-01-26 11:23:32 +01005362static void rtl8169_schedule_work(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
5364 struct rtl8169_private *tp = netdev_priv(dev);
5365
Francois Romieu4422bcd2012-01-26 11:23:32 +01005366 schedule_work(&tp->wk.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367}
5368
5369static void rtl8169_wait_for_quiescence(struct net_device *dev)
5370{
5371 struct rtl8169_private *tp = netdev_priv(dev);
5372 void __iomem *ioaddr = tp->mmio_addr;
5373
5374 synchronize_irq(dev->irq);
5375
5376 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005377 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
françois romieu811fd302011-12-04 20:30:45 +00005379 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
David S. Millerd1d08d12008-01-07 20:53:33 -08005381 tp->intr_mask = 0xffff;
5382 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005383 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
Francois Romieu4422bcd2012-01-26 11:23:32 +01005386static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
David Howellsc4028952006-11-22 14:57:56 +00005388 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005389 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Francois Romieueb2a0212007-02-15 23:37:21 +01005391 rtnl_lock();
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005394 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
françois romieuc7c2c392011-12-04 20:30:52 +00005396 rtl8169_hw_reset(tp);
5397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 rtl8169_wait_for_quiescence(dev);
5399
Francois Romieu56de4142011-03-15 17:29:31 +01005400 for (i = 0; i < NUM_RX_DESC; i++)
5401 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005404 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Francois Romieu56de4142011-03-15 17:29:31 +01005406 rtl_hw_start(dev);
5407 netif_wake_queue(dev);
5408 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005409
5410out_unlock:
5411 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
5414static void rtl8169_tx_timeout(struct net_device *dev)
5415{
Francois Romieu4422bcd2012-01-26 11:23:32 +01005416 rtl8169_schedule_work(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417}
5418
5419static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005420 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 struct skb_shared_info *info = skb_shinfo(skb);
5423 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005424 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005425 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 entry = tp->cur_tx;
5428 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005429 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 dma_addr_t mapping;
5431 u32 status, len;
5432 void *addr;
5433
5434 entry = (entry + 1) % NUM_TX_DESC;
5435
5436 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005437 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005438 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005439 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005440 if (unlikely(dma_mapping_error(d, mapping))) {
5441 if (net_ratelimit())
5442 netif_err(tp, drv, tp->dev,
5443 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005444 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Francois Romieucecb5fd2011-04-01 10:21:07 +02005447 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005448 status = opts[0] | len |
5449 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005452 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 txd->addr = cpu_to_le64(mapping);
5454
5455 tp->tx_skb[entry].len = len;
5456 }
5457
5458 if (cur_frag) {
5459 tp->tx_skb[entry].skb = skb;
5460 txd->opts1 |= cpu_to_le32(LastFrag);
5461 }
5462
5463 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005464
5465err_out:
5466 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5467 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468}
5469
Francois Romieu2b7b4312011-04-18 22:53:24 -07005470static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5471 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005473 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005474 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005475 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Francois Romieu2b7b4312011-04-18 22:53:24 -07005477 if (mss) {
5478 opts[0] |= TD_LSO;
5479 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5480 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005481 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
5483 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005484 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005486 opts[offset] |= info->checksum.udp;
5487 else
5488 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490}
5491
Stephen Hemminger613573252009-08-31 19:50:58 +00005492static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5493 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494{
5495 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005496 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 struct TxDesc *txd = tp->TxDescArray + entry;
5498 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005499 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 dma_addr_t mapping;
5501 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005502 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005503 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005506 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005507 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509
5510 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005511 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005513 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005514 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005515 if (unlikely(dma_mapping_error(d, mapping))) {
5516 if (net_ratelimit())
5517 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005518 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521 tp->tx_skb[entry].len = len;
5522 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Francois Romieu2b7b4312011-04-18 22:53:24 -07005524 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5525 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005526
Francois Romieu2b7b4312011-04-18 22:53:24 -07005527 rtl8169_tso_csum(tp, skb, opts);
5528
5529 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005530 if (frags < 0)
5531 goto err_dma_1;
5532 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005533 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005534 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005535 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005536 tp->tx_skb[entry].skb = skb;
5537 }
5538
Francois Romieu2b7b4312011-04-18 22:53:24 -07005539 txd->opts2 = cpu_to_le32(opts[1]);
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 wmb();
5542
Francois Romieucecb5fd2011-04-01 10:21:07 +02005543 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005544 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 txd->opts1 = cpu_to_le32(status);
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 tp->cur_tx += frags + 1;
5548
David Dillow4c020a92010-03-03 16:33:10 +00005549 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Francois Romieucecb5fd2011-04-01 10:21:07 +02005551 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5554 netif_stop_queue(dev);
Francois Romieu1e874e02012-01-27 15:05:38 +01005555 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5557 netif_wake_queue(dev);
5558 }
5559
Stephen Hemminger613573252009-08-31 19:50:58 +00005560 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005562err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005563 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005564err_dma_0:
5565 dev_kfree_skb(skb);
5566 dev->stats.tx_dropped++;
5567 return NETDEV_TX_OK;
5568
5569err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005571 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005572 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573}
5574
5575static void rtl8169_pcierr_interrupt(struct net_device *dev)
5576{
5577 struct rtl8169_private *tp = netdev_priv(dev);
5578 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 u16 pci_status, pci_cmd;
5580
5581 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5582 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5583
Joe Perchesbf82c182010-02-09 11:49:50 +00005584 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5585 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 /*
5588 * The recovery sequence below admits a very elaborated explanation:
5589 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005590 * - I did not see what else could be done;
5591 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 *
5593 * Feel free to adjust to your needs.
5594 */
Francois Romieua27993f2006-12-18 00:04:19 +01005595 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005596 pci_cmd &= ~PCI_COMMAND_PARITY;
5597 else
5598 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5599
5600 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 pci_write_config_word(pdev, PCI_STATUS,
5603 pci_status & (PCI_STATUS_DETECTED_PARITY |
5604 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5605 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5606
5607 /* The infamous DAC f*ckup only happens at boot time */
5608 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005609 void __iomem *ioaddr = tp->mmio_addr;
5610
Joe Perchesbf82c182010-02-09 11:49:50 +00005611 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 tp->cp_cmd &= ~PCIDAC;
5613 RTL_W16(CPlusCmd, tp->cp_cmd);
5614 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
5616
françois romieue6de30d2011-01-03 15:08:37 +00005617 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005618
Francois Romieu4422bcd2012-01-26 11:23:32 +01005619 rtl8169_schedule_work(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620}
5621
Francois Romieu07d3f512007-02-21 22:40:46 +01005622static void rtl8169_tx_interrupt(struct net_device *dev,
5623 struct rtl8169_private *tp,
5624 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625{
5626 unsigned int dirty_tx, tx_left;
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 dirty_tx = tp->dirty_tx;
5629 smp_rmb();
5630 tx_left = tp->cur_tx - dirty_tx;
5631
5632 while (tx_left > 0) {
5633 unsigned int entry = dirty_tx % NUM_TX_DESC;
5634 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 u32 status;
5636
5637 rmb();
5638 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5639 if (status & DescOwn)
5640 break;
5641
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005642 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5643 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005645 dev->stats.tx_packets++;
5646 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005647 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 tx_skb->skb = NULL;
5649 }
5650 dirty_tx++;
5651 tx_left--;
5652 }
5653
5654 if (tp->dirty_tx != dirty_tx) {
5655 tp->dirty_tx = dirty_tx;
Francois Romieu1e874e02012-01-27 15:05:38 +01005656 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (netif_queue_stopped(dev) &&
5658 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5659 netif_wake_queue(dev);
5660 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005661 /*
5662 * 8168 hack: TxPoll requests are lost when the Tx packets are
5663 * too close. Let's kick an extra TxPoll request when a burst
5664 * of start_xmit activity is detected (if it is not detected,
5665 * it is slow enough). -- FR
5666 */
Francois Romieud78ae2d2007-08-26 20:08:19 +02005667 if (tp->cur_tx != dirty_tx)
5668 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
5670}
5671
Francois Romieu126fa4b2005-05-12 20:09:17 -04005672static inline int rtl8169_fragmented_frame(u32 status)
5673{
5674 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5675}
5676
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005677static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 u32 status = opts1 & RxProtoMask;
5680
5681 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005682 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 skb->ip_summed = CHECKSUM_UNNECESSARY;
5684 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005685 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686}
5687
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005688static struct sk_buff *rtl8169_try_rx_copy(void *data,
5689 struct rtl8169_private *tp,
5690 int pkt_size,
5691 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005693 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005694 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005696 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005697 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005698 prefetch(data);
5699 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5700 if (skb)
5701 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005702 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5703
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005704 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705}
5706
Francois Romieu07d3f512007-02-21 22:40:46 +01005707static int rtl8169_rx_interrupt(struct net_device *dev,
5708 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005709 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710{
5711 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005712 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 cur_rx = tp->cur_rx;
5715 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005716 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005718 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005720 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 u32 status;
5722
5723 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005724 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 if (status & DescOwn)
5727 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005728 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005729 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5730 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005731 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005733 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005735 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005736 if (status & RxFOVF) {
Francois Romieu4422bcd2012-01-26 11:23:32 +01005737 rtl8169_schedule_work(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005738 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005739 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005740 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005742 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005743 dma_addr_t addr = le64_to_cpu(desc->addr);
Francois Romieudeb9d932011-07-12 08:24:28 +02005744 int pkt_size = (status & 0x00003fff) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
Francois Romieu126fa4b2005-05-12 20:09:17 -04005746 /*
5747 * The driver does not support incoming fragmented
5748 * frames. They are seen as a symptom of over-mtu
5749 * sized frames.
5750 */
5751 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005752 dev->stats.rx_dropped++;
5753 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005754 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005755 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005756 }
5757
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005758 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5759 tp, pkt_size, addr);
5760 rtl8169_mark_to_asic(desc, rx_buf_sz);
5761 if (!skb) {
5762 dev->stats.rx_dropped++;
5763 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
5765
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005766 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 skb_put(skb, pkt_size);
5768 skb->protocol = eth_type_trans(skb, dev);
5769
Francois Romieu7a8fc772011-03-01 17:18:33 +01005770 rtl8169_rx_vlan_tag(desc, skb);
5771
Francois Romieu56de4142011-03-15 17:29:31 +01005772 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
Francois Romieucebf8cc2007-10-18 12:06:54 +02005774 dev->stats.rx_bytes += pkt_size;
5775 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005777
5778 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005779 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005780 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5781 desc->opts2 = 0;
5782 cur_rx++;
5783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 }
5785
5786 count = cur_rx - tp->cur_rx;
5787 tp->cur_rx = cur_rx;
5788
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005789 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
5791 return count;
5792}
5793
Francois Romieu07d3f512007-02-21 22:40:46 +01005794static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795{
Francois Romieu07d3f512007-02-21 22:40:46 +01005796 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 int handled = 0;
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005800 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
David Dillowf11a3772009-05-22 15:29:34 +00005802 /* loop handling interrupts until we have no new ones or
5803 * we hit a invalid/hotplug case.
5804 */
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005805 status = rtl_get_events(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005806 while (status && status != 0xffff) {
françois romieu811fd302011-12-04 20:30:45 +00005807 status &= tp->intr_event;
5808 if (!status)
5809 break;
5810
David Dillowf11a3772009-05-22 15:29:34 +00005811 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812
David Dillowf11a3772009-05-22 15:29:34 +00005813 /* Handle all of the error cases first. These will reset
5814 * the chip, so just exit the loop.
5815 */
5816 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005817 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 }
David Dillowf11a3772009-05-22 15:29:34 +00005820
Francois Romieu1519e572011-02-03 12:02:36 +01005821 if (unlikely(status & RxFIFOOver)) {
5822 switch (tp->mac_version) {
5823 /* Work around for rx fifo overflow */
5824 case RTL_GIGA_MAC_VER_11:
Francois Romieu1519e572011-02-03 12:02:36 +01005825 netif_stop_queue(dev);
5826 rtl8169_tx_timeout(dev);
5827 goto done;
Francois Romieu1519e572011-02-03 12:02:36 +01005828 default:
5829 break;
5830 }
David Dillowf11a3772009-05-22 15:29:34 +00005831 }
5832
5833 if (unlikely(status & SYSErr)) {
5834 rtl8169_pcierr_interrupt(dev);
5835 break;
5836 }
5837
5838 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005839 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005840
5841 /* We need to see the lastest version of tp->intr_mask to
5842 * avoid ignoring an MSI interrupt and having to wait for
5843 * another event which may never come.
5844 */
5845 smp_rmb();
5846 if (status & tp->intr_mask & tp->napi_event) {
5847 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5848 tp->intr_mask = ~tp->napi_event;
5849
5850 if (likely(napi_schedule_prep(&tp->napi)))
5851 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005852 else
5853 netif_info(tp, intr, dev,
5854 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005855 }
5856
5857 /* We only get a new MSI interrupt when all active irq
5858 * sources on the chip have been acknowledged. So, ack
5859 * everything we've seen and check if new sources have become
5860 * active to avoid blocking all interrupts from the chip.
5861 */
5862 RTL_W16(IntrStatus,
5863 (status & RxFIFOOver) ? (status | RxOverflow) : status);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01005864 status = rtl_get_events(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 }
Francois Romieu1519e572011-02-03 12:02:36 +01005866done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 return IRQ_RETVAL(handled);
5868}
5869
Francois Romieu4422bcd2012-01-26 11:23:32 +01005870static void rtl_task(struct work_struct *work)
5871{
5872 struct rtl8169_private *tp =
5873 container_of(work, struct rtl8169_private, wk.work);
5874
5875 rtl_reset_work(tp);
5876}
5877
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005878static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005880 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5881 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005883 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005885 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 rtl8169_tx_interrupt(dev, tp, ioaddr);
5887
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005888 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005889 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005890
5891 /* We need for force the visibility of tp->intr_mask
5892 * for other CPUs, as we can loose an MSI interrupt
5893 * and potentially wait for a retransmit timeout if we don't.
5894 * The posted write to IntrMask is safe, as it will
5895 * eventually make it to the chip and we won't loose anything
5896 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 */
David Dillowf11a3772009-05-22 15:29:34 +00005898 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005899 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005900 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 }
5902
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005903 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Francois Romieu523a6092008-09-10 22:28:56 +02005906static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5907{
5908 struct rtl8169_private *tp = netdev_priv(dev);
5909
5910 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5911 return;
5912
5913 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5914 RTL_W32(RxMissed, 0);
5915}
5916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917static void rtl8169_down(struct net_device *dev)
5918{
5919 struct rtl8169_private *tp = netdev_priv(dev);
5920 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Francois Romieu4876cc12011-03-11 21:07:11 +01005922 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 netif_stop_queue(dev);
5925
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005926 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005927
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 spin_lock_irq(&tp->lock);
5929
Hayes Wang92fc43b2011-07-06 15:58:03 +08005930 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005931 /*
5932 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005933 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5934 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005935 */
Francois Romieu523a6092008-09-10 22:28:56 +02005936 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
5938 spin_unlock_irq(&tp->lock);
5939
5940 synchronize_irq(dev->irq);
5941
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005943 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 rtl8169_tx_clear(tp);
5946
5947 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005948
5949 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950}
5951
5952static int rtl8169_close(struct net_device *dev)
5953{
5954 struct rtl8169_private *tp = netdev_priv(dev);
5955 struct pci_dev *pdev = tp->pci_dev;
5956
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005957 pm_runtime_get_sync(&pdev->dev);
5958
Francois Romieucecb5fd2011-04-01 10:21:07 +02005959 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005960 rtl8169_update_counters(dev);
5961
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 rtl8169_down(dev);
5963
5964 free_irq(dev->irq, dev);
5965
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005966 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5967 tp->RxPhyAddr);
5968 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5969 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 tp->TxDescArray = NULL;
5971 tp->RxDescArray = NULL;
5972
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005973 pm_runtime_put_sync(&pdev->dev);
5974
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 return 0;
5976}
5977
Francois Romieu07ce4062007-02-23 23:36:39 +01005978static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
5980 struct rtl8169_private *tp = netdev_priv(dev);
5981 void __iomem *ioaddr = tp->mmio_addr;
5982 unsigned long flags;
5983 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005984 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 u32 tmp = 0;
5986
5987 if (dev->flags & IFF_PROMISC) {
5988 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005989 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 rx_mode =
5991 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5992 AcceptAllPhys;
5993 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005994 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005995 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 /* Too many to filter perfectly -- accept all multicasts. */
5997 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5998 mc_filter[1] = mc_filter[0] = 0xffffffff;
5999 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00006000 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01006001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 rx_mode = AcceptBroadcast | AcceptMyPhys;
6003 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00006004 netdev_for_each_mc_addr(ha, dev) {
6005 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6007 rx_mode |= AcceptMulticast;
6008 }
6009 }
6010
6011 spin_lock_irqsave(&tp->lock, flags);
6012
Francois Romieu1687b562011-07-19 17:21:29 +02006013 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
Francois Romieuf887cce2008-07-17 22:24:18 +02006015 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01006016 u32 data = mc_filter[0];
6017
6018 mc_filter[0] = swab32(mc_filter[1]);
6019 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02006020 }
6021
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006023 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
Francois Romieu57a9f232007-06-04 22:10:15 +02006025 RTL_W32(RxConfig, tmp);
6026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 spin_unlock_irqrestore(&tp->lock, flags);
6028}
6029
6030/**
6031 * rtl8169_get_stats - Get rtl8169 read/write statistics
6032 * @dev: The Ethernet Device to get statistics for
6033 *
6034 * Get TX/RX statistics for rtl8169
6035 */
6036static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6037{
6038 struct rtl8169_private *tp = netdev_priv(dev);
6039 void __iomem *ioaddr = tp->mmio_addr;
6040 unsigned long flags;
6041
6042 if (netif_running(dev)) {
6043 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02006044 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 spin_unlock_irqrestore(&tp->lock, flags);
6046 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02006047
Francois Romieucebf8cc2007-10-18 12:06:54 +02006048 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049}
6050
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006051static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006052{
françois romieu065c27c2011-01-03 15:08:12 +00006053 struct rtl8169_private *tp = netdev_priv(dev);
6054
Francois Romieu5d06a992006-02-23 00:47:58 +01006055 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006056 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006057
françois romieu065c27c2011-01-03 15:08:12 +00006058 rtl_pll_power_down(tp);
6059
Francois Romieu5d06a992006-02-23 00:47:58 +01006060 netif_device_detach(dev);
6061 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006062}
Francois Romieu5d06a992006-02-23 00:47:58 +01006063
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006064#ifdef CONFIG_PM
6065
6066static int rtl8169_suspend(struct device *device)
6067{
6068 struct pci_dev *pdev = to_pci_dev(device);
6069 struct net_device *dev = pci_get_drvdata(pdev);
6070
6071 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006072
Francois Romieu5d06a992006-02-23 00:47:58 +01006073 return 0;
6074}
6075
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006076static void __rtl8169_resume(struct net_device *dev)
6077{
françois romieu065c27c2011-01-03 15:08:12 +00006078 struct rtl8169_private *tp = netdev_priv(dev);
6079
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006080 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006081
6082 rtl_pll_power_up(tp);
6083
Francois Romieu4422bcd2012-01-26 11:23:32 +01006084 rtl8169_schedule_work(dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006085}
6086
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006087static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006088{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006089 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006090 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006091 struct rtl8169_private *tp = netdev_priv(dev);
6092
6093 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006094
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006095 if (netif_running(dev))
6096 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006097
Francois Romieu5d06a992006-02-23 00:47:58 +01006098 return 0;
6099}
6100
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006101static int rtl8169_runtime_suspend(struct device *device)
6102{
6103 struct pci_dev *pdev = to_pci_dev(device);
6104 struct net_device *dev = pci_get_drvdata(pdev);
6105 struct rtl8169_private *tp = netdev_priv(dev);
6106
6107 if (!tp->TxDescArray)
6108 return 0;
6109
6110 spin_lock_irq(&tp->lock);
6111 tp->saved_wolopts = __rtl8169_get_wol(tp);
6112 __rtl8169_set_wol(tp, WAKE_ANY);
6113 spin_unlock_irq(&tp->lock);
6114
6115 rtl8169_net_suspend(dev);
6116
6117 return 0;
6118}
6119
6120static int rtl8169_runtime_resume(struct device *device)
6121{
6122 struct pci_dev *pdev = to_pci_dev(device);
6123 struct net_device *dev = pci_get_drvdata(pdev);
6124 struct rtl8169_private *tp = netdev_priv(dev);
6125
6126 if (!tp->TxDescArray)
6127 return 0;
6128
6129 spin_lock_irq(&tp->lock);
6130 __rtl8169_set_wol(tp, tp->saved_wolopts);
6131 tp->saved_wolopts = 0;
6132 spin_unlock_irq(&tp->lock);
6133
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006134 rtl8169_init_phy(dev, tp);
6135
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006136 __rtl8169_resume(dev);
6137
6138 return 0;
6139}
6140
6141static int rtl8169_runtime_idle(struct device *device)
6142{
6143 struct pci_dev *pdev = to_pci_dev(device);
6144 struct net_device *dev = pci_get_drvdata(pdev);
6145 struct rtl8169_private *tp = netdev_priv(dev);
6146
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006147 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006148}
6149
Alexey Dobriyan47145212009-12-14 18:00:08 -08006150static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006151 .suspend = rtl8169_suspend,
6152 .resume = rtl8169_resume,
6153 .freeze = rtl8169_suspend,
6154 .thaw = rtl8169_resume,
6155 .poweroff = rtl8169_suspend,
6156 .restore = rtl8169_resume,
6157 .runtime_suspend = rtl8169_runtime_suspend,
6158 .runtime_resume = rtl8169_runtime_resume,
6159 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006160};
6161
6162#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6163
6164#else /* !CONFIG_PM */
6165
6166#define RTL8169_PM_OPS NULL
6167
6168#endif /* !CONFIG_PM */
6169
David S. Miller1805b2f2011-10-24 18:18:09 -04006170static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6171{
6172 void __iomem *ioaddr = tp->mmio_addr;
6173
6174 /* WoL fails with 8168b when the receiver is disabled. */
6175 switch (tp->mac_version) {
6176 case RTL_GIGA_MAC_VER_11:
6177 case RTL_GIGA_MAC_VER_12:
6178 case RTL_GIGA_MAC_VER_17:
6179 pci_clear_master(tp->pci_dev);
6180
6181 RTL_W8(ChipCmd, CmdRxEnb);
6182 /* PCI commit */
6183 RTL_R8(ChipCmd);
6184 break;
6185 default:
6186 break;
6187 }
6188}
6189
Francois Romieu1765f952008-09-13 17:21:40 +02006190static void rtl_shutdown(struct pci_dev *pdev)
6191{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006192 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006193 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006194
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006195 rtl8169_net_suspend(dev);
6196
Francois Romieucecb5fd2011-04-01 10:21:07 +02006197 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006198 rtl_rar_set(tp, dev->perm_addr);
6199
françois romieu4bb3f522009-06-17 11:41:45 +00006200 spin_lock_irq(&tp->lock);
6201
Hayes Wang92fc43b2011-07-06 15:58:03 +08006202 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006203
6204 spin_unlock_irq(&tp->lock);
6205
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006206 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006207 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6208 rtl_wol_suspend_quirk(tp);
6209 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006210 }
6211
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006212 pci_wake_from_d3(pdev, true);
6213 pci_set_power_state(pdev, PCI_D3hot);
6214 }
6215}
Francois Romieu5d06a992006-02-23 00:47:58 +01006216
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217static struct pci_driver rtl8169_pci_driver = {
6218 .name = MODULENAME,
6219 .id_table = rtl8169_pci_tbl,
6220 .probe = rtl8169_init_one,
6221 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006222 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006223 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224};
6225
Francois Romieu07d3f512007-02-21 22:40:46 +01006226static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227{
Jeff Garzik29917622006-08-19 17:48:59 -04006228 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229}
6230
Francois Romieu07d3f512007-02-21 22:40:46 +01006231static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232{
6233 pci_unregister_driver(&rtl8169_pci_driver);
6234}
6235
6236module_init(rtl8169_init_module);
6237module_exit(rtl8169_cleanup_module);