blob: f7bc310f5185126a59e581775fb922b64f5129dc [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 Romieuf0e837d92005-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 Romieufbac58f2007-10-04 22:51:38 +0200477 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200478 Speed_down = (1 << 4),
479 MEMMAP = (1 << 3),
480 IOMAP = (1 << 2),
481 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100482 PMEnable = (1 << 0), /* Power Management Enable */
483
Francois Romieu6dccd162007-02-13 23:38:05 +0100484 /* Config2 register p. 25 */
485 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);
David Howellsc4028952006-11-22 14:57:56 +0000717 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200718 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200719
720 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800721 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000722 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400723 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000724
Francois Romieub6ffd972011-06-17 17:00:05 +0200725 struct rtl_fw {
726 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200727
728#define RTL_VER_SIZE 32
729
730 char version[RTL_VER_SIZE];
731
732 struct rtl_fw_phy_action {
733 __le32 *code;
734 size_t size;
735 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200736 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300737#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738};
739
Ralf Baechle979b6c12005-06-13 14:30:40 -0700740MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700743MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200744module_param_named(debug, debug.msg_enable, int, 0);
745MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746MODULE_LICENSE("GPL");
747MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000748MODULE_FIRMWARE(FIRMWARE_8168D_1);
749MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000750MODULE_FIRMWARE(FIRMWARE_8168E_1);
751MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400752MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800753MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800754MODULE_FIRMWARE(FIRMWARE_8168F_1);
755MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000758static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
759 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100760static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100762static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100764static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200766static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700768 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200769static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200771static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700772static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Francois Romieud58d46b2011-05-03 16:38:29 +0200774static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
775{
776 int cap = pci_pcie_cap(pdev);
777
778 if (cap) {
779 u16 ctl;
780
781 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
782 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
783 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
784 }
785}
786
françois romieub646d902011-01-03 15:08:21 +0000787static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
788{
789 void __iomem *ioaddr = tp->mmio_addr;
790 int i;
791
792 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
793 for (i = 0; i < 20; i++) {
794 udelay(100);
795 if (RTL_R32(OCPAR) & OCPAR_FLAG)
796 break;
797 }
798 return RTL_R32(OCPDR);
799}
800
801static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
802{
803 void __iomem *ioaddr = tp->mmio_addr;
804 int i;
805
806 RTL_W32(OCPDR, data);
807 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
808 for (i = 0; i < 20; i++) {
809 udelay(100);
810 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
811 break;
812 }
813}
814
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800815static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000816{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800817 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000818 int i;
819
820 RTL_W8(ERIDR, cmd);
821 RTL_W32(ERIAR, 0x800010e8);
822 msleep(2);
823 for (i = 0; i < 5; i++) {
824 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200825 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000826 break;
827 }
828
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800829 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000830}
831
832#define OOB_CMD_RESET 0x00
833#define OOB_CMD_DRIVER_START 0x05
834#define OOB_CMD_DRIVER_STOP 0x06
835
Francois Romieucecb5fd2011-04-01 10:21:07 +0200836static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
837{
838 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
839}
840
françois romieub646d902011-01-03 15:08:21 +0000841static void rtl8168_driver_start(struct rtl8169_private *tp)
842{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200843 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000844 int i;
845
846 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
847
Francois Romieucecb5fd2011-04-01 10:21:07 +0200848 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000849
françois romieub646d902011-01-03 15:08:21 +0000850 for (i = 0; i < 10; i++) {
851 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000852 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000853 break;
854 }
855}
856
857static void rtl8168_driver_stop(struct rtl8169_private *tp)
858{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200859 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000860 int i;
861
862 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
863
Francois Romieucecb5fd2011-04-01 10:21:07 +0200864 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000865
françois romieub646d902011-01-03 15:08:21 +0000866 for (i = 0; i < 10; i++) {
867 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000868 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000869 break;
870 }
871}
872
hayeswang4804b3b2011-03-21 01:50:29 +0000873static int r8168dp_check_dash(struct rtl8169_private *tp)
874{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200875 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000876
Francois Romieucecb5fd2011-04-01 10:21:07 +0200877 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000878}
françois romieub646d902011-01-03 15:08:21 +0000879
françois romieu4da19632011-01-03 15:07:55 +0000880static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int i;
883
Francois Romieua6baf3a2007-11-08 23:23:21 +0100884 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Francois Romieu23714082006-01-29 00:49:09 +0100886 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100887 /*
888 * Check if the RTL8169 has completed writing to the specified
889 * MII register.
890 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200891 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
Francois Romieu23714082006-01-29 00:49:09 +0100893 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700895 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700896 * According to hardware specs a 20us delay is required after write
897 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700898 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700899 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
françois romieu4da19632011-01-03 15:07:55 +0000902static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 int i, value = -1;
905
Francois Romieua6baf3a2007-11-08 23:23:21 +0100906 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Francois Romieu23714082006-01-29 00:49:09 +0100908 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100909 /*
910 * Check if the RTL8169 has completed retrieving data from
911 * the specified MII register.
912 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100914 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 break;
916 }
Francois Romieu23714082006-01-29 00:49:09 +0100917 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700919 /*
920 * According to hardware specs a 20us delay is required after read
921 * complete indication, but before sending next command.
922 */
923 udelay(20);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return value;
926}
927
françois romieuc0e45c12011-01-03 15:08:04 +0000928static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
929{
930 int i;
931
932 RTL_W32(OCPDR, data |
933 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
934 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
935 RTL_W32(EPHY_RXER_NUM, 0);
936
937 for (i = 0; i < 100; i++) {
938 mdelay(1);
939 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
940 break;
941 }
942}
943
944static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
945{
946 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
947 (value & OCPDR_DATA_MASK));
948}
949
950static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
951{
952 int i;
953
954 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
955
956 mdelay(1);
957 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
958 RTL_W32(EPHY_RXER_NUM, 0);
959
960 for (i = 0; i < 100; i++) {
961 mdelay(1);
962 if (RTL_R32(OCPAR) & OCPAR_FLAG)
963 break;
964 }
965
966 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
967}
968
françois romieue6de30d2011-01-03 15:08:37 +0000969#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
970
971static void r8168dp_2_mdio_start(void __iomem *ioaddr)
972{
973 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
974}
975
976static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
977{
978 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
979}
980
981static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
982{
983 r8168dp_2_mdio_start(ioaddr);
984
985 r8169_mdio_write(ioaddr, reg_addr, value);
986
987 r8168dp_2_mdio_stop(ioaddr);
988}
989
990static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
991{
992 int value;
993
994 r8168dp_2_mdio_start(ioaddr);
995
996 value = r8169_mdio_read(ioaddr, reg_addr);
997
998 r8168dp_2_mdio_stop(ioaddr);
999
1000 return value;
1001}
1002
françois romieu4da19632011-01-03 15:07:55 +00001003static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001004{
françois romieuc0e45c12011-01-03 15:08:04 +00001005 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001006}
1007
françois romieu4da19632011-01-03 15:07:55 +00001008static int rtl_readphy(struct rtl8169_private *tp, int location)
1009{
françois romieuc0e45c12011-01-03 15:08:04 +00001010 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001011}
1012
1013static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1014{
1015 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1016}
1017
1018static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001019{
1020 int val;
1021
françois romieu4da19632011-01-03 15:07:55 +00001022 val = rtl_readphy(tp, reg_addr);
1023 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001024}
1025
Francois Romieuccdffb92008-07-26 14:26:06 +02001026static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1027 int val)
1028{
1029 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001030
françois romieu4da19632011-01-03 15:07:55 +00001031 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001032}
1033
1034static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1035{
1036 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001037
françois romieu4da19632011-01-03 15:07:55 +00001038 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001039}
1040
Francois Romieudacf8152008-08-02 20:44:13 +02001041static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1042{
1043 unsigned int i;
1044
1045 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1046 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1047
1048 for (i = 0; i < 100; i++) {
1049 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1050 break;
1051 udelay(10);
1052 }
1053}
1054
1055static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1056{
1057 u16 value = 0xffff;
1058 unsigned int i;
1059
1060 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1061
1062 for (i = 0; i < 100; i++) {
1063 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1064 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1065 break;
1066 }
1067 udelay(10);
1068 }
1069
1070 return value;
1071}
1072
1073static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1074{
1075 unsigned int i;
1076
1077 RTL_W32(CSIDR, value);
1078 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1079 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1080
1081 for (i = 0; i < 100; i++) {
1082 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1083 break;
1084 udelay(10);
1085 }
1086}
1087
1088static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1089{
1090 u32 value = ~0x00;
1091 unsigned int i;
1092
1093 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1094 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1095
1096 for (i = 0; i < 100; i++) {
1097 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1098 value = RTL_R32(CSIDR);
1099 break;
1100 }
1101 udelay(10);
1102 }
1103
1104 return value;
1105}
1106
Hayes Wang133ac402011-07-06 15:58:05 +08001107static
1108void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1109{
1110 unsigned int i;
1111
1112 BUG_ON((addr & 3) || (mask == 0));
1113 RTL_W32(ERIDR, val);
1114 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1115
1116 for (i = 0; i < 100; i++) {
1117 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1118 break;
1119 udelay(100);
1120 }
1121}
1122
1123static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1124{
1125 u32 value = ~0x00;
1126 unsigned int i;
1127
1128 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1129
1130 for (i = 0; i < 100; i++) {
1131 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1132 value = RTL_R32(ERIDR);
1133 break;
1134 }
1135 udelay(100);
1136 }
1137
1138 return value;
1139}
1140
1141static void
1142rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1143{
1144 u32 val;
1145
1146 val = rtl_eri_read(ioaddr, addr, type);
1147 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1148}
1149
françois romieuc28aa382011-08-02 03:53:43 +00001150struct exgmac_reg {
1151 u16 addr;
1152 u16 mask;
1153 u32 val;
1154};
1155
1156static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1157 const struct exgmac_reg *r, int len)
1158{
1159 while (len-- > 0) {
1160 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1161 r++;
1162 }
1163}
1164
françois romieudaf9df62009-10-07 12:44:20 +00001165static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1166{
1167 u8 value = 0xff;
1168 unsigned int i;
1169
1170 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1171
1172 for (i = 0; i < 300; i++) {
1173 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1174 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1175 break;
1176 }
1177 udelay(100);
1178 }
1179
1180 return value;
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1184{
1185 RTL_W16(IntrMask, 0x0000);
1186
1187 RTL_W16(IntrStatus, 0xffff);
1188}
1189
françois romieu4da19632011-01-03 15:07:55 +00001190static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
françois romieu4da19632011-01-03 15:07:55 +00001192 void __iomem *ioaddr = tp->mmio_addr;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return RTL_R32(TBICSR) & TBIReset;
1195}
1196
françois romieu4da19632011-01-03 15:07:55 +00001197static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
françois romieu4da19632011-01-03 15:07:55 +00001199 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1203{
1204 return RTL_R32(TBICSR) & TBILinkOk;
1205}
1206
1207static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1208{
1209 return RTL_R8(PHYstatus) & LinkStatus;
1210}
1211
françois romieu4da19632011-01-03 15:07:55 +00001212static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
françois romieu4da19632011-01-03 15:07:55 +00001214 void __iomem *ioaddr = tp->mmio_addr;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1217}
1218
françois romieu4da19632011-01-03 15:07:55 +00001219static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 unsigned int val;
1222
françois romieu4da19632011-01-03 15:07:55 +00001223 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1224 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Hayes Wang70090422011-07-06 15:58:06 +08001227static void rtl_link_chg_patch(struct rtl8169_private *tp)
1228{
1229 void __iomem *ioaddr = tp->mmio_addr;
1230 struct net_device *dev = tp->dev;
1231
1232 if (!netif_running(dev))
1233 return;
1234
1235 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1236 if (RTL_R8(PHYstatus) & _1000bpsF) {
1237 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1238 0x00000011, ERIAR_EXGMAC);
1239 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1240 0x00000005, ERIAR_EXGMAC);
1241 } else if (RTL_R8(PHYstatus) & _100bps) {
1242 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1243 0x0000001f, ERIAR_EXGMAC);
1244 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1245 0x00000005, ERIAR_EXGMAC);
1246 } else {
1247 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1248 0x0000001f, ERIAR_EXGMAC);
1249 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1250 0x0000003f, ERIAR_EXGMAC);
1251 }
1252 /* Reset packet filter */
1253 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1254 ERIAR_EXGMAC);
1255 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1256 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001257 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1258 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1259 if (RTL_R8(PHYstatus) & _1000bpsF) {
1260 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1261 0x00000011, ERIAR_EXGMAC);
1262 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1263 0x00000005, ERIAR_EXGMAC);
1264 } else {
1265 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1266 0x0000001f, ERIAR_EXGMAC);
1267 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1268 0x0000003f, ERIAR_EXGMAC);
1269 }
Hayes Wang70090422011-07-06 15:58:06 +08001270 }
1271}
1272
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001273static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001274 struct rtl8169_private *tp,
1275 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 unsigned long flags;
1278
1279 spin_lock_irqsave(&tp->lock, flags);
1280 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001281 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001282 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001283 if (pm)
1284 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001286 if (net_ratelimit())
1287 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001288 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001290 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001291 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001292 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 spin_unlock_irqrestore(&tp->lock, flags);
1295}
1296
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001297static void rtl8169_check_link_status(struct net_device *dev,
1298 struct rtl8169_private *tp,
1299 void __iomem *ioaddr)
1300{
1301 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1302}
1303
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001304#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1305
1306static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1307{
1308 void __iomem *ioaddr = tp->mmio_addr;
1309 u8 options;
1310 u32 wolopts = 0;
1311
1312 options = RTL_R8(Config1);
1313 if (!(options & PMEnable))
1314 return 0;
1315
1316 options = RTL_R8(Config3);
1317 if (options & LinkUp)
1318 wolopts |= WAKE_PHY;
1319 if (options & MagicPacket)
1320 wolopts |= WAKE_MAGIC;
1321
1322 options = RTL_R8(Config5);
1323 if (options & UWF)
1324 wolopts |= WAKE_UCAST;
1325 if (options & BWF)
1326 wolopts |= WAKE_BCAST;
1327 if (options & MWF)
1328 wolopts |= WAKE_MCAST;
1329
1330 return wolopts;
1331}
1332
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001333static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1334{
1335 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001336
1337 spin_lock_irq(&tp->lock);
1338
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001339 wol->supported = WAKE_ANY;
1340 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001341
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001342 spin_unlock_irq(&tp->lock);
1343}
1344
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001345static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001346{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001347 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001348 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001349 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001350 u32 opt;
1351 u16 reg;
1352 u8 mask;
1353 } cfg[] = {
1354 { WAKE_ANY, Config1, PMEnable },
1355 { WAKE_PHY, Config3, LinkUp },
1356 { WAKE_MAGIC, Config3, MagicPacket },
1357 { WAKE_UCAST, Config5, UWF },
1358 { WAKE_BCAST, Config5, BWF },
1359 { WAKE_MCAST, Config5, MWF },
1360 { WAKE_ANY, Config5, LanWake }
1361 };
1362
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001363 RTL_W8(Cfg9346, Cfg9346_Unlock);
1364
1365 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1366 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001367 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001368 options |= cfg[i].mask;
1369 RTL_W8(cfg[i].reg, options);
1370 }
1371
1372 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001373}
1374
1375static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1376{
1377 struct rtl8169_private *tp = netdev_priv(dev);
1378
1379 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001380
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001381 if (wol->wolopts)
1382 tp->features |= RTL_FEATURE_WOL;
1383 else
1384 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001385 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001386 spin_unlock_irq(&tp->lock);
1387
françois romieuea809072010-11-08 13:23:58 +00001388 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1389
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001390 return 0;
1391}
1392
Francois Romieu31bd2042011-04-26 18:58:59 +02001393static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1394{
Francois Romieu85bffe62011-04-27 08:22:39 +02001395 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001396}
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398static void rtl8169_get_drvinfo(struct net_device *dev,
1399 struct ethtool_drvinfo *info)
1400{
1401 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001402 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Rick Jones68aad782011-11-07 13:29:27 +00001404 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1405 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1406 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001407 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones68aad782011-11-07 13:29:27 +00001408 strlcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1409 rtl_fw->version, sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412static int rtl8169_get_regs_len(struct net_device *dev)
1413{
1414 return R8169_REGS_SIZE;
1415}
1416
1417static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001418 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 struct rtl8169_private *tp = netdev_priv(dev);
1421 void __iomem *ioaddr = tp->mmio_addr;
1422 int ret = 0;
1423 u32 reg;
1424
1425 reg = RTL_R32(TBICSR);
1426 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1427 (duplex == DUPLEX_FULL)) {
1428 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1429 } else if (autoneg == AUTONEG_ENABLE)
1430 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1431 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001432 netif_warn(tp, link, dev,
1433 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 ret = -EOPNOTSUPP;
1435 }
1436
1437 return ret;
1438}
1439
1440static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001441 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001444 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001445 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Hayes Wang716b50a2011-02-22 17:26:18 +08001447 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001450 int auto_nego;
1451
françois romieu4da19632011-01-03 15:07:55 +00001452 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001453 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1454 ADVERTISE_100HALF | ADVERTISE_100FULL);
1455
1456 if (adv & ADVERTISED_10baseT_Half)
1457 auto_nego |= ADVERTISE_10HALF;
1458 if (adv & ADVERTISED_10baseT_Full)
1459 auto_nego |= ADVERTISE_10FULL;
1460 if (adv & ADVERTISED_100baseT_Half)
1461 auto_nego |= ADVERTISE_100HALF;
1462 if (adv & ADVERTISED_100baseT_Full)
1463 auto_nego |= ADVERTISE_100FULL;
1464
françois romieu3577aa12009-05-19 10:46:48 +00001465 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1466
françois romieu4da19632011-01-03 15:07:55 +00001467 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001468 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1469
1470 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001471 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001472 if (adv & ADVERTISED_1000baseT_Half)
1473 giga_ctrl |= ADVERTISE_1000HALF;
1474 if (adv & ADVERTISED_1000baseT_Full)
1475 giga_ctrl |= ADVERTISE_1000FULL;
1476 } else if (adv & (ADVERTISED_1000baseT_Half |
1477 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001478 netif_info(tp, link, dev,
1479 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001480 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
françois romieu3577aa12009-05-19 10:46:48 +00001483 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001484
françois romieu4da19632011-01-03 15:07:55 +00001485 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1486 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001487 } else {
1488 giga_ctrl = 0;
1489
1490 if (speed == SPEED_10)
1491 bmcr = 0;
1492 else if (speed == SPEED_100)
1493 bmcr = BMCR_SPEED100;
1494 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001495 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001496
1497 if (duplex == DUPLEX_FULL)
1498 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001499 }
1500
françois romieu4da19632011-01-03 15:07:55 +00001501 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001502
Francois Romieucecb5fd2011-04-01 10:21:07 +02001503 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1504 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001505 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001506 rtl_writephy(tp, 0x17, 0x2138);
1507 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001508 } else {
françois romieu4da19632011-01-03 15:07:55 +00001509 rtl_writephy(tp, 0x17, 0x2108);
1510 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001511 }
1512 }
1513
Oliver Neukum54405cd2011-01-06 21:55:13 +01001514 rc = 0;
1515out:
1516 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001520 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct rtl8169_private *tp = netdev_priv(dev);
1523 int ret;
1524
Oliver Neukum54405cd2011-01-06 21:55:13 +01001525 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001526 if (ret < 0)
1527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Francois Romieu4876cc12011-03-11 21:07:11 +01001529 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1530 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001532 }
1533out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return ret;
1535}
1536
1537static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1538{
1539 struct rtl8169_private *tp = netdev_priv(dev);
1540 unsigned long flags;
1541 int ret;
1542
Francois Romieu4876cc12011-03-11 21:07:11 +01001543 del_timer_sync(&tp->timer);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001546 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001547 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return ret;
1551}
1552
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001553static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1554 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Francois Romieud58d46b2011-05-03 16:38:29 +02001556 struct rtl8169_private *tp = netdev_priv(dev);
1557
Francois Romieu2b7b4312011-04-18 22:53:24 -07001558 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001559 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Francois Romieud58d46b2011-05-03 16:38:29 +02001561 if (dev->mtu > JUMBO_1K &&
1562 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1563 features &= ~NETIF_F_IP_CSUM;
1564
Michał Mirosław350fb322011-04-08 06:35:56 +00001565 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001568static int rtl8169_set_features(struct net_device *dev,
1569 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct rtl8169_private *tp = netdev_priv(dev);
1572 void __iomem *ioaddr = tp->mmio_addr;
1573 unsigned long flags;
1574
1575 spin_lock_irqsave(&tp->lock, flags);
1576
Michał Mirosław350fb322011-04-08 06:35:56 +00001577 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 tp->cp_cmd |= RxChkSum;
1579 else
1580 tp->cp_cmd &= ~RxChkSum;
1581
Michał Mirosław350fb322011-04-08 06:35:56 +00001582 if (dev->features & NETIF_F_HW_VLAN_RX)
1583 tp->cp_cmd |= RxVlan;
1584 else
1585 tp->cp_cmd &= ~RxVlan;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 RTL_W16(CPlusCmd, tp->cp_cmd);
1588 RTL_R16(CPlusCmd);
1589
1590 spin_unlock_irqrestore(&tp->lock, flags);
1591
1592 return 0;
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1596 struct sk_buff *skb)
1597{
Jesse Grosseab6d182010-10-20 13:56:03 +00001598 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1600}
1601
Francois Romieu7a8fc772011-03-01 17:18:33 +01001602static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Francois Romieu7a8fc772011-03-01 17:18:33 +01001606 if (opts2 & RxVlanTag)
1607 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Francois Romieuccdffb92008-07-26 14:26:06 +02001612static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 struct rtl8169_private *tp = netdev_priv(dev);
1615 void __iomem *ioaddr = tp->mmio_addr;
1616 u32 status;
1617
1618 cmd->supported =
1619 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1620 cmd->port = PORT_FIBRE;
1621 cmd->transceiver = XCVR_INTERNAL;
1622
1623 status = RTL_R32(TBICSR);
1624 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1625 cmd->autoneg = !!(status & TBINwEnable);
1626
David Decotigny70739492011-04-27 18:32:40 +00001627 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001629
1630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Francois Romieuccdffb92008-07-26 14:26:06 +02001633static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Francois Romieuccdffb92008-07-26 14:26:06 +02001637 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1641{
1642 struct rtl8169_private *tp = netdev_priv(dev);
1643 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001644 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 spin_lock_irqsave(&tp->lock, flags);
1647
Francois Romieuccdffb92008-07-26 14:26:06 +02001648 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001651 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1655 void *p)
1656{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001657 struct rtl8169_private *tp = netdev_priv(dev);
1658 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Francois Romieu5b0384f2006-08-16 16:00:01 +02001660 if (regs->len > R8169_REGS_SIZE)
1661 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Francois Romieu5b0384f2006-08-16 16:00:01 +02001663 spin_lock_irqsave(&tp->lock, flags);
1664 memcpy_fromio(p, tp->mmio_addr, regs->len);
1665 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001668static u32 rtl8169_get_msglevel(struct net_device *dev)
1669{
1670 struct rtl8169_private *tp = netdev_priv(dev);
1671
1672 return tp->msg_enable;
1673}
1674
1675static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1676{
1677 struct rtl8169_private *tp = netdev_priv(dev);
1678
1679 tp->msg_enable = value;
1680}
1681
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001682static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1683 "tx_packets",
1684 "rx_packets",
1685 "tx_errors",
1686 "rx_errors",
1687 "rx_missed",
1688 "align_errors",
1689 "tx_single_collisions",
1690 "tx_multi_collisions",
1691 "unicast",
1692 "broadcast",
1693 "multicast",
1694 "tx_aborted",
1695 "tx_underrun",
1696};
1697
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001698static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001699{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001700 switch (sset) {
1701 case ETH_SS_STATS:
1702 return ARRAY_SIZE(rtl8169_gstrings);
1703 default:
1704 return -EOPNOTSUPP;
1705 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001706}
1707
Ivan Vecera355423d2009-02-06 21:49:57 -08001708static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001709{
1710 struct rtl8169_private *tp = netdev_priv(dev);
1711 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001712 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001713 struct rtl8169_counters *counters;
1714 dma_addr_t paddr;
1715 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001716 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001717
Ivan Vecera355423d2009-02-06 21:49:57 -08001718 /*
1719 * Some chips are unable to dump tally counters when the receiver
1720 * is disabled.
1721 */
1722 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1723 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001724
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001725 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001726 if (!counters)
1727 return;
1728
1729 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001730 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001731 RTL_W32(CounterAddrLow, cmd);
1732 RTL_W32(CounterAddrLow, cmd | CounterDump);
1733
Ivan Vecera355423d2009-02-06 21:49:57 -08001734 while (wait--) {
1735 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001736 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001737 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001738 }
1739 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001740 }
1741
1742 RTL_W32(CounterAddrLow, 0);
1743 RTL_W32(CounterAddrHigh, 0);
1744
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001745 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001746}
1747
Ivan Vecera355423d2009-02-06 21:49:57 -08001748static void rtl8169_get_ethtool_stats(struct net_device *dev,
1749 struct ethtool_stats *stats, u64 *data)
1750{
1751 struct rtl8169_private *tp = netdev_priv(dev);
1752
1753 ASSERT_RTNL();
1754
1755 rtl8169_update_counters(dev);
1756
1757 data[0] = le64_to_cpu(tp->counters.tx_packets);
1758 data[1] = le64_to_cpu(tp->counters.rx_packets);
1759 data[2] = le64_to_cpu(tp->counters.tx_errors);
1760 data[3] = le32_to_cpu(tp->counters.rx_errors);
1761 data[4] = le16_to_cpu(tp->counters.rx_missed);
1762 data[5] = le16_to_cpu(tp->counters.align_errors);
1763 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1764 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1765 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1766 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1767 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1768 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1769 data[12] = le16_to_cpu(tp->counters.tx_underun);
1770}
1771
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001772static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1773{
1774 switch(stringset) {
1775 case ETH_SS_STATS:
1776 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1777 break;
1778 }
1779}
1780
Jeff Garzik7282d492006-09-13 14:30:00 -04001781static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 .get_drvinfo = rtl8169_get_drvinfo,
1783 .get_regs_len = rtl8169_get_regs_len,
1784 .get_link = ethtool_op_get_link,
1785 .get_settings = rtl8169_get_settings,
1786 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001787 .get_msglevel = rtl8169_get_msglevel,
1788 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001790 .get_wol = rtl8169_get_wol,
1791 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001792 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001793 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001794 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795};
1796
Francois Romieu07d3f512007-02-21 22:40:46 +01001797static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001798 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Francois Romieu5d320a22011-05-08 17:47:36 +02001800 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001801 /*
1802 * The driver currently handles the 8168Bf and the 8168Be identically
1803 * but they can be identified more specifically through the test below
1804 * if needed:
1805 *
1806 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001807 *
1808 * Same thing for the 8101Eb and the 8101Ec:
1809 *
1810 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001811 */
Francois Romieu37441002011-06-17 22:58:54 +02001812 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001814 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int mac_version;
1816 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001817 /* 8168F family. */
1818 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1819 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1820
hayeswang01dc7fe2011-03-21 01:50:28 +00001821 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001822 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001823 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1824 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1825 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1826
Francois Romieu5b538df2008-07-20 16:22:45 +02001827 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001828 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1829 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001830 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001831
françois romieue6de30d2011-01-03 15:08:37 +00001832 /* 8168DP family. */
1833 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1834 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001835 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001836
Francois Romieuef808d52008-06-29 13:10:54 +02001837 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001838 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001839 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001840 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001841 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001842 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1843 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001844 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001845 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001846 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001847
1848 /* 8168B family. */
1849 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1850 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1851 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1852 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1853
1854 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001855 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001856 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1857 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1858 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001859 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1860 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1861 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1862 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1863 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1864 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001865 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001866 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001867 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001868 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1869 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001870 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1871 /* FIXME: where did these entries come from ? -- FR */
1872 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1873 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1874
1875 /* 8110 family. */
1876 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1877 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1878 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1879 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1880 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1881 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1882
Jean Delvaref21b75e2009-05-26 20:54:48 -07001883 /* Catch-all */
1884 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001885 };
1886 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 u32 reg;
1888
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001889 reg = RTL_R32(TxConfig);
1890 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 p++;
1892 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001893
1894 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1895 netif_notice(tp, probe, dev,
1896 "unknown MAC, using family default\n");
1897 tp->mac_version = default_version;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
1901static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1902{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001903 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Francois Romieu867763c2007-08-17 18:21:58 +02001906struct phy_reg {
1907 u16 reg;
1908 u16 val;
1909};
1910
françois romieu4da19632011-01-03 15:07:55 +00001911static void rtl_writephy_batch(struct rtl8169_private *tp,
1912 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001913{
1914 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001915 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001916 regs++;
1917 }
1918}
1919
françois romieubca03d52011-01-03 15:07:31 +00001920#define PHY_READ 0x00000000
1921#define PHY_DATA_OR 0x10000000
1922#define PHY_DATA_AND 0x20000000
1923#define PHY_BJMPN 0x30000000
1924#define PHY_READ_EFUSE 0x40000000
1925#define PHY_READ_MAC_BYTE 0x50000000
1926#define PHY_WRITE_MAC_BYTE 0x60000000
1927#define PHY_CLEAR_READCOUNT 0x70000000
1928#define PHY_WRITE 0x80000000
1929#define PHY_READCOUNT_EQ_SKIP 0x90000000
1930#define PHY_COMP_EQ_SKIPN 0xa0000000
1931#define PHY_COMP_NEQ_SKIPN 0xb0000000
1932#define PHY_WRITE_PREVIOUS 0xc0000000
1933#define PHY_SKIPN 0xd0000000
1934#define PHY_DELAY_MS 0xe0000000
1935#define PHY_WRITE_ERI_WORD 0xf0000000
1936
Hayes Wang960aee62011-06-18 11:37:48 +02001937struct fw_info {
1938 u32 magic;
1939 char version[RTL_VER_SIZE];
1940 __le32 fw_start;
1941 __le32 fw_len;
1942 u8 chksum;
1943} __packed;
1944
Francois Romieu1c361ef2011-06-17 17:16:24 +02001945#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1946
1947static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001948{
Francois Romieub6ffd972011-06-17 17:00:05 +02001949 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001950 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001951 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1952 char *version = rtl_fw->version;
1953 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001954
Francois Romieu1c361ef2011-06-17 17:16:24 +02001955 if (fw->size < FW_OPCODE_SIZE)
1956 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001957
1958 if (!fw_info->magic) {
1959 size_t i, size, start;
1960 u8 checksum = 0;
1961
1962 if (fw->size < sizeof(*fw_info))
1963 goto out;
1964
1965 for (i = 0; i < fw->size; i++)
1966 checksum += fw->data[i];
1967 if (checksum != 0)
1968 goto out;
1969
1970 start = le32_to_cpu(fw_info->fw_start);
1971 if (start > fw->size)
1972 goto out;
1973
1974 size = le32_to_cpu(fw_info->fw_len);
1975 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1976 goto out;
1977
1978 memcpy(version, fw_info->version, RTL_VER_SIZE);
1979
1980 pa->code = (__le32 *)(fw->data + start);
1981 pa->size = size;
1982 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001983 if (fw->size % FW_OPCODE_SIZE)
1984 goto out;
1985
1986 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1987
1988 pa->code = (__le32 *)fw->data;
1989 pa->size = fw->size / FW_OPCODE_SIZE;
1990 }
1991 version[RTL_VER_SIZE - 1] = 0;
1992
1993 rc = true;
1994out:
1995 return rc;
1996}
1997
Francois Romieufd112f22011-06-18 00:10:29 +02001998static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1999 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002000{
Francois Romieufd112f22011-06-18 00:10:29 +02002001 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002002 size_t index;
2003
Francois Romieu1c361ef2011-06-17 17:16:24 +02002004 for (index = 0; index < pa->size; index++) {
2005 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002006 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002007
hayeswang42b82dc2011-01-10 02:07:25 +00002008 switch(action & 0xf0000000) {
2009 case PHY_READ:
2010 case PHY_DATA_OR:
2011 case PHY_DATA_AND:
2012 case PHY_READ_EFUSE:
2013 case PHY_CLEAR_READCOUNT:
2014 case PHY_WRITE:
2015 case PHY_WRITE_PREVIOUS:
2016 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002017 break;
2018
hayeswang42b82dc2011-01-10 02:07:25 +00002019 case PHY_BJMPN:
2020 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002021 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002022 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002023 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002024 }
2025 break;
2026 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002027 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002028 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002029 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002030 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002031 }
2032 break;
2033 case PHY_COMP_EQ_SKIPN:
2034 case PHY_COMP_NEQ_SKIPN:
2035 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002036 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002037 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002038 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002039 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002040 }
2041 break;
2042
2043 case PHY_READ_MAC_BYTE:
2044 case PHY_WRITE_MAC_BYTE:
2045 case PHY_WRITE_ERI_WORD:
2046 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002047 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002048 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002049 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002050 }
2051 }
Francois Romieufd112f22011-06-18 00:10:29 +02002052 rc = true;
2053out:
2054 return rc;
2055}
françois romieubca03d52011-01-03 15:07:31 +00002056
Francois Romieufd112f22011-06-18 00:10:29 +02002057static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2058{
2059 struct net_device *dev = tp->dev;
2060 int rc = -EINVAL;
2061
2062 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2063 netif_err(tp, ifup, dev, "invalid firwmare\n");
2064 goto out;
2065 }
2066
2067 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2068 rc = 0;
2069out:
2070 return rc;
2071}
2072
2073static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2074{
2075 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2076 u32 predata, count;
2077 size_t index;
2078
2079 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002080
Francois Romieu1c361ef2011-06-17 17:16:24 +02002081 for (index = 0; index < pa->size; ) {
2082 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002083 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002084 u32 regno = (action & 0x0fff0000) >> 16;
2085
2086 if (!action)
2087 break;
françois romieubca03d52011-01-03 15:07:31 +00002088
2089 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002090 case PHY_READ:
2091 predata = rtl_readphy(tp, regno);
2092 count++;
2093 index++;
françois romieubca03d52011-01-03 15:07:31 +00002094 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002095 case PHY_DATA_OR:
2096 predata |= data;
2097 index++;
2098 break;
2099 case PHY_DATA_AND:
2100 predata &= data;
2101 index++;
2102 break;
2103 case PHY_BJMPN:
2104 index -= regno;
2105 break;
2106 case PHY_READ_EFUSE:
2107 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2108 index++;
2109 break;
2110 case PHY_CLEAR_READCOUNT:
2111 count = 0;
2112 index++;
2113 break;
2114 case PHY_WRITE:
2115 rtl_writephy(tp, regno, data);
2116 index++;
2117 break;
2118 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002119 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002120 break;
2121 case PHY_COMP_EQ_SKIPN:
2122 if (predata == data)
2123 index += regno;
2124 index++;
2125 break;
2126 case PHY_COMP_NEQ_SKIPN:
2127 if (predata != data)
2128 index += regno;
2129 index++;
2130 break;
2131 case PHY_WRITE_PREVIOUS:
2132 rtl_writephy(tp, regno, predata);
2133 index++;
2134 break;
2135 case PHY_SKIPN:
2136 index += regno + 1;
2137 break;
2138 case PHY_DELAY_MS:
2139 mdelay(data);
2140 index++;
2141 break;
2142
2143 case PHY_READ_MAC_BYTE:
2144 case PHY_WRITE_MAC_BYTE:
2145 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002146 default:
2147 BUG();
2148 }
2149 }
2150}
2151
françois romieuf1e02ed2011-01-13 13:07:53 +00002152static void rtl_release_firmware(struct rtl8169_private *tp)
2153{
Francois Romieub6ffd972011-06-17 17:00:05 +02002154 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2155 release_firmware(tp->rtl_fw->fw);
2156 kfree(tp->rtl_fw);
2157 }
2158 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002159}
2160
François Romieu953a12c2011-04-24 17:38:48 +02002161static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002162{
Francois Romieub6ffd972011-06-17 17:00:05 +02002163 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002164
2165 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002166 if (!IS_ERR_OR_NULL(rtl_fw))
2167 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002168}
2169
2170static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2171{
2172 if (rtl_readphy(tp, reg) != val)
2173 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2174 else
2175 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002176}
2177
françois romieu4da19632011-01-03 15:07:55 +00002178static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002180 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002181 { 0x1f, 0x0001 },
2182 { 0x06, 0x006e },
2183 { 0x08, 0x0708 },
2184 { 0x15, 0x4000 },
2185 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
françois romieu0b9b5712009-08-10 19:44:56 +00002187 { 0x1f, 0x0001 },
2188 { 0x03, 0x00a1 },
2189 { 0x02, 0x0008 },
2190 { 0x01, 0x0120 },
2191 { 0x00, 0x1000 },
2192 { 0x04, 0x0800 },
2193 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
françois romieu0b9b5712009-08-10 19:44:56 +00002195 { 0x03, 0xff41 },
2196 { 0x02, 0xdf60 },
2197 { 0x01, 0x0140 },
2198 { 0x00, 0x0077 },
2199 { 0x04, 0x7800 },
2200 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
françois romieu0b9b5712009-08-10 19:44:56 +00002202 { 0x03, 0x802f },
2203 { 0x02, 0x4f02 },
2204 { 0x01, 0x0409 },
2205 { 0x00, 0xf0f9 },
2206 { 0x04, 0x9800 },
2207 { 0x04, 0x9000 },
2208
2209 { 0x03, 0xdf01 },
2210 { 0x02, 0xdf20 },
2211 { 0x01, 0xff95 },
2212 { 0x00, 0xba00 },
2213 { 0x04, 0xa800 },
2214 { 0x04, 0xa000 },
2215
2216 { 0x03, 0xff41 },
2217 { 0x02, 0xdf20 },
2218 { 0x01, 0x0140 },
2219 { 0x00, 0x00bb },
2220 { 0x04, 0xb800 },
2221 { 0x04, 0xb000 },
2222
2223 { 0x03, 0xdf41 },
2224 { 0x02, 0xdc60 },
2225 { 0x01, 0x6340 },
2226 { 0x00, 0x007d },
2227 { 0x04, 0xd800 },
2228 { 0x04, 0xd000 },
2229
2230 { 0x03, 0xdf01 },
2231 { 0x02, 0xdf20 },
2232 { 0x01, 0x100a },
2233 { 0x00, 0xa0ff },
2234 { 0x04, 0xf800 },
2235 { 0x04, 0xf000 },
2236
2237 { 0x1f, 0x0000 },
2238 { 0x0b, 0x0000 },
2239 { 0x00, 0x9200 }
2240 };
2241
françois romieu4da19632011-01-03 15:07:55 +00002242 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
françois romieu4da19632011-01-03 15:07:55 +00002245static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002246{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002247 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002248 { 0x1f, 0x0002 },
2249 { 0x01, 0x90d0 },
2250 { 0x1f, 0x0000 }
2251 };
2252
françois romieu4da19632011-01-03 15:07:55 +00002253 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002254}
2255
françois romieu4da19632011-01-03 15:07:55 +00002256static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002257{
2258 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002259
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002260 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2261 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002262 return;
2263
françois romieu4da19632011-01-03 15:07:55 +00002264 rtl_writephy(tp, 0x1f, 0x0001);
2265 rtl_writephy(tp, 0x10, 0xf01b);
2266 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002267}
2268
françois romieu4da19632011-01-03 15:07:55 +00002269static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002270{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002271 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002272 { 0x1f, 0x0001 },
2273 { 0x04, 0x0000 },
2274 { 0x03, 0x00a1 },
2275 { 0x02, 0x0008 },
2276 { 0x01, 0x0120 },
2277 { 0x00, 0x1000 },
2278 { 0x04, 0x0800 },
2279 { 0x04, 0x9000 },
2280 { 0x03, 0x802f },
2281 { 0x02, 0x4f02 },
2282 { 0x01, 0x0409 },
2283 { 0x00, 0xf099 },
2284 { 0x04, 0x9800 },
2285 { 0x04, 0xa000 },
2286 { 0x03, 0xdf01 },
2287 { 0x02, 0xdf20 },
2288 { 0x01, 0xff95 },
2289 { 0x00, 0xba00 },
2290 { 0x04, 0xa800 },
2291 { 0x04, 0xf000 },
2292 { 0x03, 0xdf01 },
2293 { 0x02, 0xdf20 },
2294 { 0x01, 0x101a },
2295 { 0x00, 0xa0ff },
2296 { 0x04, 0xf800 },
2297 { 0x04, 0x0000 },
2298 { 0x1f, 0x0000 },
2299
2300 { 0x1f, 0x0001 },
2301 { 0x10, 0xf41b },
2302 { 0x14, 0xfb54 },
2303 { 0x18, 0xf5c7 },
2304 { 0x1f, 0x0000 },
2305
2306 { 0x1f, 0x0001 },
2307 { 0x17, 0x0cc0 },
2308 { 0x1f, 0x0000 }
2309 };
2310
françois romieu4da19632011-01-03 15:07:55 +00002311 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002312
françois romieu4da19632011-01-03 15:07:55 +00002313 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002314}
2315
françois romieu4da19632011-01-03 15:07:55 +00002316static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002317{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002318 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002319 { 0x1f, 0x0001 },
2320 { 0x04, 0x0000 },
2321 { 0x03, 0x00a1 },
2322 { 0x02, 0x0008 },
2323 { 0x01, 0x0120 },
2324 { 0x00, 0x1000 },
2325 { 0x04, 0x0800 },
2326 { 0x04, 0x9000 },
2327 { 0x03, 0x802f },
2328 { 0x02, 0x4f02 },
2329 { 0x01, 0x0409 },
2330 { 0x00, 0xf099 },
2331 { 0x04, 0x9800 },
2332 { 0x04, 0xa000 },
2333 { 0x03, 0xdf01 },
2334 { 0x02, 0xdf20 },
2335 { 0x01, 0xff95 },
2336 { 0x00, 0xba00 },
2337 { 0x04, 0xa800 },
2338 { 0x04, 0xf000 },
2339 { 0x03, 0xdf01 },
2340 { 0x02, 0xdf20 },
2341 { 0x01, 0x101a },
2342 { 0x00, 0xa0ff },
2343 { 0x04, 0xf800 },
2344 { 0x04, 0x0000 },
2345 { 0x1f, 0x0000 },
2346
2347 { 0x1f, 0x0001 },
2348 { 0x0b, 0x8480 },
2349 { 0x1f, 0x0000 },
2350
2351 { 0x1f, 0x0001 },
2352 { 0x18, 0x67c7 },
2353 { 0x04, 0x2000 },
2354 { 0x03, 0x002f },
2355 { 0x02, 0x4360 },
2356 { 0x01, 0x0109 },
2357 { 0x00, 0x3022 },
2358 { 0x04, 0x2800 },
2359 { 0x1f, 0x0000 },
2360
2361 { 0x1f, 0x0001 },
2362 { 0x17, 0x0cc0 },
2363 { 0x1f, 0x0000 }
2364 };
2365
françois romieu4da19632011-01-03 15:07:55 +00002366 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002367}
2368
françois romieu4da19632011-01-03 15:07:55 +00002369static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002370{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002371 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002372 { 0x10, 0xf41b },
2373 { 0x1f, 0x0000 }
2374 };
2375
françois romieu4da19632011-01-03 15:07:55 +00002376 rtl_writephy(tp, 0x1f, 0x0001);
2377 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002378
françois romieu4da19632011-01-03 15:07:55 +00002379 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002380}
2381
françois romieu4da19632011-01-03 15:07:55 +00002382static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002383{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002384 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002385 { 0x1f, 0x0001 },
2386 { 0x10, 0xf41b },
2387 { 0x1f, 0x0000 }
2388 };
2389
françois romieu4da19632011-01-03 15:07:55 +00002390 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002391}
2392
françois romieu4da19632011-01-03 15:07:55 +00002393static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002394{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002395 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002396 { 0x1f, 0x0000 },
2397 { 0x1d, 0x0f00 },
2398 { 0x1f, 0x0002 },
2399 { 0x0c, 0x1ec8 },
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));
Francois Romieu867763c2007-08-17 18:21:58 +02002404}
2405
françois romieu4da19632011-01-03 15:07:55 +00002406static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002407{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002408 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002409 { 0x1f, 0x0001 },
2410 { 0x1d, 0x3d98 },
2411 { 0x1f, 0x0000 }
2412 };
2413
françois romieu4da19632011-01-03 15:07:55 +00002414 rtl_writephy(tp, 0x1f, 0x0000);
2415 rtl_patchphy(tp, 0x14, 1 << 5);
2416 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002417
françois romieu4da19632011-01-03 15:07:55 +00002418 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002419}
2420
françois romieu4da19632011-01-03 15:07:55 +00002421static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002422{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002423 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002424 { 0x1f, 0x0001 },
2425 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002426 { 0x1f, 0x0002 },
2427 { 0x00, 0x88d4 },
2428 { 0x01, 0x82b1 },
2429 { 0x03, 0x7002 },
2430 { 0x08, 0x9e30 },
2431 { 0x09, 0x01f0 },
2432 { 0x0a, 0x5500 },
2433 { 0x0c, 0x00c8 },
2434 { 0x1f, 0x0003 },
2435 { 0x12, 0xc096 },
2436 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002437 { 0x1f, 0x0000 },
2438 { 0x1f, 0x0000 },
2439 { 0x09, 0x2000 },
2440 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002441 };
2442
françois romieu4da19632011-01-03 15:07:55 +00002443 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002444
françois romieu4da19632011-01-03 15:07:55 +00002445 rtl_patchphy(tp, 0x14, 1 << 5);
2446 rtl_patchphy(tp, 0x0d, 1 << 5);
2447 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002448}
2449
françois romieu4da19632011-01-03 15:07:55 +00002450static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002451{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002452 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002453 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002454 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002455 { 0x03, 0x802f },
2456 { 0x02, 0x4f02 },
2457 { 0x01, 0x0409 },
2458 { 0x00, 0xf099 },
2459 { 0x04, 0x9800 },
2460 { 0x04, 0x9000 },
2461 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002462 { 0x1f, 0x0002 },
2463 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002464 { 0x06, 0x0761 },
2465 { 0x1f, 0x0003 },
2466 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002467 { 0x1f, 0x0000 }
2468 };
2469
françois romieu4da19632011-01-03 15:07:55 +00002470 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002471
françois romieu4da19632011-01-03 15:07:55 +00002472 rtl_patchphy(tp, 0x16, 1 << 0);
2473 rtl_patchphy(tp, 0x14, 1 << 5);
2474 rtl_patchphy(tp, 0x0d, 1 << 5);
2475 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002476}
2477
françois romieu4da19632011-01-03 15:07:55 +00002478static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002479{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002480 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002481 { 0x1f, 0x0001 },
2482 { 0x12, 0x2300 },
2483 { 0x1d, 0x3d98 },
2484 { 0x1f, 0x0002 },
2485 { 0x0c, 0x7eb8 },
2486 { 0x06, 0x5461 },
2487 { 0x1f, 0x0003 },
2488 { 0x16, 0x0f0a },
2489 { 0x1f, 0x0000 }
2490 };
2491
françois romieu4da19632011-01-03 15:07:55 +00002492 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002493
françois romieu4da19632011-01-03 15:07:55 +00002494 rtl_patchphy(tp, 0x16, 1 << 0);
2495 rtl_patchphy(tp, 0x14, 1 << 5);
2496 rtl_patchphy(tp, 0x0d, 1 << 5);
2497 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002498}
2499
françois romieu4da19632011-01-03 15:07:55 +00002500static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002501{
françois romieu4da19632011-01-03 15:07:55 +00002502 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002503}
2504
françois romieubca03d52011-01-03 15:07:31 +00002505static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002506{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002507 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002508 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002509 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002510 { 0x06, 0x4064 },
2511 { 0x07, 0x2863 },
2512 { 0x08, 0x059c },
2513 { 0x09, 0x26b4 },
2514 { 0x0a, 0x6a19 },
2515 { 0x0b, 0xdcc8 },
2516 { 0x10, 0xf06d },
2517 { 0x14, 0x7f68 },
2518 { 0x18, 0x7fd9 },
2519 { 0x1c, 0xf0ff },
2520 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002521 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002522 { 0x12, 0xf49f },
2523 { 0x13, 0x070b },
2524 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002525 { 0x14, 0x94c0 },
2526
2527 /*
2528 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002529 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002530 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002531 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002532 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002533 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002534 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002535 { 0x06, 0x5561 },
2536
2537 /*
2538 * Can not link to 1Gbps with bad cable
2539 * Decrease SNR threshold form 21.07dB to 19.04dB
2540 */
2541 { 0x1f, 0x0001 },
2542 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002543
2544 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002545 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002546 };
françois romieubca03d52011-01-03 15:07:31 +00002547 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002548
françois romieu4da19632011-01-03 15:07:55 +00002549 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002550
françois romieubca03d52011-01-03 15:07:31 +00002551 /*
2552 * Rx Error Issue
2553 * Fine Tune Switching regulator parameter
2554 */
françois romieu4da19632011-01-03 15:07:55 +00002555 rtl_writephy(tp, 0x1f, 0x0002);
2556 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2557 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002558
françois romieudaf9df62009-10-07 12:44:20 +00002559 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002560 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002561 { 0x1f, 0x0002 },
2562 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002563 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002564 { 0x05, 0x8330 },
2565 { 0x06, 0x669a },
2566 { 0x1f, 0x0002 }
2567 };
2568 int val;
2569
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002571
françois romieu4da19632011-01-03 15:07:55 +00002572 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002573
2574 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002575 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002576 0x0065, 0x0066, 0x0067, 0x0068,
2577 0x0069, 0x006a, 0x006b, 0x006c
2578 };
2579 int i;
2580
françois romieu4da19632011-01-03 15:07:55 +00002581 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002582
2583 val &= 0xff00;
2584 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002585 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002586 }
2587 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002588 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002589 { 0x1f, 0x0002 },
2590 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002591 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x05, 0x8330 },
2593 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002594 };
2595
françois romieu4da19632011-01-03 15:07:55 +00002596 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002597 }
2598
françois romieubca03d52011-01-03 15:07:31 +00002599 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002600 rtl_writephy(tp, 0x1f, 0x0002);
2601 rtl_patchphy(tp, 0x0d, 0x0300);
2602 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002603
françois romieubca03d52011-01-03 15:07:31 +00002604 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002605 rtl_writephy(tp, 0x1f, 0x0002);
2606 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2607 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002608
françois romieu4da19632011-01-03 15:07:55 +00002609 rtl_writephy(tp, 0x1f, 0x0005);
2610 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002611
2612 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002613
françois romieu4da19632011-01-03 15:07:55 +00002614 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002615}
2616
françois romieubca03d52011-01-03 15:07:31 +00002617static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002618{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002619 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002620 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002621 { 0x1f, 0x0001 },
2622 { 0x06, 0x4064 },
2623 { 0x07, 0x2863 },
2624 { 0x08, 0x059c },
2625 { 0x09, 0x26b4 },
2626 { 0x0a, 0x6a19 },
2627 { 0x0b, 0xdcc8 },
2628 { 0x10, 0xf06d },
2629 { 0x14, 0x7f68 },
2630 { 0x18, 0x7fd9 },
2631 { 0x1c, 0xf0ff },
2632 { 0x1d, 0x3d9c },
2633 { 0x1f, 0x0003 },
2634 { 0x12, 0xf49f },
2635 { 0x13, 0x070b },
2636 { 0x1a, 0x05ad },
2637 { 0x14, 0x94c0 },
2638
françois romieubca03d52011-01-03 15:07:31 +00002639 /*
2640 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002641 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002642 */
françois romieudaf9df62009-10-07 12:44:20 +00002643 { 0x1f, 0x0002 },
2644 { 0x06, 0x5561 },
2645 { 0x1f, 0x0005 },
2646 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002647 { 0x06, 0x5561 },
2648
2649 /*
2650 * Can not link to 1Gbps with bad cable
2651 * Decrease SNR threshold form 21.07dB to 19.04dB
2652 */
2653 { 0x1f, 0x0001 },
2654 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002655
2656 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002657 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002658 };
françois romieubca03d52011-01-03 15:07:31 +00002659 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002660
françois romieu4da19632011-01-03 15:07:55 +00002661 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002662
2663 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002664 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002665 { 0x1f, 0x0002 },
2666 { 0x05, 0x669a },
2667 { 0x1f, 0x0005 },
2668 { 0x05, 0x8330 },
2669 { 0x06, 0x669a },
2670
2671 { 0x1f, 0x0002 }
2672 };
2673 int val;
2674
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002676
françois romieu4da19632011-01-03 15:07:55 +00002677 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002678 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002679 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002680 0x0065, 0x0066, 0x0067, 0x0068,
2681 0x0069, 0x006a, 0x006b, 0x006c
2682 };
2683 int i;
2684
françois romieu4da19632011-01-03 15:07:55 +00002685 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002686
2687 val &= 0xff00;
2688 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002689 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002690 }
2691 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002692 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002693 { 0x1f, 0x0002 },
2694 { 0x05, 0x2642 },
2695 { 0x1f, 0x0005 },
2696 { 0x05, 0x8330 },
2697 { 0x06, 0x2642 }
2698 };
2699
françois romieu4da19632011-01-03 15:07:55 +00002700 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002701 }
2702
françois romieubca03d52011-01-03 15:07:31 +00002703 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002704 rtl_writephy(tp, 0x1f, 0x0002);
2705 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2706 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002707
françois romieubca03d52011-01-03 15:07:31 +00002708 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002709 rtl_writephy(tp, 0x1f, 0x0002);
2710 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002711
françois romieu4da19632011-01-03 15:07:55 +00002712 rtl_writephy(tp, 0x1f, 0x0005);
2713 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002714
2715 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002716
françois romieu4da19632011-01-03 15:07:55 +00002717 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002718}
2719
françois romieu4da19632011-01-03 15:07:55 +00002720static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002721{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002722 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002723 { 0x1f, 0x0002 },
2724 { 0x10, 0x0008 },
2725 { 0x0d, 0x006c },
2726
2727 { 0x1f, 0x0000 },
2728 { 0x0d, 0xf880 },
2729
2730 { 0x1f, 0x0001 },
2731 { 0x17, 0x0cc0 },
2732
2733 { 0x1f, 0x0001 },
2734 { 0x0b, 0xa4d8 },
2735 { 0x09, 0x281c },
2736 { 0x07, 0x2883 },
2737 { 0x0a, 0x6b35 },
2738 { 0x1d, 0x3da4 },
2739 { 0x1c, 0xeffd },
2740 { 0x14, 0x7f52 },
2741 { 0x18, 0x7fc6 },
2742 { 0x08, 0x0601 },
2743 { 0x06, 0x4063 },
2744 { 0x10, 0xf074 },
2745 { 0x1f, 0x0003 },
2746 { 0x13, 0x0789 },
2747 { 0x12, 0xf4bd },
2748 { 0x1a, 0x04fd },
2749 { 0x14, 0x84b0 },
2750 { 0x1f, 0x0000 },
2751 { 0x00, 0x9200 },
2752
2753 { 0x1f, 0x0005 },
2754 { 0x01, 0x0340 },
2755 { 0x1f, 0x0001 },
2756 { 0x04, 0x4000 },
2757 { 0x03, 0x1d21 },
2758 { 0x02, 0x0c32 },
2759 { 0x01, 0x0200 },
2760 { 0x00, 0x5554 },
2761 { 0x04, 0x4800 },
2762 { 0x04, 0x4000 },
2763 { 0x04, 0xf000 },
2764 { 0x03, 0xdf01 },
2765 { 0x02, 0xdf20 },
2766 { 0x01, 0x101a },
2767 { 0x00, 0xa0ff },
2768 { 0x04, 0xf800 },
2769 { 0x04, 0xf000 },
2770 { 0x1f, 0x0000 },
2771
2772 { 0x1f, 0x0007 },
2773 { 0x1e, 0x0023 },
2774 { 0x16, 0x0000 },
2775 { 0x1f, 0x0000 }
2776 };
2777
françois romieu4da19632011-01-03 15:07:55 +00002778 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002779}
2780
françois romieue6de30d2011-01-03 15:08:37 +00002781static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2782{
2783 static const struct phy_reg phy_reg_init[] = {
2784 { 0x1f, 0x0001 },
2785 { 0x17, 0x0cc0 },
2786
2787 { 0x1f, 0x0007 },
2788 { 0x1e, 0x002d },
2789 { 0x18, 0x0040 },
2790 { 0x1f, 0x0000 }
2791 };
2792
2793 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2794 rtl_patchphy(tp, 0x0d, 1 << 5);
2795}
2796
Hayes Wang70090422011-07-06 15:58:06 +08002797static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002798{
2799 static const struct phy_reg phy_reg_init[] = {
2800 /* Enable Delay cap */
2801 { 0x1f, 0x0005 },
2802 { 0x05, 0x8b80 },
2803 { 0x06, 0xc896 },
2804 { 0x1f, 0x0000 },
2805
2806 /* Channel estimation fine tune */
2807 { 0x1f, 0x0001 },
2808 { 0x0b, 0x6c20 },
2809 { 0x07, 0x2872 },
2810 { 0x1c, 0xefff },
2811 { 0x1f, 0x0003 },
2812 { 0x14, 0x6420 },
2813 { 0x1f, 0x0000 },
2814
2815 /* Update PFM & 10M TX idle timer */
2816 { 0x1f, 0x0007 },
2817 { 0x1e, 0x002f },
2818 { 0x15, 0x1919 },
2819 { 0x1f, 0x0000 },
2820
2821 { 0x1f, 0x0007 },
2822 { 0x1e, 0x00ac },
2823 { 0x18, 0x0006 },
2824 { 0x1f, 0x0000 }
2825 };
2826
Francois Romieu15ecd032011-04-27 13:52:22 -07002827 rtl_apply_firmware(tp);
2828
hayeswang01dc7fe2011-03-21 01:50:28 +00002829 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2830
2831 /* DCO enable for 10M IDLE Power */
2832 rtl_writephy(tp, 0x1f, 0x0007);
2833 rtl_writephy(tp, 0x1e, 0x0023);
2834 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2835 rtl_writephy(tp, 0x1f, 0x0000);
2836
2837 /* For impedance matching */
2838 rtl_writephy(tp, 0x1f, 0x0002);
2839 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002840 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002841
2842 /* PHY auto speed down */
2843 rtl_writephy(tp, 0x1f, 0x0007);
2844 rtl_writephy(tp, 0x1e, 0x002d);
2845 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2846 rtl_writephy(tp, 0x1f, 0x0000);
2847 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2848
2849 rtl_writephy(tp, 0x1f, 0x0005);
2850 rtl_writephy(tp, 0x05, 0x8b86);
2851 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2852 rtl_writephy(tp, 0x1f, 0x0000);
2853
2854 rtl_writephy(tp, 0x1f, 0x0005);
2855 rtl_writephy(tp, 0x05, 0x8b85);
2856 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2857 rtl_writephy(tp, 0x1f, 0x0007);
2858 rtl_writephy(tp, 0x1e, 0x0020);
2859 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2860 rtl_writephy(tp, 0x1f, 0x0006);
2861 rtl_writephy(tp, 0x00, 0x5a00);
2862 rtl_writephy(tp, 0x1f, 0x0000);
2863 rtl_writephy(tp, 0x0d, 0x0007);
2864 rtl_writephy(tp, 0x0e, 0x003c);
2865 rtl_writephy(tp, 0x0d, 0x4007);
2866 rtl_writephy(tp, 0x0e, 0x0000);
2867 rtl_writephy(tp, 0x0d, 0x0000);
2868}
2869
Hayes Wang70090422011-07-06 15:58:06 +08002870static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2871{
2872 static const struct phy_reg phy_reg_init[] = {
2873 /* Enable Delay cap */
2874 { 0x1f, 0x0004 },
2875 { 0x1f, 0x0007 },
2876 { 0x1e, 0x00ac },
2877 { 0x18, 0x0006 },
2878 { 0x1f, 0x0002 },
2879 { 0x1f, 0x0000 },
2880 { 0x1f, 0x0000 },
2881
2882 /* Channel estimation fine tune */
2883 { 0x1f, 0x0003 },
2884 { 0x09, 0xa20f },
2885 { 0x1f, 0x0000 },
2886 { 0x1f, 0x0000 },
2887
2888 /* Green Setting */
2889 { 0x1f, 0x0005 },
2890 { 0x05, 0x8b5b },
2891 { 0x06, 0x9222 },
2892 { 0x05, 0x8b6d },
2893 { 0x06, 0x8000 },
2894 { 0x05, 0x8b76 },
2895 { 0x06, 0x8000 },
2896 { 0x1f, 0x0000 }
2897 };
2898
2899 rtl_apply_firmware(tp);
2900
2901 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2902
2903 /* For 4-corner performance improve */
2904 rtl_writephy(tp, 0x1f, 0x0005);
2905 rtl_writephy(tp, 0x05, 0x8b80);
2906 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2907 rtl_writephy(tp, 0x1f, 0x0000);
2908
2909 /* PHY auto speed down */
2910 rtl_writephy(tp, 0x1f, 0x0004);
2911 rtl_writephy(tp, 0x1f, 0x0007);
2912 rtl_writephy(tp, 0x1e, 0x002d);
2913 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2914 rtl_writephy(tp, 0x1f, 0x0002);
2915 rtl_writephy(tp, 0x1f, 0x0000);
2916 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2917
2918 /* improve 10M EEE waveform */
2919 rtl_writephy(tp, 0x1f, 0x0005);
2920 rtl_writephy(tp, 0x05, 0x8b86);
2921 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2922 rtl_writephy(tp, 0x1f, 0x0000);
2923
2924 /* Improve 2-pair detection performance */
2925 rtl_writephy(tp, 0x1f, 0x0005);
2926 rtl_writephy(tp, 0x05, 0x8b85);
2927 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0000);
2929
2930 /* EEE setting */
2931 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2932 ERIAR_EXGMAC);
2933 rtl_writephy(tp, 0x1f, 0x0005);
2934 rtl_writephy(tp, 0x05, 0x8b85);
2935 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2936 rtl_writephy(tp, 0x1f, 0x0004);
2937 rtl_writephy(tp, 0x1f, 0x0007);
2938 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04002939 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08002940 rtl_writephy(tp, 0x1f, 0x0002);
2941 rtl_writephy(tp, 0x1f, 0x0000);
2942 rtl_writephy(tp, 0x0d, 0x0007);
2943 rtl_writephy(tp, 0x0e, 0x003c);
2944 rtl_writephy(tp, 0x0d, 0x4007);
2945 rtl_writephy(tp, 0x0e, 0x0000);
2946 rtl_writephy(tp, 0x0d, 0x0000);
2947
2948 /* Green feature */
2949 rtl_writephy(tp, 0x1f, 0x0003);
2950 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2951 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2952 rtl_writephy(tp, 0x1f, 0x0000);
2953}
2954
Hayes Wangc2218922011-09-06 16:55:18 +08002955static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2956{
2957 static const struct phy_reg phy_reg_init[] = {
2958 /* Channel estimation fine tune */
2959 { 0x1f, 0x0003 },
2960 { 0x09, 0xa20f },
2961 { 0x1f, 0x0000 },
2962
2963 /* Modify green table for giga & fnet */
2964 { 0x1f, 0x0005 },
2965 { 0x05, 0x8b55 },
2966 { 0x06, 0x0000 },
2967 { 0x05, 0x8b5e },
2968 { 0x06, 0x0000 },
2969 { 0x05, 0x8b67 },
2970 { 0x06, 0x0000 },
2971 { 0x05, 0x8b70 },
2972 { 0x06, 0x0000 },
2973 { 0x1f, 0x0000 },
2974 { 0x1f, 0x0007 },
2975 { 0x1e, 0x0078 },
2976 { 0x17, 0x0000 },
2977 { 0x19, 0x00fb },
2978 { 0x1f, 0x0000 },
2979
2980 /* Modify green table for 10M */
2981 { 0x1f, 0x0005 },
2982 { 0x05, 0x8b79 },
2983 { 0x06, 0xaa00 },
2984 { 0x1f, 0x0000 },
2985
2986 /* Disable hiimpedance detection (RTCT) */
2987 { 0x1f, 0x0003 },
2988 { 0x01, 0x328a },
2989 { 0x1f, 0x0000 }
2990 };
2991
2992 rtl_apply_firmware(tp);
2993
2994 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2995
2996 /* For 4-corner performance improve */
2997 rtl_writephy(tp, 0x1f, 0x0005);
2998 rtl_writephy(tp, 0x05, 0x8b80);
2999 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3000 rtl_writephy(tp, 0x1f, 0x0000);
3001
3002 /* PHY auto speed down */
3003 rtl_writephy(tp, 0x1f, 0x0007);
3004 rtl_writephy(tp, 0x1e, 0x002d);
3005 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3006 rtl_writephy(tp, 0x1f, 0x0000);
3007 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3008
3009 /* Improve 10M EEE waveform */
3010 rtl_writephy(tp, 0x1f, 0x0005);
3011 rtl_writephy(tp, 0x05, 0x8b86);
3012 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3013 rtl_writephy(tp, 0x1f, 0x0000);
3014
3015 /* Improve 2-pair detection performance */
3016 rtl_writephy(tp, 0x1f, 0x0005);
3017 rtl_writephy(tp, 0x05, 0x8b85);
3018 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3019 rtl_writephy(tp, 0x1f, 0x0000);
3020}
3021
3022static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3023{
3024 rtl_apply_firmware(tp);
3025
3026 /* For 4-corner performance improve */
3027 rtl_writephy(tp, 0x1f, 0x0005);
3028 rtl_writephy(tp, 0x05, 0x8b80);
3029 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3030 rtl_writephy(tp, 0x1f, 0x0000);
3031
3032 /* PHY auto speed down */
3033 rtl_writephy(tp, 0x1f, 0x0007);
3034 rtl_writephy(tp, 0x1e, 0x002d);
3035 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3036 rtl_writephy(tp, 0x1f, 0x0000);
3037 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3038
3039 /* Improve 10M EEE waveform */
3040 rtl_writephy(tp, 0x1f, 0x0005);
3041 rtl_writephy(tp, 0x05, 0x8b86);
3042 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3043 rtl_writephy(tp, 0x1f, 0x0000);
3044}
3045
françois romieu4da19632011-01-03 15:07:55 +00003046static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003047{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003048 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003049 { 0x1f, 0x0003 },
3050 { 0x08, 0x441d },
3051 { 0x01, 0x9100 },
3052 { 0x1f, 0x0000 }
3053 };
3054
françois romieu4da19632011-01-03 15:07:55 +00003055 rtl_writephy(tp, 0x1f, 0x0000);
3056 rtl_patchphy(tp, 0x11, 1 << 12);
3057 rtl_patchphy(tp, 0x19, 1 << 13);
3058 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003059
françois romieu4da19632011-01-03 15:07:55 +00003060 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003061}
3062
Hayes Wang5a5e4442011-02-22 17:26:21 +08003063static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3064{
3065 static const struct phy_reg phy_reg_init[] = {
3066 { 0x1f, 0x0005 },
3067 { 0x1a, 0x0000 },
3068 { 0x1f, 0x0000 },
3069
3070 { 0x1f, 0x0004 },
3071 { 0x1c, 0x0000 },
3072 { 0x1f, 0x0000 },
3073
3074 { 0x1f, 0x0001 },
3075 { 0x15, 0x7701 },
3076 { 0x1f, 0x0000 }
3077 };
3078
3079 /* Disable ALDPS before ram code */
3080 rtl_writephy(tp, 0x1f, 0x0000);
3081 rtl_writephy(tp, 0x18, 0x0310);
3082 msleep(100);
3083
François Romieu953a12c2011-04-24 17:38:48 +02003084 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003085
3086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3087}
3088
Francois Romieu5615d9f2007-08-17 17:50:46 +02003089static void rtl_hw_phy_config(struct net_device *dev)
3090{
3091 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003092
3093 rtl8169_print_mac_version(tp);
3094
3095 switch (tp->mac_version) {
3096 case RTL_GIGA_MAC_VER_01:
3097 break;
3098 case RTL_GIGA_MAC_VER_02:
3099 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003100 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003101 break;
3102 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003103 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003104 break;
françois romieu2e9558562009-08-10 19:44:19 +00003105 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003106 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003107 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003108 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003109 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003110 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003111 case RTL_GIGA_MAC_VER_07:
3112 case RTL_GIGA_MAC_VER_08:
3113 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003114 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003115 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003116 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003117 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003118 break;
3119 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003120 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003121 break;
3122 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003123 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003124 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003125 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003126 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003127 break;
3128 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003129 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003130 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003131 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003132 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003133 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003134 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003135 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003136 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003137 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003138 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003139 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003140 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003141 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003143 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003144 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003145 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003146 break;
3147 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003148 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003149 break;
3150 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003151 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003152 break;
françois romieue6de30d2011-01-03 15:08:37 +00003153 case RTL_GIGA_MAC_VER_28:
3154 rtl8168d_4_hw_phy_config(tp);
3155 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003156 case RTL_GIGA_MAC_VER_29:
3157 case RTL_GIGA_MAC_VER_30:
3158 rtl8105e_hw_phy_config(tp);
3159 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003160 case RTL_GIGA_MAC_VER_31:
3161 /* None. */
3162 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003163 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003164 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003165 rtl8168e_1_hw_phy_config(tp);
3166 break;
3167 case RTL_GIGA_MAC_VER_34:
3168 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003169 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003170 case RTL_GIGA_MAC_VER_35:
3171 rtl8168f_1_hw_phy_config(tp);
3172 break;
3173 case RTL_GIGA_MAC_VER_36:
3174 rtl8168f_2_hw_phy_config(tp);
3175 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003176
Francois Romieu5615d9f2007-08-17 17:50:46 +02003177 default:
3178 break;
3179 }
3180}
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182static void rtl8169_phy_timer(unsigned long __opaque)
3183{
3184 struct net_device *dev = (struct net_device *)__opaque;
3185 struct rtl8169_private *tp = netdev_priv(dev);
3186 struct timer_list *timer = &tp->timer;
3187 void __iomem *ioaddr = tp->mmio_addr;
3188 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3189
Francois Romieubcf0bf92006-07-26 23:14:13 +02003190 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 spin_lock_irq(&tp->lock);
3193
françois romieu4da19632011-01-03 15:07:55 +00003194 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003195 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 * A busy loop could burn quite a few cycles on nowadays CPU.
3197 * Let's delay the execution of the timer for a few ticks.
3198 */
3199 timeout = HZ/10;
3200 goto out_mod_timer;
3201 }
3202
3203 if (tp->link_ok(ioaddr))
3204 goto out_unlock;
3205
Joe Perchesbf82c182010-02-09 11:49:50 +00003206 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
françois romieu4da19632011-01-03 15:07:55 +00003208 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210out_mod_timer:
3211 mod_timer(timer, jiffies + timeout);
3212out_unlock:
3213 spin_unlock_irq(&tp->lock);
3214}
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216#ifdef CONFIG_NET_POLL_CONTROLLER
3217/*
3218 * Polling 'interrupt' - used by things like netconsole to send skbs
3219 * without having to re-enable interrupts. It's not called while
3220 * the interrupt routine is executing.
3221 */
3222static void rtl8169_netpoll(struct net_device *dev)
3223{
3224 struct rtl8169_private *tp = netdev_priv(dev);
3225 struct pci_dev *pdev = tp->pci_dev;
3226
3227 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003228 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 enable_irq(pdev->irq);
3230}
3231#endif
3232
3233static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3234 void __iomem *ioaddr)
3235{
3236 iounmap(ioaddr);
3237 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003238 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 pci_disable_device(pdev);
3240 free_netdev(dev);
3241}
3242
Francois Romieubf793292006-11-01 00:53:05 +01003243static void rtl8169_phy_reset(struct net_device *dev,
3244 struct rtl8169_private *tp)
3245{
Francois Romieu07d3f512007-02-21 22:40:46 +01003246 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003247
françois romieu4da19632011-01-03 15:07:55 +00003248 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003249 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003250 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003251 return;
3252 msleep(1);
3253 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003254 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003255}
3256
David S. Miller8decf862011-09-22 03:23:13 -04003257static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3258{
3259 void __iomem *ioaddr = tp->mmio_addr;
3260
3261 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3262 (RTL_R8(PHYstatus) & TBI_Enable);
3263}
3264
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003265static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003267 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003268
Francois Romieu5615d9f2007-08-17 17:50:46 +02003269 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003270
Marcus Sundberg773328942008-07-10 21:28:08 +02003271 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3272 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3273 RTL_W8(0x82, 0x01);
3274 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003275
Francois Romieu6dccd162007-02-13 23:38:05 +01003276 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3277
3278 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3279 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003280
Francois Romieubcf0bf92006-07-26 23:14:13 +02003281 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003282 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3283 RTL_W8(0x82, 0x01);
3284 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003285 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003286 }
3287
Francois Romieubf793292006-11-01 00:53:05 +01003288 rtl8169_phy_reset(dev, tp);
3289
Oliver Neukum54405cd2011-01-06 21:55:13 +01003290 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003291 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3292 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3293 (tp->mii.supports_gmii ?
3294 ADVERTISED_1000baseT_Half |
3295 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003296
David S. Miller8decf862011-09-22 03:23:13 -04003297 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003298 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003299}
3300
Francois Romieu773d2022007-01-31 23:47:43 +01003301static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3302{
3303 void __iomem *ioaddr = tp->mmio_addr;
3304 u32 high;
3305 u32 low;
3306
3307 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3308 high = addr[4] | (addr[5] << 8);
3309
3310 spin_lock_irq(&tp->lock);
3311
3312 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003313
Francois Romieu773d2022007-01-31 23:47:43 +01003314 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003315 RTL_R32(MAC4);
3316
Francois Romieu78f1cd02010-03-27 19:35:46 -07003317 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003318 RTL_R32(MAC0);
3319
françois romieuc28aa382011-08-02 03:53:43 +00003320 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3321 const struct exgmac_reg e[] = {
3322 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3323 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3324 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3325 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3326 low >> 16 },
3327 };
3328
3329 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3330 }
3331
Francois Romieu773d2022007-01-31 23:47:43 +01003332 RTL_W8(Cfg9346, Cfg9346_Lock);
3333
3334 spin_unlock_irq(&tp->lock);
3335}
3336
3337static int rtl_set_mac_address(struct net_device *dev, void *p)
3338{
3339 struct rtl8169_private *tp = netdev_priv(dev);
3340 struct sockaddr *addr = p;
3341
3342 if (!is_valid_ether_addr(addr->sa_data))
3343 return -EADDRNOTAVAIL;
3344
3345 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3346
3347 rtl_rar_set(tp, dev->dev_addr);
3348
3349 return 0;
3350}
3351
Francois Romieu5f787a12006-08-17 13:02:36 +02003352static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3353{
3354 struct rtl8169_private *tp = netdev_priv(dev);
3355 struct mii_ioctl_data *data = if_mii(ifr);
3356
Francois Romieu8b4ab282008-11-19 22:05:25 -08003357 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3358}
Francois Romieu5f787a12006-08-17 13:02:36 +02003359
Francois Romieucecb5fd2011-04-01 10:21:07 +02003360static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3361 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003362{
Francois Romieu5f787a12006-08-17 13:02:36 +02003363 switch (cmd) {
3364 case SIOCGMIIPHY:
3365 data->phy_id = 32; /* Internal PHY */
3366 return 0;
3367
3368 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003369 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003370 return 0;
3371
3372 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003373 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003374 return 0;
3375 }
3376 return -EOPNOTSUPP;
3377}
3378
Francois Romieu8b4ab282008-11-19 22:05:25 -08003379static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3380{
3381 return -EOPNOTSUPP;
3382}
3383
Francois Romieu0e485152007-02-20 00:00:26 +01003384static const struct rtl_cfg_info {
3385 void (*hw_start)(struct net_device *);
3386 unsigned int region;
3387 unsigned int align;
3388 u16 intr_event;
3389 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003390 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003391 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003392} rtl_cfg_infos [] = {
3393 [RTL_CFG_0] = {
3394 .hw_start = rtl_hw_start_8169,
3395 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003396 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003397 .intr_event = SYSErr | LinkChg | RxOverflow |
3398 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003399 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003400 .features = RTL_FEATURE_GMII,
3401 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003402 },
3403 [RTL_CFG_1] = {
3404 .hw_start = rtl_hw_start_8168,
3405 .region = 2,
3406 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003407 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003408 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003409 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003410 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3411 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003412 },
3413 [RTL_CFG_2] = {
3414 .hw_start = rtl_hw_start_8101,
3415 .region = 2,
3416 .align = 8,
3417 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3418 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003419 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003420 .features = RTL_FEATURE_MSI,
3421 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003422 }
3423};
3424
Francois Romieufbac58f2007-10-04 22:51:38 +02003425/* Cfg9346_Unlock assumed. */
3426static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3427 const struct rtl_cfg_info *cfg)
3428{
3429 unsigned msi = 0;
3430 u8 cfg2;
3431
3432 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003433 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003434 if (pci_enable_msi(pdev)) {
3435 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3436 } else {
3437 cfg2 |= MSIEnable;
3438 msi = RTL_FEATURE_MSI;
3439 }
3440 }
3441 RTL_W8(Config2, cfg2);
3442 return msi;
3443}
3444
3445static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3446{
3447 if (tp->features & RTL_FEATURE_MSI) {
3448 pci_disable_msi(pdev);
3449 tp->features &= ~RTL_FEATURE_MSI;
3450 }
3451}
3452
Francois Romieu8b4ab282008-11-19 22:05:25 -08003453static const struct net_device_ops rtl8169_netdev_ops = {
3454 .ndo_open = rtl8169_open,
3455 .ndo_stop = rtl8169_close,
3456 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003457 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003458 .ndo_tx_timeout = rtl8169_tx_timeout,
3459 .ndo_validate_addr = eth_validate_addr,
3460 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003461 .ndo_fix_features = rtl8169_fix_features,
3462 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003463 .ndo_set_mac_address = rtl_set_mac_address,
3464 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003465 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003466#ifdef CONFIG_NET_POLL_CONTROLLER
3467 .ndo_poll_controller = rtl8169_netpoll,
3468#endif
3469
3470};
3471
françois romieuc0e45c12011-01-03 15:08:04 +00003472static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3473{
3474 struct mdio_ops *ops = &tp->mdio_ops;
3475
3476 switch (tp->mac_version) {
3477 case RTL_GIGA_MAC_VER_27:
3478 ops->write = r8168dp_1_mdio_write;
3479 ops->read = r8168dp_1_mdio_read;
3480 break;
françois romieue6de30d2011-01-03 15:08:37 +00003481 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003482 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003483 ops->write = r8168dp_2_mdio_write;
3484 ops->read = r8168dp_2_mdio_read;
3485 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003486 default:
3487 ops->write = r8169_mdio_write;
3488 ops->read = r8169_mdio_read;
3489 break;
3490 }
3491}
3492
David S. Miller1805b2f2011-10-24 18:18:09 -04003493static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3494{
3495 void __iomem *ioaddr = tp->mmio_addr;
3496
3497 switch (tp->mac_version) {
3498 case RTL_GIGA_MAC_VER_29:
3499 case RTL_GIGA_MAC_VER_30:
3500 case RTL_GIGA_MAC_VER_32:
3501 case RTL_GIGA_MAC_VER_33:
3502 case RTL_GIGA_MAC_VER_34:
3503 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3504 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3505 break;
3506 default:
3507 break;
3508 }
3509}
3510
3511static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3512{
3513 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3514 return false;
3515
3516 rtl_writephy(tp, 0x1f, 0x0000);
3517 rtl_writephy(tp, MII_BMCR, 0x0000);
3518
3519 rtl_wol_suspend_quirk(tp);
3520
3521 return true;
3522}
3523
françois romieu065c27c2011-01-03 15:08:12 +00003524static void r810x_phy_power_down(struct rtl8169_private *tp)
3525{
3526 rtl_writephy(tp, 0x1f, 0x0000);
3527 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3528}
3529
3530static void r810x_phy_power_up(struct rtl8169_private *tp)
3531{
3532 rtl_writephy(tp, 0x1f, 0x0000);
3533 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3534}
3535
3536static void r810x_pll_power_down(struct rtl8169_private *tp)
3537{
David S. Miller1805b2f2011-10-24 18:18:09 -04003538 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003539 return;
françois romieu065c27c2011-01-03 15:08:12 +00003540
3541 r810x_phy_power_down(tp);
3542}
3543
3544static void r810x_pll_power_up(struct rtl8169_private *tp)
3545{
3546 r810x_phy_power_up(tp);
3547}
3548
3549static void r8168_phy_power_up(struct rtl8169_private *tp)
3550{
3551 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003552 switch (tp->mac_version) {
3553 case RTL_GIGA_MAC_VER_11:
3554 case RTL_GIGA_MAC_VER_12:
3555 case RTL_GIGA_MAC_VER_17:
3556 case RTL_GIGA_MAC_VER_18:
3557 case RTL_GIGA_MAC_VER_19:
3558 case RTL_GIGA_MAC_VER_20:
3559 case RTL_GIGA_MAC_VER_21:
3560 case RTL_GIGA_MAC_VER_22:
3561 case RTL_GIGA_MAC_VER_23:
3562 case RTL_GIGA_MAC_VER_24:
3563 case RTL_GIGA_MAC_VER_25:
3564 case RTL_GIGA_MAC_VER_26:
3565 case RTL_GIGA_MAC_VER_27:
3566 case RTL_GIGA_MAC_VER_28:
3567 case RTL_GIGA_MAC_VER_31:
3568 rtl_writephy(tp, 0x0e, 0x0000);
3569 break;
3570 default:
3571 break;
3572 }
françois romieu065c27c2011-01-03 15:08:12 +00003573 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3574}
3575
3576static void r8168_phy_power_down(struct rtl8169_private *tp)
3577{
3578 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003579 switch (tp->mac_version) {
3580 case RTL_GIGA_MAC_VER_32:
3581 case RTL_GIGA_MAC_VER_33:
3582 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3583 break;
3584
3585 case RTL_GIGA_MAC_VER_11:
3586 case RTL_GIGA_MAC_VER_12:
3587 case RTL_GIGA_MAC_VER_17:
3588 case RTL_GIGA_MAC_VER_18:
3589 case RTL_GIGA_MAC_VER_19:
3590 case RTL_GIGA_MAC_VER_20:
3591 case RTL_GIGA_MAC_VER_21:
3592 case RTL_GIGA_MAC_VER_22:
3593 case RTL_GIGA_MAC_VER_23:
3594 case RTL_GIGA_MAC_VER_24:
3595 case RTL_GIGA_MAC_VER_25:
3596 case RTL_GIGA_MAC_VER_26:
3597 case RTL_GIGA_MAC_VER_27:
3598 case RTL_GIGA_MAC_VER_28:
3599 case RTL_GIGA_MAC_VER_31:
3600 rtl_writephy(tp, 0x0e, 0x0200);
3601 default:
3602 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3603 break;
3604 }
françois romieu065c27c2011-01-03 15:08:12 +00003605}
3606
3607static void r8168_pll_power_down(struct rtl8169_private *tp)
3608{
3609 void __iomem *ioaddr = tp->mmio_addr;
3610
Francois Romieucecb5fd2011-04-01 10:21:07 +02003611 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3612 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3613 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003614 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003615 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003616 }
françois romieu065c27c2011-01-03 15:08:12 +00003617
Francois Romieucecb5fd2011-04-01 10:21:07 +02003618 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3619 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003620 (RTL_R16(CPlusCmd) & ASF)) {
3621 return;
3622 }
3623
hayeswang01dc7fe2011-03-21 01:50:28 +00003624 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3625 tp->mac_version == RTL_GIGA_MAC_VER_33)
3626 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3627
David S. Miller1805b2f2011-10-24 18:18:09 -04003628 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003629 return;
françois romieu065c27c2011-01-03 15:08:12 +00003630
3631 r8168_phy_power_down(tp);
3632
3633 switch (tp->mac_version) {
3634 case RTL_GIGA_MAC_VER_25:
3635 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003636 case RTL_GIGA_MAC_VER_27:
3637 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003638 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003639 case RTL_GIGA_MAC_VER_32:
3640 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003641 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3642 break;
3643 }
3644}
3645
3646static void r8168_pll_power_up(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
3657 switch (tp->mac_version) {
3658 case RTL_GIGA_MAC_VER_25:
3659 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003660 case RTL_GIGA_MAC_VER_27:
3661 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003662 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003663 case RTL_GIGA_MAC_VER_32:
3664 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003665 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3666 break;
3667 }
3668
3669 r8168_phy_power_up(tp);
3670}
3671
Francois Romieud58d46b2011-05-03 16:38:29 +02003672static void rtl_generic_op(struct rtl8169_private *tp,
3673 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003674{
3675 if (op)
3676 op(tp);
3677}
3678
3679static void rtl_pll_power_down(struct rtl8169_private *tp)
3680{
Francois Romieud58d46b2011-05-03 16:38:29 +02003681 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003682}
3683
3684static void rtl_pll_power_up(struct rtl8169_private *tp)
3685{
Francois Romieud58d46b2011-05-03 16:38:29 +02003686 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003687}
3688
3689static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3690{
3691 struct pll_power_ops *ops = &tp->pll_power_ops;
3692
3693 switch (tp->mac_version) {
3694 case RTL_GIGA_MAC_VER_07:
3695 case RTL_GIGA_MAC_VER_08:
3696 case RTL_GIGA_MAC_VER_09:
3697 case RTL_GIGA_MAC_VER_10:
3698 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003699 case RTL_GIGA_MAC_VER_29:
3700 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003701 ops->down = r810x_pll_power_down;
3702 ops->up = r810x_pll_power_up;
3703 break;
3704
3705 case RTL_GIGA_MAC_VER_11:
3706 case RTL_GIGA_MAC_VER_12:
3707 case RTL_GIGA_MAC_VER_17:
3708 case RTL_GIGA_MAC_VER_18:
3709 case RTL_GIGA_MAC_VER_19:
3710 case RTL_GIGA_MAC_VER_20:
3711 case RTL_GIGA_MAC_VER_21:
3712 case RTL_GIGA_MAC_VER_22:
3713 case RTL_GIGA_MAC_VER_23:
3714 case RTL_GIGA_MAC_VER_24:
3715 case RTL_GIGA_MAC_VER_25:
3716 case RTL_GIGA_MAC_VER_26:
3717 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003718 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003719 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003720 case RTL_GIGA_MAC_VER_32:
3721 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003722 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003723 case RTL_GIGA_MAC_VER_35:
3724 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003725 ops->down = r8168_pll_power_down;
3726 ops->up = r8168_pll_power_up;
3727 break;
3728
3729 default:
3730 ops->down = NULL;
3731 ops->up = NULL;
3732 break;
3733 }
3734}
3735
Hayes Wange542a222011-07-06 15:58:04 +08003736static void rtl_init_rxcfg(struct rtl8169_private *tp)
3737{
3738 void __iomem *ioaddr = tp->mmio_addr;
3739
3740 switch (tp->mac_version) {
3741 case RTL_GIGA_MAC_VER_01:
3742 case RTL_GIGA_MAC_VER_02:
3743 case RTL_GIGA_MAC_VER_03:
3744 case RTL_GIGA_MAC_VER_04:
3745 case RTL_GIGA_MAC_VER_05:
3746 case RTL_GIGA_MAC_VER_06:
3747 case RTL_GIGA_MAC_VER_10:
3748 case RTL_GIGA_MAC_VER_11:
3749 case RTL_GIGA_MAC_VER_12:
3750 case RTL_GIGA_MAC_VER_13:
3751 case RTL_GIGA_MAC_VER_14:
3752 case RTL_GIGA_MAC_VER_15:
3753 case RTL_GIGA_MAC_VER_16:
3754 case RTL_GIGA_MAC_VER_17:
3755 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3756 break;
3757 case RTL_GIGA_MAC_VER_18:
3758 case RTL_GIGA_MAC_VER_19:
3759 case RTL_GIGA_MAC_VER_20:
3760 case RTL_GIGA_MAC_VER_21:
3761 case RTL_GIGA_MAC_VER_22:
3762 case RTL_GIGA_MAC_VER_23:
3763 case RTL_GIGA_MAC_VER_24:
3764 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3765 break;
3766 default:
3767 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3768 break;
3769 }
3770}
3771
Hayes Wang92fc43b2011-07-06 15:58:03 +08003772static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3773{
3774 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3775}
3776
Francois Romieud58d46b2011-05-03 16:38:29 +02003777static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3778{
3779 rtl_generic_op(tp, tp->jumbo_ops.enable);
3780}
3781
3782static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3783{
3784 rtl_generic_op(tp, tp->jumbo_ops.disable);
3785}
3786
3787static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3788{
3789 void __iomem *ioaddr = tp->mmio_addr;
3790
3791 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3792 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3793 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3794}
3795
3796static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3797{
3798 void __iomem *ioaddr = tp->mmio_addr;
3799
3800 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3801 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3802 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3803}
3804
3805static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3806{
3807 void __iomem *ioaddr = tp->mmio_addr;
3808
3809 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3810}
3811
3812static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3813{
3814 void __iomem *ioaddr = tp->mmio_addr;
3815
3816 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3817}
3818
3819static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3820{
3821 void __iomem *ioaddr = tp->mmio_addr;
3822 struct pci_dev *pdev = tp->pci_dev;
3823
3824 RTL_W8(MaxTxPacketSize, 0x3f);
3825 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3826 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3827 pci_write_config_byte(pdev, 0x79, 0x20);
3828}
3829
3830static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3831{
3832 void __iomem *ioaddr = tp->mmio_addr;
3833 struct pci_dev *pdev = tp->pci_dev;
3834
3835 RTL_W8(MaxTxPacketSize, 0x0c);
3836 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3837 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3838 pci_write_config_byte(pdev, 0x79, 0x50);
3839}
3840
3841static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3842{
3843 rtl_tx_performance_tweak(tp->pci_dev,
3844 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3845}
3846
3847static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3848{
3849 rtl_tx_performance_tweak(tp->pci_dev,
3850 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3851}
3852
3853static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3854{
3855 void __iomem *ioaddr = tp->mmio_addr;
3856
3857 r8168b_0_hw_jumbo_enable(tp);
3858
3859 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3860}
3861
3862static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3863{
3864 void __iomem *ioaddr = tp->mmio_addr;
3865
3866 r8168b_0_hw_jumbo_disable(tp);
3867
3868 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3869}
3870
3871static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3872{
3873 struct jumbo_ops *ops = &tp->jumbo_ops;
3874
3875 switch (tp->mac_version) {
3876 case RTL_GIGA_MAC_VER_11:
3877 ops->disable = r8168b_0_hw_jumbo_disable;
3878 ops->enable = r8168b_0_hw_jumbo_enable;
3879 break;
3880 case RTL_GIGA_MAC_VER_12:
3881 case RTL_GIGA_MAC_VER_17:
3882 ops->disable = r8168b_1_hw_jumbo_disable;
3883 ops->enable = r8168b_1_hw_jumbo_enable;
3884 break;
3885 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3886 case RTL_GIGA_MAC_VER_19:
3887 case RTL_GIGA_MAC_VER_20:
3888 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3889 case RTL_GIGA_MAC_VER_22:
3890 case RTL_GIGA_MAC_VER_23:
3891 case RTL_GIGA_MAC_VER_24:
3892 case RTL_GIGA_MAC_VER_25:
3893 case RTL_GIGA_MAC_VER_26:
3894 ops->disable = r8168c_hw_jumbo_disable;
3895 ops->enable = r8168c_hw_jumbo_enable;
3896 break;
3897 case RTL_GIGA_MAC_VER_27:
3898 case RTL_GIGA_MAC_VER_28:
3899 ops->disable = r8168dp_hw_jumbo_disable;
3900 ops->enable = r8168dp_hw_jumbo_enable;
3901 break;
3902 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3903 case RTL_GIGA_MAC_VER_32:
3904 case RTL_GIGA_MAC_VER_33:
3905 case RTL_GIGA_MAC_VER_34:
3906 ops->disable = r8168e_hw_jumbo_disable;
3907 ops->enable = r8168e_hw_jumbo_enable;
3908 break;
3909
3910 /*
3911 * No action needed for jumbo frames with 8169.
3912 * No jumbo for 810x at all.
3913 */
3914 default:
3915 ops->disable = NULL;
3916 ops->enable = NULL;
3917 break;
3918 }
3919}
3920
Francois Romieu6f43adc2011-04-29 15:05:51 +02003921static void rtl_hw_reset(struct rtl8169_private *tp)
3922{
3923 void __iomem *ioaddr = tp->mmio_addr;
3924 int i;
3925
3926 /* Soft reset the chip. */
3927 RTL_W8(ChipCmd, CmdReset);
3928
3929 /* Check that the chip has finished the reset. */
3930 for (i = 0; i < 100; i++) {
3931 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3932 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003933 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003934 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003935
3936 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003937}
3938
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003939static int __devinit
3940rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3941{
Francois Romieu0e485152007-02-20 00:00:26 +01003942 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3943 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003945 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003946 struct net_device *dev;
3947 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003948 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003949 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003951 if (netif_msg_drv(&debug)) {
3952 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3953 MODULENAME, RTL8169_VERSION);
3954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003957 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003958 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003959 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003960 rc = -ENOMEM;
3961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 }
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003965 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003967 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003968 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003969 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Francois Romieuccdffb92008-07-26 14:26:06 +02003971 mii = &tp->mii;
3972 mii->dev = dev;
3973 mii->mdio_read = rtl_mdio_read;
3974 mii->mdio_write = rtl_mdio_write;
3975 mii->phy_id_mask = 0x1f;
3976 mii->reg_num_mask = 0x1f;
3977 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3978
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003979 /* disable ASPM completely as that cause random device stop working
3980 * problems as well as full system hangs for some PCIe devices users */
3981 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3982 PCIE_LINK_STATE_CLKPM);
3983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3985 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003986 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003987 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003988 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 }
3990
françois romieu87aeec72010-04-26 11:42:06 +00003991 if (pci_set_mwi(pdev) < 0)
3992 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003995 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003996 netif_err(tp, probe, dev,
3997 "region #%d not an MMIO resource, aborting\n",
3998 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004000 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004004 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004005 netif_err(tp, probe, dev,
4006 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004008 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010
4011 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004012 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004013 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004014 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
4016
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004017 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
4019 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004020 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 tp->cp_cmd |= PCIDAC;
4022 dev->features |= NETIF_F_HIGHDMA;
4023 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004024 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004026 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004027 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
4029 }
4030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004032 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004033 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004034 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004036 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004038 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Jon Masone44daad2011-06-27 07:46:31 +00004040 if (!pci_is_pcie(pdev))
4041 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004042
Hayes Wange542a222011-07-06 15:58:04 +08004043 /* Identify chip attached to board */
4044 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4045
4046 rtl_init_rxcfg(tp);
4047
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004048 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Francois Romieu6f43adc2011-04-29 15:05:51 +02004050 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004052 RTL_W16(IntrStatus, 0xffff);
4053
françois romieuca52efd2009-07-24 12:34:19 +00004054 pci_set_master(pdev);
4055
Francois Romieu7a8fc772011-03-01 17:18:33 +01004056 /*
4057 * Pretend we are using VLANs; This bypasses a nasty bug where
4058 * Interrupts stop flowing on high load on 8110SCd controllers.
4059 */
4060 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4061 tp->cp_cmd |= RxVlan;
4062
françois romieuc0e45c12011-01-03 15:08:04 +00004063 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004064 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004065 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Francois Romieu85bffe62011-04-27 08:22:39 +02004069 chipset = tp->mac_version;
4070 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Francois Romieu5d06a992006-02-23 00:47:58 +01004072 RTL_W8(Cfg9346, Cfg9346_Unlock);
4073 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4074 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004075 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4076 tp->features |= RTL_FEATURE_WOL;
4077 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4078 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02004079 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004080 RTL_W8(Cfg9346, Cfg9346_Lock);
4081
David S. Miller8decf862011-09-22 03:23:13 -04004082 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 tp->set_speed = rtl8169_set_speed_tbi;
4084 tp->get_settings = rtl8169_gset_tbi;
4085 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4086 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4087 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004088 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 } else {
4090 tp->set_speed = rtl8169_set_speed_xmii;
4091 tp->get_settings = rtl8169_gset_xmii;
4092 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4093 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4094 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004095 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 }
4097
Francois Romieudf58ef512008-10-09 14:35:58 -07004098 spin_lock_init(&tp->lock);
4099
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004100 /* Get MAC address */
Joe Perches6a3c910c2011-11-16 09:38:02 +00004101 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004103 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4107 dev->irq = pdev->irq;
4108 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004110 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Michał Mirosław350fb322011-04-08 06:35:56 +00004112 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4113 * properly for all devices */
4114 dev->features |= NETIF_F_RXCSUM |
4115 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4116
4117 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4118 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4119 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4120 NETIF_F_HIGHDMA;
4121
4122 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4123 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4124 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
4126 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01004127 tp->hw_start = cfg->hw_start;
4128 tp->intr_event = cfg->intr_event;
4129 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
David S. Miller8decf862011-09-22 03:23:13 -04004131 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4132 ~(RxBOVF | RxFOVF) : ~0;
4133
Francois Romieu2efa53f2007-03-09 00:00:05 +01004134 init_timer(&tp->timer);
4135 tp->timer.data = (unsigned long) dev;
4136 tp->timer.function = rtl8169_phy_timer;
4137
Francois Romieub6ffd972011-06-17 17:00:05 +02004138 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004141 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004142 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
4144 pci_set_drvdata(pdev, dev);
4145
Joe Perchesbf82c182010-02-09 11:49:50 +00004146 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004147 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004148 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004149 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4150 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4151 "tx checksumming: %s]\n",
4152 rtl_chip_infos[chipset].jumbo_max,
4153 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Francois Romieucecb5fd2011-04-01 10:21:07 +02004156 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4157 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4158 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004159 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004160 }
françois romieub646d902011-01-03 15:08:21 +00004161
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004162 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Alan Sternf3ec4f82010-06-08 15:23:51 -04004164 if (pci_dev_run_wake(pdev))
4165 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004166
Ivan Vecera0d672e92011-02-15 02:08:39 +00004167 netif_carrier_off(dev);
4168
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004169out:
4170 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
françois romieu87aeec72010-04-26 11:42:06 +00004172err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004173 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004174 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004175err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004176 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004177err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004178 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004179 pci_disable_device(pdev);
4180err_out_free_dev_1:
4181 free_netdev(dev);
4182 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
Francois Romieu07d3f512007-02-21 22:40:46 +01004185static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
4187 struct net_device *dev = pci_get_drvdata(pdev);
4188 struct rtl8169_private *tp = netdev_priv(dev);
4189
Francois Romieucecb5fd2011-04-01 10:21:07 +02004190 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4191 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4192 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004193 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004194 }
françois romieub646d902011-01-03 15:08:21 +00004195
Tejun Heo23f333a2010-12-12 16:45:14 +01004196 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01004197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004199
François Romieu953a12c2011-04-24 17:38:48 +02004200 rtl_release_firmware(tp);
4201
Alan Sternf3ec4f82010-06-08 15:23:51 -04004202 if (pci_dev_run_wake(pdev))
4203 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004204
Ivan Veceracc098dc2009-11-29 23:12:52 -08004205 /* restore original MAC address */
4206 rtl_rar_set(tp, dev->perm_addr);
4207
Francois Romieufbac58f2007-10-04 22:51:38 +02004208 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4210 pci_set_drvdata(pdev, NULL);
4211}
4212
Francois Romieub6ffd972011-06-17 17:00:05 +02004213static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4214{
4215 struct rtl_fw *rtl_fw;
4216 const char *name;
4217 int rc = -ENOMEM;
4218
4219 name = rtl_lookup_firmware_name(tp);
4220 if (!name)
4221 goto out_no_firmware;
4222
4223 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4224 if (!rtl_fw)
4225 goto err_warn;
4226
4227 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4228 if (rc < 0)
4229 goto err_free;
4230
Francois Romieufd112f22011-06-18 00:10:29 +02004231 rc = rtl_check_firmware(tp, rtl_fw);
4232 if (rc < 0)
4233 goto err_release_firmware;
4234
Francois Romieub6ffd972011-06-17 17:00:05 +02004235 tp->rtl_fw = rtl_fw;
4236out:
4237 return;
4238
Francois Romieufd112f22011-06-18 00:10:29 +02004239err_release_firmware:
4240 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004241err_free:
4242 kfree(rtl_fw);
4243err_warn:
4244 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4245 name, rc);
4246out_no_firmware:
4247 tp->rtl_fw = NULL;
4248 goto out;
4249}
4250
François Romieu953a12c2011-04-24 17:38:48 +02004251static void rtl_request_firmware(struct rtl8169_private *tp)
4252{
Francois Romieub6ffd972011-06-17 17:00:05 +02004253 if (IS_ERR(tp->rtl_fw))
4254 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004255}
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257static int rtl8169_open(struct net_device *dev)
4258{
4259 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004260 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004262 int retval = -ENOMEM;
4263
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004264 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Neil Hormanc0cd8842010-03-29 13:16:02 -07004266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004268 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004270 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4271 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004273 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004275 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4276 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004278 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
4280 retval = rtl8169_init_ring(dev);
4281 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004282 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
David Howellsc4028952006-11-22 14:57:56 +00004284 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Francois Romieu99f252b2007-04-02 22:59:59 +02004286 smp_mb();
4287
François Romieu953a12c2011-04-24 17:38:48 +02004288 rtl_request_firmware(tp);
4289
Francois Romieufbac58f2007-10-04 22:51:38 +02004290 retval = request_irq(dev->irq, rtl8169_interrupt,
4291 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004292 dev->name, dev);
4293 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004294 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004295
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004296 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004297
françois romieueee3a962011-01-08 02:17:26 +00004298 rtl8169_init_phy(dev, tp);
4299
Michał Mirosław350fb322011-04-08 06:35:56 +00004300 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004301
françois romieu065c27c2011-01-03 15:08:12 +00004302 rtl_pll_power_up(tp);
4303
Francois Romieu07ce4062007-02-23 23:36:39 +01004304 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004306 tp->saved_wolopts = 0;
4307 pm_runtime_put_noidle(&pdev->dev);
4308
françois romieueee3a962011-01-08 02:17:26 +00004309 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310out:
4311 return retval;
4312
François Romieu953a12c2011-04-24 17:38:48 +02004313err_release_fw_2:
4314 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004315 rtl8169_rx_clear(tp);
4316err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004317 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4318 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004319 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004320err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004321 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4322 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004323 tp->TxDescArray = NULL;
4324err_pm_runtime_put:
4325 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 goto out;
4327}
4328
Hayes Wang92fc43b2011-07-06 15:58:03 +08004329static void rtl_rx_close(struct rtl8169_private *tp)
4330{
4331 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004332
Francois Romieu1687b562011-07-19 17:21:29 +02004333 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004334}
4335
françois romieue6de30d2011-01-03 15:08:37 +00004336static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
françois romieue6de30d2011-01-03 15:08:37 +00004338 void __iomem *ioaddr = tp->mmio_addr;
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 /* Disable interrupts */
4341 rtl8169_irq_mask_and_ack(ioaddr);
4342
Hayes Wang92fc43b2011-07-06 15:58:03 +08004343 rtl_rx_close(tp);
4344
Hayes Wang5d2e1952011-02-22 17:26:22 +08004345 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004346 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4347 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004348 while (RTL_R8(TxPoll) & NPQ)
4349 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004350 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4351 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4352 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004353 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004354 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4355 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004356 } else {
4357 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4358 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004359 }
4360
Hayes Wang92fc43b2011-07-06 15:58:03 +08004361 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362}
4363
Francois Romieu7f796d832007-06-11 23:04:41 +02004364static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004365{
4366 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004367
4368 /* Set DMA burst size and Interframe Gap Time */
4369 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4370 (InterFrameGap << TxInterFrameGapShift));
4371}
4372
Francois Romieu07ce4062007-02-23 23:36:39 +01004373static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
4375 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
Francois Romieu07ce4062007-02-23 23:36:39 +01004377 tp->hw_start(dev);
4378
Francois Romieu07ce4062007-02-23 23:36:39 +01004379 netif_start_queue(dev);
4380}
4381
Francois Romieu7f796d832007-06-11 23:04:41 +02004382static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4383 void __iomem *ioaddr)
4384{
4385 /*
4386 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4387 * register to be written before TxDescAddrLow to work.
4388 * Switching from MMIO to I/O access fixes the issue as well.
4389 */
4390 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004391 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004392 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004393 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004394}
4395
4396static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4397{
4398 u16 cmd;
4399
4400 cmd = RTL_R16(CPlusCmd);
4401 RTL_W16(CPlusCmd, cmd);
4402 return cmd;
4403}
4404
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004405static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004406{
4407 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004408 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004409}
4410
Francois Romieu6dccd162007-02-13 23:38:05 +01004411static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4412{
Francois Romieu37441002011-06-17 22:58:54 +02004413 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004414 u32 mac_version;
4415 u32 clk;
4416 u32 val;
4417 } cfg2_info [] = {
4418 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4419 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4420 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4421 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004422 };
4423 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004424 unsigned int i;
4425 u32 clk;
4426
4427 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004428 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004429 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4430 RTL_W32(0x7c, p->val);
4431 break;
4432 }
4433 }
4434}
4435
Francois Romieu07ce4062007-02-23 23:36:39 +01004436static void rtl_hw_start_8169(struct net_device *dev)
4437{
4438 struct rtl8169_private *tp = netdev_priv(dev);
4439 void __iomem *ioaddr = tp->mmio_addr;
4440 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004441
Francois Romieu9cb427b2006-11-02 00:10:16 +01004442 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4443 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4444 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4445 }
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004448 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4449 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4450 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4451 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004452 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4453
Hayes Wange542a222011-07-06 15:58:04 +08004454 rtl_init_rxcfg(tp);
4455
françois romieuf0298f82011-01-03 15:07:42 +00004456 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004458 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Francois Romieucecb5fd2011-04-01 10:21:07 +02004460 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4461 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4462 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4463 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004464 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
Francois Romieu7f796d832007-06-11 23:04:41 +02004466 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004467
Francois Romieucecb5fd2011-04-01 10:21:07 +02004468 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4469 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004470 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004472 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
4474
Francois Romieubcf0bf92006-07-26 23:14:13 +02004475 RTL_W16(CPlusCmd, tp->cp_cmd);
4476
Francois Romieu6dccd162007-02-13 23:38:05 +01004477 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 /*
4480 * Undocumented corner. Supposedly:
4481 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4482 */
4483 RTL_W16(IntrMitigate, 0x0000);
4484
Francois Romieu7f796d832007-06-11 23:04:41 +02004485 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004486
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 Romieuc946b302007-10-04 00:42:50 +02004491 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4492 rtl_set_rx_tx_config_registers(tp);
4493 }
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004496
4497 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4498 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
4500 RTL_W32(RxMissed, 0);
4501
Francois Romieu07ce4062007-02-23 23:36:39 +01004502 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504 /* no early-rx interrupts */
4505 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004506
4507 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004508 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
françois romieu650e8d52011-01-03 15:08:29 +00004511static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004512{
4513 u32 csi;
4514
4515 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004516 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4517}
4518
françois romieue6de30d2011-01-03 15:08:37 +00004519static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4520{
4521 rtl_csi_access_enable(ioaddr, 0x17000000);
4522}
4523
françois romieu650e8d52011-01-03 15:08:29 +00004524static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4525{
4526 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004527}
4528
4529struct ephy_info {
4530 unsigned int offset;
4531 u16 mask;
4532 u16 bits;
4533};
4534
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004535static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004536{
4537 u16 w;
4538
4539 while (len-- > 0) {
4540 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4541 rtl_ephy_write(ioaddr, e->offset, w);
4542 e++;
4543 }
4544}
4545
Francois Romieub726e492008-06-28 12:22:59 +02004546static void rtl_disable_clock_request(struct pci_dev *pdev)
4547{
Jon Masone44daad2011-06-27 07:46:31 +00004548 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004549
4550 if (cap) {
4551 u16 ctl;
4552
4553 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4554 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4555 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4556 }
4557}
4558
françois romieue6de30d2011-01-03 15:08:37 +00004559static void rtl_enable_clock_request(struct pci_dev *pdev)
4560{
Jon Masone44daad2011-06-27 07:46:31 +00004561 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004562
4563 if (cap) {
4564 u16 ctl;
4565
4566 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4567 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4568 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4569 }
4570}
4571
Francois Romieub726e492008-06-28 12:22:59 +02004572#define R8168_CPCMD_QUIRK_MASK (\
4573 EnableBist | \
4574 Mac_dbgo_oe | \
4575 Force_half_dup | \
4576 Force_rxflow_en | \
4577 Force_txflow_en | \
4578 Cxpl_dbg_sel | \
4579 ASF | \
4580 PktCntrDisable | \
4581 Mac_dbgo_sel)
4582
Francois Romieu219a1e92008-06-28 11:58:39 +02004583static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4584{
Francois Romieub726e492008-06-28 12:22:59 +02004585 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4586
4587 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4588
Francois Romieu2e68ae42008-06-28 12:00:55 +02004589 rtl_tx_performance_tweak(pdev,
4590 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004591}
4592
4593static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4594{
4595 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004596
françois romieuf0298f82011-01-03 15:07:42 +00004597 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004598
4599 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004600}
4601
4602static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4603{
Francois Romieub726e492008-06-28 12:22:59 +02004604 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4605
4606 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4607
Francois Romieu219a1e92008-06-28 11:58:39 +02004608 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004609
4610 rtl_disable_clock_request(pdev);
4611
4612 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004613}
4614
Francois Romieuef3386f2008-06-29 12:24:30 +02004615static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004616{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004617 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004618 { 0x01, 0, 0x0001 },
4619 { 0x02, 0x0800, 0x1000 },
4620 { 0x03, 0, 0x0042 },
4621 { 0x06, 0x0080, 0x0000 },
4622 { 0x07, 0, 0x2000 }
4623 };
4624
françois romieu650e8d52011-01-03 15:08:29 +00004625 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004626
4627 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4628
Francois Romieu219a1e92008-06-28 11:58:39 +02004629 __rtl_hw_start_8168cp(ioaddr, pdev);
4630}
4631
Francois Romieuef3386f2008-06-29 12:24:30 +02004632static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4633{
françois romieu650e8d52011-01-03 15:08:29 +00004634 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004635
4636 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4637
4638 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4639
4640 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4641}
4642
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004643static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4644{
françois romieu650e8d52011-01-03 15:08:29 +00004645 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004646
4647 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4648
4649 /* Magic. */
4650 RTL_W8(DBG_REG, 0x20);
4651
françois romieuf0298f82011-01-03 15:07:42 +00004652 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004653
4654 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4655
4656 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4657}
4658
Francois Romieu219a1e92008-06-28 11:58:39 +02004659static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4660{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004661 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004662 { 0x02, 0x0800, 0x1000 },
4663 { 0x03, 0, 0x0002 },
4664 { 0x06, 0x0080, 0x0000 }
4665 };
4666
françois romieu650e8d52011-01-03 15:08:29 +00004667 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004668
4669 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4670
4671 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4672
Francois Romieu219a1e92008-06-28 11:58:39 +02004673 __rtl_hw_start_8168cp(ioaddr, pdev);
4674}
4675
4676static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4677{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004678 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004679 { 0x01, 0, 0x0001 },
4680 { 0x03, 0x0400, 0x0220 }
4681 };
4682
françois romieu650e8d52011-01-03 15:08:29 +00004683 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004684
4685 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4686
Francois Romieu219a1e92008-06-28 11:58:39 +02004687 __rtl_hw_start_8168cp(ioaddr, pdev);
4688}
4689
Francois Romieu197ff762008-06-28 13:16:02 +02004690static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4691{
4692 rtl_hw_start_8168c_2(ioaddr, pdev);
4693}
4694
Francois Romieu6fb07052008-06-29 11:54:28 +02004695static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4696{
françois romieu650e8d52011-01-03 15:08:29 +00004697 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004698
4699 __rtl_hw_start_8168cp(ioaddr, pdev);
4700}
4701
Francois Romieu5b538df2008-07-20 16:22:45 +02004702static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4703{
françois romieu650e8d52011-01-03 15:08:29 +00004704 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004705
4706 rtl_disable_clock_request(pdev);
4707
françois romieuf0298f82011-01-03 15:07:42 +00004708 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004709
4710 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4711
4712 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4713}
4714
hayeswang4804b3b2011-03-21 01:50:29 +00004715static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4716{
4717 rtl_csi_access_enable_1(ioaddr);
4718
4719 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4720
4721 RTL_W8(MaxTxPacketSize, TxPacketMax);
4722
4723 rtl_disable_clock_request(pdev);
4724}
4725
françois romieue6de30d2011-01-03 15:08:37 +00004726static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4727{
4728 static const struct ephy_info e_info_8168d_4[] = {
4729 { 0x0b, ~0, 0x48 },
4730 { 0x19, 0x20, 0x50 },
4731 { 0x0c, ~0, 0x20 }
4732 };
4733 int i;
4734
4735 rtl_csi_access_enable_1(ioaddr);
4736
4737 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4738
4739 RTL_W8(MaxTxPacketSize, TxPacketMax);
4740
4741 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4742 const struct ephy_info *e = e_info_8168d_4 + i;
4743 u16 w;
4744
4745 w = rtl_ephy_read(ioaddr, e->offset);
4746 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4747 }
4748
4749 rtl_enable_clock_request(pdev);
4750}
4751
Hayes Wang70090422011-07-06 15:58:06 +08004752static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004753{
Hayes Wang70090422011-07-06 15:58:06 +08004754 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004755 { 0x00, 0x0200, 0x0100 },
4756 { 0x00, 0x0000, 0x0004 },
4757 { 0x06, 0x0002, 0x0001 },
4758 { 0x06, 0x0000, 0x0030 },
4759 { 0x07, 0x0000, 0x2000 },
4760 { 0x00, 0x0000, 0x0020 },
4761 { 0x03, 0x5800, 0x2000 },
4762 { 0x03, 0x0000, 0x0001 },
4763 { 0x01, 0x0800, 0x1000 },
4764 { 0x07, 0x0000, 0x4000 },
4765 { 0x1e, 0x0000, 0x2000 },
4766 { 0x19, 0xffff, 0xfe6c },
4767 { 0x0a, 0x0000, 0x0040 }
4768 };
4769
4770 rtl_csi_access_enable_2(ioaddr);
4771
Hayes Wang70090422011-07-06 15:58:06 +08004772 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004773
4774 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4775
4776 RTL_W8(MaxTxPacketSize, TxPacketMax);
4777
4778 rtl_disable_clock_request(pdev);
4779
4780 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004781 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4782 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004783
Francois Romieucecb5fd2011-04-01 10:21:07 +02004784 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004785}
4786
Hayes Wang70090422011-07-06 15:58:06 +08004787static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4788{
4789 static const struct ephy_info e_info_8168e_2[] = {
4790 { 0x09, 0x0000, 0x0080 },
4791 { 0x19, 0x0000, 0x0224 }
4792 };
4793
4794 rtl_csi_access_enable_1(ioaddr);
4795
4796 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4797
4798 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4799
4800 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4801 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4802 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4803 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4804 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4805 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4806 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4807 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4808 ERIAR_EXGMAC);
4809
Hayes Wang3090bd92011-09-06 16:55:15 +08004810 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004811
4812 rtl_disable_clock_request(pdev);
4813
4814 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4815 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4816
4817 /* Adjust EEE LED frequency */
4818 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4819
4820 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4821 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4822 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4823}
4824
Hayes Wangc2218922011-09-06 16:55:18 +08004825static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4826{
4827 static const struct ephy_info e_info_8168f_1[] = {
4828 { 0x06, 0x00c0, 0x0020 },
4829 { 0x08, 0x0001, 0x0002 },
4830 { 0x09, 0x0000, 0x0080 },
4831 { 0x19, 0x0000, 0x0224 }
4832 };
4833
4834 rtl_csi_access_enable_1(ioaddr);
4835
4836 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4837
4838 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4839
4840 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4841 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4842 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4843 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4844 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4845 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4846 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4847 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4848 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4849 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4850 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4851 ERIAR_EXGMAC);
4852
4853 RTL_W8(MaxTxPacketSize, EarlySize);
4854
4855 rtl_disable_clock_request(pdev);
4856
4857 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4858 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4859
4860 /* Adjust EEE LED frequency */
4861 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4862
4863 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4864 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4865 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4866}
4867
Francois Romieu07ce4062007-02-23 23:36:39 +01004868static void rtl_hw_start_8168(struct net_device *dev)
4869{
Francois Romieu2dd99532007-06-11 23:22:52 +02004870 struct rtl8169_private *tp = netdev_priv(dev);
4871 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004872 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004873
4874 RTL_W8(Cfg9346, Cfg9346_Unlock);
4875
françois romieuf0298f82011-01-03 15:07:42 +00004876 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004877
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004878 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004879
Francois Romieu0e485152007-02-20 00:00:26 +01004880 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004881
4882 RTL_W16(CPlusCmd, tp->cp_cmd);
4883
Francois Romieu0e485152007-02-20 00:00:26 +01004884 RTL_W16(IntrMitigate, 0x5151);
4885
4886 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004887 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4888 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004889 tp->intr_event |= RxFIFOOver | PCSTimeout;
4890 tp->intr_event &= ~RxOverflow;
4891 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004892
4893 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4894
Francois Romieub8363902008-06-01 12:31:57 +02004895 rtl_set_rx_mode(dev);
4896
4897 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4898 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004899
4900 RTL_R8(IntrMask);
4901
Francois Romieu219a1e92008-06-28 11:58:39 +02004902 switch (tp->mac_version) {
4903 case RTL_GIGA_MAC_VER_11:
4904 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004905 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004906
4907 case RTL_GIGA_MAC_VER_12:
4908 case RTL_GIGA_MAC_VER_17:
4909 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004910 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004911
4912 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004913 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004914 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004915
4916 case RTL_GIGA_MAC_VER_19:
4917 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004918 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004919
4920 case RTL_GIGA_MAC_VER_20:
4921 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004922 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004923
Francois Romieu197ff762008-06-28 13:16:02 +02004924 case RTL_GIGA_MAC_VER_21:
4925 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004926 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004927
Francois Romieu6fb07052008-06-29 11:54:28 +02004928 case RTL_GIGA_MAC_VER_22:
4929 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004930 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004931
Francois Romieuef3386f2008-06-29 12:24:30 +02004932 case RTL_GIGA_MAC_VER_23:
4933 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004934 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004935
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004936 case RTL_GIGA_MAC_VER_24:
4937 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004938 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004939
Francois Romieu5b538df2008-07-20 16:22:45 +02004940 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004941 case RTL_GIGA_MAC_VER_26:
4942 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004943 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004944 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004945
françois romieue6de30d2011-01-03 15:08:37 +00004946 case RTL_GIGA_MAC_VER_28:
4947 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004948 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004949
hayeswang4804b3b2011-03-21 01:50:29 +00004950 case RTL_GIGA_MAC_VER_31:
4951 rtl_hw_start_8168dp(ioaddr, pdev);
4952 break;
4953
hayeswang01dc7fe2011-03-21 01:50:28 +00004954 case RTL_GIGA_MAC_VER_32:
4955 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004956 rtl_hw_start_8168e_1(ioaddr, pdev);
4957 break;
4958 case RTL_GIGA_MAC_VER_34:
4959 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004960 break;
françois romieue6de30d2011-01-03 15:08:37 +00004961
Hayes Wangc2218922011-09-06 16:55:18 +08004962 case RTL_GIGA_MAC_VER_35:
4963 case RTL_GIGA_MAC_VER_36:
4964 rtl_hw_start_8168f_1(ioaddr, pdev);
4965 break;
4966
Francois Romieu219a1e92008-06-28 11:58:39 +02004967 default:
4968 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4969 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004970 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004971 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004972
Francois Romieu0e485152007-02-20 00:00:26 +01004973 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4974
Francois Romieub8363902008-06-01 12:31:57 +02004975 RTL_W8(Cfg9346, Cfg9346_Lock);
4976
Francois Romieu2dd99532007-06-11 23:22:52 +02004977 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004978
Francois Romieu0e485152007-02-20 00:00:26 +01004979 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004980}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
Francois Romieu2857ffb2008-08-02 21:08:49 +02004982#define R810X_CPCMD_QUIRK_MASK (\
4983 EnableBist | \
4984 Mac_dbgo_oe | \
4985 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004986 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004987 Force_txflow_en | \
4988 Cxpl_dbg_sel | \
4989 ASF | \
4990 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004991 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004992
4993static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4994{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004995 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004996 { 0x01, 0, 0x6e65 },
4997 { 0x02, 0, 0x091f },
4998 { 0x03, 0, 0xc2f9 },
4999 { 0x06, 0, 0xafb5 },
5000 { 0x07, 0, 0x0e00 },
5001 { 0x19, 0, 0xec80 },
5002 { 0x01, 0, 0x2e65 },
5003 { 0x01, 0, 0x6e65 }
5004 };
5005 u8 cfg1;
5006
françois romieu650e8d52011-01-03 15:08:29 +00005007 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005008
5009 RTL_W8(DBG_REG, FIX_NAK_1);
5010
5011 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5012
5013 RTL_W8(Config1,
5014 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5015 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5016
5017 cfg1 = RTL_R8(Config1);
5018 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5019 RTL_W8(Config1, cfg1 & ~LEDS0);
5020
Francois Romieu2857ffb2008-08-02 21:08:49 +02005021 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5022}
5023
5024static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5025{
françois romieu650e8d52011-01-03 15:08:29 +00005026 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005027
5028 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5029
5030 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5031 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005032}
5033
5034static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5035{
5036 rtl_hw_start_8102e_2(ioaddr, pdev);
5037
5038 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5039}
5040
Hayes Wang5a5e4442011-02-22 17:26:21 +08005041static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5042{
5043 static const struct ephy_info e_info_8105e_1[] = {
5044 { 0x07, 0, 0x4000 },
5045 { 0x19, 0, 0x0200 },
5046 { 0x19, 0, 0x0020 },
5047 { 0x1e, 0, 0x2000 },
5048 { 0x03, 0, 0x0001 },
5049 { 0x19, 0, 0x0100 },
5050 { 0x19, 0, 0x0004 },
5051 { 0x0a, 0, 0x0020 }
5052 };
5053
Francois Romieucecb5fd2011-04-01 10:21:07 +02005054 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005055 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5056
Francois Romieucecb5fd2011-04-01 10:21:07 +02005057 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005058 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5059
5060 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005061 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005062
5063 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5064}
5065
5066static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5067{
5068 rtl_hw_start_8105e_1(ioaddr, pdev);
5069 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5070}
5071
Francois Romieu07ce4062007-02-23 23:36:39 +01005072static void rtl_hw_start_8101(struct net_device *dev)
5073{
Francois Romieucdf1a602007-06-11 23:29:50 +02005074 struct rtl8169_private *tp = netdev_priv(dev);
5075 void __iomem *ioaddr = tp->mmio_addr;
5076 struct pci_dev *pdev = tp->pci_dev;
5077
Francois Romieucecb5fd2011-04-01 10:21:07 +02005078 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5079 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005080 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005081
5082 if (cap) {
5083 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5084 PCI_EXP_DEVCTL_NOSNOOP_EN);
5085 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005086 }
5087
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005088 RTL_W8(Cfg9346, Cfg9346_Unlock);
5089
Francois Romieu2857ffb2008-08-02 21:08:49 +02005090 switch (tp->mac_version) {
5091 case RTL_GIGA_MAC_VER_07:
5092 rtl_hw_start_8102e_1(ioaddr, pdev);
5093 break;
5094
5095 case RTL_GIGA_MAC_VER_08:
5096 rtl_hw_start_8102e_3(ioaddr, pdev);
5097 break;
5098
5099 case RTL_GIGA_MAC_VER_09:
5100 rtl_hw_start_8102e_2(ioaddr, pdev);
5101 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005102
5103 case RTL_GIGA_MAC_VER_29:
5104 rtl_hw_start_8105e_1(ioaddr, pdev);
5105 break;
5106 case RTL_GIGA_MAC_VER_30:
5107 rtl_hw_start_8105e_2(ioaddr, pdev);
5108 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005109 }
5110
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005111 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005112
françois romieuf0298f82011-01-03 15:07:42 +00005113 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005114
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005115 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005116
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005117 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005118 RTL_W16(CPlusCmd, tp->cp_cmd);
5119
5120 RTL_W16(IntrMitigate, 0x0000);
5121
5122 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5123
5124 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5125 rtl_set_rx_tx_config_registers(tp);
5126
Francois Romieucdf1a602007-06-11 23:29:50 +02005127 RTL_R8(IntrMask);
5128
Francois Romieucdf1a602007-06-11 23:29:50 +02005129 rtl_set_rx_mode(dev);
5130
5131 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01005132
Francois Romieu0e485152007-02-20 00:00:26 +01005133 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134}
5135
5136static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5137{
Francois Romieud58d46b2011-05-03 16:38:29 +02005138 struct rtl8169_private *tp = netdev_priv(dev);
5139
5140 if (new_mtu < ETH_ZLEN ||
5141 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return -EINVAL;
5143
Francois Romieud58d46b2011-05-03 16:38:29 +02005144 if (new_mtu > ETH_DATA_LEN)
5145 rtl_hw_jumbo_enable(tp);
5146 else
5147 rtl_hw_jumbo_disable(tp);
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005150 netdev_update_features(dev);
5151
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153}
5154
5155static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5156{
Al Viro95e09182007-12-22 18:55:39 +00005157 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5159}
5160
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005161static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5162 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005164 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005165 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005166
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005167 kfree(*data_buff);
5168 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 rtl8169_make_unusable_by_asic(desc);
5170}
5171
5172static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5173{
5174 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5175
5176 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5177}
5178
5179static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5180 u32 rx_buf_sz)
5181{
5182 desc->addr = cpu_to_le64(mapping);
5183 wmb();
5184 rtl8169_mark_to_asic(desc, rx_buf_sz);
5185}
5186
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005187static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005189 return (void *)ALIGN((long)data, 16);
5190}
5191
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005192static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5193 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005194{
5195 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005197 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005198 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005199 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005201 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5202 if (!data)
5203 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005204
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005205 if (rtl8169_align(data) != data) {
5206 kfree(data);
5207 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5208 if (!data)
5209 return NULL;
5210 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005211
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005212 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005213 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005214 if (unlikely(dma_mapping_error(d, mapping))) {
5215 if (net_ratelimit())
5216 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005217 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
5220 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005221 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005222
5223err_out:
5224 kfree(data);
5225 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226}
5227
5228static void rtl8169_rx_clear(struct rtl8169_private *tp)
5229{
Francois Romieu07d3f512007-02-21 22:40:46 +01005230 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
5232 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005233 if (tp->Rx_databuff[i]) {
5234 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 tp->RxDescArray + i);
5236 }
5237 }
5238}
5239
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005240static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005242 desc->opts1 |= cpu_to_le32(RingEnd);
5243}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005244
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005245static int rtl8169_rx_fill(struct rtl8169_private *tp)
5246{
5247 unsigned int i;
5248
5249 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005250 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005251
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005252 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005254
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005255 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005256 if (!data) {
5257 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005258 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005259 }
5260 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005263 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5264 return 0;
5265
5266err_out:
5267 rtl8169_rx_clear(tp);
5268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271static int rtl8169_init_ring(struct net_device *dev)
5272{
5273 struct rtl8169_private *tp = netdev_priv(dev);
5274
5275 rtl8169_init_ring_indexes(tp);
5276
5277 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005278 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005280 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281}
5282
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005283static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 struct TxDesc *desc)
5285{
5286 unsigned int len = tx_skb->len;
5287
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005288 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5289
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 desc->opts1 = 0x00;
5291 desc->opts2 = 0x00;
5292 desc->addr = 0x00;
5293 tx_skb->len = 0;
5294}
5295
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005296static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5297 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
5299 unsigned int i;
5300
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005301 for (i = 0; i < n; i++) {
5302 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 struct ring_info *tx_skb = tp->tx_skb + entry;
5304 unsigned int len = tx_skb->len;
5305
5306 if (len) {
5307 struct sk_buff *skb = tx_skb->skb;
5308
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005309 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 tp->TxDescArray + entry);
5311 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005312 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 dev_kfree_skb(skb);
5314 tx_skb->skb = NULL;
5315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 }
5317 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005318}
5319
5320static void rtl8169_tx_clear(struct rtl8169_private *tp)
5321{
5322 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 tp->cur_tx = tp->dirty_tx = 0;
5324}
5325
David Howellsc4028952006-11-22 14:57:56 +00005326static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
5328 struct rtl8169_private *tp = netdev_priv(dev);
5329
David Howellsc4028952006-11-22 14:57:56 +00005330 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 schedule_delayed_work(&tp->task, 4);
5332}
5333
5334static void rtl8169_wait_for_quiescence(struct net_device *dev)
5335{
5336 struct rtl8169_private *tp = netdev_priv(dev);
5337 void __iomem *ioaddr = tp->mmio_addr;
5338
5339 synchronize_irq(dev->irq);
5340
5341 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005342 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 rtl8169_irq_mask_and_ack(ioaddr);
5345
David S. Millerd1d08d12008-01-07 20:53:33 -08005346 tp->intr_mask = 0xffff;
5347 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005348 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
David Howellsc4028952006-11-22 14:57:56 +00005351static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352{
David Howellsc4028952006-11-22 14:57:56 +00005353 struct rtl8169_private *tp =
5354 container_of(work, struct rtl8169_private, task.work);
5355 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 int ret;
5357
Francois Romieueb2a0212007-02-15 23:37:21 +01005358 rtnl_lock();
5359
5360 if (!netif_running(dev))
5361 goto out_unlock;
5362
5363 rtl8169_wait_for_quiescence(dev);
5364 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
5366 ret = rtl8169_open(dev);
5367 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005368 if (net_ratelimit())
5369 netif_err(tp, drv, dev,
5370 "reinit failure (status = %d). Rescheduling\n",
5371 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5373 }
Francois Romieueb2a0212007-02-15 23:37:21 +01005374
5375out_unlock:
5376 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377}
5378
David Howellsc4028952006-11-22 14:57:56 +00005379static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
David Howellsc4028952006-11-22 14:57:56 +00005381 struct rtl8169_private *tp =
5382 container_of(work, struct rtl8169_private, task.work);
5383 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005384 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Francois Romieueb2a0212007-02-15 23:37:21 +01005386 rtnl_lock();
5387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005389 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
5391 rtl8169_wait_for_quiescence(dev);
5392
Francois Romieu56de4142011-03-15 17:29:31 +01005393 for (i = 0; i < NUM_RX_DESC; i++)
5394 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 rtl8169_tx_clear(tp);
5397
Hayes Wang92fc43b2011-07-06 15:58:03 +08005398 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005399 rtl_hw_start(dev);
5400 netif_wake_queue(dev);
5401 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005402
5403out_unlock:
5404 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
5407static void rtl8169_tx_timeout(struct net_device *dev)
5408{
5409 struct rtl8169_private *tp = netdev_priv(dev);
5410
françois romieue6de30d2011-01-03 15:08:37 +00005411 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
5413 /* Let's wait a bit while any (async) irq lands on */
5414 rtl8169_schedule_work(dev, rtl8169_reset_task);
5415}
5416
5417static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005418 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
5420 struct skb_shared_info *info = skb_shinfo(skb);
5421 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005422 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005423 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425 entry = tp->cur_tx;
5426 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005427 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 dma_addr_t mapping;
5429 u32 status, len;
5430 void *addr;
5431
5432 entry = (entry + 1) % NUM_TX_DESC;
5433
5434 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005435 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005436 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005437 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005438 if (unlikely(dma_mapping_error(d, mapping))) {
5439 if (net_ratelimit())
5440 netif_err(tp, drv, tp->dev,
5441 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005442 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Francois Romieucecb5fd2011-04-01 10:21:07 +02005445 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005446 status = opts[0] | len |
5447 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005450 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 txd->addr = cpu_to_le64(mapping);
5452
5453 tp->tx_skb[entry].len = len;
5454 }
5455
5456 if (cur_frag) {
5457 tp->tx_skb[entry].skb = skb;
5458 txd->opts1 |= cpu_to_le32(LastFrag);
5459 }
5460
5461 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005462
5463err_out:
5464 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5465 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
5467
Francois Romieu2b7b4312011-04-18 22:53:24 -07005468static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5469 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005471 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005472 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005473 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Francois Romieu2b7b4312011-04-18 22:53:24 -07005475 if (mss) {
5476 opts[0] |= TD_LSO;
5477 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5478 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005479 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005482 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005484 opts[offset] |= info->checksum.udp;
5485 else
5486 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488}
5489
Stephen Hemminger613573252009-08-31 19:50:58 +00005490static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5491 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492{
5493 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005494 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 struct TxDesc *txd = tp->TxDescArray + entry;
5496 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005497 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 dma_addr_t mapping;
5499 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005500 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005501 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005504 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005505 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 }
5507
5508 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005509 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005511 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005512 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005513 if (unlikely(dma_mapping_error(d, mapping))) {
5514 if (net_ratelimit())
5515 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005516 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
5519 tp->tx_skb[entry].len = len;
5520 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
Francois Romieu2b7b4312011-04-18 22:53:24 -07005522 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5523 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005524
Francois Romieu2b7b4312011-04-18 22:53:24 -07005525 rtl8169_tso_csum(tp, skb, opts);
5526
5527 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005528 if (frags < 0)
5529 goto err_dma_1;
5530 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005531 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005532 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005533 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005534 tp->tx_skb[entry].skb = skb;
5535 }
5536
Francois Romieu2b7b4312011-04-18 22:53:24 -07005537 txd->opts2 = cpu_to_le32(opts[1]);
5538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 wmb();
5540
Francois Romieucecb5fd2011-04-01 10:21:07 +02005541 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005542 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 txd->opts1 = cpu_to_le32(status);
5544
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 tp->cur_tx += frags + 1;
5546
David Dillow4c020a92010-03-03 16:33:10 +00005547 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Francois Romieucecb5fd2011-04-01 10:21:07 +02005549 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
5551 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5552 netif_stop_queue(dev);
5553 smp_rmb();
5554 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5555 netif_wake_queue(dev);
5556 }
5557
Stephen Hemminger613573252009-08-31 19:50:58 +00005558 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005560err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005561 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005562err_dma_0:
5563 dev_kfree_skb(skb);
5564 dev->stats.tx_dropped++;
5565 return NETDEV_TX_OK;
5566
5567err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005569 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005570 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571}
5572
5573static void rtl8169_pcierr_interrupt(struct net_device *dev)
5574{
5575 struct rtl8169_private *tp = netdev_priv(dev);
5576 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 u16 pci_status, pci_cmd;
5578
5579 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5580 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5581
Joe Perchesbf82c182010-02-09 11:49:50 +00005582 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5583 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
5585 /*
5586 * The recovery sequence below admits a very elaborated explanation:
5587 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005588 * - I did not see what else could be done;
5589 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 *
5591 * Feel free to adjust to your needs.
5592 */
Francois Romieua27993f2006-12-18 00:04:19 +01005593 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005594 pci_cmd &= ~PCI_COMMAND_PARITY;
5595 else
5596 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5597
5598 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
5600 pci_write_config_word(pdev, PCI_STATUS,
5601 pci_status & (PCI_STATUS_DETECTED_PARITY |
5602 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5603 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5604
5605 /* The infamous DAC f*ckup only happens at boot time */
5606 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005607 void __iomem *ioaddr = tp->mmio_addr;
5608
Joe Perchesbf82c182010-02-09 11:49:50 +00005609 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 tp->cp_cmd &= ~PCIDAC;
5611 RTL_W16(CPlusCmd, tp->cp_cmd);
5612 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 }
5614
françois romieue6de30d2011-01-03 15:08:37 +00005615 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005616
5617 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618}
5619
Francois Romieu07d3f512007-02-21 22:40:46 +01005620static void rtl8169_tx_interrupt(struct net_device *dev,
5621 struct rtl8169_private *tp,
5622 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623{
5624 unsigned int dirty_tx, tx_left;
5625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 dirty_tx = tp->dirty_tx;
5627 smp_rmb();
5628 tx_left = tp->cur_tx - dirty_tx;
5629
5630 while (tx_left > 0) {
5631 unsigned int entry = dirty_tx % NUM_TX_DESC;
5632 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 u32 status;
5634
5635 rmb();
5636 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5637 if (status & DescOwn)
5638 break;
5639
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005640 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5641 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005643 dev->stats.tx_packets++;
5644 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005645 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 tx_skb->skb = NULL;
5647 }
5648 dirty_tx++;
5649 tx_left--;
5650 }
5651
5652 if (tp->dirty_tx != dirty_tx) {
5653 tp->dirty_tx = dirty_tx;
5654 smp_wmb();
5655 if (netif_queue_stopped(dev) &&
5656 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5657 netif_wake_queue(dev);
5658 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005659 /*
5660 * 8168 hack: TxPoll requests are lost when the Tx packets are
5661 * too close. Let's kick an extra TxPoll request when a burst
5662 * of start_xmit activity is detected (if it is not detected,
5663 * it is slow enough). -- FR
5664 */
5665 smp_rmb();
5666 if (tp->cur_tx != dirty_tx)
5667 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
5669}
5670
Francois Romieu126fa4b2005-05-12 20:09:17 -04005671static inline int rtl8169_fragmented_frame(u32 status)
5672{
5673 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5674}
5675
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005676static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 u32 status = opts1 & RxProtoMask;
5679
5680 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005681 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 skb->ip_summed = CHECKSUM_UNNECESSARY;
5683 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005684 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685}
5686
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005687static struct sk_buff *rtl8169_try_rx_copy(void *data,
5688 struct rtl8169_private *tp,
5689 int pkt_size,
5690 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005692 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005693 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005695 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005696 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005697 prefetch(data);
5698 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5699 if (skb)
5700 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005701 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5702
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005703 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
Francois Romieu07d3f512007-02-21 22:40:46 +01005706static int rtl8169_rx_interrupt(struct net_device *dev,
5707 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005708 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
5710 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005711 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 cur_rx = tp->cur_rx;
5714 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005715 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005717 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005719 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 u32 status;
5721
5722 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005723 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
5725 if (status & DescOwn)
5726 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005727 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005728 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5729 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005730 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005732 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005734 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005735 if (status & RxFOVF) {
5736 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005737 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005738 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005739 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005741 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005742 dma_addr_t addr = le64_to_cpu(desc->addr);
Francois Romieudeb9d93c2011-07-12 08:24:28 +02005743 int pkt_size = (status & 0x00003fff) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
Francois Romieu126fa4b2005-05-12 20:09:17 -04005745 /*
5746 * The driver does not support incoming fragmented
5747 * frames. They are seen as a symptom of over-mtu
5748 * sized frames.
5749 */
5750 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005751 dev->stats.rx_dropped++;
5752 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005753 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005754 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005755 }
5756
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005757 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5758 tp, pkt_size, addr);
5759 rtl8169_mark_to_asic(desc, rx_buf_sz);
5760 if (!skb) {
5761 dev->stats.rx_dropped++;
5762 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 }
5764
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005765 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 skb_put(skb, pkt_size);
5767 skb->protocol = eth_type_trans(skb, dev);
5768
Francois Romieu7a8fc772011-03-01 17:18:33 +01005769 rtl8169_rx_vlan_tag(desc, skb);
5770
Francois Romieu56de4142011-03-15 17:29:31 +01005771 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772
Francois Romieucebf8cc2007-10-18 12:06:54 +02005773 dev->stats.rx_bytes += pkt_size;
5774 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005776
5777 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005778 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005779 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5780 desc->opts2 = 0;
5781 cur_rx++;
5782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 }
5784
5785 count = cur_rx - tp->cur_rx;
5786 tp->cur_rx = cur_rx;
5787
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005788 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
5790 return count;
5791}
5792
Francois Romieu07d3f512007-02-21 22:40:46 +01005793static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
Francois Romieu07d3f512007-02-21 22:40:46 +01005795 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005799 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
David Dillowf11a3772009-05-22 15:29:34 +00005801 /* loop handling interrupts until we have no new ones or
5802 * we hit a invalid/hotplug case.
5803 */
Francois Romieu865c6522008-05-11 14:51:00 +02005804 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005805 while (status && status != 0xffff) {
5806 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
David Dillowf11a3772009-05-22 15:29:34 +00005808 /* Handle all of the error cases first. These will reset
5809 * the chip, so just exit the loop.
5810 */
5811 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005812 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 }
David Dillowf11a3772009-05-22 15:29:34 +00005815
Francois Romieu1519e572011-02-03 12:02:36 +01005816 if (unlikely(status & RxFIFOOver)) {
5817 switch (tp->mac_version) {
5818 /* Work around for rx fifo overflow */
5819 case RTL_GIGA_MAC_VER_11:
5820 case RTL_GIGA_MAC_VER_22:
5821 case RTL_GIGA_MAC_VER_26:
5822 netif_stop_queue(dev);
5823 rtl8169_tx_timeout(dev);
5824 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005825 /* Testers needed. */
5826 case RTL_GIGA_MAC_VER_17:
5827 case RTL_GIGA_MAC_VER_19:
5828 case RTL_GIGA_MAC_VER_20:
5829 case RTL_GIGA_MAC_VER_21:
5830 case RTL_GIGA_MAC_VER_23:
5831 case RTL_GIGA_MAC_VER_24:
5832 case RTL_GIGA_MAC_VER_27:
5833 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005834 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005835 /* Experimental science. Pktgen proof. */
5836 case RTL_GIGA_MAC_VER_12:
5837 case RTL_GIGA_MAC_VER_25:
5838 if (status == RxFIFOOver)
5839 goto done;
5840 break;
5841 default:
5842 break;
5843 }
David Dillowf11a3772009-05-22 15:29:34 +00005844 }
5845
5846 if (unlikely(status & SYSErr)) {
5847 rtl8169_pcierr_interrupt(dev);
5848 break;
5849 }
5850
5851 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005852 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005853
5854 /* We need to see the lastest version of tp->intr_mask to
5855 * avoid ignoring an MSI interrupt and having to wait for
5856 * another event which may never come.
5857 */
5858 smp_rmb();
5859 if (status & tp->intr_mask & tp->napi_event) {
5860 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5861 tp->intr_mask = ~tp->napi_event;
5862
5863 if (likely(napi_schedule_prep(&tp->napi)))
5864 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005865 else
5866 netif_info(tp, intr, dev,
5867 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005868 }
5869
5870 /* We only get a new MSI interrupt when all active irq
5871 * sources on the chip have been acknowledged. So, ack
5872 * everything we've seen and check if new sources have become
5873 * active to avoid blocking all interrupts from the chip.
5874 */
5875 RTL_W16(IntrStatus,
5876 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5877 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
Francois Romieu1519e572011-02-03 12:02:36 +01005879done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 return IRQ_RETVAL(handled);
5881}
5882
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005883static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005885 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5886 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005888 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005890 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 rtl8169_tx_interrupt(dev, tp, ioaddr);
5892
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005893 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005894 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005895
5896 /* We need for force the visibility of tp->intr_mask
5897 * for other CPUs, as we can loose an MSI interrupt
5898 * and potentially wait for a retransmit timeout if we don't.
5899 * The posted write to IntrMask is safe, as it will
5900 * eventually make it to the chip and we won't loose anything
5901 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 */
David Dillowf11a3772009-05-22 15:29:34 +00005903 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005904 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005905 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 }
5907
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005908 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
Francois Romieu523a6092008-09-10 22:28:56 +02005911static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5912{
5913 struct rtl8169_private *tp = netdev_priv(dev);
5914
5915 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5916 return;
5917
5918 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5919 RTL_W32(RxMissed, 0);
5920}
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922static void rtl8169_down(struct net_device *dev)
5923{
5924 struct rtl8169_private *tp = netdev_priv(dev);
5925 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Francois Romieu4876cc12011-03-11 21:07:11 +01005927 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 netif_stop_queue(dev);
5930
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005931 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 spin_lock_irq(&tp->lock);
5934
Hayes Wang92fc43b2011-07-06 15:58:03 +08005935 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005936 /*
5937 * At this point device interrupts can not be enabled in any function,
5938 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5939 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5940 */
Francois Romieu523a6092008-09-10 22:28:56 +02005941 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
5943 spin_unlock_irq(&tp->lock);
5944
5945 synchronize_irq(dev->irq);
5946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005948 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 rtl8169_tx_clear(tp);
5951
5952 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005953
5954 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955}
5956
5957static int rtl8169_close(struct net_device *dev)
5958{
5959 struct rtl8169_private *tp = netdev_priv(dev);
5960 struct pci_dev *pdev = tp->pci_dev;
5961
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005962 pm_runtime_get_sync(&pdev->dev);
5963
Francois Romieucecb5fd2011-04-01 10:21:07 +02005964 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005965 rtl8169_update_counters(dev);
5966
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 rtl8169_down(dev);
5968
5969 free_irq(dev->irq, dev);
5970
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005971 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5972 tp->RxPhyAddr);
5973 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5974 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 tp->TxDescArray = NULL;
5976 tp->RxDescArray = NULL;
5977
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005978 pm_runtime_put_sync(&pdev->dev);
5979
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 return 0;
5981}
5982
Francois Romieu07ce4062007-02-23 23:36:39 +01005983static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984{
5985 struct rtl8169_private *tp = netdev_priv(dev);
5986 void __iomem *ioaddr = tp->mmio_addr;
5987 unsigned long flags;
5988 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005989 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 u32 tmp = 0;
5991
5992 if (dev->flags & IFF_PROMISC) {
5993 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005994 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 rx_mode =
5996 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5997 AcceptAllPhys;
5998 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005999 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00006000 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 /* Too many to filter perfectly -- accept all multicasts. */
6002 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
6003 mc_filter[1] = mc_filter[0] = 0xffffffff;
6004 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00006005 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01006006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 rx_mode = AcceptBroadcast | AcceptMyPhys;
6008 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00006009 netdev_for_each_mc_addr(ha, dev) {
6010 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6012 rx_mode |= AcceptMulticast;
6013 }
6014 }
6015
6016 spin_lock_irqsave(&tp->lock, flags);
6017
Francois Romieu1687b562011-07-19 17:21:29 +02006018 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Francois Romieuf887cce2008-07-17 22:24:18 +02006020 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01006021 u32 data = mc_filter[0];
6022
6023 mc_filter[0] = swab32(mc_filter[1]);
6024 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02006025 }
6026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006028 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
Francois Romieu57a9f232007-06-04 22:10:15 +02006030 RTL_W32(RxConfig, tmp);
6031
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 spin_unlock_irqrestore(&tp->lock, flags);
6033}
6034
6035/**
6036 * rtl8169_get_stats - Get rtl8169 read/write statistics
6037 * @dev: The Ethernet Device to get statistics for
6038 *
6039 * Get TX/RX statistics for rtl8169
6040 */
6041static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6042{
6043 struct rtl8169_private *tp = netdev_priv(dev);
6044 void __iomem *ioaddr = tp->mmio_addr;
6045 unsigned long flags;
6046
6047 if (netif_running(dev)) {
6048 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02006049 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 spin_unlock_irqrestore(&tp->lock, flags);
6051 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02006052
Francois Romieucebf8cc2007-10-18 12:06:54 +02006053 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054}
6055
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006056static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006057{
françois romieu065c27c2011-01-03 15:08:12 +00006058 struct rtl8169_private *tp = netdev_priv(dev);
6059
Francois Romieu5d06a992006-02-23 00:47:58 +01006060 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006061 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006062
françois romieu065c27c2011-01-03 15:08:12 +00006063 rtl_pll_power_down(tp);
6064
Francois Romieu5d06a992006-02-23 00:47:58 +01006065 netif_device_detach(dev);
6066 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006067}
Francois Romieu5d06a992006-02-23 00:47:58 +01006068
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006069#ifdef CONFIG_PM
6070
6071static int rtl8169_suspend(struct device *device)
6072{
6073 struct pci_dev *pdev = to_pci_dev(device);
6074 struct net_device *dev = pci_get_drvdata(pdev);
6075
6076 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006077
Francois Romieu5d06a992006-02-23 00:47:58 +01006078 return 0;
6079}
6080
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006081static void __rtl8169_resume(struct net_device *dev)
6082{
françois romieu065c27c2011-01-03 15:08:12 +00006083 struct rtl8169_private *tp = netdev_priv(dev);
6084
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006085 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006086
6087 rtl_pll_power_up(tp);
6088
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006089 rtl8169_schedule_work(dev, rtl8169_reset_task);
6090}
6091
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006092static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006093{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006094 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006095 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006096 struct rtl8169_private *tp = netdev_priv(dev);
6097
6098 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006099
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006100 if (netif_running(dev))
6101 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006102
Francois Romieu5d06a992006-02-23 00:47:58 +01006103 return 0;
6104}
6105
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006106static int rtl8169_runtime_suspend(struct device *device)
6107{
6108 struct pci_dev *pdev = to_pci_dev(device);
6109 struct net_device *dev = pci_get_drvdata(pdev);
6110 struct rtl8169_private *tp = netdev_priv(dev);
6111
6112 if (!tp->TxDescArray)
6113 return 0;
6114
6115 spin_lock_irq(&tp->lock);
6116 tp->saved_wolopts = __rtl8169_get_wol(tp);
6117 __rtl8169_set_wol(tp, WAKE_ANY);
6118 spin_unlock_irq(&tp->lock);
6119
6120 rtl8169_net_suspend(dev);
6121
6122 return 0;
6123}
6124
6125static int rtl8169_runtime_resume(struct device *device)
6126{
6127 struct pci_dev *pdev = to_pci_dev(device);
6128 struct net_device *dev = pci_get_drvdata(pdev);
6129 struct rtl8169_private *tp = netdev_priv(dev);
6130
6131 if (!tp->TxDescArray)
6132 return 0;
6133
6134 spin_lock_irq(&tp->lock);
6135 __rtl8169_set_wol(tp, tp->saved_wolopts);
6136 tp->saved_wolopts = 0;
6137 spin_unlock_irq(&tp->lock);
6138
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006139 rtl8169_init_phy(dev, tp);
6140
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006141 __rtl8169_resume(dev);
6142
6143 return 0;
6144}
6145
6146static int rtl8169_runtime_idle(struct device *device)
6147{
6148 struct pci_dev *pdev = to_pci_dev(device);
6149 struct net_device *dev = pci_get_drvdata(pdev);
6150 struct rtl8169_private *tp = netdev_priv(dev);
6151
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006152 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006153}
6154
Alexey Dobriyan47145212009-12-14 18:00:08 -08006155static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006156 .suspend = rtl8169_suspend,
6157 .resume = rtl8169_resume,
6158 .freeze = rtl8169_suspend,
6159 .thaw = rtl8169_resume,
6160 .poweroff = rtl8169_suspend,
6161 .restore = rtl8169_resume,
6162 .runtime_suspend = rtl8169_runtime_suspend,
6163 .runtime_resume = rtl8169_runtime_resume,
6164 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006165};
6166
6167#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6168
6169#else /* !CONFIG_PM */
6170
6171#define RTL8169_PM_OPS NULL
6172
6173#endif /* !CONFIG_PM */
6174
David S. Miller1805b2f2011-10-24 18:18:09 -04006175static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6176{
6177 void __iomem *ioaddr = tp->mmio_addr;
6178
6179 /* WoL fails with 8168b when the receiver is disabled. */
6180 switch (tp->mac_version) {
6181 case RTL_GIGA_MAC_VER_11:
6182 case RTL_GIGA_MAC_VER_12:
6183 case RTL_GIGA_MAC_VER_17:
6184 pci_clear_master(tp->pci_dev);
6185
6186 RTL_W8(ChipCmd, CmdRxEnb);
6187 /* PCI commit */
6188 RTL_R8(ChipCmd);
6189 break;
6190 default:
6191 break;
6192 }
6193}
6194
Francois Romieu1765f952008-09-13 17:21:40 +02006195static void rtl_shutdown(struct pci_dev *pdev)
6196{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006197 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006198 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006199
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006200 rtl8169_net_suspend(dev);
6201
Francois Romieucecb5fd2011-04-01 10:21:07 +02006202 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006203 rtl_rar_set(tp, dev->perm_addr);
6204
françois romieu4bb3f522009-06-17 11:41:45 +00006205 spin_lock_irq(&tp->lock);
6206
Hayes Wang92fc43b2011-07-06 15:58:03 +08006207 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006208
6209 spin_unlock_irq(&tp->lock);
6210
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006211 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006212 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6213 rtl_wol_suspend_quirk(tp);
6214 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006215 }
6216
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006217 pci_wake_from_d3(pdev, true);
6218 pci_set_power_state(pdev, PCI_D3hot);
6219 }
6220}
Francois Romieu5d06a992006-02-23 00:47:58 +01006221
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222static struct pci_driver rtl8169_pci_driver = {
6223 .name = MODULENAME,
6224 .id_table = rtl8169_pci_tbl,
6225 .probe = rtl8169_init_one,
6226 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006227 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006228 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229};
6230
Francois Romieu07d3f512007-02-21 22:40:46 +01006231static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232{
Jeff Garzik29917622006-08-19 17:48:59 -04006233 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
Francois Romieu07d3f512007-02-21 22:40:46 +01006236static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
6238 pci_unregister_driver(&rtl8169_pci_driver);
6239}
6240
6241module_init(rtl8169_init_module);
6242module_exit(rtl8169_cleanup_module);