blob: 6d74931049fb54a0ba6edb6873583d14d76014a1 [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
françois romieu811fd302011-12-04 20:30:45 +00001187static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
françois romieu811fd302011-12-04 20:30:45 +00001189 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
françois romieu811fd302011-12-04 20:30:45 +00001191 RTL_W16(IntrMask, 0x0000);
1192 RTL_W16(IntrStatus, tp->intr_event);
1193 RTL_R8(ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
françois romieu4da19632011-01-03 15:07:55 +00001196static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
françois romieu4da19632011-01-03 15:07:55 +00001198 void __iomem *ioaddr = tp->mmio_addr;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return RTL_R32(TBICSR) & TBIReset;
1201}
1202
françois romieu4da19632011-01-03 15:07:55 +00001203static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
françois romieu4da19632011-01-03 15:07:55 +00001205 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1209{
1210 return RTL_R32(TBICSR) & TBILinkOk;
1211}
1212
1213static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1214{
1215 return RTL_R8(PHYstatus) & LinkStatus;
1216}
1217
françois romieu4da19632011-01-03 15:07:55 +00001218static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
françois romieu4da19632011-01-03 15:07:55 +00001220 void __iomem *ioaddr = tp->mmio_addr;
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1223}
1224
françois romieu4da19632011-01-03 15:07:55 +00001225static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 unsigned int val;
1228
françois romieu4da19632011-01-03 15:07:55 +00001229 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1230 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Hayes Wang70090422011-07-06 15:58:06 +08001233static void rtl_link_chg_patch(struct rtl8169_private *tp)
1234{
1235 void __iomem *ioaddr = tp->mmio_addr;
1236 struct net_device *dev = tp->dev;
1237
1238 if (!netif_running(dev))
1239 return;
1240
1241 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1242 if (RTL_R8(PHYstatus) & _1000bpsF) {
1243 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1244 0x00000011, ERIAR_EXGMAC);
1245 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1246 0x00000005, ERIAR_EXGMAC);
1247 } else if (RTL_R8(PHYstatus) & _100bps) {
1248 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1249 0x0000001f, ERIAR_EXGMAC);
1250 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1251 0x00000005, ERIAR_EXGMAC);
1252 } else {
1253 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1254 0x0000001f, ERIAR_EXGMAC);
1255 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1256 0x0000003f, ERIAR_EXGMAC);
1257 }
1258 /* Reset packet filter */
1259 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1260 ERIAR_EXGMAC);
1261 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1262 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001263 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1264 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1265 if (RTL_R8(PHYstatus) & _1000bpsF) {
1266 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1267 0x00000011, ERIAR_EXGMAC);
1268 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1269 0x00000005, ERIAR_EXGMAC);
1270 } else {
1271 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1272 0x0000001f, ERIAR_EXGMAC);
1273 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1274 0x0000003f, ERIAR_EXGMAC);
1275 }
Hayes Wang70090422011-07-06 15:58:06 +08001276 }
1277}
1278
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001279static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001280 struct rtl8169_private *tp,
1281 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 unsigned long flags;
1284
1285 spin_lock_irqsave(&tp->lock, flags);
1286 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001287 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001288 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001289 if (pm)
1290 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001292 if (net_ratelimit())
1293 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001294 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001296 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001297 if (pm)
hayeswang10953db2011-11-07 20:44:37 +00001298 pm_schedule_suspend(&tp->pci_dev->dev, 5000);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 spin_unlock_irqrestore(&tp->lock, flags);
1301}
1302
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001303static void rtl8169_check_link_status(struct net_device *dev,
1304 struct rtl8169_private *tp,
1305 void __iomem *ioaddr)
1306{
1307 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1308}
1309
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001310#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1311
1312static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1313{
1314 void __iomem *ioaddr = tp->mmio_addr;
1315 u8 options;
1316 u32 wolopts = 0;
1317
1318 options = RTL_R8(Config1);
1319 if (!(options & PMEnable))
1320 return 0;
1321
1322 options = RTL_R8(Config3);
1323 if (options & LinkUp)
1324 wolopts |= WAKE_PHY;
1325 if (options & MagicPacket)
1326 wolopts |= WAKE_MAGIC;
1327
1328 options = RTL_R8(Config5);
1329 if (options & UWF)
1330 wolopts |= WAKE_UCAST;
1331 if (options & BWF)
1332 wolopts |= WAKE_BCAST;
1333 if (options & MWF)
1334 wolopts |= WAKE_MCAST;
1335
1336 return wolopts;
1337}
1338
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001339static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1340{
1341 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001342
1343 spin_lock_irq(&tp->lock);
1344
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001345 wol->supported = WAKE_ANY;
1346 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001347
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001348 spin_unlock_irq(&tp->lock);
1349}
1350
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001351static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001352{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001353 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001354 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001355 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001356 u32 opt;
1357 u16 reg;
1358 u8 mask;
1359 } cfg[] = {
1360 { WAKE_ANY, Config1, PMEnable },
1361 { WAKE_PHY, Config3, LinkUp },
1362 { WAKE_MAGIC, Config3, MagicPacket },
1363 { WAKE_UCAST, Config5, UWF },
1364 { WAKE_BCAST, Config5, BWF },
1365 { WAKE_MCAST, Config5, MWF },
1366 { WAKE_ANY, Config5, LanWake }
1367 };
1368
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001369 RTL_W8(Cfg9346, Cfg9346_Unlock);
1370
1371 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1372 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001373 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001374 options |= cfg[i].mask;
1375 RTL_W8(cfg[i].reg, options);
1376 }
1377
1378 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001379}
1380
1381static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1382{
1383 struct rtl8169_private *tp = netdev_priv(dev);
1384
1385 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001386
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001387 if (wol->wolopts)
1388 tp->features |= RTL_FEATURE_WOL;
1389 else
1390 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001391 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392 spin_unlock_irq(&tp->lock);
1393
françois romieuea809072010-11-08 13:23:58 +00001394 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1395
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001396 return 0;
1397}
1398
Francois Romieu31bd2042011-04-26 18:58:59 +02001399static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1400{
Francois Romieu85bffe62011-04-27 08:22:39 +02001401 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404static void rtl8169_get_drvinfo(struct net_device *dev,
1405 struct ethtool_drvinfo *info)
1406{
1407 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001408 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Rick Jones68aad782011-11-07 13:29:27 +00001410 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1411 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1412 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001413 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001414 if (!IS_ERR_OR_NULL(rtl_fw))
1415 strlcpy(info->fw_version, rtl_fw->version,
1416 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419static int rtl8169_get_regs_len(struct net_device *dev)
1420{
1421 return R8169_REGS_SIZE;
1422}
1423
1424static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001425 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct rtl8169_private *tp = netdev_priv(dev);
1428 void __iomem *ioaddr = tp->mmio_addr;
1429 int ret = 0;
1430 u32 reg;
1431
1432 reg = RTL_R32(TBICSR);
1433 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1434 (duplex == DUPLEX_FULL)) {
1435 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1436 } else if (autoneg == AUTONEG_ENABLE)
1437 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1438 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001439 netif_warn(tp, link, dev,
1440 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ret = -EOPNOTSUPP;
1442 }
1443
1444 return ret;
1445}
1446
1447static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001448 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001451 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001452 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Hayes Wang716b50a2011-02-22 17:26:18 +08001454 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001457 int auto_nego;
1458
françois romieu4da19632011-01-03 15:07:55 +00001459 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001460 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1461 ADVERTISE_100HALF | ADVERTISE_100FULL);
1462
1463 if (adv & ADVERTISED_10baseT_Half)
1464 auto_nego |= ADVERTISE_10HALF;
1465 if (adv & ADVERTISED_10baseT_Full)
1466 auto_nego |= ADVERTISE_10FULL;
1467 if (adv & ADVERTISED_100baseT_Half)
1468 auto_nego |= ADVERTISE_100HALF;
1469 if (adv & ADVERTISED_100baseT_Full)
1470 auto_nego |= ADVERTISE_100FULL;
1471
françois romieu3577aa12009-05-19 10:46:48 +00001472 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1473
françois romieu4da19632011-01-03 15:07:55 +00001474 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001475 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1476
1477 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001478 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001479 if (adv & ADVERTISED_1000baseT_Half)
1480 giga_ctrl |= ADVERTISE_1000HALF;
1481 if (adv & ADVERTISED_1000baseT_Full)
1482 giga_ctrl |= ADVERTISE_1000FULL;
1483 } else if (adv & (ADVERTISED_1000baseT_Half |
1484 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001485 netif_info(tp, link, dev,
1486 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001487 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
françois romieu3577aa12009-05-19 10:46:48 +00001490 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001491
françois romieu4da19632011-01-03 15:07:55 +00001492 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1493 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001494 } else {
1495 giga_ctrl = 0;
1496
1497 if (speed == SPEED_10)
1498 bmcr = 0;
1499 else if (speed == SPEED_100)
1500 bmcr = BMCR_SPEED100;
1501 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001502 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001503
1504 if (duplex == DUPLEX_FULL)
1505 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001506 }
1507
françois romieu4da19632011-01-03 15:07:55 +00001508 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001509
Francois Romieucecb5fd2011-04-01 10:21:07 +02001510 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1511 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001512 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001513 rtl_writephy(tp, 0x17, 0x2138);
1514 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001515 } else {
françois romieu4da19632011-01-03 15:07:55 +00001516 rtl_writephy(tp, 0x17, 0x2108);
1517 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001518 }
1519 }
1520
Oliver Neukum54405cd2011-01-06 21:55:13 +01001521 rc = 0;
1522out:
1523 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001527 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 struct rtl8169_private *tp = netdev_priv(dev);
1530 int ret;
1531
Oliver Neukum54405cd2011-01-06 21:55:13 +01001532 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001533 if (ret < 0)
1534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Francois Romieu4876cc12011-03-11 21:07:11 +01001536 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1537 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001539 }
1540out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return ret;
1542}
1543
1544static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1545{
1546 struct rtl8169_private *tp = netdev_priv(dev);
1547 unsigned long flags;
1548 int ret;
1549
Francois Romieu4876cc12011-03-11 21:07:11 +01001550 del_timer_sync(&tp->timer);
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001553 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001554 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return ret;
1558}
1559
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001560static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1561 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Francois Romieud58d46b2011-05-03 16:38:29 +02001563 struct rtl8169_private *tp = netdev_priv(dev);
1564
Francois Romieu2b7b4312011-04-18 22:53:24 -07001565 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001566 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Francois Romieud58d46b2011-05-03 16:38:29 +02001568 if (dev->mtu > JUMBO_1K &&
1569 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
1570 features &= ~NETIF_F_IP_CSUM;
1571
Michał Mirosław350fb322011-04-08 06:35:56 +00001572 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001575static int rtl8169_set_features(struct net_device *dev,
1576 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct rtl8169_private *tp = netdev_priv(dev);
1579 void __iomem *ioaddr = tp->mmio_addr;
1580 unsigned long flags;
1581
1582 spin_lock_irqsave(&tp->lock, flags);
1583
Michał Mirosław350fb322011-04-08 06:35:56 +00001584 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 tp->cp_cmd |= RxChkSum;
1586 else
1587 tp->cp_cmd &= ~RxChkSum;
1588
Michał Mirosław350fb322011-04-08 06:35:56 +00001589 if (dev->features & NETIF_F_HW_VLAN_RX)
1590 tp->cp_cmd |= RxVlan;
1591 else
1592 tp->cp_cmd &= ~RxVlan;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 RTL_W16(CPlusCmd, tp->cp_cmd);
1595 RTL_R16(CPlusCmd);
1596
1597 spin_unlock_irqrestore(&tp->lock, flags);
1598
1599 return 0;
1600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1603 struct sk_buff *skb)
1604{
Jesse Grosseab6d182010-10-20 13:56:03 +00001605 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1607}
1608
Francois Romieu7a8fc772011-03-01 17:18:33 +01001609static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Francois Romieu7a8fc772011-03-01 17:18:33 +01001613 if (opts2 & RxVlanTag)
1614 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Francois Romieuccdffb92008-07-26 14:26:06 +02001619static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 struct rtl8169_private *tp = netdev_priv(dev);
1622 void __iomem *ioaddr = tp->mmio_addr;
1623 u32 status;
1624
1625 cmd->supported =
1626 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1627 cmd->port = PORT_FIBRE;
1628 cmd->transceiver = XCVR_INTERNAL;
1629
1630 status = RTL_R32(TBICSR);
1631 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1632 cmd->autoneg = !!(status & TBINwEnable);
1633
David Decotigny70739492011-04-27 18:32:40 +00001634 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001636
1637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Francois Romieuccdffb92008-07-26 14:26:06 +02001640static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Francois Romieuccdffb92008-07-26 14:26:06 +02001644 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1648{
1649 struct rtl8169_private *tp = netdev_priv(dev);
1650 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001651 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 spin_lock_irqsave(&tp->lock, flags);
1654
Francois Romieuccdffb92008-07-26 14:26:06 +02001655 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001658 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1662 void *p)
1663{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001664 struct rtl8169_private *tp = netdev_priv(dev);
1665 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Francois Romieu5b0384f2006-08-16 16:00:01 +02001667 if (regs->len > R8169_REGS_SIZE)
1668 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Francois Romieu5b0384f2006-08-16 16:00:01 +02001670 spin_lock_irqsave(&tp->lock, flags);
1671 memcpy_fromio(p, tp->mmio_addr, regs->len);
1672 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001675static u32 rtl8169_get_msglevel(struct net_device *dev)
1676{
1677 struct rtl8169_private *tp = netdev_priv(dev);
1678
1679 return tp->msg_enable;
1680}
1681
1682static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1683{
1684 struct rtl8169_private *tp = netdev_priv(dev);
1685
1686 tp->msg_enable = value;
1687}
1688
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001689static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1690 "tx_packets",
1691 "rx_packets",
1692 "tx_errors",
1693 "rx_errors",
1694 "rx_missed",
1695 "align_errors",
1696 "tx_single_collisions",
1697 "tx_multi_collisions",
1698 "unicast",
1699 "broadcast",
1700 "multicast",
1701 "tx_aborted",
1702 "tx_underrun",
1703};
1704
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001705static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001706{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001707 switch (sset) {
1708 case ETH_SS_STATS:
1709 return ARRAY_SIZE(rtl8169_gstrings);
1710 default:
1711 return -EOPNOTSUPP;
1712 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001713}
1714
Ivan Vecera355423d2009-02-06 21:49:57 -08001715static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001716{
1717 struct rtl8169_private *tp = netdev_priv(dev);
1718 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001719 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001720 struct rtl8169_counters *counters;
1721 dma_addr_t paddr;
1722 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001723 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001724
Ivan Vecera355423d2009-02-06 21:49:57 -08001725 /*
1726 * Some chips are unable to dump tally counters when the receiver
1727 * is disabled.
1728 */
1729 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1730 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001731
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001732 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001733 if (!counters)
1734 return;
1735
1736 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001737 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001738 RTL_W32(CounterAddrLow, cmd);
1739 RTL_W32(CounterAddrLow, cmd | CounterDump);
1740
Ivan Vecera355423d2009-02-06 21:49:57 -08001741 while (wait--) {
1742 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001743 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001744 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001745 }
1746 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001747 }
1748
1749 RTL_W32(CounterAddrLow, 0);
1750 RTL_W32(CounterAddrHigh, 0);
1751
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001752 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001753}
1754
Ivan Vecera355423d2009-02-06 21:49:57 -08001755static void rtl8169_get_ethtool_stats(struct net_device *dev,
1756 struct ethtool_stats *stats, u64 *data)
1757{
1758 struct rtl8169_private *tp = netdev_priv(dev);
1759
1760 ASSERT_RTNL();
1761
1762 rtl8169_update_counters(dev);
1763
1764 data[0] = le64_to_cpu(tp->counters.tx_packets);
1765 data[1] = le64_to_cpu(tp->counters.rx_packets);
1766 data[2] = le64_to_cpu(tp->counters.tx_errors);
1767 data[3] = le32_to_cpu(tp->counters.rx_errors);
1768 data[4] = le16_to_cpu(tp->counters.rx_missed);
1769 data[5] = le16_to_cpu(tp->counters.align_errors);
1770 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1771 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1772 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1773 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1774 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1775 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1776 data[12] = le16_to_cpu(tp->counters.tx_underun);
1777}
1778
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001779static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1780{
1781 switch(stringset) {
1782 case ETH_SS_STATS:
1783 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1784 break;
1785 }
1786}
1787
Jeff Garzik7282d492006-09-13 14:30:00 -04001788static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 .get_drvinfo = rtl8169_get_drvinfo,
1790 .get_regs_len = rtl8169_get_regs_len,
1791 .get_link = ethtool_op_get_link,
1792 .get_settings = rtl8169_get_settings,
1793 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001794 .get_msglevel = rtl8169_get_msglevel,
1795 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001797 .get_wol = rtl8169_get_wol,
1798 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001799 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001800 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001801 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802};
1803
Francois Romieu07d3f512007-02-21 22:40:46 +01001804static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001805 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Francois Romieu5d320a22011-05-08 17:47:36 +02001807 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001808 /*
1809 * The driver currently handles the 8168Bf and the 8168Be identically
1810 * but they can be identified more specifically through the test below
1811 * if needed:
1812 *
1813 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001814 *
1815 * Same thing for the 8101Eb and the 8101Ec:
1816 *
1817 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001818 */
Francois Romieu37441002011-06-17 22:58:54 +02001819 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001821 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int mac_version;
1823 } mac_info[] = {
Hayes Wangc2218922011-09-06 16:55:18 +08001824 /* 8168F family. */
1825 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
1826 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
1827
hayeswang01dc7fe2011-03-21 01:50:28 +00001828 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001829 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001830 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1831 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1832 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1833
Francois Romieu5b538df2008-07-20 16:22:45 +02001834 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001835 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1836 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001837 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001838
françois romieue6de30d2011-01-03 15:08:37 +00001839 /* 8168DP family. */
1840 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1841 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001842 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001843
Francois Romieuef808d52008-06-29 13:10:54 +02001844 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001845 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001846 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001847 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001848 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001849 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1850 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001851 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001852 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001853 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001854
1855 /* 8168B family. */
1856 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1857 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1858 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1859 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1860
1861 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001862 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001863 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1864 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1865 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001866 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1867 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1868 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1869 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1870 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1871 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001872 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001873 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001874 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001875 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1876 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001877 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1878 /* FIXME: where did these entries come from ? -- FR */
1879 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1880 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1881
1882 /* 8110 family. */
1883 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1884 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1885 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1886 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1887 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1888 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1889
Jean Delvaref21b75e2009-05-26 20:54:48 -07001890 /* Catch-all */
1891 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001892 };
1893 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 u32 reg;
1895
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001896 reg = RTL_R32(TxConfig);
1897 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 p++;
1899 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001900
1901 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1902 netif_notice(tp, probe, dev,
1903 "unknown MAC, using family default\n");
1904 tp->mac_version = default_version;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1909{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001910 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
Francois Romieu867763c2007-08-17 18:21:58 +02001913struct phy_reg {
1914 u16 reg;
1915 u16 val;
1916};
1917
françois romieu4da19632011-01-03 15:07:55 +00001918static void rtl_writephy_batch(struct rtl8169_private *tp,
1919 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001920{
1921 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001922 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001923 regs++;
1924 }
1925}
1926
françois romieubca03d52011-01-03 15:07:31 +00001927#define PHY_READ 0x00000000
1928#define PHY_DATA_OR 0x10000000
1929#define PHY_DATA_AND 0x20000000
1930#define PHY_BJMPN 0x30000000
1931#define PHY_READ_EFUSE 0x40000000
1932#define PHY_READ_MAC_BYTE 0x50000000
1933#define PHY_WRITE_MAC_BYTE 0x60000000
1934#define PHY_CLEAR_READCOUNT 0x70000000
1935#define PHY_WRITE 0x80000000
1936#define PHY_READCOUNT_EQ_SKIP 0x90000000
1937#define PHY_COMP_EQ_SKIPN 0xa0000000
1938#define PHY_COMP_NEQ_SKIPN 0xb0000000
1939#define PHY_WRITE_PREVIOUS 0xc0000000
1940#define PHY_SKIPN 0xd0000000
1941#define PHY_DELAY_MS 0xe0000000
1942#define PHY_WRITE_ERI_WORD 0xf0000000
1943
Hayes Wang960aee62011-06-18 11:37:48 +02001944struct fw_info {
1945 u32 magic;
1946 char version[RTL_VER_SIZE];
1947 __le32 fw_start;
1948 __le32 fw_len;
1949 u8 chksum;
1950} __packed;
1951
Francois Romieu1c361ef2011-06-17 17:16:24 +02001952#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1953
1954static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001955{
Francois Romieub6ffd972011-06-17 17:00:05 +02001956 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001957 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001958 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1959 char *version = rtl_fw->version;
1960 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001961
Francois Romieu1c361ef2011-06-17 17:16:24 +02001962 if (fw->size < FW_OPCODE_SIZE)
1963 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001964
1965 if (!fw_info->magic) {
1966 size_t i, size, start;
1967 u8 checksum = 0;
1968
1969 if (fw->size < sizeof(*fw_info))
1970 goto out;
1971
1972 for (i = 0; i < fw->size; i++)
1973 checksum += fw->data[i];
1974 if (checksum != 0)
1975 goto out;
1976
1977 start = le32_to_cpu(fw_info->fw_start);
1978 if (start > fw->size)
1979 goto out;
1980
1981 size = le32_to_cpu(fw_info->fw_len);
1982 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1983 goto out;
1984
1985 memcpy(version, fw_info->version, RTL_VER_SIZE);
1986
1987 pa->code = (__le32 *)(fw->data + start);
1988 pa->size = size;
1989 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001990 if (fw->size % FW_OPCODE_SIZE)
1991 goto out;
1992
1993 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1994
1995 pa->code = (__le32 *)fw->data;
1996 pa->size = fw->size / FW_OPCODE_SIZE;
1997 }
1998 version[RTL_VER_SIZE - 1] = 0;
1999
2000 rc = true;
2001out:
2002 return rc;
2003}
2004
Francois Romieufd112f22011-06-18 00:10:29 +02002005static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2006 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002007{
Francois Romieufd112f22011-06-18 00:10:29 +02002008 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002009 size_t index;
2010
Francois Romieu1c361ef2011-06-17 17:16:24 +02002011 for (index = 0; index < pa->size; index++) {
2012 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002013 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002014
hayeswang42b82dc2011-01-10 02:07:25 +00002015 switch(action & 0xf0000000) {
2016 case PHY_READ:
2017 case PHY_DATA_OR:
2018 case PHY_DATA_AND:
2019 case PHY_READ_EFUSE:
2020 case PHY_CLEAR_READCOUNT:
2021 case PHY_WRITE:
2022 case PHY_WRITE_PREVIOUS:
2023 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002024 break;
2025
hayeswang42b82dc2011-01-10 02:07:25 +00002026 case PHY_BJMPN:
2027 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002028 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002029 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002030 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002031 }
2032 break;
2033 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002034 if (index + 2 >= pa->size) {
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_COMP_EQ_SKIPN:
2041 case PHY_COMP_NEQ_SKIPN:
2042 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002043 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002044 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002045 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002046 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002047 }
2048 break;
2049
2050 case PHY_READ_MAC_BYTE:
2051 case PHY_WRITE_MAC_BYTE:
2052 case PHY_WRITE_ERI_WORD:
2053 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002054 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002055 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002056 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002057 }
2058 }
Francois Romieufd112f22011-06-18 00:10:29 +02002059 rc = true;
2060out:
2061 return rc;
2062}
françois romieubca03d52011-01-03 15:07:31 +00002063
Francois Romieufd112f22011-06-18 00:10:29 +02002064static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2065{
2066 struct net_device *dev = tp->dev;
2067 int rc = -EINVAL;
2068
2069 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2070 netif_err(tp, ifup, dev, "invalid firwmare\n");
2071 goto out;
2072 }
2073
2074 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2075 rc = 0;
2076out:
2077 return rc;
2078}
2079
2080static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2081{
2082 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2083 u32 predata, count;
2084 size_t index;
2085
2086 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002087
Francois Romieu1c361ef2011-06-17 17:16:24 +02002088 for (index = 0; index < pa->size; ) {
2089 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002090 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002091 u32 regno = (action & 0x0fff0000) >> 16;
2092
2093 if (!action)
2094 break;
françois romieubca03d52011-01-03 15:07:31 +00002095
2096 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002097 case PHY_READ:
2098 predata = rtl_readphy(tp, regno);
2099 count++;
2100 index++;
françois romieubca03d52011-01-03 15:07:31 +00002101 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002102 case PHY_DATA_OR:
2103 predata |= data;
2104 index++;
2105 break;
2106 case PHY_DATA_AND:
2107 predata &= data;
2108 index++;
2109 break;
2110 case PHY_BJMPN:
2111 index -= regno;
2112 break;
2113 case PHY_READ_EFUSE:
2114 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2115 index++;
2116 break;
2117 case PHY_CLEAR_READCOUNT:
2118 count = 0;
2119 index++;
2120 break;
2121 case PHY_WRITE:
2122 rtl_writephy(tp, regno, data);
2123 index++;
2124 break;
2125 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002126 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002127 break;
2128 case PHY_COMP_EQ_SKIPN:
2129 if (predata == data)
2130 index += regno;
2131 index++;
2132 break;
2133 case PHY_COMP_NEQ_SKIPN:
2134 if (predata != data)
2135 index += regno;
2136 index++;
2137 break;
2138 case PHY_WRITE_PREVIOUS:
2139 rtl_writephy(tp, regno, predata);
2140 index++;
2141 break;
2142 case PHY_SKIPN:
2143 index += regno + 1;
2144 break;
2145 case PHY_DELAY_MS:
2146 mdelay(data);
2147 index++;
2148 break;
2149
2150 case PHY_READ_MAC_BYTE:
2151 case PHY_WRITE_MAC_BYTE:
2152 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002153 default:
2154 BUG();
2155 }
2156 }
2157}
2158
françois romieuf1e02ed2011-01-13 13:07:53 +00002159static void rtl_release_firmware(struct rtl8169_private *tp)
2160{
Francois Romieub6ffd972011-06-17 17:00:05 +02002161 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2162 release_firmware(tp->rtl_fw->fw);
2163 kfree(tp->rtl_fw);
2164 }
2165 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002166}
2167
François Romieu953a12c2011-04-24 17:38:48 +02002168static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002169{
Francois Romieub6ffd972011-06-17 17:00:05 +02002170 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002171
2172 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002173 if (!IS_ERR_OR_NULL(rtl_fw))
2174 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002175}
2176
2177static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2178{
2179 if (rtl_readphy(tp, reg) != val)
2180 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2181 else
2182 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002183}
2184
françois romieu4da19632011-01-03 15:07:55 +00002185static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002187 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002188 { 0x1f, 0x0001 },
2189 { 0x06, 0x006e },
2190 { 0x08, 0x0708 },
2191 { 0x15, 0x4000 },
2192 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
françois romieu0b9b5712009-08-10 19:44:56 +00002194 { 0x1f, 0x0001 },
2195 { 0x03, 0x00a1 },
2196 { 0x02, 0x0008 },
2197 { 0x01, 0x0120 },
2198 { 0x00, 0x1000 },
2199 { 0x04, 0x0800 },
2200 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
françois romieu0b9b5712009-08-10 19:44:56 +00002202 { 0x03, 0xff41 },
2203 { 0x02, 0xdf60 },
2204 { 0x01, 0x0140 },
2205 { 0x00, 0x0077 },
2206 { 0x04, 0x7800 },
2207 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
françois romieu0b9b5712009-08-10 19:44:56 +00002209 { 0x03, 0x802f },
2210 { 0x02, 0x4f02 },
2211 { 0x01, 0x0409 },
2212 { 0x00, 0xf0f9 },
2213 { 0x04, 0x9800 },
2214 { 0x04, 0x9000 },
2215
2216 { 0x03, 0xdf01 },
2217 { 0x02, 0xdf20 },
2218 { 0x01, 0xff95 },
2219 { 0x00, 0xba00 },
2220 { 0x04, 0xa800 },
2221 { 0x04, 0xa000 },
2222
2223 { 0x03, 0xff41 },
2224 { 0x02, 0xdf20 },
2225 { 0x01, 0x0140 },
2226 { 0x00, 0x00bb },
2227 { 0x04, 0xb800 },
2228 { 0x04, 0xb000 },
2229
2230 { 0x03, 0xdf41 },
2231 { 0x02, 0xdc60 },
2232 { 0x01, 0x6340 },
2233 { 0x00, 0x007d },
2234 { 0x04, 0xd800 },
2235 { 0x04, 0xd000 },
2236
2237 { 0x03, 0xdf01 },
2238 { 0x02, 0xdf20 },
2239 { 0x01, 0x100a },
2240 { 0x00, 0xa0ff },
2241 { 0x04, 0xf800 },
2242 { 0x04, 0xf000 },
2243
2244 { 0x1f, 0x0000 },
2245 { 0x0b, 0x0000 },
2246 { 0x00, 0x9200 }
2247 };
2248
françois romieu4da19632011-01-03 15:07:55 +00002249 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
françois romieu4da19632011-01-03 15:07:55 +00002252static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002253{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002254 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002255 { 0x1f, 0x0002 },
2256 { 0x01, 0x90d0 },
2257 { 0x1f, 0x0000 }
2258 };
2259
françois romieu4da19632011-01-03 15:07:55 +00002260 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002261}
2262
françois romieu4da19632011-01-03 15:07:55 +00002263static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002264{
2265 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002266
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002267 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2268 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002269 return;
2270
françois romieu4da19632011-01-03 15:07:55 +00002271 rtl_writephy(tp, 0x1f, 0x0001);
2272 rtl_writephy(tp, 0x10, 0xf01b);
2273 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002274}
2275
françois romieu4da19632011-01-03 15:07:55 +00002276static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002277{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002278 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002279 { 0x1f, 0x0001 },
2280 { 0x04, 0x0000 },
2281 { 0x03, 0x00a1 },
2282 { 0x02, 0x0008 },
2283 { 0x01, 0x0120 },
2284 { 0x00, 0x1000 },
2285 { 0x04, 0x0800 },
2286 { 0x04, 0x9000 },
2287 { 0x03, 0x802f },
2288 { 0x02, 0x4f02 },
2289 { 0x01, 0x0409 },
2290 { 0x00, 0xf099 },
2291 { 0x04, 0x9800 },
2292 { 0x04, 0xa000 },
2293 { 0x03, 0xdf01 },
2294 { 0x02, 0xdf20 },
2295 { 0x01, 0xff95 },
2296 { 0x00, 0xba00 },
2297 { 0x04, 0xa800 },
2298 { 0x04, 0xf000 },
2299 { 0x03, 0xdf01 },
2300 { 0x02, 0xdf20 },
2301 { 0x01, 0x101a },
2302 { 0x00, 0xa0ff },
2303 { 0x04, 0xf800 },
2304 { 0x04, 0x0000 },
2305 { 0x1f, 0x0000 },
2306
2307 { 0x1f, 0x0001 },
2308 { 0x10, 0xf41b },
2309 { 0x14, 0xfb54 },
2310 { 0x18, 0xf5c7 },
2311 { 0x1f, 0x0000 },
2312
2313 { 0x1f, 0x0001 },
2314 { 0x17, 0x0cc0 },
2315 { 0x1f, 0x0000 }
2316 };
2317
françois romieu4da19632011-01-03 15:07:55 +00002318 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002319
françois romieu4da19632011-01-03 15:07:55 +00002320 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002321}
2322
françois romieu4da19632011-01-03 15:07:55 +00002323static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002324{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002325 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002326 { 0x1f, 0x0001 },
2327 { 0x04, 0x0000 },
2328 { 0x03, 0x00a1 },
2329 { 0x02, 0x0008 },
2330 { 0x01, 0x0120 },
2331 { 0x00, 0x1000 },
2332 { 0x04, 0x0800 },
2333 { 0x04, 0x9000 },
2334 { 0x03, 0x802f },
2335 { 0x02, 0x4f02 },
2336 { 0x01, 0x0409 },
2337 { 0x00, 0xf099 },
2338 { 0x04, 0x9800 },
2339 { 0x04, 0xa000 },
2340 { 0x03, 0xdf01 },
2341 { 0x02, 0xdf20 },
2342 { 0x01, 0xff95 },
2343 { 0x00, 0xba00 },
2344 { 0x04, 0xa800 },
2345 { 0x04, 0xf000 },
2346 { 0x03, 0xdf01 },
2347 { 0x02, 0xdf20 },
2348 { 0x01, 0x101a },
2349 { 0x00, 0xa0ff },
2350 { 0x04, 0xf800 },
2351 { 0x04, 0x0000 },
2352 { 0x1f, 0x0000 },
2353
2354 { 0x1f, 0x0001 },
2355 { 0x0b, 0x8480 },
2356 { 0x1f, 0x0000 },
2357
2358 { 0x1f, 0x0001 },
2359 { 0x18, 0x67c7 },
2360 { 0x04, 0x2000 },
2361 { 0x03, 0x002f },
2362 { 0x02, 0x4360 },
2363 { 0x01, 0x0109 },
2364 { 0x00, 0x3022 },
2365 { 0x04, 0x2800 },
2366 { 0x1f, 0x0000 },
2367
2368 { 0x1f, 0x0001 },
2369 { 0x17, 0x0cc0 },
2370 { 0x1f, 0x0000 }
2371 };
2372
françois romieu4da19632011-01-03 15:07:55 +00002373 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002374}
2375
françois romieu4da19632011-01-03 15:07:55 +00002376static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002377{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002378 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002379 { 0x10, 0xf41b },
2380 { 0x1f, 0x0000 }
2381 };
2382
françois romieu4da19632011-01-03 15:07:55 +00002383 rtl_writephy(tp, 0x1f, 0x0001);
2384 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002385
françois romieu4da19632011-01-03 15:07:55 +00002386 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002387}
2388
françois romieu4da19632011-01-03 15:07:55 +00002389static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002390{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002391 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002392 { 0x1f, 0x0001 },
2393 { 0x10, 0xf41b },
2394 { 0x1f, 0x0000 }
2395 };
2396
françois romieu4da19632011-01-03 15:07:55 +00002397 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002398}
2399
françois romieu4da19632011-01-03 15:07:55 +00002400static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002401{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002402 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002403 { 0x1f, 0x0000 },
2404 { 0x1d, 0x0f00 },
2405 { 0x1f, 0x0002 },
2406 { 0x0c, 0x1ec8 },
2407 { 0x1f, 0x0000 }
2408 };
2409
françois romieu4da19632011-01-03 15:07:55 +00002410 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002411}
2412
françois romieu4da19632011-01-03 15:07:55 +00002413static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002414{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002415 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002416 { 0x1f, 0x0001 },
2417 { 0x1d, 0x3d98 },
2418 { 0x1f, 0x0000 }
2419 };
2420
françois romieu4da19632011-01-03 15:07:55 +00002421 rtl_writephy(tp, 0x1f, 0x0000);
2422 rtl_patchphy(tp, 0x14, 1 << 5);
2423 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002424
françois romieu4da19632011-01-03 15:07:55 +00002425 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002426}
2427
françois romieu4da19632011-01-03 15:07:55 +00002428static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002429{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002430 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002431 { 0x1f, 0x0001 },
2432 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002433 { 0x1f, 0x0002 },
2434 { 0x00, 0x88d4 },
2435 { 0x01, 0x82b1 },
2436 { 0x03, 0x7002 },
2437 { 0x08, 0x9e30 },
2438 { 0x09, 0x01f0 },
2439 { 0x0a, 0x5500 },
2440 { 0x0c, 0x00c8 },
2441 { 0x1f, 0x0003 },
2442 { 0x12, 0xc096 },
2443 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002444 { 0x1f, 0x0000 },
2445 { 0x1f, 0x0000 },
2446 { 0x09, 0x2000 },
2447 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002448 };
2449
françois romieu4da19632011-01-03 15:07:55 +00002450 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002451
françois romieu4da19632011-01-03 15:07:55 +00002452 rtl_patchphy(tp, 0x14, 1 << 5);
2453 rtl_patchphy(tp, 0x0d, 1 << 5);
2454 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002455}
2456
françois romieu4da19632011-01-03 15:07:55 +00002457static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002458{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002459 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002460 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002461 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002462 { 0x03, 0x802f },
2463 { 0x02, 0x4f02 },
2464 { 0x01, 0x0409 },
2465 { 0x00, 0xf099 },
2466 { 0x04, 0x9800 },
2467 { 0x04, 0x9000 },
2468 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002469 { 0x1f, 0x0002 },
2470 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002471 { 0x06, 0x0761 },
2472 { 0x1f, 0x0003 },
2473 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002474 { 0x1f, 0x0000 }
2475 };
2476
françois romieu4da19632011-01-03 15:07:55 +00002477 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_patchphy(tp, 0x16, 1 << 0);
2480 rtl_patchphy(tp, 0x14, 1 << 5);
2481 rtl_patchphy(tp, 0x0d, 1 << 5);
2482 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002483}
2484
françois romieu4da19632011-01-03 15:07:55 +00002485static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002486{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002487 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002488 { 0x1f, 0x0001 },
2489 { 0x12, 0x2300 },
2490 { 0x1d, 0x3d98 },
2491 { 0x1f, 0x0002 },
2492 { 0x0c, 0x7eb8 },
2493 { 0x06, 0x5461 },
2494 { 0x1f, 0x0003 },
2495 { 0x16, 0x0f0a },
2496 { 0x1f, 0x0000 }
2497 };
2498
françois romieu4da19632011-01-03 15:07:55 +00002499 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002500
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl_patchphy(tp, 0x16, 1 << 0);
2502 rtl_patchphy(tp, 0x14, 1 << 5);
2503 rtl_patchphy(tp, 0x0d, 1 << 5);
2504 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002505}
2506
françois romieu4da19632011-01-03 15:07:55 +00002507static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002508{
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002510}
2511
françois romieubca03d52011-01-03 15:07:31 +00002512static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002513{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002514 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002515 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002516 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002517 { 0x06, 0x4064 },
2518 { 0x07, 0x2863 },
2519 { 0x08, 0x059c },
2520 { 0x09, 0x26b4 },
2521 { 0x0a, 0x6a19 },
2522 { 0x0b, 0xdcc8 },
2523 { 0x10, 0xf06d },
2524 { 0x14, 0x7f68 },
2525 { 0x18, 0x7fd9 },
2526 { 0x1c, 0xf0ff },
2527 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002528 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002529 { 0x12, 0xf49f },
2530 { 0x13, 0x070b },
2531 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002532 { 0x14, 0x94c0 },
2533
2534 /*
2535 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002536 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002537 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002538 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002539 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002540 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002541 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002542 { 0x06, 0x5561 },
2543
2544 /*
2545 * Can not link to 1Gbps with bad cable
2546 * Decrease SNR threshold form 21.07dB to 19.04dB
2547 */
2548 { 0x1f, 0x0001 },
2549 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002550
2551 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002552 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002553 };
françois romieubca03d52011-01-03 15:07:31 +00002554 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002555
françois romieu4da19632011-01-03 15:07:55 +00002556 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002557
françois romieubca03d52011-01-03 15:07:31 +00002558 /*
2559 * Rx Error Issue
2560 * Fine Tune Switching regulator parameter
2561 */
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl_writephy(tp, 0x1f, 0x0002);
2563 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2564 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002565
françois romieudaf9df62009-10-07 12:44:20 +00002566 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002567 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002568 { 0x1f, 0x0002 },
2569 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002570 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002571 { 0x05, 0x8330 },
2572 { 0x06, 0x669a },
2573 { 0x1f, 0x0002 }
2574 };
2575 int val;
2576
françois romieu4da19632011-01-03 15:07:55 +00002577 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002578
françois romieu4da19632011-01-03 15:07:55 +00002579 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002580
2581 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002582 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002583 0x0065, 0x0066, 0x0067, 0x0068,
2584 0x0069, 0x006a, 0x006b, 0x006c
2585 };
2586 int i;
2587
françois romieu4da19632011-01-03 15:07:55 +00002588 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002589
2590 val &= 0xff00;
2591 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002592 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002593 }
2594 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002595 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002596 { 0x1f, 0x0002 },
2597 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002598 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002599 { 0x05, 0x8330 },
2600 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002601 };
2602
françois romieu4da19632011-01-03 15:07:55 +00002603 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002604 }
2605
françois romieubca03d52011-01-03 15:07:31 +00002606 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002607 rtl_writephy(tp, 0x1f, 0x0002);
2608 rtl_patchphy(tp, 0x0d, 0x0300);
2609 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002610
françois romieubca03d52011-01-03 15:07:31 +00002611 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002612 rtl_writephy(tp, 0x1f, 0x0002);
2613 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2614 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002615
françois romieu4da19632011-01-03 15:07:55 +00002616 rtl_writephy(tp, 0x1f, 0x0005);
2617 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002618
2619 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002620
françois romieu4da19632011-01-03 15:07:55 +00002621 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002622}
2623
françois romieubca03d52011-01-03 15:07:31 +00002624static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002625{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002626 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002627 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002628 { 0x1f, 0x0001 },
2629 { 0x06, 0x4064 },
2630 { 0x07, 0x2863 },
2631 { 0x08, 0x059c },
2632 { 0x09, 0x26b4 },
2633 { 0x0a, 0x6a19 },
2634 { 0x0b, 0xdcc8 },
2635 { 0x10, 0xf06d },
2636 { 0x14, 0x7f68 },
2637 { 0x18, 0x7fd9 },
2638 { 0x1c, 0xf0ff },
2639 { 0x1d, 0x3d9c },
2640 { 0x1f, 0x0003 },
2641 { 0x12, 0xf49f },
2642 { 0x13, 0x070b },
2643 { 0x1a, 0x05ad },
2644 { 0x14, 0x94c0 },
2645
françois romieubca03d52011-01-03 15:07:31 +00002646 /*
2647 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002648 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002649 */
françois romieudaf9df62009-10-07 12:44:20 +00002650 { 0x1f, 0x0002 },
2651 { 0x06, 0x5561 },
2652 { 0x1f, 0x0005 },
2653 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002654 { 0x06, 0x5561 },
2655
2656 /*
2657 * Can not link to 1Gbps with bad cable
2658 * Decrease SNR threshold form 21.07dB to 19.04dB
2659 */
2660 { 0x1f, 0x0001 },
2661 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002662
2663 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002664 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002665 };
françois romieubca03d52011-01-03 15:07:31 +00002666 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002667
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002669
2670 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002671 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002672 { 0x1f, 0x0002 },
2673 { 0x05, 0x669a },
2674 { 0x1f, 0x0005 },
2675 { 0x05, 0x8330 },
2676 { 0x06, 0x669a },
2677
2678 { 0x1f, 0x0002 }
2679 };
2680 int val;
2681
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002683
françois romieu4da19632011-01-03 15:07:55 +00002684 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002685 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002686 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002687 0x0065, 0x0066, 0x0067, 0x0068,
2688 0x0069, 0x006a, 0x006b, 0x006c
2689 };
2690 int i;
2691
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002693
2694 val &= 0xff00;
2695 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002697 }
2698 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002699 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002700 { 0x1f, 0x0002 },
2701 { 0x05, 0x2642 },
2702 { 0x1f, 0x0005 },
2703 { 0x05, 0x8330 },
2704 { 0x06, 0x2642 }
2705 };
2706
françois romieu4da19632011-01-03 15:07:55 +00002707 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002708 }
2709
françois romieubca03d52011-01-03 15:07:31 +00002710 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002711 rtl_writephy(tp, 0x1f, 0x0002);
2712 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2713 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002714
françois romieubca03d52011-01-03 15:07:31 +00002715 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002716 rtl_writephy(tp, 0x1f, 0x0002);
2717 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002718
françois romieu4da19632011-01-03 15:07:55 +00002719 rtl_writephy(tp, 0x1f, 0x0005);
2720 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002721
2722 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002723
françois romieu4da19632011-01-03 15:07:55 +00002724 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002725}
2726
françois romieu4da19632011-01-03 15:07:55 +00002727static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002728{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002729 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002730 { 0x1f, 0x0002 },
2731 { 0x10, 0x0008 },
2732 { 0x0d, 0x006c },
2733
2734 { 0x1f, 0x0000 },
2735 { 0x0d, 0xf880 },
2736
2737 { 0x1f, 0x0001 },
2738 { 0x17, 0x0cc0 },
2739
2740 { 0x1f, 0x0001 },
2741 { 0x0b, 0xa4d8 },
2742 { 0x09, 0x281c },
2743 { 0x07, 0x2883 },
2744 { 0x0a, 0x6b35 },
2745 { 0x1d, 0x3da4 },
2746 { 0x1c, 0xeffd },
2747 { 0x14, 0x7f52 },
2748 { 0x18, 0x7fc6 },
2749 { 0x08, 0x0601 },
2750 { 0x06, 0x4063 },
2751 { 0x10, 0xf074 },
2752 { 0x1f, 0x0003 },
2753 { 0x13, 0x0789 },
2754 { 0x12, 0xf4bd },
2755 { 0x1a, 0x04fd },
2756 { 0x14, 0x84b0 },
2757 { 0x1f, 0x0000 },
2758 { 0x00, 0x9200 },
2759
2760 { 0x1f, 0x0005 },
2761 { 0x01, 0x0340 },
2762 { 0x1f, 0x0001 },
2763 { 0x04, 0x4000 },
2764 { 0x03, 0x1d21 },
2765 { 0x02, 0x0c32 },
2766 { 0x01, 0x0200 },
2767 { 0x00, 0x5554 },
2768 { 0x04, 0x4800 },
2769 { 0x04, 0x4000 },
2770 { 0x04, 0xf000 },
2771 { 0x03, 0xdf01 },
2772 { 0x02, 0xdf20 },
2773 { 0x01, 0x101a },
2774 { 0x00, 0xa0ff },
2775 { 0x04, 0xf800 },
2776 { 0x04, 0xf000 },
2777 { 0x1f, 0x0000 },
2778
2779 { 0x1f, 0x0007 },
2780 { 0x1e, 0x0023 },
2781 { 0x16, 0x0000 },
2782 { 0x1f, 0x0000 }
2783 };
2784
françois romieu4da19632011-01-03 15:07:55 +00002785 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002786}
2787
françois romieue6de30d2011-01-03 15:08:37 +00002788static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2789{
2790 static const struct phy_reg phy_reg_init[] = {
2791 { 0x1f, 0x0001 },
2792 { 0x17, 0x0cc0 },
2793
2794 { 0x1f, 0x0007 },
2795 { 0x1e, 0x002d },
2796 { 0x18, 0x0040 },
2797 { 0x1f, 0x0000 }
2798 };
2799
2800 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2801 rtl_patchphy(tp, 0x0d, 1 << 5);
2802}
2803
Hayes Wang70090422011-07-06 15:58:06 +08002804static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002805{
2806 static const struct phy_reg phy_reg_init[] = {
2807 /* Enable Delay cap */
2808 { 0x1f, 0x0005 },
2809 { 0x05, 0x8b80 },
2810 { 0x06, 0xc896 },
2811 { 0x1f, 0x0000 },
2812
2813 /* Channel estimation fine tune */
2814 { 0x1f, 0x0001 },
2815 { 0x0b, 0x6c20 },
2816 { 0x07, 0x2872 },
2817 { 0x1c, 0xefff },
2818 { 0x1f, 0x0003 },
2819 { 0x14, 0x6420 },
2820 { 0x1f, 0x0000 },
2821
2822 /* Update PFM & 10M TX idle timer */
2823 { 0x1f, 0x0007 },
2824 { 0x1e, 0x002f },
2825 { 0x15, 0x1919 },
2826 { 0x1f, 0x0000 },
2827
2828 { 0x1f, 0x0007 },
2829 { 0x1e, 0x00ac },
2830 { 0x18, 0x0006 },
2831 { 0x1f, 0x0000 }
2832 };
2833
Francois Romieu15ecd032011-04-27 13:52:22 -07002834 rtl_apply_firmware(tp);
2835
hayeswang01dc7fe2011-03-21 01:50:28 +00002836 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2837
2838 /* DCO enable for 10M IDLE Power */
2839 rtl_writephy(tp, 0x1f, 0x0007);
2840 rtl_writephy(tp, 0x1e, 0x0023);
2841 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2842 rtl_writephy(tp, 0x1f, 0x0000);
2843
2844 /* For impedance matching */
2845 rtl_writephy(tp, 0x1f, 0x0002);
2846 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002847 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002848
2849 /* PHY auto speed down */
2850 rtl_writephy(tp, 0x1f, 0x0007);
2851 rtl_writephy(tp, 0x1e, 0x002d);
2852 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2853 rtl_writephy(tp, 0x1f, 0x0000);
2854 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2855
2856 rtl_writephy(tp, 0x1f, 0x0005);
2857 rtl_writephy(tp, 0x05, 0x8b86);
2858 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2859 rtl_writephy(tp, 0x1f, 0x0000);
2860
2861 rtl_writephy(tp, 0x1f, 0x0005);
2862 rtl_writephy(tp, 0x05, 0x8b85);
2863 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2864 rtl_writephy(tp, 0x1f, 0x0007);
2865 rtl_writephy(tp, 0x1e, 0x0020);
2866 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2867 rtl_writephy(tp, 0x1f, 0x0006);
2868 rtl_writephy(tp, 0x00, 0x5a00);
2869 rtl_writephy(tp, 0x1f, 0x0000);
2870 rtl_writephy(tp, 0x0d, 0x0007);
2871 rtl_writephy(tp, 0x0e, 0x003c);
2872 rtl_writephy(tp, 0x0d, 0x4007);
2873 rtl_writephy(tp, 0x0e, 0x0000);
2874 rtl_writephy(tp, 0x0d, 0x0000);
2875}
2876
Hayes Wang70090422011-07-06 15:58:06 +08002877static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2878{
2879 static const struct phy_reg phy_reg_init[] = {
2880 /* Enable Delay cap */
2881 { 0x1f, 0x0004 },
2882 { 0x1f, 0x0007 },
2883 { 0x1e, 0x00ac },
2884 { 0x18, 0x0006 },
2885 { 0x1f, 0x0002 },
2886 { 0x1f, 0x0000 },
2887 { 0x1f, 0x0000 },
2888
2889 /* Channel estimation fine tune */
2890 { 0x1f, 0x0003 },
2891 { 0x09, 0xa20f },
2892 { 0x1f, 0x0000 },
2893 { 0x1f, 0x0000 },
2894
2895 /* Green Setting */
2896 { 0x1f, 0x0005 },
2897 { 0x05, 0x8b5b },
2898 { 0x06, 0x9222 },
2899 { 0x05, 0x8b6d },
2900 { 0x06, 0x8000 },
2901 { 0x05, 0x8b76 },
2902 { 0x06, 0x8000 },
2903 { 0x1f, 0x0000 }
2904 };
2905
2906 rtl_apply_firmware(tp);
2907
2908 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2909
2910 /* For 4-corner performance improve */
2911 rtl_writephy(tp, 0x1f, 0x0005);
2912 rtl_writephy(tp, 0x05, 0x8b80);
2913 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2914 rtl_writephy(tp, 0x1f, 0x0000);
2915
2916 /* PHY auto speed down */
2917 rtl_writephy(tp, 0x1f, 0x0004);
2918 rtl_writephy(tp, 0x1f, 0x0007);
2919 rtl_writephy(tp, 0x1e, 0x002d);
2920 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2921 rtl_writephy(tp, 0x1f, 0x0002);
2922 rtl_writephy(tp, 0x1f, 0x0000);
2923 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2924
2925 /* improve 10M EEE waveform */
2926 rtl_writephy(tp, 0x1f, 0x0005);
2927 rtl_writephy(tp, 0x05, 0x8b86);
2928 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2929 rtl_writephy(tp, 0x1f, 0x0000);
2930
2931 /* Improve 2-pair detection performance */
2932 rtl_writephy(tp, 0x1f, 0x0005);
2933 rtl_writephy(tp, 0x05, 0x8b85);
2934 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2935 rtl_writephy(tp, 0x1f, 0x0000);
2936
2937 /* EEE setting */
2938 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2939 ERIAR_EXGMAC);
2940 rtl_writephy(tp, 0x1f, 0x0005);
2941 rtl_writephy(tp, 0x05, 0x8b85);
2942 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2943 rtl_writephy(tp, 0x1f, 0x0004);
2944 rtl_writephy(tp, 0x1f, 0x0007);
2945 rtl_writephy(tp, 0x1e, 0x0020);
David S. Miller1805b2f2011-10-24 18:18:09 -04002946 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wang70090422011-07-06 15:58:06 +08002947 rtl_writephy(tp, 0x1f, 0x0002);
2948 rtl_writephy(tp, 0x1f, 0x0000);
2949 rtl_writephy(tp, 0x0d, 0x0007);
2950 rtl_writephy(tp, 0x0e, 0x003c);
2951 rtl_writephy(tp, 0x0d, 0x4007);
2952 rtl_writephy(tp, 0x0e, 0x0000);
2953 rtl_writephy(tp, 0x0d, 0x0000);
2954
2955 /* Green feature */
2956 rtl_writephy(tp, 0x1f, 0x0003);
2957 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2958 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2959 rtl_writephy(tp, 0x1f, 0x0000);
2960}
2961
Hayes Wangc2218922011-09-06 16:55:18 +08002962static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2963{
2964 static const struct phy_reg phy_reg_init[] = {
2965 /* Channel estimation fine tune */
2966 { 0x1f, 0x0003 },
2967 { 0x09, 0xa20f },
2968 { 0x1f, 0x0000 },
2969
2970 /* Modify green table for giga & fnet */
2971 { 0x1f, 0x0005 },
2972 { 0x05, 0x8b55 },
2973 { 0x06, 0x0000 },
2974 { 0x05, 0x8b5e },
2975 { 0x06, 0x0000 },
2976 { 0x05, 0x8b67 },
2977 { 0x06, 0x0000 },
2978 { 0x05, 0x8b70 },
2979 { 0x06, 0x0000 },
2980 { 0x1f, 0x0000 },
2981 { 0x1f, 0x0007 },
2982 { 0x1e, 0x0078 },
2983 { 0x17, 0x0000 },
2984 { 0x19, 0x00fb },
2985 { 0x1f, 0x0000 },
2986
2987 /* Modify green table for 10M */
2988 { 0x1f, 0x0005 },
2989 { 0x05, 0x8b79 },
2990 { 0x06, 0xaa00 },
2991 { 0x1f, 0x0000 },
2992
2993 /* Disable hiimpedance detection (RTCT) */
2994 { 0x1f, 0x0003 },
2995 { 0x01, 0x328a },
2996 { 0x1f, 0x0000 }
2997 };
2998
2999 rtl_apply_firmware(tp);
3000
3001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3002
3003 /* For 4-corner performance improve */
3004 rtl_writephy(tp, 0x1f, 0x0005);
3005 rtl_writephy(tp, 0x05, 0x8b80);
3006 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3007 rtl_writephy(tp, 0x1f, 0x0000);
3008
3009 /* PHY auto speed down */
3010 rtl_writephy(tp, 0x1f, 0x0007);
3011 rtl_writephy(tp, 0x1e, 0x002d);
3012 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3013 rtl_writephy(tp, 0x1f, 0x0000);
3014 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3015
3016 /* Improve 10M EEE waveform */
3017 rtl_writephy(tp, 0x1f, 0x0005);
3018 rtl_writephy(tp, 0x05, 0x8b86);
3019 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021
3022 /* Improve 2-pair detection performance */
3023 rtl_writephy(tp, 0x1f, 0x0005);
3024 rtl_writephy(tp, 0x05, 0x8b85);
3025 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
3026 rtl_writephy(tp, 0x1f, 0x0000);
3027}
3028
3029static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3030{
3031 rtl_apply_firmware(tp);
3032
3033 /* For 4-corner performance improve */
3034 rtl_writephy(tp, 0x1f, 0x0005);
3035 rtl_writephy(tp, 0x05, 0x8b80);
3036 rtl_w1w0_phy(tp, 0x06, 0x0006, 0x0000);
3037 rtl_writephy(tp, 0x1f, 0x0000);
3038
3039 /* PHY auto speed down */
3040 rtl_writephy(tp, 0x1f, 0x0007);
3041 rtl_writephy(tp, 0x1e, 0x002d);
3042 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
3043 rtl_writephy(tp, 0x1f, 0x0000);
3044 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
3045
3046 /* Improve 10M EEE waveform */
3047 rtl_writephy(tp, 0x1f, 0x0005);
3048 rtl_writephy(tp, 0x05, 0x8b86);
3049 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
3050 rtl_writephy(tp, 0x1f, 0x0000);
3051}
3052
françois romieu4da19632011-01-03 15:07:55 +00003053static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003054{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003055 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003056 { 0x1f, 0x0003 },
3057 { 0x08, 0x441d },
3058 { 0x01, 0x9100 },
3059 { 0x1f, 0x0000 }
3060 };
3061
françois romieu4da19632011-01-03 15:07:55 +00003062 rtl_writephy(tp, 0x1f, 0x0000);
3063 rtl_patchphy(tp, 0x11, 1 << 12);
3064 rtl_patchphy(tp, 0x19, 1 << 13);
3065 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003066
françois romieu4da19632011-01-03 15:07:55 +00003067 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003068}
3069
Hayes Wang5a5e4442011-02-22 17:26:21 +08003070static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3071{
3072 static const struct phy_reg phy_reg_init[] = {
3073 { 0x1f, 0x0005 },
3074 { 0x1a, 0x0000 },
3075 { 0x1f, 0x0000 },
3076
3077 { 0x1f, 0x0004 },
3078 { 0x1c, 0x0000 },
3079 { 0x1f, 0x0000 },
3080
3081 { 0x1f, 0x0001 },
3082 { 0x15, 0x7701 },
3083 { 0x1f, 0x0000 }
3084 };
3085
3086 /* Disable ALDPS before ram code */
3087 rtl_writephy(tp, 0x1f, 0x0000);
3088 rtl_writephy(tp, 0x18, 0x0310);
3089 msleep(100);
3090
François Romieu953a12c2011-04-24 17:38:48 +02003091 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003092
3093 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3094}
3095
Francois Romieu5615d9f2007-08-17 17:50:46 +02003096static void rtl_hw_phy_config(struct net_device *dev)
3097{
3098 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003099
3100 rtl8169_print_mac_version(tp);
3101
3102 switch (tp->mac_version) {
3103 case RTL_GIGA_MAC_VER_01:
3104 break;
3105 case RTL_GIGA_MAC_VER_02:
3106 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003107 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003108 break;
3109 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003111 break;
françois romieu2e9558562009-08-10 19:44:19 +00003112 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003113 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003114 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003115 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003116 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003117 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003118 case RTL_GIGA_MAC_VER_07:
3119 case RTL_GIGA_MAC_VER_08:
3120 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003121 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003122 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003123 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003124 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003125 break;
3126 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003127 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003128 break;
3129 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003130 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003131 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003132 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003133 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003134 break;
3135 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003136 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003137 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003138 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003139 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003140 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003141 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003142 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003143 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003144 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003145 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003146 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003147 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003148 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003149 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003150 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003151 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003152 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003153 break;
3154 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003155 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003156 break;
3157 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003158 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003159 break;
françois romieue6de30d2011-01-03 15:08:37 +00003160 case RTL_GIGA_MAC_VER_28:
3161 rtl8168d_4_hw_phy_config(tp);
3162 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003163 case RTL_GIGA_MAC_VER_29:
3164 case RTL_GIGA_MAC_VER_30:
3165 rtl8105e_hw_phy_config(tp);
3166 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003167 case RTL_GIGA_MAC_VER_31:
3168 /* None. */
3169 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003170 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003171 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003172 rtl8168e_1_hw_phy_config(tp);
3173 break;
3174 case RTL_GIGA_MAC_VER_34:
3175 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003176 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003177 case RTL_GIGA_MAC_VER_35:
3178 rtl8168f_1_hw_phy_config(tp);
3179 break;
3180 case RTL_GIGA_MAC_VER_36:
3181 rtl8168f_2_hw_phy_config(tp);
3182 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003183
Francois Romieu5615d9f2007-08-17 17:50:46 +02003184 default:
3185 break;
3186 }
3187}
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189static void rtl8169_phy_timer(unsigned long __opaque)
3190{
3191 struct net_device *dev = (struct net_device *)__opaque;
3192 struct rtl8169_private *tp = netdev_priv(dev);
3193 struct timer_list *timer = &tp->timer;
3194 void __iomem *ioaddr = tp->mmio_addr;
3195 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3196
Francois Romieubcf0bf92006-07-26 23:14:13 +02003197 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 spin_lock_irq(&tp->lock);
3200
françois romieu4da19632011-01-03 15:07:55 +00003201 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003202 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 * A busy loop could burn quite a few cycles on nowadays CPU.
3204 * Let's delay the execution of the timer for a few ticks.
3205 */
3206 timeout = HZ/10;
3207 goto out_mod_timer;
3208 }
3209
3210 if (tp->link_ok(ioaddr))
3211 goto out_unlock;
3212
Joe Perchesbf82c182010-02-09 11:49:50 +00003213 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
françois romieu4da19632011-01-03 15:07:55 +00003215 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217out_mod_timer:
3218 mod_timer(timer, jiffies + timeout);
3219out_unlock:
3220 spin_unlock_irq(&tp->lock);
3221}
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223#ifdef CONFIG_NET_POLL_CONTROLLER
3224/*
3225 * Polling 'interrupt' - used by things like netconsole to send skbs
3226 * without having to re-enable interrupts. It's not called while
3227 * the interrupt routine is executing.
3228 */
3229static void rtl8169_netpoll(struct net_device *dev)
3230{
3231 struct rtl8169_private *tp = netdev_priv(dev);
3232 struct pci_dev *pdev = tp->pci_dev;
3233
3234 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003235 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 enable_irq(pdev->irq);
3237}
3238#endif
3239
3240static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3241 void __iomem *ioaddr)
3242{
3243 iounmap(ioaddr);
3244 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003245 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 pci_disable_device(pdev);
3247 free_netdev(dev);
3248}
3249
Francois Romieubf793292006-11-01 00:53:05 +01003250static void rtl8169_phy_reset(struct net_device *dev,
3251 struct rtl8169_private *tp)
3252{
Francois Romieu07d3f512007-02-21 22:40:46 +01003253 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003254
françois romieu4da19632011-01-03 15:07:55 +00003255 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003256 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003257 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003258 return;
3259 msleep(1);
3260 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003261 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003262}
3263
David S. Miller8decf862011-09-22 03:23:13 -04003264static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3265{
3266 void __iomem *ioaddr = tp->mmio_addr;
3267
3268 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3269 (RTL_R8(PHYstatus) & TBI_Enable);
3270}
3271
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003272static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003274 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003275
Francois Romieu5615d9f2007-08-17 17:50:46 +02003276 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003277
Marcus Sundberg773328942008-07-10 21:28:08 +02003278 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3279 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3280 RTL_W8(0x82, 0x01);
3281 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003282
Francois Romieu6dccd162007-02-13 23:38:05 +01003283 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3284
3285 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3286 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003287
Francois Romieubcf0bf92006-07-26 23:14:13 +02003288 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003289 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3290 RTL_W8(0x82, 0x01);
3291 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003292 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003293 }
3294
Francois Romieubf793292006-11-01 00:53:05 +01003295 rtl8169_phy_reset(dev, tp);
3296
Oliver Neukum54405cd2011-01-06 21:55:13 +01003297 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003298 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3299 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3300 (tp->mii.supports_gmii ?
3301 ADVERTISED_1000baseT_Half |
3302 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003303
David S. Miller8decf862011-09-22 03:23:13 -04003304 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003305 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003306}
3307
Francois Romieu773d2022007-01-31 23:47:43 +01003308static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3309{
3310 void __iomem *ioaddr = tp->mmio_addr;
3311 u32 high;
3312 u32 low;
3313
3314 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3315 high = addr[4] | (addr[5] << 8);
3316
3317 spin_lock_irq(&tp->lock);
3318
3319 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003320
Francois Romieu773d2022007-01-31 23:47:43 +01003321 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003322 RTL_R32(MAC4);
3323
Francois Romieu78f1cd02010-03-27 19:35:46 -07003324 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003325 RTL_R32(MAC0);
3326
françois romieuc28aa382011-08-02 03:53:43 +00003327 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3328 const struct exgmac_reg e[] = {
3329 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3330 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3331 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3332 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3333 low >> 16 },
3334 };
3335
3336 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3337 }
3338
Francois Romieu773d2022007-01-31 23:47:43 +01003339 RTL_W8(Cfg9346, Cfg9346_Lock);
3340
3341 spin_unlock_irq(&tp->lock);
3342}
3343
3344static int rtl_set_mac_address(struct net_device *dev, void *p)
3345{
3346 struct rtl8169_private *tp = netdev_priv(dev);
3347 struct sockaddr *addr = p;
3348
3349 if (!is_valid_ether_addr(addr->sa_data))
3350 return -EADDRNOTAVAIL;
3351
3352 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3353
3354 rtl_rar_set(tp, dev->dev_addr);
3355
3356 return 0;
3357}
3358
Francois Romieu5f787a12006-08-17 13:02:36 +02003359static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3360{
3361 struct rtl8169_private *tp = netdev_priv(dev);
3362 struct mii_ioctl_data *data = if_mii(ifr);
3363
Francois Romieu8b4ab282008-11-19 22:05:25 -08003364 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3365}
Francois Romieu5f787a12006-08-17 13:02:36 +02003366
Francois Romieucecb5fd2011-04-01 10:21:07 +02003367static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3368 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003369{
Francois Romieu5f787a12006-08-17 13:02:36 +02003370 switch (cmd) {
3371 case SIOCGMIIPHY:
3372 data->phy_id = 32; /* Internal PHY */
3373 return 0;
3374
3375 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003376 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003377 return 0;
3378
3379 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003380 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003381 return 0;
3382 }
3383 return -EOPNOTSUPP;
3384}
3385
Francois Romieu8b4ab282008-11-19 22:05:25 -08003386static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3387{
3388 return -EOPNOTSUPP;
3389}
3390
Francois Romieu0e485152007-02-20 00:00:26 +01003391static const struct rtl_cfg_info {
3392 void (*hw_start)(struct net_device *);
3393 unsigned int region;
3394 unsigned int align;
3395 u16 intr_event;
3396 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003397 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003398 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003399} rtl_cfg_infos [] = {
3400 [RTL_CFG_0] = {
3401 .hw_start = rtl_hw_start_8169,
3402 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003403 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003404 .intr_event = SYSErr | LinkChg | RxOverflow |
3405 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003406 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003407 .features = RTL_FEATURE_GMII,
3408 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003409 },
3410 [RTL_CFG_1] = {
3411 .hw_start = rtl_hw_start_8168,
3412 .region = 2,
3413 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003414 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003415 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003416 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003417 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3418 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003419 },
3420 [RTL_CFG_2] = {
3421 .hw_start = rtl_hw_start_8101,
3422 .region = 2,
3423 .align = 8,
3424 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3425 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003426 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003427 .features = RTL_FEATURE_MSI,
3428 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003429 }
3430};
3431
Francois Romieufbac58f2007-10-04 22:51:38 +02003432/* Cfg9346_Unlock assumed. */
françois romieu2ca6cf02011-12-15 08:37:43 +00003433static unsigned rtl_try_msi(struct rtl8169_private *tp,
Francois Romieufbac58f2007-10-04 22:51:38 +02003434 const struct rtl_cfg_info *cfg)
3435{
françois romieu2ca6cf02011-12-15 08:37:43 +00003436 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieufbac58f2007-10-04 22:51:38 +02003437 unsigned msi = 0;
3438 u8 cfg2;
3439
3440 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003441 if (cfg->features & RTL_FEATURE_MSI) {
françois romieu2ca6cf02011-12-15 08:37:43 +00003442 if (pci_enable_msi(tp->pci_dev)) {
3443 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
Francois Romieufbac58f2007-10-04 22:51:38 +02003444 } else {
3445 cfg2 |= MSIEnable;
3446 msi = RTL_FEATURE_MSI;
3447 }
3448 }
françois romieu2ca6cf02011-12-15 08:37:43 +00003449 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3450 RTL_W8(Config2, cfg2);
Francois Romieufbac58f2007-10-04 22:51:38 +02003451 return msi;
3452}
3453
3454static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3455{
3456 if (tp->features & RTL_FEATURE_MSI) {
3457 pci_disable_msi(pdev);
3458 tp->features &= ~RTL_FEATURE_MSI;
3459 }
3460}
3461
Francois Romieu8b4ab282008-11-19 22:05:25 -08003462static const struct net_device_ops rtl8169_netdev_ops = {
3463 .ndo_open = rtl8169_open,
3464 .ndo_stop = rtl8169_close,
3465 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003466 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003467 .ndo_tx_timeout = rtl8169_tx_timeout,
3468 .ndo_validate_addr = eth_validate_addr,
3469 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003470 .ndo_fix_features = rtl8169_fix_features,
3471 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003472 .ndo_set_mac_address = rtl_set_mac_address,
3473 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003474 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003475#ifdef CONFIG_NET_POLL_CONTROLLER
3476 .ndo_poll_controller = rtl8169_netpoll,
3477#endif
3478
3479};
3480
françois romieuc0e45c12011-01-03 15:08:04 +00003481static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3482{
3483 struct mdio_ops *ops = &tp->mdio_ops;
3484
3485 switch (tp->mac_version) {
3486 case RTL_GIGA_MAC_VER_27:
3487 ops->write = r8168dp_1_mdio_write;
3488 ops->read = r8168dp_1_mdio_read;
3489 break;
françois romieue6de30d2011-01-03 15:08:37 +00003490 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003491 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003492 ops->write = r8168dp_2_mdio_write;
3493 ops->read = r8168dp_2_mdio_read;
3494 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003495 default:
3496 ops->write = r8169_mdio_write;
3497 ops->read = r8169_mdio_read;
3498 break;
3499 }
3500}
3501
David S. Miller1805b2f2011-10-24 18:18:09 -04003502static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3503{
3504 void __iomem *ioaddr = tp->mmio_addr;
3505
3506 switch (tp->mac_version) {
3507 case RTL_GIGA_MAC_VER_29:
3508 case RTL_GIGA_MAC_VER_30:
3509 case RTL_GIGA_MAC_VER_32:
3510 case RTL_GIGA_MAC_VER_33:
3511 case RTL_GIGA_MAC_VER_34:
3512 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3513 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3514 break;
3515 default:
3516 break;
3517 }
3518}
3519
3520static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
3521{
3522 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
3523 return false;
3524
3525 rtl_writephy(tp, 0x1f, 0x0000);
3526 rtl_writephy(tp, MII_BMCR, 0x0000);
3527
3528 rtl_wol_suspend_quirk(tp);
3529
3530 return true;
3531}
3532
françois romieu065c27c2011-01-03 15:08:12 +00003533static void r810x_phy_power_down(struct rtl8169_private *tp)
3534{
3535 rtl_writephy(tp, 0x1f, 0x0000);
3536 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3537}
3538
3539static void r810x_phy_power_up(struct rtl8169_private *tp)
3540{
3541 rtl_writephy(tp, 0x1f, 0x0000);
3542 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3543}
3544
3545static void r810x_pll_power_down(struct rtl8169_private *tp)
3546{
David S. Miller1805b2f2011-10-24 18:18:09 -04003547 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003548 return;
françois romieu065c27c2011-01-03 15:08:12 +00003549
3550 r810x_phy_power_down(tp);
3551}
3552
3553static void r810x_pll_power_up(struct rtl8169_private *tp)
3554{
3555 r810x_phy_power_up(tp);
3556}
3557
3558static void r8168_phy_power_up(struct rtl8169_private *tp)
3559{
3560 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003561 switch (tp->mac_version) {
3562 case RTL_GIGA_MAC_VER_11:
3563 case RTL_GIGA_MAC_VER_12:
3564 case RTL_GIGA_MAC_VER_17:
3565 case RTL_GIGA_MAC_VER_18:
3566 case RTL_GIGA_MAC_VER_19:
3567 case RTL_GIGA_MAC_VER_20:
3568 case RTL_GIGA_MAC_VER_21:
3569 case RTL_GIGA_MAC_VER_22:
3570 case RTL_GIGA_MAC_VER_23:
3571 case RTL_GIGA_MAC_VER_24:
3572 case RTL_GIGA_MAC_VER_25:
3573 case RTL_GIGA_MAC_VER_26:
3574 case RTL_GIGA_MAC_VER_27:
3575 case RTL_GIGA_MAC_VER_28:
3576 case RTL_GIGA_MAC_VER_31:
3577 rtl_writephy(tp, 0x0e, 0x0000);
3578 break;
3579 default:
3580 break;
3581 }
françois romieu065c27c2011-01-03 15:08:12 +00003582 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3583}
3584
3585static void r8168_phy_power_down(struct rtl8169_private *tp)
3586{
3587 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003588 switch (tp->mac_version) {
3589 case RTL_GIGA_MAC_VER_32:
3590 case RTL_GIGA_MAC_VER_33:
3591 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3592 break;
3593
3594 case RTL_GIGA_MAC_VER_11:
3595 case RTL_GIGA_MAC_VER_12:
3596 case RTL_GIGA_MAC_VER_17:
3597 case RTL_GIGA_MAC_VER_18:
3598 case RTL_GIGA_MAC_VER_19:
3599 case RTL_GIGA_MAC_VER_20:
3600 case RTL_GIGA_MAC_VER_21:
3601 case RTL_GIGA_MAC_VER_22:
3602 case RTL_GIGA_MAC_VER_23:
3603 case RTL_GIGA_MAC_VER_24:
3604 case RTL_GIGA_MAC_VER_25:
3605 case RTL_GIGA_MAC_VER_26:
3606 case RTL_GIGA_MAC_VER_27:
3607 case RTL_GIGA_MAC_VER_28:
3608 case RTL_GIGA_MAC_VER_31:
3609 rtl_writephy(tp, 0x0e, 0x0200);
3610 default:
3611 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3612 break;
3613 }
françois romieu065c27c2011-01-03 15:08:12 +00003614}
3615
3616static void r8168_pll_power_down(struct rtl8169_private *tp)
3617{
3618 void __iomem *ioaddr = tp->mmio_addr;
3619
Francois Romieucecb5fd2011-04-01 10:21:07 +02003620 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3621 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3622 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003623 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003624 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003625 }
françois romieu065c27c2011-01-03 15:08:12 +00003626
Francois Romieucecb5fd2011-04-01 10:21:07 +02003627 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3628 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003629 (RTL_R16(CPlusCmd) & ASF)) {
3630 return;
3631 }
3632
hayeswang01dc7fe2011-03-21 01:50:28 +00003633 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3634 tp->mac_version == RTL_GIGA_MAC_VER_33)
3635 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3636
David S. Miller1805b2f2011-10-24 18:18:09 -04003637 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003638 return;
françois romieu065c27c2011-01-03 15:08:12 +00003639
3640 r8168_phy_power_down(tp);
3641
3642 switch (tp->mac_version) {
3643 case RTL_GIGA_MAC_VER_25:
3644 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003645 case RTL_GIGA_MAC_VER_27:
3646 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003647 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003648 case RTL_GIGA_MAC_VER_32:
3649 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003650 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3651 break;
3652 }
3653}
3654
3655static void r8168_pll_power_up(struct rtl8169_private *tp)
3656{
3657 void __iomem *ioaddr = tp->mmio_addr;
3658
Francois Romieucecb5fd2011-04-01 10:21:07 +02003659 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3660 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3661 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003662 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003663 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003664 }
françois romieu065c27c2011-01-03 15:08:12 +00003665
3666 switch (tp->mac_version) {
3667 case RTL_GIGA_MAC_VER_25:
3668 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003669 case RTL_GIGA_MAC_VER_27:
3670 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003671 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003672 case RTL_GIGA_MAC_VER_32:
3673 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003674 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3675 break;
3676 }
3677
3678 r8168_phy_power_up(tp);
3679}
3680
Francois Romieud58d46b2011-05-03 16:38:29 +02003681static void rtl_generic_op(struct rtl8169_private *tp,
3682 void (*op)(struct rtl8169_private *))
françois romieu065c27c2011-01-03 15:08:12 +00003683{
3684 if (op)
3685 op(tp);
3686}
3687
3688static void rtl_pll_power_down(struct rtl8169_private *tp)
3689{
Francois Romieud58d46b2011-05-03 16:38:29 +02003690 rtl_generic_op(tp, tp->pll_power_ops.down);
françois romieu065c27c2011-01-03 15:08:12 +00003691}
3692
3693static void rtl_pll_power_up(struct rtl8169_private *tp)
3694{
Francois Romieud58d46b2011-05-03 16:38:29 +02003695 rtl_generic_op(tp, tp->pll_power_ops.up);
françois romieu065c27c2011-01-03 15:08:12 +00003696}
3697
3698static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3699{
3700 struct pll_power_ops *ops = &tp->pll_power_ops;
3701
3702 switch (tp->mac_version) {
3703 case RTL_GIGA_MAC_VER_07:
3704 case RTL_GIGA_MAC_VER_08:
3705 case RTL_GIGA_MAC_VER_09:
3706 case RTL_GIGA_MAC_VER_10:
3707 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003708 case RTL_GIGA_MAC_VER_29:
3709 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003710 ops->down = r810x_pll_power_down;
3711 ops->up = r810x_pll_power_up;
3712 break;
3713
3714 case RTL_GIGA_MAC_VER_11:
3715 case RTL_GIGA_MAC_VER_12:
3716 case RTL_GIGA_MAC_VER_17:
3717 case RTL_GIGA_MAC_VER_18:
3718 case RTL_GIGA_MAC_VER_19:
3719 case RTL_GIGA_MAC_VER_20:
3720 case RTL_GIGA_MAC_VER_21:
3721 case RTL_GIGA_MAC_VER_22:
3722 case RTL_GIGA_MAC_VER_23:
3723 case RTL_GIGA_MAC_VER_24:
3724 case RTL_GIGA_MAC_VER_25:
3725 case RTL_GIGA_MAC_VER_26:
3726 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003727 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003728 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003729 case RTL_GIGA_MAC_VER_32:
3730 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003731 case RTL_GIGA_MAC_VER_34:
Hayes Wangc2218922011-09-06 16:55:18 +08003732 case RTL_GIGA_MAC_VER_35:
3733 case RTL_GIGA_MAC_VER_36:
françois romieu065c27c2011-01-03 15:08:12 +00003734 ops->down = r8168_pll_power_down;
3735 ops->up = r8168_pll_power_up;
3736 break;
3737
3738 default:
3739 ops->down = NULL;
3740 ops->up = NULL;
3741 break;
3742 }
3743}
3744
Hayes Wange542a222011-07-06 15:58:04 +08003745static void rtl_init_rxcfg(struct rtl8169_private *tp)
3746{
3747 void __iomem *ioaddr = tp->mmio_addr;
3748
3749 switch (tp->mac_version) {
3750 case RTL_GIGA_MAC_VER_01:
3751 case RTL_GIGA_MAC_VER_02:
3752 case RTL_GIGA_MAC_VER_03:
3753 case RTL_GIGA_MAC_VER_04:
3754 case RTL_GIGA_MAC_VER_05:
3755 case RTL_GIGA_MAC_VER_06:
3756 case RTL_GIGA_MAC_VER_10:
3757 case RTL_GIGA_MAC_VER_11:
3758 case RTL_GIGA_MAC_VER_12:
3759 case RTL_GIGA_MAC_VER_13:
3760 case RTL_GIGA_MAC_VER_14:
3761 case RTL_GIGA_MAC_VER_15:
3762 case RTL_GIGA_MAC_VER_16:
3763 case RTL_GIGA_MAC_VER_17:
3764 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3765 break;
3766 case RTL_GIGA_MAC_VER_18:
3767 case RTL_GIGA_MAC_VER_19:
3768 case RTL_GIGA_MAC_VER_20:
3769 case RTL_GIGA_MAC_VER_21:
3770 case RTL_GIGA_MAC_VER_22:
3771 case RTL_GIGA_MAC_VER_23:
3772 case RTL_GIGA_MAC_VER_24:
3773 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3774 break;
3775 default:
3776 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3777 break;
3778 }
3779}
3780
Hayes Wang92fc43b2011-07-06 15:58:03 +08003781static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3782{
3783 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3784}
3785
Francois Romieud58d46b2011-05-03 16:38:29 +02003786static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
3787{
3788 rtl_generic_op(tp, tp->jumbo_ops.enable);
3789}
3790
3791static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3792{
3793 rtl_generic_op(tp, tp->jumbo_ops.disable);
3794}
3795
3796static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3797{
3798 void __iomem *ioaddr = tp->mmio_addr;
3799
3800 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3801 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
3802 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
3803}
3804
3805static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3806{
3807 void __iomem *ioaddr = tp->mmio_addr;
3808
3809 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3810 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
3811 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
3812}
3813
3814static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3815{
3816 void __iomem *ioaddr = tp->mmio_addr;
3817
3818 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3819}
3820
3821static void r8168dp_hw_jumbo_disable(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 r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3829{
3830 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003831
3832 RTL_W8(MaxTxPacketSize, 0x3f);
3833 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
3834 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003835 rtl_tx_performance_tweak(tp->pci_dev, 0x2 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003836}
3837
3838static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3839{
3840 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieud58d46b2011-05-03 16:38:29 +02003841
3842 RTL_W8(MaxTxPacketSize, 0x0c);
3843 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
3844 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
Francois Romieu4512ff92011-12-22 18:59:37 +01003845 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieud58d46b2011-05-03 16:38:29 +02003846}
3847
3848static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3849{
3850 rtl_tx_performance_tweak(tp->pci_dev,
3851 (0x2 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3852}
3853
3854static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3855{
3856 rtl_tx_performance_tweak(tp->pci_dev,
3857 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3858}
3859
3860static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3861{
3862 void __iomem *ioaddr = tp->mmio_addr;
3863
3864 r8168b_0_hw_jumbo_enable(tp);
3865
3866 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
3867}
3868
3869static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3870{
3871 void __iomem *ioaddr = tp->mmio_addr;
3872
3873 r8168b_0_hw_jumbo_disable(tp);
3874
3875 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
3876}
3877
3878static void __devinit rtl_init_jumbo_ops(struct rtl8169_private *tp)
3879{
3880 struct jumbo_ops *ops = &tp->jumbo_ops;
3881
3882 switch (tp->mac_version) {
3883 case RTL_GIGA_MAC_VER_11:
3884 ops->disable = r8168b_0_hw_jumbo_disable;
3885 ops->enable = r8168b_0_hw_jumbo_enable;
3886 break;
3887 case RTL_GIGA_MAC_VER_12:
3888 case RTL_GIGA_MAC_VER_17:
3889 ops->disable = r8168b_1_hw_jumbo_disable;
3890 ops->enable = r8168b_1_hw_jumbo_enable;
3891 break;
3892 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
3893 case RTL_GIGA_MAC_VER_19:
3894 case RTL_GIGA_MAC_VER_20:
3895 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
3896 case RTL_GIGA_MAC_VER_22:
3897 case RTL_GIGA_MAC_VER_23:
3898 case RTL_GIGA_MAC_VER_24:
3899 case RTL_GIGA_MAC_VER_25:
3900 case RTL_GIGA_MAC_VER_26:
3901 ops->disable = r8168c_hw_jumbo_disable;
3902 ops->enable = r8168c_hw_jumbo_enable;
3903 break;
3904 case RTL_GIGA_MAC_VER_27:
3905 case RTL_GIGA_MAC_VER_28:
3906 ops->disable = r8168dp_hw_jumbo_disable;
3907 ops->enable = r8168dp_hw_jumbo_enable;
3908 break;
3909 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
3910 case RTL_GIGA_MAC_VER_32:
3911 case RTL_GIGA_MAC_VER_33:
3912 case RTL_GIGA_MAC_VER_34:
3913 ops->disable = r8168e_hw_jumbo_disable;
3914 ops->enable = r8168e_hw_jumbo_enable;
3915 break;
3916
3917 /*
3918 * No action needed for jumbo frames with 8169.
3919 * No jumbo for 810x at all.
3920 */
3921 default:
3922 ops->disable = NULL;
3923 ops->enable = NULL;
3924 break;
3925 }
3926}
3927
Francois Romieu6f43adc2011-04-29 15:05:51 +02003928static void rtl_hw_reset(struct rtl8169_private *tp)
3929{
3930 void __iomem *ioaddr = tp->mmio_addr;
3931 int i;
3932
3933 /* Soft reset the chip. */
3934 RTL_W8(ChipCmd, CmdReset);
3935
3936 /* Check that the chip has finished the reset. */
3937 for (i = 0; i < 100; i++) {
3938 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3939 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003940 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003941 }
3942}
3943
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003944static int __devinit
3945rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3946{
Francois Romieu0e485152007-02-20 00:00:26 +01003947 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3948 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003950 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003951 struct net_device *dev;
3952 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003953 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003954 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003956 if (netif_msg_drv(&debug)) {
3957 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3958 MODULENAME, RTL8169_VERSION);
3959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003962 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003963 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003964 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003965 rc = -ENOMEM;
3966 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 }
3968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003970 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003972 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003973 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003974 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Francois Romieuccdffb92008-07-26 14:26:06 +02003976 mii = &tp->mii;
3977 mii->dev = dev;
3978 mii->mdio_read = rtl_mdio_read;
3979 mii->mdio_write = rtl_mdio_write;
3980 mii->phy_id_mask = 0x1f;
3981 mii->reg_num_mask = 0x1f;
3982 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3983
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003984 /* disable ASPM completely as that cause random device stop working
3985 * problems as well as full system hangs for some PCIe devices users */
3986 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3987 PCIE_LINK_STATE_CLKPM);
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3990 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003991 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003992 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003993 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 }
3995
françois romieu87aeec72010-04-26 11:42:06 +00003996 if (pci_set_mwi(pdev) < 0)
3997 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004000 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004001 netif_err(tp, probe, dev,
4002 "region #%d not an MMIO resource, aborting\n",
4003 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004005 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004009 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004010 netif_err(tp, probe, dev,
4011 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00004013 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 }
4015
4016 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02004017 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004018 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00004019 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 }
4021
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004022 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07004025 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 tp->cp_cmd |= PCIDAC;
4027 dev->features |= NETIF_F_HIGHDMA;
4028 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004029 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004031 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00004032 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
4034 }
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02004037 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004038 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004039 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00004041 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02004043 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Jon Masone44daad2011-06-27 07:46:31 +00004045 if (!pci_is_pcie(pdev))
4046 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07004047
Hayes Wange542a222011-07-06 15:58:04 +08004048 /* Identify chip attached to board */
4049 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
4050
4051 rtl_init_rxcfg(tp);
4052
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004053 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Francois Romieu6f43adc2011-04-29 15:05:51 +02004055 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07004057 RTL_W16(IntrStatus, 0xffff);
4058
françois romieuca52efd2009-07-24 12:34:19 +00004059 pci_set_master(pdev);
4060
Francois Romieu7a8fc772011-03-01 17:18:33 +01004061 /*
4062 * Pretend we are using VLANs; This bypasses a nasty bug where
4063 * Interrupts stop flowing on high load on 8110SCd controllers.
4064 */
4065 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4066 tp->cp_cmd |= RxVlan;
4067
françois romieuc0e45c12011-01-03 15:08:04 +00004068 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004069 rtl_init_pll_power_ops(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004070 rtl_init_jumbo_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00004071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Francois Romieu85bffe62011-04-27 08:22:39 +02004074 chipset = tp->mac_version;
4075 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Francois Romieu5d06a992006-02-23 00:47:58 +01004077 RTL_W8(Cfg9346, Cfg9346_Unlock);
4078 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
4079 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07004080 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
4081 tp->features |= RTL_FEATURE_WOL;
4082 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
4083 tp->features |= RTL_FEATURE_WOL;
françois romieu2ca6cf02011-12-15 08:37:43 +00004084 tp->features |= rtl_try_msi(tp, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01004085 RTL_W8(Cfg9346, Cfg9346_Lock);
4086
David S. Miller8decf862011-09-22 03:23:13 -04004087 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 tp->set_speed = rtl8169_set_speed_tbi;
4089 tp->get_settings = rtl8169_gset_tbi;
4090 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
4091 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
4092 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004093 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 } else {
4095 tp->set_speed = rtl8169_set_speed_xmii;
4096 tp->get_settings = rtl8169_gset_xmii;
4097 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
4098 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
4099 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08004100 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 }
4102
Francois Romieudf58ef512008-10-09 14:35:58 -07004103 spin_lock_init(&tp->lock);
4104
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00004105 /* Get MAC address */
Joe Perches6a3c910c2011-11-16 09:38:02 +00004106 for (i = 0; i < ETH_ALEN; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04004108 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
4112 dev->irq = pdev->irq;
4113 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004115 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Michał Mirosław350fb322011-04-08 06:35:56 +00004117 /* don't enable SG, IP_CSUM and TSO by default - it might not work
4118 * properly for all devices */
4119 dev->features |= NETIF_F_RXCSUM |
4120 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4121
4122 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4123 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
4124 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
4125 NETIF_F_HIGHDMA;
4126
4127 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4128 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
4129 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
4131 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01004132 tp->hw_start = cfg->hw_start;
4133 tp->intr_event = cfg->intr_event;
4134 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
David S. Miller8decf862011-09-22 03:23:13 -04004136 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
4137 ~(RxBOVF | RxFOVF) : ~0;
4138
Francois Romieu2efa53f2007-03-09 00:00:05 +01004139 init_timer(&tp->timer);
4140 tp->timer.data = (unsigned long) dev;
4141 tp->timer.function = rtl8169_phy_timer;
4142
Francois Romieub6ffd972011-06-17 17:00:05 +02004143 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02004144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004146 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00004147 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
4149 pci_set_drvdata(pdev, dev);
4150
Joe Perchesbf82c182010-02-09 11:49:50 +00004151 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02004152 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00004153 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Francois Romieud58d46b2011-05-03 16:38:29 +02004154 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
4155 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
4156 "tx checksumming: %s]\n",
4157 rtl_chip_infos[chipset].jumbo_max,
4158 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Francois Romieucecb5fd2011-04-01 10:21:07 +02004161 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4162 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4163 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004164 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004165 }
françois romieub646d902011-01-03 15:08:21 +00004166
Bruno Prémont8b76ab32008-10-08 17:06:25 -07004167 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Alan Sternf3ec4f82010-06-08 15:23:51 -04004169 if (pci_dev_run_wake(pdev))
4170 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004171
Ivan Vecera0d672e92011-02-15 02:08:39 +00004172 netif_carrier_off(dev);
4173
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004174out:
4175 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
françois romieu87aeec72010-04-26 11:42:06 +00004177err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02004178 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004179 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00004180err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004181 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00004182err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004183 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004184 pci_disable_device(pdev);
4185err_out_free_dev_1:
4186 free_netdev(dev);
4187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Francois Romieu07d3f512007-02-21 22:40:46 +01004190static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
4192 struct net_device *dev = pci_get_drvdata(pdev);
4193 struct rtl8169_private *tp = netdev_priv(dev);
4194
Francois Romieucecb5fd2011-04-01 10:21:07 +02004195 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4196 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4197 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00004198 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004199 }
françois romieub646d902011-01-03 15:08:21 +00004200
Francois Romieu4422bcd2012-01-26 11:23:32 +01004201 cancel_work_sync(&tp->wk.work);
Francois Romieueb2a0212007-02-15 23:37:21 +01004202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08004204
François Romieu953a12c2011-04-24 17:38:48 +02004205 rtl_release_firmware(tp);
4206
Alan Sternf3ec4f82010-06-08 15:23:51 -04004207 if (pci_dev_run_wake(pdev))
4208 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004209
Ivan Veceracc098dc2009-11-29 23:12:52 -08004210 /* restore original MAC address */
4211 rtl_rar_set(tp, dev->perm_addr);
4212
Francois Romieufbac58f2007-10-04 22:51:38 +02004213 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 rtl8169_release_board(pdev, dev, tp->mmio_addr);
4215 pci_set_drvdata(pdev, NULL);
4216}
4217
Francois Romieub6ffd972011-06-17 17:00:05 +02004218static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4219{
4220 struct rtl_fw *rtl_fw;
4221 const char *name;
4222 int rc = -ENOMEM;
4223
4224 name = rtl_lookup_firmware_name(tp);
4225 if (!name)
4226 goto out_no_firmware;
4227
4228 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4229 if (!rtl_fw)
4230 goto err_warn;
4231
4232 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
4233 if (rc < 0)
4234 goto err_free;
4235
Francois Romieufd112f22011-06-18 00:10:29 +02004236 rc = rtl_check_firmware(tp, rtl_fw);
4237 if (rc < 0)
4238 goto err_release_firmware;
4239
Francois Romieub6ffd972011-06-17 17:00:05 +02004240 tp->rtl_fw = rtl_fw;
4241out:
4242 return;
4243
Francois Romieufd112f22011-06-18 00:10:29 +02004244err_release_firmware:
4245 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004246err_free:
4247 kfree(rtl_fw);
4248err_warn:
4249 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4250 name, rc);
4251out_no_firmware:
4252 tp->rtl_fw = NULL;
4253 goto out;
4254}
4255
François Romieu953a12c2011-04-24 17:38:48 +02004256static void rtl_request_firmware(struct rtl8169_private *tp)
4257{
Francois Romieub6ffd972011-06-17 17:00:05 +02004258 if (IS_ERR(tp->rtl_fw))
4259 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004260}
4261
Francois Romieu4422bcd2012-01-26 11:23:32 +01004262static void rtl_task(struct work_struct *);
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264static int rtl8169_open(struct net_device *dev)
4265{
4266 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00004267 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02004269 int retval = -ENOMEM;
4270
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004271 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
Neil Hormanc0cd8842010-03-29 13:16:02 -07004273 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004275 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004277 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
4278 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004280 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004282 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
4283 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02004285 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
4287 retval = rtl8169_init_ring(dev);
4288 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02004289 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Francois Romieu4422bcd2012-01-26 11:23:32 +01004291 INIT_WORK(&tp->wk.work, rtl_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Francois Romieu99f252b2007-04-02 22:59:59 +02004293 smp_mb();
4294
François Romieu953a12c2011-04-24 17:38:48 +02004295 rtl_request_firmware(tp);
4296
Francois Romieufbac58f2007-10-04 22:51:38 +02004297 retval = request_irq(dev->irq, rtl8169_interrupt,
4298 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02004299 dev->name, dev);
4300 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02004301 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02004302
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004303 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004304
françois romieueee3a962011-01-08 02:17:26 +00004305 rtl8169_init_phy(dev, tp);
4306
Michał Mirosław350fb322011-04-08 06:35:56 +00004307 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00004308
françois romieu065c27c2011-01-03 15:08:12 +00004309 rtl_pll_power_up(tp);
4310
Francois Romieu07ce4062007-02-23 23:36:39 +01004311 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004313 tp->saved_wolopts = 0;
4314 pm_runtime_put_noidle(&pdev->dev);
4315
françois romieueee3a962011-01-08 02:17:26 +00004316 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317out:
4318 return retval;
4319
François Romieu953a12c2011-04-24 17:38:48 +02004320err_release_fw_2:
4321 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02004322 rtl8169_rx_clear(tp);
4323err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004324 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4325 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004326 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02004327err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004328 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4329 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004330 tp->TxDescArray = NULL;
4331err_pm_runtime_put:
4332 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 goto out;
4334}
4335
Hayes Wang92fc43b2011-07-06 15:58:03 +08004336static void rtl_rx_close(struct rtl8169_private *tp)
4337{
4338 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004339
Francois Romieu1687b562011-07-19 17:21:29 +02004340 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004341}
4342
françois romieue6de30d2011-01-03 15:08:37 +00004343static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
françois romieue6de30d2011-01-03 15:08:37 +00004345 void __iomem *ioaddr = tp->mmio_addr;
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004348 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Hayes Wang92fc43b2011-07-06 15:58:03 +08004350 rtl_rx_close(tp);
4351
Hayes Wang5d2e1952011-02-22 17:26:22 +08004352 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004353 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4354 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004355 while (RTL_R8(TxPoll) & NPQ)
4356 udelay(20);
Hayes Wangc2218922011-09-06 16:55:18 +08004357 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
4358 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
4359 tp->mac_version == RTL_GIGA_MAC_VER_36) {
David S. Miller8decf862011-09-22 03:23:13 -04004360 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004361 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4362 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004363 } else {
4364 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4365 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004366 }
4367
Hayes Wang92fc43b2011-07-06 15:58:03 +08004368 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370
Francois Romieu7f796d832007-06-11 23:04:41 +02004371static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004372{
4373 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004374
4375 /* Set DMA burst size and Interframe Gap Time */
4376 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4377 (InterFrameGap << TxInterFrameGapShift));
4378}
4379
Francois Romieu07ce4062007-02-23 23:36:39 +01004380static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Francois Romieu07ce4062007-02-23 23:36:39 +01004384 tp->hw_start(dev);
4385
Francois Romieu07ce4062007-02-23 23:36:39 +01004386 netif_start_queue(dev);
4387}
4388
Francois Romieu7f796d832007-06-11 23:04:41 +02004389static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4390 void __iomem *ioaddr)
4391{
4392 /*
4393 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4394 * register to be written before TxDescAddrLow to work.
4395 * Switching from MMIO to I/O access fixes the issue as well.
4396 */
4397 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004398 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004399 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004400 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004401}
4402
4403static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4404{
4405 u16 cmd;
4406
4407 cmd = RTL_R16(CPlusCmd);
4408 RTL_W16(CPlusCmd, cmd);
4409 return cmd;
4410}
4411
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004412static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004413{
4414 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004415 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004416}
4417
Francois Romieu6dccd162007-02-13 23:38:05 +01004418static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4419{
Francois Romieu37441002011-06-17 22:58:54 +02004420 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004421 u32 mac_version;
4422 u32 clk;
4423 u32 val;
4424 } cfg2_info [] = {
4425 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4426 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4427 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4428 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004429 };
4430 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004431 unsigned int i;
4432 u32 clk;
4433
4434 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004435 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004436 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4437 RTL_W32(0x7c, p->val);
4438 break;
4439 }
4440 }
4441}
4442
Francois Romieu07ce4062007-02-23 23:36:39 +01004443static void rtl_hw_start_8169(struct net_device *dev)
4444{
4445 struct rtl8169_private *tp = netdev_priv(dev);
4446 void __iomem *ioaddr = tp->mmio_addr;
4447 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004448
Francois Romieu9cb427b2006-11-02 00:10:16 +01004449 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4450 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4451 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4452 }
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004455 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4456 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4457 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4458 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004459 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4460
Hayes Wange542a222011-07-06 15:58:04 +08004461 rtl_init_rxcfg(tp);
4462
françois romieuf0298f82011-01-03 15:07:42 +00004463 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004465 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Francois Romieucecb5fd2011-04-01 10:21:07 +02004467 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4468 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4469 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4470 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004471 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Francois Romieu7f796d832007-06-11 23:04:41 +02004473 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004474
Francois Romieucecb5fd2011-04-01 10:21:07 +02004475 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4476 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004477 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004479 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 }
4481
Francois Romieubcf0bf92006-07-26 23:14:13 +02004482 RTL_W16(CPlusCmd, tp->cp_cmd);
4483
Francois Romieu6dccd162007-02-13 23:38:05 +01004484 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 /*
4487 * Undocumented corner. Supposedly:
4488 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4489 */
4490 RTL_W16(IntrMitigate, 0x0000);
4491
Francois Romieu7f796d832007-06-11 23:04:41 +02004492 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004493
Francois Romieucecb5fd2011-04-01 10:21:07 +02004494 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4495 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4496 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4497 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004498 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4499 rtl_set_rx_tx_config_registers(tp);
4500 }
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004503
4504 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4505 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507 RTL_W32(RxMissed, 0);
4508
Francois Romieu07ce4062007-02-23 23:36:39 +01004509 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
4511 /* no early-rx interrupts */
4512 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004513
4514 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004515 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
françois romieu650e8d52011-01-03 15:08:29 +00004518static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004519{
4520 u32 csi;
4521
4522 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004523 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4524}
4525
françois romieue6de30d2011-01-03 15:08:37 +00004526static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4527{
4528 rtl_csi_access_enable(ioaddr, 0x17000000);
4529}
4530
françois romieu650e8d52011-01-03 15:08:29 +00004531static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4532{
4533 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004534}
4535
4536struct ephy_info {
4537 unsigned int offset;
4538 u16 mask;
4539 u16 bits;
4540};
4541
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004542static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004543{
4544 u16 w;
4545
4546 while (len-- > 0) {
4547 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4548 rtl_ephy_write(ioaddr, e->offset, w);
4549 e++;
4550 }
4551}
4552
Francois Romieub726e492008-06-28 12:22:59 +02004553static void rtl_disable_clock_request(struct pci_dev *pdev)
4554{
Jon Masone44daad2011-06-27 07:46:31 +00004555 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004556
4557 if (cap) {
4558 u16 ctl;
4559
4560 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4561 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4562 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4563 }
4564}
4565
françois romieue6de30d2011-01-03 15:08:37 +00004566static void rtl_enable_clock_request(struct pci_dev *pdev)
4567{
Jon Masone44daad2011-06-27 07:46:31 +00004568 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004569
4570 if (cap) {
4571 u16 ctl;
4572
4573 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4574 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4575 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4576 }
4577}
4578
Francois Romieub726e492008-06-28 12:22:59 +02004579#define R8168_CPCMD_QUIRK_MASK (\
4580 EnableBist | \
4581 Mac_dbgo_oe | \
4582 Force_half_dup | \
4583 Force_rxflow_en | \
4584 Force_txflow_en | \
4585 Cxpl_dbg_sel | \
4586 ASF | \
4587 PktCntrDisable | \
4588 Mac_dbgo_sel)
4589
Francois Romieu219a1e92008-06-28 11:58:39 +02004590static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4591{
Francois Romieub726e492008-06-28 12:22:59 +02004592 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4593
4594 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4595
Francois Romieu2e68ae42008-06-28 12:00:55 +02004596 rtl_tx_performance_tweak(pdev,
4597 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004598}
4599
4600static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4601{
4602 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004603
françois romieuf0298f82011-01-03 15:07:42 +00004604 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004605
4606 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004607}
4608
4609static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4610{
Francois Romieub726e492008-06-28 12:22:59 +02004611 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4612
4613 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4614
Francois Romieu219a1e92008-06-28 11:58:39 +02004615 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004616
4617 rtl_disable_clock_request(pdev);
4618
4619 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004620}
4621
Francois Romieuef3386f2008-06-29 12:24:30 +02004622static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004623{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004624 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004625 { 0x01, 0, 0x0001 },
4626 { 0x02, 0x0800, 0x1000 },
4627 { 0x03, 0, 0x0042 },
4628 { 0x06, 0x0080, 0x0000 },
4629 { 0x07, 0, 0x2000 }
4630 };
4631
françois romieu650e8d52011-01-03 15:08:29 +00004632 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004633
4634 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4635
Francois Romieu219a1e92008-06-28 11:58:39 +02004636 __rtl_hw_start_8168cp(ioaddr, pdev);
4637}
4638
Francois Romieuef3386f2008-06-29 12:24:30 +02004639static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4640{
françois romieu650e8d52011-01-03 15:08:29 +00004641 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004642
4643 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4644
4645 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4646
4647 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4648}
4649
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004650static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4651{
françois romieu650e8d52011-01-03 15:08:29 +00004652 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004653
4654 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4655
4656 /* Magic. */
4657 RTL_W8(DBG_REG, 0x20);
4658
françois romieuf0298f82011-01-03 15:07:42 +00004659 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004660
4661 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4662
4663 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4664}
4665
Francois Romieu219a1e92008-06-28 11:58:39 +02004666static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4667{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004668 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004669 { 0x02, 0x0800, 0x1000 },
4670 { 0x03, 0, 0x0002 },
4671 { 0x06, 0x0080, 0x0000 }
4672 };
4673
françois romieu650e8d52011-01-03 15:08:29 +00004674 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004675
4676 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4677
4678 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4679
Francois Romieu219a1e92008-06-28 11:58:39 +02004680 __rtl_hw_start_8168cp(ioaddr, pdev);
4681}
4682
4683static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4684{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004685 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004686 { 0x01, 0, 0x0001 },
4687 { 0x03, 0x0400, 0x0220 }
4688 };
4689
françois romieu650e8d52011-01-03 15:08:29 +00004690 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004691
4692 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4693
Francois Romieu219a1e92008-06-28 11:58:39 +02004694 __rtl_hw_start_8168cp(ioaddr, pdev);
4695}
4696
Francois Romieu197ff762008-06-28 13:16:02 +02004697static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4698{
4699 rtl_hw_start_8168c_2(ioaddr, pdev);
4700}
4701
Francois Romieu6fb07052008-06-29 11:54:28 +02004702static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4703{
françois romieu650e8d52011-01-03 15:08:29 +00004704 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004705
4706 __rtl_hw_start_8168cp(ioaddr, pdev);
4707}
4708
Francois Romieu5b538df2008-07-20 16:22:45 +02004709static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4710{
françois romieu650e8d52011-01-03 15:08:29 +00004711 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004712
4713 rtl_disable_clock_request(pdev);
4714
françois romieuf0298f82011-01-03 15:07:42 +00004715 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004716
4717 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4718
4719 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4720}
4721
hayeswang4804b3b2011-03-21 01:50:29 +00004722static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4723{
4724 rtl_csi_access_enable_1(ioaddr);
4725
4726 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4727
4728 RTL_W8(MaxTxPacketSize, TxPacketMax);
4729
4730 rtl_disable_clock_request(pdev);
4731}
4732
françois romieue6de30d2011-01-03 15:08:37 +00004733static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4734{
4735 static const struct ephy_info e_info_8168d_4[] = {
4736 { 0x0b, ~0, 0x48 },
4737 { 0x19, 0x20, 0x50 },
4738 { 0x0c, ~0, 0x20 }
4739 };
4740 int i;
4741
4742 rtl_csi_access_enable_1(ioaddr);
4743
4744 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4745
4746 RTL_W8(MaxTxPacketSize, TxPacketMax);
4747
4748 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4749 const struct ephy_info *e = e_info_8168d_4 + i;
4750 u16 w;
4751
4752 w = rtl_ephy_read(ioaddr, e->offset);
4753 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4754 }
4755
4756 rtl_enable_clock_request(pdev);
4757}
4758
Hayes Wang70090422011-07-06 15:58:06 +08004759static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004760{
Hayes Wang70090422011-07-06 15:58:06 +08004761 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004762 { 0x00, 0x0200, 0x0100 },
4763 { 0x00, 0x0000, 0x0004 },
4764 { 0x06, 0x0002, 0x0001 },
4765 { 0x06, 0x0000, 0x0030 },
4766 { 0x07, 0x0000, 0x2000 },
4767 { 0x00, 0x0000, 0x0020 },
4768 { 0x03, 0x5800, 0x2000 },
4769 { 0x03, 0x0000, 0x0001 },
4770 { 0x01, 0x0800, 0x1000 },
4771 { 0x07, 0x0000, 0x4000 },
4772 { 0x1e, 0x0000, 0x2000 },
4773 { 0x19, 0xffff, 0xfe6c },
4774 { 0x0a, 0x0000, 0x0040 }
4775 };
4776
4777 rtl_csi_access_enable_2(ioaddr);
4778
Hayes Wang70090422011-07-06 15:58:06 +08004779 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004780
4781 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4782
4783 RTL_W8(MaxTxPacketSize, TxPacketMax);
4784
4785 rtl_disable_clock_request(pdev);
4786
4787 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004788 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4789 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004790
Francois Romieucecb5fd2011-04-01 10:21:07 +02004791 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004792}
4793
Hayes Wang70090422011-07-06 15:58:06 +08004794static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4795{
4796 static const struct ephy_info e_info_8168e_2[] = {
4797 { 0x09, 0x0000, 0x0080 },
4798 { 0x19, 0x0000, 0x0224 }
4799 };
4800
4801 rtl_csi_access_enable_1(ioaddr);
4802
4803 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4804
4805 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4806
4807 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4808 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4809 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4810 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4811 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4812 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4813 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4814 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4815 ERIAR_EXGMAC);
4816
Hayes Wang3090bd92011-09-06 16:55:15 +08004817 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004818
4819 rtl_disable_clock_request(pdev);
4820
4821 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4822 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4823
4824 /* Adjust EEE LED frequency */
4825 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4826
4827 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4828 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4829 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4830}
4831
Hayes Wangc2218922011-09-06 16:55:18 +08004832static void rtl_hw_start_8168f_1(void __iomem *ioaddr, struct pci_dev *pdev)
4833{
4834 static const struct ephy_info e_info_8168f_1[] = {
4835 { 0x06, 0x00c0, 0x0020 },
4836 { 0x08, 0x0001, 0x0002 },
4837 { 0x09, 0x0000, 0x0080 },
4838 { 0x19, 0x0000, 0x0224 }
4839 };
4840
4841 rtl_csi_access_enable_1(ioaddr);
4842
4843 rtl_ephy_init(ioaddr, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
4844
4845 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4846
4847 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4848 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4849 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4850 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4851 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4852 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4853 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4854 rtl_w1w0_eri(ioaddr, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4855 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4856 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
4857 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4858 ERIAR_EXGMAC);
4859
4860 RTL_W8(MaxTxPacketSize, EarlySize);
4861
4862 rtl_disable_clock_request(pdev);
4863
4864 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4865 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4866
4867 /* Adjust EEE LED frequency */
4868 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4869
4870 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4871 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4872 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4873}
4874
Francois Romieu07ce4062007-02-23 23:36:39 +01004875static void rtl_hw_start_8168(struct net_device *dev)
4876{
Francois Romieu2dd99532007-06-11 23:22:52 +02004877 struct rtl8169_private *tp = netdev_priv(dev);
4878 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004879 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004880
4881 RTL_W8(Cfg9346, Cfg9346_Unlock);
4882
françois romieuf0298f82011-01-03 15:07:42 +00004883 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004884
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004885 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004886
Francois Romieu0e485152007-02-20 00:00:26 +01004887 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004888
4889 RTL_W16(CPlusCmd, tp->cp_cmd);
4890
Francois Romieu0e485152007-02-20 00:00:26 +01004891 RTL_W16(IntrMitigate, 0x5151);
4892
4893 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00004894 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieu0e485152007-02-20 00:00:26 +01004895 tp->intr_event |= RxFIFOOver | PCSTimeout;
4896 tp->intr_event &= ~RxOverflow;
4897 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004898
4899 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4900
Francois Romieub8363902008-06-01 12:31:57 +02004901 rtl_set_rx_mode(dev);
4902
4903 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4904 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004905
4906 RTL_R8(IntrMask);
4907
Francois Romieu219a1e92008-06-28 11:58:39 +02004908 switch (tp->mac_version) {
4909 case RTL_GIGA_MAC_VER_11:
4910 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004911 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004912
4913 case RTL_GIGA_MAC_VER_12:
4914 case RTL_GIGA_MAC_VER_17:
4915 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004916 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004917
4918 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004919 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004920 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004921
4922 case RTL_GIGA_MAC_VER_19:
4923 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004924 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004925
4926 case RTL_GIGA_MAC_VER_20:
4927 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004928 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004929
Francois Romieu197ff762008-06-28 13:16:02 +02004930 case RTL_GIGA_MAC_VER_21:
4931 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004932 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004933
Francois Romieu6fb07052008-06-29 11:54:28 +02004934 case RTL_GIGA_MAC_VER_22:
4935 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004936 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004937
Francois Romieuef3386f2008-06-29 12:24:30 +02004938 case RTL_GIGA_MAC_VER_23:
4939 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004940 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004941
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004942 case RTL_GIGA_MAC_VER_24:
4943 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004944 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004945
Francois Romieu5b538df2008-07-20 16:22:45 +02004946 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004947 case RTL_GIGA_MAC_VER_26:
4948 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004949 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004950 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004951
françois romieue6de30d2011-01-03 15:08:37 +00004952 case RTL_GIGA_MAC_VER_28:
4953 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004954 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004955
hayeswang4804b3b2011-03-21 01:50:29 +00004956 case RTL_GIGA_MAC_VER_31:
4957 rtl_hw_start_8168dp(ioaddr, pdev);
4958 break;
4959
hayeswang01dc7fe2011-03-21 01:50:28 +00004960 case RTL_GIGA_MAC_VER_32:
4961 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004962 rtl_hw_start_8168e_1(ioaddr, pdev);
4963 break;
4964 case RTL_GIGA_MAC_VER_34:
4965 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004966 break;
françois romieue6de30d2011-01-03 15:08:37 +00004967
Hayes Wangc2218922011-09-06 16:55:18 +08004968 case RTL_GIGA_MAC_VER_35:
4969 case RTL_GIGA_MAC_VER_36:
4970 rtl_hw_start_8168f_1(ioaddr, pdev);
4971 break;
4972
Francois Romieu219a1e92008-06-28 11:58:39 +02004973 default:
4974 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4975 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004976 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004977 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004978
Francois Romieu0e485152007-02-20 00:00:26 +01004979 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4980
Francois Romieub8363902008-06-01 12:31:57 +02004981 RTL_W8(Cfg9346, Cfg9346_Lock);
4982
Francois Romieu2dd99532007-06-11 23:22:52 +02004983 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004984
Francois Romieu0e485152007-02-20 00:00:26 +01004985 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004986}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Francois Romieu2857ffb2008-08-02 21:08:49 +02004988#define R810X_CPCMD_QUIRK_MASK (\
4989 EnableBist | \
4990 Mac_dbgo_oe | \
4991 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004992 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004993 Force_txflow_en | \
4994 Cxpl_dbg_sel | \
4995 ASF | \
4996 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004997 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004998
4999static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5000{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005001 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005002 { 0x01, 0, 0x6e65 },
5003 { 0x02, 0, 0x091f },
5004 { 0x03, 0, 0xc2f9 },
5005 { 0x06, 0, 0xafb5 },
5006 { 0x07, 0, 0x0e00 },
5007 { 0x19, 0, 0xec80 },
5008 { 0x01, 0, 0x2e65 },
5009 { 0x01, 0, 0x6e65 }
5010 };
5011 u8 cfg1;
5012
françois romieu650e8d52011-01-03 15:08:29 +00005013 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005014
5015 RTL_W8(DBG_REG, FIX_NAK_1);
5016
5017 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5018
5019 RTL_W8(Config1,
5020 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5021 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5022
5023 cfg1 = RTL_R8(Config1);
5024 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5025 RTL_W8(Config1, cfg1 & ~LEDS0);
5026
Francois Romieu2857ffb2008-08-02 21:08:49 +02005027 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5028}
5029
5030static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5031{
françois romieu650e8d52011-01-03 15:08:29 +00005032 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005033
5034 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5035
5036 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
5037 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005038}
5039
5040static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
5041{
5042 rtl_hw_start_8102e_2(ioaddr, pdev);
5043
5044 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
5045}
5046
Hayes Wang5a5e4442011-02-22 17:26:21 +08005047static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
5048{
5049 static const struct ephy_info e_info_8105e_1[] = {
5050 { 0x07, 0, 0x4000 },
5051 { 0x19, 0, 0x0200 },
5052 { 0x19, 0, 0x0020 },
5053 { 0x1e, 0, 0x2000 },
5054 { 0x03, 0, 0x0001 },
5055 { 0x19, 0, 0x0100 },
5056 { 0x19, 0, 0x0004 },
5057 { 0x0a, 0, 0x0020 }
5058 };
5059
Francois Romieucecb5fd2011-04-01 10:21:07 +02005060 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005061 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
5062
Francois Romieucecb5fd2011-04-01 10:21:07 +02005063 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08005064 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
5065
5066 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08005067 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005068
5069 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5070}
5071
5072static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
5073{
5074 rtl_hw_start_8105e_1(ioaddr, pdev);
5075 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
5076}
5077
Francois Romieu07ce4062007-02-23 23:36:39 +01005078static void rtl_hw_start_8101(struct net_device *dev)
5079{
Francois Romieucdf1a602007-06-11 23:29:50 +02005080 struct rtl8169_private *tp = netdev_priv(dev);
5081 void __iomem *ioaddr = tp->mmio_addr;
5082 struct pci_dev *pdev = tp->pci_dev;
5083
françois romieu811fd302011-12-04 20:30:45 +00005084 if (tp->mac_version >= RTL_GIGA_MAC_VER_30) {
5085 tp->intr_event &= ~RxFIFOOver;
5086 tp->napi_event &= ~RxFIFOOver;
5087 }
5088
Francois Romieucecb5fd2011-04-01 10:21:07 +02005089 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5090 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00005091 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02005092
5093 if (cap) {
5094 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
5095 PCI_EXP_DEVCTL_NOSNOOP_EN);
5096 }
Francois Romieucdf1a602007-06-11 23:29:50 +02005097 }
5098
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005099 RTL_W8(Cfg9346, Cfg9346_Unlock);
5100
Francois Romieu2857ffb2008-08-02 21:08:49 +02005101 switch (tp->mac_version) {
5102 case RTL_GIGA_MAC_VER_07:
5103 rtl_hw_start_8102e_1(ioaddr, pdev);
5104 break;
5105
5106 case RTL_GIGA_MAC_VER_08:
5107 rtl_hw_start_8102e_3(ioaddr, pdev);
5108 break;
5109
5110 case RTL_GIGA_MAC_VER_09:
5111 rtl_hw_start_8102e_2(ioaddr, pdev);
5112 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005113
5114 case RTL_GIGA_MAC_VER_29:
5115 rtl_hw_start_8105e_1(ioaddr, pdev);
5116 break;
5117 case RTL_GIGA_MAC_VER_30:
5118 rtl_hw_start_8105e_2(ioaddr, pdev);
5119 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005120 }
5121
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005122 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02005123
françois romieuf0298f82011-01-03 15:07:42 +00005124 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02005125
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005126 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02005127
Hayes Wangd24e9aa2011-02-22 17:26:19 +08005128 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02005129 RTL_W16(CPlusCmd, tp->cp_cmd);
5130
5131 RTL_W16(IntrMitigate, 0x0000);
5132
5133 rtl_set_rx_tx_desc_registers(tp, ioaddr);
5134
5135 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5136 rtl_set_rx_tx_config_registers(tp);
5137
Francois Romieucdf1a602007-06-11 23:29:50 +02005138 RTL_R8(IntrMask);
5139
Francois Romieucdf1a602007-06-11 23:29:50 +02005140 rtl_set_rx_mode(dev);
5141
5142 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01005143
Francois Romieu0e485152007-02-20 00:00:26 +01005144 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
5146
5147static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5148{
Francois Romieud58d46b2011-05-03 16:38:29 +02005149 struct rtl8169_private *tp = netdev_priv(dev);
5150
5151 if (new_mtu < ETH_ZLEN ||
5152 new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 return -EINVAL;
5154
Francois Romieud58d46b2011-05-03 16:38:29 +02005155 if (new_mtu > ETH_DATA_LEN)
5156 rtl_hw_jumbo_enable(tp);
5157 else
5158 rtl_hw_jumbo_disable(tp);
5159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005161 netdev_update_features(dev);
5162
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164}
5165
5166static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5167{
Al Viro95e09182007-12-22 18:55:39 +00005168 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5170}
5171
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005172static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5173 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005175 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005176 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005177
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005178 kfree(*data_buff);
5179 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 rtl8169_make_unusable_by_asic(desc);
5181}
5182
5183static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
5184{
5185 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5186
5187 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
5188}
5189
5190static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
5191 u32 rx_buf_sz)
5192{
5193 desc->addr = cpu_to_le64(mapping);
5194 wmb();
5195 rtl8169_mark_to_asic(desc, rx_buf_sz);
5196}
5197
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005198static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005200 return (void *)ALIGN((long)data, 16);
5201}
5202
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005203static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5204 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005205{
5206 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005208 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005209 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005210 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005212 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
5213 if (!data)
5214 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005215
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005216 if (rtl8169_align(data) != data) {
5217 kfree(data);
5218 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
5219 if (!data)
5220 return NULL;
5221 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005222
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005223 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005224 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005225 if (unlikely(dma_mapping_error(d, mapping))) {
5226 if (net_ratelimit())
5227 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005228 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005232 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005233
5234err_out:
5235 kfree(data);
5236 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237}
5238
5239static void rtl8169_rx_clear(struct rtl8169_private *tp)
5240{
Francois Romieu07d3f512007-02-21 22:40:46 +01005241 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
5243 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005244 if (tp->Rx_databuff[i]) {
5245 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 tp->RxDescArray + i);
5247 }
5248 }
5249}
5250
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005251static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005253 desc->opts1 |= cpu_to_le32(RingEnd);
5254}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005255
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005256static int rtl8169_rx_fill(struct rtl8169_private *tp)
5257{
5258 unsigned int i;
5259
5260 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005261 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005262
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005263 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02005265
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005266 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005267 if (!data) {
5268 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005269 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005270 }
5271 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005274 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5275 return 0;
5276
5277err_out:
5278 rtl8169_rx_clear(tp);
5279 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282static int rtl8169_init_ring(struct net_device *dev)
5283{
5284 struct rtl8169_private *tp = netdev_priv(dev);
5285
5286 rtl8169_init_ring_indexes(tp);
5287
5288 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005289 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005291 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292}
5293
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005294static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 struct TxDesc *desc)
5296{
5297 unsigned int len = tx_skb->len;
5298
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005299 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 desc->opts1 = 0x00;
5302 desc->opts2 = 0x00;
5303 desc->addr = 0x00;
5304 tx_skb->len = 0;
5305}
5306
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005307static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5308 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
5310 unsigned int i;
5311
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005312 for (i = 0; i < n; i++) {
5313 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 struct ring_info *tx_skb = tp->tx_skb + entry;
5315 unsigned int len = tx_skb->len;
5316
5317 if (len) {
5318 struct sk_buff *skb = tx_skb->skb;
5319
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005320 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 tp->TxDescArray + entry);
5322 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005323 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 dev_kfree_skb(skb);
5325 tx_skb->skb = NULL;
5326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 }
5328 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005329}
5330
5331static void rtl8169_tx_clear(struct rtl8169_private *tp)
5332{
5333 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 tp->cur_tx = tp->dirty_tx = 0;
5335}
5336
Francois Romieu4422bcd2012-01-26 11:23:32 +01005337static void rtl8169_schedule_work(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
5339 struct rtl8169_private *tp = netdev_priv(dev);
5340
Francois Romieu4422bcd2012-01-26 11:23:32 +01005341 schedule_work(&tp->wk.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
5344static void rtl8169_wait_for_quiescence(struct net_device *dev)
5345{
5346 struct rtl8169_private *tp = netdev_priv(dev);
5347 void __iomem *ioaddr = tp->mmio_addr;
5348
5349 synchronize_irq(dev->irq);
5350
5351 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005352 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
françois romieu811fd302011-12-04 20:30:45 +00005354 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
David S. Millerd1d08d12008-01-07 20:53:33 -08005356 tp->intr_mask = 0xffff;
5357 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005358 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
Francois Romieu4422bcd2012-01-26 11:23:32 +01005361static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
David Howellsc4028952006-11-22 14:57:56 +00005363 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005364 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Francois Romieueb2a0212007-02-15 23:37:21 +01005366 rtnl_lock();
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005369 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
françois romieuc7c2c392011-12-04 20:30:52 +00005371 rtl8169_hw_reset(tp);
5372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 rtl8169_wait_for_quiescence(dev);
5374
Francois Romieu56de4142011-03-15 17:29:31 +01005375 for (i = 0; i < NUM_RX_DESC; i++)
5376 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005379 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Francois Romieu56de4142011-03-15 17:29:31 +01005381 rtl_hw_start(dev);
5382 netif_wake_queue(dev);
5383 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005384
5385out_unlock:
5386 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387}
5388
5389static void rtl8169_tx_timeout(struct net_device *dev)
5390{
Francois Romieu4422bcd2012-01-26 11:23:32 +01005391 rtl8169_schedule_work(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392}
5393
5394static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005395 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
5397 struct skb_shared_info *info = skb_shinfo(skb);
5398 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005399 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005400 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
5402 entry = tp->cur_tx;
5403 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005404 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 dma_addr_t mapping;
5406 u32 status, len;
5407 void *addr;
5408
5409 entry = (entry + 1) % NUM_TX_DESC;
5410
5411 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005412 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005413 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005414 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005415 if (unlikely(dma_mapping_error(d, mapping))) {
5416 if (net_ratelimit())
5417 netif_err(tp, drv, tp->dev,
5418 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005419 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
Francois Romieucecb5fd2011-04-01 10:21:07 +02005422 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005423 status = opts[0] | len |
5424 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005427 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 txd->addr = cpu_to_le64(mapping);
5429
5430 tp->tx_skb[entry].len = len;
5431 }
5432
5433 if (cur_frag) {
5434 tp->tx_skb[entry].skb = skb;
5435 txd->opts1 |= cpu_to_le32(LastFrag);
5436 }
5437
5438 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005439
5440err_out:
5441 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5442 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443}
5444
Francois Romieu2b7b4312011-04-18 22:53:24 -07005445static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5446 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005448 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005449 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005450 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Francois Romieu2b7b4312011-04-18 22:53:24 -07005452 if (mss) {
5453 opts[0] |= TD_LSO;
5454 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5455 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005456 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
5458 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005459 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005461 opts[offset] |= info->checksum.udp;
5462 else
5463 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465}
5466
Stephen Hemminger613573252009-08-31 19:50:58 +00005467static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5468 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469{
5470 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005471 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 struct TxDesc *txd = tp->TxDescArray + entry;
5473 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005474 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 dma_addr_t mapping;
5476 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005477 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005478 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005481 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005482 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
5484
5485 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005486 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005488 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005489 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005490 if (unlikely(dma_mapping_error(d, mapping))) {
5491 if (net_ratelimit())
5492 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005493 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
5496 tp->tx_skb[entry].len = len;
5497 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Francois Romieu2b7b4312011-04-18 22:53:24 -07005499 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5500 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005501
Francois Romieu2b7b4312011-04-18 22:53:24 -07005502 rtl8169_tso_csum(tp, skb, opts);
5503
5504 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005505 if (frags < 0)
5506 goto err_dma_1;
5507 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005508 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005509 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005510 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005511 tp->tx_skb[entry].skb = skb;
5512 }
5513
Francois Romieu2b7b4312011-04-18 22:53:24 -07005514 txd->opts2 = cpu_to_le32(opts[1]);
5515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 wmb();
5517
Francois Romieucecb5fd2011-04-01 10:21:07 +02005518 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005519 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 txd->opts1 = cpu_to_le32(status);
5521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 tp->cur_tx += frags + 1;
5523
David Dillow4c020a92010-03-03 16:33:10 +00005524 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Francois Romieucecb5fd2011-04-01 10:21:07 +02005526 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
5528 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5529 netif_stop_queue(dev);
5530 smp_rmb();
5531 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5532 netif_wake_queue(dev);
5533 }
5534
Stephen Hemminger613573252009-08-31 19:50:58 +00005535 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005537err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005538 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005539err_dma_0:
5540 dev_kfree_skb(skb);
5541 dev->stats.tx_dropped++;
5542 return NETDEV_TX_OK;
5543
5544err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005546 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005547 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548}
5549
5550static void rtl8169_pcierr_interrupt(struct net_device *dev)
5551{
5552 struct rtl8169_private *tp = netdev_priv(dev);
5553 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 u16 pci_status, pci_cmd;
5555
5556 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5557 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5558
Joe Perchesbf82c182010-02-09 11:49:50 +00005559 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5560 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
5562 /*
5563 * The recovery sequence below admits a very elaborated explanation:
5564 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005565 * - I did not see what else could be done;
5566 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 *
5568 * Feel free to adjust to your needs.
5569 */
Francois Romieua27993f2006-12-18 00:04:19 +01005570 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005571 pci_cmd &= ~PCI_COMMAND_PARITY;
5572 else
5573 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5574
5575 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 pci_write_config_word(pdev, PCI_STATUS,
5578 pci_status & (PCI_STATUS_DETECTED_PARITY |
5579 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5580 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5581
5582 /* The infamous DAC f*ckup only happens at boot time */
5583 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005584 void __iomem *ioaddr = tp->mmio_addr;
5585
Joe Perchesbf82c182010-02-09 11:49:50 +00005586 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 tp->cp_cmd &= ~PCIDAC;
5588 RTL_W16(CPlusCmd, tp->cp_cmd);
5589 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 }
5591
françois romieue6de30d2011-01-03 15:08:37 +00005592 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005593
Francois Romieu4422bcd2012-01-26 11:23:32 +01005594 rtl8169_schedule_work(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
Francois Romieu07d3f512007-02-21 22:40:46 +01005597static void rtl8169_tx_interrupt(struct net_device *dev,
5598 struct rtl8169_private *tp,
5599 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600{
5601 unsigned int dirty_tx, tx_left;
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 dirty_tx = tp->dirty_tx;
5604 smp_rmb();
5605 tx_left = tp->cur_tx - dirty_tx;
5606
5607 while (tx_left > 0) {
5608 unsigned int entry = dirty_tx % NUM_TX_DESC;
5609 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 u32 status;
5611
5612 rmb();
5613 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5614 if (status & DescOwn)
5615 break;
5616
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005617 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5618 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005620 dev->stats.tx_packets++;
5621 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005622 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 tx_skb->skb = NULL;
5624 }
5625 dirty_tx++;
5626 tx_left--;
5627 }
5628
5629 if (tp->dirty_tx != dirty_tx) {
5630 tp->dirty_tx = dirty_tx;
5631 smp_wmb();
5632 if (netif_queue_stopped(dev) &&
5633 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5634 netif_wake_queue(dev);
5635 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005636 /*
5637 * 8168 hack: TxPoll requests are lost when the Tx packets are
5638 * too close. Let's kick an extra TxPoll request when a burst
5639 * of start_xmit activity is detected (if it is not detected,
5640 * it is slow enough). -- FR
5641 */
5642 smp_rmb();
5643 if (tp->cur_tx != dirty_tx)
5644 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 }
5646}
5647
Francois Romieu126fa4b2005-05-12 20:09:17 -04005648static inline int rtl8169_fragmented_frame(u32 status)
5649{
5650 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5651}
5652
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005653static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 u32 status = opts1 & RxProtoMask;
5656
5657 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005658 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 skb->ip_summed = CHECKSUM_UNNECESSARY;
5660 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005661 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662}
5663
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005664static struct sk_buff *rtl8169_try_rx_copy(void *data,
5665 struct rtl8169_private *tp,
5666 int pkt_size,
5667 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005669 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005670 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005672 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005673 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005674 prefetch(data);
5675 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5676 if (skb)
5677 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005678 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5679
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005680 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681}
5682
Francois Romieu07d3f512007-02-21 22:40:46 +01005683static int rtl8169_rx_interrupt(struct net_device *dev,
5684 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005685 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005688 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 cur_rx = tp->cur_rx;
5691 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005692 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005694 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005696 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 u32 status;
5698
5699 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005700 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 if (status & DescOwn)
5703 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005704 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005705 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5706 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005707 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005709 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005711 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005712 if (status & RxFOVF) {
Francois Romieu4422bcd2012-01-26 11:23:32 +01005713 rtl8169_schedule_work(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005714 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005715 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005716 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005718 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005719 dma_addr_t addr = le64_to_cpu(desc->addr);
Francois Romieudeb9d932011-07-12 08:24:28 +02005720 int pkt_size = (status & 0x00003fff) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Francois Romieu126fa4b2005-05-12 20:09:17 -04005722 /*
5723 * The driver does not support incoming fragmented
5724 * frames. They are seen as a symptom of over-mtu
5725 * sized frames.
5726 */
5727 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005728 dev->stats.rx_dropped++;
5729 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005730 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005731 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005732 }
5733
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005734 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5735 tp, pkt_size, addr);
5736 rtl8169_mark_to_asic(desc, rx_buf_sz);
5737 if (!skb) {
5738 dev->stats.rx_dropped++;
5739 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 }
5741
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005742 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 skb_put(skb, pkt_size);
5744 skb->protocol = eth_type_trans(skb, dev);
5745
Francois Romieu7a8fc772011-03-01 17:18:33 +01005746 rtl8169_rx_vlan_tag(desc, skb);
5747
Francois Romieu56de4142011-03-15 17:29:31 +01005748 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
Francois Romieucebf8cc2007-10-18 12:06:54 +02005750 dev->stats.rx_bytes += pkt_size;
5751 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005753
5754 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005755 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005756 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5757 desc->opts2 = 0;
5758 cur_rx++;
5759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 }
5761
5762 count = cur_rx - tp->cur_rx;
5763 tp->cur_rx = cur_rx;
5764
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005765 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 return count;
5768}
5769
Francois Romieu07d3f512007-02-21 22:40:46 +01005770static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
Francois Romieu07d3f512007-02-21 22:40:46 +01005772 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005776 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
David Dillowf11a3772009-05-22 15:29:34 +00005778 /* loop handling interrupts until we have no new ones or
5779 * we hit a invalid/hotplug case.
5780 */
Francois Romieu865c6522008-05-11 14:51:00 +02005781 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005782 while (status && status != 0xffff) {
françois romieu811fd302011-12-04 20:30:45 +00005783 status &= tp->intr_event;
5784 if (!status)
5785 break;
5786
David Dillowf11a3772009-05-22 15:29:34 +00005787 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
David Dillowf11a3772009-05-22 15:29:34 +00005789 /* Handle all of the error cases first. These will reset
5790 * the chip, so just exit the loop.
5791 */
5792 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005793 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 }
David Dillowf11a3772009-05-22 15:29:34 +00005796
Francois Romieu1519e572011-02-03 12:02:36 +01005797 if (unlikely(status & RxFIFOOver)) {
5798 switch (tp->mac_version) {
5799 /* Work around for rx fifo overflow */
5800 case RTL_GIGA_MAC_VER_11:
Francois Romieu1519e572011-02-03 12:02:36 +01005801 netif_stop_queue(dev);
5802 rtl8169_tx_timeout(dev);
5803 goto done;
Francois Romieu1519e572011-02-03 12:02:36 +01005804 default:
5805 break;
5806 }
David Dillowf11a3772009-05-22 15:29:34 +00005807 }
5808
5809 if (unlikely(status & SYSErr)) {
5810 rtl8169_pcierr_interrupt(dev);
5811 break;
5812 }
5813
5814 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005815 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005816
5817 /* We need to see the lastest version of tp->intr_mask to
5818 * avoid ignoring an MSI interrupt and having to wait for
5819 * another event which may never come.
5820 */
5821 smp_rmb();
5822 if (status & tp->intr_mask & tp->napi_event) {
5823 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5824 tp->intr_mask = ~tp->napi_event;
5825
5826 if (likely(napi_schedule_prep(&tp->napi)))
5827 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005828 else
5829 netif_info(tp, intr, dev,
5830 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005831 }
5832
5833 /* We only get a new MSI interrupt when all active irq
5834 * sources on the chip have been acknowledged. So, ack
5835 * everything we've seen and check if new sources have become
5836 * active to avoid blocking all interrupts from the chip.
5837 */
5838 RTL_W16(IntrStatus,
5839 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5840 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 }
Francois Romieu1519e572011-02-03 12:02:36 +01005842done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 return IRQ_RETVAL(handled);
5844}
5845
Francois Romieu4422bcd2012-01-26 11:23:32 +01005846static void rtl_task(struct work_struct *work)
5847{
5848 struct rtl8169_private *tp =
5849 container_of(work, struct rtl8169_private, wk.work);
5850
5851 rtl_reset_work(tp);
5852}
5853
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005854static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005856 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5857 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005859 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005861 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 rtl8169_tx_interrupt(dev, tp, ioaddr);
5863
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005864 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005865 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005866
5867 /* We need for force the visibility of tp->intr_mask
5868 * for other CPUs, as we can loose an MSI interrupt
5869 * and potentially wait for a retransmit timeout if we don't.
5870 * The posted write to IntrMask is safe, as it will
5871 * eventually make it to the chip and we won't loose anything
5872 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 */
David Dillowf11a3772009-05-22 15:29:34 +00005874 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005875 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005876 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 }
5878
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005879 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Francois Romieu523a6092008-09-10 22:28:56 +02005882static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5883{
5884 struct rtl8169_private *tp = netdev_priv(dev);
5885
5886 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5887 return;
5888
5889 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5890 RTL_W32(RxMissed, 0);
5891}
5892
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893static void rtl8169_down(struct net_device *dev)
5894{
5895 struct rtl8169_private *tp = netdev_priv(dev);
5896 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
Francois Romieu4876cc12011-03-11 21:07:11 +01005898 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
5900 netif_stop_queue(dev);
5901
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005902 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005903
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 spin_lock_irq(&tp->lock);
5905
Hayes Wang92fc43b2011-07-06 15:58:03 +08005906 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005907 /*
5908 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01005909 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
5910 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005911 */
Francois Romieu523a6092008-09-10 22:28:56 +02005912 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
5914 spin_unlock_irq(&tp->lock);
5915
5916 synchronize_irq(dev->irq);
5917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005919 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 rtl8169_tx_clear(tp);
5922
5923 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005924
5925 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926}
5927
5928static int rtl8169_close(struct net_device *dev)
5929{
5930 struct rtl8169_private *tp = netdev_priv(dev);
5931 struct pci_dev *pdev = tp->pci_dev;
5932
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005933 pm_runtime_get_sync(&pdev->dev);
5934
Francois Romieucecb5fd2011-04-01 10:21:07 +02005935 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005936 rtl8169_update_counters(dev);
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 rtl8169_down(dev);
5939
5940 free_irq(dev->irq, dev);
5941
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005942 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5943 tp->RxPhyAddr);
5944 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5945 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 tp->TxDescArray = NULL;
5947 tp->RxDescArray = NULL;
5948
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005949 pm_runtime_put_sync(&pdev->dev);
5950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 return 0;
5952}
5953
Francois Romieu07ce4062007-02-23 23:36:39 +01005954static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955{
5956 struct rtl8169_private *tp = netdev_priv(dev);
5957 void __iomem *ioaddr = tp->mmio_addr;
5958 unsigned long flags;
5959 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005960 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 u32 tmp = 0;
5962
5963 if (dev->flags & IFF_PROMISC) {
5964 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005965 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 rx_mode =
5967 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5968 AcceptAllPhys;
5969 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005970 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005971 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 /* Too many to filter perfectly -- accept all multicasts. */
5973 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5974 mc_filter[1] = mc_filter[0] = 0xffffffff;
5975 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005976 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 rx_mode = AcceptBroadcast | AcceptMyPhys;
5979 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005980 netdev_for_each_mc_addr(ha, dev) {
5981 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5983 rx_mode |= AcceptMulticast;
5984 }
5985 }
5986
5987 spin_lock_irqsave(&tp->lock, flags);
5988
Francois Romieu1687b562011-07-19 17:21:29 +02005989 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Francois Romieuf887cce2008-07-17 22:24:18 +02005991 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005992 u32 data = mc_filter[0];
5993
5994 mc_filter[0] = swab32(mc_filter[1]);
5995 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005996 }
5997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005999 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
Francois Romieu57a9f232007-06-04 22:10:15 +02006001 RTL_W32(RxConfig, tmp);
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 spin_unlock_irqrestore(&tp->lock, flags);
6004}
6005
6006/**
6007 * rtl8169_get_stats - Get rtl8169 read/write statistics
6008 * @dev: The Ethernet Device to get statistics for
6009 *
6010 * Get TX/RX statistics for rtl8169
6011 */
6012static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
6013{
6014 struct rtl8169_private *tp = netdev_priv(dev);
6015 void __iomem *ioaddr = tp->mmio_addr;
6016 unsigned long flags;
6017
6018 if (netif_running(dev)) {
6019 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02006020 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 spin_unlock_irqrestore(&tp->lock, flags);
6022 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02006023
Francois Romieucebf8cc2007-10-18 12:06:54 +02006024 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025}
6026
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006027static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006028{
françois romieu065c27c2011-01-03 15:08:12 +00006029 struct rtl8169_private *tp = netdev_priv(dev);
6030
Francois Romieu5d06a992006-02-23 00:47:58 +01006031 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006032 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006033
françois romieu065c27c2011-01-03 15:08:12 +00006034 rtl_pll_power_down(tp);
6035
Francois Romieu5d06a992006-02-23 00:47:58 +01006036 netif_device_detach(dev);
6037 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006038}
Francois Romieu5d06a992006-02-23 00:47:58 +01006039
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006040#ifdef CONFIG_PM
6041
6042static int rtl8169_suspend(struct device *device)
6043{
6044 struct pci_dev *pdev = to_pci_dev(device);
6045 struct net_device *dev = pci_get_drvdata(pdev);
6046
6047 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006048
Francois Romieu5d06a992006-02-23 00:47:58 +01006049 return 0;
6050}
6051
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006052static void __rtl8169_resume(struct net_device *dev)
6053{
françois romieu065c27c2011-01-03 15:08:12 +00006054 struct rtl8169_private *tp = netdev_priv(dev);
6055
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006056 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006057
6058 rtl_pll_power_up(tp);
6059
Francois Romieu4422bcd2012-01-26 11:23:32 +01006060 rtl8169_schedule_work(dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006061}
6062
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006063static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006064{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006065 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006066 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006067 struct rtl8169_private *tp = netdev_priv(dev);
6068
6069 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01006070
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006071 if (netif_running(dev))
6072 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006073
Francois Romieu5d06a992006-02-23 00:47:58 +01006074 return 0;
6075}
6076
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006077static int rtl8169_runtime_suspend(struct device *device)
6078{
6079 struct pci_dev *pdev = to_pci_dev(device);
6080 struct net_device *dev = pci_get_drvdata(pdev);
6081 struct rtl8169_private *tp = netdev_priv(dev);
6082
6083 if (!tp->TxDescArray)
6084 return 0;
6085
6086 spin_lock_irq(&tp->lock);
6087 tp->saved_wolopts = __rtl8169_get_wol(tp);
6088 __rtl8169_set_wol(tp, WAKE_ANY);
6089 spin_unlock_irq(&tp->lock);
6090
6091 rtl8169_net_suspend(dev);
6092
6093 return 0;
6094}
6095
6096static int rtl8169_runtime_resume(struct device *device)
6097{
6098 struct pci_dev *pdev = to_pci_dev(device);
6099 struct net_device *dev = pci_get_drvdata(pdev);
6100 struct rtl8169_private *tp = netdev_priv(dev);
6101
6102 if (!tp->TxDescArray)
6103 return 0;
6104
6105 spin_lock_irq(&tp->lock);
6106 __rtl8169_set_wol(tp, tp->saved_wolopts);
6107 tp->saved_wolopts = 0;
6108 spin_unlock_irq(&tp->lock);
6109
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00006110 rtl8169_init_phy(dev, tp);
6111
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006112 __rtl8169_resume(dev);
6113
6114 return 0;
6115}
6116
6117static int rtl8169_runtime_idle(struct device *device)
6118{
6119 struct pci_dev *pdev = to_pci_dev(device);
6120 struct net_device *dev = pci_get_drvdata(pdev);
6121 struct rtl8169_private *tp = netdev_priv(dev);
6122
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00006123 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006124}
6125
Alexey Dobriyan47145212009-12-14 18:00:08 -08006126static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006127 .suspend = rtl8169_suspend,
6128 .resume = rtl8169_resume,
6129 .freeze = rtl8169_suspend,
6130 .thaw = rtl8169_resume,
6131 .poweroff = rtl8169_suspend,
6132 .restore = rtl8169_resume,
6133 .runtime_suspend = rtl8169_runtime_suspend,
6134 .runtime_resume = rtl8169_runtime_resume,
6135 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006136};
6137
6138#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6139
6140#else /* !CONFIG_PM */
6141
6142#define RTL8169_PM_OPS NULL
6143
6144#endif /* !CONFIG_PM */
6145
David S. Miller1805b2f2011-10-24 18:18:09 -04006146static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6147{
6148 void __iomem *ioaddr = tp->mmio_addr;
6149
6150 /* WoL fails with 8168b when the receiver is disabled. */
6151 switch (tp->mac_version) {
6152 case RTL_GIGA_MAC_VER_11:
6153 case RTL_GIGA_MAC_VER_12:
6154 case RTL_GIGA_MAC_VER_17:
6155 pci_clear_master(tp->pci_dev);
6156
6157 RTL_W8(ChipCmd, CmdRxEnb);
6158 /* PCI commit */
6159 RTL_R8(ChipCmd);
6160 break;
6161 default:
6162 break;
6163 }
6164}
6165
Francois Romieu1765f952008-09-13 17:21:40 +02006166static void rtl_shutdown(struct pci_dev *pdev)
6167{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006168 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006169 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006170
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006171 rtl8169_net_suspend(dev);
6172
Francois Romieucecb5fd2011-04-01 10:21:07 +02006173 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006174 rtl_rar_set(tp, dev->perm_addr);
6175
françois romieu4bb3f522009-06-17 11:41:45 +00006176 spin_lock_irq(&tp->lock);
6177
Hayes Wang92fc43b2011-07-06 15:58:03 +08006178 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006179
6180 spin_unlock_irq(&tp->lock);
6181
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006182 if (system_state == SYSTEM_POWER_OFF) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006183 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
6184 rtl_wol_suspend_quirk(tp);
6185 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006186 }
6187
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006188 pci_wake_from_d3(pdev, true);
6189 pci_set_power_state(pdev, PCI_D3hot);
6190 }
6191}
Francois Romieu5d06a992006-02-23 00:47:58 +01006192
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193static struct pci_driver rtl8169_pci_driver = {
6194 .name = MODULENAME,
6195 .id_table = rtl8169_pci_tbl,
6196 .probe = rtl8169_init_one,
6197 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02006198 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006199 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200};
6201
Francois Romieu07d3f512007-02-21 22:40:46 +01006202static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
Jeff Garzik29917622006-08-19 17:48:59 -04006204 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
Francois Romieu07d3f512007-02-21 22:40:46 +01006207static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
6209 pci_unregister_driver(&rtl8169_pci_driver);
6210}
6211
6212module_init(rtl8169_init_module);
6213module_exit(rtl8169_cleanup_module);