blob: 9f9abb5d01a58f41d100eae578286cbe28deb750 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080045#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080047#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef RTL8169_DEBUG
50#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 if (!(expr)) { \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070053 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020054 }
Joe Perches06fa7352007-10-18 21:15:00 +020055#define dprintk(fmt, args...) \
56 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define assert(expr) do {} while (0)
59#define dprintk(fmt, args...) do {} while (0)
60#endif /* RTL8169_DEBUG */
61
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070063 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define TX_BUFFS_AVAIL(tp) \
66 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
69 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050070static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800134 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800135 RTL_GIGA_MAC_VER_35,
136 RTL_GIGA_MAC_VER_36,
Francois Romieu85bffe62011-04-27 08:22:39 +0200137 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Francois Romieu2b7b4312011-04-18 22:53:24 -0700140enum rtl_tx_desc_version {
141 RTL_TD_0 = 0,
142 RTL_TD_1 = 1,
143};
144
Francois Romieud58d46b2011-05-03 16:38:29 +0200145#define JUMBO_1K ETH_DATA_LEN
146#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
150
151#define _R(NAME,TD,FW,SZ,B) { \
152 .name = NAME, \
153 .txd_version = TD, \
154 .fw_name = FW, \
155 .jumbo_max = SZ, \
156 .jumbo_tx_csum = B \
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800159static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700161 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200162 const char *fw_name;
Francois Romieud58d46b2011-05-03 16:38:29 +0200163 u16 jumbo_max;
164 bool jumbo_tx_csum;
Francois Romieu85bffe62011-04-27 08:22:39 +0200165} rtl_chip_infos[] = {
166 /* PCI devices. */
167 [RTL_GIGA_MAC_VER_01] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200168 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200169 [RTL_GIGA_MAC_VER_02] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200170 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200171 [RTL_GIGA_MAC_VER_03] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200172 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200173 [RTL_GIGA_MAC_VER_04] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200174 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200175 [RTL_GIGA_MAC_VER_05] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200176 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200177 [RTL_GIGA_MAC_VER_06] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200178 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200179 /* PCI-E devices. */
180 [RTL_GIGA_MAC_VER_07] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200181 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200182 [RTL_GIGA_MAC_VER_08] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200183 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200184 [RTL_GIGA_MAC_VER_09] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200185 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200186 [RTL_GIGA_MAC_VER_10] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200187 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200188 [RTL_GIGA_MAC_VER_11] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200189 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200190 [RTL_GIGA_MAC_VER_12] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200191 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200192 [RTL_GIGA_MAC_VER_13] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200194 [RTL_GIGA_MAC_VER_14] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200195 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200196 [RTL_GIGA_MAC_VER_15] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200197 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200198 [RTL_GIGA_MAC_VER_16] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200200 [RTL_GIGA_MAC_VER_17] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200201 _R("RTL8168b/8111b", RTL_TD_1, NULL, JUMBO_4K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200202 [RTL_GIGA_MAC_VER_18] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200203 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200204 [RTL_GIGA_MAC_VER_19] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200205 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200206 [RTL_GIGA_MAC_VER_20] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200207 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200208 [RTL_GIGA_MAC_VER_21] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200209 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200210 [RTL_GIGA_MAC_VER_22] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200212 [RTL_GIGA_MAC_VER_23] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200213 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200214 [RTL_GIGA_MAC_VER_24] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200215 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200216 [RTL_GIGA_MAC_VER_25] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200217 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
218 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200219 [RTL_GIGA_MAC_VER_26] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200220 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
221 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200222 [RTL_GIGA_MAC_VER_27] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200223 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200224 [RTL_GIGA_MAC_VER_28] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200225 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200226 [RTL_GIGA_MAC_VER_29] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200227 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
228 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200229 [RTL_GIGA_MAC_VER_30] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200230 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
231 JUMBO_1K, true),
Francois Romieu85bffe62011-04-27 08:22:39 +0200232 [RTL_GIGA_MAC_VER_31] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200233 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200234 [RTL_GIGA_MAC_VER_32] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200235 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
236 JUMBO_9K, false),
Francois Romieu85bffe62011-04-27 08:22:39 +0200237 [RTL_GIGA_MAC_VER_33] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200238 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
239 JUMBO_9K, false),
Hayes Wang70090422011-07-06 15:58:06 +0800240 [RTL_GIGA_MAC_VER_34] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
242 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800243 [RTL_GIGA_MAC_VER_35] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200244 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
245 JUMBO_9K, false),
Hayes Wangc2218922011-09-06 16:55:18 +0800246 [RTL_GIGA_MAC_VER_36] =
Francois Romieud58d46b2011-05-03 16:38:29 +0200247 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
248 JUMBO_9K, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250#undef _R
251
Francois Romieubcf0bf92006-07-26 23:14:13 +0200252enum cfg_version {
253 RTL_CFG_0 = 0x00,
254 RTL_CFG_1,
255 RTL_CFG_2
256};
257
Francois Romieu07ce4062007-02-23 23:36:39 +0100258static void rtl_hw_start_8169(struct net_device *);
259static void rtl_hw_start_8168(struct net_device *);
260static void rtl_hw_start_8101(struct net_device *);
261
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000262static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200263 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200264 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200265 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100266 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200267 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
268 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000269 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200270 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200271 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
272 { PCI_VENDOR_ID_LINKSYS, 0x1032,
273 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100274 { 0x0001, 0x8168,
275 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {0,},
277};
278
279MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
280
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000281static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700282static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200283static struct {
284 u32 msg_enable;
285} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Francois Romieu07d3f512007-02-21 22:40:46 +0100287enum rtl_registers {
288 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100289 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100290 MAR0 = 8, /* Multicast filter. */
291 CounterAddrLow = 0x10,
292 CounterAddrHigh = 0x14,
293 TxDescStartAddrLow = 0x20,
294 TxDescStartAddrHigh = 0x24,
295 TxHDescStartAddrLow = 0x28,
296 TxHDescStartAddrHigh = 0x2c,
297 FLASH = 0x30,
298 ERSR = 0x36,
299 ChipCmd = 0x37,
300 TxPoll = 0x38,
301 IntrMask = 0x3c,
302 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700303
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800304 TxConfig = 0x40,
305#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
306#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
307
308 RxConfig = 0x44,
309#define RX128_INT_EN (1 << 15) /* 8111c and later */
310#define RX_MULTI_EN (1 << 14) /* 8111c only */
311#define RXCFG_FIFO_SHIFT 13
312 /* No threshold before first PCI xfer */
313#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
314#define RXCFG_DMA_SHIFT 8
315 /* Unlimited maximum PCI burst. */
316#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700317
Francois Romieu07d3f512007-02-21 22:40:46 +0100318 RxMissed = 0x4c,
319 Cfg9346 = 0x50,
320 Config0 = 0x51,
321 Config1 = 0x52,
322 Config2 = 0x53,
323 Config3 = 0x54,
324 Config4 = 0x55,
325 Config5 = 0x56,
326 MultiIntr = 0x5c,
327 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100328 PHYstatus = 0x6c,
329 RxMaxSize = 0xda,
330 CPlusCmd = 0xe0,
331 IntrMitigate = 0xe2,
332 RxDescAddrLow = 0xe4,
333 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000334 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
335
336#define NoEarlyTx 0x3f /* Max value : no early transmit. */
337
338 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
339
340#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800341#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000342
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 FuncEvent = 0xf0,
344 FuncEventMask = 0xf4,
345 FuncPresetState = 0xf8,
346 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
Francois Romieuf162a5d2008-06-01 22:37:49 +0200349enum rtl8110_registers {
350 TBICSR = 0x64,
351 TBI_ANAR = 0x68,
352 TBI_LPAR = 0x6a,
353};
354
355enum rtl8168_8101_registers {
356 CSIDR = 0x64,
357 CSIAR = 0x68,
358#define CSIAR_FLAG 0x80000000
359#define CSIAR_WRITE_CMD 0x80000000
360#define CSIAR_BYTE_ENABLE 0x0f
361#define CSIAR_BYTE_ENABLE_SHIFT 12
362#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000363 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200364 EPHYAR = 0x80,
365#define EPHYAR_FLAG 0x80000000
366#define EPHYAR_WRITE_CMD 0x80000000
367#define EPHYAR_REG_MASK 0x1f
368#define EPHYAR_REG_SHIFT 16
369#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800370 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200372 DBG_REG = 0xd1,
373#define FIX_NAK_1 (1 << 4)
374#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800375 TWSI = 0xd2,
376 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800377#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800378#define EN_NDP (1 << 3)
379#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000380 EFUSEAR = 0xdc,
381#define EFUSEAR_FLAG 0x80000000
382#define EFUSEAR_WRITE_CMD 0x80000000
383#define EFUSEAR_READ_CMD 0x00000000
384#define EFUSEAR_REG_MASK 0x03ff
385#define EFUSEAR_REG_SHIFT 8
386#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387};
388
françois romieuc0e45c12011-01-03 15:08:04 +0000389enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800390 LED_FREQ = 0x1a,
391 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000392 ERIDR = 0x70,
393 ERIAR = 0x74,
394#define ERIAR_FLAG 0x80000000
395#define ERIAR_WRITE_CMD 0x80000000
396#define ERIAR_READ_CMD 0x00000000
397#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000398#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800399#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
400#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
401#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
402#define ERIAR_MASK_SHIFT 12
403#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
404#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
405#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000406 EPHY_RXER_NUM = 0x7c,
407 OCPDR = 0xb0, /* OCP GPHY access */
408#define OCPDR_WRITE_CMD 0x80000000
409#define OCPDR_READ_CMD 0x00000000
410#define OCPDR_REG_MASK 0x7f
411#define OCPDR_GPHY_REG_SHIFT 16
412#define OCPDR_DATA_MASK 0xffff
413 OCPAR = 0xb4,
414#define OCPAR_FLAG 0x80000000
415#define OCPAR_GPHY_WRITE_CMD 0x8000f060
416#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000417 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
418 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200419#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800420#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000421};
422
Francois Romieu07d3f512007-02-21 22:40:46 +0100423enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100425 SYSErr = 0x8000,
426 PCSTimeout = 0x4000,
427 SWInt = 0x0100,
428 TxDescUnavail = 0x0080,
429 RxFIFOOver = 0x0040,
430 LinkChg = 0x0020,
431 RxOverflow = 0x0010,
432 TxErr = 0x0008,
433 TxOK = 0x0004,
434 RxErr = 0x0002,
435 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400438 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200439 RxFOVF = (1 << 23),
440 RxRWT = (1 << 22),
441 RxRES = (1 << 21),
442 RxRUNT = (1 << 20),
443 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800446 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100447 CmdReset = 0x10,
448 CmdRxEnb = 0x08,
449 CmdTxEnb = 0x04,
450 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Francois Romieu275391a2007-02-23 23:50:28 +0100452 /* TXPoll register p.5 */
453 HPQ = 0x80, /* Poll cmd on the high prio queue */
454 NPQ = 0x40, /* Poll cmd on the low prio queue */
455 FSWInt = 0x01, /* Forced software interrupt */
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100458 Cfg9346_Lock = 0x00,
459 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100462 AcceptErr = 0x20,
463 AcceptRunt = 0x10,
464 AcceptBroadcast = 0x08,
465 AcceptMulticast = 0x04,
466 AcceptMyPhys = 0x02,
467 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200468#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* TxConfigBits */
471 TxInterFrameGapShift = 24,
472 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
473
Francois Romieu5d06a992006-02-23 00:47:58 +0100474 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200475 LEDS1 = (1 << 7),
476 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200477 Speed_down = (1 << 4),
478 MEMMAP = (1 << 3),
479 IOMAP = (1 << 2),
480 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100481 PMEnable = (1 << 0), /* Power Management Enable */
482
Francois Romieu6dccd162007-02-13 23:38:05 +0100483 /* Config2 register p. 25 */
françois romieu2ca6cf02011-12-15 08:37:43 +0000484 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100485 PCI_Clock_66MHz = 0x01,
486 PCI_Clock_33MHz = 0x00,
487
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100488 /* Config3 register p.25 */
489 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
490 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200491 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200492 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100493
Francois Romieud58d46b2011-05-03 16:38:29 +0200494 /* Config4 register */
495 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
496
Francois Romieu5d06a992006-02-23 00:47:58 +0100497 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100498 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
499 MWF = (1 << 5), /* Accept Multicast wakeup frame */
500 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200501 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100502 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100503 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* TBICSR p.28 */
506 TBIReset = 0x80000000,
507 TBILoopback = 0x40000000,
508 TBINwEnable = 0x20000000,
509 TBINwRestart = 0x10000000,
510 TBILinkOk = 0x02000000,
511 TBINwComplete = 0x01000000,
512
513 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200514 EnableBist = (1 << 15), // 8168 8101
515 Mac_dbgo_oe = (1 << 14), // 8168 8101
516 Normal_mode = (1 << 13), // unused
517 Force_half_dup = (1 << 12), // 8168 8101
518 Force_rxflow_en = (1 << 11), // 8168 8101
519 Force_txflow_en = (1 << 10), // 8168 8101
520 Cxpl_dbg_sel = (1 << 9), // 8168 8101
521 ASF = (1 << 8), // 8168 8101
522 PktCntrDisable = (1 << 7), // 8168 8101
523 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 RxVlan = (1 << 6),
525 RxChkSum = (1 << 5),
526 PCIDAC = (1 << 4),
527 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100528 INTT_0 = 0x0000, // 8168
529 INTT_1 = 0x0001, // 8168
530 INTT_2 = 0x0002, // 8168
531 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100534 TBI_Enable = 0x80,
535 TxFlowCtrl = 0x40,
536 RxFlowCtrl = 0x20,
537 _1000bpsF = 0x10,
538 _100bps = 0x08,
539 _10bps = 0x04,
540 LinkStatus = 0x02,
541 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100544 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200545
546 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100547 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Francois Romieu2b7b4312011-04-18 22:53:24 -0700550enum rtl_desc_bit {
551 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
553 RingEnd = (1 << 30), /* End of descriptor ring */
554 FirstFrag = (1 << 29), /* First segment of a packet */
555 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700556};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Francois Romieu2b7b4312011-04-18 22:53:24 -0700558/* Generic case. */
559enum rtl_tx_desc_bit {
560 /* First doubleword. */
561 TD_LSO = (1 << 27), /* Large Send Offload */
562#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564 /* Second doubleword. */
565 TxVlanTag = (1 << 17), /* Add VLAN tag */
566};
567
568/* 8169, 8168b and 810x except 8102e. */
569enum rtl_tx_desc_bit_0 {
570 /* First doubleword. */
571#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
572 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
573 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
574 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
575};
576
577/* 8102e, 8168c and beyond. */
578enum rtl_tx_desc_bit_1 {
579 /* Second doubleword. */
580#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
581 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
582 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
583 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
584};
585
586static const struct rtl_tx_desc_info {
587 struct {
588 u32 udp;
589 u32 tcp;
590 } checksum;
591 u16 mss_shift;
592 u16 opts_offset;
593} tx_desc_info [] = {
594 [RTL_TD_0] = {
595 .checksum = {
596 .udp = TD0_IP_CS | TD0_UDP_CS,
597 .tcp = TD0_IP_CS | TD0_TCP_CS
598 },
599 .mss_shift = TD0_MSS_SHIFT,
600 .opts_offset = 0
601 },
602 [RTL_TD_1] = {
603 .checksum = {
604 .udp = TD1_IP_CS | TD1_UDP_CS,
605 .tcp = TD1_IP_CS | TD1_TCP_CS
606 },
607 .mss_shift = TD1_MSS_SHIFT,
608 .opts_offset = 1
609 }
610};
611
612enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Rx private */
614 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
615 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
616
617#define RxProtoUDP (PID1)
618#define RxProtoTCP (PID0)
619#define RxProtoIP (PID1 | PID0)
620#define RxProtoMask RxProtoIP
621
622 IPFail = (1 << 16), /* IP checksum failed */
623 UDPFail = (1 << 15), /* UDP/IP checksum failed */
624 TCPFail = (1 << 14), /* TCP/IP checksum failed */
625 RxVlanTag = (1 << 16), /* VLAN tag available */
626};
627
628#define RsvdMask 0x3fffc000
629
630struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200631 __le32 opts1;
632 __le32 opts2;
633 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634};
635
636struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200637 __le32 opts1;
638 __le32 opts2;
639 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
642struct ring_info {
643 struct sk_buff *skb;
644 u32 len;
645 u8 __pad[sizeof(void *) - sizeof(u32)];
646};
647
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200648enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200649 RTL_FEATURE_WOL = (1 << 0),
650 RTL_FEATURE_MSI = (1 << 1),
651 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200652};
653
Ivan Vecera355423d2009-02-06 21:49:57 -0800654struct rtl8169_counters {
655 __le64 tx_packets;
656 __le64 rx_packets;
657 __le64 tx_errors;
658 __le32 rx_errors;
659 __le16 rx_missed;
660 __le16 align_errors;
661 __le32 tx_one_collision;
662 __le32 tx_multi_collision;
663 __le64 rx_unicast;
664 __le64 rx_broadcast;
665 __le32 rx_multicast;
666 __le16 tx_aborted;
667 __le16 tx_underun;
668};
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670struct rtl8169_private {
671 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200672 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000673 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700674 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200675 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200676 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700677 u16 txd_version;
678 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
680 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
681 u32 dirty_rx;
682 u32 dirty_tx;
683 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
684 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
685 dma_addr_t TxPhyAddr;
686 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000687 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct timer_list timer;
690 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100691 u16 intr_event;
692 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000694
695 struct mdio_ops {
696 void (*write)(void __iomem *, int, int);
697 int (*read)(void __iomem *, int);
698 } mdio_ops;
699
françois romieu065c27c2011-01-03 15:08:12 +0000700 struct pll_power_ops {
701 void (*down)(struct rtl8169_private *);
702 void (*up)(struct rtl8169_private *);
703 } pll_power_ops;
704
Francois Romieud58d46b2011-05-03 16:38:29 +0200705 struct jumbo_ops {
706 void (*enable)(struct rtl8169_private *);
707 void (*disable)(struct rtl8169_private *);
708 } jumbo_ops;
709
Oliver Neukum54405cd2011-01-06 21:55:13 +0100710 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200711 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000712 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100713 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000714 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800716 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100717
718 struct {
719 struct work_struct work;
720 } wk;
721
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200722 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200723
724 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800725 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000726 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400727 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000728
Francois Romieub6ffd972011-06-17 17:00:05 +0200729 struct rtl_fw {
730 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200731
732#define RTL_VER_SIZE 32
733
734 char version[RTL_VER_SIZE];
735
736 struct rtl_fw_phy_action {
737 __le32 *code;
738 size_t size;
739 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200740 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300741#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742};
743
Ralf Baechle979b6c12005-06-13 14:30:40 -0700744MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700747MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200748module_param_named(debug, debug.msg_enable, int, 0);
749MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750MODULE_LICENSE("GPL");
751MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000752MODULE_FIRMWARE(FIRMWARE_8168D_1);
753MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000754MODULE_FIRMWARE(FIRMWARE_8168E_1);
755MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400756MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800757MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800758MODULE_FIRMWARE(FIRMWARE_8168F_1);
759MODULE_FIRMWARE(FIRMWARE_8168F_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000762static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
763 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100764static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100766static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100768static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200770static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700772 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200773static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200775static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700776static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Francois Romieud58d46b2011-05-03 16:38:29 +0200778static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
779{
780 int cap = pci_pcie_cap(pdev);
781
782 if (cap) {
783 u16 ctl;
784
785 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
786 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
787 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
788 }
789}
790
françois romieub646d902011-01-03 15:08:21 +0000791static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
792{
793 void __iomem *ioaddr = tp->mmio_addr;
794 int i;
795
796 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
797 for (i = 0; i < 20; i++) {
798 udelay(100);
799 if (RTL_R32(OCPAR) & OCPAR_FLAG)
800 break;
801 }
802 return RTL_R32(OCPDR);
803}
804
805static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
806{
807 void __iomem *ioaddr = tp->mmio_addr;
808 int i;
809
810 RTL_W32(OCPDR, data);
811 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
812 for (i = 0; i < 20; i++) {
813 udelay(100);
814 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
815 break;
816 }
817}
818
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800819static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000820{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800821 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000822 int i;
823
824 RTL_W8(ERIDR, cmd);
825 RTL_W32(ERIAR, 0x800010e8);
826 msleep(2);
827 for (i = 0; i < 5; i++) {
828 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200829 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000830 break;
831 }
832
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800833 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000834}
835
836#define OOB_CMD_RESET 0x00
837#define OOB_CMD_DRIVER_START 0x05
838#define OOB_CMD_DRIVER_STOP 0x06
839
Francois Romieucecb5fd2011-04-01 10:21:07 +0200840static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
841{
842 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
843}
844
françois romieub646d902011-01-03 15:08:21 +0000845static void rtl8168_driver_start(struct rtl8169_private *tp)
846{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200847 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000848 int i;
849
850 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
851
Francois Romieucecb5fd2011-04-01 10:21:07 +0200852 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000853
françois romieub646d902011-01-03 15:08:21 +0000854 for (i = 0; i < 10; i++) {
855 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000856 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000857 break;
858 }
859}
860
861static void rtl8168_driver_stop(struct rtl8169_private *tp)
862{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200863 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000864 int i;
865
866 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
867
Francois Romieucecb5fd2011-04-01 10:21:07 +0200868 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000869
françois romieub646d902011-01-03 15:08:21 +0000870 for (i = 0; i < 10; i++) {
871 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000872 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000873 break;
874 }
875}
876
hayeswang4804b3b2011-03-21 01:50:29 +0000877static int r8168dp_check_dash(struct rtl8169_private *tp)
878{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200879 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000880
Francois Romieucecb5fd2011-04-01 10:21:07 +0200881 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000882}
françois romieub646d902011-01-03 15:08:21 +0000883
françois romieu4da19632011-01-03 15:07:55 +0000884static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int i;
887
Francois Romieua6baf3a2007-11-08 23:23:21 +0100888 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Francois Romieu23714082006-01-29 00:49:09 +0100890 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100891 /*
892 * Check if the RTL8169 has completed writing to the specified
893 * MII register.
894 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200895 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
Francois Romieu23714082006-01-29 00:49:09 +0100897 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700899 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700900 * According to hardware specs a 20us delay is required after write
901 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700902 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700903 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
françois romieu4da19632011-01-03 15:07:55 +0000906static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 int i, value = -1;
909
Francois Romieua6baf3a2007-11-08 23:23:21 +0100910 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Francois Romieu23714082006-01-29 00:49:09 +0100912 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100913 /*
914 * Check if the RTL8169 has completed retrieving data from
915 * the specified MII register.
916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100918 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 }
Francois Romieu23714082006-01-29 00:49:09 +0100921 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700923 /*
924 * According to hardware specs a 20us delay is required after read
925 * complete indication, but before sending next command.
926 */
927 udelay(20);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return value;
930}
931
françois romieuc0e45c12011-01-03 15:08:04 +0000932static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
933{
934 int i;
935
936 RTL_W32(OCPDR, data |
937 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
938 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
939 RTL_W32(EPHY_RXER_NUM, 0);
940
941 for (i = 0; i < 100; i++) {
942 mdelay(1);
943 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
944 break;
945 }
946}
947
948static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
949{
950 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
951 (value & OCPDR_DATA_MASK));
952}
953
954static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
955{
956 int i;
957
958 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
959
960 mdelay(1);
961 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
962 RTL_W32(EPHY_RXER_NUM, 0);
963
964 for (i = 0; i < 100; i++) {
965 mdelay(1);
966 if (RTL_R32(OCPAR) & OCPAR_FLAG)
967 break;
968 }
969
970 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
971}
972
françois romieue6de30d2011-01-03 15:08:37 +0000973#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
974
975static void r8168dp_2_mdio_start(void __iomem *ioaddr)
976{
977 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
978}
979
980static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
981{
982 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
983}
984
985static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
986{
987 r8168dp_2_mdio_start(ioaddr);
988
989 r8169_mdio_write(ioaddr, reg_addr, value);
990
991 r8168dp_2_mdio_stop(ioaddr);
992}
993
994static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
995{
996 int value;
997
998 r8168dp_2_mdio_start(ioaddr);
999
1000 value = r8169_mdio_read(ioaddr, reg_addr);
1001
1002 r8168dp_2_mdio_stop(ioaddr);
1003
1004 return value;
1005}
1006
françois romieu4da19632011-01-03 15:07:55 +00001007static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001008{
françois romieuc0e45c12011-01-03 15:08:04 +00001009 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001010}
1011
françois romieu4da19632011-01-03 15:07:55 +00001012static int rtl_readphy(struct rtl8169_private *tp, int location)
1013{
françois romieuc0e45c12011-01-03 15:08:04 +00001014 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +00001015}
1016
1017static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1018{
1019 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1020}
1021
1022static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001023{
1024 int val;
1025
françois romieu4da19632011-01-03 15:07:55 +00001026 val = rtl_readphy(tp, reg_addr);
1027 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +00001028}
1029
Francois Romieuccdffb92008-07-26 14:26:06 +02001030static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1031 int val)
1032{
1033 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001034
françois romieu4da19632011-01-03 15:07:55 +00001035 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +02001036}
1037
1038static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1039{
1040 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +02001041
françois romieu4da19632011-01-03 15:07:55 +00001042 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +02001043}
1044
Francois Romieudacf8152008-08-02 20:44:13 +02001045static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
1046{
1047 unsigned int i;
1048
1049 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1050 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1051
1052 for (i = 0; i < 100; i++) {
1053 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
1054 break;
1055 udelay(10);
1056 }
1057}
1058
1059static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1060{
1061 u16 value = 0xffff;
1062 unsigned int i;
1063
1064 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1065
1066 for (i = 0; i < 100; i++) {
1067 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1068 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1069 break;
1070 }
1071 udelay(10);
1072 }
1073
1074 return value;
1075}
1076
1077static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1078{
1079 unsigned int i;
1080
1081 RTL_W32(CSIDR, value);
1082 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1083 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1084
1085 for (i = 0; i < 100; i++) {
1086 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1087 break;
1088 udelay(10);
1089 }
1090}
1091
1092static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1093{
1094 u32 value = ~0x00;
1095 unsigned int i;
1096
1097 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1098 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1099
1100 for (i = 0; i < 100; i++) {
1101 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1102 value = RTL_R32(CSIDR);
1103 break;
1104 }
1105 udelay(10);
1106 }
1107
1108 return value;
1109}
1110
Hayes Wang133ac402011-07-06 15:58:05 +08001111static
1112void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1113{
1114 unsigned int i;
1115
1116 BUG_ON((addr & 3) || (mask == 0));
1117 RTL_W32(ERIDR, val);
1118 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1119
1120 for (i = 0; i < 100; i++) {
1121 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1122 break;
1123 udelay(100);
1124 }
1125}
1126
1127static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1128{
1129 u32 value = ~0x00;
1130 unsigned int i;
1131
1132 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1133
1134 for (i = 0; i < 100; i++) {
1135 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1136 value = RTL_R32(ERIDR);
1137 break;
1138 }
1139 udelay(100);
1140 }
1141
1142 return value;
1143}
1144
1145static void
1146rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1147{
1148 u32 val;
1149
1150 val = rtl_eri_read(ioaddr, addr, type);
1151 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1152}
1153
françois romieuc28aa382011-08-02 03:53:43 +00001154struct exgmac_reg {
1155 u16 addr;
1156 u16 mask;
1157 u32 val;
1158};
1159
1160static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1161 const struct exgmac_reg *r, int len)
1162{
1163 while (len-- > 0) {
1164 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1165 r++;
1166 }
1167}
1168
françois romieudaf9df62009-10-07 12:44:20 +00001169static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1170{
1171 u8 value = 0xff;
1172 unsigned int i;
1173
1174 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1175
1176 for (i = 0; i < 300; i++) {
1177 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1178 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1179 break;
1180 }
1181 udelay(100);
1182 }
1183
1184 return value;
1185}
1186
Francois Romieu3e990ff2012-01-26 12:50:01 +01001187static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1188{
1189 void __iomem *ioaddr = tp->mmio_addr;
1190
1191 RTL_W16(IntrMask, bits);
1192}
1193
françois romieu811fd302011-12-04 20:30:45 +00001194static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
françois romieu811fd302011-12-04 20:30:45 +00001196 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
françois romieu811fd302011-12-04 20:30:45 +00001198 RTL_W16(IntrMask, 0x0000);
1199 RTL_W16(IntrStatus, tp->intr_event);
1200 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
françois romieu4da19632011-01-03 15:07:55 +00001203static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
françois romieu4da19632011-01-03 15:07:55 +00001205 void __iomem *ioaddr = tp->mmio_addr;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return RTL_R32(TBICSR) & TBIReset;
1208}
1209
françois romieu4da19632011-01-03 15:07:55 +00001210static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
françois romieu4da19632011-01-03 15:07:55 +00001212 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1216{
1217 return RTL_R32(TBICSR) & TBILinkOk;
1218}
1219
1220static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1221{
1222 return RTL_R8(PHYstatus) & LinkStatus;
1223}
1224
françois romieu4da19632011-01-03 15:07:55 +00001225static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
françois romieu4da19632011-01-03 15:07:55 +00001227 void __iomem *ioaddr = tp->mmio_addr;
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1230}
1231
françois romieu4da19632011-01-03 15:07:55 +00001232static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 unsigned int val;
1235
françois romieu4da19632011-01-03 15:07:55 +00001236 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1237 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Hayes Wang70090422011-07-06 15:58:06 +08001240static void rtl_link_chg_patch(struct rtl8169_private *tp)
1241{
1242 void __iomem *ioaddr = tp->mmio_addr;
1243 struct net_device *dev = tp->dev;
1244
1245 if (!netif_running(dev))
1246 return;
1247
1248 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1249 if (RTL_R8(PHYstatus) & _1000bpsF) {
1250 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1251 0x00000011, ERIAR_EXGMAC);
1252 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1253 0x00000005, ERIAR_EXGMAC);
1254 } else if (RTL_R8(PHYstatus) & _100bps) {
1255 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1256 0x0000001f, ERIAR_EXGMAC);
1257 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1258 0x00000005, ERIAR_EXGMAC);
1259 } else {
1260 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1261 0x0000001f, ERIAR_EXGMAC);
1262 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1263 0x0000003f, ERIAR_EXGMAC);
1264 }
1265 /* Reset packet filter */
1266 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1267 ERIAR_EXGMAC);
1268 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1269 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001270 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1271 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1272 if (RTL_R8(PHYstatus) & _1000bpsF) {
1273 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1274 0x00000011, ERIAR_EXGMAC);
1275 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1276 0x00000005, ERIAR_EXGMAC);
1277 } else {
1278 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1279 0x0000001f, ERIAR_EXGMAC);
1280 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1281 0x0000003f, ERIAR_EXGMAC);
1282 }
Hayes Wang70090422011-07-06 15:58:06 +08001283 }
1284}
1285
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001286static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001287 struct rtl8169_private *tp,
1288 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 unsigned long flags;
1291
1292 spin_lock_irqsave(&tp->lock, flags);
1293 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001294 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001295 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001296 if (pm)
1297 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001299 if (net_ratelimit())
1300 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001301 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001303 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001304 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001305 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 spin_unlock_irqrestore(&tp->lock, flags);
1308}
1309
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001310static void rtl8169_check_link_status(struct net_device *dev,
1311 struct rtl8169_private *tp,
1312 void __iomem *ioaddr)
1313{
1314 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1315}
1316
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001317#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1318
1319static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1320{
1321 void __iomem *ioaddr = tp->mmio_addr;
1322 u8 options;
1323 u32 wolopts = 0;
1324
1325 options = RTL_R8(Config1);
1326 if (!(options & PMEnable))
1327 return 0;
1328
1329 options = RTL_R8(Config3);
1330 if (options & LinkUp)
1331 wolopts |= WAKE_PHY;
1332 if (options & MagicPacket)
1333 wolopts |= WAKE_MAGIC;
1334
1335 options = RTL_R8(Config5);
1336 if (options & UWF)
1337 wolopts |= WAKE_UCAST;
1338 if (options & BWF)
1339 wolopts |= WAKE_BCAST;
1340 if (options & MWF)
1341 wolopts |= WAKE_MCAST;
1342
1343 return wolopts;
1344}
1345
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001346static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1347{
1348 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001349
1350 spin_lock_irq(&tp->lock);
1351
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001352 wol->supported = WAKE_ANY;
1353 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001354
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001355 spin_unlock_irq(&tp->lock);
1356}
1357
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001358static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001359{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001360 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001361 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001362 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001363 u32 opt;
1364 u16 reg;
1365 u8 mask;
1366 } cfg[] = {
1367 { WAKE_ANY, Config1, PMEnable },
1368 { WAKE_PHY, Config3, LinkUp },
1369 { WAKE_MAGIC, Config3, MagicPacket },
1370 { WAKE_UCAST, Config5, UWF },
1371 { WAKE_BCAST, Config5, BWF },
1372 { WAKE_MCAST, Config5, MWF },
1373 { WAKE_ANY, Config5, LanWake }
1374 };
1375
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001376 RTL_W8(Cfg9346, Cfg9346_Unlock);
1377
1378 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1379 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001380 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001381 options |= cfg[i].mask;
1382 RTL_W8(cfg[i].reg, options);
1383 }
1384
1385 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001386}
1387
1388static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1389{
1390 struct rtl8169_private *tp = netdev_priv(dev);
1391
1392 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001394 if (wol->wolopts)
1395 tp->features |= RTL_FEATURE_WOL;
1396 else
1397 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001398 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399 spin_unlock_irq(&tp->lock);
1400
françois romieuea809072010-11-08 13:23:58 +00001401 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1402
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403 return 0;
1404}
1405
Francois Romieu31bd2042011-04-26 18:58:59 +02001406static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1407{
Francois Romieu85bffe62011-04-27 08:22:39 +02001408 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411static void rtl8169_get_drvinfo(struct net_device *dev,
1412 struct ethtool_drvinfo *info)
1413{
1414 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001415 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Rick Jones68aad782011-11-07 13:29:27 +00001417 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1418 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1419 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001420 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001421 if (!IS_ERR_OR_NULL(rtl_fw))
1422 strlcpy(info->fw_version, rtl_fw->version,
1423 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426static int rtl8169_get_regs_len(struct net_device *dev)
1427{
1428 return R8169_REGS_SIZE;
1429}
1430
1431static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001432 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct rtl8169_private *tp = netdev_priv(dev);
1435 void __iomem *ioaddr = tp->mmio_addr;
1436 int ret = 0;
1437 u32 reg;
1438
1439 reg = RTL_R32(TBICSR);
1440 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1441 (duplex == DUPLEX_FULL)) {
1442 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1443 } else if (autoneg == AUTONEG_ENABLE)
1444 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1445 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001446 netif_warn(tp, link, dev,
1447 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ret = -EOPNOTSUPP;
1449 }
1450
1451 return ret;
1452}
1453
1454static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001455 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001458 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001459 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Hayes Wang716b50a2011-02-22 17:26:18 +08001461 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001464 int auto_nego;
1465
françois romieu4da19632011-01-03 15:07:55 +00001466 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001467 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1468 ADVERTISE_100HALF | ADVERTISE_100FULL);
1469
1470 if (adv & ADVERTISED_10baseT_Half)
1471 auto_nego |= ADVERTISE_10HALF;
1472 if (adv & ADVERTISED_10baseT_Full)
1473 auto_nego |= ADVERTISE_10FULL;
1474 if (adv & ADVERTISED_100baseT_Half)
1475 auto_nego |= ADVERTISE_100HALF;
1476 if (adv & ADVERTISED_100baseT_Full)
1477 auto_nego |= ADVERTISE_100FULL;
1478
françois romieu3577aa12009-05-19 10:46:48 +00001479 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1480
françois romieu4da19632011-01-03 15:07:55 +00001481 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001482 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1483
1484 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001485 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001486 if (adv & ADVERTISED_1000baseT_Half)
1487 giga_ctrl |= ADVERTISE_1000HALF;
1488 if (adv & ADVERTISED_1000baseT_Full)
1489 giga_ctrl |= ADVERTISE_1000FULL;
1490 } else if (adv & (ADVERTISED_1000baseT_Half |
1491 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001492 netif_info(tp, link, dev,
1493 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001494 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
françois romieu3577aa12009-05-19 10:46:48 +00001497 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001498
françois romieu4da19632011-01-03 15:07:55 +00001499 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1500 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001501 } else {
1502 giga_ctrl = 0;
1503
1504 if (speed == SPEED_10)
1505 bmcr = 0;
1506 else if (speed == SPEED_100)
1507 bmcr = BMCR_SPEED100;
1508 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001509 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001510
1511 if (duplex == DUPLEX_FULL)
1512 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001513 }
1514
françois romieu4da19632011-01-03 15:07:55 +00001515 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001516
Francois Romieucecb5fd2011-04-01 10:21:07 +02001517 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1518 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001519 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001520 rtl_writephy(tp, 0x17, 0x2138);
1521 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001522 } else {
françois romieu4da19632011-01-03 15:07:55 +00001523 rtl_writephy(tp, 0x17, 0x2108);
1524 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001525 }
1526 }
1527
Oliver Neukum54405cd2011-01-06 21:55:13 +01001528 rc = 0;
1529out:
1530 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001534 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct rtl8169_private *tp = netdev_priv(dev);
1537 int ret;
1538
Oliver Neukum54405cd2011-01-06 21:55:13 +01001539 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001540 if (ret < 0)
1541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Francois Romieu4876cc12011-03-11 21:07:11 +01001543 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1544 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001546 }
1547out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return ret;
1549}
1550
1551static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1552{
1553 struct rtl8169_private *tp = netdev_priv(dev);
1554 unsigned long flags;
1555 int ret;
1556
Francois Romieu4876cc12011-03-11 21:07:11 +01001557 del_timer_sync(&tp->timer);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001560 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001561 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return ret;
1565}
1566
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001567static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1568 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Francois Romieud58d46b2011-05-03 16:38:29 +02001570 struct rtl8169_private *tp = netdev_priv(dev);
1571
Francois Romieu2b7b4312011-04-18 22:53:24 -07001572 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001573 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Francois Romieud58d46b2011-05-03 16:38:29 +02001575 if (dev->mtu > JUMBO_1K &&
1576 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1577 features &= ~NETIF_F_IP_CSUM;
1578
Michał Mirosław350fb322011-04-08 06:35:56 +00001579 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001582static int rtl8169_set_features(struct net_device *dev,
1583 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct rtl8169_private *tp = netdev_priv(dev);
1586 void __iomem *ioaddr = tp->mmio_addr;
1587 unsigned long flags;
1588
1589 spin_lock_irqsave(&tp->lock, flags);
1590
Michał Mirosław350fb322011-04-08 06:35:56 +00001591 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 tp->cp_cmd |= RxChkSum;
1593 else
1594 tp->cp_cmd &= ~RxChkSum;
1595
Michał Mirosław350fb322011-04-08 06:35:56 +00001596 if (dev->features & NETIF_F_HW_VLAN_RX)
1597 tp->cp_cmd |= RxVlan;
1598 else
1599 tp->cp_cmd &= ~RxVlan;
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 RTL_W16(CPlusCmd, tp->cp_cmd);
1602 RTL_R16(CPlusCmd);
1603
1604 spin_unlock_irqrestore(&tp->lock, flags);
1605
1606 return 0;
1607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1610 struct sk_buff *skb)
1611{
Jesse Grosseab6d182010-10-20 13:56:03 +00001612 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1614}
1615
Francois Romieu7a8fc772011-03-01 17:18:33 +01001616static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Francois Romieu7a8fc772011-03-01 17:18:33 +01001620 if (opts2 & RxVlanTag)
1621 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Francois Romieuccdffb92008-07-26 14:26:06 +02001626static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 struct rtl8169_private *tp = netdev_priv(dev);
1629 void __iomem *ioaddr = tp->mmio_addr;
1630 u32 status;
1631
1632 cmd->supported =
1633 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1634 cmd->port = PORT_FIBRE;
1635 cmd->transceiver = XCVR_INTERNAL;
1636
1637 status = RTL_R32(TBICSR);
1638 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1639 cmd->autoneg = !!(status & TBINwEnable);
1640
David Decotigny70739492011-04-27 18:32:40 +00001641 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001643
1644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Francois Romieuccdffb92008-07-26 14:26:06 +02001647static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Francois Romieuccdffb92008-07-26 14:26:06 +02001651 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1655{
1656 struct rtl8169_private *tp = netdev_priv(dev);
1657 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001658 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 spin_lock_irqsave(&tp->lock, flags);
1661
Francois Romieuccdffb92008-07-26 14:26:06 +02001662 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001665 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
1668static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1669 void *p)
1670{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001671 struct rtl8169_private *tp = netdev_priv(dev);
1672 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Francois Romieu5b0384f2006-08-16 16:00:01 +02001674 if (regs->len > R8169_REGS_SIZE)
1675 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Francois Romieu5b0384f2006-08-16 16:00:01 +02001677 spin_lock_irqsave(&tp->lock, flags);
1678 memcpy_fromio(p, tp->mmio_addr, regs->len);
1679 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001682static u32 rtl8169_get_msglevel(struct net_device *dev)
1683{
1684 struct rtl8169_private *tp = netdev_priv(dev);
1685
1686 return tp->msg_enable;
1687}
1688
1689static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1690{
1691 struct rtl8169_private *tp = netdev_priv(dev);
1692
1693 tp->msg_enable = value;
1694}
1695
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001696static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1697 "tx_packets",
1698 "rx_packets",
1699 "tx_errors",
1700 "rx_errors",
1701 "rx_missed",
1702 "align_errors",
1703 "tx_single_collisions",
1704 "tx_multi_collisions",
1705 "unicast",
1706 "broadcast",
1707 "multicast",
1708 "tx_aborted",
1709 "tx_underrun",
1710};
1711
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001712static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001713{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001714 switch (sset) {
1715 case ETH_SS_STATS:
1716 return ARRAY_SIZE(rtl8169_gstrings);
1717 default:
1718 return -EOPNOTSUPP;
1719 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001720}
1721
Ivan Vecera355423d2009-02-06 21:49:57 -08001722static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001723{
1724 struct rtl8169_private *tp = netdev_priv(dev);
1725 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001726 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001727 struct rtl8169_counters *counters;
1728 dma_addr_t paddr;
1729 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001730 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001731
Ivan Vecera355423d2009-02-06 21:49:57 -08001732 /*
1733 * Some chips are unable to dump tally counters when the receiver
1734 * is disabled.
1735 */
1736 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1737 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001738
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001739 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001740 if (!counters)
1741 return;
1742
1743 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001744 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001745 RTL_W32(CounterAddrLow, cmd);
1746 RTL_W32(CounterAddrLow, cmd | CounterDump);
1747
Ivan Vecera355423d2009-02-06 21:49:57 -08001748 while (wait--) {
1749 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001750 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001751 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001752 }
1753 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001754 }
1755
1756 RTL_W32(CounterAddrLow, 0);
1757 RTL_W32(CounterAddrHigh, 0);
1758
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001759 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001760}
1761
Ivan Vecera355423d2009-02-06 21:49:57 -08001762static void rtl8169_get_ethtool_stats(struct net_device *dev,
1763 struct ethtool_stats *stats, u64 *data)
1764{
1765 struct rtl8169_private *tp = netdev_priv(dev);
1766
1767 ASSERT_RTNL();
1768
1769 rtl8169_update_counters(dev);
1770
1771 data[0] = le64_to_cpu(tp->counters.tx_packets);
1772 data[1] = le64_to_cpu(tp->counters.rx_packets);
1773 data[2] = le64_to_cpu(tp->counters.tx_errors);
1774 data[3] = le32_to_cpu(tp->counters.rx_errors);
1775 data[4] = le16_to_cpu(tp->counters.rx_missed);
1776 data[5] = le16_to_cpu(tp->counters.align_errors);
1777 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1778 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1779 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1780 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1781 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1782 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1783 data[12] = le16_to_cpu(tp->counters.tx_underun);
1784}
1785
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001786static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1787{
1788 switch(stringset) {
1789 case ETH_SS_STATS:
1790 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1791 break;
1792 }
1793}
1794
Jeff Garzik7282d492006-09-13 14:30:00 -04001795static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 .get_drvinfo = rtl8169_get_drvinfo,
1797 .get_regs_len = rtl8169_get_regs_len,
1798 .get_link = ethtool_op_get_link,
1799 .get_settings = rtl8169_get_settings,
1800 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001801 .get_msglevel = rtl8169_get_msglevel,
1802 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001804 .get_wol = rtl8169_get_wol,
1805 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001806 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001807 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001808 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809};
1810
Francois Romieu07d3f512007-02-21 22:40:46 +01001811static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001812 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Francois Romieu5d320a22011-05-08 17:47:36 +02001814 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001815 /*
1816 * The driver currently handles the 8168Bf and the 8168Be identically
1817 * but they can be identified more specifically through the test below
1818 * if needed:
1819 *
1820 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001821 *
1822 * Same thing for the 8101Eb and the 8101Ec:
1823 *
1824 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001825 */
Francois Romieu37441002011-06-17 22:58:54 +02001826 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001828 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 int mac_version;
1830 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001831 /* 8168F family. */
1832 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1833 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1834
hayeswang01dc7fe2011-03-21 01:50:28 +00001835 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001836 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001837 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1838 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1839 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1840
Francois Romieu5b538df2008-07-20 16:22:45 +02001841 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001842 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1843 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001844 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001845
françois romieue6de30d2011-01-03 15:08:37 +00001846 /* 8168DP family. */
1847 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1848 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001849 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001850
Francois Romieuef808d52008-06-29 13:10:54 +02001851 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001852 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001853 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001854 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001855 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001856 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1857 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001858 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001859 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001860 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001861
1862 /* 8168B family. */
1863 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1864 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1865 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1866 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1867
1868 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001869 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001870 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1871 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1872 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001873 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1874 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1875 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1876 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1877 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1878 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001879 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001880 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001881 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001882 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1883 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001884 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1885 /* FIXME: where did these entries come from ? -- FR */
1886 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1887 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1888
1889 /* 8110 family. */
1890 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1891 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1892 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1893 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1894 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1895 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1896
Jean Delvaref21b75e2009-05-26 20:54:48 -07001897 /* Catch-all */
1898 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001899 };
1900 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 u32 reg;
1902
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001903 reg = RTL_R32(TxConfig);
1904 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 p++;
1906 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001907
1908 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1909 netif_notice(tp, probe, dev,
1910 "unknown MAC, using family default\n");
1911 tp->mac_version = default_version;
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1916{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001917 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Francois Romieu867763c2007-08-17 18:21:58 +02001920struct phy_reg {
1921 u16 reg;
1922 u16 val;
1923};
1924
françois romieu4da19632011-01-03 15:07:55 +00001925static void rtl_writephy_batch(struct rtl8169_private *tp,
1926 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001927{
1928 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001929 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001930 regs++;
1931 }
1932}
1933
françois romieubca03d52011-01-03 15:07:31 +00001934#define PHY_READ 0x00000000
1935#define PHY_DATA_OR 0x10000000
1936#define PHY_DATA_AND 0x20000000
1937#define PHY_BJMPN 0x30000000
1938#define PHY_READ_EFUSE 0x40000000
1939#define PHY_READ_MAC_BYTE 0x50000000
1940#define PHY_WRITE_MAC_BYTE 0x60000000
1941#define PHY_CLEAR_READCOUNT 0x70000000
1942#define PHY_WRITE 0x80000000
1943#define PHY_READCOUNT_EQ_SKIP 0x90000000
1944#define PHY_COMP_EQ_SKIPN 0xa0000000
1945#define PHY_COMP_NEQ_SKIPN 0xb0000000
1946#define PHY_WRITE_PREVIOUS 0xc0000000
1947#define PHY_SKIPN 0xd0000000
1948#define PHY_DELAY_MS 0xe0000000
1949#define PHY_WRITE_ERI_WORD 0xf0000000
1950
Hayes Wang960aee62011-06-18 11:37:48 +02001951struct fw_info {
1952 u32 magic;
1953 char version[RTL_VER_SIZE];
1954 __le32 fw_start;
1955 __le32 fw_len;
1956 u8 chksum;
1957} __packed;
1958
Francois Romieu1c361ef2011-06-17 17:16:24 +02001959#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1960
1961static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001962{
Francois Romieub6ffd972011-06-17 17:00:05 +02001963 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001964 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001965 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1966 char *version = rtl_fw->version;
1967 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001968
Francois Romieu1c361ef2011-06-17 17:16:24 +02001969 if (fw->size < FW_OPCODE_SIZE)
1970 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001971
1972 if (!fw_info->magic) {
1973 size_t i, size, start;
1974 u8 checksum = 0;
1975
1976 if (fw->size < sizeof(*fw_info))
1977 goto out;
1978
1979 for (i = 0; i < fw->size; i++)
1980 checksum += fw->data[i];
1981 if (checksum != 0)
1982 goto out;
1983
1984 start = le32_to_cpu(fw_info->fw_start);
1985 if (start > fw->size)
1986 goto out;
1987
1988 size = le32_to_cpu(fw_info->fw_len);
1989 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1990 goto out;
1991
1992 memcpy(version, fw_info->version, RTL_VER_SIZE);
1993
1994 pa->code = (__le32 *)(fw->data + start);
1995 pa->size = size;
1996 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001997 if (fw->size % FW_OPCODE_SIZE)
1998 goto out;
1999
2000 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2001
2002 pa->code = (__le32 *)fw->data;
2003 pa->size = fw->size / FW_OPCODE_SIZE;
2004 }
2005 version[RTL_VER_SIZE - 1] = 0;
2006
2007 rc = true;
2008out:
2009 return rc;
2010}
2011
Francois Romieufd112f22011-06-18 00:10:29 +02002012static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2013 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002014{
Francois Romieufd112f22011-06-18 00:10:29 +02002015 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002016 size_t index;
2017
Francois Romieu1c361ef2011-06-17 17:16:24 +02002018 for (index = 0; index < pa->size; index++) {
2019 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002020 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002021
hayeswang42b82dc2011-01-10 02:07:25 +00002022 switch(action & 0xf0000000) {
2023 case PHY_READ:
2024 case PHY_DATA_OR:
2025 case PHY_DATA_AND:
2026 case PHY_READ_EFUSE:
2027 case PHY_CLEAR_READCOUNT:
2028 case PHY_WRITE:
2029 case PHY_WRITE_PREVIOUS:
2030 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002031 break;
2032
hayeswang42b82dc2011-01-10 02:07:25 +00002033 case PHY_BJMPN:
2034 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002035 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002036 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002037 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002038 }
2039 break;
2040 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002041 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002042 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002043 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002044 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002045 }
2046 break;
2047 case PHY_COMP_EQ_SKIPN:
2048 case PHY_COMP_NEQ_SKIPN:
2049 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002050 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002051 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002052 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002053 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002054 }
2055 break;
2056
2057 case PHY_READ_MAC_BYTE:
2058 case PHY_WRITE_MAC_BYTE:
2059 case PHY_WRITE_ERI_WORD:
2060 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002061 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002062 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002063 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002064 }
2065 }
Francois Romieufd112f22011-06-18 00:10:29 +02002066 rc = true;
2067out:
2068 return rc;
2069}
françois romieubca03d52011-01-03 15:07:31 +00002070
Francois Romieufd112f22011-06-18 00:10:29 +02002071static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2072{
2073 struct net_device *dev = tp->dev;
2074 int rc = -EINVAL;
2075
2076 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2077 netif_err(tp, ifup, dev, "invalid firwmare\n");
2078 goto out;
2079 }
2080
2081 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2082 rc = 0;
2083out:
2084 return rc;
2085}
2086
2087static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2088{
2089 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2090 u32 predata, count;
2091 size_t index;
2092
2093 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002094
Francois Romieu1c361ef2011-06-17 17:16:24 +02002095 for (index = 0; index < pa->size; ) {
2096 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002097 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002098 u32 regno = (action & 0x0fff0000) >> 16;
2099
2100 if (!action)
2101 break;
françois romieubca03d52011-01-03 15:07:31 +00002102
2103 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002104 case PHY_READ:
2105 predata = rtl_readphy(tp, regno);
2106 count++;
2107 index++;
françois romieubca03d52011-01-03 15:07:31 +00002108 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002109 case PHY_DATA_OR:
2110 predata |= data;
2111 index++;
2112 break;
2113 case PHY_DATA_AND:
2114 predata &= data;
2115 index++;
2116 break;
2117 case PHY_BJMPN:
2118 index -= regno;
2119 break;
2120 case PHY_READ_EFUSE:
2121 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2122 index++;
2123 break;
2124 case PHY_CLEAR_READCOUNT:
2125 count = 0;
2126 index++;
2127 break;
2128 case PHY_WRITE:
2129 rtl_writephy(tp, regno, data);
2130 index++;
2131 break;
2132 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002133 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002134 break;
2135 case PHY_COMP_EQ_SKIPN:
2136 if (predata == data)
2137 index += regno;
2138 index++;
2139 break;
2140 case PHY_COMP_NEQ_SKIPN:
2141 if (predata != data)
2142 index += regno;
2143 index++;
2144 break;
2145 case PHY_WRITE_PREVIOUS:
2146 rtl_writephy(tp, regno, predata);
2147 index++;
2148 break;
2149 case PHY_SKIPN:
2150 index += regno + 1;
2151 break;
2152 case PHY_DELAY_MS:
2153 mdelay(data);
2154 index++;
2155 break;
2156
2157 case PHY_READ_MAC_BYTE:
2158 case PHY_WRITE_MAC_BYTE:
2159 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002160 default:
2161 BUG();
2162 }
2163 }
2164}
2165
françois romieuf1e02ed2011-01-13 13:07:53 +00002166static void rtl_release_firmware(struct rtl8169_private *tp)
2167{
Francois Romieub6ffd972011-06-17 17:00:05 +02002168 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2169 release_firmware(tp->rtl_fw->fw);
2170 kfree(tp->rtl_fw);
2171 }
2172 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002173}
2174
François Romieu953a12c2011-04-24 17:38:48 +02002175static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002176{
Francois Romieub6ffd972011-06-17 17:00:05 +02002177 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002178
2179 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002180 if (!IS_ERR_OR_NULL(rtl_fw))
2181 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002182}
2183
2184static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2185{
2186 if (rtl_readphy(tp, reg) != val)
2187 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2188 else
2189 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002190}
2191
françois romieu4da19632011-01-03 15:07:55 +00002192static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002194 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002195 { 0x1f, 0x0001 },
2196 { 0x06, 0x006e },
2197 { 0x08, 0x0708 },
2198 { 0x15, 0x4000 },
2199 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
françois romieu0b9b5712009-08-10 19:44:56 +00002201 { 0x1f, 0x0001 },
2202 { 0x03, 0x00a1 },
2203 { 0x02, 0x0008 },
2204 { 0x01, 0x0120 },
2205 { 0x00, 0x1000 },
2206 { 0x04, 0x0800 },
2207 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
françois romieu0b9b5712009-08-10 19:44:56 +00002209 { 0x03, 0xff41 },
2210 { 0x02, 0xdf60 },
2211 { 0x01, 0x0140 },
2212 { 0x00, 0x0077 },
2213 { 0x04, 0x7800 },
2214 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
françois romieu0b9b5712009-08-10 19:44:56 +00002216 { 0x03, 0x802f },
2217 { 0x02, 0x4f02 },
2218 { 0x01, 0x0409 },
2219 { 0x00, 0xf0f9 },
2220 { 0x04, 0x9800 },
2221 { 0x04, 0x9000 },
2222
2223 { 0x03, 0xdf01 },
2224 { 0x02, 0xdf20 },
2225 { 0x01, 0xff95 },
2226 { 0x00, 0xba00 },
2227 { 0x04, 0xa800 },
2228 { 0x04, 0xa000 },
2229
2230 { 0x03, 0xff41 },
2231 { 0x02, 0xdf20 },
2232 { 0x01, 0x0140 },
2233 { 0x00, 0x00bb },
2234 { 0x04, 0xb800 },
2235 { 0x04, 0xb000 },
2236
2237 { 0x03, 0xdf41 },
2238 { 0x02, 0xdc60 },
2239 { 0x01, 0x6340 },
2240 { 0x00, 0x007d },
2241 { 0x04, 0xd800 },
2242 { 0x04, 0xd000 },
2243
2244 { 0x03, 0xdf01 },
2245 { 0x02, 0xdf20 },
2246 { 0x01, 0x100a },
2247 { 0x00, 0xa0ff },
2248 { 0x04, 0xf800 },
2249 { 0x04, 0xf000 },
2250
2251 { 0x1f, 0x0000 },
2252 { 0x0b, 0x0000 },
2253 { 0x00, 0x9200 }
2254 };
2255
françois romieu4da19632011-01-03 15:07:55 +00002256 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
françois romieu4da19632011-01-03 15:07:55 +00002259static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002260{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002261 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002262 { 0x1f, 0x0002 },
2263 { 0x01, 0x90d0 },
2264 { 0x1f, 0x0000 }
2265 };
2266
françois romieu4da19632011-01-03 15:07:55 +00002267 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002268}
2269
françois romieu4da19632011-01-03 15:07:55 +00002270static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002271{
2272 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002273
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002274 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2275 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002276 return;
2277
françois romieu4da19632011-01-03 15:07:55 +00002278 rtl_writephy(tp, 0x1f, 0x0001);
2279 rtl_writephy(tp, 0x10, 0xf01b);
2280 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002281}
2282
françois romieu4da19632011-01-03 15:07:55 +00002283static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002284{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002285 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002286 { 0x1f, 0x0001 },
2287 { 0x04, 0x0000 },
2288 { 0x03, 0x00a1 },
2289 { 0x02, 0x0008 },
2290 { 0x01, 0x0120 },
2291 { 0x00, 0x1000 },
2292 { 0x04, 0x0800 },
2293 { 0x04, 0x9000 },
2294 { 0x03, 0x802f },
2295 { 0x02, 0x4f02 },
2296 { 0x01, 0x0409 },
2297 { 0x00, 0xf099 },
2298 { 0x04, 0x9800 },
2299 { 0x04, 0xa000 },
2300 { 0x03, 0xdf01 },
2301 { 0x02, 0xdf20 },
2302 { 0x01, 0xff95 },
2303 { 0x00, 0xba00 },
2304 { 0x04, 0xa800 },
2305 { 0x04, 0xf000 },
2306 { 0x03, 0xdf01 },
2307 { 0x02, 0xdf20 },
2308 { 0x01, 0x101a },
2309 { 0x00, 0xa0ff },
2310 { 0x04, 0xf800 },
2311 { 0x04, 0x0000 },
2312 { 0x1f, 0x0000 },
2313
2314 { 0x1f, 0x0001 },
2315 { 0x10, 0xf41b },
2316 { 0x14, 0xfb54 },
2317 { 0x18, 0xf5c7 },
2318 { 0x1f, 0x0000 },
2319
2320 { 0x1f, 0x0001 },
2321 { 0x17, 0x0cc0 },
2322 { 0x1f, 0x0000 }
2323 };
2324
françois romieu4da19632011-01-03 15:07:55 +00002325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002326
françois romieu4da19632011-01-03 15:07:55 +00002327 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002328}
2329
françois romieu4da19632011-01-03 15:07:55 +00002330static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002331{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002332 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002333 { 0x1f, 0x0001 },
2334 { 0x04, 0x0000 },
2335 { 0x03, 0x00a1 },
2336 { 0x02, 0x0008 },
2337 { 0x01, 0x0120 },
2338 { 0x00, 0x1000 },
2339 { 0x04, 0x0800 },
2340 { 0x04, 0x9000 },
2341 { 0x03, 0x802f },
2342 { 0x02, 0x4f02 },
2343 { 0x01, 0x0409 },
2344 { 0x00, 0xf099 },
2345 { 0x04, 0x9800 },
2346 { 0x04, 0xa000 },
2347 { 0x03, 0xdf01 },
2348 { 0x02, 0xdf20 },
2349 { 0x01, 0xff95 },
2350 { 0x00, 0xba00 },
2351 { 0x04, 0xa800 },
2352 { 0x04, 0xf000 },
2353 { 0x03, 0xdf01 },
2354 { 0x02, 0xdf20 },
2355 { 0x01, 0x101a },
2356 { 0x00, 0xa0ff },
2357 { 0x04, 0xf800 },
2358 { 0x04, 0x0000 },
2359 { 0x1f, 0x0000 },
2360
2361 { 0x1f, 0x0001 },
2362 { 0x0b, 0x8480 },
2363 { 0x1f, 0x0000 },
2364
2365 { 0x1f, 0x0001 },
2366 { 0x18, 0x67c7 },
2367 { 0x04, 0x2000 },
2368 { 0x03, 0x002f },
2369 { 0x02, 0x4360 },
2370 { 0x01, 0x0109 },
2371 { 0x00, 0x3022 },
2372 { 0x04, 0x2800 },
2373 { 0x1f, 0x0000 },
2374
2375 { 0x1f, 0x0001 },
2376 { 0x17, 0x0cc0 },
2377 { 0x1f, 0x0000 }
2378 };
2379
françois romieu4da19632011-01-03 15:07:55 +00002380 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002381}
2382
françois romieu4da19632011-01-03 15:07:55 +00002383static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002384{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002385 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002386 { 0x10, 0xf41b },
2387 { 0x1f, 0x0000 }
2388 };
2389
françois romieu4da19632011-01-03 15:07:55 +00002390 rtl_writephy(tp, 0x1f, 0x0001);
2391 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002392
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 rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002397{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002398 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002399 { 0x1f, 0x0001 },
2400 { 0x10, 0xf41b },
2401 { 0x1f, 0x0000 }
2402 };
2403
françois romieu4da19632011-01-03 15:07:55 +00002404 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002405}
2406
françois romieu4da19632011-01-03 15:07:55 +00002407static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002408{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002409 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002410 { 0x1f, 0x0000 },
2411 { 0x1d, 0x0f00 },
2412 { 0x1f, 0x0002 },
2413 { 0x0c, 0x1ec8 },
2414 { 0x1f, 0x0000 }
2415 };
2416
françois romieu4da19632011-01-03 15:07:55 +00002417 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002418}
2419
françois romieu4da19632011-01-03 15:07:55 +00002420static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002421{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002422 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002423 { 0x1f, 0x0001 },
2424 { 0x1d, 0x3d98 },
2425 { 0x1f, 0x0000 }
2426 };
2427
françois romieu4da19632011-01-03 15:07:55 +00002428 rtl_writephy(tp, 0x1f, 0x0000);
2429 rtl_patchphy(tp, 0x14, 1 << 5);
2430 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002431
françois romieu4da19632011-01-03 15:07:55 +00002432 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002433}
2434
françois romieu4da19632011-01-03 15:07:55 +00002435static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002436{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002437 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002438 { 0x1f, 0x0001 },
2439 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002440 { 0x1f, 0x0002 },
2441 { 0x00, 0x88d4 },
2442 { 0x01, 0x82b1 },
2443 { 0x03, 0x7002 },
2444 { 0x08, 0x9e30 },
2445 { 0x09, 0x01f0 },
2446 { 0x0a, 0x5500 },
2447 { 0x0c, 0x00c8 },
2448 { 0x1f, 0x0003 },
2449 { 0x12, 0xc096 },
2450 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002451 { 0x1f, 0x0000 },
2452 { 0x1f, 0x0000 },
2453 { 0x09, 0x2000 },
2454 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002455 };
2456
françois romieu4da19632011-01-03 15:07:55 +00002457 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002458
françois romieu4da19632011-01-03 15:07:55 +00002459 rtl_patchphy(tp, 0x14, 1 << 5);
2460 rtl_patchphy(tp, 0x0d, 1 << 5);
2461 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002462}
2463
françois romieu4da19632011-01-03 15:07:55 +00002464static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002465{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002467 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002468 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002469 { 0x03, 0x802f },
2470 { 0x02, 0x4f02 },
2471 { 0x01, 0x0409 },
2472 { 0x00, 0xf099 },
2473 { 0x04, 0x9800 },
2474 { 0x04, 0x9000 },
2475 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002476 { 0x1f, 0x0002 },
2477 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002478 { 0x06, 0x0761 },
2479 { 0x1f, 0x0003 },
2480 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002481 { 0x1f, 0x0000 }
2482 };
2483
françois romieu4da19632011-01-03 15:07:55 +00002484 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002485
françois romieu4da19632011-01-03 15:07:55 +00002486 rtl_patchphy(tp, 0x16, 1 << 0);
2487 rtl_patchphy(tp, 0x14, 1 << 5);
2488 rtl_patchphy(tp, 0x0d, 1 << 5);
2489 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002490}
2491
françois romieu4da19632011-01-03 15:07:55 +00002492static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002493{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002494 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002495 { 0x1f, 0x0001 },
2496 { 0x12, 0x2300 },
2497 { 0x1d, 0x3d98 },
2498 { 0x1f, 0x0002 },
2499 { 0x0c, 0x7eb8 },
2500 { 0x06, 0x5461 },
2501 { 0x1f, 0x0003 },
2502 { 0x16, 0x0f0a },
2503 { 0x1f, 0x0000 }
2504 };
2505
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002507
françois romieu4da19632011-01-03 15:07:55 +00002508 rtl_patchphy(tp, 0x16, 1 << 0);
2509 rtl_patchphy(tp, 0x14, 1 << 5);
2510 rtl_patchphy(tp, 0x0d, 1 << 5);
2511 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002512}
2513
françois romieu4da19632011-01-03 15:07:55 +00002514static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002515{
françois romieu4da19632011-01-03 15:07:55 +00002516 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002517}
2518
françois romieubca03d52011-01-03 15:07:31 +00002519static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002520{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002521 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002522 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002523 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002524 { 0x06, 0x4064 },
2525 { 0x07, 0x2863 },
2526 { 0x08, 0x059c },
2527 { 0x09, 0x26b4 },
2528 { 0x0a, 0x6a19 },
2529 { 0x0b, 0xdcc8 },
2530 { 0x10, 0xf06d },
2531 { 0x14, 0x7f68 },
2532 { 0x18, 0x7fd9 },
2533 { 0x1c, 0xf0ff },
2534 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002535 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002536 { 0x12, 0xf49f },
2537 { 0x13, 0x070b },
2538 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002539 { 0x14, 0x94c0 },
2540
2541 /*
2542 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002543 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002544 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002545 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002546 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002547 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002548 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002549 { 0x06, 0x5561 },
2550
2551 /*
2552 * Can not link to 1Gbps with bad cable
2553 * Decrease SNR threshold form 21.07dB to 19.04dB
2554 */
2555 { 0x1f, 0x0001 },
2556 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002557
2558 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002559 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002560 };
françois romieubca03d52011-01-03 15:07:31 +00002561 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002562
françois romieu4da19632011-01-03 15:07:55 +00002563 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002564
françois romieubca03d52011-01-03 15:07:31 +00002565 /*
2566 * Rx Error Issue
2567 * Fine Tune Switching regulator parameter
2568 */
françois romieu4da19632011-01-03 15:07:55 +00002569 rtl_writephy(tp, 0x1f, 0x0002);
2570 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2571 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002572
françois romieudaf9df62009-10-07 12:44:20 +00002573 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002574 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002575 { 0x1f, 0x0002 },
2576 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002577 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002578 { 0x05, 0x8330 },
2579 { 0x06, 0x669a },
2580 { 0x1f, 0x0002 }
2581 };
2582 int val;
2583
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002585
françois romieu4da19632011-01-03 15:07:55 +00002586 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002587
2588 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002589 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002590 0x0065, 0x0066, 0x0067, 0x0068,
2591 0x0069, 0x006a, 0x006b, 0x006c
2592 };
2593 int i;
2594
françois romieu4da19632011-01-03 15:07:55 +00002595 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002596
2597 val &= 0xff00;
2598 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002599 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002600 }
2601 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002602 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002603 { 0x1f, 0x0002 },
2604 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002605 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002606 { 0x05, 0x8330 },
2607 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002608 };
2609
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002611 }
2612
françois romieubca03d52011-01-03 15:07:31 +00002613 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002614 rtl_writephy(tp, 0x1f, 0x0002);
2615 rtl_patchphy(tp, 0x0d, 0x0300);
2616 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002617
françois romieubca03d52011-01-03 15:07:31 +00002618 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002619 rtl_writephy(tp, 0x1f, 0x0002);
2620 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2621 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002622
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl_writephy(tp, 0x1f, 0x0005);
2624 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002625
2626 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002627
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002629}
2630
françois romieubca03d52011-01-03 15:07:31 +00002631static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002632{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002633 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002634 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002635 { 0x1f, 0x0001 },
2636 { 0x06, 0x4064 },
2637 { 0x07, 0x2863 },
2638 { 0x08, 0x059c },
2639 { 0x09, 0x26b4 },
2640 { 0x0a, 0x6a19 },
2641 { 0x0b, 0xdcc8 },
2642 { 0x10, 0xf06d },
2643 { 0x14, 0x7f68 },
2644 { 0x18, 0x7fd9 },
2645 { 0x1c, 0xf0ff },
2646 { 0x1d, 0x3d9c },
2647 { 0x1f, 0x0003 },
2648 { 0x12, 0xf49f },
2649 { 0x13, 0x070b },
2650 { 0x1a, 0x05ad },
2651 { 0x14, 0x94c0 },
2652
françois romieubca03d52011-01-03 15:07:31 +00002653 /*
2654 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002655 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002656 */
françois romieudaf9df62009-10-07 12:44:20 +00002657 { 0x1f, 0x0002 },
2658 { 0x06, 0x5561 },
2659 { 0x1f, 0x0005 },
2660 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002661 { 0x06, 0x5561 },
2662
2663 /*
2664 * Can not link to 1Gbps with bad cable
2665 * Decrease SNR threshold form 21.07dB to 19.04dB
2666 */
2667 { 0x1f, 0x0001 },
2668 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002669
2670 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002671 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002672 };
françois romieubca03d52011-01-03 15:07:31 +00002673 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002674
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002676
2677 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002678 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002679 { 0x1f, 0x0002 },
2680 { 0x05, 0x669a },
2681 { 0x1f, 0x0005 },
2682 { 0x05, 0x8330 },
2683 { 0x06, 0x669a },
2684
2685 { 0x1f, 0x0002 }
2686 };
2687 int val;
2688
françois romieu4da19632011-01-03 15:07:55 +00002689 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002690
françois romieu4da19632011-01-03 15:07:55 +00002691 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002692 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002693 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002694 0x0065, 0x0066, 0x0067, 0x0068,
2695 0x0069, 0x006a, 0x006b, 0x006c
2696 };
2697 int i;
2698
françois romieu4da19632011-01-03 15:07:55 +00002699 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002700
2701 val &= 0xff00;
2702 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002704 }
2705 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002706 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002707 { 0x1f, 0x0002 },
2708 { 0x05, 0x2642 },
2709 { 0x1f, 0x0005 },
2710 { 0x05, 0x8330 },
2711 { 0x06, 0x2642 }
2712 };
2713
françois romieu4da19632011-01-03 15:07:55 +00002714 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002715 }
2716
françois romieubca03d52011-01-03 15:07:31 +00002717 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy(tp, 0x1f, 0x0002);
2719 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2720 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002721
françois romieubca03d52011-01-03 15:07:31 +00002722 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002723 rtl_writephy(tp, 0x1f, 0x0002);
2724 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002725
françois romieu4da19632011-01-03 15:07:55 +00002726 rtl_writephy(tp, 0x1f, 0x0005);
2727 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002728
2729 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002730
françois romieu4da19632011-01-03 15:07:55 +00002731 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002732}
2733
françois romieu4da19632011-01-03 15:07:55 +00002734static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002735{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002736 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002737 { 0x1f, 0x0002 },
2738 { 0x10, 0x0008 },
2739 { 0x0d, 0x006c },
2740
2741 { 0x1f, 0x0000 },
2742 { 0x0d, 0xf880 },
2743
2744 { 0x1f, 0x0001 },
2745 { 0x17, 0x0cc0 },
2746
2747 { 0x1f, 0x0001 },
2748 { 0x0b, 0xa4d8 },
2749 { 0x09, 0x281c },
2750 { 0x07, 0x2883 },
2751 { 0x0a, 0x6b35 },
2752 { 0x1d, 0x3da4 },
2753 { 0x1c, 0xeffd },
2754 { 0x14, 0x7f52 },
2755 { 0x18, 0x7fc6 },
2756 { 0x08, 0x0601 },
2757 { 0x06, 0x4063 },
2758 { 0x10, 0xf074 },
2759 { 0x1f, 0x0003 },
2760 { 0x13, 0x0789 },
2761 { 0x12, 0xf4bd },
2762 { 0x1a, 0x04fd },
2763 { 0x14, 0x84b0 },
2764 { 0x1f, 0x0000 },
2765 { 0x00, 0x9200 },
2766
2767 { 0x1f, 0x0005 },
2768 { 0x01, 0x0340 },
2769 { 0x1f, 0x0001 },
2770 { 0x04, 0x4000 },
2771 { 0x03, 0x1d21 },
2772 { 0x02, 0x0c32 },
2773 { 0x01, 0x0200 },
2774 { 0x00, 0x5554 },
2775 { 0x04, 0x4800 },
2776 { 0x04, 0x4000 },
2777 { 0x04, 0xf000 },
2778 { 0x03, 0xdf01 },
2779 { 0x02, 0xdf20 },
2780 { 0x01, 0x101a },
2781 { 0x00, 0xa0ff },
2782 { 0x04, 0xf800 },
2783 { 0x04, 0xf000 },
2784 { 0x1f, 0x0000 },
2785
2786 { 0x1f, 0x0007 },
2787 { 0x1e, 0x0023 },
2788 { 0x16, 0x0000 },
2789 { 0x1f, 0x0000 }
2790 };
2791
françois romieu4da19632011-01-03 15:07:55 +00002792 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002793}
2794
françois romieue6de30d2011-01-03 15:08:37 +00002795static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2796{
2797 static const struct phy_reg phy_reg_init[] = {
2798 { 0x1f, 0x0001 },
2799 { 0x17, 0x0cc0 },
2800
2801 { 0x1f, 0x0007 },
2802 { 0x1e, 0x002d },
2803 { 0x18, 0x0040 },
2804 { 0x1f, 0x0000 }
2805 };
2806
2807 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2808 rtl_patchphy(tp, 0x0d, 1 << 5);
2809}
2810
Hayes Wang70090422011-07-06 15:58:06 +08002811static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002812{
2813 static const struct phy_reg phy_reg_init[] = {
2814 /* Enable Delay cap */
2815 { 0x1f, 0x0005 },
2816 { 0x05, 0x8b80 },
2817 { 0x06, 0xc896 },
2818 { 0x1f, 0x0000 },
2819
2820 /* Channel estimation fine tune */
2821 { 0x1f, 0x0001 },
2822 { 0x0b, 0x6c20 },
2823 { 0x07, 0x2872 },
2824 { 0x1c, 0xefff },
2825 { 0x1f, 0x0003 },
2826 { 0x14, 0x6420 },
2827 { 0x1f, 0x0000 },
2828
2829 /* Update PFM & 10M TX idle timer */
2830 { 0x1f, 0x0007 },
2831 { 0x1e, 0x002f },
2832 { 0x15, 0x1919 },
2833 { 0x1f, 0x0000 },
2834
2835 { 0x1f, 0x0007 },
2836 { 0x1e, 0x00ac },
2837 { 0x18, 0x0006 },
2838 { 0x1f, 0x0000 }
2839 };
2840
Francois Romieu15ecd032011-04-27 13:52:22 -07002841 rtl_apply_firmware(tp);
2842
hayeswang01dc7fe2011-03-21 01:50:28 +00002843 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2844
2845 /* DCO enable for 10M IDLE Power */
2846 rtl_writephy(tp, 0x1f, 0x0007);
2847 rtl_writephy(tp, 0x1e, 0x0023);
2848 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2849 rtl_writephy(tp, 0x1f, 0x0000);
2850
2851 /* For impedance matching */
2852 rtl_writephy(tp, 0x1f, 0x0002);
2853 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002854 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002855
2856 /* PHY auto speed down */
2857 rtl_writephy(tp, 0x1f, 0x0007);
2858 rtl_writephy(tp, 0x1e, 0x002d);
2859 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2860 rtl_writephy(tp, 0x1f, 0x0000);
2861 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2862
2863 rtl_writephy(tp, 0x1f, 0x0005);
2864 rtl_writephy(tp, 0x05, 0x8b86);
2865 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2866 rtl_writephy(tp, 0x1f, 0x0000);
2867
2868 rtl_writephy(tp, 0x1f, 0x0005);
2869 rtl_writephy(tp, 0x05, 0x8b85);
2870 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2871 rtl_writephy(tp, 0x1f, 0x0007);
2872 rtl_writephy(tp, 0x1e, 0x0020);
2873 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2874 rtl_writephy(tp, 0x1f, 0x0006);
2875 rtl_writephy(tp, 0x00, 0x5a00);
2876 rtl_writephy(tp, 0x1f, 0x0000);
2877 rtl_writephy(tp, 0x0d, 0x0007);
2878 rtl_writephy(tp, 0x0e, 0x003c);
2879 rtl_writephy(tp, 0x0d, 0x4007);
2880 rtl_writephy(tp, 0x0e, 0x0000);
2881 rtl_writephy(tp, 0x0d, 0x0000);
2882}
2883
Hayes Wang70090422011-07-06 15:58:06 +08002884static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2885{
2886 static const struct phy_reg phy_reg_init[] = {
2887 /* Enable Delay cap */
2888 { 0x1f, 0x0004 },
2889 { 0x1f, 0x0007 },
2890 { 0x1e, 0x00ac },
2891 { 0x18, 0x0006 },
2892 { 0x1f, 0x0002 },
2893 { 0x1f, 0x0000 },
2894 { 0x1f, 0x0000 },
2895
2896 /* Channel estimation fine tune */
2897 { 0x1f, 0x0003 },
2898 { 0x09, 0xa20f },
2899 { 0x1f, 0x0000 },
2900 { 0x1f, 0x0000 },
2901
2902 /* Green Setting */
2903 { 0x1f, 0x0005 },
2904 { 0x05, 0x8b5b },
2905 { 0x06, 0x9222 },
2906 { 0x05, 0x8b6d },
2907 { 0x06, 0x8000 },
2908 { 0x05, 0x8b76 },
2909 { 0x06, 0x8000 },
2910 { 0x1f, 0x0000 }
2911 };
2912
2913 rtl_apply_firmware(tp);
2914
2915 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2916
2917 /* For 4-corner performance improve */
2918 rtl_writephy(tp, 0x1f, 0x0005);
2919 rtl_writephy(tp, 0x05, 0x8b80);
2920 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2921 rtl_writephy(tp, 0x1f, 0x0000);
2922
2923 /* PHY auto speed down */
2924 rtl_writephy(tp, 0x1f, 0x0004);
2925 rtl_writephy(tp, 0x1f, 0x0007);
2926 rtl_writephy(tp, 0x1e, 0x002d);
2927 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2928 rtl_writephy(tp, 0x1f, 0x0002);
2929 rtl_writephy(tp, 0x1f, 0x0000);
2930 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2931
2932 /* improve 10M EEE waveform */
2933 rtl_writephy(tp, 0x1f, 0x0005);
2934 rtl_writephy(tp, 0x05, 0x8b86);
2935 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2936 rtl_writephy(tp, 0x1f, 0x0000);
2937
2938 /* Improve 2-pair detection performance */
2939 rtl_writephy(tp, 0x1f, 0x0005);
2940 rtl_writephy(tp, 0x05, 0x8b85);
2941 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2942 rtl_writephy(tp, 0x1f, 0x0000);
2943
2944 /* EEE setting */
2945 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2946 ERIAR_EXGMAC);
2947 rtl_writephy(tp, 0x1f, 0x0005);
2948 rtl_writephy(tp, 0x05, 0x8b85);
2949 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2950 rtl_writephy(tp, 0x1f, 0x0004);
2951 rtl_writephy(tp, 0x1f, 0x0007);
2952 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04002953 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08002954 rtl_writephy(tp, 0x1f, 0x0002);
2955 rtl_writephy(tp, 0x1f, 0x0000);
2956 rtl_writephy(tp, 0x0d, 0x0007);
2957 rtl_writephy(tp, 0x0e, 0x003c);
2958 rtl_writephy(tp, 0x0d, 0x4007);
2959 rtl_writephy(tp, 0x0e, 0x0000);
2960 rtl_writephy(tp, 0x0d, 0x0000);
2961
2962 /* Green feature */
2963 rtl_writephy(tp, 0x1f, 0x0003);
2964 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2965 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2966 rtl_writephy(tp, 0x1f, 0x0000);
2967}
2968
Hayes Wangc2218922011-09-06 16:55:18 +08002969static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2970{
2971 static const struct phy_reg phy_reg_init[] = {
2972 /* Channel estimation fine tune */
2973 { 0x1f, 0x0003 },
2974 { 0x09, 0xa20f },
2975 { 0x1f, 0x0000 },
2976
2977 /* Modify green table for giga & fnet */
2978 { 0x1f, 0x0005 },
2979 { 0x05, 0x8b55 },
2980 { 0x06, 0x0000 },
2981 { 0x05, 0x8b5e },
2982 { 0x06, 0x0000 },
2983 { 0x05, 0x8b67 },
2984 { 0x06, 0x0000 },
2985 { 0x05, 0x8b70 },
2986 { 0x06, 0x0000 },
2987 { 0x1f, 0x0000 },
2988 { 0x1f, 0x0007 },
2989 { 0x1e, 0x0078 },
2990 { 0x17, 0x0000 },
2991 { 0x19, 0x00fb },
2992 { 0x1f, 0x0000 },
2993
2994 /* Modify green table for 10M */
2995 { 0x1f, 0x0005 },
2996 { 0x05, 0x8b79 },
2997 { 0x06, 0xaa00 },
2998 { 0x1f, 0x0000 },
2999
3000 /* Disable hiimpedance detection (RTCT) */
3001 { 0x1f, 0x0003 },
3002 { 0x01, 0x328a },
3003 { 0x1f, 0x0000 }
3004 };
3005
3006 rtl_apply_firmware(tp);
3007
3008 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3009
3010 /* For 4-corner performance improve */
3011 rtl_writephy(tp, 0x1f, 0x0005);
3012 rtl_writephy(tp, 0x05, 0x8b80);
3013 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3014 rtl_writephy(tp, 0x1f, 0x0000);
3015
3016 /* PHY auto speed down */
3017 rtl_writephy(tp, 0x1f, 0x0007);
3018 rtl_writephy(tp, 0x1e, 0x002d);
3019 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3022
3023 /* Improve 10M EEE waveform */
3024 rtl_writephy(tp, 0x1f, 0x0005);
3025 rtl_writephy(tp, 0x05, 0x8b86);
3026 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3027 rtl_writephy(tp, 0x1f, 0x0000);
3028
3029 /* Improve 2-pair detection performance */
3030 rtl_writephy(tp, 0x1f, 0x0005);
3031 rtl_writephy(tp, 0x05, 0x8b85);
3032 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3033 rtl_writephy(tp, 0x1f, 0x0000);
3034}
3035
3036static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3037{
3038 rtl_apply_firmware(tp);
3039
3040 /* For 4-corner performance improve */
3041 rtl_writephy(tp, 0x1f, 0x0005);
3042 rtl_writephy(tp, 0x05, 0x8b80);
3043 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3044 rtl_writephy(tp, 0x1f, 0x0000);
3045
3046 /* PHY auto speed down */
3047 rtl_writephy(tp, 0x1f, 0x0007);
3048 rtl_writephy(tp, 0x1e, 0x002d);
3049 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3050 rtl_writephy(tp, 0x1f, 0x0000);
3051 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3052
3053 /* Improve 10M EEE waveform */
3054 rtl_writephy(tp, 0x1f, 0x0005);
3055 rtl_writephy(tp, 0x05, 0x8b86);
3056 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3057 rtl_writephy(tp, 0x1f, 0x0000);
3058}
3059
françois romieu4da19632011-01-03 15:07:55 +00003060static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003061{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003062 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003063 { 0x1f, 0x0003 },
3064 { 0x08, 0x441d },
3065 { 0x01, 0x9100 },
3066 { 0x1f, 0x0000 }
3067 };
3068
françois romieu4da19632011-01-03 15:07:55 +00003069 rtl_writephy(tp, 0x1f, 0x0000);
3070 rtl_patchphy(tp, 0x11, 1 << 12);
3071 rtl_patchphy(tp, 0x19, 1 << 13);
3072 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003073
françois romieu4da19632011-01-03 15:07:55 +00003074 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003075}
3076
Hayes Wang5a5e4442011-02-22 17:26:21 +08003077static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3078{
3079 static const struct phy_reg phy_reg_init[] = {
3080 { 0x1f, 0x0005 },
3081 { 0x1a, 0x0000 },
3082 { 0x1f, 0x0000 },
3083
3084 { 0x1f, 0x0004 },
3085 { 0x1c, 0x0000 },
3086 { 0x1f, 0x0000 },
3087
3088 { 0x1f, 0x0001 },
3089 { 0x15, 0x7701 },
3090 { 0x1f, 0x0000 }
3091 };
3092
3093 /* Disable ALDPS before ram code */
3094 rtl_writephy(tp, 0x1f, 0x0000);
3095 rtl_writephy(tp, 0x18, 0x0310);
3096 msleep(100);
3097
François Romieu953a12c2011-04-24 17:38:48 +02003098 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003099
3100 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3101}
3102
Francois Romieu5615d9f2007-08-17 17:50:46 +02003103static void rtl_hw_phy_config(struct net_device *dev)
3104{
3105 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003106
3107 rtl8169_print_mac_version(tp);
3108
3109 switch (tp->mac_version) {
3110 case RTL_GIGA_MAC_VER_01:
3111 break;
3112 case RTL_GIGA_MAC_VER_02:
3113 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003114 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003115 break;
3116 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003117 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003118 break;
françois romieu2e9558562009-08-10 19:44:19 +00003119 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003120 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003121 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003122 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003123 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003124 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003125 case RTL_GIGA_MAC_VER_07:
3126 case RTL_GIGA_MAC_VER_08:
3127 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003128 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003129 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003130 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003131 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003132 break;
3133 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003134 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003135 break;
3136 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003137 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003138 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003139 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003140 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003141 break;
3142 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003143 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003144 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003145 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003146 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003147 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003148 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003149 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003150 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003151 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003152 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003153 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003154 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003155 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003156 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003157 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003158 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003159 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003160 break;
3161 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003162 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003163 break;
3164 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003165 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003166 break;
françois romieue6de30d2011-01-03 15:08:37 +00003167 case RTL_GIGA_MAC_VER_28:
3168 rtl8168d_4_hw_phy_config(tp);
3169 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003170 case RTL_GIGA_MAC_VER_29:
3171 case RTL_GIGA_MAC_VER_30:
3172 rtl8105e_hw_phy_config(tp);
3173 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003174 case RTL_GIGA_MAC_VER_31:
3175 /* None. */
3176 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003177 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003178 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003179 rtl8168e_1_hw_phy_config(tp);
3180 break;
3181 case RTL_GIGA_MAC_VER_34:
3182 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003183 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003184 case RTL_GIGA_MAC_VER_35:
3185 rtl8168f_1_hw_phy_config(tp);
3186 break;
3187 case RTL_GIGA_MAC_VER_36:
3188 rtl8168f_2_hw_phy_config(tp);
3189 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003190
Francois Romieu5615d9f2007-08-17 17:50:46 +02003191 default:
3192 break;
3193 }
3194}
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196static void rtl8169_phy_timer(unsigned long __opaque)
3197{
3198 struct net_device *dev = (struct net_device *)__opaque;
3199 struct rtl8169_private *tp = netdev_priv(dev);
3200 struct timer_list *timer = &tp->timer;
3201 void __iomem *ioaddr = tp->mmio_addr;
3202 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3203
Francois Romieubcf0bf92006-07-26 23:14:13 +02003204 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 spin_lock_irq(&tp->lock);
3207
françois romieu4da19632011-01-03 15:07:55 +00003208 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 * A busy loop could burn quite a few cycles on nowadays CPU.
3211 * Let's delay the execution of the timer for a few ticks.
3212 */
3213 timeout = HZ/10;
3214 goto out_mod_timer;
3215 }
3216
3217 if (tp->link_ok(ioaddr))
3218 goto out_unlock;
3219
Joe Perchesbf82c182010-02-09 11:49:50 +00003220 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
françois romieu4da19632011-01-03 15:07:55 +00003222 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224out_mod_timer:
3225 mod_timer(timer, jiffies + timeout);
3226out_unlock:
3227 spin_unlock_irq(&tp->lock);
3228}
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230#ifdef CONFIG_NET_POLL_CONTROLLER
3231/*
3232 * Polling 'interrupt' - used by things like netconsole to send skbs
3233 * without having to re-enable interrupts. It's not called while
3234 * the interrupt routine is executing.
3235 */
3236static void rtl8169_netpoll(struct net_device *dev)
3237{
3238 struct rtl8169_private *tp = netdev_priv(dev);
3239 struct pci_dev *pdev = tp->pci_dev;
3240
3241 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003242 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 enable_irq(pdev->irq);
3244}
3245#endif
3246
3247static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3248 void __iomem *ioaddr)
3249{
3250 iounmap(ioaddr);
3251 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003252 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 pci_disable_device(pdev);
3254 free_netdev(dev);
3255}
3256
Francois Romieubf793292006-11-01 00:53:05 +01003257static void rtl8169_phy_reset(struct net_device *dev,
3258 struct rtl8169_private *tp)
3259{
Francois Romieu07d3f512007-02-21 22:40:46 +01003260 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003261
françois romieu4da19632011-01-03 15:07:55 +00003262 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003263 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003264 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003265 return;
3266 msleep(1);
3267 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003268 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003269}
3270
David S. Miller8decf862011-09-22 03:23:13 -04003271static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3272{
3273 void __iomem *ioaddr = tp->mmio_addr;
3274
3275 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3276 (RTL_R8(PHYstatus) & TBI_Enable);
3277}
3278
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003279static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003281 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003282
Francois Romieu5615d9f2007-08-17 17:50:46 +02003283 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003284
Marcus Sundberg773328942008-07-10 21:28:08 +02003285 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3286 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3287 RTL_W8(0x82, 0x01);
3288 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003289
Francois Romieu6dccd162007-02-13 23:38:05 +01003290 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3291
3292 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3293 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003294
Francois Romieubcf0bf92006-07-26 23:14:13 +02003295 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003296 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3297 RTL_W8(0x82, 0x01);
3298 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003299 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003300 }
3301
Francois Romieubf793292006-11-01 00:53:05 +01003302 rtl8169_phy_reset(dev, tp);
3303
Oliver Neukum54405cd2011-01-06 21:55:13 +01003304 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003305 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3306 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3307 (tp->mii.supports_gmii ?
3308 ADVERTISED_1000baseT_Half |
3309 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003310
David S. Miller8decf862011-09-22 03:23:13 -04003311 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003312 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003313}
3314
Francois Romieu773d2022007-01-31 23:47:43 +01003315static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3316{
3317 void __iomem *ioaddr = tp->mmio_addr;
3318 u32 high;
3319 u32 low;
3320
3321 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3322 high = addr[4] | (addr[5] << 8);
3323
3324 spin_lock_irq(&tp->lock);
3325
3326 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003327
Francois Romieu773d2022007-01-31 23:47:43 +01003328 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003329 RTL_R32(MAC4);
3330
Francois Romieu78f1cd02010-03-27 19:35:46 -07003331 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003332 RTL_R32(MAC0);
3333
françois romieuc28aa382011-08-02 03:53:43 +00003334 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3335 const struct exgmac_reg e[] = {
3336 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3337 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3338 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3339 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3340 low >> 16 },
3341 };
3342
3343 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3344 }
3345
Francois Romieu773d2022007-01-31 23:47:43 +01003346 RTL_W8(Cfg9346, Cfg9346_Lock);
3347
3348 spin_unlock_irq(&tp->lock);
3349}
3350
3351static int rtl_set_mac_address(struct net_device *dev, void *p)
3352{
3353 struct rtl8169_private *tp = netdev_priv(dev);
3354 struct sockaddr *addr = p;
3355
3356 if (!is_valid_ether_addr(addr->sa_data))
3357 return -EADDRNOTAVAIL;
3358
3359 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3360
3361 rtl_rar_set(tp, dev->dev_addr);
3362
3363 return 0;
3364}
3365
Francois Romieu5f787a12006-08-17 13:02:36 +02003366static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3367{
3368 struct rtl8169_private *tp = netdev_priv(dev);
3369 struct mii_ioctl_data *data = if_mii(ifr);
3370
Francois Romieu8b4ab282008-11-19 22:05:25 -08003371 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3372}
Francois Romieu5f787a12006-08-17 13:02:36 +02003373
Francois Romieucecb5fd2011-04-01 10:21:07 +02003374static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3375 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003376{
Francois Romieu5f787a12006-08-17 13:02:36 +02003377 switch (cmd) {
3378 case SIOCGMIIPHY:
3379 data->phy_id = 32; /* Internal PHY */
3380 return 0;
3381
3382 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003383 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003384 return 0;
3385
3386 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003387 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003388 return 0;
3389 }
3390 return -EOPNOTSUPP;
3391}
3392
Francois Romieu8b4ab282008-11-19 22:05:25 -08003393static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3394{
3395 return -EOPNOTSUPP;
3396}
3397
Francois Romieu0e485152007-02-20 00:00:26 +01003398static const struct rtl_cfg_info {
3399 void (*hw_start)(struct net_device *);
3400 unsigned int region;
3401 unsigned int align;
3402 u16 intr_event;
3403 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003404 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003405 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003406} rtl_cfg_infos [] = {
3407 [RTL_CFG_0] = {
3408 .hw_start = rtl_hw_start_8169,
3409 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003410 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003411 .intr_event = SYSErr | LinkChg | RxOverflow |
3412 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003413 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003414 .features = RTL_FEATURE_GMII,
3415 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003416 },
3417 [RTL_CFG_1] = {
3418 .hw_start = rtl_hw_start_8168,
3419 .region = 2,
3420 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003421 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003422 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003423 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003424 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3425 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003426 },
3427 [RTL_CFG_2] = {
3428 .hw_start = rtl_hw_start_8101,
3429 .region = 2,
3430 .align = 8,
3431 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3432 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003433 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003434 .features = RTL_FEATURE_MSI,
3435 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003436 }
3437};
3438
Francois Romieufbac58f2007-10-04 22:51:38 +02003439/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003440static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003441 const struct rtl_cfg_info *cfg)
3442{
françois romieu2ca6cf02011-12-15 08:37:43 +00003443 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003444 unsigned msi = 0;
3445 u8 cfg2;
3446
3447 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003448 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003449 if (pci_enable_msi(tp->pci_dev)) {
3450 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003451 } else {
3452 cfg2 |= MSIEnable;
3453 msi = RTL_FEATURE_MSI;
3454 }
3455 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003456 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3457 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003458 return msi;
3459}
3460
3461static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3462{
3463 if (tp->features & RTL_FEATURE_MSI) {
3464 pci_disable_msi(pdev);
3465 tp->features &= ~RTL_FEATURE_MSI;
3466 }
3467}
3468
Francois Romieu8b4ab282008-11-19 22:05:25 -08003469static const struct net_device_ops rtl8169_netdev_ops = {
3470 .ndo_open = rtl8169_open,
3471 .ndo_stop = rtl8169_close,
3472 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003473 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003474 .ndo_tx_timeout = rtl8169_tx_timeout,
3475 .ndo_validate_addr = eth_validate_addr,
3476 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003477 .ndo_fix_features = rtl8169_fix_features,
3478 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003479 .ndo_set_mac_address = rtl_set_mac_address,
3480 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003481 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003482#ifdef CONFIG_NET_POLL_CONTROLLER
3483 .ndo_poll_controller = rtl8169_netpoll,
3484#endif
3485
3486};
3487
françois romieuc0e45c12011-01-03 15:08:04 +00003488static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3489{
3490 struct mdio_ops *ops = &tp->mdio_ops;
3491
3492 switch (tp->mac_version) {
3493 case RTL_GIGA_MAC_VER_27:
3494 ops->write = r8168dp_1_mdio_write;
3495 ops->read = r8168dp_1_mdio_read;
3496 break;
françois romieue6de30d2011-01-03 15:08:37 +00003497 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003498 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003499 ops->write = r8168dp_2_mdio_write;
3500 ops->read = r8168dp_2_mdio_read;
3501 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003502 default:
3503 ops->write = r8169_mdio_write;
3504 ops->read = r8169_mdio_read;
3505 break;
3506 }
3507}
3508
David S. Miller1805b2f2011-10-24 18:18:09 -04003509static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3510{
3511 void __iomem *ioaddr = tp->mmio_addr;
3512
3513 switch (tp->mac_version) {
3514 case RTL_GIGA_MAC_VER_29:
3515 case RTL_GIGA_MAC_VER_30:
3516 case RTL_GIGA_MAC_VER_32:
3517 case RTL_GIGA_MAC_VER_33:
3518 case RTL_GIGA_MAC_VER_34:
3519 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3520 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3521 break;
3522 default:
3523 break;
3524 }
3525}
3526
3527static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3528{
3529 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3530 return false;
3531
3532 rtl_writephy(tp, 0x1f, 0x0000);
3533 rtl_writephy(tp, MII_BMCR, 0x0000);
3534
3535 rtl_wol_suspend_quirk(tp);
3536
3537 return true;
3538}
3539
françois romieu065c27c2011-01-03 15:08:12 +00003540static void r810x_phy_power_down(struct rtl8169_private *tp)
3541{
3542 rtl_writephy(tp, 0x1f, 0x0000);
3543 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3544}
3545
3546static void r810x_phy_power_up(struct rtl8169_private *tp)
3547{
3548 rtl_writephy(tp, 0x1f, 0x0000);
3549 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3550}
3551
3552static void r810x_pll_power_down(struct rtl8169_private *tp)
3553{
David S. Miller1805b2f2011-10-24 18:18:09 -04003554 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003555 return;
françois romieu065c27c2011-01-03 15:08:12 +00003556
3557 r810x_phy_power_down(tp);
3558}
3559
3560static void r810x_pll_power_up(struct rtl8169_private *tp)
3561{
3562 r810x_phy_power_up(tp);
3563}
3564
3565static void r8168_phy_power_up(struct rtl8169_private *tp)
3566{
3567 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003568 switch (tp->mac_version) {
3569 case RTL_GIGA_MAC_VER_11:
3570 case RTL_GIGA_MAC_VER_12:
3571 case RTL_GIGA_MAC_VER_17:
3572 case RTL_GIGA_MAC_VER_18:
3573 case RTL_GIGA_MAC_VER_19:
3574 case RTL_GIGA_MAC_VER_20:
3575 case RTL_GIGA_MAC_VER_21:
3576 case RTL_GIGA_MAC_VER_22:
3577 case RTL_GIGA_MAC_VER_23:
3578 case RTL_GIGA_MAC_VER_24:
3579 case RTL_GIGA_MAC_VER_25:
3580 case RTL_GIGA_MAC_VER_26:
3581 case RTL_GIGA_MAC_VER_27:
3582 case RTL_GIGA_MAC_VER_28:
3583 case RTL_GIGA_MAC_VER_31:
3584 rtl_writephy(tp, 0x0e, 0x0000);
3585 break;
3586 default:
3587 break;
3588 }
françois romieu065c27c2011-01-03 15:08:12 +00003589 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3590}
3591
3592static void r8168_phy_power_down(struct rtl8169_private *tp)
3593{
3594 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003595 switch (tp->mac_version) {
3596 case RTL_GIGA_MAC_VER_32:
3597 case RTL_GIGA_MAC_VER_33:
3598 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3599 break;
3600
3601 case RTL_GIGA_MAC_VER_11:
3602 case RTL_GIGA_MAC_VER_12:
3603 case RTL_GIGA_MAC_VER_17:
3604 case RTL_GIGA_MAC_VER_18:
3605 case RTL_GIGA_MAC_VER_19:
3606 case RTL_GIGA_MAC_VER_20:
3607 case RTL_GIGA_MAC_VER_21:
3608 case RTL_GIGA_MAC_VER_22:
3609 case RTL_GIGA_MAC_VER_23:
3610 case RTL_GIGA_MAC_VER_24:
3611 case RTL_GIGA_MAC_VER_25:
3612 case RTL_GIGA_MAC_VER_26:
3613 case RTL_GIGA_MAC_VER_27:
3614 case RTL_GIGA_MAC_VER_28:
3615 case RTL_GIGA_MAC_VER_31:
3616 rtl_writephy(tp, 0x0e, 0x0200);
3617 default:
3618 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3619 break;
3620 }
françois romieu065c27c2011-01-03 15:08:12 +00003621}
3622
3623static void r8168_pll_power_down(struct rtl8169_private *tp)
3624{
3625 void __iomem *ioaddr = tp->mmio_addr;
3626
Francois Romieucecb5fd2011-04-01 10:21:07 +02003627 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3628 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3629 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003630 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003631 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003632 }
françois romieu065c27c2011-01-03 15:08:12 +00003633
Francois Romieucecb5fd2011-04-01 10:21:07 +02003634 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3635 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003636 (RTL_R16(CPlusCmd) & ASF)) {
3637 return;
3638 }
3639
hayeswang01dc7fe2011-03-21 01:50:28 +00003640 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3641 tp->mac_version == RTL_GIGA_MAC_VER_33)
3642 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3643
David S. Miller1805b2f2011-10-24 18:18:09 -04003644 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003645 return;
françois romieu065c27c2011-01-03 15:08:12 +00003646
3647 r8168_phy_power_down(tp);
3648
3649 switch (tp->mac_version) {
3650 case RTL_GIGA_MAC_VER_25:
3651 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003652 case RTL_GIGA_MAC_VER_27:
3653 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003654 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003655 case RTL_GIGA_MAC_VER_32:
3656 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003657 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3658 break;
3659 }
3660}
3661
3662static void r8168_pll_power_up(struct rtl8169_private *tp)
3663{
3664 void __iomem *ioaddr = tp->mmio_addr;
3665
Francois Romieucecb5fd2011-04-01 10:21:07 +02003666 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3667 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3668 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003669 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003670 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003671 }
françois romieu065c27c2011-01-03 15:08:12 +00003672
3673 switch (tp->mac_version) {
3674 case RTL_GIGA_MAC_VER_25:
3675 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003676 case RTL_GIGA_MAC_VER_27:
3677 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003678 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003679 case RTL_GIGA_MAC_VER_32:
3680 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003681 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3682 break;
3683 }
3684
3685 r8168_phy_power_up(tp);
3686}
3687
Francois Romieud58d46b2011-05-03 16:38:29 +02003688static void rtl_generic_op(struct rtl8169_private *tp,
3689 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003690{
3691 if (op)
3692 op(tp);
3693}
3694
3695static void rtl_pll_power_down(struct rtl8169_private *tp)
3696{
Francois Romieud58d46b2011-05-03 16:38:29 +02003697 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003698}
3699
3700static void rtl_pll_power_up(struct rtl8169_private *tp)
3701{
Francois Romieud58d46b2011-05-03 16:38:29 +02003702 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003703}
3704
3705static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3706{
3707 struct pll_power_ops *ops = &tp->pll_power_ops;
3708
3709 switch (tp->mac_version) {
3710 case RTL_GIGA_MAC_VER_07:
3711 case RTL_GIGA_MAC_VER_08:
3712 case RTL_GIGA_MAC_VER_09:
3713 case RTL_GIGA_MAC_VER_10:
3714 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003715 case RTL_GIGA_MAC_VER_29:
3716 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003717 ops->down = r810x_pll_power_down;
3718 ops->up = r810x_pll_power_up;
3719 break;
3720
3721 case RTL_GIGA_MAC_VER_11:
3722 case RTL_GIGA_MAC_VER_12:
3723 case RTL_GIGA_MAC_VER_17:
3724 case RTL_GIGA_MAC_VER_18:
3725 case RTL_GIGA_MAC_VER_19:
3726 case RTL_GIGA_MAC_VER_20:
3727 case RTL_GIGA_MAC_VER_21:
3728 case RTL_GIGA_MAC_VER_22:
3729 case RTL_GIGA_MAC_VER_23:
3730 case RTL_GIGA_MAC_VER_24:
3731 case RTL_GIGA_MAC_VER_25:
3732 case RTL_GIGA_MAC_VER_26:
3733 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003734 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003735 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003736 case RTL_GIGA_MAC_VER_32:
3737 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003738 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003739 case RTL_GIGA_MAC_VER_35:
3740 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003741 ops->down = r8168_pll_power_down;
3742 ops->up = r8168_pll_power_up;
3743 break;
3744
3745 default:
3746 ops->down = NULL;
3747 ops->up = NULL;
3748 break;
3749 }
3750}
3751
Hayes Wange542a222011-07-06 15:58:04 +08003752static void rtl_init_rxcfg(struct rtl8169_private *tp)
3753{
3754 void __iomem *ioaddr = tp->mmio_addr;
3755
3756 switch (tp->mac_version) {
3757 case RTL_GIGA_MAC_VER_01:
3758 case RTL_GIGA_MAC_VER_02:
3759 case RTL_GIGA_MAC_VER_03:
3760 case RTL_GIGA_MAC_VER_04:
3761 case RTL_GIGA_MAC_VER_05:
3762 case RTL_GIGA_MAC_VER_06:
3763 case RTL_GIGA_MAC_VER_10:
3764 case RTL_GIGA_MAC_VER_11:
3765 case RTL_GIGA_MAC_VER_12:
3766 case RTL_GIGA_MAC_VER_13:
3767 case RTL_GIGA_MAC_VER_14:
3768 case RTL_GIGA_MAC_VER_15:
3769 case RTL_GIGA_MAC_VER_16:
3770 case RTL_GIGA_MAC_VER_17:
3771 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3772 break;
3773 case RTL_GIGA_MAC_VER_18:
3774 case RTL_GIGA_MAC_VER_19:
3775 case RTL_GIGA_MAC_VER_20:
3776 case RTL_GIGA_MAC_VER_21:
3777 case RTL_GIGA_MAC_VER_22:
3778 case RTL_GIGA_MAC_VER_23:
3779 case RTL_GIGA_MAC_VER_24:
3780 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3781 break;
3782 default:
3783 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3784 break;
3785 }
3786}
3787
Hayes Wang92fc43b2011-07-06 15:58:03 +08003788static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3789{
3790 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3791}
3792
Francois Romieud58d46b2011-05-03 16:38:29 +02003793static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3794{
3795 rtl_generic_op(tp, tp->jumbo_ops.enable);
3796}
3797
3798static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3799{
3800 rtl_generic_op(tp, tp->jumbo_ops.disable);
3801}
3802
3803static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3804{
3805 void __iomem *ioaddr = tp->mmio_addr;
3806
3807 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3808 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3809 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3810}
3811
3812static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3813{
3814 void __iomem *ioaddr = tp->mmio_addr;
3815
3816 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3817 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3818 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3819}
3820
3821static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3822{
3823 void __iomem *ioaddr = tp->mmio_addr;
3824
3825 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3826}
3827
3828static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3829{
3830 void __iomem *ioaddr = tp->mmio_addr;
3831
3832 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3833}
3834
3835static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3836{
3837 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003838
3839 RTL_W8(MaxTxPacketSize, 0x3f);
3840 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3841 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003842 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003843}
3844
3845static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3846{
3847 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003848
3849 RTL_W8(MaxTxPacketSize, 0x0c);
3850 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3851 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003852 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003853}
3854
3855static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3856{
3857 rtl_tx_performance_tweak(tp->pci_dev,
3858 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3859}
3860
3861static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3862{
3863 rtl_tx_performance_tweak(tp->pci_dev,
3864 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3865}
3866
3867static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3868{
3869 void __iomem *ioaddr = tp->mmio_addr;
3870
3871 r8168b_0_hw_jumbo_enable(tp);
3872
3873 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3874}
3875
3876static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3877{
3878 void __iomem *ioaddr = tp->mmio_addr;
3879
3880 r8168b_0_hw_jumbo_disable(tp);
3881
3882 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3883}
3884
3885static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3886{
3887 struct jumbo_ops *ops = &tp->jumbo_ops;
3888
3889 switch (tp->mac_version) {
3890 case RTL_GIGA_MAC_VER_11:
3891 ops->disable = r8168b_0_hw_jumbo_disable;
3892 ops->enable = r8168b_0_hw_jumbo_enable;
3893 break;
3894 case RTL_GIGA_MAC_VER_12:
3895 case RTL_GIGA_MAC_VER_17:
3896 ops->disable = r8168b_1_hw_jumbo_disable;
3897 ops->enable = r8168b_1_hw_jumbo_enable;
3898 break;
3899 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3900 case RTL_GIGA_MAC_VER_19:
3901 case RTL_GIGA_MAC_VER_20:
3902 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3903 case RTL_GIGA_MAC_VER_22:
3904 case RTL_GIGA_MAC_VER_23:
3905 case RTL_GIGA_MAC_VER_24:
3906 case RTL_GIGA_MAC_VER_25:
3907 case RTL_GIGA_MAC_VER_26:
3908 ops->disable = r8168c_hw_jumbo_disable;
3909 ops->enable = r8168c_hw_jumbo_enable;
3910 break;
3911 case RTL_GIGA_MAC_VER_27:
3912 case RTL_GIGA_MAC_VER_28:
3913 ops->disable = r8168dp_hw_jumbo_disable;
3914 ops->enable = r8168dp_hw_jumbo_enable;
3915 break;
3916 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3917 case RTL_GIGA_MAC_VER_32:
3918 case RTL_GIGA_MAC_VER_33:
3919 case RTL_GIGA_MAC_VER_34:
3920 ops->disable = r8168e_hw_jumbo_disable;
3921 ops->enable = r8168e_hw_jumbo_enable;
3922 break;
3923
3924 /*
3925 * No action needed for jumbo frames with 8169.
3926 * No jumbo for 810x at all.
3927 */
3928 default:
3929 ops->disable = NULL;
3930 ops->enable = NULL;
3931 break;
3932 }
3933}
3934
Francois Romieu6f43adc2011-04-29 15:05:51 +02003935static void rtl_hw_reset(struct rtl8169_private *tp)
3936{
3937 void __iomem *ioaddr = tp->mmio_addr;
3938 int i;
3939
3940 /* Soft reset the chip. */
3941 RTL_W8(ChipCmd, CmdReset);
3942
3943 /* Check that the chip has finished the reset. */
3944 for (i = 0; i < 100; i++) {
3945 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3946 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003947 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003948 }
3949}
3950
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003951static int __devinit
3952rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3953{
Francois Romieu0e485152007-02-20 00:00:26 +01003954 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3955 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003957 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003958 struct net_device *dev;
3959 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003960 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003961 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003963 if (netif_msg_drv(&debug)) {
3964 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3965 MODULENAME, RTL8169_VERSION);
3966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003969 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003970 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003971 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003972 rc = -ENOMEM;
3973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 }
3975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003977 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003979 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003980 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003981 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Francois Romieuccdffb92008-07-26 14:26:06 +02003983 mii = &tp->mii;
3984 mii->dev = dev;
3985 mii->mdio_read = rtl_mdio_read;
3986 mii->mdio_write = rtl_mdio_write;
3987 mii->phy_id_mask = 0x1f;
3988 mii->reg_num_mask = 0x1f;
3989 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3990
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003991 /* disable ASPM completely as that cause random device stop working
3992 * problems as well as full system hangs for some PCIe devices users */
3993 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3994 PCIE_LINK_STATE_CLKPM);
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3997 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003998 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003999 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004000 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002
françois romieu87aeec72010-04-26 11:42:06 +00004003 if (pci_set_mwi(pdev) < 0)
4004 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004007 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004008 netif_err(tp, probe, dev,
4009 "region #%d not an MMIO resource, aborting\n",
4010 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004012 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004016 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004017 netif_err(tp, probe, dev,
4018 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004020 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022
4023 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004024 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004025 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004026 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
4028
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004029 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004032 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 tp->cp_cmd |= PCIDAC;
4034 dev->features |= NETIF_F_HIGHDMA;
4035 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004036 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004038 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004039 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041 }
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004044 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004045 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004046 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004048 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004050 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Jon Masone44daad2011-06-27 07:46:31 +00004052 if (!pci_is_pcie(pdev))
4053 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004054
Hayes Wange542a222011-07-06 15:58:04 +08004055 /* Identify chip attached to board */
4056 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4057
4058 rtl_init_rxcfg(tp);
4059
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004060 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Francois Romieu6f43adc2011-04-29 15:05:51 +02004062 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004064 RTL_W16(IntrStatus, 0xffff);
4065
françois romieuca52efd2009-07-24 12:34:19 +00004066 pci_set_master(pdev);
4067
Francois Romieu7a8fc772011-03-01 17:18:33 +01004068 /*
4069 * Pretend we are using VLANs; This bypasses a nasty bug where
4070 * Interrupts stop flowing on high load on 8110SCd controllers.
4071 */
4072 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4073 tp->cp_cmd |= RxVlan;
4074
françois romieuc0e45c12011-01-03 15:08:04 +00004075 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004076 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004077 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Francois Romieu85bffe62011-04-27 08:22:39 +02004081 chipset = tp->mac_version;
4082 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Francois Romieu5d06a992006-02-23 00:47:58 +01004084 RTL_W8(Cfg9346, Cfg9346_Unlock);
4085 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4086 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004087 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4088 tp->features |= RTL_FEATURE_WOL;
4089 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4090 tp->features |= RTL_FEATURE_WOL;
françois romieu2ca6cf02011-12-15 08:37:43 +00004091 tp->features |= rtl_try_msi(tp, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004092 RTL_W8(Cfg9346, Cfg9346_Lock);
4093
David S. Miller8decf862011-09-22 03:23:13 -04004094 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 tp->set_speed = rtl8169_set_speed_tbi;
4096 tp->get_settings = rtl8169_gset_tbi;
4097 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4098 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4099 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004100 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 } else {
4102 tp->set_speed = rtl8169_set_speed_xmii;
4103 tp->get_settings = rtl8169_gset_xmii;
4104 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4105 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4106 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004107 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 }
4109
Francois Romieudf58ef512008-10-09 14:35:58 -07004110 spin_lock_init(&tp->lock);
4111
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004112 /* Get MAC address */
Joe Perches6a3c910c2011-11-16 09:38:02 +00004113 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004115 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4119 dev->irq = pdev->irq;
4120 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004122 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Michał Mirosław350fb322011-04-08 06:35:56 +00004124 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4125 * properly for all devices */
4126 dev->features |= NETIF_F_RXCSUM |
4127 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4128
4129 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4130 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4131 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4132 NETIF_F_HIGHDMA;
4133
4134 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4135 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4136 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
4138 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01004139 tp->hw_start = cfg->hw_start;
4140 tp->intr_event = cfg->intr_event;
4141 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
David S. Miller8decf862011-09-22 03:23:13 -04004143 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4144 ~(RxBOVF | RxFOVF) : ~0;
4145
Francois Romieu2efa53f2007-03-09 00:00:05 +01004146 init_timer(&tp->timer);
4147 tp->timer.data = (unsigned long) dev;
4148 tp->timer.function = rtl8169_phy_timer;
4149
Francois Romieub6ffd972011-06-17 17:00:05 +02004150 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004153 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004154 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
4156 pci_set_drvdata(pdev, dev);
4157
Joe Perchesbf82c182010-02-09 11:49:50 +00004158 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004159 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004160 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004161 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4162 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4163 "tx checksumming: %s]\n",
4164 rtl_chip_infos[chipset].jumbo_max,
4165 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Francois Romieucecb5fd2011-04-01 10:21:07 +02004168 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4169 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4170 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004171 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004172 }
françois romieub646d902011-01-03 15:08:21 +00004173
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004174 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Alan Sternf3ec4f82010-06-08 15:23:51 -04004176 if (pci_dev_run_wake(pdev))
4177 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004178
Ivan Vecera0d672e92011-02-15 02:08:39 +00004179 netif_carrier_off(dev);
4180
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004181out:
4182 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
françois romieu87aeec72010-04-26 11:42:06 +00004184err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004185 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004186 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004187err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004188 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004189err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004190 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004191 pci_disable_device(pdev);
4192err_out_free_dev_1:
4193 free_netdev(dev);
4194 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
Francois Romieu07d3f512007-02-21 22:40:46 +01004197static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 struct net_device *dev = pci_get_drvdata(pdev);
4200 struct rtl8169_private *tp = netdev_priv(dev);
4201
Francois Romieucecb5fd2011-04-01 10:21:07 +02004202 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4203 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4204 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004205 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004206 }
françois romieub646d902011-01-03 15:08:21 +00004207
Francois Romieu4422bcd2012-01-26 11:23:32 +01004208 cancel_work_sync(&tp->wk.work);
Francois Romieueb2a0212007-02-15 23:37:21 +01004209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004211
François Romieu953a12c2011-04-24 17:38:48 +02004212 rtl_release_firmware(tp);
4213
Alan Sternf3ec4f82010-06-08 15:23:51 -04004214 if (pci_dev_run_wake(pdev))
4215 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004216
Ivan Veceracc098dc2009-11-29 23:12:52 -08004217 /* restore original MAC address */
4218 rtl_rar_set(tp, dev->perm_addr);
4219
Francois Romieufbac58f2007-10-04 22:51:38 +02004220 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4222 pci_set_drvdata(pdev, NULL);
4223}
4224
Francois Romieub6ffd972011-06-17 17:00:05 +02004225static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4226{
4227 struct rtl_fw *rtl_fw;
4228 const char *name;
4229 int rc = -ENOMEM;
4230
4231 name = rtl_lookup_firmware_name(tp);
4232 if (!name)
4233 goto out_no_firmware;
4234
4235 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4236 if (!rtl_fw)
4237 goto err_warn;
4238
4239 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4240 if (rc < 0)
4241 goto err_free;
4242
Francois Romieufd112f22011-06-18 00:10:29 +02004243 rc = rtl_check_firmware(tp, rtl_fw);
4244 if (rc < 0)
4245 goto err_release_firmware;
4246
Francois Romieub6ffd972011-06-17 17:00:05 +02004247 tp->rtl_fw = rtl_fw;
4248out:
4249 return;
4250
Francois Romieufd112f22011-06-18 00:10:29 +02004251err_release_firmware:
4252 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004253err_free:
4254 kfree(rtl_fw);
4255err_warn:
4256 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4257 name, rc);
4258out_no_firmware:
4259 tp->rtl_fw = NULL;
4260 goto out;
4261}
4262
François Romieu953a12c2011-04-24 17:38:48 +02004263static void rtl_request_firmware(struct rtl8169_private *tp)
4264{
Francois Romieub6ffd972011-06-17 17:00:05 +02004265 if (IS_ERR(tp->rtl_fw))
4266 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004267}
4268
Francois Romieu4422bcd2012-01-26 11:23:32 +01004269static void rtl_task(struct work_struct *);
4270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271static int rtl8169_open(struct net_device *dev)
4272{
4273 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004274 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004276 int retval = -ENOMEM;
4277
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004278 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Neil Hormanc0cd8842010-03-29 13:16:02 -07004280 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004282 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004284 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4285 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004287 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004289 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4290 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004292 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294 retval = rtl8169_init_ring(dev);
4295 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004296 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Francois Romieu4422bcd2012-01-26 11:23:32 +01004298 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Francois Romieu99f252b2007-04-02 22:59:59 +02004300 smp_mb();
4301
François Romieu953a12c2011-04-24 17:38:48 +02004302 rtl_request_firmware(tp);
4303
Francois Romieufbac58f2007-10-04 22:51:38 +02004304 retval = request_irq(dev->irq, rtl8169_interrupt,
4305 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004306 dev->name, dev);
4307 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004308 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004309
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004310 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004311
françois romieueee3a962011-01-08 02:17:26 +00004312 rtl8169_init_phy(dev, tp);
4313
Michał Mirosław350fb322011-04-08 06:35:56 +00004314 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004315
françois romieu065c27c2011-01-03 15:08:12 +00004316 rtl_pll_power_up(tp);
4317
Francois Romieu07ce4062007-02-23 23:36:39 +01004318 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004320 tp->saved_wolopts = 0;
4321 pm_runtime_put_noidle(&pdev->dev);
4322
françois romieueee3a962011-01-08 02:17:26 +00004323 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324out:
4325 return retval;
4326
François Romieu953a12c2011-04-24 17:38:48 +02004327err_release_fw_2:
4328 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004329 rtl8169_rx_clear(tp);
4330err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004331 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4332 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004333 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004334err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004335 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4336 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004337 tp->TxDescArray = NULL;
4338err_pm_runtime_put:
4339 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 goto out;
4341}
4342
Hayes Wang92fc43b2011-07-06 15:58:03 +08004343static void rtl_rx_close(struct rtl8169_private *tp)
4344{
4345 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004346
Francois Romieu1687b562011-07-19 17:21:29 +02004347 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004348}
4349
françois romieue6de30d2011-01-03 15:08:37 +00004350static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
françois romieue6de30d2011-01-03 15:08:37 +00004352 void __iomem *ioaddr = tp->mmio_addr;
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004355 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Hayes Wang92fc43b2011-07-06 15:58:03 +08004357 rtl_rx_close(tp);
4358
Hayes Wang5d2e1952011-02-22 17:26:22 +08004359 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004360 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4361 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004362 while (RTL_R8(TxPoll) & NPQ)
4363 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004364 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4365 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4366 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004367 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004368 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4369 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004370 } else {
4371 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4372 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004373 }
4374
Hayes Wang92fc43b2011-07-06 15:58:03 +08004375 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376}
4377
Francois Romieu7f796d832007-06-11 23:04:41 +02004378static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004379{
4380 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004381
4382 /* Set DMA burst size and Interframe Gap Time */
4383 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4384 (InterFrameGap << TxInterFrameGapShift));
4385}
4386
Francois Romieu07ce4062007-02-23 23:36:39 +01004387static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
4389 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Francois Romieu07ce4062007-02-23 23:36:39 +01004391 tp->hw_start(dev);
4392
Francois Romieu3e990ff2012-01-26 12:50:01 +01004393 rtl_irq_enable(tp, tp->intr_event);
4394
Francois Romieu07ce4062007-02-23 23:36:39 +01004395 netif_start_queue(dev);
4396}
4397
Francois Romieu7f796d832007-06-11 23:04:41 +02004398static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4399 void __iomem *ioaddr)
4400{
4401 /*
4402 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4403 * register to be written before TxDescAddrLow to work.
4404 * Switching from MMIO to I/O access fixes the issue as well.
4405 */
4406 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004407 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004408 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004409 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004410}
4411
4412static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4413{
4414 u16 cmd;
4415
4416 cmd = RTL_R16(CPlusCmd);
4417 RTL_W16(CPlusCmd, cmd);
4418 return cmd;
4419}
4420
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004421static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004422{
4423 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004424 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004425}
4426
Francois Romieu6dccd162007-02-13 23:38:05 +01004427static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4428{
Francois Romieu37441002011-06-17 22:58:54 +02004429 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004430 u32 mac_version;
4431 u32 clk;
4432 u32 val;
4433 } cfg2_info [] = {
4434 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4435 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4436 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4437 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004438 };
4439 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004440 unsigned int i;
4441 u32 clk;
4442
4443 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004444 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004445 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4446 RTL_W32(0x7c, p->val);
4447 break;
4448 }
4449 }
4450}
4451
Francois Romieu07ce4062007-02-23 23:36:39 +01004452static void rtl_hw_start_8169(struct net_device *dev)
4453{
4454 struct rtl8169_private *tp = netdev_priv(dev);
4455 void __iomem *ioaddr = tp->mmio_addr;
4456 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004457
Francois Romieu9cb427b2006-11-02 00:10:16 +01004458 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4459 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4460 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4461 }
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004464 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4465 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4466 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4467 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004468 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4469
Hayes Wange542a222011-07-06 15:58:04 +08004470 rtl_init_rxcfg(tp);
4471
françois romieuf0298f82011-01-03 15:07:42 +00004472 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004474 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Francois Romieucecb5fd2011-04-01 10:21:07 +02004476 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4477 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4478 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4479 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004480 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Francois Romieu7f796d832007-06-11 23:04:41 +02004482 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004483
Francois Romieucecb5fd2011-04-01 10:21:07 +02004484 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4485 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004486 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004488 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 }
4490
Francois Romieubcf0bf92006-07-26 23:14:13 +02004491 RTL_W16(CPlusCmd, tp->cp_cmd);
4492
Francois Romieu6dccd162007-02-13 23:38:05 +01004493 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 /*
4496 * Undocumented corner. Supposedly:
4497 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4498 */
4499 RTL_W16(IntrMitigate, 0x0000);
4500
Francois Romieu7f796d832007-06-11 23:04:41 +02004501 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004502
Francois Romieucecb5fd2011-04-01 10:21:07 +02004503 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4504 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4505 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4506 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004507 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4508 rtl_set_rx_tx_config_registers(tp);
4509 }
4510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004512
4513 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4514 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
4516 RTL_W32(RxMissed, 0);
4517
Francois Romieu07ce4062007-02-23 23:36:39 +01004518 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 /* no early-rx interrupts */
4521 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
françois romieu650e8d52011-01-03 15:08:29 +00004524static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004525{
4526 u32 csi;
4527
4528 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004529 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4530}
4531
françois romieue6de30d2011-01-03 15:08:37 +00004532static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4533{
4534 rtl_csi_access_enable(ioaddr, 0x17000000);
4535}
4536
françois romieu650e8d52011-01-03 15:08:29 +00004537static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4538{
4539 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004540}
4541
4542struct ephy_info {
4543 unsigned int offset;
4544 u16 mask;
4545 u16 bits;
4546};
4547
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004548static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004549{
4550 u16 w;
4551
4552 while (len-- > 0) {
4553 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4554 rtl_ephy_write(ioaddr, e->offset, w);
4555 e++;
4556 }
4557}
4558
Francois Romieub726e492008-06-28 12:22:59 +02004559static void rtl_disable_clock_request(struct pci_dev *pdev)
4560{
Jon Masone44daad2011-06-27 07:46:31 +00004561 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004562
4563 if (cap) {
4564 u16 ctl;
4565
4566 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4567 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4568 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4569 }
4570}
4571
françois romieue6de30d2011-01-03 15:08:37 +00004572static void rtl_enable_clock_request(struct pci_dev *pdev)
4573{
Jon Masone44daad2011-06-27 07:46:31 +00004574 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004575
4576 if (cap) {
4577 u16 ctl;
4578
4579 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4580 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4581 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4582 }
4583}
4584
Francois Romieub726e492008-06-28 12:22:59 +02004585#define R8168_CPCMD_QUIRK_MASK (\
4586 EnableBist | \
4587 Mac_dbgo_oe | \
4588 Force_half_dup | \
4589 Force_rxflow_en | \
4590 Force_txflow_en | \
4591 Cxpl_dbg_sel | \
4592 ASF | \
4593 PktCntrDisable | \
4594 Mac_dbgo_sel)
4595
Francois Romieu219a1e92008-06-28 11:58:39 +02004596static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4597{
Francois Romieub726e492008-06-28 12:22:59 +02004598 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4599
4600 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4601
Francois Romieu2e68ae42008-06-28 12:00:55 +02004602 rtl_tx_performance_tweak(pdev,
4603 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004604}
4605
4606static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4607{
4608 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004609
françois romieuf0298f82011-01-03 15:07:42 +00004610 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004611
4612 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004613}
4614
4615static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4616{
Francois Romieub726e492008-06-28 12:22:59 +02004617 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4618
4619 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4620
Francois Romieu219a1e92008-06-28 11:58:39 +02004621 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004622
4623 rtl_disable_clock_request(pdev);
4624
4625 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004626}
4627
Francois Romieuef3386f2008-06-29 12:24:30 +02004628static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004629{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004630 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004631 { 0x01, 0, 0x0001 },
4632 { 0x02, 0x0800, 0x1000 },
4633 { 0x03, 0, 0x0042 },
4634 { 0x06, 0x0080, 0x0000 },
4635 { 0x07, 0, 0x2000 }
4636 };
4637
françois romieu650e8d52011-01-03 15:08:29 +00004638 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004639
4640 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4641
Francois Romieu219a1e92008-06-28 11:58:39 +02004642 __rtl_hw_start_8168cp(ioaddr, pdev);
4643}
4644
Francois Romieuef3386f2008-06-29 12:24:30 +02004645static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4646{
françois romieu650e8d52011-01-03 15:08:29 +00004647 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004648
4649 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4650
4651 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4652
4653 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4654}
4655
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004656static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4657{
françois romieu650e8d52011-01-03 15:08:29 +00004658 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004659
4660 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4661
4662 /* Magic. */
4663 RTL_W8(DBG_REG, 0x20);
4664
françois romieuf0298f82011-01-03 15:07:42 +00004665 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004666
4667 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4668
4669 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4670}
4671
Francois Romieu219a1e92008-06-28 11:58:39 +02004672static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4673{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004674 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004675 { 0x02, 0x0800, 0x1000 },
4676 { 0x03, 0, 0x0002 },
4677 { 0x06, 0x0080, 0x0000 }
4678 };
4679
françois romieu650e8d52011-01-03 15:08:29 +00004680 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004681
4682 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4683
4684 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4685
Francois Romieu219a1e92008-06-28 11:58:39 +02004686 __rtl_hw_start_8168cp(ioaddr, pdev);
4687}
4688
4689static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4690{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004691 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004692 { 0x01, 0, 0x0001 },
4693 { 0x03, 0x0400, 0x0220 }
4694 };
4695
françois romieu650e8d52011-01-03 15:08:29 +00004696 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004697
4698 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4699
Francois Romieu219a1e92008-06-28 11:58:39 +02004700 __rtl_hw_start_8168cp(ioaddr, pdev);
4701}
4702
Francois Romieu197ff762008-06-28 13:16:02 +02004703static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4704{
4705 rtl_hw_start_8168c_2(ioaddr, pdev);
4706}
4707
Francois Romieu6fb07052008-06-29 11:54:28 +02004708static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4709{
françois romieu650e8d52011-01-03 15:08:29 +00004710 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004711
4712 __rtl_hw_start_8168cp(ioaddr, pdev);
4713}
4714
Francois Romieu5b538df2008-07-20 16:22:45 +02004715static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4716{
françois romieu650e8d52011-01-03 15:08:29 +00004717 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004718
4719 rtl_disable_clock_request(pdev);
4720
françois romieuf0298f82011-01-03 15:07:42 +00004721 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004722
4723 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4724
4725 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4726}
4727
hayeswang4804b3b2011-03-21 01:50:29 +00004728static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4729{
4730 rtl_csi_access_enable_1(ioaddr);
4731
4732 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4733
4734 RTL_W8(MaxTxPacketSize, TxPacketMax);
4735
4736 rtl_disable_clock_request(pdev);
4737}
4738
françois romieue6de30d2011-01-03 15:08:37 +00004739static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4740{
4741 static const struct ephy_info e_info_8168d_4[] = {
4742 { 0x0b, ~0, 0x48 },
4743 { 0x19, 0x20, 0x50 },
4744 { 0x0c, ~0, 0x20 }
4745 };
4746 int i;
4747
4748 rtl_csi_access_enable_1(ioaddr);
4749
4750 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4751
4752 RTL_W8(MaxTxPacketSize, TxPacketMax);
4753
4754 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4755 const struct ephy_info *e = e_info_8168d_4 + i;
4756 u16 w;
4757
4758 w = rtl_ephy_read(ioaddr, e->offset);
4759 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4760 }
4761
4762 rtl_enable_clock_request(pdev);
4763}
4764
Hayes Wang70090422011-07-06 15:58:06 +08004765static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004766{
Hayes Wang70090422011-07-06 15:58:06 +08004767 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004768 { 0x00, 0x0200, 0x0100 },
4769 { 0x00, 0x0000, 0x0004 },
4770 { 0x06, 0x0002, 0x0001 },
4771 { 0x06, 0x0000, 0x0030 },
4772 { 0x07, 0x0000, 0x2000 },
4773 { 0x00, 0x0000, 0x0020 },
4774 { 0x03, 0x5800, 0x2000 },
4775 { 0x03, 0x0000, 0x0001 },
4776 { 0x01, 0x0800, 0x1000 },
4777 { 0x07, 0x0000, 0x4000 },
4778 { 0x1e, 0x0000, 0x2000 },
4779 { 0x19, 0xffff, 0xfe6c },
4780 { 0x0a, 0x0000, 0x0040 }
4781 };
4782
4783 rtl_csi_access_enable_2(ioaddr);
4784
Hayes Wang70090422011-07-06 15:58:06 +08004785 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004786
4787 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4788
4789 RTL_W8(MaxTxPacketSize, TxPacketMax);
4790
4791 rtl_disable_clock_request(pdev);
4792
4793 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004794 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4795 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004796
Francois Romieucecb5fd2011-04-01 10:21:07 +02004797 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004798}
4799
Hayes Wang70090422011-07-06 15:58:06 +08004800static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4801{
4802 static const struct ephy_info e_info_8168e_2[] = {
4803 { 0x09, 0x0000, 0x0080 },
4804 { 0x19, 0x0000, 0x0224 }
4805 };
4806
4807 rtl_csi_access_enable_1(ioaddr);
4808
4809 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4810
4811 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4812
4813 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4814 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4815 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4816 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4817 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4818 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4819 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4820 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4821 ERIAR_EXGMAC);
4822
Hayes Wang3090bd92011-09-06 16:55:15 +08004823 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004824
4825 rtl_disable_clock_request(pdev);
4826
4827 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4828 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4829
4830 /* Adjust EEE LED frequency */
4831 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4832
4833 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4834 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4835 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4836}
4837
Hayes Wangc2218922011-09-06 16:55:18 +08004838static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4839{
4840 static const struct ephy_info e_info_8168f_1[] = {
4841 { 0x06, 0x00c0, 0x0020 },
4842 { 0x08, 0x0001, 0x0002 },
4843 { 0x09, 0x0000, 0x0080 },
4844 { 0x19, 0x0000, 0x0224 }
4845 };
4846
4847 rtl_csi_access_enable_1(ioaddr);
4848
4849 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4850
4851 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4852
4853 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4854 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4855 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4856 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4857 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4858 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4859 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4860 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4861 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4862 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4863 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4864 ERIAR_EXGMAC);
4865
4866 RTL_W8(MaxTxPacketSize, EarlySize);
4867
4868 rtl_disable_clock_request(pdev);
4869
4870 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4871 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4872
4873 /* Adjust EEE LED frequency */
4874 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4875
4876 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4877 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4878 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4879}
4880
Francois Romieu07ce4062007-02-23 23:36:39 +01004881static void rtl_hw_start_8168(struct net_device *dev)
4882{
Francois Romieu2dd99532007-06-11 23:22:52 +02004883 struct rtl8169_private *tp = netdev_priv(dev);
4884 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004885 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004886
4887 RTL_W8(Cfg9346, Cfg9346_Unlock);
4888
françois romieuf0298f82011-01-03 15:07:42 +00004889 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004890
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004891 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004892
Francois Romieu0e485152007-02-20 00:00:26 +01004893 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004894
4895 RTL_W16(CPlusCmd, tp->cp_cmd);
4896
Francois Romieu0e485152007-02-20 00:00:26 +01004897 RTL_W16(IntrMitigate, 0x5151);
4898
4899 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004900 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieu0e485152007-02-20 00:00:26 +01004901 tp->intr_event |= RxFIFOOver | PCSTimeout;
4902 tp->intr_event &= ~RxOverflow;
4903 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004904
4905 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4906
Francois Romieub8363902008-06-01 12:31:57 +02004907 rtl_set_rx_mode(dev);
4908
4909 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4910 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004911
4912 RTL_R8(IntrMask);
4913
Francois Romieu219a1e92008-06-28 11:58:39 +02004914 switch (tp->mac_version) {
4915 case RTL_GIGA_MAC_VER_11:
4916 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004917 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004918
4919 case RTL_GIGA_MAC_VER_12:
4920 case RTL_GIGA_MAC_VER_17:
4921 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004922 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004923
4924 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004925 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004926 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004927
4928 case RTL_GIGA_MAC_VER_19:
4929 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004930 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004931
4932 case RTL_GIGA_MAC_VER_20:
4933 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004934 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004935
Francois Romieu197ff762008-06-28 13:16:02 +02004936 case RTL_GIGA_MAC_VER_21:
4937 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004938 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004939
Francois Romieu6fb07052008-06-29 11:54:28 +02004940 case RTL_GIGA_MAC_VER_22:
4941 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004942 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004943
Francois Romieuef3386f2008-06-29 12:24:30 +02004944 case RTL_GIGA_MAC_VER_23:
4945 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004946 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004947
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004948 case RTL_GIGA_MAC_VER_24:
4949 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004950 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004951
Francois Romieu5b538df2008-07-20 16:22:45 +02004952 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004953 case RTL_GIGA_MAC_VER_26:
4954 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004955 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004956 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004957
françois romieue6de30d2011-01-03 15:08:37 +00004958 case RTL_GIGA_MAC_VER_28:
4959 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004960 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004961
hayeswang4804b3b2011-03-21 01:50:29 +00004962 case RTL_GIGA_MAC_VER_31:
4963 rtl_hw_start_8168dp(ioaddr, pdev);
4964 break;
4965
hayeswang01dc7fe2011-03-21 01:50:28 +00004966 case RTL_GIGA_MAC_VER_32:
4967 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004968 rtl_hw_start_8168e_1(ioaddr, pdev);
4969 break;
4970 case RTL_GIGA_MAC_VER_34:
4971 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004972 break;
françois romieue6de30d2011-01-03 15:08:37 +00004973
Hayes Wangc2218922011-09-06 16:55:18 +08004974 case RTL_GIGA_MAC_VER_35:
4975 case RTL_GIGA_MAC_VER_36:
4976 rtl_hw_start_8168f_1(ioaddr, pdev);
4977 break;
4978
Francois Romieu219a1e92008-06-28 11:58:39 +02004979 default:
4980 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4981 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004982 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004983 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004984
Francois Romieu0e485152007-02-20 00:00:26 +01004985 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4986
Francois Romieub8363902008-06-01 12:31:57 +02004987 RTL_W8(Cfg9346, Cfg9346_Lock);
4988
Francois Romieu2dd99532007-06-11 23:22:52 +02004989 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu07ce4062007-02-23 23:36:39 +01004990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Francois Romieu2857ffb2008-08-02 21:08:49 +02004992#define R810X_CPCMD_QUIRK_MASK (\
4993 EnableBist | \
4994 Mac_dbgo_oe | \
4995 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004996 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004997 Force_txflow_en | \
4998 Cxpl_dbg_sel | \
4999 ASF | \
5000 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005001 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005002
5003static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5004{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005005 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005006 { 0x01, 0, 0x6e65 },
5007 { 0x02, 0, 0x091f },
5008 { 0x03, 0, 0xc2f9 },
5009 { 0x06, 0, 0xafb5 },
5010 { 0x07, 0, 0x0e00 },
5011 { 0x19, 0, 0xec80 },
5012 { 0x01, 0, 0x2e65 },
5013 { 0x01, 0, 0x6e65 }
5014 };
5015 u8 cfg1;
5016
françois romieu650e8d52011-01-03 15:08:29 +00005017 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005018
5019 RTL_W8(DBG_REG, FIX_NAK_1);
5020
5021 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5022
5023 RTL_W8(Config1,
5024 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5025 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5026
5027 cfg1 = RTL_R8(Config1);
5028 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5029 RTL_W8(Config1, cfg1 & ~LEDS0);
5030
Francois Romieu2857ffb2008-08-02 21:08:49 +02005031 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5032}
5033
5034static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5035{
françois romieu650e8d52011-01-03 15:08:29 +00005036 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005037
5038 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5039
5040 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5041 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005042}
5043
5044static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5045{
5046 rtl_hw_start_8102e_2(ioaddr, pdev);
5047
5048 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5049}
5050
Hayes Wang5a5e4442011-02-22 17:26:21 +08005051static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5052{
5053 static const struct ephy_info e_info_8105e_1[] = {
5054 { 0x07, 0, 0x4000 },
5055 { 0x19, 0, 0x0200 },
5056 { 0x19, 0, 0x0020 },
5057 { 0x1e, 0, 0x2000 },
5058 { 0x03, 0, 0x0001 },
5059 { 0x19, 0, 0x0100 },
5060 { 0x19, 0, 0x0004 },
5061 { 0x0a, 0, 0x0020 }
5062 };
5063
Francois Romieucecb5fd2011-04-01 10:21:07 +02005064 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005065 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5066
Francois Romieucecb5fd2011-04-01 10:21:07 +02005067 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005068 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5069
5070 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005071 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005072
5073 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5074}
5075
5076static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5077{
5078 rtl_hw_start_8105e_1(ioaddr, pdev);
5079 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5080}
5081
Francois Romieu07ce4062007-02-23 23:36:39 +01005082static void rtl_hw_start_8101(struct net_device *dev)
5083{
Francois Romieucdf1a602007-06-11 23:29:50 +02005084 struct rtl8169_private *tp = netdev_priv(dev);
5085 void __iomem *ioaddr = tp->mmio_addr;
5086 struct pci_dev *pdev = tp->pci_dev;
5087
françois romieu811fd302011-12-04 20:30:45 +00005088 if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
5089 tp->intr_event &= ~RxFIFOOver;
5090 tp->napi_event &= ~RxFIFOOver;
5091 }
5092
Francois Romieucecb5fd2011-04-01 10:21:07 +02005093 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5094 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005095 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005096
5097 if (cap) {
5098 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5099 PCI_EXP_DEVCTL_NOSNOOP_EN);
5100 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005101 }
5102
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005103 RTL_W8(Cfg9346, Cfg9346_Unlock);
5104
Francois Romieu2857ffb2008-08-02 21:08:49 +02005105 switch (tp->mac_version) {
5106 case RTL_GIGA_MAC_VER_07:
5107 rtl_hw_start_8102e_1(ioaddr, pdev);
5108 break;
5109
5110 case RTL_GIGA_MAC_VER_08:
5111 rtl_hw_start_8102e_3(ioaddr, pdev);
5112 break;
5113
5114 case RTL_GIGA_MAC_VER_09:
5115 rtl_hw_start_8102e_2(ioaddr, pdev);
5116 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005117
5118 case RTL_GIGA_MAC_VER_29:
5119 rtl_hw_start_8105e_1(ioaddr, pdev);
5120 break;
5121 case RTL_GIGA_MAC_VER_30:
5122 rtl_hw_start_8105e_2(ioaddr, pdev);
5123 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005124 }
5125
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005126 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005127
françois romieuf0298f82011-01-03 15:07:42 +00005128 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005129
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005130 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005131
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005132 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005133 RTL_W16(CPlusCmd, tp->cp_cmd);
5134
5135 RTL_W16(IntrMitigate, 0x0000);
5136
5137 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5138
5139 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5140 rtl_set_rx_tx_config_registers(tp);
5141
Francois Romieucdf1a602007-06-11 23:29:50 +02005142 RTL_R8(IntrMask);
5143
Francois Romieucdf1a602007-06-11 23:29:50 +02005144 rtl_set_rx_mode(dev);
5145
5146 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147}
5148
5149static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5150{
Francois Romieud58d46b2011-05-03 16:38:29 +02005151 struct rtl8169_private *tp = netdev_priv(dev);
5152
5153 if (new_mtu < ETH_ZLEN ||
5154 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return -EINVAL;
5156
Francois Romieud58d46b2011-05-03 16:38:29 +02005157 if (new_mtu > ETH_DATA_LEN)
5158 rtl_hw_jumbo_enable(tp);
5159 else
5160 rtl_hw_jumbo_disable(tp);
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005163 netdev_update_features(dev);
5164
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166}
5167
5168static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5169{
Al Viro95e09182007-12-22 18:55:39 +00005170 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5172}
5173
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005174static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5175 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005177 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005178 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005179
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005180 kfree(*data_buff);
5181 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 rtl8169_make_unusable_by_asic(desc);
5183}
5184
5185static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5186{
5187 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5188
5189 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5190}
5191
5192static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5193 u32 rx_buf_sz)
5194{
5195 desc->addr = cpu_to_le64(mapping);
5196 wmb();
5197 rtl8169_mark_to_asic(desc, rx_buf_sz);
5198}
5199
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005200static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005202 return (void *)ALIGN((long)data, 16);
5203}
5204
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005205static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5206 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005207{
5208 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005210 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005211 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005212 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005214 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5215 if (!data)
5216 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005217
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005218 if (rtl8169_align(data) != data) {
5219 kfree(data);
5220 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5221 if (!data)
5222 return NULL;
5223 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005224
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005225 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005226 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005227 if (unlikely(dma_mapping_error(d, mapping))) {
5228 if (net_ratelimit())
5229 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005230 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
5233 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005234 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005235
5236err_out:
5237 kfree(data);
5238 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
5241static void rtl8169_rx_clear(struct rtl8169_private *tp)
5242{
Francois Romieu07d3f512007-02-21 22:40:46 +01005243 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
5245 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005246 if (tp->Rx_databuff[i]) {
5247 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 tp->RxDescArray + i);
5249 }
5250 }
5251}
5252
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005253static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005255 desc->opts1 |= cpu_to_le32(RingEnd);
5256}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005257
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005258static int rtl8169_rx_fill(struct rtl8169_private *tp)
5259{
5260 unsigned int i;
5261
5262 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005263 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005264
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005265 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005267
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005268 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005269 if (!data) {
5270 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005271 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005272 }
5273 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005276 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5277 return 0;
5278
5279err_out:
5280 rtl8169_rx_clear(tp);
5281 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}
5283
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284static int rtl8169_init_ring(struct net_device *dev)
5285{
5286 struct rtl8169_private *tp = netdev_priv(dev);
5287
5288 rtl8169_init_ring_indexes(tp);
5289
5290 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005291 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005293 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294}
5295
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005296static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 struct TxDesc *desc)
5298{
5299 unsigned int len = tx_skb->len;
5300
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005301 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 desc->opts1 = 0x00;
5304 desc->opts2 = 0x00;
5305 desc->addr = 0x00;
5306 tx_skb->len = 0;
5307}
5308
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005309static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5310 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311{
5312 unsigned int i;
5313
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005314 for (i = 0; i < n; i++) {
5315 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 struct ring_info *tx_skb = tp->tx_skb + entry;
5317 unsigned int len = tx_skb->len;
5318
5319 if (len) {
5320 struct sk_buff *skb = tx_skb->skb;
5321
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005322 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 tp->TxDescArray + entry);
5324 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005325 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 dev_kfree_skb(skb);
5327 tx_skb->skb = NULL;
5328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 }
5330 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005331}
5332
5333static void rtl8169_tx_clear(struct rtl8169_private *tp)
5334{
5335 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 tp->cur_tx = tp->dirty_tx = 0;
5337}
5338
Francois Romieu4422bcd2012-01-26 11:23:32 +01005339static void rtl8169_schedule_work(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
5341 struct rtl8169_private *tp = netdev_priv(dev);
5342
Francois Romieu4422bcd2012-01-26 11:23:32 +01005343 schedule_work(&tp->wk.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344}
5345
5346static void rtl8169_wait_for_quiescence(struct net_device *dev)
5347{
5348 struct rtl8169_private *tp = netdev_priv(dev);
5349 void __iomem *ioaddr = tp->mmio_addr;
5350
5351 synchronize_irq(dev->irq);
5352
5353 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005354 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
françois romieu811fd302011-12-04 20:30:45 +00005356 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
David S. Millerd1d08d12008-01-07 20:53:33 -08005358 tp->intr_mask = 0xffff;
5359 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005360 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361}
5362
Francois Romieu4422bcd2012-01-26 11:23:32 +01005363static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
David Howellsc4028952006-11-22 14:57:56 +00005365 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005366 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Francois Romieueb2a0212007-02-15 23:37:21 +01005368 rtnl_lock();
5369
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005371 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
françois romieuc7c2c392011-12-04 20:30:52 +00005373 rtl8169_hw_reset(tp);
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 rtl8169_wait_for_quiescence(dev);
5376
Francois Romieu56de4142011-03-15 17:29:31 +01005377 for (i = 0; i < NUM_RX_DESC; i++)
5378 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005381 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Francois Romieu56de4142011-03-15 17:29:31 +01005383 rtl_hw_start(dev);
5384 netif_wake_queue(dev);
5385 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005386
5387out_unlock:
5388 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
5391static void rtl8169_tx_timeout(struct net_device *dev)
5392{
Francois Romieu4422bcd2012-01-26 11:23:32 +01005393 rtl8169_schedule_work(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394}
5395
5396static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005397 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398{
5399 struct skb_shared_info *info = skb_shinfo(skb);
5400 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005401 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005402 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
5404 entry = tp->cur_tx;
5405 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005406 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 dma_addr_t mapping;
5408 u32 status, len;
5409 void *addr;
5410
5411 entry = (entry + 1) % NUM_TX_DESC;
5412
5413 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005414 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005415 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005416 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005417 if (unlikely(dma_mapping_error(d, mapping))) {
5418 if (net_ratelimit())
5419 netif_err(tp, drv, tp->dev,
5420 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005421 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
Francois Romieucecb5fd2011-04-01 10:21:07 +02005424 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005425 status = opts[0] | len |
5426 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005429 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 txd->addr = cpu_to_le64(mapping);
5431
5432 tp->tx_skb[entry].len = len;
5433 }
5434
5435 if (cur_frag) {
5436 tp->tx_skb[entry].skb = skb;
5437 txd->opts1 |= cpu_to_le32(LastFrag);
5438 }
5439
5440 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005441
5442err_out:
5443 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5444 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
5446
Francois Romieu2b7b4312011-04-18 22:53:24 -07005447static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5448 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005450 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005451 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005452 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Francois Romieu2b7b4312011-04-18 22:53:24 -07005454 if (mss) {
5455 opts[0] |= TD_LSO;
5456 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5457 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005458 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
5460 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005461 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005463 opts[offset] |= info->checksum.udp;
5464 else
5465 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467}
5468
Stephen Hemminger613573252009-08-31 19:50:58 +00005469static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5470 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471{
5472 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005473 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 struct TxDesc *txd = tp->TxDescArray + entry;
5475 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005476 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 dma_addr_t mapping;
5478 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005479 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005480 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005483 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005484 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 }
5486
5487 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005488 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005490 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005491 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005492 if (unlikely(dma_mapping_error(d, mapping))) {
5493 if (net_ratelimit())
5494 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005495 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
5498 tp->tx_skb[entry].len = len;
5499 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Francois Romieu2b7b4312011-04-18 22:53:24 -07005501 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5502 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005503
Francois Romieu2b7b4312011-04-18 22:53:24 -07005504 rtl8169_tso_csum(tp, skb, opts);
5505
5506 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005507 if (frags < 0)
5508 goto err_dma_1;
5509 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005510 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005511 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005512 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005513 tp->tx_skb[entry].skb = skb;
5514 }
5515
Francois Romieu2b7b4312011-04-18 22:53:24 -07005516 txd->opts2 = cpu_to_le32(opts[1]);
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 wmb();
5519
Francois Romieucecb5fd2011-04-01 10:21:07 +02005520 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005521 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 txd->opts1 = cpu_to_le32(status);
5523
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 tp->cur_tx += frags + 1;
5525
David Dillow4c020a92010-03-03 16:33:10 +00005526 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Francois Romieucecb5fd2011-04-01 10:21:07 +02005528 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5531 netif_stop_queue(dev);
5532 smp_rmb();
5533 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5534 netif_wake_queue(dev);
5535 }
5536
Stephen Hemminger613573252009-08-31 19:50:58 +00005537 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005539err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005540 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005541err_dma_0:
5542 dev_kfree_skb(skb);
5543 dev->stats.tx_dropped++;
5544 return NETDEV_TX_OK;
5545
5546err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005548 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005549 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550}
5551
5552static void rtl8169_pcierr_interrupt(struct net_device *dev)
5553{
5554 struct rtl8169_private *tp = netdev_priv(dev);
5555 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 u16 pci_status, pci_cmd;
5557
5558 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5559 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5560
Joe Perchesbf82c182010-02-09 11:49:50 +00005561 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5562 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
5564 /*
5565 * The recovery sequence below admits a very elaborated explanation:
5566 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005567 * - I did not see what else could be done;
5568 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 *
5570 * Feel free to adjust to your needs.
5571 */
Francois Romieua27993f2006-12-18 00:04:19 +01005572 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005573 pci_cmd &= ~PCI_COMMAND_PARITY;
5574 else
5575 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5576
5577 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579 pci_write_config_word(pdev, PCI_STATUS,
5580 pci_status & (PCI_STATUS_DETECTED_PARITY |
5581 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5582 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5583
5584 /* The infamous DAC f*ckup only happens at boot time */
5585 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005586 void __iomem *ioaddr = tp->mmio_addr;
5587
Joe Perchesbf82c182010-02-09 11:49:50 +00005588 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 tp->cp_cmd &= ~PCIDAC;
5590 RTL_W16(CPlusCmd, tp->cp_cmd);
5591 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 }
5593
françois romieue6de30d2011-01-03 15:08:37 +00005594 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005595
Francois Romieu4422bcd2012-01-26 11:23:32 +01005596 rtl8169_schedule_work(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597}
5598
Francois Romieu07d3f512007-02-21 22:40:46 +01005599static void rtl8169_tx_interrupt(struct net_device *dev,
5600 struct rtl8169_private *tp,
5601 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602{
5603 unsigned int dirty_tx, tx_left;
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 dirty_tx = tp->dirty_tx;
5606 smp_rmb();
5607 tx_left = tp->cur_tx - dirty_tx;
5608
5609 while (tx_left > 0) {
5610 unsigned int entry = dirty_tx % NUM_TX_DESC;
5611 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 u32 status;
5613
5614 rmb();
5615 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5616 if (status & DescOwn)
5617 break;
5618
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005619 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5620 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005622 dev->stats.tx_packets++;
5623 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005624 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 tx_skb->skb = NULL;
5626 }
5627 dirty_tx++;
5628 tx_left--;
5629 }
5630
5631 if (tp->dirty_tx != dirty_tx) {
5632 tp->dirty_tx = dirty_tx;
5633 smp_wmb();
5634 if (netif_queue_stopped(dev) &&
5635 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5636 netif_wake_queue(dev);
5637 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005638 /*
5639 * 8168 hack: TxPoll requests are lost when the Tx packets are
5640 * too close. Let's kick an extra TxPoll request when a burst
5641 * of start_xmit activity is detected (if it is not detected,
5642 * it is slow enough). -- FR
5643 */
5644 smp_rmb();
5645 if (tp->cur_tx != dirty_tx)
5646 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 }
5648}
5649
Francois Romieu126fa4b2005-05-12 20:09:17 -04005650static inline int rtl8169_fragmented_frame(u32 status)
5651{
5652 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5653}
5654
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005655static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 u32 status = opts1 & RxProtoMask;
5658
5659 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005660 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 skb->ip_summed = CHECKSUM_UNNECESSARY;
5662 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005663 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664}
5665
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005666static struct sk_buff *rtl8169_try_rx_copy(void *data,
5667 struct rtl8169_private *tp,
5668 int pkt_size,
5669 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005671 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005672 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005674 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005675 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005676 prefetch(data);
5677 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5678 if (skb)
5679 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005680 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5681
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005682 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683}
5684
Francois Romieu07d3f512007-02-21 22:40:46 +01005685static int rtl8169_rx_interrupt(struct net_device *dev,
5686 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005687 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688{
5689 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005690 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 cur_rx = tp->cur_rx;
5693 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005694 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005696 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005698 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 u32 status;
5700
5701 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005702 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
5704 if (status & DescOwn)
5705 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005706 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005707 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5708 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005709 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005711 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005713 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005714 if (status & RxFOVF) {
Francois Romieu4422bcd2012-01-26 11:23:32 +01005715 rtl8169_schedule_work(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005716 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005717 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005718 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005720 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005721 dma_addr_t addr = le64_to_cpu(desc->addr);
Francois Romieudeb9d932011-07-12 08:24:28 +02005722 int pkt_size = (status & 0x00003fff) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Francois Romieu126fa4b2005-05-12 20:09:17 -04005724 /*
5725 * The driver does not support incoming fragmented
5726 * frames. They are seen as a symptom of over-mtu
5727 * sized frames.
5728 */
5729 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005730 dev->stats.rx_dropped++;
5731 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005732 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005733 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005734 }
5735
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005736 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5737 tp, pkt_size, addr);
5738 rtl8169_mark_to_asic(desc, rx_buf_sz);
5739 if (!skb) {
5740 dev->stats.rx_dropped++;
5741 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 }
5743
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005744 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 skb_put(skb, pkt_size);
5746 skb->protocol = eth_type_trans(skb, dev);
5747
Francois Romieu7a8fc772011-03-01 17:18:33 +01005748 rtl8169_rx_vlan_tag(desc, skb);
5749
Francois Romieu56de4142011-03-15 17:29:31 +01005750 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Francois Romieucebf8cc2007-10-18 12:06:54 +02005752 dev->stats.rx_bytes += pkt_size;
5753 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005755
5756 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005757 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005758 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5759 desc->opts2 = 0;
5760 cur_rx++;
5761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 }
5763
5764 count = cur_rx - tp->cur_rx;
5765 tp->cur_rx = cur_rx;
5766
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005767 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 return count;
5770}
5771
Francois Romieu07d3f512007-02-21 22:40:46 +01005772static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
Francois Romieu07d3f512007-02-21 22:40:46 +01005774 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005778 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
David Dillowf11a3772009-05-22 15:29:34 +00005780 /* loop handling interrupts until we have no new ones or
5781 * we hit a invalid/hotplug case.
5782 */
Francois Romieu865c6522008-05-11 14:51:00 +02005783 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005784 while (status && status != 0xffff) {
françois romieu811fd302011-12-04 20:30:45 +00005785 status &= tp->intr_event;
5786 if (!status)
5787 break;
5788
David Dillowf11a3772009-05-22 15:29:34 +00005789 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
David Dillowf11a3772009-05-22 15:29:34 +00005791 /* Handle all of the error cases first. These will reset
5792 * the chip, so just exit the loop.
5793 */
5794 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005795 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 }
David Dillowf11a3772009-05-22 15:29:34 +00005798
Francois Romieu1519e572011-02-03 12:02:36 +01005799 if (unlikely(status & RxFIFOOver)) {
5800 switch (tp->mac_version) {
5801 /* Work around for rx fifo overflow */
5802 case RTL_GIGA_MAC_VER_11:
Francois Romieu1519e572011-02-03 12:02:36 +01005803 netif_stop_queue(dev);
5804 rtl8169_tx_timeout(dev);
5805 goto done;
Francois Romieu1519e572011-02-03 12:02:36 +01005806 default:
5807 break;
5808 }
David Dillowf11a3772009-05-22 15:29:34 +00005809 }
5810
5811 if (unlikely(status & SYSErr)) {
5812 rtl8169_pcierr_interrupt(dev);
5813 break;
5814 }
5815
5816 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005817 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005818
5819 /* We need to see the lastest version of tp->intr_mask to
5820 * avoid ignoring an MSI interrupt and having to wait for
5821 * another event which may never come.
5822 */
5823 smp_rmb();
5824 if (status & tp->intr_mask & tp->napi_event) {
5825 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5826 tp->intr_mask = ~tp->napi_event;
5827
5828 if (likely(napi_schedule_prep(&tp->napi)))
5829 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005830 else
5831 netif_info(tp, intr, dev,
5832 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005833 }
5834
5835 /* We only get a new MSI interrupt when all active irq
5836 * sources on the chip have been acknowledged. So, ack
5837 * everything we've seen and check if new sources have become
5838 * active to avoid blocking all interrupts from the chip.
5839 */
5840 RTL_W16(IntrStatus,
5841 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5842 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 }
Francois Romieu1519e572011-02-03 12:02:36 +01005844done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 return IRQ_RETVAL(handled);
5846}
5847
Francois Romieu4422bcd2012-01-26 11:23:32 +01005848static void rtl_task(struct work_struct *work)
5849{
5850 struct rtl8169_private *tp =
5851 container_of(work, struct rtl8169_private, wk.work);
5852
5853 rtl_reset_work(tp);
5854}
5855
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005856static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005858 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5859 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005861 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005863 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 rtl8169_tx_interrupt(dev, tp, ioaddr);
5865
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005866 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005867 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005868
5869 /* We need for force the visibility of tp->intr_mask
5870 * for other CPUs, as we can loose an MSI interrupt
5871 * and potentially wait for a retransmit timeout if we don't.
5872 * The posted write to IntrMask is safe, as it will
5873 * eventually make it to the chip and we won't loose anything
5874 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 */
David Dillowf11a3772009-05-22 15:29:34 +00005876 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005877 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005878 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 }
5880
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005881 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Francois Romieu523a6092008-09-10 22:28:56 +02005884static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5885{
5886 struct rtl8169_private *tp = netdev_priv(dev);
5887
5888 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5889 return;
5890
5891 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5892 RTL_W32(RxMissed, 0);
5893}
5894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895static void rtl8169_down(struct net_device *dev)
5896{
5897 struct rtl8169_private *tp = netdev_priv(dev);
5898 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Francois Romieu4876cc12011-03-11 21:07:11 +01005900 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
5902 netif_stop_queue(dev);
5903
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005904 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005905
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 spin_lock_irq(&tp->lock);
5907
Hayes Wang92fc43b2011-07-06 15:58:03 +08005908 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005909 /*
5910 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005911 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5912 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005913 */
Francois Romieu523a6092008-09-10 22:28:56 +02005914 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
5916 spin_unlock_irq(&tp->lock);
5917
5918 synchronize_irq(dev->irq);
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005921 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 rtl8169_tx_clear(tp);
5924
5925 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005926
5927 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
5930static int rtl8169_close(struct net_device *dev)
5931{
5932 struct rtl8169_private *tp = netdev_priv(dev);
5933 struct pci_dev *pdev = tp->pci_dev;
5934
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005935 pm_runtime_get_sync(&pdev->dev);
5936
Francois Romieucecb5fd2011-04-01 10:21:07 +02005937 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005938 rtl8169_update_counters(dev);
5939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 rtl8169_down(dev);
5941
5942 free_irq(dev->irq, dev);
5943
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005944 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5945 tp->RxPhyAddr);
5946 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5947 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 tp->TxDescArray = NULL;
5949 tp->RxDescArray = NULL;
5950
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005951 pm_runtime_put_sync(&pdev->dev);
5952
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 return 0;
5954}
5955
Francois Romieu07ce4062007-02-23 23:36:39 +01005956static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957{
5958 struct rtl8169_private *tp = netdev_priv(dev);
5959 void __iomem *ioaddr = tp->mmio_addr;
5960 unsigned long flags;
5961 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005962 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 u32 tmp = 0;
5964
5965 if (dev->flags & IFF_PROMISC) {
5966 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005967 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 rx_mode =
5969 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5970 AcceptAllPhys;
5971 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005972 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005973 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 /* Too many to filter perfectly -- accept all multicasts. */
5975 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5976 mc_filter[1] = mc_filter[0] = 0xffffffff;
5977 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005978 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005979
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 rx_mode = AcceptBroadcast | AcceptMyPhys;
5981 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005982 netdev_for_each_mc_addr(ha, dev) {
5983 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5985 rx_mode |= AcceptMulticast;
5986 }
5987 }
5988
5989 spin_lock_irqsave(&tp->lock, flags);
5990
Francois Romieu1687b562011-07-19 17:21:29 +02005991 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
Francois Romieuf887cce2008-07-17 22:24:18 +02005993 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005994 u32 data = mc_filter[0];
5995
5996 mc_filter[0] = swab32(mc_filter[1]);
5997 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005998 }
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07006001 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Francois Romieu57a9f232007-06-04 22:10:15 +02006003 RTL_W32(RxConfig, tmp);
6004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 spin_unlock_irqrestore(&tp->lock, flags);
6006}
6007
6008/**
6009 * rtl8169_get_stats - Get rtl8169 read/write statistics
6010 * @dev: The Ethernet Device to get statistics for
6011 *
6012 * Get TX/RX statistics for rtl8169
6013 */
6014static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6015{
6016 struct rtl8169_private *tp = netdev_priv(dev);
6017 void __iomem *ioaddr = tp->mmio_addr;
6018 unsigned long flags;
6019
6020 if (netif_running(dev)) {
6021 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02006022 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 spin_unlock_irqrestore(&tp->lock, flags);
6024 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02006025
Francois Romieucebf8cc2007-10-18 12:06:54 +02006026 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027}
6028
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006029static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006030{
françois romieu065c27c2011-01-03 15:08:12 +00006031 struct rtl8169_private *tp = netdev_priv(dev);
6032
Francois Romieu5d06a992006-02-23 00:47:58 +01006033 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006034 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006035
françois romieu065c27c2011-01-03 15:08:12 +00006036 rtl_pll_power_down(tp);
6037
Francois Romieu5d06a992006-02-23 00:47:58 +01006038 netif_device_detach(dev);
6039 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006040}
Francois Romieu5d06a992006-02-23 00:47:58 +01006041
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006042#ifdef CONFIG_PM
6043
6044static int rtl8169_suspend(struct device *device)
6045{
6046 struct pci_dev *pdev = to_pci_dev(device);
6047 struct net_device *dev = pci_get_drvdata(pdev);
6048
6049 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006050
Francois Romieu5d06a992006-02-23 00:47:58 +01006051 return 0;
6052}
6053
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006054static void __rtl8169_resume(struct net_device *dev)
6055{
françois romieu065c27c2011-01-03 15:08:12 +00006056 struct rtl8169_private *tp = netdev_priv(dev);
6057
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006058 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006059
6060 rtl_pll_power_up(tp);
6061
Francois Romieu4422bcd2012-01-26 11:23:32 +01006062 rtl8169_schedule_work(dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006063}
6064
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006065static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006066{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006067 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006068 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006069 struct rtl8169_private *tp = netdev_priv(dev);
6070
6071 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006072
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006073 if (netif_running(dev))
6074 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006075
Francois Romieu5d06a992006-02-23 00:47:58 +01006076 return 0;
6077}
6078
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006079static int rtl8169_runtime_suspend(struct device *device)
6080{
6081 struct pci_dev *pdev = to_pci_dev(device);
6082 struct net_device *dev = pci_get_drvdata(pdev);
6083 struct rtl8169_private *tp = netdev_priv(dev);
6084
6085 if (!tp->TxDescArray)
6086 return 0;
6087
6088 spin_lock_irq(&tp->lock);
6089 tp->saved_wolopts = __rtl8169_get_wol(tp);
6090 __rtl8169_set_wol(tp, WAKE_ANY);
6091 spin_unlock_irq(&tp->lock);
6092
6093 rtl8169_net_suspend(dev);
6094
6095 return 0;
6096}
6097
6098static int rtl8169_runtime_resume(struct device *device)
6099{
6100 struct pci_dev *pdev = to_pci_dev(device);
6101 struct net_device *dev = pci_get_drvdata(pdev);
6102 struct rtl8169_private *tp = netdev_priv(dev);
6103
6104 if (!tp->TxDescArray)
6105 return 0;
6106
6107 spin_lock_irq(&tp->lock);
6108 __rtl8169_set_wol(tp, tp->saved_wolopts);
6109 tp->saved_wolopts = 0;
6110 spin_unlock_irq(&tp->lock);
6111
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006112 rtl8169_init_phy(dev, tp);
6113
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006114 __rtl8169_resume(dev);
6115
6116 return 0;
6117}
6118
6119static int rtl8169_runtime_idle(struct device *device)
6120{
6121 struct pci_dev *pdev = to_pci_dev(device);
6122 struct net_device *dev = pci_get_drvdata(pdev);
6123 struct rtl8169_private *tp = netdev_priv(dev);
6124
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006125 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006126}
6127
Alexey Dobriyan47145212009-12-14 18:00:08 -08006128static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006129 .suspend = rtl8169_suspend,
6130 .resume = rtl8169_resume,
6131 .freeze = rtl8169_suspend,
6132 .thaw = rtl8169_resume,
6133 .poweroff = rtl8169_suspend,
6134 .restore = rtl8169_resume,
6135 .runtime_suspend = rtl8169_runtime_suspend,
6136 .runtime_resume = rtl8169_runtime_resume,
6137 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006138};
6139
6140#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6141
6142#else /* !CONFIG_PM */
6143
6144#define RTL8169_PM_OPS NULL
6145
6146#endif /* !CONFIG_PM */
6147
David S. Miller1805b2f2011-10-24 18:18:09 -04006148static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6149{
6150 void __iomem *ioaddr = tp->mmio_addr;
6151
6152 /* WoL fails with 8168b when the receiver is disabled. */
6153 switch (tp->mac_version) {
6154 case RTL_GIGA_MAC_VER_11:
6155 case RTL_GIGA_MAC_VER_12:
6156 case RTL_GIGA_MAC_VER_17:
6157 pci_clear_master(tp->pci_dev);
6158
6159 RTL_W8(ChipCmd, CmdRxEnb);
6160 /* PCI commit */
6161 RTL_R8(ChipCmd);
6162 break;
6163 default:
6164 break;
6165 }
6166}
6167
Francois Romieu1765f952008-09-13 17:21:40 +02006168static void rtl_shutdown(struct pci_dev *pdev)
6169{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006170 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006171 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006172
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006173 rtl8169_net_suspend(dev);
6174
Francois Romieucecb5fd2011-04-01 10:21:07 +02006175 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006176 rtl_rar_set(tp, dev->perm_addr);
6177
françois romieu4bb3f522009-06-17 11:41:45 +00006178 spin_lock_irq(&tp->lock);
6179
Hayes Wang92fc43b2011-07-06 15:58:03 +08006180 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006181
6182 spin_unlock_irq(&tp->lock);
6183
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006184 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006185 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6186 rtl_wol_suspend_quirk(tp);
6187 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006188 }
6189
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006190 pci_wake_from_d3(pdev, true);
6191 pci_set_power_state(pdev, PCI_D3hot);
6192 }
6193}
Francois Romieu5d06a992006-02-23 00:47:58 +01006194
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195static struct pci_driver rtl8169_pci_driver = {
6196 .name = MODULENAME,
6197 .id_table = rtl8169_pci_tbl,
6198 .probe = rtl8169_init_one,
6199 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006200 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006201 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202};
6203
Francois Romieu07d3f512007-02-21 22:40:46 +01006204static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205{
Jeff Garzik29917622006-08-19 17:48:59 -04006206 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207}
6208
Francois Romieu07d3f512007-02-21 22:40:46 +01006209static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210{
6211 pci_unregister_driver(&rtl8169_pci_driver);
6212}
6213
6214module_init(rtl8169_init_module);
6215module_exit(rtl8169_cleanup_module);