blob: 7f06508df9ff2bc24939536c577916db3345338c [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 Romieuf162a5d2008-06-01 22:37:49 +0200477 Speed_down = (1 << 4),
478 MEMMAP = (1 << 3),
479 IOMAP = (1 << 2),
480 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100481 PMEnable = (1 << 0), /* Power Management Enable */
482
Francois Romieu6dccd162007-02-13 23:38:05 +0100483 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000484 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100485 PCI_Clock_66MHz = 0x01,
486 PCI_Clock_33MHz = 0x00,
487
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100488 /* Config3 register p.25 */
489 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
490 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200491 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200492 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493
Francois Romieud58d46b2011-05-03 16:38:29 +0200494 /* Config4 register */
495 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
496
Francois Romieu5d06a992006-02-23 00:47:58 +0100497 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
499 MWF = (1 << 5), /* Accept Multicast wakeup frame */
500 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200501 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100502 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100503 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* TBICSR p.28 */
506 TBIReset = 0x80000000,
507 TBILoopback = 0x40000000,
508 TBINwEnable = 0x20000000,
509 TBINwRestart = 0x10000000,
510 TBILinkOk = 0x02000000,
511 TBINwComplete = 0x01000000,
512
513 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200514 EnableBist = (1 << 15), // 8168 8101
515 Mac_dbgo_oe = (1 << 14), // 8168 8101
516 Normal_mode = (1 << 13), // unused
517 Force_half_dup = (1 << 12), // 8168 8101
518 Force_rxflow_en = (1 << 11), // 8168 8101
519 Force_txflow_en = (1 << 10), // 8168 8101
520 Cxpl_dbg_sel = (1 << 9), // 8168 8101
521 ASF = (1 << 8), // 8168 8101
522 PktCntrDisable = (1 << 7), // 8168 8101
523 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 RxVlan = (1 << 6),
525 RxChkSum = (1 << 5),
526 PCIDAC = (1 << 4),
527 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100528 INTT_0 = 0x0000, // 8168
529 INTT_1 = 0x0001, // 8168
530 INTT_2 = 0x0002, // 8168
531 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100534 TBI_Enable = 0x80,
535 TxFlowCtrl = 0x40,
536 RxFlowCtrl = 0x20,
537 _1000bpsF = 0x10,
538 _100bps = 0x08,
539 _10bps = 0x04,
540 LinkStatus = 0x02,
541 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200545
546 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100547 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Francois Romieu2b7b4312011-04-18 22:53:24 -0700550enum rtl_desc_bit {
551 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
553 RingEnd = (1 << 30), /* End of descriptor ring */
554 FirstFrag = (1 << 29), /* First segment of a packet */
555 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700556};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Francois Romieu2b7b4312011-04-18 22:53:24 -0700558/* Generic case. */
559enum rtl_tx_desc_bit {
560 /* First doubleword. */
561 TD_LSO = (1 << 27), /* Large Send Offload */
562#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564 /* Second doubleword. */
565 TxVlanTag = (1 << 17), /* Add VLAN tag */
566};
567
568/* 8169, 8168b and 810x except 8102e. */
569enum rtl_tx_desc_bit_0 {
570 /* First doubleword. */
571#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
572 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
573 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
574 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
575};
576
577/* 8102e, 8168c and beyond. */
578enum rtl_tx_desc_bit_1 {
579 /* Second doubleword. */
580#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
581 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
582 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
583 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
584};
585
586static const struct rtl_tx_desc_info {
587 struct {
588 u32 udp;
589 u32 tcp;
590 } checksum;
591 u16 mss_shift;
592 u16 opts_offset;
593} tx_desc_info [] = {
594 [RTL_TD_0] = {
595 .checksum = {
596 .udp = TD0_IP_CS | TD0_UDP_CS,
597 .tcp = TD0_IP_CS | TD0_TCP_CS
598 },
599 .mss_shift = TD0_MSS_SHIFT,
600 .opts_offset = 0
601 },
602 [RTL_TD_1] = {
603 .checksum = {
604 .udp = TD1_IP_CS | TD1_UDP_CS,
605 .tcp = TD1_IP_CS | TD1_TCP_CS
606 },
607 .mss_shift = TD1_MSS_SHIFT,
608 .opts_offset = 1
609 }
610};
611
612enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Rx private */
614 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
615 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
616
617#define RxProtoUDP (PID1)
618#define RxProtoTCP (PID0)
619#define RxProtoIP (PID1 | PID0)
620#define RxProtoMask RxProtoIP
621
622 IPFail = (1 << 16), /* IP checksum failed */
623 UDPFail = (1 << 15), /* UDP/IP checksum failed */
624 TCPFail = (1 << 14), /* TCP/IP checksum failed */
625 RxVlanTag = (1 << 16), /* VLAN tag available */
626};
627
628#define RsvdMask 0x3fffc000
629
630struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200631 __le32 opts1;
632 __le32 opts2;
633 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634};
635
636struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200637 __le32 opts1;
638 __le32 opts2;
639 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642struct ring_info {
643 struct sk_buff *skb;
644 u32 len;
645 u8 __pad[sizeof(void *) - sizeof(u32)];
646};
647
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200648enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200649 RTL_FEATURE_WOL = (1 << 0),
650 RTL_FEATURE_MSI = (1 << 1),
651 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200652};
653
Ivan Vecera355423d2009-02-06 21:49:57 -0800654struct rtl8169_counters {
655 __le64 tx_packets;
656 __le64 rx_packets;
657 __le64 tx_errors;
658 __le32 rx_errors;
659 __le16 rx_missed;
660 __le16 align_errors;
661 __le32 tx_one_collision;
662 __le32 tx_multi_collision;
663 __le64 rx_unicast;
664 __le64 rx_broadcast;
665 __le32 rx_multicast;
666 __le16 tx_aborted;
667 __le16 tx_underun;
668};
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670struct rtl8169_private {
671 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200672 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000673 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700674 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200675 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200676 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700677 u16 txd_version;
678 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
680 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
681 u32 dirty_rx;
682 u32 dirty_tx;
683 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
684 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
685 dma_addr_t TxPhyAddr;
686 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000687 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct timer_list timer;
690 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100691 u16 intr_event;
692 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000694
695 struct mdio_ops {
696 void (*write)(void __iomem *, int, int);
697 int (*read)(void __iomem *, int);
698 } mdio_ops;
699
françois romieu065c27c2011-01-03 15:08:12 +0000700 struct pll_power_ops {
701 void (*down)(struct rtl8169_private *);
702 void (*up)(struct rtl8169_private *);
703 } pll_power_ops;
704
Francois Romieud58d46b2011-05-03 16:38:29 +0200705 struct jumbo_ops {
706 void (*enable)(struct rtl8169_private *);
707 void (*disable)(struct rtl8169_private *);
708 } jumbo_ops;
709
Oliver Neukum54405cd2011-01-06 21:55:13 +0100710 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200711 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000712 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100713 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000714 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800716 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
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
françois romieu811fd302011-12-04 20:30:45 +00001183static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
françois romieu811fd302011-12-04 20:30:45 +00001185 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
françois romieu811fd302011-12-04 20:30:45 +00001187 RTL_W16(IntrMask, 0x0000);
1188 RTL_W16(IntrStatus, tp->intr_event);
1189 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
françois romieu4da19632011-01-03 15:07:55 +00001192static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
françois romieu4da19632011-01-03 15:07:55 +00001194 void __iomem *ioaddr = tp->mmio_addr;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return RTL_R32(TBICSR) & TBIReset;
1197}
1198
françois romieu4da19632011-01-03 15:07:55 +00001199static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
françois romieu4da19632011-01-03 15:07:55 +00001201 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1205{
1206 return RTL_R32(TBICSR) & TBILinkOk;
1207}
1208
1209static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1210{
1211 return RTL_R8(PHYstatus) & LinkStatus;
1212}
1213
françois romieu4da19632011-01-03 15:07:55 +00001214static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
françois romieu4da19632011-01-03 15:07:55 +00001216 void __iomem *ioaddr = tp->mmio_addr;
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1219}
1220
françois romieu4da19632011-01-03 15:07:55 +00001221static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 unsigned int val;
1224
françois romieu4da19632011-01-03 15:07:55 +00001225 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1226 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Hayes Wang70090422011-07-06 15:58:06 +08001229static void rtl_link_chg_patch(struct rtl8169_private *tp)
1230{
1231 void __iomem *ioaddr = tp->mmio_addr;
1232 struct net_device *dev = tp->dev;
1233
1234 if (!netif_running(dev))
1235 return;
1236
1237 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1238 if (RTL_R8(PHYstatus) & _1000bpsF) {
1239 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1240 0x00000011, ERIAR_EXGMAC);
1241 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1242 0x00000005, ERIAR_EXGMAC);
1243 } else if (RTL_R8(PHYstatus) & _100bps) {
1244 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1245 0x0000001f, ERIAR_EXGMAC);
1246 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1247 0x00000005, ERIAR_EXGMAC);
1248 } else {
1249 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1250 0x0000001f, ERIAR_EXGMAC);
1251 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1252 0x0000003f, ERIAR_EXGMAC);
1253 }
1254 /* Reset packet filter */
1255 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1256 ERIAR_EXGMAC);
1257 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1258 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001259 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1260 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1261 if (RTL_R8(PHYstatus) & _1000bpsF) {
1262 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1263 0x00000011, ERIAR_EXGMAC);
1264 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1265 0x00000005, ERIAR_EXGMAC);
1266 } else {
1267 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1268 0x0000001f, ERIAR_EXGMAC);
1269 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1270 0x0000003f, ERIAR_EXGMAC);
1271 }
Hayes Wang70090422011-07-06 15:58:06 +08001272 }
1273}
1274
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001275static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001276 struct rtl8169_private *tp,
1277 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 unsigned long flags;
1280
1281 spin_lock_irqsave(&tp->lock, flags);
1282 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001283 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001284 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001285 if (pm)
1286 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001288 if (net_ratelimit())
1289 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001290 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001292 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001293 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001294 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 spin_unlock_irqrestore(&tp->lock, flags);
1297}
1298
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001299static void rtl8169_check_link_status(struct net_device *dev,
1300 struct rtl8169_private *tp,
1301 void __iomem *ioaddr)
1302{
1303 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1304}
1305
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001306#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1307
1308static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1309{
1310 void __iomem *ioaddr = tp->mmio_addr;
1311 u8 options;
1312 u32 wolopts = 0;
1313
1314 options = RTL_R8(Config1);
1315 if (!(options & PMEnable))
1316 return 0;
1317
1318 options = RTL_R8(Config3);
1319 if (options & LinkUp)
1320 wolopts |= WAKE_PHY;
1321 if (options & MagicPacket)
1322 wolopts |= WAKE_MAGIC;
1323
1324 options = RTL_R8(Config5);
1325 if (options & UWF)
1326 wolopts |= WAKE_UCAST;
1327 if (options & BWF)
1328 wolopts |= WAKE_BCAST;
1329 if (options & MWF)
1330 wolopts |= WAKE_MCAST;
1331
1332 return wolopts;
1333}
1334
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001335static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1336{
1337 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001338
1339 spin_lock_irq(&tp->lock);
1340
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001341 wol->supported = WAKE_ANY;
1342 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001343
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001344 spin_unlock_irq(&tp->lock);
1345}
1346
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001347static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001348{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001349 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001350 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001351 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001352 u32 opt;
1353 u16 reg;
1354 u8 mask;
1355 } cfg[] = {
1356 { WAKE_ANY, Config1, PMEnable },
1357 { WAKE_PHY, Config3, LinkUp },
1358 { WAKE_MAGIC, Config3, MagicPacket },
1359 { WAKE_UCAST, Config5, UWF },
1360 { WAKE_BCAST, Config5, BWF },
1361 { WAKE_MCAST, Config5, MWF },
1362 { WAKE_ANY, Config5, LanWake }
1363 };
1364
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001365 RTL_W8(Cfg9346, Cfg9346_Unlock);
1366
1367 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1368 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001369 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001370 options |= cfg[i].mask;
1371 RTL_W8(cfg[i].reg, options);
1372 }
1373
1374 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001375}
1376
1377static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1378{
1379 struct rtl8169_private *tp = netdev_priv(dev);
1380
1381 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001383 if (wol->wolopts)
1384 tp->features |= RTL_FEATURE_WOL;
1385 else
1386 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001387 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001388 spin_unlock_irq(&tp->lock);
1389
françois romieuea809072010-11-08 13:23:58 +00001390 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1391
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392 return 0;
1393}
1394
Francois Romieu31bd2042011-04-26 18:58:59 +02001395static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1396{
Francois Romieu85bffe62011-04-27 08:22:39 +02001397 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400static void rtl8169_get_drvinfo(struct net_device *dev,
1401 struct ethtool_drvinfo *info)
1402{
1403 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001404 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Rick Jones68aad782011-11-07 13:29:27 +00001406 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1407 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1408 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001409 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001410 if (!IS_ERR_OR_NULL(rtl_fw))
1411 strlcpy(info->fw_version, rtl_fw->version,
1412 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static int rtl8169_get_regs_len(struct net_device *dev)
1416{
1417 return R8169_REGS_SIZE;
1418}
1419
1420static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001421 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct rtl8169_private *tp = netdev_priv(dev);
1424 void __iomem *ioaddr = tp->mmio_addr;
1425 int ret = 0;
1426 u32 reg;
1427
1428 reg = RTL_R32(TBICSR);
1429 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1430 (duplex == DUPLEX_FULL)) {
1431 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1432 } else if (autoneg == AUTONEG_ENABLE)
1433 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1434 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001435 netif_warn(tp, link, dev,
1436 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 ret = -EOPNOTSUPP;
1438 }
1439
1440 return ret;
1441}
1442
1443static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001444 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001447 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001448 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Hayes Wang716b50a2011-02-22 17:26:18 +08001450 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001453 int auto_nego;
1454
françois romieu4da19632011-01-03 15:07:55 +00001455 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001456 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1457 ADVERTISE_100HALF | ADVERTISE_100FULL);
1458
1459 if (adv & ADVERTISED_10baseT_Half)
1460 auto_nego |= ADVERTISE_10HALF;
1461 if (adv & ADVERTISED_10baseT_Full)
1462 auto_nego |= ADVERTISE_10FULL;
1463 if (adv & ADVERTISED_100baseT_Half)
1464 auto_nego |= ADVERTISE_100HALF;
1465 if (adv & ADVERTISED_100baseT_Full)
1466 auto_nego |= ADVERTISE_100FULL;
1467
françois romieu3577aa12009-05-19 10:46:48 +00001468 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1469
françois romieu4da19632011-01-03 15:07:55 +00001470 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001471 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1472
1473 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001474 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001475 if (adv & ADVERTISED_1000baseT_Half)
1476 giga_ctrl |= ADVERTISE_1000HALF;
1477 if (adv & ADVERTISED_1000baseT_Full)
1478 giga_ctrl |= ADVERTISE_1000FULL;
1479 } else if (adv & (ADVERTISED_1000baseT_Half |
1480 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001481 netif_info(tp, link, dev,
1482 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001483 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
françois romieu3577aa12009-05-19 10:46:48 +00001486 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001487
françois romieu4da19632011-01-03 15:07:55 +00001488 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1489 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001490 } else {
1491 giga_ctrl = 0;
1492
1493 if (speed == SPEED_10)
1494 bmcr = 0;
1495 else if (speed == SPEED_100)
1496 bmcr = BMCR_SPEED100;
1497 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001498 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001499
1500 if (duplex == DUPLEX_FULL)
1501 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001502 }
1503
françois romieu4da19632011-01-03 15:07:55 +00001504 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001505
Francois Romieucecb5fd2011-04-01 10:21:07 +02001506 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1507 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001508 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001509 rtl_writephy(tp, 0x17, 0x2138);
1510 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001511 } else {
françois romieu4da19632011-01-03 15:07:55 +00001512 rtl_writephy(tp, 0x17, 0x2108);
1513 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001514 }
1515 }
1516
Oliver Neukum54405cd2011-01-06 21:55:13 +01001517 rc = 0;
1518out:
1519 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001523 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct rtl8169_private *tp = netdev_priv(dev);
1526 int ret;
1527
Oliver Neukum54405cd2011-01-06 21:55:13 +01001528 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001529 if (ret < 0)
1530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Francois Romieu4876cc12011-03-11 21:07:11 +01001532 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1533 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001535 }
1536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return ret;
1538}
1539
1540static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1541{
1542 struct rtl8169_private *tp = netdev_priv(dev);
1543 unsigned long flags;
1544 int ret;
1545
Francois Romieu4876cc12011-03-11 21:07:11 +01001546 del_timer_sync(&tp->timer);
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001549 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001550 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return ret;
1554}
1555
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001556static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1557 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Francois Romieud58d46b2011-05-03 16:38:29 +02001559 struct rtl8169_private *tp = netdev_priv(dev);
1560
Francois Romieu2b7b4312011-04-18 22:53:24 -07001561 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001562 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Francois Romieud58d46b2011-05-03 16:38:29 +02001564 if (dev->mtu > JUMBO_1K &&
1565 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1566 features &= ~NETIF_F_IP_CSUM;
1567
Michał Mirosław350fb322011-04-08 06:35:56 +00001568 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001571static int rtl8169_set_features(struct net_device *dev,
1572 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct rtl8169_private *tp = netdev_priv(dev);
1575 void __iomem *ioaddr = tp->mmio_addr;
1576 unsigned long flags;
1577
1578 spin_lock_irqsave(&tp->lock, flags);
1579
Michał Mirosław350fb322011-04-08 06:35:56 +00001580 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 tp->cp_cmd |= RxChkSum;
1582 else
1583 tp->cp_cmd &= ~RxChkSum;
1584
Michał Mirosław350fb322011-04-08 06:35:56 +00001585 if (dev->features & NETIF_F_HW_VLAN_RX)
1586 tp->cp_cmd |= RxVlan;
1587 else
1588 tp->cp_cmd &= ~RxVlan;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 RTL_W16(CPlusCmd, tp->cp_cmd);
1591 RTL_R16(CPlusCmd);
1592
1593 spin_unlock_irqrestore(&tp->lock, flags);
1594
1595 return 0;
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1599 struct sk_buff *skb)
1600{
Jesse Grosseab6d182010-10-20 13:56:03 +00001601 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1603}
1604
Francois Romieu7a8fc772011-03-01 17:18:33 +01001605static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Francois Romieu7a8fc772011-03-01 17:18:33 +01001609 if (opts2 & RxVlanTag)
1610 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Francois Romieuccdffb92008-07-26 14:26:06 +02001615static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct rtl8169_private *tp = netdev_priv(dev);
1618 void __iomem *ioaddr = tp->mmio_addr;
1619 u32 status;
1620
1621 cmd->supported =
1622 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1623 cmd->port = PORT_FIBRE;
1624 cmd->transceiver = XCVR_INTERNAL;
1625
1626 status = RTL_R32(TBICSR);
1627 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1628 cmd->autoneg = !!(status & TBINwEnable);
1629
David Decotigny70739492011-04-27 18:32:40 +00001630 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001632
1633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Francois Romieuccdffb92008-07-26 14:26:06 +02001636static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Francois Romieuccdffb92008-07-26 14:26:06 +02001640 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1644{
1645 struct rtl8169_private *tp = netdev_priv(dev);
1646 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001647 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 spin_lock_irqsave(&tp->lock, flags);
1650
Francois Romieuccdffb92008-07-26 14:26:06 +02001651 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001654 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1658 void *p)
1659{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001660 struct rtl8169_private *tp = netdev_priv(dev);
1661 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Francois Romieu5b0384f2006-08-16 16:00:01 +02001663 if (regs->len > R8169_REGS_SIZE)
1664 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Francois Romieu5b0384f2006-08-16 16:00:01 +02001666 spin_lock_irqsave(&tp->lock, flags);
1667 memcpy_fromio(p, tp->mmio_addr, regs->len);
1668 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001671static u32 rtl8169_get_msglevel(struct net_device *dev)
1672{
1673 struct rtl8169_private *tp = netdev_priv(dev);
1674
1675 return tp->msg_enable;
1676}
1677
1678static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1679{
1680 struct rtl8169_private *tp = netdev_priv(dev);
1681
1682 tp->msg_enable = value;
1683}
1684
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001685static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1686 "tx_packets",
1687 "rx_packets",
1688 "tx_errors",
1689 "rx_errors",
1690 "rx_missed",
1691 "align_errors",
1692 "tx_single_collisions",
1693 "tx_multi_collisions",
1694 "unicast",
1695 "broadcast",
1696 "multicast",
1697 "tx_aborted",
1698 "tx_underrun",
1699};
1700
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001701static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001702{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001703 switch (sset) {
1704 case ETH_SS_STATS:
1705 return ARRAY_SIZE(rtl8169_gstrings);
1706 default:
1707 return -EOPNOTSUPP;
1708 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001709}
1710
Ivan Vecera355423d2009-02-06 21:49:57 -08001711static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001712{
1713 struct rtl8169_private *tp = netdev_priv(dev);
1714 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001715 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001716 struct rtl8169_counters *counters;
1717 dma_addr_t paddr;
1718 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001719 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001720
Ivan Vecera355423d2009-02-06 21:49:57 -08001721 /*
1722 * Some chips are unable to dump tally counters when the receiver
1723 * is disabled.
1724 */
1725 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1726 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001727
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001728 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001729 if (!counters)
1730 return;
1731
1732 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001733 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001734 RTL_W32(CounterAddrLow, cmd);
1735 RTL_W32(CounterAddrLow, cmd | CounterDump);
1736
Ivan Vecera355423d2009-02-06 21:49:57 -08001737 while (wait--) {
1738 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001739 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001740 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001741 }
1742 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001743 }
1744
1745 RTL_W32(CounterAddrLow, 0);
1746 RTL_W32(CounterAddrHigh, 0);
1747
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001748 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001749}
1750
Ivan Vecera355423d2009-02-06 21:49:57 -08001751static void rtl8169_get_ethtool_stats(struct net_device *dev,
1752 struct ethtool_stats *stats, u64 *data)
1753{
1754 struct rtl8169_private *tp = netdev_priv(dev);
1755
1756 ASSERT_RTNL();
1757
1758 rtl8169_update_counters(dev);
1759
1760 data[0] = le64_to_cpu(tp->counters.tx_packets);
1761 data[1] = le64_to_cpu(tp->counters.rx_packets);
1762 data[2] = le64_to_cpu(tp->counters.tx_errors);
1763 data[3] = le32_to_cpu(tp->counters.rx_errors);
1764 data[4] = le16_to_cpu(tp->counters.rx_missed);
1765 data[5] = le16_to_cpu(tp->counters.align_errors);
1766 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1767 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1768 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1769 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1770 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1771 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1772 data[12] = le16_to_cpu(tp->counters.tx_underun);
1773}
1774
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001775static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1776{
1777 switch(stringset) {
1778 case ETH_SS_STATS:
1779 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1780 break;
1781 }
1782}
1783
Jeff Garzik7282d492006-09-13 14:30:00 -04001784static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 .get_drvinfo = rtl8169_get_drvinfo,
1786 .get_regs_len = rtl8169_get_regs_len,
1787 .get_link = ethtool_op_get_link,
1788 .get_settings = rtl8169_get_settings,
1789 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001790 .get_msglevel = rtl8169_get_msglevel,
1791 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001793 .get_wol = rtl8169_get_wol,
1794 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001795 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001796 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001797 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798};
1799
Francois Romieu07d3f512007-02-21 22:40:46 +01001800static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001801 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Francois Romieu5d320a22011-05-08 17:47:36 +02001803 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001804 /*
1805 * The driver currently handles the 8168Bf and the 8168Be identically
1806 * but they can be identified more specifically through the test below
1807 * if needed:
1808 *
1809 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001810 *
1811 * Same thing for the 8101Eb and the 8101Ec:
1812 *
1813 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001814 */
Francois Romieu37441002011-06-17 22:58:54 +02001815 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001817 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 int mac_version;
1819 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001820 /* 8168F family. */
1821 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1822 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1823
hayeswang01dc7fe2011-03-21 01:50:28 +00001824 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001825 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001826 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1827 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1828 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1829
Francois Romieu5b538df2008-07-20 16:22:45 +02001830 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001831 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1832 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001833 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001834
françois romieue6de30d2011-01-03 15:08:37 +00001835 /* 8168DP family. */
1836 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1837 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001838 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001839
Francois Romieuef808d52008-06-29 13:10:54 +02001840 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001841 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001842 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001843 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001844 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001845 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1846 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001847 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001848 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001849 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001850
1851 /* 8168B family. */
1852 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1853 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1854 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1855 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1856
1857 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001858 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001859 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1860 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1861 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001862 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1863 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1864 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1865 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1866 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1867 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001868 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001869 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001870 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001871 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1872 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001873 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1874 /* FIXME: where did these entries come from ? -- FR */
1875 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1876 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1877
1878 /* 8110 family. */
1879 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1880 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1881 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1882 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1883 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1884 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1885
Jean Delvaref21b75e2009-05-26 20:54:48 -07001886 /* Catch-all */
1887 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001888 };
1889 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 u32 reg;
1891
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001892 reg = RTL_R32(TxConfig);
1893 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 p++;
1895 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001896
1897 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1898 netif_notice(tp, probe, dev,
1899 "unknown MAC, using family default\n");
1900 tp->mac_version = default_version;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1905{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001906 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Francois Romieu867763c2007-08-17 18:21:58 +02001909struct phy_reg {
1910 u16 reg;
1911 u16 val;
1912};
1913
françois romieu4da19632011-01-03 15:07:55 +00001914static void rtl_writephy_batch(struct rtl8169_private *tp,
1915 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001916{
1917 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001918 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001919 regs++;
1920 }
1921}
1922
françois romieubca03d52011-01-03 15:07:31 +00001923#define PHY_READ 0x00000000
1924#define PHY_DATA_OR 0x10000000
1925#define PHY_DATA_AND 0x20000000
1926#define PHY_BJMPN 0x30000000
1927#define PHY_READ_EFUSE 0x40000000
1928#define PHY_READ_MAC_BYTE 0x50000000
1929#define PHY_WRITE_MAC_BYTE 0x60000000
1930#define PHY_CLEAR_READCOUNT 0x70000000
1931#define PHY_WRITE 0x80000000
1932#define PHY_READCOUNT_EQ_SKIP 0x90000000
1933#define PHY_COMP_EQ_SKIPN 0xa0000000
1934#define PHY_COMP_NEQ_SKIPN 0xb0000000
1935#define PHY_WRITE_PREVIOUS 0xc0000000
1936#define PHY_SKIPN 0xd0000000
1937#define PHY_DELAY_MS 0xe0000000
1938#define PHY_WRITE_ERI_WORD 0xf0000000
1939
Hayes Wang960aee62011-06-18 11:37:48 +02001940struct fw_info {
1941 u32 magic;
1942 char version[RTL_VER_SIZE];
1943 __le32 fw_start;
1944 __le32 fw_len;
1945 u8 chksum;
1946} __packed;
1947
Francois Romieu1c361ef2011-06-17 17:16:24 +02001948#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1949
1950static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001951{
Francois Romieub6ffd972011-06-17 17:00:05 +02001952 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001953 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001954 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1955 char *version = rtl_fw->version;
1956 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001957
Francois Romieu1c361ef2011-06-17 17:16:24 +02001958 if (fw->size < FW_OPCODE_SIZE)
1959 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001960
1961 if (!fw_info->magic) {
1962 size_t i, size, start;
1963 u8 checksum = 0;
1964
1965 if (fw->size < sizeof(*fw_info))
1966 goto out;
1967
1968 for (i = 0; i < fw->size; i++)
1969 checksum += fw->data[i];
1970 if (checksum != 0)
1971 goto out;
1972
1973 start = le32_to_cpu(fw_info->fw_start);
1974 if (start > fw->size)
1975 goto out;
1976
1977 size = le32_to_cpu(fw_info->fw_len);
1978 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1979 goto out;
1980
1981 memcpy(version, fw_info->version, RTL_VER_SIZE);
1982
1983 pa->code = (__le32 *)(fw->data + start);
1984 pa->size = size;
1985 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001986 if (fw->size % FW_OPCODE_SIZE)
1987 goto out;
1988
1989 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1990
1991 pa->code = (__le32 *)fw->data;
1992 pa->size = fw->size / FW_OPCODE_SIZE;
1993 }
1994 version[RTL_VER_SIZE - 1] = 0;
1995
1996 rc = true;
1997out:
1998 return rc;
1999}
2000
Francois Romieufd112f22011-06-18 00:10:29 +02002001static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2002 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002003{
Francois Romieufd112f22011-06-18 00:10:29 +02002004 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002005 size_t index;
2006
Francois Romieu1c361ef2011-06-17 17:16:24 +02002007 for (index = 0; index < pa->size; index++) {
2008 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002009 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002010
hayeswang42b82dc2011-01-10 02:07:25 +00002011 switch(action & 0xf0000000) {
2012 case PHY_READ:
2013 case PHY_DATA_OR:
2014 case PHY_DATA_AND:
2015 case PHY_READ_EFUSE:
2016 case PHY_CLEAR_READCOUNT:
2017 case PHY_WRITE:
2018 case PHY_WRITE_PREVIOUS:
2019 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002020 break;
2021
hayeswang42b82dc2011-01-10 02:07:25 +00002022 case PHY_BJMPN:
2023 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002024 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002025 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002026 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002027 }
2028 break;
2029 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002030 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002031 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002032 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002033 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002034 }
2035 break;
2036 case PHY_COMP_EQ_SKIPN:
2037 case PHY_COMP_NEQ_SKIPN:
2038 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002039 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002040 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002041 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002042 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002043 }
2044 break;
2045
2046 case PHY_READ_MAC_BYTE:
2047 case PHY_WRITE_MAC_BYTE:
2048 case PHY_WRITE_ERI_WORD:
2049 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002050 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002051 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002052 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002053 }
2054 }
Francois Romieufd112f22011-06-18 00:10:29 +02002055 rc = true;
2056out:
2057 return rc;
2058}
françois romieubca03d52011-01-03 15:07:31 +00002059
Francois Romieufd112f22011-06-18 00:10:29 +02002060static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2061{
2062 struct net_device *dev = tp->dev;
2063 int rc = -EINVAL;
2064
2065 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2066 netif_err(tp, ifup, dev, "invalid firwmare\n");
2067 goto out;
2068 }
2069
2070 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2071 rc = 0;
2072out:
2073 return rc;
2074}
2075
2076static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2077{
2078 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2079 u32 predata, count;
2080 size_t index;
2081
2082 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002083
Francois Romieu1c361ef2011-06-17 17:16:24 +02002084 for (index = 0; index < pa->size; ) {
2085 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002086 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002087 u32 regno = (action & 0x0fff0000) >> 16;
2088
2089 if (!action)
2090 break;
françois romieubca03d52011-01-03 15:07:31 +00002091
2092 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002093 case PHY_READ:
2094 predata = rtl_readphy(tp, regno);
2095 count++;
2096 index++;
françois romieubca03d52011-01-03 15:07:31 +00002097 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002098 case PHY_DATA_OR:
2099 predata |= data;
2100 index++;
2101 break;
2102 case PHY_DATA_AND:
2103 predata &= data;
2104 index++;
2105 break;
2106 case PHY_BJMPN:
2107 index -= regno;
2108 break;
2109 case PHY_READ_EFUSE:
2110 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2111 index++;
2112 break;
2113 case PHY_CLEAR_READCOUNT:
2114 count = 0;
2115 index++;
2116 break;
2117 case PHY_WRITE:
2118 rtl_writephy(tp, regno, data);
2119 index++;
2120 break;
2121 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002122 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002123 break;
2124 case PHY_COMP_EQ_SKIPN:
2125 if (predata == data)
2126 index += regno;
2127 index++;
2128 break;
2129 case PHY_COMP_NEQ_SKIPN:
2130 if (predata != data)
2131 index += regno;
2132 index++;
2133 break;
2134 case PHY_WRITE_PREVIOUS:
2135 rtl_writephy(tp, regno, predata);
2136 index++;
2137 break;
2138 case PHY_SKIPN:
2139 index += regno + 1;
2140 break;
2141 case PHY_DELAY_MS:
2142 mdelay(data);
2143 index++;
2144 break;
2145
2146 case PHY_READ_MAC_BYTE:
2147 case PHY_WRITE_MAC_BYTE:
2148 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002149 default:
2150 BUG();
2151 }
2152 }
2153}
2154
françois romieuf1e02ed2011-01-13 13:07:53 +00002155static void rtl_release_firmware(struct rtl8169_private *tp)
2156{
Francois Romieub6ffd972011-06-17 17:00:05 +02002157 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2158 release_firmware(tp->rtl_fw->fw);
2159 kfree(tp->rtl_fw);
2160 }
2161 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002162}
2163
François Romieu953a12c2011-04-24 17:38:48 +02002164static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002165{
Francois Romieub6ffd972011-06-17 17:00:05 +02002166 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002167
2168 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002169 if (!IS_ERR_OR_NULL(rtl_fw))
2170 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002171}
2172
2173static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2174{
2175 if (rtl_readphy(tp, reg) != val)
2176 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2177 else
2178 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002179}
2180
françois romieu4da19632011-01-03 15:07:55 +00002181static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002183 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002184 { 0x1f, 0x0001 },
2185 { 0x06, 0x006e },
2186 { 0x08, 0x0708 },
2187 { 0x15, 0x4000 },
2188 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
françois romieu0b9b5712009-08-10 19:44:56 +00002190 { 0x1f, 0x0001 },
2191 { 0x03, 0x00a1 },
2192 { 0x02, 0x0008 },
2193 { 0x01, 0x0120 },
2194 { 0x00, 0x1000 },
2195 { 0x04, 0x0800 },
2196 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
françois romieu0b9b5712009-08-10 19:44:56 +00002198 { 0x03, 0xff41 },
2199 { 0x02, 0xdf60 },
2200 { 0x01, 0x0140 },
2201 { 0x00, 0x0077 },
2202 { 0x04, 0x7800 },
2203 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
françois romieu0b9b5712009-08-10 19:44:56 +00002205 { 0x03, 0x802f },
2206 { 0x02, 0x4f02 },
2207 { 0x01, 0x0409 },
2208 { 0x00, 0xf0f9 },
2209 { 0x04, 0x9800 },
2210 { 0x04, 0x9000 },
2211
2212 { 0x03, 0xdf01 },
2213 { 0x02, 0xdf20 },
2214 { 0x01, 0xff95 },
2215 { 0x00, 0xba00 },
2216 { 0x04, 0xa800 },
2217 { 0x04, 0xa000 },
2218
2219 { 0x03, 0xff41 },
2220 { 0x02, 0xdf20 },
2221 { 0x01, 0x0140 },
2222 { 0x00, 0x00bb },
2223 { 0x04, 0xb800 },
2224 { 0x04, 0xb000 },
2225
2226 { 0x03, 0xdf41 },
2227 { 0x02, 0xdc60 },
2228 { 0x01, 0x6340 },
2229 { 0x00, 0x007d },
2230 { 0x04, 0xd800 },
2231 { 0x04, 0xd000 },
2232
2233 { 0x03, 0xdf01 },
2234 { 0x02, 0xdf20 },
2235 { 0x01, 0x100a },
2236 { 0x00, 0xa0ff },
2237 { 0x04, 0xf800 },
2238 { 0x04, 0xf000 },
2239
2240 { 0x1f, 0x0000 },
2241 { 0x0b, 0x0000 },
2242 { 0x00, 0x9200 }
2243 };
2244
françois romieu4da19632011-01-03 15:07:55 +00002245 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
françois romieu4da19632011-01-03 15:07:55 +00002248static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002249{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002250 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002251 { 0x1f, 0x0002 },
2252 { 0x01, 0x90d0 },
2253 { 0x1f, 0x0000 }
2254 };
2255
françois romieu4da19632011-01-03 15:07:55 +00002256 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002257}
2258
françois romieu4da19632011-01-03 15:07:55 +00002259static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002260{
2261 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002262
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002263 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2264 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002265 return;
2266
françois romieu4da19632011-01-03 15:07:55 +00002267 rtl_writephy(tp, 0x1f, 0x0001);
2268 rtl_writephy(tp, 0x10, 0xf01b);
2269 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002270}
2271
françois romieu4da19632011-01-03 15:07:55 +00002272static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002273{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002274 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002275 { 0x1f, 0x0001 },
2276 { 0x04, 0x0000 },
2277 { 0x03, 0x00a1 },
2278 { 0x02, 0x0008 },
2279 { 0x01, 0x0120 },
2280 { 0x00, 0x1000 },
2281 { 0x04, 0x0800 },
2282 { 0x04, 0x9000 },
2283 { 0x03, 0x802f },
2284 { 0x02, 0x4f02 },
2285 { 0x01, 0x0409 },
2286 { 0x00, 0xf099 },
2287 { 0x04, 0x9800 },
2288 { 0x04, 0xa000 },
2289 { 0x03, 0xdf01 },
2290 { 0x02, 0xdf20 },
2291 { 0x01, 0xff95 },
2292 { 0x00, 0xba00 },
2293 { 0x04, 0xa800 },
2294 { 0x04, 0xf000 },
2295 { 0x03, 0xdf01 },
2296 { 0x02, 0xdf20 },
2297 { 0x01, 0x101a },
2298 { 0x00, 0xa0ff },
2299 { 0x04, 0xf800 },
2300 { 0x04, 0x0000 },
2301 { 0x1f, 0x0000 },
2302
2303 { 0x1f, 0x0001 },
2304 { 0x10, 0xf41b },
2305 { 0x14, 0xfb54 },
2306 { 0x18, 0xf5c7 },
2307 { 0x1f, 0x0000 },
2308
2309 { 0x1f, 0x0001 },
2310 { 0x17, 0x0cc0 },
2311 { 0x1f, 0x0000 }
2312 };
2313
françois romieu4da19632011-01-03 15:07:55 +00002314 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002315
françois romieu4da19632011-01-03 15:07:55 +00002316 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002317}
2318
françois romieu4da19632011-01-03 15:07:55 +00002319static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002320{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002321 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002322 { 0x1f, 0x0001 },
2323 { 0x04, 0x0000 },
2324 { 0x03, 0x00a1 },
2325 { 0x02, 0x0008 },
2326 { 0x01, 0x0120 },
2327 { 0x00, 0x1000 },
2328 { 0x04, 0x0800 },
2329 { 0x04, 0x9000 },
2330 { 0x03, 0x802f },
2331 { 0x02, 0x4f02 },
2332 { 0x01, 0x0409 },
2333 { 0x00, 0xf099 },
2334 { 0x04, 0x9800 },
2335 { 0x04, 0xa000 },
2336 { 0x03, 0xdf01 },
2337 { 0x02, 0xdf20 },
2338 { 0x01, 0xff95 },
2339 { 0x00, 0xba00 },
2340 { 0x04, 0xa800 },
2341 { 0x04, 0xf000 },
2342 { 0x03, 0xdf01 },
2343 { 0x02, 0xdf20 },
2344 { 0x01, 0x101a },
2345 { 0x00, 0xa0ff },
2346 { 0x04, 0xf800 },
2347 { 0x04, 0x0000 },
2348 { 0x1f, 0x0000 },
2349
2350 { 0x1f, 0x0001 },
2351 { 0x0b, 0x8480 },
2352 { 0x1f, 0x0000 },
2353
2354 { 0x1f, 0x0001 },
2355 { 0x18, 0x67c7 },
2356 { 0x04, 0x2000 },
2357 { 0x03, 0x002f },
2358 { 0x02, 0x4360 },
2359 { 0x01, 0x0109 },
2360 { 0x00, 0x3022 },
2361 { 0x04, 0x2800 },
2362 { 0x1f, 0x0000 },
2363
2364 { 0x1f, 0x0001 },
2365 { 0x17, 0x0cc0 },
2366 { 0x1f, 0x0000 }
2367 };
2368
françois romieu4da19632011-01-03 15:07:55 +00002369 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002370}
2371
françois romieu4da19632011-01-03 15:07:55 +00002372static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002373{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002374 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002375 { 0x10, 0xf41b },
2376 { 0x1f, 0x0000 }
2377 };
2378
françois romieu4da19632011-01-03 15:07:55 +00002379 rtl_writephy(tp, 0x1f, 0x0001);
2380 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002381
françois romieu4da19632011-01-03 15:07:55 +00002382 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002383}
2384
françois romieu4da19632011-01-03 15:07:55 +00002385static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002386{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002387 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002388 { 0x1f, 0x0001 },
2389 { 0x10, 0xf41b },
2390 { 0x1f, 0x0000 }
2391 };
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002394}
2395
françois romieu4da19632011-01-03 15:07:55 +00002396static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002397{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002398 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002399 { 0x1f, 0x0000 },
2400 { 0x1d, 0x0f00 },
2401 { 0x1f, 0x0002 },
2402 { 0x0c, 0x1ec8 },
2403 { 0x1f, 0x0000 }
2404 };
2405
françois romieu4da19632011-01-03 15:07:55 +00002406 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002407}
2408
françois romieu4da19632011-01-03 15:07:55 +00002409static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002410{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002411 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002412 { 0x1f, 0x0001 },
2413 { 0x1d, 0x3d98 },
2414 { 0x1f, 0x0000 }
2415 };
2416
françois romieu4da19632011-01-03 15:07:55 +00002417 rtl_writephy(tp, 0x1f, 0x0000);
2418 rtl_patchphy(tp, 0x14, 1 << 5);
2419 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002420
françois romieu4da19632011-01-03 15:07:55 +00002421 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002422}
2423
françois romieu4da19632011-01-03 15:07:55 +00002424static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002425{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002426 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002427 { 0x1f, 0x0001 },
2428 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002429 { 0x1f, 0x0002 },
2430 { 0x00, 0x88d4 },
2431 { 0x01, 0x82b1 },
2432 { 0x03, 0x7002 },
2433 { 0x08, 0x9e30 },
2434 { 0x09, 0x01f0 },
2435 { 0x0a, 0x5500 },
2436 { 0x0c, 0x00c8 },
2437 { 0x1f, 0x0003 },
2438 { 0x12, 0xc096 },
2439 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002440 { 0x1f, 0x0000 },
2441 { 0x1f, 0x0000 },
2442 { 0x09, 0x2000 },
2443 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002444 };
2445
françois romieu4da19632011-01-03 15:07:55 +00002446 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_patchphy(tp, 0x14, 1 << 5);
2449 rtl_patchphy(tp, 0x0d, 1 << 5);
2450 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002451}
2452
françois romieu4da19632011-01-03 15:07:55 +00002453static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002454{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002455 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002456 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002457 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002458 { 0x03, 0x802f },
2459 { 0x02, 0x4f02 },
2460 { 0x01, 0x0409 },
2461 { 0x00, 0xf099 },
2462 { 0x04, 0x9800 },
2463 { 0x04, 0x9000 },
2464 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002465 { 0x1f, 0x0002 },
2466 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002467 { 0x06, 0x0761 },
2468 { 0x1f, 0x0003 },
2469 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002470 { 0x1f, 0x0000 }
2471 };
2472
françois romieu4da19632011-01-03 15:07:55 +00002473 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002474
françois romieu4da19632011-01-03 15:07:55 +00002475 rtl_patchphy(tp, 0x16, 1 << 0);
2476 rtl_patchphy(tp, 0x14, 1 << 5);
2477 rtl_patchphy(tp, 0x0d, 1 << 5);
2478 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002479}
2480
françois romieu4da19632011-01-03 15:07:55 +00002481static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002482{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002483 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002484 { 0x1f, 0x0001 },
2485 { 0x12, 0x2300 },
2486 { 0x1d, 0x3d98 },
2487 { 0x1f, 0x0002 },
2488 { 0x0c, 0x7eb8 },
2489 { 0x06, 0x5461 },
2490 { 0x1f, 0x0003 },
2491 { 0x16, 0x0f0a },
2492 { 0x1f, 0x0000 }
2493 };
2494
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002496
françois romieu4da19632011-01-03 15:07:55 +00002497 rtl_patchphy(tp, 0x16, 1 << 0);
2498 rtl_patchphy(tp, 0x14, 1 << 5);
2499 rtl_patchphy(tp, 0x0d, 1 << 5);
2500 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002501}
2502
françois romieu4da19632011-01-03 15:07:55 +00002503static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002504{
françois romieu4da19632011-01-03 15:07:55 +00002505 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002506}
2507
françois romieubca03d52011-01-03 15:07:31 +00002508static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002509{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002510 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002511 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002512 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002513 { 0x06, 0x4064 },
2514 { 0x07, 0x2863 },
2515 { 0x08, 0x059c },
2516 { 0x09, 0x26b4 },
2517 { 0x0a, 0x6a19 },
2518 { 0x0b, 0xdcc8 },
2519 { 0x10, 0xf06d },
2520 { 0x14, 0x7f68 },
2521 { 0x18, 0x7fd9 },
2522 { 0x1c, 0xf0ff },
2523 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002524 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002525 { 0x12, 0xf49f },
2526 { 0x13, 0x070b },
2527 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002528 { 0x14, 0x94c0 },
2529
2530 /*
2531 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002532 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002533 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002534 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002535 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002536 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002537 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002538 { 0x06, 0x5561 },
2539
2540 /*
2541 * Can not link to 1Gbps with bad cable
2542 * Decrease SNR threshold form 21.07dB to 19.04dB
2543 */
2544 { 0x1f, 0x0001 },
2545 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002546
2547 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002548 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002549 };
françois romieubca03d52011-01-03 15:07:31 +00002550 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002551
françois romieu4da19632011-01-03 15:07:55 +00002552 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002553
françois romieubca03d52011-01-03 15:07:31 +00002554 /*
2555 * Rx Error Issue
2556 * Fine Tune Switching regulator parameter
2557 */
françois romieu4da19632011-01-03 15:07:55 +00002558 rtl_writephy(tp, 0x1f, 0x0002);
2559 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2560 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002561
françois romieudaf9df62009-10-07 12:44:20 +00002562 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002563 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002564 { 0x1f, 0x0002 },
2565 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002566 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002567 { 0x05, 0x8330 },
2568 { 0x06, 0x669a },
2569 { 0x1f, 0x0002 }
2570 };
2571 int val;
2572
françois romieu4da19632011-01-03 15:07:55 +00002573 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002574
françois romieu4da19632011-01-03 15:07:55 +00002575 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002576
2577 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002578 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002579 0x0065, 0x0066, 0x0067, 0x0068,
2580 0x0069, 0x006a, 0x006b, 0x006c
2581 };
2582 int i;
2583
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002585
2586 val &= 0xff00;
2587 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002588 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002589 }
2590 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002591 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002592 { 0x1f, 0x0002 },
2593 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002594 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002595 { 0x05, 0x8330 },
2596 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002597 };
2598
françois romieu4da19632011-01-03 15:07:55 +00002599 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002600 }
2601
françois romieubca03d52011-01-03 15:07:31 +00002602 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002603 rtl_writephy(tp, 0x1f, 0x0002);
2604 rtl_patchphy(tp, 0x0d, 0x0300);
2605 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002606
françois romieubca03d52011-01-03 15:07:31 +00002607 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002608 rtl_writephy(tp, 0x1f, 0x0002);
2609 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2610 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002611
françois romieu4da19632011-01-03 15:07:55 +00002612 rtl_writephy(tp, 0x1f, 0x0005);
2613 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002614
2615 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002616
françois romieu4da19632011-01-03 15:07:55 +00002617 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002618}
2619
françois romieubca03d52011-01-03 15:07:31 +00002620static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002621{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002622 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002623 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002624 { 0x1f, 0x0001 },
2625 { 0x06, 0x4064 },
2626 { 0x07, 0x2863 },
2627 { 0x08, 0x059c },
2628 { 0x09, 0x26b4 },
2629 { 0x0a, 0x6a19 },
2630 { 0x0b, 0xdcc8 },
2631 { 0x10, 0xf06d },
2632 { 0x14, 0x7f68 },
2633 { 0x18, 0x7fd9 },
2634 { 0x1c, 0xf0ff },
2635 { 0x1d, 0x3d9c },
2636 { 0x1f, 0x0003 },
2637 { 0x12, 0xf49f },
2638 { 0x13, 0x070b },
2639 { 0x1a, 0x05ad },
2640 { 0x14, 0x94c0 },
2641
françois romieubca03d52011-01-03 15:07:31 +00002642 /*
2643 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002644 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002645 */
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x1f, 0x0002 },
2647 { 0x06, 0x5561 },
2648 { 0x1f, 0x0005 },
2649 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002650 { 0x06, 0x5561 },
2651
2652 /*
2653 * Can not link to 1Gbps with bad cable
2654 * Decrease SNR threshold form 21.07dB to 19.04dB
2655 */
2656 { 0x1f, 0x0001 },
2657 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002658
2659 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002660 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002661 };
françois romieubca03d52011-01-03 15:07:31 +00002662 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002663
françois romieu4da19632011-01-03 15:07:55 +00002664 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002665
2666 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002667 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002668 { 0x1f, 0x0002 },
2669 { 0x05, 0x669a },
2670 { 0x1f, 0x0005 },
2671 { 0x05, 0x8330 },
2672 { 0x06, 0x669a },
2673
2674 { 0x1f, 0x0002 }
2675 };
2676 int val;
2677
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002679
françois romieu4da19632011-01-03 15:07:55 +00002680 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002681 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002682 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002683 0x0065, 0x0066, 0x0067, 0x0068,
2684 0x0069, 0x006a, 0x006b, 0x006c
2685 };
2686 int i;
2687
françois romieu4da19632011-01-03 15:07:55 +00002688 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002689
2690 val &= 0xff00;
2691 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002693 }
2694 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002695 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002696 { 0x1f, 0x0002 },
2697 { 0x05, 0x2642 },
2698 { 0x1f, 0x0005 },
2699 { 0x05, 0x8330 },
2700 { 0x06, 0x2642 }
2701 };
2702
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002704 }
2705
françois romieubca03d52011-01-03 15:07:31 +00002706 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002707 rtl_writephy(tp, 0x1f, 0x0002);
2708 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2709 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002710
françois romieubca03d52011-01-03 15:07:31 +00002711 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002712 rtl_writephy(tp, 0x1f, 0x0002);
2713 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002714
françois romieu4da19632011-01-03 15:07:55 +00002715 rtl_writephy(tp, 0x1f, 0x0005);
2716 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002717
2718 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002719
françois romieu4da19632011-01-03 15:07:55 +00002720 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002721}
2722
françois romieu4da19632011-01-03 15:07:55 +00002723static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002724{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002725 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002726 { 0x1f, 0x0002 },
2727 { 0x10, 0x0008 },
2728 { 0x0d, 0x006c },
2729
2730 { 0x1f, 0x0000 },
2731 { 0x0d, 0xf880 },
2732
2733 { 0x1f, 0x0001 },
2734 { 0x17, 0x0cc0 },
2735
2736 { 0x1f, 0x0001 },
2737 { 0x0b, 0xa4d8 },
2738 { 0x09, 0x281c },
2739 { 0x07, 0x2883 },
2740 { 0x0a, 0x6b35 },
2741 { 0x1d, 0x3da4 },
2742 { 0x1c, 0xeffd },
2743 { 0x14, 0x7f52 },
2744 { 0x18, 0x7fc6 },
2745 { 0x08, 0x0601 },
2746 { 0x06, 0x4063 },
2747 { 0x10, 0xf074 },
2748 { 0x1f, 0x0003 },
2749 { 0x13, 0x0789 },
2750 { 0x12, 0xf4bd },
2751 { 0x1a, 0x04fd },
2752 { 0x14, 0x84b0 },
2753 { 0x1f, 0x0000 },
2754 { 0x00, 0x9200 },
2755
2756 { 0x1f, 0x0005 },
2757 { 0x01, 0x0340 },
2758 { 0x1f, 0x0001 },
2759 { 0x04, 0x4000 },
2760 { 0x03, 0x1d21 },
2761 { 0x02, 0x0c32 },
2762 { 0x01, 0x0200 },
2763 { 0x00, 0x5554 },
2764 { 0x04, 0x4800 },
2765 { 0x04, 0x4000 },
2766 { 0x04, 0xf000 },
2767 { 0x03, 0xdf01 },
2768 { 0x02, 0xdf20 },
2769 { 0x01, 0x101a },
2770 { 0x00, 0xa0ff },
2771 { 0x04, 0xf800 },
2772 { 0x04, 0xf000 },
2773 { 0x1f, 0x0000 },
2774
2775 { 0x1f, 0x0007 },
2776 { 0x1e, 0x0023 },
2777 { 0x16, 0x0000 },
2778 { 0x1f, 0x0000 }
2779 };
2780
françois romieu4da19632011-01-03 15:07:55 +00002781 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002782}
2783
françois romieue6de30d2011-01-03 15:08:37 +00002784static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2785{
2786 static const struct phy_reg phy_reg_init[] = {
2787 { 0x1f, 0x0001 },
2788 { 0x17, 0x0cc0 },
2789
2790 { 0x1f, 0x0007 },
2791 { 0x1e, 0x002d },
2792 { 0x18, 0x0040 },
2793 { 0x1f, 0x0000 }
2794 };
2795
2796 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2797 rtl_patchphy(tp, 0x0d, 1 << 5);
2798}
2799
Hayes Wang70090422011-07-06 15:58:06 +08002800static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002801{
2802 static const struct phy_reg phy_reg_init[] = {
2803 /* Enable Delay cap */
2804 { 0x1f, 0x0005 },
2805 { 0x05, 0x8b80 },
2806 { 0x06, 0xc896 },
2807 { 0x1f, 0x0000 },
2808
2809 /* Channel estimation fine tune */
2810 { 0x1f, 0x0001 },
2811 { 0x0b, 0x6c20 },
2812 { 0x07, 0x2872 },
2813 { 0x1c, 0xefff },
2814 { 0x1f, 0x0003 },
2815 { 0x14, 0x6420 },
2816 { 0x1f, 0x0000 },
2817
2818 /* Update PFM & 10M TX idle timer */
2819 { 0x1f, 0x0007 },
2820 { 0x1e, 0x002f },
2821 { 0x15, 0x1919 },
2822 { 0x1f, 0x0000 },
2823
2824 { 0x1f, 0x0007 },
2825 { 0x1e, 0x00ac },
2826 { 0x18, 0x0006 },
2827 { 0x1f, 0x0000 }
2828 };
2829
Francois Romieu15ecd032011-04-27 13:52:22 -07002830 rtl_apply_firmware(tp);
2831
hayeswang01dc7fe2011-03-21 01:50:28 +00002832 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2833
2834 /* DCO enable for 10M IDLE Power */
2835 rtl_writephy(tp, 0x1f, 0x0007);
2836 rtl_writephy(tp, 0x1e, 0x0023);
2837 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2838 rtl_writephy(tp, 0x1f, 0x0000);
2839
2840 /* For impedance matching */
2841 rtl_writephy(tp, 0x1f, 0x0002);
2842 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002843 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002844
2845 /* PHY auto speed down */
2846 rtl_writephy(tp, 0x1f, 0x0007);
2847 rtl_writephy(tp, 0x1e, 0x002d);
2848 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2849 rtl_writephy(tp, 0x1f, 0x0000);
2850 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2851
2852 rtl_writephy(tp, 0x1f, 0x0005);
2853 rtl_writephy(tp, 0x05, 0x8b86);
2854 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2855 rtl_writephy(tp, 0x1f, 0x0000);
2856
2857 rtl_writephy(tp, 0x1f, 0x0005);
2858 rtl_writephy(tp, 0x05, 0x8b85);
2859 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2860 rtl_writephy(tp, 0x1f, 0x0007);
2861 rtl_writephy(tp, 0x1e, 0x0020);
2862 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2863 rtl_writephy(tp, 0x1f, 0x0006);
2864 rtl_writephy(tp, 0x00, 0x5a00);
2865 rtl_writephy(tp, 0x1f, 0x0000);
2866 rtl_writephy(tp, 0x0d, 0x0007);
2867 rtl_writephy(tp, 0x0e, 0x003c);
2868 rtl_writephy(tp, 0x0d, 0x4007);
2869 rtl_writephy(tp, 0x0e, 0x0000);
2870 rtl_writephy(tp, 0x0d, 0x0000);
2871}
2872
Hayes Wang70090422011-07-06 15:58:06 +08002873static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2874{
2875 static const struct phy_reg phy_reg_init[] = {
2876 /* Enable Delay cap */
2877 { 0x1f, 0x0004 },
2878 { 0x1f, 0x0007 },
2879 { 0x1e, 0x00ac },
2880 { 0x18, 0x0006 },
2881 { 0x1f, 0x0002 },
2882 { 0x1f, 0x0000 },
2883 { 0x1f, 0x0000 },
2884
2885 /* Channel estimation fine tune */
2886 { 0x1f, 0x0003 },
2887 { 0x09, 0xa20f },
2888 { 0x1f, 0x0000 },
2889 { 0x1f, 0x0000 },
2890
2891 /* Green Setting */
2892 { 0x1f, 0x0005 },
2893 { 0x05, 0x8b5b },
2894 { 0x06, 0x9222 },
2895 { 0x05, 0x8b6d },
2896 { 0x06, 0x8000 },
2897 { 0x05, 0x8b76 },
2898 { 0x06, 0x8000 },
2899 { 0x1f, 0x0000 }
2900 };
2901
2902 rtl_apply_firmware(tp);
2903
2904 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2905
2906 /* For 4-corner performance improve */
2907 rtl_writephy(tp, 0x1f, 0x0005);
2908 rtl_writephy(tp, 0x05, 0x8b80);
2909 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2910 rtl_writephy(tp, 0x1f, 0x0000);
2911
2912 /* PHY auto speed down */
2913 rtl_writephy(tp, 0x1f, 0x0004);
2914 rtl_writephy(tp, 0x1f, 0x0007);
2915 rtl_writephy(tp, 0x1e, 0x002d);
2916 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2917 rtl_writephy(tp, 0x1f, 0x0002);
2918 rtl_writephy(tp, 0x1f, 0x0000);
2919 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2920
2921 /* improve 10M EEE waveform */
2922 rtl_writephy(tp, 0x1f, 0x0005);
2923 rtl_writephy(tp, 0x05, 0x8b86);
2924 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2925 rtl_writephy(tp, 0x1f, 0x0000);
2926
2927 /* Improve 2-pair detection performance */
2928 rtl_writephy(tp, 0x1f, 0x0005);
2929 rtl_writephy(tp, 0x05, 0x8b85);
2930 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2931 rtl_writephy(tp, 0x1f, 0x0000);
2932
2933 /* EEE setting */
2934 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2935 ERIAR_EXGMAC);
2936 rtl_writephy(tp, 0x1f, 0x0005);
2937 rtl_writephy(tp, 0x05, 0x8b85);
2938 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2939 rtl_writephy(tp, 0x1f, 0x0004);
2940 rtl_writephy(tp, 0x1f, 0x0007);
2941 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04002942 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08002943 rtl_writephy(tp, 0x1f, 0x0002);
2944 rtl_writephy(tp, 0x1f, 0x0000);
2945 rtl_writephy(tp, 0x0d, 0x0007);
2946 rtl_writephy(tp, 0x0e, 0x003c);
2947 rtl_writephy(tp, 0x0d, 0x4007);
2948 rtl_writephy(tp, 0x0e, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0000);
2950
2951 /* Green feature */
2952 rtl_writephy(tp, 0x1f, 0x0003);
2953 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2954 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2955 rtl_writephy(tp, 0x1f, 0x0000);
2956}
2957
Hayes Wangc2218922011-09-06 16:55:18 +08002958static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2959{
2960 static const struct phy_reg phy_reg_init[] = {
2961 /* Channel estimation fine tune */
2962 { 0x1f, 0x0003 },
2963 { 0x09, 0xa20f },
2964 { 0x1f, 0x0000 },
2965
2966 /* Modify green table for giga & fnet */
2967 { 0x1f, 0x0005 },
2968 { 0x05, 0x8b55 },
2969 { 0x06, 0x0000 },
2970 { 0x05, 0x8b5e },
2971 { 0x06, 0x0000 },
2972 { 0x05, 0x8b67 },
2973 { 0x06, 0x0000 },
2974 { 0x05, 0x8b70 },
2975 { 0x06, 0x0000 },
2976 { 0x1f, 0x0000 },
2977 { 0x1f, 0x0007 },
2978 { 0x1e, 0x0078 },
2979 { 0x17, 0x0000 },
2980 { 0x19, 0x00fb },
2981 { 0x1f, 0x0000 },
2982
2983 /* Modify green table for 10M */
2984 { 0x1f, 0x0005 },
2985 { 0x05, 0x8b79 },
2986 { 0x06, 0xaa00 },
2987 { 0x1f, 0x0000 },
2988
2989 /* Disable hiimpedance detection (RTCT) */
2990 { 0x1f, 0x0003 },
2991 { 0x01, 0x328a },
2992 { 0x1f, 0x0000 }
2993 };
2994
2995 rtl_apply_firmware(tp);
2996
2997 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2998
2999 /* For 4-corner performance improve */
3000 rtl_writephy(tp, 0x1f, 0x0005);
3001 rtl_writephy(tp, 0x05, 0x8b80);
3002 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3003 rtl_writephy(tp, 0x1f, 0x0000);
3004
3005 /* PHY auto speed down */
3006 rtl_writephy(tp, 0x1f, 0x0007);
3007 rtl_writephy(tp, 0x1e, 0x002d);
3008 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3009 rtl_writephy(tp, 0x1f, 0x0000);
3010 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3011
3012 /* Improve 10M EEE waveform */
3013 rtl_writephy(tp, 0x1f, 0x0005);
3014 rtl_writephy(tp, 0x05, 0x8b86);
3015 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3016 rtl_writephy(tp, 0x1f, 0x0000);
3017
3018 /* Improve 2-pair detection performance */
3019 rtl_writephy(tp, 0x1f, 0x0005);
3020 rtl_writephy(tp, 0x05, 0x8b85);
3021 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3022 rtl_writephy(tp, 0x1f, 0x0000);
3023}
3024
3025static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3026{
3027 rtl_apply_firmware(tp);
3028
3029 /* For 4-corner performance improve */
3030 rtl_writephy(tp, 0x1f, 0x0005);
3031 rtl_writephy(tp, 0x05, 0x8b80);
3032 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3033 rtl_writephy(tp, 0x1f, 0x0000);
3034
3035 /* PHY auto speed down */
3036 rtl_writephy(tp, 0x1f, 0x0007);
3037 rtl_writephy(tp, 0x1e, 0x002d);
3038 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3039 rtl_writephy(tp, 0x1f, 0x0000);
3040 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3041
3042 /* Improve 10M EEE waveform */
3043 rtl_writephy(tp, 0x1f, 0x0005);
3044 rtl_writephy(tp, 0x05, 0x8b86);
3045 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3046 rtl_writephy(tp, 0x1f, 0x0000);
3047}
3048
françois romieu4da19632011-01-03 15:07:55 +00003049static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003050{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003051 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003052 { 0x1f, 0x0003 },
3053 { 0x08, 0x441d },
3054 { 0x01, 0x9100 },
3055 { 0x1f, 0x0000 }
3056 };
3057
françois romieu4da19632011-01-03 15:07:55 +00003058 rtl_writephy(tp, 0x1f, 0x0000);
3059 rtl_patchphy(tp, 0x11, 1 << 12);
3060 rtl_patchphy(tp, 0x19, 1 << 13);
3061 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003062
françois romieu4da19632011-01-03 15:07:55 +00003063 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003064}
3065
Hayes Wang5a5e4442011-02-22 17:26:21 +08003066static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3067{
3068 static const struct phy_reg phy_reg_init[] = {
3069 { 0x1f, 0x0005 },
3070 { 0x1a, 0x0000 },
3071 { 0x1f, 0x0000 },
3072
3073 { 0x1f, 0x0004 },
3074 { 0x1c, 0x0000 },
3075 { 0x1f, 0x0000 },
3076
3077 { 0x1f, 0x0001 },
3078 { 0x15, 0x7701 },
3079 { 0x1f, 0x0000 }
3080 };
3081
3082 /* Disable ALDPS before ram code */
3083 rtl_writephy(tp, 0x1f, 0x0000);
3084 rtl_writephy(tp, 0x18, 0x0310);
3085 msleep(100);
3086
François Romieu953a12c2011-04-24 17:38:48 +02003087 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003088
3089 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3090}
3091
Francois Romieu5615d9f2007-08-17 17:50:46 +02003092static void rtl_hw_phy_config(struct net_device *dev)
3093{
3094 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003095
3096 rtl8169_print_mac_version(tp);
3097
3098 switch (tp->mac_version) {
3099 case RTL_GIGA_MAC_VER_01:
3100 break;
3101 case RTL_GIGA_MAC_VER_02:
3102 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003103 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003104 break;
3105 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003106 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003107 break;
françois romieu2e9558562009-08-10 19:44:19 +00003108 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003109 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003110 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003111 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003112 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003113 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003114 case RTL_GIGA_MAC_VER_07:
3115 case RTL_GIGA_MAC_VER_08:
3116 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003117 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003118 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003119 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003120 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003121 break;
3122 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003123 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003124 break;
3125 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003126 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003127 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003128 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003129 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003130 break;
3131 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003132 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003133 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003134 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003135 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003136 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003137 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003138 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003139 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003140 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003141 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003142 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003143 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003144 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003145 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003146 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003147 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003148 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003149 break;
3150 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003151 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003152 break;
3153 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003154 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003155 break;
françois romieue6de30d2011-01-03 15:08:37 +00003156 case RTL_GIGA_MAC_VER_28:
3157 rtl8168d_4_hw_phy_config(tp);
3158 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003159 case RTL_GIGA_MAC_VER_29:
3160 case RTL_GIGA_MAC_VER_30:
3161 rtl8105e_hw_phy_config(tp);
3162 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003163 case RTL_GIGA_MAC_VER_31:
3164 /* None. */
3165 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003166 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003167 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003168 rtl8168e_1_hw_phy_config(tp);
3169 break;
3170 case RTL_GIGA_MAC_VER_34:
3171 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003172 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003173 case RTL_GIGA_MAC_VER_35:
3174 rtl8168f_1_hw_phy_config(tp);
3175 break;
3176 case RTL_GIGA_MAC_VER_36:
3177 rtl8168f_2_hw_phy_config(tp);
3178 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003179
Francois Romieu5615d9f2007-08-17 17:50:46 +02003180 default:
3181 break;
3182 }
3183}
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185static void rtl8169_phy_timer(unsigned long __opaque)
3186{
3187 struct net_device *dev = (struct net_device *)__opaque;
3188 struct rtl8169_private *tp = netdev_priv(dev);
3189 struct timer_list *timer = &tp->timer;
3190 void __iomem *ioaddr = tp->mmio_addr;
3191 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3192
Francois Romieubcf0bf92006-07-26 23:14:13 +02003193 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 spin_lock_irq(&tp->lock);
3196
françois romieu4da19632011-01-03 15:07:55 +00003197 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003198 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 * A busy loop could burn quite a few cycles on nowadays CPU.
3200 * Let's delay the execution of the timer for a few ticks.
3201 */
3202 timeout = HZ/10;
3203 goto out_mod_timer;
3204 }
3205
3206 if (tp->link_ok(ioaddr))
3207 goto out_unlock;
3208
Joe Perchesbf82c182010-02-09 11:49:50 +00003209 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
françois romieu4da19632011-01-03 15:07:55 +00003211 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213out_mod_timer:
3214 mod_timer(timer, jiffies + timeout);
3215out_unlock:
3216 spin_unlock_irq(&tp->lock);
3217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219#ifdef CONFIG_NET_POLL_CONTROLLER
3220/*
3221 * Polling 'interrupt' - used by things like netconsole to send skbs
3222 * without having to re-enable interrupts. It's not called while
3223 * the interrupt routine is executing.
3224 */
3225static void rtl8169_netpoll(struct net_device *dev)
3226{
3227 struct rtl8169_private *tp = netdev_priv(dev);
3228 struct pci_dev *pdev = tp->pci_dev;
3229
3230 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003231 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 enable_irq(pdev->irq);
3233}
3234#endif
3235
3236static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3237 void __iomem *ioaddr)
3238{
3239 iounmap(ioaddr);
3240 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003241 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 pci_disable_device(pdev);
3243 free_netdev(dev);
3244}
3245
Francois Romieubf793292006-11-01 00:53:05 +01003246static void rtl8169_phy_reset(struct net_device *dev,
3247 struct rtl8169_private *tp)
3248{
Francois Romieu07d3f512007-02-21 22:40:46 +01003249 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003250
françois romieu4da19632011-01-03 15:07:55 +00003251 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003252 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003253 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003254 return;
3255 msleep(1);
3256 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003257 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003258}
3259
David S. Miller8decf862011-09-22 03:23:13 -04003260static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3261{
3262 void __iomem *ioaddr = tp->mmio_addr;
3263
3264 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3265 (RTL_R8(PHYstatus) & TBI_Enable);
3266}
3267
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003268static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003270 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003271
Francois Romieu5615d9f2007-08-17 17:50:46 +02003272 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003273
Marcus Sundberg773328942008-07-10 21:28:08 +02003274 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3275 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3276 RTL_W8(0x82, 0x01);
3277 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003278
Francois Romieu6dccd162007-02-13 23:38:05 +01003279 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3280
3281 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3282 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003283
Francois Romieubcf0bf92006-07-26 23:14:13 +02003284 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003285 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3286 RTL_W8(0x82, 0x01);
3287 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003288 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003289 }
3290
Francois Romieubf793292006-11-01 00:53:05 +01003291 rtl8169_phy_reset(dev, tp);
3292
Oliver Neukum54405cd2011-01-06 21:55:13 +01003293 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003294 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3295 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3296 (tp->mii.supports_gmii ?
3297 ADVERTISED_1000baseT_Half |
3298 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003299
David S. Miller8decf862011-09-22 03:23:13 -04003300 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003301 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003302}
3303
Francois Romieu773d2022007-01-31 23:47:43 +01003304static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3305{
3306 void __iomem *ioaddr = tp->mmio_addr;
3307 u32 high;
3308 u32 low;
3309
3310 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3311 high = addr[4] | (addr[5] << 8);
3312
3313 spin_lock_irq(&tp->lock);
3314
3315 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00003316
Francois Romieu773d2022007-01-31 23:47:43 +01003317 RTL_W32(MAC4, high);
françois romieu908ba2bf2010-04-26 11:42:58 +00003318 RTL_R32(MAC4);
3319
Francois Romieu78f1cd02010-03-27 19:35:46 -07003320 RTL_W32(MAC0, low);
françois romieu908ba2bf2010-04-26 11:42:58 +00003321 RTL_R32(MAC0);
3322
françois romieuc28aa382011-08-02 03:53:43 +00003323 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3324 const struct exgmac_reg e[] = {
3325 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3326 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3327 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3328 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3329 low >> 16 },
3330 };
3331
3332 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3333 }
3334
Francois Romieu773d2022007-01-31 23:47:43 +01003335 RTL_W8(Cfg9346, Cfg9346_Lock);
3336
3337 spin_unlock_irq(&tp->lock);
3338}
3339
3340static int rtl_set_mac_address(struct net_device *dev, void *p)
3341{
3342 struct rtl8169_private *tp = netdev_priv(dev);
3343 struct sockaddr *addr = p;
3344
3345 if (!is_valid_ether_addr(addr->sa_data))
3346 return -EADDRNOTAVAIL;
3347
3348 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3349
3350 rtl_rar_set(tp, dev->dev_addr);
3351
3352 return 0;
3353}
3354
Francois Romieu5f787a12006-08-17 13:02:36 +02003355static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3356{
3357 struct rtl8169_private *tp = netdev_priv(dev);
3358 struct mii_ioctl_data *data = if_mii(ifr);
3359
Francois Romieu8b4ab282008-11-19 22:05:25 -08003360 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3361}
Francois Romieu5f787a12006-08-17 13:02:36 +02003362
Francois Romieucecb5fd2011-04-01 10:21:07 +02003363static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3364 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003365{
Francois Romieu5f787a12006-08-17 13:02:36 +02003366 switch (cmd) {
3367 case SIOCGMIIPHY:
3368 data->phy_id = 32; /* Internal PHY */
3369 return 0;
3370
3371 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003372 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003373 return 0;
3374
3375 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003376 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003377 return 0;
3378 }
3379 return -EOPNOTSUPP;
3380}
3381
Francois Romieu8b4ab282008-11-19 22:05:25 -08003382static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3383{
3384 return -EOPNOTSUPP;
3385}
3386
Francois Romieu0e485152007-02-20 00:00:26 +01003387static const struct rtl_cfg_info {
3388 void (*hw_start)(struct net_device *);
3389 unsigned int region;
3390 unsigned int align;
3391 u16 intr_event;
3392 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003393 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003394 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003395} rtl_cfg_infos [] = {
3396 [RTL_CFG_0] = {
3397 .hw_start = rtl_hw_start_8169,
3398 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003399 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003400 .intr_event = SYSErr | LinkChg | RxOverflow |
3401 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003402 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003403 .features = RTL_FEATURE_GMII,
3404 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003405 },
3406 [RTL_CFG_1] = {
3407 .hw_start = rtl_hw_start_8168,
3408 .region = 2,
3409 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003410 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003411 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003412 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003413 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3414 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003415 },
3416 [RTL_CFG_2] = {
3417 .hw_start = rtl_hw_start_8101,
3418 .region = 2,
3419 .align = 8,
3420 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3421 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003422 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003423 .features = RTL_FEATURE_MSI,
3424 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003425 }
3426};
3427
Francois Romieufbac58f2007-10-04 22:51:38 +02003428/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003429static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003430 const struct rtl_cfg_info *cfg)
3431{
françois romieu2ca6cf02011-12-15 08:37:43 +00003432 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003433 unsigned msi = 0;
3434 u8 cfg2;
3435
3436 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003437 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003438 if (pci_enable_msi(tp->pci_dev)) {
3439 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003440 } else {
3441 cfg2 |= MSIEnable;
3442 msi = RTL_FEATURE_MSI;
3443 }
3444 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003445 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3446 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003447 return msi;
3448}
3449
3450static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3451{
3452 if (tp->features & RTL_FEATURE_MSI) {
3453 pci_disable_msi(pdev);
3454 tp->features &= ~RTL_FEATURE_MSI;
3455 }
3456}
3457
Francois Romieu8b4ab282008-11-19 22:05:25 -08003458static const struct net_device_ops rtl8169_netdev_ops = {
3459 .ndo_open = rtl8169_open,
3460 .ndo_stop = rtl8169_close,
3461 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003462 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003463 .ndo_tx_timeout = rtl8169_tx_timeout,
3464 .ndo_validate_addr = eth_validate_addr,
3465 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003466 .ndo_fix_features = rtl8169_fix_features,
3467 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003468 .ndo_set_mac_address = rtl_set_mac_address,
3469 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003470 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003471#ifdef CONFIG_NET_POLL_CONTROLLER
3472 .ndo_poll_controller = rtl8169_netpoll,
3473#endif
3474
3475};
3476
françois romieuc0e45c12011-01-03 15:08:04 +00003477static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3478{
3479 struct mdio_ops *ops = &tp->mdio_ops;
3480
3481 switch (tp->mac_version) {
3482 case RTL_GIGA_MAC_VER_27:
3483 ops->write = r8168dp_1_mdio_write;
3484 ops->read = r8168dp_1_mdio_read;
3485 break;
françois romieue6de30d2011-01-03 15:08:37 +00003486 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003487 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003488 ops->write = r8168dp_2_mdio_write;
3489 ops->read = r8168dp_2_mdio_read;
3490 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003491 default:
3492 ops->write = r8169_mdio_write;
3493 ops->read = r8169_mdio_read;
3494 break;
3495 }
3496}
3497
David S. Miller1805b2f2011-10-24 18:18:09 -04003498static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3499{
3500 void __iomem *ioaddr = tp->mmio_addr;
3501
3502 switch (tp->mac_version) {
3503 case RTL_GIGA_MAC_VER_29:
3504 case RTL_GIGA_MAC_VER_30:
3505 case RTL_GIGA_MAC_VER_32:
3506 case RTL_GIGA_MAC_VER_33:
3507 case RTL_GIGA_MAC_VER_34:
3508 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3509 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3510 break;
3511 default:
3512 break;
3513 }
3514}
3515
3516static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3517{
3518 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3519 return false;
3520
3521 rtl_writephy(tp, 0x1f, 0x0000);
3522 rtl_writephy(tp, MII_BMCR, 0x0000);
3523
3524 rtl_wol_suspend_quirk(tp);
3525
3526 return true;
3527}
3528
françois romieu065c27c2011-01-03 15:08:12 +00003529static void r810x_phy_power_down(struct rtl8169_private *tp)
3530{
3531 rtl_writephy(tp, 0x1f, 0x0000);
3532 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3533}
3534
3535static void r810x_phy_power_up(struct rtl8169_private *tp)
3536{
3537 rtl_writephy(tp, 0x1f, 0x0000);
3538 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3539}
3540
3541static void r810x_pll_power_down(struct rtl8169_private *tp)
3542{
David S. Miller1805b2f2011-10-24 18:18:09 -04003543 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003544 return;
françois romieu065c27c2011-01-03 15:08:12 +00003545
3546 r810x_phy_power_down(tp);
3547}
3548
3549static void r810x_pll_power_up(struct rtl8169_private *tp)
3550{
3551 r810x_phy_power_up(tp);
3552}
3553
3554static void r8168_phy_power_up(struct rtl8169_private *tp)
3555{
3556 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003557 switch (tp->mac_version) {
3558 case RTL_GIGA_MAC_VER_11:
3559 case RTL_GIGA_MAC_VER_12:
3560 case RTL_GIGA_MAC_VER_17:
3561 case RTL_GIGA_MAC_VER_18:
3562 case RTL_GIGA_MAC_VER_19:
3563 case RTL_GIGA_MAC_VER_20:
3564 case RTL_GIGA_MAC_VER_21:
3565 case RTL_GIGA_MAC_VER_22:
3566 case RTL_GIGA_MAC_VER_23:
3567 case RTL_GIGA_MAC_VER_24:
3568 case RTL_GIGA_MAC_VER_25:
3569 case RTL_GIGA_MAC_VER_26:
3570 case RTL_GIGA_MAC_VER_27:
3571 case RTL_GIGA_MAC_VER_28:
3572 case RTL_GIGA_MAC_VER_31:
3573 rtl_writephy(tp, 0x0e, 0x0000);
3574 break;
3575 default:
3576 break;
3577 }
françois romieu065c27c2011-01-03 15:08:12 +00003578 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3579}
3580
3581static void r8168_phy_power_down(struct rtl8169_private *tp)
3582{
3583 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003584 switch (tp->mac_version) {
3585 case RTL_GIGA_MAC_VER_32:
3586 case RTL_GIGA_MAC_VER_33:
3587 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3588 break;
3589
3590 case RTL_GIGA_MAC_VER_11:
3591 case RTL_GIGA_MAC_VER_12:
3592 case RTL_GIGA_MAC_VER_17:
3593 case RTL_GIGA_MAC_VER_18:
3594 case RTL_GIGA_MAC_VER_19:
3595 case RTL_GIGA_MAC_VER_20:
3596 case RTL_GIGA_MAC_VER_21:
3597 case RTL_GIGA_MAC_VER_22:
3598 case RTL_GIGA_MAC_VER_23:
3599 case RTL_GIGA_MAC_VER_24:
3600 case RTL_GIGA_MAC_VER_25:
3601 case RTL_GIGA_MAC_VER_26:
3602 case RTL_GIGA_MAC_VER_27:
3603 case RTL_GIGA_MAC_VER_28:
3604 case RTL_GIGA_MAC_VER_31:
3605 rtl_writephy(tp, 0x0e, 0x0200);
3606 default:
3607 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3608 break;
3609 }
françois romieu065c27c2011-01-03 15:08:12 +00003610}
3611
3612static void r8168_pll_power_down(struct rtl8169_private *tp)
3613{
3614 void __iomem *ioaddr = tp->mmio_addr;
3615
Francois Romieucecb5fd2011-04-01 10:21:07 +02003616 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3617 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3618 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003619 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003620 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003621 }
françois romieu065c27c2011-01-03 15:08:12 +00003622
Francois Romieucecb5fd2011-04-01 10:21:07 +02003623 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3624 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003625 (RTL_R16(CPlusCmd) & ASF)) {
3626 return;
3627 }
3628
hayeswang01dc7fe2011-03-21 01:50:28 +00003629 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3630 tp->mac_version == RTL_GIGA_MAC_VER_33)
3631 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3632
David S. Miller1805b2f2011-10-24 18:18:09 -04003633 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003634 return;
françois romieu065c27c2011-01-03 15:08:12 +00003635
3636 r8168_phy_power_down(tp);
3637
3638 switch (tp->mac_version) {
3639 case RTL_GIGA_MAC_VER_25:
3640 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003641 case RTL_GIGA_MAC_VER_27:
3642 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003643 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003644 case RTL_GIGA_MAC_VER_32:
3645 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003646 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3647 break;
3648 }
3649}
3650
3651static void r8168_pll_power_up(struct rtl8169_private *tp)
3652{
3653 void __iomem *ioaddr = tp->mmio_addr;
3654
Francois Romieucecb5fd2011-04-01 10:21:07 +02003655 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3656 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3657 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003658 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003659 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003660 }
françois romieu065c27c2011-01-03 15:08:12 +00003661
3662 switch (tp->mac_version) {
3663 case RTL_GIGA_MAC_VER_25:
3664 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003665 case RTL_GIGA_MAC_VER_27:
3666 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003667 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003668 case RTL_GIGA_MAC_VER_32:
3669 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003670 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3671 break;
3672 }
3673
3674 r8168_phy_power_up(tp);
3675}
3676
Francois Romieud58d46b2011-05-03 16:38:29 +02003677static void rtl_generic_op(struct rtl8169_private *tp,
3678 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003679{
3680 if (op)
3681 op(tp);
3682}
3683
3684static void rtl_pll_power_down(struct rtl8169_private *tp)
3685{
Francois Romieud58d46b2011-05-03 16:38:29 +02003686 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003687}
3688
3689static void rtl_pll_power_up(struct rtl8169_private *tp)
3690{
Francois Romieud58d46b2011-05-03 16:38:29 +02003691 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003692}
3693
3694static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3695{
3696 struct pll_power_ops *ops = &tp->pll_power_ops;
3697
3698 switch (tp->mac_version) {
3699 case RTL_GIGA_MAC_VER_07:
3700 case RTL_GIGA_MAC_VER_08:
3701 case RTL_GIGA_MAC_VER_09:
3702 case RTL_GIGA_MAC_VER_10:
3703 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003704 case RTL_GIGA_MAC_VER_29:
3705 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003706 ops->down = r810x_pll_power_down;
3707 ops->up = r810x_pll_power_up;
3708 break;
3709
3710 case RTL_GIGA_MAC_VER_11:
3711 case RTL_GIGA_MAC_VER_12:
3712 case RTL_GIGA_MAC_VER_17:
3713 case RTL_GIGA_MAC_VER_18:
3714 case RTL_GIGA_MAC_VER_19:
3715 case RTL_GIGA_MAC_VER_20:
3716 case RTL_GIGA_MAC_VER_21:
3717 case RTL_GIGA_MAC_VER_22:
3718 case RTL_GIGA_MAC_VER_23:
3719 case RTL_GIGA_MAC_VER_24:
3720 case RTL_GIGA_MAC_VER_25:
3721 case RTL_GIGA_MAC_VER_26:
3722 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003723 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003724 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003725 case RTL_GIGA_MAC_VER_32:
3726 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003727 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003728 case RTL_GIGA_MAC_VER_35:
3729 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003730 ops->down = r8168_pll_power_down;
3731 ops->up = r8168_pll_power_up;
3732 break;
3733
3734 default:
3735 ops->down = NULL;
3736 ops->up = NULL;
3737 break;
3738 }
3739}
3740
Hayes Wange542a222011-07-06 15:58:04 +08003741static void rtl_init_rxcfg(struct rtl8169_private *tp)
3742{
3743 void __iomem *ioaddr = tp->mmio_addr;
3744
3745 switch (tp->mac_version) {
3746 case RTL_GIGA_MAC_VER_01:
3747 case RTL_GIGA_MAC_VER_02:
3748 case RTL_GIGA_MAC_VER_03:
3749 case RTL_GIGA_MAC_VER_04:
3750 case RTL_GIGA_MAC_VER_05:
3751 case RTL_GIGA_MAC_VER_06:
3752 case RTL_GIGA_MAC_VER_10:
3753 case RTL_GIGA_MAC_VER_11:
3754 case RTL_GIGA_MAC_VER_12:
3755 case RTL_GIGA_MAC_VER_13:
3756 case RTL_GIGA_MAC_VER_14:
3757 case RTL_GIGA_MAC_VER_15:
3758 case RTL_GIGA_MAC_VER_16:
3759 case RTL_GIGA_MAC_VER_17:
3760 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3761 break;
3762 case RTL_GIGA_MAC_VER_18:
3763 case RTL_GIGA_MAC_VER_19:
3764 case RTL_GIGA_MAC_VER_20:
3765 case RTL_GIGA_MAC_VER_21:
3766 case RTL_GIGA_MAC_VER_22:
3767 case RTL_GIGA_MAC_VER_23:
3768 case RTL_GIGA_MAC_VER_24:
3769 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3770 break;
3771 default:
3772 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3773 break;
3774 }
3775}
3776
Hayes Wang92fc43b2011-07-06 15:58:03 +08003777static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3778{
3779 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3780}
3781
Francois Romieud58d46b2011-05-03 16:38:29 +02003782static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3783{
3784 rtl_generic_op(tp, tp->jumbo_ops.enable);
3785}
3786
3787static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3788{
3789 rtl_generic_op(tp, tp->jumbo_ops.disable);
3790}
3791
3792static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3793{
3794 void __iomem *ioaddr = tp->mmio_addr;
3795
3796 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3797 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3798 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3799}
3800
3801static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3802{
3803 void __iomem *ioaddr = tp->mmio_addr;
3804
3805 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3806 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3807 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3808}
3809
3810static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3811{
3812 void __iomem *ioaddr = tp->mmio_addr;
3813
3814 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3815}
3816
3817static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3818{
3819 void __iomem *ioaddr = tp->mmio_addr;
3820
3821 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3822}
3823
3824static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3825{
3826 void __iomem *ioaddr = tp->mmio_addr;
3827 struct pci_dev *pdev = tp->pci_dev;
3828
3829 RTL_W8(MaxTxPacketSize, 0x3f);
3830 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3831 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
3832 pci_write_config_byte(pdev, 0x79, 0x20);
3833}
3834
3835static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3836{
3837 void __iomem *ioaddr = tp->mmio_addr;
3838 struct pci_dev *pdev = tp->pci_dev;
3839
3840 RTL_W8(MaxTxPacketSize, 0x0c);
3841 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3842 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
3843 pci_write_config_byte(pdev, 0x79, 0x50);
3844}
3845
3846static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3847{
3848 rtl_tx_performance_tweak(tp->pci_dev,
3849 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3850}
3851
3852static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3853{
3854 rtl_tx_performance_tweak(tp->pci_dev,
3855 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3856}
3857
3858static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3859{
3860 void __iomem *ioaddr = tp->mmio_addr;
3861
3862 r8168b_0_hw_jumbo_enable(tp);
3863
3864 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3865}
3866
3867static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3868{
3869 void __iomem *ioaddr = tp->mmio_addr;
3870
3871 r8168b_0_hw_jumbo_disable(tp);
3872
3873 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3874}
3875
3876static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3877{
3878 struct jumbo_ops *ops = &tp->jumbo_ops;
3879
3880 switch (tp->mac_version) {
3881 case RTL_GIGA_MAC_VER_11:
3882 ops->disable = r8168b_0_hw_jumbo_disable;
3883 ops->enable = r8168b_0_hw_jumbo_enable;
3884 break;
3885 case RTL_GIGA_MAC_VER_12:
3886 case RTL_GIGA_MAC_VER_17:
3887 ops->disable = r8168b_1_hw_jumbo_disable;
3888 ops->enable = r8168b_1_hw_jumbo_enable;
3889 break;
3890 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3891 case RTL_GIGA_MAC_VER_19:
3892 case RTL_GIGA_MAC_VER_20:
3893 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3894 case RTL_GIGA_MAC_VER_22:
3895 case RTL_GIGA_MAC_VER_23:
3896 case RTL_GIGA_MAC_VER_24:
3897 case RTL_GIGA_MAC_VER_25:
3898 case RTL_GIGA_MAC_VER_26:
3899 ops->disable = r8168c_hw_jumbo_disable;
3900 ops->enable = r8168c_hw_jumbo_enable;
3901 break;
3902 case RTL_GIGA_MAC_VER_27:
3903 case RTL_GIGA_MAC_VER_28:
3904 ops->disable = r8168dp_hw_jumbo_disable;
3905 ops->enable = r8168dp_hw_jumbo_enable;
3906 break;
3907 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3908 case RTL_GIGA_MAC_VER_32:
3909 case RTL_GIGA_MAC_VER_33:
3910 case RTL_GIGA_MAC_VER_34:
3911 ops->disable = r8168e_hw_jumbo_disable;
3912 ops->enable = r8168e_hw_jumbo_enable;
3913 break;
3914
3915 /*
3916 * No action needed for jumbo frames with 8169.
3917 * No jumbo for 810x at all.
3918 */
3919 default:
3920 ops->disable = NULL;
3921 ops->enable = NULL;
3922 break;
3923 }
3924}
3925
Francois Romieu6f43adc2011-04-29 15:05:51 +02003926static void rtl_hw_reset(struct rtl8169_private *tp)
3927{
3928 void __iomem *ioaddr = tp->mmio_addr;
3929 int i;
3930
3931 /* Soft reset the chip. */
3932 RTL_W8(ChipCmd, CmdReset);
3933
3934 /* Check that the chip has finished the reset. */
3935 for (i = 0; i < 100; i++) {
3936 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3937 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003938 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003939 }
3940}
3941
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003942static int __devinit
3943rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3944{
Francois Romieu0e485152007-02-20 00:00:26 +01003945 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3946 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003948 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003949 struct net_device *dev;
3950 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003951 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003952 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003954 if (netif_msg_drv(&debug)) {
3955 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3956 MODULENAME, RTL8169_VERSION);
3957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003960 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003961 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003962 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003963 rc = -ENOMEM;
3964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003968 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003970 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003971 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003972 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Francois Romieuccdffb92008-07-26 14:26:06 +02003974 mii = &tp->mii;
3975 mii->dev = dev;
3976 mii->mdio_read = rtl_mdio_read;
3977 mii->mdio_write = rtl_mdio_write;
3978 mii->phy_id_mask = 0x1f;
3979 mii->reg_num_mask = 0x1f;
3980 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3981
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003982 /* disable ASPM completely as that cause random device stop working
3983 * problems as well as full system hangs for some PCIe devices users */
3984 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3985 PCIE_LINK_STATE_CLKPM);
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3988 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003989 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003990 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003991 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
3993
françois romieu87aeec72010-04-26 11:42:06 +00003994 if (pci_set_mwi(pdev) < 0)
3995 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003998 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003999 netif_err(tp, probe, dev,
4000 "region #%d not an MMIO resource, aborting\n",
4001 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004003 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004007 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004008 netif_err(tp, probe, dev,
4009 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004011 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 }
4013
4014 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004015 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004016 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004017 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 }
4019
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004020 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004023 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 tp->cp_cmd |= PCIDAC;
4025 dev->features |= NETIF_F_HIGHDMA;
4026 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004027 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004029 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004030 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
4032 }
4033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004035 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004036 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004037 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004039 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004041 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Jon Masone44daad2011-06-27 07:46:31 +00004043 if (!pci_is_pcie(pdev))
4044 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004045
Hayes Wange542a222011-07-06 15:58:04 +08004046 /* Identify chip attached to board */
4047 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4048
4049 rtl_init_rxcfg(tp);
4050
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004051 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Francois Romieu6f43adc2011-04-29 15:05:51 +02004053 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004055 RTL_W16(IntrStatus, 0xffff);
4056
françois romieuca52efd2009-07-24 12:34:19 +00004057 pci_set_master(pdev);
4058
Francois Romieu7a8fc772011-03-01 17:18:33 +01004059 /*
4060 * Pretend we are using VLANs; This bypasses a nasty bug where
4061 * Interrupts stop flowing on high load on 8110SCd controllers.
4062 */
4063 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4064 tp->cp_cmd |= RxVlan;
4065
françois romieuc0e45c12011-01-03 15:08:04 +00004066 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004067 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004068 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Francois Romieu85bffe62011-04-27 08:22:39 +02004072 chipset = tp->mac_version;
4073 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Francois Romieu5d06a992006-02-23 00:47:58 +01004075 RTL_W8(Cfg9346, Cfg9346_Unlock);
4076 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4077 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004078 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4079 tp->features |= RTL_FEATURE_WOL;
4080 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4081 tp->features |= RTL_FEATURE_WOL;
françois romieu2ca6cf02011-12-15 08:37:43 +00004082 tp->features |= rtl_try_msi(tp, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004083 RTL_W8(Cfg9346, Cfg9346_Lock);
4084
David S. Miller8decf862011-09-22 03:23:13 -04004085 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 tp->set_speed = rtl8169_set_speed_tbi;
4087 tp->get_settings = rtl8169_gset_tbi;
4088 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4089 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4090 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004091 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 } else {
4093 tp->set_speed = rtl8169_set_speed_xmii;
4094 tp->get_settings = rtl8169_gset_xmii;
4095 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4096 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4097 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004098 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
4100
Francois Romieudf58ef512008-10-09 14:35:58 -07004101 spin_lock_init(&tp->lock);
4102
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004103 /* Get MAC address */
Joe Perches6a3c910c2011-11-16 09:38:02 +00004104 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004106 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4110 dev->irq = pdev->irq;
4111 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004113 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Michał Mirosław350fb322011-04-08 06:35:56 +00004115 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4116 * properly for all devices */
4117 dev->features |= NETIF_F_RXCSUM |
4118 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4119
4120 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4121 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4122 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4123 NETIF_F_HIGHDMA;
4124
4125 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4126 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4127 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01004130 tp->hw_start = cfg->hw_start;
4131 tp->intr_event = cfg->intr_event;
4132 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
David S. Miller8decf862011-09-22 03:23:13 -04004134 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4135 ~(RxBOVF | RxFOVF) : ~0;
4136
Francois Romieu2efa53f2007-03-09 00:00:05 +01004137 init_timer(&tp->timer);
4138 tp->timer.data = (unsigned long) dev;
4139 tp->timer.function = rtl8169_phy_timer;
4140
Francois Romieub6ffd972011-06-17 17:00:05 +02004141 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004144 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004145 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
4147 pci_set_drvdata(pdev, dev);
4148
Joe Perchesbf82c182010-02-09 11:49:50 +00004149 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004150 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004151 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004152 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4153 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4154 "tx checksumming: %s]\n",
4155 rtl_chip_infos[chipset].jumbo_max,
4156 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Francois Romieucecb5fd2011-04-01 10:21:07 +02004159 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4160 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4161 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004162 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004163 }
françois romieub646d902011-01-03 15:08:21 +00004164
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004165 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Alan Sternf3ec4f82010-06-08 15:23:51 -04004167 if (pci_dev_run_wake(pdev))
4168 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004169
Ivan Vecera0d672e92011-02-15 02:08:39 +00004170 netif_carrier_off(dev);
4171
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004172out:
4173 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
françois romieu87aeec72010-04-26 11:42:06 +00004175err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004176 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004177 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004178err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004179 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004180err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004181 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004182 pci_disable_device(pdev);
4183err_out_free_dev_1:
4184 free_netdev(dev);
4185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Francois Romieu07d3f512007-02-21 22:40:46 +01004188static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
4190 struct net_device *dev = pci_get_drvdata(pdev);
4191 struct rtl8169_private *tp = netdev_priv(dev);
4192
Francois Romieucecb5fd2011-04-01 10:21:07 +02004193 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4194 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4195 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004196 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004197 }
françois romieub646d902011-01-03 15:08:21 +00004198
Tejun Heo23f333a2010-12-12 16:45:14 +01004199 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01004200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004202
François Romieu953a12c2011-04-24 17:38:48 +02004203 rtl_release_firmware(tp);
4204
Alan Sternf3ec4f82010-06-08 15:23:51 -04004205 if (pci_dev_run_wake(pdev))
4206 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004207
Ivan Veceracc098dc2009-11-29 23:12:52 -08004208 /* restore original MAC address */
4209 rtl_rar_set(tp, dev->perm_addr);
4210
Francois Romieufbac58f2007-10-04 22:51:38 +02004211 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4213 pci_set_drvdata(pdev, NULL);
4214}
4215
Francois Romieub6ffd972011-06-17 17:00:05 +02004216static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4217{
4218 struct rtl_fw *rtl_fw;
4219 const char *name;
4220 int rc = -ENOMEM;
4221
4222 name = rtl_lookup_firmware_name(tp);
4223 if (!name)
4224 goto out_no_firmware;
4225
4226 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4227 if (!rtl_fw)
4228 goto err_warn;
4229
4230 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4231 if (rc < 0)
4232 goto err_free;
4233
Francois Romieufd112f22011-06-18 00:10:29 +02004234 rc = rtl_check_firmware(tp, rtl_fw);
4235 if (rc < 0)
4236 goto err_release_firmware;
4237
Francois Romieub6ffd972011-06-17 17:00:05 +02004238 tp->rtl_fw = rtl_fw;
4239out:
4240 return;
4241
Francois Romieufd112f22011-06-18 00:10:29 +02004242err_release_firmware:
4243 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004244err_free:
4245 kfree(rtl_fw);
4246err_warn:
4247 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4248 name, rc);
4249out_no_firmware:
4250 tp->rtl_fw = NULL;
4251 goto out;
4252}
4253
François Romieu953a12c2011-04-24 17:38:48 +02004254static void rtl_request_firmware(struct rtl8169_private *tp)
4255{
Francois Romieub6ffd972011-06-17 17:00:05 +02004256 if (IS_ERR(tp->rtl_fw))
4257 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004258}
4259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260static int rtl8169_open(struct net_device *dev)
4261{
4262 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004263 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004265 int retval = -ENOMEM;
4266
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004267 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Neil Hormanc0cd8842010-03-29 13:16:02 -07004269 /*
Justin P. Mattocka9d7e792012-03-24 09:00:04 -07004270 * Rx and Tx descriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004271 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004273 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4274 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004276 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004278 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4279 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004281 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283 retval = rtl8169_init_ring(dev);
4284 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004285 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
David Howellsc4028952006-11-22 14:57:56 +00004287 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Francois Romieu99f252b2007-04-02 22:59:59 +02004289 smp_mb();
4290
François Romieu953a12c2011-04-24 17:38:48 +02004291 rtl_request_firmware(tp);
4292
Francois Romieufbac58f2007-10-04 22:51:38 +02004293 retval = request_irq(dev->irq, rtl8169_interrupt,
4294 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004295 dev->name, dev);
4296 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004297 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004298
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004299 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004300
françois romieueee3a962011-01-08 02:17:26 +00004301 rtl8169_init_phy(dev, tp);
4302
Michał Mirosław350fb322011-04-08 06:35:56 +00004303 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004304
françois romieu065c27c2011-01-03 15:08:12 +00004305 rtl_pll_power_up(tp);
4306
Francois Romieu07ce4062007-02-23 23:36:39 +01004307 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004309 tp->saved_wolopts = 0;
4310 pm_runtime_put_noidle(&pdev->dev);
4311
françois romieueee3a962011-01-08 02:17:26 +00004312 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313out:
4314 return retval;
4315
François Romieu953a12c2011-04-24 17:38:48 +02004316err_release_fw_2:
4317 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004318 rtl8169_rx_clear(tp);
4319err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004320 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4321 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004322 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004323err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004324 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4325 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004326 tp->TxDescArray = NULL;
4327err_pm_runtime_put:
4328 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 goto out;
4330}
4331
Hayes Wang92fc43b2011-07-06 15:58:03 +08004332static void rtl_rx_close(struct rtl8169_private *tp)
4333{
4334 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004335
Francois Romieu1687b562011-07-19 17:21:29 +02004336 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004337}
4338
françois romieue6de30d2011-01-03 15:08:37 +00004339static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
françois romieue6de30d2011-01-03 15:08:37 +00004341 void __iomem *ioaddr = tp->mmio_addr;
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004344 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Hayes Wang92fc43b2011-07-06 15:58:03 +08004346 rtl_rx_close(tp);
4347
Hayes Wang5d2e1952011-02-22 17:26:22 +08004348 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004349 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4350 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004351 while (RTL_R8(TxPoll) & NPQ)
4352 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004353 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4354 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4355 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004356 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004357 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4358 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004359 } else {
4360 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4361 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004362 }
4363
Hayes Wang92fc43b2011-07-06 15:58:03 +08004364 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
Francois Romieu7f796d832007-06-11 23:04:41 +02004367static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004368{
4369 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004370
4371 /* Set DMA burst size and Interframe Gap Time */
4372 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4373 (InterFrameGap << TxInterFrameGapShift));
4374}
4375
Francois Romieu07ce4062007-02-23 23:36:39 +01004376static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
4378 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Francois Romieu07ce4062007-02-23 23:36:39 +01004380 tp->hw_start(dev);
4381
Francois Romieu07ce4062007-02-23 23:36:39 +01004382 netif_start_queue(dev);
4383}
4384
Francois Romieu7f796d832007-06-11 23:04:41 +02004385static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4386 void __iomem *ioaddr)
4387{
4388 /*
4389 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4390 * register to be written before TxDescAddrLow to work.
4391 * Switching from MMIO to I/O access fixes the issue as well.
4392 */
4393 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004394 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004395 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004396 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004397}
4398
4399static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4400{
4401 u16 cmd;
4402
4403 cmd = RTL_R16(CPlusCmd);
4404 RTL_W16(CPlusCmd, cmd);
4405 return cmd;
4406}
4407
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004408static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004409{
4410 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004411 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004412}
4413
Francois Romieu6dccd162007-02-13 23:38:05 +01004414static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4415{
Francois Romieu37441002011-06-17 22:58:54 +02004416 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004417 u32 mac_version;
4418 u32 clk;
4419 u32 val;
4420 } cfg2_info [] = {
4421 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4422 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4423 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4424 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004425 };
4426 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004427 unsigned int i;
4428 u32 clk;
4429
4430 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004431 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004432 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4433 RTL_W32(0x7c, p->val);
4434 break;
4435 }
4436 }
4437}
4438
Francois Romieu07ce4062007-02-23 23:36:39 +01004439static void rtl_hw_start_8169(struct net_device *dev)
4440{
4441 struct rtl8169_private *tp = netdev_priv(dev);
4442 void __iomem *ioaddr = tp->mmio_addr;
4443 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004444
Francois Romieu9cb427b2006-11-02 00:10:16 +01004445 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4446 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4447 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4448 }
4449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004451 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4452 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4453 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4454 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004455 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4456
Hayes Wange542a222011-07-06 15:58:04 +08004457 rtl_init_rxcfg(tp);
4458
françois romieuf0298f82011-01-03 15:07:42 +00004459 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004461 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Francois Romieucecb5fd2011-04-01 10:21:07 +02004463 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4464 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4465 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4466 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004467 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Francois Romieu7f796d832007-06-11 23:04:41 +02004469 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004470
Francois Romieucecb5fd2011-04-01 10:21:07 +02004471 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4472 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004473 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004475 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 }
4477
Francois Romieubcf0bf92006-07-26 23:14:13 +02004478 RTL_W16(CPlusCmd, tp->cp_cmd);
4479
Francois Romieu6dccd162007-02-13 23:38:05 +01004480 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 /*
4483 * Undocumented corner. Supposedly:
4484 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4485 */
4486 RTL_W16(IntrMitigate, 0x0000);
4487
Francois Romieu7f796d832007-06-11 23:04:41 +02004488 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004489
Francois Romieucecb5fd2011-04-01 10:21:07 +02004490 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4491 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4492 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4493 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004494 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4495 rtl_set_rx_tx_config_registers(tp);
4496 }
4497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004499
4500 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4501 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 RTL_W32(RxMissed, 0);
4504
Francois Romieu07ce4062007-02-23 23:36:39 +01004505 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507 /* no early-rx interrupts */
4508 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004509
4510 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004511 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
françois romieu650e8d52011-01-03 15:08:29 +00004514static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004515{
4516 u32 csi;
4517
4518 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004519 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4520}
4521
françois romieue6de30d2011-01-03 15:08:37 +00004522static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4523{
4524 rtl_csi_access_enable(ioaddr, 0x17000000);
4525}
4526
françois romieu650e8d52011-01-03 15:08:29 +00004527static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4528{
4529 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004530}
4531
4532struct ephy_info {
4533 unsigned int offset;
4534 u16 mask;
4535 u16 bits;
4536};
4537
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004538static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004539{
4540 u16 w;
4541
4542 while (len-- > 0) {
4543 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4544 rtl_ephy_write(ioaddr, e->offset, w);
4545 e++;
4546 }
4547}
4548
Francois Romieub726e492008-06-28 12:22:59 +02004549static void rtl_disable_clock_request(struct pci_dev *pdev)
4550{
Jon Masone44daad2011-06-27 07:46:31 +00004551 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004552
4553 if (cap) {
4554 u16 ctl;
4555
4556 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4557 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4558 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4559 }
4560}
4561
françois romieue6de30d2011-01-03 15:08:37 +00004562static void rtl_enable_clock_request(struct pci_dev *pdev)
4563{
Jon Masone44daad2011-06-27 07:46:31 +00004564 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004565
4566 if (cap) {
4567 u16 ctl;
4568
4569 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4570 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4571 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4572 }
4573}
4574
Francois Romieub726e492008-06-28 12:22:59 +02004575#define R8168_CPCMD_QUIRK_MASK (\
4576 EnableBist | \
4577 Mac_dbgo_oe | \
4578 Force_half_dup | \
4579 Force_rxflow_en | \
4580 Force_txflow_en | \
4581 Cxpl_dbg_sel | \
4582 ASF | \
4583 PktCntrDisable | \
4584 Mac_dbgo_sel)
4585
Francois Romieu219a1e92008-06-28 11:58:39 +02004586static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4587{
Francois Romieub726e492008-06-28 12:22:59 +02004588 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4589
4590 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4591
Francois Romieu2e68ae42008-06-28 12:00:55 +02004592 rtl_tx_performance_tweak(pdev,
4593 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004594}
4595
4596static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4597{
4598 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004599
françois romieuf0298f82011-01-03 15:07:42 +00004600 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004601
4602 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004603}
4604
4605static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4606{
Francois Romieub726e492008-06-28 12:22:59 +02004607 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4608
4609 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4610
Francois Romieu219a1e92008-06-28 11:58:39 +02004611 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004612
4613 rtl_disable_clock_request(pdev);
4614
4615 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004616}
4617
Francois Romieuef3386f2008-06-29 12:24:30 +02004618static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004619{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004620 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004621 { 0x01, 0, 0x0001 },
4622 { 0x02, 0x0800, 0x1000 },
4623 { 0x03, 0, 0x0042 },
4624 { 0x06, 0x0080, 0x0000 },
4625 { 0x07, 0, 0x2000 }
4626 };
4627
françois romieu650e8d52011-01-03 15:08:29 +00004628 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004629
4630 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4631
Francois Romieu219a1e92008-06-28 11:58:39 +02004632 __rtl_hw_start_8168cp(ioaddr, pdev);
4633}
4634
Francois Romieuef3386f2008-06-29 12:24:30 +02004635static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4636{
françois romieu650e8d52011-01-03 15:08:29 +00004637 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004638
4639 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4640
4641 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4642
4643 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4644}
4645
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004646static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4647{
françois romieu650e8d52011-01-03 15:08:29 +00004648 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004649
4650 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4651
4652 /* Magic. */
4653 RTL_W8(DBG_REG, 0x20);
4654
françois romieuf0298f82011-01-03 15:07:42 +00004655 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004656
4657 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4658
4659 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4660}
4661
Francois Romieu219a1e92008-06-28 11:58:39 +02004662static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4663{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004664 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004665 { 0x02, 0x0800, 0x1000 },
4666 { 0x03, 0, 0x0002 },
4667 { 0x06, 0x0080, 0x0000 }
4668 };
4669
françois romieu650e8d52011-01-03 15:08:29 +00004670 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004671
4672 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4673
4674 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4675
Francois Romieu219a1e92008-06-28 11:58:39 +02004676 __rtl_hw_start_8168cp(ioaddr, pdev);
4677}
4678
4679static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4680{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004681 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004682 { 0x01, 0, 0x0001 },
4683 { 0x03, 0x0400, 0x0220 }
4684 };
4685
françois romieu650e8d52011-01-03 15:08:29 +00004686 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004687
4688 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4689
Francois Romieu219a1e92008-06-28 11:58:39 +02004690 __rtl_hw_start_8168cp(ioaddr, pdev);
4691}
4692
Francois Romieu197ff762008-06-28 13:16:02 +02004693static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4694{
4695 rtl_hw_start_8168c_2(ioaddr, pdev);
4696}
4697
Francois Romieu6fb07052008-06-29 11:54:28 +02004698static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4699{
françois romieu650e8d52011-01-03 15:08:29 +00004700 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004701
4702 __rtl_hw_start_8168cp(ioaddr, pdev);
4703}
4704
Francois Romieu5b538df2008-07-20 16:22:45 +02004705static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4706{
françois romieu650e8d52011-01-03 15:08:29 +00004707 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004708
4709 rtl_disable_clock_request(pdev);
4710
françois romieuf0298f82011-01-03 15:07:42 +00004711 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004712
4713 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4714
4715 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4716}
4717
hayeswang4804b3b2011-03-21 01:50:29 +00004718static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4719{
4720 rtl_csi_access_enable_1(ioaddr);
4721
4722 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4723
4724 RTL_W8(MaxTxPacketSize, TxPacketMax);
4725
4726 rtl_disable_clock_request(pdev);
4727}
4728
françois romieue6de30d2011-01-03 15:08:37 +00004729static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4730{
4731 static const struct ephy_info e_info_8168d_4[] = {
4732 { 0x0b, ~0, 0x48 },
4733 { 0x19, 0x20, 0x50 },
4734 { 0x0c, ~0, 0x20 }
4735 };
4736 int i;
4737
4738 rtl_csi_access_enable_1(ioaddr);
4739
4740 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4741
4742 RTL_W8(MaxTxPacketSize, TxPacketMax);
4743
4744 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4745 const struct ephy_info *e = e_info_8168d_4 + i;
4746 u16 w;
4747
4748 w = rtl_ephy_read(ioaddr, e->offset);
4749 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4750 }
4751
4752 rtl_enable_clock_request(pdev);
4753}
4754
Hayes Wang70090422011-07-06 15:58:06 +08004755static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004756{
Hayes Wang70090422011-07-06 15:58:06 +08004757 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004758 { 0x00, 0x0200, 0x0100 },
4759 { 0x00, 0x0000, 0x0004 },
4760 { 0x06, 0x0002, 0x0001 },
4761 { 0x06, 0x0000, 0x0030 },
4762 { 0x07, 0x0000, 0x2000 },
4763 { 0x00, 0x0000, 0x0020 },
4764 { 0x03, 0x5800, 0x2000 },
4765 { 0x03, 0x0000, 0x0001 },
4766 { 0x01, 0x0800, 0x1000 },
4767 { 0x07, 0x0000, 0x4000 },
4768 { 0x1e, 0x0000, 0x2000 },
4769 { 0x19, 0xffff, 0xfe6c },
4770 { 0x0a, 0x0000, 0x0040 }
4771 };
4772
4773 rtl_csi_access_enable_2(ioaddr);
4774
Hayes Wang70090422011-07-06 15:58:06 +08004775 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004776
4777 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4778
4779 RTL_W8(MaxTxPacketSize, TxPacketMax);
4780
4781 rtl_disable_clock_request(pdev);
4782
4783 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004784 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4785 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004786
Francois Romieucecb5fd2011-04-01 10:21:07 +02004787 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004788}
4789
Hayes Wang70090422011-07-06 15:58:06 +08004790static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4791{
4792 static const struct ephy_info e_info_8168e_2[] = {
4793 { 0x09, 0x0000, 0x0080 },
4794 { 0x19, 0x0000, 0x0224 }
4795 };
4796
4797 rtl_csi_access_enable_1(ioaddr);
4798
4799 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4800
4801 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4802
4803 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4804 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4805 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4806 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4807 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4808 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4809 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4810 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4811 ERIAR_EXGMAC);
4812
Hayes Wang3090bd92011-09-06 16:55:15 +08004813 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004814
4815 rtl_disable_clock_request(pdev);
4816
4817 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4818 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4819
4820 /* Adjust EEE LED frequency */
4821 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4822
4823 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4824 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4825 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4826}
4827
Hayes Wangc2218922011-09-06 16:55:18 +08004828static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4829{
4830 static const struct ephy_info e_info_8168f_1[] = {
4831 { 0x06, 0x00c0, 0x0020 },
4832 { 0x08, 0x0001, 0x0002 },
4833 { 0x09, 0x0000, 0x0080 },
4834 { 0x19, 0x0000, 0x0224 }
4835 };
4836
4837 rtl_csi_access_enable_1(ioaddr);
4838
4839 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4840
4841 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4842
4843 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4844 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4845 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4846 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4847 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4848 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4849 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4850 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4851 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4852 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4853 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4854 ERIAR_EXGMAC);
4855
4856 RTL_W8(MaxTxPacketSize, EarlySize);
4857
4858 rtl_disable_clock_request(pdev);
4859
4860 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4861 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4862
4863 /* Adjust EEE LED frequency */
4864 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4865
4866 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4867 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4868 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4869}
4870
Francois Romieu07ce4062007-02-23 23:36:39 +01004871static void rtl_hw_start_8168(struct net_device *dev)
4872{
Francois Romieu2dd99532007-06-11 23:22:52 +02004873 struct rtl8169_private *tp = netdev_priv(dev);
4874 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004875 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004876
4877 RTL_W8(Cfg9346, Cfg9346_Unlock);
4878
françois romieuf0298f82011-01-03 15:07:42 +00004879 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004880
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004881 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004882
Francois Romieu0e485152007-02-20 00:00:26 +01004883 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004884
4885 RTL_W16(CPlusCmd, tp->cp_cmd);
4886
Francois Romieu0e485152007-02-20 00:00:26 +01004887 RTL_W16(IntrMitigate, 0x5151);
4888
4889 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004890 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieu0e485152007-02-20 00:00:26 +01004891 tp->intr_event |= RxFIFOOver | PCSTimeout;
4892 tp->intr_event &= ~RxOverflow;
4893 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004894
4895 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4896
Francois Romieub8363902008-06-01 12:31:57 +02004897 rtl_set_rx_mode(dev);
4898
4899 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4900 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004901
4902 RTL_R8(IntrMask);
4903
Francois Romieu219a1e92008-06-28 11:58:39 +02004904 switch (tp->mac_version) {
4905 case RTL_GIGA_MAC_VER_11:
4906 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004907 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004908
4909 case RTL_GIGA_MAC_VER_12:
4910 case RTL_GIGA_MAC_VER_17:
4911 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004912 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004913
4914 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004915 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004916 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004917
4918 case RTL_GIGA_MAC_VER_19:
4919 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004920 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004921
4922 case RTL_GIGA_MAC_VER_20:
4923 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004924 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004925
Francois Romieu197ff762008-06-28 13:16:02 +02004926 case RTL_GIGA_MAC_VER_21:
4927 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004928 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004929
Francois Romieu6fb07052008-06-29 11:54:28 +02004930 case RTL_GIGA_MAC_VER_22:
4931 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004932 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004933
Francois Romieuef3386f2008-06-29 12:24:30 +02004934 case RTL_GIGA_MAC_VER_23:
4935 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004936 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004937
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004938 case RTL_GIGA_MAC_VER_24:
4939 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004940 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004941
Francois Romieu5b538df2008-07-20 16:22:45 +02004942 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004943 case RTL_GIGA_MAC_VER_26:
4944 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004945 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004946 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004947
françois romieue6de30d2011-01-03 15:08:37 +00004948 case RTL_GIGA_MAC_VER_28:
4949 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004950 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004951
hayeswang4804b3b2011-03-21 01:50:29 +00004952 case RTL_GIGA_MAC_VER_31:
4953 rtl_hw_start_8168dp(ioaddr, pdev);
4954 break;
4955
hayeswang01dc7fe2011-03-21 01:50:28 +00004956 case RTL_GIGA_MAC_VER_32:
4957 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004958 rtl_hw_start_8168e_1(ioaddr, pdev);
4959 break;
4960 case RTL_GIGA_MAC_VER_34:
4961 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004962 break;
françois romieue6de30d2011-01-03 15:08:37 +00004963
Hayes Wangc2218922011-09-06 16:55:18 +08004964 case RTL_GIGA_MAC_VER_35:
4965 case RTL_GIGA_MAC_VER_36:
4966 rtl_hw_start_8168f_1(ioaddr, pdev);
4967 break;
4968
Francois Romieu219a1e92008-06-28 11:58:39 +02004969 default:
4970 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4971 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004972 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004973 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004974
Francois Romieu0e485152007-02-20 00:00:26 +01004975 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4976
Francois Romieub8363902008-06-01 12:31:57 +02004977 RTL_W8(Cfg9346, Cfg9346_Lock);
4978
Francois Romieu2dd99532007-06-11 23:22:52 +02004979 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004980
Francois Romieu0e485152007-02-20 00:00:26 +01004981 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004982}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Francois Romieu2857ffb2008-08-02 21:08:49 +02004984#define R810X_CPCMD_QUIRK_MASK (\
4985 EnableBist | \
4986 Mac_dbgo_oe | \
4987 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004988 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004989 Force_txflow_en | \
4990 Cxpl_dbg_sel | \
4991 ASF | \
4992 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004993 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004994
4995static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4996{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004997 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004998 { 0x01, 0, 0x6e65 },
4999 { 0x02, 0, 0x091f },
5000 { 0x03, 0, 0xc2f9 },
5001 { 0x06, 0, 0xafb5 },
5002 { 0x07, 0, 0x0e00 },
5003 { 0x19, 0, 0xec80 },
5004 { 0x01, 0, 0x2e65 },
5005 { 0x01, 0, 0x6e65 }
5006 };
5007 u8 cfg1;
5008
françois romieu650e8d52011-01-03 15:08:29 +00005009 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005010
5011 RTL_W8(DBG_REG, FIX_NAK_1);
5012
5013 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5014
5015 RTL_W8(Config1,
5016 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5017 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5018
5019 cfg1 = RTL_R8(Config1);
5020 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5021 RTL_W8(Config1, cfg1 & ~LEDS0);
5022
Francois Romieu2857ffb2008-08-02 21:08:49 +02005023 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5024}
5025
5026static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5027{
françois romieu650e8d52011-01-03 15:08:29 +00005028 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005029
5030 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5031
5032 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5033 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005034}
5035
5036static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5037{
5038 rtl_hw_start_8102e_2(ioaddr, pdev);
5039
5040 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5041}
5042
Hayes Wang5a5e4442011-02-22 17:26:21 +08005043static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5044{
5045 static const struct ephy_info e_info_8105e_1[] = {
5046 { 0x07, 0, 0x4000 },
5047 { 0x19, 0, 0x0200 },
5048 { 0x19, 0, 0x0020 },
5049 { 0x1e, 0, 0x2000 },
5050 { 0x03, 0, 0x0001 },
5051 { 0x19, 0, 0x0100 },
5052 { 0x19, 0, 0x0004 },
5053 { 0x0a, 0, 0x0020 }
5054 };
5055
Francois Romieucecb5fd2011-04-01 10:21:07 +02005056 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005057 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5058
Francois Romieucecb5fd2011-04-01 10:21:07 +02005059 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005060 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5061
5062 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005063 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005064
5065 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5066}
5067
5068static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5069{
5070 rtl_hw_start_8105e_1(ioaddr, pdev);
5071 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5072}
5073
Francois Romieu07ce4062007-02-23 23:36:39 +01005074static void rtl_hw_start_8101(struct net_device *dev)
5075{
Francois Romieucdf1a602007-06-11 23:29:50 +02005076 struct rtl8169_private *tp = netdev_priv(dev);
5077 void __iomem *ioaddr = tp->mmio_addr;
5078 struct pci_dev *pdev = tp->pci_dev;
5079
françois romieu811fd302011-12-04 20:30:45 +00005080 if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
5081 tp->intr_event &= ~RxFIFOOver;
5082 tp->napi_event &= ~RxFIFOOver;
5083 }
5084
Francois Romieucecb5fd2011-04-01 10:21:07 +02005085 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5086 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005087 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005088
5089 if (cap) {
5090 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5091 PCI_EXP_DEVCTL_NOSNOOP_EN);
5092 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005093 }
5094
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005095 RTL_W8(Cfg9346, Cfg9346_Unlock);
5096
Francois Romieu2857ffb2008-08-02 21:08:49 +02005097 switch (tp->mac_version) {
5098 case RTL_GIGA_MAC_VER_07:
5099 rtl_hw_start_8102e_1(ioaddr, pdev);
5100 break;
5101
5102 case RTL_GIGA_MAC_VER_08:
5103 rtl_hw_start_8102e_3(ioaddr, pdev);
5104 break;
5105
5106 case RTL_GIGA_MAC_VER_09:
5107 rtl_hw_start_8102e_2(ioaddr, pdev);
5108 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005109
5110 case RTL_GIGA_MAC_VER_29:
5111 rtl_hw_start_8105e_1(ioaddr, pdev);
5112 break;
5113 case RTL_GIGA_MAC_VER_30:
5114 rtl_hw_start_8105e_2(ioaddr, pdev);
5115 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005116 }
5117
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005118 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005119
françois romieuf0298f82011-01-03 15:07:42 +00005120 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005121
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005122 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005123
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005124 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005125 RTL_W16(CPlusCmd, tp->cp_cmd);
5126
5127 RTL_W16(IntrMitigate, 0x0000);
5128
5129 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5130
5131 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5132 rtl_set_rx_tx_config_registers(tp);
5133
Francois Romieucdf1a602007-06-11 23:29:50 +02005134 RTL_R8(IntrMask);
5135
Francois Romieucdf1a602007-06-11 23:29:50 +02005136 rtl_set_rx_mode(dev);
5137
5138 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01005139
Francois Romieu0e485152007-02-20 00:00:26 +01005140 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141}
5142
5143static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5144{
Francois Romieud58d46b2011-05-03 16:38:29 +02005145 struct rtl8169_private *tp = netdev_priv(dev);
5146
5147 if (new_mtu < ETH_ZLEN ||
5148 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 return -EINVAL;
5150
Francois Romieud58d46b2011-05-03 16:38:29 +02005151 if (new_mtu > ETH_DATA_LEN)
5152 rtl_hw_jumbo_enable(tp);
5153 else
5154 rtl_hw_jumbo_disable(tp);
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005157 netdev_update_features(dev);
5158
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160}
5161
5162static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5163{
Al Viro95e09182007-12-22 18:55:39 +00005164 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5166}
5167
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005168static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5169 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005171 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005172 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005173
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005174 kfree(*data_buff);
5175 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 rtl8169_make_unusable_by_asic(desc);
5177}
5178
5179static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5180{
5181 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5182
5183 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5184}
5185
5186static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5187 u32 rx_buf_sz)
5188{
5189 desc->addr = cpu_to_le64(mapping);
5190 wmb();
5191 rtl8169_mark_to_asic(desc, rx_buf_sz);
5192}
5193
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005194static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005196 return (void *)ALIGN((long)data, 16);
5197}
5198
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005199static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5200 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005201{
5202 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005204 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005205 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005206 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005208 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5209 if (!data)
5210 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005211
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005212 if (rtl8169_align(data) != data) {
5213 kfree(data);
5214 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5215 if (!data)
5216 return NULL;
5217 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005218
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005219 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005220 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005221 if (unlikely(dma_mapping_error(d, mapping))) {
5222 if (net_ratelimit())
5223 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005224 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
5227 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005228 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005229
5230err_out:
5231 kfree(data);
5232 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233}
5234
5235static void rtl8169_rx_clear(struct rtl8169_private *tp)
5236{
Francois Romieu07d3f512007-02-21 22:40:46 +01005237 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
5239 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005240 if (tp->Rx_databuff[i]) {
5241 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 tp->RxDescArray + i);
5243 }
5244 }
5245}
5246
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005247static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005249 desc->opts1 |= cpu_to_le32(RingEnd);
5250}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005251
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005252static int rtl8169_rx_fill(struct rtl8169_private *tp)
5253{
5254 unsigned int i;
5255
5256 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005257 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005258
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005259 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005261
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005262 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005263 if (!data) {
5264 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005265 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005266 }
5267 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005270 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5271 return 0;
5272
5273err_out:
5274 rtl8169_rx_clear(tp);
5275 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276}
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278static int rtl8169_init_ring(struct net_device *dev)
5279{
5280 struct rtl8169_private *tp = netdev_priv(dev);
5281
5282 rtl8169_init_ring_indexes(tp);
5283
5284 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005285 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005287 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288}
5289
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005290static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 struct TxDesc *desc)
5292{
5293 unsigned int len = tx_skb->len;
5294
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005295 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 desc->opts1 = 0x00;
5298 desc->opts2 = 0x00;
5299 desc->addr = 0x00;
5300 tx_skb->len = 0;
5301}
5302
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005303static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5304 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305{
5306 unsigned int i;
5307
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005308 for (i = 0; i < n; i++) {
5309 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 struct ring_info *tx_skb = tp->tx_skb + entry;
5311 unsigned int len = tx_skb->len;
5312
5313 if (len) {
5314 struct sk_buff *skb = tx_skb->skb;
5315
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005316 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 tp->TxDescArray + entry);
5318 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005319 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 dev_kfree_skb(skb);
5321 tx_skb->skb = NULL;
5322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 }
5324 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005325}
5326
5327static void rtl8169_tx_clear(struct rtl8169_private *tp)
5328{
5329 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 tp->cur_tx = tp->dirty_tx = 0;
5331}
5332
David Howellsc4028952006-11-22 14:57:56 +00005333static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
5335 struct rtl8169_private *tp = netdev_priv(dev);
5336
David Howellsc4028952006-11-22 14:57:56 +00005337 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 schedule_delayed_work(&tp->task, 4);
5339}
5340
5341static void rtl8169_wait_for_quiescence(struct net_device *dev)
5342{
5343 struct rtl8169_private *tp = netdev_priv(dev);
5344 void __iomem *ioaddr = tp->mmio_addr;
5345
5346 synchronize_irq(dev->irq);
5347
5348 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005349 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
françois romieu811fd302011-12-04 20:30:45 +00005351 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
David S. Millerd1d08d12008-01-07 20:53:33 -08005353 tp->intr_mask = 0xffff;
5354 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005355 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356}
5357
David Howellsc4028952006-11-22 14:57:56 +00005358static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
David Howellsc4028952006-11-22 14:57:56 +00005360 struct rtl8169_private *tp =
5361 container_of(work, struct rtl8169_private, task.work);
5362 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 int ret;
5364
Francois Romieueb2a0212007-02-15 23:37:21 +01005365 rtnl_lock();
5366
5367 if (!netif_running(dev))
5368 goto out_unlock;
5369
5370 rtl8169_wait_for_quiescence(dev);
5371 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
5373 ret = rtl8169_open(dev);
5374 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005375 if (net_ratelimit())
5376 netif_err(tp, drv, dev,
5377 "reinit failure (status = %d). Rescheduling\n",
5378 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 rtl8169_schedule_work(dev, rtl8169_reinit_task);
5380 }
Francois Romieueb2a0212007-02-15 23:37:21 +01005381
5382out_unlock:
5383 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
David Howellsc4028952006-11-22 14:57:56 +00005386static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387{
David Howellsc4028952006-11-22 14:57:56 +00005388 struct rtl8169_private *tp =
5389 container_of(work, struct rtl8169_private, task.work);
5390 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005391 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Francois Romieueb2a0212007-02-15 23:37:21 +01005393 rtnl_lock();
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005396 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
françois romieuc7c2c392011-12-04 20:30:52 +00005398 rtl8169_hw_reset(tp);
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 rtl8169_wait_for_quiescence(dev);
5401
Francois Romieu56de4142011-03-15 17:29:31 +01005402 for (i = 0; i < NUM_RX_DESC; i++)
5403 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005406 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
Francois Romieu56de4142011-03-15 17:29:31 +01005408 rtl_hw_start(dev);
5409 netif_wake_queue(dev);
5410 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005411
5412out_unlock:
5413 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414}
5415
5416static void rtl8169_tx_timeout(struct net_device *dev)
5417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 rtl8169_schedule_work(dev, rtl8169_reset_task);
5419}
5420
5421static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005422 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
5424 struct skb_shared_info *info = skb_shinfo(skb);
5425 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005426 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005427 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
5429 entry = tp->cur_tx;
5430 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005431 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 dma_addr_t mapping;
5433 u32 status, len;
5434 void *addr;
5435
5436 entry = (entry + 1) % NUM_TX_DESC;
5437
5438 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005439 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005440 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005441 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005442 if (unlikely(dma_mapping_error(d, mapping))) {
5443 if (net_ratelimit())
5444 netif_err(tp, drv, tp->dev,
5445 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005446 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Francois Romieucecb5fd2011-04-01 10:21:07 +02005449 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005450 status = opts[0] | len |
5451 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
5453 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005454 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 txd->addr = cpu_to_le64(mapping);
5456
5457 tp->tx_skb[entry].len = len;
5458 }
5459
5460 if (cur_frag) {
5461 tp->tx_skb[entry].skb = skb;
5462 txd->opts1 |= cpu_to_le32(LastFrag);
5463 }
5464
5465 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005466
5467err_out:
5468 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5469 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
Francois Romieu2b7b4312011-04-18 22:53:24 -07005472static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5473 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005475 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005476 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005477 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Francois Romieu2b7b4312011-04-18 22:53:24 -07005479 if (mss) {
5480 opts[0] |= TD_LSO;
5481 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5482 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005483 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
5485 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005486 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005488 opts[offset] |= info->checksum.udp;
5489 else
5490 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Stephen Hemminger613573252009-08-31 19:50:58 +00005494static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5495 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
5497 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005498 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 struct TxDesc *txd = tp->TxDescArray + entry;
5500 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005501 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 dma_addr_t mapping;
5503 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005504 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005505 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005508 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005509 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 }
5511
5512 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005513 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005515 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005516 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005517 if (unlikely(dma_mapping_error(d, mapping))) {
5518 if (net_ratelimit())
5519 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005520 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
5523 tp->tx_skb[entry].len = len;
5524 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Francois Romieu2b7b4312011-04-18 22:53:24 -07005526 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5527 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005528
Francois Romieu2b7b4312011-04-18 22:53:24 -07005529 rtl8169_tso_csum(tp, skb, opts);
5530
5531 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005532 if (frags < 0)
5533 goto err_dma_1;
5534 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005535 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005536 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005537 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005538 tp->tx_skb[entry].skb = skb;
5539 }
5540
Francois Romieu2b7b4312011-04-18 22:53:24 -07005541 txd->opts2 = cpu_to_le32(opts[1]);
5542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 wmb();
5544
Francois Romieucecb5fd2011-04-01 10:21:07 +02005545 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005546 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 txd->opts1 = cpu_to_le32(status);
5548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 tp->cur_tx += frags + 1;
5550
David Dillow4c020a92010-03-03 16:33:10 +00005551 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Francois Romieucecb5fd2011-04-01 10:21:07 +02005553 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5556 netif_stop_queue(dev);
5557 smp_rmb();
5558 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5559 netif_wake_queue(dev);
5560 }
5561
Stephen Hemminger613573252009-08-31 19:50:58 +00005562 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005564err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005565 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005566err_dma_0:
5567 dev_kfree_skb(skb);
5568 dev->stats.tx_dropped++;
5569 return NETDEV_TX_OK;
5570
5571err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005573 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005574 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575}
5576
5577static void rtl8169_pcierr_interrupt(struct net_device *dev)
5578{
5579 struct rtl8169_private *tp = netdev_priv(dev);
5580 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 u16 pci_status, pci_cmd;
5582
5583 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5584 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5585
Joe Perchesbf82c182010-02-09 11:49:50 +00005586 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5587 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
5589 /*
5590 * The recovery sequence below admits a very elaborated explanation:
5591 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005592 * - I did not see what else could be done;
5593 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 *
5595 * Feel free to adjust to your needs.
5596 */
Francois Romieua27993f2006-12-18 00:04:19 +01005597 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005598 pci_cmd &= ~PCI_COMMAND_PARITY;
5599 else
5600 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5601
5602 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604 pci_write_config_word(pdev, PCI_STATUS,
5605 pci_status & (PCI_STATUS_DETECTED_PARITY |
5606 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5607 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5608
5609 /* The infamous DAC f*ckup only happens at boot time */
5610 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005611 void __iomem *ioaddr = tp->mmio_addr;
5612
Joe Perchesbf82c182010-02-09 11:49:50 +00005613 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 tp->cp_cmd &= ~PCIDAC;
5615 RTL_W16(CPlusCmd, tp->cp_cmd);
5616 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
5618
françois romieue6de30d2011-01-03 15:08:37 +00005619 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005620
5621 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
5623
Francois Romieu07d3f512007-02-21 22:40:46 +01005624static void rtl8169_tx_interrupt(struct net_device *dev,
5625 struct rtl8169_private *tp,
5626 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627{
5628 unsigned int dirty_tx, tx_left;
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 dirty_tx = tp->dirty_tx;
5631 smp_rmb();
5632 tx_left = tp->cur_tx - dirty_tx;
5633
5634 while (tx_left > 0) {
5635 unsigned int entry = dirty_tx % NUM_TX_DESC;
5636 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 u32 status;
5638
5639 rmb();
5640 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5641 if (status & DescOwn)
5642 break;
5643
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005644 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5645 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005647 dev->stats.tx_packets++;
5648 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005649 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 tx_skb->skb = NULL;
5651 }
5652 dirty_tx++;
5653 tx_left--;
5654 }
5655
5656 if (tp->dirty_tx != dirty_tx) {
5657 tp->dirty_tx = dirty_tx;
5658 smp_wmb();
5659 if (netif_queue_stopped(dev) &&
5660 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5661 netif_wake_queue(dev);
5662 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005663 /*
5664 * 8168 hack: TxPoll requests are lost when the Tx packets are
5665 * too close. Let's kick an extra TxPoll request when a burst
5666 * of start_xmit activity is detected (if it is not detected,
5667 * it is slow enough). -- FR
5668 */
5669 smp_rmb();
5670 if (tp->cur_tx != dirty_tx)
5671 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 }
5673}
5674
Francois Romieu126fa4b2005-05-12 20:09:17 -04005675static inline int rtl8169_fragmented_frame(u32 status)
5676{
5677 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5678}
5679
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005680static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 u32 status = opts1 & RxProtoMask;
5683
5684 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005685 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 skb->ip_summed = CHECKSUM_UNNECESSARY;
5687 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005688 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
5690
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005691static struct sk_buff *rtl8169_try_rx_copy(void *data,
5692 struct rtl8169_private *tp,
5693 int pkt_size,
5694 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005696 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005697 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005699 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005700 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005701 prefetch(data);
5702 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5703 if (skb)
5704 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005705 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5706
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005707 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
Francois Romieu07d3f512007-02-21 22:40:46 +01005710static int rtl8169_rx_interrupt(struct net_device *dev,
5711 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005712 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713{
5714 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005715 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 cur_rx = tp->cur_rx;
5718 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005719 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005721 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005723 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 u32 status;
5725
5726 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005727 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 if (status & DescOwn)
5730 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005731 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005732 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5733 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005734 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005736 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005738 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005739 if (status & RxFOVF) {
5740 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005741 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005742 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005743 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005745 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005746 dma_addr_t addr = le64_to_cpu(desc->addr);
Francois Romieudeb9d93c2011-07-12 08:24:28 +02005747 int pkt_size = (status & 0x00003fff) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Francois Romieu126fa4b2005-05-12 20:09:17 -04005749 /*
5750 * The driver does not support incoming fragmented
5751 * frames. They are seen as a symptom of over-mtu
5752 * sized frames.
5753 */
5754 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005755 dev->stats.rx_dropped++;
5756 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005757 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005758 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005759 }
5760
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005761 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5762 tp, pkt_size, addr);
5763 rtl8169_mark_to_asic(desc, rx_buf_sz);
5764 if (!skb) {
5765 dev->stats.rx_dropped++;
5766 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 }
5768
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005769 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 skb_put(skb, pkt_size);
5771 skb->protocol = eth_type_trans(skb, dev);
5772
Francois Romieu7a8fc772011-03-01 17:18:33 +01005773 rtl8169_rx_vlan_tag(desc, skb);
5774
Francois Romieu56de4142011-03-15 17:29:31 +01005775 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
Francois Romieucebf8cc2007-10-18 12:06:54 +02005777 dev->stats.rx_bytes += pkt_size;
5778 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005780
5781 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005782 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005783 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5784 desc->opts2 = 0;
5785 cur_rx++;
5786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 }
5788
5789 count = cur_rx - tp->cur_rx;
5790 tp->cur_rx = cur_rx;
5791
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005792 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
5794 return count;
5795}
5796
Francois Romieu07d3f512007-02-21 22:40:46 +01005797static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
Francois Romieu07d3f512007-02-21 22:40:46 +01005799 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005803 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
David Dillowf11a3772009-05-22 15:29:34 +00005805 /* loop handling interrupts until we have no new ones or
5806 * we hit a invalid/hotplug case.
5807 */
Francois Romieu865c6522008-05-11 14:51:00 +02005808 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005809 while (status && status != 0xffff) {
françois romieu811fd302011-12-04 20:30:45 +00005810 status &= tp->intr_event;
5811 if (!status)
5812 break;
5813
David Dillowf11a3772009-05-22 15:29:34 +00005814 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
David Dillowf11a3772009-05-22 15:29:34 +00005816 /* Handle all of the error cases first. These will reset
5817 * the chip, so just exit the loop.
5818 */
5819 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005820 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 }
David Dillowf11a3772009-05-22 15:29:34 +00005823
Francois Romieu1519e572011-02-03 12:02:36 +01005824 if (unlikely(status & RxFIFOOver)) {
5825 switch (tp->mac_version) {
5826 /* Work around for rx fifo overflow */
5827 case RTL_GIGA_MAC_VER_11:
Francois Romieu1519e572011-02-03 12:02:36 +01005828 netif_stop_queue(dev);
5829 rtl8169_tx_timeout(dev);
5830 goto done;
Francois Romieu1519e572011-02-03 12:02:36 +01005831 default:
5832 break;
5833 }
David Dillowf11a3772009-05-22 15:29:34 +00005834 }
5835
5836 if (unlikely(status & SYSErr)) {
5837 rtl8169_pcierr_interrupt(dev);
5838 break;
5839 }
5840
5841 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005842 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005843
5844 /* We need to see the lastest version of tp->intr_mask to
5845 * avoid ignoring an MSI interrupt and having to wait for
5846 * another event which may never come.
5847 */
5848 smp_rmb();
5849 if (status & tp->intr_mask & tp->napi_event) {
5850 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5851 tp->intr_mask = ~tp->napi_event;
5852
5853 if (likely(napi_schedule_prep(&tp->napi)))
5854 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005855 else
5856 netif_info(tp, intr, dev,
5857 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005858 }
5859
5860 /* We only get a new MSI interrupt when all active irq
5861 * sources on the chip have been acknowledged. So, ack
5862 * everything we've seen and check if new sources have become
5863 * active to avoid blocking all interrupts from the chip.
5864 */
5865 RTL_W16(IntrStatus,
5866 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5867 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 }
Francois Romieu1519e572011-02-03 12:02:36 +01005869done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 return IRQ_RETVAL(handled);
5871}
5872
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005873static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005875 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5876 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005878 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005880 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 rtl8169_tx_interrupt(dev, tp, ioaddr);
5882
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005883 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005884 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005885
5886 /* We need for force the visibility of tp->intr_mask
5887 * for other CPUs, as we can loose an MSI interrupt
5888 * and potentially wait for a retransmit timeout if we don't.
5889 * The posted write to IntrMask is safe, as it will
5890 * eventually make it to the chip and we won't loose anything
5891 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 */
David Dillowf11a3772009-05-22 15:29:34 +00005893 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005894 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005895 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 }
5897
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005898 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Francois Romieu523a6092008-09-10 22:28:56 +02005901static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5902{
5903 struct rtl8169_private *tp = netdev_priv(dev);
5904
5905 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5906 return;
5907
5908 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5909 RTL_W32(RxMissed, 0);
5910}
5911
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912static void rtl8169_down(struct net_device *dev)
5913{
5914 struct rtl8169_private *tp = netdev_priv(dev);
5915 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
Francois Romieu4876cc12011-03-11 21:07:11 +01005917 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 netif_stop_queue(dev);
5920
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005921 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 spin_lock_irq(&tp->lock);
5924
Hayes Wang92fc43b2011-07-06 15:58:03 +08005925 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005926 /*
5927 * At this point device interrupts can not be enabled in any function,
5928 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5929 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5930 */
Francois Romieu523a6092008-09-10 22:28:56 +02005931 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
5933 spin_unlock_irq(&tp->lock);
5934
5935 synchronize_irq(dev->irq);
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005938 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 rtl8169_tx_clear(tp);
5941
5942 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005943
5944 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945}
5946
5947static int rtl8169_close(struct net_device *dev)
5948{
5949 struct rtl8169_private *tp = netdev_priv(dev);
5950 struct pci_dev *pdev = tp->pci_dev;
5951
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005952 pm_runtime_get_sync(&pdev->dev);
5953
Francois Romieucecb5fd2011-04-01 10:21:07 +02005954 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005955 rtl8169_update_counters(dev);
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 rtl8169_down(dev);
5958
5959 free_irq(dev->irq, dev);
5960
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005961 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5962 tp->RxPhyAddr);
5963 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5964 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 tp->TxDescArray = NULL;
5966 tp->RxDescArray = NULL;
5967
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005968 pm_runtime_put_sync(&pdev->dev);
5969
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 return 0;
5971}
5972
Francois Romieu07ce4062007-02-23 23:36:39 +01005973static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974{
5975 struct rtl8169_private *tp = netdev_priv(dev);
5976 void __iomem *ioaddr = tp->mmio_addr;
5977 unsigned long flags;
5978 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005979 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 u32 tmp = 0;
5981
5982 if (dev->flags & IFF_PROMISC) {
5983 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005984 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 rx_mode =
5986 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5987 AcceptAllPhys;
5988 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005989 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005990 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 /* Too many to filter perfectly -- accept all multicasts. */
5992 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5993 mc_filter[1] = mc_filter[0] = 0xffffffff;
5994 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005995 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 rx_mode = AcceptBroadcast | AcceptMyPhys;
5998 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005999 netdev_for_each_mc_addr(ha, dev) {
6000 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
6002 rx_mode |= AcceptMulticast;
6003 }
6004 }
6005
6006 spin_lock_irqsave(&tp->lock, flags);
6007
Francois Romieu1687b562011-07-19 17:21:29 +02006008 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Francois Romieuf887cce2008-07-17 22:24:18 +02006010 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01006011 u32 data = mc_filter[0];
6012
6013 mc_filter[0] = swab32(mc_filter[1]);
6014 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02006015 }
6016
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006018 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Francois Romieu57a9f232007-06-04 22:10:15 +02006020 RTL_W32(RxConfig, tmp);
6021
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 spin_unlock_irqrestore(&tp->lock, flags);
6023}
6024
6025/**
6026 * rtl8169_get_stats - Get rtl8169 read/write statistics
6027 * @dev: The Ethernet Device to get statistics for
6028 *
6029 * Get TX/RX statistics for rtl8169
6030 */
6031static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6032{
6033 struct rtl8169_private *tp = netdev_priv(dev);
6034 void __iomem *ioaddr = tp->mmio_addr;
6035 unsigned long flags;
6036
6037 if (netif_running(dev)) {
6038 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02006039 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 spin_unlock_irqrestore(&tp->lock, flags);
6041 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02006042
Francois Romieucebf8cc2007-10-18 12:06:54 +02006043 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044}
6045
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006046static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006047{
françois romieu065c27c2011-01-03 15:08:12 +00006048 struct rtl8169_private *tp = netdev_priv(dev);
6049
Francois Romieu5d06a992006-02-23 00:47:58 +01006050 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006051 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006052
françois romieu065c27c2011-01-03 15:08:12 +00006053 rtl_pll_power_down(tp);
6054
Francois Romieu5d06a992006-02-23 00:47:58 +01006055 netif_device_detach(dev);
6056 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006057}
Francois Romieu5d06a992006-02-23 00:47:58 +01006058
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006059#ifdef CONFIG_PM
6060
6061static int rtl8169_suspend(struct device *device)
6062{
6063 struct pci_dev *pdev = to_pci_dev(device);
6064 struct net_device *dev = pci_get_drvdata(pdev);
6065
6066 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006067
Francois Romieu5d06a992006-02-23 00:47:58 +01006068 return 0;
6069}
6070
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006071static void __rtl8169_resume(struct net_device *dev)
6072{
françois romieu065c27c2011-01-03 15:08:12 +00006073 struct rtl8169_private *tp = netdev_priv(dev);
6074
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006075 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006076
6077 rtl_pll_power_up(tp);
6078
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006079 rtl8169_schedule_work(dev, rtl8169_reset_task);
6080}
6081
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006082static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006083{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006084 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006085 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006086 struct rtl8169_private *tp = netdev_priv(dev);
6087
6088 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006089
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006090 if (netif_running(dev))
6091 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006092
Francois Romieu5d06a992006-02-23 00:47:58 +01006093 return 0;
6094}
6095
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006096static int rtl8169_runtime_suspend(struct device *device)
6097{
6098 struct pci_dev *pdev = to_pci_dev(device);
6099 struct net_device *dev = pci_get_drvdata(pdev);
6100 struct rtl8169_private *tp = netdev_priv(dev);
6101
6102 if (!tp->TxDescArray)
6103 return 0;
6104
6105 spin_lock_irq(&tp->lock);
6106 tp->saved_wolopts = __rtl8169_get_wol(tp);
6107 __rtl8169_set_wol(tp, WAKE_ANY);
6108 spin_unlock_irq(&tp->lock);
6109
6110 rtl8169_net_suspend(dev);
6111
6112 return 0;
6113}
6114
6115static int rtl8169_runtime_resume(struct device *device)
6116{
6117 struct pci_dev *pdev = to_pci_dev(device);
6118 struct net_device *dev = pci_get_drvdata(pdev);
6119 struct rtl8169_private *tp = netdev_priv(dev);
6120
6121 if (!tp->TxDescArray)
6122 return 0;
6123
6124 spin_lock_irq(&tp->lock);
6125 __rtl8169_set_wol(tp, tp->saved_wolopts);
6126 tp->saved_wolopts = 0;
6127 spin_unlock_irq(&tp->lock);
6128
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006129 rtl8169_init_phy(dev, tp);
6130
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006131 __rtl8169_resume(dev);
6132
6133 return 0;
6134}
6135
6136static int rtl8169_runtime_idle(struct device *device)
6137{
6138 struct pci_dev *pdev = to_pci_dev(device);
6139 struct net_device *dev = pci_get_drvdata(pdev);
6140 struct rtl8169_private *tp = netdev_priv(dev);
6141
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006142 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006143}
6144
Alexey Dobriyan47145212009-12-14 18:00:08 -08006145static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006146 .suspend = rtl8169_suspend,
6147 .resume = rtl8169_resume,
6148 .freeze = rtl8169_suspend,
6149 .thaw = rtl8169_resume,
6150 .poweroff = rtl8169_suspend,
6151 .restore = rtl8169_resume,
6152 .runtime_suspend = rtl8169_runtime_suspend,
6153 .runtime_resume = rtl8169_runtime_resume,
6154 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006155};
6156
6157#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6158
6159#else /* !CONFIG_PM */
6160
6161#define RTL8169_PM_OPS NULL
6162
6163#endif /* !CONFIG_PM */
6164
David S. Miller1805b2f2011-10-24 18:18:09 -04006165static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6166{
6167 void __iomem *ioaddr = tp->mmio_addr;
6168
6169 /* WoL fails with 8168b when the receiver is disabled. */
6170 switch (tp->mac_version) {
6171 case RTL_GIGA_MAC_VER_11:
6172 case RTL_GIGA_MAC_VER_12:
6173 case RTL_GIGA_MAC_VER_17:
6174 pci_clear_master(tp->pci_dev);
6175
6176 RTL_W8(ChipCmd, CmdRxEnb);
6177 /* PCI commit */
6178 RTL_R8(ChipCmd);
6179 break;
6180 default:
6181 break;
6182 }
6183}
6184
Francois Romieu1765f952008-09-13 17:21:40 +02006185static void rtl_shutdown(struct pci_dev *pdev)
6186{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006187 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006188 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006189
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006190 rtl8169_net_suspend(dev);
6191
Francois Romieucecb5fd2011-04-01 10:21:07 +02006192 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006193 rtl_rar_set(tp, dev->perm_addr);
6194
françois romieu4bb3f522009-06-17 11:41:45 +00006195 spin_lock_irq(&tp->lock);
6196
Hayes Wang92fc43b2011-07-06 15:58:03 +08006197 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006198
6199 spin_unlock_irq(&tp->lock);
6200
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006201 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006202 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6203 rtl_wol_suspend_quirk(tp);
6204 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006205 }
6206
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006207 pci_wake_from_d3(pdev, true);
6208 pci_set_power_state(pdev, PCI_D3hot);
6209 }
6210}
Francois Romieu5d06a992006-02-23 00:47:58 +01006211
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212static struct pci_driver rtl8169_pci_driver = {
6213 .name = MODULENAME,
6214 .id_table = rtl8169_pci_tbl,
6215 .probe = rtl8169_init_one,
6216 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006217 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006218 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219};
6220
Francois Romieu07d3f512007-02-21 22:40:46 +01006221static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222{
Jeff Garzik29917622006-08-19 17:48:59 -04006223 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224}
6225
Francois Romieu07d3f512007-02-21 22:40:46 +01006226static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227{
6228 pci_unregister_driver(&rtl8169_pci_driver);
6229}
6230
6231module_init(rtl8169_init_module);
6232module_exit(rtl8169_cleanup_module);